]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-condition-util.c
journal: adjust audit log messages a bit
[thirdparty/systemd.git] / src / test / test-condition-util.c
CommitLineData
b08f2be6
RC
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"
d1bddcec
LP
25#include "sd-id128.h"
26
27static 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}
b08f2be6
RC
42
43static void test_condition_test_ac_power(void) {
44 Condition *condition;
45
46 condition = condition_new(CONDITION_AC_POWER, "true", false, false);
d1bddcec 47 assert_se(condition_test(condition) == on_ac_power());
b08f2be6
RC
48 condition_free(condition);
49
50 condition = condition_new(CONDITION_AC_POWER, "false", false, false);
d1bddcec 51 assert_se(condition_test(condition) != on_ac_power());
b08f2be6
RC
52 condition_free(condition);
53
54 condition = condition_new(CONDITION_AC_POWER, "false", false, true);
d1bddcec 55 assert_se(condition_test(condition) == on_ac_power());
b08f2be6
RC
56 condition_free(condition);
57}
58
59static void test_condition_test_host(void) {
60 Condition *condition;
61 sd_id128_t id;
62 int r;
63 char sid[SD_ID128_STRING_MAX];
76082570 64 _cleanup_free_ char *hostname = NULL;
b08f2be6
RC
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);
d1bddcec 71 assert_se(condition_test(condition));
b08f2be6
RC
72 condition_free(condition);
73
74 condition = condition_new(CONDITION_HOST, "garbage value jjjjjjjjjjjjjj", false, false);
d1bddcec 75 assert_se(!condition_test(condition));
b08f2be6
RC
76 condition_free(condition);
77
78 condition = condition_new(CONDITION_HOST, sid, false, true);
d1bddcec 79 assert_se(!condition_test(condition));
b08f2be6
RC
80 condition_free(condition);
81
82 hostname = gethostname_malloc();
83 assert_se(hostname);
84
85 condition = condition_new(CONDITION_HOST, hostname, false, false);
d1bddcec 86 assert_se(condition_test(condition));
b08f2be6
RC
87 condition_free(condition);
88}
89
90static void test_condition_test_architecture(void) {
91 Condition *condition;
b08f2be6 92 const char *sa;
592fd144 93 int a;
b08f2be6
RC
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);
d1bddcec 102 assert_se(condition_test(condition));
b08f2be6
RC
103 condition_free(condition);
104
105 condition = condition_new(CONDITION_ARCHITECTURE, "garbage value", false, false);
d1bddcec 106 assert_se(condition_test(condition) < 0);
b08f2be6
RC
107 condition_free(condition);
108
109 condition = condition_new(CONDITION_ARCHITECTURE, sa, false, true);
d1bddcec 110 assert_se(!condition_test(condition));
b08f2be6
RC
111 condition_free(condition);
112}
113
07318c29
LP
114static void test_condition_test_kernel_command_line(void) {
115 Condition *condition;
116
117 condition = condition_new(CONDITION_KERNEL_COMMAND_LINE, "thisreallyshouldntbeonthekernelcommandline", false, false);
d1bddcec 118 assert_se(!condition_test(condition));
07318c29
LP
119 condition_free(condition);
120
121 condition = condition_new(CONDITION_KERNEL_COMMAND_LINE, "andthis=neither", false, false);
d1bddcec 122 assert_se(!condition_test(condition));
07318c29
LP
123 condition_free(condition);
124}
125
b08f2be6
RC
126int main(int argc, char *argv[]) {
127 log_parse_environment();
128 log_open();
129
d1bddcec 130 test_condition_test_path_exists();
b08f2be6
RC
131 test_condition_test_ac_power();
132 test_condition_test_host();
133 test_condition_test_architecture();
07318c29 134 test_condition_test_kernel_command_line();
b08f2be6
RC
135
136 return 0;
137}