]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-string-util.c
Add string_contains_word_strv()
[thirdparty/systemd.git] / src / test / test-string-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "alloc-util.h"
4 #include "locale-util.h"
5 #include "macro.h"
6 #include "string-util.h"
7 #include "strv.h"
8 #include "tests.h"
9 #include "utf8.h"
10 #include "util.h"
11
12 static void test_string_erase(void) {
13 log_info("/* %s */", __func__);
14
15 char *x;
16 x = strdupa("");
17 assert_se(streq(string_erase(x), ""));
18
19 x = strdupa("1");
20 assert_se(streq(string_erase(x), ""));
21
22 x = strdupa("123456789");
23 assert_se(streq(string_erase(x), ""));
24
25 assert_se(x[1] == '\0');
26 assert_se(x[2] == '\0');
27 assert_se(x[3] == '\0');
28 assert_se(x[4] == '\0');
29 assert_se(x[5] == '\0');
30 assert_se(x[6] == '\0');
31 assert_se(x[7] == '\0');
32 assert_se(x[8] == '\0');
33 assert_se(x[9] == '\0');
34 }
35
36 static void test_free_and_strndup_one(char **t, const char *src, size_t l, const char *expected, bool change) {
37 log_debug("%s: \"%s\", \"%s\", %zd (expect \"%s\", %s)",
38 __func__, strnull(*t), strnull(src), l, strnull(expected), yes_no(change));
39
40 int r = free_and_strndup(t, src, l);
41 assert_se(streq_ptr(*t, expected));
42 assert_se(r == change); /* check that change occurs only when necessary */
43 }
44
45 static void test_free_and_strndup(void) {
46 log_info("/* %s */", __func__);
47
48 static const struct test_case {
49 const char *src;
50 size_t len;
51 const char *expected;
52 } cases[] = {
53 {"abc", 0, ""},
54 {"abc", 0, ""},
55 {"abc", 1, "a"},
56 {"abc", 2, "ab"},
57 {"abc", 3, "abc"},
58 {"abc", 4, "abc"},
59 {"abc", 5, "abc"},
60 {"abc", 5, "abc"},
61 {"abc", 4, "abc"},
62 {"abc", 3, "abc"},
63 {"abc", 2, "ab"},
64 {"abc", 1, "a"},
65 {"abc", 0, ""},
66
67 {"", 0, ""},
68 {"", 1, ""},
69 {"", 2, ""},
70 {"", 0, ""},
71 {"", 1, ""},
72 {"", 2, ""},
73 {"", 2, ""},
74 {"", 1, ""},
75 {"", 0, ""},
76
77 {NULL, 0, NULL},
78
79 {"foo", 3, "foo"},
80 {"foobar", 6, "foobar"},
81 };
82
83 _cleanup_free_ char *t = NULL;
84 const char *prev_expected = t;
85
86 for (unsigned i = 0; i < ELEMENTSOF(cases); i++) {
87 test_free_and_strndup_one(&t,
88 cases[i].src, cases[i].len, cases[i].expected,
89 !streq_ptr(cases[i].expected, prev_expected));
90 prev_expected = t;
91 }
92 }
93
94 static void test_ascii_strcasecmp_n(void) {
95 log_info("/* %s */", __func__);
96
97 assert_se(ascii_strcasecmp_n("", "", 0) == 0);
98 assert_se(ascii_strcasecmp_n("", "", 1) == 0);
99 assert_se(ascii_strcasecmp_n("", "a", 1) < 0);
100 assert_se(ascii_strcasecmp_n("", "a", 2) < 0);
101 assert_se(ascii_strcasecmp_n("a", "", 1) > 0);
102 assert_se(ascii_strcasecmp_n("a", "", 2) > 0);
103 assert_se(ascii_strcasecmp_n("a", "a", 1) == 0);
104 assert_se(ascii_strcasecmp_n("a", "a", 2) == 0);
105 assert_se(ascii_strcasecmp_n("a", "b", 1) < 0);
106 assert_se(ascii_strcasecmp_n("a", "b", 2) < 0);
107 assert_se(ascii_strcasecmp_n("b", "a", 1) > 0);
108 assert_se(ascii_strcasecmp_n("b", "a", 2) > 0);
109 assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxYxxxx", 9) == 0);
110 assert_se(ascii_strcasecmp_n("xxxxxxxxx", "xxxxyxxxx", 9) < 0);
111 assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxyxxxx", 9) < 0);
112 assert_se(ascii_strcasecmp_n("xxxxxxxxx", "xxxxYxxxx", 9) < 0);
113 assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxYxxxx", 9) < 0);
114
115 assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxYxxxx", 9) == 0);
116 assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxxxxxx", 9) > 0);
117 assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxXxxxx", 9) > 0);
118 assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxxxxxx", 9) > 0);
119 assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxXxxxx", 9) > 0);
120 }
121
122 static void test_ascii_strcasecmp_nn(void) {
123 log_info("/* %s */", __func__);
124
125 assert_se(ascii_strcasecmp_nn("", 0, "", 0) == 0);
126 assert_se(ascii_strcasecmp_nn("", 0, "", 1) < 0);
127 assert_se(ascii_strcasecmp_nn("", 1, "", 0) > 0);
128 assert_se(ascii_strcasecmp_nn("", 1, "", 1) == 0);
129
130 assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaAa", 4) == 0);
131 assert_se(ascii_strcasecmp_nn("aaa", 3, "aaAa", 4) < 0);
132 assert_se(ascii_strcasecmp_nn("aaa", 4, "aaAa", 4) < 0);
133 assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaA", 3) > 0);
134 assert_se(ascii_strcasecmp_nn("aaaa", 4, "AAA", 4) > 0);
135
136 assert_se(ascii_strcasecmp_nn("aaaa", 4, "bbbb", 4) < 0);
137 assert_se(ascii_strcasecmp_nn("aaAA", 4, "BBbb", 4) < 0);
138 assert_se(ascii_strcasecmp_nn("BBbb", 4, "aaaa", 4) > 0);
139 }
140
141 static void test_cellescape(void) {
142 char buf[40];
143
144 log_info("/* %s */", __func__);
145
146 assert_se(streq(cellescape(buf, 1, ""), ""));
147 assert_se(streq(cellescape(buf, 1, "1"), ""));
148 assert_se(streq(cellescape(buf, 1, "12"), ""));
149
150 assert_se(streq(cellescape(buf, 2, ""), ""));
151 assert_se(streq(cellescape(buf, 2, "1"), "1"));
152 assert_se(streq(cellescape(buf, 2, "12"), "."));
153 assert_se(streq(cellescape(buf, 2, "123"), "."));
154
155 assert_se(streq(cellescape(buf, 3, ""), ""));
156 assert_se(streq(cellescape(buf, 3, "1"), "1"));
157 assert_se(streq(cellescape(buf, 3, "12"), "12"));
158 assert_se(streq(cellescape(buf, 3, "123"), ".."));
159 assert_se(streq(cellescape(buf, 3, "1234"), ".."));
160
161 assert_se(streq(cellescape(buf, 4, ""), ""));
162 assert_se(streq(cellescape(buf, 4, "1"), "1"));
163 assert_se(streq(cellescape(buf, 4, "12"), "12"));
164 assert_se(streq(cellescape(buf, 4, "123"), "123"));
165 assert_se(streq(cellescape(buf, 4, "1234"), is_locale_utf8() ? "…" : "..."));
166 assert_se(streq(cellescape(buf, 4, "12345"), is_locale_utf8() ? "…" : "..."));
167
168 assert_se(streq(cellescape(buf, 5, ""), ""));
169 assert_se(streq(cellescape(buf, 5, "1"), "1"));
170 assert_se(streq(cellescape(buf, 5, "12"), "12"));
171 assert_se(streq(cellescape(buf, 5, "123"), "123"));
172 assert_se(streq(cellescape(buf, 5, "1234"), "1234"));
173 assert_se(streq(cellescape(buf, 5, "12345"), is_locale_utf8() ? "1…" : "1..."));
174 assert_se(streq(cellescape(buf, 5, "123456"), is_locale_utf8() ? "1…" : "1..."));
175
176 assert_se(streq(cellescape(buf, 1, "\020"), ""));
177 assert_se(streq(cellescape(buf, 2, "\020"), "."));
178 assert_se(streq(cellescape(buf, 3, "\020"), ".."));
179 assert_se(streq(cellescape(buf, 4, "\020"), "…"));
180 assert_se(streq(cellescape(buf, 5, "\020"), "\\020"));
181
182 assert_se(streq(cellescape(buf, 5, "1234\020"), "1…"));
183 assert_se(streq(cellescape(buf, 6, "1234\020"), "12…"));
184 assert_se(streq(cellescape(buf, 7, "1234\020"), "123…"));
185 assert_se(streq(cellescape(buf, 8, "1234\020"), "1234…"));
186 assert_se(streq(cellescape(buf, 9, "1234\020"), "1234\\020"));
187
188 assert_se(streq(cellescape(buf, 1, "\t\n"), ""));
189 assert_se(streq(cellescape(buf, 2, "\t\n"), "."));
190 assert_se(streq(cellescape(buf, 3, "\t\n"), ".."));
191 assert_se(streq(cellescape(buf, 4, "\t\n"), "…"));
192 assert_se(streq(cellescape(buf, 5, "\t\n"), "\\t\\n"));
193
194 assert_se(streq(cellescape(buf, 5, "1234\t\n"), "1…"));
195 assert_se(streq(cellescape(buf, 6, "1234\t\n"), "12…"));
196 assert_se(streq(cellescape(buf, 7, "1234\t\n"), "123…"));
197 assert_se(streq(cellescape(buf, 8, "1234\t\n"), "1234…"));
198 assert_se(streq(cellescape(buf, 9, "1234\t\n"), "1234\\t\\n"));
199
200 assert_se(streq(cellescape(buf, 4, "x\t\020\n"), "…"));
201 assert_se(streq(cellescape(buf, 5, "x\t\020\n"), "x…"));
202 assert_se(streq(cellescape(buf, 6, "x\t\020\n"), "x…"));
203 assert_se(streq(cellescape(buf, 7, "x\t\020\n"), "x\\t…"));
204 assert_se(streq(cellescape(buf, 8, "x\t\020\n"), "x\\t…"));
205 assert_se(streq(cellescape(buf, 9, "x\t\020\n"), "x\\t…"));
206 assert_se(streq(cellescape(buf, 10, "x\t\020\n"), "x\\t\\020\\n"));
207
208 assert_se(streq(cellescape(buf, 6, "1\011"), "1\\t"));
209 assert_se(streq(cellescape(buf, 6, "1\020"), "1\\020"));
210 assert_se(streq(cellescape(buf, 6, "1\020x"), is_locale_utf8() ? "1…" : "1..."));
211
212 assert_se(streq(cellescape(buf, 40, "1\020"), "1\\020"));
213 assert_se(streq(cellescape(buf, 40, "1\020x"), "1\\020x"));
214
215 assert_se(streq(cellescape(buf, 40, "\a\b\f\n\r\t\v\\\"'"), "\\a\\b\\f\\n\\r\\t\\v\\\\\\\"\\'"));
216 assert_se(streq(cellescape(buf, 6, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a…" : "\\a..."));
217 assert_se(streq(cellescape(buf, 7, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a…" : "\\a..."));
218 assert_se(streq(cellescape(buf, 8, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a\\b…" : "\\a\\b..."));
219
220 assert_se(streq(cellescape(buf, sizeof buf, "1\020"), "1\\020"));
221 assert_se(streq(cellescape(buf, sizeof buf, "1\020x"), "1\\020x"));
222 }
223
224 static void test_streq_ptr(void) {
225 log_info("/* %s */", __func__);
226
227 assert_se(streq_ptr(NULL, NULL));
228 assert_se(!streq_ptr("abc", "cdef"));
229 }
230
231 static void test_strstrip(void) {
232 log_info("/* %s */", __func__);
233
234 char *ret, input[] = " hello, waldo. ";
235
236 ret = strstrip(input);
237 assert_se(streq(ret, "hello, waldo."));
238 }
239
240 static void test_strextend(void) {
241 log_info("/* %s */", __func__);
242
243 _cleanup_free_ char *str = NULL;
244
245 assert_se(strextend(&str, NULL));
246 assert_se(streq_ptr(str, ""));
247 assert_se(strextend(&str, "", "0", "", "", "123", NULL));
248 assert_se(streq_ptr(str, "0123"));
249 assert_se(strextend(&str, "456", "78", "9", NULL));
250 assert_se(streq_ptr(str, "0123456789"));
251 }
252
253 static void test_strextend_with_separator(void) {
254 log_info("/* %s */", __func__);
255
256 _cleanup_free_ char *str = NULL;
257
258 assert_se(strextend_with_separator(&str, NULL, NULL));
259 assert_se(streq_ptr(str, ""));
260 str = mfree(str);
261
262 assert_se(strextend_with_separator(&str, "...", NULL));
263 assert_se(streq_ptr(str, ""));
264 assert_se(strextend_with_separator(&str, "...", NULL));
265 assert_se(streq_ptr(str, ""));
266 str = mfree(str);
267
268 assert_se(strextend_with_separator(&str, "xyz", "a", "bb", "ccc", NULL));
269 assert_se(streq_ptr(str, "axyzbbxyzccc"));
270 str = mfree(str);
271
272 assert_se(strextend_with_separator(&str, ",", "start", "", "1", "234", NULL));
273 assert_se(streq_ptr(str, "start,,1,234"));
274 assert_se(strextend_with_separator(&str, ";", "more", "5", "678", NULL));
275 assert_se(streq_ptr(str, "start,,1,234;more;5;678"));
276 }
277
278 static void test_strrep(void) {
279 log_info("/* %s */", __func__);
280
281 _cleanup_free_ char *one, *three, *zero;
282 one = strrep("waldo", 1);
283 three = strrep("waldo", 3);
284 zero = strrep("waldo", 0);
285
286 assert_se(streq(one, "waldo"));
287 assert_se(streq(three, "waldowaldowaldo"));
288 assert_se(streq(zero, ""));
289 }
290
291 static void test_string_has_cc(void) {
292 assert_se(string_has_cc("abc\1", NULL));
293 assert_se(string_has_cc("abc\x7f", NULL));
294 assert_se(string_has_cc("abc\x7f", NULL));
295 assert_se(string_has_cc("abc\t\x7f", "\t"));
296 assert_se(string_has_cc("abc\t\x7f", "\t"));
297 assert_se(string_has_cc("\x7f", "\t"));
298 assert_se(string_has_cc("\x7f", "\t\a"));
299
300 assert_se(!string_has_cc("abc\t\t", "\t"));
301 assert_se(!string_has_cc("abc\t\t\a", "\t\a"));
302 assert_se(!string_has_cc("a\ab\tc", "\t\a"));
303 }
304
305 static void test_ascii_strlower(void) {
306 log_info("/* %s */", __func__);
307
308 char a[] = "AabBcC Jk Ii Od LKJJJ kkd LK";
309 assert_se(streq(ascii_strlower(a), "aabbcc jk ii od lkjjj kkd lk"));
310 }
311
312 static void test_strshorten(void) {
313 log_info("/* %s */", __func__);
314
315 char s[] = "foobar";
316
317 assert_se(strlen(strshorten(s, 6)) == 6);
318 assert_se(strlen(strshorten(s, 12)) == 6);
319 assert_se(strlen(strshorten(s, 2)) == 2);
320 assert_se(strlen(strshorten(s, 0)) == 0);
321 }
322
323 static void test_strjoina(void) {
324 log_info("/* %s */", __func__);
325
326 char *actual;
327
328 actual = strjoina("", "foo", "bar");
329 assert_se(streq(actual, "foobar"));
330
331 actual = strjoina("foo", "bar", "baz");
332 assert_se(streq(actual, "foobarbaz"));
333
334 actual = strjoina("foo", "", "bar", "baz");
335 assert_se(streq(actual, "foobarbaz"));
336
337 actual = strjoina("foo");
338 assert_se(streq(actual, "foo"));
339
340 actual = strjoina(NULL);
341 assert_se(streq(actual, ""));
342
343 actual = strjoina(NULL, "foo");
344 assert_se(streq(actual, ""));
345
346 actual = strjoina("foo", NULL, "bar");
347 assert_se(streq(actual, "foo"));
348 }
349
350 static void test_strjoin(void) {
351 char *actual;
352
353 actual = strjoin("", "foo", "bar");
354 assert_se(streq(actual, "foobar"));
355 mfree(actual);
356
357 actual = strjoin("foo", "bar", "baz");
358 assert_se(streq(actual, "foobarbaz"));
359 mfree(actual);
360
361 actual = strjoin("foo", "", "bar", "baz");
362 assert_se(streq(actual, "foobarbaz"));
363 mfree(actual);
364
365 actual = strjoin("foo", NULL);
366 assert_se(streq(actual, "foo"));
367 mfree(actual);
368
369 actual = strjoin(NULL, NULL);
370 assert_se(streq(actual, ""));
371 mfree(actual);
372
373 actual = strjoin(NULL, "foo");
374 assert_se(streq(actual, ""));
375 mfree(actual);
376
377 actual = strjoin("foo", NULL, "bar");
378 assert_se(streq(actual, "foo"));
379 mfree(actual);
380 }
381
382 static void test_strcmp_ptr(void) {
383 log_info("/* %s */", __func__);
384
385 assert_se(strcmp_ptr(NULL, NULL) == 0);
386 assert_se(strcmp_ptr("", NULL) > 0);
387 assert_se(strcmp_ptr("foo", NULL) > 0);
388 assert_se(strcmp_ptr(NULL, "") < 0);
389 assert_se(strcmp_ptr(NULL, "bar") < 0);
390 assert_se(strcmp_ptr("foo", "bar") > 0);
391 assert_se(strcmp_ptr("bar", "baz") < 0);
392 assert_se(strcmp_ptr("foo", "foo") == 0);
393 assert_se(strcmp_ptr("", "") == 0);
394 }
395
396 static void test_foreach_word(void) {
397 log_info("/* %s */", __func__);
398
399 const char *word, *state;
400 size_t l;
401 int i = 0;
402 const char test[] = "test abc d\te f ";
403 const char * const expected[] = {
404 "test",
405 "abc",
406 "d",
407 "e",
408 "f",
409 "",
410 NULL
411 };
412
413 FOREACH_WORD(word, l, test, state)
414 assert_se(strneq(expected[i++], word, l));
415 }
416
417 static void check(const char *test, char** expected, bool trailing) {
418 int i = 0, r;
419
420 printf("<<<%s>>>\n", test);
421 for (;;) {
422 _cleanup_free_ char *word = NULL;
423
424 r = extract_first_word(&test, &word, NULL, EXTRACT_UNQUOTE);
425 if (r == 0) {
426 assert_se(!trailing);
427 break;
428 } else if (r < 0) {
429 assert_se(trailing);
430 break;
431 }
432
433 assert_se(streq(word, expected[i++]));
434 printf("<%s>\n", word);
435 }
436 assert_se(expected[i] == NULL);
437 }
438
439 static void test_foreach_word_quoted(void) {
440 log_info("/* %s */", __func__);
441
442 check("test a b c 'd' e '' '' hhh '' '' \"a b c\"",
443 STRV_MAKE("test",
444 "a",
445 "b",
446 "c",
447 "d",
448 "e",
449 "",
450 "",
451 "hhh",
452 "",
453 "",
454 "a b c"),
455 false);
456
457 check("test \"xxx",
458 STRV_MAKE("test"),
459 true);
460
461 check("test\\",
462 STRV_MAKE_EMPTY,
463 true);
464 }
465
466 static void test_endswith(void) {
467 log_info("/* %s */", __func__);
468
469 assert_se(endswith("foobar", "bar"));
470 assert_se(endswith("foobar", ""));
471 assert_se(endswith("foobar", "foobar"));
472 assert_se(endswith("", ""));
473
474 assert_se(!endswith("foobar", "foo"));
475 assert_se(!endswith("foobar", "foobarfoofoo"));
476 }
477
478 static void test_endswith_no_case(void) {
479 log_info("/* %s */", __func__);
480
481 assert_se(endswith_no_case("fooBAR", "bar"));
482 assert_se(endswith_no_case("foobar", ""));
483 assert_se(endswith_no_case("foobar", "FOOBAR"));
484 assert_se(endswith_no_case("", ""));
485
486 assert_se(!endswith_no_case("foobar", "FOO"));
487 assert_se(!endswith_no_case("foobar", "FOOBARFOOFOO"));
488 }
489
490 static void test_delete_chars(void) {
491 log_info("/* %s */", __func__);
492
493 char *s, input[] = " hello, waldo. abc";
494
495 s = delete_chars(input, WHITESPACE);
496 assert_se(streq(s, "hello,waldo.abc"));
497 assert_se(s == input);
498 }
499
500 static void test_delete_trailing_chars(void) {
501 log_info("/* %s */", __func__);
502
503 char *s,
504 input1[] = " \n \r k \n \r ",
505 input2[] = "kkkkthiskkkiskkkaktestkkk",
506 input3[] = "abcdef";
507
508 s = delete_trailing_chars(input1, WHITESPACE);
509 assert_se(streq(s, " \n \r k"));
510 assert_se(s == input1);
511
512 s = delete_trailing_chars(input2, "kt");
513 assert_se(streq(s, "kkkkthiskkkiskkkaktes"));
514 assert_se(s == input2);
515
516 s = delete_trailing_chars(input3, WHITESPACE);
517 assert_se(streq(s, "abcdef"));
518 assert_se(s == input3);
519
520 s = delete_trailing_chars(input3, "fe");
521 assert_se(streq(s, "abcd"));
522 assert_se(s == input3);
523 }
524
525 static void test_delete_trailing_slashes(void) {
526 log_info("/* %s */", __func__);
527
528 char s1[] = "foobar//",
529 s2[] = "foobar/",
530 s3[] = "foobar",
531 s4[] = "";
532
533 assert_se(streq(delete_trailing_chars(s1, "_"), "foobar//"));
534 assert_se(streq(delete_trailing_chars(s1, "/"), "foobar"));
535 assert_se(streq(delete_trailing_chars(s2, "/"), "foobar"));
536 assert_se(streq(delete_trailing_chars(s3, "/"), "foobar"));
537 assert_se(streq(delete_trailing_chars(s4, "/"), ""));
538 }
539
540 static void test_skip_leading_chars(void) {
541 log_info("/* %s */", __func__);
542
543 char input1[] = " \n \r k \n \r ",
544 input2[] = "kkkkthiskkkiskkkaktestkkk",
545 input3[] = "abcdef";
546
547 assert_se(streq(skip_leading_chars(input1, WHITESPACE), "k \n \r "));
548 assert_se(streq(skip_leading_chars(input2, "k"), "thiskkkiskkkaktestkkk"));
549 assert_se(streq(skip_leading_chars(input2, "tk"), "hiskkkiskkkaktestkkk"));
550 assert_se(streq(skip_leading_chars(input3, WHITESPACE), "abcdef"));
551 assert_se(streq(skip_leading_chars(input3, "bcaef"), "def"));
552 }
553
554 static void test_in_charset(void) {
555 log_info("/* %s */", __func__);
556
557 assert_se(in_charset("dddaaabbbcccc", "abcd"));
558 assert_se(!in_charset("dddaaabbbcccc", "abc f"));
559 }
560
561 static void test_split_pair(void) {
562 log_info("/* %s */", __func__);
563
564 _cleanup_free_ char *a = NULL, *b = NULL;
565
566 assert_se(split_pair("", "", &a, &b) == -EINVAL);
567 assert_se(split_pair("foo=bar", "", &a, &b) == -EINVAL);
568 assert_se(split_pair("", "=", &a, &b) == -EINVAL);
569 assert_se(split_pair("foo=bar", "=", &a, &b) >= 0);
570 assert_se(streq(a, "foo"));
571 assert_se(streq(b, "bar"));
572 free(a);
573 free(b);
574 assert_se(split_pair("==", "==", &a, &b) >= 0);
575 assert_se(streq(a, ""));
576 assert_se(streq(b, ""));
577 free(a);
578 free(b);
579
580 assert_se(split_pair("===", "==", &a, &b) >= 0);
581 assert_se(streq(a, ""));
582 assert_se(streq(b, "="));
583 }
584
585 static void test_first_word(void) {
586 log_info("/* %s */", __func__);
587
588 assert_se(first_word("Hello", ""));
589 assert_se(first_word("Hello", "Hello"));
590 assert_se(first_word("Hello world", "Hello"));
591 assert_se(first_word("Hello\tworld", "Hello"));
592 assert_se(first_word("Hello\nworld", "Hello"));
593 assert_se(first_word("Hello\rworld", "Hello"));
594 assert_se(first_word("Hello ", "Hello"));
595
596 assert_se(!first_word("Hello", "Hellooo"));
597 assert_se(!first_word("Hello", "xxxxx"));
598 assert_se(!first_word("Hellooo", "Hello"));
599 }
600
601 static void test_strlen_ptr(void) {
602 log_info("/* %s */", __func__);
603
604 assert_se(strlen_ptr("foo") == 3);
605 assert_se(strlen_ptr("") == 0);
606 assert_se(strlen_ptr(NULL) == 0);
607 }
608
609 static void test_memory_startswith(void) {
610 log_info("/* %s */", __func__);
611
612 assert_se(streq(memory_startswith("", 0, ""), ""));
613 assert_se(streq(memory_startswith("", 1, ""), ""));
614 assert_se(streq(memory_startswith("x", 2, ""), "x"));
615 assert_se(!memory_startswith("", 1, "x"));
616 assert_se(!memory_startswith("", 1, "xxxxxxxx"));
617 assert_se(streq(memory_startswith("xxx", 4, "x"), "xx"));
618 assert_se(streq(memory_startswith("xxx", 4, "xx"), "x"));
619 assert_se(streq(memory_startswith("xxx", 4, "xxx"), ""));
620 assert_se(!memory_startswith("xxx", 4, "xxxx"));
621 }
622
623 static void test_memory_startswith_no_case(void) {
624 log_info("/* %s */", __func__);
625
626 assert_se(streq(memory_startswith_no_case("", 0, ""), ""));
627 assert_se(streq(memory_startswith_no_case("", 1, ""), ""));
628 assert_se(streq(memory_startswith_no_case("x", 2, ""), "x"));
629 assert_se(streq(memory_startswith_no_case("X", 2, ""), "X"));
630 assert_se(!memory_startswith_no_case("", 1, "X"));
631 assert_se(!memory_startswith_no_case("", 1, "xxxxXXXX"));
632 assert_se(streq(memory_startswith_no_case("xxx", 4, "X"), "xx"));
633 assert_se(streq(memory_startswith_no_case("XXX", 4, "x"), "XX"));
634 assert_se(streq(memory_startswith_no_case("XXX", 4, "X"), "XX"));
635 assert_se(streq(memory_startswith_no_case("xxx", 4, "XX"), "x"));
636 assert_se(streq(memory_startswith_no_case("XXX", 4, "xx"), "X"));
637 assert_se(streq(memory_startswith_no_case("XXX", 4, "XX"), "X"));
638 assert_se(streq(memory_startswith_no_case("xxx", 4, "XXX"), ""));
639 assert_se(streq(memory_startswith_no_case("XXX", 4, "xxx"), ""));
640 assert_se(streq(memory_startswith_no_case("XXX", 4, "XXX"), ""));
641
642 assert_se(memory_startswith_no_case((char[2]){'x', 'x'}, 2, "xx"));
643 assert_se(memory_startswith_no_case((char[2]){'x', 'X'}, 2, "xX"));
644 assert_se(memory_startswith_no_case((char[2]){'X', 'x'}, 2, "Xx"));
645 assert_se(memory_startswith_no_case((char[2]){'X', 'X'}, 2, "XX"));
646 }
647
648 static void test_string_truncate_lines_one(const char *input, size_t n_lines, const char *output, bool truncation) {
649 _cleanup_free_ char *b = NULL;
650 int k;
651
652 assert_se((k = string_truncate_lines(input, n_lines, &b)) >= 0);
653 assert_se(streq(b, output));
654 assert_se(!!k == truncation);
655 }
656
657 static void test_string_truncate_lines(void) {
658 log_info("/* %s */", __func__);
659
660 test_string_truncate_lines_one("", 0, "", false);
661 test_string_truncate_lines_one("", 1, "", false);
662 test_string_truncate_lines_one("", 2, "", false);
663 test_string_truncate_lines_one("", 3, "", false);
664
665 test_string_truncate_lines_one("x", 0, "", true);
666 test_string_truncate_lines_one("x", 1, "x", false);
667 test_string_truncate_lines_one("x", 2, "x", false);
668 test_string_truncate_lines_one("x", 3, "x", false);
669
670 test_string_truncate_lines_one("x\n", 0, "", true);
671 test_string_truncate_lines_one("x\n", 1, "x", false);
672 test_string_truncate_lines_one("x\n", 2, "x", false);
673 test_string_truncate_lines_one("x\n", 3, "x", false);
674
675 test_string_truncate_lines_one("x\ny", 0, "", true);
676 test_string_truncate_lines_one("x\ny", 1, "x", true);
677 test_string_truncate_lines_one("x\ny", 2, "x\ny", false);
678 test_string_truncate_lines_one("x\ny", 3, "x\ny", false);
679
680 test_string_truncate_lines_one("x\ny\n", 0, "", true);
681 test_string_truncate_lines_one("x\ny\n", 1, "x", true);
682 test_string_truncate_lines_one("x\ny\n", 2, "x\ny", false);
683 test_string_truncate_lines_one("x\ny\n", 3, "x\ny", false);
684
685 test_string_truncate_lines_one("x\ny\nz", 0, "", true);
686 test_string_truncate_lines_one("x\ny\nz", 1, "x", true);
687 test_string_truncate_lines_one("x\ny\nz", 2, "x\ny", true);
688 test_string_truncate_lines_one("x\ny\nz", 3, "x\ny\nz", false);
689
690 test_string_truncate_lines_one("x\ny\nz\n", 0, "", true);
691 test_string_truncate_lines_one("x\ny\nz\n", 1, "x", true);
692 test_string_truncate_lines_one("x\ny\nz\n", 2, "x\ny", true);
693 test_string_truncate_lines_one("x\ny\nz\n", 3, "x\ny\nz", false);
694
695 test_string_truncate_lines_one("\n", 0, "", false);
696 test_string_truncate_lines_one("\n", 1, "", false);
697 test_string_truncate_lines_one("\n", 2, "", false);
698 test_string_truncate_lines_one("\n", 3, "", false);
699
700 test_string_truncate_lines_one("\n\n", 0, "", false);
701 test_string_truncate_lines_one("\n\n", 1, "", false);
702 test_string_truncate_lines_one("\n\n", 2, "", false);
703 test_string_truncate_lines_one("\n\n", 3, "", false);
704
705 test_string_truncate_lines_one("\n\n\n", 0, "", false);
706 test_string_truncate_lines_one("\n\n\n", 1, "", false);
707 test_string_truncate_lines_one("\n\n\n", 2, "", false);
708 test_string_truncate_lines_one("\n\n\n", 3, "", false);
709
710 test_string_truncate_lines_one("\nx\n\n", 0, "", true);
711 test_string_truncate_lines_one("\nx\n\n", 1, "", true);
712 test_string_truncate_lines_one("\nx\n\n", 2, "\nx", false);
713 test_string_truncate_lines_one("\nx\n\n", 3, "\nx", false);
714
715 test_string_truncate_lines_one("\n\nx\n", 0, "", true);
716 test_string_truncate_lines_one("\n\nx\n", 1, "", true);
717 test_string_truncate_lines_one("\n\nx\n", 2, "", true);
718 test_string_truncate_lines_one("\n\nx\n", 3, "\n\nx", false);
719 }
720
721 static void test_string_extract_lines_one(const char *input, size_t i, const char *output, bool more) {
722 _cleanup_free_ char *b = NULL;
723 int k;
724
725 assert_se((k = string_extract_line(input, i, &b)) >= 0);
726 assert_se(streq(b ?: input, output));
727 assert_se(!!k == more);
728 }
729
730 static void test_string_extract_line(void) {
731 log_info("/* %s */", __func__);
732
733 test_string_extract_lines_one("", 0, "", false);
734 test_string_extract_lines_one("", 1, "", false);
735 test_string_extract_lines_one("", 2, "", false);
736 test_string_extract_lines_one("", 3, "", false);
737
738 test_string_extract_lines_one("x", 0, "x", false);
739 test_string_extract_lines_one("x", 1, "", false);
740 test_string_extract_lines_one("x", 2, "", false);
741 test_string_extract_lines_one("x", 3, "", false);
742
743 test_string_extract_lines_one("x\n", 0, "x", false);
744 test_string_extract_lines_one("x\n", 1, "", false);
745 test_string_extract_lines_one("x\n", 2, "", false);
746 test_string_extract_lines_one("x\n", 3, "", false);
747
748 test_string_extract_lines_one("x\ny", 0, "x", true);
749 test_string_extract_lines_one("x\ny", 1, "y", false);
750 test_string_extract_lines_one("x\ny", 2, "", false);
751 test_string_extract_lines_one("x\ny", 3, "", false);
752
753 test_string_extract_lines_one("x\ny\n", 0, "x", true);
754 test_string_extract_lines_one("x\ny\n", 1, "y", false);
755 test_string_extract_lines_one("x\ny\n", 2, "", false);
756 test_string_extract_lines_one("x\ny\n", 3, "", false);
757
758 test_string_extract_lines_one("x\ny\nz", 0, "x", true);
759 test_string_extract_lines_one("x\ny\nz", 1, "y", true);
760 test_string_extract_lines_one("x\ny\nz", 2, "z", false);
761 test_string_extract_lines_one("x\ny\nz", 3, "", false);
762
763 test_string_extract_lines_one("\n", 0, "", false);
764 test_string_extract_lines_one("\n", 1, "", false);
765 test_string_extract_lines_one("\n", 2, "", false);
766 test_string_extract_lines_one("\n", 3, "", false);
767
768 test_string_extract_lines_one("\n\n", 0, "", true);
769 test_string_extract_lines_one("\n\n", 1, "", false);
770 test_string_extract_lines_one("\n\n", 2, "", false);
771 test_string_extract_lines_one("\n\n", 3, "", false);
772
773 test_string_extract_lines_one("\n\n\n", 0, "", true);
774 test_string_extract_lines_one("\n\n\n", 1, "", true);
775 test_string_extract_lines_one("\n\n\n", 2, "", false);
776 test_string_extract_lines_one("\n\n\n", 3, "", false);
777
778 test_string_extract_lines_one("\n\n\n\n", 0, "", true);
779 test_string_extract_lines_one("\n\n\n\n", 1, "", true);
780 test_string_extract_lines_one("\n\n\n\n", 2, "", true);
781 test_string_extract_lines_one("\n\n\n\n", 3, "", false);
782
783 test_string_extract_lines_one("\nx\n\n\n", 0, "", true);
784 test_string_extract_lines_one("\nx\n\n\n", 1, "x", true);
785 test_string_extract_lines_one("\nx\n\n\n", 2, "", true);
786 test_string_extract_lines_one("\nx\n\n\n", 3, "", false);
787
788 test_string_extract_lines_one("\n\nx\n\n", 0, "", true);
789 test_string_extract_lines_one("\n\nx\n\n", 1, "", true);
790 test_string_extract_lines_one("\n\nx\n\n", 2, "x", true);
791 test_string_extract_lines_one("\n\nx\n\n", 3, "", false);
792
793 test_string_extract_lines_one("\n\n\nx\n", 0, "", true);
794 test_string_extract_lines_one("\n\n\nx\n", 1, "", true);
795 test_string_extract_lines_one("\n\n\nx\n", 2, "", true);
796 test_string_extract_lines_one("\n\n\nx\n", 3, "x", false);
797 }
798
799 static void test_string_contains_word_strv(void) {
800 log_info("/* %s */", __func__);
801
802 const char *w;
803
804 assert_se(string_contains_word_strv("a b cc", NULL, STRV_MAKE("a", "b"), NULL));
805
806 assert_se(string_contains_word_strv("a b cc", NULL, STRV_MAKE("a", "b"), &w));
807 assert_se(streq(w, "a"));
808
809 assert_se(!string_contains_word_strv("a b cc", NULL, STRV_MAKE("d"), &w));
810 assert_se(w == NULL);
811
812 assert_se(string_contains_word_strv("a b cc", NULL, STRV_MAKE("b", "a"), &w));
813 assert_se(streq(w, "a"));
814
815 assert_se(string_contains_word_strv("b a b cc", NULL, STRV_MAKE("b", "a", "b"), &w));
816 assert_se(streq(w, "b"));
817
818 assert_se(string_contains_word_strv("a b cc", NULL, STRV_MAKE("b", ""), &w));
819 assert_se(streq(w, "b"));
820
821 assert_se(!string_contains_word_strv("a b cc", NULL, STRV_MAKE(""), &w));
822 assert_se(w == NULL);
823
824 assert_se(string_contains_word_strv("a b cc", " ", STRV_MAKE(""), &w));
825 assert_se(streq(w, ""));
826 }
827
828 static void test_string_contains_word(void) {
829 log_info("/* %s */", __func__);
830
831 assert_se( string_contains_word("a b cc", NULL, "a"));
832 assert_se( string_contains_word("a b cc", NULL, "b"));
833 assert_se(!string_contains_word("a b cc", NULL, "c"));
834 assert_se( string_contains_word("a b cc", NULL, "cc"));
835 assert_se(!string_contains_word("a b cc", NULL, "d"));
836 assert_se(!string_contains_word("a b cc", NULL, "a b"));
837 assert_se(!string_contains_word("a b cc", NULL, "a b c"));
838 assert_se(!string_contains_word("a b cc", NULL, "b c"));
839 assert_se(!string_contains_word("a b cc", NULL, "b cc"));
840 assert_se(!string_contains_word("a b cc", NULL, "a "));
841 assert_se(!string_contains_word("a b cc", NULL, " b "));
842 assert_se(!string_contains_word("a b cc", NULL, " cc"));
843
844 assert_se( string_contains_word(" a b\t\tcc", NULL, "a"));
845 assert_se( string_contains_word(" a b\t\tcc", NULL, "b"));
846 assert_se(!string_contains_word(" a b\t\tcc", NULL, "c"));
847 assert_se( string_contains_word(" a b\t\tcc", NULL, "cc"));
848 assert_se(!string_contains_word(" a b\t\tcc", NULL, "d"));
849 assert_se(!string_contains_word(" a b\t\tcc", NULL, "a b"));
850 assert_se(!string_contains_word(" a b\t\tcc", NULL, "a b\t\tc"));
851 assert_se(!string_contains_word(" a b\t\tcc", NULL, "b\t\tc"));
852 assert_se(!string_contains_word(" a b\t\tcc", NULL, "b\t\tcc"));
853 assert_se(!string_contains_word(" a b\t\tcc", NULL, "a "));
854 assert_se(!string_contains_word(" a b\t\tcc", NULL, " b "));
855 assert_se(!string_contains_word(" a b\t\tcc", NULL, " cc"));
856
857 assert_se(!string_contains_word(" a b\t\tcc", NULL, ""));
858 assert_se(!string_contains_word(" a b\t\tcc", NULL, " "));
859 assert_se(!string_contains_word(" a b\t\tcc", NULL, " "));
860 assert_se( string_contains_word(" a b\t\tcc", " ", ""));
861 assert_se( string_contains_word(" a b\t\tcc", "\t", ""));
862 assert_se( string_contains_word(" a b\t\tcc", WHITESPACE, ""));
863
864 assert_se( string_contains_word("a:b:cc", ":#", "a"));
865 assert_se( string_contains_word("a:b:cc", ":#", "b"));
866 assert_se(!string_contains_word("a:b:cc", ":#", "c"));
867 assert_se( string_contains_word("a:b:cc", ":#", "cc"));
868 assert_se(!string_contains_word("a:b:cc", ":#", "d"));
869 assert_se(!string_contains_word("a:b:cc", ":#", "a:b"));
870 assert_se(!string_contains_word("a:b:cc", ":#", "a:b:c"));
871 assert_se(!string_contains_word("a:b:cc", ":#", "b:c"));
872 assert_se(!string_contains_word("a#b#cc", ":#", "b:cc"));
873 assert_se( string_contains_word("a#b#cc", ":#", "b"));
874 assert_se( string_contains_word("a#b#cc", ":#", "cc"));
875 assert_se(!string_contains_word("a:b:cc", ":#", "a:"));
876 assert_se(!string_contains_word("a:b cc", ":#", "b"));
877 assert_se( string_contains_word("a:b cc", ":#", "b cc"));
878 assert_se(!string_contains_word("a:b:cc", ":#", ":cc"));
879 }
880
881 int main(int argc, char *argv[]) {
882 test_setup_logging(LOG_DEBUG);
883
884 test_string_erase();
885 test_free_and_strndup();
886 test_ascii_strcasecmp_n();
887 test_ascii_strcasecmp_nn();
888 test_cellescape();
889 test_streq_ptr();
890 test_strstrip();
891 test_strextend();
892 test_strextend_with_separator();
893 test_strrep();
894 test_string_has_cc();
895 test_ascii_strlower();
896 test_strshorten();
897 test_strjoina();
898 test_strjoin();
899 test_strcmp_ptr();
900 test_foreach_word();
901 test_foreach_word_quoted();
902 test_endswith();
903 test_endswith_no_case();
904 test_delete_chars();
905 test_delete_trailing_chars();
906 test_delete_trailing_slashes();
907 test_skip_leading_chars();
908 test_in_charset();
909 test_split_pair();
910 test_first_word();
911 test_strlen_ptr();
912 test_memory_startswith();
913 test_memory_startswith_no_case();
914 test_string_truncate_lines();
915 test_string_extract_line();
916 test_string_contains_word_strv();
917 test_string_contains_word();
918
919 return 0;
920 }