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