]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
9fe4ea21 | 2 | |
6571a64e | 3 | #include "alloc-util.h" |
8409f688 | 4 | #include "locale-util.h" |
6571a64e | 5 | #include "macro.h" |
9fe4ea21 | 6 | #include "string-util.h" |
6571a64e | 7 | #include "strv.h" |
7f546026 | 8 | #include "tests.h" |
9924aef6 | 9 | #include "utf8.h" |
ca78ad1d | 10 | #include "util.h" |
9fe4ea21 | 11 | |
7f546026 ZJS |
12 | static void test_free_and_strndup_one(char **t, const char *src, size_t l, const char *expected, bool change) { |
13 | int r; | |
14 | ||
15 | log_debug("%s: \"%s\", \"%s\", %zd (expect \"%s\", %s)", | |
16 | __func__, strnull(*t), strnull(src), l, strnull(expected), yes_no(change)); | |
17 | ||
18 | r = free_and_strndup(t, src, l); | |
19 | assert_se(streq_ptr(*t, expected)); | |
20 | assert_se(r == change); /* check that change occurs only when necessary */ | |
21 | } | |
22 | ||
23 | static void test_free_and_strndup(void) { | |
24 | static const struct test_case { | |
25 | const char *src; | |
26 | size_t len; | |
27 | const char *expected; | |
28 | } cases[] = { | |
29 | {"abc", 0, ""}, | |
30 | {"abc", 0, ""}, | |
31 | {"abc", 1, "a"}, | |
32 | {"abc", 2, "ab"}, | |
33 | {"abc", 3, "abc"}, | |
34 | {"abc", 4, "abc"}, | |
35 | {"abc", 5, "abc"}, | |
36 | {"abc", 5, "abc"}, | |
37 | {"abc", 4, "abc"}, | |
38 | {"abc", 3, "abc"}, | |
39 | {"abc", 2, "ab"}, | |
40 | {"abc", 1, "a"}, | |
41 | {"abc", 0, ""}, | |
42 | ||
43 | {"", 0, ""}, | |
44 | {"", 1, ""}, | |
45 | {"", 2, ""}, | |
46 | {"", 0, ""}, | |
47 | {"", 1, ""}, | |
48 | {"", 2, ""}, | |
49 | {"", 2, ""}, | |
50 | {"", 1, ""}, | |
51 | {"", 0, ""}, | |
52 | ||
53 | {NULL, 0, NULL}, | |
54 | ||
55 | {"foo", 3, "foo"}, | |
56 | {"foobar", 6, "foobar"}, | |
57 | }; | |
58 | ||
59 | _cleanup_free_ char *t = NULL; | |
60 | const char *prev_expected = t; | |
61 | ||
62 | for (unsigned i = 0; i < ELEMENTSOF(cases); i++) { | |
63 | test_free_and_strndup_one(&t, | |
64 | cases[i].src, cases[i].len, cases[i].expected, | |
65 | !streq_ptr(cases[i].expected, prev_expected)); | |
66 | prev_expected = t; | |
67 | } | |
68 | } | |
69 | ||
522d85ae LP |
70 | static void test_ascii_strcasecmp_n(void) { |
71 | ||
72 | assert_se(ascii_strcasecmp_n("", "", 0) == 0); | |
73 | assert_se(ascii_strcasecmp_n("", "", 1) == 0); | |
74 | assert_se(ascii_strcasecmp_n("", "a", 1) < 0); | |
75 | assert_se(ascii_strcasecmp_n("", "a", 2) < 0); | |
76 | assert_se(ascii_strcasecmp_n("a", "", 1) > 0); | |
77 | assert_se(ascii_strcasecmp_n("a", "", 2) > 0); | |
78 | assert_se(ascii_strcasecmp_n("a", "a", 1) == 0); | |
79 | assert_se(ascii_strcasecmp_n("a", "a", 2) == 0); | |
80 | assert_se(ascii_strcasecmp_n("a", "b", 1) < 0); | |
81 | assert_se(ascii_strcasecmp_n("a", "b", 2) < 0); | |
82 | assert_se(ascii_strcasecmp_n("b", "a", 1) > 0); | |
83 | assert_se(ascii_strcasecmp_n("b", "a", 2) > 0); | |
84 | assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxYxxxx", 9) == 0); | |
85 | assert_se(ascii_strcasecmp_n("xxxxxxxxx", "xxxxyxxxx", 9) < 0); | |
86 | assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxyxxxx", 9) < 0); | |
87 | assert_se(ascii_strcasecmp_n("xxxxxxxxx", "xxxxYxxxx", 9) < 0); | |
88 | assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxYxxxx", 9) < 0); | |
89 | ||
90 | assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxYxxxx", 9) == 0); | |
91 | assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxxxxxx", 9) > 0); | |
92 | assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxXxxxx", 9) > 0); | |
93 | assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxxxxxx", 9) > 0); | |
94 | assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxXxxxx", 9) > 0); | |
95 | } | |
96 | ||
c1749834 LP |
97 | static void test_ascii_strcasecmp_nn(void) { |
98 | assert_se(ascii_strcasecmp_nn("", 0, "", 0) == 0); | |
99 | assert_se(ascii_strcasecmp_nn("", 0, "", 1) < 0); | |
100 | assert_se(ascii_strcasecmp_nn("", 1, "", 0) > 0); | |
101 | assert_se(ascii_strcasecmp_nn("", 1, "", 1) == 0); | |
102 | ||
103 | assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaAa", 4) == 0); | |
104 | assert_se(ascii_strcasecmp_nn("aaa", 3, "aaAa", 4) < 0); | |
105 | assert_se(ascii_strcasecmp_nn("aaa", 4, "aaAa", 4) < 0); | |
106 | assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaA", 3) > 0); | |
107 | assert_se(ascii_strcasecmp_nn("aaaa", 4, "AAA", 4) > 0); | |
108 | ||
109 | assert_se(ascii_strcasecmp_nn("aaaa", 4, "bbbb", 4) < 0); | |
110 | assert_se(ascii_strcasecmp_nn("aaAA", 4, "BBbb", 4) < 0); | |
111 | assert_se(ascii_strcasecmp_nn("BBbb", 4, "aaaa", 4) > 0); | |
112 | } | |
113 | ||
8409f688 ZJS |
114 | static void test_cellescape(void) { |
115 | char buf[40]; | |
116 | ||
61f6e276 LP |
117 | assert_se(streq(cellescape(buf, 1, ""), "")); |
118 | assert_se(streq(cellescape(buf, 1, "1"), "")); | |
119 | assert_se(streq(cellescape(buf, 1, "12"), "")); | |
120 | ||
121 | assert_se(streq(cellescape(buf, 2, ""), "")); | |
122 | assert_se(streq(cellescape(buf, 2, "1"), "1")); | |
123 | assert_se(streq(cellescape(buf, 2, "12"), ".")); | |
124 | assert_se(streq(cellescape(buf, 2, "123"), ".")); | |
125 | ||
126 | assert_se(streq(cellescape(buf, 3, ""), "")); | |
127 | assert_se(streq(cellescape(buf, 3, "1"), "1")); | |
128 | assert_se(streq(cellescape(buf, 3, "12"), "12")); | |
129 | assert_se(streq(cellescape(buf, 3, "123"), "..")); | |
130 | assert_se(streq(cellescape(buf, 3, "1234"), "..")); | |
131 | ||
132 | assert_se(streq(cellescape(buf, 4, ""), "")); | |
133 | assert_se(streq(cellescape(buf, 4, "1"), "1")); | |
134 | assert_se(streq(cellescape(buf, 4, "12"), "12")); | |
135 | assert_se(streq(cellescape(buf, 4, "123"), "123")); | |
136 | assert_se(streq(cellescape(buf, 4, "1234"), is_locale_utf8() ? "…" : "...")); | |
137 | assert_se(streq(cellescape(buf, 4, "12345"), is_locale_utf8() ? "…" : "...")); | |
138 | ||
139 | assert_se(streq(cellescape(buf, 5, ""), "")); | |
140 | assert_se(streq(cellescape(buf, 5, "1"), "1")); | |
141 | assert_se(streq(cellescape(buf, 5, "12"), "12")); | |
142 | assert_se(streq(cellescape(buf, 5, "123"), "123")); | |
143 | assert_se(streq(cellescape(buf, 5, "1234"), "1234")); | |
144 | assert_se(streq(cellescape(buf, 5, "12345"), is_locale_utf8() ? "1…" : "1...")); | |
145 | assert_se(streq(cellescape(buf, 5, "123456"), is_locale_utf8() ? "1…" : "1...")); | |
146 | ||
147 | assert_se(streq(cellescape(buf, 1, "\020"), "")); | |
148 | assert_se(streq(cellescape(buf, 2, "\020"), ".")); | |
149 | assert_se(streq(cellescape(buf, 3, "\020"), "..")); | |
150 | assert_se(streq(cellescape(buf, 4, "\020"), "…")); | |
151 | assert_se(streq(cellescape(buf, 5, "\020"), "\\020")); | |
152 | ||
153 | assert_se(streq(cellescape(buf, 5, "1234\020"), "1…")); | |
154 | assert_se(streq(cellescape(buf, 6, "1234\020"), "12…")); | |
155 | assert_se(streq(cellescape(buf, 7, "1234\020"), "123…")); | |
156 | assert_se(streq(cellescape(buf, 8, "1234\020"), "1234…")); | |
157 | assert_se(streq(cellescape(buf, 9, "1234\020"), "1234\\020")); | |
158 | ||
159 | assert_se(streq(cellescape(buf, 1, "\t\n"), "")); | |
160 | assert_se(streq(cellescape(buf, 2, "\t\n"), ".")); | |
161 | assert_se(streq(cellescape(buf, 3, "\t\n"), "..")); | |
162 | assert_se(streq(cellescape(buf, 4, "\t\n"), "…")); | |
163 | assert_se(streq(cellescape(buf, 5, "\t\n"), "\\t\\n")); | |
164 | ||
165 | assert_se(streq(cellescape(buf, 5, "1234\t\n"), "1…")); | |
166 | assert_se(streq(cellescape(buf, 6, "1234\t\n"), "12…")); | |
167 | assert_se(streq(cellescape(buf, 7, "1234\t\n"), "123…")); | |
168 | assert_se(streq(cellescape(buf, 8, "1234\t\n"), "1234…")); | |
169 | assert_se(streq(cellescape(buf, 9, "1234\t\n"), "1234\\t\\n")); | |
170 | ||
171 | assert_se(streq(cellescape(buf, 4, "x\t\020\n"), "…")); | |
172 | assert_se(streq(cellescape(buf, 5, "x\t\020\n"), "x…")); | |
173 | assert_se(streq(cellescape(buf, 6, "x\t\020\n"), "x…")); | |
174 | assert_se(streq(cellescape(buf, 7, "x\t\020\n"), "x\\t…")); | |
175 | assert_se(streq(cellescape(buf, 8, "x\t\020\n"), "x\\t…")); | |
176 | assert_se(streq(cellescape(buf, 9, "x\t\020\n"), "x\\t…")); | |
177 | assert_se(streq(cellescape(buf, 10, "x\t\020\n"), "x\\t\\020\\n")); | |
178 | ||
179 | assert_se(streq(cellescape(buf, 6, "1\011"), "1\\t")); | |
180 | assert_se(streq(cellescape(buf, 6, "1\020"), "1\\020")); | |
181 | assert_se(streq(cellescape(buf, 6, "1\020x"), is_locale_utf8() ? "1…" : "1...")); | |
8409f688 ZJS |
182 | |
183 | assert_se(streq(cellescape(buf, 40, "1\020"), "1\\020")); | |
184 | assert_se(streq(cellescape(buf, 40, "1\020x"), "1\\020x")); | |
185 | ||
186 | assert_se(streq(cellescape(buf, 40, "\a\b\f\n\r\t\v\\\"'"), "\\a\\b\\f\\n\\r\\t\\v\\\\\\\"\\'")); | |
61f6e276 LP |
187 | assert_se(streq(cellescape(buf, 6, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a…" : "\\a...")); |
188 | assert_se(streq(cellescape(buf, 7, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a…" : "\\a...")); | |
189 | assert_se(streq(cellescape(buf, 8, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a\\b…" : "\\a\\b...")); | |
8409f688 ZJS |
190 | |
191 | assert_se(streq(cellescape(buf, sizeof buf, "1\020"), "1\\020")); | |
192 | assert_se(streq(cellescape(buf, sizeof buf, "1\020x"), "1\\020x")); | |
193 | } | |
194 | ||
6571a64e RC |
195 | static void test_streq_ptr(void) { |
196 | assert_se(streq_ptr(NULL, NULL)); | |
197 | assert_se(!streq_ptr("abc", "cdef")); | |
198 | } | |
199 | ||
200 | static void test_strstrip(void) { | |
201 | char *r; | |
202 | char input[] = " hello, waldo. "; | |
203 | ||
204 | r = strstrip(input); | |
205 | assert_se(streq(r, "hello, waldo.")); | |
206 | } | |
207 | ||
208 | static void test_strextend(void) { | |
bb8ad9ea LP |
209 | _cleanup_free_ char *str = NULL; |
210 | ||
211 | assert_se(strextend(&str, NULL)); | |
212 | assert_se(streq_ptr(str, "")); | |
213 | assert_se(strextend(&str, "", "0", "", "", "123", NULL)); | |
214 | assert_se(streq_ptr(str, "0123")); | |
215 | assert_se(strextend(&str, "456", "78", "9", NULL)); | |
216 | assert_se(streq_ptr(str, "0123456789")); | |
217 | } | |
218 | ||
219 | static void test_strextend_with_separator(void) { | |
220 | _cleanup_free_ char *str = NULL; | |
221 | ||
222 | assert_se(strextend_with_separator(&str, NULL, NULL)); | |
223 | assert_se(streq_ptr(str, "")); | |
224 | str = mfree(str); | |
225 | ||
226 | assert_se(strextend_with_separator(&str, "...", NULL)); | |
227 | assert_se(streq_ptr(str, "")); | |
228 | assert_se(strextend_with_separator(&str, "...", NULL)); | |
229 | assert_se(streq_ptr(str, "")); | |
230 | str = mfree(str); | |
231 | ||
232 | assert_se(strextend_with_separator(&str, "xyz", "a", "bb", "ccc", NULL)); | |
233 | assert_se(streq_ptr(str, "axyzbbxyzccc")); | |
234 | str = mfree(str); | |
235 | ||
236 | assert_se(strextend_with_separator(&str, ",", "start", "", "1", "234", NULL)); | |
237 | assert_se(streq_ptr(str, "start,,1,234")); | |
238 | assert_se(strextend_with_separator(&str, ";", "more", "5", "678", NULL)); | |
239 | assert_se(streq_ptr(str, "start,,1,234;more;5;678")); | |
6571a64e RC |
240 | } |
241 | ||
242 | static void test_strrep(void) { | |
243 | _cleanup_free_ char *one, *three, *zero; | |
244 | one = strrep("waldo", 1); | |
245 | three = strrep("waldo", 3); | |
246 | zero = strrep("waldo", 0); | |
247 | ||
248 | assert_se(streq(one, "waldo")); | |
249 | assert_se(streq(three, "waldowaldowaldo")); | |
250 | assert_se(streq(zero, "")); | |
251 | } | |
252 | ||
6571a64e RC |
253 | static void test_string_has_cc(void) { |
254 | assert_se(string_has_cc("abc\1", NULL)); | |
255 | assert_se(string_has_cc("abc\x7f", NULL)); | |
256 | assert_se(string_has_cc("abc\x7f", NULL)); | |
257 | assert_se(string_has_cc("abc\t\x7f", "\t")); | |
258 | assert_se(string_has_cc("abc\t\x7f", "\t")); | |
259 | assert_se(string_has_cc("\x7f", "\t")); | |
260 | assert_se(string_has_cc("\x7f", "\t\a")); | |
261 | ||
262 | assert_se(!string_has_cc("abc\t\t", "\t")); | |
263 | assert_se(!string_has_cc("abc\t\t\a", "\t\a")); | |
264 | assert_se(!string_has_cc("a\ab\tc", "\t\a")); | |
265 | } | |
266 | ||
267 | static void test_ascii_strlower(void) { | |
268 | char a[] = "AabBcC Jk Ii Od LKJJJ kkd LK"; | |
269 | assert_se(streq(ascii_strlower(a), "aabbcc jk ii od lkjjj kkd lk")); | |
270 | } | |
271 | ||
272 | static void test_strshorten(void) { | |
273 | char s[] = "foobar"; | |
274 | ||
275 | assert_se(strlen(strshorten(s, 6)) == 6); | |
276 | assert_se(strlen(strshorten(s, 12)) == 6); | |
277 | assert_se(strlen(strshorten(s, 2)) == 2); | |
278 | assert_se(strlen(strshorten(s, 0)) == 0); | |
279 | } | |
280 | ||
281 | static void test_strjoina(void) { | |
282 | char *actual; | |
283 | ||
284 | actual = strjoina("", "foo", "bar"); | |
285 | assert_se(streq(actual, "foobar")); | |
286 | ||
287 | actual = strjoina("foo", "bar", "baz"); | |
288 | assert_se(streq(actual, "foobarbaz")); | |
289 | ||
290 | actual = strjoina("foo", "", "bar", "baz"); | |
291 | assert_se(streq(actual, "foobarbaz")); | |
292 | ||
293 | actual = strjoina("foo"); | |
294 | assert_se(streq(actual, "foo")); | |
295 | ||
296 | actual = strjoina(NULL); | |
297 | assert_se(streq(actual, "")); | |
298 | ||
299 | actual = strjoina(NULL, "foo"); | |
300 | assert_se(streq(actual, "")); | |
301 | ||
302 | actual = strjoina("foo", NULL, "bar"); | |
303 | assert_se(streq(actual, "foo")); | |
304 | } | |
305 | ||
306 | static void test_strcmp_ptr(void) { | |
307 | assert_se(strcmp_ptr(NULL, NULL) == 0); | |
308 | assert_se(strcmp_ptr("", NULL) > 0); | |
309 | assert_se(strcmp_ptr("foo", NULL) > 0); | |
310 | assert_se(strcmp_ptr(NULL, "") < 0); | |
311 | assert_se(strcmp_ptr(NULL, "bar") < 0); | |
312 | assert_se(strcmp_ptr("foo", "bar") > 0); | |
313 | assert_se(strcmp_ptr("bar", "baz") < 0); | |
314 | assert_se(strcmp_ptr("foo", "foo") == 0); | |
315 | assert_se(strcmp_ptr("", "") == 0); | |
316 | } | |
317 | ||
318 | static void test_foreach_word(void) { | |
319 | const char *word, *state; | |
320 | size_t l; | |
321 | int i = 0; | |
322 | const char test[] = "test abc d\te f "; | |
323 | const char * const expected[] = { | |
324 | "test", | |
325 | "abc", | |
326 | "d", | |
327 | "e", | |
328 | "f", | |
329 | "", | |
330 | NULL | |
331 | }; | |
332 | ||
333 | FOREACH_WORD(word, l, test, state) | |
334 | assert_se(strneq(expected[i++], word, l)); | |
335 | } | |
336 | ||
337 | static void check(const char *test, char** expected, bool trailing) { | |
bc8ec170 | 338 | int i = 0, r; |
6571a64e RC |
339 | |
340 | printf("<<<%s>>>\n", test); | |
bc8ec170 ZJS |
341 | for (;;) { |
342 | _cleanup_free_ char *word = NULL; | |
343 | ||
4ec85141 | 344 | r = extract_first_word(&test, &word, NULL, EXTRACT_UNQUOTE); |
bc8ec170 ZJS |
345 | if (r == 0) { |
346 | assert_se(!trailing); | |
347 | break; | |
348 | } else if (r < 0) { | |
349 | assert_se(trailing); | |
350 | break; | |
351 | } | |
352 | ||
353 | assert_se(streq(word, expected[i++])); | |
354 | printf("<%s>\n", word); | |
6571a64e | 355 | } |
6571a64e | 356 | assert_se(expected[i] == NULL); |
6571a64e RC |
357 | } |
358 | ||
359 | static void test_foreach_word_quoted(void) { | |
360 | check("test a b c 'd' e '' '' hhh '' '' \"a b c\"", | |
361 | STRV_MAKE("test", | |
362 | "a", | |
363 | "b", | |
364 | "c", | |
365 | "d", | |
366 | "e", | |
367 | "", | |
368 | "", | |
369 | "hhh", | |
370 | "", | |
371 | "", | |
372 | "a b c"), | |
373 | false); | |
374 | ||
375 | check("test \"xxx", | |
376 | STRV_MAKE("test"), | |
377 | true); | |
378 | ||
379 | check("test\\", | |
380 | STRV_MAKE_EMPTY, | |
381 | true); | |
382 | } | |
383 | ||
384 | static void test_endswith(void) { | |
385 | assert_se(endswith("foobar", "bar")); | |
386 | assert_se(endswith("foobar", "")); | |
387 | assert_se(endswith("foobar", "foobar")); | |
388 | assert_se(endswith("", "")); | |
389 | ||
390 | assert_se(!endswith("foobar", "foo")); | |
391 | assert_se(!endswith("foobar", "foobarfoofoo")); | |
392 | } | |
393 | ||
394 | static void test_endswith_no_case(void) { | |
395 | assert_se(endswith_no_case("fooBAR", "bar")); | |
396 | assert_se(endswith_no_case("foobar", "")); | |
397 | assert_se(endswith_no_case("foobar", "FOOBAR")); | |
398 | assert_se(endswith_no_case("", "")); | |
399 | ||
400 | assert_se(!endswith_no_case("foobar", "FOO")); | |
401 | assert_se(!endswith_no_case("foobar", "FOOBARFOOFOO")); | |
402 | } | |
403 | ||
404 | static void test_delete_chars(void) { | |
7546145e LP |
405 | char *s, input[] = " hello, waldo. abc"; |
406 | ||
407 | s = delete_chars(input, WHITESPACE); | |
408 | assert_se(streq(s, "hello,waldo.abc")); | |
409 | assert_se(s == input); | |
410 | } | |
411 | ||
412 | static void test_delete_trailing_chars(void) { | |
413 | ||
414 | char *s, | |
415 | input1[] = " \n \r k \n \r ", | |
416 | input2[] = "kkkkthiskkkiskkkaktestkkk", | |
417 | input3[] = "abcdef"; | |
418 | ||
419 | s = delete_trailing_chars(input1, WHITESPACE); | |
420 | assert_se(streq(s, " \n \r k")); | |
421 | assert_se(s == input1); | |
422 | ||
423 | s = delete_trailing_chars(input2, "kt"); | |
424 | assert_se(streq(s, "kkkkthiskkkiskkkaktes")); | |
425 | assert_se(s == input2); | |
426 | ||
427 | s = delete_trailing_chars(input3, WHITESPACE); | |
428 | assert_se(streq(s, "abcdef")); | |
429 | assert_se(s == input3); | |
430 | ||
431 | s = delete_trailing_chars(input3, "fe"); | |
432 | assert_se(streq(s, "abcd")); | |
433 | assert_se(s == input3); | |
434 | } | |
435 | ||
ca4d708d ZJS |
436 | static void test_delete_trailing_slashes(void) { |
437 | char s1[] = "foobar//", | |
438 | s2[] = "foobar/", | |
439 | s3[] = "foobar", | |
440 | s4[] = ""; | |
441 | ||
442 | assert_se(streq(delete_trailing_chars(s1, "_"), "foobar//")); | |
443 | assert_se(streq(delete_trailing_chars(s1, "/"), "foobar")); | |
444 | assert_se(streq(delete_trailing_chars(s2, "/"), "foobar")); | |
445 | assert_se(streq(delete_trailing_chars(s3, "/"), "foobar")); | |
446 | assert_se(streq(delete_trailing_chars(s4, "/"), "")); | |
447 | } | |
448 | ||
7546145e LP |
449 | static void test_skip_leading_chars(void) { |
450 | char input1[] = " \n \r k \n \r ", | |
451 | input2[] = "kkkkthiskkkiskkkaktestkkk", | |
452 | input3[] = "abcdef"; | |
6571a64e | 453 | |
7546145e LP |
454 | assert_se(streq(skip_leading_chars(input1, WHITESPACE), "k \n \r ")); |
455 | assert_se(streq(skip_leading_chars(input2, "k"), "thiskkkiskkkaktestkkk")); | |
456 | assert_se(streq(skip_leading_chars(input2, "tk"), "hiskkkiskkkaktestkkk")); | |
457 | assert_se(streq(skip_leading_chars(input3, WHITESPACE), "abcdef")); | |
458 | assert_se(streq(skip_leading_chars(input3, "bcaef"), "def")); | |
6571a64e RC |
459 | } |
460 | ||
461 | static void test_in_charset(void) { | |
462 | assert_se(in_charset("dddaaabbbcccc", "abcd")); | |
463 | assert_se(!in_charset("dddaaabbbcccc", "abc f")); | |
464 | } | |
465 | ||
466 | static void test_split_pair(void) { | |
467 | _cleanup_free_ char *a = NULL, *b = NULL; | |
468 | ||
469 | assert_se(split_pair("", "", &a, &b) == -EINVAL); | |
470 | assert_se(split_pair("foo=bar", "", &a, &b) == -EINVAL); | |
471 | assert_se(split_pair("", "=", &a, &b) == -EINVAL); | |
472 | assert_se(split_pair("foo=bar", "=", &a, &b) >= 0); | |
473 | assert_se(streq(a, "foo")); | |
474 | assert_se(streq(b, "bar")); | |
475 | free(a); | |
476 | free(b); | |
477 | assert_se(split_pair("==", "==", &a, &b) >= 0); | |
478 | assert_se(streq(a, "")); | |
479 | assert_se(streq(b, "")); | |
480 | free(a); | |
481 | free(b); | |
482 | ||
483 | assert_se(split_pair("===", "==", &a, &b) >= 0); | |
484 | assert_se(streq(a, "")); | |
485 | assert_se(streq(b, "=")); | |
486 | } | |
487 | ||
488 | static void test_first_word(void) { | |
489 | assert_se(first_word("Hello", "")); | |
490 | assert_se(first_word("Hello", "Hello")); | |
491 | assert_se(first_word("Hello world", "Hello")); | |
492 | assert_se(first_word("Hello\tworld", "Hello")); | |
493 | assert_se(first_word("Hello\nworld", "Hello")); | |
494 | assert_se(first_word("Hello\rworld", "Hello")); | |
495 | assert_se(first_word("Hello ", "Hello")); | |
496 | ||
497 | assert_se(!first_word("Hello", "Hellooo")); | |
498 | assert_se(!first_word("Hello", "xxxxx")); | |
499 | assert_se(!first_word("Hellooo", "Hello")); | |
500 | } | |
501 | ||
7bf7ce28 LP |
502 | static void test_strlen_ptr(void) { |
503 | assert_se(strlen_ptr("foo") == 3); | |
504 | assert_se(strlen_ptr("") == 0); | |
505 | assert_se(strlen_ptr(NULL) == 0); | |
506 | } | |
507 | ||
9b8ff183 LP |
508 | static void test_memory_startswith(void) { |
509 | assert_se(streq(memory_startswith("", 0, ""), "")); | |
510 | assert_se(streq(memory_startswith("", 1, ""), "")); | |
511 | assert_se(streq(memory_startswith("x", 2, ""), "x")); | |
512 | assert_se(!memory_startswith("", 1, "x")); | |
513 | assert_se(!memory_startswith("", 1, "xxxxxxxx")); | |
514 | assert_se(streq(memory_startswith("xxx", 4, "x"), "xx")); | |
515 | assert_se(streq(memory_startswith("xxx", 4, "xx"), "x")); | |
516 | assert_se(streq(memory_startswith("xxx", 4, "xxx"), "")); | |
517 | assert_se(!memory_startswith("xxx", 4, "xxxx")); | |
518 | } | |
519 | ||
21224070 DP |
520 | static void test_memory_startswith_no_case(void) { |
521 | assert_se(streq(memory_startswith_no_case("", 0, ""), "")); | |
522 | assert_se(streq(memory_startswith_no_case("", 1, ""), "")); | |
523 | assert_se(streq(memory_startswith_no_case("x", 2, ""), "x")); | |
524 | assert_se(streq(memory_startswith_no_case("X", 2, ""), "X")); | |
525 | assert_se(!memory_startswith_no_case("", 1, "X")); | |
526 | assert_se(!memory_startswith_no_case("", 1, "xxxxXXXX")); | |
527 | assert_se(streq(memory_startswith_no_case("xxx", 4, "X"), "xx")); | |
528 | assert_se(streq(memory_startswith_no_case("XXX", 4, "x"), "XX")); | |
529 | assert_se(streq(memory_startswith_no_case("XXX", 4, "X"), "XX")); | |
530 | assert_se(streq(memory_startswith_no_case("xxx", 4, "XX"), "x")); | |
531 | assert_se(streq(memory_startswith_no_case("XXX", 4, "xx"), "X")); | |
532 | assert_se(streq(memory_startswith_no_case("XXX", 4, "XX"), "X")); | |
533 | assert_se(streq(memory_startswith_no_case("xxx", 4, "XXX"), "")); | |
534 | assert_se(streq(memory_startswith_no_case("XXX", 4, "xxx"), "")); | |
535 | assert_se(streq(memory_startswith_no_case("XXX", 4, "XXX"), "")); | |
536 | ||
537 | assert_se(memory_startswith_no_case((char[2]){'x', 'x'}, 2, "xx")); | |
538 | assert_se(memory_startswith_no_case((char[2]){'x', 'X'}, 2, "xX")); | |
539 | assert_se(memory_startswith_no_case((char[2]){'X', 'x'}, 2, "Xx")); | |
540 | assert_se(memory_startswith_no_case((char[2]){'X', 'X'}, 2, "XX")); | |
541 | } | |
542 | ||
9fe4ea21 | 543 | int main(int argc, char *argv[]) { |
7f546026 ZJS |
544 | test_setup_logging(LOG_DEBUG); |
545 | ||
7f546026 | 546 | test_free_and_strndup(); |
522d85ae | 547 | test_ascii_strcasecmp_n(); |
c1749834 | 548 | test_ascii_strcasecmp_nn(); |
8409f688 | 549 | test_cellescape(); |
6571a64e RC |
550 | test_streq_ptr(); |
551 | test_strstrip(); | |
552 | test_strextend(); | |
bb8ad9ea | 553 | test_strextend_with_separator(); |
6571a64e | 554 | test_strrep(); |
6571a64e RC |
555 | test_string_has_cc(); |
556 | test_ascii_strlower(); | |
557 | test_strshorten(); | |
558 | test_strjoina(); | |
559 | test_strcmp_ptr(); | |
560 | test_foreach_word(); | |
561 | test_foreach_word_quoted(); | |
562 | test_endswith(); | |
563 | test_endswith_no_case(); | |
564 | test_delete_chars(); | |
7546145e | 565 | test_delete_trailing_chars(); |
ca4d708d | 566 | test_delete_trailing_slashes(); |
7546145e | 567 | test_skip_leading_chars(); |
6571a64e RC |
568 | test_in_charset(); |
569 | test_split_pair(); | |
570 | test_first_word(); | |
7bf7ce28 | 571 | test_strlen_ptr(); |
9b8ff183 | 572 | test_memory_startswith(); |
21224070 | 573 | test_memory_startswith_no_case(); |
6571a64e | 574 | |
9fe4ea21 LP |
575 | return 0; |
576 | } |