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/>.
33 #include "load-fragment.h"
36 static void test_unit_file_get_set(void) {
42 h = hashmap_new(string_hash_func, string_compare_func);
45 r = unit_file_get_list(UNIT_FILE_SYSTEM, NULL, h);
46 log_info("unit_file_get_list: %s", strerror(-r));
49 HASHMAP_FOREACH(p, h, i)
50 printf("%s = %s\n", p->path, unit_file_state_to_string(p->state));
52 unit_file_list_free(h);
55 static void check_execcommand(ExecCommand *c,
61 log_info("%s %s %s %s",
62 c->path, c->argv[0], c->argv[1], c->argv[2]);
63 assert_se(streq(c->path, path));
64 assert_se(streq(c->argv[0], argv0));
65 assert_se(streq(c->argv[1], argv1));
66 assert_se(c->argv[2] == NULL);
67 assert_se(c->ignore == ignore);
70 static void test_config_parse_exec(void) {
71 /* int config_parse_exec( */
72 /* const char *filename, */
74 /* const char *section, */
75 /* const char *lvalue, */
77 /* const char *rvalue, */
82 ExecCommand *c = NULL, *c1;
85 r = config_parse_exec("fake", 1, "section",
86 "LValue", 0, "/RValue r1",
89 check_execcommand(c, "/RValue", "/RValue", "r1", false);
91 r = config_parse_exec("fake", 2, "section",
92 "LValue", 0, "/RValue///slashes/// r1",
97 check_execcommand(c1, "/RValue/slashes", "/RValue///slashes///",
101 r = config_parse_exec("fake", 3, "section",
102 "LValue", 0, "@/RValue///slashes2/// argv0 r1",
105 c1 = c1->command_next;
106 check_execcommand(c1, "/RValue/slashes2", "argv0", "r1", false);
108 /* ignore && honour_argv0 */
109 r = config_parse_exec("fake", 4, "section",
110 "LValue", 0, "-@/RValue///slashes3/// argv0a r1",
113 c1 = c1->command_next;
114 check_execcommand(c1,
115 "/RValue/slashes3", "argv0a", "r1", true);
117 /* ignore && honour_argv0 */
118 r = config_parse_exec("fake", 4, "section",
119 "LValue", 0, "@-/RValue///slashes4/// argv0b r1",
122 c1 = c1->command_next;
123 check_execcommand(c1,
124 "/RValue/slashes4", "argv0b", "r1", true);
126 /* ignore && ignore */
127 r = config_parse_exec("fake", 4, "section",
128 "LValue", 0, "--/RValue argv0 r1",
131 assert_se(c1->command_next == NULL);
133 /* ignore && ignore */
134 r = config_parse_exec("fake", 4, "section",
135 "LValue", 0, "-@-/RValue argv0 r1",
138 assert_se(c1->command_next == NULL);
141 r = config_parse_exec("fake", 5, "section",
143 "-@/RValue argv0 r1 ; "
147 c1 = c1->command_next;
148 check_execcommand(c1,
149 "/RValue", "argv0", "r1", true);
151 c1 = c1->command_next;
152 check_execcommand(c1,
153 "/goo/goo", "/goo/goo", "boo", false);
155 /* trailing semicolon */
156 r = config_parse_exec("fake", 5, "section",
158 "-@/RValue argv0 r1 ; ",
161 c1 = c1->command_next;
162 check_execcommand(c1,
163 "/RValue", "argv0", "r1", true);
165 assert_se(c1->command_next == NULL);
167 /* escaped semicolon */
168 r = config_parse_exec("fake", 5, "section",
173 c1 = c1->command_next;
174 check_execcommand(c1,
175 "/usr/bin/find", "/usr/bin/find", ";", false);
177 exec_command_free_list(c);
194 static void test_load_env_file_1(void) {
195 char _cleanup_strv_free_ **data = NULL;
198 char name[] = "/tmp/test-load-env-file.XXXXXX";
199 int _cleanup_close_ fd = mkstemp(name);
201 assert_se(write(fd, env_file_1, sizeof(env_file_1)) == sizeof(env_file_1));
203 r = load_env_file(name, &data);
205 assert(streq(data[0], "a"));
206 assert(streq(data[1], "bc"));
207 assert(streq(data[2], "def"));
208 assert(streq(data[3], "g\\"));
209 assert(streq(data[4], "h"));
210 assert(streq(data[5], "i\\"));
211 assert(data[6] == NULL);
215 static void test_load_env_file_2(void) {
216 char _cleanup_strv_free_ **data = NULL;
219 char name[] = "/tmp/test-load-env-file.XXXXXX";
220 int _cleanup_close_ fd = mkstemp(name);
222 assert_se(write(fd, env_file_2, sizeof(env_file_2)) == sizeof(env_file_2));
224 r = load_env_file(name, &data);
226 assert(streq(data[0], "a"));
227 assert(data[1] == NULL);
232 int main(int argc, char *argv[]) {
234 test_unit_file_get_set();
235 test_config_parse_exec();
236 test_load_env_file_1();
237 test_load_env_file_2();