]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-strv.c
shutdown: Make all mounts private
[thirdparty/systemd.git] / src / test / test-strv.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "escape.h"
5 #include "string-util.h"
6 #include "strv.h"
7 #include "tests.h"
8
9 TEST(str_in_set) {
10 assert_se(STR_IN_SET("x", "x", "y", "z"));
11 assert_se(!STR_IN_SET("X", "x", "y", "z"));
12 assert_se(!STR_IN_SET("", "x", "y", "z"));
13 assert_se(STR_IN_SET("x", "w", "x"));
14 }
15
16 TEST(strptr_in_set) {
17 assert_se(STRPTR_IN_SET("x", "x", "y", "z"));
18 assert_se(!STRPTR_IN_SET("X", "x", "y", "z"));
19 assert_se(!STRPTR_IN_SET("", "x", "y", "z"));
20 assert_se(STRPTR_IN_SET("x", "w", "x"));
21
22 assert_se(!STRPTR_IN_SET(NULL, "x", "y", "z"));
23 assert_se(!STRPTR_IN_SET(NULL, ""));
24 /* strv cannot contain a null, hence the result below */
25 assert_se(!STRPTR_IN_SET(NULL, NULL));
26 }
27
28 TEST(startswith_set) {
29 assert_se(!STARTSWITH_SET("foo", "bar", "baz", "waldo"));
30 assert_se(!STARTSWITH_SET("foo", "bar"));
31
32 assert_se(STARTSWITH_SET("abc", "a", "ab", "abc"));
33 assert_se(STARTSWITH_SET("abc", "ax", "ab", "abc"));
34 assert_se(STARTSWITH_SET("abc", "ax", "abx", "abc"));
35 assert_se(!STARTSWITH_SET("abc", "ax", "abx", "abcx"));
36
37 assert_se(streq_ptr(STARTSWITH_SET("foobar", "hhh", "kkk", "foo", "zzz"), "bar"));
38 assert_se(streq_ptr(STARTSWITH_SET("foobar", "hhh", "kkk", "", "zzz"), "foobar"));
39 assert_se(streq_ptr(STARTSWITH_SET("", "hhh", "kkk", "zzz", ""), ""));
40 }
41
42 static const char* const input_table_multiple[] = {
43 "one",
44 "two",
45 "three",
46 NULL,
47 };
48
49 static const char* const input_table_quoted[] = {
50 "one",
51 " two\t three ",
52 " four five",
53 NULL,
54 };
55
56 static const char* const input_table_quoted_joined[] = {
57 "one",
58 " two\t three " " four five",
59 NULL,
60 };
61
62 static const char* const input_table_one[] = {
63 "one",
64 NULL,
65 };
66
67 static const char* const input_table_none[] = {
68 NULL,
69 };
70
71 static const char* const input_table_two_empties[] = {
72 "",
73 "",
74 NULL,
75 };
76
77 static const char* const input_table_one_empty[] = {
78 "",
79 NULL,
80 };
81
82 static const char* const input_table_unescape[] = {
83 "ID_VENDOR=QEMU",
84 "ID_VENDOR_ENC=QEMUx20x20x20x20",
85 "ID_MODEL_ENC=QEMUx20HARDDISKx20x20x20",
86 NULL,
87 };
88
89 static const char* const input_table_retain_escape[] = {
90 "ID_VENDOR=QEMU",
91 "ID_VENDOR_ENC=QEMU\\x20\\x20\\x20\\x20",
92 "ID_MODEL_ENC=QEMU\\x20HARDDISK\\x20\\x20\\x20",
93 NULL,
94 };
95
96 TEST(strv_find) {
97 assert_se(strv_find((char **)input_table_multiple, "three"));
98 assert_se(!strv_find((char **)input_table_multiple, "four"));
99 }
100
101 TEST(strv_find_prefix) {
102 assert_se(strv_find_prefix((char **)input_table_multiple, "o"));
103 assert_se(strv_find_prefix((char **)input_table_multiple, "one"));
104 assert_se(strv_find_prefix((char **)input_table_multiple, ""));
105 assert_se(!strv_find_prefix((char **)input_table_multiple, "xxx"));
106 assert_se(!strv_find_prefix((char **)input_table_multiple, "onee"));
107 }
108
109 TEST(strv_find_startswith) {
110 char *r;
111
112 r = strv_find_startswith((char **)input_table_multiple, "o");
113 assert_se(r && streq(r, "ne"));
114
115 r = strv_find_startswith((char **)input_table_multiple, "one");
116 assert_se(r && streq(r, ""));
117
118 r = strv_find_startswith((char **)input_table_multiple, "");
119 assert_se(r && streq(r, "one"));
120
121 assert_se(!strv_find_startswith((char **)input_table_multiple, "xxx"));
122 assert_se(!strv_find_startswith((char **)input_table_multiple, "onee"));
123 }
124
125 TEST(strv_join) {
126 _cleanup_free_ char *p = strv_join((char **)input_table_multiple, ", ");
127 assert_se(p);
128 assert_se(streq(p, "one, two, three"));
129
130 _cleanup_free_ char *q = strv_join((char **)input_table_multiple, ";");
131 assert_se(q);
132 assert_se(streq(q, "one;two;three"));
133
134 _cleanup_free_ char *r = strv_join((char **)input_table_multiple, NULL);
135 assert_se(r);
136 assert_se(streq(r, "one two three"));
137
138 _cleanup_free_ char *s = strv_join(STRV_MAKE("1", "2", "3,3"), ",");
139 assert_se(s);
140 assert_se(streq(s, "1,2,3,3"));
141
142 _cleanup_free_ char *t = strv_join((char **)input_table_one, ", ");
143 assert_se(t);
144 assert_se(streq(t, "one"));
145
146 _cleanup_free_ char *u = strv_join((char **)input_table_none, ", ");
147 assert_se(u);
148 assert_se(streq(u, ""));
149
150 _cleanup_free_ char *v = strv_join((char **)input_table_two_empties, ", ");
151 assert_se(v);
152 assert_se(streq(v, ", "));
153
154 _cleanup_free_ char *w = strv_join((char **)input_table_one_empty, ", ");
155 assert_se(w);
156 assert_se(streq(w, ""));
157 }
158
159 TEST(strv_join_full) {
160 _cleanup_free_ char *p = strv_join_full((char **)input_table_multiple, ", ", "foo", false);
161 assert_se(p);
162 assert_se(streq(p, "fooone, footwo, foothree"));
163
164 _cleanup_free_ char *q = strv_join_full((char **)input_table_multiple, ";", "foo", false);
165 assert_se(q);
166 assert_se(streq(q, "fooone;footwo;foothree"));
167
168 _cleanup_free_ char *r = strv_join_full(STRV_MAKE("a", "a;b", "a:c"), ";", NULL, true);
169 assert_se(r);
170 assert_se(streq(r, "a;a\\;b;a:c"));
171
172 _cleanup_free_ char *s = strv_join_full(STRV_MAKE("a", "a;b", "a;;c", ";", ";x"), ";", NULL, true);
173 assert_se(s);
174 assert_se(streq(s, "a;a\\;b;a\\;\\;c;\\;;\\;x"));
175
176 _cleanup_free_ char *t = strv_join_full(STRV_MAKE("a", "a;b", "a:c", ";"), ";", "=", true);
177 assert_se(t);
178 assert_se(streq(t, "=a;=a\\;b;=a:c;=\\;"));
179 t = mfree(t);
180
181 _cleanup_free_ char *u = strv_join_full((char **)input_table_multiple, NULL, "foo", false);
182 assert_se(u);
183 assert_se(streq(u, "fooone footwo foothree"));
184
185 _cleanup_free_ char *v = strv_join_full((char **)input_table_one, ", ", "foo", false);
186 assert_se(v);
187 assert_se(streq(v, "fooone"));
188
189 _cleanup_free_ char *w = strv_join_full((char **)input_table_none, ", ", "foo", false);
190 assert_se(w);
191 assert_se(streq(w, ""));
192
193 _cleanup_free_ char *x = strv_join_full((char **)input_table_two_empties, ", ", "foo", false);
194 assert_se(x);
195 assert_se(streq(x, "foo, foo"));
196
197 _cleanup_free_ char *y = strv_join_full((char **)input_table_one_empty, ", ", "foo", false);
198 assert_se(y);
199 assert_se(streq(y, "foo"));
200 }
201
202 static void test_strv_unquote_one(const char *quoted, char **list) {
203 _cleanup_strv_free_ char **s;
204 _cleanup_free_ char *j;
205 unsigned i = 0;
206 int r;
207
208 log_info("/* %s */", __func__);
209
210 r = strv_split_full(&s, quoted, WHITESPACE, EXTRACT_UNQUOTE);
211 assert_se(r == (int) strv_length(list));
212 assert_se(s);
213 j = strv_join(s, " | ");
214 assert_se(j);
215 puts(j);
216
217 STRV_FOREACH(t, s)
218 assert_se(streq(list[i++], *t));
219
220 assert_se(list[i] == NULL);
221 }
222
223 TEST(strv_unquote) {
224 test_strv_unquote_one(" foo=bar \"waldo\" zzz ", STRV_MAKE("foo=bar", "waldo", "zzz"));
225 test_strv_unquote_one("", STRV_MAKE_EMPTY);
226 test_strv_unquote_one(" ", STRV_MAKE_EMPTY);
227 test_strv_unquote_one(" ", STRV_MAKE_EMPTY);
228 test_strv_unquote_one(" x", STRV_MAKE("x"));
229 test_strv_unquote_one("x ", STRV_MAKE("x"));
230 test_strv_unquote_one(" x ", STRV_MAKE("x"));
231 test_strv_unquote_one(" \"x\" ", STRV_MAKE("x"));
232 test_strv_unquote_one(" 'x' ", STRV_MAKE("x"));
233 test_strv_unquote_one(" 'x\"' ", STRV_MAKE("x\""));
234 test_strv_unquote_one(" \"x'\" ", STRV_MAKE("x'"));
235 test_strv_unquote_one("a '--b=c \"d e\"'", STRV_MAKE("a", "--b=c \"d e\""));
236
237 /* trailing backslashes */
238 test_strv_unquote_one(" x\\\\", STRV_MAKE("x\\"));
239 }
240
241 static void test_invalid_unquote_one(const char *quoted) {
242 char **s = NULL;
243 int r;
244
245 log_info("/* %s */", __func__);
246
247 r = strv_split_full(&s, quoted, WHITESPACE, EXTRACT_UNQUOTE);
248 assert_se(s == NULL);
249 assert_se(r == -EINVAL);
250 }
251
252 TEST(invalid_unquote) {
253 test_invalid_unquote_one(" x\\");
254 test_invalid_unquote_one("a --b='c \"d e\"''");
255 test_invalid_unquote_one("a --b='c \"d e\" '\"");
256 test_invalid_unquote_one("a --b='c \"d e\"garbage");
257 test_invalid_unquote_one("'");
258 test_invalid_unquote_one("\"");
259 test_invalid_unquote_one("'x'y'g");
260 }
261
262 TEST(strv_split) {
263 _cleanup_(strv_free_erasep) char **l = NULL;
264 const char str[] = "one,two,three";
265
266 l = strv_split(str, ",");
267 assert_se(l);
268 assert_se(strv_equal(l, (char**) input_table_multiple));
269
270 strv_free_erase(l);
271
272 l = strv_split(" one two\t three", WHITESPACE);
273 assert_se(l);
274 assert_se(strv_equal(l, (char**) input_table_multiple));
275
276 strv_free_erase(l);
277
278 /* Setting NULL for separator is equivalent to WHITESPACE */
279 l = strv_split(" one two\t three", NULL);
280 assert_se(l);
281 assert_se(strv_equal(l, (char**) input_table_multiple));
282
283 strv_free_erase(l);
284
285 assert_se(strv_split_full(&l, " one two\t three", NULL, 0) == 3);
286 assert_se(strv_equal(l, (char**) input_table_multiple));
287
288 strv_free_erase(l);
289
290 assert_se(strv_split_full(&l, " 'one' \" two\t three \" ' four five'", NULL, EXTRACT_UNQUOTE) == 3);
291 assert_se(strv_equal(l, (char**) input_table_quoted));
292
293 l = strv_free_erase(l);
294
295 /* missing last quote causes extraction to fail. */
296 assert_se(strv_split_full(&l, " 'one' \" two\t three \" ' four five", NULL, EXTRACT_UNQUOTE) == -EINVAL);
297 assert_se(!l);
298
299 /* missing last quote, but the last element is _not_ ignored with EXTRACT_RELAX. */
300 assert_se(strv_split_full(&l, " 'one' \" two\t three \" ' four five", NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX) == 3);
301 assert_se(strv_equal(l, (char**) input_table_quoted));
302
303 l = strv_free_erase(l);
304
305 /* missing separator between items */
306 assert_se(strv_split_full(&l, " 'one' \" two\t three \"' four five'", NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX) == 2);
307 assert_se(strv_equal(l, (char**) input_table_quoted_joined));
308
309 l = strv_free_erase(l);
310
311 assert_se(strv_split_full(&l, " 'one' \" two\t three \"' four five", NULL,
312 EXTRACT_UNQUOTE | EXTRACT_RELAX | EXTRACT_UNESCAPE_RELAX) == 2);
313 assert_se(strv_equal(l, (char**) input_table_quoted_joined));
314
315 l = strv_free_erase(l);
316
317 assert_se(strv_split_full(&l, "\\", NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX | EXTRACT_UNESCAPE_RELAX) == 1);
318 assert_se(strv_equal(l, STRV_MAKE("\\")));
319
320 l = strv_free_erase(l);
321
322 assert_se(l = strv_split("\\", NULL));
323 assert_se(strv_equal(l, STRV_MAKE("\\")));
324
325 l = strv_free_erase(l);
326
327 assert_se(l = strv_split("aa\\ bb\\", NULL));
328 assert_se(strv_equal(l, STRV_MAKE("aa\\", "bb\\")));
329
330 l = strv_free_erase(l);
331
332 assert_se(l = strv_split("aa\" bb'", NULL));
333 assert_se(strv_equal(l, STRV_MAKE("aa\"", "bb'")));
334 }
335
336 TEST(strv_split_empty) {
337 _cleanup_strv_free_ char **l = NULL;
338
339 l = strv_split("", WHITESPACE);
340 assert_se(l);
341 assert_se(strv_isempty(l));
342 l = strv_free(l);
343
344 assert_se(l = strv_split("", NULL));
345 assert_se(strv_isempty(l));
346 l = strv_free(l);
347
348 assert_se(strv_split_full(&l, "", NULL, 0) == 0);
349 assert_se(l);
350 assert_se(strv_isempty(l));
351 l = strv_free(l);
352
353 assert_se(strv_split_full(&l, "", NULL, EXTRACT_UNQUOTE) == 0);
354 assert_se(l);
355 assert_se(strv_isempty(l));
356 l = strv_free(l);
357
358 assert_se(strv_split_full(&l, "", WHITESPACE, EXTRACT_UNQUOTE) == 0);
359 assert_se(l);
360 assert_se(strv_isempty(l));
361 l = strv_free(l);
362
363 assert_se(strv_split_full(&l, "", WHITESPACE, EXTRACT_UNQUOTE | EXTRACT_RELAX) == 0);
364 assert_se(l);
365 assert_se(strv_isempty(l));
366 strv_free(l);
367
368 l = strv_split(" ", WHITESPACE);
369 assert_se(l);
370 assert_se(strv_isempty(l));
371 strv_free(l);
372
373 l = strv_split(" ", NULL);
374 assert_se(l);
375 assert_se(strv_isempty(l));
376 l = strv_free(l);
377
378 assert_se(strv_split_full(&l, " ", NULL, 0) == 0);
379 assert_se(l);
380 assert_se(strv_isempty(l));
381 l = strv_free(l);
382
383 assert_se(strv_split_full(&l, " ", WHITESPACE, EXTRACT_UNQUOTE) == 0);
384 assert_se(l);
385 assert_se(strv_isempty(l));
386 l = strv_free(l);
387
388 assert_se(strv_split_full(&l, " ", NULL, EXTRACT_UNQUOTE) == 0);
389 assert_se(l);
390 assert_se(strv_isempty(l));
391 l = strv_free(l);
392
393 assert_se(strv_split_full(&l, " ", NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX) == 0);
394 assert_se(l);
395 assert_se(strv_isempty(l));
396 }
397
398 TEST(strv_split_full) {
399 _cleanup_strv_free_ char **l = NULL;
400 const char *str = ":foo\\:bar::waldo:";
401 int r;
402
403 r = strv_split_full(&l, str, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
404 assert_se(r == (int) strv_length(l));
405 assert_se(streq_ptr(l[0], ""));
406 assert_se(streq_ptr(l[1], "foo:bar"));
407 assert_se(streq_ptr(l[2], ""));
408 assert_se(streq_ptr(l[3], "waldo"));
409 assert_se(streq_ptr(l[4], ""));
410 assert_se(streq_ptr(l[5], NULL));
411 }
412
413 TEST(strv_split_and_extend_full) {
414 _cleanup_strv_free_ char **l = NULL;
415 const char *str1 = ":foo\\:bar:";
416 const char *str2 = "waldo::::::baz";
417 int r;
418
419 r = strv_split_and_extend(&l, "", ":", false);
420 assert_se(r == (int) strv_length(l));
421 r = strv_split_and_extend_full(&l, str1, ":", false, EXTRACT_DONT_COALESCE_SEPARATORS);
422 assert_se(r == (int) strv_length(l));
423 assert_se(streq_ptr(l[0], ""));
424 assert_se(streq_ptr(l[1], "foo:bar"));
425 assert_se(streq_ptr(l[2], ""));
426 r = strv_split_and_extend_full(&l, str2, ":", false, 0);
427 assert_se(r == (int) strv_length(l));
428 assert_se(streq_ptr(l[3], "waldo"));
429 assert_se(streq_ptr(l[4], "baz"));
430 assert_se(streq_ptr(l[5], NULL));
431 }
432
433 TEST(strv_split_colon_pairs) {
434 _cleanup_strv_free_ char **l = NULL;
435 const char *str = "one:two three four:five six seven:eight\\:nine ten\\:eleven\\\\",
436 *str_inval="one:two three:four:five";
437 int r;
438
439 r = strv_split_colon_pairs(&l, str);
440 assert_se(r == (int) strv_length(l));
441 assert_se(r == 12);
442 assert_se(streq_ptr(l[0], "one"));
443 assert_se(streq_ptr(l[1], "two"));
444 assert_se(streq_ptr(l[2], "three"));
445 assert_se(streq_ptr(l[3], ""));
446 assert_se(streq_ptr(l[4], "four"));
447 assert_se(streq_ptr(l[5], "five"));
448 assert_se(streq_ptr(l[6], "six"));
449 assert_se(streq_ptr(l[7], ""));
450 assert_se(streq_ptr(l[8], "seven"));
451 assert_se(streq_ptr(l[9], "eight:nine"));
452 assert_se(streq_ptr(l[10], "ten:eleven\\"));
453 assert_se(streq_ptr(l[11], ""));
454 assert_se(streq_ptr(l[12], NULL));
455
456 r = strv_split_colon_pairs(&l, str_inval);
457 assert_se(r == -EINVAL);
458 }
459
460 TEST(strv_split_newlines) {
461 unsigned i = 0;
462 _cleanup_strv_free_ char **l = NULL;
463 const char str[] = "one\ntwo\nthree";
464
465 l = strv_split_newlines(str);
466 assert_se(l);
467
468 STRV_FOREACH(s, l)
469 assert_se(streq(*s, input_table_multiple[i++]));
470 }
471
472 TEST(strv_split_newlines_full) {
473 const char str[] =
474 "ID_VENDOR=QEMU\n"
475 "ID_VENDOR_ENC=QEMU\\x20\\x20\\x20\\x20\n"
476 "ID_MODEL_ENC=QEMU\\x20HARDDISK\\x20\\x20\\x20\n"
477 "\n\n\n";
478 _cleanup_strv_free_ char **l = NULL;
479
480 assert_se(strv_split_newlines_full(&l, str, 0) == 3);
481 assert_se(strv_equal(l, (char**) input_table_unescape));
482
483 l = strv_free(l);
484
485 assert_se(strv_split_newlines_full(&l, str, EXTRACT_RETAIN_ESCAPE) == 3);
486 assert_se(strv_equal(l, (char**) input_table_retain_escape));
487 }
488
489 TEST(strv_overlap) {
490 const char * const input_table[] = {
491 "one",
492 "two",
493 "three",
494 NULL
495 };
496 const char * const input_table_overlap[] = {
497 "two",
498 NULL
499 };
500 const char * const input_table_unique[] = {
501 "four",
502 "five",
503 "six",
504 NULL
505 };
506
507 assert_se(strv_overlap((char **)input_table, (char**)input_table_overlap));
508 assert_se(!strv_overlap((char **)input_table, (char**)input_table_unique));
509 }
510
511 TEST(strv_sort) {
512 const char* input_table[] = {
513 "durian",
514 "apple",
515 "citrus",
516 "CAPITAL LETTERS FIRST",
517 "banana",
518 NULL
519 };
520
521 strv_sort((char **)input_table);
522
523 assert_se(streq(input_table[0], "CAPITAL LETTERS FIRST"));
524 assert_se(streq(input_table[1], "apple"));
525 assert_se(streq(input_table[2], "banana"));
526 assert_se(streq(input_table[3], "citrus"));
527 assert_se(streq(input_table[4], "durian"));
528 }
529
530 TEST(strv_extend_strv_concat) {
531 _cleanup_strv_free_ char **a = NULL, **b = NULL;
532
533 a = strv_new("without", "suffix");
534 b = strv_new("with", "suffix");
535 assert_se(a);
536 assert_se(b);
537
538 assert_se(strv_extend_strv_concat(&a, b, "_suffix") >= 0);
539
540 assert_se(streq(a[0], "without"));
541 assert_se(streq(a[1], "suffix"));
542 assert_se(streq(a[2], "with_suffix"));
543 assert_se(streq(a[3], "suffix_suffix"));
544 }
545
546 TEST(strv_extend_strv) {
547 _cleanup_strv_free_ char **a = NULL, **b = NULL, **n = NULL;
548
549 a = strv_new("abc", "def", "ghi");
550 b = strv_new("jkl", "mno", "abc", "pqr");
551 assert_se(a);
552 assert_se(b);
553
554 assert_se(strv_extend_strv(&a, b, true) == 3);
555
556 assert_se(streq(a[0], "abc"));
557 assert_se(streq(a[1], "def"));
558 assert_se(streq(a[2], "ghi"));
559 assert_se(streq(a[3], "jkl"));
560 assert_se(streq(a[4], "mno"));
561 assert_se(streq(a[5], "pqr"));
562 assert_se(strv_length(a) == 6);
563
564 assert_se(strv_extend_strv(&n, b, false) >= 0);
565 assert_se(streq(n[0], "jkl"));
566 assert_se(streq(n[1], "mno"));
567 assert_se(streq(n[2], "abc"));
568 assert_se(streq(n[3], "pqr"));
569 assert_se(strv_length(n) == 4);
570 }
571
572 TEST(strv_extend_with_size) {
573 _cleanup_strv_free_ char **a = NULL;
574 size_t n = SIZE_MAX;
575
576 a = strv_new("test", "test1");
577 assert_se(a);
578
579 assert_se(strv_extend_with_size(&a, &n, "test2") >= 0);
580 assert_se(n == 3);
581 assert_se(strv_extend_with_size(&a, &n, "test3") >= 0);
582 assert_se(n == 4);
583
584 assert_se(streq(a[0], "test"));
585 assert_se(streq(a[1], "test1"));
586 assert_se(streq(a[2], "test2"));
587 assert_se(streq(a[3], "test3"));
588 assert_se(a[4] == NULL);
589 }
590
591 TEST(strv_extend) {
592 _cleanup_strv_free_ char **a = NULL, **b = NULL;
593
594 a = strv_new("test", "test1");
595 assert_se(a);
596 assert_se(strv_extend(&a, "test2") >= 0);
597 assert_se(strv_extend(&b, "test3") >= 0);
598
599 assert_se(streq(a[0], "test"));
600 assert_se(streq(a[1], "test1"));
601 assert_se(streq(a[2], "test2"));
602 assert_se(streq(b[0], "test3"));
603 }
604
605 TEST(strv_extendf) {
606 _cleanup_strv_free_ char **a = NULL, **b = NULL;
607
608 a = strv_new("test", "test1");
609 assert_se(a);
610 assert_se(strv_extendf(&a, "test2 %s %d %s", "foo", 128, "bar") >= 0);
611 assert_se(strv_extendf(&b, "test3 %s %s %d", "bar", "foo", 128) >= 0);
612
613 assert_se(streq(a[0], "test"));
614 assert_se(streq(a[1], "test1"));
615 assert_se(streq(a[2], "test2 foo 128 bar"));
616 assert_se(streq(b[0], "test3 bar foo 128"));
617 }
618
619 TEST(strv_foreach) {
620 _cleanup_strv_free_ char **a;
621 unsigned i = 0;
622
623 a = strv_new("one", "two", "three");
624 assert_se(a);
625
626 STRV_FOREACH(check, a)
627 assert_se(streq(*check, input_table_multiple[i++]));
628 }
629
630 TEST(strv_foreach_backwards) {
631 _cleanup_strv_free_ char **a;
632 unsigned i = 2;
633
634 a = strv_new("one", "two", "three");
635
636 assert_se(a);
637
638 STRV_FOREACH_BACKWARDS(check, a)
639 assert_se(streq_ptr(*check, input_table_multiple[i--]));
640
641 STRV_FOREACH_BACKWARDS(check, (char**) NULL)
642 assert_not_reached();
643
644 STRV_FOREACH_BACKWARDS(check, STRV_MAKE_EMPTY)
645 assert_not_reached();
646
647 unsigned count = 0;
648 STRV_FOREACH_BACKWARDS(check, STRV_MAKE("ONE"))
649 count++;
650 assert_se(count == 1);
651 }
652
653 TEST(strv_foreach_pair) {
654 _cleanup_strv_free_ char **a = NULL;
655
656 a = strv_new("pair_one", "pair_one",
657 "pair_two", "pair_two",
658 "pair_three", "pair_three");
659 STRV_FOREACH_PAIR(x, y, a)
660 assert_se(streq(*x, *y));
661 }
662
663 static void test_strv_from_stdarg_alloca_one(char **l, const char *first, ...) {
664 char **j;
665 unsigned i;
666
667 log_info("/* %s */", __func__);
668
669 j = strv_from_stdarg_alloca(first);
670
671 for (i = 0;; i++) {
672 assert_se(streq_ptr(l[i], j[i]));
673
674 if (!l[i])
675 break;
676 }
677 }
678
679 TEST(strv_from_stdarg_alloca) {
680 test_strv_from_stdarg_alloca_one(STRV_MAKE("foo", "bar"), "foo", "bar", NULL);
681 test_strv_from_stdarg_alloca_one(STRV_MAKE("foo"), "foo", NULL);
682 test_strv_from_stdarg_alloca_one(STRV_MAKE_EMPTY, NULL);
683 }
684
685 TEST(strv_insert) {
686 _cleanup_strv_free_ char **a = NULL;
687
688 assert_se(strv_insert(&a, 0, strdup("first")) == 0);
689 assert_se(streq(a[0], "first"));
690 assert_se(!a[1]);
691
692 assert_se(strv_insert(&a, 0, NULL) == 0);
693 assert_se(streq(a[0], "first"));
694 assert_se(!a[1]);
695
696 assert_se(strv_insert(&a, 1, strdup("two")) == 0);
697 assert_se(streq(a[0], "first"));
698 assert_se(streq(a[1], "two"));
699 assert_se(!a[2]);
700
701 assert_se(strv_insert(&a, 4, strdup("tri")) == 0);
702 assert_se(streq(a[0], "first"));
703 assert_se(streq(a[1], "two"));
704 assert_se(streq(a[2], "tri"));
705 assert_se(!a[3]);
706
707 assert_se(strv_insert(&a, 1, strdup("duo")) == 0);
708 assert_se(streq(a[0], "first"));
709 assert_se(streq(a[1], "duo"));
710 assert_se(streq(a[2], "two"));
711 assert_se(streq(a[3], "tri"));
712 assert_se(!a[4]);
713 }
714
715 TEST(strv_push_prepend) {
716 _cleanup_strv_free_ char **a = NULL;
717
718 assert_se(a = strv_new("foo", "bar", "three"));
719
720 assert_se(strv_push_prepend(&a, strdup("first")) >= 0);
721 assert_se(streq(a[0], "first"));
722 assert_se(streq(a[1], "foo"));
723 assert_se(streq(a[2], "bar"));
724 assert_se(streq(a[3], "three"));
725 assert_se(!a[4]);
726
727 assert_se(strv_consume_prepend(&a, strdup("first2")) >= 0);
728 assert_se(streq(a[0], "first2"));
729 assert_se(streq(a[1], "first"));
730 assert_se(streq(a[2], "foo"));
731 assert_se(streq(a[3], "bar"));
732 assert_se(streq(a[4], "three"));
733 assert_se(!a[5]);
734 }
735
736 TEST(strv_push_with_size) {
737 _cleanup_strv_free_ char **a = NULL;
738 size_t n = 0;
739 char *i, *j;
740
741 assert_se(i = strdup("foo"));
742 assert_se(strv_push_with_size(&a, &n, i) >= 0);
743 assert_se(n == 1);
744
745 assert_se(i = strdup("a"));
746 assert_se(j = strdup("b"));
747 assert_se(strv_push_with_size(&a, &n, i) >= 0);
748 assert_se(n == 2);
749 assert_se(strv_push_with_size(&a, &n, j) >= 0);
750 assert_se(n == 3);
751
752 assert_se(streq_ptr(a[0], "foo"));
753 assert_se(streq_ptr(a[1], "a"));
754 assert_se(streq_ptr(a[2], "b"));
755 assert_se(streq_ptr(a[3], NULL));
756
757 assert_se(n = strv_length(a));
758 }
759
760 TEST(strv_push) {
761 _cleanup_strv_free_ char **a = NULL;
762 char *i, *j;
763
764 assert_se(i = strdup("foo"));
765 assert_se(strv_push(&a, i) >= 0);
766
767 assert_se(i = strdup("a"));
768 assert_se(j = strdup("b"));
769 assert_se(strv_push_pair(&a, i, j) >= 0);
770
771 assert_se(streq_ptr(a[0], "foo"));
772 assert_se(streq_ptr(a[1], "a"));
773 assert_se(streq_ptr(a[2], "b"));
774 assert_se(streq_ptr(a[3], NULL));
775 }
776
777 TEST(strv_compare) {
778 _cleanup_strv_free_ char **a = NULL;
779 _cleanup_strv_free_ char **b = NULL;
780 _cleanup_strv_free_ char **c = NULL;
781 _cleanup_strv_free_ char **d = NULL;
782
783 a = strv_new("one", "two", "three");
784 assert_se(a);
785 b = strv_new("one", "two", "three");
786 assert_se(b);
787 c = strv_new("one", "two", "three", "four");
788 assert_se(c);
789 d = strv_new(NULL);
790 assert_se(d);
791
792 assert_se(strv_compare(a, a) == 0);
793 assert_se(strv_compare(a, b) == 0);
794 assert_se(strv_compare(d, d) == 0);
795 assert_se(strv_compare(d, NULL) == 0);
796 assert_se(strv_compare(NULL, NULL) == 0);
797
798 assert_se(strv_compare(a, c) < 0);
799 assert_se(strv_compare(b, c) < 0);
800 assert_se(strv_compare(b, d) == 1);
801 assert_se(strv_compare(b, NULL) == 1);
802 }
803
804 TEST(strv_is_uniq) {
805 _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL, **d = NULL;
806
807 a = strv_new(NULL);
808 assert_se(a);
809 assert_se(strv_is_uniq(a));
810
811 b = strv_new("foo");
812 assert_se(b);
813 assert_se(strv_is_uniq(b));
814
815 c = strv_new("foo", "bar");
816 assert_se(c);
817 assert_se(strv_is_uniq(c));
818
819 d = strv_new("foo", "bar", "waldo", "bar", "piep");
820 assert_se(d);
821 assert_se(!strv_is_uniq(d));
822 }
823
824 TEST(strv_reverse) {
825 _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL, **d = NULL;
826
827 a = strv_new(NULL);
828 assert_se(a);
829
830 strv_reverse(a);
831 assert_se(strv_isempty(a));
832
833 b = strv_new("foo");
834 assert_se(b);
835 strv_reverse(b);
836 assert_se(streq_ptr(b[0], "foo"));
837 assert_se(streq_ptr(b[1], NULL));
838
839 c = strv_new("foo", "bar");
840 assert_se(c);
841 strv_reverse(c);
842 assert_se(streq_ptr(c[0], "bar"));
843 assert_se(streq_ptr(c[1], "foo"));
844 assert_se(streq_ptr(c[2], NULL));
845
846 d = strv_new("foo", "bar", "waldo");
847 assert_se(d);
848 strv_reverse(d);
849 assert_se(streq_ptr(d[0], "waldo"));
850 assert_se(streq_ptr(d[1], "bar"));
851 assert_se(streq_ptr(d[2], "foo"));
852 assert_se(streq_ptr(d[3], NULL));
853 }
854
855 TEST(strv_shell_escape) {
856 _cleanup_strv_free_ char **v = NULL;
857
858 v = strv_new("foo:bar", "bar,baz", "wal\\do");
859 assert_se(v);
860 assert_se(strv_shell_escape(v, ",:"));
861 assert_se(streq_ptr(v[0], "foo\\:bar"));
862 assert_se(streq_ptr(v[1], "bar\\,baz"));
863 assert_se(streq_ptr(v[2], "wal\\\\do"));
864 assert_se(streq_ptr(v[3], NULL));
865 }
866
867 static void test_strv_skip_one(char **a, size_t n, char **b) {
868 a = strv_skip(a, n);
869 assert_se(strv_equal(a, b));
870 }
871
872 TEST(strv_skip) {
873 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 0, STRV_MAKE("foo", "bar", "baz"));
874 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 1, STRV_MAKE("bar", "baz"));
875 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 2, STRV_MAKE("baz"));
876 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 3, STRV_MAKE(NULL));
877 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 4, STRV_MAKE(NULL));
878 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 55, STRV_MAKE(NULL));
879
880 test_strv_skip_one(STRV_MAKE("quux"), 0, STRV_MAKE("quux"));
881 test_strv_skip_one(STRV_MAKE("quux"), 1, STRV_MAKE(NULL));
882 test_strv_skip_one(STRV_MAKE("quux"), 55, STRV_MAKE(NULL));
883
884 test_strv_skip_one(STRV_MAKE(NULL), 0, STRV_MAKE(NULL));
885 test_strv_skip_one(STRV_MAKE(NULL), 1, STRV_MAKE(NULL));
886 test_strv_skip_one(STRV_MAKE(NULL), 55, STRV_MAKE(NULL));
887 }
888
889 TEST(strv_extend_n) {
890 _cleanup_strv_free_ char **v = NULL;
891
892 v = strv_new("foo", "bar");
893 assert_se(v);
894
895 assert_se(strv_extend_n(&v, "waldo", 3) >= 0);
896 assert_se(strv_extend_n(&v, "piep", 2) >= 0);
897
898 assert_se(streq(v[0], "foo"));
899 assert_se(streq(v[1], "bar"));
900 assert_se(streq(v[2], "waldo"));
901 assert_se(streq(v[3], "waldo"));
902 assert_se(streq(v[4], "waldo"));
903 assert_se(streq(v[5], "piep"));
904 assert_se(streq(v[6], "piep"));
905 assert_se(v[7] == NULL);
906
907 v = strv_free(v);
908
909 assert_se(strv_extend_n(&v, "foo", 1) >= 0);
910 assert_se(strv_extend_n(&v, "bar", 0) >= 0);
911
912 assert_se(streq(v[0], "foo"));
913 assert_se(v[1] == NULL);
914 }
915
916 TEST(foreach_string) {
917 const char * const t[] = {
918 "foo",
919 "bar",
920 "waldo",
921 NULL
922 };
923
924 unsigned i = 0;
925 FOREACH_STRING(x, "foo", "bar", "waldo")
926 assert_se(streq_ptr(t[i++], x));
927 assert_se(i == 3);
928
929 FOREACH_STRING(x, "zzz")
930 assert_se(streq(x, "zzz"));
931 }
932
933 TEST(strv_fnmatch) {
934 _cleanup_strv_free_ char **v = NULL;
935 size_t pos;
936
937 assert_se(!strv_fnmatch(STRV_MAKE_EMPTY, "a"));
938
939 v = strv_new("xxx", "*\\*", "yyy");
940 assert_se(!strv_fnmatch_full(v, "\\", 0, NULL));
941 assert_se(strv_fnmatch_full(v, "\\", FNM_NOESCAPE, &pos));
942 assert_se(pos == 1);
943 }
944
945 DEFINE_TEST_MAIN(LOG_INFO);