diff options
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/test-calendarspec.c | 3 | ||||
-rw-r--r-- | src/test/test-copy.c | 2 | ||||
-rw-r--r-- | src/test/test-dissect-image.c | 66 | ||||
-rw-r--r-- | src/test/test-execute.c | 8 | ||||
-rw-r--r-- | src/test/test-fs-util.c | 103 | ||||
-rw-r--r-- | src/test/test-hash.c | 82 | ||||
-rw-r--r-- | src/test/test-id128.c | 6 | ||||
-rw-r--r-- | src/test/test-path-util.c | 60 | ||||
-rw-r--r-- | src/test/test-time.c | 4 |
9 files changed, 280 insertions, 54 deletions
diff --git a/src/test/test-calendarspec.c b/src/test/test-calendarspec.c index b3d1160ea7..b8320b081b 100644 --- a/src/test/test-calendarspec.c +++ b/src/test/test-calendarspec.c @@ -186,6 +186,9 @@ int main(int argc, char* argv[]) { test_one("Monday *-*-*", "Mon *-*-* 00:00:00"); test_one("*-*-*", "*-*-* 00:00:00"); test_one("*:*:*", "*-*-* *:*:*"); + test_one("*:*", "*-*-* *:*:00"); + test_one("12:*", "*-*-* 12:*:00"); + test_one("*:30", "*-*-* *:30:00"); test_next("2016-03-27 03:17:00", "", 12345, 1459048620000000); test_next("2016-03-27 03:17:00", "CET", 12345, 1459041420000000); diff --git a/src/test/test-copy.c b/src/test/test-copy.c index 91d2a0bcd4..e65516f080 100644 --- a/src/test/test-copy.c +++ b/src/test/test-copy.c @@ -144,7 +144,7 @@ static void test_copy_tree(void) { assert_se((f = strjoin(original_dir, *p))); assert_se((l = strjoin(copy_dir, *link))); - assert_se(readlink_and_canonicalize(l, &target) == 0); + assert_se(readlink_and_canonicalize(l, NULL, &target) == 0); assert_se(path_equal(f, target)); } diff --git a/src/test/test-dissect-image.c b/src/test/test-dissect-image.c new file mode 100644 index 0000000000..0512a15e88 --- /dev/null +++ b/src/test/test-dissect-image.c @@ -0,0 +1,66 @@ +/*** + This file is part of systemd. + + Copyright 2016 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include <fcntl.h> +#include <stdio.h> + +#include "dissect-image.h" +#include "log.h" +#include "loop-util.h" +#include "string-util.h" + +int main(int argc, char *argv[]) { + _cleanup_(loop_device_unrefp) LoopDevice *d = NULL; + _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL; + int r, i; + + log_set_max_level(LOG_DEBUG); + + if (argc < 2) { + log_error("Requires one command line argument."); + return EXIT_FAILURE; + } + + r = loop_device_make_by_path(argv[1], O_RDONLY, &d); + if (r < 0) { + log_error_errno(r, "Failed to set up loopback device: %m"); + return EXIT_FAILURE; + } + + r = dissect_image(d->fd, NULL, 0, &m); + if (r < 0) { + log_error_errno(r, "Failed to dissect image: %m"); + return EXIT_FAILURE; + } + + for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) { + + if (!m->partitions[i].found) + continue; + + printf("Found %s partition, %s of type %s at #%i (%s)\n", + partition_designator_to_string(i), + m->partitions[i].rw ? "writable" : "read-only", + strna(m->partitions[i].fstype), + m->partitions[i].partno, + strna(m->partitions[i].node)); + } + + return EXIT_SUCCESS; +} diff --git a/src/test/test-execute.c b/src/test/test-execute.c index b2ea358b8c..4670458ffb 100644 --- a/src/test/test-execute.c +++ b/src/test/test-execute.c @@ -409,8 +409,8 @@ static void test_exec_spec_interpolation(Manager *m) { test(m, "exec-spec-interpolation.service", 0, CLD_EXITED); } -static int run_tests(UnitFileScope scope, test_function_t *tests) { - test_function_t *test = NULL; +static int run_tests(UnitFileScope scope, const test_function_t *tests) { + const test_function_t *test = NULL; Manager *m = NULL; int r; @@ -433,7 +433,7 @@ static int run_tests(UnitFileScope scope, test_function_t *tests) { } int main(int argc, char *argv[]) { - test_function_t user_tests[] = { + static const test_function_t user_tests[] = { test_exec_workingdirectory, test_exec_personality, test_exec_ignoresigpipe, @@ -464,7 +464,7 @@ int main(int argc, char *argv[]) { test_exec_spec_interpolation, NULL, }; - test_function_t system_tests[] = { + static const test_function_t system_tests[] = { test_exec_systemcall_system_mode_with_user, NULL, }; diff --git a/src/test/test-fs-util.c b/src/test/test-fs-util.c index 53a3cdc663..b502cd0ad1 100644 --- a/src/test/test-fs-util.c +++ b/src/test/test-fs-util.c @@ -60,66 +60,131 @@ static void test_chase_symlinks(void) { p = strjoina(temp, "/start"); assert_se(symlink("top/dot/dotdota", p) >= 0); - r = chase_symlinks(p, NULL, &result); - assert_se(r >= 0); + /* Paths that use symlinks underneath the "root" */ + + 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, &result); + r = chase_symlinks(p, 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)); + assert_se(mkdir(q, 0700) >= 0); - r = chase_symlinks(p, temp, &result); - assert_se(r >= 0); + r = chase_symlinks(p, temp, 0, &result); + assert_se(r > 0); assert_se(path_equal(result, q)); p = strjoina(temp, "/slash"); assert_se(symlink("/", p) >= 0); result = mfree(result); - r = chase_symlinks(p, NULL, &result); - assert_se(r >= 0); + r = chase_symlinks(p, NULL, 0, &result); + assert_se(r > 0); assert_se(path_equal(result, "/")); result = mfree(result); - r = chase_symlinks(p, temp, &result); - assert_se(r >= 0); + r = chase_symlinks(p, temp, 0, &result); + assert_se(r > 0); assert_se(path_equal(result, temp)); + /* 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)); + + 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)); + + 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)); + + /* Paths that contain repeated slashes */ + p = strjoina(temp, "/slashslash"); assert_se(symlink("///usr///", p) >= 0); result = mfree(result); - r = chase_symlinks(p, NULL, &result); - assert_se(r >= 0); + 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, &result); - assert_se(r >= 0); + r = chase_symlinks(p, temp, 0, &result); + assert_se(r > 0); assert_se(path_equal(result, q)); + /* Paths using . */ + result = mfree(result); - r = chase_symlinks("/etc/./.././", NULL, &result); - assert_se(r >= 0); + r = chase_symlinks("/etc/./.././", NULL, 0, &result); + assert_se(r > 0); assert_se(path_equal(result, "/")); result = mfree(result); - r = chase_symlinks("/etc/./.././", "/etc", &result); - assert_se(r == -EINVAL); + r = chase_symlinks("/etc/./.././", "/etc", 0, &result); + assert_se(r > 0 && path_equal(result, "/etc")); result = mfree(result); - r = chase_symlinks("/etc/machine-id/foo", NULL, &result); + r = chase_symlinks("/etc/machine-id/foo", NULL, 0, &result); assert_se(r == -ENOTDIR); + /* 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, &result); + r = chase_symlinks(p, NULL, 0, &result); assert_se(r == -ELOOP); + /* Path which doesn't exist */ + + p = strjoina(temp, "/idontexist"); + r = chase_symlinks(p, NULL, 0, &result); + assert_se(r == -ENOENT); + + r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result); + assert_se(r == 0); + assert_se(path_equal(result, p)); + result = mfree(result); + + p = strjoina(temp, "/idontexist/meneither"); + r = chase_symlinks(p, NULL, 0, &result); + assert_se(r == -ENOENT); + + r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result); + assert_se(r == 0); + assert_se(path_equal(result, p)); + result = mfree(result); + + /* Path which doesn't exist, but contains weird stuff */ + + p = strjoina(temp, "/idontexist/.."); + r = chase_symlinks(p, NULL, 0, &result); + assert_se(r == -ENOENT); + + r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result); + assert_se(r == -ENOENT); + assert_se(rm_rf(temp, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0); } diff --git a/src/test/test-hash.c b/src/test/test-hash.c new file mode 100644 index 0000000000..1972b94cfe --- /dev/null +++ b/src/test/test-hash.c @@ -0,0 +1,82 @@ +/*** + This file is part of systemd. + + Copyright 2016 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include "alloc-util.h" +#include "log.h" +#include "string-util.h" +#include "khash.h" + +int main(int argc, char *argv[]) { + _cleanup_(khash_unrefp) khash *h = NULL, *copy = NULL; + _cleanup_free_ char *s = NULL; + + log_set_max_level(LOG_DEBUG); + + assert_se(khash_new(&h, NULL) == -EINVAL); + assert_se(khash_new(&h, "") == -EINVAL); + assert_se(khash_new(&h, "foobar") == -EOPNOTSUPP); + + assert_se(khash_new(&h, "sha256") >= 0); + assert_se(khash_get_size(h) == 32); + assert_se(streq(khash_get_algorithm(h), "sha256")); + + assert_se(khash_digest_string(h, &s) >= 0); + assert_se(streq(s, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")); + s = mfree(s); + + assert_se(khash_put(h, "foobar", 6) >= 0); + assert_se(khash_digest_string(h, &s) >= 0); + assert_se(streq(s, "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2")); + s = mfree(s); + + assert_se(khash_put(h, "piep", 4) >= 0); + assert_se(khash_digest_string(h, &s) >= 0); + assert_se(streq(s, "f114d872b5ea075d3be9040d0b7a429514b3f9324a8e8e3dc3fb24c34ee56bea")); + s = mfree(s); + + assert_se(khash_put(h, "foo", 3) >= 0); + assert_se(khash_dup(h, ©) >= 0); + + assert_se(khash_put(h, "bar", 3) >= 0); + assert_se(khash_put(copy, "bar", 3) >= 0); + + assert_se(khash_digest_string(h, &s) >= 0); + assert_se(streq(s, "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2")); + s = mfree(s); + + assert_se(khash_digest_string(copy, &s) >= 0); + assert_se(streq(s, "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2")); + s = mfree(s); + + h = khash_unref(h); + + assert_se(khash_new_with_key(&h, "hmac(sha256)", "quux", 4) >= 0); + assert_se(khash_get_size(h) == 32); + assert_se(streq(khash_get_algorithm(h), "hmac(sha256)")); + + assert_se(khash_digest_string(h, &s) >= 0); + assert_se(streq(s, "abed9f8218ab473f77218a6a7d39abf1d21fa46d0700c4898e330ba88309d5ae")); + s = mfree(s); + + assert_se(khash_put(h, "foobar", 6) >= 0); + assert_se(khash_digest_string(h, &s) >= 0); + assert_se(streq(s, "33f6c70a60db66007d5325d5d1dea37c371354e5b83347a59ad339ce9f4ba3dc")); + + return 0; +} diff --git a/src/test/test-id128.c b/src/test/test-id128.c index 1c8e5549da..ab5a111ba9 100644 --- a/src/test/test-id128.c +++ b/src/test/test-id128.c @@ -153,5 +153,11 @@ int main(int argc, char *argv[]) { assert_se(id128_read_fd(fd, ID128_UUID, &id2) >= 0); assert_se(sd_id128_equal(id, id2)); + assert_se(sd_id128_get_machine_app_specific(SD_ID128_MAKE(f0,3d,aa,eb,1c,33,4b,43,a7,32,17,29,44,bf,77,2e), &id) >= 0); + assert_se(sd_id128_get_machine_app_specific(SD_ID128_MAKE(f0,3d,aa,eb,1c,33,4b,43,a7,32,17,29,44,bf,77,2e), &id2) >= 0); + assert_se(sd_id128_equal(id, id2)); + assert_se(sd_id128_get_machine_app_specific(SD_ID128_MAKE(51,df,0b,4b,c3,b0,4c,97,80,e2,99,b9,8c,a3,73,b8), &id2) >= 0); + assert_se(!sd_id128_equal(id, id2)); + return 0; } diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c index a6a09a0031..22df20a1eb 100644 --- a/src/test/test-path-util.c +++ b/src/test/test-path-util.c @@ -337,17 +337,17 @@ static void test_path_is_mount_point(void) { _cleanup_free_ char *dir1 = NULL, *dir1file = NULL, *dirlink1 = NULL, *dirlink1file = NULL; _cleanup_free_ char *dir2 = NULL, *dir2file = NULL; - assert_se(path_is_mount_point("/", AT_SYMLINK_FOLLOW) > 0); - assert_se(path_is_mount_point("/", 0) > 0); + assert_se(path_is_mount_point("/", NULL, AT_SYMLINK_FOLLOW) > 0); + assert_se(path_is_mount_point("/", NULL, 0) > 0); - assert_se(path_is_mount_point("/proc", AT_SYMLINK_FOLLOW) > 0); - assert_se(path_is_mount_point("/proc", 0) > 0); + assert_se(path_is_mount_point("/proc", NULL, AT_SYMLINK_FOLLOW) > 0); + assert_se(path_is_mount_point("/proc", NULL, 0) > 0); - assert_se(path_is_mount_point("/proc/1", AT_SYMLINK_FOLLOW) == 0); - assert_se(path_is_mount_point("/proc/1", 0) == 0); + assert_se(path_is_mount_point("/proc/1", NULL, AT_SYMLINK_FOLLOW) == 0); + assert_se(path_is_mount_point("/proc/1", NULL, 0) == 0); - assert_se(path_is_mount_point("/sys", AT_SYMLINK_FOLLOW) > 0); - assert_se(path_is_mount_point("/sys", 0) > 0); + assert_se(path_is_mount_point("/sys", NULL, AT_SYMLINK_FOLLOW) > 0); + assert_se(path_is_mount_point("/sys", NULL, 0) > 0); /* we'll create a hierarchy of different kinds of dir/file/link * layouts: @@ -381,10 +381,10 @@ static void test_path_is_mount_point(void) { assert_se(link1); assert_se(symlink("file2", link2) == 0); - assert_se(path_is_mount_point(file1, AT_SYMLINK_FOLLOW) == 0); - assert_se(path_is_mount_point(file1, 0) == 0); - assert_se(path_is_mount_point(link1, AT_SYMLINK_FOLLOW) == 0); - assert_se(path_is_mount_point(link1, 0) == 0); + assert_se(path_is_mount_point(file1, NULL, AT_SYMLINK_FOLLOW) == 0); + assert_se(path_is_mount_point(file1, NULL, 0) == 0); + assert_se(path_is_mount_point(link1, NULL, AT_SYMLINK_FOLLOW) == 0); + assert_se(path_is_mount_point(link1, NULL, 0) == 0); /* directory mountpoints */ dir1 = path_join(NULL, tmp_dir, "dir1"); @@ -400,10 +400,10 @@ static void test_path_is_mount_point(void) { assert_se(dir2); assert_se(mkdir(dir2, 0755) == 0); - assert_se(path_is_mount_point(dir1, AT_SYMLINK_FOLLOW) == 0); - assert_se(path_is_mount_point(dir1, 0) == 0); - assert_se(path_is_mount_point(dirlink1, AT_SYMLINK_FOLLOW) == 0); - assert_se(path_is_mount_point(dirlink1, 0) == 0); + assert_se(path_is_mount_point(dir1, NULL, AT_SYMLINK_FOLLOW) == 0); + assert_se(path_is_mount_point(dir1, NULL, 0) == 0); + assert_se(path_is_mount_point(dirlink1, NULL, AT_SYMLINK_FOLLOW) == 0); + assert_se(path_is_mount_point(dirlink1, NULL, 0) == 0); /* file in subdirectory mountpoints */ dir1file = path_join(NULL, dir1, "file"); @@ -412,10 +412,10 @@ static void test_path_is_mount_point(void) { assert_se(fd > 0); close(fd); - assert_se(path_is_mount_point(dir1file, AT_SYMLINK_FOLLOW) == 0); - assert_se(path_is_mount_point(dir1file, 0) == 0); - assert_se(path_is_mount_point(dirlink1file, AT_SYMLINK_FOLLOW) == 0); - assert_se(path_is_mount_point(dirlink1file, 0) == 0); + assert_se(path_is_mount_point(dir1file, NULL, AT_SYMLINK_FOLLOW) == 0); + assert_se(path_is_mount_point(dir1file, NULL, 0) == 0); + assert_se(path_is_mount_point(dirlink1file, NULL, AT_SYMLINK_FOLLOW) == 0); + assert_se(path_is_mount_point(dirlink1file, NULL, 0) == 0); /* these tests will only work as root */ if (mount(file1, file2, NULL, MS_BIND, NULL) >= 0) { @@ -423,10 +423,10 @@ static void test_path_is_mount_point(void) { /* files */ /* capture results in vars, to avoid dangling mounts on failure */ - rf = path_is_mount_point(file2, 0); - rt = path_is_mount_point(file2, AT_SYMLINK_FOLLOW); - rlf = path_is_mount_point(link2, 0); - rlt = path_is_mount_point(link2, AT_SYMLINK_FOLLOW); + rf = path_is_mount_point(file2, NULL, 0); + rt = path_is_mount_point(file2, NULL, AT_SYMLINK_FOLLOW); + rlf = path_is_mount_point(link2, NULL, 0); + rlt = path_is_mount_point(link2, NULL, AT_SYMLINK_FOLLOW); assert_se(umount(file2) == 0); @@ -444,13 +444,13 @@ static void test_path_is_mount_point(void) { assert_se(mount(dir2, dir1, NULL, MS_BIND, NULL) >= 0); - rf = path_is_mount_point(dir1, 0); - rt = path_is_mount_point(dir1, AT_SYMLINK_FOLLOW); - rlf = path_is_mount_point(dirlink1, 0); - rlt = path_is_mount_point(dirlink1, AT_SYMLINK_FOLLOW); + rf = path_is_mount_point(dir1, NULL, 0); + rt = path_is_mount_point(dir1, NULL, AT_SYMLINK_FOLLOW); + rlf = path_is_mount_point(dirlink1, NULL, 0); + rlt = path_is_mount_point(dirlink1, NULL, AT_SYMLINK_FOLLOW); /* its parent is a mount point, but not /file itself */ - rl1f = path_is_mount_point(dirlink1file, 0); - rl1t = path_is_mount_point(dirlink1file, AT_SYMLINK_FOLLOW); + rl1f = path_is_mount_point(dirlink1file, NULL, 0); + rl1t = path_is_mount_point(dirlink1file, NULL, AT_SYMLINK_FOLLOW); assert_se(umount(dir1) == 0); diff --git a/src/test/test-time.c b/src/test/test-time.c index 7078a0374d..8259491813 100644 --- a/src/test/test-time.c +++ b/src/test/test-time.c @@ -42,6 +42,10 @@ static void test_parse_sec(void) { assert_se(u == 2500 * USEC_PER_MSEC); assert_se(parse_sec(".7", &u) >= 0); assert_se(u == 700 * USEC_PER_MSEC); + assert_se(parse_sec("23us", &u) >= 0); + assert_se(u == 23); + assert_se(parse_sec("23µs", &u) >= 0); + assert_se(u == 23); assert_se(parse_sec("infinity", &u) >= 0); assert_se(u == USEC_INFINITY); assert_se(parse_sec(" infinity ", &u) >= 0); |