Debian Codegraph @

src/test/test-load-fragment.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4 #include <unistd.h>
5
6 #include "sd-daemon.h"
7 #include "sd-id128.h"
8
9 #include "all-units.h"
10 #include "alloc-util.h"
11 #include "capability-util.h"
12 #include "conf-parser.h"
13 #include "fileio.h"
14 #include "format-util.h"
15 #include "hashmap.h"
16 #include "hostname-setup.h"
17 #include "install.h"
18 #include "install-printf.h"
19 #include "load-fragment.h"
20 #include "manager.h"
21 #include "open-file.h"
22 #include "pcre2-util.h"
23 #include "rm-rf.h"
24 #include "service.h"
25 #include "set.h"
26 #include "specifier.h"
27 #include "string-util.h"
28 #include "strv.h"
29 #include "tests.h"
30 #include "user-util.h"
31
32 /* Nontrivial value serves as a placeholder to check that parsing function (didn't) change it */
33 #define CGROUP_LIMIT_DUMMY 3
34
35 static char *runtime_dir = NULL;
36
37 STATIC_DESTRUCTOR_REGISTER(runtime_dir, rm_rf_physical_and_freep);
38
39 /* For testing type compatibility. */
40 _unused_ static ConfigPerfItemLookup unused_lookup = load_fragment_gperf_lookup;
41
42 TEST_RET(unit_file_get_list) {
43 int r;
44 _cleanup_hashmap_free_ Hashmap *h = NULL;
45 UnitFileList *p;
46
47 r = unit_file_get_list(RUNTIME_SCOPE_SYSTEM, NULL, NULL, NULL, &h);
48 if (IN_SET(r, -EPERM, -EACCES))
49 return log_tests_skipped_errno(r, "unit_file_get_list");
50
51 log_full_errno(r == 0 ? LOG_INFO : LOG_ERR, r,
52 "unit_file_get_list: %m");
53 if (r < 0)
54 return EXIT_FAILURE;
55
56 HASHMAP_FOREACH(p, h)
57 printf("%s = %s\n", p->path, unit_file_state_to_string(p->state));
58
59 return 0;
60 }
61
62 static void check_execcommand(ExecCommand *c,
63 const char* path,
64 const char* argv0,
65 const char* argv1,
66 const char* argv2,
67 bool ignore) {
68 size_t n;
69
70 ASSERT_NOT_NULL(c);
71 log_info("expect: \"%s\" [\"%s\" \"%s\" \"%s\"]",
72 path, argv0 ?: path, strnull(argv1), strnull(argv2));
73 n = strv_length(c->argv);
74 log_info("actual: \"%s\" [\"%s\" \"%s\" \"%s\"]",
75 c->path, c->argv[0], n > 0 ? c->argv[1] : "(null)", n > 1 ? c->argv[2] : "(null)");
76 ASSERT_STREQ(c->path, path);
77 ASSERT_STREQ(c->argv[0], argv0 ?: path);
78 if (n > 0)
79 ASSERT_STREQ(c->argv[1], argv1);
80 if (n > 1)
81 ASSERT_STREQ(c->argv[2], argv2);
82 ASSERT_EQ(FLAGS_SET(c->flags, EXEC_COMMAND_IGNORE_FAILURE), ignore);
83 }
84
85 TEST(config_parse_exec) {
86 /* int config_parse_exec(
87 const char *unit,
88 const char *filename,
89 unsigned line,
90 const char *section,
91 unsigned section_line,
92 const char *lvalue,
93 int ltype,
94 const char *rvalue,
95 void *data,
96 void *userdata) */
97 int r;
98
99 ExecCommand *c = NULL, *c1;
100 const char *ccc;
101 _cleanup_(manager_freep) Manager *m = NULL;
102 _cleanup_(unit_freep) Unit *u = NULL;
103
104 r = manager_new(RUNTIME_SCOPE_USER, MANAGER_TEST_RUN_MINIMAL, &m);
105 if (manager_errno_skip_test(r)) {
106 log_notice_errno(r, "Skipping test: manager_new: %m");
107 return;
108 }
109
110 ASSERT_OK(r);
111 ASSERT_OK(manager_startup(m, NULL, NULL, NULL, NULL));
112
113 ASSERT_NOT_NULL(u = unit_new(m, sizeof(Service)));
114
115 log_info("/* basic test */");
116 ASSERT_OK(config_parse_exec(NULL, "fake", 1, "section", 1,
117 "LValue", 0, "/RValue r1",
118 &c, u));
119 check_execcommand(c, "/RValue", "/RValue", "r1", NULL, false);
120
121 log_info("/* test slashes */");
122 ASSERT_OK(config_parse_exec(NULL, "fake", 2, "section", 1,
123 "LValue", 0, "/RValue///slashes r1///",
124 &c, u));
125 c1 = c->command_next;
126 check_execcommand(c1, "/RValue/slashes", "/RValue///slashes", "r1///", NULL, false);
127
128 log_info("/* trailing slash */");
129 ASSERT_ERROR(config_parse_exec(NULL, "fake", 4, "section", 1,
130 "LValue", 0, "/RValue/ argv0 r1",
131 &c, u), ENOEXEC);
132 ASSERT_NULL(c1->command_next);
133
134 log_info("/* honour_argv0 */");
135 ASSERT_OK(config_parse_exec(NULL, "fake", 3, "section", 1,
136 "LValue", 0, "@/RValue///slashes2 ///argv0 r1",
137 &c, u));
138 c1 = c1->command_next;
139 check_execcommand(c1, "/RValue/slashes2", "///argv0", "r1", NULL, false);
140
141 log_info("/* honour_argv0, no args */");
142 ASSERT_ERROR(config_parse_exec(NULL, "fake", 3, "section", 1,
143 "LValue", 0, "@/RValue",
144 &c, u), ENOEXEC);
145 ASSERT_NULL(c1->command_next);
146
147 log_info("/* no command, whitespace only, reset */");
148 ASSERT_OK_ZERO(config_parse_exec(NULL, "fake", 3, "section", 1,
149 "LValue", 0, "",
150 &c, u));
151 ASSERT_NULL(c);
152
153 log_info("/* ignore && honour_argv0 */");
154 ASSERT_OK(config_parse_exec(NULL, "fake", 4, "section", 1,
155 "LValue", 0, "-@/RValue///slashes3 argv0a r1",
156 &c, u));
157 c1 = c;
158 check_execcommand(c1, "/RValue/slashes3", "argv0a", "r1", NULL, true);
159
160 log_info("/* ignore && honour_argv0 */");
161 ASSERT_OK(config_parse_exec(NULL, "fake", 4, "section", 1,
162 "LValue", 0, "@-/RValue///slashes4 argv0b r1",
163 &c, u));
164 c1 = c1->command_next;
165 check_execcommand(c1, "/RValue/slashes4", "argv0b", "r1", NULL, true);
166
167 log_info("/* ignore && ignore */");
168 ASSERT_OK_ZERO(config_parse_exec(NULL, "fake", 4, "section", 1,
169 "LValue", 0, "--/RValue argv0 r1",
170 &c, u));
171 ASSERT_NULL(c1->command_next);
172
173 log_info("/* ignore && ignore (2) */");
174 ASSERT_OK_ZERO(config_parse_exec(NULL, "fake", 4, "section", 1,
175 "LValue", 0, "-@-/RValue argv0 r1",
176 &c, u));
177 ASSERT_NULL(c1->command_next);
178
179 log_info("/* semicolon */");
180 ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1,
181 "LValue", 0,
182 "-@/RValue argv0 r1 ; "
183 "/goo/goo boo",
184 &c, u));
185 c1 = c1->command_next;
186 check_execcommand(c1, "/RValue", "argv0", "r1", NULL, true);
187
188 c1 = c1->command_next;
189 check_execcommand(c1, "/goo/goo", NULL, "boo", NULL, false);
190
191 log_info("/* two semicolons in a row */");
192 ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1,
193 "LValue", 0,
194 "-@/RValue argv0 r1 ; ; "
195 "/goo/goo boo",
196 &c, u));
197 c1 = c1->command_next;
198 check_execcommand(c1, "/RValue", "argv0", "r1", NULL, true);
199 c1 = c1->command_next;
200 check_execcommand(c1, "/goo/goo", "/goo/goo", "boo", NULL, false);
201
202 log_info("/* trailing semicolon */");
203 ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1,
204 "LValue", 0,
205 "-@/RValue argv0 r1 ; ",
206 &c, u));
207 c1 = c1->command_next;
208 check_execcommand(c1, "/RValue", "argv0", "r1", NULL, true);
209
210 ASSERT_NULL(c1->command_next);
211
212 log_info("/* trailing semicolon, no whitespace */");
213 ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1,
214 "LValue", 0,
215 "-@/RValue argv0 r1 ;",
216 &c, u));
217 c1 = c1->command_next;
218 check_execcommand(c1, "/RValue", "argv0", "r1", NULL, true);
219
220 ASSERT_NULL(c1->command_next);
221
222 log_info("/* trailing semicolon in single quotes */");
223 ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1,
224 "LValue", 0,
225 "-@/RValue argv0 r1 ';'",
226 &c, u));
227 c1 = c1->command_next;
228 check_execcommand(c1, "/RValue", "argv0", "r1", ";", true);
229
230 log_info("/* escaped semicolon */");
231 ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1,
232 "LValue", 0,
233 "/bin/find \\;",
234 &c, u));
235 c1 = c1->command_next;
236 check_execcommand(c1, "/bin/find", NULL, ";", NULL, false);
237
238 log_info("/* escaped semicolon with following arg */");
239 ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1,
240 "LValue", 0,
241 "/sbin/find \\; /x",
242 &c, u));
243 c1 = c1->command_next;
244 check_execcommand(c1,
245 "/sbin/find", NULL, ";", "/x", false);
246
247 log_info("/* escaped semicolon as part of an expression */");
248 ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1,
249 "LValue", 0,
250 "/sbin/find \\;x",
251 &c, u));
252 c1 = c1->command_next;
253 check_execcommand(c1,
254 "/sbin/find", NULL, "\\;x", NULL, false);
255
256 log_info("/* encoded semicolon */");
257 ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1,
258 "LValue", 0,
259 "/bin/find \\073",
260 &c, u));
261 c1 = c1->command_next;
262 check_execcommand(c1, "/bin/find", NULL, ";", NULL, false);
263
264 log_info("/* quoted semicolon */");
265 ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1,
266 "LValue", 0,
267 "/bin/find \";\"",
268 &c, u));
269 c1 = c1->command_next;
270 check_execcommand(c1, "/bin/find", NULL, ";", NULL, false);
271
272 log_info("/* quoted semicolon with following arg */");
273 ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1,
274 "LValue", 0,
275 "/sbin/find \";\" /x",
276 &c, u));
277 c1 = c1->command_next;
278 check_execcommand(c1,
279 "/sbin/find", NULL, ";", "/x", false);
280
281 log_info("/* spaces in the filename */");
282 ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1,
283 "LValue", 0,
284 "\"/PATH WITH SPACES/daemon\" -1 -2",
285 &c, u));
286 c1 = c1->command_next;
287 check_execcommand(c1,
288 "/PATH WITH SPACES/daemon", NULL, "-1", "-2", false);
289
290 log_info("/* spaces in the filename, no args */");
291 ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1,
292 "LValue", 0,
293 "\"/PATH WITH SPACES/daemon -1 -2\"",
294 &c, u));
295 c1 = c1->command_next;
296 check_execcommand(c1,
297 "/PATH WITH SPACES/daemon -1 -2", NULL, NULL, NULL, false);
298
299 log_info("/* spaces in the filename, everything quoted */");
300 ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1,
301 "LValue", 0,
302 "\"/PATH WITH SPACES/daemon\" \"-1\" '-2'",
303 &c, u));
304 c1 = c1->command_next;
305 check_execcommand(c1,
306 "/PATH WITH SPACES/daemon", NULL, "-1", "-2", false);
307
308 log_info("/* escaped spaces in the filename */");
309 ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1,
310 "LValue", 0,
311 "\"/PATH\\sWITH\\sSPACES/daemon\" '-1 -2'",
312 &c, u));
313 c1 = c1->command_next;
314 check_execcommand(c1,
315 "/PATH WITH SPACES/daemon", NULL, "-1 -2", NULL, false);
316
317 log_info("/* escaped spaces in the filename (2) */");
318 ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1,
319 "LValue", 0,
320 "\"/PATH\\x20WITH\\x20SPACES/daemon\" \"-1 -2\"",
321 &c, u));
322 c1 = c1->command_next;
323 check_execcommand(c1,
324 "/PATH WITH SPACES/daemon", NULL, "-1 -2", NULL, false);
325
326 for (ccc = "abfnrtv\\\'\"x"; *ccc; ccc++) {
327 /* \\x is an incomplete hexadecimal sequence, invalid because of the slash */
328 char path[] = "/path\\X";
329 path[sizeof(path) - 2] = *ccc;
330
331 log_info("/* invalid character: \\%c */", *ccc);
332 ASSERT_ERROR(config_parse_exec(NULL, "fake", 4, "section", 1,
333 "LValue", 0, path,
334 &c, u), ENOEXEC);
335 ASSERT_NULL(c1->command_next);
336 }
337
338 log_info("/* valid character: \\s */");
339 ASSERT_OK(config_parse_exec(NULL, "fake", 4, "section", 1,
340 "LValue", 0, "/path\\s",
341 &c, u));
342 c1 = c1->command_next;
343 check_execcommand(c1, "/path ", NULL, NULL, NULL, false);
344
345 log_info("/* quoted backslashes */");
346 ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1,
347 "LValue", 0,
348 "/bin/grep '\\w+\\K'",
349 &c, u));
350 c1 = c1->command_next;
351 check_execcommand(c1, "/bin/grep", NULL, "\\w+\\K", NULL, false);
352
353 log_info("/* trailing backslash: \\ */");
354 /* backslash is invalid */
355 ASSERT_ERROR(config_parse_exec(NULL, "fake", 4, "section", 1,
356 "LValue", 0, "/path\\",
357 &c, u), ENOEXEC);
358 ASSERT_NULL(c1->command_next);
359
360 log_info("/* missing ending ' */");
361 ASSERT_ERROR(config_parse_exec(NULL, "fake", 4, "section", 1,
362 "LValue", 0, "/path 'foo",
363 &c, u), ENOEXEC);
364 ASSERT_NULL(c1->command_next);
365
366 log_info("/* missing ending ' with trailing backslash */");
367 ASSERT_ERROR(config_parse_exec(NULL, "fake", 4, "section", 1,
368 "LValue", 0, "/path 'foo\\",
369 &c, u), ENOEXEC);
370 ASSERT_NULL(c1->command_next);
371
372 log_info("/* invalid space between modifiers */");
373 ASSERT_OK_ZERO(config_parse_exec(NULL, "fake", 4, "section", 1,
374 "LValue", 0, "- /path",
375 &c, u));
376 ASSERT_NULL(c1->command_next);
377
378 log_info("/* only modifiers, no path */");
379 ASSERT_OK_ZERO(config_parse_exec(NULL, "fake", 4, "section", 1,
380 "LValue", 0, "-",
381 &c, u));
382 ASSERT_NULL(c1->command_next);
383
384 log_info("/* long arg */"); /* See issue #22957. */
385
386 char x[LONG_LINE_MAX-100], *y;
387 y = mempcpy(x, "/bin/echo ", STRLEN("/bin/echo "));
388 memset(y, 'x', sizeof(x) - STRLEN("/bin/echo ") - 1);
389 x[sizeof(x) - 1] = '\0';
390
391 ASSERT_OK(config_parse_exec(NULL, "fake", 5, "section", 1,
392 "LValue", 0, x,
393 &c, u));
394 c1 = c1->command_next;
395 check_execcommand(c1,
396 "/bin/echo", NULL, y, NULL, false);
397
398 log_info("/* empty argument, reset */");
399 ASSERT_OK_ZERO(config_parse_exec(NULL, "fake", 4, "section", 1,
400 "LValue", 0, "",
401 &c, u));
402 ASSERT_NULL(c);
403
404 exec_command_free_list(c);
405 }
406
407 TEST(config_parse_log_extra_fields) {
408 /* int config_parse_log_extra_fields(
409 const char *unit,
410 const char *filename,
411 unsigned line,
412 const char *section,
413 unsigned section_line,
414 const char *lvalue,
415 int ltype,
416 const char *rvalue,
417 void *data,
418 void *userdata) */
419
420 int r;
421
422 _cleanup_(manager_freep) Manager *m = NULL;
423 _cleanup_(unit_freep) Unit *u = NULL;
424 ExecContext c = {};
425
426 r = manager_new(RUNTIME_SCOPE_USER, MANAGER_TEST_RUN_MINIMAL, &m);
427 if (manager_errno_skip_test(r)) {
428 log_notice_errno(r, "Skipping test: manager_new: %m");
429 return;
430 }
431
432 ASSERT_OK(r);
433 ASSERT_OK(manager_startup(m, NULL, NULL, NULL, NULL));
434
435 ASSERT_NOT_NULL(u = unit_new(m, sizeof(Service)));
436
437 log_info("/* %s – basic test */", __func__);
438 ASSERT_OK(config_parse_log_extra_fields(NULL, "fake", 1, "section", 1,
439 "LValue", 0, "FOO=BAR \"QOOF=quux ' ' \"",
440 &c, u));
441 ASSERT_EQ(c.n_log_extra_fields, 2U);
442 ASSERT_STRNEQ(c.log_extra_fields[0].iov_base, "FOO=BAR", c.log_extra_fields[0].iov_len);
443 ASSERT_STRNEQ(c.log_extra_fields[1].iov_base, "QOOF=quux ' ' ", c.log_extra_fields[1].iov_len);
444
445 log_info("/* %s – add some */", __func__);
446 ASSERT_OK(config_parse_log_extra_fields(NULL, "fake", 1, "section", 1,
447 "LValue", 0, "FOO2=BAR2 QOOF2=quux ' '",
448 &c, u));
449 ASSERT_EQ(c.n_log_extra_fields, 4U);
450 ASSERT_STRNEQ(c.log_extra_fields[0].iov_base, "FOO=BAR", c.log_extra_fields[0].iov_len);
451 ASSERT_STRNEQ(c.log_extra_fields[1].iov_base, "QOOF=quux ' ' ", c.log_extra_fields[1].iov_len);
452 ASSERT_STRNEQ(c.log_extra_fields[2].iov_base, "FOO2=BAR2", c.log_extra_fields[2].iov_len);
453 ASSERT_STRNEQ(c.log_extra_fields[3].iov_base, "QOOF2=quux", c.log_extra_fields[3].iov_len);
454
455 exec_context_dump(&c, stdout, " --> ");
456
457 log_info("/* %s – reset */", __func__);
458 ASSERT_OK(config_parse_log_extra_fields(NULL, "fake", 1, "section", 1,
459 "LValue", 0, "",
460 &c, u));
461 ASSERT_EQ(c.n_log_extra_fields, 0U);
462
463 exec_context_free_log_extra_fields(&c);
464
465 log_info("/* %s – bye */", __func__);
466 }
467
468 TEST(install_printf, .sd_booted = true) {
469 char name[] = "name.service",
470 path[] = "/run/systemd/system/name.service";
471 InstallInfo i = { .name = name, .path = path, };
472 InstallInfo i2 = { .name= name, .path = path, };
473 char name3[] = "[email protected]",
474 path3[] = "/run/systemd/system/name.service";
475 InstallInfo i3 = { .name = name3, .path = path3, };
476 InstallInfo i4 = { .name = name3, .path = path3, };
477
478 _cleanup_free_ char *mid = NULL, *bid = NULL, *host = NULL, *gid = NULL, *group = NULL, *uid = NULL, *user = NULL;
479
480 if (sd_id128_get_machine(NULL) >= 0) {
481 ASSERT_OK(specifier_machine_id('m', NULL, NULL, NULL, &mid));
482 ASSERT_NOT_NULL(mid);
483 }
484 if (sd_booted() > 0) {
485 ASSERT_OK(specifier_boot_id('b', NULL, NULL, NULL, &bid));
486 ASSERT_NOT_NULL(bid);
487 }
488 ASSERT_NOT_NULL(host = gethostname_malloc());
489 ASSERT_NOT_NULL(group = gid_to_name(getgid()));
490 ASSERT_OK(asprintf(&gid, UID_FMT, getgid()));
491 ASSERT_NOT_NULL(user = uid_to_name(getuid()));
492 ASSERT_OK(asprintf(&uid, UID_FMT, getuid()));
493
494 #define expect(scope, src, pattern, result) \
495 do { \
496 _cleanup_free_ char *t = NULL, \
497 *d1 = ASSERT_PTR(strdup(i.name)), \
498 *d2 = ASSERT_PTR(strdup(i.path)); \
499 int r = install_name_printf(scope, &src, pattern, &t); \
500 if (result) \
501 ASSERT_OK(r); \
502 else \
503 ASSERT_FAIL(r); \
504 memzero(i.name, strlen(i.name)); \
505 memzero(i.path, strlen(i.path)); \
506 if (result) { \
507 printf("%s\n", t); \
508 ASSERT_STREQ(t, result); \
509 } else \
510 ASSERT_NULL(t); \
511 strcpy(i.name, d1); \
512 strcpy(i.path, d2); \
513 } while (false)
514
515 expect(RUNTIME_SCOPE_SYSTEM, i, "%n", "name.service");
516 expect(RUNTIME_SCOPE_SYSTEM, i, "%N", "name");
517 expect(RUNTIME_SCOPE_SYSTEM, i, "%p", "name");
518 expect(RUNTIME_SCOPE_SYSTEM, i, "%i", "");
519 expect(RUNTIME_SCOPE_SYSTEM, i, "%j", "name");
520 expect(RUNTIME_SCOPE_SYSTEM, i, "%g", "root");
521 expect(RUNTIME_SCOPE_SYSTEM, i, "%G", "0");
522 expect(RUNTIME_SCOPE_SYSTEM, i, "%u", "root");
523 expect(RUNTIME_SCOPE_SYSTEM, i, "%U", "0");
524
525 expect(RUNTIME_SCOPE_SYSTEM, i, "%m", mid);
526 expect(RUNTIME_SCOPE_SYSTEM, i, "%b", bid);
527 expect(RUNTIME_SCOPE_SYSTEM, i, "%H", host);
528
529 expect(RUNTIME_SCOPE_SYSTEM, i2, "%g", "root");
530 expect(RUNTIME_SCOPE_SYSTEM, i2, "%G", "0");
531 expect(RUNTIME_SCOPE_SYSTEM, i2, "%u", "root");
532 expect(RUNTIME_SCOPE_SYSTEM, i2, "%U", "0");
533
534 expect(RUNTIME_SCOPE_USER, i2, "%g", group);
535 expect(RUNTIME_SCOPE_USER, i2, "%G", gid);
536 expect(RUNTIME_SCOPE_USER, i2, "%u", user);
537 expect(RUNTIME_SCOPE_USER, i2, "%U", uid);
538
539 /* gcc-12.0.1-0.9.fc36.x86_64 insist that streq(…, NULL) is called,
540 * even though the call is inside of a conditional where the pointer is checked. :( */
541 #pragma GCC diagnostic push
542 #pragma GCC diagnostic ignored "-Wnonnull"
543 expect(RUNTIME_SCOPE_GLOBAL, i2, "%g", NULL);
544 expect(RUNTIME_SCOPE_GLOBAL, i2, "%G", NULL);
545 expect(RUNTIME_SCOPE_GLOBAL, i2, "%u", NULL);
546 expect(RUNTIME_SCOPE_GLOBAL, i2, "%U", NULL);
547 #pragma GCC diagnostic pop
548
549 expect(RUNTIME_SCOPE_SYSTEM, i3, "%n", "[email protected]");
550 expect(RUNTIME_SCOPE_SYSTEM, i3, "%N", "name@inst");
551 expect(RUNTIME_SCOPE_SYSTEM, i3, "%p", "name");
552 expect(RUNTIME_SCOPE_USER, i3, "%g", group);
553 expect(RUNTIME_SCOPE_USER, i3, "%G", gid);
554 expect(RUNTIME_SCOPE_USER, i3, "%u", user);
555 expect(RUNTIME_SCOPE_USER, i3, "%U", uid);
556
557 expect(RUNTIME_SCOPE_SYSTEM, i3, "%m", mid);
558 expect(RUNTIME_SCOPE_SYSTEM, i3, "%b", bid);
559 expect(RUNTIME_SCOPE_SYSTEM, i3, "%H", host);
560
561 expect(RUNTIME_SCOPE_USER, i4, "%g", group);
562 expect(RUNTIME_SCOPE_USER, i4, "%G", gid);
563 expect(RUNTIME_SCOPE_USER, i4, "%u", user);
564 expect(RUNTIME_SCOPE_USER, i4, "%U", uid);
565 }
566
567 static uint64_t make_cap(int cap) {
568 return ((uint64_t) 1ULL << (uint64_t) cap);
569 }
570
571 TEST(config_parse_capability_set) {
572 /* int config_parse_capability_set(
573 const char *unit,
574 const char *filename,
575 unsigned line,
576 const char *section,
577 unsigned section_line,
578 const char *lvalue,
579 int ltype,
580 const char *rvalue,
581 void *data,
582 void *userdata) */
583
584 uint64_t capability_bounding_set = 0;
585
586 ASSERT_OK(config_parse_capability_set(NULL, "fake", 1, "section", 1,
587 "CapabilityBoundingSet", 0, "CAP_NET_RAW",
588 &capability_bounding_set, NULL));
589 ASSERT_EQ(capability_bounding_set, make_cap(CAP_NET_RAW));
590
591 ASSERT_OK(config_parse_capability_set(NULL, "fake", 1, "section", 1,
592 "CapabilityBoundingSet", 0, "CAP_NET_ADMIN",
593 &capability_bounding_set, NULL));
594 ASSERT_EQ(capability_bounding_set, make_cap(CAP_NET_RAW) | make_cap(CAP_NET_ADMIN));
595
596 ASSERT_OK(config_parse_capability_set(NULL, "fake", 1, "section", 1,
597 "CapabilityBoundingSet", 0, "~CAP_NET_ADMIN",
598 &capability_bounding_set, NULL));
599 ASSERT_EQ(capability_bounding_set, make_cap(CAP_NET_RAW));
600
601 ASSERT_OK(config_parse_capability_set(NULL, "fake", 1, "section", 1,
602 "CapabilityBoundingSet", 0, "",
603 &capability_bounding_set, NULL));
604 ASSERT_EQ(capability_bounding_set, UINT64_C(0));
605
606 ASSERT_OK(config_parse_capability_set(NULL, "fake", 1, "section", 1,
607 "CapabilityBoundingSet", 0, "~",
608 &capability_bounding_set, NULL));
609 ASSERT_TRUE(cap_test_all(capability_bounding_set));
610
611 capability_bounding_set = 0;
612 ASSERT_OK(config_parse_capability_set(NULL, "fake", 1, "section", 1,
613 "CapabilityBoundingSet", 0, " 'CAP_NET_RAW' WAT_CAP??? CAP_NET_ADMIN CAP'_trailing_garbage",
614 &capability_bounding_set, NULL));
615 ASSERT_EQ(capability_bounding_set, make_cap(CAP_NET_RAW) | make_cap(CAP_NET_ADMIN));
616 }
617
618 TEST(config_parse_rlimit) {
619 struct rlimit * rl[_RLIMIT_MAX] = {};
620
621 ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "55", rl, NULL));
622 ASSERT_NOT_NULL(rl[RLIMIT_NOFILE]);
623 ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_cur, 55U);
624 ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_cur, rl[RLIMIT_NOFILE]->rlim_max);
625
626 ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "55:66", rl, NULL));
627 ASSERT_NOT_NULL(rl[RLIMIT_NOFILE]);
628 ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_cur, 55U);
629 ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_max, 66U);
630
631 ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "infinity", rl, NULL));
632 ASSERT_NOT_NULL(rl[RLIMIT_NOFILE]);
633 ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_cur, RLIM_INFINITY);
634 ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_cur, rl[RLIMIT_NOFILE]->rlim_max);
635
636 ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "infinity:infinity", rl, NULL));
637 ASSERT_NOT_NULL(rl[RLIMIT_NOFILE]);
638 ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_cur, RLIM_INFINITY);
639 ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_cur, rl[RLIMIT_NOFILE]->rlim_max);
640
641 rl[RLIMIT_NOFILE]->rlim_cur = 10;
642 rl[RLIMIT_NOFILE]->rlim_max = 20;
643
644 /* Invalid values don't change rl */
645 ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "10:20:30", rl, NULL));
646 ASSERT_NOT_NULL(rl[RLIMIT_NOFILE]);
647 ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_cur, 10U);
648 ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_max, 20U);
649
650 ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "wat:wat", rl, NULL));
651 ASSERT_NOT_NULL(rl[RLIMIT_NOFILE]);
652 ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_cur, 10U);
653 ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_max, 20U);
654
655 ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "66:wat", rl, NULL));
656 ASSERT_NOT_NULL(rl[RLIMIT_NOFILE]);
657 ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_cur, 10U);
658 ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_max, 20U);
659
660 ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "200:100", rl, NULL));
661 ASSERT_NOT_NULL(rl[RLIMIT_NOFILE]);
662 ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_cur, 10U);
663 ASSERT_EQ(rl[RLIMIT_NOFILE]->rlim_max, 20U);
664
665 rl[RLIMIT_NOFILE] = mfree(rl[RLIMIT_NOFILE]);
666
667 ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "56", rl, NULL));
668 ASSERT_NOT_NULL(rl[RLIMIT_CPU]);
669 ASSERT_EQ(rl[RLIMIT_CPU]->rlim_cur, 56U);
670 ASSERT_EQ(rl[RLIMIT_CPU]->rlim_cur, rl[RLIMIT_CPU]->rlim_max);
671
672 ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "57s", rl, NULL));
673 ASSERT_NOT_NULL(rl[RLIMIT_CPU]);
674 ASSERT_EQ(rl[RLIMIT_CPU]->rlim_cur, 57U);
675 ASSERT_EQ(rl[RLIMIT_CPU]->rlim_cur, rl[RLIMIT_CPU]->rlim_max);
676
677 ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "40s:1m", rl, NULL));
678 ASSERT_NOT_NULL(rl[RLIMIT_CPU]);
679 ASSERT_EQ(rl[RLIMIT_CPU]->rlim_cur, 40U);
680 ASSERT_EQ(rl[RLIMIT_CPU]->rlim_max, 60U);
681
682 ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "infinity", rl, NULL));
683 ASSERT_NOT_NULL(rl[RLIMIT_CPU]);
684 ASSERT_EQ(rl[RLIMIT_CPU]->rlim_cur, RLIM_INFINITY);
685 ASSERT_EQ(rl[RLIMIT_CPU]->rlim_cur, rl[RLIMIT_CPU]->rlim_max);
686
687 ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitCPU", RLIMIT_CPU, "1234ms", rl, NULL));
688 ASSERT_NOT_NULL(rl[RLIMIT_CPU]);
689 ASSERT_EQ(rl[RLIMIT_CPU]->rlim_cur, 2U);
690 ASSERT_EQ(rl[RLIMIT_CPU]->rlim_cur, rl[RLIMIT_CPU]->rlim_max);
691
692 rl[RLIMIT_CPU] = mfree(rl[RLIMIT_CPU]);
693
694 ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "58", rl, NULL));
695 ASSERT_NOT_NULL(rl[RLIMIT_RTTIME]);
696 ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_cur, 58U);
697 ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_cur, rl[RLIMIT_RTTIME]->rlim_max);
698
699 ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "58:60", rl, NULL));
700 ASSERT_NOT_NULL(rl[RLIMIT_RTTIME]);
701 ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_cur, 58U);
702 ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_max, 60U);
703
704 ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "59s", rl, NULL));
705 ASSERT_NOT_NULL(rl[RLIMIT_RTTIME]);
706 ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_cur, 59 * USEC_PER_SEC);
707 ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_cur, rl[RLIMIT_RTTIME]->rlim_max);
708
709 ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "59s:123s", rl, NULL));
710 ASSERT_NOT_NULL(rl[RLIMIT_RTTIME]);
711 ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_cur, 59 * USEC_PER_SEC);
712 ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_max, 123 * USEC_PER_SEC);
713
714 ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "infinity", rl, NULL));
715 ASSERT_NOT_NULL(rl[RLIMIT_RTTIME]);
716 ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_cur, RLIM_INFINITY);
717 ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_cur, rl[RLIMIT_RTTIME]->rlim_max);
718
719 ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "infinity:infinity", rl, NULL));
720 ASSERT_NOT_NULL(rl[RLIMIT_RTTIME]);
721 ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_cur, RLIM_INFINITY);
722 ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_cur, rl[RLIMIT_RTTIME]->rlim_max);
723
724 ASSERT_OK(config_parse_rlimit(NULL, "fake", 1, "section", 1, "LimitRTTIME", RLIMIT_RTTIME, "2345ms", rl, NULL));
725 ASSERT_NOT_NULL(rl[RLIMIT_RTTIME]);
726 ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_cur, 2345 * USEC_PER_MSEC);
727 ASSERT_EQ(rl[RLIMIT_RTTIME]->rlim_cur, rl[RLIMIT_RTTIME]->rlim_max);
728
729 rl[RLIMIT_RTTIME] = mfree(rl[RLIMIT_RTTIME]);
730 }
731
732 TEST(config_parse_pass_environ) {
733 /* int config_parse_pass_environ(
734 const char *unit,
735 const char *filename,
736 unsigned line,
737 const char *section,
738 unsigned section_line,
739 const char *lvalue,
740 int ltype,
741 const char *rvalue,
742 void *data,
743 void *userdata) */
744
745 _cleanup_strv_free_ char **passenv = NULL;
746
747 ASSERT_OK(config_parse_pass_environ(NULL, "fake", 1, "section", 1,
748 "PassEnvironment", 0, "A B",
749 &passenv, NULL));
750 ASSERT_EQ(strv_length(passenv), 2U);
751 ASSERT_STREQ(passenv[0], "A");
752 ASSERT_STREQ(passenv[1], "B");
753
754 ASSERT_OK(config_parse_pass_environ(NULL, "fake", 1, "section", 1,
755 "PassEnvironment", 0, "",
756 &passenv, NULL));
757 ASSERT_TRUE(strv_isempty(passenv));
758
759 ASSERT_OK(config_parse_pass_environ(NULL, "fake", 1, "section", 1,
760 "PassEnvironment", 0, "'invalid name' 'normal_name' A=1 'special_name$$' \\",
761 &passenv, NULL));
762 ASSERT_EQ(strv_length(passenv), 1U);
763 ASSERT_STREQ(passenv[0], "normal_name");
764 }
765
766 TEST(config_parse_unit_env_file) {
767 /* int config_parse_unit_env_file(
768 const char *unit,
769 const char *filename,
770 unsigned line,
771 const char *section,
772 unsigned section_line,
773 const char *lvalue,
774 int ltype,
775 const char *rvalue,
776 void *data,
777 void *userdata) */
778
779 _cleanup_(manager_freep) Manager *m = NULL;
780 Unit *u;
781 _cleanup_strv_free_ char **files = NULL;
782 int r;
783
784 r = manager_new(RUNTIME_SCOPE_USER, MANAGER_TEST_RUN_MINIMAL, &m);
785 if (manager_errno_skip_test(r)) {
786 log_notice_errno(r, "Skipping test: manager_new: %m");
787 return;
788 }
789
790 ASSERT_OK(r);
791 ASSERT_OK(manager_startup(m, NULL, NULL, NULL, NULL));
792
793 ASSERT_NOT_NULL(u = unit_new(m, sizeof(Service)));
794 ASSERT_OK_ZERO(unit_add_name(u, "foobar.service"));
795
796 ASSERT_OK_ZERO(config_parse_unit_env_file(u->id, "fake", 1, "section", 1,
797 "EnvironmentFile", 0, "not-absolute",
798 &files, u));
799 ASSERT_TRUE(strv_isempty(files));
800
801 ASSERT_OK_ZERO(config_parse_unit_env_file(u->id, "fake", 1, "section", 1,
802 "EnvironmentFile", 0, "/absolute1",
803 &files, u));
804 ASSERT_EQ(strv_length(files), 1U);
805
806 ASSERT_OK_ZERO(config_parse_unit_env_file(u->id, "fake", 1, "section", 1,
807 "EnvironmentFile", 0, "/absolute2",
808 &files, u));
809 ASSERT_EQ(strv_length(files), 2U);
810 ASSERT_STREQ(files[0], "/absolute1");
811 ASSERT_STREQ(files[1], "/absolute2");
812
813 ASSERT_OK_ZERO(config_parse_unit_env_file(u->id, "fake", 1, "section", 1,
814 "EnvironmentFile", 0, "",
815 &files, u));
816 ASSERT_TRUE(strv_isempty(files));
817
818 ASSERT_OK_ZERO(config_parse_unit_env_file(u->id, "fake", 1, "section", 1,
819 "EnvironmentFile", 0, "/path/%n.conf",
820 &files, u));
821 ASSERT_EQ(strv_length(files), 1U);
822 ASSERT_STREQ(files[0], "/path/foobar.service.conf");
823 }
824
825 TEST(unit_dump_config_items) {
826 unit_dump_config_items(stdout);
827 }
828
829 TEST(config_parse_memory_limit) {
830 /* int config_parse_memory_limit(
831 const char *unit,
832 const char *filename,
833 unsigned line,
834 const char *section,
835 unsigned section_line,
836 const char *lvalue,
837 int ltype,
838 const char *rvalue,
839 void *data,
840 void *userdata) */
841 CGroupContext c;
842 struct limit_test {
843 const char *limit;
844 const char *value;
845 uint64_t *result;
846 uint64_t expected;
847 } limit_tests[]= {
848 { "MemoryMin", "", &c.memory_min, CGROUP_LIMIT_MIN },
849 { "MemoryMin", "0", &c.memory_min, CGROUP_LIMIT_MIN },
850 { "MemoryMin", "10", &c.memory_min, 10 },
851 { "MemoryMin", "infinity", &c.memory_min, CGROUP_LIMIT_MAX },
852 { "MemoryLow", "", &c.memory_low, CGROUP_LIMIT_MIN },
853 { "MemoryLow", "0", &c.memory_low, CGROUP_LIMIT_MIN },
854 { "MemoryLow", "10", &c.memory_low, 10 },
855 { "MemoryLow", "infinity", &c.memory_low, CGROUP_LIMIT_MAX },
856 { "MemoryHigh", "", &c.memory_high, CGROUP_LIMIT_MAX },
857 { "MemoryHigh", "0", &c.memory_high, CGROUP_LIMIT_DUMMY },
858 { "MemoryHigh", "10", &c.memory_high, 10 },
859 { "MemoryHigh", "infinity", &c.memory_high, CGROUP_LIMIT_MAX },
860 { "MemoryMax", "", &c.memory_max, CGROUP_LIMIT_MAX },
861 { "MemoryMax", "0", &c.memory_max, CGROUP_LIMIT_DUMMY },
862 { "MemoryMax", "10", &c.memory_max, 10 },
863 { "MemoryMax", "infinity", &c.memory_max, CGROUP_LIMIT_MAX },
864 };
865
866 FOREACH_ELEMENT(test, limit_tests) {
867 c.memory_min = CGROUP_LIMIT_DUMMY;
868 c.memory_low = CGROUP_LIMIT_DUMMY;
869 c.memory_high = CGROUP_LIMIT_DUMMY;
870 c.memory_max = CGROUP_LIMIT_DUMMY;
871 log_info("%s=%s\t%"PRIu64"==%"PRIu64,
872 test->limit, test->value,
873 *test->result, test->expected);
874 ASSERT_OK(config_parse_memory_limit(NULL, "fake", 1, "section", 1,
875 test->limit, 1,
876 test->value, &c, NULL));
877 ASSERT_EQ(*test->result, test->expected);
878 }
879 }
880
881 TEST(contains_instance_specifier_superset) {
882 ASSERT_TRUE(contains_instance_specifier_superset("foobar@a%i"));
883 ASSERT_TRUE(contains_instance_specifier_superset("foobar@%ia"));
884 ASSERT_TRUE(contains_instance_specifier_superset("foobar@%n"));
885 ASSERT_TRUE(contains_instance_specifier_superset("foobar@%n.service"));
886 ASSERT_TRUE(contains_instance_specifier_superset("foobar@%N"));
887 ASSERT_TRUE(contains_instance_specifier_superset("foobar@%N.service"));
888 ASSERT_TRUE(contains_instance_specifier_superset("foobar@baz.%N.service"));
889 ASSERT_TRUE(contains_instance_specifier_superset("@%N.service"));
890 ASSERT_TRUE(contains_instance_specifier_superset("@%N"));
891 ASSERT_TRUE(contains_instance_specifier_superset("@%a%N"));
892
893 ASSERT_FALSE(contains_instance_specifier_superset("foobar@%i.service"));
894 ASSERT_FALSE(contains_instance_specifier_superset("foobar%ia.service"));
895 ASSERT_FALSE(contains_instance_specifier_superset("foobar@%%n.service"));
896 ASSERT_FALSE(contains_instance_specifier_superset("[email protected]"));
897 ASSERT_FALSE(contains_instance_specifier_superset("%N.service"));
898 ASSERT_FALSE(contains_instance_specifier_superset("%N"));
899 ASSERT_FALSE(contains_instance_specifier_superset("@%aN"));
900 ASSERT_FALSE(contains_instance_specifier_superset("@%a%b"));
901 }
902
903 TEST(unit_is_recursive_template_dependency) {
904 _cleanup_(manager_freep) Manager *m = NULL;
905 Unit *u;
906 int r;
907
908 r = manager_new(RUNTIME_SCOPE_USER, MANAGER_TEST_RUN_MINIMAL, &m);
909 if (manager_errno_skip_test(r)) {
910 log_notice_errno(r, "Skipping test: manager_new: %m");
911 return;
912 }
913
914 ASSERT_OK(r);
915 ASSERT_OK(manager_startup(m, NULL, NULL, NULL, NULL));
916
917 ASSERT_NOT_NULL(u = unit_new(m, sizeof(Service)));
918 ASSERT_OK_ZERO(unit_add_name(u, "[email protected]"));
919 u->fragment_path = strdup("/[email protected]");
920
921 ASSERT_OK(hashmap_put_strdup(&m->unit_id_map, "foobar@[email protected]", "/[email protected]"));
922 ASSERT_OK(hashmap_put_strdup(&m->unit_id_map, "foobar@[email protected]", "/custom.service"));
923
924 /* Test that %n, %N and any extension of %i specifiers in the instance are detected as recursive. */
925 ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@[email protected]", "foobar@%N.service"), 1);
926 ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@[email protected]", "foobar@%n.service"), 1);
927 ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@[email protected]", "foobar@a%i.service"), 1);
928 ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@[email protected]", "foobar@%ia.service"), 1);
929 ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@[email protected]", "foobar@%x%n.service"), 1);
930 /* Test that %i on its own is not detected as recursive. */
931 ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@[email protected]", "foobar@%i.service"), 0);
932 /* Test that a specifier other than %i, %n and %N is not detected as recursive. */
933 ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@[email protected]", "foobar@%xn.service"), 0);
934 /* Test that an expanded specifier is not detected as recursive. */
935 ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@[email protected]", "foobar@[email protected]"), 0);
936 /* Test that a dependency with a custom fragment path is not detected as recursive. */
937 ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@[email protected]", "foobar@%n.service"), 0);
938 /* Test that a dependency without a fragment path is not detected as recursive. */
939 ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@[email protected]", "foobar@%n.service"), 0);
940 /* Test that a dependency with a different prefix is not detected as recursive. */
941 ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "quux@[email protected]", "quux@%n.service"), 0);
942 /* Test that a dependency of a different type is not detected as recursive. */
943 ASSERT_EQ(unit_is_likely_recursive_template_dependency(u, "foobar@[email protected]", "foobar@%n.mount"), 0);
944 }
945
946 #define TEST_PATTERN(_regex, _allowed_patterns_count, _denied_patterns_count) \
947 { \
948 .regex = _regex, \
949 .allowed_patterns_count = _allowed_patterns_count, \
950 .denied_patterns_count = _denied_patterns_count \
951 }
952
953 TEST(config_parse_log_filter_patterns) {
954 ExecContext c = {};
955
956 static const struct {
957 const char *regex;
958 size_t allowed_patterns_count;
959 size_t denied_patterns_count;
960 } regex_tests[] = {
961 TEST_PATTERN("", 0, 0),
962 TEST_PATTERN(".*", 1, 0),
963 TEST_PATTERN("~.*", 1, 1),
964 TEST_PATTERN("", 0, 0),
965 TEST_PATTERN("~.*", 0, 1),
966 TEST_PATTERN("[.*", 0, 1), /* Invalid pattern. */
967 TEST_PATTERN(".*gg.*", 1, 1),
968 TEST_PATTERN("~.*", 1, 1), /* Already in the patterns list. */
969 TEST_PATTERN("[.*", 1, 1), /* Invalid pattern. */
970 TEST_PATTERN("\\x7ehello", 2, 1),
971 TEST_PATTERN("", 0, 0),
972 TEST_PATTERN("~foobar", 0, 1),
973 };
974
975 if (ERRNO_IS_NOT_SUPPORTED(dlopen_pcre2(LOG_DEBUG)))
976 return (void) log_tests_skipped("PCRE2 support is not available");
977
978 FOREACH_ELEMENT(test, regex_tests) {
979 ASSERT_OK(config_parse_log_filter_patterns(NULL, "fake", 1, "section", 1, "LogFilterPatterns", 1,
980 test->regex, &c, NULL));
981
982 ASSERT_EQ(set_size(c.log_filter_allowed_patterns), test->allowed_patterns_count);
983 ASSERT_EQ(set_size(c.log_filter_denied_patterns), test->denied_patterns_count);
984
985 /* Ensure `~` is properly removed */
986 const char *p;
987 SET_FOREACH(p, c.log_filter_allowed_patterns)
988 ASSERT_TRUE(p && p[0] != '~');
989 SET_FOREACH(p, c.log_filter_denied_patterns)
990 ASSERT_TRUE(p && p[0] != '~');
991 }
992
993 exec_context_done(&c);
994 }
995
996 TEST(config_parse_open_file) {
997 _cleanup_(manager_freep) Manager *m = NULL;
998 _cleanup_(unit_freep) Unit *u = NULL;
999 _cleanup_(open_file_freep) OpenFile *of = NULL;
1000 int r;
1001
1002 r = manager_new(RUNTIME_SCOPE_USER, MANAGER_TEST_RUN_MINIMAL, &m);
1003 if (manager_errno_skip_test(r)) {
1004 log_notice_errno(r, "Skipping test: manager_new: %m");
1005 return;
1006 }
1007
1008 ASSERT_OK(r);
1009 ASSERT_OK(manager_startup(m, NULL, NULL, NULL, NULL));
1010
1011 ASSERT_NOT_NULL(u = unit_new(m, sizeof(Service)));
1012 ASSERT_OK_ZERO(unit_add_name(u, "foobar.service"));
1013
1014 ASSERT_OK(config_parse_open_file(NULL, "fake", 1, "section", 1,
1015 "OpenFile", 0, "/proc/1/ns/mnt:host-mount-namespace:read-only",
1016 &of, u));
1017 ASSERT_NOT_NULL(of);
1018 ASSERT_STREQ(of->path, "/proc/1/ns/mnt");
1019 ASSERT_STREQ(of->fdname, "host-mount-namespace");
1020 ASSERT_EQ(of->flags, OPENFILE_READ_ONLY);
1021
1022 of = open_file_free(of);
1023 ASSERT_OK(config_parse_open_file(NULL, "fake", 1, "section", 1,
1024 "OpenFile", 0, "/proc/1/ns/mnt::read-only",
1025 &of, u));
1026 ASSERT_NOT_NULL(of);
1027 ASSERT_STREQ(of->path, "/proc/1/ns/mnt");
1028 ASSERT_STREQ(of->fdname, "mnt");
1029 ASSERT_EQ(of->flags, OPENFILE_READ_ONLY);
1030
1031 ASSERT_OK(config_parse_open_file(NULL, "fake", 1, "section", 1,
1032 "OpenFile", 0, "",
1033 &of, u));
1034 ASSERT_NULL(of);
1035 }
1036
1037 TEST(config_parse_service_refresh_on_reload) {
1038 ServiceRefreshOnReload flags;
1039 _cleanup_strv_free_ char **l = NULL;
1040 int r;
1041
1042 r = service_refresh_on_reload_from_string_many("extensions", &flags);
1043 ASSERT_OK(r);
1044 ASSERT_EQ(flags, SERVICE_RELOAD_EXTENSIONS);
1045 ASSERT_OK(service_refresh_on_reload_to_strv(flags, &l));
1046 ASSERT_TRUE(strv_equal(l, STRV_MAKE("extensions")));
1047
1048 l = strv_free(l);
1049
1050 r = service_refresh_on_reload_from_string_many("credentials extensions", &flags);
1051 ASSERT_OK(r);
1052 ASSERT_EQ(flags, SERVICE_RELOAD_EXTENSIONS|SERVICE_RELOAD_CREDENTIALS);
1053 ASSERT_OK(service_refresh_on_reload_to_strv(flags, &l));
1054 ASSERT_TRUE(strv_equal(l, STRV_MAKE("extensions", "credentials")));
1055
1056 ASSERT_ERROR(service_refresh_on_reload_from_string_many("hoge", &flags), EINVAL);
1057
1058 _cleanup_(manager_freep) Manager *m = NULL;
1059 _cleanup_(unit_freep) Unit *u = NULL;
1060
1061 r = manager_new(RUNTIME_SCOPE_USER, MANAGER_TEST_RUN_MINIMAL, &m);
1062 if (manager_errno_skip_test(r)) {
1063 log_notice_errno(r, "Skipping test: manager_new: %m");
1064 return;
1065 }
1066
1067 ASSERT_OK(r);
1068 ASSERT_OK(manager_startup(m, NULL, NULL, NULL, NULL));
1069
1070 ASSERT_NOT_NULL(u = unit_new(m, sizeof(Service)));
1071 ASSERT_OK_ZERO(unit_add_name(u, "foobar.service"));
1072
1073 ASSERT_FALSE(SERVICE(u)->refresh_on_reload_set);
1074
1075 ASSERT_OK(config_parse_service_refresh_on_reload(
1076 NULL, "fake", 1, "section", 1,
1077 "RefreshOnReload", 0, "no",
1078 NULL, u));
1079 ASSERT_TRUE(SERVICE(u)->refresh_on_reload_set);
1080 ASSERT_EQ(SERVICE(u)->refresh_on_reload_flags, 0);
1081
1082 ASSERT_OK(config_parse_service_refresh_on_reload(
1083 NULL, "fake", 1, "section", 1,
1084 "RefreshOnReload", 0, "yes",
1085 NULL, u));
1086 ASSERT_TRUE(SERVICE(u)->refresh_on_reload_set);
1087 ASSERT_EQ(SERVICE(u)->refresh_on_reload_flags, _SERVICE_REFRESH_ON_RELOAD_ALL);
1088
1089 ASSERT_OK(config_parse_service_refresh_on_reload(
1090 NULL, "fake", 1, "section", 1,
1091 "RefreshOnReload", 0, "~extensions",
1092 NULL, u));
1093 ASSERT_TRUE(SERVICE(u)->refresh_on_reload_set);
1094 ASSERT_EQ(SERVICE(u)->refresh_on_reload_flags, _SERVICE_REFRESH_ON_RELOAD_ALL & ~SERVICE_RELOAD_EXTENSIONS);
1095
1096 ASSERT_OK(config_parse_service_refresh_on_reload(
1097 NULL, "fake", 1, "section", 1,
1098 "RefreshOnReload", 0, "~extensions credentials",
1099 NULL, u));
1100 ASSERT_TRUE(SERVICE(u)->refresh_on_reload_set);
1101 ASSERT_EQ(SERVICE(u)->refresh_on_reload_flags, 0);
1102
1103 ASSERT_OK(config_parse_service_refresh_on_reload(
1104 NULL, "fake", 1, "section", 1,
1105 "RefreshOnReload", 0, "",
1106 NULL, u));
1107 ASSERT_FALSE(SERVICE(u)->refresh_on_reload_set);
1108
1109 ASSERT_OK(config_parse_service_refresh_on_reload(
1110 NULL, "fake", 1, "section", 1,
1111 "RefreshOnReload", 0, "~extensions",
1112 NULL, u));
1113 ASSERT_TRUE(SERVICE(u)->refresh_on_reload_set);
1114 ASSERT_EQ(SERVICE(u)->refresh_on_reload_flags, SERVICE_REFRESH_ON_RELOAD_DEFAULT & ~SERVICE_RELOAD_EXTENSIONS);
1115 }
1116
1117 static int intro(void) {
1118 if (enter_cgroup_subroot(NULL) == -ENOMEDIUM)
1119 return log_tests_skipped("cgroupfs not available");
1120
1121 ASSERT_NOT_NULL(runtime_dir = setup_fake_runtime_dir());
1122 return EXIT_SUCCESS;
1123 }
1124
1125 DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);