]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
ada94e69 ZJS |
2 | |
3 | #include <sys/stat.h> | |
4 | ||
5 | #include "alloc-util.h" | |
6 | #include "fd-util.h" | |
7 | #include "log.h" | |
8 | #include "selinux-util.h" | |
b5ec6ada | 9 | #include "string-util.h" |
6d7c4033 | 10 | #include "tests.h" |
ada94e69 | 11 | #include "time-util.h" |
b5ec6ada | 12 | #include "util.h" |
ada94e69 ZJS |
13 | |
14 | static void test_testing(void) { | |
15 | bool b; | |
16 | ||
17 | log_info("============ %s ==========", __func__); | |
18 | ||
19 | b = mac_selinux_use(); | |
b5ec6ada | 20 | log_info("mac_selinux_use → %s", yes_no(b)); |
ada94e69 | 21 | |
6d395665 GT |
22 | b = mac_selinux_use(); |
23 | log_info("mac_selinux_use → %s", yes_no(b)); | |
ada94e69 ZJS |
24 | |
25 | mac_selinux_retest(); | |
26 | ||
27 | b = mac_selinux_use(); | |
b5ec6ada | 28 | log_info("mac_selinux_use → %s", yes_no(b)); |
ada94e69 | 29 | |
6d395665 GT |
30 | b = mac_selinux_use(); |
31 | log_info("mac_selinux_use → %s", yes_no(b)); | |
ada94e69 ZJS |
32 | } |
33 | ||
34 | static void test_loading(void) { | |
35 | usec_t n1, n2; | |
36 | int r; | |
37 | ||
38 | log_info("============ %s ==========", __func__); | |
39 | ||
40 | n1 = now(CLOCK_MONOTONIC); | |
41 | r = mac_selinux_init(); | |
42 | n2 = now(CLOCK_MONOTONIC); | |
08493e73 | 43 | log_info_errno(r, "mac_selinux_init → %d %.2fs (%m)", r, (n2 - n1)/1e6); |
ada94e69 ZJS |
44 | } |
45 | ||
46 | static void test_cleanup(void) { | |
47 | usec_t n1, n2; | |
48 | ||
49 | log_info("============ %s ==========", __func__); | |
50 | ||
51 | n1 = now(CLOCK_MONOTONIC); | |
52 | mac_selinux_finish(); | |
53 | n2 = now(CLOCK_MONOTONIC); | |
54 | log_info("mac_selinux_finish → %.2fs", (n2 - n1)/1e6); | |
55 | } | |
56 | ||
57 | static void test_misc(const char* fname) { | |
58 | _cleanup_(mac_selinux_freep) char *label = NULL, *label2 = NULL, *label3 = NULL; | |
59 | int r; | |
60 | _cleanup_close_ int fd = -1; | |
61 | ||
62 | log_info("============ %s ==========", __func__); | |
63 | ||
64 | r = mac_selinux_get_our_label(&label); | |
08493e73 | 65 | log_info_errno(r, "mac_selinux_get_our_label → %d, \"%s\" (%m)", |
b5ec6ada | 66 | r, strnull(label)); |
ada94e69 ZJS |
67 | |
68 | r = mac_selinux_get_create_label_from_exe(fname, &label2); | |
08493e73 | 69 | log_info_errno(r, "mac_selinux_create_label_from_exe → %d, \"%s\" (%m)", |
b5ec6ada | 70 | r, strnull(label2)); |
ada94e69 ZJS |
71 | |
72 | fd = socket(AF_INET, SOCK_DGRAM, 0); | |
73 | assert_se(fd >= 0); | |
74 | ||
75 | r = mac_selinux_get_child_mls_label(fd, fname, label2, &label3); | |
08493e73 | 76 | log_info_errno(r, "mac_selinux_get_child_mls_label → %d, \"%s\" (%m)", |
b5ec6ada | 77 | r, strnull(label3)); |
ada94e69 ZJS |
78 | } |
79 | ||
80 | static void test_create_file_prepare(const char* fname) { | |
81 | int r; | |
82 | ||
83 | log_info("============ %s ==========", __func__); | |
84 | ||
85 | r = mac_selinux_create_file_prepare(fname, S_IRWXU); | |
86 | log_info_errno(r, "mac_selinux_create_file_prepare → %d (%m)", r); | |
87 | ||
88 | mac_selinux_create_file_clear(); | |
89 | } | |
90 | ||
91 | int main(int argc, char **argv) { | |
92 | const char *path = SYSTEMD_BINARY_PATH; | |
93 | if (argc >= 2) | |
94 | path = argv[1]; | |
95 | ||
6d7c4033 | 96 | test_setup_logging(LOG_DEBUG); |
ada94e69 ZJS |
97 | |
98 | test_testing(); | |
99 | test_loading(); | |
100 | test_misc(path); | |
101 | test_create_file_prepare(path); | |
102 | test_cleanup(); | |
103 | ||
104 | return 0; | |
105 | } |