chiark / gitweb /
tests: add tests for config_parse_exec
[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         /* semicolon */
115         r = config_parse_exec("fake", 5, "section",
116                               "LValue", 0,
117                               "-@/RValue argv0 r1 ; "
118                               "/goo/goo boo",
119                               &c, NULL);
120         assert_se(r >= 0);
121         c1 = c1->command_next;
122         check_execcommand(c1,
123                           "/RValue", "argv0", "r1", true);
124
125         c1 = c1->command_next;
126         check_execcommand(c1,
127                           "/goo/goo", "/goo/goo", "boo", false);
128
129         /* trailing semicolon */
130         r = config_parse_exec("fake", 5, "section",
131                               "LValue", 0,
132                               "-@/RValue argv0 r1 ; ",
133                               &c, NULL);
134         assert_se(r >= 0);
135         c1 = c1->command_next;
136         check_execcommand(c1,
137                           "/RValue", "argv0", "r1", true);
138
139         assert_se(c1->command_next == NULL);
140
141         exec_command_free_list(c);
142 }
143
144 int main(int argc, char *argv[]) {
145
146         test_unit_file_get_set();
147         test_config_parse_exec();
148
149         return 0;
150 }