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