chiark / gitweb /
1d74e1b011c143b5b7ddf51ca5425340defa4858
[elogind.git] / src / test / test-conf-parser.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2015 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 "conf-parser.h"
21 #include "log.h"
22 #include "macro.h"
23 #include "string-util.h"
24 #include "strv.h"
25 #include "util.h"
26
27 static void test_config_parse_path_one(const char *rvalue, const char *expected) {
28         char *path = NULL;
29
30         assert_se(config_parse_path("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &path, NULL) >= 0);
31         assert_se(streq_ptr(expected, path));
32
33         free(path);
34 }
35
36 static void test_config_parse_log_level_one(const char *rvalue, int expected) {
37         int log_level = 0;
38
39         assert_se(config_parse_log_level("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &log_level, NULL) >= 0);
40         assert_se(expected == log_level);
41 }
42
43 #if 0 /// UNNEEDED by elogind
44 static void test_config_parse_log_facility_one(const char *rvalue, int expected) {
45         int log_facility = 0;
46
47         assert_se(config_parse_log_facility("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &log_facility, NULL) >= 0);
48         assert_se(expected == log_facility);
49 }
50 #endif // 0
51
52 static void test_config_parse_iec_size_one(const char *rvalue, size_t expected) {
53         size_t iec_size = 0;
54
55         assert_se(config_parse_iec_size("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &iec_size, NULL) >= 0);
56         assert_se(expected == iec_size);
57 }
58
59 #if 0 /// UNNEEDED by elogind
60 static void test_config_parse_si_size_one(const char *rvalue, size_t expected) {
61         size_t si_size = 0;
62
63         assert_se(config_parse_si_size("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &si_size, NULL) >= 0);
64         assert_se(expected == si_size);
65 }
66 #endif // 0
67
68 static void test_config_parse_int_one(const char *rvalue, int expected) {
69         int v = -1;
70
71         assert_se(config_parse_int("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
72         assert_se(expected == v);
73 }
74
75 static void test_config_parse_unsigned_one(const char *rvalue, unsigned expected) {
76         unsigned v = 0;
77
78         assert_se(config_parse_unsigned("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
79         assert_se(expected == v);
80 }
81
82 static void test_config_parse_strv_one(const char *rvalue, char **expected) {
83         char **strv = 0;
84
85         assert_se(config_parse_strv("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &strv, NULL) >= 0);
86         assert_se(strv_equal(expected, strv));
87
88         strv_free(strv);
89 }
90
91 static void test_config_parse_mode_one(const char *rvalue, mode_t expected) {
92         mode_t v = 0;
93
94         assert_se(config_parse_mode("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
95         assert_se(expected == v);
96 }
97
98 static void test_config_parse_sec_one(const char *rvalue, usec_t expected) {
99         usec_t v = 0;
100
101         assert_se(config_parse_sec("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
102         assert_se(expected == v);
103 }
104
105 #if 0 /// UNNEEDED by elogind
106 static void test_config_parse_nsec_one(const char *rvalue, nsec_t expected) {
107         nsec_t v = 0;
108
109         assert_se(config_parse_nsec("unit", "filename", 1, "nsection", 1, "lvalue", 0, rvalue, &v, NULL) >= 0);
110         assert_se(expected == v);
111 }
112 #endif // 0
113
114 static void test_config_parse_path(void) {
115         test_config_parse_path_one("/path", "/path");
116         test_config_parse_path_one("/path//////////", "/path");
117         test_config_parse_path_one("///path/foo///bar////bar//", "/path/foo/bar/bar");
118
119         test_config_parse_path_one("not_absolute/path", NULL);
120 }
121
122 static void test_config_parse_log_level(void) {
123         test_config_parse_log_level_one("debug", LOG_DEBUG);
124         test_config_parse_log_level_one("info", LOG_INFO);
125
126         test_config_parse_log_level_one("garbage", 0);
127 }
128
129 #if 0 /// UNNEEDED by elogind
130 static void test_config_parse_log_facility(void) {
131         test_config_parse_log_facility_one("mail", LOG_MAIL);
132         test_config_parse_log_facility_one("user", LOG_USER);
133
134         test_config_parse_log_facility_one("garbage", 0);
135 }
136 #endif // 0
137
138 static void test_config_parse_iec_size(void) {
139         test_config_parse_iec_size_one("1024", 1024);
140         test_config_parse_iec_size_one("2K", 2048);
141         test_config_parse_iec_size_one("10M", 10 * 1024 * 1024);
142         test_config_parse_iec_size_one("1G", 1 * 1024 * 1024 * 1024);
143         test_config_parse_iec_size_one("0G", 0);
144         test_config_parse_iec_size_one("0", 0);
145
146         test_config_parse_iec_size_one("-982", 0);
147         test_config_parse_iec_size_one("49874444198739873000000G", 0);
148         test_config_parse_iec_size_one("garbage", 0);
149 }
150
151 #if 0 /// UNNEEDED by elogind
152 static void test_config_parse_si_size(void) {
153         test_config_parse_si_size_one("1024", 1024);
154         test_config_parse_si_size_one("2K", 2000);
155         test_config_parse_si_size_one("10M", 10 * 1000 * 1000);
156         test_config_parse_si_size_one("1G", 1 * 1000 * 1000 * 1000);
157         test_config_parse_si_size_one("0G", 0);
158         test_config_parse_si_size_one("0", 0);
159
160         test_config_parse_si_size_one("-982", 0);
161         test_config_parse_si_size_one("49874444198739873000000G", 0);
162         test_config_parse_si_size_one("garbage", 0);
163 }
164 #endif // 0
165
166 static void test_config_parse_int(void) {
167         test_config_parse_int_one("1024", 1024);
168         test_config_parse_int_one("-1024", -1024);
169         test_config_parse_int_one("0", 0);
170
171         test_config_parse_int_one("99999999999999999999999999999999999999999999999999999999", -1);
172         test_config_parse_int_one("-99999999999999999999999999999999999999999999999999999999", -1);
173         test_config_parse_int_one("1G", -1);
174         test_config_parse_int_one("garbage", -1);
175 }
176
177 static void test_config_parse_unsigned(void) {
178         test_config_parse_unsigned_one("10241024", 10241024);
179         test_config_parse_unsigned_one("1024", 1024);
180         test_config_parse_unsigned_one("0", 0);
181
182         test_config_parse_unsigned_one("99999999999999999999999999999999999999999999999999999999", 0);
183         test_config_parse_unsigned_one("1G", 0);
184         test_config_parse_unsigned_one("garbage", 0);
185         test_config_parse_unsigned_one("1000garbage", 0);
186 }
187
188 static void test_config_parse_strv(void) {
189         test_config_parse_strv_one("", STRV_MAKE_EMPTY);
190         test_config_parse_strv_one("foo", STRV_MAKE("foo"));
191         test_config_parse_strv_one("foo bar foo", STRV_MAKE("foo", "bar", "foo"));
192         test_config_parse_strv_one("\"foo bar\" foo", STRV_MAKE("foo bar", "foo"));
193 }
194
195 static void test_config_parse_mode(void) {
196         test_config_parse_mode_one("777", 0777);
197         test_config_parse_mode_one("644", 0644);
198
199         test_config_parse_mode_one("-777", 0);
200         test_config_parse_mode_one("999", 0);
201         test_config_parse_mode_one("garbage", 0);
202         test_config_parse_mode_one("777garbage", 0);
203         test_config_parse_mode_one("777 garbage", 0);
204 }
205
206 static void test_config_parse_sec(void) {
207         test_config_parse_sec_one("1", 1 * USEC_PER_SEC);
208         test_config_parse_sec_one("1s", 1 * USEC_PER_SEC);
209         test_config_parse_sec_one("100ms", 100 * USEC_PER_MSEC);
210         test_config_parse_sec_one("5min 20s", 5 * 60 * USEC_PER_SEC + 20 * USEC_PER_SEC);
211
212         test_config_parse_sec_one("-1", 0);
213         test_config_parse_sec_one("10foo", 0);
214         test_config_parse_sec_one("garbage", 0);
215 }
216
217 #if 0 /// UNNEEDED by elogind
218 static void test_config_parse_nsec(void) {
219         test_config_parse_nsec_one("1", 1);
220         test_config_parse_nsec_one("1s", 1 * NSEC_PER_SEC);
221         test_config_parse_nsec_one("100ms", 100 * NSEC_PER_MSEC);
222         test_config_parse_nsec_one("5min 20s", 5 * 60 * NSEC_PER_SEC + 20 * NSEC_PER_SEC);
223
224         test_config_parse_nsec_one("-1", 0);
225         test_config_parse_nsec_one("10foo", 0);
226         test_config_parse_nsec_one("garbage", 0);
227 }
228
229 static void test_config_parse_iec_uint64(void) {
230         uint64_t offset = 0;
231         assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4M", &offset, NULL) == 0);
232         assert_se(offset == 4 * 1024 * 1024);
233
234         assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4.5M", &offset, NULL) == 0);
235 }
236 #endif // 0
237
238 int main(int argc, char **argv) {
239         log_parse_environment();
240         log_open();
241
242         test_config_parse_path();
243         test_config_parse_log_level();
244 #if 0 /// UNNEEDED by elogind
245         test_config_parse_log_facility();
246 #endif // 0
247         test_config_parse_iec_size();
248 #if 0 /// UNNEEDED by elogind
249         test_config_parse_si_size();
250 #endif // 0
251         test_config_parse_int();
252         test_config_parse_unsigned();
253         test_config_parse_strv();
254         test_config_parse_mode();
255         test_config_parse_sec();
256 #if 0 /// UNNEEDED by elogind
257         test_config_parse_nsec();
258         test_config_parse_iec_uint64();
259 #endif // 0
260
261         return 0;
262 }