]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-strv.c
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / test / test-strv.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
f90cf44c 2/***
f90cf44c 3 Copyright 2010 Lennart Poettering
539ad707 4 Copyright 2013 Thomas H.P. Andersen
f90cf44c
LP
5***/
6
7#include <string.h>
2c4b304e 8
b5efdb8a 9#include "alloc-util.h"
e7b2ea7c 10#include "escape.h"
2c4b304e 11#include "specifier.h"
07630cea 12#include "string-util.h"
682cfdff 13#include "strv.h"
07630cea 14#include "util.h"
f90cf44c 15
3a7719d3 16static void test_specifier_printf(void) {
1731e34a 17 static const Specifier table[] = {
2c4b304e
LP
18 { 'a', specifier_string, (char*) "AAAA" },
19 { 'b', specifier_string, (char*) "BBBB" },
19f6d710
LP
20 { 'm', specifier_machine_id, NULL },
21 { 'B', specifier_boot_id, NULL },
22 { 'H', specifier_host_name, NULL },
23 { 'v', specifier_kernel_release, NULL },
1731e34a 24 {}
2c4b304e
LP
25 };
26
1731e34a
LP
27 _cleanup_free_ char *w = NULL;
28 int r;
29
19f6d710
LP
30 r = specifier_printf("xxx a=%a b=%b yyy", table, NULL, &w);
31 assert_se(r >= 0);
32 assert_se(w);
33
9f316366 34 puts(w);
19f6d710 35 assert_se(streq(w, "xxx a=AAAA b=BBBB yyy"));
9f316366 36
19f6d710
LP
37 free(w);
38 r = specifier_printf("machine=%m, boot=%B, host=%H, version=%v", table, NULL, &w);
39 assert_se(r >= 0);
9f316366 40 assert_se(w);
19f6d710 41 puts(w);
3a7719d3
DB
42}
43
c7bf9d51
ZJS
44static void test_str_in_set(void) {
45 assert_se(STR_IN_SET("x", "x", "y", "z"));
46 assert_se(!STR_IN_SET("X", "x", "y", "z"));
47 assert_se(!STR_IN_SET("", "x", "y", "z"));
48 assert_se(STR_IN_SET("x", "w", "x"));
49}
50
51static void test_strptr_in_set(void) {
52 assert_se(STRPTR_IN_SET("x", "x", "y", "z"));
53 assert_se(!STRPTR_IN_SET("X", "x", "y", "z"));
54 assert_se(!STRPTR_IN_SET("", "x", "y", "z"));
55 assert_se(STRPTR_IN_SET("x", "w", "x"));
56
57 assert_se(!STRPTR_IN_SET(NULL, "x", "y", "z"));
58 assert_se(!STRPTR_IN_SET(NULL, ""));
59 /* strv cannot contain a null, hence the result below */
60 assert_se(!STRPTR_IN_SET(NULL, NULL));
61}
62
a6fde353
ZJS
63static const char* const input_table_multiple[] = {
64 "one",
65 "two",
66 "three",
67 NULL,
68};
69
70static const char* const input_table_one[] = {
71 "one",
72 NULL,
73};
74
75static const char* const input_table_none[] = {
76 NULL,
77};
78
afe773b0
ZJS
79static const char* const input_table_two_empties[] = {
80 "",
81 "",
82 NULL,
83};
84
85static const char* const input_table_one_empty[] = {
86 "",
87 NULL,
88};
89
a6fde353
ZJS
90static void test_strv_find(void) {
91 assert_se(strv_find((char **)input_table_multiple, "three"));
92 assert_se(!strv_find((char **)input_table_multiple, "four"));
539ad707
TA
93}
94
95static void test_strv_find_prefix(void) {
a6fde353
ZJS
96 assert_se(strv_find_prefix((char **)input_table_multiple, "o"));
97 assert_se(strv_find_prefix((char **)input_table_multiple, "one"));
98 assert_se(strv_find_prefix((char **)input_table_multiple, ""));
99 assert_se(!strv_find_prefix((char **)input_table_multiple, "xxx"));
100 assert_se(!strv_find_prefix((char **)input_table_multiple, "onee"));
3a7719d3 101}
f90cf44c 102
7bd57a87
RC
103static void test_strv_find_startswith(void) {
104 char *r;
105
106 r = strv_find_startswith((char **)input_table_multiple, "o");
107 assert_se(r && streq(r, "ne"));
108
109 r = strv_find_startswith((char **)input_table_multiple, "one");
110 assert_se(r && streq(r, ""));
111
112 r = strv_find_startswith((char **)input_table_multiple, "");
113 assert_se(r && streq(r, "one"));
114
115 assert_se(!strv_find_startswith((char **)input_table_multiple, "xxx"));
116 assert_se(!strv_find_startswith((char **)input_table_multiple, "onee"));
117}
118
682cfdff 119static void test_strv_join(void) {
afe773b0 120 _cleanup_free_ char *p = NULL, *q = NULL, *r = NULL, *s = NULL, *t = NULL, *v = NULL, *w = NULL;
682cfdff 121
539ad707 122 p = strv_join((char **)input_table_multiple, ", ");
04045d84 123 assert_se(p);
7b68d618 124 assert_se(streq(p, "one, two, three"));
682cfdff 125
539ad707 126 q = strv_join((char **)input_table_multiple, ";");
04045d84 127 assert_se(q);
7b68d618 128 assert_se(streq(q, "one;two;three"));
682cfdff
DB
129
130 r = strv_join((char **)input_table_multiple, NULL);
04045d84 131 assert_se(r);
7b68d618 132 assert_se(streq(r, "one two three"));
539ad707
TA
133
134 s = strv_join((char **)input_table_one, ", ");
04045d84 135 assert_se(s);
7b68d618 136 assert_se(streq(s, "one"));
539ad707
TA
137
138 t = strv_join((char **)input_table_none, ", ");
04045d84 139 assert_se(t);
7b68d618 140 assert_se(streq(t, ""));
afe773b0
ZJS
141
142 v = strv_join((char **)input_table_two_empties, ", ");
143 assert_se(v);
144 assert_se(streq(v, ", "));
145
146 w = strv_join((char **)input_table_one_empty, ", ");
147 assert_se(w);
148 assert_se(streq(w, ""));
682cfdff
DB
149}
150
30bcc052 151static void test_strv_unquote(const char *quoted, char **list) {
70f75a52 152 _cleanup_strv_free_ char **s;
a2a5291b 153 _cleanup_free_ char *j;
70f75a52
LP
154 unsigned i = 0;
155 char **t;
b2fadec6 156 int r;
70f75a52 157
8adaf7bd 158 r = strv_split_extract(&s, quoted, WHITESPACE, EXTRACT_QUOTES);
8dd4c05b 159 assert_se(r == (int) strv_length(list));
70f75a52 160 assert_se(s);
a2a5291b 161 j = strv_join(s, " | ");
bdf7026e 162 assert_se(j);
a2a5291b 163 puts(j);
70f75a52
LP
164
165 STRV_FOREACH(t, s)
166 assert_se(streq(list[i++], *t));
167
168 assert_se(list[i] == NULL);
169}
170
a2a5291b 171static void test_invalid_unquote(const char *quoted) {
b2fadec6
ZJS
172 char **s = NULL;
173 int r;
a2a5291b 174
8adaf7bd 175 r = strv_split_extract(&s, quoted, WHITESPACE, EXTRACT_QUOTES);
bdf7026e
TA
176 assert_se(s == NULL);
177 assert_se(r == -EINVAL);
a2a5291b
ZJS
178}
179
aed2ebfe
DB
180static void test_strv_split(void) {
181 char **s;
182 unsigned i = 0;
183 _cleanup_strv_free_ char **l = NULL;
184 const char str[] = "one,two,three";
185
186 l = strv_split(str, ",");
187
bdf7026e 188 assert_se(l);
aed2ebfe
DB
189
190 STRV_FOREACH(s, l) {
191 assert_se(streq(*s, input_table_multiple[i++]));
192 }
193}
194
8adaf7bd
RM
195static void test_strv_split_extract(void) {
196 _cleanup_strv_free_ char **l = NULL;
197 const char *str = ":foo\\:bar::waldo:";
198 int r;
199
200 r = strv_split_extract(&l, str, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
8dd4c05b 201 assert_se(r == (int) strv_length(l));
8adaf7bd
RM
202 assert_se(streq_ptr(l[0], ""));
203 assert_se(streq_ptr(l[1], "foo:bar"));
204 assert_se(streq_ptr(l[2], ""));
205 assert_se(streq_ptr(l[3], "waldo"));
206 assert_se(streq_ptr(l[4], ""));
207 assert_se(streq_ptr(l[5], NULL));
208}
209
aed2ebfe
DB
210static void test_strv_split_newlines(void) {
211 unsigned i = 0;
212 char **s;
213 _cleanup_strv_free_ char **l = NULL;
214 const char str[] = "one\ntwo\nthree";
215
216 l = strv_split_newlines(str);
217
bdf7026e 218 assert_se(l);
aed2ebfe
DB
219
220 STRV_FOREACH(s, l) {
221 assert_se(streq(*s, input_table_multiple[i++]));
222 }
223}
224
2f213f74
DB
225static void test_strv_split_nulstr(void) {
226 _cleanup_strv_free_ char **l = NULL;
227 const char nulstr[] = "str0\0str1\0str2\0str3\0";
228
229 l = strv_split_nulstr (nulstr);
04045d84 230 assert_se(l);
2f213f74
DB
231
232 assert_se(streq(l[0], "str0"));
233 assert_se(streq(l[1], "str1"));
234 assert_se(streq(l[2], "str2"));
235 assert_se(streq(l[3], "str3"));
236}
237
10ddd913
TA
238static void test_strv_parse_nulstr(void) {
239 _cleanup_strv_free_ char **l = NULL;
240 const char nulstr[] = "fuck\0fuck2\0fuck3\0\0fuck5\0\0xxx";
241
242 l = strv_parse_nulstr(nulstr, sizeof(nulstr)-1);
04045d84 243 assert_se(l);
10ddd913
TA
244 puts("Parse nulstr:");
245 strv_print(l);
246
7b68d618
DB
247 assert_se(streq(l[0], "fuck"));
248 assert_se(streq(l[1], "fuck2"));
249 assert_se(streq(l[2], "fuck3"));
250 assert_se(streq(l[3], ""));
251 assert_se(streq(l[4], "fuck5"));
252 assert_se(streq(l[5], ""));
253 assert_se(streq(l[6], "xxx"));
10ddd913
TA
254}
255
539ad707
TA
256static void test_strv_overlap(void) {
257 const char * const input_table[] = {
258 "one",
259 "two",
260 "three",
261 NULL
262 };
263 const char * const input_table_overlap[] = {
264 "two",
265 NULL
266 };
267 const char * const input_table_unique[] = {
268 "four",
269 "five",
270 "six",
271 NULL
272 };
273
7b68d618
DB
274 assert_se(strv_overlap((char **)input_table, (char**)input_table_overlap));
275 assert_se(!strv_overlap((char **)input_table, (char**)input_table_unique));
539ad707
TA
276}
277
278static void test_strv_sort(void) {
1e64bbc1 279 const char* input_table[] = {
539ad707
TA
280 "durian",
281 "apple",
282 "citrus",
283 "CAPITAL LETTERS FIRST",
284 "banana",
285 NULL
286 };
287
288 strv_sort((char **)input_table);
289
7b68d618
DB
290 assert_se(streq(input_table[0], "CAPITAL LETTERS FIRST"));
291 assert_se(streq(input_table[1], "apple"));
292 assert_se(streq(input_table[2], "banana"));
293 assert_se(streq(input_table[3], "citrus"));
294 assert_se(streq(input_table[4], "durian"));
3a7719d3 295}
e3aa71c3 296
e3e45d4f 297static void test_strv_extend_strv_concat(void) {
7d6884b6 298 _cleanup_strv_free_ char **a = NULL, **b = NULL;
343a8969 299
7b68d618
DB
300 a = strv_new("without", "suffix", NULL);
301 b = strv_new("with", "suffix", NULL);
04045d84
DB
302 assert_se(a);
303 assert_se(b);
343a8969 304
e3e45d4f 305 assert_se(strv_extend_strv_concat(&a, b, "_suffix") >= 0);
343a8969 306
e3e45d4f
SP
307 assert_se(streq(a[0], "without"));
308 assert_se(streq(a[1], "suffix"));
309 assert_se(streq(a[2], "with_suffix"));
310 assert_se(streq(a[3], "suffix_suffix"));
343a8969
DB
311}
312
e3e45d4f 313static void test_strv_extend_strv(void) {
ea9b54f8 314 _cleanup_strv_free_ char **a = NULL, **b = NULL, **n = NULL;
a1022300 315
7b68d618 316 a = strv_new("abc", "def", "ghi", NULL);
e287086b 317 b = strv_new("jkl", "mno", "abc", "pqr", NULL);
04045d84
DB
318 assert_se(a);
319 assert_se(b);
a1022300 320
e287086b 321 assert_se(strv_extend_strv(&a, b, true) == 3);
a1022300 322
e3e45d4f
SP
323 assert_se(streq(a[0], "abc"));
324 assert_se(streq(a[1], "def"));
325 assert_se(streq(a[2], "ghi"));
326 assert_se(streq(a[3], "jkl"));
327 assert_se(streq(a[4], "mno"));
328 assert_se(streq(a[5], "pqr"));
e3e45d4f 329 assert_se(strv_length(a) == 6);
ea9b54f8
ZJS
330
331 assert_se(strv_extend_strv(&n, b, false) >= 0);
332 assert_se(streq(n[0], "jkl"));
333 assert_se(streq(n[1], "mno"));
334 assert_se(streq(n[2], "abc"));
335 assert_se(streq(n[3], "pqr"));
336 assert_se(strv_length(n) == 4);
a1022300
DB
337}
338
e3e45d4f
SP
339static void test_strv_extend(void) {
340 _cleanup_strv_free_ char **a = NULL, **b = NULL;
40857008
DB
341
342 a = strv_new("test", "test1", NULL);
04045d84 343 assert_se(a);
e3e45d4f
SP
344 assert_se(strv_extend(&a, "test2") >= 0);
345 assert_se(strv_extend(&b, "test3") >= 0);
40857008 346
e3e45d4f
SP
347 assert_se(streq(a[0], "test"));
348 assert_se(streq(a[1], "test1"));
349 assert_se(streq(a[2], "test2"));
350 assert_se(streq(b[0], "test3"));
40857008
DB
351}
352
4a336a69
RC
353static void test_strv_extendf(void) {
354 _cleanup_strv_free_ char **a = NULL, **b = NULL;
355
356 a = strv_new("test", "test1", NULL);
357 assert_se(a);
358 assert_se(strv_extendf(&a, "test2 %s %d %s", "foo", 128, "bar") >= 0);
359 assert_se(strv_extendf(&b, "test3 %s %s %d", "bar", "foo", 128) >= 0);
360
361 assert_se(streq(a[0], "test"));
362 assert_se(streq(a[1], "test1"));
363 assert_se(streq(a[2], "test2 foo 128 bar"));
364 assert_se(streq(b[0], "test3 bar foo 128"));
365}
366
02f19706 367static void test_strv_foreach(void) {
250a918d
LP
368 _cleanup_strv_free_ char **a;
369 unsigned i = 0;
370 char **check;
02f19706 371
250a918d 372 a = strv_new("one", "two", "three", NULL);
02f19706 373
250a918d 374 assert_se(a);
02f19706 375
250a918d
LP
376 STRV_FOREACH(check, a) {
377 assert_se(streq(*check, input_table_multiple[i++]));
378 }
02f19706
DB
379}
380
381static void test_strv_foreach_backwards(void) {
250a918d
LP
382 _cleanup_strv_free_ char **a;
383 unsigned i = 2;
384 char **check;
02f19706 385
250a918d 386 a = strv_new("one", "two", "three", NULL);
02f19706 387
250a918d 388 assert_se(a);
02f19706 389
4a39c774 390 STRV_FOREACH_BACKWARDS(check, a)
5fba7bbf 391 assert_se(streq_ptr(*check, input_table_multiple[i--]));
4a39c774
LP
392
393 STRV_FOREACH_BACKWARDS(check, (char**) NULL)
394 assert_not_reached("Let's see that we check empty strv right, too.");
395
396 STRV_FOREACH_BACKWARDS(check, (char**) { NULL })
397 assert_not_reached("Let's see that we check empty strv right, too.");
02f19706
DB
398}
399
4c325b2e
DB
400static void test_strv_foreach_pair(void) {
401 _cleanup_strv_free_ char **a = NULL;
402 char **x, **y;
403
404 a = strv_new("pair_one", "pair_one",
405 "pair_two", "pair_two",
406 "pair_three", "pair_three",
407 NULL);
408
409 STRV_FOREACH_PAIR(x, y, a) {
410 assert_se(streq(*x, *y));
411 }
412}
413
897e7561 414static void test_strv_from_stdarg_alloca_one(char **l, const char *first, ...) {
250a918d
LP
415 char **j;
416 unsigned i;
417
418 j = strv_from_stdarg_alloca(first);
419
420 for (i = 0;; i++) {
421 assert_se(streq_ptr(l[i], j[i]));
422
423 if (!l[i])
424 break;
425 }
426}
427
428static void test_strv_from_stdarg_alloca(void) {
897e7561
LP
429 test_strv_from_stdarg_alloca_one(STRV_MAKE("foo", "bar"), "foo", "bar", NULL);
430 test_strv_from_stdarg_alloca_one(STRV_MAKE("foo"), "foo", NULL);
431 test_strv_from_stdarg_alloca_one(STRV_MAKE_EMPTY, NULL);
250a918d
LP
432}
433
6e888894
ZJS
434static void test_strv_insert(void) {
435 _cleanup_strv_free_ char **a = NULL;
436
437 assert_se(strv_insert(&a, 0, strdup("first")) == 0);
438 assert_se(streq(a[0], "first"));
439 assert_se(!a[1]);
440
441 assert_se(strv_insert(&a, 0, NULL) == 0);
442 assert_se(streq(a[0], "first"));
443 assert_se(!a[1]);
444
445 assert_se(strv_insert(&a, 1, strdup("two")) == 0);
446 assert_se(streq(a[0], "first"));
447 assert_se(streq(a[1], "two"));
448 assert_se(!a[2]);
449
450 assert_se(strv_insert(&a, 4, strdup("tri")) == 0);
451 assert_se(streq(a[0], "first"));
452 assert_se(streq(a[1], "two"));
453 assert_se(streq(a[2], "tri"));
454 assert_se(!a[3]);
455
456 assert_se(strv_insert(&a, 1, strdup("duo")) == 0);
457 assert_se(streq(a[0], "first"));
458 assert_se(streq(a[1], "duo"));
459 assert_se(streq(a[2], "two"));
460 assert_se(streq(a[3], "tri"));
461 assert_se(!a[4]);
462}
463
7bd57a87
RC
464static void test_strv_push_prepend(void) {
465 _cleanup_strv_free_ char **a = NULL;
466
467 a = strv_new("foo", "bar", "three", NULL);
468
469 assert_se(strv_push_prepend(&a, strdup("first")) >= 0);
470 assert_se(streq(a[0], "first"));
471 assert_se(streq(a[1], "foo"));
472 assert_se(streq(a[2], "bar"));
473 assert_se(streq(a[3], "three"));
474 assert_se(!a[4]);
475
476 assert_se(strv_consume_prepend(&a, strdup("first2")) >= 0);
477 assert_se(streq(a[0], "first2"));
478 assert_se(streq(a[1], "first"));
479 assert_se(streq(a[2], "foo"));
480 assert_se(streq(a[3], "bar"));
481 assert_se(streq(a[4], "three"));
482 assert_se(!a[5]);
483}
484
98940a3c
LP
485static void test_strv_push(void) {
486 _cleanup_strv_free_ char **a = NULL;
487 char *i, *j;
488
489 assert_se(i = strdup("foo"));
490 assert_se(strv_push(&a, i) >= 0);
491
492 assert_se(i = strdup("a"));
493 assert_se(j = strdup("b"));
494 assert_se(strv_push_pair(&a, i, j) >= 0);
495
496 assert_se(streq_ptr(a[0], "foo"));
497 assert_se(streq_ptr(a[1], "a"));
498 assert_se(streq_ptr(a[2], "b"));
499 assert_se(streq_ptr(a[3], NULL));
500}
501
e74aa253
RC
502static void test_strv_equal(void) {
503 _cleanup_strv_free_ char **a = NULL;
504 _cleanup_strv_free_ char **b = NULL;
505 _cleanup_strv_free_ char **c = NULL;
506
507 a = strv_new("one", "two", "three", NULL);
508 assert_se(a);
509 b = strv_new("one", "two", "three", NULL);
510 assert_se(a);
511 c = strv_new("one", "two", "three", "four", NULL);
512 assert_se(a);
513
514 assert_se(strv_equal(a, a));
515 assert_se(strv_equal(a, b));
516 assert_se(strv_equal(NULL, NULL));
517
518 assert_se(!strv_equal(a, c));
519 assert_se(!strv_equal(b, c));
520 assert_se(!strv_equal(b, NULL));
521}
522
e1dd6790
LP
523static void test_strv_is_uniq(void) {
524 _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL, **d = NULL;
525
526 a = strv_new(NULL, NULL);
527 assert_se(a);
528 assert_se(strv_is_uniq(a));
529
530 b = strv_new("foo", NULL);
531 assert_se(b);
532 assert_se(strv_is_uniq(b));
533
534 c = strv_new("foo", "bar", NULL);
535 assert_se(c);
536 assert_se(strv_is_uniq(c));
537
538 d = strv_new("foo", "bar", "waldo", "bar", "piep", NULL);
539 assert_se(d);
540 assert_se(!strv_is_uniq(d));
541}
542
543static void test_strv_reverse(void) {
544 _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL, **d = NULL;
545
546 a = strv_new(NULL, NULL);
547 assert_se(a);
548
549 strv_reverse(a);
550 assert_se(strv_isempty(a));
551
552 b = strv_new("foo", NULL);
553 assert_se(b);
554 strv_reverse(b);
555 assert_se(streq_ptr(b[0], "foo"));
556 assert_se(streq_ptr(b[1], NULL));
557
558 c = strv_new("foo", "bar", NULL);
559 assert_se(c);
560 strv_reverse(c);
561 assert_se(streq_ptr(c[0], "bar"));
562 assert_se(streq_ptr(c[1], "foo"));
563 assert_se(streq_ptr(c[2], NULL));
564
565 d = strv_new("foo", "bar", "waldo", NULL);
566 assert_se(d);
567 strv_reverse(d);
568 assert_se(streq_ptr(d[0], "waldo"));
569 assert_se(streq_ptr(d[1], "bar"));
570 assert_se(streq_ptr(d[2], "foo"));
571 assert_se(streq_ptr(d[3], NULL));
572}
573
04c14b25
RM
574static void test_strv_shell_escape(void) {
575 _cleanup_strv_free_ char **v = NULL;
576
577 v = strv_new("foo:bar", "bar,baz", "wal\\do", NULL);
578 assert_se(v);
579 assert_se(strv_shell_escape(v, ",:"));
580 assert_se(streq_ptr(v[0], "foo\\:bar"));
581 assert_se(streq_ptr(v[1], "bar\\,baz"));
582 assert_se(streq_ptr(v[2], "wal\\\\do"));
583 assert_se(streq_ptr(v[3], NULL));
584}
585
e3ead6bb
LP
586static void test_strv_skip_one(char **a, size_t n, char **b) {
587 a = strv_skip(a, n);
588 assert_se(strv_equal(a, b));
589}
590
591static void test_strv_skip(void) {
592 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 0, STRV_MAKE("foo", "bar", "baz"));
593 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 1, STRV_MAKE("bar", "baz"));
594 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 2, STRV_MAKE("baz"));
595 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 3, STRV_MAKE(NULL));
596 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 4, STRV_MAKE(NULL));
597 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 55, STRV_MAKE(NULL));
598
599 test_strv_skip_one(STRV_MAKE("quux"), 0, STRV_MAKE("quux"));
600 test_strv_skip_one(STRV_MAKE("quux"), 1, STRV_MAKE(NULL));
601 test_strv_skip_one(STRV_MAKE("quux"), 55, STRV_MAKE(NULL));
602
603 test_strv_skip_one(STRV_MAKE(NULL), 0, STRV_MAKE(NULL));
604 test_strv_skip_one(STRV_MAKE(NULL), 1, STRV_MAKE(NULL));
605 test_strv_skip_one(STRV_MAKE(NULL), 55, STRV_MAKE(NULL));
606}
607
8dd4c05b
LP
608static void test_strv_extend_n(void) {
609 _cleanup_strv_free_ char **v = NULL;
610
611 v = strv_new("foo", "bar", NULL);
612 assert_se(v);
613
614 assert_se(strv_extend_n(&v, "waldo", 3) >= 0);
615 assert_se(strv_extend_n(&v, "piep", 2) >= 0);
616
617 assert_se(streq(v[0], "foo"));
618 assert_se(streq(v[1], "bar"));
619 assert_se(streq(v[2], "waldo"));
620 assert_se(streq(v[3], "waldo"));
621 assert_se(streq(v[4], "waldo"));
622 assert_se(streq(v[5], "piep"));
623 assert_se(streq(v[6], "piep"));
624 assert_se(v[7] == NULL);
625
626 v = strv_free(v);
627
628 assert_se(strv_extend_n(&v, "foo", 1) >= 0);
629 assert_se(strv_extend_n(&v, "bar", 0) >= 0);
630
631 assert_se(streq(v[0], "foo"));
632 assert_se(v[1] == NULL);
633}
634
e287086b
LP
635static void test_strv_make_nulstr_one(char **l) {
636 _cleanup_free_ char *b = NULL, *c = NULL;
637 _cleanup_strv_free_ char **q = NULL;
f41794d0 638 const char *s = NULL;
e287086b 639 size_t n, m;
f41794d0 640 unsigned i = 0;
e287086b
LP
641
642 assert_se(strv_make_nulstr(l, &b, &n) >= 0);
643 assert_se(q = strv_parse_nulstr(b, n));
644 assert_se(strv_equal(l, q));
645
646 assert_se(strv_make_nulstr(q, &c, &m) >= 0);
647 assert_se(m == n);
648 assert_se(memcmp(b, c, m) == 0);
f41794d0 649
b60df13b 650 NULSTR_FOREACH(s, b)
f41794d0 651 assert_se(streq(s, l[i++]));
f41794d0 652 assert_se(i == strv_length(l));
e287086b
LP
653}
654
655static void test_strv_make_nulstr(void) {
656 test_strv_make_nulstr_one(NULL);
657 test_strv_make_nulstr_one(STRV_MAKE(NULL));
658 test_strv_make_nulstr_one(STRV_MAKE("foo"));
659 test_strv_make_nulstr_one(STRV_MAKE("foo", "bar"));
660 test_strv_make_nulstr_one(STRV_MAKE("foo", "bar", "quuux"));
661}
662
bff8f950
ZJS
663static void test_strv_free_free(void) {
664 char ***t;
665
666 assert_se(t = new(char**, 3));
667 assert_se(t[0] = strv_new("a", "b", NULL));
668 assert_se(t[1] = strv_new("c", "d", "e", NULL));
669 t[2] = NULL;
670
671 t = strv_free_free(t);
672}
673
ebde5cb2
RC
674static void test_foreach_string(void) {
675 const char * const t[] = {
676 "foo",
677 "bar",
678 "waldo",
679 NULL
680 };
681 const char *x;
682 unsigned i = 0;
683
684 FOREACH_STRING(x, "foo", "bar", "waldo")
685 assert_se(streq_ptr(t[i++], x));
686
687 assert_se(i == 3);
688
689 FOREACH_STRING(x, "zzz")
690 assert_se(streq(x, "zzz"));
691}
692
2027927b 693static void test_strv_fnmatch(void) {
57681e84 694 _cleanup_strv_free_ char **v = NULL;
2027927b
EV
695
696 assert_se(!strv_fnmatch(STRV_MAKE_EMPTY, "a", 0));
697
698 v = strv_new("*\\*", NULL);
699 assert_se(!strv_fnmatch(v, "\\", 0));
700 assert_se(strv_fnmatch(v, "\\", FNM_NOESCAPE));
701}
702
3a7719d3 703int main(int argc, char *argv[]) {
3a7719d3 704 test_specifier_printf();
c7bf9d51
ZJS
705 test_str_in_set();
706 test_strptr_in_set();
02f19706
DB
707 test_strv_foreach();
708 test_strv_foreach_backwards();
4c325b2e 709 test_strv_foreach_pair();
539ad707
TA
710 test_strv_find();
711 test_strv_find_prefix();
7bd57a87 712 test_strv_find_startswith();
682cfdff 713 test_strv_join();
a6fde353 714
30bcc052
ZJS
715 test_strv_unquote(" foo=bar \"waldo\" zzz ", STRV_MAKE("foo=bar", "waldo", "zzz"));
716 test_strv_unquote("", STRV_MAKE_EMPTY);
717 test_strv_unquote(" ", STRV_MAKE_EMPTY);
718 test_strv_unquote(" ", STRV_MAKE_EMPTY);
719 test_strv_unquote(" x", STRV_MAKE("x"));
720 test_strv_unquote("x ", STRV_MAKE("x"));
721 test_strv_unquote(" x ", STRV_MAKE("x"));
722 test_strv_unquote(" \"x\" ", STRV_MAKE("x"));
723 test_strv_unquote(" 'x' ", STRV_MAKE("x"));
724 test_strv_unquote(" 'x\"' ", STRV_MAKE("x\""));
725 test_strv_unquote(" \"x'\" ", STRV_MAKE("x'"));
726 test_strv_unquote("a '--b=c \"d e\"'", STRV_MAKE("a", "--b=c \"d e\""));
73381fcf 727
ba774317
ZJS
728 /* trailing backslashes */
729 test_strv_unquote(" x\\\\", STRV_MAKE("x\\"));
730 test_invalid_unquote(" x\\");
731
f88e6be5
LP
732 test_invalid_unquote("a --b='c \"d e\"''");
733 test_invalid_unquote("a --b='c \"d e\" '\"");
a2a5291b 734 test_invalid_unquote("a --b='c \"d e\"garbage");
b2fadec6
ZJS
735 test_invalid_unquote("'");
736 test_invalid_unquote("\"");
f88e6be5 737 test_invalid_unquote("'x'y'g");
70f75a52 738
aed2ebfe 739 test_strv_split();
8adaf7bd 740 test_strv_split_extract();
aed2ebfe 741 test_strv_split_newlines();
2f213f74 742 test_strv_split_nulstr();
10ddd913 743 test_strv_parse_nulstr();
539ad707
TA
744 test_strv_overlap();
745 test_strv_sort();
e3e45d4f
SP
746 test_strv_extend_strv();
747 test_strv_extend_strv_concat();
748 test_strv_extend();
4a336a69 749 test_strv_extendf();
250a918d 750 test_strv_from_stdarg_alloca();
6e888894 751 test_strv_insert();
7bd57a87 752 test_strv_push_prepend();
98940a3c 753 test_strv_push();
e74aa253 754 test_strv_equal();
e1dd6790
LP
755 test_strv_is_uniq();
756 test_strv_reverse();
04c14b25 757 test_strv_shell_escape();
e3ead6bb 758 test_strv_skip();
8dd4c05b 759 test_strv_extend_n();
e287086b 760 test_strv_make_nulstr();
bff8f950 761 test_strv_free_free();
2c4b304e 762
ebde5cb2 763 test_foreach_string();
2027927b 764 test_strv_fnmatch();
ebde5cb2 765
f90cf44c
LP
766 return 0;
767}