]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-condition.c
Merge pull request #5598 from pfl/ndisc_prefix_delegation
[thirdparty/systemd.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 "sd-id128.h"
21
22 #include "alloc-util.h"
23 #include "apparmor-util.h"
24 #include "architecture.h"
25 #include "audit-util.h"
26 #include "condition.h"
27 #include "hostname-util.h"
28 #include "id128-util.h"
29 #include "ima-util.h"
30 #include "log.h"
31 #include "macro.h"
32 #include "selinux-util.h"
33 #include "smack-util.h"
34 #include "strv.h"
35 #include "virt.h"
36 #include "util.h"
37
38 static void test_condition_test_path(void) {
39 Condition *condition;
40
41 condition = condition_new(CONDITION_PATH_EXISTS, "/bin/sh", false, false);
42 assert_se(condition);
43 assert_se(condition_test(condition));
44 condition_free(condition);
45
46 condition = condition_new(CONDITION_PATH_EXISTS, "/bin/s?", false, false);
47 assert_se(condition);
48 assert_se(!condition_test(condition));
49 condition_free(condition);
50
51 condition = condition_new(CONDITION_PATH_EXISTS_GLOB, "/bin/s?", false, false);
52 assert_se(condition);
53 assert_se(condition_test(condition));
54 condition_free(condition);
55
56 condition = condition_new(CONDITION_PATH_EXISTS_GLOB, "/bin/s?", false, true);
57 assert_se(condition);
58 assert_se(!condition_test(condition));
59 condition_free(condition);
60
61 condition = condition_new(CONDITION_PATH_EXISTS, "/thiscertainlywontexist", false, false);
62 assert_se(condition);
63 assert_se(!condition_test(condition));
64 condition_free(condition);
65
66 condition = condition_new(CONDITION_PATH_EXISTS, "/thiscertainlywontexist", false, true);
67 assert_se(condition);
68 assert_se(condition_test(condition));
69 condition_free(condition);
70
71 condition = condition_new(CONDITION_PATH_IS_DIRECTORY, "/bin", false, false);
72 assert_se(condition);
73 assert_se(condition_test(condition));
74 condition_free(condition);
75
76 condition = condition_new(CONDITION_DIRECTORY_NOT_EMPTY, "/bin", false, false);
77 assert_se(condition);
78 assert_se(condition_test(condition));
79 condition_free(condition);
80
81 condition = condition_new(CONDITION_FILE_NOT_EMPTY, "/bin/sh", false, false);
82 assert_se(condition);
83 assert_se(condition_test(condition));
84 condition_free(condition);
85
86 condition = condition_new(CONDITION_FILE_IS_EXECUTABLE, "/bin/sh", false, false);
87 assert_se(condition);
88 assert_se(condition_test(condition));
89 condition_free(condition);
90
91 condition = condition_new(CONDITION_FILE_IS_EXECUTABLE, "/etc/passwd", false, false);
92 assert_se(condition);
93 assert_se(!condition_test(condition));
94 condition_free(condition);
95
96 condition = condition_new(CONDITION_PATH_IS_MOUNT_POINT, "/proc", false, false);
97 assert_se(condition);
98 assert_se(condition_test(condition));
99 condition_free(condition);
100
101 condition = condition_new(CONDITION_PATH_IS_MOUNT_POINT, "/", false, false);
102 assert_se(condition);
103 assert_se(condition_test(condition));
104 condition_free(condition);
105
106 condition = condition_new(CONDITION_PATH_IS_MOUNT_POINT, "/bin", false, false);
107 assert_se(condition);
108 assert_se(!condition_test(condition));
109 condition_free(condition);
110
111 condition = condition_new(CONDITION_PATH_IS_READ_WRITE, "/tmp", false, false);
112 assert_se(condition);
113 assert_se(condition_test(condition));
114 condition_free(condition);
115
116 condition = condition_new(CONDITION_PATH_IS_SYMBOLIC_LINK, "/dev/stdout", false, false);
117 assert_se(condition);
118 assert_se(condition_test(condition));
119 condition_free(condition);
120 }
121
122 static void test_condition_test_ac_power(void) {
123 Condition *condition;
124
125 condition = condition_new(CONDITION_AC_POWER, "true", false, false);
126 assert_se(condition);
127 assert_se(condition_test(condition) == on_ac_power());
128 condition_free(condition);
129
130 condition = condition_new(CONDITION_AC_POWER, "false", false, false);
131 assert_se(condition);
132 assert_se(condition_test(condition) != on_ac_power());
133 condition_free(condition);
134
135 condition = condition_new(CONDITION_AC_POWER, "false", false, true);
136 assert_se(condition);
137 assert_se(condition_test(condition) == on_ac_power());
138 condition_free(condition);
139 }
140
141 static void test_condition_test_host(void) {
142 _cleanup_free_ char *hostname = NULL;
143 char sid[SD_ID128_STRING_MAX];
144 Condition *condition;
145 sd_id128_t id;
146 int r;
147
148 r = sd_id128_get_machine(&id);
149 assert_se(r >= 0);
150 assert_se(sd_id128_to_string(id, sid));
151
152 condition = condition_new(CONDITION_HOST, sid, false, false);
153 assert_se(condition);
154 assert_se(condition_test(condition));
155 condition_free(condition);
156
157 condition = condition_new(CONDITION_HOST, "garbage value jjjjjjjjjjjjjj", false, false);
158 assert_se(condition);
159 assert_se(!condition_test(condition));
160 condition_free(condition);
161
162 condition = condition_new(CONDITION_HOST, sid, false, true);
163 assert_se(condition);
164 assert_se(!condition_test(condition));
165 condition_free(condition);
166
167 hostname = gethostname_malloc();
168 assert_se(hostname);
169
170 /* if hostname looks like an id128 then skip testing it */
171 if (id128_is_valid(hostname))
172 log_notice("hostname is an id128, skipping test");
173 else {
174 condition = condition_new(CONDITION_HOST, hostname, false, false);
175 assert_se(condition);
176 assert_se(condition_test(condition));
177 condition_free(condition);
178 }
179 }
180
181 static void test_condition_test_architecture(void) {
182 Condition *condition;
183 const char *sa;
184 int a;
185
186 a = uname_architecture();
187 assert_se(a >= 0);
188
189 sa = architecture_to_string(a);
190 assert_se(sa);
191
192 condition = condition_new(CONDITION_ARCHITECTURE, sa, false, false);
193 assert_se(condition);
194 assert_se(condition_test(condition) > 0);
195 condition_free(condition);
196
197 condition = condition_new(CONDITION_ARCHITECTURE, "garbage value", false, false);
198 assert_se(condition);
199 assert_se(condition_test(condition) == 0);
200 condition_free(condition);
201
202 condition = condition_new(CONDITION_ARCHITECTURE, sa, false, true);
203 assert_se(condition);
204 assert_se(condition_test(condition) == 0);
205 condition_free(condition);
206 }
207
208 static void test_condition_test_kernel_command_line(void) {
209 Condition *condition;
210
211 condition = condition_new(CONDITION_KERNEL_COMMAND_LINE, "thisreallyshouldntbeonthekernelcommandline", false, false);
212 assert_se(condition);
213 assert_se(!condition_test(condition));
214 condition_free(condition);
215
216 condition = condition_new(CONDITION_KERNEL_COMMAND_LINE, "andthis=neither", false, false);
217 assert_se(condition);
218 assert_se(!condition_test(condition));
219 condition_free(condition);
220 }
221
222 static void test_condition_test_null(void) {
223 Condition *condition;
224
225 condition = condition_new(CONDITION_NULL, NULL, false, false);
226 assert_se(condition);
227 assert_se(condition_test(condition));
228 condition_free(condition);
229
230 condition = condition_new(CONDITION_NULL, NULL, false, true);
231 assert_se(condition);
232 assert_se(!condition_test(condition));
233 condition_free(condition);
234 }
235
236 static void test_condition_test_security(void) {
237 Condition *condition;
238
239 condition = condition_new(CONDITION_SECURITY, "garbage oifdsjfoidsjoj", false, false);
240 assert_se(condition);
241 assert_se(!condition_test(condition));
242 condition_free(condition);
243
244 condition = condition_new(CONDITION_SECURITY, "selinux", false, true);
245 assert_se(condition);
246 assert_se(condition_test(condition) != mac_selinux_use());
247 condition_free(condition);
248
249 condition = condition_new(CONDITION_SECURITY, "ima", false, false);
250 assert_se(condition);
251 assert_se(condition_test(condition) == use_ima());
252 condition_free(condition);
253
254 condition = condition_new(CONDITION_SECURITY, "apparmor", false, false);
255 assert_se(condition);
256 assert_se(condition_test(condition) == mac_apparmor_use());
257 condition_free(condition);
258
259 condition = condition_new(CONDITION_SECURITY, "smack", false, false);
260 assert_se(condition);
261 assert_se(condition_test(condition) == mac_smack_use());
262 condition_free(condition);
263
264 condition = condition_new(CONDITION_SECURITY, "audit", false, false);
265 assert_se(condition);
266 assert_se(condition_test(condition) == use_audit());
267 condition_free(condition);
268 }
269
270 static void test_condition_test_virtualization(void) {
271 Condition *condition;
272 const char *virt;
273 int r;
274
275 condition = condition_new(CONDITION_VIRTUALIZATION, "garbage oifdsjfoidsjoj", false, false);
276 assert_se(condition);
277 r = condition_test(condition);
278 log_info("ConditionVirtualization=garbage → %i", r);
279 assert_se(r == 0);
280 condition_free(condition);
281
282 condition = condition_new(CONDITION_VIRTUALIZATION, "container", false, false);
283 assert_se(condition);
284 r = condition_test(condition);
285 log_info("ConditionVirtualization=container → %i", r);
286 assert_se(r == !!detect_container());
287 condition_free(condition);
288
289 condition = condition_new(CONDITION_VIRTUALIZATION, "vm", false, false);
290 assert_se(condition);
291 r = condition_test(condition);
292 log_info("ConditionVirtualization=vm → %i", r);
293 assert_se(r == (detect_vm() && !detect_container()));
294 condition_free(condition);
295
296 condition = condition_new(CONDITION_VIRTUALIZATION, "private-users", false, false);
297 assert_se(condition);
298 r = condition_test(condition);
299 log_info("ConditionVirtualization=private-users → %i", r);
300 assert_se(r == !!running_in_userns());
301 condition_free(condition);
302
303 NULSTR_FOREACH(virt,
304 "kvm\0"
305 "qemu\0"
306 "bochs\0"
307 "xen\0"
308 "uml\0"
309 "vmware\0"
310 "oracle\0"
311 "microsoft\0"
312 "zvm\0"
313 "parallels\0"
314 "bhyve\0"
315 "vm_other\0") {
316
317 condition = condition_new(CONDITION_VIRTUALIZATION, virt, false, false);
318 assert_se(condition);
319 r = condition_test(condition);
320 log_info("ConditionVirtualization=%s → %i", virt, r);
321 assert_se(r >= 0);
322 condition_free(condition);
323 }
324 }
325
326 int main(int argc, char *argv[]) {
327 log_set_max_level(LOG_DEBUG);
328 log_parse_environment();
329 log_open();
330
331 test_condition_test_path();
332 test_condition_test_ac_power();
333 test_condition_test_host();
334 test_condition_test_architecture();
335 test_condition_test_kernel_command_line();
336 test_condition_test_null();
337 test_condition_test_security();
338 test_condition_test_virtualization();
339
340 return 0;
341 }