chiark / gitweb /
Drop my copyright headers
[elogind.git] / src / test / test-selinux.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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"
9 #include "string-util.h"
10 #include "time-util.h"
11 #include "util.h"
12
13 static void test_testing(void) {
14         bool b;
15
16         log_info("============ %s ==========", __func__);
17
18         b = mac_selinux_use();
19         log_info("mac_selinux_use → %s", yes_no(b));
20
21         b = mac_selinux_use();
22         log_info("mac_selinux_use → %s", yes_no(b));
23
24         mac_selinux_retest();
25
26         b = mac_selinux_use();
27         log_info("mac_selinux_use → %s", yes_no(b));
28
29         b = mac_selinux_use();
30         log_info("mac_selinux_use → %s", yes_no(b));
31 }
32
33 static void test_loading(void) {
34         usec_t n1, n2;
35         int r;
36
37         log_info("============ %s ==========", __func__);
38
39         n1 = now(CLOCK_MONOTONIC);
40         r = mac_selinux_init();
41         n2 = now(CLOCK_MONOTONIC);
42         log_info_errno(r, "mac_selinux_init → %d %.2fs (%m)", r, (n2 - n1)/1e6);
43 }
44
45 static void test_cleanup(void) {
46         usec_t n1, n2;
47
48         log_info("============ %s ==========", __func__);
49
50         n1 = now(CLOCK_MONOTONIC);
51         mac_selinux_finish();
52         n2 = now(CLOCK_MONOTONIC);
53         log_info("mac_selinux_finish → %.2fs", (n2 - n1)/1e6);
54 }
55
56 #if 0 /// UNNEEDED by elogind
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);
65         log_info_errno(r, "mac_selinux_get_our_label → %d, \"%s\" (%m)",
66                        r, strnull(label));
67
68         r = mac_selinux_get_create_label_from_exe(fname, &label2);
69         log_info_errno(r, "mac_selinux_create_label_from_exe → %d, \"%s\" (%m)",
70                        r, strnull(label2));
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);
76         log_info_errno(r, "mac_selinux_get_child_mls_label → %d, \"%s\" (%m)",
77                        r, strnull(label3));
78 }
79 #endif // 0
80
81 static void test_create_file_prepare(const char* fname) {
82         int r;
83
84         log_info("============ %s ==========", __func__);
85
86         r = mac_selinux_create_file_prepare(fname, S_IRWXU);
87         log_info_errno(r, "mac_selinux_create_file_prepare → %d (%m)", r);
88
89         mac_selinux_create_file_clear();
90 }
91
92 int main(int argc, char **argv) {
93         const char *path = SYSTEMD_BINARY_PATH;
94         if (argc >= 2)
95                 path = argv[1];
96
97         log_set_max_level(LOG_DEBUG);
98         log_parse_environment();
99
100         test_testing();
101         test_loading();
102 #if 0 /// UNNEEDED by elogind
103         test_misc(path);
104 #endif // 0
105         test_create_file_prepare(path);
106         test_cleanup();
107
108         return 0;
109 }