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/>.
30 #include "install-printf.h"
31 #include "specifier.h"
35 #include "load-fragment.h"
39 static int test_unit_file_get_set(void) {
45 h = hashmap_new(string_hash_func, string_compare_func);
48 r = unit_file_get_list(UNIT_FILE_SYSTEM, NULL, h);
49 log_full(r == 0 ? LOG_INFO : LOG_ERR,
50 "unit_file_get_list: %s", strerror(-r));
54 HASHMAP_FOREACH(p, h, i)
55 printf("%s = %s\n", p->path, unit_file_state_to_string(p->state));
57 unit_file_list_free(h);
62 static void check_execcommand(ExecCommand *c,
68 log_info("%s %s %s %s",
69 c->path, c->argv[0], c->argv[1], c->argv[2]);
70 assert_se(streq(c->path, path));
71 assert_se(streq(c->argv[0], argv0));
72 assert_se(streq(c->argv[1], argv1));
73 assert_se(c->argv[2] == NULL);
74 assert_se(c->ignore == ignore);
77 static void test_config_parse_exec(void) {
78 /* int config_parse_exec( */
79 /* const char *filename, */
81 /* const char *section, */
82 /* const char *lvalue, */
84 /* const char *rvalue, */
89 ExecCommand *c = NULL, *c1;
92 r = config_parse_exec(NULL, "fake", 1, "section",
93 "LValue", 0, "/RValue r1",
96 check_execcommand(c, "/RValue", "/RValue", "r1", false);
98 r = config_parse_exec(NULL, "fake", 2, "section",
99 "LValue", 0, "/RValue///slashes/// r1",
103 c1 = c->command_next;
104 check_execcommand(c1, "/RValue/slashes", "/RValue///slashes///",
108 r = config_parse_exec(NULL, "fake", 3, "section",
109 "LValue", 0, "@/RValue///slashes2/// argv0 r1",
112 c1 = c1->command_next;
113 check_execcommand(c1, "/RValue/slashes2", "argv0", "r1", false);
115 /* ignore && honour_argv0 */
116 r = config_parse_exec(NULL, "fake", 4, "section",
117 "LValue", 0, "-@/RValue///slashes3/// argv0a r1",
120 c1 = c1->command_next;
121 check_execcommand(c1,
122 "/RValue/slashes3", "argv0a", "r1", true);
124 /* ignore && honour_argv0 */
125 r = config_parse_exec(NULL, "fake", 4, "section",
126 "LValue", 0, "@-/RValue///slashes4/// argv0b r1",
129 c1 = c1->command_next;
130 check_execcommand(c1,
131 "/RValue/slashes4", "argv0b", "r1", true);
133 /* ignore && ignore */
134 r = config_parse_exec(NULL, "fake", 4, "section",
135 "LValue", 0, "--/RValue argv0 r1",
138 assert_se(c1->command_next == NULL);
140 /* ignore && ignore */
141 r = config_parse_exec(NULL, "fake", 4, "section",
142 "LValue", 0, "-@-/RValue argv0 r1",
145 assert_se(c1->command_next == NULL);
148 r = config_parse_exec(NULL, "fake", 5, "section",
150 "-@/RValue argv0 r1 ; "
154 c1 = c1->command_next;
155 check_execcommand(c1,
156 "/RValue", "argv0", "r1", true);
158 c1 = c1->command_next;
159 check_execcommand(c1,
160 "/goo/goo", "/goo/goo", "boo", false);
162 /* trailing semicolon */
163 r = config_parse_exec(NULL, "fake", 5, "section",
165 "-@/RValue argv0 r1 ; ",
168 c1 = c1->command_next;
169 check_execcommand(c1,
170 "/RValue", "argv0", "r1", true);
172 assert_se(c1->command_next == NULL);
174 /* escaped semicolon */
175 r = config_parse_exec(NULL, "fake", 5, "section",
180 c1 = c1->command_next;
181 check_execcommand(c1,
182 "/usr/bin/find", "/usr/bin/find", ";", false);
184 exec_command_free_list(c);
202 "#SPAMD_ARGS=\"-d --socketpath=/var/lib/bulwark/spamd \\\n" \
203 "#--nouser-config \\\n" \
209 "HWMON_MODULES=\"coretemp f71882fg\"\n" \
211 "# For compatibility reasons\n" \
213 "MODULE_0=coretemp\n" \
217 static void test_load_env_file_1(void) {
218 _cleanup_strv_free_ char **data = NULL;
221 char name[] = "/tmp/test-load-env-file.XXXXXX";
222 _cleanup_close_ int fd = mkstemp(name);
224 assert_se(write(fd, env_file_1, sizeof(env_file_1)) == sizeof(env_file_1));
226 r = load_env_file(name, NULL, &data);
228 assert(streq(data[0], "a=a"));
229 assert(streq(data[1], "b=bc"));
230 assert(streq(data[2], "d=def"));
231 assert(streq(data[3], "g=g "));
232 assert(streq(data[4], "h=h"));
233 assert(streq(data[5], "i=i"));
234 assert(data[6] == NULL);
238 static void test_load_env_file_2(void) {
239 _cleanup_strv_free_ char **data = NULL;
242 char name[] = "/tmp/test-load-env-file.XXXXXX";
243 _cleanup_close_ int fd = mkstemp(name);
245 assert_se(write(fd, env_file_2, sizeof(env_file_2)) == sizeof(env_file_2));
247 r = load_env_file(name, NULL, &data);
249 assert(streq(data[0], "a=a"));
250 assert(data[1] == NULL);
254 static void test_load_env_file_3(void) {
255 _cleanup_strv_free_ char **data = NULL;
258 char name[] = "/tmp/test-load-env-file.XXXXXX";
259 _cleanup_close_ int fd = mkstemp(name);
261 assert_se(write(fd, env_file_3, sizeof(env_file_3)) == sizeof(env_file_3));
263 r = load_env_file(name, NULL, &data);
265 assert(data == NULL);
269 static void test_load_env_file_4(void) {
270 _cleanup_strv_free_ char **data = NULL;
273 char name[] = "/tmp/test-load-env-file.XXXXXX";
274 _cleanup_close_ int fd = mkstemp(name);
276 assert_se(write(fd, env_file_4, sizeof(env_file_4)) == sizeof(env_file_4));
278 r = load_env_file(name, NULL, &data);
280 assert(streq(data[0], "HWMON_MODULES=coretemp f71882fg"));
281 assert(streq(data[1], "MODULE_0=coretemp"));
282 assert(streq(data[2], "MODULE_1=f71882fg"));
283 assert(data[3] == NULL);
288 #pragma GCC diagnostic push
289 #pragma GCC diagnostic ignored "-Wnonnull"
291 static void test_install_printf(void) {
292 char name[] = "name.service",
293 path[] = "/run/systemd/system/name.service",
294 user[] = "xxxx-no-such-user";
295 InstallInfo i = {name, path, user};
296 InstallInfo i2 = {name, path, NULL};
297 char name3[] = "name@inst.service",
298 path3[] = "/run/systemd/system/name.service";
299 InstallInfo i3 = {name3, path3, user};
300 InstallInfo i4 = {name3, path3, NULL};
302 _cleanup_free_ char *mid, *bid, *host;
304 assert_se((mid = specifier_machine_id('m', NULL, NULL)));
305 assert_se((bid = specifier_boot_id('b', NULL, NULL)));
306 assert_se((host = gethostname_malloc()));
308 #define expect(src, pattern, result) \
310 _cleanup_free_ char *t = install_full_printf(&src, pattern); \
311 _cleanup_free_ char \
312 *d1 = strdup(i.name), \
313 *d2 = strdup(i.path), \
314 *d3 = strdup(i.user); \
315 memzero(i.name, strlen(i.name)); \
316 memzero(i.path, strlen(i.path)); \
317 memzero(i.user, strlen(i.user)); \
318 assert(d1 && d2 && d3); \
321 assert(streq(t, result)); \
322 } else assert(t == NULL); \
323 strcpy(i.name, d1); \
324 strcpy(i.path, d2); \
325 strcpy(i.user, d3); \
328 assert_se(setenv("USER", "root", 1) == 0);
330 expect(i, "%n", "name.service");
331 expect(i, "%N", "name");
332 expect(i, "%p", "name");
334 expect(i, "%u", "xxxx-no-such-user");
335 expect(i, "%U", NULL);
336 expect(i, "%m", mid);
337 expect(i, "%b", bid);
338 expect(i, "%H", host);
340 expect(i2, "%u", "root");
341 expect(i2, "%U", "0");
343 expect(i3, "%n", "name@inst.service");
344 expect(i3, "%N", "name@inst");
345 expect(i3, "%p", "name");
346 expect(i3, "%u", "xxxx-no-such-user");
347 expect(i3, "%U", NULL);
348 expect(i3, "%m", mid);
349 expect(i3, "%b", bid);
350 expect(i3, "%H", host);
352 expect(i4, "%u", "root");
353 expect(i4, "%U", "0");
355 #pragma GCC diagnostic pop
357 int main(int argc, char *argv[]) {
360 log_parse_environment();
363 r = test_unit_file_get_set();
364 test_config_parse_exec();
365 test_load_env_file_1();
366 test_load_env_file_2();
367 test_load_env_file_3();
368 test_load_env_file_4();
369 test_install_printf();