]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
d376cbb7 RC |
2 | |
3 | #include "alloc-util.h" | |
9a135c08 | 4 | #include "env-util.h" |
d376cbb7 RC |
5 | #include "log.h" |
6 | #include "macro.h" | |
7 | #include "proc-cmdline.h" | |
8 | #include "special.h" | |
9 | #include "string-util.h" | |
dcd61450 | 10 | #include "util.h" |
d376cbb7 | 11 | |
96287a49 ZJS |
12 | static int obj; |
13 | ||
14 | static int parse_item(const char *key, const char *value, void *data) { | |
d376cbb7 | 15 | assert_se(key); |
96287a49 | 16 | assert_se(data == &obj); |
d376cbb7 RC |
17 | |
18 | log_info("kernel cmdline option <%s> = <%s>", key, strna(value)); | |
19 | return 0; | |
20 | } | |
21 | ||
1d84ad94 | 22 | static void test_proc_cmdline_parse(void) { |
9a135c08 ZJS |
23 | log_info("/* %s */", __func__); |
24 | ||
25 | assert_se(proc_cmdline_parse(parse_item, &obj, PROC_CMDLINE_STRIP_RD_PREFIX) >= 0); | |
d376cbb7 RC |
26 | } |
27 | ||
9a135c08 ZJS |
28 | static void test_proc_cmdline_override(void) { |
29 | log_info("/* %s */", __func__); | |
dcd61450 | 30 | |
9a135c08 ZJS |
31 | assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=tuet zumm") == 0); |
32 | ||
33 | /* Test if the override works */ | |
34 | _cleanup_free_ char *line = NULL, *value = NULL; | |
35 | assert_se(proc_cmdline(&line) >= 0); | |
36 | ||
37 | /* Test if parsing makes uses of the override */ | |
38 | assert_se(streq(line, "foo_bar=quux wuff-piep=tuet zumm")); | |
39 | assert_se(proc_cmdline_get_key("foo_bar", 0, &value) > 0 && streq_ptr(value, "quux")); | |
40 | } | |
41 | ||
42 | static int parse_item_given(const char *key, const char *value, void *data) { | |
43 | assert_se(key); | |
44 | assert_se(data); | |
45 | ||
46 | bool *strip = data; | |
47 | ||
48 | log_info("%s: option <%s> = <%s>", __func__, key, strna(value)); | |
49 | if (streq(key, "foo_bar")) | |
50 | assert_se(streq(value, "quux")); | |
51 | else if (streq(key, "wuff-piep")) | |
52 | assert_se(streq(value, "tuet ")); | |
53 | else if (in_initrd() && *strip && streq(key, "zumm")) | |
54 | assert_se(!value); | |
55 | else if (in_initrd() && !*strip && streq(key, "rd.zumm")) | |
56 | assert_se(!value); | |
57 | else | |
58 | assert_not_reached("Bad key!"); | |
59 | ||
60 | return 0; | |
61 | } | |
62 | ||
63 | static void test_proc_cmdline_given(bool flip_initrd) { | |
64 | log_info("/* %s (flip: %s) */", __func__, yes_no(flip_initrd)); | |
65 | ||
66 | if (flip_initrd) | |
67 | in_initrd_force(!in_initrd()); | |
68 | ||
69 | bool t = true, f = false; | |
70 | assert_se(proc_cmdline_parse_given("foo_bar=quux wuff-piep=\"tuet \" rd.zumm", | |
71 | parse_item_given, &t, PROC_CMDLINE_STRIP_RD_PREFIX) >= 0); | |
72 | ||
73 | assert_se(proc_cmdline_parse_given("foo_bar=quux wuff-piep=\"tuet \" rd.zumm", | |
74 | parse_item_given, &f, 0) >= 0); | |
75 | ||
76 | ||
77 | if (flip_initrd) | |
78 | in_initrd_force(!in_initrd()); | |
d376cbb7 RC |
79 | } |
80 | ||
1d84ad94 LP |
81 | static void test_proc_cmdline_get_key(void) { |
82 | _cleanup_free_ char *value = NULL; | |
83 | ||
9a135c08 | 84 | log_info("/* %s */", __func__); |
120bffad | 85 | assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=tuet zumm") == 0); |
1d84ad94 LP |
86 | |
87 | assert_se(proc_cmdline_get_key("", 0, &value) == -EINVAL); | |
88 | assert_se(proc_cmdline_get_key("abc", 0, NULL) == 0); | |
89 | assert_se(proc_cmdline_get_key("abc", 0, &value) == 0 && value == NULL); | |
90 | assert_se(proc_cmdline_get_key("abc", PROC_CMDLINE_VALUE_OPTIONAL, &value) == 0 && value == NULL); | |
91 | ||
92 | assert_se(proc_cmdline_get_key("foo_bar", 0, &value) > 0 && streq_ptr(value, "quux")); | |
93 | value = mfree(value); | |
94 | assert_se(proc_cmdline_get_key("foo_bar", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "quux")); | |
95 | value = mfree(value); | |
96 | assert_se(proc_cmdline_get_key("foo-bar", 0, &value) > 0 && streq_ptr(value, "quux")); | |
97 | value = mfree(value); | |
98 | assert_se(proc_cmdline_get_key("foo-bar", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "quux")); | |
99 | value = mfree(value); | |
100 | assert_se(proc_cmdline_get_key("foo-bar", 0, NULL) == 0); | |
101 | assert_se(proc_cmdline_get_key("foo-bar", PROC_CMDLINE_VALUE_OPTIONAL, NULL) == -EINVAL); | |
102 | ||
103 | assert_se(proc_cmdline_get_key("wuff-piep", 0, &value) > 0 && streq_ptr(value, "tuet")); | |
104 | value = mfree(value); | |
105 | assert_se(proc_cmdline_get_key("wuff-piep", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "tuet")); | |
106 | value = mfree(value); | |
107 | assert_se(proc_cmdline_get_key("wuff_piep", 0, &value) > 0 && streq_ptr(value, "tuet")); | |
108 | value = mfree(value); | |
109 | assert_se(proc_cmdline_get_key("wuff_piep", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "tuet")); | |
110 | value = mfree(value); | |
111 | assert_se(proc_cmdline_get_key("wuff_piep", 0, NULL) == 0); | |
112 | assert_se(proc_cmdline_get_key("wuff_piep", PROC_CMDLINE_VALUE_OPTIONAL, NULL) == -EINVAL); | |
113 | ||
114 | assert_se(proc_cmdline_get_key("zumm", 0, &value) == 0 && value == NULL); | |
115 | assert_se(proc_cmdline_get_key("zumm", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && value == NULL); | |
116 | assert_se(proc_cmdline_get_key("zumm", 0, NULL) > 0); | |
117 | } | |
118 | ||
119 | static void test_proc_cmdline_get_bool(void) { | |
120 | bool value = false; | |
121 | ||
9a135c08 | 122 | log_info("/* %s */", __func__); |
120bffad | 123 | assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar bar-waldo=1 x_y-z=0 quux=miep") == 0); |
1d84ad94 LP |
124 | |
125 | assert_se(proc_cmdline_get_bool("", &value) == -EINVAL); | |
126 | assert_se(proc_cmdline_get_bool("abc", &value) == 0 && value == false); | |
127 | assert_se(proc_cmdline_get_bool("foo_bar", &value) > 0 && value == true); | |
128 | assert_se(proc_cmdline_get_bool("foo-bar", &value) > 0 && value == true); | |
129 | assert_se(proc_cmdline_get_bool("bar-waldo", &value) > 0 && value == true); | |
130 | assert_se(proc_cmdline_get_bool("bar_waldo", &value) > 0 && value == true); | |
131 | assert_se(proc_cmdline_get_bool("x_y-z", &value) > 0 && value == false); | |
132 | assert_se(proc_cmdline_get_bool("x-y-z", &value) > 0 && value == false); | |
133 | assert_se(proc_cmdline_get_bool("x-y_z", &value) > 0 && value == false); | |
134 | assert_se(proc_cmdline_get_bool("x_y_z", &value) > 0 && value == false); | |
135 | assert_se(proc_cmdline_get_bool("quux", &value) == -EINVAL && value == false); | |
136 | } | |
137 | ||
78b30ee0 LP |
138 | static void test_proc_cmdline_get_key_many(void) { |
139 | _cleanup_free_ char *value1 = NULL, *value2 = NULL, *value3 = NULL, *value4 = NULL; | |
140 | ||
141 | log_info("/* %s */", __func__); | |
142 | assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=tuet zumm") == 0); | |
143 | ||
144 | assert_se(proc_cmdline_get_key_many(0, | |
145 | "wuff-piep", &value3, | |
146 | "foo_bar", &value1, | |
147 | "idontexist", &value2, | |
148 | "zumm", &value4) == 2); | |
149 | ||
150 | assert_se(streq_ptr(value1, "quux")); | |
151 | assert_se(!value2); | |
152 | assert_se(streq_ptr(value3, "tuet")); | |
153 | assert_se(!value4); | |
154 | } | |
155 | ||
1d84ad94 | 156 | static void test_proc_cmdline_key_streq(void) { |
9a135c08 | 157 | log_info("/* %s */", __func__); |
1d84ad94 LP |
158 | |
159 | assert_se(proc_cmdline_key_streq("", "")); | |
160 | assert_se(proc_cmdline_key_streq("a", "a")); | |
161 | assert_se(!proc_cmdline_key_streq("", "a")); | |
162 | assert_se(!proc_cmdline_key_streq("a", "")); | |
163 | assert_se(proc_cmdline_key_streq("a", "a")); | |
164 | assert_se(!proc_cmdline_key_streq("a", "b")); | |
165 | assert_se(proc_cmdline_key_streq("x-y-z", "x-y-z")); | |
166 | assert_se(proc_cmdline_key_streq("x-y-z", "x_y_z")); | |
167 | assert_se(proc_cmdline_key_streq("x-y-z", "x-y_z")); | |
168 | assert_se(proc_cmdline_key_streq("x-y-z", "x_y-z")); | |
169 | assert_se(proc_cmdline_key_streq("x_y-z", "x-y_z")); | |
170 | assert_se(!proc_cmdline_key_streq("x_y-z", "x-z_z")); | |
171 | } | |
172 | ||
173 | static void test_proc_cmdline_key_startswith(void) { | |
9a135c08 | 174 | log_info("/* %s */", __func__); |
1d84ad94 LP |
175 | |
176 | assert_se(proc_cmdline_key_startswith("", "")); | |
177 | assert_se(proc_cmdline_key_startswith("x", "")); | |
178 | assert_se(!proc_cmdline_key_startswith("", "x")); | |
179 | assert_se(proc_cmdline_key_startswith("x", "x")); | |
180 | assert_se(!proc_cmdline_key_startswith("x", "y")); | |
181 | assert_se(!proc_cmdline_key_startswith("foo-bar", "quux")); | |
182 | assert_se(proc_cmdline_key_startswith("foo-bar", "foo")); | |
183 | assert_se(proc_cmdline_key_startswith("foo-bar", "foo-bar")); | |
184 | assert_se(proc_cmdline_key_startswith("foo-bar", "foo_bar")); | |
185 | assert_se(proc_cmdline_key_startswith("foo-bar", "foo_")); | |
186 | assert_se(!proc_cmdline_key_startswith("foo-bar", "foo_xx")); | |
187 | } | |
188 | ||
9a135c08 ZJS |
189 | static void test_runlevel_to_target(void) { |
190 | log_info("/* %s */", __func__); | |
191 | ||
192 | in_initrd_force(false); | |
193 | assert_se(streq_ptr(runlevel_to_target(NULL), NULL)); | |
194 | assert_se(streq_ptr(runlevel_to_target("unknown-runlevel"), NULL)); | |
195 | assert_se(streq_ptr(runlevel_to_target("rd.unknown-runlevel"), NULL)); | |
196 | assert_se(streq_ptr(runlevel_to_target("3"), SPECIAL_MULTI_USER_TARGET)); | |
197 | assert_se(streq_ptr(runlevel_to_target("rd.rescue"), NULL)); | |
198 | ||
199 | in_initrd_force(true); | |
200 | assert_se(streq_ptr(runlevel_to_target(NULL), NULL)); | |
201 | assert_se(streq_ptr(runlevel_to_target("unknown-runlevel"), NULL)); | |
202 | assert_se(streq_ptr(runlevel_to_target("rd.unknown-runlevel"), NULL)); | |
203 | assert_se(streq_ptr(runlevel_to_target("3"), NULL)); | |
204 | assert_se(streq_ptr(runlevel_to_target("rd.rescue"), SPECIAL_RESCUE_TARGET)); | |
205 | } | |
206 | ||
d376cbb7 RC |
207 | int main(void) { |
208 | log_parse_environment(); | |
209 | log_open(); | |
210 | ||
1d84ad94 | 211 | test_proc_cmdline_parse(); |
9a135c08 ZJS |
212 | test_proc_cmdline_override(); |
213 | test_proc_cmdline_given(false); | |
214 | /* Repeat the same thing, but now flip our ininitrdness */ | |
215 | test_proc_cmdline_given(true); | |
1d84ad94 LP |
216 | test_proc_cmdline_key_streq(); |
217 | test_proc_cmdline_key_startswith(); | |
218 | test_proc_cmdline_get_key(); | |
219 | test_proc_cmdline_get_bool(); | |
78b30ee0 | 220 | test_proc_cmdline_get_key_many(); |
d376cbb7 RC |
221 | test_runlevel_to_target(); |
222 | ||
223 | return 0; | |
224 | } |