chiark / gitweb /
test-util: remove superfluous const
[elogind.git] / src / test / test-util.c
index 4bb51545be471eab0c5da73547de847029aace6a..9515a8cbf1374da17c814c544716b26ca7fc52d4 100644 (file)
@@ -603,7 +603,7 @@ static void test_get_process_comm(void) {
 
         r = get_process_environ(me, &env);
         assert_se(r >= 0 || r == -EACCES);
-        log_info("self strlen(environ): '%zd'", strlen(env));
+        log_info("self strlen(environ): '%zu'", strlen(env));
 
         if (!detect_container(NULL))
                 assert_se(get_ctty_devnr(1, &h) == -ENOENT);
@@ -918,15 +918,15 @@ static void test_files_same(void) {
 }
 
 static void test_is_valid_documentation_url(void) {
-        assert_se(is_valid_documentation_url("http://www.freedesktop.org/wiki/Software/systemd"));
-        assert_se(is_valid_documentation_url("https://www.kernel.org/doc/Documentation/binfmt_misc.txt"));
-        assert_se(is_valid_documentation_url("file:foo"));
-        assert_se(is_valid_documentation_url("man:systemd.special(7)"));
-        assert_se(is_valid_documentation_url("info:bar"));
+        assert_se(documentation_url_is_valid("http://www.freedesktop.org/wiki/Software/systemd"));
+        assert_se(documentation_url_is_valid("https://www.kernel.org/doc/Documentation/binfmt_misc.txt"));
+        assert_se(documentation_url_is_valid("file:/foo/foo"));
+        assert_se(documentation_url_is_valid("man:systemd.special(7)"));
+        assert_se(documentation_url_is_valid("info:bar"));
 
-        assert_se(!is_valid_documentation_url("foo:"));
-        assert_se(!is_valid_documentation_url("info:"));
-        assert_se(!is_valid_documentation_url(""));
+        assert_se(!documentation_url_is_valid("foo:"));
+        assert_se(!documentation_url_is_valid("info:"));
+        assert_se(!documentation_url_is_valid(""));
 }
 
 static void test_file_in_same_dir(void) {
@@ -1072,17 +1072,29 @@ static void test_strshorten(void) {
         assert_se(strlen(strshorten(s, 0)) == 0);
 }
 
-static void test_strappenda(void) {
+static void test_strjoina(void) {
         char *actual;
 
-        actual = strappenda("", "foo", "bar");
+        actual = strjoina("", "foo", "bar");
         assert_se(streq(actual, "foobar"));
 
-        actual = strappenda("foo", "bar", "baz");
+        actual = strjoina("foo", "bar", "baz");
         assert_se(streq(actual, "foobarbaz"));
 
-        actual = strappenda("foo", "", "bar", "baz");
+        actual = strjoina("foo", "", "bar", "baz");
         assert_se(streq(actual, "foobarbaz"));
+
+        actual = strjoina("foo");
+        assert_se(streq(actual, "foo"));
+
+        actual = strjoina(NULL);
+        assert_se(streq(actual, ""));
+
+        actual = strjoina(NULL, "foo");
+        assert_se(streq(actual, ""));
+
+        actual = strjoina("foo", NULL, "bar");
+        assert_se(streq(actual, "foo"));
 }
 
 static void test_is_symlink(void) {
@@ -1225,19 +1237,19 @@ static void test_glob_exists(void) {
 static void test_execute_directory(void) {
         char template_lo[] = "/tmp/test-readlink_and_make_absolute-lo.XXXXXXX";
         char template_hi[] = "/tmp/test-readlink_and_make_absolute-hi.XXXXXXX";
-        const char const* dirs[] = {template_hi, template_lo, NULL};
+        const char * dirs[] = {template_hi, template_lo, NULL};
         const char *name, *name2, *name3, *overridden, *override, *masked, *mask;
 
         assert_se(mkdtemp(template_lo));
         assert_se(mkdtemp(template_hi));
 
-        name = strappenda(template_lo, "/script");
-        name2 = strappenda(template_hi, "/script2");
-        name3 = strappenda(template_lo, "/useless");
-        overridden = strappenda(template_lo, "/overridden");
-        override = strappenda(template_hi, "/overridden");
-        masked = strappenda(template_lo, "/masked");
-        mask = strappenda(template_hi, "/masked");
+        name = strjoina(template_lo, "/script");
+        name2 = strjoina(template_hi, "/script2");
+        name3 = strjoina(template_lo, "/useless");
+        overridden = strjoina(template_lo, "/overridden");
+        override = strjoina(template_hi, "/overridden");
+        masked = strjoina(template_lo, "/masked");
+        mask = strjoina(template_hi, "/masked");
 
         assert_se(write_string_file(name, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works") == 0);
         assert_se(write_string_file(name2, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works2") == 0);
@@ -1254,11 +1266,11 @@ static void test_execute_directory(void) {
 
         execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL);
 
-        assert(chdir(template_lo) == 0);
+        assert_se(chdir(template_lo) == 0);
         assert_se(access("it_works", F_OK) >= 0);
         assert_se(access("failed", F_OK) < 0);
 
-        assert(chdir(template_hi) == 0);
+        assert_se(chdir(template_hi) == 0);
         assert_se(access("it_works2", F_OK) >= 0);
         assert_se(access("failed", F_OK) < 0);
 
@@ -1464,6 +1476,42 @@ static void test_uid_ptr(void) {
         assert_se(PTR_TO_UID(UID_TO_PTR(1000)) == 1000);
 }
 
+static void test_sparse_write_one(int fd, const char *buffer, size_t n) {
+        char check[n];
+
+        assert_se(lseek(fd, 0, SEEK_SET) == 0);
+        assert_se(ftruncate(fd, 0) >= 0);
+        assert_se(sparse_write(fd, buffer, n, 4) == (ssize_t) n);
+
+        assert_se(lseek(fd, 0, SEEK_CUR) == (off_t) n);
+        assert_se(ftruncate(fd, n) >= 0);
+
+        assert_se(lseek(fd, 0, SEEK_SET) == 0);
+        assert_se(read(fd, check, n) == (ssize_t) n);
+
+        assert_se(memcmp(buffer, check, n) == 0);
+}
+
+static void test_sparse_write(void) {
+        const char test_a[] = "test";
+        const char test_b[] = "\0\0\0\0test\0\0\0\0";
+        const char test_c[] = "\0\0test\0\0\0\0";
+        const char test_d[] = "\0\0test\0\0\0test\0\0\0\0test\0\0\0\0\0test\0\0\0test\0\0\0\0test\0\0\0\0\0\0\0\0";
+        const char test_e[] = "test\0\0\0\0test";
+        _cleanup_close_ int fd = -1;
+        char fn[] = "/tmp/sparseXXXXXX";
+
+        fd = mkostemp(fn, O_CLOEXEC);
+        assert_se(fd >= 0);
+        unlink(fn);
+
+        test_sparse_write_one(fd, test_a, sizeof(test_a));
+        test_sparse_write_one(fd, test_b, sizeof(test_b));
+        test_sparse_write_one(fd, test_c, sizeof(test_c));
+        test_sparse_write_one(fd, test_d, sizeof(test_d));
+        test_sparse_write_one(fd, test_e, sizeof(test_e));
+}
+
 int main(int argc, char *argv[]) {
         log_parse_environment();
         log_open();
@@ -1526,7 +1574,7 @@ int main(int argc, char *argv[]) {
         test_read_one_char();
         test_ignore_signals();
         test_strshorten();
-        test_strappenda();
+        test_strjoina();
         test_is_symlink();
         test_pid_is_unwaited();
         test_pid_is_alive();
@@ -1540,6 +1588,7 @@ int main(int argc, char *argv[]) {
         test_raw_clone();
         test_same_fd();
         test_uid_ptr();
+        test_sparse_write();
 
         return 0;
 }