1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 Lennart Poettering
7 Copyright 2013 Zbigniew Jędrzejewski-Szmek
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
31 #include "install-printf.h"
32 #include "specifier.h"
36 #include "load-fragment.h"
39 #include "test-helper.h"
41 static int test_unit_file_get_set(void) {
47 h = hashmap_new(&string_hash_ops);
50 r = unit_file_get_list(UNIT_FILE_SYSTEM, NULL, h);
52 if (r == -EPERM || r == -EACCES) {
53 printf("Skipping test: unit_file_get_list: %s", strerror(-r));
54 return EXIT_TEST_SKIP;
57 log_full(r == 0 ? LOG_INFO : LOG_ERR,
58 "unit_file_get_list: %s", strerror(-r));
62 HASHMAP_FOREACH(p, h, i)
63 printf("%s = %s\n", p->path, unit_file_state_to_string(p->state));
65 unit_file_list_free(h);
70 static void check_execcommand(ExecCommand *c,
76 log_info("%s %s %s %s",
77 c->path, c->argv[0], c->argv[1], c->argv[2]);
78 assert_se(streq(c->path, path));
79 assert_se(streq(c->argv[0], argv0));
80 assert_se(streq(c->argv[1], argv1));
81 assert_se(c->argv[2] == NULL);
82 assert_se(c->ignore == ignore);
85 static void test_config_parse_exec(void) {
86 /* int config_parse_exec( */
87 /* const char *filename, */
89 /* const char *section, */
90 /* unsigned section_line, */
91 /* const char *lvalue, */
93 /* const char *rvalue, */
98 ExecCommand *c = NULL, *c1;
101 r = config_parse_exec(NULL, "fake", 1, "section", 1,
102 "LValue", 0, "/RValue r1",
105 check_execcommand(c, "/RValue", "/RValue", "r1", false);
107 r = config_parse_exec(NULL, "fake", 2, "section", 1,
108 "LValue", 0, "/RValue///slashes/// r1",
112 c1 = c->command_next;
113 check_execcommand(c1, "/RValue/slashes", "/RValue///slashes///",
117 r = config_parse_exec(NULL, "fake", 3, "section", 1,
118 "LValue", 0, "@/RValue///slashes2/// argv0 r1",
121 c1 = c1->command_next;
122 check_execcommand(c1, "/RValue/slashes2", "argv0", "r1", false);
124 /* ignore && honour_argv0 */
125 r = config_parse_exec(NULL, "fake", 4, "section", 1,
126 "LValue", 0, "-@/RValue///slashes3/// argv0a r1",
129 c1 = c1->command_next;
130 check_execcommand(c1,
131 "/RValue/slashes3", "argv0a", "r1", true);
133 /* ignore && honour_argv0 */
134 r = config_parse_exec(NULL, "fake", 4, "section", 1,
135 "LValue", 0, "@-/RValue///slashes4/// argv0b r1",
138 c1 = c1->command_next;
139 check_execcommand(c1,
140 "/RValue/slashes4", "argv0b", "r1", true);
142 /* ignore && ignore */
143 r = config_parse_exec(NULL, "fake", 4, "section", 1,
144 "LValue", 0, "--/RValue argv0 r1",
147 assert_se(c1->command_next == NULL);
149 /* ignore && ignore */
150 r = config_parse_exec(NULL, "fake", 4, "section", 1,
151 "LValue", 0, "-@-/RValue argv0 r1",
154 assert_se(c1->command_next == NULL);
157 r = config_parse_exec(NULL, "fake", 5, "section", 1,
159 "-@/RValue argv0 r1 ; "
163 c1 = c1->command_next;
164 check_execcommand(c1,
165 "/RValue", "argv0", "r1", true);
167 c1 = c1->command_next;
168 check_execcommand(c1,
169 "/goo/goo", "/goo/goo", "boo", false);
171 /* trailing semicolon */
172 r = config_parse_exec(NULL, "fake", 5, "section", 1,
174 "-@/RValue argv0 r1 ; ",
177 c1 = c1->command_next;
178 check_execcommand(c1,
179 "/RValue", "argv0", "r1", true);
181 assert_se(c1->command_next == NULL);
183 /* escaped semicolon */
184 r = config_parse_exec(NULL, "fake", 5, "section", 1,
189 c1 = c1->command_next;
190 check_execcommand(c1,
191 "/usr/bin/find", "/usr/bin/find", ";", false);
193 exec_command_free_list(c);
211 "#SPAMD_ARGS=\"-d --socketpath=/var/lib/bulwark/spamd \\\n" \
212 "#--nouser-config \\\n" \
218 "HWMON_MODULES=\"coretemp f71882fg\"\n" \
220 "# For compatibility reasons\n" \
222 "MODULE_0=coretemp\n" \
229 static void test_load_env_file_1(void) {
230 _cleanup_strv_free_ char **data = NULL;
233 char name[] = "/tmp/test-load-env-file.XXXXXX";
234 _cleanup_close_ int fd;
236 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
238 assert_se(write(fd, env_file_1, sizeof(env_file_1)) == sizeof(env_file_1));
240 r = load_env_file(NULL, name, NULL, &data);
242 assert_se(streq(data[0], "a=a"));
243 assert_se(streq(data[1], "b=bc"));
244 assert_se(streq(data[2], "d=def"));
245 assert_se(streq(data[3], "g=g "));
246 assert_se(streq(data[4], "h=h"));
247 assert_se(streq(data[5], "i=i"));
248 assert_se(data[6] == NULL);
252 static void test_load_env_file_2(void) {
253 _cleanup_strv_free_ char **data = NULL;
256 char name[] = "/tmp/test-load-env-file.XXXXXX";
257 _cleanup_close_ int fd;
259 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
261 assert_se(write(fd, env_file_2, sizeof(env_file_2)) == sizeof(env_file_2));
263 r = load_env_file(NULL, name, NULL, &data);
265 assert_se(streq(data[0], "a=a"));
266 assert_se(data[1] == NULL);
270 static void test_load_env_file_3(void) {
271 _cleanup_strv_free_ char **data = NULL;
274 char name[] = "/tmp/test-load-env-file.XXXXXX";
275 _cleanup_close_ int fd;
277 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
279 assert_se(write(fd, env_file_3, sizeof(env_file_3)) == sizeof(env_file_3));
281 r = load_env_file(NULL, name, NULL, &data);
283 assert_se(data == NULL);
287 static void test_load_env_file_4(void) {
288 _cleanup_strv_free_ char **data = NULL;
289 char name[] = "/tmp/test-load-env-file.XXXXXX";
290 _cleanup_close_ int fd;
293 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
295 assert_se(write(fd, env_file_4, sizeof(env_file_4)) == sizeof(env_file_4));
297 r = load_env_file(NULL, name, NULL, &data);
299 assert_se(streq(data[0], "HWMON_MODULES=coretemp f71882fg"));
300 assert_se(streq(data[1], "MODULE_0=coretemp"));
301 assert_se(streq(data[2], "MODULE_1=f71882fg"));
302 assert_se(data[3] == NULL);
306 static void test_load_env_file_5(void) {
307 _cleanup_strv_free_ char **data = NULL;
310 char name[] = "/tmp/test-load-env-file.XXXXXX";
311 _cleanup_close_ int fd;
313 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
315 assert_se(write(fd, env_file_5, sizeof(env_file_5)) == sizeof(env_file_5));
317 r = load_env_file(NULL, name, NULL, &data);
319 assert_se(streq(data[0], "a="));
320 assert_se(streq(data[1], "b="));
321 assert_se(data[2] == NULL);
325 static void test_install_printf(void) {
326 char name[] = "name.service",
327 path[] = "/run/systemd/system/name.service",
328 user[] = "xxxx-no-such-user";
329 InstallInfo i = {name, path, user};
330 InstallInfo i2 = {name, path, NULL};
331 char name3[] = "name@inst.service",
332 path3[] = "/run/systemd/system/name.service";
333 InstallInfo i3 = {name3, path3, user};
334 InstallInfo i4 = {name3, path3, NULL};
336 _cleanup_free_ char *mid, *bid, *host;
338 assert_se(specifier_machine_id('m', NULL, NULL, &mid) >= 0 && mid);
339 assert_se(specifier_boot_id('b', NULL, NULL, &bid) >= 0 && bid);
340 assert_se((host = gethostname_malloc()));
342 #define expect(src, pattern, result) \
344 _cleanup_free_ char *t = NULL; \
345 _cleanup_free_ char \
346 *d1 = strdup(i.name), \
347 *d2 = strdup(i.path), \
348 *d3 = strdup(i.user); \
349 assert_se(install_full_printf(&src, pattern, &t) >= 0 || !result); \
350 memzero(i.name, strlen(i.name)); \
351 memzero(i.path, strlen(i.path)); \
352 memzero(i.user, strlen(i.user)); \
353 assert_se(d1 && d2 && d3); \
356 assert_se(streq(t, result)); \
357 } else assert_se(t == NULL); \
358 strcpy(i.name, d1); \
359 strcpy(i.path, d2); \
360 strcpy(i.user, d3); \
363 assert_se(setenv("USER", "root", 1) == 0);
365 expect(i, "%n", "name.service");
366 expect(i, "%N", "name");
367 expect(i, "%p", "name");
369 expect(i, "%u", "xxxx-no-such-user");
371 DISABLE_WARNING_NONNULL;
372 expect(i, "%U", NULL);
375 expect(i, "%m", mid);
376 expect(i, "%b", bid);
377 expect(i, "%H", host);
379 expect(i2, "%u", "root");
380 expect(i2, "%U", "0");
382 expect(i3, "%n", "name@inst.service");
383 expect(i3, "%N", "name@inst");
384 expect(i3, "%p", "name");
385 expect(i3, "%u", "xxxx-no-such-user");
387 DISABLE_WARNING_NONNULL;
388 expect(i3, "%U", NULL);
391 expect(i3, "%m", mid);
392 expect(i3, "%b", bid);
393 expect(i3, "%H", host);
395 expect(i4, "%u", "root");
396 expect(i4, "%U", "0");
399 int main(int argc, char *argv[]) {
402 log_parse_environment();
405 r = test_unit_file_get_set();
406 test_config_parse_exec();
407 test_load_env_file_1();
408 test_load_env_file_2();
409 test_load_env_file_3();
410 test_load_env_file_4();
411 test_load_env_file_5();
412 TEST_REQ_RUNNING_SYSTEMD(test_install_printf());