2 This file is part of systemd
4 Copyright 2014 Ronny Chevalier
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.
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.
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/>.
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"
32 #include "selinux-util.h"
33 #include "smack-util.h"
38 static void test_condition_test_path(void) {
41 condition
= condition_new(CONDITION_PATH_EXISTS
, "/bin/sh", false, false);
43 assert_se(condition_test(condition
));
44 condition_free(condition
);
46 condition
= condition_new(CONDITION_PATH_EXISTS
, "/bin/s?", false, false);
48 assert_se(!condition_test(condition
));
49 condition_free(condition
);
51 condition
= condition_new(CONDITION_PATH_EXISTS_GLOB
, "/bin/s?", false, false);
53 assert_se(condition_test(condition
));
54 condition_free(condition
);
56 condition
= condition_new(CONDITION_PATH_EXISTS_GLOB
, "/bin/s?", false, true);
58 assert_se(!condition_test(condition
));
59 condition_free(condition
);
61 condition
= condition_new(CONDITION_PATH_EXISTS
, "/thiscertainlywontexist", false, false);
63 assert_se(!condition_test(condition
));
64 condition_free(condition
);
66 condition
= condition_new(CONDITION_PATH_EXISTS
, "/thiscertainlywontexist", false, true);
68 assert_se(condition_test(condition
));
69 condition_free(condition
);
71 condition
= condition_new(CONDITION_PATH_IS_DIRECTORY
, "/bin", false, false);
73 assert_se(condition_test(condition
));
74 condition_free(condition
);
76 condition
= condition_new(CONDITION_DIRECTORY_NOT_EMPTY
, "/bin", false, false);
78 assert_se(condition_test(condition
));
79 condition_free(condition
);
81 condition
= condition_new(CONDITION_FILE_NOT_EMPTY
, "/bin/sh", false, false);
83 assert_se(condition_test(condition
));
84 condition_free(condition
);
86 condition
= condition_new(CONDITION_FILE_IS_EXECUTABLE
, "/bin/sh", false, false);
88 assert_se(condition_test(condition
));
89 condition_free(condition
);
91 condition
= condition_new(CONDITION_FILE_IS_EXECUTABLE
, "/etc/passwd", false, false);
93 assert_se(!condition_test(condition
));
94 condition_free(condition
);
96 condition
= condition_new(CONDITION_PATH_IS_MOUNT_POINT
, "/proc", false, false);
98 assert_se(condition_test(condition
));
99 condition_free(condition
);
101 condition
= condition_new(CONDITION_PATH_IS_MOUNT_POINT
, "/", false, false);
102 assert_se(condition
);
103 assert_se(condition_test(condition
));
104 condition_free(condition
);
106 condition
= condition_new(CONDITION_PATH_IS_MOUNT_POINT
, "/bin", false, false);
107 assert_se(condition
);
108 assert_se(!condition_test(condition
));
109 condition_free(condition
);
111 condition
= condition_new(CONDITION_PATH_IS_READ_WRITE
, "/tmp", false, false);
112 assert_se(condition
);
113 assert_se(condition_test(condition
));
114 condition_free(condition
);
116 condition
= condition_new(CONDITION_PATH_IS_SYMBOLIC_LINK
, "/dev/stdout", false, false);
117 assert_se(condition
);
118 assert_se(condition_test(condition
));
119 condition_free(condition
);
122 static void test_condition_test_ac_power(void) {
123 Condition
*condition
;
125 condition
= condition_new(CONDITION_AC_POWER
, "true", false, false);
126 assert_se(condition
);
127 assert_se(condition_test(condition
) == on_ac_power());
128 condition_free(condition
);
130 condition
= condition_new(CONDITION_AC_POWER
, "false", false, false);
131 assert_se(condition
);
132 assert_se(condition_test(condition
) != on_ac_power());
133 condition_free(condition
);
135 condition
= condition_new(CONDITION_AC_POWER
, "false", false, true);
136 assert_se(condition
);
137 assert_se(condition_test(condition
) == on_ac_power());
138 condition_free(condition
);
141 static void test_condition_test_host(void) {
142 _cleanup_free_
char *hostname
= NULL
;
143 char sid
[SD_ID128_STRING_MAX
];
144 Condition
*condition
;
148 r
= sd_id128_get_machine(&id
);
150 assert_se(sd_id128_to_string(id
, sid
));
152 condition
= condition_new(CONDITION_HOST
, sid
, false, false);
153 assert_se(condition
);
154 assert_se(condition_test(condition
));
155 condition_free(condition
);
157 condition
= condition_new(CONDITION_HOST
, "garbage value jjjjjjjjjjjjjj", false, false);
158 assert_se(condition
);
159 assert_se(!condition_test(condition
));
160 condition_free(condition
);
162 condition
= condition_new(CONDITION_HOST
, sid
, false, true);
163 assert_se(condition
);
164 assert_se(!condition_test(condition
));
165 condition_free(condition
);
167 hostname
= gethostname_malloc();
170 /* if hostname looks like an id128 then skip testing it */
171 if (id128_is_valid(hostname
))
172 log_notice("hostname is an id128, skipping test");
174 condition
= condition_new(CONDITION_HOST
, hostname
, false, false);
175 assert_se(condition
);
176 assert_se(condition_test(condition
));
177 condition_free(condition
);
181 static void test_condition_test_architecture(void) {
182 Condition
*condition
;
186 a
= uname_architecture();
189 sa
= architecture_to_string(a
);
192 condition
= condition_new(CONDITION_ARCHITECTURE
, sa
, false, false);
193 assert_se(condition
);
194 assert_se(condition_test(condition
) > 0);
195 condition_free(condition
);
197 condition
= condition_new(CONDITION_ARCHITECTURE
, "garbage value", false, false);
198 assert_se(condition
);
199 assert_se(condition_test(condition
) == 0);
200 condition_free(condition
);
202 condition
= condition_new(CONDITION_ARCHITECTURE
, sa
, false, true);
203 assert_se(condition
);
204 assert_se(condition_test(condition
) == 0);
205 condition_free(condition
);
208 static void test_condition_test_kernel_command_line(void) {
209 Condition
*condition
;
211 condition
= condition_new(CONDITION_KERNEL_COMMAND_LINE
, "thisreallyshouldntbeonthekernelcommandline", false, false);
212 assert_se(condition
);
213 assert_se(!condition_test(condition
));
214 condition_free(condition
);
216 condition
= condition_new(CONDITION_KERNEL_COMMAND_LINE
, "andthis=neither", false, false);
217 assert_se(condition
);
218 assert_se(!condition_test(condition
));
219 condition_free(condition
);
222 static void test_condition_test_null(void) {
223 Condition
*condition
;
225 condition
= condition_new(CONDITION_NULL
, NULL
, false, false);
226 assert_se(condition
);
227 assert_se(condition_test(condition
));
228 condition_free(condition
);
230 condition
= condition_new(CONDITION_NULL
, NULL
, false, true);
231 assert_se(condition
);
232 assert_se(!condition_test(condition
));
233 condition_free(condition
);
236 static void test_condition_test_security(void) {
237 Condition
*condition
;
239 condition
= condition_new(CONDITION_SECURITY
, "garbage oifdsjfoidsjoj", false, false);
240 assert_se(condition
);
241 assert_se(!condition_test(condition
));
242 condition_free(condition
);
244 condition
= condition_new(CONDITION_SECURITY
, "selinux", false, true);
245 assert_se(condition
);
246 assert_se(condition_test(condition
) != mac_selinux_use());
247 condition_free(condition
);
249 condition
= condition_new(CONDITION_SECURITY
, "ima", false, false);
250 assert_se(condition
);
251 assert_se(condition_test(condition
) == use_ima());
252 condition_free(condition
);
254 condition
= condition_new(CONDITION_SECURITY
, "apparmor", false, false);
255 assert_se(condition
);
256 assert_se(condition_test(condition
) == mac_apparmor_use());
257 condition_free(condition
);
259 condition
= condition_new(CONDITION_SECURITY
, "smack", false, false);
260 assert_se(condition
);
261 assert_se(condition_test(condition
) == mac_smack_use());
262 condition_free(condition
);
264 condition
= condition_new(CONDITION_SECURITY
, "audit", false, false);
265 assert_se(condition
);
266 assert_se(condition_test(condition
) == use_audit());
267 condition_free(condition
);
270 static void test_condition_test_virtualization(void) {
271 Condition
*condition
;
275 condition
= condition_new(CONDITION_VIRTUALIZATION
, "garbage oifdsjfoidsjoj", false, false);
276 assert_se(condition
);
277 r
= condition_test(condition
);
278 log_info("ConditionVirtualization=garbage → %i", r
);
280 condition_free(condition
);
282 condition
= condition_new(CONDITION_VIRTUALIZATION
, "container", false, false);
283 assert_se(condition
);
284 r
= condition_test(condition
);
285 log_info("ConditionVirtualization=container → %i", r
);
286 assert_se(r
== !!detect_container());
287 condition_free(condition
);
289 condition
= condition_new(CONDITION_VIRTUALIZATION
, "vm", false, false);
290 assert_se(condition
);
291 r
= condition_test(condition
);
292 log_info("ConditionVirtualization=vm → %i", r
);
293 assert_se(r
== (detect_vm() && !detect_container()));
294 condition_free(condition
);
296 condition
= condition_new(CONDITION_VIRTUALIZATION
, "private-users", false, false);
297 assert_se(condition
);
298 r
= condition_test(condition
);
299 log_info("ConditionVirtualization=private-users → %i", r
);
300 assert_se(r
== !!running_in_userns());
301 condition_free(condition
);
317 condition
= condition_new(CONDITION_VIRTUALIZATION
, virt
, false, false);
318 assert_se(condition
);
319 r
= condition_test(condition
);
320 log_info("ConditionVirtualization=%s → %i", virt
, r
);
322 condition_free(condition
);
326 int main(int argc
, char *argv
[]) {
327 log_set_max_level(LOG_DEBUG
);
328 log_parse_environment();
331 test_condition_test_path();
332 test_condition_test_ac_power();
333 test_condition_test_host();
334 test_condition_test_architecture();
335 test_condition_test_kernel_command_line();
336 test_condition_test_null();
337 test_condition_test_security();
338 test_condition_test_virtualization();