]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
f90cf44c | 2 | |
b5efdb8a | 3 | #include "alloc-util.h" |
e7b2ea7c | 4 | #include "escape.h" |
07630cea | 5 | #include "string-util.h" |
682cfdff | 6 | #include "strv.h" |
4f7452a8 | 7 | #include "tests.h" |
f90cf44c | 8 | |
4f7452a8 | 9 | TEST(str_in_set) { |
c7bf9d51 ZJS |
10 | assert_se(STR_IN_SET("x", "x", "y", "z")); |
11 | assert_se(!STR_IN_SET("X", "x", "y", "z")); | |
12 | assert_se(!STR_IN_SET("", "x", "y", "z")); | |
13 | assert_se(STR_IN_SET("x", "w", "x")); | |
14 | } | |
15 | ||
4f7452a8 | 16 | TEST(strptr_in_set) { |
c7bf9d51 ZJS |
17 | assert_se(STRPTR_IN_SET("x", "x", "y", "z")); |
18 | assert_se(!STRPTR_IN_SET("X", "x", "y", "z")); | |
19 | assert_se(!STRPTR_IN_SET("", "x", "y", "z")); | |
20 | assert_se(STRPTR_IN_SET("x", "w", "x")); | |
21 | ||
22 | assert_se(!STRPTR_IN_SET(NULL, "x", "y", "z")); | |
23 | assert_se(!STRPTR_IN_SET(NULL, "")); | |
24 | /* strv cannot contain a null, hence the result below */ | |
25 | assert_se(!STRPTR_IN_SET(NULL, NULL)); | |
26 | } | |
27 | ||
4f7452a8 | 28 | TEST(startswith_set) { |
52f15520 LP |
29 | assert_se(!STARTSWITH_SET("foo", "bar", "baz", "waldo")); |
30 | assert_se(!STARTSWITH_SET("foo", "bar")); | |
31 | ||
32 | assert_se(STARTSWITH_SET("abc", "a", "ab", "abc")); | |
33 | assert_se(STARTSWITH_SET("abc", "ax", "ab", "abc")); | |
34 | assert_se(STARTSWITH_SET("abc", "ax", "abx", "abc")); | |
35 | assert_se(!STARTSWITH_SET("abc", "ax", "abx", "abcx")); | |
36 | ||
37 | assert_se(streq_ptr(STARTSWITH_SET("foobar", "hhh", "kkk", "foo", "zzz"), "bar")); | |
38 | assert_se(streq_ptr(STARTSWITH_SET("foobar", "hhh", "kkk", "", "zzz"), "foobar")); | |
39 | assert_se(streq_ptr(STARTSWITH_SET("", "hhh", "kkk", "zzz", ""), "")); | |
40 | } | |
41 | ||
a6fde353 ZJS |
42 | static const char* const input_table_multiple[] = { |
43 | "one", | |
44 | "two", | |
45 | "three", | |
46 | NULL, | |
47 | }; | |
48 | ||
1f938af8 YW |
49 | static const char* const input_table_quoted[] = { |
50 | "one", | |
51 | " two\t three ", | |
52 | " four five", | |
53 | NULL, | |
54 | }; | |
55 | ||
0645b83a ZJS |
56 | static const char* const input_table_quoted_joined[] = { |
57 | "one", | |
58 | " two\t three " " four five", | |
59 | NULL, | |
60 | }; | |
61 | ||
a6fde353 ZJS |
62 | static const char* const input_table_one[] = { |
63 | "one", | |
64 | NULL, | |
65 | }; | |
66 | ||
67 | static const char* const input_table_none[] = { | |
68 | NULL, | |
69 | }; | |
70 | ||
afe773b0 ZJS |
71 | static const char* const input_table_two_empties[] = { |
72 | "", | |
73 | "", | |
74 | NULL, | |
75 | }; | |
76 | ||
77 | static const char* const input_table_one_empty[] = { | |
78 | "", | |
79 | NULL, | |
80 | }; | |
81 | ||
f385c447 YW |
82 | static const char* const input_table_unescape[] = { |
83 | "ID_VENDOR=QEMU", | |
84 | "ID_VENDOR_ENC=QEMUx20x20x20x20", | |
85 | "ID_MODEL_ENC=QEMUx20HARDDISKx20x20x20", | |
86 | NULL, | |
87 | }; | |
88 | ||
89 | static const char* const input_table_retain_escape[] = { | |
90 | "ID_VENDOR=QEMU", | |
91 | "ID_VENDOR_ENC=QEMU\\x20\\x20\\x20\\x20", | |
92 | "ID_MODEL_ENC=QEMU\\x20HARDDISK\\x20\\x20\\x20", | |
93 | NULL, | |
94 | }; | |
95 | ||
4f7452a8 | 96 | TEST(strv_find) { |
a6fde353 ZJS |
97 | assert_se(strv_find((char **)input_table_multiple, "three")); |
98 | assert_se(!strv_find((char **)input_table_multiple, "four")); | |
539ad707 TA |
99 | } |
100 | ||
4f7452a8 | 101 | TEST(strv_find_prefix) { |
a6fde353 ZJS |
102 | assert_se(strv_find_prefix((char **)input_table_multiple, "o")); |
103 | assert_se(strv_find_prefix((char **)input_table_multiple, "one")); | |
104 | assert_se(strv_find_prefix((char **)input_table_multiple, "")); | |
105 | assert_se(!strv_find_prefix((char **)input_table_multiple, "xxx")); | |
106 | assert_se(!strv_find_prefix((char **)input_table_multiple, "onee")); | |
3a7719d3 | 107 | } |
f90cf44c | 108 | |
4f7452a8 | 109 | TEST(strv_find_startswith) { |
7bd57a87 RC |
110 | char *r; |
111 | ||
112 | r = strv_find_startswith((char **)input_table_multiple, "o"); | |
113 | assert_se(r && streq(r, "ne")); | |
114 | ||
115 | r = strv_find_startswith((char **)input_table_multiple, "one"); | |
116 | assert_se(r && streq(r, "")); | |
117 | ||
118 | r = strv_find_startswith((char **)input_table_multiple, ""); | |
119 | assert_se(r && streq(r, "one")); | |
120 | ||
121 | assert_se(!strv_find_startswith((char **)input_table_multiple, "xxx")); | |
122 | assert_se(!strv_find_startswith((char **)input_table_multiple, "onee")); | |
123 | } | |
124 | ||
4f7452a8 | 125 | TEST(strv_join) { |
d4d9f034 | 126 | _cleanup_free_ char *p = strv_join((char **)input_table_multiple, ", "); |
04045d84 | 127 | assert_se(p); |
7b68d618 | 128 | assert_se(streq(p, "one, two, three")); |
682cfdff | 129 | |
d4d9f034 | 130 | _cleanup_free_ char *q = strv_join((char **)input_table_multiple, ";"); |
04045d84 | 131 | assert_se(q); |
7b68d618 | 132 | assert_se(streq(q, "one;two;three")); |
682cfdff | 133 | |
d4d9f034 | 134 | _cleanup_free_ char *r = strv_join((char **)input_table_multiple, NULL); |
04045d84 | 135 | assert_se(r); |
7b68d618 | 136 | assert_se(streq(r, "one two three")); |
539ad707 | 137 | |
d4d9f034 | 138 | _cleanup_free_ char *s = strv_join(STRV_MAKE("1", "2", "3,3"), ","); |
04045d84 | 139 | assert_se(s); |
d4d9f034 | 140 | assert_se(streq(s, "1,2,3,3")); |
539ad707 | 141 | |
d4d9f034 | 142 | _cleanup_free_ char *t = strv_join((char **)input_table_one, ", "); |
04045d84 | 143 | assert_se(t); |
d4d9f034 ZJS |
144 | assert_se(streq(t, "one")); |
145 | ||
146 | _cleanup_free_ char *u = strv_join((char **)input_table_none, ", "); | |
147 | assert_se(u); | |
148 | assert_se(streq(u, "")); | |
afe773b0 | 149 | |
d4d9f034 | 150 | _cleanup_free_ char *v = strv_join((char **)input_table_two_empties, ", "); |
afe773b0 ZJS |
151 | assert_se(v); |
152 | assert_se(streq(v, ", ")); | |
153 | ||
d4d9f034 | 154 | _cleanup_free_ char *w = strv_join((char **)input_table_one_empty, ", "); |
afe773b0 ZJS |
155 | assert_se(w); |
156 | assert_se(streq(w, "")); | |
682cfdff DB |
157 | } |
158 | ||
4f7452a8 | 159 | TEST(strv_join_full) { |
d4d9f034 | 160 | _cleanup_free_ char *p = strv_join_full((char **)input_table_multiple, ", ", "foo", false); |
474a595a YW |
161 | assert_se(p); |
162 | assert_se(streq(p, "fooone, footwo, foothree")); | |
163 | ||
d4d9f034 | 164 | _cleanup_free_ char *q = strv_join_full((char **)input_table_multiple, ";", "foo", false); |
474a595a YW |
165 | assert_se(q); |
166 | assert_se(streq(q, "fooone;footwo;foothree")); | |
167 | ||
d4d9f034 | 168 | _cleanup_free_ char *r = strv_join_full(STRV_MAKE("a", "a;b", "a:c"), ";", NULL, true); |
474a595a | 169 | assert_se(r); |
d4d9f034 | 170 | assert_se(streq(r, "a;a\\;b;a:c")); |
474a595a | 171 | |
d4d9f034 | 172 | _cleanup_free_ char *s = strv_join_full(STRV_MAKE("a", "a;b", "a;;c", ";", ";x"), ";", NULL, true); |
474a595a | 173 | assert_se(s); |
d4d9f034 | 174 | assert_se(streq(s, "a;a\\;b;a\\;\\;c;\\;;\\;x")); |
474a595a | 175 | |
d4d9f034 | 176 | _cleanup_free_ char *t = strv_join_full(STRV_MAKE("a", "a;b", "a:c", ";"), ";", "=", true); |
474a595a | 177 | assert_se(t); |
d4d9f034 ZJS |
178 | assert_se(streq(t, "=a;=a\\;b;=a:c;=\\;")); |
179 | t = mfree(t); | |
180 | ||
181 | _cleanup_free_ char *u = strv_join_full((char **)input_table_multiple, NULL, "foo", false); | |
182 | assert_se(u); | |
183 | assert_se(streq(u, "fooone footwo foothree")); | |
474a595a | 184 | |
d4d9f034 | 185 | _cleanup_free_ char *v = strv_join_full((char **)input_table_one, ", ", "foo", false); |
474a595a | 186 | assert_se(v); |
d4d9f034 | 187 | assert_se(streq(v, "fooone")); |
474a595a | 188 | |
d4d9f034 | 189 | _cleanup_free_ char *w = strv_join_full((char **)input_table_none, ", ", "foo", false); |
474a595a | 190 | assert_se(w); |
d4d9f034 ZJS |
191 | assert_se(streq(w, "")); |
192 | ||
193 | _cleanup_free_ char *x = strv_join_full((char **)input_table_two_empties, ", ", "foo", false); | |
194 | assert_se(x); | |
195 | assert_se(streq(x, "foo, foo")); | |
196 | ||
197 | _cleanup_free_ char *y = strv_join_full((char **)input_table_one_empty, ", ", "foo", false); | |
198 | assert_se(y); | |
199 | assert_se(streq(y, "foo")); | |
474a595a YW |
200 | } |
201 | ||
4f7452a8 | 202 | static void test_strv_unquote_one(const char *quoted, char **list) { |
3d41b6b8 DDM |
203 | _cleanup_strv_free_ char **s = NULL; |
204 | _cleanup_free_ char *j = NULL; | |
70f75a52 | 205 | unsigned i = 0; |
b2fadec6 | 206 | int r; |
70f75a52 | 207 | |
0bf2c5e5 ZJS |
208 | log_info("/* %s */", __func__); |
209 | ||
90e30d76 | 210 | r = strv_split_full(&s, quoted, WHITESPACE, EXTRACT_UNQUOTE); |
8dd4c05b | 211 | assert_se(r == (int) strv_length(list)); |
70f75a52 | 212 | assert_se(s); |
a2a5291b | 213 | j = strv_join(s, " | "); |
bdf7026e | 214 | assert_se(j); |
a2a5291b | 215 | puts(j); |
70f75a52 LP |
216 | |
217 | STRV_FOREACH(t, s) | |
218 | assert_se(streq(list[i++], *t)); | |
219 | ||
220 | assert_se(list[i] == NULL); | |
221 | } | |
222 | ||
4f7452a8 JJ |
223 | TEST(strv_unquote) { |
224 | test_strv_unquote_one(" foo=bar \"waldo\" zzz ", STRV_MAKE("foo=bar", "waldo", "zzz")); | |
225 | test_strv_unquote_one("", STRV_MAKE_EMPTY); | |
226 | test_strv_unquote_one(" ", STRV_MAKE_EMPTY); | |
227 | test_strv_unquote_one(" ", STRV_MAKE_EMPTY); | |
228 | test_strv_unquote_one(" x", STRV_MAKE("x")); | |
229 | test_strv_unquote_one("x ", STRV_MAKE("x")); | |
230 | test_strv_unquote_one(" x ", STRV_MAKE("x")); | |
231 | test_strv_unquote_one(" \"x\" ", STRV_MAKE("x")); | |
232 | test_strv_unquote_one(" 'x' ", STRV_MAKE("x")); | |
233 | test_strv_unquote_one(" 'x\"' ", STRV_MAKE("x\"")); | |
234 | test_strv_unquote_one(" \"x'\" ", STRV_MAKE("x'")); | |
235 | test_strv_unquote_one("a '--b=c \"d e\"'", STRV_MAKE("a", "--b=c \"d e\"")); | |
236 | ||
237 | /* trailing backslashes */ | |
238 | test_strv_unquote_one(" x\\\\", STRV_MAKE("x\\")); | |
239 | } | |
240 | ||
241 | static void test_invalid_unquote_one(const char *quoted) { | |
b2fadec6 ZJS |
242 | char **s = NULL; |
243 | int r; | |
a2a5291b | 244 | |
0bf2c5e5 ZJS |
245 | log_info("/* %s */", __func__); |
246 | ||
90e30d76 | 247 | r = strv_split_full(&s, quoted, WHITESPACE, EXTRACT_UNQUOTE); |
bdf7026e TA |
248 | assert_se(s == NULL); |
249 | assert_se(r == -EINVAL); | |
a2a5291b ZJS |
250 | } |
251 | ||
4f7452a8 JJ |
252 | TEST(invalid_unquote) { |
253 | test_invalid_unquote_one(" x\\"); | |
254 | test_invalid_unquote_one("a --b='c \"d e\"''"); | |
255 | test_invalid_unquote_one("a --b='c \"d e\" '\""); | |
256 | test_invalid_unquote_one("a --b='c \"d e\"garbage"); | |
257 | test_invalid_unquote_one("'"); | |
258 | test_invalid_unquote_one("\""); | |
259 | test_invalid_unquote_one("'x'y'g"); | |
260 | } | |
261 | ||
262 | TEST(strv_split) { | |
5d2a48da | 263 | _cleanup_strv_free_erase_ char **l = NULL; |
aed2ebfe DB |
264 | const char str[] = "one,two,three"; |
265 | ||
266 | l = strv_split(str, ","); | |
bdf7026e | 267 | assert_se(l); |
1f938af8 | 268 | assert_se(strv_equal(l, (char**) input_table_multiple)); |
aed2ebfe | 269 | |
b4f386d5 | 270 | strv_free_erase(l); |
0745ce75 YW |
271 | |
272 | l = strv_split(" one two\t three", WHITESPACE); | |
273 | assert_se(l); | |
1f938af8 YW |
274 | assert_se(strv_equal(l, (char**) input_table_multiple)); |
275 | ||
b4f386d5 | 276 | strv_free_erase(l); |
1f938af8 YW |
277 | |
278 | /* Setting NULL for separator is equivalent to WHITESPACE */ | |
279 | l = strv_split(" one two\t three", NULL); | |
280 | assert_se(l); | |
281 | assert_se(strv_equal(l, (char**) input_table_multiple)); | |
282 | ||
b4f386d5 | 283 | strv_free_erase(l); |
1f938af8 | 284 | |
90e30d76 | 285 | assert_se(strv_split_full(&l, " one two\t three", NULL, 0) == 3); |
1f938af8 YW |
286 | assert_se(strv_equal(l, (char**) input_table_multiple)); |
287 | ||
b4f386d5 | 288 | strv_free_erase(l); |
1f938af8 | 289 | |
90e30d76 | 290 | assert_se(strv_split_full(&l, " 'one' \" two\t three \" ' four five'", NULL, EXTRACT_UNQUOTE) == 3); |
1f938af8 YW |
291 | assert_se(strv_equal(l, (char**) input_table_quoted)); |
292 | ||
0645b83a | 293 | l = strv_free_erase(l); |
1f938af8 | 294 | |
0645b83a | 295 | /* missing last quote causes extraction to fail. */ |
90e30d76 | 296 | assert_se(strv_split_full(&l, " 'one' \" two\t three \" ' four five", NULL, EXTRACT_UNQUOTE) == -EINVAL); |
0645b83a | 297 | assert_se(!l); |
1f938af8 | 298 | |
0645b83a | 299 | /* missing last quote, but the last element is _not_ ignored with EXTRACT_RELAX. */ |
90e30d76 | 300 | assert_se(strv_split_full(&l, " 'one' \" two\t three \" ' four five", NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX) == 3); |
1f938af8 YW |
301 | assert_se(strv_equal(l, (char**) input_table_quoted)); |
302 | ||
0645b83a | 303 | l = strv_free_erase(l); |
1f938af8 | 304 | |
0645b83a | 305 | /* missing separator between items */ |
90e30d76 | 306 | assert_se(strv_split_full(&l, " 'one' \" two\t three \"' four five'", NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX) == 2); |
0645b83a | 307 | assert_se(strv_equal(l, (char**) input_table_quoted_joined)); |
1f938af8 | 308 | |
0645b83a | 309 | l = strv_free_erase(l); |
1f938af8 | 310 | |
90e30d76 | 311 | assert_se(strv_split_full(&l, " 'one' \" two\t three \"' four five", NULL, |
3141089f | 312 | EXTRACT_UNQUOTE | EXTRACT_RELAX | EXTRACT_UNESCAPE_RELAX) == 2); |
0645b83a | 313 | assert_se(strv_equal(l, (char**) input_table_quoted_joined)); |
29965a2a | 314 | |
0645b83a | 315 | l = strv_free_erase(l); |
29965a2a | 316 | |
3141089f | 317 | assert_se(strv_split_full(&l, "\\", NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX | EXTRACT_UNESCAPE_RELAX) == 1); |
29965a2a | 318 | assert_se(strv_equal(l, STRV_MAKE("\\"))); |
b38a9d2d ZJS |
319 | |
320 | l = strv_free_erase(l); | |
321 | ||
322 | assert_se(l = strv_split("\\", NULL)); | |
323 | assert_se(strv_equal(l, STRV_MAKE("\\"))); | |
324 | ||
325 | l = strv_free_erase(l); | |
326 | ||
327 | assert_se(l = strv_split("aa\\ bb\\", NULL)); | |
328 | assert_se(strv_equal(l, STRV_MAKE("aa\\", "bb\\"))); | |
329 | ||
330 | l = strv_free_erase(l); | |
331 | ||
332 | assert_se(l = strv_split("aa\" bb'", NULL)); | |
333 | assert_se(strv_equal(l, STRV_MAKE("aa\"", "bb'"))); | |
0745ce75 YW |
334 | } |
335 | ||
4f7452a8 | 336 | TEST(strv_split_empty) { |
0745ce75 YW |
337 | _cleanup_strv_free_ char **l = NULL; |
338 | ||
339 | l = strv_split("", WHITESPACE); | |
340 | assert_se(l); | |
341 | assert_se(strv_isempty(l)); | |
0645b83a | 342 | l = strv_free(l); |
0745ce75 | 343 | |
0645b83a | 344 | assert_se(l = strv_split("", NULL)); |
1f938af8 | 345 | assert_se(strv_isempty(l)); |
0645b83a | 346 | l = strv_free(l); |
1f938af8 | 347 | |
90e30d76 | 348 | assert_se(strv_split_full(&l, "", NULL, 0) == 0); |
1f938af8 YW |
349 | assert_se(l); |
350 | assert_se(strv_isempty(l)); | |
0645b83a | 351 | l = strv_free(l); |
1f938af8 | 352 | |
90e30d76 | 353 | assert_se(strv_split_full(&l, "", NULL, EXTRACT_UNQUOTE) == 0); |
1f938af8 YW |
354 | assert_se(l); |
355 | assert_se(strv_isempty(l)); | |
0645b83a | 356 | l = strv_free(l); |
1f938af8 | 357 | |
90e30d76 | 358 | assert_se(strv_split_full(&l, "", WHITESPACE, EXTRACT_UNQUOTE) == 0); |
1f938af8 YW |
359 | assert_se(l); |
360 | assert_se(strv_isempty(l)); | |
0645b83a | 361 | l = strv_free(l); |
1f938af8 | 362 | |
90e30d76 | 363 | assert_se(strv_split_full(&l, "", WHITESPACE, EXTRACT_UNQUOTE | EXTRACT_RELAX) == 0); |
1f938af8 YW |
364 | assert_se(l); |
365 | assert_se(strv_isempty(l)); | |
0745ce75 | 366 | strv_free(l); |
0645b83a | 367 | |
0745ce75 YW |
368 | l = strv_split(" ", WHITESPACE); |
369 | assert_se(l); | |
370 | assert_se(strv_isempty(l)); | |
1f938af8 | 371 | strv_free(l); |
0645b83a | 372 | |
1f938af8 YW |
373 | l = strv_split(" ", NULL); |
374 | assert_se(l); | |
375 | assert_se(strv_isempty(l)); | |
0645b83a | 376 | l = strv_free(l); |
1f938af8 | 377 | |
90e30d76 | 378 | assert_se(strv_split_full(&l, " ", NULL, 0) == 0); |
1f938af8 YW |
379 | assert_se(l); |
380 | assert_se(strv_isempty(l)); | |
0645b83a | 381 | l = strv_free(l); |
1f938af8 | 382 | |
90e30d76 | 383 | assert_se(strv_split_full(&l, " ", WHITESPACE, EXTRACT_UNQUOTE) == 0); |
1f938af8 YW |
384 | assert_se(l); |
385 | assert_se(strv_isempty(l)); | |
0645b83a | 386 | l = strv_free(l); |
1f938af8 | 387 | |
90e30d76 | 388 | assert_se(strv_split_full(&l, " ", NULL, EXTRACT_UNQUOTE) == 0); |
1f938af8 YW |
389 | assert_se(l); |
390 | assert_se(strv_isempty(l)); | |
0645b83a | 391 | l = strv_free(l); |
1f938af8 | 392 | |
90e30d76 | 393 | assert_se(strv_split_full(&l, " ", NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX) == 0); |
1f938af8 YW |
394 | assert_se(l); |
395 | assert_se(strv_isempty(l)); | |
aed2ebfe DB |
396 | } |
397 | ||
4f7452a8 | 398 | TEST(strv_split_full) { |
8adaf7bd RM |
399 | _cleanup_strv_free_ char **l = NULL; |
400 | const char *str = ":foo\\:bar::waldo:"; | |
401 | int r; | |
402 | ||
90e30d76 | 403 | r = strv_split_full(&l, str, ":", EXTRACT_DONT_COALESCE_SEPARATORS); |
8dd4c05b | 404 | assert_se(r == (int) strv_length(l)); |
8adaf7bd RM |
405 | assert_se(streq_ptr(l[0], "")); |
406 | assert_se(streq_ptr(l[1], "foo:bar")); | |
407 | assert_se(streq_ptr(l[2], "")); | |
408 | assert_se(streq_ptr(l[3], "waldo")); | |
409 | assert_se(streq_ptr(l[4], "")); | |
410 | assert_se(streq_ptr(l[5], NULL)); | |
411 | } | |
412 | ||
4f7452a8 | 413 | TEST(strv_split_and_extend_full) { |
3c318caa FS |
414 | _cleanup_strv_free_ char **l = NULL; |
415 | const char *str1 = ":foo\\:bar:"; | |
416 | const char *str2 = "waldo::::::baz"; | |
417 | int r; | |
418 | ||
3c318caa FS |
419 | r = strv_split_and_extend(&l, "", ":", false); |
420 | assert_se(r == (int) strv_length(l)); | |
421 | r = strv_split_and_extend_full(&l, str1, ":", false, EXTRACT_DONT_COALESCE_SEPARATORS); | |
422 | assert_se(r == (int) strv_length(l)); | |
423 | assert_se(streq_ptr(l[0], "")); | |
424 | assert_se(streq_ptr(l[1], "foo:bar")); | |
425 | assert_se(streq_ptr(l[2], "")); | |
426 | r = strv_split_and_extend_full(&l, str2, ":", false, 0); | |
427 | assert_se(r == (int) strv_length(l)); | |
428 | assert_se(streq_ptr(l[3], "waldo")); | |
429 | assert_se(streq_ptr(l[4], "baz")); | |
430 | assert_se(streq_ptr(l[5], NULL)); | |
431 | } | |
432 | ||
4f7452a8 | 433 | TEST(strv_split_colon_pairs) { |
a082edd5 LB |
434 | _cleanup_strv_free_ char **l = NULL; |
435 | const char *str = "one:two three four:five six seven:eight\\:nine ten\\:eleven\\\\", | |
436 | *str_inval="one:two three:four:five"; | |
437 | int r; | |
438 | ||
a082edd5 LB |
439 | r = strv_split_colon_pairs(&l, str); |
440 | assert_se(r == (int) strv_length(l)); | |
441 | assert_se(r == 12); | |
442 | assert_se(streq_ptr(l[0], "one")); | |
443 | assert_se(streq_ptr(l[1], "two")); | |
444 | assert_se(streq_ptr(l[2], "three")); | |
445 | assert_se(streq_ptr(l[3], "")); | |
446 | assert_se(streq_ptr(l[4], "four")); | |
447 | assert_se(streq_ptr(l[5], "five")); | |
448 | assert_se(streq_ptr(l[6], "six")); | |
449 | assert_se(streq_ptr(l[7], "")); | |
450 | assert_se(streq_ptr(l[8], "seven")); | |
451 | assert_se(streq_ptr(l[9], "eight:nine")); | |
452 | assert_se(streq_ptr(l[10], "ten:eleven\\")); | |
453 | assert_se(streq_ptr(l[11], "")); | |
454 | assert_se(streq_ptr(l[12], NULL)); | |
455 | ||
456 | r = strv_split_colon_pairs(&l, str_inval); | |
457 | assert_se(r == -EINVAL); | |
458 | } | |
459 | ||
4f7452a8 | 460 | TEST(strv_split_newlines) { |
aed2ebfe | 461 | unsigned i = 0; |
aed2ebfe DB |
462 | _cleanup_strv_free_ char **l = NULL; |
463 | const char str[] = "one\ntwo\nthree"; | |
464 | ||
0bf2c5e5 | 465 | l = strv_split_newlines(str); |
bdf7026e | 466 | assert_se(l); |
aed2ebfe | 467 | |
38cd55b0 | 468 | STRV_FOREACH(s, l) |
aed2ebfe | 469 | assert_se(streq(*s, input_table_multiple[i++])); |
aed2ebfe DB |
470 | } |
471 | ||
4f7452a8 | 472 | TEST(strv_split_newlines_full) { |
f385c447 YW |
473 | const char str[] = |
474 | "ID_VENDOR=QEMU\n" | |
475 | "ID_VENDOR_ENC=QEMU\\x20\\x20\\x20\\x20\n" | |
476 | "ID_MODEL_ENC=QEMU\\x20HARDDISK\\x20\\x20\\x20\n" | |
477 | "\n\n\n"; | |
478 | _cleanup_strv_free_ char **l = NULL; | |
479 | ||
f385c447 YW |
480 | assert_se(strv_split_newlines_full(&l, str, 0) == 3); |
481 | assert_se(strv_equal(l, (char**) input_table_unescape)); | |
482 | ||
483 | l = strv_free(l); | |
484 | ||
485 | assert_se(strv_split_newlines_full(&l, str, EXTRACT_RETAIN_ESCAPE) == 3); | |
486 | assert_se(strv_equal(l, (char**) input_table_retain_escape)); | |
487 | } | |
488 | ||
4f7452a8 | 489 | TEST(strv_overlap) { |
539ad707 TA |
490 | const char * const input_table[] = { |
491 | "one", | |
492 | "two", | |
493 | "three", | |
494 | NULL | |
495 | }; | |
496 | const char * const input_table_overlap[] = { | |
497 | "two", | |
498 | NULL | |
499 | }; | |
500 | const char * const input_table_unique[] = { | |
501 | "four", | |
502 | "five", | |
503 | "six", | |
504 | NULL | |
505 | }; | |
506 | ||
7b68d618 DB |
507 | assert_se(strv_overlap((char **)input_table, (char**)input_table_overlap)); |
508 | assert_se(!strv_overlap((char **)input_table, (char**)input_table_unique)); | |
539ad707 TA |
509 | } |
510 | ||
4f7452a8 | 511 | TEST(strv_sort) { |
1e64bbc1 | 512 | const char* input_table[] = { |
539ad707 TA |
513 | "durian", |
514 | "apple", | |
515 | "citrus", | |
516 | "CAPITAL LETTERS FIRST", | |
517 | "banana", | |
518 | NULL | |
519 | }; | |
520 | ||
521 | strv_sort((char **)input_table); | |
522 | ||
7b68d618 DB |
523 | assert_se(streq(input_table[0], "CAPITAL LETTERS FIRST")); |
524 | assert_se(streq(input_table[1], "apple")); | |
525 | assert_se(streq(input_table[2], "banana")); | |
526 | assert_se(streq(input_table[3], "citrus")); | |
527 | assert_se(streq(input_table[4], "durian")); | |
3a7719d3 | 528 | } |
e3aa71c3 | 529 | |
4f7452a8 | 530 | TEST(strv_extend_strv_concat) { |
7d6884b6 | 531 | _cleanup_strv_free_ char **a = NULL, **b = NULL; |
343a8969 | 532 | |
bea1a013 LP |
533 | a = strv_new("without", "suffix"); |
534 | b = strv_new("with", "suffix"); | |
04045d84 DB |
535 | assert_se(a); |
536 | assert_se(b); | |
343a8969 | 537 | |
e3e45d4f | 538 | assert_se(strv_extend_strv_concat(&a, b, "_suffix") >= 0); |
343a8969 | 539 | |
e3e45d4f SP |
540 | assert_se(streq(a[0], "without")); |
541 | assert_se(streq(a[1], "suffix")); | |
542 | assert_se(streq(a[2], "with_suffix")); | |
543 | assert_se(streq(a[3], "suffix_suffix")); | |
343a8969 DB |
544 | } |
545 | ||
4f7452a8 | 546 | TEST(strv_extend_strv) { |
ea9b54f8 | 547 | _cleanup_strv_free_ char **a = NULL, **b = NULL, **n = NULL; |
a1022300 | 548 | |
bea1a013 LP |
549 | a = strv_new("abc", "def", "ghi"); |
550 | b = strv_new("jkl", "mno", "abc", "pqr"); | |
04045d84 DB |
551 | assert_se(a); |
552 | assert_se(b); | |
a1022300 | 553 | |
e287086b | 554 | assert_se(strv_extend_strv(&a, b, true) == 3); |
a1022300 | 555 | |
e3e45d4f SP |
556 | assert_se(streq(a[0], "abc")); |
557 | assert_se(streq(a[1], "def")); | |
558 | assert_se(streq(a[2], "ghi")); | |
559 | assert_se(streq(a[3], "jkl")); | |
560 | assert_se(streq(a[4], "mno")); | |
561 | assert_se(streq(a[5], "pqr")); | |
e3e45d4f | 562 | assert_se(strv_length(a) == 6); |
ea9b54f8 ZJS |
563 | |
564 | assert_se(strv_extend_strv(&n, b, false) >= 0); | |
565 | assert_se(streq(n[0], "jkl")); | |
566 | assert_se(streq(n[1], "mno")); | |
567 | assert_se(streq(n[2], "abc")); | |
568 | assert_se(streq(n[3], "pqr")); | |
569 | assert_se(strv_length(n) == 4); | |
a1022300 DB |
570 | } |
571 | ||
3ec3ae68 ZJS |
572 | TEST(strv_extend_with_size) { |
573 | _cleanup_strv_free_ char **a = NULL; | |
574 | size_t n = SIZE_MAX; | |
575 | ||
576 | a = strv_new("test", "test1"); | |
577 | assert_se(a); | |
578 | ||
579 | assert_se(strv_extend_with_size(&a, &n, "test2") >= 0); | |
580 | assert_se(n == 3); | |
581 | assert_se(strv_extend_with_size(&a, &n, "test3") >= 0); | |
582 | assert_se(n == 4); | |
583 | ||
584 | assert_se(streq(a[0], "test")); | |
585 | assert_se(streq(a[1], "test1")); | |
586 | assert_se(streq(a[2], "test2")); | |
587 | assert_se(streq(a[3], "test3")); | |
588 | assert_se(a[4] == NULL); | |
589 | } | |
590 | ||
4f7452a8 | 591 | TEST(strv_extend) { |
e3e45d4f | 592 | _cleanup_strv_free_ char **a = NULL, **b = NULL; |
40857008 | 593 | |
bea1a013 | 594 | a = strv_new("test", "test1"); |
04045d84 | 595 | assert_se(a); |
e3e45d4f SP |
596 | assert_se(strv_extend(&a, "test2") >= 0); |
597 | assert_se(strv_extend(&b, "test3") >= 0); | |
40857008 | 598 | |
e3e45d4f SP |
599 | assert_se(streq(a[0], "test")); |
600 | assert_se(streq(a[1], "test1")); | |
601 | assert_se(streq(a[2], "test2")); | |
602 | assert_se(streq(b[0], "test3")); | |
40857008 DB |
603 | } |
604 | ||
4f7452a8 | 605 | TEST(strv_extendf) { |
4a336a69 RC |
606 | _cleanup_strv_free_ char **a = NULL, **b = NULL; |
607 | ||
bea1a013 | 608 | a = strv_new("test", "test1"); |
4a336a69 RC |
609 | assert_se(a); |
610 | assert_se(strv_extendf(&a, "test2 %s %d %s", "foo", 128, "bar") >= 0); | |
611 | assert_se(strv_extendf(&b, "test3 %s %s %d", "bar", "foo", 128) >= 0); | |
612 | ||
613 | assert_se(streq(a[0], "test")); | |
614 | assert_se(streq(a[1], "test1")); | |
615 | assert_se(streq(a[2], "test2 foo 128 bar")); | |
616 | assert_se(streq(b[0], "test3 bar foo 128")); | |
617 | } | |
618 | ||
4f7452a8 | 619 | TEST(strv_foreach) { |
250a918d LP |
620 | _cleanup_strv_free_ char **a; |
621 | unsigned i = 0; | |
02f19706 | 622 | |
0bf2c5e5 | 623 | a = strv_new("one", "two", "three"); |
250a918d | 624 | assert_se(a); |
02f19706 | 625 | |
0bf2c5e5 | 626 | STRV_FOREACH(check, a) |
250a918d | 627 | assert_se(streq(*check, input_table_multiple[i++])); |
02f19706 DB |
628 | } |
629 | ||
4f7452a8 | 630 | TEST(strv_foreach_backwards) { |
250a918d LP |
631 | _cleanup_strv_free_ char **a; |
632 | unsigned i = 2; | |
02f19706 | 633 | |
bea1a013 | 634 | a = strv_new("one", "two", "three"); |
02f19706 | 635 | |
250a918d | 636 | assert_se(a); |
02f19706 | 637 | |
4a39c774 | 638 | STRV_FOREACH_BACKWARDS(check, a) |
5fba7bbf | 639 | assert_se(streq_ptr(*check, input_table_multiple[i--])); |
4a39c774 LP |
640 | |
641 | STRV_FOREACH_BACKWARDS(check, (char**) NULL) | |
04499a70 | 642 | assert_not_reached(); |
4a39c774 | 643 | |
9b01798b | 644 | STRV_FOREACH_BACKWARDS(check, STRV_MAKE_EMPTY) |
04499a70 | 645 | assert_not_reached(); |
9b01798b ZJS |
646 | |
647 | unsigned count = 0; | |
648 | STRV_FOREACH_BACKWARDS(check, STRV_MAKE("ONE")) | |
649 | count++; | |
650 | assert_se(count == 1); | |
02f19706 DB |
651 | } |
652 | ||
4f7452a8 | 653 | TEST(strv_foreach_pair) { |
4c325b2e | 654 | _cleanup_strv_free_ char **a = NULL; |
4c325b2e DB |
655 | |
656 | a = strv_new("pair_one", "pair_one", | |
657 | "pair_two", "pair_two", | |
bea1a013 | 658 | "pair_three", "pair_three"); |
0bf2c5e5 | 659 | STRV_FOREACH_PAIR(x, y, a) |
4c325b2e | 660 | assert_se(streq(*x, *y)); |
4c325b2e DB |
661 | } |
662 | ||
897e7561 | 663 | static void test_strv_from_stdarg_alloca_one(char **l, const char *first, ...) { |
250a918d LP |
664 | char **j; |
665 | unsigned i; | |
666 | ||
0bf2c5e5 ZJS |
667 | log_info("/* %s */", __func__); |
668 | ||
250a918d LP |
669 | j = strv_from_stdarg_alloca(first); |
670 | ||
671 | for (i = 0;; i++) { | |
672 | assert_se(streq_ptr(l[i], j[i])); | |
673 | ||
674 | if (!l[i]) | |
675 | break; | |
676 | } | |
677 | } | |
678 | ||
4f7452a8 | 679 | TEST(strv_from_stdarg_alloca) { |
897e7561 LP |
680 | test_strv_from_stdarg_alloca_one(STRV_MAKE("foo", "bar"), "foo", "bar", NULL); |
681 | test_strv_from_stdarg_alloca_one(STRV_MAKE("foo"), "foo", NULL); | |
682 | test_strv_from_stdarg_alloca_one(STRV_MAKE_EMPTY, NULL); | |
250a918d LP |
683 | } |
684 | ||
4f7452a8 | 685 | TEST(strv_insert) { |
6e888894 ZJS |
686 | _cleanup_strv_free_ char **a = NULL; |
687 | ||
688 | assert_se(strv_insert(&a, 0, strdup("first")) == 0); | |
689 | assert_se(streq(a[0], "first")); | |
690 | assert_se(!a[1]); | |
691 | ||
692 | assert_se(strv_insert(&a, 0, NULL) == 0); | |
693 | assert_se(streq(a[0], "first")); | |
694 | assert_se(!a[1]); | |
695 | ||
696 | assert_se(strv_insert(&a, 1, strdup("two")) == 0); | |
697 | assert_se(streq(a[0], "first")); | |
698 | assert_se(streq(a[1], "two")); | |
699 | assert_se(!a[2]); | |
700 | ||
701 | assert_se(strv_insert(&a, 4, strdup("tri")) == 0); | |
702 | assert_se(streq(a[0], "first")); | |
703 | assert_se(streq(a[1], "two")); | |
704 | assert_se(streq(a[2], "tri")); | |
705 | assert_se(!a[3]); | |
706 | ||
707 | assert_se(strv_insert(&a, 1, strdup("duo")) == 0); | |
708 | assert_se(streq(a[0], "first")); | |
709 | assert_se(streq(a[1], "duo")); | |
710 | assert_se(streq(a[2], "two")); | |
711 | assert_se(streq(a[3], "tri")); | |
712 | assert_se(!a[4]); | |
713 | } | |
714 | ||
4f7452a8 | 715 | TEST(strv_push_prepend) { |
7bd57a87 RC |
716 | _cleanup_strv_free_ char **a = NULL; |
717 | ||
42b97781 | 718 | assert_se(a = strv_new("foo", "bar", "three")); |
7bd57a87 RC |
719 | |
720 | assert_se(strv_push_prepend(&a, strdup("first")) >= 0); | |
721 | assert_se(streq(a[0], "first")); | |
722 | assert_se(streq(a[1], "foo")); | |
723 | assert_se(streq(a[2], "bar")); | |
724 | assert_se(streq(a[3], "three")); | |
725 | assert_se(!a[4]); | |
726 | ||
727 | assert_se(strv_consume_prepend(&a, strdup("first2")) >= 0); | |
728 | assert_se(streq(a[0], "first2")); | |
729 | assert_se(streq(a[1], "first")); | |
730 | assert_se(streq(a[2], "foo")); | |
731 | assert_se(streq(a[3], "bar")); | |
732 | assert_se(streq(a[4], "three")); | |
733 | assert_se(!a[5]); | |
734 | } | |
735 | ||
3ec3ae68 ZJS |
736 | TEST(strv_push_with_size) { |
737 | _cleanup_strv_free_ char **a = NULL; | |
738 | size_t n = 0; | |
739 | char *i, *j; | |
740 | ||
741 | assert_se(i = strdup("foo")); | |
742 | assert_se(strv_push_with_size(&a, &n, i) >= 0); | |
743 | assert_se(n == 1); | |
744 | ||
745 | assert_se(i = strdup("a")); | |
746 | assert_se(j = strdup("b")); | |
747 | assert_se(strv_push_with_size(&a, &n, i) >= 0); | |
748 | assert_se(n == 2); | |
749 | assert_se(strv_push_with_size(&a, &n, j) >= 0); | |
750 | assert_se(n == 3); | |
751 | ||
752 | assert_se(streq_ptr(a[0], "foo")); | |
753 | assert_se(streq_ptr(a[1], "a")); | |
754 | assert_se(streq_ptr(a[2], "b")); | |
755 | assert_se(streq_ptr(a[3], NULL)); | |
756 | ||
757 | assert_se(n = strv_length(a)); | |
758 | } | |
759 | ||
4f7452a8 | 760 | TEST(strv_push) { |
98940a3c LP |
761 | _cleanup_strv_free_ char **a = NULL; |
762 | char *i, *j; | |
763 | ||
764 | assert_se(i = strdup("foo")); | |
765 | assert_se(strv_push(&a, i) >= 0); | |
766 | ||
767 | assert_se(i = strdup("a")); | |
768 | assert_se(j = strdup("b")); | |
769 | assert_se(strv_push_pair(&a, i, j) >= 0); | |
770 | ||
771 | assert_se(streq_ptr(a[0], "foo")); | |
772 | assert_se(streq_ptr(a[1], "a")); | |
773 | assert_se(streq_ptr(a[2], "b")); | |
774 | assert_se(streq_ptr(a[3], NULL)); | |
775 | } | |
776 | ||
4f7452a8 | 777 | TEST(strv_compare) { |
e74aa253 RC |
778 | _cleanup_strv_free_ char **a = NULL; |
779 | _cleanup_strv_free_ char **b = NULL; | |
780 | _cleanup_strv_free_ char **c = NULL; | |
8b75798d | 781 | _cleanup_strv_free_ char **d = NULL; |
e74aa253 | 782 | |
bea1a013 | 783 | a = strv_new("one", "two", "three"); |
e74aa253 | 784 | assert_se(a); |
bea1a013 | 785 | b = strv_new("one", "two", "three"); |
8b75798d | 786 | assert_se(b); |
bea1a013 | 787 | c = strv_new("one", "two", "three", "four"); |
8b75798d YW |
788 | assert_se(c); |
789 | d = strv_new(NULL); | |
790 | assert_se(d); | |
e74aa253 | 791 | |
8b75798d YW |
792 | assert_se(strv_compare(a, a) == 0); |
793 | assert_se(strv_compare(a, b) == 0); | |
794 | assert_se(strv_compare(d, d) == 0); | |
795 | assert_se(strv_compare(d, NULL) == 0); | |
796 | assert_se(strv_compare(NULL, NULL) == 0); | |
e74aa253 | 797 | |
8b75798d YW |
798 | assert_se(strv_compare(a, c) < 0); |
799 | assert_se(strv_compare(b, c) < 0); | |
800 | assert_se(strv_compare(b, d) == 1); | |
801 | assert_se(strv_compare(b, NULL) == 1); | |
e74aa253 RC |
802 | } |
803 | ||
4f7452a8 | 804 | TEST(strv_is_uniq) { |
e1dd6790 LP |
805 | _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL, **d = NULL; |
806 | ||
bea1a013 | 807 | a = strv_new(NULL); |
e1dd6790 LP |
808 | assert_se(a); |
809 | assert_se(strv_is_uniq(a)); | |
810 | ||
bea1a013 | 811 | b = strv_new("foo"); |
e1dd6790 LP |
812 | assert_se(b); |
813 | assert_se(strv_is_uniq(b)); | |
814 | ||
bea1a013 | 815 | c = strv_new("foo", "bar"); |
e1dd6790 LP |
816 | assert_se(c); |
817 | assert_se(strv_is_uniq(c)); | |
818 | ||
bea1a013 | 819 | d = strv_new("foo", "bar", "waldo", "bar", "piep"); |
e1dd6790 LP |
820 | assert_se(d); |
821 | assert_se(!strv_is_uniq(d)); | |
822 | } | |
823 | ||
4f7452a8 | 824 | TEST(strv_reverse) { |
e1dd6790 LP |
825 | _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL, **d = NULL; |
826 | ||
bea1a013 | 827 | a = strv_new(NULL); |
e1dd6790 LP |
828 | assert_se(a); |
829 | ||
830 | strv_reverse(a); | |
831 | assert_se(strv_isempty(a)); | |
832 | ||
bea1a013 | 833 | b = strv_new("foo"); |
e1dd6790 LP |
834 | assert_se(b); |
835 | strv_reverse(b); | |
836 | assert_se(streq_ptr(b[0], "foo")); | |
837 | assert_se(streq_ptr(b[1], NULL)); | |
838 | ||
bea1a013 | 839 | c = strv_new("foo", "bar"); |
e1dd6790 LP |
840 | assert_se(c); |
841 | strv_reverse(c); | |
842 | assert_se(streq_ptr(c[0], "bar")); | |
843 | assert_se(streq_ptr(c[1], "foo")); | |
844 | assert_se(streq_ptr(c[2], NULL)); | |
845 | ||
bea1a013 | 846 | d = strv_new("foo", "bar", "waldo"); |
e1dd6790 LP |
847 | assert_se(d); |
848 | strv_reverse(d); | |
849 | assert_se(streq_ptr(d[0], "waldo")); | |
850 | assert_se(streq_ptr(d[1], "bar")); | |
851 | assert_se(streq_ptr(d[2], "foo")); | |
852 | assert_se(streq_ptr(d[3], NULL)); | |
853 | } | |
854 | ||
4f7452a8 | 855 | TEST(strv_shell_escape) { |
04c14b25 RM |
856 | _cleanup_strv_free_ char **v = NULL; |
857 | ||
bea1a013 | 858 | v = strv_new("foo:bar", "bar,baz", "wal\\do"); |
04c14b25 RM |
859 | assert_se(v); |
860 | assert_se(strv_shell_escape(v, ",:")); | |
861 | assert_se(streq_ptr(v[0], "foo\\:bar")); | |
862 | assert_se(streq_ptr(v[1], "bar\\,baz")); | |
863 | assert_se(streq_ptr(v[2], "wal\\\\do")); | |
864 | assert_se(streq_ptr(v[3], NULL)); | |
865 | } | |
866 | ||
e3ead6bb LP |
867 | static void test_strv_skip_one(char **a, size_t n, char **b) { |
868 | a = strv_skip(a, n); | |
869 | assert_se(strv_equal(a, b)); | |
870 | } | |
871 | ||
4f7452a8 | 872 | TEST(strv_skip) { |
e3ead6bb LP |
873 | test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 0, STRV_MAKE("foo", "bar", "baz")); |
874 | test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 1, STRV_MAKE("bar", "baz")); | |
875 | test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 2, STRV_MAKE("baz")); | |
876 | test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 3, STRV_MAKE(NULL)); | |
877 | test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 4, STRV_MAKE(NULL)); | |
878 | test_strv_skip_one(STRV_MAKE("foo", "bar", "baz"), 55, STRV_MAKE(NULL)); | |
879 | ||
880 | test_strv_skip_one(STRV_MAKE("quux"), 0, STRV_MAKE("quux")); | |
881 | test_strv_skip_one(STRV_MAKE("quux"), 1, STRV_MAKE(NULL)); | |
882 | test_strv_skip_one(STRV_MAKE("quux"), 55, STRV_MAKE(NULL)); | |
883 | ||
884 | test_strv_skip_one(STRV_MAKE(NULL), 0, STRV_MAKE(NULL)); | |
885 | test_strv_skip_one(STRV_MAKE(NULL), 1, STRV_MAKE(NULL)); | |
886 | test_strv_skip_one(STRV_MAKE(NULL), 55, STRV_MAKE(NULL)); | |
887 | } | |
888 | ||
4f7452a8 | 889 | TEST(strv_extend_n) { |
8dd4c05b LP |
890 | _cleanup_strv_free_ char **v = NULL; |
891 | ||
bea1a013 | 892 | v = strv_new("foo", "bar"); |
8dd4c05b LP |
893 | assert_se(v); |
894 | ||
895 | assert_se(strv_extend_n(&v, "waldo", 3) >= 0); | |
896 | assert_se(strv_extend_n(&v, "piep", 2) >= 0); | |
897 | ||
898 | assert_se(streq(v[0], "foo")); | |
899 | assert_se(streq(v[1], "bar")); | |
900 | assert_se(streq(v[2], "waldo")); | |
901 | assert_se(streq(v[3], "waldo")); | |
902 | assert_se(streq(v[4], "waldo")); | |
903 | assert_se(streq(v[5], "piep")); | |
904 | assert_se(streq(v[6], "piep")); | |
905 | assert_se(v[7] == NULL); | |
906 | ||
907 | v = strv_free(v); | |
908 | ||
909 | assert_se(strv_extend_n(&v, "foo", 1) >= 0); | |
910 | assert_se(strv_extend_n(&v, "bar", 0) >= 0); | |
911 | ||
912 | assert_se(streq(v[0], "foo")); | |
913 | assert_se(v[1] == NULL); | |
914 | } | |
915 | ||
4f7452a8 | 916 | TEST(foreach_string) { |
ebde5cb2 RC |
917 | const char * const t[] = { |
918 | "foo", | |
919 | "bar", | |
920 | "waldo", | |
921 | NULL | |
922 | }; | |
ebde5cb2 | 923 | |
5980d463 | 924 | unsigned i = 0; |
ebde5cb2 RC |
925 | FOREACH_STRING(x, "foo", "bar", "waldo") |
926 | assert_se(streq_ptr(t[i++], x)); | |
ebde5cb2 RC |
927 | assert_se(i == 3); |
928 | ||
929 | FOREACH_STRING(x, "zzz") | |
930 | assert_se(streq(x, "zzz")); | |
931 | } | |
932 | ||
4f7452a8 | 933 | TEST(strv_fnmatch) { |
57681e84 | 934 | _cleanup_strv_free_ char **v = NULL; |
0ef84b80 | 935 | size_t pos; |
2027927b | 936 | |
191a3f16 | 937 | assert_se(!strv_fnmatch(STRV_MAKE_EMPTY, "a")); |
2027927b | 938 | |
0ef84b80 ZJS |
939 | v = strv_new("xxx", "*\\*", "yyy"); |
940 | assert_se(!strv_fnmatch_full(v, "\\", 0, NULL)); | |
941 | assert_se(strv_fnmatch_full(v, "\\", FNM_NOESCAPE, &pos)); | |
f21b863e | 942 | assert_se(pos == 1); |
2027927b EV |
943 | } |
944 | ||
6658f7c7 DDM |
945 | TEST(strv_extend_join) { |
946 | _cleanup_strv_free_ char **v = NULL; | |
947 | ||
948 | assert_se(strv_extend_assignment(&v, "MESSAGE", "ABC") >= 0); | |
949 | assert_se(strv_extend_assignment(&v, "ABC", "QER") >= 0); | |
950 | assert_se(strv_extend_assignment(&v, "MISSING", NULL) >= 0); | |
951 | ||
952 | assert_se(strv_length(v) == 2); | |
953 | assert_se(streq(v[0], "MESSAGE=ABC")); | |
954 | assert_se(streq(v[1], "ABC=QER")); | |
955 | } | |
956 | ||
4ea517a6 LP |
957 | TEST(strv_copy_n) { |
958 | char **x = STRV_MAKE("a", "b", "c", "d", "e"); | |
959 | _cleanup_strv_free_ char **l = NULL; | |
960 | ||
961 | l = strv_copy_n(x, 0); | |
962 | assert_se(strv_equal(l, NULL)); | |
963 | strv_free(l); | |
964 | ||
965 | l = strv_copy_n(x, 0); | |
966 | assert_se(strv_equal(l, (char**) { NULL })); | |
967 | strv_free(l); | |
968 | ||
969 | l = strv_copy_n(x, 1); | |
970 | assert_se(strv_equal(l, STRV_MAKE("a"))); | |
971 | strv_free(l); | |
972 | ||
973 | l = strv_copy_n(x, 2); | |
974 | assert_se(strv_equal(l, STRV_MAKE("a", "b"))); | |
975 | strv_free(l); | |
976 | ||
977 | l = strv_copy_n(x, 3); | |
978 | assert_se(strv_equal(l, STRV_MAKE("a", "b", "c"))); | |
979 | strv_free(l); | |
980 | ||
981 | l = strv_copy_n(x, 4); | |
982 | assert_se(strv_equal(l, STRV_MAKE("a", "b", "c", "d"))); | |
983 | strv_free(l); | |
984 | ||
985 | l = strv_copy_n(x, 5); | |
986 | assert_se(strv_equal(l, STRV_MAKE("a", "b", "c", "d", "e"))); | |
987 | strv_free(l); | |
988 | ||
989 | l = strv_copy_n(x, 6); | |
990 | assert_se(strv_equal(l, STRV_MAKE("a", "b", "c", "d", "e"))); | |
991 | strv_free(l); | |
992 | ||
993 | l = strv_copy_n(x, SIZE_MAX); | |
994 | assert_se(strv_equal(l, STRV_MAKE("a", "b", "c", "d", "e"))); | |
995 | } | |
996 | ||
2ed74695 LB |
997 | TEST(strv_find_first_field) { |
998 | char **haystack = STRV_MAKE("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"); | |
999 | ||
1000 | assert_se(strv_find_first_field(NULL, NULL) == NULL); | |
1001 | assert_se(strv_find_first_field(NULL, haystack) == NULL); | |
1002 | assert_se(strv_find_first_field(STRV_MAKE("k", "l", "m", "d", "b"), NULL) == NULL); | |
1003 | assert_se(strv_find_first_field(STRV_MAKE("k", "l", "m", "d", "b"), haystack) == NULL); | |
1004 | assert_se(streq_ptr(strv_find_first_field(STRV_MAKE("k", "l", "m", "d", "a", "c"), haystack), "b")); | |
1005 | assert_se(streq_ptr(strv_find_first_field(STRV_MAKE("k", "l", "m", "d", "c", "a"), haystack), "d")); | |
1006 | assert_se(streq_ptr(strv_find_first_field(STRV_MAKE("i", "k", "l", "m", "d", "c", "a", "b"), haystack), "j")); | |
1007 | } | |
1008 | ||
2e6f012b MY |
1009 | TEST(endswith_strv) { |
1010 | assert_se(streq_ptr(endswith_strv("waldo", STRV_MAKE("xxx", "yyy", "ldo", "zzz")), "ldo")); | |
1011 | assert_se(streq_ptr(endswith_strv("waldo", STRV_MAKE("xxx", "yyy", "zzz")), NULL)); | |
1012 | assert_se(streq_ptr(endswith_strv("waldo", STRV_MAKE("waldo")), "waldo")); | |
1013 | assert_se(streq_ptr(endswith_strv("waldo", STRV_MAKE("w", "o", "ldo")), "o")); | |
1014 | assert_se(streq_ptr(endswith_strv("waldo", STRV_MAKE("knurz", "", "waldo")), "")); | |
cb599f88 LP |
1015 | } |
1016 | ||
4f7452a8 | 1017 | DEFINE_TEST_MAIN(LOG_INFO); |