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