chiark / gitweb /
de8be3257ad8f73cf511a0169f497c4c505cb1d1
[elogind.git] / src / test / test-unit-file.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2012 Lennart Poettering
7   Copyright 2013 Zbigniew JÄ™drzejewski-Szmek
8
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.
13
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.
18
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/>.
21 ***/
22
23 #include <assert.h>
24 #include <stdio.h>
25 #include <stddef.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include "install.h"
30 #include "util.h"
31 #include "macro.h"
32 #include "hashmap.h"
33 #include "load-fragment.h"
34 #include "strv.h"
35
36 static void test_unit_file_get_set(void) {
37         int r;
38         Hashmap *h;
39         Iterator i;
40         UnitFileList *p;
41
42         h = hashmap_new(string_hash_func, string_compare_func);
43         assert(h);
44
45         r = unit_file_get_list(UNIT_FILE_SYSTEM, NULL, h);
46         log_info("unit_file_get_list: %s", strerror(-r));
47         assert(r >= 0);
48
49         HASHMAP_FOREACH(p, h, i)
50                 printf("%s = %s\n", p->path, unit_file_state_to_string(p->state));
51
52         unit_file_list_free(h);
53 }
54
55 static void check_execcommand(ExecCommand *c,
56                               const char* path,
57                               const char* argv0,
58                               const char* argv1,
59                               bool ignore) {
60         assert_se(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);
68 }
69
70 static void test_config_parse_exec(void) {
71         /* int config_parse_exec( */
72         /*         const char *filename, */
73         /*         unsigned line, */
74         /*         const char *section, */
75         /*         const char *lvalue, */
76         /*         int ltype, */
77         /*         const char *rvalue, */
78         /*         void *data, */
79         /*         void *userdata) */
80         int r;
81
82         ExecCommand *c = NULL, *c1;
83
84         /* basic test */
85         r = config_parse_exec("fake", 1, "section",
86                               "LValue", 0, "/RValue r1",
87                               &c, NULL);
88         assert_se(r >= 0);
89         check_execcommand(c, "/RValue", "/RValue", "r1", false);
90
91         r = config_parse_exec("fake", 2, "section",
92                               "LValue", 0, "/RValue///slashes/// r1",
93                               &c, NULL);
94        /* test slashes */
95         assert_se(r >= 0);
96         c1 = c->command_next;
97         check_execcommand(c1, "/RValue/slashes", "/RValue///slashes///",
98                           "r1", false);
99
100         /* honour_argv0 */
101         r = config_parse_exec("fake", 3, "section",
102                               "LValue", 0, "@/RValue///slashes2/// argv0 r1",
103                               &c, NULL);
104         assert_se(r >= 0);
105         c1 = c1->command_next;
106         check_execcommand(c1, "/RValue/slashes2", "argv0", "r1", false);
107
108         /* ignore && honour_argv0 */
109         r = config_parse_exec("fake", 4, "section",
110                               "LValue", 0, "-@/RValue///slashes3/// argv0a r1",
111                               &c, NULL);
112         assert_se(r >= 0);
113         c1 = c1->command_next;
114         check_execcommand(c1,
115                           "/RValue/slashes3", "argv0a", "r1", true);
116
117         /* ignore && honour_argv0 */
118         r = config_parse_exec("fake", 4, "section",
119                               "LValue", 0, "@-/RValue///slashes4/// argv0b r1",
120                               &c, NULL);
121         assert_se(r >= 0);
122         c1 = c1->command_next;
123         check_execcommand(c1,
124                           "/RValue/slashes4", "argv0b", "r1", true);
125
126         /* ignore && ignore */
127         r = config_parse_exec("fake", 4, "section",
128                               "LValue", 0, "--/RValue argv0 r1",
129                               &c, NULL);
130         assert_se(r == 0);
131         assert_se(c1->command_next == NULL);
132
133         /* ignore && ignore */
134         r = config_parse_exec("fake", 4, "section",
135                               "LValue", 0, "-@-/RValue argv0 r1",
136                               &c, NULL);
137         assert_se(r == 0);
138         assert_se(c1->command_next == NULL);
139
140         /* semicolon */
141         r = config_parse_exec("fake", 5, "section",
142                               "LValue", 0,
143                               "-@/RValue argv0 r1 ; "
144                               "/goo/goo boo",
145                               &c, NULL);
146         assert_se(r >= 0);
147         c1 = c1->command_next;
148         check_execcommand(c1,
149                           "/RValue", "argv0", "r1", true);
150
151         c1 = c1->command_next;
152         check_execcommand(c1,
153                           "/goo/goo", "/goo/goo", "boo", false);
154
155         /* trailing semicolon */
156         r = config_parse_exec("fake", 5, "section",
157                               "LValue", 0,
158                               "-@/RValue argv0 r1 ; ",
159                               &c, NULL);
160         assert_se(r >= 0);
161         c1 = c1->command_next;
162         check_execcommand(c1,
163                           "/RValue", "argv0", "r1", true);
164
165         assert_se(c1->command_next == NULL);
166
167         /* escaped semicolon */
168         r = config_parse_exec("fake", 5, "section",
169                               "LValue", 0,
170                               "/usr/bin/find \\;",
171                               &c, NULL);
172         assert_se(r >= 0);
173         c1 = c1->command_next;
174         check_execcommand(c1,
175                           "/usr/bin/find", "/usr/bin/find", ";", false);
176
177         exec_command_free_list(c);
178 }
179
180 #define env_file_1 \
181         "a\n"      \
182         "b\\\n"    \
183         "c\n"      \
184         "d\\\n"    \
185         "e\\\n"    \
186         "f\n"      \
187         "g\\ \n"   \
188         "h\n"      \
189         "i\\"
190
191 #define env_file_2 \
192         "a\\\n"
193
194 static void test_load_env_file_1(void) {
195         char _cleanup_strv_free_ **data = NULL;
196         int r;
197
198         char name[] = "/tmp/test-load-env-file.XXXXXX";
199         int _cleanup_close_ fd = mkstemp(name);
200         assert(fd >= 0);
201         assert_se(write(fd, env_file_1, sizeof(env_file_1)) == sizeof(env_file_1));
202
203         r = load_env_file(name, &data);
204         assert(r == 0);
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);
212         unlink(name);
213 }
214
215 static void test_load_env_file_2(void) {
216         char _cleanup_strv_free_ **data = NULL;
217         int r;
218
219         char name[] = "/tmp/test-load-env-file.XXXXXX";
220         int _cleanup_close_ fd = mkstemp(name);
221         assert(fd >= 0);
222         assert_se(write(fd, env_file_2, sizeof(env_file_2)) == sizeof(env_file_2));
223
224         r = load_env_file(name, &data);
225         assert(r == 0);
226         assert(streq(data[0], "a"));
227         assert(data[1] == NULL);
228         unlink(name);
229 }
230
231
232 int main(int argc, char *argv[]) {
233
234         test_unit_file_get_set();
235         test_config_parse_exec();
236         test_load_env_file_1();
237         test_load_env_file_2();
238
239         return 0;
240 }