]>
Commit | Line | Data |
---|---|---|
0763adbe RC |
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" | |
07630cea | 21 | #include "log.h" |
0763adbe | 22 | #include "macro.h" |
07630cea | 23 | #include "string-util.h" |
0763adbe | 24 | #include "strv.h" |
07630cea | 25 | #include "util.h" |
0763adbe RC |
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 | static void test_config_parse_log_facility_one(const char *rvalue, int expected) { | |
44 | int log_facility = 0; | |
45 | ||
46 | assert_se(config_parse_log_facility("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &log_facility, NULL) >= 0); | |
47 | assert_se(expected == log_facility); | |
48 | } | |
49 | ||
50 | static void test_config_parse_iec_size_one(const char *rvalue, size_t expected) { | |
51 | size_t iec_size = 0; | |
52 | ||
53 | assert_se(config_parse_iec_size("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &iec_size, NULL) >= 0); | |
54 | assert_se(expected == iec_size); | |
55 | } | |
56 | ||
57 | static void test_config_parse_si_size_one(const char *rvalue, size_t expected) { | |
58 | size_t si_size = 0; | |
59 | ||
60 | assert_se(config_parse_si_size("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &si_size, NULL) >= 0); | |
61 | assert_se(expected == si_size); | |
62 | } | |
63 | ||
64 | static void test_config_parse_int_one(const char *rvalue, int expected) { | |
65 | int v = -1; | |
66 | ||
67 | assert_se(config_parse_int("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0); | |
68 | assert_se(expected == v); | |
69 | } | |
70 | ||
71 | static void test_config_parse_unsigned_one(const char *rvalue, unsigned expected) { | |
72 | unsigned v = 0; | |
73 | ||
74 | assert_se(config_parse_unsigned("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0); | |
75 | assert_se(expected == v); | |
76 | } | |
77 | ||
78 | static void test_config_parse_strv_one(const char *rvalue, char **expected) { | |
79 | char **strv = 0; | |
80 | ||
81 | assert_se(config_parse_strv("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &strv, NULL) >= 0); | |
82 | assert_se(strv_equal(expected, strv)); | |
83 | ||
84 | strv_free(strv); | |
85 | } | |
86 | ||
87 | static void test_config_parse_mode_one(const char *rvalue, mode_t expected) { | |
88 | mode_t v = 0; | |
89 | ||
90 | assert_se(config_parse_mode("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0); | |
91 | assert_se(expected == v); | |
92 | } | |
93 | ||
94 | static void test_config_parse_sec_one(const char *rvalue, usec_t expected) { | |
95 | usec_t v = 0; | |
96 | ||
97 | assert_se(config_parse_sec("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue, &v, NULL) >= 0); | |
98 | assert_se(expected == v); | |
99 | } | |
100 | ||
101 | static void test_config_parse_nsec_one(const char *rvalue, nsec_t expected) { | |
102 | nsec_t v = 0; | |
103 | ||
104 | assert_se(config_parse_nsec("unit", "filename", 1, "nsection", 1, "lvalue", 0, rvalue, &v, NULL) >= 0); | |
105 | assert_se(expected == v); | |
106 | } | |
107 | ||
108 | static void test_config_parse_path(void) { | |
109 | test_config_parse_path_one("/path", "/path"); | |
110 | test_config_parse_path_one("/path//////////", "/path"); | |
111 | test_config_parse_path_one("///path/foo///bar////bar//", "/path/foo/bar/bar"); | |
112 | ||
113 | test_config_parse_path_one("not_absolute/path", NULL); | |
114 | } | |
115 | ||
116 | static void test_config_parse_log_level(void) { | |
117 | test_config_parse_log_level_one("debug", LOG_DEBUG); | |
118 | test_config_parse_log_level_one("info", LOG_INFO); | |
119 | ||
120 | test_config_parse_log_level_one("garbage", 0); | |
121 | } | |
122 | ||
123 | static void test_config_parse_log_facility(void) { | |
124 | test_config_parse_log_facility_one("mail", LOG_MAIL); | |
125 | test_config_parse_log_facility_one("user", LOG_USER); | |
126 | ||
127 | test_config_parse_log_facility_one("garbage", 0); | |
128 | } | |
129 | ||
130 | static void test_config_parse_iec_size(void) { | |
131 | test_config_parse_iec_size_one("1024", 1024); | |
132 | test_config_parse_iec_size_one("2K", 2048); | |
133 | test_config_parse_iec_size_one("10M", 10 * 1024 * 1024); | |
134 | test_config_parse_iec_size_one("1G", 1 * 1024 * 1024 * 1024); | |
135 | test_config_parse_iec_size_one("0G", 0); | |
136 | test_config_parse_iec_size_one("0", 0); | |
137 | ||
138 | test_config_parse_iec_size_one("-982", 0); | |
139 | test_config_parse_iec_size_one("49874444198739873000000G", 0); | |
140 | test_config_parse_iec_size_one("garbage", 0); | |
141 | } | |
142 | ||
143 | static void test_config_parse_si_size(void) { | |
144 | test_config_parse_si_size_one("1024", 1024); | |
145 | test_config_parse_si_size_one("2K", 2000); | |
146 | test_config_parse_si_size_one("10M", 10 * 1000 * 1000); | |
147 | test_config_parse_si_size_one("1G", 1 * 1000 * 1000 * 1000); | |
148 | test_config_parse_si_size_one("0G", 0); | |
149 | test_config_parse_si_size_one("0", 0); | |
150 | ||
151 | test_config_parse_si_size_one("-982", 0); | |
152 | test_config_parse_si_size_one("49874444198739873000000G", 0); | |
153 | test_config_parse_si_size_one("garbage", 0); | |
154 | } | |
155 | ||
156 | static void test_config_parse_int(void) { | |
157 | test_config_parse_int_one("1024", 1024); | |
158 | test_config_parse_int_one("-1024", -1024); | |
159 | test_config_parse_int_one("0", 0); | |
160 | ||
161 | test_config_parse_int_one("99999999999999999999999999999999999999999999999999999999", -1); | |
162 | test_config_parse_int_one("-99999999999999999999999999999999999999999999999999999999", -1); | |
163 | test_config_parse_int_one("1G", -1); | |
164 | test_config_parse_int_one("garbage", -1); | |
165 | } | |
166 | ||
167 | static void test_config_parse_unsigned(void) { | |
168 | test_config_parse_unsigned_one("10241024", 10241024); | |
169 | test_config_parse_unsigned_one("1024", 1024); | |
170 | test_config_parse_unsigned_one("0", 0); | |
171 | ||
172 | test_config_parse_unsigned_one("99999999999999999999999999999999999999999999999999999999", 0); | |
173 | test_config_parse_unsigned_one("1G", 0); | |
174 | test_config_parse_unsigned_one("garbage", 0); | |
175 | test_config_parse_unsigned_one("1000garbage", 0); | |
176 | } | |
177 | ||
178 | static void test_config_parse_strv(void) { | |
179 | test_config_parse_strv_one("", STRV_MAKE_EMPTY); | |
180 | test_config_parse_strv_one("foo", STRV_MAKE("foo")); | |
181 | test_config_parse_strv_one("foo bar foo", STRV_MAKE("foo", "bar", "foo")); | |
182 | test_config_parse_strv_one("\"foo bar\" foo", STRV_MAKE("foo bar", "foo")); | |
183 | } | |
184 | ||
185 | static void test_config_parse_mode(void) { | |
186 | test_config_parse_mode_one("777", 0777); | |
187 | test_config_parse_mode_one("644", 0644); | |
188 | ||
189 | test_config_parse_mode_one("-777", 0); | |
190 | test_config_parse_mode_one("999", 0); | |
191 | test_config_parse_mode_one("garbage", 0); | |
192 | test_config_parse_mode_one("777garbage", 0); | |
193 | test_config_parse_mode_one("777 garbage", 0); | |
194 | } | |
195 | ||
196 | static void test_config_parse_sec(void) { | |
197 | test_config_parse_sec_one("1", 1 * USEC_PER_SEC); | |
198 | test_config_parse_sec_one("1s", 1 * USEC_PER_SEC); | |
199 | test_config_parse_sec_one("100ms", 100 * USEC_PER_MSEC); | |
200 | test_config_parse_sec_one("5min 20s", 5 * 60 * USEC_PER_SEC + 20 * USEC_PER_SEC); | |
201 | ||
202 | test_config_parse_sec_one("-1", 0); | |
203 | test_config_parse_sec_one("10foo", 0); | |
204 | test_config_parse_sec_one("garbage", 0); | |
205 | } | |
206 | ||
207 | static void test_config_parse_nsec(void) { | |
208 | test_config_parse_nsec_one("1", 1); | |
209 | test_config_parse_nsec_one("1s", 1 * NSEC_PER_SEC); | |
210 | test_config_parse_nsec_one("100ms", 100 * NSEC_PER_MSEC); | |
211 | test_config_parse_nsec_one("5min 20s", 5 * 60 * NSEC_PER_SEC + 20 * NSEC_PER_SEC); | |
212 | ||
213 | test_config_parse_nsec_one("-1", 0); | |
214 | test_config_parse_nsec_one("10foo", 0); | |
215 | test_config_parse_nsec_one("garbage", 0); | |
216 | } | |
217 | ||
218 | int main(int argc, char **argv) { | |
219 | log_parse_environment(); | |
220 | log_open(); | |
221 | ||
222 | test_config_parse_path(); | |
223 | test_config_parse_log_level(); | |
224 | test_config_parse_log_facility(); | |
225 | test_config_parse_iec_size(); | |
226 | test_config_parse_si_size(); | |
227 | test_config_parse_int(); | |
228 | test_config_parse_unsigned(); | |
229 | test_config_parse_strv(); | |
230 | test_config_parse_mode(); | |
231 | test_config_parse_sec(); | |
232 | test_config_parse_nsec(); | |
233 | ||
234 | return 0; | |
235 | } |