2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include "alloc-util.h"
23 #include "proc-cmdline.h"
25 #include "string-util.h"
30 static int parse_item(const char *key
, const char *value
, void *data
) {
32 assert_se(data
== &obj
);
34 log_info("kernel cmdline option <%s> = <%s>", key
, strna(value
));
38 static void test_proc_cmdline_parse(void) {
39 assert_se(proc_cmdline_parse(parse_item
, &obj
, true) >= 0);
42 static void test_runlevel_to_target(void) {
43 in_initrd_force(false);
44 assert_se(streq_ptr(runlevel_to_target(NULL
), NULL
));
45 assert_se(streq_ptr(runlevel_to_target("unknown-runlevel"), NULL
));
46 assert_se(streq_ptr(runlevel_to_target("rd.unknown-runlevel"), NULL
));
47 assert_se(streq_ptr(runlevel_to_target("3"), SPECIAL_MULTI_USER_TARGET
));
48 assert_se(streq_ptr(runlevel_to_target("rd.rescue"), NULL
));
50 in_initrd_force(true);
51 assert_se(streq_ptr(runlevel_to_target(NULL
), NULL
));
52 assert_se(streq_ptr(runlevel_to_target("unknown-runlevel"), NULL
));
53 assert_se(streq_ptr(runlevel_to_target("rd.unknown-runlevel"), NULL
));
54 assert_se(streq_ptr(runlevel_to_target("3"), NULL
));
55 assert_se(streq_ptr(runlevel_to_target("rd.rescue"), SPECIAL_RESCUE_TARGET
));
58 static void test_proc_cmdline_get_key(void) {
59 _cleanup_free_
char *value
= NULL
;
61 putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=tuet zumm");
63 assert_se(proc_cmdline_get_key("", 0, &value
) == -EINVAL
);
64 assert_se(proc_cmdline_get_key("abc", 0, NULL
) == 0);
65 assert_se(proc_cmdline_get_key("abc", 0, &value
) == 0 && value
== NULL
);
66 assert_se(proc_cmdline_get_key("abc", PROC_CMDLINE_VALUE_OPTIONAL
, &value
) == 0 && value
== NULL
);
68 assert_se(proc_cmdline_get_key("foo_bar", 0, &value
) > 0 && streq_ptr(value
, "quux"));
70 assert_se(proc_cmdline_get_key("foo_bar", PROC_CMDLINE_VALUE_OPTIONAL
, &value
) > 0 && streq_ptr(value
, "quux"));
72 assert_se(proc_cmdline_get_key("foo-bar", 0, &value
) > 0 && streq_ptr(value
, "quux"));
74 assert_se(proc_cmdline_get_key("foo-bar", PROC_CMDLINE_VALUE_OPTIONAL
, &value
) > 0 && streq_ptr(value
, "quux"));
76 assert_se(proc_cmdline_get_key("foo-bar", 0, NULL
) == 0);
77 assert_se(proc_cmdline_get_key("foo-bar", PROC_CMDLINE_VALUE_OPTIONAL
, NULL
) == -EINVAL
);
79 assert_se(proc_cmdline_get_key("wuff-piep", 0, &value
) > 0 && streq_ptr(value
, "tuet"));
81 assert_se(proc_cmdline_get_key("wuff-piep", PROC_CMDLINE_VALUE_OPTIONAL
, &value
) > 0 && streq_ptr(value
, "tuet"));
83 assert_se(proc_cmdline_get_key("wuff_piep", 0, &value
) > 0 && streq_ptr(value
, "tuet"));
85 assert_se(proc_cmdline_get_key("wuff_piep", PROC_CMDLINE_VALUE_OPTIONAL
, &value
) > 0 && streq_ptr(value
, "tuet"));
87 assert_se(proc_cmdline_get_key("wuff_piep", 0, NULL
) == 0);
88 assert_se(proc_cmdline_get_key("wuff_piep", PROC_CMDLINE_VALUE_OPTIONAL
, NULL
) == -EINVAL
);
90 assert_se(proc_cmdline_get_key("zumm", 0, &value
) == 0 && value
== NULL
);
91 assert_se(proc_cmdline_get_key("zumm", PROC_CMDLINE_VALUE_OPTIONAL
, &value
) > 0 && value
== NULL
);
92 assert_se(proc_cmdline_get_key("zumm", 0, NULL
) > 0);
95 static void test_proc_cmdline_get_bool(void) {
98 putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar bar-waldo=1 x_y-z=0 quux=miep");
100 assert_se(proc_cmdline_get_bool("", &value
) == -EINVAL
);
101 assert_se(proc_cmdline_get_bool("abc", &value
) == 0 && value
== false);
102 assert_se(proc_cmdline_get_bool("foo_bar", &value
) > 0 && value
== true);
103 assert_se(proc_cmdline_get_bool("foo-bar", &value
) > 0 && value
== true);
104 assert_se(proc_cmdline_get_bool("bar-waldo", &value
) > 0 && value
== true);
105 assert_se(proc_cmdline_get_bool("bar_waldo", &value
) > 0 && value
== true);
106 assert_se(proc_cmdline_get_bool("x_y-z", &value
) > 0 && value
== false);
107 assert_se(proc_cmdline_get_bool("x-y-z", &value
) > 0 && value
== false);
108 assert_se(proc_cmdline_get_bool("x-y_z", &value
) > 0 && value
== false);
109 assert_se(proc_cmdline_get_bool("x_y_z", &value
) > 0 && value
== false);
110 assert_se(proc_cmdline_get_bool("quux", &value
) == -EINVAL
&& value
== false);
113 static void test_proc_cmdline_key_streq(void) {
115 assert_se(proc_cmdline_key_streq("", ""));
116 assert_se(proc_cmdline_key_streq("a", "a"));
117 assert_se(!proc_cmdline_key_streq("", "a"));
118 assert_se(!proc_cmdline_key_streq("a", ""));
119 assert_se(proc_cmdline_key_streq("a", "a"));
120 assert_se(!proc_cmdline_key_streq("a", "b"));
121 assert_se(proc_cmdline_key_streq("x-y-z", "x-y-z"));
122 assert_se(proc_cmdline_key_streq("x-y-z", "x_y_z"));
123 assert_se(proc_cmdline_key_streq("x-y-z", "x-y_z"));
124 assert_se(proc_cmdline_key_streq("x-y-z", "x_y-z"));
125 assert_se(proc_cmdline_key_streq("x_y-z", "x-y_z"));
126 assert_se(!proc_cmdline_key_streq("x_y-z", "x-z_z"));
129 static void test_proc_cmdline_key_startswith(void) {
131 assert_se(proc_cmdline_key_startswith("", ""));
132 assert_se(proc_cmdline_key_startswith("x", ""));
133 assert_se(!proc_cmdline_key_startswith("", "x"));
134 assert_se(proc_cmdline_key_startswith("x", "x"));
135 assert_se(!proc_cmdline_key_startswith("x", "y"));
136 assert_se(!proc_cmdline_key_startswith("foo-bar", "quux"));
137 assert_se(proc_cmdline_key_startswith("foo-bar", "foo"));
138 assert_se(proc_cmdline_key_startswith("foo-bar", "foo-bar"));
139 assert_se(proc_cmdline_key_startswith("foo-bar", "foo_bar"));
140 assert_se(proc_cmdline_key_startswith("foo-bar", "foo_"));
141 assert_se(!proc_cmdline_key_startswith("foo-bar", "foo_xx"));
145 log_parse_environment();
148 test_proc_cmdline_parse();
149 test_proc_cmdline_key_streq();
150 test_proc_cmdline_key_startswith();
151 test_proc_cmdline_get_key();
152 test_proc_cmdline_get_bool();
153 test_runlevel_to_target();