1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
5 #include "nulstr-util.h"
7 #include "string-util.h"
10 static void test_specifier_printf(void) {
11 static const Specifier table
[] = {
12 { 'X', specifier_string
, (char*) "AAAA" },
13 { 'Y', specifier_string
, (char*) "BBBB" },
14 { 'm', specifier_machine_id
, NULL
},
15 { 'b', specifier_boot_id
, NULL
},
16 { 'H', specifier_host_name
, NULL
},
17 { 'v', specifier_kernel_release
, NULL
},
18 { 'a', specifier_architecture
, NULL
},
19 { 'o', specifier_os_id
, NULL
},
20 { 'w', specifier_os_version_id
, NULL
},
21 { 'B', specifier_os_build_id
, NULL
},
22 { 'W', specifier_os_variant_id
, NULL
},
26 _cleanup_free_
char *w
= NULL
;
29 log_info("/* %s */", __func__
);
31 r
= specifier_printf("xxx a=%X b=%Y yyy", table
, NULL
, &w
);
36 assert_se(streq(w
, "xxx a=AAAA b=BBBB yyy"));
39 r
= specifier_printf("machine=%m, boot=%b, host=%H, version=%v, arch=%a", table
, NULL
, &w
);
45 specifier_printf("os=%o, os-version=%w, build=%B, variant=%W", table
, NULL
, &w
);
50 static void test_str_in_set(void) {
51 log_info("/* %s */", __func__
);
53 assert_se(STR_IN_SET("x", "x", "y", "z"));
54 assert_se(!STR_IN_SET("X", "x", "y", "z"));
55 assert_se(!STR_IN_SET("", "x", "y", "z"));
56 assert_se(STR_IN_SET("x", "w", "x"));
59 static void test_strptr_in_set(void) {
60 log_info("/* %s */", __func__
);
62 assert_se(STRPTR_IN_SET("x", "x", "y", "z"));
63 assert_se(!STRPTR_IN_SET("X", "x", "y", "z"));
64 assert_se(!STRPTR_IN_SET("", "x", "y", "z"));
65 assert_se(STRPTR_IN_SET("x", "w", "x"));
67 assert_se(!STRPTR_IN_SET(NULL
, "x", "y", "z"));
68 assert_se(!STRPTR_IN_SET(NULL
, ""));
69 /* strv cannot contain a null, hence the result below */
70 assert_se(!STRPTR_IN_SET(NULL
, NULL
));
73 static void test_startswith_set(void) {
74 log_info("/* %s */", __func__
);
76 assert_se(!STARTSWITH_SET("foo", "bar", "baz", "waldo"));
77 assert_se(!STARTSWITH_SET("foo", "bar"));
79 assert_se(STARTSWITH_SET("abc", "a", "ab", "abc"));
80 assert_se(STARTSWITH_SET("abc", "ax", "ab", "abc"));
81 assert_se(STARTSWITH_SET("abc", "ax", "abx", "abc"));
82 assert_se(!STARTSWITH_SET("abc", "ax", "abx", "abcx"));
84 assert_se(streq_ptr(STARTSWITH_SET("foobar", "hhh", "kkk", "foo", "zzz"), "bar"));
85 assert_se(streq_ptr(STARTSWITH_SET("foobar", "hhh", "kkk", "", "zzz"), "foobar"));
86 assert_se(streq_ptr(STARTSWITH_SET("", "hhh", "kkk", "zzz", ""), ""));
89 static const char* const input_table_multiple
[] = {
96 static const char* const input_table_quoted
[] = {
103 static const char* const input_table_quoted_joined
[] = {
105 " two\t three " " four five",
109 static const char* const input_table_one
[] = {
114 static const char* const input_table_none
[] = {
118 static const char* const input_table_two_empties
[] = {
124 static const char* const input_table_one_empty
[] = {
129 static void test_strv_find(void) {
130 log_info("/* %s */", __func__
);
132 assert_se(strv_find((char **)input_table_multiple
, "three"));
133 assert_se(!strv_find((char **)input_table_multiple
, "four"));
136 static void test_strv_find_prefix(void) {
137 log_info("/* %s */", __func__
);
139 assert_se(strv_find_prefix((char **)input_table_multiple
, "o"));
140 assert_se(strv_find_prefix((char **)input_table_multiple
, "one"));
141 assert_se(strv_find_prefix((char **)input_table_multiple
, ""));
142 assert_se(!strv_find_prefix((char **)input_table_multiple
, "xxx"));
143 assert_se(!strv_find_prefix((char **)input_table_multiple
, "onee"));
146 static void test_strv_find_startswith(void) {
149 log_info("/* %s */", __func__
);
151 r
= strv_find_startswith((char **)input_table_multiple
, "o");
152 assert_se(r
&& streq(r
, "ne"));
154 r
= strv_find_startswith((char **)input_table_multiple
, "one");
155 assert_se(r
&& streq(r
, ""));
157 r
= strv_find_startswith((char **)input_table_multiple
, "");
158 assert_se(r
&& streq(r
, "one"));
160 assert_se(!strv_find_startswith((char **)input_table_multiple
, "xxx"));
161 assert_se(!strv_find_startswith((char **)input_table_multiple
, "onee"));
164 static void test_strv_join(void) {
165 log_info("/* %s */", __func__
);
167 _cleanup_free_
char *p
= strv_join((char **)input_table_multiple
, ", ");
169 assert_se(streq(p
, "one, two, three"));
171 _cleanup_free_
char *q
= strv_join((char **)input_table_multiple
, ";");
173 assert_se(streq(q
, "one;two;three"));
175 _cleanup_free_
char *r
= strv_join((char **)input_table_multiple
, NULL
);
177 assert_se(streq(r
, "one two three"));
179 _cleanup_free_
char *s
= strv_join(STRV_MAKE("1", "2", "3,3"), ",");
181 assert_se(streq(s
, "1,2,3,3"));
183 _cleanup_free_
char *t
= strv_join((char **)input_table_one
, ", ");
185 assert_se(streq(t
, "one"));
187 _cleanup_free_
char *u
= strv_join((char **)input_table_none
, ", ");
189 assert_se(streq(u
, ""));
191 _cleanup_free_
char *v
= strv_join((char **)input_table_two_empties
, ", ");
193 assert_se(streq(v
, ", "));
195 _cleanup_free_
char *w
= strv_join((char **)input_table_one_empty
, ", ");
197 assert_se(streq(w
, ""));
200 static void test_strv_join_full(void) {
201 log_info("/* %s */", __func__
);
203 _cleanup_free_
char *p
= strv_join_full((char **)input_table_multiple
, ", ", "foo", false);
205 assert_se(streq(p
, "fooone, footwo, foothree"));
207 _cleanup_free_
char *q
= strv_join_full((char **)input_table_multiple
, ";", "foo", false);
209 assert_se(streq(q
, "fooone;footwo;foothree"));
211 _cleanup_free_
char *r
= strv_join_full(STRV_MAKE("a", "a;b", "a:c"), ";", NULL
, true);
213 assert_se(streq(r
, "a;a\\;b;a:c"));
215 _cleanup_free_
char *s
= strv_join_full(STRV_MAKE("a", "a;b", "a;;c", ";", ";x"), ";", NULL
, true);
217 assert_se(streq(s
, "a;a\\;b;a\\;\\;c;\\;;\\;x"));
219 _cleanup_free_
char *t
= strv_join_full(STRV_MAKE("a", "a;b", "a:c", ";"), ";", "=", true);
221 assert_se(streq(t
, "=a;=a\\;b;=a:c;=\\;"));
224 _cleanup_free_
char *u
= strv_join_full((char **)input_table_multiple
, NULL
, "foo", false);
226 assert_se(streq(u
, "fooone footwo foothree"));
228 _cleanup_free_
char *v
= strv_join_full((char **)input_table_one
, ", ", "foo", false);
230 assert_se(streq(v
, "fooone"));
232 _cleanup_free_
char *w
= strv_join_full((char **)input_table_none
, ", ", "foo", false);
234 assert_se(streq(w
, ""));
236 _cleanup_free_
char *x
= strv_join_full((char **)input_table_two_empties
, ", ", "foo", false);
238 assert_se(streq(x
, "foo, foo"));
240 _cleanup_free_
char *y
= strv_join_full((char **)input_table_one_empty
, ", ", "foo", false);
242 assert_se(streq(y
, "foo"));
245 static void test_strv_unquote(const char *quoted
, char **list
) {
246 _cleanup_strv_free_
char **s
;
247 _cleanup_free_
char *j
;
252 log_info("/* %s */", __func__
);
254 r
= strv_split_full(&s
, quoted
, WHITESPACE
, EXTRACT_UNQUOTE
);
255 assert_se(r
== (int) strv_length(list
));
257 j
= strv_join(s
, " | ");
262 assert_se(streq(list
[i
++], *t
));
264 assert_se(list
[i
] == NULL
);
267 static void test_invalid_unquote(const char *quoted
) {
271 log_info("/* %s */", __func__
);
273 r
= strv_split_full(&s
, quoted
, WHITESPACE
, EXTRACT_UNQUOTE
);
274 assert_se(s
== NULL
);
275 assert_se(r
== -EINVAL
);
278 static void test_strv_split(void) {
279 _cleanup_(strv_free_erasep
) char **l
= NULL
;
280 const char str
[] = "one,two,three";
282 log_info("/* %s */", __func__
);
284 l
= strv_split(str
, ",");
286 assert_se(strv_equal(l
, (char**) input_table_multiple
));
290 l
= strv_split(" one two\t three", WHITESPACE
);
292 assert_se(strv_equal(l
, (char**) input_table_multiple
));
296 /* Setting NULL for separator is equivalent to WHITESPACE */
297 l
= strv_split(" one two\t three", NULL
);
299 assert_se(strv_equal(l
, (char**) input_table_multiple
));
303 assert_se(strv_split_full(&l
, " one two\t three", NULL
, 0) == 3);
304 assert_se(strv_equal(l
, (char**) input_table_multiple
));
308 assert_se(strv_split_full(&l
, " 'one' \" two\t three \" ' four five'", NULL
, EXTRACT_UNQUOTE
) == 3);
309 assert_se(strv_equal(l
, (char**) input_table_quoted
));
311 l
= strv_free_erase(l
);
313 /* missing last quote causes extraction to fail. */
314 assert_se(strv_split_full(&l
, " 'one' \" two\t three \" ' four five", NULL
, EXTRACT_UNQUOTE
) == -EINVAL
);
317 /* missing last quote, but the last element is _not_ ignored with EXTRACT_RELAX. */
318 assert_se(strv_split_full(&l
, " 'one' \" two\t three \" ' four five", NULL
, EXTRACT_UNQUOTE
| EXTRACT_RELAX
) == 3);
319 assert_se(strv_equal(l
, (char**) input_table_quoted
));
321 l
= strv_free_erase(l
);
323 /* missing separator between items */
324 assert_se(strv_split_full(&l
, " 'one' \" two\t three \"' four five'", NULL
, EXTRACT_UNQUOTE
| EXTRACT_RELAX
) == 2);
325 assert_se(strv_equal(l
, (char**) input_table_quoted_joined
));
327 l
= strv_free_erase(l
);
329 assert_se(strv_split_full(&l
, " 'one' \" two\t three \"' four five", NULL
,
330 EXTRACT_UNQUOTE
| EXTRACT_RELAX
| EXTRACT_CUNESCAPE_RELAX
) == 2);
331 assert_se(strv_equal(l
, (char**) input_table_quoted_joined
));
333 l
= strv_free_erase(l
);
335 assert_se(strv_split_full(&l
, "\\", NULL
, EXTRACT_UNQUOTE
| EXTRACT_RELAX
| EXTRACT_CUNESCAPE_RELAX
) == 1);
336 assert_se(strv_equal(l
, STRV_MAKE("\\")));
339 static void test_strv_split_empty(void) {
340 _cleanup_strv_free_
char **l
= NULL
;
342 log_info("/* %s */", __func__
);
344 l
= strv_split("", WHITESPACE
);
346 assert_se(strv_isempty(l
));
349 assert_se(l
= strv_split("", NULL
));
350 assert_se(strv_isempty(l
));
353 assert_se(strv_split_full(&l
, "", NULL
, 0) == 0);
355 assert_se(strv_isempty(l
));
358 assert_se(strv_split_full(&l
, "", NULL
, EXTRACT_UNQUOTE
) == 0);
360 assert_se(strv_isempty(l
));
363 assert_se(strv_split_full(&l
, "", WHITESPACE
, EXTRACT_UNQUOTE
) == 0);
365 assert_se(strv_isempty(l
));
368 assert_se(strv_split_full(&l
, "", WHITESPACE
, EXTRACT_UNQUOTE
| EXTRACT_RELAX
) == 0);
370 assert_se(strv_isempty(l
));
373 l
= strv_split(" ", WHITESPACE
);
375 assert_se(strv_isempty(l
));
378 l
= strv_split(" ", NULL
);
380 assert_se(strv_isempty(l
));
383 assert_se(strv_split_full(&l
, " ", NULL
, 0) == 0);
385 assert_se(strv_isempty(l
));
388 assert_se(strv_split_full(&l
, " ", WHITESPACE
, EXTRACT_UNQUOTE
) == 0);
390 assert_se(strv_isempty(l
));
393 assert_se(strv_split_full(&l
, " ", NULL
, EXTRACT_UNQUOTE
) == 0);
395 assert_se(strv_isempty(l
));
398 assert_se(strv_split_full(&l
, " ", NULL
, EXTRACT_UNQUOTE
| EXTRACT_RELAX
) == 0);
400 assert_se(strv_isempty(l
));
403 static void test_strv_split_full(void) {
404 _cleanup_strv_free_
char **l
= NULL
;
405 const char *str
= ":foo\\:bar::waldo:";
408 log_info("/* %s */", __func__
);
410 r
= strv_split_full(&l
, str
, ":", EXTRACT_DONT_COALESCE_SEPARATORS
);
411 assert_se(r
== (int) strv_length(l
));
412 assert_se(streq_ptr(l
[0], ""));
413 assert_se(streq_ptr(l
[1], "foo:bar"));
414 assert_se(streq_ptr(l
[2], ""));
415 assert_se(streq_ptr(l
[3], "waldo"));
416 assert_se(streq_ptr(l
[4], ""));
417 assert_se(streq_ptr(l
[5], NULL
));
420 static void test_strv_split_colon_pairs(void) {
421 _cleanup_strv_free_
char **l
= NULL
;
422 const char *str
= "one:two three four:five six seven:eight\\:nine ten\\:eleven\\\\",
423 *str_inval
="one:two three:four:five";
426 log_info("/* %s */", __func__
);
428 r
= strv_split_colon_pairs(&l
, str
);
429 assert_se(r
== (int) strv_length(l
));
431 assert_se(streq_ptr(l
[0], "one"));
432 assert_se(streq_ptr(l
[1], "two"));
433 assert_se(streq_ptr(l
[2], "three"));
434 assert_se(streq_ptr(l
[3], ""));
435 assert_se(streq_ptr(l
[4], "four"));
436 assert_se(streq_ptr(l
[5], "five"));
437 assert_se(streq_ptr(l
[6], "six"));
438 assert_se(streq_ptr(l
[7], ""));
439 assert_se(streq_ptr(l
[8], "seven"));
440 assert_se(streq_ptr(l
[9], "eight:nine"));
441 assert_se(streq_ptr(l
[10], "ten:eleven\\"));
442 assert_se(streq_ptr(l
[11], ""));
443 assert_se(streq_ptr(l
[12], NULL
));
445 r
= strv_split_colon_pairs(&l
, str_inval
);
446 assert_se(r
== -EINVAL
);
449 static void test_strv_split_newlines(void) {
452 _cleanup_strv_free_
char **l
= NULL
;
453 const char str
[] = "one\ntwo\nthree";
455 log_info("/* %s */", __func__
);
457 l
= strv_split_newlines(str
);
461 assert_se(streq(*s
, input_table_multiple
[i
++]));
464 static void test_strv_split_nulstr(void) {
465 _cleanup_strv_free_
char **l
= NULL
;
466 const char nulstr
[] = "str0\0str1\0str2\0str3\0";
468 log_info("/* %s */", __func__
);
470 l
= strv_split_nulstr (nulstr
);
473 assert_se(streq(l
[0], "str0"));
474 assert_se(streq(l
[1], "str1"));
475 assert_se(streq(l
[2], "str2"));
476 assert_se(streq(l
[3], "str3"));
479 static void test_strv_parse_nulstr(void) {
480 _cleanup_strv_free_
char **l
= NULL
;
481 const char nulstr
[] = "hoge\0hoge2\0hoge3\0\0hoge5\0\0xxx";
483 log_info("/* %s */", __func__
);
485 l
= strv_parse_nulstr(nulstr
, sizeof(nulstr
)-1);
487 puts("Parse nulstr:");
490 assert_se(streq(l
[0], "hoge"));
491 assert_se(streq(l
[1], "hoge2"));
492 assert_se(streq(l
[2], "hoge3"));
493 assert_se(streq(l
[3], ""));
494 assert_se(streq(l
[4], "hoge5"));
495 assert_se(streq(l
[5], ""));
496 assert_se(streq(l
[6], "xxx"));
499 static void test_strv_overlap(void) {
500 const char * const input_table
[] = {
506 const char * const input_table_overlap
[] = {
510 const char * const input_table_unique
[] = {
517 log_info("/* %s */", __func__
);
519 assert_se(strv_overlap((char **)input_table
, (char**)input_table_overlap
));
520 assert_se(!strv_overlap((char **)input_table
, (char**)input_table_unique
));
523 static void test_strv_sort(void) {
524 const char* input_table
[] = {
528 "CAPITAL LETTERS FIRST",
533 log_info("/* %s */", __func__
);
535 strv_sort((char **)input_table
);
537 assert_se(streq(input_table
[0], "CAPITAL LETTERS FIRST"));
538 assert_se(streq(input_table
[1], "apple"));
539 assert_se(streq(input_table
[2], "banana"));
540 assert_se(streq(input_table
[3], "citrus"));
541 assert_se(streq(input_table
[4], "durian"));
544 static void test_strv_extend_strv_concat(void) {
545 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
547 log_info("/* %s */", __func__
);
549 a
= strv_new("without", "suffix");
550 b
= strv_new("with", "suffix");
554 assert_se(strv_extend_strv_concat(&a
, b
, "_suffix") >= 0);
556 assert_se(streq(a
[0], "without"));
557 assert_se(streq(a
[1], "suffix"));
558 assert_se(streq(a
[2], "with_suffix"));
559 assert_se(streq(a
[3], "suffix_suffix"));
562 static void test_strv_extend_strv(void) {
563 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
, **n
= NULL
;
565 log_info("/* %s */", __func__
);
567 a
= strv_new("abc", "def", "ghi");
568 b
= strv_new("jkl", "mno", "abc", "pqr");
572 assert_se(strv_extend_strv(&a
, b
, true) == 3);
574 assert_se(streq(a
[0], "abc"));
575 assert_se(streq(a
[1], "def"));
576 assert_se(streq(a
[2], "ghi"));
577 assert_se(streq(a
[3], "jkl"));
578 assert_se(streq(a
[4], "mno"));
579 assert_se(streq(a
[5], "pqr"));
580 assert_se(strv_length(a
) == 6);
582 assert_se(strv_extend_strv(&n
, b
, false) >= 0);
583 assert_se(streq(n
[0], "jkl"));
584 assert_se(streq(n
[1], "mno"));
585 assert_se(streq(n
[2], "abc"));
586 assert_se(streq(n
[3], "pqr"));
587 assert_se(strv_length(n
) == 4);
590 static void test_strv_extend(void) {
591 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
593 log_info("/* %s */", __func__
);
595 a
= strv_new("test", "test1");
597 assert_se(strv_extend(&a
, "test2") >= 0);
598 assert_se(strv_extend(&b
, "test3") >= 0);
600 assert_se(streq(a
[0], "test"));
601 assert_se(streq(a
[1], "test1"));
602 assert_se(streq(a
[2], "test2"));
603 assert_se(streq(b
[0], "test3"));
606 static void test_strv_extendf(void) {
607 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
609 log_info("/* %s */", __func__
);
611 a
= strv_new("test", "test1");
613 assert_se(strv_extendf(&a
, "test2 %s %d %s", "foo", 128, "bar") >= 0);
614 assert_se(strv_extendf(&b
, "test3 %s %s %d", "bar", "foo", 128) >= 0);
616 assert_se(streq(a
[0], "test"));
617 assert_se(streq(a
[1], "test1"));
618 assert_se(streq(a
[2], "test2 foo 128 bar"));
619 assert_se(streq(b
[0], "test3 bar foo 128"));
622 static void test_strv_foreach(void) {
623 _cleanup_strv_free_
char **a
;
627 log_info("/* %s */", __func__
);
629 a
= strv_new("one", "two", "three");
632 STRV_FOREACH(check
, a
)
633 assert_se(streq(*check
, input_table_multiple
[i
++]));
636 static void test_strv_foreach_backwards(void) {
637 _cleanup_strv_free_
char **a
;
641 log_info("/* %s */", __func__
);
643 a
= strv_new("one", "two", "three");
647 STRV_FOREACH_BACKWARDS(check
, a
)
648 assert_se(streq_ptr(*check
, input_table_multiple
[i
--]));
650 STRV_FOREACH_BACKWARDS(check
, (char**) NULL
)
651 assert_not_reached("Let's see that we check empty strv right, too.");
653 STRV_FOREACH_BACKWARDS(check
, (char**) { NULL
})
654 assert_not_reached("Let's see that we check empty strv right, too.");
657 static void test_strv_foreach_pair(void) {
658 _cleanup_strv_free_
char **a
= NULL
;
661 log_info("/* %s */", __func__
);
663 a
= strv_new("pair_one", "pair_one",
664 "pair_two", "pair_two",
665 "pair_three", "pair_three");
666 STRV_FOREACH_PAIR(x
, y
, a
)
667 assert_se(streq(*x
, *y
));
670 static void test_strv_from_stdarg_alloca_one(char **l
, const char *first
, ...) {
674 log_info("/* %s */", __func__
);
676 j
= strv_from_stdarg_alloca(first
);
679 assert_se(streq_ptr(l
[i
], j
[i
]));
686 static void test_strv_from_stdarg_alloca(void) {
687 log_info("/* %s */", __func__
);
689 test_strv_from_stdarg_alloca_one(STRV_MAKE("foo", "bar"), "foo", "bar", NULL
);
690 test_strv_from_stdarg_alloca_one(STRV_MAKE("foo"), "foo", NULL
);
691 test_strv_from_stdarg_alloca_one(STRV_MAKE_EMPTY
, NULL
);
694 static void test_strv_insert(void) {
695 _cleanup_strv_free_
char **a
= NULL
;
697 log_info("/* %s */", __func__
);
699 assert_se(strv_insert(&a
, 0, strdup("first")) == 0);
700 assert_se(streq(a
[0], "first"));
703 assert_se(strv_insert(&a
, 0, NULL
) == 0);
704 assert_se(streq(a
[0], "first"));
707 assert_se(strv_insert(&a
, 1, strdup("two")) == 0);
708 assert_se(streq(a
[0], "first"));
709 assert_se(streq(a
[1], "two"));
712 assert_se(strv_insert(&a
, 4, strdup("tri")) == 0);
713 assert_se(streq(a
[0], "first"));
714 assert_se(streq(a
[1], "two"));
715 assert_se(streq(a
[2], "tri"));
718 assert_se(strv_insert(&a
, 1, strdup("duo")) == 0);
719 assert_se(streq(a
[0], "first"));
720 assert_se(streq(a
[1], "duo"));
721 assert_se(streq(a
[2], "two"));
722 assert_se(streq(a
[3], "tri"));
726 static void test_strv_push_prepend(void) {
727 _cleanup_strv_free_
char **a
= NULL
;
729 log_info("/* %s */", __func__
);
731 assert_se(a
= strv_new("foo", "bar", "three"));
733 assert_se(strv_push_prepend(&a
, strdup("first")) >= 0);
734 assert_se(streq(a
[0], "first"));
735 assert_se(streq(a
[1], "foo"));
736 assert_se(streq(a
[2], "bar"));
737 assert_se(streq(a
[3], "three"));
740 assert_se(strv_consume_prepend(&a
, strdup("first2")) >= 0);
741 assert_se(streq(a
[0], "first2"));
742 assert_se(streq(a
[1], "first"));
743 assert_se(streq(a
[2], "foo"));
744 assert_se(streq(a
[3], "bar"));
745 assert_se(streq(a
[4], "three"));
749 static void test_strv_push(void) {
750 _cleanup_strv_free_
char **a
= NULL
;
753 log_info("/* %s */", __func__
);
755 assert_se(i
= strdup("foo"));
756 assert_se(strv_push(&a
, i
) >= 0);
758 assert_se(i
= strdup("a"));
759 assert_se(j
= strdup("b"));
760 assert_se(strv_push_pair(&a
, i
, j
) >= 0);
762 assert_se(streq_ptr(a
[0], "foo"));
763 assert_se(streq_ptr(a
[1], "a"));
764 assert_se(streq_ptr(a
[2], "b"));
765 assert_se(streq_ptr(a
[3], NULL
));
768 static void test_strv_compare(void) {
769 _cleanup_strv_free_
char **a
= NULL
;
770 _cleanup_strv_free_
char **b
= NULL
;
771 _cleanup_strv_free_
char **c
= NULL
;
772 _cleanup_strv_free_
char **d
= NULL
;
774 log_info("/* %s */", __func__
);
776 a
= strv_new("one", "two", "three");
778 b
= strv_new("one", "two", "three");
780 c
= strv_new("one", "two", "three", "four");
785 assert_se(strv_compare(a
, a
) == 0);
786 assert_se(strv_compare(a
, b
) == 0);
787 assert_se(strv_compare(d
, d
) == 0);
788 assert_se(strv_compare(d
, NULL
) == 0);
789 assert_se(strv_compare(NULL
, NULL
) == 0);
791 assert_se(strv_compare(a
, c
) < 0);
792 assert_se(strv_compare(b
, c
) < 0);
793 assert_se(strv_compare(b
, d
) == 1);
794 assert_se(strv_compare(b
, NULL
) == 1);
797 static void test_strv_is_uniq(void) {
798 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
, **c
= NULL
, **d
= NULL
;
800 log_info("/* %s */", __func__
);
804 assert_se(strv_is_uniq(a
));
808 assert_se(strv_is_uniq(b
));
810 c
= strv_new("foo", "bar");
812 assert_se(strv_is_uniq(c
));
814 d
= strv_new("foo", "bar", "waldo", "bar", "piep");
816 assert_se(!strv_is_uniq(d
));
819 static void test_strv_reverse(void) {
820 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
, **c
= NULL
, **d
= NULL
;
822 log_info("/* %s */", __func__
);
828 assert_se(strv_isempty(a
));
833 assert_se(streq_ptr(b
[0], "foo"));
834 assert_se(streq_ptr(b
[1], NULL
));
836 c
= strv_new("foo", "bar");
839 assert_se(streq_ptr(c
[0], "bar"));
840 assert_se(streq_ptr(c
[1], "foo"));
841 assert_se(streq_ptr(c
[2], NULL
));
843 d
= strv_new("foo", "bar", "waldo");
846 assert_se(streq_ptr(d
[0], "waldo"));
847 assert_se(streq_ptr(d
[1], "bar"));
848 assert_se(streq_ptr(d
[2], "foo"));
849 assert_se(streq_ptr(d
[3], NULL
));
852 static void test_strv_shell_escape(void) {
853 _cleanup_strv_free_
char **v
= NULL
;
855 log_info("/* %s */", __func__
);
857 v
= strv_new("foo:bar", "bar,baz", "wal\\do");
859 assert_se(strv_shell_escape(v
, ",:"));
860 assert_se(streq_ptr(v
[0], "foo\\:bar"));
861 assert_se(streq_ptr(v
[1], "bar\\,baz"));
862 assert_se(streq_ptr(v
[2], "wal\\\\do"));
863 assert_se(streq_ptr(v
[3], NULL
));
866 static void test_strv_skip_one(char **a
, size_t n
, char **b
) {
868 assert_se(strv_equal(a
, b
));
871 static void test_strv_skip(void) {
872 log_info("/* %s */", __func__
);
874 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 0, STRV_MAKE("foo", "bar", "baz"));
875 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 1, STRV_MAKE("bar", "baz"));
876 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 2, STRV_MAKE("baz"));
877 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 3, STRV_MAKE(NULL
));
878 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 4, STRV_MAKE(NULL
));
879 test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 55, STRV_MAKE(NULL
));
881 test_strv_skip_one(STRV_MAKE("quux"), 0, STRV_MAKE("quux"));
882 test_strv_skip_one(STRV_MAKE("quux"), 1, STRV_MAKE(NULL
));
883 test_strv_skip_one(STRV_MAKE("quux"), 55, STRV_MAKE(NULL
));
885 test_strv_skip_one(STRV_MAKE(NULL
), 0, STRV_MAKE(NULL
));
886 test_strv_skip_one(STRV_MAKE(NULL
), 1, STRV_MAKE(NULL
));
887 test_strv_skip_one(STRV_MAKE(NULL
), 55, STRV_MAKE(NULL
));
890 static void test_strv_extend_n(void) {
891 _cleanup_strv_free_
char **v
= NULL
;
893 log_info("/* %s */", __func__
);
895 v
= strv_new("foo", "bar");
898 assert_se(strv_extend_n(&v
, "waldo", 3) >= 0);
899 assert_se(strv_extend_n(&v
, "piep", 2) >= 0);
901 assert_se(streq(v
[0], "foo"));
902 assert_se(streq(v
[1], "bar"));
903 assert_se(streq(v
[2], "waldo"));
904 assert_se(streq(v
[3], "waldo"));
905 assert_se(streq(v
[4], "waldo"));
906 assert_se(streq(v
[5], "piep"));
907 assert_se(streq(v
[6], "piep"));
908 assert_se(v
[7] == NULL
);
912 assert_se(strv_extend_n(&v
, "foo", 1) >= 0);
913 assert_se(strv_extend_n(&v
, "bar", 0) >= 0);
915 assert_se(streq(v
[0], "foo"));
916 assert_se(v
[1] == NULL
);
919 static void test_strv_make_nulstr_one(char **l
) {
920 _cleanup_free_
char *b
= NULL
, *c
= NULL
;
921 _cleanup_strv_free_
char **q
= NULL
;
922 const char *s
= NULL
;
926 log_info("/* %s */", __func__
);
928 assert_se(strv_make_nulstr(l
, &b
, &n
) >= 0);
929 assert_se(q
= strv_parse_nulstr(b
, n
));
930 assert_se(strv_equal(l
, q
));
932 assert_se(strv_make_nulstr(q
, &c
, &m
) >= 0);
934 assert_se(memcmp(b
, c
, m
) == 0);
937 assert_se(streq(s
, l
[i
++]));
938 assert_se(i
== strv_length(l
));
941 static void test_strv_make_nulstr(void) {
942 log_info("/* %s */", __func__
);
944 test_strv_make_nulstr_one(NULL
);
945 test_strv_make_nulstr_one(STRV_MAKE(NULL
));
946 test_strv_make_nulstr_one(STRV_MAKE("foo"));
947 test_strv_make_nulstr_one(STRV_MAKE("foo", "bar"));
948 test_strv_make_nulstr_one(STRV_MAKE("foo", "bar", "quuux"));
951 static void test_strv_free_free(void) {
954 log_info("/* %s */", __func__
);
956 assert_se(t
= new(char**, 3));
957 assert_se(t
[0] = strv_new("a", "b"));
958 assert_se(t
[1] = strv_new("c", "d", "e"));
961 t
= strv_free_free(t
);
964 static void test_foreach_string(void) {
965 const char * const t
[] = {
974 log_info("/* %s */", __func__
);
976 FOREACH_STRING(x
, "foo", "bar", "waldo")
977 assert_se(streq_ptr(t
[i
++], x
));
981 FOREACH_STRING(x
, "zzz")
982 assert_se(streq(x
, "zzz"));
985 static void test_strv_fnmatch(void) {
986 _cleanup_strv_free_
char **v
= NULL
;
989 log_info("/* %s */", __func__
);
991 assert_se(!strv_fnmatch(STRV_MAKE_EMPTY
, "a"));
993 v
= strv_new("xxx", "*\\*", "yyy");
994 assert_se(!strv_fnmatch_full(v
, "\\", 0, NULL
));
995 assert_se(strv_fnmatch_full(v
, "\\", FNM_NOESCAPE
, &pos
));
999 int main(int argc
, char *argv
[]) {
1000 test_specifier_printf();
1002 test_strptr_in_set();
1003 test_startswith_set();
1004 test_strv_foreach();
1005 test_strv_foreach_backwards();
1006 test_strv_foreach_pair();
1008 test_strv_find_prefix();
1009 test_strv_find_startswith();
1011 test_strv_join_full();
1013 test_strv_unquote(" foo=bar \"waldo\" zzz ", STRV_MAKE("foo=bar", "waldo", "zzz"));
1014 test_strv_unquote("", STRV_MAKE_EMPTY
);
1015 test_strv_unquote(" ", STRV_MAKE_EMPTY
);
1016 test_strv_unquote(" ", STRV_MAKE_EMPTY
);
1017 test_strv_unquote(" x", STRV_MAKE("x"));
1018 test_strv_unquote("x ", STRV_MAKE("x"));
1019 test_strv_unquote(" x ", STRV_MAKE("x"));
1020 test_strv_unquote(" \"x\" ", STRV_MAKE("x"));
1021 test_strv_unquote(" 'x' ", STRV_MAKE("x"));
1022 test_strv_unquote(" 'x\"' ", STRV_MAKE("x\""));
1023 test_strv_unquote(" \"x'\" ", STRV_MAKE("x'"));
1024 test_strv_unquote("a '--b=c \"d e\"'", STRV_MAKE("a", "--b=c \"d e\""));
1026 /* trailing backslashes */
1027 test_strv_unquote(" x\\\\", STRV_MAKE("x\\"));
1028 test_invalid_unquote(" x\\");
1030 test_invalid_unquote("a --b='c \"d e\"''");
1031 test_invalid_unquote("a --b='c \"d e\" '\"");
1032 test_invalid_unquote("a --b='c \"d e\"garbage");
1033 test_invalid_unquote("'");
1034 test_invalid_unquote("\"");
1035 test_invalid_unquote("'x'y'g");
1038 test_strv_split_empty();
1039 test_strv_split_full();
1040 test_strv_split_colon_pairs();
1041 test_strv_split_newlines();
1042 test_strv_split_nulstr();
1043 test_strv_parse_nulstr();
1044 test_strv_overlap();
1046 test_strv_extend_strv();
1047 test_strv_extend_strv_concat();
1049 test_strv_extendf();
1050 test_strv_from_stdarg_alloca();
1052 test_strv_push_prepend();
1054 test_strv_compare();
1055 test_strv_is_uniq();
1056 test_strv_reverse();
1057 test_strv_shell_escape();
1059 test_strv_extend_n();
1060 test_strv_make_nulstr();
1061 test_strv_free_free();
1063 test_foreach_string();
1064 test_strv_fnmatch();