chiark / gitweb /
Prep v236 : Add missing SPDX-License-Identifier (8/9) src/test
[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   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 #if 0 /// UNNEEDED by elogind
75 static void test_misc(const char* fname) {
76         _cleanup_(mac_selinux_freep) char *label = NULL, *label2 = NULL, *label3 = NULL;
77         int r;
78         _cleanup_close_ int fd = -1;
79
80         log_info("============ %s ==========", __func__);
81
82         r = mac_selinux_get_our_label(&label);
83         log_info_errno(r, "mac_selinux_get_our_label → %d, \"%s\" (%m)",
84                        r, strnull(label));
85
86         r = mac_selinux_get_create_label_from_exe(fname, &label2);
87         log_info_errno(r, "mac_selinux_create_label_from_exe → %d, \"%s\" (%m)",
88                        r, strnull(label2));
89
90         fd = socket(AF_INET, SOCK_DGRAM, 0);
91         assert_se(fd >= 0);
92
93         r = mac_selinux_get_child_mls_label(fd, fname, label2, &label3);
94         log_info_errno(r, "mac_selinux_get_child_mls_label → %d, \"%s\" (%m)",
95                        r, strnull(label3));
96 }
97 #endif // 0
98
99 static void test_create_file_prepare(const char* fname) {
100         int r;
101
102         log_info("============ %s ==========", __func__);
103
104         r = mac_selinux_create_file_prepare(fname, S_IRWXU);
105         log_info_errno(r, "mac_selinux_create_file_prepare → %d (%m)", r);
106
107         mac_selinux_create_file_clear();
108 }
109
110 int main(int argc, char **argv) {
111         const char *path = SYSTEMD_BINARY_PATH;
112         if (argc >= 2)
113                 path = argv[1];
114
115         log_set_max_level(LOG_DEBUG);
116         log_parse_environment();
117
118         test_testing();
119         test_loading();
120 #if 0 /// UNNEEDED by elogind
121         test_misc(path);
122 #endif // 0
123         test_create_file_prepare(path);
124         test_cleanup();
125
126         return 0;
127 }