diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2023-04-04 04:39:33 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2023-04-04 04:46:18 +0200 |
commit | 9853542dd396ff7c294b21aed72dc9316152b6e7 (patch) | |
tree | 866f277158f959aef74bf1005b32e1e03dba4e3f /src/test/test-id128.c | |
parent | sd-id128: introduce id128_write_at() (diff) | |
download | systemd-9853542dd396ff7c294b21aed72dc9316152b6e7.tar.xz systemd-9853542dd396ff7c294b21aed72dc9316152b6e7.zip |
test: add tests for id128_{read,write}_at()
Diffstat (limited to 'src/test/test-id128.c')
-rw-r--r-- | src/test/test-id128.c | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/test/test-id128.c b/src/test/test-id128.c index 0a97366b6d..75c3582734 100644 --- a/src/test/test-id128.c +++ b/src/test/test-id128.c @@ -11,6 +11,8 @@ #include "fd-util.h" #include "id128-util.h" #include "macro.h" +#include "path-util.h" +#include "rm-rf.h" #include "string-util.h" #include "tests.h" #include "tmpfile-util.h" @@ -215,4 +217,76 @@ TEST(benchmark_sd_id128_get_machine_app_specific) { log_info("%lf µs each\n", (double) q / iterations); } +TEST(id128_at) { + _cleanup_(rm_rf_physical_and_freep) char *t = NULL; + _cleanup_close_ int tfd = -EBADF; + _cleanup_free_ char *p = NULL; + sd_id128_t id, i; + + tfd = mkdtemp_open(NULL, O_PATH, &t); + assert_se(tfd >= 0); + assert_se(mkdirat(tfd, "etc", 0755) >= 0); + assert_se(symlinkat("etc", tfd, "etc2") >= 0); + assert_se(symlinkat("machine-id", tfd, "etc/hoge-id") >= 0); + + assert_se(sd_id128_randomize(&id) == 0); + + assert_se(id128_write_at(tfd, "etc/machine-id", ID128_FORMAT_PLAIN, id) >= 0); + if (geteuid() == 0) + assert_se(id128_write_at(tfd, "etc/machine-id", ID128_FORMAT_PLAIN, id) >= 0); + else + assert_se(id128_write_at(tfd, "etc/machine-id", ID128_FORMAT_PLAIN, id) == -EACCES); + assert_se(unlinkat(tfd, "etc/machine-id", 0) >= 0); + assert_se(id128_write_at(tfd, "etc2/machine-id", ID128_FORMAT_PLAIN, id) >= 0); + assert_se(unlinkat(tfd, "etc/machine-id", 0) >= 0); + assert_se(id128_write_at(tfd, "etc/hoge-id", ID128_FORMAT_PLAIN, id) >= 0); + assert_se(unlinkat(tfd, "etc/machine-id", 0) >= 0); + assert_se(id128_write_at(tfd, "etc2/hoge-id", ID128_FORMAT_PLAIN, id) >= 0); + + /* id128_read_at() */ + i = SD_ID128_NULL; /* Not necessary in real code, but for testing that the id is really assigned. */ + assert_se(id128_read_at(tfd, "etc/machine-id", ID128_FORMAT_PLAIN, &i) >= 0); + assert_se(sd_id128_equal(id, i)); + + i = SD_ID128_NULL; + assert_se(id128_read_at(tfd, "etc2/machine-id", ID128_FORMAT_PLAIN, &i) >= 0); + assert_se(sd_id128_equal(id, i)); + + i = SD_ID128_NULL; + assert_se(id128_read_at(tfd, "etc/hoge-id", ID128_FORMAT_PLAIN, &i) >= 0); + assert_se(sd_id128_equal(id, i)); + + i = SD_ID128_NULL; + assert_se(id128_read_at(tfd, "etc2/hoge-id", ID128_FORMAT_PLAIN, &i) >= 0); + assert_se(sd_id128_equal(id, i)); + + /* id128_read() */ + assert_se(p = path_join(t, "/etc/machine-id")); + + i = SD_ID128_NULL; + assert_se(id128_read(p, ID128_FORMAT_PLAIN, &i) >= 0); + assert_se(sd_id128_equal(id, i)); + + free(p); + assert_se(p = path_join(t, "/etc2/machine-id")); + + i = SD_ID128_NULL; + assert_se(id128_read(p, ID128_FORMAT_PLAIN, &i) >= 0); + assert_se(sd_id128_equal(id, i)); + + free(p); + assert_se(p = path_join(t, "/etc/hoge-id")); + + i = SD_ID128_NULL; + assert_se(id128_read(p, ID128_FORMAT_PLAIN, &i) >= 0); + assert_se(sd_id128_equal(id, i)); + + free(p); + assert_se(p = path_join(t, "/etc2/hoge-id")); + + i = SD_ID128_NULL; + assert_se(id128_read(p, ID128_FORMAT_PLAIN, &i) >= 0); + assert_se(sd_id128_equal(id, i)); +} + DEFINE_TEST_MAIN(LOG_INFO); |