]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-selinux.c
aae818068ac52c666444722733aa431b9a585f94
[thirdparty/systemd.git] / src / test / test-selinux.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2016 Zbigniew Jędrzejewski-Szmek
4 ***/
5
6 #include <sys/stat.h>
7
8 #include "alloc-util.h"
9 #include "fd-util.h"
10 #include "log.h"
11 #include "selinux-util.h"
12 #include "string-util.h"
13 #include "time-util.h"
14 #include "util.h"
15
16 static void test_testing(void) {
17 bool b;
18
19 log_info("============ %s ==========", __func__);
20
21 b = mac_selinux_use();
22 log_info("mac_selinux_use → %s", yes_no(b));
23
24 b = mac_selinux_use();
25 log_info("mac_selinux_use → %s", yes_no(b));
26
27 mac_selinux_retest();
28
29 b = mac_selinux_use();
30 log_info("mac_selinux_use → %s", yes_no(b));
31
32 b = mac_selinux_use();
33 log_info("mac_selinux_use → %s", yes_no(b));
34 }
35
36 static void test_loading(void) {
37 usec_t n1, n2;
38 int r;
39
40 log_info("============ %s ==========", __func__);
41
42 n1 = now(CLOCK_MONOTONIC);
43 r = mac_selinux_init();
44 n2 = now(CLOCK_MONOTONIC);
45 log_info_errno(r, "mac_selinux_init → %d %.2fs (%m)", r, (n2 - n1)/1e6);
46 }
47
48 static void test_cleanup(void) {
49 usec_t n1, n2;
50
51 log_info("============ %s ==========", __func__);
52
53 n1 = now(CLOCK_MONOTONIC);
54 mac_selinux_finish();
55 n2 = now(CLOCK_MONOTONIC);
56 log_info("mac_selinux_finish → %.2fs", (n2 - n1)/1e6);
57 }
58
59 static void test_misc(const char* fname) {
60 _cleanup_(mac_selinux_freep) char *label = NULL, *label2 = NULL, *label3 = NULL;
61 int r;
62 _cleanup_close_ int fd = -1;
63
64 log_info("============ %s ==========", __func__);
65
66 r = mac_selinux_get_our_label(&label);
67 log_info_errno(r, "mac_selinux_get_our_label → %d, \"%s\" (%m)",
68 r, strnull(label));
69
70 r = mac_selinux_get_create_label_from_exe(fname, &label2);
71 log_info_errno(r, "mac_selinux_create_label_from_exe → %d, \"%s\" (%m)",
72 r, strnull(label2));
73
74 fd = socket(AF_INET, SOCK_DGRAM, 0);
75 assert_se(fd >= 0);
76
77 r = mac_selinux_get_child_mls_label(fd, fname, label2, &label3);
78 log_info_errno(r, "mac_selinux_get_child_mls_label → %d, \"%s\" (%m)",
79 r, strnull(label3));
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 log_set_max_level(LOG_DEBUG);
99 log_parse_environment();
100
101 test_testing();
102 test_loading();
103 test_misc(path);
104 test_create_file_prepare(path);
105 test_cleanup();
106
107 return 0;
108 }