]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-selinux.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.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 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <sys/stat.h>
22
23 #include "alloc-util.h"
24 #include "fd-util.h"
25 #include "log.h"
26 #include "selinux-util.h"
27 #include "string-util.h"
28 #include "time-util.h"
29 #include "util.h"
30
31 static void test_testing(void) {
32 bool b;
33
34 log_info("============ %s ==========", __func__);
35
36 b = mac_selinux_use();
37 log_info("mac_selinux_use → %s", yes_no(b));
38
39 b = mac_selinux_use();
40 log_info("mac_selinux_use → %s", yes_no(b));
41
42 mac_selinux_retest();
43
44 b = mac_selinux_use();
45 log_info("mac_selinux_use → %s", yes_no(b));
46
47 b = mac_selinux_use();
48 log_info("mac_selinux_use → %s", yes_no(b));
49 }
50
51 static void test_loading(void) {
52 usec_t n1, n2;
53 int r;
54
55 log_info("============ %s ==========", __func__);
56
57 n1 = now(CLOCK_MONOTONIC);
58 r = mac_selinux_init();
59 n2 = now(CLOCK_MONOTONIC);
60 log_info_errno(r, "mac_selinux_init → %d %.2fs (%m)", r, (n2 - n1)/1e6);
61 }
62
63 static void test_cleanup(void) {
64 usec_t n1, n2;
65
66 log_info("============ %s ==========", __func__);
67
68 n1 = now(CLOCK_MONOTONIC);
69 mac_selinux_finish();
70 n2 = now(CLOCK_MONOTONIC);
71 log_info("mac_selinux_finish → %.2fs", (n2 - n1)/1e6);
72 }
73
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
97 static void test_create_file_prepare(const char* fname) {
98 int r;
99
100 log_info("============ %s ==========", __func__);
101
102 r = mac_selinux_create_file_prepare(fname, S_IRWXU);
103 log_info_errno(r, "mac_selinux_create_file_prepare → %d (%m)", r);
104
105 mac_selinux_create_file_clear();
106 }
107
108 int main(int argc, char **argv) {
109 const char *path = SYSTEMD_BINARY_PATH;
110 if (argc >= 2)
111 path = argv[1];
112
113 log_set_max_level(LOG_DEBUG);
114 log_parse_environment();
115
116 test_testing();
117 test_loading();
118 test_misc(path);
119 test_create_file_prepare(path);
120 test_cleanup();
121
122 return 0;
123 }