chiark / gitweb /
hashmap: allow hashmap_move() to fail
[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 "systemd/sd-id128.h"
26
27 static void test_condition_test_ac_power(void) {
28         Condition *condition;
29
30         condition = condition_new(CONDITION_AC_POWER, "true", false, false);
31         assert_se(condition_test_ac_power(condition) == on_ac_power());
32         condition_free(condition);
33
34         condition = condition_new(CONDITION_AC_POWER, "false", false, false);
35         assert_se(condition_test_ac_power(condition) != on_ac_power());
36         condition_free(condition);
37
38         condition = condition_new(CONDITION_AC_POWER, "false", false, true);
39         assert_se(condition_test_ac_power(condition) == on_ac_power());
40         condition_free(condition);
41 }
42
43 static void test_condition_test_host(void) {
44         Condition *condition;
45         sd_id128_t id;
46         int r;
47         char sid[SD_ID128_STRING_MAX];
48         _cleanup_free_ char *hostname = NULL;
49
50         r = sd_id128_get_machine(&id);
51         assert_se(r >= 0);
52         assert_se(sd_id128_to_string(id, sid));
53
54         condition = condition_new(CONDITION_HOST, sid, false, false);
55         assert_se(condition_test_host(condition));
56         condition_free(condition);
57
58         condition = condition_new(CONDITION_HOST, "garbage value jjjjjjjjjjjjjj", false, false);
59         assert_se(!condition_test_host(condition));
60         condition_free(condition);
61
62         condition = condition_new(CONDITION_HOST, sid, false, true);
63         assert_se(!condition_test_host(condition));
64         condition_free(condition);
65
66         hostname = gethostname_malloc();
67         assert_se(hostname);
68
69         condition = condition_new(CONDITION_HOST, hostname, false, false);
70         assert_se(condition_test_host(condition));
71         condition_free(condition);
72 }
73
74 static void test_condition_test_architecture(void) {
75         Condition *condition;
76         Architecture a;
77         const char *sa;
78
79         a = uname_architecture();
80         assert_se(a >= 0);
81
82         sa = architecture_to_string(a);
83         assert_se(sa);
84
85         condition = condition_new(CONDITION_ARCHITECTURE, sa, false, false);
86         assert_se(condition_test_architecture(condition));
87         condition_free(condition);
88
89         condition = condition_new(CONDITION_ARCHITECTURE, "garbage value", false, false);
90         assert_se(!condition_test_architecture(condition));
91         condition_free(condition);
92
93         condition = condition_new(CONDITION_ARCHITECTURE, sa, false, true);
94         assert_se(!condition_test_architecture(condition));
95         condition_free(condition);
96 }
97
98 int main(int argc, char *argv[]) {
99         log_parse_environment();
100         log_open();
101
102         test_condition_test_ac_power();
103         test_condition_test_host();
104         test_condition_test_architecture();
105
106         return 0;
107 }