chiark / gitweb /
core: lift restriction on order of - and @ in ExecStart
[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
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <assert.h>
23 #include <stdio.h>
24 #include <stddef.h>
25 #include <string.h>
26
27 #include "install.h"
28 #include "util.h"
29 #include "macro.h"
30 #include "hashmap.h"
31 #include "load-fragment.h"
32
33 static void test_unit_file_get_set(void) {
34         int r;
35         Hashmap *h;
36         Iterator i;
37         UnitFileList *p;
38
39         h = hashmap_new(string_hash_func, string_compare_func);
40         assert(h);
41
42         r = unit_file_get_list(UNIT_FILE_SYSTEM, NULL, h);
43         log_info("unit_file_get_list: %s", strerror(-r));
44         assert(r >= 0);
45
46         HASHMAP_FOREACH(p, h, i)
47                 printf("%s = %s\n", p->path, unit_file_state_to_string(p->state));
48
49         unit_file_list_free(h);
50 }
51
52 static void check_execcommand(ExecCommand *c,
53                               const char* path,
54                               const char* argv0,
55                               const char* argv1,
56                               bool ignore) {
57         assert_se(c);
58         log_info("%s %s %s %s",
59                  c->path, c->argv[0], c->argv[1], c->argv[2]);
60         assert_se(streq(c->path, path));
61         assert_se(streq(c->argv[0], argv0));
62         assert_se(streq(c->argv[1], argv1));
63         assert_se(c->argv[2] == NULL);
64         assert_se(c->ignore == ignore);
65 }
66
67 static void test_config_parse_exec(void) {
68         /* int config_parse_exec( */
69         /*         const char *filename, */
70         /*         unsigned line, */
71         /*         const char *section, */
72         /*         const char *lvalue, */
73         /*         int ltype, */
74         /*         const char *rvalue, */
75         /*         void *data, */
76         /*         void *userdata) */
77         int r;
78
79         ExecCommand *c = NULL, *c1;
80
81         /* basic test */
82         r = config_parse_exec("fake", 1, "section",
83                               "LValue", 0, "/RValue r1",
84                               &c, NULL);
85         assert_se(r >= 0);
86         check_execcommand(c, "/RValue", "/RValue", "r1", false);
87
88         r = config_parse_exec("fake", 2, "section",
89                               "LValue", 0, "/RValue///slashes/// r1",
90                               &c, NULL);
91        /* test slashes */
92         assert_se(r >= 0);
93         c1 = c->command_next;
94         check_execcommand(c1, "/RValue/slashes", "/RValue///slashes///",
95                           "r1", false);
96
97         /* honour_argv0 */
98         r = config_parse_exec("fake", 3, "section",
99                               "LValue", 0, "@/RValue///slashes2/// argv0 r1",
100                               &c, NULL);
101         assert_se(r >= 0);
102         c1 = c1->command_next;
103         check_execcommand(c1, "/RValue/slashes2", "argv0", "r1", false);
104
105         /* ignore && honour_argv0 */
106         r = config_parse_exec("fake", 4, "section",
107                               "LValue", 0, "-@/RValue///slashes3/// argv0a r1",
108                               &c, NULL);
109         assert_se(r >= 0);
110         c1 = c1->command_next;
111         check_execcommand(c1,
112                           "/RValue/slashes3", "argv0a", "r1", true);
113
114         /* ignore && honour_argv0 */
115         r = config_parse_exec("fake", 4, "section",
116                               "LValue", 0, "@-/RValue///slashes4/// argv0b r1",
117                               &c, NULL);
118         assert_se(r >= 0);
119         c1 = c1->command_next;
120         check_execcommand(c1,
121                           "/RValue/slashes4", "argv0b", "r1", true);
122
123         /* ignore && ignore */
124         r = config_parse_exec("fake", 4, "section",
125                               "LValue", 0, "--/RValue argv0 r1",
126                               &c, NULL);
127         assert_se(r == 0);
128         assert_se(c1->command_next == NULL);
129
130         /* ignore && ignore */
131         r = config_parse_exec("fake", 4, "section",
132                               "LValue", 0, "-@-/RValue argv0 r1",
133                               &c, NULL);
134         assert_se(r == 0);
135         assert_se(c1->command_next == NULL);
136
137         /* semicolon */
138         r = config_parse_exec("fake", 5, "section",
139                               "LValue", 0,
140                               "-@/RValue argv0 r1 ; "
141                               "/goo/goo boo",
142                               &c, NULL);
143         assert_se(r >= 0);
144         c1 = c1->command_next;
145         check_execcommand(c1,
146                           "/RValue", "argv0", "r1", true);
147
148         c1 = c1->command_next;
149         check_execcommand(c1,
150                           "/goo/goo", "/goo/goo", "boo", false);
151
152         /* trailing semicolon */
153         r = config_parse_exec("fake", 5, "section",
154                               "LValue", 0,
155                               "-@/RValue argv0 r1 ; ",
156                               &c, NULL);
157         assert_se(r >= 0);
158         c1 = c1->command_next;
159         check_execcommand(c1,
160                           "/RValue", "argv0", "r1", true);
161
162         assert_se(c1->command_next == NULL);
163
164         /* escaped semicolon */
165         r = config_parse_exec("fake", 5, "section",
166                               "LValue", 0,
167                               "/usr/bin/find \\;",
168                               &c, NULL);
169         assert_se(r >= 0);
170         c1 = c1->command_next;
171         check_execcommand(c1,
172                           "/usr/bin/find", "/usr/bin/find", ";", false);
173
174         exec_command_free_list(c);
175 }
176
177 int main(int argc, char *argv[]) {
178
179         test_unit_file_get_set();
180         test_config_parse_exec();
181
182         return 0;
183 }