]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-string-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
4 #include "locale-util.h"
6 #include "string-util.h"
12 static void test_string_erase(void) {
13 log_info("/* %s */", __func__
);
17 assert_se(streq(string_erase(x
), ""));
20 assert_se(streq(string_erase(x
), ""));
22 x
= strdupa("123456789");
23 assert_se(streq(string_erase(x
), ""));
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');
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
));
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 */
45 static void test_free_and_strndup(void) {
46 log_info("/* %s */", __func__
);
48 static const struct test_case
{
80 {"foobar", 6, "foobar"},
83 _cleanup_free_
char *t
= NULL
;
84 const char *prev_expected
= t
;
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
));
94 static void test_ascii_strcasecmp_n(void) {
95 log_info("/* %s */", __func__
);
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);
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);
122 static void test_ascii_strcasecmp_nn(void) {
123 log_info("/* %s */", __func__
);
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);
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);
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);
141 static void test_cellescape(void) {
144 log_info("/* %s */", __func__
);
146 assert_se(streq(cellescape(buf
, 1, ""), ""));
147 assert_se(streq(cellescape(buf
, 1, "1"), ""));
148 assert_se(streq(cellescape(buf
, 1, "12"), ""));
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"), "."));
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"), ".."));
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() ? "…" : "..."));
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..."));
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"));
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"));
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"));
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"));
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"));
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..."));
212 assert_se(streq(cellescape(buf
, 40, "1\020"), "1\\020"));
213 assert_se(streq(cellescape(buf
, 40, "1\020x"), "1\\020x"));
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..."));
220 assert_se(streq(cellescape(buf
, sizeof buf
, "1\020"), "1\\020"));
221 assert_se(streq(cellescape(buf
, sizeof buf
, "1\020x"), "1\\020x"));
224 static void test_streq_ptr(void) {
225 log_info("/* %s */", __func__
);
227 assert_se(streq_ptr(NULL
, NULL
));
228 assert_se(!streq_ptr("abc", "cdef"));
231 static void test_strstrip(void) {
232 log_info("/* %s */", __func__
);
234 char *ret
, input
[] = " hello, waldo. ";
236 ret
= strstrip(input
);
237 assert_se(streq(ret
, "hello, waldo."));
240 static void test_strextend(void) {
241 log_info("/* %s */", __func__
);
243 _cleanup_free_
char *str
= NULL
;
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"));
253 static void test_strextend_with_separator(void) {
254 log_info("/* %s */", __func__
);
256 _cleanup_free_
char *str
= NULL
;
258 assert_se(strextend_with_separator(&str
, NULL
, NULL
));
259 assert_se(streq_ptr(str
, ""));
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
, ""));
268 assert_se(strextend_with_separator(&str
, "xyz", "a", "bb", "ccc", NULL
));
269 assert_se(streq_ptr(str
, "axyzbbxyzccc"));
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"));
278 static void test_strrep(void) {
279 log_info("/* %s */", __func__
);
281 _cleanup_free_
char *one
, *three
, *zero
;
282 one
= strrep("waldo", 1);
283 three
= strrep("waldo", 3);
284 zero
= strrep("waldo", 0);
286 assert_se(streq(one
, "waldo"));
287 assert_se(streq(three
, "waldowaldowaldo"));
288 assert_se(streq(zero
, ""));
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"));
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"));
305 static void test_ascii_strlower(void) {
306 log_info("/* %s */", __func__
);
308 char a
[] = "AabBcC Jk Ii Od LKJJJ kkd LK";
309 assert_se(streq(ascii_strlower(a
), "aabbcc jk ii od lkjjj kkd lk"));
312 static void test_strshorten(void) {
313 log_info("/* %s */", __func__
);
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);
323 static void test_strjoina(void) {
324 log_info("/* %s */", __func__
);
328 actual
= strjoina("", "foo", "bar");
329 assert_se(streq(actual
, "foobar"));
331 actual
= strjoina("foo", "bar", "baz");
332 assert_se(streq(actual
, "foobarbaz"));
334 actual
= strjoina("foo", "", "bar", "baz");
335 assert_se(streq(actual
, "foobarbaz"));
337 actual
= strjoina("foo");
338 assert_se(streq(actual
, "foo"));
340 actual
= strjoina(NULL
);
341 assert_se(streq(actual
, ""));
343 actual
= strjoina(NULL
, "foo");
344 assert_se(streq(actual
, ""));
346 actual
= strjoina("foo", NULL
, "bar");
347 assert_se(streq(actual
, "foo"));
350 static void test_strjoin(void) {
353 actual
= strjoin("", "foo", "bar");
354 assert_se(streq(actual
, "foobar"));
357 actual
= strjoin("foo", "bar", "baz");
358 assert_se(streq(actual
, "foobarbaz"));
361 actual
= strjoin("foo", "", "bar", "baz");
362 assert_se(streq(actual
, "foobarbaz"));
365 actual
= strjoin("foo", NULL
);
366 assert_se(streq(actual
, "foo"));
369 actual
= strjoin(NULL
, NULL
);
370 assert_se(streq(actual
, ""));
373 actual
= strjoin(NULL
, "foo");
374 assert_se(streq(actual
, ""));
377 actual
= strjoin("foo", NULL
, "bar");
378 assert_se(streq(actual
, "foo"));
382 static void test_strcmp_ptr(void) {
383 log_info("/* %s */", __func__
);
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);
396 static void test_foreach_word(void) {
397 log_info("/* %s */", __func__
);
399 const char *word
, *state
;
402 const char test
[] = "test abc d\te f ";
403 const char * const expected
[] = {
413 FOREACH_WORD(word
, l
, test
, state
)
414 assert_se(strneq(expected
[i
++], word
, l
));
417 static void check(const char *test
, char** expected
, bool trailing
) {
420 printf("<<<%s>>>\n", test
);
422 _cleanup_free_
char *word
= NULL
;
424 r
= extract_first_word(&test
, &word
, NULL
, EXTRACT_UNQUOTE
);
426 assert_se(!trailing
);
433 assert_se(streq(word
, expected
[i
++]));
434 printf("<%s>\n", word
);
436 assert_se(expected
[i
] == NULL
);
439 static void test_foreach_word_quoted(void) {
440 log_info("/* %s */", __func__
);
442 check("test a b c 'd' e '' '' hhh '' '' \"a b c\"",
466 static void test_endswith(void) {
467 log_info("/* %s */", __func__
);
469 assert_se(endswith("foobar", "bar"));
470 assert_se(endswith("foobar", ""));
471 assert_se(endswith("foobar", "foobar"));
472 assert_se(endswith("", ""));
474 assert_se(!endswith("foobar", "foo"));
475 assert_se(!endswith("foobar", "foobarfoofoo"));
478 static void test_endswith_no_case(void) {
479 log_info("/* %s */", __func__
);
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("", ""));
486 assert_se(!endswith_no_case("foobar", "FOO"));
487 assert_se(!endswith_no_case("foobar", "FOOBARFOOFOO"));
490 static void test_delete_chars(void) {
491 log_info("/* %s */", __func__
);
493 char *s
, input
[] = " hello, waldo. abc";
495 s
= delete_chars(input
, WHITESPACE
);
496 assert_se(streq(s
, "hello,waldo.abc"));
497 assert_se(s
== input
);
500 static void test_delete_trailing_chars(void) {
501 log_info("/* %s */", __func__
);
504 input1
[] = " \n \r k \n \r ",
505 input2
[] = "kkkkthiskkkiskkkaktestkkk",
508 s
= delete_trailing_chars(input1
, WHITESPACE
);
509 assert_se(streq(s
, " \n \r k"));
510 assert_se(s
== input1
);
512 s
= delete_trailing_chars(input2
, "kt");
513 assert_se(streq(s
, "kkkkthiskkkiskkkaktes"));
514 assert_se(s
== input2
);
516 s
= delete_trailing_chars(input3
, WHITESPACE
);
517 assert_se(streq(s
, "abcdef"));
518 assert_se(s
== input3
);
520 s
= delete_trailing_chars(input3
, "fe");
521 assert_se(streq(s
, "abcd"));
522 assert_se(s
== input3
);
525 static void test_delete_trailing_slashes(void) {
526 log_info("/* %s */", __func__
);
528 char s1
[] = "foobar//",
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
, "/"), ""));
540 static void test_skip_leading_chars(void) {
541 log_info("/* %s */", __func__
);
543 char input1
[] = " \n \r k \n \r ",
544 input2
[] = "kkkkthiskkkiskkkaktestkkk",
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"));
554 static void test_in_charset(void) {
555 log_info("/* %s */", __func__
);
557 assert_se(in_charset("dddaaabbbcccc", "abcd"));
558 assert_se(!in_charset("dddaaabbbcccc", "abc f"));
561 static void test_split_pair(void) {
562 log_info("/* %s */", __func__
);
564 _cleanup_free_
char *a
= NULL
, *b
= NULL
;
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"));
574 assert_se(split_pair("==", "==", &a
, &b
) >= 0);
575 assert_se(streq(a
, ""));
576 assert_se(streq(b
, ""));
580 assert_se(split_pair("===", "==", &a
, &b
) >= 0);
581 assert_se(streq(a
, ""));
582 assert_se(streq(b
, "="));
585 static void test_first_word(void) {
586 log_info("/* %s */", __func__
);
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"));
596 assert_se(!first_word("Hello", "Hellooo"));
597 assert_se(!first_word("Hello", "xxxxx"));
598 assert_se(!first_word("Hellooo", "Hello"));
601 static void test_strlen_ptr(void) {
602 log_info("/* %s */", __func__
);
604 assert_se(strlen_ptr("foo") == 3);
605 assert_se(strlen_ptr("") == 0);
606 assert_se(strlen_ptr(NULL
) == 0);
609 static void test_memory_startswith(void) {
610 log_info("/* %s */", __func__
);
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"));
623 static void test_memory_startswith_no_case(void) {
624 log_info("/* %s */", __func__
);
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"), ""));
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"));
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
;
652 assert_se((k
= string_truncate_lines(input
, n_lines
, &b
)) >= 0);
653 assert_se(streq(b
, output
));
654 assert_se(!!k
== truncation
);
657 static void test_string_truncate_lines(void) {
658 log_info("/* %s */", __func__
);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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
;
725 assert_se((k
= string_extract_line(input
, i
, &b
)) >= 0);
726 assert_se(streq(b
?: input
, output
));
727 assert_se(!!k
== more
);
730 static void test_string_extract_line(void) {
731 log_info("/* %s */", __func__
);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
799 static void test_string_contains_word_strv(void) {
800 log_info("/* %s */", __func__
);
804 assert_se(string_contains_word_strv("a b cc", NULL
, STRV_MAKE("a", "b"), NULL
));
806 assert_se(string_contains_word_strv("a b cc", NULL
, STRV_MAKE("a", "b"), &w
));
807 assert_se(streq(w
, "a"));
809 assert_se(!string_contains_word_strv("a b cc", NULL
, STRV_MAKE("d"), &w
));
810 assert_se(w
== NULL
);
812 assert_se(string_contains_word_strv("a b cc", NULL
, STRV_MAKE("b", "a"), &w
));
813 assert_se(streq(w
, "a"));
815 assert_se(string_contains_word_strv("b a b cc", NULL
, STRV_MAKE("b", "a", "b"), &w
));
816 assert_se(streq(w
, "b"));
818 assert_se(string_contains_word_strv("a b cc", NULL
, STRV_MAKE("b", ""), &w
));
819 assert_se(streq(w
, "b"));
821 assert_se(!string_contains_word_strv("a b cc", NULL
, STRV_MAKE(""), &w
));
822 assert_se(w
== NULL
);
824 assert_se(string_contains_word_strv("a b cc", " ", STRV_MAKE(""), &w
));
825 assert_se(streq(w
, ""));
828 static void test_string_contains_word(void) {
829 log_info("/* %s */", __func__
);
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"));
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"));
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
, ""));
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"));
881 int main(int argc
, char *argv
[]) {
882 test_setup_logging(LOG_DEBUG
);
885 test_free_and_strndup();
886 test_ascii_strcasecmp_n();
887 test_ascii_strcasecmp_nn();
892 test_strextend_with_separator();
894 test_string_has_cc();
895 test_ascii_strlower();
901 test_foreach_word_quoted();
903 test_endswith_no_case();
905 test_delete_trailing_chars();
906 test_delete_trailing_slashes();
907 test_skip_leading_chars();
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();