]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-proc-cmdline.c
4fd91253781c6695bb8a72c56679da76c60591f3
[thirdparty/systemd.git] / src / test / test-proc-cmdline.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "env-util.h"
5 #include "errno-util.h"
6 #include "initrd-util.h"
7 #include "log.h"
8 #include "macro.h"
9 #include "nulstr-util.h"
10 #include "proc-cmdline.h"
11 #include "process-util.h"
12 #include "special.h"
13 #include "string-util.h"
14 #include "strv.h"
15 #include "tests.h"
16
17 static int obj;
18
19 static int parse_item(const char *key, const char *value, void *data) {
20 assert_se(key);
21 assert_se(data == &obj);
22
23 log_info("kernel cmdline option <%s> = <%s>", key, strna(value));
24 return 0;
25 }
26
27 TEST(proc_cmdline_parse) {
28 assert_se(proc_cmdline_parse(parse_item, &obj, PROC_CMDLINE_STRIP_RD_PREFIX) >= 0);
29 }
30
31 TEST(proc_cmdline_override) {
32 _cleanup_free_ char *line = NULL, *value = NULL;
33 _cleanup_strv_free_ char **args = NULL;
34
35 assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=tuet zumm some_arg_with_space='foo bar' and_one_more=\"zzz aaa\"") == 0);
36 assert_se(putenv((char*) "SYSTEMD_EFI_OPTIONS=different") == 0);
37
38 /* First test if the overrides for /proc/cmdline still work */
39 assert_se(proc_cmdline(&line) >= 0);
40 assert_se(streq(line, "foo_bar=quux wuff-piep=tuet zumm some_arg_with_space='foo bar' and_one_more=\"zzz aaa\""));
41 line = mfree(line);
42 assert_se(proc_cmdline_strv(&args) >= 0);
43 assert_se(strv_equal(args, STRV_MAKE("foo_bar=quux", "wuff-piep=tuet", "zumm", "some_arg_with_space=foo bar", "and_one_more=zzz aaa")));
44 args = strv_free(args);
45
46 /* Test if parsing makes uses of the override */
47 assert_se(proc_cmdline_get_key("foo_bar", 0, &value) > 0 && streq_ptr(value, "quux"));
48 value = mfree(value);
49
50 assert_se(proc_cmdline_get_key("some_arg_with_space", 0, &value) > 0 && streq_ptr(value, "foo bar"));
51 value = mfree(value);
52
53 assert_se(proc_cmdline_get_key("and_one_more", 0, &value) > 0 && streq_ptr(value, "zzz aaa"));
54 value = mfree(value);
55
56 assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=hoge") == 0);
57 assert_se(putenv((char*) "SYSTEMD_EFI_OPTIONS=foo_bar=quux wuff-piep=tuet zumm some_arg_with_space='foo bar' and_one_more=\"zzz aaa\"") == 0);
58
59 assert_se(proc_cmdline(&line) >= 0);
60 assert_se(streq(line, "hoge"));
61 line = mfree(line);
62 assert_se(proc_cmdline_strv(&args) >= 0);
63 assert_se(strv_equal(args, STRV_MAKE("hoge")));
64 args = strv_free(args);
65
66 #if ENABLE_EFI
67 assert_se(proc_cmdline_get_key("foo_bar", 0, &value) > 0 && streq_ptr(value, "quux"));
68 value = mfree(value);
69
70 assert_se(proc_cmdline_get_key("some_arg_with_space", 0, &value) > 0 && streq_ptr(value, "foo bar"));
71 value = mfree(value);
72
73 assert_se(proc_cmdline_get_key("and_one_more", 0, &value) > 0 && streq_ptr(value, "zzz aaa"));
74 value = mfree(value);
75 #endif
76 }
77
78 static int parse_item_given(const char *key, const char *value, void *data) {
79 assert_se(key);
80 assert_se(data);
81
82 bool *strip = data;
83
84 log_info("%s: option <%s> = <%s>", __func__, key, strna(value));
85 if (proc_cmdline_key_streq(key, "foo_bar"))
86 assert_se(streq(value, "quux"));
87 else if (proc_cmdline_key_streq(key, "wuff-piep"))
88 assert_se(streq(value, "tuet "));
89 else if (proc_cmdline_key_streq(key, "space"))
90 assert_se(streq(value, "x y z"));
91 else if (proc_cmdline_key_streq(key, "miepf"))
92 assert_se(streq(value, "uuu"));
93 else if (in_initrd() && *strip && proc_cmdline_key_streq(key, "zumm"))
94 assert_se(!value);
95 else if (in_initrd() && !*strip && proc_cmdline_key_streq(key, "rd.zumm"))
96 assert_se(!value);
97 else
98 assert_not_reached();
99
100 return 0;
101 }
102
103 static void test_proc_cmdline_given_one(bool flip_initrd) {
104 log_info("/* %s (flip: %s) */", __func__, yes_no(flip_initrd));
105
106 if (flip_initrd)
107 in_initrd_force(!in_initrd());
108
109 bool t = true, f = false;
110 assert_se(proc_cmdline_parse(parse_item_given, &t, PROC_CMDLINE_STRIP_RD_PREFIX) >= 0);
111 assert_se(proc_cmdline_parse(parse_item_given, &f, 0) >= 0);
112
113 if (flip_initrd)
114 in_initrd_force(!in_initrd());
115 }
116
117 TEST(test_proc_cmdline_given) {
118 assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=\"tuet \" rd.zumm space='x y z' miepf=\"uuu\"") == 0);
119 assert_se(putenv((char*) "SYSTEMD_EFI_OPTIONS=miepf=\"uuu\"") == 0);
120
121 test_proc_cmdline_given_one(false);
122 /* Repeat the same thing, but now flip our ininitrdness */
123 test_proc_cmdline_given_one(true);
124 }
125
126 TEST(proc_cmdline_get_key) {
127 _cleanup_free_ char *value = NULL;
128
129 assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=tuet zumm-ghh spaaace='ö ü ß' ticks=\"''\"\n\nkkk=uuu\n\n\n") == 0);
130
131 assert_se(proc_cmdline_get_key("", 0, &value) == -EINVAL);
132 assert_se(proc_cmdline_get_key("abc", 0, NULL) == 0);
133 assert_se(proc_cmdline_get_key("abc", 0, &value) == 0 && value == NULL);
134 assert_se(proc_cmdline_get_key("abc", PROC_CMDLINE_VALUE_OPTIONAL, &value) == 0 && value == NULL);
135
136 assert_se(proc_cmdline_get_key("foo_bar", 0, &value) > 0 && streq_ptr(value, "quux"));
137 value = mfree(value);
138 assert_se(proc_cmdline_get_key("foo_bar", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "quux"));
139 value = mfree(value);
140 assert_se(proc_cmdline_get_key("foo_bar", 0, NULL) == 0);
141 assert_se(proc_cmdline_get_key("foo-bar", 0, &value) > 0 && streq_ptr(value, "quux"));
142 value = mfree(value);
143 assert_se(proc_cmdline_get_key("foo-bar", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "quux"));
144 value = mfree(value);
145 assert_se(proc_cmdline_get_key("foo-bar", 0, NULL) == 0);
146 assert_se(proc_cmdline_get_key("foo-bar", PROC_CMDLINE_VALUE_OPTIONAL, NULL) == -EINVAL);
147
148 assert_se(proc_cmdline_get_key("wuff-piep", 0, &value) > 0 && streq_ptr(value, "tuet"));
149 value = mfree(value);
150 assert_se(proc_cmdline_get_key("wuff-piep", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "tuet"));
151 value = mfree(value);
152 assert_se(proc_cmdline_get_key("wuff_piep", 0, &value) > 0 && streq_ptr(value, "tuet"));
153 value = mfree(value);
154 assert_se(proc_cmdline_get_key("wuff_piep", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "tuet"));
155 value = mfree(value);
156 assert_se(proc_cmdline_get_key("wuff_piep", 0, NULL) == 0);
157 assert_se(proc_cmdline_get_key("wuff_piep", PROC_CMDLINE_VALUE_OPTIONAL, NULL) == -EINVAL);
158
159 assert_se(proc_cmdline_get_key("zumm-ghh", 0, &value) == 0 && value == NULL);
160 assert_se(proc_cmdline_get_key("zumm-ghh", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && value == NULL);
161 assert_se(proc_cmdline_get_key("zumm-ghh", 0, NULL) > 0);
162 assert_se(proc_cmdline_get_key("zumm_ghh", 0, &value) == 0 && value == NULL);
163 assert_se(proc_cmdline_get_key("zumm_ghh", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && value == NULL);
164 assert_se(proc_cmdline_get_key("zumm_ghh", 0, NULL) > 0);
165
166 assert_se(proc_cmdline_get_key("spaaace", 0, &value) > 0 && streq_ptr(value, "ö ü ß"));
167 value = mfree(value);
168
169 assert_se(proc_cmdline_get_key("ticks", 0, &value) > 0 && streq_ptr(value, "''"));
170 value = mfree(value);
171
172 assert_se(proc_cmdline_get_key("kkk", 0, &value) > 0 && streq_ptr(value, "uuu"));
173 }
174
175 TEST(proc_cmdline_get_bool) {
176 bool value = false;
177
178 assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar bar-waldo=1 x_y-z=0 quux=miep\nda=yes\nthe=1") == 0);
179 assert_se(putenv((char*) "SYSTEMD_EFI_OPTIONS=") == 0);
180
181 assert_se(proc_cmdline_get_bool("", &value) == -EINVAL);
182 assert_se(proc_cmdline_get_bool("abc", &value) == 0 && value == false);
183 assert_se(proc_cmdline_get_bool("foo_bar", &value) > 0 && value == true);
184 assert_se(proc_cmdline_get_bool("foo-bar", &value) > 0 && value == true);
185 assert_se(proc_cmdline_get_bool("bar-waldo", &value) > 0 && value == true);
186 assert_se(proc_cmdline_get_bool("bar_waldo", &value) > 0 && value == true);
187 assert_se(proc_cmdline_get_bool("x_y-z", &value) > 0 && value == false);
188 assert_se(proc_cmdline_get_bool("x-y-z", &value) > 0 && value == false);
189 assert_se(proc_cmdline_get_bool("x-y_z", &value) > 0 && value == false);
190 assert_se(proc_cmdline_get_bool("x_y_z", &value) > 0 && value == false);
191 assert_se(proc_cmdline_get_bool("quux", &value) == -EINVAL && value == false);
192 assert_se(proc_cmdline_get_bool("da", &value) > 0 && value == true);
193 assert_se(proc_cmdline_get_bool("the", &value) > 0 && value == true);
194 }
195
196 #if ENABLE_EFI
197 TEST(proc_cmdline_get_bool_efi) {
198 bool value = false;
199
200 assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=") == 0);
201 assert_se(putenv((char*) "SYSTEMD_EFI_OPTIONS=foo_bar bar-waldo=1 x_y-z=0 quux=miep\nda=yes\nthe=1") == 0);
202
203 assert_se(proc_cmdline_get_bool("", &value) == -EINVAL);
204 assert_se(proc_cmdline_get_bool("abc", &value) == 0 && value == false);
205 assert_se(proc_cmdline_get_bool("foo_bar", &value) > 0 && value == true);
206 assert_se(proc_cmdline_get_bool("foo-bar", &value) > 0 && value == true);
207 assert_se(proc_cmdline_get_bool("bar-waldo", &value) > 0 && value == true);
208 assert_se(proc_cmdline_get_bool("bar_waldo", &value) > 0 && value == true);
209 assert_se(proc_cmdline_get_bool("x_y-z", &value) > 0 && value == false);
210 assert_se(proc_cmdline_get_bool("x-y-z", &value) > 0 && value == false);
211 assert_se(proc_cmdline_get_bool("x-y_z", &value) > 0 && value == false);
212 assert_se(proc_cmdline_get_bool("x_y_z", &value) > 0 && value == false);
213 assert_se(proc_cmdline_get_bool("quux", &value) == -EINVAL && value == false);
214 assert_se(proc_cmdline_get_bool("da", &value) > 0 && value == true);
215 assert_se(proc_cmdline_get_bool("the", &value) > 0 && value == true);
216 }
217 #endif
218
219 TEST(proc_cmdline_get_key_many) {
220 _cleanup_free_ char *value1 = NULL, *value2 = NULL, *value3 = NULL, *value4 = NULL, *value5 = NULL, *value6 = NULL, *value7 = NULL;
221
222 assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=tuet zumm SPACE='one two' doubleticks=\" aaa aaa \"\n\nzummm='\n'\n") == 0);
223
224 assert_se(proc_cmdline_get_key_many(0,
225 "wuff-piep", &value3,
226 "foo_bar", &value1,
227 "idontexist", &value2,
228 "zumm", &value4,
229 "SPACE", &value5,
230 "doubleticks", &value6,
231 "zummm", &value7) == 5);
232
233 assert_se(streq_ptr(value1, "quux"));
234 assert_se(!value2);
235 assert_se(streq_ptr(value3, "tuet"));
236 assert_se(!value4);
237 assert_se(streq_ptr(value5, "one two"));
238 assert_se(streq_ptr(value6, " aaa aaa "));
239 assert_se(streq_ptr(value7, "\n"));
240 }
241
242 TEST(proc_cmdline_key_streq) {
243 assert_se(proc_cmdline_key_streq("", ""));
244 assert_se(proc_cmdline_key_streq("a", "a"));
245 assert_se(!proc_cmdline_key_streq("", "a"));
246 assert_se(!proc_cmdline_key_streq("a", ""));
247 assert_se(proc_cmdline_key_streq("a", "a"));
248 assert_se(!proc_cmdline_key_streq("a", "b"));
249 assert_se(proc_cmdline_key_streq("x-y-z", "x-y-z"));
250 assert_se(proc_cmdline_key_streq("x-y-z", "x_y_z"));
251 assert_se(proc_cmdline_key_streq("x-y-z", "x-y_z"));
252 assert_se(proc_cmdline_key_streq("x-y-z", "x_y-z"));
253 assert_se(proc_cmdline_key_streq("x_y-z", "x-y_z"));
254 assert_se(!proc_cmdline_key_streq("x_y-z", "x-z_z"));
255 }
256
257 TEST(proc_cmdline_key_startswith) {
258 assert_se(proc_cmdline_key_startswith("", ""));
259 assert_se(proc_cmdline_key_startswith("x", ""));
260 assert_se(!proc_cmdline_key_startswith("", "x"));
261 assert_se(proc_cmdline_key_startswith("x", "x"));
262 assert_se(!proc_cmdline_key_startswith("x", "y"));
263 assert_se(!proc_cmdline_key_startswith("foo-bar", "quux"));
264 assert_se(proc_cmdline_key_startswith("foo-bar", "foo"));
265 assert_se(proc_cmdline_key_startswith("foo-bar", "foo-bar"));
266 assert_se(proc_cmdline_key_startswith("foo-bar", "foo_bar"));
267 assert_se(proc_cmdline_key_startswith("foo-bar", "foo_"));
268 assert_se(!proc_cmdline_key_startswith("foo-bar", "foo_xx"));
269 }
270
271 #define test_proc_cmdline_filter_pid1_args_one(nulstr, expected) \
272 ({ \
273 _cleanup_strv_free_ char **a = NULL, **b = NULL; \
274 const char s[] = (nulstr); \
275 \
276 /* This emulates get_process_cmdline_strv(). */ \
277 assert_se(a = strv_parse_nulstr_full(s, ELEMENTSOF(s), \
278 /* drop_trailing_nuls = */ true)); \
279 assert_se(proc_cmdline_filter_pid1_args(a, &b) >= 0); \
280 assert_se(strv_equal(b, expected)); \
281 })
282
283 TEST(proc_cmdline_filter_pid1_args) {
284 test_proc_cmdline_filter_pid1_args_one("systemd\0",
285 STRV_MAKE_EMPTY);
286
287 /* short option */
288 test_proc_cmdline_filter_pid1_args_one("systemd\0"
289 "-a\0" /* unknown option */
290 "-abc\0" /* unknown options */
291 "-h\0" /* known option */
292 "-hDbs\0" /* known options */
293 "-hsx\0" /* mixed (known and unknown) options */
294 "-z\0drop1\0" /* option with argument */
295 "-z\0-z\0accept1\0" /* the second -z is handled as argument */
296 "-az\0drop2\0" /* options with argument */
297 "-za\0accept2\0" /* options with argument */
298 "-z\0--\0-x\0", /* "--" is handled as argument */
299 STRV_MAKE("accept1", "accept2"));
300
301 /* long option */
302 test_proc_cmdline_filter_pid1_args_one("systemd\0"
303 "--unknown\0accept1\0" /* unknown option */
304 "--system\0accept2\0" /* no argument */
305 "--log-level\0drop1\0" /* required argument (separated with space) */
306 "--log-level=drop2\0accept3\0" /* required argument (concatenated with '=') */
307 "--log-level\0--log-level\0accept4\0" /* the second "--log-level" is handled as argument */
308 "--log-level\0--\0-x\0" /* "--" is handled as argument */
309 "--log-color\0--log-level\0drop3\0" /* optional argument ("--log-level" is handled as another option) */
310 "--log-color\0accept5\0" /* optional argument (separated with space) */
311 "--log-color=drop4\0accept6\0" /* optional argument (concatenated with '=') */
312 "--log-color\0--\0" /* "--" is _not_ handled as argument, and remaining strings are accepted */
313 "remaining\0-x\0--foo\0",
314 STRV_MAKE("accept1", "accept2", "accept3", "accept4", "accept5", "accept6", "remaining", "-x", "--foo"));
315
316 /* test for "--" */
317 test_proc_cmdline_filter_pid1_args_one("systemd\0"
318 "-a\0"
319 "--dropped\0"
320 "--\0" /* remaining strings are accepted */
321 "-x\0"
322 "-abc\0"
323 "--hoge\0"
324 "accepted\0",
325 STRV_MAKE("-x", "-abc", "--hoge", "accepted"));
326
327 /* test for space */
328 test_proc_cmdline_filter_pid1_args_one("/usr/lib/systemd/systemd\0"
329 "--switched-root\0"
330 "--system\0"
331 "--deserialize\030\0" /* followed with space */
332 "--deserialize=31\0" /* followed with '=' */
333 "--exit-code=42\0"
334 "\0\0\0"
335 "systemd.log_level=debug\0"
336 "--unit\0foo.target\0"
337 " ' quoted '\0"
338 "systemd.log_target=console\0"
339 "\t\0"
340 " arg with space \0"
341 "3\0"
342 "\0\0\0",
343 STRV_MAKE("", "", "", "systemd.log_level=debug", " ' quoted '", "systemd.log_target=console", "\t", " arg with space ", "3"));
344 }
345
346 static int intro(void) {
347 if (access("/proc/cmdline", R_OK) < 0 && ERRNO_IS_PRIVILEGE(errno))
348 return log_tests_skipped("can't read /proc/cmdline");
349
350 return EXIT_SUCCESS;
351 }
352
353 DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);