Debian Codegraph @

src/test/test-selinux.c

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