]>
Commit | Line | Data |
---|---|---|
539ad707 TA |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | Copyright 2013 Thomas H.P. Andersen | |
8 | ||
9 | systemd is free software; you can redistribute it and/or modify it | |
10 | under the terms of the GNU Lesser General Public License as published by | |
11 | the Free Software Foundation; either version 2.1 of the License, or | |
12 | (at your option) any later version. | |
13 | ||
14 | systemd is distributed in the hope that it will be useful, but | |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | Lesser General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU Lesser General Public License | |
20 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
21 | ***/ | |
22 | ||
23 | #include <string.h> | |
dbd73f9e TA |
24 | #include <unistd.h> |
25 | #include <fcntl.h> | |
d6dd604b | 26 | #include <locale.h> |
2a371001 | 27 | #include <errno.h> |
539ad707 TA |
28 | |
29 | #include "util.h" | |
893fa014 | 30 | #include "strv.h" |
65b3903f ZJS |
31 | #include "def.h" |
32 | #include "fileio.h" | |
9480794b | 33 | #include "conf-parser.h" |
539ad707 TA |
34 | |
35 | static void test_streq_ptr(void) { | |
8354c34e TA |
36 | assert_se(streq_ptr(NULL, NULL)); |
37 | assert_se(!streq_ptr("abc", "cdef")); | |
539ad707 TA |
38 | } |
39 | ||
40 | static void test_first_word(void) { | |
8354c34e TA |
41 | assert_se(first_word("Hello", "")); |
42 | assert_se(first_word("Hello", "Hello")); | |
43 | assert_se(first_word("Hello world", "Hello")); | |
44 | assert_se(first_word("Hello\tworld", "Hello")); | |
45 | assert_se(first_word("Hello\nworld", "Hello")); | |
46 | assert_se(first_word("Hello\rworld", "Hello")); | |
47 | assert_se(first_word("Hello ", "Hello")); | |
48 | ||
49 | assert_se(!first_word("Hello", "Hellooo")); | |
50 | assert_se(!first_word("Hello", "xxxxx")); | |
51 | assert_se(!first_word("Hellooo", "Hello")); | |
52 | } | |
53 | ||
dbd73f9e TA |
54 | static void test_close_many(void) { |
55 | int fds[3]; | |
56 | char name0[] = "/tmp/test-close-many.XXXXXX"; | |
57 | char name1[] = "/tmp/test-close-many.XXXXXX"; | |
58 | char name2[] = "/tmp/test-close-many.XXXXXX"; | |
59 | ||
2d5bdf5b LP |
60 | fds[0] = mkostemp_safe(name0, O_RDWR|O_CLOEXEC); |
61 | fds[1] = mkostemp_safe(name1, O_RDWR|O_CLOEXEC); | |
62 | fds[2] = mkostemp_safe(name2, O_RDWR|O_CLOEXEC); | |
dbd73f9e TA |
63 | |
64 | close_many(fds, 2); | |
65 | ||
66 | assert_se(fcntl(fds[0], F_GETFD) == -1); | |
67 | assert_se(fcntl(fds[1], F_GETFD) == -1); | |
68 | assert_se(fcntl(fds[2], F_GETFD) >= 0); | |
69 | ||
03e334a1 | 70 | safe_close(fds[2]); |
dbd73f9e TA |
71 | |
72 | unlink(name0); | |
73 | unlink(name1); | |
74 | unlink(name2); | |
75 | } | |
76 | ||
8354c34e TA |
77 | static void test_parse_boolean(void) { |
78 | assert_se(parse_boolean("1") == 1); | |
79 | assert_se(parse_boolean("y") == 1); | |
80 | assert_se(parse_boolean("Y") == 1); | |
81 | assert_se(parse_boolean("yes") == 1); | |
82 | assert_se(parse_boolean("YES") == 1); | |
83 | assert_se(parse_boolean("true") == 1); | |
84 | assert_se(parse_boolean("TRUE") == 1); | |
85 | assert_se(parse_boolean("on") == 1); | |
86 | assert_se(parse_boolean("ON") == 1); | |
87 | ||
88 | assert_se(parse_boolean("0") == 0); | |
89 | assert_se(parse_boolean("n") == 0); | |
90 | assert_se(parse_boolean("N") == 0); | |
91 | assert_se(parse_boolean("no") == 0); | |
92 | assert_se(parse_boolean("NO") == 0); | |
93 | assert_se(parse_boolean("false") == 0); | |
94 | assert_se(parse_boolean("FALSE") == 0); | |
95 | assert_se(parse_boolean("off") == 0); | |
96 | assert_se(parse_boolean("OFF") == 0); | |
97 | ||
98 | assert_se(parse_boolean("garbage") < 0); | |
99 | assert_se(parse_boolean("") < 0); | |
539ad707 TA |
100 | } |
101 | ||
8d99e5f5 TA |
102 | static void test_parse_pid(void) { |
103 | int r; | |
104 | pid_t pid; | |
105 | ||
106 | r = parse_pid("100", &pid); | |
107 | assert_se(r == 0); | |
108 | assert_se(pid == 100); | |
109 | ||
110 | r = parse_pid("0x7FFFFFFF", &pid); | |
111 | assert_se(r == 0); | |
112 | assert_se(pid == 2147483647); | |
113 | ||
114 | pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */ | |
115 | r = parse_pid("0", &pid); | |
116 | assert_se(r == -ERANGE); | |
117 | assert_se(pid == 65); | |
118 | ||
119 | pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */ | |
120 | r = parse_pid("-100", &pid); | |
121 | assert_se(r == -ERANGE); | |
122 | assert_se(pid == 65); | |
123 | ||
124 | pid = 65; /* pid is left unchanged on ERANGE. Set to known arbitrary value. */ | |
125 | r = parse_pid("0xFFFFFFFFFFFFFFFFF", &pid); | |
126 | assert(r == -ERANGE); | |
127 | assert_se(pid == 65); | |
128 | } | |
129 | ||
130 | static void test_parse_uid(void) { | |
131 | int r; | |
132 | uid_t uid; | |
133 | ||
134 | r = parse_uid("100", &uid); | |
135 | assert_se(r == 0); | |
136 | assert_se(uid == 100); | |
137 | } | |
138 | ||
139 | static void test_safe_atolli(void) { | |
140 | int r; | |
141 | long long l; | |
142 | ||
143 | r = safe_atolli("12345", &l); | |
144 | assert_se(r == 0); | |
145 | assert_se(l == 12345); | |
146 | ||
147 | r = safe_atolli("junk", &l); | |
148 | assert_se(r == -EINVAL); | |
149 | } | |
150 | ||
151 | static void test_safe_atod(void) { | |
152 | int r; | |
153 | double d; | |
d6dd604b LP |
154 | char *e; |
155 | ||
156 | r = safe_atod("junk", &d); | |
157 | assert_se(r == -EINVAL); | |
8d99e5f5 TA |
158 | |
159 | r = safe_atod("0.2244", &d); | |
160 | assert_se(r == 0); | |
161 | assert_se(abs(d - 0.2244) < 0.000001); | |
162 | ||
d6dd604b | 163 | r = safe_atod("0,5", &d); |
8d99e5f5 | 164 | assert_se(r == -EINVAL); |
d6dd604b LP |
165 | |
166 | errno = 0; | |
167 | strtod("0,5", &e); | |
168 | assert_se(*e == ','); | |
169 | ||
170 | /* Check if this really is locale independent */ | |
171 | setlocale(LC_NUMERIC, "de_DE.utf8"); | |
172 | ||
173 | r = safe_atod("0.2244", &d); | |
174 | assert_se(r == 0); | |
175 | assert_se(abs(d - 0.2244) < 0.000001); | |
176 | ||
177 | r = safe_atod("0,5", &d); | |
178 | assert_se(r == -EINVAL); | |
179 | ||
180 | errno = 0; | |
181 | assert_se(abs(strtod("0,5", &e) - 0.5) < 0.00001); | |
182 | ||
183 | /* And check again, reset */ | |
184 | setlocale(LC_NUMERIC, "C"); | |
185 | ||
186 | r = safe_atod("0.2244", &d); | |
187 | assert_se(r == 0); | |
188 | assert_se(abs(d - 0.2244) < 0.000001); | |
189 | ||
190 | r = safe_atod("0,5", &d); | |
191 | assert_se(r == -EINVAL); | |
192 | ||
193 | errno = 0; | |
194 | strtod("0,5", &e); | |
195 | assert_se(*e == ','); | |
8d99e5f5 TA |
196 | } |
197 | ||
dbd73f9e | 198 | static void test_strappend(void) { |
998b087f | 199 | _cleanup_free_ char *t1, *t2, *t3, *t4; |
dbd73f9e | 200 | |
998b087f TA |
201 | t1 = strappend(NULL, NULL); |
202 | assert_se(streq(t1, "")); | |
dbd73f9e | 203 | |
998b087f TA |
204 | t2 = strappend(NULL, "suf"); |
205 | assert_se(streq(t2, "suf")); | |
dbd73f9e | 206 | |
998b087f TA |
207 | t3 = strappend("pre", NULL); |
208 | assert_se(streq(t3, "pre")); | |
dbd73f9e | 209 | |
998b087f TA |
210 | t4 = strappend("pre", "suf"); |
211 | assert_se(streq(t4, "presuf")); | |
dbd73f9e TA |
212 | } |
213 | ||
1ef04f0b | 214 | static void test_strstrip(void) { |
998b087f TA |
215 | char *r; |
216 | char input[] = " hello, waldo. "; | |
1ef04f0b | 217 | |
998b087f TA |
218 | r = strstrip(input); |
219 | assert_se(streq(r, "hello, waldo.")); | |
1ef04f0b TA |
220 | } |
221 | ||
222 | static void test_delete_chars(void) { | |
998b087f TA |
223 | char *r; |
224 | char input[] = " hello, waldo. abc"; | |
1ef04f0b | 225 | |
998b087f TA |
226 | r = delete_chars(input, WHITESPACE); |
227 | assert_se(streq(r, "hello,waldo.abc")); | |
1ef04f0b TA |
228 | } |
229 | ||
230 | static void test_in_charset(void) { | |
998b087f TA |
231 | assert_se(in_charset("dddaaabbbcccc", "abcd")); |
232 | assert_se(!in_charset("dddaaabbbcccc", "abc f")); | |
1ef04f0b TA |
233 | } |
234 | ||
44f4c86c DB |
235 | static void test_hexchar(void) { |
236 | assert_se(hexchar(0xa) == 'a'); | |
237 | assert_se(hexchar(0x0) == '0'); | |
238 | } | |
239 | ||
240 | static void test_unhexchar(void) { | |
241 | assert_se(unhexchar('a') == 0xA); | |
242 | assert_se(unhexchar('A') == 0xA); | |
243 | assert_se(unhexchar('0') == 0x0); | |
244 | } | |
245 | ||
246 | static void test_octchar(void) { | |
247 | assert_se(octchar(00) == '0'); | |
248 | assert_se(octchar(07) == '7'); | |
249 | } | |
250 | ||
251 | static void test_unoctchar(void) { | |
252 | assert_se(unoctchar('0') == 00); | |
253 | assert_se(unoctchar('7') == 07); | |
254 | } | |
255 | ||
256 | static void test_decchar(void) { | |
257 | assert_se(decchar(0) == '0'); | |
258 | assert_se(decchar(9) == '9'); | |
259 | } | |
260 | ||
261 | static void test_undecchar(void) { | |
262 | assert_se(undecchar('0') == 0); | |
263 | assert_se(undecchar('9') == 9); | |
264 | } | |
265 | ||
b4ecc959 TA |
266 | static void test_cescape(void) { |
267 | _cleanup_free_ char *escaped; | |
268 | escaped = cescape("abc\\\"\b\f\n\r\t\v\003\177\234\313"); | |
269 | assert_se(streq(escaped, "abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\003\\177\\234\\313")); | |
270 | } | |
271 | ||
272 | static void test_cunescape(void) { | |
273 | _cleanup_free_ char *unescaped; | |
274 | unescaped = cunescape("abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\003\\177\\234\\313"); | |
275 | assert_se(streq(unescaped, "abc\\\"\b\f\n\r\t\v\003\177\234\313")); | |
276 | } | |
277 | ||
1ef04f0b TA |
278 | static void test_foreach_word(void) { |
279 | char *w, *state; | |
280 | size_t l; | |
281 | int i = 0; | |
282 | const char test[] = "test abc d\te f "; | |
283 | const char * const expected[] = { | |
284 | "test", | |
285 | "abc", | |
286 | "d", | |
287 | "e", | |
288 | "f", | |
289 | "", | |
290 | NULL | |
291 | }; | |
292 | ||
293 | FOREACH_WORD(w, l, test, state) { | |
294 | assert_se(strneq(expected[i++], w, l)); | |
295 | } | |
296 | } | |
297 | ||
539ad707 TA |
298 | static void test_foreach_word_quoted(void) { |
299 | char *w, *state; | |
300 | size_t l; | |
1ef04f0b TA |
301 | int i = 0; |
302 | const char test[] = "test a b c 'd' e '' '' hhh '' '' \"a b c\""; | |
303 | const char * const expected[] = { | |
304 | "test", | |
305 | "a", | |
306 | "b", | |
307 | "c", | |
308 | "d", | |
309 | "e", | |
310 | "", | |
311 | "", | |
312 | "hhh", | |
313 | "", | |
314 | "", | |
315 | "a b c", | |
316 | NULL | |
317 | }; | |
318 | ||
539ad707 TA |
319 | printf("<%s>\n", test); |
320 | FOREACH_WORD_QUOTED(w, l, test, state) { | |
1ef04f0b | 321 | _cleanup_free_ char *t = NULL; |
539ad707 TA |
322 | |
323 | assert_se(t = strndup(w, l)); | |
1ef04f0b | 324 | assert_se(strneq(expected[i++], w, l)); |
539ad707 | 325 | printf("<%s>\n", t); |
539ad707 TA |
326 | } |
327 | } | |
328 | ||
329 | static void test_default_term_for_tty(void) { | |
330 | puts(default_term_for_tty("/dev/tty23")); | |
331 | puts(default_term_for_tty("/dev/ttyS23")); | |
332 | puts(default_term_for_tty("/dev/tty0")); | |
333 | puts(default_term_for_tty("/dev/pty0")); | |
334 | puts(default_term_for_tty("/dev/pts/0")); | |
335 | puts(default_term_for_tty("/dev/console")); | |
336 | puts(default_term_for_tty("tty23")); | |
337 | puts(default_term_for_tty("ttyS23")); | |
338 | puts(default_term_for_tty("tty0")); | |
339 | puts(default_term_for_tty("pty0")); | |
340 | puts(default_term_for_tty("pts/0")); | |
341 | puts(default_term_for_tty("console")); | |
342 | } | |
343 | ||
0d585d82 TA |
344 | static void test_memdup_multiply(void) { |
345 | int org[] = {1, 2, 3}; | |
346 | int *dup; | |
347 | ||
348 | dup = (int*)memdup_multiply(org, sizeof(int), 3); | |
349 | ||
350 | assert_se(dup); | |
351 | assert_se(dup[0] == 1); | |
352 | assert_se(dup[1] == 2); | |
353 | assert_se(dup[2] == 3); | |
354 | free(dup); | |
355 | } | |
356 | ||
aa3c5cf8 LP |
357 | static void test_hostname_is_valid(void) { |
358 | assert(hostname_is_valid("foobar")); | |
359 | assert(hostname_is_valid("foobar.com")); | |
360 | assert(!hostname_is_valid("fööbar")); | |
361 | assert(!hostname_is_valid("")); | |
362 | assert(!hostname_is_valid(".")); | |
363 | assert(!hostname_is_valid("..")); | |
364 | assert(!hostname_is_valid("foobar.")); | |
365 | assert(!hostname_is_valid(".foobar")); | |
366 | assert(!hostname_is_valid("foo..bar")); | |
367 | assert(!hostname_is_valid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")); | |
368 | } | |
369 | ||
144e51ec CR |
370 | static void test_u64log2(void) { |
371 | assert(u64log2(0) == 0); | |
372 | assert(u64log2(8) == 3); | |
373 | assert(u64log2(9) == 3); | |
374 | assert(u64log2(15) == 3); | |
375 | assert(u64log2(16) == 4); | |
376 | assert(u64log2(1024*1024) == 20); | |
377 | assert(u64log2(1024*1024+5) == 20); | |
378 | } | |
379 | ||
49aa47c7 | 380 | static void test_get_process_comm(void) { |
143bfdaf | 381 | struct stat st; |
49aa47c7 LP |
382 | _cleanup_free_ char *a = NULL, *c = NULL, *d = NULL, *f = NULL, *i = NULL; |
383 | unsigned long long b; | |
384 | pid_t e; | |
385 | uid_t u; | |
386 | gid_t g; | |
387 | dev_t h; | |
388 | int r; | |
389 | ||
143bfdaf HHPF |
390 | if (stat("/proc/1/comm", &st) == 0) { |
391 | assert_se(get_process_comm(1, &a) >= 0); | |
392 | log_info("pid1 comm: '%s'", a); | |
393 | } else { | |
394 | log_warning("/proc/1/comm does not exist."); | |
395 | } | |
49aa47c7 LP |
396 | |
397 | assert_se(get_starttime_of_pid(1, &b) >= 0); | |
398 | log_info("pid1 starttime: '%llu'", b); | |
399 | ||
400 | assert_se(get_process_cmdline(1, 0, true, &c) >= 0); | |
401 | log_info("pid1 cmdline: '%s'", c); | |
402 | ||
403 | assert_se(get_process_cmdline(1, 8, false, &d) >= 0); | |
404 | log_info("pid1 cmdline truncated: '%s'", d); | |
405 | ||
406 | assert_se(get_parent_of_pid(1, &e) >= 0); | |
407 | log_info("pid1 ppid: '%llu'", (unsigned long long) e); | |
408 | assert_se(e == 0); | |
409 | ||
410 | assert_se(is_kernel_thread(1) == 0); | |
411 | ||
412 | r = get_process_exe(1, &f); | |
413 | assert_se(r >= 0 || r == -EACCES); | |
414 | log_info("pid1 exe: '%s'", strna(f)); | |
415 | ||
416 | assert_se(get_process_uid(1, &u) == 0); | |
417 | log_info("pid1 uid: '%llu'", (unsigned long long) u); | |
418 | assert_se(u == 0); | |
419 | ||
420 | assert_se(get_process_gid(1, &g) == 0); | |
421 | log_info("pid1 gid: '%llu'", (unsigned long long) g); | |
422 | assert_se(g == 0); | |
423 | ||
424 | assert(get_ctty_devnr(1, &h) == -ENOENT); | |
425 | ||
426 | getenv_for_pid(1, "PATH", &i); | |
427 | log_info("pid1 $PATH: '%s'", strna(i)); | |
428 | } | |
429 | ||
2a371001 ZJS |
430 | static void test_protect_errno(void) { |
431 | errno = 12; | |
432 | { | |
433 | PROTECT_ERRNO; | |
434 | errno = 11; | |
435 | } | |
436 | assert(errno == 12); | |
437 | } | |
438 | ||
5556b5fe | 439 | static void test_parse_size(void) { |
b32ff512 ZJS |
440 | off_t bytes; |
441 | ||
5556b5fe | 442 | assert_se(parse_size("111", 1024, &bytes) == 0); |
b32ff512 ZJS |
443 | assert_se(bytes == 111); |
444 | ||
9480794b ZJS |
445 | assert_se(parse_size("111.4", 1024, &bytes) == 0); |
446 | assert_se(bytes == 111); | |
447 | ||
5556b5fe | 448 | assert_se(parse_size(" 112 B", 1024, &bytes) == 0); |
b32ff512 ZJS |
449 | assert_se(bytes == 112); |
450 | ||
9480794b ZJS |
451 | assert_se(parse_size(" 112.6 B", 1024, &bytes) == 0); |
452 | assert_se(bytes == 112); | |
453 | ||
454 | assert_se(parse_size("3.5 K", 1024, &bytes) == 0); | |
455 | assert_se(bytes == 3*1024 + 512); | |
456 | ||
457 | assert_se(parse_size("3. K", 1024, &bytes) == 0); | |
458 | assert_se(bytes == 3*1024); | |
459 | ||
460 | assert_se(parse_size("3.0 K", 1024, &bytes) == 0); | |
b32ff512 ZJS |
461 | assert_se(bytes == 3*1024); |
462 | ||
840292be | 463 | assert_se(parse_size("3. 0 K", 1024, &bytes) == -EINVAL); |
b32ff512 | 464 | |
9480794b ZJS |
465 | assert_se(parse_size(" 4 M 11.5K", 1024, &bytes) == 0); |
466 | assert_se(bytes == 4*1024*1024 + 11 * 1024 + 512); | |
467 | ||
840292be ZJS |
468 | assert_se(parse_size("3B3.5G", 1024, &bytes) == -EINVAL); |
469 | ||
470 | assert_se(parse_size("3.5G3B", 1024, &bytes) == 0); | |
9480794b | 471 | assert_se(bytes == 3ULL*1024*1024*1024 + 512*1024*1024 + 3); |
b32ff512 | 472 | |
840292be ZJS |
473 | assert_se(parse_size("3.5G 4B", 1024, &bytes) == 0); |
474 | assert_se(bytes == 3ULL*1024*1024*1024 + 512*1024*1024 + 4); | |
475 | ||
476 | assert_se(parse_size("3B3G4T", 1024, &bytes) == -EINVAL); | |
477 | ||
478 | assert_se(parse_size("4T3G3B", 1024, &bytes) == 0); | |
479 | assert_se(bytes == (4ULL*1024 + 3)*1024*1024*1024 + 3); | |
480 | ||
481 | assert_se(parse_size(" 4 T 3 G 3 B", 1024, &bytes) == 0); | |
b32ff512 ZJS |
482 | assert_se(bytes == (4ULL*1024 + 3)*1024*1024*1024 + 3); |
483 | ||
5556b5fe | 484 | assert_se(parse_size("12P", 1024, &bytes) == 0); |
b32ff512 ZJS |
485 | assert_se(bytes == 12ULL * 1024*1024*1024*1024*1024); |
486 | ||
840292be ZJS |
487 | assert_se(parse_size("12P12P", 1024, &bytes) == -EINVAL); |
488 | ||
5556b5fe | 489 | assert_se(parse_size("3E 2P", 1024, &bytes) == 0); |
b32ff512 ZJS |
490 | assert_se(bytes == (3 * 1024 + 2ULL) * 1024*1024*1024*1024*1024); |
491 | ||
5556b5fe | 492 | assert_se(parse_size("12X", 1024, &bytes) == -EINVAL); |
b32ff512 | 493 | |
9480794b ZJS |
494 | assert_se(parse_size("12.5X", 1024, &bytes) == -EINVAL); |
495 | ||
496 | assert_se(parse_size("12.5e3", 1024, &bytes) == -EINVAL); | |
497 | ||
5556b5fe LP |
498 | assert_se(parse_size("1024E", 1024, &bytes) == -ERANGE); |
499 | assert_se(parse_size("-1", 1024, &bytes) == -ERANGE); | |
500 | assert_se(parse_size("-1024E", 1024, &bytes) == -ERANGE); | |
b32ff512 | 501 | |
5556b5fe | 502 | assert_se(parse_size("-1024P", 1024, &bytes) == -ERANGE); |
b32ff512 | 503 | |
5556b5fe | 504 | assert_se(parse_size("-10B 20K", 1024, &bytes) == -ERANGE); |
b32ff512 ZJS |
505 | } |
506 | ||
9480794b ZJS |
507 | static void test_config_parse_iec_off(void) { |
508 | off_t offset = 0; | |
509 | assert_se(config_parse_iec_off(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4M", &offset, NULL) == 0); | |
510 | assert_se(offset == 4 * 1024 * 1024); | |
511 | ||
512 | assert_se(config_parse_iec_off(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4.5M", &offset, NULL) == 0); | |
513 | } | |
514 | ||
b4ecc959 TA |
515 | static void test_strextend(void) { |
516 | _cleanup_free_ char *str = strdup("0123"); | |
517 | strextend(&str, "456", "78", "9", NULL); | |
518 | assert_se(streq(str, "0123456789")); | |
519 | } | |
520 | ||
521 | static void test_strrep(void) { | |
522 | _cleanup_free_ char *one, *three, *zero; | |
523 | one = strrep("waldo", 1); | |
524 | three = strrep("waldo", 3); | |
525 | zero = strrep("waldo", 0); | |
526 | ||
527 | assert_se(streq(one, "waldo")); | |
528 | assert_se(streq(three, "waldowaldowaldo")); | |
529 | assert_se(streq(zero, "")); | |
530 | } | |
531 | ||
d4ac85c6 LP |
532 | static void test_split_pair(void) { |
533 | _cleanup_free_ char *a = NULL, *b = NULL; | |
534 | ||
535 | assert_se(split_pair("", "", &a, &b) == -EINVAL); | |
536 | assert_se(split_pair("foo=bar", "", &a, &b) == -EINVAL); | |
537 | assert_se(split_pair("", "=", &a, &b) == -EINVAL); | |
538 | assert_se(split_pair("foo=bar", "=", &a, &b) >= 0); | |
539 | assert_se(streq(a, "foo")); | |
540 | assert_se(streq(b, "bar")); | |
541 | free(a); | |
542 | free(b); | |
543 | assert_se(split_pair("==", "==", &a, &b) >= 0); | |
544 | assert_se(streq(a, "")); | |
545 | assert_se(streq(b, "")); | |
546 | free(a); | |
547 | free(b); | |
548 | ||
549 | assert_se(split_pair("===", "==", &a, &b) >= 0); | |
550 | assert_se(streq(a, "")); | |
551 | assert_se(streq(b, "=")); | |
552 | } | |
553 | ||
22f5f628 DR |
554 | static void test_fstab_node_to_udev_node(void) { |
555 | char *n; | |
556 | ||
557 | n = fstab_node_to_udev_node("LABEL=applé/jack"); | |
558 | puts(n); | |
559 | assert_se(streq(n, "/dev/disk/by-label/applé\\x2fjack")); | |
560 | free(n); | |
561 | ||
562 | n = fstab_node_to_udev_node("PARTLABEL=pinkié pie"); | |
563 | puts(n); | |
564 | assert_se(streq(n, "/dev/disk/by-partlabel/pinkié\\x20pie")); | |
565 | free(n); | |
566 | ||
567 | n = fstab_node_to_udev_node("UUID=037b9d94-148e-4ee4-8d38-67bfe15bb535"); | |
568 | puts(n); | |
569 | assert_se(streq(n, "/dev/disk/by-uuid/037b9d94-148e-4ee4-8d38-67bfe15bb535")); | |
570 | free(n); | |
571 | ||
572 | n = fstab_node_to_udev_node("PARTUUID=037b9d94-148e-4ee4-8d38-67bfe15bb535"); | |
573 | puts(n); | |
574 | assert_se(streq(n, "/dev/disk/by-partuuid/037b9d94-148e-4ee4-8d38-67bfe15bb535")); | |
575 | free(n); | |
576 | ||
22f5f628 DR |
577 | n = fstab_node_to_udev_node("PONIES=awesome"); |
578 | puts(n); | |
579 | assert_se(streq(n, "PONIES=awesome")); | |
580 | free(n); | |
581 | ||
582 | n = fstab_node_to_udev_node("/dev/xda1"); | |
583 | puts(n); | |
584 | assert_se(streq(n, "/dev/xda1")); | |
585 | free(n); | |
586 | } | |
587 | ||
893fa014 ZJS |
588 | static void test_get_files_in_directory(void) { |
589 | _cleanup_strv_free_ char **l = NULL, **t = NULL; | |
590 | ||
591 | assert_se(get_files_in_directory("/tmp", &l) >= 0); | |
510b857f | 592 | assert_se(get_files_in_directory(".", &t) >= 0); |
893fa014 ZJS |
593 | assert_se(get_files_in_directory(".", NULL) >= 0); |
594 | } | |
595 | ||
cabb7806 LP |
596 | static void test_in_set(void) { |
597 | assert_se(IN_SET(1, 1)); | |
598 | assert_se(IN_SET(1, 1, 2, 3, 4)); | |
599 | assert_se(IN_SET(2, 1, 2, 3, 4)); | |
600 | assert_se(IN_SET(3, 1, 2, 3, 4)); | |
601 | assert_se(IN_SET(4, 1, 2, 3, 4)); | |
602 | assert_se(!IN_SET(0, 1)); | |
603 | assert_se(!IN_SET(0, 1, 2, 3, 4)); | |
604 | } | |
605 | ||
87b02843 ZJS |
606 | static void test_writing_tmpfile(void) { |
607 | char name[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX"; | |
39883f62 | 608 | _cleanup_free_ char *contents = NULL; |
65b3903f ZJS |
609 | size_t size; |
610 | int fd, r; | |
65b3903f | 611 | struct iovec iov[3]; |
39883f62 | 612 | |
65b3903f ZJS |
613 | IOVEC_SET_STRING(iov[0], "abc\n"); |
614 | IOVEC_SET_STRING(iov[1], ALPHANUMERICAL "\n"); | |
615 | IOVEC_SET_STRING(iov[2], ""); | |
616 | ||
2d5bdf5b | 617 | fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC); |
87b02843 | 618 | printf("tmpfile: %s", name); |
65b3903f | 619 | |
87b02843 ZJS |
620 | r = writev(fd, iov, 3); |
621 | assert(r >= 0); | |
65b3903f ZJS |
622 | |
623 | r = read_full_file(name, &contents, &size); | |
624 | assert(r == 0); | |
625 | printf("contents: %s", contents); | |
626 | assert(streq(contents, "abc\n" ALPHANUMERICAL "\n")); | |
627 | } | |
628 | ||
29bfbcd6 LP |
629 | static void test_hexdump(void) { |
630 | uint8_t data[146]; | |
631 | unsigned i; | |
632 | ||
633 | hexdump(stdout, NULL, 0); | |
634 | hexdump(stdout, "", 0); | |
635 | hexdump(stdout, "", 1); | |
636 | hexdump(stdout, "x", 1); | |
637 | hexdump(stdout, "x", 2); | |
638 | hexdump(stdout, "foobar", 7); | |
639 | hexdump(stdout, "f\nobar", 7); | |
640 | hexdump(stdout, "xxxxxxxxxxxxxxxxxxxxyz", 23); | |
641 | ||
642 | for (i = 0; i < ELEMENTSOF(data); i++) | |
643 | data[i] = i*2; | |
644 | ||
645 | hexdump(stdout, data, sizeof(data)); | |
646 | } | |
647 | ||
8fe90522 ZJS |
648 | static void test_log2i(void) { |
649 | assert_se(log2i(1) == 0); | |
650 | assert_se(log2i(2) == 1); | |
651 | assert_se(log2i(3) == 1); | |
652 | assert_se(log2i(4) == 2); | |
653 | assert_se(log2i(32) == 5); | |
654 | assert_se(log2i(33) == 5); | |
655 | assert_se(log2i(63) == 5); | |
656 | assert_se(log2i(INT_MAX) == sizeof(int)*8-2); | |
657 | } | |
658 | ||
c4a7b2c5 LP |
659 | static void test_foreach_string(void) { |
660 | const char * const t[] = { | |
661 | "foo", | |
662 | "bar", | |
663 | "waldo", | |
664 | NULL | |
665 | }; | |
666 | const char *x; | |
667 | unsigned i = 0; | |
668 | ||
669 | FOREACH_STRING(x, "foo", "bar", "waldo") | |
670 | assert_se(streq_ptr(t[i++], x)); | |
671 | ||
672 | assert_se(i == 3); | |
673 | ||
674 | FOREACH_STRING(x, "zzz") | |
675 | assert_se(streq(x, "zzz")); | |
676 | } | |
677 | ||
539ad707 | 678 | int main(int argc, char *argv[]) { |
9480794b ZJS |
679 | log_parse_environment(); |
680 | log_open(); | |
681 | ||
539ad707 TA |
682 | test_streq_ptr(); |
683 | test_first_word(); | |
dbd73f9e | 684 | test_close_many(); |
8354c34e | 685 | test_parse_boolean(); |
8d99e5f5 TA |
686 | test_parse_pid(); |
687 | test_parse_uid(); | |
688 | test_safe_atolli(); | |
689 | test_safe_atod(); | |
dbd73f9e | 690 | test_strappend(); |
1ef04f0b TA |
691 | test_strstrip(); |
692 | test_delete_chars(); | |
693 | test_in_charset(); | |
44f4c86c DB |
694 | test_hexchar(); |
695 | test_unhexchar(); | |
696 | test_octchar(); | |
697 | test_unoctchar(); | |
698 | test_decchar(); | |
699 | test_undecchar(); | |
b4ecc959 TA |
700 | test_cescape(); |
701 | test_cunescape(); | |
1ef04f0b | 702 | test_foreach_word(); |
539ad707 | 703 | test_foreach_word_quoted(); |
1ef04f0b | 704 | test_default_term_for_tty(); |
0d585d82 | 705 | test_memdup_multiply(); |
aa3c5cf8 | 706 | test_hostname_is_valid(); |
144e51ec | 707 | test_u64log2(); |
49aa47c7 | 708 | test_get_process_comm(); |
2a371001 | 709 | test_protect_errno(); |
5556b5fe | 710 | test_parse_size(); |
9480794b | 711 | test_config_parse_iec_off(); |
b4ecc959 TA |
712 | test_strextend(); |
713 | test_strrep(); | |
d4ac85c6 | 714 | test_split_pair(); |
22f5f628 | 715 | test_fstab_node_to_udev_node(); |
893fa014 | 716 | test_get_files_in_directory(); |
cabb7806 | 717 | test_in_set(); |
87b02843 | 718 | test_writing_tmpfile(); |
29bfbcd6 | 719 | test_hexdump(); |
8fe90522 | 720 | test_log2i(); |
c4a7b2c5 | 721 | test_foreach_string(); |
539ad707 TA |
722 | |
723 | return 0; | |
724 | } |