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_func, string_compare_func);
50 r = unit_file_get_list(UNIT_FILE_SYSTEM, NULL, h);
51 log_full(r == 0 ? LOG_INFO : LOG_ERR,
52 "unit_file_get_list: %s", strerror(-r));
56 HASHMAP_FOREACH(p, h, i)
57 printf("%s = %s\n", p->path, unit_file_state_to_string(p->state));
59 unit_file_list_free(h);
64 static void check_execcommand(ExecCommand *c,
70 log_info("%s %s %s %s",
71 c->path, c->argv[0], c->argv[1], c->argv[2]);
72 assert_se(streq(c->path, path));
73 assert_se(streq(c->argv[0], argv0));
74 assert_se(streq(c->argv[1], argv1));
75 assert_se(c->argv[2] == NULL);
76 assert_se(c->ignore == ignore);
79 static void test_config_parse_exec(void) {
80 /* int config_parse_exec( */
81 /* const char *filename, */
83 /* const char *section, */
84 /* unsigned section_line, */
85 /* const char *lvalue, */
87 /* const char *rvalue, */
92 ExecCommand *c = NULL, *c1;
95 r = config_parse_exec(NULL, "fake", 1, "section", 1,
96 "LValue", 0, "/RValue r1",
99 check_execcommand(c, "/RValue", "/RValue", "r1", false);
101 r = config_parse_exec(NULL, "fake", 2, "section", 1,
102 "LValue", 0, "/RValue///slashes/// r1",
106 c1 = c->command_next;
107 check_execcommand(c1, "/RValue/slashes", "/RValue///slashes///",
111 r = config_parse_exec(NULL, "fake", 3, "section", 1,
112 "LValue", 0, "@/RValue///slashes2/// argv0 r1",
115 c1 = c1->command_next;
116 check_execcommand(c1, "/RValue/slashes2", "argv0", "r1", false);
118 /* ignore && honour_argv0 */
119 r = config_parse_exec(NULL, "fake", 4, "section", 1,
120 "LValue", 0, "-@/RValue///slashes3/// argv0a r1",
123 c1 = c1->command_next;
124 check_execcommand(c1,
125 "/RValue/slashes3", "argv0a", "r1", true);
127 /* ignore && honour_argv0 */
128 r = config_parse_exec(NULL, "fake", 4, "section", 1,
129 "LValue", 0, "@-/RValue///slashes4/// argv0b r1",
132 c1 = c1->command_next;
133 check_execcommand(c1,
134 "/RValue/slashes4", "argv0b", "r1", true);
136 /* ignore && ignore */
137 r = config_parse_exec(NULL, "fake", 4, "section", 1,
138 "LValue", 0, "--/RValue argv0 r1",
141 assert_se(c1->command_next == NULL);
143 /* ignore && ignore */
144 r = config_parse_exec(NULL, "fake", 4, "section", 1,
145 "LValue", 0, "-@-/RValue argv0 r1",
148 assert_se(c1->command_next == NULL);
151 r = config_parse_exec(NULL, "fake", 5, "section", 1,
153 "-@/RValue argv0 r1 ; "
157 c1 = c1->command_next;
158 check_execcommand(c1,
159 "/RValue", "argv0", "r1", true);
161 c1 = c1->command_next;
162 check_execcommand(c1,
163 "/goo/goo", "/goo/goo", "boo", false);
165 /* trailing semicolon */
166 r = config_parse_exec(NULL, "fake", 5, "section", 1,
168 "-@/RValue argv0 r1 ; ",
171 c1 = c1->command_next;
172 check_execcommand(c1,
173 "/RValue", "argv0", "r1", true);
175 assert_se(c1->command_next == NULL);
177 /* escaped semicolon */
178 r = config_parse_exec(NULL, "fake", 5, "section", 1,
183 c1 = c1->command_next;
184 check_execcommand(c1,
185 "/usr/bin/find", "/usr/bin/find", ";", false);
187 exec_command_free_list(c);
205 "#SPAMD_ARGS=\"-d --socketpath=/var/lib/bulwark/spamd \\\n" \
206 "#--nouser-config \\\n" \
212 "HWMON_MODULES=\"coretemp f71882fg\"\n" \
214 "# For compatibility reasons\n" \
216 "MODULE_0=coretemp\n" \
220 static void test_load_env_file_1(void) {
221 _cleanup_strv_free_ char **data = NULL;
224 char name[] = "/tmp/test-load-env-file.XXXXXX";
225 _cleanup_close_ int fd;
227 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
229 assert_se(write(fd, env_file_1, sizeof(env_file_1)) == sizeof(env_file_1));
231 r = load_env_file(name, NULL, &data);
233 assert(streq(data[0], "a=a"));
234 assert(streq(data[1], "b=bc"));
235 assert(streq(data[2], "d=def"));
236 assert(streq(data[3], "g=g "));
237 assert(streq(data[4], "h=h"));
238 assert(streq(data[5], "i=i"));
239 assert(data[6] == NULL);
243 static void test_load_env_file_2(void) {
244 _cleanup_strv_free_ char **data = NULL;
247 char name[] = "/tmp/test-load-env-file.XXXXXX";
248 _cleanup_close_ int fd;
250 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
252 assert_se(write(fd, env_file_2, sizeof(env_file_2)) == sizeof(env_file_2));
254 r = load_env_file(name, NULL, &data);
256 assert(streq(data[0], "a=a"));
257 assert(data[1] == NULL);
261 static void test_load_env_file_3(void) {
262 _cleanup_strv_free_ char **data = NULL;
265 char name[] = "/tmp/test-load-env-file.XXXXXX";
266 _cleanup_close_ int fd;
268 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
270 assert_se(write(fd, env_file_3, sizeof(env_file_3)) == sizeof(env_file_3));
272 r = load_env_file(name, NULL, &data);
274 assert(data == NULL);
278 static void test_load_env_file_4(void) {
279 _cleanup_strv_free_ char **data = NULL;
280 char name[] = "/tmp/test-load-env-file.XXXXXX";
281 _cleanup_close_ int fd;
284 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
286 assert_se(write(fd, env_file_4, sizeof(env_file_4)) == sizeof(env_file_4));
288 r = load_env_file(name, NULL, &data);
290 assert(streq(data[0], "HWMON_MODULES=coretemp f71882fg"));
291 assert(streq(data[1], "MODULE_0=coretemp"));
292 assert(streq(data[2], "MODULE_1=f71882fg"));
293 assert(data[3] == NULL);
298 #pragma GCC diagnostic push
299 #pragma GCC diagnostic ignored "-Wnonnull"
301 static void test_install_printf(void) {
302 char name[] = "name.service",
303 path[] = "/run/systemd/system/name.service",
304 user[] = "xxxx-no-such-user";
305 InstallInfo i = {name, path, user};
306 InstallInfo i2 = {name, path, NULL};
307 char name3[] = "name@inst.service",
308 path3[] = "/run/systemd/system/name.service";
309 InstallInfo i3 = {name3, path3, user};
310 InstallInfo i4 = {name3, path3, NULL};
312 _cleanup_free_ char *mid, *bid, *host;
314 assert_se(specifier_machine_id('m', NULL, NULL, &mid) >= 0 && mid);
315 assert_se(specifier_boot_id('b', NULL, NULL, &bid) >= 0 && bid);
316 assert_se((host = gethostname_malloc()));
318 #define expect(src, pattern, result) \
320 _cleanup_free_ char *t = NULL; \
321 _cleanup_free_ char \
322 *d1 = strdup(i.name), \
323 *d2 = strdup(i.path), \
324 *d3 = strdup(i.user); \
325 assert_se(install_full_printf(&src, pattern, &t) >= 0 || !result); \
326 memzero(i.name, strlen(i.name)); \
327 memzero(i.path, strlen(i.path)); \
328 memzero(i.user, strlen(i.user)); \
329 assert(d1 && d2 && d3); \
332 assert(streq(t, result)); \
333 } else assert(t == NULL); \
334 strcpy(i.name, d1); \
335 strcpy(i.path, d2); \
336 strcpy(i.user, d3); \
339 assert_se(setenv("USER", "root", 1) == 0);
341 expect(i, "%n", "name.service");
342 expect(i, "%N", "name");
343 expect(i, "%p", "name");
345 expect(i, "%u", "xxxx-no-such-user");
346 expect(i, "%U", NULL);
347 expect(i, "%m", mid);
348 expect(i, "%b", bid);
349 expect(i, "%H", host);
351 expect(i2, "%u", "root");
352 expect(i2, "%U", "0");
354 expect(i3, "%n", "name@inst.service");
355 expect(i3, "%N", "name@inst");
356 expect(i3, "%p", "name");
357 expect(i3, "%u", "xxxx-no-such-user");
358 expect(i3, "%U", NULL);
359 expect(i3, "%m", mid);
360 expect(i3, "%b", bid);
361 expect(i3, "%H", host);
363 expect(i4, "%u", "root");
364 expect(i4, "%U", "0");
366 #pragma GCC diagnostic pop
368 int main(int argc, char *argv[]) {
371 log_parse_environment();
374 r = test_unit_file_get_set();
375 test_config_parse_exec();
376 test_load_env_file_1();
377 test_load_env_file_2();
378 test_load_env_file_3();
379 test_load_env_file_4();
380 TEST_REQ_RUNNING_SYSTEMD(test_install_printf());