chiark / gitweb /
Prep v236 : Add missing SPDX-License-Identifier (8/9) src/test
authorSven Eden <yamakuzure@gmx.net>
Tue, 13 Mar 2018 18:19:10 +0000 (19:19 +0100)
committerSven Eden <yamakuzure@gmx.net>
Mon, 26 Mar 2018 16:26:00 +0000 (18:26 +0200)
42 files changed:
src/test/test-alloc-util.c
src/test/test-cgroup.c
src/test/test-conf-files.c
src/test/test-conf-parser.c
src/test/test-copy.c
src/test/test-dlopen.c
src/test/test-ellipsize.c
src/test/test-escape.c
src/test/test-exec-util.c
src/test/test-extract-word.c
src/test/test-fd-util.c
src/test/test-fs-util.c
src/test/test-hash.c
src/test/test-hashmap-plain.c
src/test/test-hashmap.c
src/test/test-helper.h
src/test/test-hexdecoct.c
src/test/test-id128.c
src/test/test-io-util.c
src/test/test-ipcrm.c
src/test/test-list.c
src/test/test-locale-util.c
src/test/test-log.c
src/test/test-parse-util.c
src/test/test-path-util.c
src/test/test-prioq.c
src/test/test-proc-cmdline.c
src/test/test-process-util.c
src/test/test-random-util.c
src/test/test-selinux.c
src/test/test-set.c
src/test/test-signal-util.c
src/test/test-siphash24.c
src/test/test-sizeof.c
src/test/test-stat-util.c
src/test/test-string-util.c
src/test/test-strip-tab-ansi.c
src/test/test-unaligned.c
src/test/test-user-util.c
src/test/test-utf8.c
src/test/test-util.c
src/test/test-verbs.c

index cc4821eaf57d3bbedf5a22f24346517bcd819388..50ccf4700abbbb33cb501b3c05606951377fb864 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
@@ -17,6 +18,8 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+//#include <stdint.h>
+
 #include "alloc-util.h"
 #include "macro.h"
 #include "util.h"
@@ -34,22 +37,42 @@ static void test_alloca(void) {
         assert_se(!memcmp(t, zero, 997));
 }
 
-static void test_memdup_multiply(void) {
+static void test_memdup_multiply_and_greedy_realloc(void) {
         int org[] = {1, 2, 3};
-        int *dup;
-
-        dup = (int*)memdup_multiply(org, sizeof(int), 3);
+        _cleanup_free_ int *dup;
+        int *p;
+        size_t i, allocated = 3;
 
+        dup = (int*) memdup_suffix0_multiply(org, sizeof(int), 3);
         assert_se(dup);
         assert_se(dup[0] == 1);
         assert_se(dup[1] == 2);
         assert_se(dup[2] == 3);
+        assert_se(*(uint8_t*) (dup + 3) == (uint8_t) 0);
         free(dup);
+
+        dup = (int*) memdup_multiply(org, sizeof(int), 3);
+        assert_se(dup);
+        assert_se(dup[0] == 1);
+        assert_se(dup[1] == 2);
+        assert_se(dup[2] == 3);
+
+        p = dup;
+        assert_se(greedy_realloc0((void**) &dup, &allocated, 2, sizeof(int)) == p);
+
+        p = (int *) greedy_realloc0((void**) &dup, &allocated, 10, sizeof(int));
+        assert_se(p == dup);
+        assert_se(allocated >= 10);
+        assert_se(p[0] == 1);
+        assert_se(p[1] == 2);
+        assert_se(p[2] == 3);
+        for (i = 3; i < allocated; i++)
+                assert_se(p[i] == 0);
 }
 
 int main(int argc, char *argv[]) {
         test_alloca();
-        test_memdup_multiply();
+        test_memdup_multiply_and_greedy_realloc();
 
         return 0;
 }
index e56473a630fc9b66554d2b842ea390032d2de0d0..5b06b9b1ad4d6fa51e2301d8b3f9d51ba8b146b2 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
index 777b5ca32b0ec8d4ae61c4581e876e0c440e19f5..de91efac7218015837973c89722c50e7afe9ba8e 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
index d2567f67df414d4588231fdf3031106dc6a53d7c..62dd558f0e190226af8d8536e7a1b7b9e026f3f7 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
@@ -327,7 +328,7 @@ static void test_config_parse(unsigned i, const char *s) {
         r = config_parse(NULL, name, f,
                          "Section\0",
                          config_item_table_lookup, items,
-                         false, false, true, NULL);
+                         CONFIG_PARSE_WARN, NULL);
 
         switch (i) {
         case 0 ... 3:
index fc5a8c169f57feadd9b1f1037a2b8cc0e3cff8ef..4cc0706568c772f04f786f9c56917c939e739787 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd
 
index 9f5343a7ea5572155a249ed7a42935b68993ce1b..dd43da559f743a50db61b75707096483282bad5e 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
index d4f09b08a5879bd6a05110868827ecb2b8db348a..421774c07423a36c1676bd7aa0620ad4eeb38e63 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
index e7bc49dd96c2e05099cb629ff8e636100ebfb864..81156299278675c42234deb793610ccf30347b4c 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
index a8880e45c6fc91ffba430b5466570038049c9c77..02d745439e8409bb010ca37706ebc4d5ef6d439d 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
index 8b75ebdf5520352557d19f095ee9f64fc2db9507..c8f735607b6ec638b1170f4011c8c09a3dfd8c81 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
index d1d9e1d2f042db5230c2625a5c5159bc44482cc2..6b611b4b9cf6be028035dbdeb6e6d099be720f14 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
@@ -24,6 +25,9 @@
 #include "fd-util.h"
 #include "fileio.h"
 #include "macro.h"
+#include "random-util.h"
+#include "string-util.h"
+#include "util.h"
 
 static void test_close_many(void) {
         int fds[3];
@@ -102,7 +106,55 @@ static void test_open_serialization_fd(void) {
         fd = open_serialization_fd("test");
         assert_se(fd >= 0);
 
-        write(fd, "test\n", 5);
+        assert_se(write(fd, "test\n", 5) == 5);
+}
+
+static void test_acquire_data_fd_one(unsigned flags) {
+        char wbuffer[196*1024 - 7];
+        char rbuffer[sizeof(wbuffer)];
+        int fd;
+
+        fd = acquire_data_fd("foo", 3, flags);
+        assert_se(fd >= 0);
+
+        zero(rbuffer);
+        assert_se(read(fd, rbuffer, sizeof(rbuffer)) == 3);
+        assert_se(streq(rbuffer, "foo"));
+
+        fd = safe_close(fd);
+
+        fd = acquire_data_fd("", 0, flags);
+        assert_se(fd >= 0);
+
+        zero(rbuffer);
+        assert_se(read(fd, rbuffer, sizeof(rbuffer)) == 0);
+        assert_se(streq(rbuffer, ""));
+
+        fd = safe_close(fd);
+
+        random_bytes(wbuffer, sizeof(wbuffer));
+
+        fd = acquire_data_fd(wbuffer, sizeof(wbuffer), flags);
+        assert_se(fd >= 0);
+
+        zero(rbuffer);
+        assert_se(read(fd, rbuffer, sizeof(rbuffer)) == sizeof(rbuffer));
+        assert_se(memcmp(rbuffer, wbuffer, sizeof(rbuffer)) == 0);
+
+        fd = safe_close(fd);
+}
+
+static void test_acquire_data_fd(void) {
+
+        test_acquire_data_fd_one(0);
+        test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL);
+        test_acquire_data_fd_one(ACQUIRE_NO_MEMFD);
+        test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD);
+        test_acquire_data_fd_one(ACQUIRE_NO_PIPE);
+        test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_PIPE);
+        test_acquire_data_fd_one(ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE);
+        test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE);
+        test_acquire_data_fd_one(ACQUIRE_NO_DEV_NULL|ACQUIRE_NO_MEMFD|ACQUIRE_NO_PIPE|ACQUIRE_NO_TMPFILE);
 }
 
 int main(int argc, char *argv[]) {
@@ -112,6 +164,7 @@ int main(int argc, char *argv[]) {
         test_same_fd();
 #endif // 0
         test_open_serialization_fd();
+        test_acquire_data_fd();
 
         return 0;
 }
index 467a7cb9dd788578d967249cb01256d77e60583f..0edb259f83a599cf3a2509d3e14b7628f93a7c10 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
@@ -34,7 +35,7 @@
 static void test_chase_symlinks(void) {
         _cleanup_free_ char *result = NULL;
         char temp[] = "/tmp/test-chase.XXXXXX";
-        const char *top, *p, *q;
+        const char *top, *p, *pslash, *q, *qslash;
         int r;
 
         assert_se(mkdtemp(temp));
@@ -65,93 +66,134 @@ static void test_chase_symlinks(void) {
         r = chase_symlinks(p, NULL, 0, &result);
         assert_se(r > 0);
         assert_se(path_equal(result, "/usr"));
+        result = mfree(result);
 
+        pslash = strjoina(p, "/");
+        r = chase_symlinks(pslash, NULL, 0, &result);
+        assert_se(r > 0);
+        assert_se(path_equal(result, "/usr/"));
         result = mfree(result);
+
         r = chase_symlinks(p, temp, 0, &result);
         assert_se(r == -ENOENT);
 
+        r = chase_symlinks(pslash, temp, 0, &result);
+        assert_se(r == -ENOENT);
+
         q = strjoina(temp, "/usr");
 
         r = chase_symlinks(p, temp, CHASE_NONEXISTENT, &result);
         assert_se(r == 0);
         assert_se(path_equal(result, q));
+        result = mfree(result);
 
-        assert_se(mkdir(q, 0700) >= 0);
+        qslash = strjoina(q, "/");
 
+        r = chase_symlinks(pslash, temp, CHASE_NONEXISTENT, &result);
+        assert_se(r == 0);
+        assert_se(path_equal(result, qslash));
         result = mfree(result);
+
+        assert_se(mkdir(q, 0700) >= 0);
+
         r = chase_symlinks(p, temp, 0, &result);
         assert_se(r > 0);
         assert_se(path_equal(result, q));
+        result = mfree(result);
+
+        r = chase_symlinks(pslash, temp, 0, &result);
+        assert_se(r > 0);
+        assert_se(path_equal(result, qslash));
+        result = mfree(result);
 
         p = strjoina(temp, "/slash");
         assert_se(symlink("/", p) >= 0);
 
-        result = mfree(result);
         r = chase_symlinks(p, NULL, 0, &result);
         assert_se(r > 0);
         assert_se(path_equal(result, "/"));
-
         result = mfree(result);
+
         r = chase_symlinks(p, temp, 0, &result);
         assert_se(r > 0);
         assert_se(path_equal(result, temp));
+        result = mfree(result);
 
         /* Paths that would "escape" outside of the "root" */
 
         p = strjoina(temp, "/6dots");
         assert_se(symlink("../../..", p) >= 0);
 
-        result = mfree(result);
         r = chase_symlinks(p, temp, 0, &result);
         assert_se(r > 0 && path_equal(result, temp));
+        result = mfree(result);
 
         p = strjoina(temp, "/6dotsusr");
         assert_se(symlink("../../../usr", p) >= 0);
 
-        result = mfree(result);
         r = chase_symlinks(p, temp, 0, &result);
         assert_se(r > 0 && path_equal(result, q));
+        result = mfree(result);
 
         p = strjoina(temp, "/top/8dotsusr");
         assert_se(symlink("../../../../usr", p) >= 0);
 
-        result = mfree(result);
         r = chase_symlinks(p, temp, 0, &result);
         assert_se(r > 0 && path_equal(result, q));
+        result = mfree(result);
 
         /* Paths that contain repeated slashes */
 
         p = strjoina(temp, "/slashslash");
         assert_se(symlink("///usr///", p) >= 0);
 
-        result = mfree(result);
         r = chase_symlinks(p, NULL, 0, &result);
         assert_se(r > 0);
         assert_se(path_equal(result, "/usr"));
-
         result = mfree(result);
+
         r = chase_symlinks(p, temp, 0, &result);
         assert_se(r > 0);
         assert_se(path_equal(result, q));
+        result = mfree(result);
 
         /* Paths using . */
 
-        result = mfree(result);
         r = chase_symlinks("/etc/./.././", NULL, 0, &result);
         assert_se(r > 0);
         assert_se(path_equal(result, "/"));
-
         result = mfree(result);
+
         r = chase_symlinks("/etc/./.././", "/etc", 0, &result);
         assert_se(r > 0 && path_equal(result, "/etc"));
+        result = mfree(result);
+
+        r = chase_symlinks("/../.././//../../etc", NULL, 0, &result);
+        assert_se(r > 0);
+        assert_se(streq(result, "/etc"));
+        result = mfree(result);
 
+        r = chase_symlinks("/../.././//../../test-chase.fsldajfl", NULL, CHASE_NONEXISTENT, &result);
+        assert_se(r == 0);
+        assert_se(streq(result, "/test-chase.fsldajfl"));
         result = mfree(result);
+
+        r = chase_symlinks("/../.././//../../etc", "/", CHASE_PREFIX_ROOT, &result);
+        assert_se(r > 0);
+        assert_se(streq(result, "/etc"));
+        result = mfree(result);
+
+        r = chase_symlinks("/../.././//../../test-chase.fsldajfl", "/", CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &result);
+        assert_se(r == 0);
+        assert_se(streq(result, "/test-chase.fsldajfl"));
+        result = mfree(result);
+
         r = chase_symlinks("/etc/machine-id/foo", NULL, 0, &result);
         assert_se(r == -ENOTDIR);
+        result = mfree(result);
 
         /* Path that loops back to self */
 
-        result = mfree(result);
         p = strjoina(temp, "/recursive-symlink");
         assert_se(symlink("recursive-symlink", p) >= 0);
         r = chase_symlinks(p, NULL, 0, &result);
@@ -220,8 +262,9 @@ static void test_readlink_and_make_absolute(void) {
         char name2[] = "test-readlink_and_make_absolute/original";
         char name_alias[] = "/tmp/test-readlink_and_make_absolute-alias";
         char *r = NULL;
+        _cleanup_free_ char *pwd = NULL;
 
-        assert_se(mkdir_safe(tempdir, 0755, getuid(), getgid()) >= 0);
+        assert_se(mkdir_safe(tempdir, 0755, getuid(), getgid(), false) >= 0);
         assert_se(touch(name) >= 0);
 
         assert_se(symlink(name, name_alias) >= 0);
@@ -230,6 +273,8 @@ static void test_readlink_and_make_absolute(void) {
         free(r);
         assert_se(unlink(name_alias) >= 0);
 
+        assert_se(pwd = get_current_dir_name());
+
         assert_se(chdir(tempdir) >= 0);
         assert_se(symlink(name2, name_alias) >= 0);
         assert_se(readlink_and_make_absolute(name_alias, &r) >= 0);
@@ -237,6 +282,8 @@ static void test_readlink_and_make_absolute(void) {
         free(r);
         assert_se(unlink(name_alias) >= 0);
 
+        assert_se(chdir(pwd) >= 0);
+
         assert_se(rm_rf(tempdir, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
 }
 
@@ -317,6 +364,34 @@ static void test_dot_or_dot_dot(void) {
         assert_se(!dot_or_dot_dot("..foo"));
 }
 
+#if 0 /// Uses functions that elogind does not need
+static void test_access_fd(void) {
+        _cleanup_(rmdir_and_freep) char *p = NULL;
+        _cleanup_close_ int fd = -1;
+
+        assert_se(mkdtemp_malloc("/tmp/access-fd.XXXXXX", &p) >= 0);
+
+        fd = open(p, O_RDONLY|O_DIRECTORY|O_CLOEXEC);
+        assert_se(fd >= 0);
+
+        assert_se(access_fd(fd, R_OK) >= 0);
+        assert_se(access_fd(fd, F_OK) >= 0);
+        assert_se(access_fd(fd, W_OK) >= 0);
+
+        assert_se(fchmod(fd, 0000) >= 0);
+
+        assert_se(access_fd(fd, F_OK) >= 0);
+
+        if (geteuid() == 0) {
+                assert_se(access_fd(fd, R_OK) >= 0);
+                assert_se(access_fd(fd, W_OK) >= 0);
+        } else {
+                assert_se(access_fd(fd, R_OK) == -EACCES);
+                assert_se(access_fd(fd, W_OK) == -EACCES);
+        }
+}
+#endif // 0
+
 int main(int argc, char *argv[]) {
         test_unlink_noerrno();
         test_get_files_in_directory();
@@ -326,6 +401,9 @@ int main(int argc, char *argv[]) {
 #endif // 0
         test_chase_symlinks();
         test_dot_or_dot_dot();
+#if 0 /// Uses functions that elogind does not need
+        test_access_fd();
+#endif // 0
 
         return 0;
 }
index 02d1cfaee30a3d19e5672571625417683d3b929f..f3b4258d6b5b4227831fcf4cbe427a131af83cf9 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
index 7c28e84e90316f4b3a83cc9bb48910edf0ccd59d..d130f58326fa9b37f4a4fc9b85209029b10ece60 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd
 
index 2615c98eb05da2f4721c498944bf2d696616edac..dd9195425efd9f7e690fdbdb003e73cae5b503db 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd
 
index 8af32c8744f0bc73680195d5c6c3c05e8f8fe99e..1ee93f5c55d22691a643bc527fd7b16d7131c997 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 #pragma once
 
 /***
@@ -40,4 +41,4 @@
                -ENOMEDIUM /* cannot determine cgroup */         \
                )
 
-void enter_cgroup_subroot(void);
+int enter_cgroup_subroot(void);
index 350c16aa09a2712b99483fef5a2e2fadbfb17a49..fdb1d1ae39ccb887b5c3c210583a0ded044b582d 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
@@ -109,72 +110,72 @@ static void test_unhexmem(void) {
 static void test_base32hexmem(void) {
         char *b32;
 
-        b32 = base32hexmem("", strlen(""), true);
+        b32 = base32hexmem("", STRLEN(""), true);
         assert_se(b32);
         assert_se(streq(b32, ""));
         free(b32);
 
-        b32 = base32hexmem("f", strlen("f"), true);
+        b32 = base32hexmem("f", STRLEN("f"), true);
         assert_se(b32);
         assert_se(streq(b32, "CO======"));
         free(b32);
 
-        b32 = base32hexmem("fo", strlen("fo"), true);
+        b32 = base32hexmem("fo", STRLEN("fo"), true);
         assert_se(b32);
         assert_se(streq(b32, "CPNG===="));
         free(b32);
 
-        b32 = base32hexmem("foo", strlen("foo"), true);
+        b32 = base32hexmem("foo", STRLEN("foo"), true);
         assert_se(b32);
         assert_se(streq(b32, "CPNMU==="));
         free(b32);
 
-        b32 = base32hexmem("foob", strlen("foob"), true);
+        b32 = base32hexmem("foob", STRLEN("foob"), true);
         assert_se(b32);
         assert_se(streq(b32, "CPNMUOG="));
         free(b32);
 
-        b32 = base32hexmem("fooba", strlen("fooba"), true);
+        b32 = base32hexmem("fooba", STRLEN("fooba"), true);
         assert_se(b32);
         assert_se(streq(b32, "CPNMUOJ1"));
         free(b32);
 
-        b32 = base32hexmem("foobar", strlen("foobar"), true);
+        b32 = base32hexmem("foobar", STRLEN("foobar"), true);
         assert_se(b32);
         assert_se(streq(b32, "CPNMUOJ1E8======"));
         free(b32);
 
-        b32 = base32hexmem("", strlen(""), false);
+        b32 = base32hexmem("", STRLEN(""), false);
         assert_se(b32);
         assert_se(streq(b32, ""));
         free(b32);
 
-        b32 = base32hexmem("f", strlen("f"), false);
+        b32 = base32hexmem("f", STRLEN("f"), false);
         assert_se(b32);
         assert_se(streq(b32, "CO"));
         free(b32);
 
-        b32 = base32hexmem("fo", strlen("fo"), false);
+        b32 = base32hexmem("fo", STRLEN("fo"), false);
         assert_se(b32);
         assert_se(streq(b32, "CPNG"));
         free(b32);
 
-        b32 = base32hexmem("foo", strlen("foo"), false);
+        b32 = base32hexmem("foo", STRLEN("foo"), false);
         assert_se(b32);
         assert_se(streq(b32, "CPNMU"));
         free(b32);
 
-        b32 = base32hexmem("foob", strlen("foob"), false);
+        b32 = base32hexmem("foob", STRLEN("foob"), false);
         assert_se(b32);
         assert_se(streq(b32, "CPNMUOG"));
         free(b32);
 
-        b32 = base32hexmem("fooba", strlen("fooba"), false);
+        b32 = base32hexmem("fooba", STRLEN("fooba"), false);
         assert_se(b32);
         assert_se(streq(b32, "CPNMUOJ1"));
         free(b32);
 
-        b32 = base32hexmem("foobar", strlen("foobar"), false);
+        b32 = base32hexmem("foobar", STRLEN("foobar"), false);
         assert_se(b32);
         assert_se(streq(b32, "CPNMUOJ1E8"));
         free(b32);
@@ -184,121 +185,121 @@ static void test_unbase32hexmem(void) {
         void *mem;
         size_t len;
 
-        assert_se(unbase32hexmem("", strlen(""), true, &mem, &len) == 0);
+        assert_se(unbase32hexmem("", STRLEN(""), true, &mem, &len) == 0);
         assert_se(streq(strndupa(mem, len), ""));
         free(mem);
 
-        assert_se(unbase32hexmem("CO======", strlen("CO======"), true, &mem, &len) == 0);
+        assert_se(unbase32hexmem("CO======", STRLEN("CO======"), true, &mem, &len) == 0);
         assert_se(streq(strndupa(mem, len), "f"));
         free(mem);
 
-        assert_se(unbase32hexmem("CPNG====", strlen("CPNG===="), true, &mem, &len) == 0);
+        assert_se(unbase32hexmem("CPNG====", STRLEN("CPNG===="), true, &mem, &len) == 0);
         assert_se(streq(strndupa(mem, len), "fo"));
         free(mem);
 
-        assert_se(unbase32hexmem("CPNMU===", strlen("CPNMU==="), true, &mem, &len) == 0);
+        assert_se(unbase32hexmem("CPNMU===", STRLEN("CPNMU==="), true, &mem, &len) == 0);
         assert_se(streq(strndupa(mem, len), "foo"));
         free(mem);
 
-        assert_se(unbase32hexmem("CPNMUOG=", strlen("CPNMUOG="), true, &mem, &len) == 0);
+        assert_se(unbase32hexmem("CPNMUOG=", STRLEN("CPNMUOG="), true, &mem, &len) == 0);
         assert_se(streq(strndupa(mem, len), "foob"));
         free(mem);
 
-        assert_se(unbase32hexmem("CPNMUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == 0);
+        assert_se(unbase32hexmem("CPNMUOJ1", STRLEN("CPNMUOJ1"), true, &mem, &len) == 0);
         assert_se(streq(strndupa(mem, len), "fooba"));
         free(mem);
 
-        assert_se(unbase32hexmem("CPNMUOJ1E8======", strlen("CPNMUOJ1E8======"), true, &mem, &len) == 0);
+        assert_se(unbase32hexmem("CPNMUOJ1E8======", STRLEN("CPNMUOJ1E8======"), true, &mem, &len) == 0);
         assert_se(streq(strndupa(mem, len), "foobar"));
         free(mem);
 
-        assert_se(unbase32hexmem("A", strlen("A"), true, &mem, &len) == -EINVAL);
-        assert_se(unbase32hexmem("A=======", strlen("A======="), true, &mem, &len) == -EINVAL);
-        assert_se(unbase32hexmem("AAA=====", strlen("AAA====="), true, &mem, &len) == -EINVAL);
-        assert_se(unbase32hexmem("AAAAAA==", strlen("AAAAAA=="), true, &mem, &len) == -EINVAL);
-        assert_se(unbase32hexmem("AB======", strlen("AB======"), true, &mem, &len) == -EINVAL);
-        assert_se(unbase32hexmem("AAAB====", strlen("AAAB===="), true, &mem, &len) == -EINVAL);
-        assert_se(unbase32hexmem("AAAAB===", strlen("AAAAB==="), true, &mem, &len) == -EINVAL);
-        assert_se(unbase32hexmem("AAAAAAB=", strlen("AAAAAAB="), true, &mem, &len) == -EINVAL);
-
-        assert_se(unbase32hexmem("XPNMUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
-        assert_se(unbase32hexmem("CXNMUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
-        assert_se(unbase32hexmem("CPXMUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
-        assert_se(unbase32hexmem("CPNXUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
-        assert_se(unbase32hexmem("CPNMXOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
-        assert_se(unbase32hexmem("CPNMUXJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
-        assert_se(unbase32hexmem("CPNMUOX1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
-        assert_se(unbase32hexmem("CPNMUOJX", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
-
-        assert_se(unbase32hexmem("", strlen(""), false, &mem, &len) == 0);
+        assert_se(unbase32hexmem("A", STRLEN("A"), true, &mem, &len) == -EINVAL);
+        assert_se(unbase32hexmem("A=======", STRLEN("A======="), true, &mem, &len) == -EINVAL);
+        assert_se(unbase32hexmem("AAA=====", STRLEN("AAA====="), true, &mem, &len) == -EINVAL);
+        assert_se(unbase32hexmem("AAAAAA==", STRLEN("AAAAAA=="), true, &mem, &len) == -EINVAL);
+        assert_se(unbase32hexmem("AB======", STRLEN("AB======"), true, &mem, &len) == -EINVAL);
+        assert_se(unbase32hexmem("AAAB====", STRLEN("AAAB===="), true, &mem, &len) == -EINVAL);
+        assert_se(unbase32hexmem("AAAAB===", STRLEN("AAAAB==="), true, &mem, &len) == -EINVAL);
+        assert_se(unbase32hexmem("AAAAAAB=", STRLEN("AAAAAAB="), true, &mem, &len) == -EINVAL);
+
+        assert_se(unbase32hexmem("XPNMUOJ1", STRLEN("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
+        assert_se(unbase32hexmem("CXNMUOJ1", STRLEN("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
+        assert_se(unbase32hexmem("CPXMUOJ1", STRLEN("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
+        assert_se(unbase32hexmem("CPNXUOJ1", STRLEN("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
+        assert_se(unbase32hexmem("CPNMXOJ1", STRLEN("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
+        assert_se(unbase32hexmem("CPNMUXJ1", STRLEN("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
+        assert_se(unbase32hexmem("CPNMUOX1", STRLEN("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
+        assert_se(unbase32hexmem("CPNMUOJX", STRLEN("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
+
+        assert_se(unbase32hexmem("", STRLEN(""), false, &mem, &len) == 0);
         assert_se(streq(strndupa(mem, len), ""));
         free(mem);
 
-        assert_se(unbase32hexmem("CO", strlen("CO"), false, &mem, &len) == 0);
+        assert_se(unbase32hexmem("CO", STRLEN("CO"), false, &mem, &len) == 0);
         assert_se(streq(strndupa(mem, len), "f"));
         free(mem);
 
-        assert_se(unbase32hexmem("CPNG", strlen("CPNG"), false, &mem, &len) == 0);
+        assert_se(unbase32hexmem("CPNG", STRLEN("CPNG"), false, &mem, &len) == 0);
         assert_se(streq(strndupa(mem, len), "fo"));
         free(mem);
 
-        assert_se(unbase32hexmem("CPNMU", strlen("CPNMU"), false, &mem, &len) == 0);
+        assert_se(unbase32hexmem("CPNMU", STRLEN("CPNMU"), false, &mem, &len) == 0);
         assert_se(streq(strndupa(mem, len), "foo"));
         free(mem);
 
-        assert_se(unbase32hexmem("CPNMUOG", strlen("CPNMUOG"), false, &mem, &len) == 0);
+        assert_se(unbase32hexmem("CPNMUOG", STRLEN("CPNMUOG"), false, &mem, &len) == 0);
         assert_se(streq(strndupa(mem, len), "foob"));
         free(mem);
 
-        assert_se(unbase32hexmem("CPNMUOJ1", strlen("CPNMUOJ1"), false, &mem, &len) == 0);
+        assert_se(unbase32hexmem("CPNMUOJ1", STRLEN("CPNMUOJ1"), false, &mem, &len) == 0);
         assert_se(streq(strndupa(mem, len), "fooba"));
         free(mem);
 
-        assert_se(unbase32hexmem("CPNMUOJ1E8", strlen("CPNMUOJ1E8"), false, &mem, &len) == 0);
+        assert_se(unbase32hexmem("CPNMUOJ1E8", STRLEN("CPNMUOJ1E8"), false, &mem, &len) == 0);
         assert_se(streq(strndupa(mem, len), "foobar"));
         free(mem);
 
-        assert_se(unbase32hexmem("CPNMUOG=", strlen("CPNMUOG="), false, &mem, &len) == -EINVAL);
-        assert_se(unbase32hexmem("CPNMUOJ1E8======", strlen("CPNMUOJ1E8======"), false, &mem, &len) == -EINVAL);
-        assert_se(unbase32hexmem("A", strlen("A"), false, &mem, &len) == -EINVAL);
-        assert_se(unbase32hexmem("A", strlen("A"), false, &mem, &len) == -EINVAL);
-        assert_se(unbase32hexmem("AAA", strlen("AAA"), false, &mem, &len) == -EINVAL);
-        assert_se(unbase32hexmem("AAAAAA", strlen("AAAAAA"), false, &mem, &len) == -EINVAL);
-        assert_se(unbase32hexmem("AB", strlen("AB"), false, &mem, &len) == -EINVAL);
-        assert_se(unbase32hexmem("AAAB", strlen("AAAB"), false, &mem, &len) == -EINVAL);
-        assert_se(unbase32hexmem("AAAAB", strlen("AAAAB"), false, &mem, &len) == -EINVAL);
-        assert_se(unbase32hexmem("AAAAAAB", strlen("AAAAAAB"), false, &mem, &len) == -EINVAL);
+        assert_se(unbase32hexmem("CPNMUOG=", STRLEN("CPNMUOG="), false, &mem, &len) == -EINVAL);
+        assert_se(unbase32hexmem("CPNMUOJ1E8======", STRLEN("CPNMUOJ1E8======"), false, &mem, &len) == -EINVAL);
+        assert_se(unbase32hexmem("A", STRLEN("A"), false, &mem, &len) == -EINVAL);
+        assert_se(unbase32hexmem("A", STRLEN("A"), false, &mem, &len) == -EINVAL);
+        assert_se(unbase32hexmem("AAA", STRLEN("AAA"), false, &mem, &len) == -EINVAL);
+        assert_se(unbase32hexmem("AAAAAA", STRLEN("AAAAAA"), false, &mem, &len) == -EINVAL);
+        assert_se(unbase32hexmem("AB", STRLEN("AB"), false, &mem, &len) == -EINVAL);
+        assert_se(unbase32hexmem("AAAB", STRLEN("AAAB"), false, &mem, &len) == -EINVAL);
+        assert_se(unbase32hexmem("AAAAB", STRLEN("AAAAB"), false, &mem, &len) == -EINVAL);
+        assert_se(unbase32hexmem("AAAAAAB", STRLEN("AAAAAAB"), false, &mem, &len) == -EINVAL);
 }
 
 /* https://tools.ietf.org/html/rfc4648#section-10 */
 static void test_base64mem(void) {
         char *b64;
 
-        assert_se(base64mem("", strlen(""), &b64) == 0);
+        assert_se(base64mem("", STRLEN(""), &b64) == 0);
         assert_se(streq(b64, ""));
         free(b64);
 
-        assert_se(base64mem("f", strlen("f"), &b64) == 4);
+        assert_se(base64mem("f", STRLEN("f"), &b64) == 4);
         assert_se(streq(b64, "Zg=="));
         free(b64);
 
-        assert_se(base64mem("fo", strlen("fo"), &b64) == 4);
+        assert_se(base64mem("fo", STRLEN("fo"), &b64) == 4);
         assert_se(streq(b64, "Zm8="));
         free(b64);
 
-        assert_se(base64mem("foo", strlen("foo"), &b64) == 4);
+        assert_se(base64mem("foo", STRLEN("foo"), &b64) == 4);
         assert_se(streq(b64, "Zm9v"));
         free(b64);
 
-        assert_se(base64mem("foob", strlen("foob"), &b64) == 8);
+        assert_se(base64mem("foob", STRLEN("foob"), &b64) == 8);
         assert_se(streq(b64, "Zm9vYg=="));
         free(b64);
 
-        assert_se(base64mem("fooba", strlen("fooba"), &b64) == 8);
+        assert_se(base64mem("fooba", STRLEN("fooba"), &b64) == 8);
         assert_se(streq(b64, "Zm9vYmE="));
         free(b64);
 
-        assert_se(base64mem("foobar", strlen("foobar"), &b64) == 8);
+        assert_se(base64mem("foobar", STRLEN("foobar"), &b64) == 8);
         assert_se(streq(b64, "Zm9vYmFy"));
         free(b64);
 }
index e5f45206f16a086d5d89585a98948285efd058b0..b7fca1540cefbe702e2c95fcc1459daee8394109 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
index 10bd3833bc65523e6ff930bb91e5b9a004202442..9ed9864f29975fa1fd8731bbae89af256a78d6f4 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
index 2a3852b8922dd303fd43c24ea7972323a4019402..d4ca6bdcef1b7bd113bace9348ffc16fd43f6e59 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
index 0ccd745cc913bf7d21161b2da9a5f7aa9fc9130b..25ffd8fb86639621f3afbcc2be6a1dccfeb9f922 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd
 
index 9e69567e4cadee8b76d0230e5be9167290a387de..4467bdd48df2d2a449074474c75caf9281b4293f 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd
 
index 2417b3aefcad60fd4cbb26b961c40018258fb736..3754b1285cc10140996f67abf703e9d75f635edb 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
index 120a8b103ed0730548a8097b109961154de9b6f9..241145806e1fcd5dbfa16ed6e9ed9814a63d522c 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
 #include <locale.h>
 #include <math.h>
 
+#include "alloc-util.h"
+#include "errno-list.h"
 #include "log.h"
 #include "parse-util.h"
+#include "string-util.h"
 
 static void test_parse_boolean(void) {
         assert_se(parse_boolean("1") == 1);
@@ -79,6 +83,9 @@ static void test_parse_pid(void) {
 
         r = parse_pid("junk", &pid);
         assert_se(r == -EINVAL);
+
+        r = parse_pid("", &pid);
+        assert_se(r == -EINVAL);
 }
 
 static void test_parse_mode(void) {
@@ -98,6 +105,8 @@ static void test_parse_mode(void) {
 static void test_parse_size(void) {
         uint64_t bytes;
 
+        assert_se(parse_size("", 1024, &bytes) == -EINVAL);
+
         assert_se(parse_size("111", 1024, &bytes) == 0);
         assert_se(bytes == 111);
 
@@ -258,6 +267,10 @@ static void test_parse_range(void) {
         assert_se(lower == 9999);
         assert_se(upper == 9999);
 
+        assert_se(parse_range("-123", &lower, &upper) == -EINVAL);
+        assert_se(lower == 9999);
+        assert_se(upper == 9999);
+
         assert_se(parse_range("-111-123", &lower, &upper) == -EINVAL);
         assert_se(lower == 9999);
         assert_se(upper == 9999);
@@ -372,6 +385,15 @@ static void test_safe_atolli(void) {
 
         r = safe_atolli("junk", &l);
         assert_se(r == -EINVAL);
+
+        r = safe_atolli("123x", &l);
+        assert_se(r == -EINVAL);
+
+        r = safe_atolli("12.3", &l);
+        assert_se(r == -EINVAL);
+
+        r = safe_atolli("", &l);
+        assert_se(r == -EINVAL);
 }
 
 static void test_safe_atou16(void) {
@@ -400,6 +422,12 @@ static void test_safe_atou16(void) {
 
         r = safe_atou16("123x", &l);
         assert_se(r == -EINVAL);
+
+        r = safe_atou16("12.3", &l);
+        assert_se(r == -EINVAL);
+
+        r = safe_atou16("", &l);
+        assert_se(r == -EINVAL);
 }
 
 static void test_safe_atoi16(void) {
@@ -433,6 +461,12 @@ static void test_safe_atoi16(void) {
 
         r = safe_atoi16("123x", &l);
         assert_se(r == -EINVAL);
+
+        r = safe_atoi16("12.3", &l);
+        assert_se(r == -EINVAL);
+
+        r = safe_atoi16("", &l);
+        assert_se(r == -EINVAL);
 }
 
 static void test_safe_atou64(void) {
@@ -461,6 +495,12 @@ static void test_safe_atou64(void) {
 
         r = safe_atou64("123x", &l);
         assert_se(r == -EINVAL);
+
+        r = safe_atou64("12.3", &l);
+        assert_se(r == -EINVAL);
+
+        r = safe_atou64("", &l);
+        assert_se(r == -EINVAL);
 }
 
 static void test_safe_atoi64(void) {
@@ -494,6 +534,12 @@ static void test_safe_atoi64(void) {
 
         r = safe_atoi64("123x", &l);
         assert_se(r == -EINVAL);
+
+        r = safe_atoi64("12.3", &l);
+        assert_se(r == -EINVAL);
+
+        r = safe_atoi64("", &l);
+        assert_se(r == -EINVAL);
 }
 
 static void test_safe_atod(void) {
@@ -515,6 +561,9 @@ static void test_safe_atod(void) {
         strtod("0,5", &e);
         assert_se(*e == ',');
 
+        r = safe_atod("", &d);
+        assert_se(r == -EINVAL);
+
         /* Check if this really is locale independent */
         if (setlocale(LC_NUMERIC, "de_DE.utf8")) {
 
@@ -530,6 +579,9 @@ static void test_safe_atod(void) {
 #if defined(__GLIBC__)
                 assert_se(fabs(strtod("0,5", &e) - 0.5) < 0.00001);
 #endif // __GLIBC__
+
+                r = safe_atod("", &d);
+                assert_se(r == -EINVAL);
         }
 
         /* And check again, reset */
@@ -545,6 +597,9 @@ static void test_safe_atod(void) {
         errno = 0;
         strtod("0,5", &e);
         assert_se(*e == ',');
+
+        r = safe_atod("", &d);
+        assert_se(r == -EINVAL);
 }
 
 static void test_parse_percent(void) {
@@ -563,6 +618,7 @@ static void test_parse_percent(void) {
         assert_se(parse_percent("%%") == -EINVAL);
         assert_se(parse_percent("%1") == -EINVAL);
         assert_se(parse_percent("1%%") == -EINVAL);
+        assert_se(parse_percent("3.2%") == -EINVAL);
 }
 
 static void test_parse_percent_unbounded(void) {
@@ -603,6 +659,8 @@ static void test_parse_nice(void) {
 static void test_parse_dev(void) {
         dev_t dev;
 
+        assert_se(parse_dev("", &dev) == -EINVAL);
+        assert_se(parse_dev("junk", &dev) == -EINVAL);
         assert_se(parse_dev("0", &dev) == -EINVAL);
         assert_se(parse_dev("5", &dev) == -EINVAL);
         assert_se(parse_dev("5:", &dev) == -EINVAL);
@@ -613,6 +671,74 @@ static void test_parse_dev(void) {
         assert_se(parse_dev("8:11", &dev) >= 0 && major(dev) == 8 && minor(dev) == 11);
 }
 
+static void test_parse_errno(void) {
+        assert_se(parse_errno("EILSEQ") == EILSEQ);
+        assert_se(parse_errno("EINVAL") == EINVAL);
+        assert_se(parse_errno("0") == 0);
+        assert_se(parse_errno("1") == 1);
+        assert_se(parse_errno("4095") == 4095);
+
+        assert_se(parse_errno("-1") == -ERANGE);
+        assert_se(parse_errno("-3") == -ERANGE);
+        assert_se(parse_errno("4096") == -ERANGE);
+
+        assert_se(parse_errno("") == -EINVAL);
+        assert_se(parse_errno("12.3") == -EINVAL);
+        assert_se(parse_errno("123junk") == -EINVAL);
+        assert_se(parse_errno("junk123") == -EINVAL);
+        assert_se(parse_errno("255EILSEQ") == -EINVAL);
+        assert_se(parse_errno("EINVAL12") == -EINVAL);
+        assert_se(parse_errno("-EINVAL") == -EINVAL);
+        assert_se(parse_errno("EINVALaaa") == -EINVAL);
+}
+
+static void test_parse_syscall_and_errno(void) {
+        _cleanup_free_ char *n = NULL;
+        int e;
+
+        assert_se(parse_syscall_and_errno("uname:EILSEQ", &n, &e) >= 0);
+        assert_se(streq(n, "uname"));
+        assert_se(e == errno_from_name("EILSEQ") && e >= 0);
+        n = mfree(n);
+
+        assert_se(parse_syscall_and_errno("uname:EINVAL", &n, &e) >= 0);
+        assert_se(streq(n, "uname"));
+        assert_se(e == errno_from_name("EINVAL") && e >= 0);
+        n = mfree(n);
+
+        assert_se(parse_syscall_and_errno("@sync:4095", &n, &e) >= 0);
+        assert_se(streq(n, "@sync"));
+        assert_se(e == 4095);
+        n = mfree(n);
+
+        /* If errno is omitted, then e is set to -1 */
+        assert_se(parse_syscall_and_errno("mount", &n, &e) >= 0);
+        assert_se(streq(n, "mount"));
+        assert_se(e == -1);
+        n = mfree(n);
+
+        /* parse_syscall_and_errno() does not check the syscall name is valid or not. */
+        assert_se(parse_syscall_and_errno("hoge:255", &n, &e) >= 0);
+        assert_se(streq(n, "hoge"));
+        assert_se(e == 255);
+        n = mfree(n);
+
+        /* The function checks the syscall name is empty or not. */
+        assert_se(parse_syscall_and_errno("", &n, &e) == -EINVAL);
+        assert_se(parse_syscall_and_errno(":255", &n, &e) == -EINVAL);
+
+        /* errno must be a valid errno name or number between 0 and ERRNO_MAX == 4095 */
+        assert_se(parse_syscall_and_errno("hoge:4096", &n, &e) == -ERANGE);
+        assert_se(parse_syscall_and_errno("hoge:-3", &n, &e) == -ERANGE);
+        assert_se(parse_syscall_and_errno("hoge:12.3", &n, &e) == -EINVAL);
+        assert_se(parse_syscall_and_errno("hoge:123junk", &n, &e) == -EINVAL);
+        assert_se(parse_syscall_and_errno("hoge:junk123", &n, &e) == -EINVAL);
+        assert_se(parse_syscall_and_errno("hoge:255:EILSEQ", &n, &e) == -EINVAL);
+        assert_se(parse_syscall_and_errno("hoge:-EINVAL", &n, &e) == -EINVAL);
+        assert_se(parse_syscall_and_errno("hoge:EINVALaaa", &n, &e) == -EINVAL);
+        assert_se(parse_syscall_and_errno("hoge:", &n, &e) == -EINVAL);
+}
+
 int main(int argc, char *argv[]) {
         log_parse_environment();
         log_open();
@@ -636,6 +762,8 @@ int main(int argc, char *argv[]) {
         test_parse_nice();
 #endif // 0
         test_parse_dev();
+        test_parse_errno();
+        test_parse_syscall_and_errno();
 
         return 0;
 }
index 5772f3aa0720ea6d7ede373d552360f74b37ced9..b2b5fd19fc44399d49d7989f99ff0263cdcd161f 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
@@ -400,6 +401,21 @@ static void test_file_in_same_dir(void) {
         free(t);
 }
 
+static void test_last_path_component(void) {
+        assert_se(streq(last_path_component("a/b/c"), "c"));
+        assert_se(streq(last_path_component("a/b/c/"), "c/"));
+        assert_se(streq(last_path_component("/"), "/"));
+        assert_se(streq(last_path_component("//"), "/"));
+        assert_se(streq(last_path_component("///"), "/"));
+        assert_se(streq(last_path_component("."), "."));
+        assert_se(streq(last_path_component("./."), "."));
+        assert_se(streq(last_path_component("././"), "./"));
+        assert_se(streq(last_path_component("././/"), ".//"));
+        assert_se(streq(last_path_component("/foo/a"), "a"));
+        assert_se(streq(last_path_component("/foo/a/"), "a/"));
+        assert_se(streq(last_path_component(""), ""));
+}
+
 static void test_filename_is_valid(void) {
         char foo[FILENAME_MAX+2];
         int i;
@@ -489,6 +505,7 @@ int main(int argc, char **argv) {
         test_path_startswith();
         test_prefix_root();
         test_file_in_same_dir();
+        test_last_path_component();
         test_filename_is_valid();
         test_hidden_or_backup_file();
         test_skip_dev_prefix();
index d81880a6556efbf513ce68b1dc4957cd1731b018..f558384a18fc7ef9915b587ecadb18f63f77f93f 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
index 59dcf407160ddb22c1b627b65db39750641087da..faa1f8920f3ede9f85a04df95ec3856cfc980f05 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
index fb2cce77776baffe0a942837be91ba2b12dd0f92..0cb9a82d34e27c2f663691299ea9303ada2a9664 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
@@ -48,7 +49,7 @@ static void test_get_process_comm(pid_t pid) {
         struct stat st;
         _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL;
         _cleanup_free_ char *env = NULL;
-        char path[strlen("/proc//comm") + DECIMAL_STR_MAX(pid_t)];
+        char path[STRLEN("/proc//comm") + DECIMAL_STR_MAX(pid_t)];
 #if 0 /// UNNEEDED by elogind
         pid_t e;
         uid_t u;
@@ -390,7 +391,7 @@ static void test_rename_process_now(const char *p, int ret) {
         /* we cannot expect cmdline to be renamed properly without privileges */
         if (geteuid() == 0) {
                 log_info("cmdline = <%s>", cmdline);
-                assert_se(strneq(p, cmdline, strlen("test-process-util")));
+                assert_se(strneq(p, cmdline, STRLEN("test-process-util")));
                 assert_se(startswith(p, cmdline));
         } else
                 log_info("cmdline = <%s> (not verified)", cmdline);
index 50f4da270dc3c31edfbacc94a390c5e78fafcc8e..d7ccc91f9ba479f85b7d4a56d59f9d9d1a4f3b6c 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
index 964a228a5efb8be8bdf14a4546ec77c1b480f4c2..c888c09b29561c6ee1a302f438d67db02ea1812a 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
index 21540fa1def03164fc9b04b5021834a04e94437a..0a29a62621095ce32bc85f222664c4fc3d66067c 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd
 
index 0db8a1e32180d13893bd90ec2de577c7fb453df9..3f8eb325c2428380c891533476cc6d23d90a421a 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
index b74b7ad2ddaae7afea6016231fbbf5c5e5cae094..7469457be235e77c731b82c476dd41c91c0c5259 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
index cf122b674d710606a7a960d8e86bb5197a3f5df3..2b339faad77ef64c00ac83dd5f3aec28f61f44df 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
index 2825dbc5240ae0deeae5aeba033b2b309576cae5..bcb9a658f04fa5e7fbaadbe7ef3cc0419baa5370 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
@@ -72,16 +73,16 @@ static void test_path_is_os_tree(void) {
         assert_se(path_is_os_tree("/idontexist") == -ENOENT);
 }
 
-static void test_path_check_fstype(void) {
+static void test_path_is_fs_type(void) {
         /* run might not be a mount point in build chroots */
         if (path_is_mount_point("/run", NULL, AT_SYMLINK_FOLLOW) > 0) {
-                assert_se(path_check_fstype("/run", TMPFS_MAGIC) > 0);
-                assert_se(path_check_fstype("/run", BTRFS_SUPER_MAGIC) == 0);
+                assert_se(path_is_fs_type("/run", TMPFS_MAGIC) > 0);
+                assert_se(path_is_fs_type("/run", BTRFS_SUPER_MAGIC) == 0);
         }
-        assert_se(path_check_fstype("/proc", PROC_SUPER_MAGIC) > 0);
-        assert_se(path_check_fstype("/proc", BTRFS_SUPER_MAGIC) == 0);
-        assert_se(path_check_fstype("/proc", BTRFS_SUPER_MAGIC) == 0);
-        assert_se(path_check_fstype("/i-dont-exist", BTRFS_SUPER_MAGIC) == -ENOENT);
+        assert_se(path_is_fs_type("/proc", PROC_SUPER_MAGIC) > 0);
+        assert_se(path_is_fs_type("/proc", BTRFS_SUPER_MAGIC) == 0);
+        assert_se(path_is_fs_type("/proc", BTRFS_SUPER_MAGIC) == 0);
+        assert_se(path_is_fs_type("/i-dont-exist", BTRFS_SUPER_MAGIC) == -ENOENT);
 }
 
 static void test_path_is_temporary_fs(void) {
@@ -98,7 +99,7 @@ int main(int argc, char *argv[]) {
 #if 0 /// UNNEEDED by elogind
         test_is_symlink();
         test_path_is_os_tree();
-        test_path_check_fstype();
+        test_path_is_fs_type();
         test_path_is_temporary_fs();
 #endif // 0
 
index 12340f075f33897f2f9420fb91b7f7fbe8c7be90..0f841e5d7ebae251b14cd6a7dd3ef11ade3b7a38 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
@@ -321,11 +322,62 @@ static void test_endswith_no_case(void) {
 
 #if 0 /// UNNEEDED by elogind
 static void test_delete_chars(void) {
-        char *r;
-        char input[] = "   hello, waldo.   abc";
+        char *s, input[] = "   hello, waldo.   abc";
+
+        s = delete_chars(input, WHITESPACE);
+        assert_se(streq(s, "hello,waldo.abc"));
+        assert_se(s == input);
+}
+#endif // 0
+
+static void test_delete_trailing_chars(void) {
+
+        char *s,
+                input1[] = " \n \r k \n \r ",
+                input2[] = "kkkkthiskkkiskkkaktestkkk",
+                input3[] = "abcdef";
+
+        s = delete_trailing_chars(input1, WHITESPACE);
+        assert_se(streq(s, " \n \r k"));
+        assert_se(s == input1);
+
+        s = delete_trailing_chars(input2, "kt");
+        assert_se(streq(s, "kkkkthiskkkiskkkaktes"));
+        assert_se(s == input2);
 
-        r = delete_chars(input, WHITESPACE);
-        assert_se(streq(r, "hello,waldo.abc"));
+        s = delete_trailing_chars(input3, WHITESPACE);
+        assert_se(streq(s, "abcdef"));
+        assert_se(s == input3);
+
+        s = delete_trailing_chars(input3, "fe");
+        assert_se(streq(s, "abcd"));
+        assert_se(s == input3);
+}
+
+static void test_delete_trailing_slashes(void) {
+        char s1[] = "foobar//",
+             s2[] = "foobar/",
+             s3[] = "foobar",
+             s4[] = "";
+
+        assert_se(streq(delete_trailing_chars(s1, "_"), "foobar//"));
+        assert_se(streq(delete_trailing_chars(s1, "/"), "foobar"));
+        assert_se(streq(delete_trailing_chars(s2, "/"), "foobar"));
+        assert_se(streq(delete_trailing_chars(s3, "/"), "foobar"));
+        assert_se(streq(delete_trailing_chars(s4, "/"), ""));
+}
+
+#if 0 /// UNNEEDED by elogind
+static void test_skip_leading_chars(void) {
+        char input1[] = " \n \r k \n \r ",
+                input2[] = "kkkkthiskkkiskkkaktestkkk",
+                input3[] = "abcdef";
+
+        assert_se(streq(skip_leading_chars(input1, WHITESPACE), "k \n \r "));
+        assert_se(streq(skip_leading_chars(input2, "k"), "thiskkkiskkkaktestkkk"));
+        assert_se(streq(skip_leading_chars(input2, "tk"), "hiskkkiskkkaktestkkk"));
+        assert_se(streq(skip_leading_chars(input3, WHITESPACE), "abcdef"));
+        assert_se(streq(skip_leading_chars(input3, "bcaef"), "def"));
 }
 #endif // 0
 
@@ -401,6 +453,11 @@ int main(int argc, char *argv[]) {
         test_endswith_no_case();
 #if 0 /// UNNEEDED by elogind
         test_delete_chars();
+#endif // 0
+        test_delete_trailing_chars();
+        test_delete_trailing_slashes();
+#if 0 /// UNNEEDED by elogind
+        test_skip_leading_chars();
 #endif // 0
         test_in_charset();
         test_split_pair();
index 72b0f6fc1112585f05b91cd734c8dadffacc8c9b..aabb7c40e70762e8f99e950b222c49ba08fdd66d 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
index 4f643989432a91a0d10976649667b0c07f776359..00445fa302ab400086109d64b6201da587830b6e 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd
 
index d79c0781b774cd3bf6f867a9ca04e06ad0d73866..942e2313d5dc3d505789b350573df7e15a27d633 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
@@ -22,6 +23,7 @@
 #include "string-util.h"
 #include "user-util.h"
 #include "util.h"
+#include "path-util.h"
 
 static void test_uid_to_name_one(uid_t uid, const char *name) {
         _cleanup_free_ char *t = NULL;
@@ -143,19 +145,57 @@ static void test_valid_home(void) {
         assert_se(valid_home("/home/foo"));
 }
 
+static void test_get_user_creds_one(const char *id, const char *name, uid_t uid, gid_t gid, const char *home, const char *shell) {
+        const char *rhome;
+        const char *rshell;
+        uid_t ruid;
+        gid_t rgid;
+
+        assert_se(get_user_creds(&id, &ruid, &rgid, &rhome, &rshell) >= 0);
+        assert_se(streq_ptr(id, name));
+        assert_se(ruid == uid);
+        assert_se(rgid == gid);
+        assert_se(path_equal(rhome, home));
+        assert_se(path_equal(rshell, shell));
+}
+
+#if 0 /// UNNEEDED by elogind
+static void test_get_group_creds_one(const char *id, const char *name, gid_t gid) {
+        gid_t rgid;
+
+        assert_se(get_group_creds(&id, &rgid) >= 0);
+        assert_se(streq_ptr(id, name));
+        assert_se(rgid == gid);
+}
+#endif // 0
+
 int main(int argc, char*argv[]) {
 
         test_uid_to_name_one(0, "root");
+        test_uid_to_name_one(UID_NOBODY, NOBODY_USER_NAME);
         test_uid_to_name_one(0xFFFF, "65535");
         test_uid_to_name_one(0xFFFFFFFF, "4294967295");
 
         test_gid_to_name_one(0, "root");
 #if 0 /// UNNEEDED by elogind
+        test_gid_to_name_one(GID_NOBODY, NOBODY_GROUP_NAME);
         test_gid_to_name_one(TTY_GID, "tty");
 #endif // 0
         test_gid_to_name_one(0xFFFF, "65535");
         test_gid_to_name_one(0xFFFFFFFF, "4294967295");
 
+        test_get_user_creds_one("root", "root", 0, 0, "/root", "/bin/sh");
+        test_get_user_creds_one("0", "root", 0, 0, "/root", "/bin/sh");
+        test_get_user_creds_one(NOBODY_USER_NAME, NOBODY_USER_NAME, UID_NOBODY, GID_NOBODY, "/", "/sbin/nologin");
+        test_get_user_creds_one("65534", NOBODY_USER_NAME, UID_NOBODY, GID_NOBODY, "/", "/sbin/nologin");
+
+#if 0 /// UNNEEDED by elogind
+        test_get_group_creds_one("root", "root", 0);
+        test_get_group_creds_one("0", "root", 0);
+        test_get_group_creds_one(NOBODY_GROUP_NAME, NOBODY_GROUP_NAME, GID_NOBODY);
+        test_get_group_creds_one("65534", NOBODY_GROUP_NAME, GID_NOBODY);
+#endif // 0
+
         test_parse_uid();
         test_uid_ptr();
 
index 1ce5a5a24d0b18fd1ad366c36cd3cb7f023c06c4..1ea0901f690812993aad29d83a3223126109046b 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
index 655192990da29547a5c3890da75413f75a27a4aa..c08a51a95c28ba1f4f2d13b9e3d66371932ad491 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
index 0fcdd9e78d8049fa273d3fe4ffc382c1d6a8beee..88276a21ea188819ab3570dfda7020720898a64e 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.