chiark / gitweb /
condition: unify condition logic in one file
[elogind.git] / src / test / test-condition-util.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 "condition-util.h"
21 #include "macro.h"
22 #include "util.h"
23 #include "log.h"
24 #include "architecture.h"
25 #include "sd-id128.h"
26
27 static void test_condition_test_path_exists(void) {
28         Condition *condition;
29
30         condition = condition_new(CONDITION_PATH_EXISTS, "/bin/sh", false, false);
31         assert_se(condition_test(condition));
32         condition_free(condition);
33
34         condition = condition_new(CONDITION_PATH_EXISTS, "/thiscertainlywontexist", false, false);
35         assert_se(!condition_test(condition));
36         condition_free(condition);
37
38         condition = condition_new(CONDITION_PATH_EXISTS, "/thiscertainlywontexist", false, true);
39         assert_se(condition_test(condition));
40         condition_free(condition);
41 }
42
43 static void test_condition_test_ac_power(void) {
44         Condition *condition;
45
46         condition = condition_new(CONDITION_AC_POWER, "true", false, false);
47         assert_se(condition_test(condition) == on_ac_power());
48         condition_free(condition);
49
50         condition = condition_new(CONDITION_AC_POWER, "false", false, false);
51         assert_se(condition_test(condition) != on_ac_power());
52         condition_free(condition);
53
54         condition = condition_new(CONDITION_AC_POWER, "false", false, true);
55         assert_se(condition_test(condition) == on_ac_power());
56         condition_free(condition);
57 }
58
59 static void test_condition_test_host(void) {
60         Condition *condition;
61         sd_id128_t id;
62         int r;
63         char sid[SD_ID128_STRING_MAX];
64         _cleanup_free_ char *hostname = NULL;
65
66         r = sd_id128_get_machine(&id);
67         assert_se(r >= 0);
68         assert_se(sd_id128_to_string(id, sid));
69
70         condition = condition_new(CONDITION_HOST, sid, false, false);
71         assert_se(condition_test(condition));
72         condition_free(condition);
73
74         condition = condition_new(CONDITION_HOST, "garbage value jjjjjjjjjjjjjj", false, false);
75         assert_se(!condition_test(condition));
76         condition_free(condition);
77
78         condition = condition_new(CONDITION_HOST, sid, false, true);
79         assert_se(!condition_test(condition));
80         condition_free(condition);
81
82         hostname = gethostname_malloc();
83         assert_se(hostname);
84
85         condition = condition_new(CONDITION_HOST, hostname, false, false);
86         assert_se(condition_test(condition));
87         condition_free(condition);
88 }
89
90 static void test_condition_test_architecture(void) {
91         Condition *condition;
92         const char *sa;
93         int a;
94
95         a = uname_architecture();
96         assert_se(a >= 0);
97
98         sa = architecture_to_string(a);
99         assert_se(sa);
100
101         condition = condition_new(CONDITION_ARCHITECTURE, sa, false, false);
102         assert_se(condition_test(condition));
103         condition_free(condition);
104
105         condition = condition_new(CONDITION_ARCHITECTURE, "garbage value", false, false);
106         assert_se(condition_test(condition) < 0);
107         condition_free(condition);
108
109         condition = condition_new(CONDITION_ARCHITECTURE, sa, false, true);
110         assert_se(!condition_test(condition));
111         condition_free(condition);
112 }
113
114 static void test_condition_test_kernel_command_line(void) {
115         Condition *condition;
116
117         condition = condition_new(CONDITION_KERNEL_COMMAND_LINE, "thisreallyshouldntbeonthekernelcommandline", false, false);
118         assert_se(!condition_test(condition));
119         condition_free(condition);
120
121         condition = condition_new(CONDITION_KERNEL_COMMAND_LINE, "andthis=neither", false, false);
122         assert_se(!condition_test(condition));
123         condition_free(condition);
124 }
125
126 int main(int argc, char *argv[]) {
127         log_parse_environment();
128         log_open();
129
130         test_condition_test_path_exists();
131         test_condition_test_ac_power();
132         test_condition_test_host();
133         test_condition_test_architecture();
134         test_condition_test_kernel_command_line();
135
136         return 0;
137 }