]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-selinux.c
Merge pull request #2768 from benjarobin/fix-2718
[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 "time-util.h"
27
28 static void test_testing(void) {
29 bool b;
30
31 log_info("============ %s ==========", __func__);
32
33 b = mac_selinux_use();
34 log_info("mac_selinux_use → %d", b);
35
36 b = mac_selinux_have();
37 log_info("mac_selinux_have → %d", b);
38
39 mac_selinux_retest();
40
41 b = mac_selinux_use();
42 log_info("mac_selinux_use → %d", b);
43
44 b = mac_selinux_have();
45 log_info("mac_selinux_have → %d", b);
46 }
47
48 static void test_loading(void) {
49 usec_t n1, n2;
50 int r;
51
52 log_info("============ %s ==========", __func__);
53
54 n1 = now(CLOCK_MONOTONIC);
55 r = mac_selinux_init();
56 n2 = now(CLOCK_MONOTONIC);
57 log_info_errno(r, "mac_selinux_init → %d (%m) %.2fs", r, (n2 - n1)/1e6);
58 }
59
60 static void test_cleanup(void) {
61 usec_t n1, n2;
62
63 log_info("============ %s ==========", __func__);
64
65 n1 = now(CLOCK_MONOTONIC);
66 mac_selinux_finish();
67 n2 = now(CLOCK_MONOTONIC);
68 log_info("mac_selinux_finish → %.2fs", (n2 - n1)/1e6);
69 }
70
71 static void test_misc(const char* fname) {
72 _cleanup_(mac_selinux_freep) char *label = NULL, *label2 = NULL, *label3 = NULL;
73 int r;
74 _cleanup_close_ int fd = -1;
75
76 log_info("============ %s ==========", __func__);
77
78 r = mac_selinux_get_our_label(&label);
79 log_info_errno(r, "mac_selinux_get_our_label → %d (%m), \"%s\"", r, label);
80
81 r = mac_selinux_get_create_label_from_exe(fname, &label2);
82 log_info_errno(r, "mac_selinux_create_label_from_exe → %d (%m), \"%s\"", r, label2);
83
84 fd = socket(AF_INET, SOCK_DGRAM, 0);
85 assert_se(fd >= 0);
86
87 r = mac_selinux_get_child_mls_label(fd, fname, label2, &label3);
88 log_info_errno(r, "mac_selinux_get_child_mls_label → %d (%m), \"%s\"", r, label3);
89 }
90
91 static void test_create_file_prepare(const char* fname) {
92 int r;
93
94 log_info("============ %s ==========", __func__);
95
96 r = mac_selinux_create_file_prepare(fname, S_IRWXU);
97 log_info_errno(r, "mac_selinux_create_file_prepare → %d (%m)", r);
98
99 mac_selinux_create_file_clear();
100 }
101
102 int main(int argc, char **argv) {
103 const char *path = SYSTEMD_BINARY_PATH;
104 if (argc >= 2)
105 path = argv[1];
106
107 log_set_max_level(LOG_DEBUG);
108 log_parse_environment();
109
110 test_testing();
111 test_loading();
112 test_misc(path);
113 test_create_file_prepare(path);
114 test_cleanup();
115
116 return 0;
117 }