chiark / gitweb /
Fix service file to match installed elogind binary location
[elogind.git] / src / test / test-selinux.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2016 Zbigniew Jędrzejewski-Szmek
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <sys/stat.h>
21
22 #include "alloc-util.h"
23 #include "fd-util.h"
24 #include "log.h"
25 #include "selinux-util.h"
26 #include "string-util.h"
27 #include "time-util.h"
28 #include "util.h"
29
30 static void test_testing(void) {
31         bool b;
32
33         log_info("============ %s ==========", __func__);
34
35         b = mac_selinux_use();
36         log_info("mac_selinux_use → %s", yes_no(b));
37
38         b = mac_selinux_use();
39         log_info("mac_selinux_use → %s", yes_no(b));
40
41         mac_selinux_retest();
42
43         b = mac_selinux_use();
44         log_info("mac_selinux_use → %s", yes_no(b));
45
46         b = mac_selinux_use();
47         log_info("mac_selinux_use → %s", yes_no(b));
48 }
49
50 static void test_loading(void) {
51         usec_t n1, n2;
52         int r;
53
54         log_info("============ %s ==========", __func__);
55
56         n1 = now(CLOCK_MONOTONIC);
57         r = mac_selinux_init();
58         n2 = now(CLOCK_MONOTONIC);
59         log_info_errno(r, "mac_selinux_init → %d %.2fs (%m)", r, (n2 - n1)/1e6);
60 }
61
62 static void test_cleanup(void) {
63         usec_t n1, n2;
64
65         log_info("============ %s ==========", __func__);
66
67         n1 = now(CLOCK_MONOTONIC);
68         mac_selinux_finish();
69         n2 = now(CLOCK_MONOTONIC);
70         log_info("mac_selinux_finish → %.2fs", (n2 - n1)/1e6);
71 }
72
73 #if 0 /// UNNEEDED by elogind
74 static void test_misc(const char* fname) {
75         _cleanup_(mac_selinux_freep) char *label = NULL, *label2 = NULL, *label3 = NULL;
76         int r;
77         _cleanup_close_ int fd = -1;
78
79         log_info("============ %s ==========", __func__);
80
81         r = mac_selinux_get_our_label(&label);
82         log_info_errno(r, "mac_selinux_get_our_label → %d, \"%s\" (%m)",
83                        r, strnull(label));
84
85         r = mac_selinux_get_create_label_from_exe(fname, &label2);
86         log_info_errno(r, "mac_selinux_create_label_from_exe → %d, \"%s\" (%m)",
87                        r, strnull(label2));
88
89         fd = socket(AF_INET, SOCK_DGRAM, 0);
90         assert_se(fd >= 0);
91
92         r = mac_selinux_get_child_mls_label(fd, fname, label2, &label3);
93         log_info_errno(r, "mac_selinux_get_child_mls_label → %d, \"%s\" (%m)",
94                        r, strnull(label3));
95 }
96 #endif // 0
97
98 static void test_create_file_prepare(const char* fname) {
99         int r;
100
101         log_info("============ %s ==========", __func__);
102
103         r = mac_selinux_create_file_prepare(fname, S_IRWXU);
104         log_info_errno(r, "mac_selinux_create_file_prepare → %d (%m)", r);
105
106         mac_selinux_create_file_clear();
107 }
108
109 int main(int argc, char **argv) {
110         const char *path = SYSTEMD_BINARY_PATH;
111         if (argc >= 2)
112                 path = argv[1];
113
114         log_set_max_level(LOG_DEBUG);
115         log_parse_environment();
116
117         test_testing();
118         test_loading();
119 #if 0 /// UNNEEDED by elogind
120         test_misc(path);
121 #endif // 0
122         test_create_file_prepare(path);
123         test_cleanup();
124
125         return 0;
126 }