Debian Codegraph @

src/test/test-strv.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fnmatch.h>
4
5 #include "alloc-util.h"
6 #include "extract-word.h"
7 #include "string-util.h"
8 #include "strv.h"
9 #include "tests.h"
10
11 TEST(str_in_set) {
12 assert_se(STR_IN_SET("x", "x", "y", "z"));
13 assert_se(!STR_IN_SET("X", "x", "y", "z"));
14 assert_se(!STR_IN_SET("", "x", "y", "z"));
15 assert_se(STR_IN_SET("x", "w", "x"));
16 }
17
18 TEST(strptr_in_set) {
19 assert_se(STRPTR_IN_SET("x", "x", "y", "z"));
20 assert_se(!STRPTR_IN_SET("X", "x", "y", "z"));
21 assert_se(!STRPTR_IN_SET("", "x", "y", "z"));
22 assert_se(STRPTR_IN_SET("x", "w", "x"));
23
24 assert_se(!STRPTR_IN_SET(NULL, "x", "y", "z"));
25 assert_se(!STRPTR_IN_SET(NULL, ""));
26 /* strv cannot contain a null, hence the result below */
27 assert_se(!STRPTR_IN_SET(NULL, NULL));
28 }
29
30 TEST(startswith_set) {
31 assert_se(!STARTSWITH_SET("foo", "bar", "baz", "waldo"));
32 assert_se(!STARTSWITH_SET("foo", "bar"));
33
34 assert_se(STARTSWITH_SET("abc", "a", "ab", "abc"));
35 assert_se(STARTSWITH_SET("abc", "ax", "ab", "abc"));
36 assert_se(STARTSWITH_SET("abc", "ax", "abx", "abc"));
37 assert_se(!STARTSWITH_SET("abc", "ax", "abx", "abcx"));
38
39 ASSERT_STREQ(STARTSWITH_SET("foobar", "hhh", "kkk", "foo", "zzz"), "bar");
40 ASSERT_STREQ(STARTSWITH_SET("foobar", "hhh", "kkk", "", "zzz"), "foobar");
41 ASSERT_STREQ(STARTSWITH_SET("", "hhh", "kkk", "zzz", ""), "");
42 }
43
44 static const char* const input_table_multiple[] = {
45 "one",
46 "two",
47 "three",
48 NULL,
49 };
50
51 static const char* const input_table_quoted[] = {
52 "one",
53 " two\t three ",
54 " four five",
55 NULL,
56 };
57
58 static const char* const input_table_quoted_joined[] = {
59 "one",
60 " two\t three " " four five",
61 NULL,
62 };
63
64 static const char* const input_table_one[] = {
65 "one",
66 NULL,
67 };
68
69 static const char* const input_table_none[] = {
70 NULL,
71 };
72
73 static const char* const input_table_two_empties[] = {
74 "",
75 "",
76 NULL,
77 };
78
79 static const char* const input_table_one_empty[] = {
80 "",
81 NULL,
82 };
83
84 static const char* const input_table_unescape[] = {
85 "ID_VENDOR=QEMU",
86 "ID_VENDOR_ENC=QEMUx20x20x20x20",
87 "ID_MODEL_ENC=QEMUx20HARDDISKx20x20x20",
88 NULL,
89 };
90
91 static const char* const input_table_retain_escape[] = {
92 "ID_VENDOR=QEMU",
93 "ID_VENDOR_ENC=QEMU\\x20\\x20\\x20\\x20",
94 "ID_MODEL_ENC=QEMU\\x20HARDDISK\\x20\\x20\\x20",
95 NULL,
96 };
97
98 TEST(strv_find) {
99 assert_se(strv_find((char **)input_table_multiple, "three"));
100 assert_se(!strv_find((char **)input_table_multiple, "four"));
101 }
102
103 TEST(strv_find_prefix) {
104 assert_se(strv_find_prefix((char **)input_table_multiple, "o"));
105 assert_se(strv_find_prefix((char **)input_table_multiple, "one"));
106 assert_se(strv_find_prefix((char **)input_table_multiple, ""));
107 assert_se(!strv_find_prefix((char **)input_table_multiple, "xxx"));
108 assert_se(!strv_find_prefix((char **)input_table_multiple, "onee"));
109 }
110
111 TEST(strv_find_startswith) {
112 char *r;
113
114 r = strv_find_startswith((char **)input_table_multiple, "o");
115 assert_se(r && streq(r, "ne"));
116
117 r = strv_find_startswith((char **)input_table_multiple, "one");
118 assert_se(r && streq(r, ""));
119
120 r = strv_find_startswith((char **)input_table_multiple, "");
121 assert_se(r && streq(r, "one"));
122
123 assert_se(!strv_find_startswith((char **)input_table_multiple, "xxx"));
124 assert_se(!strv_find_startswith((char **)input_table_multiple, "onee"));
125 }
126
127 TEST(strv_join) {
128 _cleanup_free_ char *p = strv_join((char **)input_table_multiple, ", ");
129 assert_se(p);
130 ASSERT_STREQ(p, "one, two, three");
131
132 _cleanup_free_ char *q = strv_join((char **)input_table_multiple, ";");
133 assert_se(q);
134 ASSERT_STREQ(q, "one;two;three");
135
136 _cleanup_free_ char *r = strv_join((char **)input_table_multiple, NULL);
137 assert_se(r);
138 ASSERT_STREQ(r, "one two three");
139
140 _cleanup_free_ char *s = strv_join(STRV_MAKE("1", "2", "3,3"), ",");
141 assert_se(s);
142 ASSERT_STREQ(s, "1,2,3,3");
143
144 _cleanup_free_ char *t = strv_join((char **)input_table_one, ", ");
145 assert_se(t);
146 ASSERT_STREQ(t, "one");
147
148 _cleanup_free_ char *u = strv_join((char **)input_table_none, ", ");
149 assert_se(u);
150 ASSERT_STREQ(u, "");
151
152 _cleanup_free_ char *v = strv_join((char **)input_table_two_empties, ", ");
153 assert_se(v);
154 ASSERT_STREQ(v, ", ");
155
156 _cleanup_free_ char *w = strv_join((char **)input_table_one_empty, ", ");
157 assert_se(w);
158 ASSERT_STREQ(w, "");
159 }
160
161 TEST(strv_join_full) {
162 _cleanup_free_ char *p = strv_join_full((char **)input_table_multiple, ", ", "foo", false);
163 assert_se(p);
164 ASSERT_STREQ(p, "fooone, footwo, foothree");
165
166 _cleanup_free_ char *q = strv_join_full((char **)input_table_multiple, ";", "foo", false);
167 assert_se(q);
168 ASSERT_STREQ(q, "fooone;footwo;foothree");
169
170 _cleanup_free_ char *r = strv_join_full(STRV_MAKE("a", "a;b", "a:c"), ";", NULL, true);
171 assert_se(r);
172 ASSERT_STREQ(r, "a;a\\;b;a:c");
173
174 _cleanup_free_ char *s = strv_join_full(STRV_MAKE("a", "a;b", "a;;c", ";", ";x"), ";", NULL, true);
175 assert_se(s);
176 ASSERT_STREQ(s, "a;a\\;b;a\\;\\;c;\\;;\\;x");
177
178 _cleanup_free_ char *t = strv_join_full(STRV_MAKE("a", "a;b", "a:c", ";"), ";", "=", true);
179 assert_se(t);
180 ASSERT_STREQ(t, "=a;=a\\;b;=a:c;=\\;");
181 t = mfree(t);
182
183 _cleanup_free_ char *u = strv_join_full((char **)input_table_multiple, NULL, "foo", false);
184 assert_se(u);
185 ASSERT_STREQ(u, "fooone footwo foothree");
186
187 _cleanup_free_ char *v = strv_join_full((char **)input_table_one, ", ", "foo", false);
188 assert_se(v);
189 ASSERT_STREQ(v, "fooone");
190
191 _cleanup_free_ char *w = strv_join_full((char **)input_table_none, ", ", "foo", false);
192 assert_se(w);
193 ASSERT_STREQ(w, "");
194
195 _cleanup_free_ char *x = strv_join_full((char **)input_table_two_empties, ", ", "foo", false);
196 assert_se(x);
197 ASSERT_STREQ(x, "foo, foo");
198
199 _cleanup_free_ char *y = strv_join_full((char **)input_table_one_empty, ", ", "foo", false);
200 assert_se(y);
201 ASSERT_STREQ(y, "foo");
202 }
203
204 static void test_strv_unquote_one(const char *quoted, char **list) {
205 _cleanup_strv_free_ char **s = NULL;
206 _cleanup_free_ char *j = NULL;
207 unsigned i = 0;
208 int r;
209
210 log_info("/* %s */", __func__);
211
212 r = strv_split_full(&s, quoted, WHITESPACE, EXTRACT_UNQUOTE);
213 assert_se(r == (int) strv_length(list));
214 assert_se(s);
215 j = strv_join(s, " | ");
216 assert_se(j);
217 puts(j);
218
219 STRV_FOREACH(t, s)
220 ASSERT_STREQ(list[i++], *t);
221
222 ASSERT_NULL(list[i]);
223 }
224
225 TEST(strv_unquote) {
226 test_strv_unquote_one(" foo=bar \"waldo\" zzz ", STRV_MAKE("foo=bar", "waldo", "zzz"));
227 test_strv_unquote_one("", STRV_EMPTY);
228 test_strv_unquote_one(" ", STRV_EMPTY);
229 test_strv_unquote_one(" ", STRV_EMPTY);
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(" 'x\"' ", STRV_MAKE("x\""));
236 test_strv_unquote_one(" \"x'\" ", STRV_MAKE("x'"));
237 test_strv_unquote_one("a '--b=c \"d e\"'", STRV_MAKE("a", "--b=c \"d e\""));
238
239 /* trailing backslashes */
240 test_strv_unquote_one(" x\\\\", STRV_MAKE("x\\"));
241 }
242
243 static void test_invalid_unquote_one(const char *quoted) {
244 char **s = NULL;
245 int r;
246
247 log_info("/* %s */", __func__);
248
249 r = strv_split_full(&s, quoted, WHITESPACE, EXTRACT_UNQUOTE);
250 ASSERT_NULL(s);
251 assert_se(r == -EINVAL);
252 }
253
254 TEST(invalid_unquote) {
255 test_invalid_unquote_one(" x\\");
256 test_invalid_unquote_one("a --b='c \"d e\"''");
257 test_invalid_unquote_one("a --b='c \"d e\" '\"");
258 test_invalid_unquote_one("a --b='c \"d e\"garbage");
259 test_invalid_unquote_one("'");
260 test_invalid_unquote_one("\"");
261 test_invalid_unquote_one("'x'y'g");
262 }
263
264 TEST(strv_split) {
265 _cleanup_strv_free_erase_ char **l = NULL;
266 const char str[] = "one,two,three";
267
268 l = strv_split(str, ",");
269 assert_se(l);
270 assert_se(strv_equal(l, (char**) input_table_multiple));
271
272 strv_free_erase(l);
273
274 l = strv_split(" one two\t three", WHITESPACE);
275 assert_se(l);
276 assert_se(strv_equal(l, (char**) input_table_multiple));
277
278 strv_free_erase(l);
279
280 /* Setting NULL for separator is equivalent to WHITESPACE */
281 l = strv_split(" one two\t three", NULL);
282 assert_se(l);
283 assert_se(strv_equal(l, (char**) input_table_multiple));
284
285 strv_free_erase(l);
286
287 assert_se(strv_split_full(&l, " one two\t three", NULL, 0) == 3);
288 assert_se(strv_equal(l, (char**) input_table_multiple));
289
290 strv_free_erase(l);
291
292 assert_se(strv_split_full(&l, " 'one' \" two\t three \" ' four five'", NULL, EXTRACT_UNQUOTE) == 3);
293 assert_se(strv_equal(l, (char**) input_table_quoted));
294
295 l = strv_free_erase(l);
296
297 /* missing last quote causes extraction to fail. */
298 assert_se(strv_split_full(&l, " 'one' \" two\t three \" ' four five", NULL, EXTRACT_UNQUOTE) == -EINVAL);
299 assert_se(!l);
300
301 /* missing last quote, but the last element is _not_ ignored with EXTRACT_RELAX. */
302 assert_se(strv_split_full(&l, " 'one' \" two\t three \" ' four five", NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX) == 3);
303 assert_se(strv_equal(l, (char**) input_table_quoted));
304
305 l = strv_free_erase(l);
306
307 /* missing separator between items */
308 assert_se(strv_split_full(&l, " 'one' \" two\t three \"' four five'", NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX) == 2);
309 assert_se(strv_equal(l, (char**) input_table_quoted_joined));
310
311 l = strv_free_erase(l);
312
313 assert_se(strv_split_full(&l, " 'one' \" two\t three \"' four five", NULL,
314 EXTRACT_UNQUOTE | EXTRACT_RELAX | EXTRACT_UNESCAPE_RELAX) == 2);
315 assert_se(strv_equal(l, (char**) input_table_quoted_joined));
316
317 l = strv_free_erase(l);
318
319 assert_se(strv_split_full(&l, "\\", NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX | EXTRACT_UNESCAPE_RELAX) == 1);
320 assert_se(strv_equal(l, STRV_MAKE("\\")));
321
322 l = strv_free_erase(l);
323
324 assert_se(l = strv_split("\\", NULL));
325 assert_se(strv_equal(l, STRV_MAKE("\\")));
326
327 l = strv_free_erase(l);
328
329 assert_se(l = strv_split("aa\\ bb\\", NULL));
330 assert_se(strv_equal(l, STRV_MAKE("aa\\", "bb\\")));
331
332 l = strv_free_erase(l);
333
334 assert_se(l = strv_split("aa\" bb'", NULL));
335 assert_se(strv_equal(l, STRV_MAKE("aa\"", "bb'")));
336 }
337
338 TEST(strv_split_empty) {
339 _cleanup_strv_free_ char **l = NULL;
340
341 l = strv_split("", WHITESPACE);
342 assert_se(l);
343 assert_se(strv_isempty(l));
344 l = strv_free(l);
345
346 assert_se(l = strv_split("", NULL));
347 assert_se(strv_isempty(l));
348 l = strv_free(l);
349
350 assert_se(strv_split_full(&l, "", NULL, 0) == 0);
351 assert_se(l);
352 assert_se(strv_isempty(l));
353 l = strv_free(l);
354
355 assert_se(strv_split_full(&l, "", NULL, EXTRACT_UNQUOTE) == 0);
356 assert_se(l);
357 assert_se(strv_isempty(l));
358 l = strv_free(l);
359
360 assert_se(strv_split_full(&l, "", WHITESPACE, EXTRACT_UNQUOTE) == 0);
361 assert_se(l);
362 assert_se(strv_isempty(l));
363 l = strv_free(l);
364
365 assert_se(strv_split_full(&l, "", WHITESPACE, EXTRACT_UNQUOTE | EXTRACT_RELAX) == 0);
366 assert_se(l);
367 assert_se(strv_isempty(l));
368 strv_free(l);
369
370 l = strv_split(" ", WHITESPACE);
371 assert_se(l);
372 assert_se(strv_isempty(l));
373 strv_free(l);
374
375 l = strv_split(" ", NULL);
376 assert_se(l);
377 assert_se(strv_isempty(l));
378 l = strv_free(l);
379
380 assert_se(strv_split_full(&l, " ", NULL, 0) == 0);
381 assert_se(l);
382 assert_se(strv_isempty(l));
383 l = strv_free(l);
384
385 assert_se(strv_split_full(&l, " ", WHITESPACE, EXTRACT_UNQUOTE) == 0);
386 assert_se(l);
387 assert_se(strv_isempty(l));
388 l = strv_free(l);
389
390 assert_se(strv_split_full(&l, " ", NULL, EXTRACT_UNQUOTE) == 0);
391 assert_se(l);
392 assert_se(strv_isempty(l));
393 l = strv_free(l);
394
395 assert_se(strv_split_full(&l, " ", NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX) == 0);
396 assert_se(l);
397 assert_se(strv_isempty(l));
398 }
399
400 TEST(strv_split_full) {
401 _cleanup_strv_free_ char **l = NULL;
402 const char *str = ":foo\\:bar::waldo:";
403 int r;
404
405 r = strv_split_full(&l, str, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
406 assert_se(r == (int) strv_length(l));
407 ASSERT_STREQ(l[0], "");
408 ASSERT_STREQ(l[1], "foo:bar");
409 ASSERT_STREQ(l[2], "");
410 ASSERT_STREQ(l[3], "waldo");
411 ASSERT_STREQ(l[4], "");
412 ASSERT_STREQ(l[5], NULL);
413 }
414
415 TEST(strv_split_and_extend_full) {
416 _cleanup_strv_free_ char **l = NULL;
417 const char *str1 = ":foo\\:bar:";
418 const char *str2 = "waldo::::::baz";
419 int r;
420
421 r = strv_split_and_extend(&l, "", ":", false);
422 assert_se(r == (int) strv_length(l));
423 r = strv_split_and_extend_full(&l, str1, ":", false, EXTRACT_DONT_COALESCE_SEPARATORS);
424 assert_se(r == (int) strv_length(l));
425 ASSERT_STREQ(l[0], "");
426 ASSERT_STREQ(l[1], "foo:bar");
427 ASSERT_STREQ(l[2], "");
428 r = strv_split_and_extend_full(&l, str2, ":", false, 0);
429 assert_se(r == (int) strv_length(l));
430 ASSERT_STREQ(l[3], "waldo");
431 ASSERT_STREQ(l[4], "baz");
432 ASSERT_STREQ(l[5], NULL);
433 }
434
435 TEST(strv_split_colon_pairs) {
436 _cleanup_strv_free_ char **l = NULL;
437 const char *str = "one:two three four:five six seven:eight\\:nine ten\\:eleven\\\\",
438 *str_inval="one:two three:four:five";
439 int r;
440
441 r = strv_split_colon_pairs(&l, str);
442 assert_se(r == (int) strv_length(l));
443 assert_se(r == 12);
444 ASSERT_STREQ(l[0], "one");
445 ASSERT_STREQ(l[1], "two");
446 ASSERT_STREQ(l[2], "three");
447 ASSERT_STREQ(l[3], "");
448 ASSERT_STREQ(l[4], "four");
449 ASSERT_STREQ(l[5], "five");
450 ASSERT_STREQ(l[6], "six");
451 ASSERT_STREQ(l[7], "");
452 ASSERT_STREQ(l[8], "seven");
453 ASSERT_STREQ(l[9], "eight:nine");
454 ASSERT_STREQ(l[10], "ten:eleven\\");
455 ASSERT_STREQ(l[11], "");
456 ASSERT_STREQ(l[12], NULL);
457
458 r = strv_split_colon_pairs(&l, str_inval);
459 assert_se(r == -EINVAL);
460 }
461
462 TEST(strv_split_newlines) {
463 unsigned i = 0;
464 _cleanup_strv_free_ char **l = NULL;
465 const char str[] = "one\ntwo\nthree";
466
467 l = strv_split_newlines(str);
468 assert_se(l);
469
470 STRV_FOREACH(s, l)
471 ASSERT_STREQ(*s, input_table_multiple[i++]);
472 }
473
474 TEST(strv_split_newlines_full) {
475 const char str[] =
476 "ID_VENDOR=QEMU\n"
477 "ID_VENDOR_ENC=QEMU\\x20\\x20\\x20\\x20\n"
478 "ID_MODEL_ENC=QEMU\\x20HARDDISK\\x20\\x20\\x20\n"
479 "\n\n\n";
480 _cleanup_strv_free_ char **l = NULL;
481
482 assert_se(strv_split_newlines_full(&l, str, 0) == 3);
483 assert_se(strv_equal(l, (char**) input_table_unescape));
484
485 l = strv_free(l);
486
487 assert_se(strv_split_newlines_full(&l, str, EXTRACT_RETAIN_ESCAPE) == 3);
488 assert_se(strv_equal(l, (char**) input_table_retain_escape));
489 }
490
491 TEST(strv_overlap) {
492 const char * const input_table[] = {
493 "one",
494 "two",
495 "three",
496 NULL
497 };
498 const char * const input_table_overlap[] = {
499 "two",
500 NULL
501 };
502 const char * const input_table_unique[] = {
503 "four",
504 "five",
505 "six",
506 NULL
507 };
508
509 assert_se(strv_overlap((char **)input_table, (char**)input_table_overlap));
510 assert_se(!strv_overlap((char **)input_table, (char**)input_table_unique));
511 }
512
513 TEST(strv_sort) {
514 const char* input_table[] = {
515 "durian",
516 "apple",
517 "citrus",
518 "CAPITAL LETTERS FIRST",
519 "banana",
520 NULL
521 };
522
523 strv_sort((char **)input_table);
524
525 ASSERT_STREQ(input_table[0], "CAPITAL LETTERS FIRST");
526 ASSERT_STREQ(input_table[1], "apple");
527 ASSERT_STREQ(input_table[2], "banana");
528 ASSERT_STREQ(input_table[3], "citrus");
529 ASSERT_STREQ(input_table[4], "durian");
530 }
531
532 TEST(strv_sort_uniq) {
533 static const char* input_table[] = {
534 "durian",
535 "apple",
536 "citrus",
537 "CAPITAL LETTERS FIRST",
538 "banana",
539 "durian",
540 "apple",
541 "citrus",
542 "CAPITAL LETTERS FIRST",
543 "banana",
544 "durian",
545 "apple",
546 "citrus",
547 "CAPITAL LETTERS FIRST",
548 "banana",
549 NULL
550 };
551
552 _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL;
553
554 ASSERT_NULL(strv_sort_uniq(a));
555
556 ASSERT_NOT_NULL((a = strv_new(NULL)));
557 assert_se(strv_sort_uniq(a) == a);
558 ASSERT_NULL(a[0]);
559 a = strv_free(a);
560
561 ASSERT_NOT_NULL((a = strv_new("a", "a", "a", "a", "a")));
562 assert_se(strv_sort_uniq(a) == a);
563 ASSERT_STREQ(a[0], "a");
564 ASSERT_NULL(a[1]);
565 a = strv_free(a);
566
567 ASSERT_NOT_NULL((a = strv_new("a", "a", "a", "a", "b")));
568 assert_se(strv_sort_uniq(a) == a);
569 ASSERT_STREQ(a[0], "a");
570 ASSERT_STREQ(a[1], "b");
571 ASSERT_NULL(a[2]);
572 a = strv_free(a);
573
574 ASSERT_NOT_NULL((a = strv_new("b", "a", "a", "a", "a")));
575 assert_se(strv_sort_uniq(a) == a);
576 ASSERT_STREQ(a[0], "a");
577 ASSERT_STREQ(a[1], "b");
578 ASSERT_NULL(a[2]);
579 a = strv_free(a);
580
581 ASSERT_NOT_NULL((a = strv_new("a", "a", "b", "a", "b")));
582 assert_se(strv_sort_uniq(a) == a);
583 ASSERT_STREQ(a[0], "a");
584 ASSERT_STREQ(a[1], "b");
585 ASSERT_NULL(a[2]);
586 a = strv_free(a);
587
588 ASSERT_NOT_NULL((a = strv_copy((char**) input_table)));
589 ASSERT_NOT_NULL((b = strv_copy((char**) input_table)));
590 ASSERT_NOT_NULL((c = strv_copy((char**) input_table)));
591
592 assert_se(strv_sort_uniq(a) == a);
593 assert_se(strv_sort(strv_uniq(b)) == b);
594 assert_se(strv_uniq(strv_sort(c)) == c);
595
596 assert_se(strv_equal(a, b));
597 assert_se(strv_equal(a, c));
598
599 ASSERT_STREQ(a[0], "CAPITAL LETTERS FIRST");
600 ASSERT_STREQ(a[1], "apple");
601 ASSERT_STREQ(a[2], "banana");
602 ASSERT_STREQ(a[3], "citrus");
603 ASSERT_STREQ(a[4], "durian");
604 ASSERT_NULL(a[5]);
605 }
606
607 TEST(strv_extend_strv_biconcat) {
608 _cleanup_strv_free_ char **a = NULL, **b = NULL;
609
610 a = strv_new("without", "suffix");
611 b = strv_new("with", "suffix");
612 assert_se(a);
613 assert_se(b);
614
615 assert_se(strv_extend_strv_biconcat(&a, "prefix_", (const char* const*) b, "_suffix") >= 0);
616
617 ASSERT_STREQ(a[0], "without");
618 ASSERT_STREQ(a[1], "suffix");
619 ASSERT_STREQ(a[2], "prefix_with_suffix");
620 ASSERT_STREQ(a[3], "prefix_suffix_suffix");
621 }
622
623 TEST(strv_extend_strv_concat) {
624 _cleanup_strv_free_ char **a = NULL, **b = NULL;
625
626 a = strv_new("without", "suffix");
627 b = strv_new("with", "suffix");
628 assert_se(a);
629 assert_se(b);
630
631 assert_se(strv_extend_strv_concat(&a, (const char* const*) b, "_suffix") >= 0);
632
633 ASSERT_STREQ(a[0], "without");
634 ASSERT_STREQ(a[1], "suffix");
635 ASSERT_STREQ(a[2], "with_suffix");
636 ASSERT_STREQ(a[3], "suffix_suffix");
637 }
638
639 TEST(strv_extend_strv) {
640 _cleanup_strv_free_ char **a = NULL, **b = NULL, **n = NULL;
641
642 a = strv_new("abc", "def", "ghi");
643 b = strv_new("jkl", "mno", "abc", "pqr");
644 assert_se(a);
645 assert_se(b);
646
647 assert_se(strv_extend_strv(&a, b, true) == 3);
648
649 ASSERT_STREQ(a[0], "abc");
650 ASSERT_STREQ(a[1], "def");
651 ASSERT_STREQ(a[2], "ghi");
652 ASSERT_STREQ(a[3], "jkl");
653 ASSERT_STREQ(a[4], "mno");
654 ASSERT_STREQ(a[5], "pqr");
655 assert_se(strv_length(a) == 6);
656
657 assert_se(strv_extend_strv(&n, b, false) >= 0);
658 ASSERT_STREQ(n[0], "jkl");
659 ASSERT_STREQ(n[1], "mno");
660 ASSERT_STREQ(n[2], "abc");
661 ASSERT_STREQ(n[3], "pqr");
662 assert_se(strv_length(n) == 4);
663 }
664
665 TEST(strv_extend_strv_consume) {
666 _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL, **n = NULL;
667 const char *s1, *s2, *s3;
668
669 ASSERT_NOT_NULL((a = strv_new("abc", "def", "ghi")));
670 ASSERT_NOT_NULL((b = strv_new("jkl", "mno", "abc", "pqr")));
671
672 s1 = b[0];
673 s2 = b[1];
674 s3 = b[3];
675
676 ASSERT_EQ(strv_extend_strv_consume(&a, TAKE_PTR(b), true), 3);
677
678 assert_se(s1 == a[3]);
679 assert_se(s2 == a[4]);
680 assert_se(s3 == a[5]);
681
682 ASSERT_STREQ(a[0], "abc");
683 ASSERT_STREQ(a[1], "def");
684 ASSERT_STREQ(a[2], "ghi");
685 ASSERT_STREQ(a[3], "jkl");
686 ASSERT_STREQ(a[4], "mno");
687 ASSERT_STREQ(a[5], "pqr");
688 ASSERT_EQ(strv_length(a), (size_t) 6);
689
690 ASSERT_NOT_NULL((c = strv_new("jkl", "mno")));
691
692 s1 = c[0];
693 s2 = c[1];
694
695 ASSERT_EQ(strv_extend_strv_consume(&n, TAKE_PTR(c), false), 2);
696
697 assert_se(s1 == n[0]);
698 assert_se(s2 == n[1]);
699
700 ASSERT_STREQ(n[0], "jkl");
701 ASSERT_STREQ(n[1], "mno");
702 ASSERT_EQ(strv_length(n), (size_t) 2);
703 }
704
705 TEST(strv_extend_with_size) {
706 _cleanup_strv_free_ char **a = NULL;
707 size_t n = SIZE_MAX;
708
709 a = strv_new("test", "test1");
710 assert_se(a);
711
712 assert_se(strv_extend_with_size(&a, &n, "test2") >= 0);
713 assert_se(n == 3);
714 assert_se(strv_extend_with_size(&a, &n, "test3") >= 0);
715 assert_se(n == 4);
716
717 ASSERT_STREQ(a[0], "test");
718 ASSERT_STREQ(a[1], "test1");
719 ASSERT_STREQ(a[2], "test2");
720 ASSERT_STREQ(a[3], "test3");
721 ASSERT_NULL(a[4]);
722 }
723
724 TEST(strv_extend) {
725 _cleanup_strv_free_ char **a = NULL, **b = NULL;
726
727 a = strv_new("test", "test1");
728 assert_se(a);
729 assert_se(strv_extend(&a, "test2") >= 0);
730 assert_se(strv_extend(&b, "test3") >= 0);
731
732 ASSERT_STREQ(a[0], "test");
733 ASSERT_STREQ(a[1], "test1");
734 ASSERT_STREQ(a[2], "test2");
735 ASSERT_STREQ(b[0], "test3");
736 }
737
738 TEST(strv_extendf) {
739 _cleanup_strv_free_ char **a = NULL, **b = NULL;
740
741 a = strv_new("test", "test1");
742 assert_se(a);
743 assert_se(strv_extendf(&a, "test2 %s %d %s", "foo", 128, "bar") >= 0);
744 assert_se(strv_extendf(&b, "test3 %s %s %d", "bar", "foo", 128) >= 0);
745
746 ASSERT_STREQ(a[0], "test");
747 ASSERT_STREQ(a[1], "test1");
748 ASSERT_STREQ(a[2], "test2 foo 128 bar");
749 ASSERT_STREQ(b[0], "test3 bar foo 128");
750 }
751
752 TEST(strv_extendf_with_size) {
753 _cleanup_strv_free_ char **a = NULL;
754 size_t n = 0;
755
756 ASSERT_OK(strv_extendf_with_size(&a, &n, "test2 %s %d %s", "foo", 128, "bar"));
757 ASSERT_OK(strv_extendf_with_size(&a, &n, "test3 %s %s %d", "bar", "foo", 128));
758
759 ASSERT_EQ(n, 2u);
760 ASSERT_EQ(strv_length(a), n);
761 ASSERT_STREQ(a[0], "test2 foo 128 bar");
762 ASSERT_STREQ(a[1], "test3 bar foo 128");
763 }
764
765 TEST(strv_extend_joined) {
766 _cleanup_strv_free_ char **a = NULL;
767
768 ASSERT_OK(strv_extend_joined(&a, "hoge"));
769 ASSERT_OK(strv_extend_joined(&a, "aaa", "bbb", "ccc"));
770
771 ASSERT_EQ(strv_length(a), 2u);
772 ASSERT_STREQ(a[0], "hoge");
773 ASSERT_STREQ(a[1], "aaabbbccc");
774 }
775
776 TEST(strv_extend_joined_with_size) {
777 _cleanup_strv_free_ char **a = NULL;
778 size_t n = 0;
779
780 ASSERT_OK(strv_extend_joined_with_size(&a, &n, "hoge"));
781 ASSERT_OK(strv_extend_joined_with_size(&a, &n, "aaa", "bbb", "ccc"));
782
783 ASSERT_EQ(n, 2u);
784 ASSERT_EQ(strv_length(a), n);
785 ASSERT_STREQ(a[0], "hoge");
786 ASSERT_STREQ(a[1], "aaabbbccc");
787 }
788
789 TEST(strv_foreach) {
790 _cleanup_strv_free_ char **a;
791 unsigned i = 0;
792
793 a = strv_new("one", "two", "three");
794 assert_se(a);
795
796 STRV_FOREACH(check, a)
797 ASSERT_STREQ(*check, input_table_multiple[i++]);
798 }
799
800 TEST(strv_foreach_backwards) {
801 _cleanup_strv_free_ char **a;
802 unsigned i = 3;
803
804 a = strv_new("one", "two", "three");
805
806 assert_se(a);
807
808 STRV_FOREACH_BACKWARDS(check, a)
809 ASSERT_STREQ(*check, input_table_multiple[--i]);
810
811 STRV_FOREACH_BACKWARDS(check, (char**) NULL)
812 assert_not_reached();
813
814 STRV_FOREACH_BACKWARDS(check, STRV_EMPTY)
815 assert_not_reached();
816
817 unsigned count = 0;
818 STRV_FOREACH_BACKWARDS(check, STRV_MAKE("ONE"))
819 count++;
820 assert_se(count == 1);
821 }
822
823 TEST(strv_foreach_pair) {
824 _cleanup_strv_free_ char **a = NULL;
825
826 a = strv_new("pair_one", "pair_one",
827 "pair_two", "pair_two",
828 "pair_three", "pair_three");
829 STRV_FOREACH_PAIR(x, y, a)
830 ASSERT_STREQ(*x, *y);
831 }
832
833 TEST(strv_insert) {
834 _cleanup_strv_free_ char **a = NULL;
835
836 assert_se(strv_insert(&a, 0, strdup("first")) == 0);
837 ASSERT_STREQ(a[0], "first");
838 assert_se(!a[1]);
839
840 assert_se(strv_insert(&a, 0, NULL) == 0);
841 ASSERT_STREQ(a[0], "first");
842 assert_se(!a[1]);
843
844 assert_se(strv_insert(&a, 1, strdup("two")) == 0);
845 ASSERT_STREQ(a[0], "first");
846 ASSERT_STREQ(a[1], "two");
847 assert_se(!a[2]);
848
849 assert_se(strv_insert(&a, 4, strdup("tri")) == 0);
850 ASSERT_STREQ(a[0], "first");
851 ASSERT_STREQ(a[1], "two");
852 ASSERT_STREQ(a[2], "tri");
853 assert_se(!a[3]);
854
855 assert_se(strv_insert(&a, 1, strdup("duo")) == 0);
856 ASSERT_STREQ(a[0], "first");
857 ASSERT_STREQ(a[1], "duo");
858 ASSERT_STREQ(a[2], "two");
859 ASSERT_STREQ(a[3], "tri");
860 assert_se(!a[4]);
861 }
862
863 TEST(strv_push_prepend) {
864 _cleanup_strv_free_ char **a = NULL;
865
866 assert_se(a = strv_new("foo", "bar", "three"));
867
868 assert_se(strv_push_prepend(&a, strdup("first")) >= 0);
869 ASSERT_STREQ(a[0], "first");
870 ASSERT_STREQ(a[1], "foo");
871 ASSERT_STREQ(a[2], "bar");
872 ASSERT_STREQ(a[3], "three");
873 assert_se(!a[4]);
874
875 assert_se(strv_consume_prepend(&a, strdup("first2")) >= 0);
876 ASSERT_STREQ(a[0], "first2");
877 ASSERT_STREQ(a[1], "first");
878 ASSERT_STREQ(a[2], "foo");
879 ASSERT_STREQ(a[3], "bar");
880 ASSERT_STREQ(a[4], "three");
881 assert_se(!a[5]);
882 }
883
884 TEST(strv_push_with_size) {
885 _cleanup_strv_free_ char **a = NULL;
886 size_t n = 0;
887 char *i, *j;
888
889 assert_se(i = strdup("foo"));
890 assert_se(strv_push_with_size(&a, &n, i) >= 0);
891 assert_se(n == 1);
892
893 assert_se(i = strdup("a"));
894 assert_se(j = strdup("b"));
895 assert_se(strv_push_with_size(&a, &n, i) >= 0);
896 assert_se(n == 2);
897 assert_se(strv_push_with_size(&a, &n, j) >= 0);
898 assert_se(n == 3);
899
900 ASSERT_STREQ(a[0], "foo");
901 ASSERT_STREQ(a[1], "a");
902 ASSERT_STREQ(a[2], "b");
903 ASSERT_STREQ(a[3], NULL);
904
905 assert_se(n = strv_length(a));
906 }
907
908 TEST(strv_push) {
909 _cleanup_strv_free_ char **a = NULL;
910 char *i, *j;
911
912 assert_se(i = strdup("foo"));
913 assert_se(strv_push(&a, i) >= 0);
914
915 assert_se(i = strdup("a"));
916 assert_se(j = strdup("b"));
917 assert_se(strv_push_pair(&a, i, j) >= 0);
918
919 ASSERT_STREQ(a[0], "foo");
920 ASSERT_STREQ(a[1], "a");
921 ASSERT_STREQ(a[2], "b");
922 ASSERT_STREQ(a[3], NULL);
923 }
924
925 TEST(strv_compare) {
926 _cleanup_strv_free_ char **a = NULL;
927 _cleanup_strv_free_ char **b = NULL;
928 _cleanup_strv_free_ char **c = NULL;
929 _cleanup_strv_free_ char **d = NULL;
930
931 a = strv_new("one", "two", "three");
932 assert_se(a);
933 b = strv_new("one", "two", "three");
934 assert_se(b);
935 c = strv_new("one", "two", "three", "four");
936 assert_se(c);
937 d = strv_new(NULL);
938 assert_se(d);
939
940 assert_se(strv_compare(a, a) == 0);
941 assert_se(strv_compare(a, b) == 0);
942 assert_se(strv_compare(d, d) == 0);
943 assert_se(strv_compare(d, NULL) == 0);
944 assert_se(strv_compare(NULL, NULL) == 0);
945
946 assert_se(strv_compare(a, c) < 0);
947 assert_se(strv_compare(b, c) < 0);
948 assert_se(strv_compare(b, d) == 1);
949 assert_se(strv_compare(b, NULL) == 1);
950 }
951
952 TEST(strv_is_uniq) {
953 _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL, **d = NULL;
954
955 a = strv_new(NULL);
956 assert_se(a);
957 assert_se(strv_is_uniq(a));
958
959 b = strv_new("foo");
960 assert_se(b);
961 assert_se(strv_is_uniq(b));
962
963 c = strv_new("foo", "bar");
964 assert_se(c);
965 assert_se(strv_is_uniq(c));
966
967 d = strv_new("foo", "bar", "waldo", "bar", "piep");
968 assert_se(d);
969 assert_se(!strv_is_uniq(d));
970 }
971
972 TEST(strv_reverse) {
973 _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL, **d = NULL;
974
975 a = strv_new(NULL);
976 assert_se(a);
977
978 strv_reverse(a);
979 assert_se(strv_isempty(a));
980
981 b = strv_new("foo");
982 assert_se(b);
983 strv_reverse(b);
984 ASSERT_STREQ(b[0], "foo");
985 ASSERT_STREQ(b[1], NULL);
986
987 c = strv_new("foo", "bar");
988 assert_se(c);
989 strv_reverse(c);
990 ASSERT_STREQ(c[0], "bar");
991 ASSERT_STREQ(c[1], "foo");
992 ASSERT_STREQ(c[2], NULL);
993
994 d = strv_new("foo", "bar", "waldo");
995 assert_se(d);
996 strv_reverse(d);
997 ASSERT_STREQ(d[0], "waldo");
998 ASSERT_STREQ(d[1], "bar");
999 ASSERT_STREQ(d[2], "foo");
1000 ASSERT_STREQ(d[3], NULL);
1001 }
1002
1003 TEST(strv_shell_escape) {
1004 _cleanup_strv_free_ char **v = NULL;
1005
1006 v = strv_new("foo:bar", "bar,baz", "wal\\do");
1007 assert_se(v);
1008 assert_se(strv_shell_escape(v, ",:"));
1009 ASSERT_STREQ(v[0], "foo\\:bar");
1010 ASSERT_STREQ(v[1], "bar\\,baz");
1011 ASSERT_STREQ(v[2], "wal\\\\do");
1012 ASSERT_STREQ(v[3], NULL);
1013 }
1014
1015 static void test_strv_skip_one(char **a, size_t n, char **b) {
1016 a = strv_skip(a, n);
1017 assert_se(strv_equal(a, b));
1018 }
1019
1020 TEST(strv_skip) {
1021 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 0, STRV_MAKE("foo", "bar", "baz"));
1022 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 1, STRV_MAKE("bar", "baz"));
1023 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 2, STRV_MAKE("baz"));
1024 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 3, NULL);
1025 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 4, NULL);
1026 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 55, NULL);
1027
1028 test_strv_skip_one(STRV_MAKE("quux"), 0, STRV_MAKE("quux"));
1029 test_strv_skip_one(STRV_MAKE("quux"), 1, NULL);
1030 test_strv_skip_one(STRV_MAKE("quux"), 55, NULL);
1031
1032 test_strv_skip_one(STRV_MAKE(NULL), 0, NULL);
1033 test_strv_skip_one(STRV_MAKE(NULL), 1, NULL);
1034 test_strv_skip_one(STRV_MAKE(NULL), 55, NULL);
1035
1036 test_strv_skip_one(NULL, 0, NULL);
1037 test_strv_skip_one(NULL, 1, NULL);
1038 test_strv_skip_one(NULL, 55, NULL);
1039 }
1040
1041 TEST(strv_extend_n) {
1042 _cleanup_strv_free_ char **v = NULL;
1043
1044 v = strv_new("foo", "bar");
1045 assert_se(v);
1046
1047 assert_se(strv_extend_n(&v, "waldo", 3) >= 0);
1048 assert_se(strv_extend_n(&v, "piep", 2) >= 0);
1049
1050 ASSERT_STREQ(v[0], "foo");
1051 ASSERT_STREQ(v[1], "bar");
1052 ASSERT_STREQ(v[2], "waldo");
1053 ASSERT_STREQ(v[3], "waldo");
1054 ASSERT_STREQ(v[4], "waldo");
1055 ASSERT_STREQ(v[5], "piep");
1056 ASSERT_STREQ(v[6], "piep");
1057 ASSERT_NULL(v[7]);
1058
1059 v = strv_free(v);
1060
1061 assert_se(strv_extend_n(&v, "foo", 1) >= 0);
1062 assert_se(strv_extend_n(&v, "bar", 0) >= 0);
1063
1064 ASSERT_STREQ(v[0], "foo");
1065 ASSERT_NULL(v[1]);
1066 }
1067
1068 TEST(foreach_string) {
1069 const char * const t[] = {
1070 "foo",
1071 "bar",
1072 "waldo",
1073 NULL
1074 };
1075
1076 unsigned i = 0;
1077 FOREACH_STRING(x, "foo", "bar", "waldo")
1078 ASSERT_STREQ(t[i++], x);
1079 assert_se(i == 3);
1080
1081 FOREACH_STRING(x, "zzz")
1082 ASSERT_STREQ(x, "zzz");
1083 }
1084
1085 TEST(strv_fnmatch) {
1086 _cleanup_strv_free_ char **v = NULL;
1087 size_t pos;
1088
1089 assert_se(!strv_fnmatch(STRV_EMPTY, "a"));
1090
1091 v = strv_new("xxx", "*\\*", "yyy");
1092 assert_se(!strv_fnmatch_full(v, "\\", 0, NULL));
1093 assert_se(strv_fnmatch_full(v, "\\", FNM_NOESCAPE, &pos));
1094 assert_se(pos == 1);
1095 }
1096
1097 TEST(strv_extend_join) {
1098 _cleanup_strv_free_ char **v = NULL;
1099
1100 assert_se(strv_extend_assignment(&v, "MESSAGE", "ABC") >= 0);
1101 assert_se(strv_extend_assignment(&v, "ABC", "QER") >= 0);
1102 assert_se(strv_extend_assignment(&v, "MISSING", NULL) >= 0);
1103
1104 assert_se(strv_length(v) == 2);
1105 ASSERT_STREQ(v[0], "MESSAGE=ABC");
1106 ASSERT_STREQ(v[1], "ABC=QER");
1107 }
1108
1109 TEST(strv_copy_n) {
1110 char **x = STRV_MAKE("a", "b", "c", "d", "e");
1111 _cleanup_strv_free_ char **l = NULL;
1112
1113 l = strv_copy_n(x, 0);
1114 assert_se(strv_equal(l, NULL));
1115 strv_free(l);
1116
1117 l = strv_copy_n(x, 0);
1118 assert_se(strv_equal(l, (char**) { NULL }));
1119 strv_free(l);
1120
1121 l = strv_copy_n(x, 1);
1122 assert_se(strv_equal(l, STRV_MAKE("a")));
1123 strv_free(l);
1124
1125 l = strv_copy_n(x, 2);
1126 assert_se(strv_equal(l, STRV_MAKE("a", "b")));
1127 strv_free(l);
1128
1129 l = strv_copy_n(x, 3);
1130 assert_se(strv_equal(l, STRV_MAKE("a", "b", "c")));
1131 strv_free(l);
1132
1133 l = strv_copy_n(x, 4);
1134 assert_se(strv_equal(l, STRV_MAKE("a", "b", "c", "d")));
1135 strv_free(l);
1136
1137 l = strv_copy_n(x, 5);
1138 assert_se(strv_equal(l, STRV_MAKE("a", "b", "c", "d", "e")));
1139 strv_free(l);
1140
1141 l = strv_copy_n(x, 6);
1142 assert_se(strv_equal(l, STRV_MAKE("a", "b", "c", "d", "e")));
1143 strv_free(l);
1144
1145 l = strv_copy_n(x, SIZE_MAX);
1146 assert_se(strv_equal(l, STRV_MAKE("a", "b", "c", "d", "e")));
1147 }
1148
1149 TEST(strv_find_first_field) {
1150 char **haystack = STRV_MAKE("a", "b", "c", "d", "e", "f", "g", "h", "i", "j");
1151
1152 ASSERT_NULL(strv_find_first_field(NULL, NULL));
1153 ASSERT_NULL(strv_find_first_field(NULL, haystack));
1154 ASSERT_NULL(strv_find_first_field(STRV_MAKE("k", "l", "m", "d", "b"), NULL));
1155 ASSERT_NULL(strv_find_first_field(STRV_MAKE("k", "l", "m", "d", "b"), haystack));
1156 ASSERT_STREQ(strv_find_first_field(STRV_MAKE("k", "l", "m", "d", "a", "c"), haystack), "b");
1157 ASSERT_STREQ(strv_find_first_field(STRV_MAKE("k", "l", "m", "d", "c", "a"), haystack), "d");
1158 ASSERT_STREQ(strv_find_first_field(STRV_MAKE("i", "k", "l", "m", "d", "c", "a", "b"), haystack), "j");
1159 }
1160
1161 TEST(endswith_strv) {
1162 ASSERT_STREQ(endswith_strv("waldo", STRV_MAKE("xxx", "yyy", "ldo", "zzz")), "ldo");
1163 ASSERT_STREQ(endswith_strv("waldo", STRV_MAKE("xxx", "yyy", "zzz")), NULL);
1164 ASSERT_STREQ(endswith_strv("waldo", STRV_MAKE("waldo")), "waldo");
1165 ASSERT_STREQ(endswith_strv("waldo", STRV_MAKE("w", "o", "ldo")), "o");
1166 ASSERT_STREQ(endswith_strv("waldo", STRV_MAKE("knurz", "", "waldo")), "");
1167 }
1168
1169 TEST(strv_extend_many) {
1170 _cleanup_strv_free_ char **l = NULL;
1171
1172 assert_se(strv_extend_many(&l, NULL) >= 0);
1173 assert_se(strv_isempty(l));
1174
1175 assert_se(strv_extend_many(&l, NULL, NULL, NULL) >= 0);
1176 assert_se(strv_isempty(l));
1177
1178 assert_se(strv_extend_many(&l, "foo") >= 0);
1179 assert_se(strv_equal(l, STRV_MAKE("foo")));
1180
1181 assert_se(strv_extend_many(&l, NULL, "bar", NULL) >= 0);
1182 assert_se(strv_equal(l, STRV_MAKE("foo", "bar")));
1183
1184 assert_se(strv_extend_many(&l, "waldo", "quux") >= 0);
1185 assert_se(strv_equal(l, STRV_MAKE("foo", "bar", "waldo", "quux")));
1186
1187 assert_se(strv_extend_many(&l, "1", "2", "3", "4") >= 0);
1188 assert_se(strv_equal(l, STRV_MAKE("foo", "bar", "waldo", "quux", "1", "2", "3", "4")));
1189
1190 assert_se(strv_extend_many(&l, "yes", NULL, "no") >= 0);
1191 assert_se(strv_equal(l, STRV_MAKE("foo", "bar", "waldo", "quux", "1", "2", "3", "4", "yes", "no")));
1192 }
1193
1194 TEST(strv_rebreak_lines) {
1195 _cleanup_strv_free_ char **l = NULL;
1196
1197 assert_se(strv_rebreak_lines(NULL, SIZE_MAX, &l) >= 0);
1198 assert_se(strv_equal(l, NULL));
1199 l = strv_free(l);
1200
1201 assert_se(strv_rebreak_lines(STRV_MAKE(""), SIZE_MAX, &l) >= 0);
1202 assert_se(strv_equal(l, STRV_MAKE("")));
1203 l = strv_free(l);
1204
1205 assert_se(strv_rebreak_lines(STRV_MAKE("", ""), SIZE_MAX, &l) >= 0);
1206 assert_se(strv_equal(l, STRV_MAKE("", "")));
1207 l = strv_free(l);
1208
1209 assert_se(strv_rebreak_lines(STRV_MAKE("foo"), SIZE_MAX, &l) >= 0);
1210 assert_se(strv_equal(l, STRV_MAKE("foo")));
1211 l = strv_free(l);
1212
1213 assert_se(strv_rebreak_lines(STRV_MAKE("foo", "bar"), SIZE_MAX, &l) >= 0);
1214 assert_se(strv_equal(l, STRV_MAKE("foo", "bar")));
1215 l = strv_free(l);
1216
1217 assert_se(strv_rebreak_lines(STRV_MAKE("Foo fOo foO FOo", "bar Bar bAr baR BAr"), 10, &l) >= 0);
1218 assert_se(strv_equal(l, STRV_MAKE("Foo fOo", "foO FOo", "bar Bar", "bAr baR", "BAr")));
1219 l = strv_free(l);
1220
1221 assert_se(strv_rebreak_lines(STRV_MAKE(" foo ",
1222 " foo bar waldo quux "),
1223 10, &l) >= 0);
1224 assert_se(strv_equal(l, STRV_MAKE(" foo",
1225 " foo",
1226 "bar",
1227 "waldo quux")));
1228 l = strv_free(l);
1229
1230 assert_se(strv_rebreak_lines(STRV_MAKE(" ",
1231 "\tfoo bar\t",
1232 "FOO\tBAR"),
1233 10, &l) >= 0);
1234 assert_se(strv_equal(l, STRV_MAKE("",
1235 "\tfoo",
1236 "bar",
1237 "FOO",
1238 "BAR")));
1239 l = strv_free(l);
1240
1241 /* Now make sure that breaking the lines a 2nd time does not modify the output anymore */
1242 for (size_t i = 1; i < 100; i++) {
1243 _cleanup_strv_free_ char **a = NULL, **b = NULL;
1244
1245 assert_se(strv_rebreak_lines(STRV_MAKE("foobar waldo waldo quux piep\tschnurz pimm"), i, &a) >= 0);
1246 assert_se(strv_rebreak_lines(a, i, &b) >= 0);
1247
1248 assert_se(strv_equal(a, b));
1249 }
1250 }
1251
1252 TEST(strv_find_closest) {
1253 char **l = STRV_MAKE("aaa", "aaaa", "bbb", "ccc");
1254
1255 /* prefix match */
1256 ASSERT_STREQ(strv_find_closest(l, "a"), "aaa");
1257 ASSERT_STREQ(strv_find_closest(l, "aa"), "aaa");
1258 ASSERT_STREQ(strv_find_closest(l, "aaa"), "aaa");
1259 ASSERT_STREQ(strv_find_closest(l, "aaaa"), "aaaa");
1260 ASSERT_STREQ(strv_find_closest(l, "b"), "bbb");
1261 ASSERT_STREQ(strv_find_closest(l, "bb"), "bbb");
1262 ASSERT_STREQ(strv_find_closest(l, "bbb"), "bbb");
1263 ASSERT_STREQ(strv_find_closest(l, "c"), "ccc");
1264 ASSERT_STREQ(strv_find_closest(l, "cc"), "ccc");
1265 ASSERT_STREQ(strv_find_closest(l, "ccc"), "ccc");
1266
1267 /* levenshtein match */
1268 ASSERT_STREQ(strv_find_closest(l, "aab"), "aaa");
1269 ASSERT_STREQ(strv_find_closest(l, "abb"), "bbb");
1270 ASSERT_STREQ(strv_find_closest(l, "cbc"), "ccc");
1271 ASSERT_STREQ(strv_find_closest(l, "aax"), "aaa");
1272 ASSERT_STREQ(strv_find_closest(l, "bbbb"), "bbb");
1273 ASSERT_STREQ(strv_find_closest(l, "cbbb"), "bbb");
1274 ASSERT_STREQ(strv_find_closest(l, "bbbx"), "bbb");
1275
1276 ASSERT_NULL(strv_find_closest(l, "sfajosajfosdjaofjdsaf"));
1277 }
1278
1279 TEST(strv_equal_ignore_order) {
1280
1281 ASSERT_TRUE(strv_equal_ignore_order(NULL, NULL));
1282 ASSERT_TRUE(strv_equal_ignore_order(NULL, STRV_MAKE(NULL)));
1283 ASSERT_TRUE(strv_equal_ignore_order(STRV_MAKE(NULL), NULL));
1284 ASSERT_TRUE(strv_equal_ignore_order(STRV_MAKE(NULL), STRV_MAKE(NULL)));
1285
1286 ASSERT_FALSE(strv_equal_ignore_order(STRV_MAKE("foo"), NULL));
1287 ASSERT_FALSE(strv_equal_ignore_order(STRV_MAKE("foo"), STRV_MAKE(NULL)));
1288 ASSERT_FALSE(strv_equal_ignore_order(NULL, STRV_MAKE("foo")));
1289 ASSERT_FALSE(strv_equal_ignore_order(STRV_MAKE(NULL), STRV_MAKE("foo")));
1290 ASSERT_TRUE(strv_equal_ignore_order(STRV_MAKE("foo"), STRV_MAKE("foo")));
1291 ASSERT_FALSE(strv_equal_ignore_order(STRV_MAKE("foo"), STRV_MAKE("foo", "bar")));
1292 ASSERT_FALSE(strv_equal_ignore_order(STRV_MAKE("foo", "bar"), STRV_MAKE("foo")));
1293 ASSERT_TRUE(strv_equal_ignore_order(STRV_MAKE("foo", "bar"), STRV_MAKE("foo", "bar")));
1294 ASSERT_TRUE(strv_equal_ignore_order(STRV_MAKE("bar", "foo"), STRV_MAKE("foo", "bar")));
1295 ASSERT_FALSE(strv_equal_ignore_order(STRV_MAKE("bar", "foo"), STRV_MAKE("foo", "bar", "quux")));
1296 ASSERT_FALSE(strv_equal_ignore_order(STRV_MAKE("bar", "foo", "quux"), STRV_MAKE("foo", "bar")));
1297 ASSERT_TRUE(strv_equal_ignore_order(STRV_MAKE("bar", "foo", "quux"), STRV_MAKE("quux", "foo", "bar")));
1298 ASSERT_TRUE(strv_equal_ignore_order(STRV_MAKE("bar", "foo"), STRV_MAKE("bar", "foo", "bar", "foo", "foo")));
1299 }
1300
1301 TEST(strv_filter_prefix) {
1302 char **base = STRV_MAKE("foo", "bar", "baz", "foox", "zzz", "farb", "foerb");
1303
1304 _cleanup_strv_free_ char **x = ASSERT_PTR(strv_filter_prefix(base, "fo"));
1305 ASSERT_TRUE(strv_equal(x, STRV_MAKE("foo", "foox", "foerb")));
1306 x = strv_free(x);
1307
1308 x = ASSERT_PTR(strv_filter_prefix(base, ""));
1309 ASSERT_TRUE(strv_equal(x, base));
1310 x = strv_free(x);
1311
1312 x = ASSERT_PTR(strv_filter_prefix(base, "z"));
1313 ASSERT_TRUE(strv_equal(x, STRV_MAKE("zzz")));
1314 x = strv_free(x);
1315
1316 x = ASSERT_PTR(strv_filter_prefix(base, "zzz"));
1317 ASSERT_TRUE(strv_equal(x, STRV_MAKE("zzz")));
1318 }
1319
1320 DEFINE_TEST_MAIN(LOG_INFO);