]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-condition.c
test: always check whether condition allocation worked
[thirdparty/systemd.git] / src / test / test-condition.c
1 /***
2 This file is part of systemd
3
4 Copyright 2014 Ronny Chevalier
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include "sd-id128.h"
21
22 #include "alloc-util.h"
23 #include "apparmor-util.h"
24 #include "architecture.h"
25 #include "audit-util.h"
26 #include "condition.h"
27 #include "hostname-util.h"
28 #include "id128-util.h"
29 #include "ima-util.h"
30 #include "log.h"
31 #include "macro.h"
32 #include "selinux-util.h"
33 #include "smack-util.h"
34 #include "util.h"
35
36 static void test_condition_test_path(void) {
37 Condition *condition;
38
39 condition = condition_new(CONDITION_PATH_EXISTS, "/bin/sh", false, false);
40 assert_se(condition);
41 assert_se(condition_test(condition));
42 condition_free(condition);
43
44 condition = condition_new(CONDITION_PATH_EXISTS, "/bin/s?", false, false);
45 assert_se(condition);
46 assert_se(!condition_test(condition));
47 condition_free(condition);
48
49 condition = condition_new(CONDITION_PATH_EXISTS_GLOB, "/bin/s?", false, false);
50 assert_se(condition);
51 assert_se(condition_test(condition));
52 condition_free(condition);
53
54 condition = condition_new(CONDITION_PATH_EXISTS_GLOB, "/bin/s?", false, true);
55 assert_se(condition);
56 assert_se(!condition_test(condition));
57 condition_free(condition);
58
59 condition = condition_new(CONDITION_PATH_EXISTS, "/thiscertainlywontexist", false, false);
60 assert_se(condition);
61 assert_se(!condition_test(condition));
62 condition_free(condition);
63
64 condition = condition_new(CONDITION_PATH_EXISTS, "/thiscertainlywontexist", false, true);
65 assert_se(condition);
66 assert_se(condition_test(condition));
67 condition_free(condition);
68
69 condition = condition_new(CONDITION_PATH_IS_DIRECTORY, "/bin", false, false);
70 assert_se(condition);
71 assert_se(condition_test(condition));
72 condition_free(condition);
73
74 condition = condition_new(CONDITION_DIRECTORY_NOT_EMPTY, "/bin", false, false);
75 assert_se(condition);
76 assert_se(condition_test(condition));
77 condition_free(condition);
78
79 condition = condition_new(CONDITION_FILE_NOT_EMPTY, "/bin/sh", false, false);
80 assert_se(condition);
81 assert_se(condition_test(condition));
82 condition_free(condition);
83
84 condition = condition_new(CONDITION_FILE_IS_EXECUTABLE, "/bin/sh", false, false);
85 assert_se(condition);
86 assert_se(condition_test(condition));
87 condition_free(condition);
88
89 condition = condition_new(CONDITION_FILE_IS_EXECUTABLE, "/etc/passwd", false, false);
90 assert_se(condition);
91 assert_se(!condition_test(condition));
92 condition_free(condition);
93
94 condition = condition_new(CONDITION_PATH_IS_MOUNT_POINT, "/proc", false, false);
95 assert_se(condition);
96 assert_se(condition_test(condition));
97 condition_free(condition);
98
99 condition = condition_new(CONDITION_PATH_IS_MOUNT_POINT, "/", false, false);
100 assert_se(condition);
101 assert_se(condition_test(condition));
102 condition_free(condition);
103
104 condition = condition_new(CONDITION_PATH_IS_MOUNT_POINT, "/bin", false, false);
105 assert_se(condition);
106 assert_se(!condition_test(condition));
107 condition_free(condition);
108
109 condition = condition_new(CONDITION_PATH_IS_READ_WRITE, "/tmp", false, false);
110 assert_se(condition);
111 assert_se(condition_test(condition));
112 condition_free(condition);
113
114 condition = condition_new(CONDITION_PATH_IS_SYMBOLIC_LINK, "/dev/stdout", false, false);
115 assert_se(condition);
116 assert_se(condition_test(condition));
117 condition_free(condition);
118 }
119
120 static void test_condition_test_ac_power(void) {
121 Condition *condition;
122
123 condition = condition_new(CONDITION_AC_POWER, "true", false, false);
124 assert_se(condition);
125 assert_se(condition_test(condition) == on_ac_power());
126 condition_free(condition);
127
128 condition = condition_new(CONDITION_AC_POWER, "false", false, false);
129 assert_se(condition);
130 assert_se(condition_test(condition) != on_ac_power());
131 condition_free(condition);
132
133 condition = condition_new(CONDITION_AC_POWER, "false", false, true);
134 assert_se(condition);
135 assert_se(condition_test(condition) == on_ac_power());
136 condition_free(condition);
137 }
138
139 static void test_condition_test_host(void) {
140 _cleanup_free_ char *hostname = NULL;
141 char sid[SD_ID128_STRING_MAX];
142 Condition *condition;
143 sd_id128_t id;
144 int r;
145
146 r = sd_id128_get_machine(&id);
147 assert_se(r >= 0);
148 assert_se(sd_id128_to_string(id, sid));
149
150 condition = condition_new(CONDITION_HOST, sid, false, false);
151 assert_se(condition);
152 assert_se(condition_test(condition));
153 condition_free(condition);
154
155 condition = condition_new(CONDITION_HOST, "garbage value jjjjjjjjjjjjjj", false, false);
156 assert_se(condition);
157 assert_se(!condition_test(condition));
158 condition_free(condition);
159
160 condition = condition_new(CONDITION_HOST, sid, false, true);
161 assert_se(condition);
162 assert_se(!condition_test(condition));
163 condition_free(condition);
164
165 hostname = gethostname_malloc();
166 assert_se(hostname);
167
168 /* if hostname looks like an id128 then skip testing it */
169 if (id128_is_valid(hostname))
170 log_notice("hostname is an id128, skipping test");
171 else {
172 condition = condition_new(CONDITION_HOST, hostname, false, false);
173 assert_se(condition);
174 assert_se(condition_test(condition));
175 condition_free(condition);
176 }
177 }
178
179 static void test_condition_test_architecture(void) {
180 Condition *condition;
181 const char *sa;
182 int a;
183
184 a = uname_architecture();
185 assert_se(a >= 0);
186
187 sa = architecture_to_string(a);
188 assert_se(sa);
189
190 condition = condition_new(CONDITION_ARCHITECTURE, sa, false, false);
191 assert_se(condition);
192 assert_se(condition_test(condition) > 0);
193 condition_free(condition);
194
195 condition = condition_new(CONDITION_ARCHITECTURE, "garbage value", false, false);
196 assert_se(condition);
197 assert_se(condition_test(condition) == 0);
198 condition_free(condition);
199
200 condition = condition_new(CONDITION_ARCHITECTURE, sa, false, true);
201 assert_se(condition);
202 assert_se(condition_test(condition) == 0);
203 condition_free(condition);
204 }
205
206 static void test_condition_test_kernel_command_line(void) {
207 Condition *condition;
208
209 condition = condition_new(CONDITION_KERNEL_COMMAND_LINE, "thisreallyshouldntbeonthekernelcommandline", false, false);
210 assert_se(condition);
211 assert_se(!condition_test(condition));
212 condition_free(condition);
213
214 condition = condition_new(CONDITION_KERNEL_COMMAND_LINE, "andthis=neither", false, false);
215 assert_se(condition);
216 assert_se(!condition_test(condition));
217 condition_free(condition);
218 }
219
220 static void test_condition_test_null(void) {
221 Condition *condition;
222
223 condition = condition_new(CONDITION_NULL, NULL, false, false);
224 assert_se(condition);
225 assert_se(condition_test(condition));
226 condition_free(condition);
227
228 condition = condition_new(CONDITION_NULL, NULL, false, true);
229 assert_se(condition);
230 assert_se(!condition_test(condition));
231 condition_free(condition);
232 }
233
234 static void test_condition_test_security(void) {
235 Condition *condition;
236
237 condition = condition_new(CONDITION_SECURITY, "garbage oifdsjfoidsjoj", false, false);
238 assert_se(condition);
239 assert_se(!condition_test(condition));
240 condition_free(condition);
241
242 condition = condition_new(CONDITION_SECURITY, "selinux", false, true);
243 assert_se(condition);
244 assert_se(condition_test(condition) != mac_selinux_have());
245 condition_free(condition);
246
247 condition = condition_new(CONDITION_SECURITY, "ima", false, false);
248 assert_se(condition);
249 assert_se(condition_test(condition) == use_ima());
250 condition_free(condition);
251
252 condition = condition_new(CONDITION_SECURITY, "apparmor", false, false);
253 assert_se(condition);
254 assert_se(condition_test(condition) == mac_apparmor_use());
255 condition_free(condition);
256
257 condition = condition_new(CONDITION_SECURITY, "smack", false, false);
258 assert_se(condition);
259 assert_se(condition_test(condition) == mac_smack_use());
260 condition_free(condition);
261
262 condition = condition_new(CONDITION_SECURITY, "audit", false, false);
263 assert_se(condition);
264 assert_se(condition_test(condition) == use_audit());
265 condition_free(condition);
266 }
267
268 int main(int argc, char *argv[]) {
269 log_parse_environment();
270 log_open();
271
272 test_condition_test_path();
273 test_condition_test_ac_power();
274 test_condition_test_host();
275 test_condition_test_architecture();
276 test_condition_test_kernel_command_line();
277 test_condition_test_null();
278 test_condition_test_security();
279
280 return 0;
281 }