]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-selinux.c
Merge pull request #7388 from keszybz/doc-tweak
[thirdparty/systemd.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 static void test_misc(const char* fname) {
74 _cleanup_(mac_selinux_freep) char *label = NULL, *label2 = NULL, *label3 = NULL;
75 int r;
76 _cleanup_close_ int fd = -1;
77
78 log_info("============ %s ==========", __func__);
79
80 r = mac_selinux_get_our_label(&label);
81 log_info_errno(r, "mac_selinux_get_our_label → %d, \"%s\" (%m)",
82 r, strnull(label));
83
84 r = mac_selinux_get_create_label_from_exe(fname, &label2);
85 log_info_errno(r, "mac_selinux_create_label_from_exe → %d, \"%s\" (%m)",
86 r, strnull(label2));
87
88 fd = socket(AF_INET, SOCK_DGRAM, 0);
89 assert_se(fd >= 0);
90
91 r = mac_selinux_get_child_mls_label(fd, fname, label2, &label3);
92 log_info_errno(r, "mac_selinux_get_child_mls_label → %d, \"%s\" (%m)",
93 r, strnull(label3));
94 }
95
96 static void test_create_file_prepare(const char* fname) {
97 int r;
98
99 log_info("============ %s ==========", __func__);
100
101 r = mac_selinux_create_file_prepare(fname, S_IRWXU);
102 log_info_errno(r, "mac_selinux_create_file_prepare → %d (%m)", r);
103
104 mac_selinux_create_file_clear();
105 }
106
107 int main(int argc, char **argv) {
108 const char *path = SYSTEMD_BINARY_PATH;
109 if (argc >= 2)
110 path = argv[1];
111
112 log_set_max_level(LOG_DEBUG);
113 log_parse_environment();
114
115 test_testing();
116 test_loading();
117 test_misc(path);
118 test_create_file_prepare(path);
119 test_cleanup();
120
121 return 0;
122 }