chiark / gitweb /
tree-wide: drop license boilerplate
[elogind.git] / src / test / test-selinux.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   This file is part of systemd.
4
5   Copyright 2016 Zbigniew Jędrzejewski-Szmek
6 ***/
7
8 #include <sys/stat.h>
9
10 #include "alloc-util.h"
11 #include "fd-util.h"
12 #include "log.h"
13 #include "selinux-util.h"
14 #include "string-util.h"
15 #include "time-util.h"
16 #include "util.h"
17
18 static void test_testing(void) {
19         bool b;
20
21         log_info("============ %s ==========", __func__);
22
23         b = mac_selinux_use();
24         log_info("mac_selinux_use → %s", yes_no(b));
25
26         b = mac_selinux_use();
27         log_info("mac_selinux_use → %s", yes_no(b));
28
29         mac_selinux_retest();
30
31         b = mac_selinux_use();
32         log_info("mac_selinux_use → %s", yes_no(b));
33
34         b = mac_selinux_use();
35         log_info("mac_selinux_use → %s", yes_no(b));
36 }
37
38 static void test_loading(void) {
39         usec_t n1, n2;
40         int r;
41
42         log_info("============ %s ==========", __func__);
43
44         n1 = now(CLOCK_MONOTONIC);
45         r = mac_selinux_init();
46         n2 = now(CLOCK_MONOTONIC);
47         log_info_errno(r, "mac_selinux_init → %d %.2fs (%m)", r, (n2 - n1)/1e6);
48 }
49
50 static void test_cleanup(void) {
51         usec_t n1, n2;
52
53         log_info("============ %s ==========", __func__);
54
55         n1 = now(CLOCK_MONOTONIC);
56         mac_selinux_finish();
57         n2 = now(CLOCK_MONOTONIC);
58         log_info("mac_selinux_finish → %.2fs", (n2 - n1)/1e6);
59 }
60
61 #if 0 /// UNNEEDED by elogind
62 static void test_misc(const char* fname) {
63         _cleanup_(mac_selinux_freep) char *label = NULL, *label2 = NULL, *label3 = NULL;
64         int r;
65         _cleanup_close_ int fd = -1;
66
67         log_info("============ %s ==========", __func__);
68
69         r = mac_selinux_get_our_label(&label);
70         log_info_errno(r, "mac_selinux_get_our_label → %d, \"%s\" (%m)",
71                        r, strnull(label));
72
73         r = mac_selinux_get_create_label_from_exe(fname, &label2);
74         log_info_errno(r, "mac_selinux_create_label_from_exe → %d, \"%s\" (%m)",
75                        r, strnull(label2));
76
77         fd = socket(AF_INET, SOCK_DGRAM, 0);
78         assert_se(fd >= 0);
79
80         r = mac_selinux_get_child_mls_label(fd, fname, label2, &label3);
81         log_info_errno(r, "mac_selinux_get_child_mls_label → %d, \"%s\" (%m)",
82                        r, strnull(label3));
83 }
84 #endif // 0
85
86 static void test_create_file_prepare(const char* fname) {
87         int r;
88
89         log_info("============ %s ==========", __func__);
90
91         r = mac_selinux_create_file_prepare(fname, S_IRWXU);
92         log_info_errno(r, "mac_selinux_create_file_prepare → %d (%m)", r);
93
94         mac_selinux_create_file_clear();
95 }
96
97 int main(int argc, char **argv) {
98         const char *path = SYSTEMD_BINARY_PATH;
99         if (argc >= 2)
100                 path = argv[1];
101
102         log_set_max_level(LOG_DEBUG);
103         log_parse_environment();
104
105         test_testing();
106         test_loading();
107 #if 0 /// UNNEEDED by elogind
108         test_misc(path);
109 #endif // 0
110         test_create_file_prepare(path);
111         test_cleanup();
112
113         return 0;
114 }