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