chiark / gitweb /
d320a992592f6887aff9283568051699dd25f515
[elogind.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 #if 0 /// UNNEEDED by elogind
60 static void test_misc(const char* fname) {
61         _cleanup_(mac_selinux_freep) char *label = NULL, *label2 = NULL, *label3 = NULL;
62         int r;
63         _cleanup_close_ int fd = -1;
64
65         log_info("============ %s ==========", __func__);
66
67         r = mac_selinux_get_our_label(&label);
68         log_info_errno(r, "mac_selinux_get_our_label → %d, \"%s\" (%m)",
69                        r, strnull(label));
70
71         r = mac_selinux_get_create_label_from_exe(fname, &label2);
72         log_info_errno(r, "mac_selinux_create_label_from_exe → %d, \"%s\" (%m)",
73                        r, strnull(label2));
74
75         fd = socket(AF_INET, SOCK_DGRAM, 0);
76         assert_se(fd >= 0);
77
78         r = mac_selinux_get_child_mls_label(fd, fname, label2, &label3);
79         log_info_errno(r, "mac_selinux_get_child_mls_label → %d, \"%s\" (%m)",
80                        r, strnull(label3));
81 }
82 #endif // 0
83
84 static void test_create_file_prepare(const char* fname) {
85         int r;
86
87         log_info("============ %s ==========", __func__);
88
89         r = mac_selinux_create_file_prepare(fname, S_IRWXU);
90         log_info_errno(r, "mac_selinux_create_file_prepare → %d (%m)", r);
91
92         mac_selinux_create_file_clear();
93 }
94
95 int main(int argc, char **argv) {
96         const char *path = SYSTEMD_BINARY_PATH;
97         if (argc >= 2)
98                 path = argv[1];
99
100         log_set_max_level(LOG_DEBUG);
101         log_parse_environment();
102
103         test_testing();
104         test_loading();
105 #if 0 /// UNNEEDED by elogind
106         test_misc(path);
107 #endif // 0
108         test_create_file_prepare(path);
109         test_cleanup();
110
111         return 0;
112 }