]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-util.c
test-parse-util: Move parse-util tests into their own test case
[thirdparty/systemd.git] / src / test / test-util.c
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 <errno.h>
24 #include <fcntl.h>
25 #include <signal.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <sys/xattr.h>
30 #include <unistd.h>
31
32 #include "alloc-util.h"
33 #include "conf-parser.h"
34 #include "cpu-set-util.h"
35 #include "def.h"
36 #include "escape.h"
37 #include "fd-util.h"
38 #include "fileio.h"
39 #include "fs-util.h"
40 #include "fstab-util.h"
41 #include "glob-util.h"
42 #include "hexdecoct.h"
43 #include "io-util.h"
44 #include "mkdir.h"
45 #include "parse-util.h"
46 #include "path-util.h"
47 #include "proc-cmdline.h"
48 #include "process-util.h"
49 #include "rm-rf.h"
50 #include "signal-util.h"
51 #include "stat-util.h"
52 #include "string-util.h"
53 #include "strv.h"
54 #include "user-util.h"
55 #include "util.h"
56 #include "virt.h"
57 #include "web-util.h"
58 #include "xattr-util.h"
59
60 static void test_streq_ptr(void) {
61 assert_se(streq_ptr(NULL, NULL));
62 assert_se(!streq_ptr("abc", "cdef"));
63 }
64
65 static void test_align_power2(void) {
66 unsigned long i, p2;
67
68 assert_se(ALIGN_POWER2(0) == 0);
69 assert_se(ALIGN_POWER2(1) == 1);
70 assert_se(ALIGN_POWER2(2) == 2);
71 assert_se(ALIGN_POWER2(3) == 4);
72 assert_se(ALIGN_POWER2(12) == 16);
73
74 assert_se(ALIGN_POWER2(ULONG_MAX) == 0);
75 assert_se(ALIGN_POWER2(ULONG_MAX - 1) == 0);
76 assert_se(ALIGN_POWER2(ULONG_MAX - 1024) == 0);
77 assert_se(ALIGN_POWER2(ULONG_MAX / 2) == ULONG_MAX / 2 + 1);
78 assert_se(ALIGN_POWER2(ULONG_MAX + 1) == 0);
79
80 for (i = 1; i < 131071; ++i) {
81 for (p2 = 1; p2 < i; p2 <<= 1)
82 /* empty */ ;
83
84 assert_se(ALIGN_POWER2(i) == p2);
85 }
86
87 for (i = ULONG_MAX - 1024; i < ULONG_MAX; ++i) {
88 for (p2 = 1; p2 && p2 < i; p2 <<= 1)
89 /* empty */ ;
90
91 assert_se(ALIGN_POWER2(i) == p2);
92 }
93 }
94
95 static void test_max(void) {
96 static const struct {
97 int a;
98 int b[CONST_MAX(10, 100)];
99 } val1 = {
100 .a = CONST_MAX(10, 100),
101 };
102 int d = 0;
103
104 assert_cc(sizeof(val1.b) == sizeof(int) * 100);
105
106 /* CONST_MAX returns (void) instead of a value if the passed arguments
107 * are not of the same type or not constant expressions. */
108 assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 10)), int));
109 assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 1U)), void));
110
111 assert_se(val1.a == 100);
112 assert_se(MAX(++d, 0) == 1);
113 assert_se(d == 1);
114
115 assert_cc(MAXSIZE(char[3], uint16_t) == 3);
116 assert_cc(MAXSIZE(char[3], uint32_t) == 4);
117 assert_cc(MAXSIZE(char, long) == sizeof(long));
118
119 assert_se(MAX(-5, 5) == 5);
120 assert_se(MAX(5, 5) == 5);
121 assert_se(MAX(MAX(1, MAX(2, MAX(3, 4))), 5) == 5);
122 assert_se(MAX(MAX(1, MAX(2, MAX(3, 2))), 1) == 3);
123 assert_se(MAX(MIN(1, MIN(2, MIN(3, 4))), 5) == 5);
124 assert_se(MAX(MAX(1, MIN(2, MIN(3, 2))), 1) == 2);
125 assert_se(LESS_BY(8, 4) == 4);
126 assert_se(LESS_BY(8, 8) == 0);
127 assert_se(LESS_BY(4, 8) == 0);
128 assert_se(LESS_BY(16, LESS_BY(8, 4)) == 12);
129 assert_se(LESS_BY(4, LESS_BY(8, 4)) == 0);
130 assert_se(CLAMP(-5, 0, 1) == 0);
131 assert_se(CLAMP(5, 0, 1) == 1);
132 assert_se(CLAMP(5, -10, 1) == 1);
133 assert_se(CLAMP(5, -10, 10) == 5);
134 assert_se(CLAMP(CLAMP(0, -10, 10), CLAMP(-5, 10, 20), CLAMP(100, -5, 20)) == 10);
135 }
136
137 static void test_container_of(void) {
138 struct mytype {
139 uint8_t pad1[3];
140 uint64_t v1;
141 uint8_t pad2[2];
142 uint32_t v2;
143 } _packed_ myval = { };
144
145 assert_cc(sizeof(myval) == 17);
146 assert_se(container_of(&myval.v1, struct mytype, v1) == &myval);
147 assert_se(container_of(&myval.v2, struct mytype, v2) == &myval);
148 assert_se(container_of(&container_of(&myval.v2,
149 struct mytype,
150 v2)->v1,
151 struct mytype,
152 v1) == &myval);
153 }
154
155 static void test_alloca(void) {
156 static const uint8_t zero[997] = { };
157 char *t;
158
159 t = alloca_align(17, 512);
160 assert_se(!((uintptr_t)t & 0xff));
161 memzero(t, 17);
162
163 t = alloca0_align(997, 1024);
164 assert_se(!((uintptr_t)t & 0x1ff));
165 assert_se(!memcmp(t, zero, 997));
166 }
167
168 static void test_div_round_up(void) {
169 int div;
170
171 /* basic tests */
172 assert_se(DIV_ROUND_UP(0, 8) == 0);
173 assert_se(DIV_ROUND_UP(1, 8) == 1);
174 assert_se(DIV_ROUND_UP(8, 8) == 1);
175 assert_se(DIV_ROUND_UP(12, 8) == 2);
176 assert_se(DIV_ROUND_UP(16, 8) == 2);
177
178 /* test multiple evaluation */
179 div = 0;
180 assert_se(DIV_ROUND_UP(div++, 8) == 0 && div == 1);
181 assert_se(DIV_ROUND_UP(++div, 8) == 1 && div == 2);
182 assert_se(DIV_ROUND_UP(8, div++) == 4 && div == 3);
183 assert_se(DIV_ROUND_UP(8, ++div) == 2 && div == 4);
184
185 /* overflow test with exact division */
186 assert_se(sizeof(0U) == 4);
187 assert_se(0xfffffffaU % 10U == 0U);
188 assert_se(0xfffffffaU / 10U == 429496729U);
189 assert_se(DIV_ROUND_UP(0xfffffffaU, 10U) == 429496729U);
190 assert_se((0xfffffffaU + 10U - 1U) / 10U == 0U);
191 assert_se(0xfffffffaU / 10U + !!(0xfffffffaU % 10U) == 429496729U);
192
193 /* overflow test with rounded division */
194 assert_se(0xfffffffdU % 10U == 3U);
195 assert_se(0xfffffffdU / 10U == 429496729U);
196 assert_se(DIV_ROUND_UP(0xfffffffdU, 10U) == 429496730U);
197 assert_se((0xfffffffdU + 10U - 1U) / 10U == 0U);
198 assert_se(0xfffffffdU / 10U + !!(0xfffffffdU % 10U) == 429496730U);
199 }
200
201 static void test_first_word(void) {
202 assert_se(first_word("Hello", ""));
203 assert_se(first_word("Hello", "Hello"));
204 assert_se(first_word("Hello world", "Hello"));
205 assert_se(first_word("Hello\tworld", "Hello"));
206 assert_se(first_word("Hello\nworld", "Hello"));
207 assert_se(first_word("Hello\rworld", "Hello"));
208 assert_se(first_word("Hello ", "Hello"));
209
210 assert_se(!first_word("Hello", "Hellooo"));
211 assert_se(!first_word("Hello", "xxxxx"));
212 assert_se(!first_word("Hellooo", "Hello"));
213 }
214
215 static void test_close_many(void) {
216 int fds[3];
217 char name0[] = "/tmp/test-close-many.XXXXXX";
218 char name1[] = "/tmp/test-close-many.XXXXXX";
219 char name2[] = "/tmp/test-close-many.XXXXXX";
220
221 fds[0] = mkostemp_safe(name0, O_RDWR|O_CLOEXEC);
222 fds[1] = mkostemp_safe(name1, O_RDWR|O_CLOEXEC);
223 fds[2] = mkostemp_safe(name2, O_RDWR|O_CLOEXEC);
224
225 close_many(fds, 2);
226
227 assert_se(fcntl(fds[0], F_GETFD) == -1);
228 assert_se(fcntl(fds[1], F_GETFD) == -1);
229 assert_se(fcntl(fds[2], F_GETFD) >= 0);
230
231 safe_close(fds[2]);
232
233 unlink(name0);
234 unlink(name1);
235 unlink(name2);
236 }
237
238 static void test_parse_uid(void) {
239 int r;
240 uid_t uid;
241
242 r = parse_uid("100", &uid);
243 assert_se(r == 0);
244 assert_se(uid == 100);
245
246 r = parse_uid("65535", &uid);
247 assert_se(r == -ENXIO);
248
249 r = parse_uid("asdsdas", &uid);
250 assert_se(r == -EINVAL);
251 }
252
253 static void test_strappend(void) {
254 _cleanup_free_ char *t1, *t2, *t3, *t4;
255
256 t1 = strappend(NULL, NULL);
257 assert_se(streq(t1, ""));
258
259 t2 = strappend(NULL, "suf");
260 assert_se(streq(t2, "suf"));
261
262 t3 = strappend("pre", NULL);
263 assert_se(streq(t3, "pre"));
264
265 t4 = strappend("pre", "suf");
266 assert_se(streq(t4, "presuf"));
267 }
268
269 static void test_strstrip(void) {
270 char *r;
271 char input[] = " hello, waldo. ";
272
273 r = strstrip(input);
274 assert_se(streq(r, "hello, waldo."));
275 }
276
277 static void test_delete_chars(void) {
278 char *r;
279 char input[] = " hello, waldo. abc";
280
281 r = delete_chars(input, WHITESPACE);
282 assert_se(streq(r, "hello,waldo.abc"));
283 }
284
285 static void test_in_charset(void) {
286 assert_se(in_charset("dddaaabbbcccc", "abcd"));
287 assert_se(!in_charset("dddaaabbbcccc", "abc f"));
288 }
289
290 static void test_hexchar(void) {
291 assert_se(hexchar(0xa) == 'a');
292 assert_se(hexchar(0x0) == '0');
293 }
294
295 static void test_unhexchar(void) {
296 assert_se(unhexchar('a') == 0xA);
297 assert_se(unhexchar('A') == 0xA);
298 assert_se(unhexchar('0') == 0x0);
299 }
300
301 static void test_base32hexchar(void) {
302 assert_se(base32hexchar(0) == '0');
303 assert_se(base32hexchar(9) == '9');
304 assert_se(base32hexchar(10) == 'A');
305 assert_se(base32hexchar(31) == 'V');
306 }
307
308 static void test_unbase32hexchar(void) {
309 assert_se(unbase32hexchar('0') == 0);
310 assert_se(unbase32hexchar('9') == 9);
311 assert_se(unbase32hexchar('A') == 10);
312 assert_se(unbase32hexchar('V') == 31);
313 assert_se(unbase32hexchar('=') == -EINVAL);
314 }
315
316 static void test_base64char(void) {
317 assert_se(base64char(0) == 'A');
318 assert_se(base64char(26) == 'a');
319 assert_se(base64char(63) == '/');
320 }
321
322 static void test_unbase64char(void) {
323 assert_se(unbase64char('A') == 0);
324 assert_se(unbase64char('Z') == 25);
325 assert_se(unbase64char('a') == 26);
326 assert_se(unbase64char('z') == 51);
327 assert_se(unbase64char('0') == 52);
328 assert_se(unbase64char('9') == 61);
329 assert_se(unbase64char('+') == 62);
330 assert_se(unbase64char('/') == 63);
331 assert_se(unbase64char('=') == -EINVAL);
332 }
333
334 static void test_octchar(void) {
335 assert_se(octchar(00) == '0');
336 assert_se(octchar(07) == '7');
337 }
338
339 static void test_unoctchar(void) {
340 assert_se(unoctchar('0') == 00);
341 assert_se(unoctchar('7') == 07);
342 }
343
344 static void test_decchar(void) {
345 assert_se(decchar(0) == '0');
346 assert_se(decchar(9) == '9');
347 }
348
349 static void test_undecchar(void) {
350 assert_se(undecchar('0') == 0);
351 assert_se(undecchar('9') == 9);
352 }
353
354 static void test_unhexmem(void) {
355 const char *hex = "efa214921";
356 const char *hex_invalid = "efa214921o";
357 _cleanup_free_ char *hex2 = NULL;
358 _cleanup_free_ void *mem = NULL;
359 size_t len;
360
361 assert_se(unhexmem(hex, strlen(hex), &mem, &len) == 0);
362 assert_se(unhexmem(hex, strlen(hex) + 1, &mem, &len) == -EINVAL);
363 assert_se(unhexmem(hex_invalid, strlen(hex_invalid), &mem, &len) == -EINVAL);
364
365 assert_se((hex2 = hexmem(mem, len)));
366
367 free(mem);
368
369 assert_se(memcmp(hex, hex2, strlen(hex)) == 0);
370
371 free(hex2);
372
373 assert_se(unhexmem(hex, strlen(hex) - 1, &mem, &len) == 0);
374 assert_se((hex2 = hexmem(mem, len)));
375 assert_se(memcmp(hex, hex2, strlen(hex) - 1) == 0);
376 }
377
378 /* https://tools.ietf.org/html/rfc4648#section-10 */
379 static void test_base32hexmem(void) {
380 char *b32;
381
382 b32 = base32hexmem("", strlen(""), true);
383 assert_se(b32);
384 assert_se(streq(b32, ""));
385 free(b32);
386
387 b32 = base32hexmem("f", strlen("f"), true);
388 assert_se(b32);
389 assert_se(streq(b32, "CO======"));
390 free(b32);
391
392 b32 = base32hexmem("fo", strlen("fo"), true);
393 assert_se(b32);
394 assert_se(streq(b32, "CPNG===="));
395 free(b32);
396
397 b32 = base32hexmem("foo", strlen("foo"), true);
398 assert_se(b32);
399 assert_se(streq(b32, "CPNMU==="));
400 free(b32);
401
402 b32 = base32hexmem("foob", strlen("foob"), true);
403 assert_se(b32);
404 assert_se(streq(b32, "CPNMUOG="));
405 free(b32);
406
407 b32 = base32hexmem("fooba", strlen("fooba"), true);
408 assert_se(b32);
409 assert_se(streq(b32, "CPNMUOJ1"));
410 free(b32);
411
412 b32 = base32hexmem("foobar", strlen("foobar"), true);
413 assert_se(b32);
414 assert_se(streq(b32, "CPNMUOJ1E8======"));
415 free(b32);
416
417 b32 = base32hexmem("", strlen(""), false);
418 assert_se(b32);
419 assert_se(streq(b32, ""));
420 free(b32);
421
422 b32 = base32hexmem("f", strlen("f"), false);
423 assert_se(b32);
424 assert_se(streq(b32, "CO"));
425 free(b32);
426
427 b32 = base32hexmem("fo", strlen("fo"), false);
428 assert_se(b32);
429 assert_se(streq(b32, "CPNG"));
430 free(b32);
431
432 b32 = base32hexmem("foo", strlen("foo"), false);
433 assert_se(b32);
434 assert_se(streq(b32, "CPNMU"));
435 free(b32);
436
437 b32 = base32hexmem("foob", strlen("foob"), false);
438 assert_se(b32);
439 assert_se(streq(b32, "CPNMUOG"));
440 free(b32);
441
442 b32 = base32hexmem("fooba", strlen("fooba"), false);
443 assert_se(b32);
444 assert_se(streq(b32, "CPNMUOJ1"));
445 free(b32);
446
447 b32 = base32hexmem("foobar", strlen("foobar"), false);
448 assert_se(b32);
449 assert_se(streq(b32, "CPNMUOJ1E8"));
450 free(b32);
451 }
452
453 static void test_unbase32hexmem(void) {
454 void *mem;
455 size_t len;
456
457 assert_se(unbase32hexmem("", strlen(""), true, &mem, &len) == 0);
458 assert_se(streq(strndupa(mem, len), ""));
459 free(mem);
460
461 assert_se(unbase32hexmem("CO======", strlen("CO======"), true, &mem, &len) == 0);
462 assert_se(streq(strndupa(mem, len), "f"));
463 free(mem);
464
465 assert_se(unbase32hexmem("CPNG====", strlen("CPNG===="), true, &mem, &len) == 0);
466 assert_se(streq(strndupa(mem, len), "fo"));
467 free(mem);
468
469 assert_se(unbase32hexmem("CPNMU===", strlen("CPNMU==="), true, &mem, &len) == 0);
470 assert_se(streq(strndupa(mem, len), "foo"));
471 free(mem);
472
473 assert_se(unbase32hexmem("CPNMUOG=", strlen("CPNMUOG="), true, &mem, &len) == 0);
474 assert_se(streq(strndupa(mem, len), "foob"));
475 free(mem);
476
477 assert_se(unbase32hexmem("CPNMUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == 0);
478 assert_se(streq(strndupa(mem, len), "fooba"));
479 free(mem);
480
481 assert_se(unbase32hexmem("CPNMUOJ1E8======", strlen("CPNMUOJ1E8======"), true, &mem, &len) == 0);
482 assert_se(streq(strndupa(mem, len), "foobar"));
483 free(mem);
484
485 assert_se(unbase32hexmem("A", strlen("A"), true, &mem, &len) == -EINVAL);
486 assert_se(unbase32hexmem("A=======", strlen("A======="), true, &mem, &len) == -EINVAL);
487 assert_se(unbase32hexmem("AAA=====", strlen("AAA====="), true, &mem, &len) == -EINVAL);
488 assert_se(unbase32hexmem("AAAAAA==", strlen("AAAAAA=="), true, &mem, &len) == -EINVAL);
489 assert_se(unbase32hexmem("AB======", strlen("AB======"), true, &mem, &len) == -EINVAL);
490 assert_se(unbase32hexmem("AAAB====", strlen("AAAB===="), true, &mem, &len) == -EINVAL);
491 assert_se(unbase32hexmem("AAAAB===", strlen("AAAAB==="), true, &mem, &len) == -EINVAL);
492 assert_se(unbase32hexmem("AAAAAAB=", strlen("AAAAAAB="), true, &mem, &len) == -EINVAL);
493
494 assert_se(unbase32hexmem("XPNMUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
495 assert_se(unbase32hexmem("CXNMUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
496 assert_se(unbase32hexmem("CPXMUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
497 assert_se(unbase32hexmem("CPNXUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
498 assert_se(unbase32hexmem("CPNMXOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
499 assert_se(unbase32hexmem("CPNMUXJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
500 assert_se(unbase32hexmem("CPNMUOX1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
501 assert_se(unbase32hexmem("CPNMUOJX", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL);
502
503 assert_se(unbase32hexmem("", strlen(""), false, &mem, &len) == 0);
504 assert_se(streq(strndupa(mem, len), ""));
505 free(mem);
506
507 assert_se(unbase32hexmem("CO", strlen("CO"), false, &mem, &len) == 0);
508 assert_se(streq(strndupa(mem, len), "f"));
509 free(mem);
510
511 assert_se(unbase32hexmem("CPNG", strlen("CPNG"), false, &mem, &len) == 0);
512 assert_se(streq(strndupa(mem, len), "fo"));
513 free(mem);
514
515 assert_se(unbase32hexmem("CPNMU", strlen("CPNMU"), false, &mem, &len) == 0);
516 assert_se(streq(strndupa(mem, len), "foo"));
517 free(mem);
518
519 assert_se(unbase32hexmem("CPNMUOG", strlen("CPNMUOG"), false, &mem, &len) == 0);
520 assert_se(streq(strndupa(mem, len), "foob"));
521 free(mem);
522
523 assert_se(unbase32hexmem("CPNMUOJ1", strlen("CPNMUOJ1"), false, &mem, &len) == 0);
524 assert_se(streq(strndupa(mem, len), "fooba"));
525 free(mem);
526
527 assert_se(unbase32hexmem("CPNMUOJ1E8", strlen("CPNMUOJ1E8"), false, &mem, &len) == 0);
528 assert_se(streq(strndupa(mem, len), "foobar"));
529 free(mem);
530
531 assert_se(unbase32hexmem("CPNMUOG=", strlen("CPNMUOG="), false, &mem, &len) == -EINVAL);
532 assert_se(unbase32hexmem("CPNMUOJ1E8======", strlen("CPNMUOJ1E8======"), false, &mem, &len) == -EINVAL);
533 assert_se(unbase32hexmem("A", strlen("A"), false, &mem, &len) == -EINVAL);
534 assert_se(unbase32hexmem("A", strlen("A"), false, &mem, &len) == -EINVAL);
535 assert_se(unbase32hexmem("AAA", strlen("AAA"), false, &mem, &len) == -EINVAL);
536 assert_se(unbase32hexmem("AAAAAA", strlen("AAAAAA"), false, &mem, &len) == -EINVAL);
537 assert_se(unbase32hexmem("AB", strlen("AB"), false, &mem, &len) == -EINVAL);
538 assert_se(unbase32hexmem("AAAB", strlen("AAAB"), false, &mem, &len) == -EINVAL);
539 assert_se(unbase32hexmem("AAAAB", strlen("AAAAB"), false, &mem, &len) == -EINVAL);
540 assert_se(unbase32hexmem("AAAAAAB", strlen("AAAAAAB"), false, &mem, &len) == -EINVAL);
541 }
542
543 /* https://tools.ietf.org/html/rfc4648#section-10 */
544 static void test_base64mem(void) {
545 char *b64;
546
547 b64 = base64mem("", strlen(""));
548 assert_se(b64);
549 assert_se(streq(b64, ""));
550 free(b64);
551
552 b64 = base64mem("f", strlen("f"));
553 assert_se(b64);
554 assert_se(streq(b64, "Zg=="));
555 free(b64);
556
557 b64 = base64mem("fo", strlen("fo"));
558 assert_se(b64);
559 assert_se(streq(b64, "Zm8="));
560 free(b64);
561
562 b64 = base64mem("foo", strlen("foo"));
563 assert_se(b64);
564 assert_se(streq(b64, "Zm9v"));
565 free(b64);
566
567 b64 = base64mem("foob", strlen("foob"));
568 assert_se(b64);
569 assert_se(streq(b64, "Zm9vYg=="));
570 free(b64);
571
572 b64 = base64mem("fooba", strlen("fooba"));
573 assert_se(b64);
574 assert_se(streq(b64, "Zm9vYmE="));
575 free(b64);
576
577 b64 = base64mem("foobar", strlen("foobar"));
578 assert_se(b64);
579 assert_se(streq(b64, "Zm9vYmFy"));
580 free(b64);
581 }
582
583 static void test_unbase64mem(void) {
584 void *mem;
585 size_t len;
586
587 assert_se(unbase64mem("", strlen(""), &mem, &len) == 0);
588 assert_se(streq(strndupa(mem, len), ""));
589 free(mem);
590
591 assert_se(unbase64mem("Zg==", strlen("Zg=="), &mem, &len) == 0);
592 assert_se(streq(strndupa(mem, len), "f"));
593 free(mem);
594
595 assert_se(unbase64mem("Zm8=", strlen("Zm8="), &mem, &len) == 0);
596 assert_se(streq(strndupa(mem, len), "fo"));
597 free(mem);
598
599 assert_se(unbase64mem("Zm9v", strlen("Zm9v"), &mem, &len) == 0);
600 assert_se(streq(strndupa(mem, len), "foo"));
601 free(mem);
602
603 assert_se(unbase64mem("Zm9vYg==", strlen("Zm9vYg=="), &mem, &len) == 0);
604 assert_se(streq(strndupa(mem, len), "foob"));
605 free(mem);
606
607 assert_se(unbase64mem("Zm9vYmE=", strlen("Zm9vYmE="), &mem, &len) == 0);
608 assert_se(streq(strndupa(mem, len), "fooba"));
609 free(mem);
610
611 assert_se(unbase64mem("Zm9vYmFy", strlen("Zm9vYmFy"), &mem, &len) == 0);
612 assert_se(streq(strndupa(mem, len), "foobar"));
613 free(mem);
614
615 assert_se(unbase64mem("A", strlen("A"), &mem, &len) == -EINVAL);
616 assert_se(unbase64mem("A====", strlen("A===="), &mem, &len) == -EINVAL);
617 assert_se(unbase64mem("AAB==", strlen("AAB=="), &mem, &len) == -EINVAL);
618 assert_se(unbase64mem("AAAB=", strlen("AAAB="), &mem, &len) == -EINVAL);
619 }
620
621 static void test_cescape(void) {
622 _cleanup_free_ char *escaped;
623
624 assert_se(escaped = cescape("abc\\\"\b\f\n\r\t\v\a\003\177\234\313"));
625 assert_se(streq(escaped, "abc\\\\\\\"\\b\\f\\n\\r\\t\\v\\a\\003\\177\\234\\313"));
626 }
627
628 static void test_cunescape(void) {
629 _cleanup_free_ char *unescaped;
630
631 assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", 0, &unescaped) < 0);
632 assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", UNESCAPE_RELAX, &unescaped) >= 0);
633 assert_se(streq_ptr(unescaped, "abc\\\"\b\f\a\n\r\t\v\003\177\234\313\\000\\x00"));
634 unescaped = mfree(unescaped);
635
636 /* incomplete sequences */
637 assert_se(cunescape("\\x0", 0, &unescaped) < 0);
638 assert_se(cunescape("\\x0", UNESCAPE_RELAX, &unescaped) >= 0);
639 assert_se(streq_ptr(unescaped, "\\x0"));
640 unescaped = mfree(unescaped);
641
642 assert_se(cunescape("\\x", 0, &unescaped) < 0);
643 assert_se(cunescape("\\x", UNESCAPE_RELAX, &unescaped) >= 0);
644 assert_se(streq_ptr(unescaped, "\\x"));
645 unescaped = mfree(unescaped);
646
647 assert_se(cunescape("\\", 0, &unescaped) < 0);
648 assert_se(cunescape("\\", UNESCAPE_RELAX, &unescaped) >= 0);
649 assert_se(streq_ptr(unescaped, "\\"));
650 unescaped = mfree(unescaped);
651
652 assert_se(cunescape("\\11", 0, &unescaped) < 0);
653 assert_se(cunescape("\\11", UNESCAPE_RELAX, &unescaped) >= 0);
654 assert_se(streq_ptr(unescaped, "\\11"));
655 unescaped = mfree(unescaped);
656
657 assert_se(cunescape("\\1", 0, &unescaped) < 0);
658 assert_se(cunescape("\\1", UNESCAPE_RELAX, &unescaped) >= 0);
659 assert_se(streq_ptr(unescaped, "\\1"));
660 unescaped = mfree(unescaped);
661
662 assert_se(cunescape("\\u0000", 0, &unescaped) < 0);
663 assert_se(cunescape("\\u00DF\\U000000df\\u03a0\\U00000041", UNESCAPE_RELAX, &unescaped) >= 0);
664 assert_se(streq_ptr(unescaped, "ßßΠA"));
665 unescaped = mfree(unescaped);
666
667 assert_se(cunescape("\\073", 0, &unescaped) >= 0);
668 assert_se(streq_ptr(unescaped, ";"));
669 }
670
671 static void test_foreach_word(void) {
672 const char *word, *state;
673 size_t l;
674 int i = 0;
675 const char test[] = "test abc d\te f ";
676 const char * const expected[] = {
677 "test",
678 "abc",
679 "d",
680 "e",
681 "f",
682 "",
683 NULL
684 };
685
686 FOREACH_WORD(word, l, test, state)
687 assert_se(strneq(expected[i++], word, l));
688 }
689
690 static void check(const char *test, char** expected, bool trailing) {
691 const char *word, *state;
692 size_t l;
693 int i = 0;
694
695 printf("<<<%s>>>\n", test);
696 FOREACH_WORD_QUOTED(word, l, test, state) {
697 _cleanup_free_ char *t = NULL;
698
699 assert_se(t = strndup(word, l));
700 assert_se(strneq(expected[i++], word, l));
701 printf("<%s>\n", t);
702 }
703 printf("<<<%s>>>\n", state);
704 assert_se(expected[i] == NULL);
705 assert_se(isempty(state) == !trailing);
706 }
707
708 static void test_foreach_word_quoted(void) {
709 check("test a b c 'd' e '' '' hhh '' '' \"a b c\"",
710 STRV_MAKE("test",
711 "a",
712 "b",
713 "c",
714 "d",
715 "e",
716 "",
717 "",
718 "hhh",
719 "",
720 "",
721 "a b c"),
722 false);
723
724 check("test \"xxx",
725 STRV_MAKE("test"),
726 true);
727
728 check("test\\",
729 STRV_MAKE_EMPTY,
730 true);
731 }
732
733 static void test_memdup_multiply(void) {
734 int org[] = {1, 2, 3};
735 int *dup;
736
737 dup = (int*)memdup_multiply(org, sizeof(int), 3);
738
739 assert_se(dup);
740 assert_se(dup[0] == 1);
741 assert_se(dup[1] == 2);
742 assert_se(dup[2] == 3);
743 free(dup);
744 }
745
746 static void test_u64log2(void) {
747 assert_se(u64log2(0) == 0);
748 assert_se(u64log2(8) == 3);
749 assert_se(u64log2(9) == 3);
750 assert_se(u64log2(15) == 3);
751 assert_se(u64log2(16) == 4);
752 assert_se(u64log2(1024*1024) == 20);
753 assert_se(u64log2(1024*1024+5) == 20);
754 }
755
756 static void test_protect_errno(void) {
757 errno = 12;
758 {
759 PROTECT_ERRNO;
760 errno = 11;
761 }
762 assert_se(errno == 12);
763 }
764
765 static void test_parse_cpu_set(void) {
766 cpu_set_t *c = NULL;
767 int ncpus;
768 int cpu;
769
770 /* Simple range (from CPUAffinity example) */
771 ncpus = parse_cpu_set_and_warn("1 2", &c, NULL, "fake", 1, "CPUAffinity");
772 assert_se(ncpus >= 1024);
773 assert_se(CPU_ISSET_S(1, CPU_ALLOC_SIZE(ncpus), c));
774 assert_se(CPU_ISSET_S(2, CPU_ALLOC_SIZE(ncpus), c));
775 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 2);
776 c = mfree(c);
777
778 /* A more interesting range */
779 ncpus = parse_cpu_set_and_warn("0 1 2 3 8 9 10 11", &c, NULL, "fake", 1, "CPUAffinity");
780 assert_se(ncpus >= 1024);
781 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 8);
782 for (cpu = 0; cpu < 4; cpu++)
783 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
784 for (cpu = 8; cpu < 12; cpu++)
785 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
786 c = mfree(c);
787
788 /* Quoted strings */
789 ncpus = parse_cpu_set_and_warn("8 '9' 10 \"11\"", &c, NULL, "fake", 1, "CPUAffinity");
790 assert_se(ncpus >= 1024);
791 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 4);
792 for (cpu = 8; cpu < 12; cpu++)
793 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
794 c = mfree(c);
795
796 /* Use commas as separators */
797 ncpus = parse_cpu_set_and_warn("0,1,2,3 8,9,10,11", &c, NULL, "fake", 1, "CPUAffinity");
798 assert_se(ncpus >= 1024);
799 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 8);
800 for (cpu = 0; cpu < 4; cpu++)
801 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
802 for (cpu = 8; cpu < 12; cpu++)
803 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
804 c = mfree(c);
805
806 /* Commas with spaces (and trailing comma, space) */
807 ncpus = parse_cpu_set_and_warn("0, 1, 2, 3, 4, 5, 6, 7, ", &c, NULL, "fake", 1, "CPUAffinity");
808 assert_se(ncpus >= 1024);
809 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 8);
810 for (cpu = 0; cpu < 8; cpu++)
811 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
812 c = mfree(c);
813
814 /* Ranges */
815 ncpus = parse_cpu_set_and_warn("0-3,8-11", &c, NULL, "fake", 1, "CPUAffinity");
816 assert_se(ncpus >= 1024);
817 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 8);
818 for (cpu = 0; cpu < 4; cpu++)
819 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
820 for (cpu = 8; cpu < 12; cpu++)
821 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
822 c = mfree(c);
823
824 /* Ranges with trailing comma, space */
825 ncpus = parse_cpu_set_and_warn("0-3 8-11, ", &c, NULL, "fake", 1, "CPUAffinity");
826 assert_se(ncpus >= 1024);
827 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 8);
828 for (cpu = 0; cpu < 4; cpu++)
829 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
830 for (cpu = 8; cpu < 12; cpu++)
831 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
832 c = mfree(c);
833
834 /* Negative range (returns empty cpu_set) */
835 ncpus = parse_cpu_set_and_warn("3-0", &c, NULL, "fake", 1, "CPUAffinity");
836 assert_se(ncpus >= 1024);
837 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 0);
838 c = mfree(c);
839
840 /* Overlapping ranges */
841 ncpus = parse_cpu_set_and_warn("0-7 4-11", &c, NULL, "fake", 1, "CPUAffinity");
842 assert_se(ncpus >= 1024);
843 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 12);
844 for (cpu = 0; cpu < 12; cpu++)
845 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
846 c = mfree(c);
847
848 /* Mix ranges and individual CPUs */
849 ncpus = parse_cpu_set_and_warn("0,1 4-11", &c, NULL, "fake", 1, "CPUAffinity");
850 assert_se(ncpus >= 1024);
851 assert_se(CPU_COUNT_S(CPU_ALLOC_SIZE(ncpus), c) == 10);
852 assert_se(CPU_ISSET_S(0, CPU_ALLOC_SIZE(ncpus), c));
853 assert_se(CPU_ISSET_S(1, CPU_ALLOC_SIZE(ncpus), c));
854 for (cpu = 4; cpu < 12; cpu++)
855 assert_se(CPU_ISSET_S(cpu, CPU_ALLOC_SIZE(ncpus), c));
856 c = mfree(c);
857
858 /* Garbage */
859 ncpus = parse_cpu_set_and_warn("0 1 2 3 garbage", &c, NULL, "fake", 1, "CPUAffinity");
860 assert_se(ncpus < 0);
861 assert_se(!c);
862
863 /* Range with garbage */
864 ncpus = parse_cpu_set_and_warn("0-3 8-garbage", &c, NULL, "fake", 1, "CPUAffinity");
865 assert_se(ncpus < 0);
866 assert_se(!c);
867
868 /* Empty string */
869 c = NULL;
870 ncpus = parse_cpu_set_and_warn("", &c, NULL, "fake", 1, "CPUAffinity");
871 assert_se(ncpus == 0); /* empty string returns 0 */
872 assert_se(!c);
873
874 /* Runnaway quoted string */
875 ncpus = parse_cpu_set_and_warn("0 1 2 3 \"4 5 6 7 ", &c, NULL, "fake", 1, "CPUAffinity");
876 assert_se(ncpus < 0);
877 assert_se(!c);
878 }
879
880 static void test_config_parse_iec_uint64(void) {
881 uint64_t offset = 0;
882 assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4M", &offset, NULL) == 0);
883 assert_se(offset == 4 * 1024 * 1024);
884
885 assert_se(config_parse_iec_uint64(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4.5M", &offset, NULL) == 0);
886 }
887
888 static void test_strextend(void) {
889 _cleanup_free_ char *str = strdup("0123");
890 strextend(&str, "456", "78", "9", NULL);
891 assert_se(streq(str, "0123456789"));
892 }
893
894 static void test_strrep(void) {
895 _cleanup_free_ char *one, *three, *zero;
896 one = strrep("waldo", 1);
897 three = strrep("waldo", 3);
898 zero = strrep("waldo", 0);
899
900 assert_se(streq(one, "waldo"));
901 assert_se(streq(three, "waldowaldowaldo"));
902 assert_se(streq(zero, ""));
903 }
904
905 static void test_split_pair(void) {
906 _cleanup_free_ char *a = NULL, *b = NULL;
907
908 assert_se(split_pair("", "", &a, &b) == -EINVAL);
909 assert_se(split_pair("foo=bar", "", &a, &b) == -EINVAL);
910 assert_se(split_pair("", "=", &a, &b) == -EINVAL);
911 assert_se(split_pair("foo=bar", "=", &a, &b) >= 0);
912 assert_se(streq(a, "foo"));
913 assert_se(streq(b, "bar"));
914 free(a);
915 free(b);
916 assert_se(split_pair("==", "==", &a, &b) >= 0);
917 assert_se(streq(a, ""));
918 assert_se(streq(b, ""));
919 free(a);
920 free(b);
921
922 assert_se(split_pair("===", "==", &a, &b) >= 0);
923 assert_se(streq(a, ""));
924 assert_se(streq(b, "="));
925 }
926
927 static void test_fstab_node_to_udev_node(void) {
928 char *n;
929
930 n = fstab_node_to_udev_node("LABEL=applé/jack");
931 puts(n);
932 assert_se(streq(n, "/dev/disk/by-label/applé\\x2fjack"));
933 free(n);
934
935 n = fstab_node_to_udev_node("PARTLABEL=pinkié pie");
936 puts(n);
937 assert_se(streq(n, "/dev/disk/by-partlabel/pinkié\\x20pie"));
938 free(n);
939
940 n = fstab_node_to_udev_node("UUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
941 puts(n);
942 assert_se(streq(n, "/dev/disk/by-uuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
943 free(n);
944
945 n = fstab_node_to_udev_node("PARTUUID=037b9d94-148e-4ee4-8d38-67bfe15bb535");
946 puts(n);
947 assert_se(streq(n, "/dev/disk/by-partuuid/037b9d94-148e-4ee4-8d38-67bfe15bb535"));
948 free(n);
949
950 n = fstab_node_to_udev_node("PONIES=awesome");
951 puts(n);
952 assert_se(streq(n, "PONIES=awesome"));
953 free(n);
954
955 n = fstab_node_to_udev_node("/dev/xda1");
956 puts(n);
957 assert_se(streq(n, "/dev/xda1"));
958 free(n);
959 }
960
961 static void test_get_files_in_directory(void) {
962 _cleanup_strv_free_ char **l = NULL, **t = NULL;
963
964 assert_se(get_files_in_directory("/tmp", &l) >= 0);
965 assert_se(get_files_in_directory(".", &t) >= 0);
966 assert_se(get_files_in_directory(".", NULL) >= 0);
967 }
968
969 static void test_in_set(void) {
970 assert_se(IN_SET(1, 1));
971 assert_se(IN_SET(1, 1, 2, 3, 4));
972 assert_se(IN_SET(2, 1, 2, 3, 4));
973 assert_se(IN_SET(3, 1, 2, 3, 4));
974 assert_se(IN_SET(4, 1, 2, 3, 4));
975 assert_se(!IN_SET(0, 1));
976 assert_se(!IN_SET(0, 1, 2, 3, 4));
977 }
978
979 static void test_writing_tmpfile(void) {
980 char name[] = "/tmp/test-systemd_writing_tmpfile.XXXXXX";
981 _cleanup_free_ char *contents = NULL;
982 size_t size;
983 int fd, r;
984 struct iovec iov[3];
985
986 IOVEC_SET_STRING(iov[0], "abc\n");
987 IOVEC_SET_STRING(iov[1], ALPHANUMERICAL "\n");
988 IOVEC_SET_STRING(iov[2], "");
989
990 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
991 printf("tmpfile: %s", name);
992
993 r = writev(fd, iov, 3);
994 assert_se(r >= 0);
995
996 r = read_full_file(name, &contents, &size);
997 assert_se(r == 0);
998 printf("contents: %s", contents);
999 assert_se(streq(contents, "abc\n" ALPHANUMERICAL "\n"));
1000
1001 unlink(name);
1002 }
1003
1004 static void test_hexdump(void) {
1005 uint8_t data[146];
1006 unsigned i;
1007
1008 hexdump(stdout, NULL, 0);
1009 hexdump(stdout, "", 0);
1010 hexdump(stdout, "", 1);
1011 hexdump(stdout, "x", 1);
1012 hexdump(stdout, "x", 2);
1013 hexdump(stdout, "foobar", 7);
1014 hexdump(stdout, "f\nobar", 7);
1015 hexdump(stdout, "xxxxxxxxxxxxxxxxxxxxyz", 23);
1016
1017 for (i = 0; i < ELEMENTSOF(data); i++)
1018 data[i] = i*2;
1019
1020 hexdump(stdout, data, sizeof(data));
1021 }
1022
1023 static void test_log2i(void) {
1024 assert_se(log2i(1) == 0);
1025 assert_se(log2i(2) == 1);
1026 assert_se(log2i(3) == 1);
1027 assert_se(log2i(4) == 2);
1028 assert_se(log2i(32) == 5);
1029 assert_se(log2i(33) == 5);
1030 assert_se(log2i(63) == 5);
1031 assert_se(log2i(INT_MAX) == sizeof(int)*8-2);
1032 }
1033
1034 static void test_foreach_string(void) {
1035 const char * const t[] = {
1036 "foo",
1037 "bar",
1038 "waldo",
1039 NULL
1040 };
1041 const char *x;
1042 unsigned i = 0;
1043
1044 FOREACH_STRING(x, "foo", "bar", "waldo")
1045 assert_se(streq_ptr(t[i++], x));
1046
1047 assert_se(i == 3);
1048
1049 FOREACH_STRING(x, "zzz")
1050 assert_se(streq(x, "zzz"));
1051 }
1052
1053 static void test_filename_is_valid(void) {
1054 char foo[FILENAME_MAX+2];
1055 int i;
1056
1057 assert_se(!filename_is_valid(""));
1058 assert_se(!filename_is_valid("/bar/foo"));
1059 assert_se(!filename_is_valid("/"));
1060 assert_se(!filename_is_valid("."));
1061 assert_se(!filename_is_valid(".."));
1062
1063 for (i=0; i<FILENAME_MAX+1; i++)
1064 foo[i] = 'a';
1065 foo[FILENAME_MAX+1] = '\0';
1066
1067 assert_se(!filename_is_valid(foo));
1068
1069 assert_se(filename_is_valid("foo_bar-333"));
1070 assert_se(filename_is_valid("o.o"));
1071 }
1072
1073 static void test_string_has_cc(void) {
1074 assert_se(string_has_cc("abc\1", NULL));
1075 assert_se(string_has_cc("abc\x7f", NULL));
1076 assert_se(string_has_cc("abc\x7f", NULL));
1077 assert_se(string_has_cc("abc\t\x7f", "\t"));
1078 assert_se(string_has_cc("abc\t\x7f", "\t"));
1079 assert_se(string_has_cc("\x7f", "\t"));
1080 assert_se(string_has_cc("\x7f", "\t\a"));
1081
1082 assert_se(!string_has_cc("abc\t\t", "\t"));
1083 assert_se(!string_has_cc("abc\t\t\a", "\t\a"));
1084 assert_se(!string_has_cc("a\ab\tc", "\t\a"));
1085 }
1086
1087 static void test_ascii_strlower(void) {
1088 char a[] = "AabBcC Jk Ii Od LKJJJ kkd LK";
1089 assert_se(streq(ascii_strlower(a), "aabbcc jk ii od lkjjj kkd lk"));
1090 }
1091
1092 static void test_files_same(void) {
1093 _cleanup_close_ int fd = -1;
1094 char name[] = "/tmp/test-files_same.XXXXXX";
1095 char name_alias[] = "/tmp/test-files_same.alias";
1096
1097 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1098 assert_se(fd >= 0);
1099 assert_se(symlink(name, name_alias) >= 0);
1100
1101 assert_se(files_same(name, name));
1102 assert_se(files_same(name, name_alias));
1103
1104 unlink(name);
1105 unlink(name_alias);
1106 }
1107
1108 static void test_is_valid_documentation_url(void) {
1109 assert_se(documentation_url_is_valid("http://www.freedesktop.org/wiki/Software/systemd"));
1110 assert_se(documentation_url_is_valid("https://www.kernel.org/doc/Documentation/binfmt_misc.txt"));
1111 assert_se(documentation_url_is_valid("file:/foo/foo"));
1112 assert_se(documentation_url_is_valid("man:systemd.special(7)"));
1113 assert_se(documentation_url_is_valid("info:bar"));
1114
1115 assert_se(!documentation_url_is_valid("foo:"));
1116 assert_se(!documentation_url_is_valid("info:"));
1117 assert_se(!documentation_url_is_valid(""));
1118 }
1119
1120 static void test_file_in_same_dir(void) {
1121 char *t;
1122
1123 t = file_in_same_dir("/", "a");
1124 assert_se(streq(t, "/a"));
1125 free(t);
1126
1127 t = file_in_same_dir("/", "/a");
1128 assert_se(streq(t, "/a"));
1129 free(t);
1130
1131 t = file_in_same_dir("", "a");
1132 assert_se(streq(t, "a"));
1133 free(t);
1134
1135 t = file_in_same_dir("a/", "a");
1136 assert_se(streq(t, "a/a"));
1137 free(t);
1138
1139 t = file_in_same_dir("bar/foo", "bar");
1140 assert_se(streq(t, "bar/bar"));
1141 free(t);
1142 }
1143
1144 static void test_endswith(void) {
1145 assert_se(endswith("foobar", "bar"));
1146 assert_se(endswith("foobar", ""));
1147 assert_se(endswith("foobar", "foobar"));
1148 assert_se(endswith("", ""));
1149
1150 assert_se(!endswith("foobar", "foo"));
1151 assert_se(!endswith("foobar", "foobarfoofoo"));
1152 }
1153
1154 static void test_endswith_no_case(void) {
1155 assert_se(endswith_no_case("fooBAR", "bar"));
1156 assert_se(endswith_no_case("foobar", ""));
1157 assert_se(endswith_no_case("foobar", "FOOBAR"));
1158 assert_se(endswith_no_case("", ""));
1159
1160 assert_se(!endswith_no_case("foobar", "FOO"));
1161 assert_se(!endswith_no_case("foobar", "FOOBARFOOFOO"));
1162 }
1163
1164 static void test_close_nointr(void) {
1165 char name[] = "/tmp/test-test-close_nointr.XXXXXX";
1166 int fd;
1167
1168 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1169 assert_se(fd >= 0);
1170 assert_se(close_nointr(fd) >= 0);
1171 assert_se(close_nointr(fd) < 0);
1172
1173 unlink(name);
1174 }
1175
1176
1177 static void test_unlink_noerrno(void) {
1178 char name[] = "/tmp/test-close_nointr.XXXXXX";
1179 int fd;
1180
1181 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1182 assert_se(fd >= 0);
1183 assert_se(close_nointr(fd) >= 0);
1184
1185 {
1186 PROTECT_ERRNO;
1187 errno = -42;
1188 assert_se(unlink_noerrno(name) >= 0);
1189 assert_se(errno == -42);
1190 assert_se(unlink_noerrno(name) < 0);
1191 assert_se(errno == -42);
1192 }
1193 }
1194
1195 static void test_readlink_and_make_absolute(void) {
1196 char tempdir[] = "/tmp/test-readlink_and_make_absolute";
1197 char name[] = "/tmp/test-readlink_and_make_absolute/original";
1198 char name2[] = "test-readlink_and_make_absolute/original";
1199 char name_alias[] = "/tmp/test-readlink_and_make_absolute-alias";
1200 char *r = NULL;
1201
1202 assert_se(mkdir_safe(tempdir, 0755, getuid(), getgid()) >= 0);
1203 assert_se(touch(name) >= 0);
1204
1205 assert_se(symlink(name, name_alias) >= 0);
1206 assert_se(readlink_and_make_absolute(name_alias, &r) >= 0);
1207 assert_se(streq(r, name));
1208 free(r);
1209 assert_se(unlink(name_alias) >= 0);
1210
1211 assert_se(chdir(tempdir) >= 0);
1212 assert_se(symlink(name2, name_alias) >= 0);
1213 assert_se(readlink_and_make_absolute(name_alias, &r) >= 0);
1214 assert_se(streq(r, name));
1215 free(r);
1216 assert_se(unlink(name_alias) >= 0);
1217
1218 assert_se(rm_rf(tempdir, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
1219 }
1220
1221 static void test_ignore_signals(void) {
1222 assert_se(ignore_signals(SIGINT, -1) >= 0);
1223 assert_se(kill(getpid(), SIGINT) >= 0);
1224 assert_se(ignore_signals(SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE, -1) >= 0);
1225 assert_se(kill(getpid(), SIGUSR1) >= 0);
1226 assert_se(kill(getpid(), SIGUSR2) >= 0);
1227 assert_se(kill(getpid(), SIGTERM) >= 0);
1228 assert_se(kill(getpid(), SIGPIPE) >= 0);
1229 assert_se(default_signals(SIGINT, SIGUSR1, SIGUSR2, SIGTERM, SIGPIPE, -1) >= 0);
1230 }
1231
1232 static void test_strshorten(void) {
1233 char s[] = "foobar";
1234
1235 assert_se(strlen(strshorten(s, 6)) == 6);
1236 assert_se(strlen(strshorten(s, 12)) == 6);
1237 assert_se(strlen(strshorten(s, 2)) == 2);
1238 assert_se(strlen(strshorten(s, 0)) == 0);
1239 }
1240
1241 static void test_strjoina(void) {
1242 char *actual;
1243
1244 actual = strjoina("", "foo", "bar");
1245 assert_se(streq(actual, "foobar"));
1246
1247 actual = strjoina("foo", "bar", "baz");
1248 assert_se(streq(actual, "foobarbaz"));
1249
1250 actual = strjoina("foo", "", "bar", "baz");
1251 assert_se(streq(actual, "foobarbaz"));
1252
1253 actual = strjoina("foo");
1254 assert_se(streq(actual, "foo"));
1255
1256 actual = strjoina(NULL);
1257 assert_se(streq(actual, ""));
1258
1259 actual = strjoina(NULL, "foo");
1260 assert_se(streq(actual, ""));
1261
1262 actual = strjoina("foo", NULL, "bar");
1263 assert_se(streq(actual, "foo"));
1264 }
1265
1266 static void test_is_symlink(void) {
1267 char name[] = "/tmp/test-is_symlink.XXXXXX";
1268 char name_link[] = "/tmp/test-is_symlink.link";
1269 _cleanup_close_ int fd = -1;
1270
1271 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1272 assert_se(fd >= 0);
1273 assert_se(symlink(name, name_link) >= 0);
1274
1275 assert_se(is_symlink(name) == 0);
1276 assert_se(is_symlink(name_link) == 1);
1277 assert_se(is_symlink("/a/file/which/does/not/exist/i/guess") < 0);
1278
1279
1280 unlink(name);
1281 unlink(name_link);
1282 }
1283
1284 static void test_search_and_fopen(void) {
1285 const char *dirs[] = {"/tmp/foo/bar", "/tmp", NULL};
1286 char name[] = "/tmp/test-search_and_fopen.XXXXXX";
1287 int fd = -1;
1288 int r;
1289 FILE *f;
1290
1291 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1292 assert_se(fd >= 0);
1293 close(fd);
1294
1295 r = search_and_fopen(basename(name), "r", NULL, dirs, &f);
1296 assert_se(r >= 0);
1297 fclose(f);
1298
1299 r = search_and_fopen(name, "r", NULL, dirs, &f);
1300 assert_se(r >= 0);
1301 fclose(f);
1302
1303 r = search_and_fopen(basename(name), "r", "/", dirs, &f);
1304 assert_se(r >= 0);
1305 fclose(f);
1306
1307 r = search_and_fopen("/a/file/which/does/not/exist/i/guess", "r", NULL, dirs, &f);
1308 assert_se(r < 0);
1309 r = search_and_fopen("afilewhichdoesnotexistiguess", "r", NULL, dirs, &f);
1310 assert_se(r < 0);
1311
1312 r = unlink(name);
1313 assert_se(r == 0);
1314
1315 r = search_and_fopen(basename(name), "r", NULL, dirs, &f);
1316 assert_se(r < 0);
1317 }
1318
1319
1320 static void test_search_and_fopen_nulstr(void) {
1321 const char dirs[] = "/tmp/foo/bar\0/tmp\0";
1322 char name[] = "/tmp/test-search_and_fopen.XXXXXX";
1323 int fd = -1;
1324 int r;
1325 FILE *f;
1326
1327 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1328 assert_se(fd >= 0);
1329 close(fd);
1330
1331 r = search_and_fopen_nulstr(basename(name), "r", NULL, dirs, &f);
1332 assert_se(r >= 0);
1333 fclose(f);
1334
1335 r = search_and_fopen_nulstr(name, "r", NULL, dirs, &f);
1336 assert_se(r >= 0);
1337 fclose(f);
1338
1339 r = search_and_fopen_nulstr("/a/file/which/does/not/exist/i/guess", "r", NULL, dirs, &f);
1340 assert_se(r < 0);
1341 r = search_and_fopen_nulstr("afilewhichdoesnotexistiguess", "r", NULL, dirs, &f);
1342 assert_se(r < 0);
1343
1344 r = unlink(name);
1345 assert_se(r == 0);
1346
1347 r = search_and_fopen_nulstr(basename(name), "r", NULL, dirs, &f);
1348 assert_se(r < 0);
1349 }
1350
1351 static void test_glob_exists(void) {
1352 char name[] = "/tmp/test-glob_exists.XXXXXX";
1353 int fd = -1;
1354 int r;
1355
1356 fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
1357 assert_se(fd >= 0);
1358 close(fd);
1359
1360 r = glob_exists("/tmp/test-glob_exists*");
1361 assert_se(r == 1);
1362
1363 r = unlink(name);
1364 assert_se(r == 0);
1365 r = glob_exists("/tmp/test-glob_exists*");
1366 assert_se(r == 0);
1367 }
1368
1369 static void test_execute_directory(void) {
1370 char template_lo[] = "/tmp/test-readlink_and_make_absolute-lo.XXXXXXX";
1371 char template_hi[] = "/tmp/test-readlink_and_make_absolute-hi.XXXXXXX";
1372 const char * dirs[] = {template_hi, template_lo, NULL};
1373 const char *name, *name2, *name3, *overridden, *override, *masked, *mask;
1374
1375 assert_se(mkdtemp(template_lo));
1376 assert_se(mkdtemp(template_hi));
1377
1378 name = strjoina(template_lo, "/script");
1379 name2 = strjoina(template_hi, "/script2");
1380 name3 = strjoina(template_lo, "/useless");
1381 overridden = strjoina(template_lo, "/overridden");
1382 override = strjoina(template_hi, "/overridden");
1383 masked = strjoina(template_lo, "/masked");
1384 mask = strjoina(template_hi, "/masked");
1385
1386 assert_se(write_string_file(name, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works", WRITE_STRING_FILE_CREATE) == 0);
1387 assert_se(write_string_file(name2, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works2", WRITE_STRING_FILE_CREATE) == 0);
1388 assert_se(write_string_file(overridden, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed", WRITE_STRING_FILE_CREATE) == 0);
1389 assert_se(write_string_file(override, "#!/bin/sh\necho 'Executing '$0", WRITE_STRING_FILE_CREATE) == 0);
1390 assert_se(write_string_file(masked, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed", WRITE_STRING_FILE_CREATE) == 0);
1391 assert_se(symlink("/dev/null", mask) == 0);
1392 assert_se(chmod(name, 0755) == 0);
1393 assert_se(chmod(name2, 0755) == 0);
1394 assert_se(chmod(overridden, 0755) == 0);
1395 assert_se(chmod(override, 0755) == 0);
1396 assert_se(chmod(masked, 0755) == 0);
1397 assert_se(touch(name3) >= 0);
1398
1399 execute_directories(dirs, DEFAULT_TIMEOUT_USEC, NULL);
1400
1401 assert_se(chdir(template_lo) == 0);
1402 assert_se(access("it_works", F_OK) >= 0);
1403 assert_se(access("failed", F_OK) < 0);
1404
1405 assert_se(chdir(template_hi) == 0);
1406 assert_se(access("it_works2", F_OK) >= 0);
1407 assert_se(access("failed", F_OK) < 0);
1408
1409 (void) rm_rf(template_lo, REMOVE_ROOT|REMOVE_PHYSICAL);
1410 (void) rm_rf(template_hi, REMOVE_ROOT|REMOVE_PHYSICAL);
1411 }
1412
1413 static int parse_item(const char *key, const char *value) {
1414 assert_se(key);
1415
1416 log_info("kernel cmdline option <%s> = <%s>", key, strna(value));
1417 return 0;
1418 }
1419
1420 static void test_parse_proc_cmdline(void) {
1421 assert_se(parse_proc_cmdline(parse_item) >= 0);
1422 }
1423
1424 static void test_raw_clone(void) {
1425 pid_t parent, pid, pid2;
1426
1427 parent = getpid();
1428 log_info("before clone: getpid()→"PID_FMT, parent);
1429 assert_se(raw_getpid() == parent);
1430
1431 pid = raw_clone(0, NULL);
1432 assert_se(pid >= 0);
1433
1434 pid2 = raw_getpid();
1435 log_info("raw_clone: "PID_FMT" getpid()→"PID_FMT" raw_getpid()→"PID_FMT,
1436 pid, getpid(), pid2);
1437 if (pid == 0) {
1438 assert_se(pid2 != parent);
1439 _exit(EXIT_SUCCESS);
1440 } else {
1441 int status;
1442
1443 assert_se(pid2 == parent);
1444 waitpid(pid, &status, __WCLONE);
1445 assert_se(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
1446 }
1447 }
1448
1449 static void test_same_fd(void) {
1450 _cleanup_close_pair_ int p[2] = { -1, -1 };
1451 _cleanup_close_ int a = -1, b = -1, c = -1;
1452
1453 assert_se(pipe2(p, O_CLOEXEC) >= 0);
1454 assert_se((a = dup(p[0])) >= 0);
1455 assert_se((b = open("/dev/null", O_RDONLY|O_CLOEXEC)) >= 0);
1456 assert_se((c = dup(a)) >= 0);
1457
1458 assert_se(same_fd(p[0], p[0]) > 0);
1459 assert_se(same_fd(p[1], p[1]) > 0);
1460 assert_se(same_fd(a, a) > 0);
1461 assert_se(same_fd(b, b) > 0);
1462
1463 assert_se(same_fd(a, p[0]) > 0);
1464 assert_se(same_fd(p[0], a) > 0);
1465 assert_se(same_fd(c, p[0]) > 0);
1466 assert_se(same_fd(p[0], c) > 0);
1467 assert_se(same_fd(a, c) > 0);
1468 assert_se(same_fd(c, a) > 0);
1469
1470 assert_se(same_fd(p[0], p[1]) == 0);
1471 assert_se(same_fd(p[1], p[0]) == 0);
1472 assert_se(same_fd(p[0], b) == 0);
1473 assert_se(same_fd(b, p[0]) == 0);
1474 assert_se(same_fd(p[1], a) == 0);
1475 assert_se(same_fd(a, p[1]) == 0);
1476 assert_se(same_fd(p[1], b) == 0);
1477 assert_se(same_fd(b, p[1]) == 0);
1478
1479 assert_se(same_fd(a, b) == 0);
1480 assert_se(same_fd(b, a) == 0);
1481 }
1482
1483 static void test_uid_ptr(void) {
1484
1485 assert_se(UID_TO_PTR(0) != NULL);
1486 assert_se(UID_TO_PTR(1000) != NULL);
1487
1488 assert_se(PTR_TO_UID(UID_TO_PTR(0)) == 0);
1489 assert_se(PTR_TO_UID(UID_TO_PTR(1000)) == 1000);
1490 }
1491
1492 static void test_sparse_write_one(int fd, const char *buffer, size_t n) {
1493 char check[n];
1494
1495 assert_se(lseek(fd, 0, SEEK_SET) == 0);
1496 assert_se(ftruncate(fd, 0) >= 0);
1497 assert_se(sparse_write(fd, buffer, n, 4) == (ssize_t) n);
1498
1499 assert_se(lseek(fd, 0, SEEK_CUR) == (off_t) n);
1500 assert_se(ftruncate(fd, n) >= 0);
1501
1502 assert_se(lseek(fd, 0, SEEK_SET) == 0);
1503 assert_se(read(fd, check, n) == (ssize_t) n);
1504
1505 assert_se(memcmp(buffer, check, n) == 0);
1506 }
1507
1508 static void test_sparse_write(void) {
1509 const char test_a[] = "test";
1510 const char test_b[] = "\0\0\0\0test\0\0\0\0";
1511 const char test_c[] = "\0\0test\0\0\0\0";
1512 const char test_d[] = "\0\0test\0\0\0test\0\0\0\0test\0\0\0\0\0test\0\0\0test\0\0\0\0test\0\0\0\0\0\0\0\0";
1513 const char test_e[] = "test\0\0\0\0test";
1514 _cleanup_close_ int fd = -1;
1515 char fn[] = "/tmp/sparseXXXXXX";
1516
1517 fd = mkostemp(fn, O_CLOEXEC);
1518 assert_se(fd >= 0);
1519 unlink(fn);
1520
1521 test_sparse_write_one(fd, test_a, sizeof(test_a));
1522 test_sparse_write_one(fd, test_b, sizeof(test_b));
1523 test_sparse_write_one(fd, test_c, sizeof(test_c));
1524 test_sparse_write_one(fd, test_d, sizeof(test_d));
1525 test_sparse_write_one(fd, test_e, sizeof(test_e));
1526 }
1527
1528 static void test_shell_escape_one(const char *s, const char *bad, const char *expected) {
1529 _cleanup_free_ char *r;
1530
1531 assert_se(r = shell_escape(s, bad));
1532 assert_se(streq_ptr(r, expected));
1533 }
1534
1535 static void test_shell_escape(void) {
1536 test_shell_escape_one("", "", "");
1537 test_shell_escape_one("\\", "", "\\\\");
1538 test_shell_escape_one("foobar", "", "foobar");
1539 test_shell_escape_one("foobar", "o", "f\\o\\obar");
1540 test_shell_escape_one("foo:bar,baz", ",:", "foo\\:bar\\,baz");
1541 }
1542
1543 static void test_shell_maybe_quote_one(const char *s, const char *expected) {
1544 _cleanup_free_ char *r;
1545
1546 assert_se(r = shell_maybe_quote(s));
1547 assert_se(streq(r, expected));
1548 }
1549
1550 static void test_shell_maybe_quote(void) {
1551
1552 test_shell_maybe_quote_one("", "");
1553 test_shell_maybe_quote_one("\\", "\"\\\\\"");
1554 test_shell_maybe_quote_one("\"", "\"\\\"\"");
1555 test_shell_maybe_quote_one("foobar", "foobar");
1556 test_shell_maybe_quote_one("foo bar", "\"foo bar\"");
1557 test_shell_maybe_quote_one("foo \"bar\" waldo", "\"foo \\\"bar\\\" waldo\"");
1558 test_shell_maybe_quote_one("foo$bar", "\"foo\\$bar\"");
1559 }
1560
1561 static void test_tempfn(void) {
1562 char *ret = NULL, *p;
1563
1564 assert_se(tempfn_xxxxxx("/foo/bar/waldo", NULL, &ret) >= 0);
1565 assert_se(streq_ptr(ret, "/foo/bar/.#waldoXXXXXX"));
1566 free(ret);
1567
1568 assert_se(tempfn_xxxxxx("/foo/bar/waldo", "[miau]", &ret) >= 0);
1569 assert_se(streq_ptr(ret, "/foo/bar/.#[miau]waldoXXXXXX"));
1570 free(ret);
1571
1572 assert_se(tempfn_random("/foo/bar/waldo", NULL, &ret) >= 0);
1573 assert_se(p = startswith(ret, "/foo/bar/.#waldo"));
1574 assert_se(strlen(p) == 16);
1575 assert_se(in_charset(p, "0123456789abcdef"));
1576 free(ret);
1577
1578 assert_se(tempfn_random("/foo/bar/waldo", "[wuff]", &ret) >= 0);
1579 assert_se(p = startswith(ret, "/foo/bar/.#[wuff]waldo"));
1580 assert_se(strlen(p) == 16);
1581 assert_se(in_charset(p, "0123456789abcdef"));
1582 free(ret);
1583
1584 assert_se(tempfn_random_child("/foo/bar/waldo", NULL, &ret) >= 0);
1585 assert_se(p = startswith(ret, "/foo/bar/waldo/.#"));
1586 assert_se(strlen(p) == 16);
1587 assert_se(in_charset(p, "0123456789abcdef"));
1588 free(ret);
1589
1590 assert_se(tempfn_random_child("/foo/bar/waldo", "[kikiriki]", &ret) >= 0);
1591 assert_se(p = startswith(ret, "/foo/bar/waldo/.#[kikiriki]"));
1592 assert_se(strlen(p) == 16);
1593 assert_se(in_charset(p, "0123456789abcdef"));
1594 free(ret);
1595 }
1596
1597 static void test_strcmp_ptr(void) {
1598 assert_se(strcmp_ptr(NULL, NULL) == 0);
1599 assert_se(strcmp_ptr("", NULL) > 0);
1600 assert_se(strcmp_ptr("foo", NULL) > 0);
1601 assert_se(strcmp_ptr(NULL, "") < 0);
1602 assert_se(strcmp_ptr(NULL, "bar") < 0);
1603 assert_se(strcmp_ptr("foo", "bar") > 0);
1604 assert_se(strcmp_ptr("bar", "baz") < 0);
1605 assert_se(strcmp_ptr("foo", "foo") == 0);
1606 assert_se(strcmp_ptr("", "") == 0);
1607 }
1608
1609 static void test_fgetxattrat_fake(void) {
1610 char t[] = "/var/tmp/xattrtestXXXXXX";
1611 _cleanup_close_ int fd = -1;
1612 const char *x;
1613 char v[3] = {};
1614 int r;
1615
1616 assert_se(mkdtemp(t));
1617 x = strjoina(t, "/test");
1618 assert_se(touch(x) >= 0);
1619
1620 r = setxattr(x, "user.foo", "bar", 3, 0);
1621 if (r < 0 && errno == EOPNOTSUPP) /* no xattrs supported on /var/tmp... */
1622 goto cleanup;
1623 assert_se(r >= 0);
1624
1625 fd = open(t, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY);
1626 assert_se(fd >= 0);
1627
1628 assert_se(fgetxattrat_fake(fd, "test", "user.foo", v, 3, 0) >= 0);
1629 assert_se(memcmp(v, "bar", 3) == 0);
1630
1631 safe_close(fd);
1632 fd = open("/", O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY);
1633 assert_se(fd >= 0);
1634 assert_se(fgetxattrat_fake(fd, "usr", "user.idontexist", v, 3, 0) == -ENODATA);
1635
1636 cleanup:
1637 assert_se(unlink(x) >= 0);
1638 assert_se(rmdir(t) >= 0);
1639 }
1640
1641 int main(int argc, char *argv[]) {
1642 log_parse_environment();
1643 log_open();
1644
1645 test_streq_ptr();
1646 test_align_power2();
1647 test_max();
1648 test_container_of();
1649 test_alloca();
1650 test_div_round_up();
1651 test_first_word();
1652 test_close_many();
1653 test_parse_uid();
1654 test_strappend();
1655 test_strstrip();
1656 test_delete_chars();
1657 test_in_charset();
1658 test_hexchar();
1659 test_unhexchar();
1660 test_base32hexchar();
1661 test_unbase32hexchar();
1662 test_base64char();
1663 test_unbase64char();
1664 test_octchar();
1665 test_unoctchar();
1666 test_decchar();
1667 test_undecchar();
1668 test_unhexmem();
1669 test_base32hexmem();
1670 test_unbase32hexmem();
1671 test_base64mem();
1672 test_unbase64mem();
1673 test_cescape();
1674 test_cunescape();
1675 test_foreach_word();
1676 test_foreach_word_quoted();
1677 test_memdup_multiply();
1678 test_u64log2();
1679 test_protect_errno();
1680 test_parse_cpu_set();
1681 test_config_parse_iec_uint64();
1682 test_strextend();
1683 test_strrep();
1684 test_split_pair();
1685 test_fstab_node_to_udev_node();
1686 test_get_files_in_directory();
1687 test_in_set();
1688 test_writing_tmpfile();
1689 test_hexdump();
1690 test_log2i();
1691 test_foreach_string();
1692 test_filename_is_valid();
1693 test_string_has_cc();
1694 test_ascii_strlower();
1695 test_files_same();
1696 test_is_valid_documentation_url();
1697 test_file_in_same_dir();
1698 test_endswith();
1699 test_endswith_no_case();
1700 test_close_nointr();
1701 test_unlink_noerrno();
1702 test_readlink_and_make_absolute();
1703 test_ignore_signals();
1704 test_strshorten();
1705 test_strjoina();
1706 test_is_symlink();
1707 test_search_and_fopen();
1708 test_search_and_fopen_nulstr();
1709 test_glob_exists();
1710 test_execute_directory();
1711 test_parse_proc_cmdline();
1712 test_raw_clone();
1713 test_same_fd();
1714 test_uid_ptr();
1715 test_sparse_write();
1716 test_shell_escape();
1717 test_shell_maybe_quote();
1718 test_tempfn();
1719 test_strcmp_ptr();
1720 test_fgetxattrat_fake();
1721
1722 return 0;
1723 }