chiark / gitweb /
util: add function getting proc environ
[elogind.git] / src / test / test-condition.c
1 /***
2   This file is part of systemd
3
4   Copyright 2014 Ronny Chevalier
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include "condition.h"
21 #include "macro.h"
22 #include "util.h"
23 #include "log.h"
24 #include "architecture.h"
25 #include "sd-id128.h"
26
27 static void test_condition_test_path_exists(void) {
28         Condition *condition;
29
30         condition = condition_new(CONDITION_PATH_EXISTS, "/bin/sh", false, false);
31         assert_se(condition_test(condition));
32         condition_free(condition);
33
34         condition = condition_new(CONDITION_PATH_EXISTS, "/bin/s?", false, false);
35         assert_se(!condition_test(condition));
36         condition_free(condition);
37
38         condition = condition_new(CONDITION_PATH_EXISTS_GLOB, "/bin/s?", false, false);
39         assert_se(condition_test(condition));
40         condition_free(condition);
41
42         condition = condition_new(CONDITION_PATH_EXISTS_GLOB, "/bin/s?", false, true);
43         assert_se(!condition_test(condition));
44         condition_free(condition);
45
46         condition = condition_new(CONDITION_PATH_EXISTS, "/thiscertainlywontexist", false, false);
47         assert_se(!condition_test(condition));
48         condition_free(condition);
49
50         condition = condition_new(CONDITION_PATH_EXISTS, "/thiscertainlywontexist", false, true);
51         assert_se(condition_test(condition));
52         condition_free(condition);
53
54         condition = condition_new(CONDITION_PATH_IS_DIRECTORY, "/bin", false, false);
55         assert_se(condition_test(condition));
56         condition_free(condition);
57
58         condition = condition_new(CONDITION_DIRECTORY_NOT_EMPTY, "/bin", false, false);
59         assert_se(condition_test(condition));
60         condition_free(condition);
61
62         condition = condition_new(CONDITION_FILE_NOT_EMPTY, "/bin/sh", false, false);
63         assert_se(condition_test(condition));
64         condition_free(condition);
65
66         condition = condition_new(CONDITION_FILE_IS_EXECUTABLE, "/bin/sh", false, false);
67         assert_se(condition_test(condition));
68         condition_free(condition);
69
70         condition = condition_new(CONDITION_FILE_IS_EXECUTABLE, "/etc/passwd", false, false);
71         assert_se(!condition_test(condition));
72         condition_free(condition);
73
74         condition = condition_new(CONDITION_PATH_IS_MOUNT_POINT, "/proc", false, false);
75         assert_se(condition_test(condition));
76         condition_free(condition);
77
78         condition = condition_new(CONDITION_PATH_IS_MOUNT_POINT, "/", false, false);
79         assert_se(condition_test(condition));
80         condition_free(condition);
81
82         condition = condition_new(CONDITION_PATH_IS_MOUNT_POINT, "/bin", false, false);
83         assert_se(!condition_test(condition));
84         condition_free(condition);
85 }
86
87 static void test_condition_test_ac_power(void) {
88         Condition *condition;
89
90         condition = condition_new(CONDITION_AC_POWER, "true", false, false);
91         assert_se(condition_test(condition) == on_ac_power());
92         condition_free(condition);
93
94         condition = condition_new(CONDITION_AC_POWER, "false", false, false);
95         assert_se(condition_test(condition) != on_ac_power());
96         condition_free(condition);
97
98         condition = condition_new(CONDITION_AC_POWER, "false", false, true);
99         assert_se(condition_test(condition) == on_ac_power());
100         condition_free(condition);
101 }
102
103 static void test_condition_test_host(void) {
104         Condition *condition;
105         sd_id128_t id;
106         int r;
107         char sid[SD_ID128_STRING_MAX];
108         _cleanup_free_ char *hostname = NULL;
109
110         r = sd_id128_get_machine(&id);
111         assert_se(r >= 0);
112         assert_se(sd_id128_to_string(id, sid));
113
114         condition = condition_new(CONDITION_HOST, sid, false, false);
115         assert_se(condition_test(condition));
116         condition_free(condition);
117
118         condition = condition_new(CONDITION_HOST, "garbage value jjjjjjjjjjjjjj", false, false);
119         assert_se(!condition_test(condition));
120         condition_free(condition);
121
122         condition = condition_new(CONDITION_HOST, sid, false, true);
123         assert_se(!condition_test(condition));
124         condition_free(condition);
125
126         hostname = gethostname_malloc();
127         assert_se(hostname);
128
129         condition = condition_new(CONDITION_HOST, hostname, false, false);
130         assert_se(condition_test(condition));
131         condition_free(condition);
132 }
133
134 static void test_condition_test_architecture(void) {
135         Condition *condition;
136         const char *sa;
137         int a;
138
139         a = uname_architecture();
140         assert_se(a >= 0);
141
142         sa = architecture_to_string(a);
143         assert_se(sa);
144
145         condition = condition_new(CONDITION_ARCHITECTURE, sa, false, false);
146         assert_se(condition_test(condition));
147         condition_free(condition);
148
149         condition = condition_new(CONDITION_ARCHITECTURE, "garbage value", false, false);
150         assert_se(condition_test(condition) < 0);
151         condition_free(condition);
152
153         condition = condition_new(CONDITION_ARCHITECTURE, sa, false, true);
154         assert_se(!condition_test(condition));
155         condition_free(condition);
156 }
157
158 static void test_condition_test_kernel_command_line(void) {
159         Condition *condition;
160
161         condition = condition_new(CONDITION_KERNEL_COMMAND_LINE, "thisreallyshouldntbeonthekernelcommandline", false, false);
162         assert_se(!condition_test(condition));
163         condition_free(condition);
164
165         condition = condition_new(CONDITION_KERNEL_COMMAND_LINE, "andthis=neither", false, false);
166         assert_se(!condition_test(condition));
167         condition_free(condition);
168 }
169
170 static void test_condition_test_null(void) {
171         Condition *condition;
172
173         condition = condition_new(CONDITION_NULL, NULL, false, false);
174         assert_se(condition_test(condition));
175         condition_free(condition);
176
177         condition = condition_new(CONDITION_NULL, NULL, false, true);
178         assert_se(!condition_test(condition));
179         condition_free(condition);
180 }
181
182 int main(int argc, char *argv[]) {
183         log_parse_environment();
184         log_open();
185
186         test_condition_test_path_exists();
187         test_condition_test_ac_power();
188         test_condition_test_host();
189         test_condition_test_architecture();
190         test_condition_test_kernel_command_line();
191         test_condition_test_null();
192
193         return 0;
194 }