]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-proc-cmdline.c
Merge pull request #12431 from poettering/tmpfiles-chmod-chown-order
[thirdparty/systemd.git] / src / test / test-proc-cmdline.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "alloc-util.h"
4 #include "env-util.h"
5 #include "log.h"
6 #include "macro.h"
7 #include "proc-cmdline.h"
8 #include "special.h"
9 #include "string-util.h"
10 #include "util.h"
11
12 static int obj;
13
14 static int parse_item(const char *key, const char *value, void *data) {
15 assert_se(key);
16 assert_se(data == &obj);
17
18 log_info("kernel cmdline option <%s> = <%s>", key, strna(value));
19 return 0;
20 }
21
22 static void test_proc_cmdline_parse(void) {
23 log_info("/* %s */", __func__);
24
25 assert_se(proc_cmdline_parse(parse_item, &obj, PROC_CMDLINE_STRIP_RD_PREFIX) >= 0);
26 }
27
28 static void test_proc_cmdline_override(void) {
29 log_info("/* %s */", __func__);
30
31 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);
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 some_arg_with_space='foo bar' and_one_more=\"zzz aaa\""));
39 assert_se(proc_cmdline_get_key("foo_bar", 0, &value) > 0 && streq_ptr(value, "quux"));
40 value = mfree(value);
41
42 assert_se(proc_cmdline_get_key("some_arg_with_space", 0, &value) > 0 && streq_ptr(value, "foo bar"));
43 value = mfree(value);
44
45 assert_se(proc_cmdline_get_key("and_one_more", 0, &value) > 0 && streq_ptr(value, "zzz aaa"));
46 value = mfree(value);
47 }
48
49 static int parse_item_given(const char *key, const char *value, void *data) {
50 assert_se(key);
51 assert_se(data);
52
53 bool *strip = data;
54
55 log_info("%s: option <%s> = <%s>", __func__, key, strna(value));
56 if (proc_cmdline_key_streq(key, "foo_bar"))
57 assert_se(streq(value, "quux"));
58 else if (proc_cmdline_key_streq(key, "wuff-piep"))
59 assert_se(streq(value, "tuet "));
60 else if (proc_cmdline_key_streq(key, "space"))
61 assert_se(streq(value, "x y z"));
62 else if (proc_cmdline_key_streq(key, "miepf"))
63 assert_se(streq(value, "uuu"));
64 else if (in_initrd() && *strip && proc_cmdline_key_streq(key, "zumm"))
65 assert_se(!value);
66 else if (in_initrd() && !*strip && proc_cmdline_key_streq(key, "rd.zumm"))
67 assert_se(!value);
68 else
69 assert_not_reached("Bad key!");
70
71 return 0;
72 }
73
74 static void test_proc_cmdline_given(bool flip_initrd) {
75 log_info("/* %s (flip: %s) */", __func__, yes_no(flip_initrd));
76
77 if (flip_initrd)
78 in_initrd_force(!in_initrd());
79
80 bool t = true, f = false;
81 assert_se(proc_cmdline_parse_given("foo_bar=quux wuff-piep=\"tuet \" rd.zumm space='x y z' miepf=\"uuu\"",
82 parse_item_given, &t, PROC_CMDLINE_STRIP_RD_PREFIX) >= 0);
83
84 assert_se(proc_cmdline_parse_given("foo_bar=quux wuff-piep=\"tuet \" rd.zumm space='x y z' miepf=\"uuu\"",
85 parse_item_given, &f, 0) >= 0);
86
87 if (flip_initrd)
88 in_initrd_force(!in_initrd());
89 }
90
91 static void test_proc_cmdline_get_key(void) {
92 _cleanup_free_ char *value = NULL;
93
94 log_info("/* %s */", __func__);
95 assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar=quux wuff-piep=tuet zumm spaaace='ö ü ß' ticks=\"''\"\n\nkkk=uuu\n\n\n") == 0);
96
97 assert_se(proc_cmdline_get_key("", 0, &value) == -EINVAL);
98 assert_se(proc_cmdline_get_key("abc", 0, NULL) == 0);
99 assert_se(proc_cmdline_get_key("abc", 0, &value) == 0 && value == NULL);
100 assert_se(proc_cmdline_get_key("abc", PROC_CMDLINE_VALUE_OPTIONAL, &value) == 0 && value == NULL);
101
102 assert_se(proc_cmdline_get_key("foo_bar", 0, &value) > 0 && streq_ptr(value, "quux"));
103 value = mfree(value);
104 assert_se(proc_cmdline_get_key("foo_bar", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "quux"));
105 value = mfree(value);
106 assert_se(proc_cmdline_get_key("foo-bar", 0, &value) > 0 && streq_ptr(value, "quux"));
107 value = mfree(value);
108 assert_se(proc_cmdline_get_key("foo-bar", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "quux"));
109 value = mfree(value);
110 assert_se(proc_cmdline_get_key("foo-bar", 0, NULL) == 0);
111 assert_se(proc_cmdline_get_key("foo-bar", PROC_CMDLINE_VALUE_OPTIONAL, NULL) == -EINVAL);
112
113 assert_se(proc_cmdline_get_key("wuff-piep", 0, &value) > 0 && streq_ptr(value, "tuet"));
114 value = mfree(value);
115 assert_se(proc_cmdline_get_key("wuff-piep", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "tuet"));
116 value = mfree(value);
117 assert_se(proc_cmdline_get_key("wuff_piep", 0, &value) > 0 && streq_ptr(value, "tuet"));
118 value = mfree(value);
119 assert_se(proc_cmdline_get_key("wuff_piep", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && streq_ptr(value, "tuet"));
120 value = mfree(value);
121 assert_se(proc_cmdline_get_key("wuff_piep", 0, NULL) == 0);
122 assert_se(proc_cmdline_get_key("wuff_piep", PROC_CMDLINE_VALUE_OPTIONAL, NULL) == -EINVAL);
123
124 assert_se(proc_cmdline_get_key("zumm", 0, &value) == 0 && value == NULL);
125 assert_se(proc_cmdline_get_key("zumm", PROC_CMDLINE_VALUE_OPTIONAL, &value) > 0 && value == NULL);
126 assert_se(proc_cmdline_get_key("zumm", 0, NULL) > 0);
127
128 assert_se(proc_cmdline_get_key("spaaace", 0, &value) > 0 && streq_ptr(value, "ö ü ß"));
129 value = mfree(value);
130
131 assert_se(proc_cmdline_get_key("ticks", 0, &value) > 0 && streq_ptr(value, "''"));
132 value = mfree(value);
133
134 assert_se(proc_cmdline_get_key("kkk", 0, &value) > 0 && streq_ptr(value, "uuu"));
135 }
136
137 static void test_proc_cmdline_get_bool(void) {
138 bool value = false;
139
140 log_info("/* %s */", __func__);
141 assert_se(putenv((char*) "SYSTEMD_PROC_CMDLINE=foo_bar bar-waldo=1 x_y-z=0 quux=miep\nda=yes\nthe=1") == 0);
142
143 assert_se(proc_cmdline_get_bool("", &value) == -EINVAL);
144 assert_se(proc_cmdline_get_bool("abc", &value) == 0 && value == false);
145 assert_se(proc_cmdline_get_bool("foo_bar", &value) > 0 && value == true);
146 assert_se(proc_cmdline_get_bool("foo-bar", &value) > 0 && value == true);
147 assert_se(proc_cmdline_get_bool("bar-waldo", &value) > 0 && value == true);
148 assert_se(proc_cmdline_get_bool("bar_waldo", &value) > 0 && value == true);
149 assert_se(proc_cmdline_get_bool("x_y-z", &value) > 0 && value == false);
150 assert_se(proc_cmdline_get_bool("x-y-z", &value) > 0 && value == false);
151 assert_se(proc_cmdline_get_bool("x-y_z", &value) > 0 && value == false);
152 assert_se(proc_cmdline_get_bool("x_y_z", &value) > 0 && value == false);
153 assert_se(proc_cmdline_get_bool("quux", &value) == -EINVAL && value == false);
154 assert_se(proc_cmdline_get_bool("da", &value) > 0 && value == true);
155 assert_se(proc_cmdline_get_bool("the", &value) > 0 && value == true);
156 }
157
158 static void test_proc_cmdline_get_key_many(void) {
159 _cleanup_free_ char *value1 = NULL, *value2 = NULL, *value3 = NULL, *value4 = NULL, *value5 = NULL, *value6 = NULL, *value7 = NULL;
160
161 log_info("/* %s */", __func__);
162 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);
163
164 assert_se(proc_cmdline_get_key_many(0,
165 "wuff-piep", &value3,
166 "foo_bar", &value1,
167 "idontexist", &value2,
168 "zumm", &value4,
169 "SPACE", &value5,
170 "doubleticks", &value6,
171 "zummm", &value7) == 5);
172
173
174 assert_se(streq_ptr(value1, "quux"));
175 assert_se(!value2);
176 assert_se(streq_ptr(value3, "tuet"));
177 assert_se(!value4);
178 assert_se(streq_ptr(value5, "one two"));
179 assert_se(streq_ptr(value6, " aaa aaa "));
180 assert_se(streq_ptr(value7, "\n"));
181 }
182
183 static void test_proc_cmdline_key_streq(void) {
184 log_info("/* %s */", __func__);
185
186 assert_se(proc_cmdline_key_streq("", ""));
187 assert_se(proc_cmdline_key_streq("a", "a"));
188 assert_se(!proc_cmdline_key_streq("", "a"));
189 assert_se(!proc_cmdline_key_streq("a", ""));
190 assert_se(proc_cmdline_key_streq("a", "a"));
191 assert_se(!proc_cmdline_key_streq("a", "b"));
192 assert_se(proc_cmdline_key_streq("x-y-z", "x-y-z"));
193 assert_se(proc_cmdline_key_streq("x-y-z", "x_y_z"));
194 assert_se(proc_cmdline_key_streq("x-y-z", "x-y_z"));
195 assert_se(proc_cmdline_key_streq("x-y-z", "x_y-z"));
196 assert_se(proc_cmdline_key_streq("x_y-z", "x-y_z"));
197 assert_se(!proc_cmdline_key_streq("x_y-z", "x-z_z"));
198 }
199
200 static void test_proc_cmdline_key_startswith(void) {
201 log_info("/* %s */", __func__);
202
203 assert_se(proc_cmdline_key_startswith("", ""));
204 assert_se(proc_cmdline_key_startswith("x", ""));
205 assert_se(!proc_cmdline_key_startswith("", "x"));
206 assert_se(proc_cmdline_key_startswith("x", "x"));
207 assert_se(!proc_cmdline_key_startswith("x", "y"));
208 assert_se(!proc_cmdline_key_startswith("foo-bar", "quux"));
209 assert_se(proc_cmdline_key_startswith("foo-bar", "foo"));
210 assert_se(proc_cmdline_key_startswith("foo-bar", "foo-bar"));
211 assert_se(proc_cmdline_key_startswith("foo-bar", "foo_bar"));
212 assert_se(proc_cmdline_key_startswith("foo-bar", "foo_"));
213 assert_se(!proc_cmdline_key_startswith("foo-bar", "foo_xx"));
214 }
215
216 static void test_runlevel_to_target(void) {
217 log_info("/* %s */", __func__);
218
219 in_initrd_force(false);
220 assert_se(streq_ptr(runlevel_to_target(NULL), NULL));
221 assert_se(streq_ptr(runlevel_to_target("unknown-runlevel"), NULL));
222 assert_se(streq_ptr(runlevel_to_target("rd.unknown-runlevel"), NULL));
223 assert_se(streq_ptr(runlevel_to_target("3"), SPECIAL_MULTI_USER_TARGET));
224 assert_se(streq_ptr(runlevel_to_target("rd.rescue"), NULL));
225
226 in_initrd_force(true);
227 assert_se(streq_ptr(runlevel_to_target(NULL), NULL));
228 assert_se(streq_ptr(runlevel_to_target("unknown-runlevel"), NULL));
229 assert_se(streq_ptr(runlevel_to_target("rd.unknown-runlevel"), NULL));
230 assert_se(streq_ptr(runlevel_to_target("3"), NULL));
231 assert_se(streq_ptr(runlevel_to_target("rd.rescue"), SPECIAL_RESCUE_TARGET));
232 }
233
234 int main(void) {
235 log_parse_environment();
236 log_open();
237
238 test_proc_cmdline_parse();
239 test_proc_cmdline_override();
240 test_proc_cmdline_given(false);
241 /* Repeat the same thing, but now flip our ininitrdness */
242 test_proc_cmdline_given(true);
243 test_proc_cmdline_key_streq();
244 test_proc_cmdline_key_startswith();
245 test_proc_cmdline_get_key();
246 test_proc_cmdline_get_bool();
247 test_proc_cmdline_get_key_many();
248 test_runlevel_to_target();
249
250 return 0;
251 }