Debian Codegraph @

src/test/test-secure-bits.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "securebits-util.h"
4 #include "strv.h"
5 #include "tests.h"
6
7 static const char * const string_bits[] = {
8 "keep-caps",
9 "keep-caps-locked",
10 "no-setuid-fixup",
11 "no-setuid-fixup-locked",
12 "noroot",
13 "noroot-locked",
14 NULL
15 };
16
17 TEST(secure_bits_basic) {
18 _cleanup_free_ char *joined = NULL, *str = NULL, *joined_ssv = NULL;
19 _cleanup_strv_free_ char **ssv = NULL;
20 int r;
21
22 /* Check if converting each bit from string and back to string yields
23 * the same value */
24 STRV_FOREACH(bit, string_bits) {
25 _cleanup_free_ char *s = NULL;
26 _cleanup_strv_free_ char **sv = NULL;
27
28 r = secure_bits_from_string(*bit);
29 assert_se(r > 0);
30 assert_se(secure_bits_is_valid(r));
31
32 assert_se(secure_bits_to_string_alloc(r, &s) >= 0);
33 printf("%s = 0x%x = %s\n", *bit, (unsigned)r, s);
34 ASSERT_STREQ(*bit, s);
35
36 ASSERT_OK(secure_bits_to_strv(r, &sv));
37 ASSERT_EQ(strv_length(sv), (size_t) 1);
38 ASSERT_STREQ(*bit, sv[0]);
39 }
40
41 /* Ditto, but with all bits at once */
42 joined = strv_join((char**)string_bits, " ");
43 assert_se(joined);
44 r = secure_bits_from_string(joined);
45 assert_se(r > 0);
46 assert_se(secure_bits_is_valid(r));
47 assert_se(secure_bits_to_string_alloc(r, &str) >= 0);
48 printf("%s = 0x%x = %s\n", joined, (unsigned)r, str);
49 ASSERT_STREQ(joined, str);
50
51 ASSERT_OK(secure_bits_to_strv(r, &ssv));
52 joined_ssv = strv_join(ssv, " ");
53 ASSERT_STREQ(joined, joined_ssv);
54
55 str = mfree(str);
56 ssv = strv_free(ssv);
57
58 /* Empty string */
59 assert_se(secure_bits_from_string("") == 0);
60 assert_se(secure_bits_from_string(" ") == 0);
61
62 /* Only invalid entries */
63 assert_se(secure_bits_from_string("foo bar baz") == 0);
64
65 /* Empty secure bits */
66 ASSERT_OK(secure_bits_to_string_alloc(0, &str));
67 ASSERT_TRUE(isempty(str));
68 ASSERT_OK(secure_bits_to_strv(0, &ssv));
69 ASSERT_TRUE(strv_isempty(ssv));
70
71 str = mfree(str);
72
73 /* Bits to string with check */
74 assert_se(secure_bits_to_string_alloc_with_check(INT_MAX, &str) == -EINVAL);
75 ASSERT_NULL(str);
76 assert_se(secure_bits_to_string_alloc_with_check(
77 (1 << SECURE_KEEP_CAPS) | (1 << SECURE_KEEP_CAPS_LOCKED),
78 &str) >= 0);
79 ASSERT_STREQ(str, "keep-caps keep-caps-locked");
80 }
81
82 TEST(secure_bits_mix) {
83 static struct sbit_table {
84 const char *input;
85 const char *expected;
86 } sbit_table[] = {
87 { "keep-caps keep-caps keep-caps", "keep-caps" },
88 { "keep-caps noroot keep-caps", "keep-caps noroot" },
89 { "noroot foo bar baz noroot", "noroot" },
90 { "noroot \"foo\" \"bar keep-caps", "noroot" },
91 { "\"noroot foo\" bar keep-caps", "keep-caps" },
92 {}
93 };
94
95 for (const struct sbit_table *s = sbit_table; s->input; s++) {
96 _cleanup_free_ char *str = NULL;
97 int r;
98
99 r = secure_bits_from_string(s->input);
100 assert_se(r > 0);
101 assert_se(secure_bits_is_valid(r));
102 assert_se(secure_bits_to_string_alloc(r, &str) >= 0);
103 printf("%s = 0x%x = %s\n", s->input, (unsigned)r, str);
104 ASSERT_STREQ(s->expected, str);
105 }
106 }
107
108 DEFINE_TEST_MAIN(LOG_DEBUG);