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