From: Sven Eden Date: Tue, 13 Mar 2018 18:19:10 +0000 (+0100) Subject: Prep v236 : Add missing SPDX-License-Identifier (8/9) src/test X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=8a17d92e544e7842a5740d7a66e0ef93daca18e6;p=elogind.git Prep v236 : Add missing SPDX-License-Identifier (8/9) src/test --- diff --git a/src/test/test-alloc-util.c b/src/test/test-alloc-util.c index cc4821eaf..50ccf4700 100644 --- a/src/test/test-alloc-util.c +++ b/src/test/test-alloc-util.c @@ -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 . ***/ +//#include + #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; } diff --git a/src/test/test-cgroup.c b/src/test/test-cgroup.c index e56473a63..5b06b9b1a 100644 --- a/src/test/test-cgroup.c +++ b/src/test/test-cgroup.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. diff --git a/src/test/test-conf-files.c b/src/test/test-conf-files.c index 777b5ca32..de91efac7 100644 --- a/src/test/test-conf-files.c +++ b/src/test/test-conf-files.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. diff --git a/src/test/test-conf-parser.c b/src/test/test-conf-parser.c index d2567f67d..62dd558f0 100644 --- a/src/test/test-conf-parser.c +++ b/src/test/test-conf-parser.c @@ -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: diff --git a/src/test/test-copy.c b/src/test/test-copy.c index fc5a8c169..4cc070656 100644 --- a/src/test/test-copy.c +++ b/src/test/test-copy.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd diff --git a/src/test/test-dlopen.c b/src/test/test-dlopen.c index 9f5343a7e..dd43da559 100644 --- a/src/test/test-dlopen.c +++ b/src/test/test-dlopen.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. diff --git a/src/test/test-ellipsize.c b/src/test/test-ellipsize.c index d4f09b08a..421774c07 100644 --- a/src/test/test-ellipsize.c +++ b/src/test/test-ellipsize.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. diff --git a/src/test/test-escape.c b/src/test/test-escape.c index e7bc49dd9..811562992 100644 --- a/src/test/test-escape.c +++ b/src/test/test-escape.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. diff --git a/src/test/test-exec-util.c b/src/test/test-exec-util.c index a8880e45c..02d745439 100644 --- a/src/test/test-exec-util.c +++ b/src/test/test-exec-util.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. diff --git a/src/test/test-extract-word.c b/src/test/test-extract-word.c index 8b75ebdf5..c8f735607 100644 --- a/src/test/test-extract-word.c +++ b/src/test/test-extract-word.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. diff --git a/src/test/test-fd-util.c b/src/test/test-fd-util.c index d1d9e1d2f..6b611b4b9 100644 --- a/src/test/test-fd-util.c +++ b/src/test/test-fd-util.c @@ -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; } diff --git a/src/test/test-fs-util.c b/src/test/test-fs-util.c index 467a7cb9d..0edb259f8 100644 --- a/src/test/test-fs-util.c +++ b/src/test/test-fs-util.c @@ -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; } diff --git a/src/test/test-hash.c b/src/test/test-hash.c index 02d1cfaee..f3b4258d6 100644 --- a/src/test/test-hash.c +++ b/src/test/test-hash.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. diff --git a/src/test/test-hashmap-plain.c b/src/test/test-hashmap-plain.c index 7c28e84e9..d130f5832 100644 --- a/src/test/test-hashmap-plain.c +++ b/src/test/test-hashmap-plain.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd diff --git a/src/test/test-hashmap.c b/src/test/test-hashmap.c index 2615c98eb..dd9195425 100644 --- a/src/test/test-hashmap.c +++ b/src/test/test-hashmap.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd diff --git a/src/test/test-helper.h b/src/test/test-helper.h index 8af32c874..1ee93f5c5 100644 --- a/src/test/test-helper.h +++ b/src/test/test-helper.h @@ -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); diff --git a/src/test/test-hexdecoct.c b/src/test/test-hexdecoct.c index 350c16aa0..fdb1d1ae3 100644 --- a/src/test/test-hexdecoct.c +++ b/src/test/test-hexdecoct.c @@ -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); } diff --git a/src/test/test-id128.c b/src/test/test-id128.c index e5f45206f..b7fca1540 100644 --- a/src/test/test-id128.c +++ b/src/test/test-id128.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. diff --git a/src/test/test-io-util.c b/src/test/test-io-util.c index 10bd3833b..9ed9864f2 100644 --- a/src/test/test-io-util.c +++ b/src/test/test-io-util.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. diff --git a/src/test/test-ipcrm.c b/src/test/test-ipcrm.c index 2a3852b89..d4ca6bdce 100644 --- a/src/test/test-ipcrm.c +++ b/src/test/test-ipcrm.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. diff --git a/src/test/test-list.c b/src/test/test-list.c index 0ccd745cc..25ffd8fb8 100644 --- a/src/test/test-list.c +++ b/src/test/test-list.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd diff --git a/src/test/test-locale-util.c b/src/test/test-locale-util.c index 9e69567e4..4467bdd48 100644 --- a/src/test/test-locale-util.c +++ b/src/test/test-locale-util.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd diff --git a/src/test/test-log.c b/src/test/test-log.c index 2417b3aef..3754b1285 100644 --- a/src/test/test-log.c +++ b/src/test/test-log.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. diff --git a/src/test/test-parse-util.c b/src/test/test-parse-util.c index 120a8b103..241145806 100644 --- a/src/test/test-parse-util.c +++ b/src/test/test-parse-util.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. @@ -21,8 +22,11 @@ #include #include +#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; } diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c index 5772f3aa0..b2b5fd19f 100644 --- a/src/test/test-path-util.c +++ b/src/test/test-path-util.c @@ -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(); diff --git a/src/test/test-prioq.c b/src/test/test-prioq.c index d81880a65..f558384a1 100644 --- a/src/test/test-prioq.c +++ b/src/test/test-prioq.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. diff --git a/src/test/test-proc-cmdline.c b/src/test/test-proc-cmdline.c index 59dcf4071..faa1f8920 100644 --- a/src/test/test-proc-cmdline.c +++ b/src/test/test-proc-cmdline.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c index fb2cce777..0cb9a82d3 100644 --- a/src/test/test-process-util.c +++ b/src/test/test-process-util.c @@ -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); diff --git a/src/test/test-random-util.c b/src/test/test-random-util.c index 50f4da270..d7ccc91f9 100644 --- a/src/test/test-random-util.c +++ b/src/test/test-random-util.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. diff --git a/src/test/test-selinux.c b/src/test/test-selinux.c index 964a228a5..c888c09b2 100644 --- a/src/test/test-selinux.c +++ b/src/test/test-selinux.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. diff --git a/src/test/test-set.c b/src/test/test-set.c index 21540fa1d..0a29a6262 100644 --- a/src/test/test-set.c +++ b/src/test/test-set.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd diff --git a/src/test/test-signal-util.c b/src/test/test-signal-util.c index 0db8a1e32..3f8eb325c 100644 --- a/src/test/test-signal-util.c +++ b/src/test/test-signal-util.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. diff --git a/src/test/test-siphash24.c b/src/test/test-siphash24.c index b74b7ad2d..7469457be 100644 --- a/src/test/test-siphash24.c +++ b/src/test/test-siphash24.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. diff --git a/src/test/test-sizeof.c b/src/test/test-sizeof.c index cf122b674..2b339faad 100644 --- a/src/test/test-sizeof.c +++ b/src/test/test-sizeof.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. diff --git a/src/test/test-stat-util.c b/src/test/test-stat-util.c index 2825dbc52..bcb9a658f 100644 --- a/src/test/test-stat-util.c +++ b/src/test/test-stat-util.c @@ -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 diff --git a/src/test/test-string-util.c b/src/test/test-string-util.c index 12340f075..0f841e5d7 100644 --- a/src/test/test-string-util.c +++ b/src/test/test-string-util.c @@ -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(); diff --git a/src/test/test-strip-tab-ansi.c b/src/test/test-strip-tab-ansi.c index 72b0f6fc1..aabb7c40e 100644 --- a/src/test/test-strip-tab-ansi.c +++ b/src/test/test-strip-tab-ansi.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. diff --git a/src/test/test-unaligned.c b/src/test/test-unaligned.c index 4f6439894..00445fa30 100644 --- a/src/test/test-unaligned.c +++ b/src/test/test-unaligned.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd diff --git a/src/test/test-user-util.c b/src/test/test-user-util.c index d79c0781b..942e2313d 100644 --- a/src/test/test-user-util.c +++ b/src/test/test-user-util.c @@ -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(); diff --git a/src/test/test-utf8.c b/src/test/test-utf8.c index 1ce5a5a24..1ea0901f6 100644 --- a/src/test/test-utf8.c +++ b/src/test/test-utf8.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. diff --git a/src/test/test-util.c b/src/test/test-util.c index 655192990..c08a51a95 100644 --- a/src/test/test-util.c +++ b/src/test/test-util.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd. diff --git a/src/test/test-verbs.c b/src/test/test-verbs.c index 0fcdd9e78..88276a21e 100644 --- a/src/test/test-verbs.c +++ b/src/test/test-verbs.c @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ /*** This file is part of systemd.