]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-cap-list.c
tree-wide: remove Lennart's copyright lines
[thirdparty/systemd.git] / src / test / test-cap-list.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <sys/prctl.h>
4
5 #include "alloc-util.h"
6 #include "cap-list.h"
7 #include "capability-util.h"
8 #include "parse-util.h"
9 #include "string-util.h"
10 #include "util.h"
11
12 /* verify the capability parser */
13 static void test_cap_list(void) {
14 int i;
15
16 assert_se(!capability_to_name(-1));
17 assert_se(!capability_to_name(capability_list_length()));
18
19 for (i = 0; i < capability_list_length(); i++) {
20 const char *n;
21
22 assert_se(n = capability_to_name(i));
23 assert_se(capability_from_name(n) == i);
24 printf("%s = %i\n", n, i);
25 }
26
27 assert_se(capability_from_name("asdfbsd") == -EINVAL);
28 assert_se(capability_from_name("CAP_AUDIT_READ") == CAP_AUDIT_READ);
29 assert_se(capability_from_name("cap_audit_read") == CAP_AUDIT_READ);
30 assert_se(capability_from_name("cAp_aUdIt_rEAd") == CAP_AUDIT_READ);
31 assert_se(capability_from_name("0") == 0);
32 assert_se(capability_from_name("15") == 15);
33 assert_se(capability_from_name("-1") == -EINVAL);
34
35 for (i = 0; i < capability_list_length(); i++) {
36 _cleanup_cap_free_charp_ char *a = NULL;
37 const char *b;
38 unsigned u;
39
40 assert_se(a = cap_to_name(i));
41
42 /* quit the loop as soon as libcap starts returning
43 * numeric ids, formatted as strings */
44 if (safe_atou(a, &u) >= 0)
45 break;
46
47 assert_se(b = capability_to_name(i));
48
49 printf("%s vs. %s\n", a, b);
50
51 assert_se(strcasecmp(a, b) == 0);
52 }
53 }
54
55 static void test_capability_set_one(uint64_t c, const char *t) {
56 _cleanup_free_ char *t1 = NULL;
57 uint64_t c1, c_masked = c & ((UINT64_C(1) << capability_list_length()) - 1);
58
59 assert_se(capability_set_to_string_alloc(c, &t1) == 0);
60 assert_se(streq(t1, t));
61
62 assert_se(capability_set_from_string(t1, &c1) == 0);
63 assert_se(c1 == c_masked);
64
65 free(t1);
66 assert_se(t1 = strjoin("'cap_chown cap_dac_override' \"cap_setgid cap_setuid\"", t,
67 " hogehoge foobar 12345 3.14 -3 ", t));
68 assert_se(capability_set_from_string(t1, &c1) == 0);
69 assert_se(c1 == c_masked);
70 }
71
72 static void test_capability_set(void) {
73 uint64_t c;
74
75 assert_se(capability_set_from_string(NULL, &c) == 0);
76 assert_se(c == 0);
77
78 assert_se(capability_set_from_string("", &c) == 0);
79 assert_se(c == 0);
80
81 assert_se(capability_set_from_string("0", &c) == 0);
82 assert_se(c == UINT64_C(1));
83
84 assert_se(capability_set_from_string("1", &c) == 0);
85 assert_se(c == UINT64_C(1) << 1);
86
87 assert_se(capability_set_from_string("0 1 2 3", &c) == 0);
88 assert_se(c == (UINT64_C(1) << 4) - 1);
89
90 test_capability_set_one(0, "");
91 test_capability_set_one(
92 UINT64_C(1) << CAP_DAC_OVERRIDE,
93 "cap_dac_override");
94 test_capability_set_one(
95 UINT64_C(1) << CAP_DAC_OVERRIDE |
96 UINT64_C(1) << capability_list_length(),
97 "cap_dac_override");
98 test_capability_set_one(
99 UINT64_C(1) << capability_list_length(), "");
100 test_capability_set_one(
101 UINT64_C(1) << CAP_CHOWN |
102 UINT64_C(1) << CAP_DAC_OVERRIDE |
103 UINT64_C(1) << CAP_DAC_READ_SEARCH |
104 UINT64_C(1) << CAP_FOWNER |
105 UINT64_C(1) << CAP_SETGID |
106 UINT64_C(1) << CAP_SETUID |
107 UINT64_C(1) << CAP_SYS_PTRACE |
108 UINT64_C(1) << CAP_SYS_ADMIN |
109 UINT64_C(1) << CAP_AUDIT_CONTROL |
110 UINT64_C(1) << CAP_MAC_OVERRIDE |
111 UINT64_C(1) << CAP_SYSLOG |
112 UINT64_C(1) << (capability_list_length() + 1),
113 "cap_chown cap_dac_override cap_dac_read_search cap_fowner "
114 "cap_setgid cap_setuid cap_sys_ptrace cap_sys_admin "
115 "cap_audit_control cap_mac_override cap_syslog");
116 }
117
118 int main(int argc, char *argv[]) {
119 test_cap_list();
120 test_capability_set();
121
122 return 0;
123 }