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"
38 #include "test-helper.h"
40 static int test_unit_file_get_set(void) {
46 h = hashmap_new(string_hash_func, string_compare_func);
49 r = unit_file_get_list(UNIT_FILE_SYSTEM, NULL, h);
50 log_full(r == 0 ? LOG_INFO : LOG_ERR,
51 "unit_file_get_list: %s", strerror(-r));
55 HASHMAP_FOREACH(p, h, i)
56 printf("%s = %s\n", p->path, unit_file_state_to_string(p->state));
58 unit_file_list_free(h);
63 static void check_execcommand(ExecCommand *c,
69 log_info("%s %s %s %s",
70 c->path, c->argv[0], c->argv[1], c->argv[2]);
71 assert_se(streq(c->path, path));
72 assert_se(streq(c->argv[0], argv0));
73 assert_se(streq(c->argv[1], argv1));
74 assert_se(c->argv[2] == NULL);
75 assert_se(c->ignore == ignore);
78 static void test_config_parse_exec(void) {
79 /* int config_parse_exec( */
80 /* const char *filename, */
82 /* const char *section, */
83 /* unsigned section_line, */
84 /* const char *lvalue, */
86 /* const char *rvalue, */
91 ExecCommand *c = NULL, *c1;
94 r = config_parse_exec(NULL, "fake", 1, "section", 1,
95 "LValue", 0, "/RValue r1",
98 check_execcommand(c, "/RValue", "/RValue", "r1", false);
100 r = config_parse_exec(NULL, "fake", 2, "section", 1,
101 "LValue", 0, "/RValue///slashes/// r1",
105 c1 = c->command_next;
106 check_execcommand(c1, "/RValue/slashes", "/RValue///slashes///",
110 r = config_parse_exec(NULL, "fake", 3, "section", 1,
111 "LValue", 0, "@/RValue///slashes2/// argv0 r1",
114 c1 = c1->command_next;
115 check_execcommand(c1, "/RValue/slashes2", "argv0", "r1", false);
117 /* ignore && honour_argv0 */
118 r = config_parse_exec(NULL, "fake", 4, "section", 1,
119 "LValue", 0, "-@/RValue///slashes3/// argv0a r1",
122 c1 = c1->command_next;
123 check_execcommand(c1,
124 "/RValue/slashes3", "argv0a", "r1", true);
126 /* ignore && honour_argv0 */
127 r = config_parse_exec(NULL, "fake", 4, "section", 1,
128 "LValue", 0, "@-/RValue///slashes4/// argv0b r1",
131 c1 = c1->command_next;
132 check_execcommand(c1,
133 "/RValue/slashes4", "argv0b", "r1", true);
135 /* ignore && ignore */
136 r = config_parse_exec(NULL, "fake", 4, "section", 1,
137 "LValue", 0, "--/RValue argv0 r1",
140 assert_se(c1->command_next == NULL);
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);
150 r = config_parse_exec(NULL, "fake", 5, "section", 1,
152 "-@/RValue argv0 r1 ; "
156 c1 = c1->command_next;
157 check_execcommand(c1,
158 "/RValue", "argv0", "r1", true);
160 c1 = c1->command_next;
161 check_execcommand(c1,
162 "/goo/goo", "/goo/goo", "boo", false);
164 /* trailing semicolon */
165 r = config_parse_exec(NULL, "fake", 5, "section", 1,
167 "-@/RValue argv0 r1 ; ",
170 c1 = c1->command_next;
171 check_execcommand(c1,
172 "/RValue", "argv0", "r1", true);
174 assert_se(c1->command_next == NULL);
176 /* escaped semicolon */
177 r = config_parse_exec(NULL, "fake", 5, "section", 1,
182 c1 = c1->command_next;
183 check_execcommand(c1,
184 "/usr/bin/find", "/usr/bin/find", ";", false);
186 exec_command_free_list(c);
204 "#SPAMD_ARGS=\"-d --socketpath=/var/lib/bulwark/spamd \\\n" \
205 "#--nouser-config \\\n" \
211 "HWMON_MODULES=\"coretemp f71882fg\"\n" \
213 "# For compatibility reasons\n" \
215 "MODULE_0=coretemp\n" \
219 static void test_load_env_file_1(void) {
220 _cleanup_strv_free_ char **data = NULL;
223 char name[] = "/tmp/test-load-env-file.XXXXXX";
224 _cleanup_close_ int fd = mkstemp(name);
226 assert_se(write(fd, env_file_1, sizeof(env_file_1)) == sizeof(env_file_1));
228 r = load_env_file(name, NULL, &data);
230 assert(streq(data[0], "a=a"));
231 assert(streq(data[1], "b=bc"));
232 assert(streq(data[2], "d=def"));
233 assert(streq(data[3], "g=g "));
234 assert(streq(data[4], "h=h"));
235 assert(streq(data[5], "i=i"));
236 assert(data[6] == NULL);
240 static void test_load_env_file_2(void) {
241 _cleanup_strv_free_ char **data = NULL;
244 char name[] = "/tmp/test-load-env-file.XXXXXX";
245 _cleanup_close_ int fd = mkstemp(name);
247 assert_se(write(fd, env_file_2, sizeof(env_file_2)) == sizeof(env_file_2));
249 r = load_env_file(name, NULL, &data);
251 assert(streq(data[0], "a=a"));
252 assert(data[1] == NULL);
256 static void test_load_env_file_3(void) {
257 _cleanup_strv_free_ char **data = NULL;
260 char name[] = "/tmp/test-load-env-file.XXXXXX";
261 _cleanup_close_ int fd = mkstemp(name);
263 assert_se(write(fd, env_file_3, sizeof(env_file_3)) == sizeof(env_file_3));
265 r = load_env_file(name, NULL, &data);
267 assert(data == NULL);
271 static void test_load_env_file_4(void) {
272 _cleanup_strv_free_ char **data = NULL;
275 char name[] = "/tmp/test-load-env-file.XXXXXX";
276 _cleanup_close_ int fd = mkstemp(name);
278 assert_se(write(fd, env_file_4, sizeof(env_file_4)) == sizeof(env_file_4));
280 r = load_env_file(name, NULL, &data);
282 assert(streq(data[0], "HWMON_MODULES=coretemp f71882fg"));
283 assert(streq(data[1], "MODULE_0=coretemp"));
284 assert(streq(data[2], "MODULE_1=f71882fg"));
285 assert(data[3] == NULL);
290 #pragma GCC diagnostic push
291 #pragma GCC diagnostic ignored "-Wnonnull"
293 static void test_install_printf(void) {
294 char name[] = "name.service",
295 path[] = "/run/systemd/system/name.service",
296 user[] = "xxxx-no-such-user";
297 InstallInfo i = {name, path, user};
298 InstallInfo i2 = {name, path, NULL};
299 char name3[] = "name@inst.service",
300 path3[] = "/run/systemd/system/name.service";
301 InstallInfo i3 = {name3, path3, user};
302 InstallInfo i4 = {name3, path3, NULL};
304 _cleanup_free_ char *mid, *bid, *host;
306 assert_se(specifier_machine_id('m', NULL, NULL, &mid) >= 0 && mid);
307 assert_se(specifier_boot_id('b', NULL, NULL, &bid) >= 0 && bid);
308 assert_se((host = gethostname_malloc()));
310 #define expect(src, pattern, result) \
312 _cleanup_free_ char *t = NULL; \
313 _cleanup_free_ char \
314 *d1 = strdup(i.name), \
315 *d2 = strdup(i.path), \
316 *d3 = strdup(i.user); \
317 assert_se(install_full_printf(&src, pattern, &t) >= 0 || !result); \
318 memzero(i.name, strlen(i.name)); \
319 memzero(i.path, strlen(i.path)); \
320 memzero(i.user, strlen(i.user)); \
321 assert(d1 && d2 && d3); \
324 assert(streq(t, result)); \
325 } else assert(t == NULL); \
326 strcpy(i.name, d1); \
327 strcpy(i.path, d2); \
328 strcpy(i.user, d3); \
331 assert_se(setenv("USER", "root", 1) == 0);
333 expect(i, "%n", "name.service");
334 expect(i, "%N", "name");
335 expect(i, "%p", "name");
337 expect(i, "%u", "xxxx-no-such-user");
338 expect(i, "%U", NULL);
339 expect(i, "%m", mid);
340 expect(i, "%b", bid);
341 expect(i, "%H", host);
343 expect(i2, "%u", "root");
344 expect(i2, "%U", "0");
346 expect(i3, "%n", "name@inst.service");
347 expect(i3, "%N", "name@inst");
348 expect(i3, "%p", "name");
349 expect(i3, "%u", "xxxx-no-such-user");
350 expect(i3, "%U", NULL);
351 expect(i3, "%m", mid);
352 expect(i3, "%b", bid);
353 expect(i3, "%H", host);
355 expect(i4, "%u", "root");
356 expect(i4, "%U", "0");
358 #pragma GCC diagnostic pop
360 int main(int argc, char *argv[]) {
363 log_parse_environment();
366 r = test_unit_file_get_set();
367 test_config_parse_exec();
368 test_load_env_file_1();
369 test_load_env_file_2();
370 test_load_env_file_3();
371 test_load_env_file_4();
372 TEST_REQ_RUNNING_SYSTEMD(test_install_printf());