diff options
author | Mark Hindley <mark@hindley.org.uk> | 2018-11-12 09:22:09 +0000 |
---|---|---|
committer | Mark Hindley <mark@hindley.org.uk> | 2018-11-12 09:22:09 +0000 |
commit | a7bee29ae34de50a86bf64d4ea9f69bdbc3b27e4 (patch) | |
tree | ac6c9abfefdc4286c4f52167738c4e4d35c96cd4 /src/test/test-proc-cmdline.c | |
parent | 98f0f1042d219289b880436fad42823433648d18 (diff) | |
parent | 2cc17d30309a1db16cccbf376a59ae40e47b6959 (diff) |
Merge branch 'merge_upstream' into debian_WIP
Diffstat (limited to 'src/test/test-proc-cmdline.c')
-rw-r--r-- | src/test/test-proc-cmdline.c | 96 |
1 files changed, 82 insertions, 14 deletions
diff --git a/src/test/test-proc-cmdline.c b/src/test/test-proc-cmdline.c index 275c377cc..d77c7f641 100644 --- a/src/test/test-proc-cmdline.c +++ b/src/test/test-proc-cmdline.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #include "alloc-util.h" +//#include "env-util.h" #include "log.h" #include "macro.h" #include "proc-cmdline.h" @@ -19,29 +20,69 @@ static int parse_item(const char *key, const char *value, void *data) { } static void test_proc_cmdline_parse(void) { - assert_se(proc_cmdline_parse(parse_item, &obj, true) >= 0); + log_info("/* %s */", __func__); + + assert_se(proc_cmdline_parse(parse_item, &obj, PROC_CMDLINE_STRIP_RD_PREFIX) >= 0); } #if 0 /// UNNEEDED by elogind -static void test_runlevel_to_target(void) { - in_initrd_force(false); - assert_se(streq_ptr(runlevel_to_target(NULL), NULL)); - assert_se(streq_ptr(runlevel_to_target("unknown-runlevel"), NULL)); - assert_se(streq_ptr(runlevel_to_target("rd.unknown-runlevel"), NULL)); - assert_se(streq_ptr(runlevel_to_target("3"), SPECIAL_MULTI_USER_TARGET)); - assert_se(streq_ptr(runlevel_to_target("rd.rescue"), NULL)); +static void test_proc_cmdline_override(void) { + log_info("/* %s */", __func__); - in_initrd_force(true); - assert_se(streq_ptr(runlevel_to_target(NULL), NULL)); - assert_se(streq_ptr(runlevel_to_target("unknown-runlevel"), NULL)); - assert_se(streq_ptr(runlevel_to_target("rd.unknown-runlevel"), NULL)); - assert_se(streq_ptr(runlevel_to_target("3"), NULL)); - assert_se(streq_ptr(runlevel_to_target("rd.rescue"), SPECIAL_RESCUE_TARGET)); + assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=tuet zumm") == 0); + + /* Test if the override works */ + _cleanup_free_ char *line = NULL, *value = NULL; + assert_se(proc_cmdline(&line) >= 0); + + /* Test if parsing makes uses of the override */ + assert_se(streq(line, "foo_bar=quux wuff-piep=tuet zumm")); + assert_se(proc_cmdline_get_key("foo_bar", 0, &value) > 0 && streq_ptr(value, "quux")); +} + +static int parse_item_given(const char *key, const char *value, void *data) { + assert_se(key); + assert_se(data); + + bool *strip = data; + + log_info("%s: option <%s> = <%s>", __func__, key, strna(value)); + if (streq(key, "foo_bar")) + assert_se(streq(value, "quux")); + else if (streq(key, "wuff-piep")) + assert_se(streq(value, "tuet ")); + else if (in_initrd() && *strip && streq(key, "zumm")) + assert_se(!value); + else if (in_initrd() && !*strip && streq(key, "rd.zumm")) + assert_se(!value); + else + assert_not_reached("Bad key!"); + + return 0; +} + +static void test_proc_cmdline_given(bool flip_initrd) { + log_info("/* %s (flip: %s) */", __func__, yes_no(flip_initrd)); + + if (flip_initrd) + in_initrd_force(!in_initrd()); + + bool t = true, f = false; + assert_se(proc_cmdline_parse_given("foo_bar=quux wuff-piep=\"tuet \" rd.zumm", + parse_item_given, &t, PROC_CMDLINE_STRIP_RD_PREFIX) >= 0); + + assert_se(proc_cmdline_parse_given("foo_bar=quux wuff-piep=\"tuet \" rd.zumm", + parse_item_given, &f, 0) >= 0); + + + if (flip_initrd) + in_initrd_force(!in_initrd()); } static void test_proc_cmdline_get_key(void) { _cleanup_free_ char *value = NULL; + log_info("/* %s */", __func__); putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=tuet zumm"); assert_se(proc_cmdline_get_key("", 0, &value) == -EINVAL); @@ -79,6 +120,7 @@ static void test_proc_cmdline_get_key(void) { static void test_proc_cmdline_get_bool(void) { bool value = false; + log_info("/* %s */", __func__); putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar bar-waldo=1 x_y-z=0 quux=miep"); assert_se(proc_cmdline_get_bool("", &value) == -EINVAL); @@ -96,6 +138,7 @@ static void test_proc_cmdline_get_bool(void) { #endif // 0 static void test_proc_cmdline_key_streq(void) { + log_info("/* %s */", __func__); assert_se(proc_cmdline_key_streq("", "")); assert_se(proc_cmdline_key_streq("a", "a")); @@ -113,6 +156,7 @@ static void test_proc_cmdline_key_streq(void) { #if 0 /// UNNEEDED by elogind static void test_proc_cmdline_key_startswith(void) { + log_info("/* %s */", __func__); assert_se(proc_cmdline_key_startswith("", "")); assert_se(proc_cmdline_key_startswith("x", "")); @@ -126,6 +170,24 @@ static void test_proc_cmdline_key_startswith(void) { assert_se(proc_cmdline_key_startswith("foo-bar", "foo_")); assert_se(!proc_cmdline_key_startswith("foo-bar", "foo_xx")); } + +static void test_runlevel_to_target(void) { + log_info("/* %s */", __func__); + + in_initrd_force(false); + assert_se(streq_ptr(runlevel_to_target(NULL), NULL)); + assert_se(streq_ptr(runlevel_to_target("unknown-runlevel"), NULL)); + assert_se(streq_ptr(runlevel_to_target("rd.unknown-runlevel"), NULL)); + assert_se(streq_ptr(runlevel_to_target("3"), SPECIAL_MULTI_USER_TARGET)); + assert_se(streq_ptr(runlevel_to_target("rd.rescue"), NULL)); + + in_initrd_force(true); + assert_se(streq_ptr(runlevel_to_target(NULL), NULL)); + assert_se(streq_ptr(runlevel_to_target("unknown-runlevel"), NULL)); + assert_se(streq_ptr(runlevel_to_target("rd.unknown-runlevel"), NULL)); + assert_se(streq_ptr(runlevel_to_target("3"), NULL)); + assert_se(streq_ptr(runlevel_to_target("rd.rescue"), SPECIAL_RESCUE_TARGET)); +} #endif // 0 int main(void) { @@ -133,6 +195,12 @@ int main(void) { log_open(); test_proc_cmdline_parse(); +#if 0 /// UNNEEDED by elogind + test_proc_cmdline_override(); + test_proc_cmdline_given(false); + /* Repeat the same thing, but now flip our ininitrdness */ + test_proc_cmdline_given(true); +#endif // 0 test_proc_cmdline_key_streq(); #if 0 /// UNNEEDED by elogind test_proc_cmdline_key_startswith(); |