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"
36 static void test_condition_test_path(void) {
39 condition
= condition_new(CONDITION_PATH_EXISTS
, "/bin/sh", false, false);
41 assert_se(condition_test(condition
));
42 condition_free(condition
);
44 condition
= condition_new(CONDITION_PATH_EXISTS
, "/bin/s?", false, false);
46 assert_se(!condition_test(condition
));
47 condition_free(condition
);
49 condition
= condition_new(CONDITION_PATH_EXISTS_GLOB
, "/bin/s?", false, false);
51 assert_se(condition_test(condition
));
52 condition_free(condition
);
54 condition
= condition_new(CONDITION_PATH_EXISTS_GLOB
, "/bin/s?", false, true);
56 assert_se(!condition_test(condition
));
57 condition_free(condition
);
59 condition
= condition_new(CONDITION_PATH_EXISTS
, "/thiscertainlywontexist", false, false);
61 assert_se(!condition_test(condition
));
62 condition_free(condition
);
64 condition
= condition_new(CONDITION_PATH_EXISTS
, "/thiscertainlywontexist", false, true);
66 assert_se(condition_test(condition
));
67 condition_free(condition
);
69 condition
= condition_new(CONDITION_PATH_IS_DIRECTORY
, "/bin", false, false);
71 assert_se(condition_test(condition
));
72 condition_free(condition
);
74 condition
= condition_new(CONDITION_DIRECTORY_NOT_EMPTY
, "/bin", false, false);
76 assert_se(condition_test(condition
));
77 condition_free(condition
);
79 condition
= condition_new(CONDITION_FILE_NOT_EMPTY
, "/bin/sh", false, false);
81 assert_se(condition_test(condition
));
82 condition_free(condition
);
84 condition
= condition_new(CONDITION_FILE_IS_EXECUTABLE
, "/bin/sh", false, false);
86 assert_se(condition_test(condition
));
87 condition_free(condition
);
89 condition
= condition_new(CONDITION_FILE_IS_EXECUTABLE
, "/etc/passwd", false, false);
91 assert_se(!condition_test(condition
));
92 condition_free(condition
);
94 condition
= condition_new(CONDITION_PATH_IS_MOUNT_POINT
, "/proc", false, false);
96 assert_se(condition_test(condition
));
97 condition_free(condition
);
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
);
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
);
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
);
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
);
120 static void test_condition_test_ac_power(void) {
121 Condition
*condition
;
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
);
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
);
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
);
139 static void test_condition_test_host(void) {
140 _cleanup_free_
char *hostname
= NULL
;
141 char sid
[SD_ID128_STRING_MAX
];
142 Condition
*condition
;
146 r
= sd_id128_get_machine(&id
);
148 assert_se(sd_id128_to_string(id
, sid
));
150 condition
= condition_new(CONDITION_HOST
, sid
, false, false);
151 assert_se(condition
);
152 assert_se(condition_test(condition
));
153 condition_free(condition
);
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
);
160 condition
= condition_new(CONDITION_HOST
, sid
, false, true);
161 assert_se(condition
);
162 assert_se(!condition_test(condition
));
163 condition_free(condition
);
165 hostname
= gethostname_malloc();
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");
172 condition
= condition_new(CONDITION_HOST
, hostname
, false, false);
173 assert_se(condition
);
174 assert_se(condition_test(condition
));
175 condition_free(condition
);
179 static void test_condition_test_architecture(void) {
180 Condition
*condition
;
184 a
= uname_architecture();
187 sa
= architecture_to_string(a
);
190 condition
= condition_new(CONDITION_ARCHITECTURE
, sa
, false, false);
191 assert_se(condition
);
192 assert_se(condition_test(condition
) > 0);
193 condition_free(condition
);
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
);
200 condition
= condition_new(CONDITION_ARCHITECTURE
, sa
, false, true);
201 assert_se(condition
);
202 assert_se(condition_test(condition
) == 0);
203 condition_free(condition
);
206 static void test_condition_test_kernel_command_line(void) {
207 Condition
*condition
;
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
);
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
);
220 static void test_condition_test_null(void) {
221 Condition
*condition
;
223 condition
= condition_new(CONDITION_NULL
, NULL
, false, false);
224 assert_se(condition
);
225 assert_se(condition_test(condition
));
226 condition_free(condition
);
228 condition
= condition_new(CONDITION_NULL
, NULL
, false, true);
229 assert_se(condition
);
230 assert_se(!condition_test(condition
));
231 condition_free(condition
);
234 static void test_condition_test_security(void) {
235 Condition
*condition
;
237 condition
= condition_new(CONDITION_SECURITY
, "garbage oifdsjfoidsjoj", false, false);
238 assert_se(condition
);
239 assert_se(!condition_test(condition
));
240 condition_free(condition
);
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
);
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
);
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
);
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
);
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
);
268 int main(int argc
, char *argv
[]) {
269 log_parse_environment();
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();