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