]>
Commit | Line | Data |
---|---|---|
d452335a LP |
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
2 | ||
3 | #include "image-policy.h" | |
4 | #include "pretty-print.h" | |
5 | #include "string-util.h" | |
6 | #include "tests.h" | |
7 | #include "pager.h" | |
8 | ||
9 | static void test_policy(const ImagePolicy *p, const char *name) { | |
10 | _cleanup_free_ char *as_string = NULL, *as_string_simplified = NULL; | |
11 | _cleanup_free_ ImagePolicy *parsed = NULL; | |
12 | ||
13 | assert_se(image_policy_to_string(p, /* simplified= */ false, &as_string) >= 0); | |
14 | assert_se(image_policy_to_string(p, /* simplified= */ true, &as_string_simplified) >= 0); | |
15 | ||
16 | printf("%s%s", ansi_underline(), name); | |
17 | ||
18 | if (!streq(as_string_simplified, name)) { | |
19 | printf(" → %s", as_string_simplified); | |
20 | ||
21 | if (!streq(as_string, as_string_simplified)) | |
22 | printf(" (aka %s)", as_string); | |
23 | } | |
24 | ||
25 | printf("%s\n", ansi_normal()); | |
26 | ||
27 | assert_se(image_policy_from_string(as_string, &parsed) >= 0); | |
28 | assert_se(image_policy_equal(p, parsed)); | |
29 | parsed = image_policy_free(parsed); | |
30 | ||
31 | assert_se(image_policy_from_string(as_string_simplified, &parsed) >= 0); | |
32 | assert_se(image_policy_equivalent(p, parsed)); | |
33 | parsed = image_policy_free(parsed); | |
34 | ||
35 | for (PartitionDesignator d = 0; d < _PARTITION_DESIGNATOR_MAX; d++) { | |
36 | _cleanup_free_ char *k = NULL; | |
37 | PartitionPolicyFlags f; | |
38 | ||
39 | f = image_policy_get(p, d); | |
40 | if (f < 0) { | |
41 | f = image_policy_get_exhaustively(p, d); | |
42 | assert_se(f >= 0); | |
43 | assert_se(partition_policy_flags_to_string(f, /* simplified= */ true, &k) >= 0); | |
44 | ||
45 | printf("%s\t%s → n/a (exhaustively: %s)%s\n", ansi_grey(), partition_designator_to_string(d), k, ansi_normal()); | |
46 | } else { | |
47 | assert_se(partition_policy_flags_to_string(f, /* simplified= */ true, &k) >= 0); | |
48 | printf("\t%s → %s\n", partition_designator_to_string(d), k); | |
49 | } | |
50 | } | |
51 | ||
52 | _cleanup_free_ char *w = NULL; | |
53 | assert_se(partition_policy_flags_to_string(image_policy_default(p), /* simplified= */ true, &w) >= 0); | |
54 | printf("\tdefault → %s\n", w); | |
55 | } | |
56 | ||
57 | static void test_policy_string(const char *t) { | |
58 | _cleanup_free_ ImagePolicy *parsed = NULL; | |
59 | ||
60 | assert_se(image_policy_from_string(t, &parsed) >= 0); | |
61 | test_policy(parsed, t); | |
62 | } | |
63 | ||
64 | static void test_policy_equiv(const char *s, bool (*func)(const ImagePolicy *p)) { | |
65 | _cleanup_(image_policy_freep) ImagePolicy *p = NULL; | |
66 | ||
67 | assert_se(image_policy_from_string(s, &p) >= 0); | |
68 | ||
69 | assert_se(func(p)); | |
70 | assert_se(func == image_policy_equiv_ignore || !image_policy_equiv_ignore(p)); | |
71 | assert_se(func == image_policy_equiv_allow || !image_policy_equiv_allow(p)); | |
72 | assert_se(func == image_policy_equiv_deny || !image_policy_equiv_deny(p)); | |
73 | } | |
74 | ||
75 | TEST_RET(test_image_policy_to_string) { | |
76 | test_policy(&image_policy_allow, "*"); | |
77 | test_policy(&image_policy_ignore, "-"); | |
78 | test_policy(&image_policy_deny, "~"); | |
79 | test_policy(&image_policy_sysext, "sysext"); | |
a594288d | 80 | test_policy(&image_policy_sysext_strict, "sysext-strict"); |
484d26da | 81 | test_policy(&image_policy_confext, "confext"); |
d4fee894 | 82 | test_policy(&image_policy_confext_strict, "confext-strict"); |
d452335a LP |
83 | test_policy(&image_policy_container, "container"); |
84 | test_policy(&image_policy_host, "host"); | |
85 | test_policy(&image_policy_service, "service"); | |
86 | test_policy(NULL, "null"); | |
87 | ||
88 | test_policy_string(""); | |
89 | test_policy_string("-"); | |
90 | test_policy_string("*"); | |
91 | test_policy_string("~"); | |
92 | test_policy_string("swap=open"); | |
93 | test_policy_string("swap=open:root=signed"); | |
94 | test_policy_string("swap=open:root=signed+read-only-on+growfs-off:=absent"); | |
95 | test_policy_string("=-"); | |
96 | test_policy_string("="); | |
97 | ||
98 | test_policy_equiv("", image_policy_equiv_ignore); | |
99 | test_policy_equiv("-", image_policy_equiv_ignore); | |
100 | test_policy_equiv("*", image_policy_equiv_allow); | |
101 | test_policy_equiv("~", image_policy_equiv_deny); | |
102 | test_policy_equiv("=absent", image_policy_equiv_deny); | |
103 | test_policy_equiv("=open", image_policy_equiv_allow); | |
104 | test_policy_equiv("=verity+signed+encrypted+unprotected+unused+absent", image_policy_equiv_allow); | |
105 | test_policy_equiv("=signed+verity+encrypted+unused+unprotected+absent", image_policy_equiv_allow); | |
106 | test_policy_equiv("=ignore", image_policy_equiv_ignore); | |
107 | test_policy_equiv("=absent+unused", image_policy_equiv_ignore); | |
108 | test_policy_equiv("=unused+absent", image_policy_equiv_ignore); | |
109 | test_policy_equiv("root=ignore:=ignore", image_policy_equiv_ignore); | |
110 | ||
111 | assert_se(image_policy_from_string("pfft", NULL) == -EINVAL); | |
112 | assert_se(image_policy_from_string("öäüß", NULL) == -EINVAL); | |
113 | assert_se(image_policy_from_string(":", NULL) == -EINVAL); | |
114 | assert_se(image_policy_from_string("a=", NULL) == -EBADSLT); | |
115 | assert_se(image_policy_from_string("=a", NULL) == -EBADRQC); | |
116 | assert_se(image_policy_from_string("==", NULL) == -EBADRQC); | |
117 | assert_se(image_policy_from_string("root=verity:root=encrypted", NULL) == -ENOTUNIQ); | |
118 | assert_se(image_policy_from_string("root=grbl", NULL) == -EBADRQC); | |
119 | assert_se(image_policy_from_string("wowza=grbl", NULL) == -EBADSLT); | |
120 | ||
121 | return 0; | |
122 | } | |
123 | ||
637d57dd LP |
124 | TEST(extend) { |
125 | assert_se(partition_policy_flags_extend(0) == _PARTITION_POLICY_MASK); | |
126 | assert_se(partition_policy_flags_extend(_PARTITION_POLICY_MASK) == _PARTITION_POLICY_MASK); | |
127 | assert_se(partition_policy_flags_extend(PARTITION_POLICY_UNPROTECTED) == (PARTITION_POLICY_UNPROTECTED|_PARTITION_POLICY_PFLAGS_MASK)); | |
128 | assert_se(partition_policy_flags_extend(PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_READ_ONLY_ON) == (PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_READ_ONLY_ON|_PARTITION_POLICY_GROWFS_MASK)); | |
129 | assert_se(partition_policy_flags_extend(PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_READ_ONLY_ON|PARTITION_POLICY_GROWFS_OFF) == (PARTITION_POLICY_UNPROTECTED|PARTITION_POLICY_READ_ONLY_ON|PARTITION_POLICY_GROWFS_OFF)); | |
130 | assert_se(partition_policy_flags_extend(PARTITION_POLICY_GROWFS_ON) == (PARTITION_POLICY_GROWFS_ON|_PARTITION_POLICY_USE_MASK|_PARTITION_POLICY_READ_ONLY_MASK)); | |
131 | } | |
132 | ||
d452335a | 133 | DEFINE_TEST_MAIN(LOG_INFO); |