]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-conf-parser.c
2 This file is part of systemd.
4 Copyright 2015 Ronny Chevalier
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.
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.
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/>.
20 #include "conf-parser.h"
23 #include "string-util.h"
27 static void test_config_parse_path_one(const char *rvalue
, const char *expected
) {
30 assert_se(config_parse_path("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &path
, NULL
) >= 0);
31 assert_se(streq_ptr(expected
, path
));
36 static void test_config_parse_log_level_one(const char *rvalue
, int expected
) {
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
);
43 static void test_config_parse_log_facility_one(const char *rvalue
, int expected
) {
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
);
50 static void test_config_parse_iec_size_one(const char *rvalue
, size_t expected
) {
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
);
57 static void test_config_parse_si_size_one(const char *rvalue
, size_t expected
) {
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
);
64 static void test_config_parse_int_one(const char *rvalue
, int expected
) {
67 assert_se(config_parse_int("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &v
, NULL
) >= 0);
68 assert_se(expected
== v
);
71 static void test_config_parse_unsigned_one(const char *rvalue
, unsigned expected
) {
74 assert_se(config_parse_unsigned("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &v
, NULL
) >= 0);
75 assert_se(expected
== v
);
78 static void test_config_parse_strv_one(const char *rvalue
, char **expected
) {
81 assert_se(config_parse_strv("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &strv
, NULL
) >= 0);
82 assert_se(strv_equal(expected
, strv
));
87 static void test_config_parse_mode_one(const char *rvalue
, mode_t expected
) {
90 assert_se(config_parse_mode("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &v
, NULL
) >= 0);
91 assert_se(expected
== v
);
94 static void test_config_parse_sec_one(const char *rvalue
, usec_t expected
) {
97 assert_se(config_parse_sec("unit", "filename", 1, "section", 1, "lvalue", 0, rvalue
, &v
, NULL
) >= 0);
98 assert_se(expected
== v
);
101 static void test_config_parse_nsec_one(const char *rvalue
, nsec_t expected
) {
104 assert_se(config_parse_nsec("unit", "filename", 1, "nsection", 1, "lvalue", 0, rvalue
, &v
, NULL
) >= 0);
105 assert_se(expected
== v
);
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");
113 test_config_parse_path_one("not_absolute/path", NULL
);
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
);
120 test_config_parse_log_level_one("garbage", 0);
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
);
127 test_config_parse_log_facility_one("garbage", 0);
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);
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);
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);
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);
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);
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);
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);
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);
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"));
185 static void test_config_parse_mode(void) {
186 test_config_parse_mode_one("777", 0777);
187 test_config_parse_mode_one("644", 0644);
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);
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
);
202 test_config_parse_sec_one("-1", 0);
203 test_config_parse_sec_one("10foo", 0);
204 test_config_parse_sec_one("garbage", 0);
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
);
213 test_config_parse_nsec_one("-1", 0);
214 test_config_parse_nsec_one("10foo", 0);
215 test_config_parse_nsec_one("garbage", 0);
218 int main(int argc
, char **argv
) {
219 log_parse_environment();
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();