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/>.
20 #include "condition.h"
24 #include "architecture.h"
27 static void test_condition_test_path_exists(void) {
30 condition = condition_new(CONDITION_PATH_EXISTS, "/bin/sh", false, false);
31 assert_se(condition_test(condition));
32 condition_free(condition);
34 condition = condition_new(CONDITION_PATH_EXISTS, "/bin/s?", false, false);
35 assert_se(!condition_test(condition));
36 condition_free(condition);
38 condition = condition_new(CONDITION_PATH_EXISTS_GLOB, "/bin/s?", false, false);
39 assert_se(condition_test(condition));
40 condition_free(condition);
42 condition = condition_new(CONDITION_PATH_EXISTS_GLOB, "/bin/s?", false, true);
43 assert_se(!condition_test(condition));
44 condition_free(condition);
46 condition = condition_new(CONDITION_PATH_EXISTS, "/thiscertainlywontexist", false, false);
47 assert_se(!condition_test(condition));
48 condition_free(condition);
50 condition = condition_new(CONDITION_PATH_EXISTS, "/thiscertainlywontexist", false, true);
51 assert_se(condition_test(condition));
52 condition_free(condition);
54 condition = condition_new(CONDITION_PATH_IS_DIRECTORY, "/bin", false, false);
55 assert_se(condition_test(condition));
56 condition_free(condition);
58 condition = condition_new(CONDITION_DIRECTORY_NOT_EMPTY, "/bin", false, false);
59 assert_se(condition_test(condition));
60 condition_free(condition);
62 condition = condition_new(CONDITION_FILE_NOT_EMPTY, "/bin/sh", false, false);
63 assert_se(condition_test(condition));
64 condition_free(condition);
66 condition = condition_new(CONDITION_FILE_IS_EXECUTABLE, "/bin/sh", false, false);
67 assert_se(condition_test(condition));
68 condition_free(condition);
70 condition = condition_new(CONDITION_FILE_IS_EXECUTABLE, "/etc/passwd", false, false);
71 assert_se(!condition_test(condition));
72 condition_free(condition);
74 condition = condition_new(CONDITION_PATH_IS_MOUNT_POINT, "/proc", false, false);
75 assert_se(condition_test(condition));
76 condition_free(condition);
78 condition = condition_new(CONDITION_PATH_IS_MOUNT_POINT, "/", false, false);
79 assert_se(condition_test(condition));
80 condition_free(condition);
82 condition = condition_new(CONDITION_PATH_IS_MOUNT_POINT, "/bin", false, false);
83 assert_se(!condition_test(condition));
84 condition_free(condition);
87 static void test_condition_test_ac_power(void) {
90 condition = condition_new(CONDITION_AC_POWER, "true", false, false);
91 assert_se(condition_test(condition) == on_ac_power());
92 condition_free(condition);
94 condition = condition_new(CONDITION_AC_POWER, "false", false, false);
95 assert_se(condition_test(condition) != on_ac_power());
96 condition_free(condition);
98 condition = condition_new(CONDITION_AC_POWER, "false", false, true);
99 assert_se(condition_test(condition) == on_ac_power());
100 condition_free(condition);
103 static void test_condition_test_host(void) {
104 Condition *condition;
107 char sid[SD_ID128_STRING_MAX];
108 _cleanup_free_ char *hostname = NULL;
110 r = sd_id128_get_machine(&id);
112 assert_se(sd_id128_to_string(id, sid));
114 condition = condition_new(CONDITION_HOST, sid, false, false);
115 assert_se(condition_test(condition));
116 condition_free(condition);
118 condition = condition_new(CONDITION_HOST, "garbage value jjjjjjjjjjjjjj", false, false);
119 assert_se(!condition_test(condition));
120 condition_free(condition);
122 condition = condition_new(CONDITION_HOST, sid, false, true);
123 assert_se(!condition_test(condition));
124 condition_free(condition);
126 hostname = gethostname_malloc();
129 condition = condition_new(CONDITION_HOST, hostname, false, false);
130 assert_se(condition_test(condition));
131 condition_free(condition);
134 static void test_condition_test_architecture(void) {
135 Condition *condition;
139 a = uname_architecture();
142 sa = architecture_to_string(a);
145 condition = condition_new(CONDITION_ARCHITECTURE, sa, false, false);
146 assert_se(condition_test(condition));
147 condition_free(condition);
149 condition = condition_new(CONDITION_ARCHITECTURE, "garbage value", false, false);
150 assert_se(condition_test(condition) < 0);
151 condition_free(condition);
153 condition = condition_new(CONDITION_ARCHITECTURE, sa, false, true);
154 assert_se(!condition_test(condition));
155 condition_free(condition);
158 static void test_condition_test_kernel_command_line(void) {
159 Condition *condition;
161 condition = condition_new(CONDITION_KERNEL_COMMAND_LINE, "thisreallyshouldntbeonthekernelcommandline", false, false);
162 assert_se(!condition_test(condition));
163 condition_free(condition);
165 condition = condition_new(CONDITION_KERNEL_COMMAND_LINE, "andthis=neither", false, false);
166 assert_se(!condition_test(condition));
167 condition_free(condition);
170 static void test_condition_test_null(void) {
171 Condition *condition;
173 condition = condition_new(CONDITION_NULL, NULL, false, false);
174 assert_se(condition_test(condition));
175 condition_free(condition);
177 condition = condition_new(CONDITION_NULL, NULL, false, true);
178 assert_se(!condition_test(condition));
179 condition_free(condition);
182 int main(int argc, char *argv[]) {
183 log_parse_environment();
186 test_condition_test_path_exists();
187 test_condition_test_ac_power();
188 test_condition_test_host();
189 test_condition_test_architecture();
190 test_condition_test_kernel_command_line();
191 test_condition_test_null();