1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include <sys/utsname.h>
10 #include "alloc-util.h"
11 #include "apparmor-util.h"
12 #include "architecture.h"
13 #include "audit-util.h"
14 #include "cgroup-util.h"
15 #include "condition.h"
16 #include "cpu-set-util.h"
17 #include "efi-loader.h"
18 #include "errno-util.h"
19 #include "hostname-util.h"
20 #include "id128-util.h"
22 #include "limits-util.h"
25 #include "nulstr-util.h"
26 #include "process-util.h"
27 #include "selinux-util.h"
29 #include "smack-util.h"
30 #include "string-util.h"
33 #include "tomoyo-util.h"
34 #include "user-record.h"
35 #include "user-util.h"
38 static void test_condition_test_path(void) {
41 condition
= condition_new(CONDITION_PATH_EXISTS
, "/bin/sh", false, false);
43 assert_se(condition_test(condition
, environ
));
44 condition_free(condition
);
46 condition
= condition_new(CONDITION_PATH_EXISTS
, "/bin/s?", false, false);
48 assert_se(condition_test(condition
, environ
) == 0);
49 condition_free(condition
);
51 condition
= condition_new(CONDITION_PATH_EXISTS_GLOB
, "/bin/s?", false, false);
53 assert_se(condition_test(condition
, environ
) > 0);
54 condition_free(condition
);
56 condition
= condition_new(CONDITION_PATH_EXISTS_GLOB
, "/bin/s?", false, true);
58 assert_se(condition_test(condition
, environ
) == 0);
59 condition_free(condition
);
61 condition
= condition_new(CONDITION_PATH_EXISTS
, "/thiscertainlywontexist", false, false);
63 assert_se(condition_test(condition
, environ
) == 0);
64 condition_free(condition
);
66 condition
= condition_new(CONDITION_PATH_EXISTS
, "/thiscertainlywontexist", false, true);
68 assert_se(condition_test(condition
, environ
) > 0);
69 condition_free(condition
);
71 condition
= condition_new(CONDITION_PATH_IS_DIRECTORY
, "/bin", false, false);
73 assert_se(condition_test(condition
, environ
) > 0);
74 condition_free(condition
);
76 condition
= condition_new(CONDITION_DIRECTORY_NOT_EMPTY
, "/bin", false, false);
78 assert_se(condition_test(condition
, environ
) > 0);
79 condition_free(condition
);
81 condition
= condition_new(CONDITION_FILE_NOT_EMPTY
, "/bin/sh", false, false);
83 assert_se(condition_test(condition
, environ
) > 0);
84 condition_free(condition
);
86 condition
= condition_new(CONDITION_FILE_IS_EXECUTABLE
, "/bin/sh", false, false);
88 assert_se(condition_test(condition
, environ
) > 0);
89 condition_free(condition
);
91 condition
= condition_new(CONDITION_FILE_IS_EXECUTABLE
, "/etc/passwd", false, false);
93 assert_se(condition_test(condition
, environ
) == 0);
94 condition_free(condition
);
96 condition
= condition_new(CONDITION_PATH_IS_MOUNT_POINT
, "/proc", false, false);
98 assert_se(condition_test(condition
, environ
) > 0);
99 condition_free(condition
);
101 condition
= condition_new(CONDITION_PATH_IS_MOUNT_POINT
, "/", false, false);
102 assert_se(condition
);
103 assert_se(condition_test(condition
, environ
) > 0);
104 condition_free(condition
);
106 condition
= condition_new(CONDITION_PATH_IS_MOUNT_POINT
, "/bin", false, false);
107 assert_se(condition
);
108 assert_se(condition_test(condition
, environ
) == 0);
109 condition_free(condition
);
111 condition
= condition_new(CONDITION_PATH_IS_READ_WRITE
, "/tmp", false, false);
112 assert_se(condition
);
113 assert_se(condition_test(condition
, environ
) > 0);
114 condition_free(condition
);
116 condition
= condition_new(CONDITION_PATH_IS_ENCRYPTED
, "/sys", false, false);
117 assert_se(condition
);
118 assert_se(condition_test(condition
, environ
) == 0);
119 condition_free(condition
);
121 condition
= condition_new(CONDITION_PATH_IS_SYMBOLIC_LINK
, "/dev/stdout", false, false);
122 assert_se(condition
);
123 assert_se(condition_test(condition
, environ
) > 0);
124 condition_free(condition
);
127 static void test_condition_test_control_group_hierarchy(void) {
128 Condition
*condition
;
132 if (r
== -ENOMEDIUM
) {
133 log_tests_skipped("cgroup not mounted");
138 condition
= condition_new(CONDITION_CONTROL_GROUP_CONTROLLER
, "v1", false, false);
139 assert_se(condition
);
140 assert_se(condition_test(condition
, environ
) == (r
< CGROUP_UNIFIED_ALL
));
141 condition_free(condition
);
143 condition
= condition_new(CONDITION_CONTROL_GROUP_CONTROLLER
, "v2", false, false);
144 assert_se(condition
);
145 assert_se(condition_test(condition
, environ
) == (r
>= CGROUP_UNIFIED_ALL
));
146 condition_free(condition
);
149 static void test_condition_test_control_group_controller(void) {
150 Condition
*condition
;
151 CGroupMask system_mask
;
152 _cleanup_free_
char *controller_name
= NULL
;
156 if (r
== -ENOMEDIUM
) {
157 log_tests_skipped("cgroup not mounted");
162 /* Invalid controllers are ignored */
163 condition
= condition_new(CONDITION_CONTROL_GROUP_CONTROLLER
, "thisisnotarealcontroller", false, false);
164 assert_se(condition
);
165 assert_se(condition_test(condition
, environ
) > 0);
166 condition_free(condition
);
168 condition
= condition_new(CONDITION_CONTROL_GROUP_CONTROLLER
, "thisisnotarealcontroller", false, true);
169 assert_se(condition
);
170 assert_se(condition_test(condition
, environ
) == 0);
171 condition_free(condition
);
173 assert_se(cg_mask_supported(&system_mask
) >= 0);
175 /* Individual valid controllers one by one */
176 for (CGroupController controller
= 0; controller
< _CGROUP_CONTROLLER_MAX
; controller
++) {
177 const char *local_controller_name
= cgroup_controller_to_string(controller
);
178 log_info("chosen controller is '%s'", local_controller_name
);
179 if (system_mask
& CGROUP_CONTROLLER_TO_MASK(controller
)) {
180 log_info("this controller is available");
181 condition
= condition_new(CONDITION_CONTROL_GROUP_CONTROLLER
, local_controller_name
, false, false);
182 assert_se(condition
);
183 assert_se(condition_test(condition
, environ
) > 0);
184 condition_free(condition
);
186 condition
= condition_new(CONDITION_CONTROL_GROUP_CONTROLLER
, local_controller_name
, false, true);
187 assert_se(condition
);
188 assert_se(condition_test(condition
, environ
) == 0);
189 condition_free(condition
);
191 log_info("this controller is unavailable");
192 condition
= condition_new(CONDITION_CONTROL_GROUP_CONTROLLER
, local_controller_name
, false, false);
193 assert_se(condition
);
194 assert_se(condition_test(condition
, environ
) == 0);
195 condition_free(condition
);
197 condition
= condition_new(CONDITION_CONTROL_GROUP_CONTROLLER
, local_controller_name
, false, true);
198 assert_se(condition
);
199 assert_se(condition_test(condition
, environ
) > 0);
200 condition_free(condition
);
204 /* Multiple valid controllers at the same time */
205 assert_se(cg_mask_to_string(system_mask
, &controller_name
) >= 0);
207 condition
= condition_new(CONDITION_CONTROL_GROUP_CONTROLLER
, strempty(controller_name
), false, false);
208 assert_se(condition
);
209 assert_se(condition_test(condition
, environ
) > 0);
210 condition_free(condition
);
212 condition
= condition_new(CONDITION_CONTROL_GROUP_CONTROLLER
, strempty(controller_name
), false, true);
213 assert_se(condition
);
214 assert_se(condition_test(condition
, environ
) == 0);
215 condition_free(condition
);
218 static void test_condition_test_ac_power(void) {
219 Condition
*condition
;
221 condition
= condition_new(CONDITION_AC_POWER
, "true", false, false);
222 assert_se(condition
);
223 assert_se(condition_test(condition
, environ
) == on_ac_power());
224 condition_free(condition
);
226 condition
= condition_new(CONDITION_AC_POWER
, "false", false, false);
227 assert_se(condition
);
228 assert_se(condition_test(condition
, environ
) != on_ac_power());
229 condition_free(condition
);
231 condition
= condition_new(CONDITION_AC_POWER
, "false", false, true);
232 assert_se(condition
);
233 assert_se(condition_test(condition
, environ
) == on_ac_power());
234 condition_free(condition
);
237 static void test_condition_test_host(void) {
238 _cleanup_free_
char *hostname
= NULL
;
239 char sid
[SD_ID128_STRING_MAX
];
240 Condition
*condition
;
244 r
= sd_id128_get_machine(&id
);
246 assert_se(sd_id128_to_string(id
, sid
));
248 condition
= condition_new(CONDITION_HOST
, sid
, false, false);
249 assert_se(condition
);
250 assert_se(condition_test(condition
, environ
) > 0);
251 condition_free(condition
);
253 condition
= condition_new(CONDITION_HOST
, "garbage value jjjjjjjjjjjjjj", false, false);
254 assert_se(condition
);
255 assert_se(condition_test(condition
, environ
) == 0);
256 condition_free(condition
);
258 condition
= condition_new(CONDITION_HOST
, sid
, false, true);
259 assert_se(condition
);
260 assert_se(condition_test(condition
, environ
) == 0);
261 condition_free(condition
);
263 hostname
= gethostname_malloc();
266 /* if hostname looks like an id128 then skip testing it */
267 if (id128_is_valid(hostname
))
268 log_notice("hostname is an id128, skipping test");
270 condition
= condition_new(CONDITION_HOST
, hostname
, false, false);
271 assert_se(condition
);
272 assert_se(condition_test(condition
, environ
) > 0);
273 condition_free(condition
);
277 static void test_condition_test_architecture(void) {
278 Condition
*condition
;
282 a
= uname_architecture();
285 sa
= architecture_to_string(a
);
288 condition
= condition_new(CONDITION_ARCHITECTURE
, sa
, false, false);
289 assert_se(condition
);
290 assert_se(condition_test(condition
, environ
) > 0);
291 condition_free(condition
);
293 condition
= condition_new(CONDITION_ARCHITECTURE
, "garbage value", false, false);
294 assert_se(condition
);
295 assert_se(condition_test(condition
, environ
) == 0);
296 condition_free(condition
);
298 condition
= condition_new(CONDITION_ARCHITECTURE
, sa
, false, true);
299 assert_se(condition
);
300 assert_se(condition_test(condition
, environ
) == 0);
301 condition_free(condition
);
304 static void test_condition_test_kernel_command_line(void) {
305 Condition
*condition
;
308 condition
= condition_new(CONDITION_KERNEL_COMMAND_LINE
, "thisreallyshouldntbeonthekernelcommandline", false, false);
309 assert_se(condition
);
310 r
= condition_test(condition
, environ
);
311 if (ERRNO_IS_PRIVILEGE(r
))
314 condition_free(condition
);
316 condition
= condition_new(CONDITION_KERNEL_COMMAND_LINE
, "andthis=neither", false, false);
317 assert_se(condition
);
318 assert_se(condition_test(condition
, environ
) == 0);
319 condition_free(condition
);
322 static void test_condition_test_kernel_version(void) {
323 Condition
*condition
;
327 condition
= condition_new(CONDITION_KERNEL_VERSION
, "*thisreallyshouldntbeinthekernelversion*", false, false);
328 assert_se(condition
);
329 assert_se(condition_test(condition
, environ
) == 0);
330 condition_free(condition
);
332 condition
= condition_new(CONDITION_KERNEL_VERSION
, "*", false, false);
333 assert_se(condition
);
334 assert_se(condition_test(condition
, environ
) > 0);
335 condition_free(condition
);
337 /* An artificially empty condition. It evaluates to true, but normally
338 * such condition cannot be created, because the condition list is reset instead. */
339 condition
= condition_new(CONDITION_KERNEL_VERSION
, "", false, false);
340 assert_se(condition
);
341 assert_se(condition_test(condition
, environ
) > 0);
342 condition_free(condition
);
344 assert_se(uname(&u
) >= 0);
346 condition
= condition_new(CONDITION_KERNEL_VERSION
, u
.release
, false, false);
347 assert_se(condition
);
348 assert_se(condition_test(condition
, environ
) > 0);
349 condition_free(condition
);
351 strshorten(u
.release
, 4);
352 strcpy(strchr(u
.release
, 0), "*");
354 condition
= condition_new(CONDITION_KERNEL_VERSION
, u
.release
, false, false);
355 assert_se(condition
);
356 assert_se(condition_test(condition
, environ
) > 0);
357 condition_free(condition
);
359 /* 0.1.2 would be a very very very old kernel */
360 condition
= condition_new(CONDITION_KERNEL_VERSION
, "> 0.1.2", false, false);
361 assert_se(condition
);
362 assert_se(condition_test(condition
, environ
) > 0);
363 condition_free(condition
);
365 condition
= condition_new(CONDITION_KERNEL_VERSION
, ">0.1.2", false, false);
366 assert_se(condition
);
367 assert_se(condition_test(condition
, environ
) > 0);
368 condition_free(condition
);
370 condition
= condition_new(CONDITION_KERNEL_VERSION
, "'>0.1.2' '<9.0.0'", false, false);
371 assert_se(condition
);
372 assert_se(condition_test(condition
, environ
) > 0);
373 condition_free(condition
);
375 condition
= condition_new(CONDITION_KERNEL_VERSION
, "> 0.1.2 < 9.0.0", false, false);
376 assert_se(condition
);
377 assert_se(condition_test(condition
, environ
) == -EINVAL
);
378 condition_free(condition
);
380 condition
= condition_new(CONDITION_KERNEL_VERSION
, ">", false, false);
381 assert_se(condition
);
382 assert_se(condition_test(condition
, environ
) == -EINVAL
);
383 condition_free(condition
);
385 condition
= condition_new(CONDITION_KERNEL_VERSION
, ">= 0.1.2", false, false);
386 assert_se(condition
);
387 assert_se(condition_test(condition
, environ
) > 0);
388 condition_free(condition
);
390 condition
= condition_new(CONDITION_KERNEL_VERSION
, "< 0.1.2", false, false);
391 assert_se(condition
);
392 assert_se(condition_test(condition
, environ
) == 0);
393 condition_free(condition
);
395 condition
= condition_new(CONDITION_KERNEL_VERSION
, "<= 0.1.2", false, false);
396 assert_se(condition
);
397 assert_se(condition_test(condition
, environ
) == 0);
398 condition_free(condition
);
400 condition
= condition_new(CONDITION_KERNEL_VERSION
, "= 0.1.2", false, false);
401 assert_se(condition
);
402 assert_se(condition_test(condition
, environ
) == 0);
403 condition_free(condition
);
405 /* 4711.8.15 is a very very very future kernel */
406 condition
= condition_new(CONDITION_KERNEL_VERSION
, "< 4711.8.15", false, false);
407 assert_se(condition
);
408 assert_se(condition_test(condition
, environ
) > 0);
409 condition_free(condition
);
411 condition
= condition_new(CONDITION_KERNEL_VERSION
, "<= 4711.8.15", false, false);
412 assert_se(condition
);
413 assert_se(condition_test(condition
, environ
) > 0);
414 condition_free(condition
);
416 condition
= condition_new(CONDITION_KERNEL_VERSION
, "= 4711.8.15", false, false);
417 assert_se(condition
);
418 assert_se(condition_test(condition
, environ
) == 0);
419 condition_free(condition
);
421 condition
= condition_new(CONDITION_KERNEL_VERSION
, "> 4711.8.15", false, false);
422 assert_se(condition
);
423 assert_se(condition_test(condition
, environ
) == 0);
424 condition_free(condition
);
426 condition
= condition_new(CONDITION_KERNEL_VERSION
, ">= 4711.8.15", false, false);
427 assert_se(condition
);
428 assert_se(condition_test(condition
, environ
) == 0);
429 condition_free(condition
);
431 assert_se(uname(&u
) >= 0);
433 v
= strjoina(">=", u
.release
);
434 condition
= condition_new(CONDITION_KERNEL_VERSION
, v
, false, false);
435 assert_se(condition
);
436 assert_se(condition_test(condition
, environ
) > 0);
437 condition_free(condition
);
439 v
= strjoina("= ", u
.release
);
440 condition
= condition_new(CONDITION_KERNEL_VERSION
, v
, false, false);
441 assert_se(condition
);
442 assert_se(condition_test(condition
, environ
) > 0);
443 condition_free(condition
);
445 v
= strjoina("<=", u
.release
);
446 condition
= condition_new(CONDITION_KERNEL_VERSION
, v
, false, false);
447 assert_se(condition
);
448 assert_se(condition_test(condition
, environ
) > 0);
449 condition_free(condition
);
451 v
= strjoina("> ", u
.release
);
452 condition
= condition_new(CONDITION_KERNEL_VERSION
, v
, false, false);
453 assert_se(condition
);
454 assert_se(condition_test(condition
, environ
) == 0);
455 condition_free(condition
);
457 v
= strjoina("< ", u
.release
);
458 condition
= condition_new(CONDITION_KERNEL_VERSION
, v
, false, false);
459 assert_se(condition
);
460 assert_se(condition_test(condition
, environ
) == 0);
461 condition_free(condition
);
464 #if defined(__i386__) || defined(__x86_64__)
465 static void test_condition_test_cpufeature(void) {
466 Condition
*condition
;
468 condition
= condition_new(CONDITION_CPU_FEATURE
, "fpu", false, false);
469 assert_se(condition
);
470 assert_se(condition_test(condition
, environ
) > 0);
471 condition_free(condition
);
473 condition
= condition_new(CONDITION_CPU_FEATURE
, "somecpufeaturethatreallydoesntmakesense", false, false);
474 assert_se(condition
);
475 assert_se(condition_test(condition
, environ
) == 0);
476 condition_free(condition
);
478 condition
= condition_new(CONDITION_CPU_FEATURE
, "a", false, false);
479 assert_se(condition
);
480 assert_se(condition_test(condition
, environ
) == 0);
481 condition_free(condition
);
485 static void test_condition_test_security(void) {
486 Condition
*condition
;
488 condition
= condition_new(CONDITION_SECURITY
, "garbage oifdsjfoidsjoj", false, false);
489 assert_se(condition
);
490 assert_se(condition_test(condition
, environ
) == 0);
491 condition_free(condition
);
493 condition
= condition_new(CONDITION_SECURITY
, "selinux", false, true);
494 assert_se(condition
);
495 assert_se(condition_test(condition
, environ
) != mac_selinux_use());
496 condition_free(condition
);
498 condition
= condition_new(CONDITION_SECURITY
, "apparmor", false, false);
499 assert_se(condition
);
500 assert_se(condition_test(condition
, environ
) == mac_apparmor_use());
501 condition_free(condition
);
503 condition
= condition_new(CONDITION_SECURITY
, "tomoyo", false, false);
504 assert_se(condition
);
505 assert_se(condition_test(condition
, environ
) == mac_tomoyo_use());
506 condition_free(condition
);
508 condition
= condition_new(CONDITION_SECURITY
, "ima", false, false);
509 assert_se(condition
);
510 assert_se(condition_test(condition
, environ
) == use_ima());
511 condition_free(condition
);
513 condition
= condition_new(CONDITION_SECURITY
, "smack", false, false);
514 assert_se(condition
);
515 assert_se(condition_test(condition
, environ
) == mac_smack_use());
516 condition_free(condition
);
518 condition
= condition_new(CONDITION_SECURITY
, "audit", false, false);
519 assert_se(condition
);
520 assert_se(condition_test(condition
, environ
) == use_audit());
521 condition_free(condition
);
523 condition
= condition_new(CONDITION_SECURITY
, "uefi-secureboot", false, false);
524 assert_se(condition
);
525 assert_se(condition_test(condition
, environ
) == is_efi_secure_boot());
526 condition_free(condition
);
529 static void print_securities(void) {
530 log_info("------ enabled security technologies ------");
531 log_info("SELinux: %s", yes_no(mac_selinux_use()));
532 log_info("AppArmor: %s", yes_no(mac_apparmor_use()));
533 log_info("Tomoyo: %s", yes_no(mac_tomoyo_use()));
534 log_info("IMA: %s", yes_no(use_ima()));
535 log_info("SMACK: %s", yes_no(mac_smack_use()));
536 log_info("Audit: %s", yes_no(use_audit()));
537 log_info("UEFI secure boot: %s", yes_no(is_efi_secure_boot()));
538 log_info("-------------------------------------------");
541 static void test_condition_test_virtualization(void) {
542 Condition
*condition
;
546 condition
= condition_new(CONDITION_VIRTUALIZATION
, "garbage oifdsjfoidsjoj", false, false);
547 assert_se(condition
);
548 r
= condition_test(condition
, environ
);
549 if (ERRNO_IS_PRIVILEGE(r
))
551 log_info("ConditionVirtualization=garbage → %i", r
);
553 condition_free(condition
);
555 condition
= condition_new(CONDITION_VIRTUALIZATION
, "container", false, false);
556 assert_se(condition
);
557 r
= condition_test(condition
, environ
);
558 log_info("ConditionVirtualization=container → %i", r
);
559 assert_se(r
== !!detect_container());
560 condition_free(condition
);
562 condition
= condition_new(CONDITION_VIRTUALIZATION
, "vm", false, false);
563 assert_se(condition
);
564 r
= condition_test(condition
, environ
);
565 log_info("ConditionVirtualization=vm → %i", r
);
566 assert_se(r
== (detect_vm() && !detect_container()));
567 condition_free(condition
);
569 condition
= condition_new(CONDITION_VIRTUALIZATION
, "private-users", false, false);
570 assert_se(condition
);
571 r
= condition_test(condition
, environ
);
572 log_info("ConditionVirtualization=private-users → %i", r
);
573 assert_se(r
== !!running_in_userns());
574 condition_free(condition
);
591 condition
= condition_new(CONDITION_VIRTUALIZATION
, virt
, false, false);
592 assert_se(condition
);
593 r
= condition_test(condition
, environ
);
594 log_info("ConditionVirtualization=%s → %i", virt
, r
);
596 condition_free(condition
);
600 static void test_condition_test_user(void) {
601 Condition
*condition
;
606 condition
= condition_new(CONDITION_USER
, "garbage oifdsjfoidsjoj", false, false);
607 assert_se(condition
);
608 r
= condition_test(condition
, environ
);
609 log_info("ConditionUser=garbage → %i", r
);
611 condition_free(condition
);
613 assert_se(asprintf(&uid
, "%"PRIu32
, UINT32_C(0xFFFF)) > 0);
614 condition
= condition_new(CONDITION_USER
, uid
, false, false);
615 assert_se(condition
);
616 r
= condition_test(condition
, environ
);
617 log_info("ConditionUser=%s → %i", uid
, r
);
619 condition_free(condition
);
622 assert_se(asprintf(&uid
, "%u", (unsigned)getuid()) > 0);
623 condition
= condition_new(CONDITION_USER
, uid
, false, false);
624 assert_se(condition
);
625 r
= condition_test(condition
, environ
);
626 log_info("ConditionUser=%s → %i", uid
, r
);
628 condition_free(condition
);
631 assert_se(asprintf(&uid
, "%u", (unsigned)getuid()+1) > 0);
632 condition
= condition_new(CONDITION_USER
, uid
, false, false);
633 assert_se(condition
);
634 r
= condition_test(condition
, environ
);
635 log_info("ConditionUser=%s → %i", uid
, r
);
637 condition_free(condition
);
640 username
= getusername_malloc();
642 condition
= condition_new(CONDITION_USER
, username
, false, false);
643 assert_se(condition
);
644 r
= condition_test(condition
, environ
);
645 log_info("ConditionUser=%s → %i", username
, r
);
647 condition_free(condition
);
650 username
= (char*)(geteuid() == 0 ? NOBODY_USER_NAME
: "root");
651 condition
= condition_new(CONDITION_USER
, username
, false, false);
652 assert_se(condition
);
653 r
= condition_test(condition
, environ
);
654 log_info("ConditionUser=%s → %i", username
, r
);
656 condition_free(condition
);
658 condition
= condition_new(CONDITION_USER
, "@system", false, false);
659 assert_se(condition
);
660 r
= condition_test(condition
, environ
);
661 log_info("ConditionUser=@system → %i", r
);
662 if (uid_is_system(getuid()) || uid_is_system(geteuid()))
666 condition_free(condition
);
669 static void test_condition_test_group(void) {
670 Condition
*condition
;
673 gid_t
*gids
, max_gid
;
674 int ngroups_max
, ngroups
, r
, i
;
676 assert_se(0 < asprintf(&gid
, "%u", UINT32_C(0xFFFF)));
677 condition
= condition_new(CONDITION_GROUP
, gid
, false, false);
678 assert_se(condition
);
679 r
= condition_test(condition
, environ
);
680 log_info("ConditionGroup=%s → %i", gid
, r
);
682 condition_free(condition
);
685 assert_se(0 < asprintf(&gid
, "%u", getgid()));
686 condition
= condition_new(CONDITION_GROUP
, gid
, false, false);
687 assert_se(condition
);
688 r
= condition_test(condition
, environ
);
689 log_info("ConditionGroup=%s → %i", gid
, r
);
691 condition_free(condition
);
694 ngroups_max
= sysconf(_SC_NGROUPS_MAX
);
695 assert(ngroups_max
> 0);
697 gids
= newa(gid_t
, ngroups_max
);
699 ngroups
= getgroups(ngroups_max
, gids
);
700 assert(ngroups
>= 0);
703 for (i
= 0; i
< ngroups
; i
++) {
704 assert_se(0 < asprintf(&gid
, "%u", gids
[i
]));
705 condition
= condition_new(CONDITION_GROUP
, gid
, false, false);
706 assert_se(condition
);
707 r
= condition_test(condition
, environ
);
708 log_info("ConditionGroup=%s → %i", gid
, r
);
710 condition_free(condition
);
712 max_gid
= gids
[i
] > max_gid
? gids
[i
] : max_gid
;
714 groupname
= gid_to_name(gids
[i
]);
715 assert_se(groupname
);
716 condition
= condition_new(CONDITION_GROUP
, groupname
, false, false);
717 assert_se(condition
);
718 r
= condition_test(condition
, environ
);
719 log_info("ConditionGroup=%s → %i", groupname
, r
);
721 condition_free(condition
);
723 max_gid
= gids
[i
] > max_gid
? gids
[i
] : max_gid
;
726 assert_se(0 < asprintf(&gid
, "%u", max_gid
+1));
727 condition
= condition_new(CONDITION_GROUP
, gid
, false, false);
728 assert_se(condition
);
729 r
= condition_test(condition
, environ
);
730 log_info("ConditionGroup=%s → %i", gid
, r
);
732 condition_free(condition
);
735 groupname
= (char*)(getegid() == 0 ? NOBODY_GROUP_NAME
: "root");
736 condition
= condition_new(CONDITION_GROUP
, groupname
, false, false);
737 assert_se(condition
);
738 r
= condition_test(condition
, environ
);
739 log_info("ConditionGroup=%s → %i", groupname
, r
);
741 condition_free(condition
);
744 static void test_condition_test_cpus_one(const char *s
, bool result
) {
745 Condition
*condition
;
748 log_debug("%s=%s", condition_type_to_string(CONDITION_CPUS
), s
);
750 condition
= condition_new(CONDITION_CPUS
, s
, false, false);
751 assert_se(condition
);
753 r
= condition_test(condition
, environ
);
755 assert_se(r
== result
);
756 condition_free(condition
);
759 static void test_condition_test_cpus(void) {
760 _cleanup_free_
char *t
= NULL
;
763 cpus
= cpus_in_affinity_mask();
764 assert_se(cpus
>= 0);
766 test_condition_test_cpus_one("> 0", true);
767 test_condition_test_cpus_one(">= 0", true);
768 test_condition_test_cpus_one("!= 0", true);
769 test_condition_test_cpus_one("<= 0", false);
770 test_condition_test_cpus_one("< 0", false);
771 test_condition_test_cpus_one("= 0", false);
773 test_condition_test_cpus_one("> 100000", false);
774 test_condition_test_cpus_one("= 100000", false);
775 test_condition_test_cpus_one(">= 100000", false);
776 test_condition_test_cpus_one("< 100000", true);
777 test_condition_test_cpus_one("!= 100000", true);
778 test_condition_test_cpus_one("<= 100000", true);
780 assert_se(asprintf(&t
, "= %i", cpus
) >= 0);
781 test_condition_test_cpus_one(t
, true);
784 assert_se(asprintf(&t
, "<= %i", cpus
) >= 0);
785 test_condition_test_cpus_one(t
, true);
788 assert_se(asprintf(&t
, ">= %i", cpus
) >= 0);
789 test_condition_test_cpus_one(t
, true);
792 assert_se(asprintf(&t
, "!= %i", cpus
) >= 0);
793 test_condition_test_cpus_one(t
, false);
796 assert_se(asprintf(&t
, "< %i", cpus
) >= 0);
797 test_condition_test_cpus_one(t
, false);
800 assert_se(asprintf(&t
, "> %i", cpus
) >= 0);
801 test_condition_test_cpus_one(t
, false);
805 static void test_condition_test_memory_one(const char *s
, bool result
) {
806 Condition
*condition
;
809 log_debug("%s=%s", condition_type_to_string(CONDITION_MEMORY
), s
);
811 condition
= condition_new(CONDITION_MEMORY
, s
, false, false);
812 assert_se(condition
);
814 r
= condition_test(condition
, environ
);
816 assert_se(r
== result
);
817 condition_free(condition
);
820 static void test_condition_test_memory(void) {
821 _cleanup_free_
char *t
= NULL
;
824 memory
= physical_memory();
826 test_condition_test_memory_one("> 0", true);
827 test_condition_test_memory_one(">= 0", true);
828 test_condition_test_memory_one("!= 0", true);
829 test_condition_test_memory_one("<= 0", false);
830 test_condition_test_memory_one("< 0", false);
831 test_condition_test_memory_one("= 0", false);
833 test_condition_test_memory_one("> 18446744073709547520", false);
834 test_condition_test_memory_one("= 18446744073709547520", false);
835 test_condition_test_memory_one(">= 18446744073709547520", false);
836 test_condition_test_memory_one("< 18446744073709547520", true);
837 test_condition_test_memory_one("!= 18446744073709547520", true);
838 test_condition_test_memory_one("<= 18446744073709547520", true);
840 assert_se(asprintf(&t
, "= %" PRIu64
, memory
) >= 0);
841 test_condition_test_memory_one(t
, true);
844 assert_se(asprintf(&t
, "<= %" PRIu64
, memory
) >= 0);
845 test_condition_test_memory_one(t
, true);
848 assert_se(asprintf(&t
, ">= %" PRIu64
, memory
) >= 0);
849 test_condition_test_memory_one(t
, true);
852 assert_se(asprintf(&t
, "!= %" PRIu64
, memory
) >= 0);
853 test_condition_test_memory_one(t
, false);
856 assert_se(asprintf(&t
, "< %" PRIu64
, memory
) >= 0);
857 test_condition_test_memory_one(t
, false);
860 assert_se(asprintf(&t
, "> %" PRIu64
, memory
) >= 0);
861 test_condition_test_memory_one(t
, false);
865 static void test_condition_test_environment_one(const char *s
, bool result
) {
866 Condition
*condition
;
869 log_debug("%s=%s", condition_type_to_string(CONDITION_ENVIRONMENT
), s
);
871 condition
= condition_new(CONDITION_ENVIRONMENT
, s
, false, false);
872 assert_se(condition
);
874 r
= condition_test(condition
, environ
);
876 assert_se(r
== result
);
877 condition_free(condition
);
880 static void test_condition_test_environment(void) {
881 assert_se(setenv("EXISTINGENVVAR", "foo", false) >= 0);
883 test_condition_test_environment_one("MISSINGENVVAR", false);
884 test_condition_test_environment_one("MISSINGENVVAR=foo", false);
885 test_condition_test_environment_one("MISSINGENVVAR=", false);
887 test_condition_test_environment_one("EXISTINGENVVAR", true);
888 test_condition_test_environment_one("EXISTINGENVVAR=foo", true);
889 test_condition_test_environment_one("EXISTINGENVVAR=bar", false);
890 test_condition_test_environment_one("EXISTINGENVVAR=", false);
893 int main(int argc
, char *argv
[]) {
894 test_setup_logging(LOG_DEBUG
);
896 test_condition_test_path();
897 test_condition_test_ac_power();
898 test_condition_test_host();
899 test_condition_test_architecture();
900 test_condition_test_kernel_command_line();
901 test_condition_test_kernel_version();
902 test_condition_test_security();
904 test_condition_test_virtualization();
905 test_condition_test_user();
906 test_condition_test_group();
907 test_condition_test_control_group_hierarchy();
908 test_condition_test_control_group_controller();
909 test_condition_test_cpus();
910 test_condition_test_memory();
911 test_condition_test_environment();
912 #if defined(__i386__) || defined(__x86_64__)
913 test_condition_test_cpufeature();