]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-condition.c
resolve: allow configurable bind address
[thirdparty/systemd.git] / src / test / test-condition.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
b08f2be6 2
c465a29f
FS
3#include <stdio.h>
4#include <sys/types.h>
5022f08a 5#include <sys/utsname.h>
c465a29f
FS
6#include <unistd.h>
7
d1bddcec 8#include "sd-id128.h"
07630cea 9
b5efdb8a 10#include "alloc-util.h"
07630cea
LP
11#include "apparmor-util.h"
12#include "architecture.h"
430f0182 13#include "audit-util.h"
e16647c3 14#include "cgroup-util.h"
2767027b 15#include "condition.h"
f44b3035 16#include "cpu-set-util.h"
0bb2f0f1 17#include "efi-loader.h"
3c14dc61 18#include "errno-util.h"
07630cea 19#include "hostname-util.h"
40a23924 20#include "id128-util.h"
015df1f7 21#include "ima-util.h"
a70984c0 22#include "limits-util.h"
07630cea
LP
23#include "log.h"
24#include "macro.h"
d8b4d14d 25#include "nulstr-util.h"
a70984c0 26#include "process-util.h"
07630cea 27#include "selinux-util.h"
e16647c3 28#include "set.h"
015df1f7 29#include "smack-util.h"
5022f08a 30#include "string-util.h"
239a5707 31#include "strv.h"
d8b4d14d 32#include "tests.h"
fc65dabd 33#include "tomoyo-util.h"
c465a29f 34#include "user-util.h"
5022f08a 35#include "virt.h"
d1bddcec 36
015df1f7 37static void test_condition_test_path(void) {
d1bddcec
LP
38 Condition *condition;
39
40 condition = condition_new(CONDITION_PATH_EXISTS, "/bin/sh", false, false);
4d548a7d 41 assert_se(condition);
a0b191b7 42 assert_se(condition_test(condition, environ));
d1bddcec
LP
43 condition_free(condition);
44
b80ba1da 45 condition = condition_new(CONDITION_PATH_EXISTS, "/bin/s?", false, false);
4d548a7d 46 assert_se(condition);
a0b191b7 47 assert_se(condition_test(condition, environ) == 0);
b80ba1da
LP
48 condition_free(condition);
49
50 condition = condition_new(CONDITION_PATH_EXISTS_GLOB, "/bin/s?", false, false);
4d548a7d 51 assert_se(condition);
a0b191b7 52 assert_se(condition_test(condition, environ) > 0);
b80ba1da
LP
53 condition_free(condition);
54
55 condition = condition_new(CONDITION_PATH_EXISTS_GLOB, "/bin/s?", false, true);
4d548a7d 56 assert_se(condition);
a0b191b7 57 assert_se(condition_test(condition, environ) == 0);
b80ba1da
LP
58 condition_free(condition);
59
d1bddcec 60 condition = condition_new(CONDITION_PATH_EXISTS, "/thiscertainlywontexist", false, false);
4d548a7d 61 assert_se(condition);
a0b191b7 62 assert_se(condition_test(condition, environ) == 0);
d1bddcec
LP
63 condition_free(condition);
64
65 condition = condition_new(CONDITION_PATH_EXISTS, "/thiscertainlywontexist", false, true);
4d548a7d 66 assert_se(condition);
a0b191b7 67 assert_se(condition_test(condition, environ) > 0);
d1bddcec 68 condition_free(condition);
b80ba1da
LP
69
70 condition = condition_new(CONDITION_PATH_IS_DIRECTORY, "/bin", false, false);
4d548a7d 71 assert_se(condition);
a0b191b7 72 assert_se(condition_test(condition, environ) > 0);
b80ba1da
LP
73 condition_free(condition);
74
75 condition = condition_new(CONDITION_DIRECTORY_NOT_EMPTY, "/bin", false, false);
4d548a7d 76 assert_se(condition);
a0b191b7 77 assert_se(condition_test(condition, environ) > 0);
b80ba1da
LP
78 condition_free(condition);
79
80 condition = condition_new(CONDITION_FILE_NOT_EMPTY, "/bin/sh", false, false);
4d548a7d 81 assert_se(condition);
a0b191b7 82 assert_se(condition_test(condition, environ) > 0);
b80ba1da
LP
83 condition_free(condition);
84
85 condition = condition_new(CONDITION_FILE_IS_EXECUTABLE, "/bin/sh", false, false);
4d548a7d 86 assert_se(condition);
a0b191b7 87 assert_se(condition_test(condition, environ) > 0);
b80ba1da
LP
88 condition_free(condition);
89
90 condition = condition_new(CONDITION_FILE_IS_EXECUTABLE, "/etc/passwd", false, false);
4d548a7d 91 assert_se(condition);
a0b191b7 92 assert_se(condition_test(condition, environ) == 0);
b80ba1da
LP
93 condition_free(condition);
94
95 condition = condition_new(CONDITION_PATH_IS_MOUNT_POINT, "/proc", false, false);
4d548a7d 96 assert_se(condition);
a0b191b7 97 assert_se(condition_test(condition, environ) > 0);
b80ba1da
LP
98 condition_free(condition);
99
100 condition = condition_new(CONDITION_PATH_IS_MOUNT_POINT, "/", false, false);
4d548a7d 101 assert_se(condition);
a0b191b7 102 assert_se(condition_test(condition, environ) > 0);
b80ba1da
LP
103 condition_free(condition);
104
105 condition = condition_new(CONDITION_PATH_IS_MOUNT_POINT, "/bin", false, false);
4d548a7d 106 assert_se(condition);
a0b191b7 107 assert_se(condition_test(condition, environ) == 0);
b80ba1da 108 condition_free(condition);
015df1f7
RC
109
110 condition = condition_new(CONDITION_PATH_IS_READ_WRITE, "/tmp", false, false);
4d548a7d 111 assert_se(condition);
a0b191b7 112 assert_se(condition_test(condition, environ) > 0);
015df1f7
RC
113 condition_free(condition);
114
7f19247b
LP
115 condition = condition_new(CONDITION_PATH_IS_ENCRYPTED, "/sys", false, false);
116 assert_se(condition);
a0b191b7 117 assert_se(condition_test(condition, environ) == 0);
7f19247b
LP
118 condition_free(condition);
119
015df1f7 120 condition = condition_new(CONDITION_PATH_IS_SYMBOLIC_LINK, "/dev/stdout", false, false);
4d548a7d 121 assert_se(condition);
a0b191b7 122 assert_se(condition_test(condition, environ) > 0);
015df1f7 123 condition_free(condition);
d1bddcec 124}
b08f2be6 125
8b81c382 126static void test_condition_test_control_group_controller(void) {
e16647c3
CD
127 Condition *condition;
128 CGroupMask system_mask;
129 CGroupController controller;
130 _cleanup_free_ char *controller_name = NULL;
131 int r;
132
d4d99bc6 133 r = cg_unified();
e16647c3
CD
134 if (r < 0) {
135 log_notice_errno(r, "Skipping ConditionControlGroupController tests: %m");
8b81c382 136 return;
e16647c3
CD
137 }
138
139 /* Invalid controllers are ignored */
140 condition = condition_new(CONDITION_CONTROL_GROUP_CONTROLLER, "thisisnotarealcontroller", false, false);
141 assert_se(condition);
a0b191b7 142 assert_se(condition_test(condition, environ) > 0);
e16647c3
CD
143 condition_free(condition);
144
145 condition = condition_new(CONDITION_CONTROL_GROUP_CONTROLLER, "thisisnotarealcontroller", false, true);
146 assert_se(condition);
a0b191b7 147 assert_se(condition_test(condition, environ) == 0);
e16647c3
CD
148 condition_free(condition);
149
150 assert_se(cg_mask_supported(&system_mask) >= 0);
151
152 /* Individual valid controllers one by one */
153 for (controller = 0; controller < _CGROUP_CONTROLLER_MAX; controller++) {
154 const char *local_controller_name = cgroup_controller_to_string(controller);
155 log_info("chosen controller is '%s'", local_controller_name);
156 if (system_mask & CGROUP_CONTROLLER_TO_MASK(controller)) {
157 log_info("this controller is available");
158 condition = condition_new(CONDITION_CONTROL_GROUP_CONTROLLER, local_controller_name, false, false);
159 assert_se(condition);
a0b191b7 160 assert_se(condition_test(condition, environ) > 0);
e16647c3
CD
161 condition_free(condition);
162
163 condition = condition_new(CONDITION_CONTROL_GROUP_CONTROLLER, local_controller_name, false, true);
164 assert_se(condition);
a0b191b7 165 assert_se(condition_test(condition, environ) == 0);
e16647c3
CD
166 condition_free(condition);
167 } else {
168 log_info("this controller is unavailable");
169 condition = condition_new(CONDITION_CONTROL_GROUP_CONTROLLER, local_controller_name, false, false);
170 assert_se(condition);
a0b191b7 171 assert_se(condition_test(condition, environ) == 0);
e16647c3
CD
172 condition_free(condition);
173
174 condition = condition_new(CONDITION_CONTROL_GROUP_CONTROLLER, local_controller_name, false, true);
175 assert_se(condition);
a0b191b7 176 assert_se(condition_test(condition, environ) > 0);
e16647c3
CD
177 condition_free(condition);
178 }
179 }
180
181 /* Multiple valid controllers at the same time */
182 assert_se(cg_mask_to_string(system_mask, &controller_name) >= 0);
183
2767027b 184 condition = condition_new(CONDITION_CONTROL_GROUP_CONTROLLER, strempty(controller_name), false, false);
e16647c3 185 assert_se(condition);
a0b191b7 186 assert_se(condition_test(condition, environ) > 0);
e16647c3
CD
187 condition_free(condition);
188
2767027b 189 condition = condition_new(CONDITION_CONTROL_GROUP_CONTROLLER, strempty(controller_name), false, true);
e16647c3 190 assert_se(condition);
a0b191b7 191 assert_se(condition_test(condition, environ) == 0);
e16647c3 192 condition_free(condition);
e16647c3
CD
193}
194
b08f2be6
RC
195static void test_condition_test_ac_power(void) {
196 Condition *condition;
197
198 condition = condition_new(CONDITION_AC_POWER, "true", false, false);
4d548a7d 199 assert_se(condition);
a0b191b7 200 assert_se(condition_test(condition, environ) == on_ac_power());
b08f2be6
RC
201 condition_free(condition);
202
203 condition = condition_new(CONDITION_AC_POWER, "false", false, false);
4d548a7d 204 assert_se(condition);
a0b191b7 205 assert_se(condition_test(condition, environ) != on_ac_power());
b08f2be6
RC
206 condition_free(condition);
207
208 condition = condition_new(CONDITION_AC_POWER, "false", false, true);
4d548a7d 209 assert_se(condition);
a0b191b7 210 assert_se(condition_test(condition, environ) == on_ac_power());
b08f2be6
RC
211 condition_free(condition);
212}
213
214static void test_condition_test_host(void) {
4d548a7d
LP
215 _cleanup_free_ char *hostname = NULL;
216 char sid[SD_ID128_STRING_MAX];
b08f2be6
RC
217 Condition *condition;
218 sd_id128_t id;
219 int r;
b08f2be6
RC
220
221 r = sd_id128_get_machine(&id);
222 assert_se(r >= 0);
223 assert_se(sd_id128_to_string(id, sid));
224
225 condition = condition_new(CONDITION_HOST, sid, false, false);
4d548a7d 226 assert_se(condition);
a0b191b7 227 assert_se(condition_test(condition, environ) > 0);
b08f2be6
RC
228 condition_free(condition);
229
230 condition = condition_new(CONDITION_HOST, "garbage value jjjjjjjjjjjjjj", false, false);
4d548a7d 231 assert_se(condition);
a0b191b7 232 assert_se(condition_test(condition, environ) == 0);
b08f2be6
RC
233 condition_free(condition);
234
235 condition = condition_new(CONDITION_HOST, sid, false, true);
4d548a7d 236 assert_se(condition);
a0b191b7 237 assert_se(condition_test(condition, environ) == 0);
b08f2be6
RC
238 condition_free(condition);
239
240 hostname = gethostname_malloc();
241 assert_se(hostname);
242
40a23924 243 /* if hostname looks like an id128 then skip testing it */
ce5fcc69 244 if (id128_is_valid(hostname))
40a23924 245 log_notice("hostname is an id128, skipping test");
ce5fcc69 246 else {
40a23924 247 condition = condition_new(CONDITION_HOST, hostname, false, false);
4d548a7d 248 assert_se(condition);
a0b191b7 249 assert_se(condition_test(condition, environ) > 0);
40a23924
SM
250 condition_free(condition);
251 }
b08f2be6
RC
252}
253
254static void test_condition_test_architecture(void) {
255 Condition *condition;
b08f2be6 256 const char *sa;
592fd144 257 int a;
b08f2be6
RC
258
259 a = uname_architecture();
260 assert_se(a >= 0);
261
262 sa = architecture_to_string(a);
263 assert_se(sa);
264
265 condition = condition_new(CONDITION_ARCHITECTURE, sa, false, false);
4d548a7d 266 assert_se(condition);
a0b191b7 267 assert_se(condition_test(condition, environ) > 0);
b08f2be6
RC
268 condition_free(condition);
269
270 condition = condition_new(CONDITION_ARCHITECTURE, "garbage value", false, false);
4d548a7d 271 assert_se(condition);
a0b191b7 272 assert_se(condition_test(condition, environ) == 0);
b08f2be6
RC
273 condition_free(condition);
274
275 condition = condition_new(CONDITION_ARCHITECTURE, sa, false, true);
4d548a7d 276 assert_se(condition);
a0b191b7 277 assert_se(condition_test(condition, environ) == 0);
b08f2be6
RC
278 condition_free(condition);
279}
280
07318c29
LP
281static void test_condition_test_kernel_command_line(void) {
282 Condition *condition;
3c14dc61 283 int r;
07318c29
LP
284
285 condition = condition_new(CONDITION_KERNEL_COMMAND_LINE, "thisreallyshouldntbeonthekernelcommandline", false, false);
4d548a7d 286 assert_se(condition);
a0b191b7 287 r = condition_test(condition, environ);
3c14dc61
TM
288 if (ERRNO_IS_PRIVILEGE(r))
289 return;
290 assert_se(r == 0);
07318c29
LP
291 condition_free(condition);
292
293 condition = condition_new(CONDITION_KERNEL_COMMAND_LINE, "andthis=neither", false, false);
4d548a7d 294 assert_se(condition);
a0b191b7 295 assert_se(condition_test(condition, environ) == 0);
07318c29
LP
296 condition_free(condition);
297}
298
5022f08a
LP
299static void test_condition_test_kernel_version(void) {
300 Condition *condition;
301 struct utsname u;
68c58c67 302 const char *v;
5022f08a
LP
303
304 condition = condition_new(CONDITION_KERNEL_VERSION, "*thisreallyshouldntbeinthekernelversion*", false, false);
305 assert_se(condition);
a0b191b7 306 assert_se(condition_test(condition, environ) == 0);
5022f08a
LP
307 condition_free(condition);
308
309 condition = condition_new(CONDITION_KERNEL_VERSION, "*", false, false);
310 assert_se(condition);
a0b191b7 311 assert_se(condition_test(condition, environ) > 0);
5022f08a
LP
312 condition_free(condition);
313
910c6d09
ZJS
314 /* An artificially empty condition. It evaluates to true, but normally
315 * such condition cannot be created, because the condition list is reset instead. */
5022f08a
LP
316 condition = condition_new(CONDITION_KERNEL_VERSION, "", false, false);
317 assert_se(condition);
a0b191b7 318 assert_se(condition_test(condition, environ) > 0);
5022f08a
LP
319 condition_free(condition);
320
321 assert_se(uname(&u) >= 0);
322
323 condition = condition_new(CONDITION_KERNEL_VERSION, u.release, false, false);
324 assert_se(condition);
a0b191b7 325 assert_se(condition_test(condition, environ) > 0);
5022f08a
LP
326 condition_free(condition);
327
328 strshorten(u.release, 4);
329 strcpy(strchr(u.release, 0), "*");
330
331 condition = condition_new(CONDITION_KERNEL_VERSION, u.release, false, false);
332 assert_se(condition);
a0b191b7 333 assert_se(condition_test(condition, environ) > 0);
5022f08a 334 condition_free(condition);
68c58c67
LP
335
336 /* 0.1.2 would be a very very very old kernel */
337 condition = condition_new(CONDITION_KERNEL_VERSION, "> 0.1.2", false, false);
338 assert_se(condition);
a0b191b7 339 assert_se(condition_test(condition, environ) > 0);
68c58c67
LP
340 condition_free(condition);
341
910c6d09
ZJS
342 condition = condition_new(CONDITION_KERNEL_VERSION, ">0.1.2", false, false);
343 assert_se(condition);
a0b191b7 344 assert_se(condition_test(condition, environ) > 0);
910c6d09
ZJS
345 condition_free(condition);
346
347 condition = condition_new(CONDITION_KERNEL_VERSION, "'>0.1.2' '<9.0.0'", false, false);
348 assert_se(condition);
a0b191b7 349 assert_se(condition_test(condition, environ) > 0);
910c6d09
ZJS
350 condition_free(condition);
351
352 condition = condition_new(CONDITION_KERNEL_VERSION, "> 0.1.2 < 9.0.0", false, false);
353 assert_se(condition);
a0b191b7 354 assert_se(condition_test(condition, environ) == -EINVAL);
910c6d09
ZJS
355 condition_free(condition);
356
357 condition = condition_new(CONDITION_KERNEL_VERSION, ">", false, false);
358 assert_se(condition);
a0b191b7 359 assert_se(condition_test(condition, environ) == -EINVAL);
910c6d09
ZJS
360 condition_free(condition);
361
68c58c67
LP
362 condition = condition_new(CONDITION_KERNEL_VERSION, ">= 0.1.2", false, false);
363 assert_se(condition);
a0b191b7 364 assert_se(condition_test(condition, environ) > 0);
68c58c67
LP
365 condition_free(condition);
366
367 condition = condition_new(CONDITION_KERNEL_VERSION, "< 0.1.2", false, false);
368 assert_se(condition);
a0b191b7 369 assert_se(condition_test(condition, environ) == 0);
68c58c67
LP
370 condition_free(condition);
371
372 condition = condition_new(CONDITION_KERNEL_VERSION, "<= 0.1.2", false, false);
373 assert_se(condition);
a0b191b7 374 assert_se(condition_test(condition, environ) == 0);
68c58c67
LP
375 condition_free(condition);
376
377 condition = condition_new(CONDITION_KERNEL_VERSION, "= 0.1.2", false, false);
378 assert_se(condition);
a0b191b7 379 assert_se(condition_test(condition, environ) == 0);
68c58c67
LP
380 condition_free(condition);
381
382 /* 4711.8.15 is a very very very future kernel */
383 condition = condition_new(CONDITION_KERNEL_VERSION, "< 4711.8.15", false, false);
384 assert_se(condition);
a0b191b7 385 assert_se(condition_test(condition, environ) > 0);
68c58c67
LP
386 condition_free(condition);
387
388 condition = condition_new(CONDITION_KERNEL_VERSION, "<= 4711.8.15", false, false);
389 assert_se(condition);
a0b191b7 390 assert_se(condition_test(condition, environ) > 0);
68c58c67
LP
391 condition_free(condition);
392
393 condition = condition_new(CONDITION_KERNEL_VERSION, "= 4711.8.15", false, false);
394 assert_se(condition);
a0b191b7 395 assert_se(condition_test(condition, environ) == 0);
68c58c67
LP
396 condition_free(condition);
397
398 condition = condition_new(CONDITION_KERNEL_VERSION, "> 4711.8.15", false, false);
399 assert_se(condition);
a0b191b7 400 assert_se(condition_test(condition, environ) == 0);
68c58c67
LP
401 condition_free(condition);
402
403 condition = condition_new(CONDITION_KERNEL_VERSION, ">= 4711.8.15", false, false);
404 assert_se(condition);
a0b191b7 405 assert_se(condition_test(condition, environ) == 0);
68c58c67
LP
406 condition_free(condition);
407
408 assert_se(uname(&u) >= 0);
409
410 v = strjoina(">=", u.release);
411 condition = condition_new(CONDITION_KERNEL_VERSION, v, false, false);
412 assert_se(condition);
a0b191b7 413 assert_se(condition_test(condition, environ) > 0);
68c58c67
LP
414 condition_free(condition);
415
416 v = strjoina("= ", u.release);
417 condition = condition_new(CONDITION_KERNEL_VERSION, v, false, false);
418 assert_se(condition);
a0b191b7 419 assert_se(condition_test(condition, environ) > 0);
68c58c67
LP
420 condition_free(condition);
421
422 v = strjoina("<=", u.release);
423 condition = condition_new(CONDITION_KERNEL_VERSION, v, false, false);
424 assert_se(condition);
a0b191b7 425 assert_se(condition_test(condition, environ) > 0);
68c58c67
LP
426 condition_free(condition);
427
428 v = strjoina("> ", u.release);
429 condition = condition_new(CONDITION_KERNEL_VERSION, v, false, false);
430 assert_se(condition);
a0b191b7 431 assert_se(condition_test(condition, environ) == 0);
68c58c67
LP
432 condition_free(condition);
433
434 v = strjoina("< ", u.release);
435 condition = condition_new(CONDITION_KERNEL_VERSION, v, false, false);
436 assert_se(condition);
a0b191b7 437 assert_se(condition_test(condition, environ) == 0);
68c58c67 438 condition_free(condition);
5022f08a
LP
439}
440
015df1f7
RC
441static void test_condition_test_security(void) {
442 Condition *condition;
443
444 condition = condition_new(CONDITION_SECURITY, "garbage oifdsjfoidsjoj", false, false);
4d548a7d 445 assert_se(condition);
a0b191b7 446 assert_se(condition_test(condition, environ) == 0);
015df1f7
RC
447 condition_free(condition);
448
449 condition = condition_new(CONDITION_SECURITY, "selinux", false, true);
4d548a7d 450 assert_se(condition);
a0b191b7 451 assert_se(condition_test(condition, environ) != mac_selinux_use());
015df1f7
RC
452 condition_free(condition);
453
fc65dabd 454 condition = condition_new(CONDITION_SECURITY, "apparmor", false, false);
4d548a7d 455 assert_se(condition);
a0b191b7 456 assert_se(condition_test(condition, environ) == mac_apparmor_use());
015df1f7
RC
457 condition_free(condition);
458
fc65dabd 459 condition = condition_new(CONDITION_SECURITY, "tomoyo", false, false);
4d548a7d 460 assert_se(condition);
a0b191b7 461 assert_se(condition_test(condition, environ) == mac_tomoyo_use());
fc65dabd
ZJS
462 condition_free(condition);
463
464 condition = condition_new(CONDITION_SECURITY, "ima", false, false);
465 assert_se(condition);
a0b191b7 466 assert_se(condition_test(condition, environ) == use_ima());
015df1f7
RC
467 condition_free(condition);
468
469 condition = condition_new(CONDITION_SECURITY, "smack", false, false);
4d548a7d 470 assert_se(condition);
a0b191b7 471 assert_se(condition_test(condition, environ) == mac_smack_use());
015df1f7
RC
472 condition_free(condition);
473
474 condition = condition_new(CONDITION_SECURITY, "audit", false, false);
4d548a7d 475 assert_se(condition);
a0b191b7 476 assert_se(condition_test(condition, environ) == use_audit());
015df1f7 477 condition_free(condition);
fc65dabd
ZJS
478
479 condition = condition_new(CONDITION_SECURITY, "uefi-secureboot", false, false);
480 assert_se(condition);
a0b191b7 481 assert_se(condition_test(condition, environ) == is_efi_secure_boot());
fc65dabd
ZJS
482 condition_free(condition);
483}
484
485static void print_securities(void) {
486 log_info("------ enabled security technologies ------");
487 log_info("SELinux: %s", yes_no(mac_selinux_use()));
488 log_info("AppArmor: %s", yes_no(mac_apparmor_use()));
489 log_info("Tomoyo: %s", yes_no(mac_tomoyo_use()));
490 log_info("IMA: %s", yes_no(use_ima()));
491 log_info("SMACK: %s", yes_no(mac_smack_use()));
492 log_info("Audit: %s", yes_no(use_audit()));
493 log_info("UEFI secure boot: %s", yes_no(is_efi_secure_boot()));
494 log_info("-------------------------------------------");
015df1f7
RC
495}
496
239a5707
ZJS
497static void test_condition_test_virtualization(void) {
498 Condition *condition;
499 const char *virt;
500 int r;
501
502 condition = condition_new(CONDITION_VIRTUALIZATION, "garbage oifdsjfoidsjoj", false, false);
503 assert_se(condition);
a0b191b7 504 r = condition_test(condition, environ);
3c14dc61
TM
505 if (ERRNO_IS_PRIVILEGE(r))
506 return;
239a5707
ZJS
507 log_info("ConditionVirtualization=garbage → %i", r);
508 assert_se(r == 0);
509 condition_free(condition);
510
511 condition = condition_new(CONDITION_VIRTUALIZATION, "container", false, false);
512 assert_se(condition);
a0b191b7 513 r = condition_test(condition, environ);
239a5707
ZJS
514 log_info("ConditionVirtualization=container → %i", r);
515 assert_se(r == !!detect_container());
516 condition_free(condition);
517
518 condition = condition_new(CONDITION_VIRTUALIZATION, "vm", false, false);
519 assert_se(condition);
a0b191b7 520 r = condition_test(condition, environ);
239a5707
ZJS
521 log_info("ConditionVirtualization=vm → %i", r);
522 assert_se(r == (detect_vm() && !detect_container()));
523 condition_free(condition);
524
525 condition = condition_new(CONDITION_VIRTUALIZATION, "private-users", false, false);
526 assert_se(condition);
a0b191b7 527 r = condition_test(condition, environ);
239a5707
ZJS
528 log_info("ConditionVirtualization=private-users → %i", r);
529 assert_se(r == !!running_in_userns());
530 condition_free(condition);
531
532 NULSTR_FOREACH(virt,
533 "kvm\0"
534 "qemu\0"
535 "bochs\0"
536 "xen\0"
537 "uml\0"
538 "vmware\0"
539 "oracle\0"
540 "microsoft\0"
541 "zvm\0"
542 "parallels\0"
543 "bhyve\0"
544 "vm_other\0") {
545
546 condition = condition_new(CONDITION_VIRTUALIZATION, virt, false, false);
547 assert_se(condition);
a0b191b7 548 r = condition_test(condition, environ);
239a5707
ZJS
549 log_info("ConditionVirtualization=%s → %i", virt, r);
550 assert_se(r >= 0);
551 condition_free(condition);
552 }
553}
554
c465a29f
FS
555static void test_condition_test_user(void) {
556 Condition *condition;
557 char* uid;
558 char* username;
559 int r;
560
561 condition = condition_new(CONDITION_USER, "garbage oifdsjfoidsjoj", false, false);
562 assert_se(condition);
a0b191b7 563 r = condition_test(condition, environ);
c465a29f
FS
564 log_info("ConditionUser=garbage → %i", r);
565 assert_se(r == 0);
566 condition_free(condition);
567
568 assert_se(asprintf(&uid, "%"PRIu32, UINT32_C(0xFFFF)) > 0);
569 condition = condition_new(CONDITION_USER, uid, false, false);
570 assert_se(condition);
a0b191b7 571 r = condition_test(condition, environ);
c465a29f
FS
572 log_info("ConditionUser=%s → %i", uid, r);
573 assert_se(r == 0);
574 condition_free(condition);
575 free(uid);
576
577 assert_se(asprintf(&uid, "%u", (unsigned)getuid()) > 0);
578 condition = condition_new(CONDITION_USER, uid, false, false);
579 assert_se(condition);
a0b191b7 580 r = condition_test(condition, environ);
c465a29f
FS
581 log_info("ConditionUser=%s → %i", uid, r);
582 assert_se(r > 0);
583 condition_free(condition);
584 free(uid);
585
586 assert_se(asprintf(&uid, "%u", (unsigned)getuid()+1) > 0);
587 condition = condition_new(CONDITION_USER, uid, false, false);
588 assert_se(condition);
a0b191b7 589 r = condition_test(condition, environ);
c465a29f
FS
590 log_info("ConditionUser=%s → %i", uid, r);
591 assert_se(r == 0);
592 condition_free(condition);
593 free(uid);
594
595 username = getusername_malloc();
596 assert_se(username);
597 condition = condition_new(CONDITION_USER, username, false, false);
598 assert_se(condition);
a0b191b7 599 r = condition_test(condition, environ);
c465a29f
FS
600 log_info("ConditionUser=%s → %i", username, r);
601 assert_se(r > 0);
602 condition_free(condition);
603 free(username);
604
605 username = (char*)(geteuid() == 0 ? NOBODY_USER_NAME : "root");
606 condition = condition_new(CONDITION_USER, username, false, false);
607 assert_se(condition);
a0b191b7 608 r = condition_test(condition, environ);
c465a29f
FS
609 log_info("ConditionUser=%s → %i", username, r);
610 assert_se(r == 0);
611 condition_free(condition);
534bab66
FS
612
613 condition = condition_new(CONDITION_USER, "@system", false, false);
614 assert_se(condition);
a0b191b7 615 r = condition_test(condition, environ);
534bab66 616 log_info("ConditionUser=@system → %i", r);
ece877d4 617 if (uid_is_system(getuid()) || uid_is_system(geteuid()))
534bab66
FS
618 assert_se(r > 0);
619 else
620 assert_se(r == 0);
621 condition_free(condition);
c465a29f
FS
622}
623
624static void test_condition_test_group(void) {
625 Condition *condition;
626 char* gid;
627 char* groupname;
628 gid_t *gids, max_gid;
ecaa5ad8 629 int ngroups_max, ngroups, r, i;
c465a29f
FS
630
631 assert_se(0 < asprintf(&gid, "%u", UINT32_C(0xFFFF)));
632 condition = condition_new(CONDITION_GROUP, gid, false, false);
633 assert_se(condition);
a0b191b7 634 r = condition_test(condition, environ);
c465a29f
FS
635 log_info("ConditionGroup=%s → %i", gid, r);
636 assert_se(r == 0);
637 condition_free(condition);
638 free(gid);
639
640 assert_se(0 < asprintf(&gid, "%u", getgid()));
641 condition = condition_new(CONDITION_GROUP, gid, false, false);
642 assert_se(condition);
a0b191b7 643 r = condition_test(condition, environ);
c465a29f
FS
644 log_info("ConditionGroup=%s → %i", gid, r);
645 assert_se(r > 0);
646 condition_free(condition);
647 free(gid);
648
649 ngroups_max = sysconf(_SC_NGROUPS_MAX);
650 assert(ngroups_max > 0);
651
cf409d15 652 gids = newa(gid_t, ngroups_max);
c465a29f 653
ecaa5ad8
AJ
654 ngroups = getgroups(ngroups_max, gids);
655 assert(ngroups >= 0);
c465a29f
FS
656
657 max_gid = getgid();
ecaa5ad8 658 for (i = 0; i < ngroups; i++) {
c465a29f
FS
659 assert_se(0 < asprintf(&gid, "%u", gids[i]));
660 condition = condition_new(CONDITION_GROUP, gid, false, false);
661 assert_se(condition);
a0b191b7 662 r = condition_test(condition, environ);
c465a29f
FS
663 log_info("ConditionGroup=%s → %i", gid, r);
664 assert_se(r > 0);
665 condition_free(condition);
666 free(gid);
667 max_gid = gids[i] > max_gid ? gids[i] : max_gid;
668
669 groupname = gid_to_name(gids[i]);
670 assert_se(groupname);
671 condition = condition_new(CONDITION_GROUP, groupname, false, false);
672 assert_se(condition);
a0b191b7 673 r = condition_test(condition, environ);
c465a29f
FS
674 log_info("ConditionGroup=%s → %i", groupname, r);
675 assert_se(r > 0);
676 condition_free(condition);
677 free(groupname);
678 max_gid = gids[i] > max_gid ? gids[i] : max_gid;
679 }
680
681 assert_se(0 < asprintf(&gid, "%u", max_gid+1));
682 condition = condition_new(CONDITION_GROUP, gid, false, false);
683 assert_se(condition);
a0b191b7 684 r = condition_test(condition, environ);
c465a29f
FS
685 log_info("ConditionGroup=%s → %i", gid, r);
686 assert_se(r == 0);
687 condition_free(condition);
688 free(gid);
689
98cd752a 690 groupname = (char*)(getegid() == 0 ? NOBODY_GROUP_NAME : "root");
c465a29f
FS
691 condition = condition_new(CONDITION_GROUP, groupname, false, false);
692 assert_se(condition);
a0b191b7 693 r = condition_test(condition, environ);
c465a29f
FS
694 log_info("ConditionGroup=%s → %i", groupname, r);
695 assert_se(r == 0);
696 condition_free(condition);
697}
698
a70984c0
LP
699static void test_condition_test_cpus_one(const char *s, bool result) {
700 Condition *condition;
701 int r;
702
703 log_debug("%s=%s", condition_type_to_string(CONDITION_CPUS), s);
704
705 condition = condition_new(CONDITION_CPUS, s, false, false);
706 assert_se(condition);
707
a0b191b7 708 r = condition_test(condition, environ);
a70984c0
LP
709 assert_se(r >= 0);
710 assert_se(r == result);
711 condition_free(condition);
712}
713
714static void test_condition_test_cpus(void) {
715 _cleanup_free_ char *t = NULL;
716 int cpus;
717
718 cpus = cpus_in_affinity_mask();
719 assert_se(cpus >= 0);
720
721 test_condition_test_cpus_one("> 0", true);
722 test_condition_test_cpus_one(">= 0", true);
723 test_condition_test_cpus_one("!= 0", true);
724 test_condition_test_cpus_one("<= 0", false);
725 test_condition_test_cpus_one("< 0", false);
726 test_condition_test_cpus_one("= 0", false);
727
728 test_condition_test_cpus_one("> 100000", false);
729 test_condition_test_cpus_one("= 100000", false);
730 test_condition_test_cpus_one(">= 100000", false);
731 test_condition_test_cpus_one("< 100000", true);
732 test_condition_test_cpus_one("!= 100000", true);
733 test_condition_test_cpus_one("<= 100000", true);
734
735 assert_se(asprintf(&t, "= %i", cpus) >= 0);
736 test_condition_test_cpus_one(t, true);
737 t = mfree(t);
738
739 assert_se(asprintf(&t, "<= %i", cpus) >= 0);
740 test_condition_test_cpus_one(t, true);
741 t = mfree(t);
742
743 assert_se(asprintf(&t, ">= %i", cpus) >= 0);
744 test_condition_test_cpus_one(t, true);
745 t = mfree(t);
746
747 assert_se(asprintf(&t, "!= %i", cpus) >= 0);
748 test_condition_test_cpus_one(t, false);
749 t = mfree(t);
750
751 assert_se(asprintf(&t, "< %i", cpus) >= 0);
752 test_condition_test_cpus_one(t, false);
753 t = mfree(t);
754
755 assert_se(asprintf(&t, "> %i", cpus) >= 0);
756 test_condition_test_cpus_one(t, false);
757 t = mfree(t);
758}
759
760static void test_condition_test_memory_one(const char *s, bool result) {
761 Condition *condition;
762 int r;
763
764 log_debug("%s=%s", condition_type_to_string(CONDITION_MEMORY), s);
765
766 condition = condition_new(CONDITION_MEMORY, s, false, false);
767 assert_se(condition);
768
a0b191b7 769 r = condition_test(condition, environ);
a70984c0
LP
770 assert_se(r >= 0);
771 assert_se(r == result);
772 condition_free(condition);
773}
774
775static void test_condition_test_memory(void) {
776 _cleanup_free_ char *t = NULL;
777 uint64_t memory;
778
779 memory = physical_memory();
780
781 test_condition_test_memory_one("> 0", true);
782 test_condition_test_memory_one(">= 0", true);
783 test_condition_test_memory_one("!= 0", true);
784 test_condition_test_memory_one("<= 0", false);
785 test_condition_test_memory_one("< 0", false);
786 test_condition_test_memory_one("= 0", false);
787
788 test_condition_test_memory_one("> 18446744073709547520", false);
789 test_condition_test_memory_one("= 18446744073709547520", false);
790 test_condition_test_memory_one(">= 18446744073709547520", false);
791 test_condition_test_memory_one("< 18446744073709547520", true);
792 test_condition_test_memory_one("!= 18446744073709547520", true);
793 test_condition_test_memory_one("<= 18446744073709547520", true);
794
795 assert_se(asprintf(&t, "= %" PRIu64, memory) >= 0);
796 test_condition_test_memory_one(t, true);
797 t = mfree(t);
798
799 assert_se(asprintf(&t, "<= %" PRIu64, memory) >= 0);
800 test_condition_test_memory_one(t, true);
801 t = mfree(t);
802
803 assert_se(asprintf(&t, ">= %" PRIu64, memory) >= 0);
804 test_condition_test_memory_one(t, true);
805 t = mfree(t);
806
807 assert_se(asprintf(&t, "!= %" PRIu64, memory) >= 0);
808 test_condition_test_memory_one(t, false);
809 t = mfree(t);
810
811 assert_se(asprintf(&t, "< %" PRIu64, memory) >= 0);
812 test_condition_test_memory_one(t, false);
813 t = mfree(t);
814
815 assert_se(asprintf(&t, "> %" PRIu64, memory) >= 0);
816 test_condition_test_memory_one(t, false);
817 t = mfree(t);
818}
819
a0b191b7
LP
820static void test_condition_test_environment_one(const char *s, bool result) {
821 Condition *condition;
822 int r;
823
824 log_debug("%s=%s", condition_type_to_string(CONDITION_ENVIRONMENT), s);
825
826 condition = condition_new(CONDITION_ENVIRONMENT, s, false, false);
827 assert_se(condition);
828
829 r = condition_test(condition, environ);
830 assert_se(r >= 0);
831 assert_se(r == result);
832 condition_free(condition);
833}
834
835static void test_condition_test_environment(void) {
836 assert_se(setenv("EXISTINGENVVAR", "foo", false) >= 0);
837
838 test_condition_test_environment_one("MISSINGENVVAR", false);
839 test_condition_test_environment_one("MISSINGENVVAR=foo", false);
840 test_condition_test_environment_one("MISSINGENVVAR=", false);
841
842 test_condition_test_environment_one("EXISTINGENVVAR", true);
843 test_condition_test_environment_one("EXISTINGENVVAR=foo", true);
844 test_condition_test_environment_one("EXISTINGENVVAR=bar", false);
845 test_condition_test_environment_one("EXISTINGENVVAR=", false);
846}
847
b08f2be6 848int main(int argc, char *argv[]) {
6d7c4033 849 test_setup_logging(LOG_DEBUG);
b08f2be6 850
015df1f7 851 test_condition_test_path();
b08f2be6
RC
852 test_condition_test_ac_power();
853 test_condition_test_host();
854 test_condition_test_architecture();
07318c29 855 test_condition_test_kernel_command_line();
5022f08a 856 test_condition_test_kernel_version();
015df1f7 857 test_condition_test_security();
fc65dabd 858 print_securities();
239a5707 859 test_condition_test_virtualization();
c465a29f
FS
860 test_condition_test_user();
861 test_condition_test_group();
e16647c3 862 test_condition_test_control_group_controller();
a70984c0
LP
863 test_condition_test_cpus();
864 test_condition_test_memory();
a0b191b7 865 test_condition_test_environment();
b08f2be6
RC
866
867 return 0;
868}