]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
b5b46d59 | 2 | |
07630cea | 3 | #include <fcntl.h> |
b5b46d59 | 4 | #include <stddef.h> |
07630cea | 5 | #include <stdio.h> |
b5b46d59 | 6 | #include <string.h> |
a8107a54 | 7 | #include <sys/capability.h> |
b9893505 | 8 | #include <unistd.h> |
b5b46d59 | 9 | |
57b7a260 | 10 | #include "all-units.h" |
e4de7287 | 11 | #include "alloc-util.h" |
a103496c | 12 | #include "capability-util.h" |
4f424df7 | 13 | #include "conf-parser.h" |
686d13b9 | 14 | #include "env-file.h" |
3ffd4af2 | 15 | #include "fd-util.h" |
627d2bac | 16 | #include "fs-util.h" |
b5b46d59 | 17 | #include "hashmap.h" |
07630cea LP |
18 | #include "hostname-util.h" |
19 | #include "install-printf.h" | |
20 | #include "install.h" | |
2c5417ad | 21 | #include "load-fragment.h" |
07630cea | 22 | #include "macro.h" |
d2120590 | 23 | #include "rm-rf.h" |
07630cea LP |
24 | #include "specifier.h" |
25 | #include "string-util.h" | |
b9893505 | 26 | #include "strv.h" |
143bfdaf | 27 | #include "test-helper.h" |
d2120590 | 28 | #include "tests.h" |
e4de7287 | 29 | #include "tmpfile-util.h" |
79413b67 | 30 | #include "user-util.h" |
07630cea | 31 | #include "util.h" |
b5b46d59 | 32 | |
751e7576 | 33 | static int test_unit_file_get_set(void) { |
b5b46d59 LP |
34 | int r; |
35 | Hashmap *h; | |
36 | Iterator i; | |
37 | UnitFileList *p; | |
38 | ||
d5099efc | 39 | h = hashmap_new(&string_hash_ops); |
bdf7026e | 40 | assert_se(h); |
b5b46d59 | 41 | |
313fe66f | 42 | r = unit_file_get_list(UNIT_FILE_SYSTEM, NULL, h, NULL, NULL); |
730d989a ZJS |
43 | if (IN_SET(r, -EPERM, -EACCES)) |
44 | return log_tests_skipped_errno(r, "unit_file_get_list"); | |
552c693e | 45 | |
9eec7d12 ZJS |
46 | log_full_errno(r == 0 ? LOG_INFO : LOG_ERR, r, |
47 | "unit_file_get_list: %m"); | |
751e7576 CH |
48 | if (r < 0) |
49 | return EXIT_FAILURE; | |
b5b46d59 LP |
50 | |
51 | HASHMAP_FOREACH(p, h, i) | |
52 | printf("%s = %s\n", p->path, unit_file_state_to_string(p->state)); | |
53 | ||
54 | unit_file_list_free(h); | |
751e7576 CH |
55 | |
56 | return 0; | |
2c5417ad ZJS |
57 | } |
58 | ||
59 | static void check_execcommand(ExecCommand *c, | |
60 | const char* path, | |
61 | const char* argv0, | |
62 | const char* argv1, | |
503dbda6 | 63 | const char* argv2, |
2c5417ad | 64 | bool ignore) { |
4d8629de ZJS |
65 | size_t n; |
66 | ||
2c5417ad | 67 | assert_se(c); |
c8539536 ZJS |
68 | log_info("expect: \"%s\" [\"%s\" \"%s\" \"%s\"]", |
69 | path, argv0 ?: path, argv1, argv2); | |
4d8629de | 70 | n = strv_length(c->argv); |
c8539536 | 71 | log_info("actual: \"%s\" [\"%s\" \"%s\" \"%s\"]", |
4d8629de | 72 | c->path, c->argv[0], n > 0 ? c->argv[1] : NULL, n > 1 ? c->argv[2] : NULL); |
2c5417ad | 73 | assert_se(streq(c->path, path)); |
c8539536 | 74 | assert_se(streq(c->argv[0], argv0 ?: path)); |
4d8629de ZJS |
75 | if (n > 0) |
76 | assert_se(streq_ptr(c->argv[1], argv1)); | |
77 | if (n > 1) | |
78 | assert_se(streq_ptr(c->argv[2], argv2)); | |
3ed0cd26 | 79 | assert_se(!!(c->flags & EXEC_COMMAND_IGNORE_FAILURE) == ignore); |
2c5417ad ZJS |
80 | } |
81 | ||
82 | static void test_config_parse_exec(void) { | |
c8539536 | 83 | /* int config_parse_exec( |
470dca63 | 84 | const char *unit, |
c8539536 ZJS |
85 | const char *filename, |
86 | unsigned line, | |
87 | const char *section, | |
88 | unsigned section_line, | |
89 | const char *lvalue, | |
90 | int ltype, | |
91 | const char *rvalue, | |
92 | void *data, | |
93 | void *userdata) */ | |
2c5417ad ZJS |
94 | int r; |
95 | ||
96 | ExecCommand *c = NULL, *c1; | |
c8539536 | 97 | const char *ccc; |
c70cac54 | 98 | _cleanup_(manager_freep) Manager *m = NULL; |
dc409696 | 99 | _cleanup_(unit_freep) Unit *u = NULL; |
139891f0 | 100 | |
e0a3da1f | 101 | r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_MINIMAL, &m); |
139891f0 | 102 | if (MANAGER_SKIP_TEST(r)) { |
9eec7d12 | 103 | log_notice_errno(r, "Skipping test: manager_new: %m"); |
139891f0 WC |
104 | return; |
105 | } | |
106 | ||
107 | assert_se(r >= 0); | |
108 | assert_se(manager_startup(m, NULL, NULL) >= 0); | |
109 | ||
110 | assert_se(u = unit_new(m, sizeof(Service))); | |
2c5417ad | 111 | |
c8539536 | 112 | log_info("/* basic test */"); |
71a61510 | 113 | r = config_parse_exec(NULL, "fake", 1, "section", 1, |
2c5417ad | 114 | "LValue", 0, "/RValue r1", |
139891f0 | 115 | &c, u); |
2c5417ad | 116 | assert_se(r >= 0); |
503dbda6 | 117 | check_execcommand(c, "/RValue", "/RValue", "r1", NULL, false); |
2c5417ad | 118 | |
71a61510 | 119 | r = config_parse_exec(NULL, "fake", 2, "section", 1, |
c8539536 | 120 | "LValue", 0, "/RValue///slashes r1///", |
139891f0 | 121 | &c, u); |
c8539536 ZJS |
122 | |
123 | log_info("/* test slashes */"); | |
2c5417ad ZJS |
124 | assert_se(r >= 0); |
125 | c1 = c->command_next; | |
c8539536 | 126 | check_execcommand(c1, "/RValue/slashes", "/RValue///slashes", "r1///", NULL, false); |
2c5417ad | 127 | |
c8539536 ZJS |
128 | log_info("/* trailing slash */"); |
129 | r = config_parse_exec(NULL, "fake", 4, "section", 1, | |
130 | "LValue", 0, "/RValue/ argv0 r1", | |
139891f0 | 131 | &c, u); |
bb28e684 | 132 | assert_se(r == -ENOEXEC); |
c8539536 ZJS |
133 | assert_se(c1->command_next == NULL); |
134 | ||
135 | log_info("/* honour_argv0 */"); | |
71a61510 | 136 | r = config_parse_exec(NULL, "fake", 3, "section", 1, |
c8539536 | 137 | "LValue", 0, "@/RValue///slashes2 ///argv0 r1", |
139891f0 | 138 | &c, u); |
2c5417ad ZJS |
139 | assert_se(r >= 0); |
140 | c1 = c1->command_next; | |
c8539536 | 141 | check_execcommand(c1, "/RValue/slashes2", "///argv0", "r1", NULL, false); |
2c5417ad | 142 | |
e01ff428 ZJS |
143 | log_info("/* honour_argv0, no args */"); |
144 | r = config_parse_exec(NULL, "fake", 3, "section", 1, | |
145 | "LValue", 0, "@/RValue", | |
139891f0 | 146 | &c, u); |
bb28e684 | 147 | assert_se(r == -ENOEXEC); |
e01ff428 ZJS |
148 | assert_se(c1->command_next == NULL); |
149 | ||
c83f1f30 | 150 | log_info("/* no command, whitespace only, reset */"); |
e01ff428 ZJS |
151 | r = config_parse_exec(NULL, "fake", 3, "section", 1, |
152 | "LValue", 0, " ", | |
139891f0 | 153 | &c, u); |
e01ff428 | 154 | assert_se(r == 0); |
c83f1f30 | 155 | assert_se(c == NULL); |
e01ff428 | 156 | |
c8539536 | 157 | log_info("/* ignore && honour_argv0 */"); |
71a61510 | 158 | r = config_parse_exec(NULL, "fake", 4, "section", 1, |
c8539536 | 159 | "LValue", 0, "-@/RValue///slashes3 argv0a r1", |
139891f0 | 160 | &c, u); |
2c5417ad | 161 | assert_se(r >= 0); |
c83f1f30 | 162 | c1 = c; |
503dbda6 | 163 | check_execcommand(c1, "/RValue/slashes3", "argv0a", "r1", NULL, true); |
2c5417ad | 164 | |
c8539536 | 165 | log_info("/* ignore && honour_argv0 */"); |
71a61510 | 166 | r = config_parse_exec(NULL, "fake", 4, "section", 1, |
c8539536 | 167 | "LValue", 0, "@-/RValue///slashes4 argv0b r1", |
139891f0 | 168 | &c, u); |
0f67f1ef ZJS |
169 | assert_se(r >= 0); |
170 | c1 = c1->command_next; | |
503dbda6 | 171 | check_execcommand(c1, "/RValue/slashes4", "argv0b", "r1", NULL, true); |
0f67f1ef | 172 | |
c8539536 | 173 | log_info("/* ignore && ignore */"); |
71a61510 | 174 | r = config_parse_exec(NULL, "fake", 4, "section", 1, |
0f67f1ef | 175 | "LValue", 0, "--/RValue argv0 r1", |
139891f0 | 176 | &c, u); |
0f67f1ef ZJS |
177 | assert_se(r == 0); |
178 | assert_se(c1->command_next == NULL); | |
179 | ||
c8539536 | 180 | log_info("/* ignore && ignore (2) */"); |
71a61510 | 181 | r = config_parse_exec(NULL, "fake", 4, "section", 1, |
0f67f1ef | 182 | "LValue", 0, "-@-/RValue argv0 r1", |
139891f0 | 183 | &c, u); |
0f67f1ef ZJS |
184 | assert_se(r == 0); |
185 | assert_se(c1->command_next == NULL); | |
186 | ||
c8539536 | 187 | log_info("/* semicolon */"); |
71a61510 | 188 | r = config_parse_exec(NULL, "fake", 5, "section", 1, |
2c5417ad ZJS |
189 | "LValue", 0, |
190 | "-@/RValue argv0 r1 ; " | |
191 | "/goo/goo boo", | |
139891f0 | 192 | &c, u); |
2c5417ad ZJS |
193 | assert_se(r >= 0); |
194 | c1 = c1->command_next; | |
503dbda6 | 195 | check_execcommand(c1, "/RValue", "argv0", "r1", NULL, true); |
2c5417ad ZJS |
196 | |
197 | c1 = c1->command_next; | |
c8539536 | 198 | check_execcommand(c1, "/goo/goo", NULL, "boo", NULL, false); |
2c5417ad | 199 | |
0e9800d5 FB |
200 | log_info("/* two semicolons in a row */"); |
201 | r = config_parse_exec(NULL, "fake", 5, "section", 1, | |
202 | "LValue", 0, | |
203 | "-@/RValue argv0 r1 ; ; " | |
204 | "/goo/goo boo", | |
139891f0 | 205 | &c, u); |
bb28e684 | 206 | assert_se(r == -ENOEXEC); |
0e9800d5 FB |
207 | c1 = c1->command_next; |
208 | check_execcommand(c1, "/RValue", "argv0", "r1", NULL, true); | |
209 | ||
210 | /* second command fails because the executable name is ";" */ | |
211 | assert_se(c1->command_next == NULL); | |
212 | ||
c8539536 | 213 | log_info("/* trailing semicolon */"); |
71a61510 | 214 | r = config_parse_exec(NULL, "fake", 5, "section", 1, |
2c5417ad ZJS |
215 | "LValue", 0, |
216 | "-@/RValue argv0 r1 ; ", | |
139891f0 | 217 | &c, u); |
2c5417ad ZJS |
218 | assert_se(r >= 0); |
219 | c1 = c1->command_next; | |
503dbda6 | 220 | check_execcommand(c1, "/RValue", "argv0", "r1", NULL, true); |
2c5417ad ZJS |
221 | |
222 | assert_se(c1->command_next == NULL); | |
223 | ||
0e9800d5 FB |
224 | log_info("/* trailing semicolon, no whitespace */"); |
225 | r = config_parse_exec(NULL, "fake", 5, "section", 1, | |
226 | "LValue", 0, | |
227 | "-@/RValue argv0 r1 ;", | |
139891f0 | 228 | &c, u); |
0e9800d5 FB |
229 | assert_se(r >= 0); |
230 | c1 = c1->command_next; | |
231 | check_execcommand(c1, "/RValue", "argv0", "r1", NULL, true); | |
232 | ||
233 | assert_se(c1->command_next == NULL); | |
234 | ||
235 | log_info("/* trailing semicolon in single quotes */"); | |
236 | r = config_parse_exec(NULL, "fake", 5, "section", 1, | |
237 | "LValue", 0, | |
238 | "-@/RValue argv0 r1 ';'", | |
139891f0 | 239 | &c, u); |
0e9800d5 FB |
240 | assert_se(r >= 0); |
241 | c1 = c1->command_next; | |
46a0d98a | 242 | check_execcommand(c1, "/RValue", "argv0", "r1", ";", true); |
0e9800d5 | 243 | |
c8539536 | 244 | log_info("/* escaped semicolon */"); |
71a61510 | 245 | r = config_parse_exec(NULL, "fake", 5, "section", 1, |
7e1a84f5 | 246 | "LValue", 0, |
503dbda6 | 247 | "/bin/find \\;", |
139891f0 | 248 | &c, u); |
503dbda6 ZJS |
249 | assert_se(r >= 0); |
250 | c1 = c1->command_next; | |
c8539536 | 251 | check_execcommand(c1, "/bin/find", NULL, ";", NULL, false); |
503dbda6 | 252 | |
c8539536 | 253 | log_info("/* escaped semicolon with following arg */"); |
503dbda6 ZJS |
254 | r = config_parse_exec(NULL, "fake", 5, "section", 1, |
255 | "LValue", 0, | |
0e9800d5 | 256 | "/sbin/find \\; /x", |
139891f0 | 257 | &c, u); |
0e9800d5 FB |
258 | assert_se(r >= 0); |
259 | c1 = c1->command_next; | |
260 | check_execcommand(c1, | |
261 | "/sbin/find", NULL, ";", "/x", false); | |
262 | ||
263 | log_info("/* escaped semicolon as part of an expression */"); | |
264 | r = config_parse_exec(NULL, "fake", 5, "section", 1, | |
265 | "LValue", 0, | |
266 | "/sbin/find \\;x", | |
139891f0 | 267 | &c, u); |
7e1a84f5 OS |
268 | assert_se(r >= 0); |
269 | c1 = c1->command_next; | |
270 | check_execcommand(c1, | |
0e9800d5 | 271 | "/sbin/find", NULL, "\\;x", NULL, false); |
c8539536 | 272 | |
80979f1c DM |
273 | log_info("/* encoded semicolon */"); |
274 | r = config_parse_exec(NULL, "fake", 5, "section", 1, | |
275 | "LValue", 0, | |
276 | "/bin/find \\073", | |
139891f0 | 277 | &c, u); |
80979f1c DM |
278 | assert_se(r >= 0); |
279 | c1 = c1->command_next; | |
ce54255f | 280 | check_execcommand(c1, "/bin/find", NULL, ";", NULL, false); |
80979f1c | 281 | |
0e9800d5 FB |
282 | log_info("/* quoted semicolon */"); |
283 | r = config_parse_exec(NULL, "fake", 5, "section", 1, | |
284 | "LValue", 0, | |
285 | "/bin/find \";\"", | |
139891f0 | 286 | &c, u); |
0e9800d5 FB |
287 | assert_se(r >= 0); |
288 | c1 = c1->command_next; | |
46a0d98a | 289 | check_execcommand(c1, "/bin/find", NULL, ";", NULL, false); |
0e9800d5 FB |
290 | |
291 | log_info("/* quoted semicolon with following arg */"); | |
292 | r = config_parse_exec(NULL, "fake", 5, "section", 1, | |
293 | "LValue", 0, | |
294 | "/sbin/find \";\" /x", | |
139891f0 | 295 | &c, u); |
0e9800d5 FB |
296 | assert_se(r >= 0); |
297 | c1 = c1->command_next; | |
298 | check_execcommand(c1, | |
46a0d98a | 299 | "/sbin/find", NULL, ";", "/x", false); |
0e9800d5 | 300 | |
c8539536 ZJS |
301 | log_info("/* spaces in the filename */"); |
302 | r = config_parse_exec(NULL, "fake", 5, "section", 1, | |
303 | "LValue", 0, | |
304 | "\"/PATH WITH SPACES/daemon\" -1 -2", | |
139891f0 | 305 | &c, u); |
c8539536 ZJS |
306 | assert_se(r >= 0); |
307 | c1 = c1->command_next; | |
308 | check_execcommand(c1, | |
309 | "/PATH WITH SPACES/daemon", NULL, "-1", "-2", false); | |
310 | ||
311 | log_info("/* spaces in the filename, no args */"); | |
312 | r = config_parse_exec(NULL, "fake", 5, "section", 1, | |
313 | "LValue", 0, | |
314 | "\"/PATH WITH SPACES/daemon -1 -2\"", | |
139891f0 | 315 | &c, u); |
c8539536 ZJS |
316 | assert_se(r >= 0); |
317 | c1 = c1->command_next; | |
318 | check_execcommand(c1, | |
319 | "/PATH WITH SPACES/daemon -1 -2", NULL, NULL, NULL, false); | |
320 | ||
321 | log_info("/* spaces in the filename, everything quoted */"); | |
322 | r = config_parse_exec(NULL, "fake", 5, "section", 1, | |
323 | "LValue", 0, | |
324 | "\"/PATH WITH SPACES/daemon\" \"-1\" '-2'", | |
139891f0 | 325 | &c, u); |
c8539536 ZJS |
326 | assert_se(r >= 0); |
327 | c1 = c1->command_next; | |
328 | check_execcommand(c1, | |
329 | "/PATH WITH SPACES/daemon", NULL, "-1", "-2", false); | |
330 | ||
331 | log_info("/* escaped spaces in the filename */"); | |
332 | r = config_parse_exec(NULL, "fake", 5, "section", 1, | |
333 | "LValue", 0, | |
334 | "\"/PATH\\sWITH\\sSPACES/daemon\" '-1 -2'", | |
139891f0 | 335 | &c, u); |
c8539536 ZJS |
336 | assert_se(r >= 0); |
337 | c1 = c1->command_next; | |
338 | check_execcommand(c1, | |
339 | "/PATH WITH SPACES/daemon", NULL, "-1 -2", NULL, false); | |
340 | ||
341 | log_info("/* escaped spaces in the filename (2) */"); | |
342 | r = config_parse_exec(NULL, "fake", 5, "section", 1, | |
343 | "LValue", 0, | |
344 | "\"/PATH\\x20WITH\\x20SPACES/daemon\" \"-1 -2\"", | |
139891f0 | 345 | &c, u); |
c8539536 ZJS |
346 | assert_se(r >= 0); |
347 | c1 = c1->command_next; | |
348 | check_execcommand(c1, | |
349 | "/PATH WITH SPACES/daemon", NULL, "-1 -2", NULL, false); | |
350 | ||
351 | for (ccc = "abfnrtv\\\'\"x"; *ccc; ccc++) { | |
352 | /* \\x is an incomplete hexadecimal sequence, invalid because of the slash */ | |
353 | char path[] = "/path\\X"; | |
354 | path[sizeof(path) - 2] = *ccc; | |
355 | ||
356 | log_info("/* invalid character: \\%c */", *ccc); | |
357 | r = config_parse_exec(NULL, "fake", 4, "section", 1, | |
358 | "LValue", 0, path, | |
139891f0 | 359 | &c, u); |
bb28e684 | 360 | assert_se(r == -ENOEXEC); |
c8539536 ZJS |
361 | assert_se(c1->command_next == NULL); |
362 | } | |
363 | ||
364 | log_info("/* valid character: \\s */"); | |
365 | r = config_parse_exec(NULL, "fake", 4, "section", 1, | |
366 | "LValue", 0, "/path\\s", | |
139891f0 | 367 | &c, u); |
c8539536 ZJS |
368 | assert_se(r >= 0); |
369 | c1 = c1->command_next; | |
370 | check_execcommand(c1, "/path ", NULL, NULL, NULL, false); | |
371 | ||
80979f1c DM |
372 | log_info("/* quoted backslashes */"); |
373 | r = config_parse_exec(NULL, "fake", 5, "section", 1, | |
374 | "LValue", 0, | |
375 | "/bin/grep '\\w+\\K'", | |
139891f0 | 376 | &c, u); |
80979f1c DM |
377 | assert_se(r >= 0); |
378 | c1 = c1->command_next; | |
379 | check_execcommand(c1, "/bin/grep", NULL, "\\w+\\K", NULL, false); | |
380 | ||
c8539536 ZJS |
381 | log_info("/* trailing backslash: \\ */"); |
382 | /* backslash is invalid */ | |
383 | r = config_parse_exec(NULL, "fake", 4, "section", 1, | |
384 | "LValue", 0, "/path\\", | |
139891f0 | 385 | &c, u); |
bb28e684 | 386 | assert_se(r == -ENOEXEC); |
c8539536 | 387 | assert_se(c1->command_next == NULL); |
7e1a84f5 | 388 | |
470dca63 MP |
389 | log_info("/* missing ending ' */"); |
390 | r = config_parse_exec(NULL, "fake", 4, "section", 1, | |
391 | "LValue", 0, "/path 'foo", | |
139891f0 | 392 | &c, u); |
bb28e684 | 393 | assert_se(r == -ENOEXEC); |
470dca63 MP |
394 | assert_se(c1->command_next == NULL); |
395 | ||
396 | log_info("/* missing ending ' with trailing backslash */"); | |
397 | r = config_parse_exec(NULL, "fake", 4, "section", 1, | |
398 | "LValue", 0, "/path 'foo\\", | |
139891f0 | 399 | &c, u); |
bb28e684 | 400 | assert_se(r == -ENOEXEC); |
470dca63 MP |
401 | assert_se(c1->command_next == NULL); |
402 | ||
35b1078e MP |
403 | log_info("/* invalid space between modifiers */"); |
404 | r = config_parse_exec(NULL, "fake", 4, "section", 1, | |
405 | "LValue", 0, "- /path", | |
139891f0 | 406 | &c, u); |
35b1078e MP |
407 | assert_se(r == 0); |
408 | assert_se(c1->command_next == NULL); | |
409 | ||
410 | log_info("/* only modifiers, no path */"); | |
411 | r = config_parse_exec(NULL, "fake", 4, "section", 1, | |
412 | "LValue", 0, "-", | |
139891f0 | 413 | &c, u); |
35b1078e MP |
414 | assert_se(r == 0); |
415 | assert_se(c1->command_next == NULL); | |
416 | ||
417 | log_info("/* empty argument, reset */"); | |
418 | r = config_parse_exec(NULL, "fake", 4, "section", 1, | |
419 | "LValue", 0, "", | |
139891f0 | 420 | &c, u); |
35b1078e MP |
421 | assert_se(r == 0); |
422 | assert_se(c == NULL); | |
423 | ||
2c5417ad ZJS |
424 | exec_command_free_list(c); |
425 | } | |
426 | ||
88bff424 ZJS |
427 | static void test_config_parse_log_extra_fields(void) { |
428 | /* int config_parse_log_extra_fields( | |
429 | const char *unit, | |
430 | const char *filename, | |
431 | unsigned line, | |
432 | const char *section, | |
433 | unsigned section_line, | |
434 | const char *lvalue, | |
435 | int ltype, | |
436 | const char *rvalue, | |
437 | void *data, | |
438 | void *userdata) */ | |
439 | ||
440 | int r; | |
441 | ||
c70cac54 | 442 | _cleanup_(manager_freep) Manager *m = NULL; |
dc409696 | 443 | _cleanup_(unit_freep) Unit *u = NULL; |
88bff424 ZJS |
444 | ExecContext c = {}; |
445 | ||
446 | r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_MINIMAL, &m); | |
447 | if (MANAGER_SKIP_TEST(r)) { | |
448 | log_notice_errno(r, "Skipping test: manager_new: %m"); | |
449 | return; | |
450 | } | |
451 | ||
452 | assert_se(r >= 0); | |
453 | assert_se(manager_startup(m, NULL, NULL) >= 0); | |
454 | ||
455 | assert_se(u = unit_new(m, sizeof(Service))); | |
456 | ||
457 | log_info("/* %s – basic test */", __func__); | |
458 | r = config_parse_log_extra_fields(NULL, "fake", 1, "section", 1, | |
459 | "LValue", 0, "FOO=BAR \"QOOF=quux ' ' \"", | |
460 | &c, u); | |
461 | assert_se(r >= 0); | |
462 | assert_se(c.n_log_extra_fields == 2); | |
463 | assert_se(strneq(c.log_extra_fields[0].iov_base, "FOO=BAR", c.log_extra_fields[0].iov_len)); | |
464 | assert_se(strneq(c.log_extra_fields[1].iov_base, "QOOF=quux ' ' ", c.log_extra_fields[1].iov_len)); | |
465 | ||
466 | log_info("/* %s – add some */", __func__); | |
467 | r = config_parse_log_extra_fields(NULL, "fake", 1, "section", 1, | |
468 | "LValue", 0, "FOO2=BAR2 QOOF2=quux ' '", | |
469 | &c, u); | |
470 | assert_se(r >= 0); | |
471 | assert_se(c.n_log_extra_fields == 4); | |
472 | assert_se(strneq(c.log_extra_fields[0].iov_base, "FOO=BAR", c.log_extra_fields[0].iov_len)); | |
473 | assert_se(strneq(c.log_extra_fields[1].iov_base, "QOOF=quux ' ' ", c.log_extra_fields[1].iov_len)); | |
474 | assert_se(strneq(c.log_extra_fields[2].iov_base, "FOO2=BAR2", c.log_extra_fields[2].iov_len)); | |
475 | assert_se(strneq(c.log_extra_fields[3].iov_base, "QOOF2=quux", c.log_extra_fields[3].iov_len)); | |
476 | ||
477 | exec_context_dump(&c, stdout, " --> "); | |
478 | ||
479 | log_info("/* %s – reset */", __func__); | |
480 | r = config_parse_log_extra_fields(NULL, "fake", 1, "section", 1, | |
481 | "LValue", 0, "", | |
482 | &c, u); | |
483 | assert_se(r >= 0); | |
484 | assert_se(c.n_log_extra_fields == 0); | |
485 | ||
486 | exec_context_free_log_extra_fields(&c); | |
487 | ||
88bff424 ZJS |
488 | log_info("/* %s – bye */", __func__); |
489 | } | |
490 | ||
f73141d7 LP |
491 | #define env_file_1 \ |
492 | "a=a\n" \ | |
493 | "b=b\\\n" \ | |
494 | "c\n" \ | |
495 | "d=d\\\n" \ | |
496 | "e\\\n" \ | |
497 | "f\n" \ | |
498 | "g=g\\ \n" \ | |
499 | "h=h\n" \ | |
500 | "i=i\\" | |
501 | ||
502 | #define env_file_2 \ | |
503 | "a=a\\\n" | |
b9893505 | 504 | |
a3aa7ee6 ZJS |
505 | #define env_file_3 \ |
506 | "#SPAMD_ARGS=\"-d --socketpath=/var/lib/bulwark/spamd \\\n" \ | |
507 | "#--nouser-config \\\n" \ | |
508 | "normal=line" | |
509 | ||
d3b6d0c2 ZJS |
510 | #define env_file_4 \ |
511 | "# Generated\n" \ | |
512 | "\n" \ | |
513 | "HWMON_MODULES=\"coretemp f71882fg\"\n" \ | |
514 | "\n" \ | |
515 | "# For compatibility reasons\n" \ | |
516 | "\n" \ | |
517 | "MODULE_0=coretemp\n" \ | |
518 | "MODULE_1=f71882fg" | |
519 | ||
58f10d40 ILG |
520 | #define env_file_5 \ |
521 | "a=\n" \ | |
522 | "b=" | |
d3b6d0c2 | 523 | |
b9893505 | 524 | static void test_load_env_file_1(void) { |
7fd1b19b | 525 | _cleanup_strv_free_ char **data = NULL; |
b9893505 ZJS |
526 | int r; |
527 | ||
627d2bac | 528 | _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX"; |
2d5bdf5b LP |
529 | _cleanup_close_ int fd; |
530 | ||
646853bd | 531 | fd = mkostemp_safe(name); |
bdf7026e | 532 | assert_se(fd >= 0); |
2d78717b | 533 | assert_se(write(fd, env_file_1, strlen(env_file_1)) == strlen(env_file_1)); |
b9893505 | 534 | |
aa8fbc74 | 535 | r = load_env_file(NULL, name, &data); |
bdf7026e TA |
536 | assert_se(r == 0); |
537 | assert_se(streq(data[0], "a=a")); | |
538 | assert_se(streq(data[1], "b=bc")); | |
539 | assert_se(streq(data[2], "d=def")); | |
540 | assert_se(streq(data[3], "g=g ")); | |
541 | assert_se(streq(data[4], "h=h")); | |
542 | assert_se(streq(data[5], "i=i")); | |
543 | assert_se(data[6] == NULL); | |
b9893505 ZJS |
544 | } |
545 | ||
546 | static void test_load_env_file_2(void) { | |
7fd1b19b | 547 | _cleanup_strv_free_ char **data = NULL; |
b9893505 ZJS |
548 | int r; |
549 | ||
627d2bac | 550 | _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX"; |
2d5bdf5b LP |
551 | _cleanup_close_ int fd; |
552 | ||
646853bd | 553 | fd = mkostemp_safe(name); |
bdf7026e | 554 | assert_se(fd >= 0); |
2d78717b | 555 | assert_se(write(fd, env_file_2, strlen(env_file_2)) == strlen(env_file_2)); |
b9893505 | 556 | |
aa8fbc74 | 557 | r = load_env_file(NULL, name, &data); |
bdf7026e TA |
558 | assert_se(r == 0); |
559 | assert_se(streq(data[0], "a=a")); | |
560 | assert_se(data[1] == NULL); | |
b9893505 ZJS |
561 | } |
562 | ||
a3aa7ee6 | 563 | static void test_load_env_file_3(void) { |
7fd1b19b | 564 | _cleanup_strv_free_ char **data = NULL; |
a3aa7ee6 ZJS |
565 | int r; |
566 | ||
627d2bac | 567 | _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX"; |
2d5bdf5b LP |
568 | _cleanup_close_ int fd; |
569 | ||
646853bd | 570 | fd = mkostemp_safe(name); |
bdf7026e | 571 | assert_se(fd >= 0); |
2d78717b | 572 | assert_se(write(fd, env_file_3, strlen(env_file_3)) == strlen(env_file_3)); |
a3aa7ee6 | 573 | |
aa8fbc74 | 574 | r = load_env_file(NULL, name, &data); |
bdf7026e TA |
575 | assert_se(r == 0); |
576 | assert_se(data == NULL); | |
a3aa7ee6 ZJS |
577 | } |
578 | ||
d3b6d0c2 | 579 | static void test_load_env_file_4(void) { |
7fd1b19b | 580 | _cleanup_strv_free_ char **data = NULL; |
627d2bac | 581 | _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX"; |
2d5bdf5b | 582 | _cleanup_close_ int fd; |
d3b6d0c2 ZJS |
583 | int r; |
584 | ||
646853bd | 585 | fd = mkostemp_safe(name); |
bdf7026e | 586 | assert_se(fd >= 0); |
2d78717b | 587 | assert_se(write(fd, env_file_4, strlen(env_file_4)) == strlen(env_file_4)); |
d3b6d0c2 | 588 | |
aa8fbc74 | 589 | r = load_env_file(NULL, name, &data); |
bdf7026e TA |
590 | assert_se(r == 0); |
591 | assert_se(streq(data[0], "HWMON_MODULES=coretemp f71882fg")); | |
592 | assert_se(streq(data[1], "MODULE_0=coretemp")); | |
593 | assert_se(streq(data[2], "MODULE_1=f71882fg")); | |
594 | assert_se(data[3] == NULL); | |
d3b6d0c2 ZJS |
595 | } |
596 | ||
58f10d40 ILG |
597 | static void test_load_env_file_5(void) { |
598 | _cleanup_strv_free_ char **data = NULL; | |
599 | int r; | |
600 | ||
627d2bac | 601 | _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-load-env-file.XXXXXX"; |
58f10d40 ILG |
602 | _cleanup_close_ int fd; |
603 | ||
646853bd | 604 | fd = mkostemp_safe(name); |
58f10d40 | 605 | assert_se(fd >= 0); |
2d78717b | 606 | assert_se(write(fd, env_file_5, strlen(env_file_5)) == strlen(env_file_5)); |
58f10d40 | 607 | |
aa8fbc74 | 608 | r = load_env_file(NULL, name, &data); |
58f10d40 ILG |
609 | assert_se(r == 0); |
610 | assert_se(streq(data[0], "a=")); | |
611 | assert_se(streq(data[1], "b=")); | |
612 | assert_se(data[2] == NULL); | |
58f10d40 | 613 | } |
d3b6d0c2 | 614 | |
7742f7e9 ZJS |
615 | static void test_install_printf(void) { |
616 | char name[] = "name.service", | |
79413b67 LP |
617 | path[] = "/run/systemd/system/name.service"; |
618 | UnitFileInstallInfo i = { .name = name, .path = path, }; | |
619 | UnitFileInstallInfo i2 = { .name= name, .path = path, }; | |
7742f7e9 | 620 | char name3[] = "name@inst.service", |
ad88e758 | 621 | path3[] = "/run/systemd/system/name.service"; |
79413b67 LP |
622 | UnitFileInstallInfo i3 = { .name = name3, .path = path3, }; |
623 | UnitFileInstallInfo i4 = { .name = name3, .path = path3, }; | |
7742f7e9 | 624 | |
b75f0c69 | 625 | _cleanup_free_ char *mid = NULL, *bid = NULL, *host = NULL, *gid = NULL, *group = NULL, *uid = NULL, *user = NULL; |
7742f7e9 | 626 | |
19f6d710 LP |
627 | assert_se(specifier_machine_id('m', NULL, NULL, &mid) >= 0 && mid); |
628 | assert_se(specifier_boot_id('b', NULL, NULL, &bid) >= 0 && bid); | |
dcd6361e | 629 | assert_se(host = gethostname_malloc()); |
b75f0c69 DC |
630 | assert_se(group = gid_to_name(getgid())); |
631 | assert_se(asprintf(&gid, UID_FMT, getgid()) >= 0); | |
dcd6361e | 632 | assert_se(user = uid_to_name(getuid())); |
79413b67 | 633 | assert_se(asprintf(&uid, UID_FMT, getuid()) >= 0); |
7742f7e9 ZJS |
634 | |
635 | #define expect(src, pattern, result) \ | |
f73141d7 | 636 | do { \ |
19f6d710 | 637 | _cleanup_free_ char *t = NULL; \ |
7fd1b19b | 638 | _cleanup_free_ char \ |
7742f7e9 | 639 | *d1 = strdup(i.name), \ |
79413b67 | 640 | *d2 = strdup(i.path); \ |
19f6d710 | 641 | assert_se(install_full_printf(&src, pattern, &t) >= 0 || !result); \ |
7742f7e9 ZJS |
642 | memzero(i.name, strlen(i.name)); \ |
643 | memzero(i.path, strlen(i.path)); \ | |
79413b67 | 644 | assert_se(d1 && d2); \ |
7742f7e9 ZJS |
645 | if (result) { \ |
646 | printf("%s\n", t); \ | |
79413b67 LP |
647 | assert_se(streq(t, result)); \ |
648 | } else assert_se(t == NULL); \ | |
7742f7e9 ZJS |
649 | strcpy(i.name, d1); \ |
650 | strcpy(i.path, d2); \ | |
9ed794a3 | 651 | } while (false) |
7742f7e9 | 652 | |
7742f7e9 ZJS |
653 | expect(i, "%n", "name.service"); |
654 | expect(i, "%N", "name"); | |
655 | expect(i, "%p", "name"); | |
656 | expect(i, "%i", ""); | |
250e9fad | 657 | expect(i, "%j", "name"); |
b75f0c69 DC |
658 | expect(i, "%g", group); |
659 | expect(i, "%G", gid); | |
79413b67 LP |
660 | expect(i, "%u", user); |
661 | expect(i, "%U", uid); | |
8fca4e30 | 662 | |
7742f7e9 ZJS |
663 | expect(i, "%m", mid); |
664 | expect(i, "%b", bid); | |
665 | expect(i, "%H", host); | |
666 | ||
b75f0c69 DC |
667 | expect(i2, "%g", group); |
668 | expect(i2, "%G", gid); | |
79413b67 LP |
669 | expect(i2, "%u", user); |
670 | expect(i2, "%U", uid); | |
7742f7e9 ZJS |
671 | |
672 | expect(i3, "%n", "name@inst.service"); | |
673 | expect(i3, "%N", "name@inst"); | |
674 | expect(i3, "%p", "name"); | |
b75f0c69 DC |
675 | expect(i3, "%g", group); |
676 | expect(i3, "%G", gid); | |
79413b67 LP |
677 | expect(i3, "%u", user); |
678 | expect(i3, "%U", uid); | |
8fca4e30 | 679 | |
7742f7e9 ZJS |
680 | expect(i3, "%m", mid); |
681 | expect(i3, "%b", bid); | |
682 | expect(i3, "%H", host); | |
683 | ||
b75f0c69 DC |
684 | expect(i4, "%g", group); |
685 | expect(i4, "%G", gid); | |
79413b67 LP |
686 | expect(i4, "%u", user); |
687 | expect(i4, "%U", uid); | |
7742f7e9 | 688 | } |
b9893505 | 689 | |
a8107a54 EV |
690 | static uint64_t make_cap(int cap) { |
691 | return ((uint64_t) 1ULL << (uint64_t) cap); | |
692 | } | |
693 | ||
a103496c IP |
694 | static void test_config_parse_capability_set(void) { |
695 | /* int config_parse_capability_set( | |
a8107a54 EV |
696 | const char *unit, |
697 | const char *filename, | |
698 | unsigned line, | |
699 | const char *section, | |
700 | unsigned section_line, | |
701 | const char *lvalue, | |
702 | int ltype, | |
703 | const char *rvalue, | |
704 | void *data, | |
705 | void *userdata) */ | |
706 | int r; | |
a103496c | 707 | uint64_t capability_bounding_set = 0; |
a8107a54 | 708 | |
a103496c | 709 | r = config_parse_capability_set(NULL, "fake", 1, "section", 1, |
a8107a54 | 710 | "CapabilityBoundingSet", 0, "CAP_NET_RAW", |
a103496c | 711 | &capability_bounding_set, NULL); |
a8107a54 | 712 | assert_se(r >= 0); |
a103496c | 713 | assert_se(capability_bounding_set == make_cap(CAP_NET_RAW)); |
a8107a54 | 714 | |
a103496c | 715 | r = config_parse_capability_set(NULL, "fake", 1, "section", 1, |
a8107a54 | 716 | "CapabilityBoundingSet", 0, "CAP_NET_ADMIN", |
a103496c | 717 | &capability_bounding_set, NULL); |
a8107a54 | 718 | assert_se(r >= 0); |
a103496c | 719 | assert_se(capability_bounding_set == (make_cap(CAP_NET_RAW) | make_cap(CAP_NET_ADMIN))); |
a8107a54 | 720 | |
7dd09746 YW |
721 | r = config_parse_capability_set(NULL, "fake", 1, "section", 1, |
722 | "CapabilityBoundingSet", 0, "~CAP_NET_ADMIN", | |
723 | &capability_bounding_set, NULL); | |
724 | assert_se(r >= 0); | |
725 | assert_se(capability_bounding_set == make_cap(CAP_NET_RAW)); | |
726 | ||
a103496c | 727 | r = config_parse_capability_set(NULL, "fake", 1, "section", 1, |
a8107a54 | 728 | "CapabilityBoundingSet", 0, "", |
a103496c | 729 | &capability_bounding_set, NULL); |
a8107a54 | 730 | assert_se(r >= 0); |
a103496c | 731 | assert_se(capability_bounding_set == UINT64_C(0)); |
a8107a54 | 732 | |
a103496c | 733 | r = config_parse_capability_set(NULL, "fake", 1, "section", 1, |
a8107a54 | 734 | "CapabilityBoundingSet", 0, "~", |
a103496c | 735 | &capability_bounding_set, NULL); |
a8107a54 | 736 | assert_se(r >= 0); |
a103496c | 737 | assert_se(cap_test_all(capability_bounding_set)); |
4b03af4a | 738 | |
a103496c IP |
739 | capability_bounding_set = 0; |
740 | r = config_parse_capability_set(NULL, "fake", 1, "section", 1, | |
4b03af4a | 741 | "CapabilityBoundingSet", 0, " 'CAP_NET_RAW' WAT_CAP??? CAP_NET_ADMIN CAP'_trailing_garbage", |
a103496c | 742 | &capability_bounding_set, NULL); |
4b03af4a | 743 | assert_se(r >= 0); |
a103496c | 744 | assert_se(capability_bounding_set == (make_cap(CAP_NET_RAW) | make_cap(CAP_NET_ADMIN))); |
a8107a54 EV |
745 | } |
746 | ||
a4c18002 LP |
747 | static void test_config_parse_rlimit(void) { |
748 | struct rlimit * rl[_RLIMIT_MAX] = {}; | |
749 | ||
4f424df7 | 750 | assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "55", rl, NULL) >= 0); |
a4c18002 LP |
751 | assert_se(rl[RLIMIT_NOFILE]); |
752 | assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 55); | |
753 | assert_se(rl[RLIMIT_NOFILE]->rlim_cur == rl[RLIMIT_NOFILE]->rlim_max); | |
754 | ||
4f424df7 | 755 | assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "55:66", rl, NULL) >= 0); |
91518d20 KZ |
756 | assert_se(rl[RLIMIT_NOFILE]); |
757 | assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 55); | |
758 | assert_se(rl[RLIMIT_NOFILE]->rlim_max == 66); | |
759 | ||
4f424df7 | 760 | assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "infinity", rl, NULL) >= 0); |
a4c18002 LP |
761 | assert_se(rl[RLIMIT_NOFILE]); |
762 | assert_se(rl[RLIMIT_NOFILE]->rlim_cur == RLIM_INFINITY); | |
763 | assert_se(rl[RLIMIT_NOFILE]->rlim_cur == rl[RLIMIT_NOFILE]->rlim_max); | |
764 | ||
4f424df7 | 765 | assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "infinity:infinity", rl, NULL) >= 0); |
91518d20 KZ |
766 | assert_se(rl[RLIMIT_NOFILE]); |
767 | assert_se(rl[RLIMIT_NOFILE]->rlim_cur == RLIM_INFINITY); | |
768 | assert_se(rl[RLIMIT_NOFILE]->rlim_cur == rl[RLIMIT_NOFILE]->rlim_max); | |
769 | ||
d0a7c5f6 LP |
770 | rl[RLIMIT_NOFILE]->rlim_cur = 10; |
771 | rl[RLIMIT_NOFILE]->rlim_max = 20; | |
772 | ||
773 | /* Invalid values don't change rl */ | |
4f424df7 | 774 | assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "10:20:30", rl, NULL) >= 0); |
0316f2ae EV |
775 | assert_se(rl[RLIMIT_NOFILE]); |
776 | assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 10); | |
777 | assert_se(rl[RLIMIT_NOFILE]->rlim_max == 20); | |
778 | ||
4f424df7 | 779 | assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "wat:wat", rl, NULL) >= 0); |
0316f2ae EV |
780 | assert_se(rl[RLIMIT_NOFILE]); |
781 | assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 10); | |
782 | assert_se(rl[RLIMIT_NOFILE]->rlim_max == 20); | |
783 | ||
4f424df7 | 784 | assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "66:wat", rl, NULL) >= 0); |
0316f2ae EV |
785 | assert_se(rl[RLIMIT_NOFILE]); |
786 | assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 10); | |
787 | assert_se(rl[RLIMIT_NOFILE]->rlim_max == 20); | |
788 | ||
4f424df7 | 789 | assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "200:100", rl, NULL) >= 0); |
0316f2ae EV |
790 | assert_se(rl[RLIMIT_NOFILE]); |
791 | assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 10); | |
792 | assert_se(rl[RLIMIT_NOFILE]->rlim_max == 20); | |
793 | ||
a4c18002 LP |
794 | rl[RLIMIT_NOFILE] = mfree(rl[RLIMIT_NOFILE]); |
795 | ||
4f424df7 | 796 | assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "56", rl, NULL) >= 0); |
a4c18002 LP |
797 | assert_se(rl[RLIMIT_CPU]); |
798 | assert_se(rl[RLIMIT_CPU]->rlim_cur == 56); | |
799 | assert_se(rl[RLIMIT_CPU]->rlim_cur == rl[RLIMIT_CPU]->rlim_max); | |
800 | ||
4f424df7 | 801 | assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "57s", rl, NULL) >= 0); |
a4c18002 LP |
802 | assert_se(rl[RLIMIT_CPU]); |
803 | assert_se(rl[RLIMIT_CPU]->rlim_cur == 57); | |
804 | assert_se(rl[RLIMIT_CPU]->rlim_cur == rl[RLIMIT_CPU]->rlim_max); | |
805 | ||
4f424df7 | 806 | assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "40s:1m", rl, NULL) >= 0); |
91518d20 KZ |
807 | assert_se(rl[RLIMIT_CPU]); |
808 | assert_se(rl[RLIMIT_CPU]->rlim_cur == 40); | |
809 | assert_se(rl[RLIMIT_CPU]->rlim_max == 60); | |
810 | ||
4f424df7 | 811 | assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "infinity", rl, NULL) >= 0); |
a4c18002 LP |
812 | assert_se(rl[RLIMIT_CPU]); |
813 | assert_se(rl[RLIMIT_CPU]->rlim_cur == RLIM_INFINITY); | |
814 | assert_se(rl[RLIMIT_CPU]->rlim_cur == rl[RLIMIT_CPU]->rlim_max); | |
815 | ||
4f424df7 | 816 | assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "1234ms", rl, NULL) >= 0); |
a4c18002 LP |
817 | assert_se(rl[RLIMIT_CPU]); |
818 | assert_se(rl[RLIMIT_CPU]->rlim_cur == 2); | |
819 | assert_se(rl[RLIMIT_CPU]->rlim_cur == rl[RLIMIT_CPU]->rlim_max); | |
820 | ||
821 | rl[RLIMIT_CPU] = mfree(rl[RLIMIT_CPU]); | |
822 | ||
4f424df7 | 823 | assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "58", rl, NULL) >= 0); |
a4c18002 LP |
824 | assert_se(rl[RLIMIT_RTTIME]); |
825 | assert_se(rl[RLIMIT_RTTIME]->rlim_cur == 58); | |
826 | assert_se(rl[RLIMIT_RTTIME]->rlim_cur == rl[RLIMIT_RTTIME]->rlim_max); | |
827 | ||
4f424df7 | 828 | assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "58:60", rl, NULL) >= 0); |
91518d20 KZ |
829 | assert_se(rl[RLIMIT_RTTIME]); |
830 | assert_se(rl[RLIMIT_RTTIME]->rlim_cur == 58); | |
831 | assert_se(rl[RLIMIT_RTTIME]->rlim_max == 60); | |
832 | ||
4f424df7 | 833 | assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "59s", rl, NULL) >= 0); |
a4c18002 LP |
834 | assert_se(rl[RLIMIT_RTTIME]); |
835 | assert_se(rl[RLIMIT_RTTIME]->rlim_cur == 59 * USEC_PER_SEC); | |
836 | assert_se(rl[RLIMIT_RTTIME]->rlim_cur == rl[RLIMIT_RTTIME]->rlim_max); | |
837 | ||
4f424df7 | 838 | assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "59s:123s", rl, NULL) >= 0); |
91518d20 KZ |
839 | assert_se(rl[RLIMIT_RTTIME]); |
840 | assert_se(rl[RLIMIT_RTTIME]->rlim_cur == 59 * USEC_PER_SEC); | |
841 | assert_se(rl[RLIMIT_RTTIME]->rlim_max == 123 * USEC_PER_SEC); | |
842 | ||
4f424df7 | 843 | assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "infinity", rl, NULL) >= 0); |
a4c18002 LP |
844 | assert_se(rl[RLIMIT_RTTIME]); |
845 | assert_se(rl[RLIMIT_RTTIME]->rlim_cur == RLIM_INFINITY); | |
846 | assert_se(rl[RLIMIT_RTTIME]->rlim_cur == rl[RLIMIT_RTTIME]->rlim_max); | |
847 | ||
4f424df7 | 848 | assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "infinity:infinity", rl, NULL) >= 0); |
91518d20 KZ |
849 | assert_se(rl[RLIMIT_RTTIME]); |
850 | assert_se(rl[RLIMIT_RTTIME]->rlim_cur == RLIM_INFINITY); | |
851 | assert_se(rl[RLIMIT_RTTIME]->rlim_cur == rl[RLIMIT_RTTIME]->rlim_max); | |
852 | ||
4f424df7 | 853 | assert_se(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "2345ms", rl, NULL) >= 0); |
a4c18002 LP |
854 | assert_se(rl[RLIMIT_RTTIME]); |
855 | assert_se(rl[RLIMIT_RTTIME]->rlim_cur == 2345 * USEC_PER_MSEC); | |
856 | assert_se(rl[RLIMIT_RTTIME]->rlim_cur == rl[RLIMIT_RTTIME]->rlim_max); | |
857 | ||
858 | rl[RLIMIT_RTTIME] = mfree(rl[RLIMIT_RTTIME]); | |
859 | } | |
860 | ||
4ad4beec EV |
861 | static void test_config_parse_pass_environ(void) { |
862 | /* int config_parse_pass_environ( | |
863 | const char *unit, | |
864 | const char *filename, | |
865 | unsigned line, | |
866 | const char *section, | |
867 | unsigned section_line, | |
868 | const char *lvalue, | |
869 | int ltype, | |
870 | const char *rvalue, | |
871 | void *data, | |
872 | void *userdata) */ | |
873 | int r; | |
874 | _cleanup_strv_free_ char **passenv = NULL; | |
875 | ||
876 | r = config_parse_pass_environ(NULL, "fake", 1, "section", 1, | |
877 | "PassEnvironment", 0, "A B", | |
878 | &passenv, NULL); | |
879 | assert_se(r >= 0); | |
880 | assert_se(strv_length(passenv) == 2); | |
881 | assert_se(streq(passenv[0], "A")); | |
882 | assert_se(streq(passenv[1], "B")); | |
883 | ||
884 | r = config_parse_pass_environ(NULL, "fake", 1, "section", 1, | |
885 | "PassEnvironment", 0, "", | |
886 | &passenv, NULL); | |
887 | assert_se(r >= 0); | |
888 | assert_se(strv_isempty(passenv)); | |
889 | ||
890 | r = config_parse_pass_environ(NULL, "fake", 1, "section", 1, | |
891 | "PassEnvironment", 0, "'invalid name' 'normal_name' A=1 \\", | |
892 | &passenv, NULL); | |
893 | assert_se(r >= 0); | |
894 | assert_se(strv_length(passenv) == 1); | |
895 | assert_se(streq(passenv[0], "normal_name")); | |
896 | ||
897 | } | |
898 | ||
0c700d39 EV |
899 | static void test_unit_dump_config_items(void) { |
900 | unit_dump_config_items(stdout); | |
901 | } | |
902 | ||
2c5417ad | 903 | int main(int argc, char *argv[]) { |
f942504e | 904 | _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL; |
751e7576 | 905 | int r; |
2c5417ad | 906 | |
6d7c4033 | 907 | test_setup_logging(LOG_INFO); |
c1b6628d | 908 | |
651d47d1 | 909 | r = enter_cgroup_subroot(); |
317bb217 ZJS |
910 | if (r == -ENOMEDIUM) |
911 | return log_tests_skipped("cgroupfs not available"); | |
8c759b33 | 912 | |
d2120590 LP |
913 | assert_se(runtime_dir = setup_fake_runtime_dir()); |
914 | ||
751e7576 | 915 | r = test_unit_file_get_set(); |
2c5417ad | 916 | test_config_parse_exec(); |
88bff424 | 917 | test_config_parse_log_extra_fields(); |
a103496c | 918 | test_config_parse_capability_set(); |
a4c18002 | 919 | test_config_parse_rlimit(); |
4ad4beec | 920 | test_config_parse_pass_environ(); |
b9893505 ZJS |
921 | test_load_env_file_1(); |
922 | test_load_env_file_2(); | |
a3aa7ee6 | 923 | test_load_env_file_3(); |
d3b6d0c2 | 924 | test_load_env_file_4(); |
58f10d40 | 925 | test_load_env_file_5(); |
143bfdaf | 926 | TEST_REQ_RUNNING_SYSTEMD(test_install_printf()); |
0c700d39 | 927 | test_unit_dump_config_items(); |
b5b46d59 | 928 | |
751e7576 | 929 | return r; |
b5b46d59 | 930 | } |