]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-cap-list.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
8 #include "capability-util.h"
9 #include "parse-util.h"
10 #include "string-util.h"
13 /* verify the capability parser */
14 static void test_cap_list(void) {
15 assert_se(!capability_to_name(-1));
16 assert_se(!capability_to_name(capability_list_length()));
18 for (int i
= 0; i
< capability_list_length(); i
++) {
21 assert_se(n
= capability_to_name(i
));
22 assert_se(capability_from_name(n
) == i
);
23 printf("%s = %i\n", n
, i
);
26 assert_se(capability_from_name("asdfbsd") == -EINVAL
);
27 assert_se(capability_from_name("CAP_AUDIT_READ") == CAP_AUDIT_READ
);
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("0") == 0);
31 assert_se(capability_from_name("15") == 15);
32 assert_se(capability_from_name("63") == 63);
33 assert_se(capability_from_name("64") == -EINVAL
);
34 assert_se(capability_from_name("-1") == -EINVAL
);
36 for (int i
= 0; i
< capability_list_length(); i
++) {
37 _cleanup_cap_free_charp_
char *a
= NULL
;
41 assert_se(a
= cap_to_name(i
));
43 /* quit the loop as soon as libcap starts returning
44 * numeric ids, formatted as strings */
45 if (safe_atou(a
, &u
) >= 0)
48 assert_se(b
= capability_to_name(i
));
50 printf("%s vs. %s\n", a
, b
);
52 assert_se(strcasecmp(a
, b
) == 0);
56 static void test_capability_set_one(uint64_t c
, const char *t
) {
57 _cleanup_free_
char *t1
= NULL
;
58 uint64_t c1
, c_masked
= c
& ((UINT64_C(1) << capability_list_length()) - 1);
60 assert_se(capability_set_to_string_alloc(c
, &t1
) == 0);
61 assert_se(streq(t1
, t
));
63 assert_se(capability_set_from_string(t1
, &c1
) == 0);
64 assert_se(c1
== c_masked
);
67 assert_se(t1
= strjoin("'cap_chown cap_dac_override' \"cap_setgid cap_setuid\"", t
,
68 " hogehoge foobar 18446744073709551616 3.14 -3 ", t
));
69 assert_se(capability_set_from_string(t1
, &c1
) == 0);
70 assert_se(c1
== c_masked
);
73 static void test_capability_set(void) {
76 assert_se(capability_set_from_string(NULL
, &c
) == 0);
79 assert_se(capability_set_from_string("", &c
) == 0);
82 assert_se(capability_set_from_string("0", &c
) == 0);
83 assert_se(c
== UINT64_C(1));
85 assert_se(capability_set_from_string("1", &c
) == 0);
86 assert_se(c
== UINT64_C(1) << 1);
88 assert_se(capability_set_from_string("0 1 2 3", &c
) == 0);
89 assert_se(c
== (UINT64_C(1) << 4) - 1);
91 test_capability_set_one(0, "");
92 test_capability_set_one(
93 UINT64_C(1) << CAP_DAC_OVERRIDE
,
95 test_capability_set_one(
96 UINT64_C(1) << CAP_DAC_OVERRIDE
|
97 UINT64_C(1) << capability_list_length(),
99 test_capability_set_one(
100 UINT64_C(1) << capability_list_length(), "");
101 test_capability_set_one(
102 UINT64_C(1) << CAP_CHOWN
|
103 UINT64_C(1) << CAP_DAC_OVERRIDE
|
104 UINT64_C(1) << CAP_DAC_READ_SEARCH
|
105 UINT64_C(1) << CAP_FOWNER
|
106 UINT64_C(1) << CAP_SETGID
|
107 UINT64_C(1) << CAP_SETUID
|
108 UINT64_C(1) << CAP_SYS_PTRACE
|
109 UINT64_C(1) << CAP_SYS_ADMIN
|
110 UINT64_C(1) << CAP_AUDIT_CONTROL
|
111 UINT64_C(1) << CAP_MAC_OVERRIDE
|
112 UINT64_C(1) << CAP_SYSLOG
|
113 UINT64_C(1) << (capability_list_length() + 1),
114 "cap_chown cap_dac_override cap_dac_read_search cap_fowner "
115 "cap_setgid cap_setuid cap_sys_ptrace cap_sys_admin "
116 "cap_audit_control cap_mac_override cap_syslog");
119 int main(int argc
, char *argv
[]) {
121 test_capability_set();