diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2023-04-16 19:39:58 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-16 19:39:58 +0900 |
commit | f384ce1187299978ae2a9c6d4f7eeff4f89dec9d (patch) | |
tree | 0215b0cda625be7735b8882f8e131e4b084de8f4 /src/test | |
parent | d8e75260e9667225638e32cd1e4cf897cb7b7adb (diff) | |
parent | 841834d9c358163308deb70642249e8b2ba76c1a (diff) | |
download | systemd-f384ce1187299978ae2a9c6d4f7eeff4f89dec9d.tar.gz |
Merge pull request #27283 from mrc0mmand/assorted-test-tweaks
test: a bunch of assorted tweaks, Saturday edition
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/meson.build | 1 | ||||
-rw-r--r-- | src/test/test-env-file.c | 20 | ||||
-rw-r--r-- | src/test/test-env-util.c | 35 | ||||
-rw-r--r-- | src/test/test-format-table.c | 43 | ||||
-rw-r--r-- | src/test/test-secure-bits.c | 84 | ||||
-rw-r--r-- | src/test/test-specifier.c | 27 |
6 files changed, 209 insertions, 1 deletions
diff --git a/src/test/meson.build b/src/test/meson.build index 85c3115e14..da843a7e47 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -141,6 +141,7 @@ simple_tests += files( 'test-rm-rf.c', 'test-sd-hwdb.c', 'test-sd-path.c', + 'test-secure-bits.c', 'test-selinux.c', 'test-serialize.c', 'test-set.c', diff --git a/src/test/test-env-file.c b/src/test/test-env-file.c index 461a0f0810..c8ec0e2278 100644 --- a/src/test/test-env-file.c +++ b/src/test/test-env-file.c @@ -58,7 +58,6 @@ "d= \" \\n\\t\\$\\`\\\\\n" \ "\" \n" - TEST(load_env_file_1) { _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX"; assert_se(write_tmpfile(name, env_file_1) == 0); @@ -129,6 +128,25 @@ TEST(load_env_file_6) { assert_se(data[4] == NULL); } +TEST(load_env_file_invalid_utf8) { + /* Test out a couple of assignments where the key/value has an invalid + * UTF-8 character ("noncharacter") + * + * See: https://en.wikipedia.org/wiki/Universal_Character_Set_characters#Non-characters + */ + FOREACH_STRING(s, + "fo\ufffeo=bar", + "foo=b\uffffar", + "baz=hello world\ufffe") { + _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX"; + assert_se(write_tmpfile(name, s) == 0); + + _cleanup_strv_free_ char **data = NULL; + assert_se(load_env_file(NULL, name, &data) == -EINVAL); + assert_se(!data); + } +} + TEST(write_and_load_env_file) { /* Make sure that our writer, parser and the shell agree on what our env var files mean */ diff --git a/src/test/test-env-util.c b/src/test/test-env-util.c index 8f2293b6c0..0f58d2fed0 100644 --- a/src/test/test-env-util.c +++ b/src/test/test-env-util.c @@ -484,4 +484,39 @@ TEST(strv_env_name_is_valid) { assert_se(!strv_env_name_is_valid(STRV_MAKE("HOME", "USER", "SHELL", "USER"))); } +TEST(getenv_path_list) { + _cleanup_strv_free_ char **path_list = NULL; + + /* Empty paths */ + FOREACH_STRING(s, "", ":", ":::::", " : ::: :: :") { + assert_se(setenv("TEST_GETENV_PATH_LIST", s, 1) >= 0); + assert_se(getenv_path_list("TEST_GETENV_PATH_LIST", &path_list) == -EINVAL); + assert_se(!path_list); + } + + /* Invalid paths */ + FOREACH_STRING(s, ".", "..", "/../", "/", "/foo/bar/baz/../foo", "foo/bar/baz") { + assert_se(setenv("TEST_GETENV_PATH_LIST", s, 1) >= 0); + assert_se(getenv_path_list("TEST_GETENV_PATH_LIST", &path_list) == -EINVAL); + assert_se(!path_list); + } + + /* Valid paths mixed with invalid ones */ + assert_se(setenv("TEST_GETENV_PATH_LIST", "/foo:/bar/baz:/../:/hello", 1) >= 0); + assert_se(getenv_path_list("TEST_GETENV_PATH_LIST", &path_list) == -EINVAL); + assert_se(!path_list); + + /* Finally some valid paths */ + assert_se(setenv("TEST_GETENV_PATH_LIST", "/foo:/bar/baz:/hello/world:/path with spaces:/final", 1) >= 0); + assert_se(getenv_path_list("TEST_GETENV_PATH_LIST", &path_list) >= 0); + assert_se(streq(path_list[0], "/foo")); + assert_se(streq(path_list[1], "/bar/baz")); + assert_se(streq(path_list[2], "/hello/world")); + assert_se(streq(path_list[3], "/path with spaces")); + assert_se(streq(path_list[4], "/final")); + assert_se(path_list[5] == NULL); + + assert_se(unsetenv("TEST_GETENV_PATH_LIST") >= 0); +} + DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-format-table.c b/src/test/test-format-table.c index a1bc8b99ac..7d544b18fd 100644 --- a/src/test/test-format-table.c +++ b/src/test/test-format-table.c @@ -583,6 +583,49 @@ TEST(path_basename) { assert_se(streq(formatted, "bar\nbar\nbaz\n")); } +TEST(dup_cell) { + _cleanup_(table_unrefp) Table *t = NULL; + _cleanup_free_ char *formatted = NULL; + + assert_se(t = table_new("foo", "bar", "x", "baz", ".", "%", "!", "~", "+")); + table_set_width(t, 75); + + assert_se(table_add_many(t, + TABLE_STRING, "hello", + TABLE_UINT8, UINT8_C(42), + TABLE_UINT16, UINT16_C(666), + TABLE_UINT32, UINT32_C(253), + TABLE_PERCENT, 0, + TABLE_PATH_BASENAME, "/foo/bar", + TABLE_STRING, "aaa", + TABLE_STRING, "bbb", + TABLE_STRING, "ccc") >= 0); + + /* Add the second row by duping cells */ + for (size_t i = 0; i < table_get_columns(t); i++) + assert_se(table_dup_cell(t, table_get_cell(t, 1, i)) >= 0); + + /* Another row, but dupe the last three strings from the same cell */ + assert_se(table_add_many(t, + TABLE_STRING, "aaa", + TABLE_UINT8, UINT8_C(0), + TABLE_UINT16, UINT16_C(65535), + TABLE_UINT32, UINT32_C(4294967295), + TABLE_PERCENT, 100, + TABLE_PATH_BASENAME, "../") >= 0); + + for (size_t i = 6; i < table_get_columns(t); i++) + assert_se(table_dup_cell(t, table_get_cell(t, 2, 0)) >= 0); + + assert_se(table_format(t, &formatted) >= 0); + printf("%s\n", formatted); + assert_se(streq(formatted, + "FOO BAR X BAZ . % ! ~ +\n" + "hello 42 666 253 0% bar aaa bbb ccc\n" + "hello 42 666 253 0% bar aaa bbb ccc\n" + "aaa 0 65535 4294967295 100% ../ hello hello hello\n")); +} + static int intro(void) { assert_se(setenv("SYSTEMD_COLORS", "0", 1) >= 0); assert_se(setenv("COLUMNS", "40", 1) >= 0); diff --git a/src/test/test-secure-bits.c b/src/test/test-secure-bits.c new file mode 100644 index 0000000000..d44bef6a09 --- /dev/null +++ b/src/test/test-secure-bits.c @@ -0,0 +1,84 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include <errno.h> + +#include "securebits-util.h" +#include "strv.h" +#include "tests.h" +#include "unit-file.h" + +static const char * const string_bits[] = { + "keep-caps", + "keep-caps-locked", + "no-setuid-fixup", + "no-setuid-fixup-locked", + "noroot", + "noroot-locked", + NULL +}; + +TEST(secure_bits_basic) { + _cleanup_free_ char *joined = NULL, *str = NULL; + int r; + + /* Check if converting each bit from string and back to string yields + * the same value */ + STRV_FOREACH(bit, string_bits) { + _cleanup_free_ char *s = NULL; + + r = secure_bits_from_string(*bit); + assert_se(r > 0); + assert_se(secure_bits_to_string_alloc(r, &s) >= 0); + printf("%s = 0x%x = %s\n", *bit, (unsigned)r, s); + assert_se(streq(*bit, s)); + } + + /* Ditto, but with all bits at once */ + joined = strv_join((char**)string_bits, " "); + assert_se(joined); + r = secure_bits_from_string(joined); + assert_se(r > 0); + assert_se(secure_bits_to_string_alloc(r, &str) >= 0); + printf("%s = 0x%x = %s\n", joined, (unsigned)r, str); + assert_se(streq(joined, str)); + + str = mfree(str); + + /* Empty string */ + assert_se(secure_bits_from_string("") == 0); + assert_se(secure_bits_from_string(" ") == 0); + + /* Only invalid entries */ + assert_se(secure_bits_from_string("foo bar baz") == 0); + + /* Empty secure bits */ + assert_se(secure_bits_to_string_alloc(0, &str) >= 0); + assert_se(isempty(str)); +} + +TEST(secure_bits_mix) { + static struct sbit_table { + const char *input; + const char *expected; + } sbit_table[] = { + { "keep-caps keep-caps keep-caps", "keep-caps" }, + { "keep-caps noroot keep-caps", "keep-caps noroot" }, + { "noroot foo bar baz noroot", "noroot" }, + { "noroot \"foo\" \"bar keep-caps", "noroot" }, + { "\"noroot foo\" bar keep-caps", "keep-caps" }, + {} + }; + + for (const struct sbit_table *s = sbit_table; s->input; s++) { + _cleanup_free_ char *str = NULL; + int r; + + r = secure_bits_from_string(s->input); + assert_se(r > 0); + assert_se(secure_bits_to_string_alloc(r, &str) >= 0); + printf("%s = 0x%x = %s\n", s->input, (unsigned)r, str); + assert_se(streq(s->expected, str)); + } +} + +DEFINE_TEST_MAIN(LOG_DEBUG); diff --git a/src/test/test-specifier.c b/src/test/test-specifier.c index 6d4093ec05..d2a7f922bd 100644 --- a/src/test/test-specifier.c +++ b/src/test/test-specifier.c @@ -1,5 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include "sd-id128.h" + #include "alloc-util.h" #include "log.h" #include "specifier.h" @@ -144,6 +146,31 @@ TEST(specifiers) { } } +/* Bunch of specifiers that are not part of the common lists */ +TEST(specifiers_assorted) { + const sd_id128_t id = SD_ID128_ALLF; + const uint64_t llu = UINT64_MAX; + const Specifier table[] = { + /* Used in src/partition/repart.c */ + { 'a', specifier_uuid, &id }, + { 'b', specifier_uint64, &llu }, + {} + }; + + for (const Specifier *s = table; s->specifier; s++) { + char spec[3]; + _cleanup_free_ char *resolved = NULL; + int r; + + xsprintf(spec, "%%%c", s->specifier); + + r = specifier_printf(spec, SIZE_MAX, table, NULL, NULL, &resolved); + assert_se(r >= 0); + + log_info("%%%c → %s", s->specifier, resolved); + } +} + TEST(specifiers_missing_data_ok) { _cleanup_free_ char *resolved = NULL; |