chiark / gitweb /
basic/string-util: add a convenience function to cescape mostly-ascii fields
[elogind.git] / src / test / test-string-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   This file is part of systemd.
4
5   Copyright 2015 Lennart Poettering
6 ***/
7
8 #include "alloc-util.h"
9 //#include "locale-util.h"
10 #include "macro.h"
11 #include "string-util.h"
12 #include "strv.h"
13
14 static void test_string_erase(void) {
15         char *x;
16
17         x = strdupa("");
18         assert_se(streq(string_erase(x), ""));
19
20         x = strdupa("1");
21         assert_se(streq(string_erase(x), ""));
22
23         x = strdupa("123456789");
24         assert_se(streq(string_erase(x), ""));
25
26         assert_se(x[1] == '\0');
27         assert_se(x[2] == '\0');
28         assert_se(x[3] == '\0');
29         assert_se(x[4] == '\0');
30         assert_se(x[5] == '\0');
31         assert_se(x[6] == '\0');
32         assert_se(x[7] == '\0');
33         assert_se(x[8] == '\0');
34         assert_se(x[9] == '\0');
35 }
36
37 #if 0 /// UNNEEDED by elogind
38 static void test_ascii_strcasecmp_n(void) {
39
40         assert_se(ascii_strcasecmp_n("", "", 0) == 0);
41         assert_se(ascii_strcasecmp_n("", "", 1) == 0);
42         assert_se(ascii_strcasecmp_n("", "a", 1) < 0);
43         assert_se(ascii_strcasecmp_n("", "a", 2) < 0);
44         assert_se(ascii_strcasecmp_n("a", "", 1) > 0);
45         assert_se(ascii_strcasecmp_n("a", "", 2) > 0);
46         assert_se(ascii_strcasecmp_n("a", "a", 1) == 0);
47         assert_se(ascii_strcasecmp_n("a", "a", 2) == 0);
48         assert_se(ascii_strcasecmp_n("a", "b", 1) < 0);
49         assert_se(ascii_strcasecmp_n("a", "b", 2) < 0);
50         assert_se(ascii_strcasecmp_n("b", "a", 1) > 0);
51         assert_se(ascii_strcasecmp_n("b", "a", 2) > 0);
52         assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxYxxxx", 9) == 0);
53         assert_se(ascii_strcasecmp_n("xxxxxxxxx", "xxxxyxxxx", 9) < 0);
54         assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxyxxxx", 9) < 0);
55         assert_se(ascii_strcasecmp_n("xxxxxxxxx", "xxxxYxxxx", 9) < 0);
56         assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxYxxxx", 9) < 0);
57
58         assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxYxxxx", 9) == 0);
59         assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxxxxxx", 9) > 0);
60         assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxXxxxx", 9) > 0);
61         assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxxxxxx", 9) > 0);
62         assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxXxxxx", 9) > 0);
63 }
64
65 static void test_ascii_strcasecmp_nn(void) {
66         assert_se(ascii_strcasecmp_nn("", 0, "", 0) == 0);
67         assert_se(ascii_strcasecmp_nn("", 0, "", 1) < 0);
68         assert_se(ascii_strcasecmp_nn("", 1, "", 0) > 0);
69         assert_se(ascii_strcasecmp_nn("", 1, "", 1) == 0);
70
71         assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaAa", 4) == 0);
72         assert_se(ascii_strcasecmp_nn("aaa", 3, "aaAa", 4) < 0);
73         assert_se(ascii_strcasecmp_nn("aaa", 4, "aaAa", 4) < 0);
74         assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaA", 3) > 0);
75         assert_se(ascii_strcasecmp_nn("aaaa", 4, "AAA", 4) > 0);
76
77         assert_se(ascii_strcasecmp_nn("aaaa", 4, "bbbb", 4) < 0);
78         assert_se(ascii_strcasecmp_nn("aaAA", 4, "BBbb", 4) < 0);
79         assert_se(ascii_strcasecmp_nn("BBbb", 4, "aaaa", 4) > 0);
80 }
81 #endif // 0
82
83 static void test_cellescape(void) {
84         char buf[40];
85
86         assert_se(streq(cellescape(buf, 10, "1"), "1"));
87         assert_se(streq(cellescape(buf, 10, "12"), "12"));
88         assert_se(streq(cellescape(buf, 10, "123"), is_locale_utf8() ? "1…" : "1..."));
89
90         assert_se(streq(cellescape(buf, 10, "1\011"), "1\\t"));
91         assert_se(streq(cellescape(buf, 10, "1\020"), "1\\020"));
92         assert_se(streq(cellescape(buf, 10, "1\020x"), is_locale_utf8() ? "1…" : "1..."));
93
94         assert_se(streq(cellescape(buf, 40, "1\020"), "1\\020"));
95         assert_se(streq(cellescape(buf, 40, "1\020x"), "1\\020x"));
96
97         assert_se(streq(cellescape(buf, 40, "\a\b\f\n\r\t\v\\\"'"), "\\a\\b\\f\\n\\r\\t\\v\\\\\\\"\\'"));
98         assert_se(streq(cellescape(buf, 10, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a…" : "\\a..."));
99         assert_se(streq(cellescape(buf, 11, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a…" : "\\a..."));
100         assert_se(streq(cellescape(buf, 12, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a\\b…" : "\\a\\b..."));
101
102         assert_se(streq(cellescape(buf, sizeof buf, "1\020"), "1\\020"));
103         assert_se(streq(cellescape(buf, sizeof buf, "1\020x"), "1\\020x"));
104 }
105
106 static void test_streq_ptr(void) {
107         assert_se(streq_ptr(NULL, NULL));
108         assert_se(!streq_ptr("abc", "cdef"));
109 }
110
111 static void test_strstrip(void) {
112         char *r;
113         char input[] = "   hello, waldo.   ";
114
115         r = strstrip(input);
116         assert_se(streq(r, "hello, waldo."));
117 }
118
119 static void test_strextend(void) {
120         _cleanup_free_ char *str = NULL;
121
122         assert_se(strextend(&str, NULL));
123         assert_se(streq_ptr(str, ""));
124         assert_se(strextend(&str, "", "0", "", "", "123", NULL));
125         assert_se(streq_ptr(str, "0123"));
126         assert_se(strextend(&str, "456", "78", "9", NULL));
127         assert_se(streq_ptr(str, "0123456789"));
128 }
129
130 static void test_strextend_with_separator(void) {
131         _cleanup_free_ char *str = NULL;
132
133         assert_se(strextend_with_separator(&str, NULL, NULL));
134         assert_se(streq_ptr(str, ""));
135         str = mfree(str);
136
137         assert_se(strextend_with_separator(&str, "...", NULL));
138         assert_se(streq_ptr(str, ""));
139         assert_se(strextend_with_separator(&str, "...", NULL));
140         assert_se(streq_ptr(str, ""));
141         str = mfree(str);
142
143         assert_se(strextend_with_separator(&str, "xyz", "a", "bb", "ccc", NULL));
144         assert_se(streq_ptr(str, "axyzbbxyzccc"));
145         str = mfree(str);
146
147         assert_se(strextend_with_separator(&str, ",", "start", "", "1", "234", NULL));
148         assert_se(streq_ptr(str, "start,,1,234"));
149         assert_se(strextend_with_separator(&str, ";", "more", "5", "678", NULL));
150         assert_se(streq_ptr(str, "start,,1,234;more;5;678"));
151 }
152
153 static void test_strrep(void) {
154         _cleanup_free_ char *one, *three, *zero;
155         one = strrep("waldo", 1);
156         three = strrep("waldo", 3);
157         zero = strrep("waldo", 0);
158
159         assert_se(streq(one, "waldo"));
160         assert_se(streq(three, "waldowaldowaldo"));
161         assert_se(streq(zero, ""));
162 }
163
164 static void test_strappend(void) {
165         _cleanup_free_ char *t1, *t2, *t3, *t4;
166
167         t1 = strappend(NULL, NULL);
168         assert_se(streq(t1, ""));
169
170         t2 = strappend(NULL, "suf");
171         assert_se(streq(t2, "suf"));
172
173         t3 = strappend("pre", NULL);
174         assert_se(streq(t3, "pre"));
175
176         t4 = strappend("pre", "suf");
177         assert_se(streq(t4, "presuf"));
178 }
179
180 static void test_string_has_cc(void) {
181         assert_se(string_has_cc("abc\1", NULL));
182         assert_se(string_has_cc("abc\x7f", NULL));
183         assert_se(string_has_cc("abc\x7f", NULL));
184         assert_se(string_has_cc("abc\t\x7f", "\t"));
185         assert_se(string_has_cc("abc\t\x7f", "\t"));
186         assert_se(string_has_cc("\x7f", "\t"));
187         assert_se(string_has_cc("\x7f", "\t\a"));
188
189         assert_se(!string_has_cc("abc\t\t", "\t"));
190         assert_se(!string_has_cc("abc\t\t\a", "\t\a"));
191         assert_se(!string_has_cc("a\ab\tc", "\t\a"));
192 }
193
194 #if 0 /// UNNEEDED by elogind
195 static void test_ascii_strlower(void) {
196         char a[] = "AabBcC Jk Ii Od LKJJJ kkd LK";
197         assert_se(streq(ascii_strlower(a), "aabbcc jk ii od lkjjj kkd lk"));
198 }
199 #endif // 0
200
201 static void test_strshorten(void) {
202         char s[] = "foobar";
203
204         assert_se(strlen(strshorten(s, 6)) == 6);
205         assert_se(strlen(strshorten(s, 12)) == 6);
206         assert_se(strlen(strshorten(s, 2)) == 2);
207         assert_se(strlen(strshorten(s, 0)) == 0);
208 }
209
210 static void test_strjoina(void) {
211         char *actual;
212
213         actual = strjoina("", "foo", "bar");
214         assert_se(streq(actual, "foobar"));
215
216         actual = strjoina("foo", "bar", "baz");
217         assert_se(streq(actual, "foobarbaz"));
218
219         actual = strjoina("foo", "", "bar", "baz");
220         assert_se(streq(actual, "foobarbaz"));
221
222         actual = strjoina("foo");
223         assert_se(streq(actual, "foo"));
224
225         actual = strjoina(NULL);
226         assert_se(streq(actual, ""));
227
228         actual = strjoina(NULL, "foo");
229         assert_se(streq(actual, ""));
230
231         actual = strjoina("foo", NULL, "bar");
232         assert_se(streq(actual, "foo"));
233 }
234
235 static void test_strcmp_ptr(void) {
236         assert_se(strcmp_ptr(NULL, NULL) == 0);
237         assert_se(strcmp_ptr("", NULL) > 0);
238         assert_se(strcmp_ptr("foo", NULL) > 0);
239         assert_se(strcmp_ptr(NULL, "") < 0);
240         assert_se(strcmp_ptr(NULL, "bar") < 0);
241         assert_se(strcmp_ptr("foo", "bar") > 0);
242         assert_se(strcmp_ptr("bar", "baz") < 0);
243         assert_se(strcmp_ptr("foo", "foo") == 0);
244         assert_se(strcmp_ptr("", "") == 0);
245 }
246
247 static void test_foreach_word(void) {
248         const char *word, *state;
249         size_t l;
250         int i = 0;
251         const char test[] = "test abc d\te   f   ";
252         const char * const expected[] = {
253                 "test",
254                 "abc",
255                 "d",
256                 "e",
257                 "f",
258                 "",
259                 NULL
260         };
261
262         FOREACH_WORD(word, l, test, state)
263                 assert_se(strneq(expected[i++], word, l));
264 }
265
266 static void check(const char *test, char** expected, bool trailing) {
267         int i = 0, r;
268
269         printf("<<<%s>>>\n", test);
270         for (;;) {
271                 _cleanup_free_ char *word = NULL;
272
273                 r = extract_first_word(&test, &word, NULL, EXTRACT_QUOTES);
274                 if (r == 0) {
275                         assert_se(!trailing);
276                         break;
277                 } else if (r < 0) {
278                         assert_se(trailing);
279                         break;
280                 }
281
282                 assert_se(streq(word, expected[i++]));
283                 printf("<%s>\n", word);
284         }
285         assert_se(expected[i] == NULL);
286 }
287
288 static void test_foreach_word_quoted(void) {
289         check("test a b c 'd' e '' '' hhh '' '' \"a b c\"",
290               STRV_MAKE("test",
291                         "a",
292                         "b",
293                         "c",
294                         "d",
295                         "e",
296                         "",
297                         "",
298                         "hhh",
299                         "",
300                         "",
301                         "a b c"),
302               false);
303
304         check("test \"xxx",
305               STRV_MAKE("test"),
306               true);
307
308         check("test\\",
309               STRV_MAKE_EMPTY,
310               true);
311 }
312
313 static void test_endswith(void) {
314         assert_se(endswith("foobar", "bar"));
315         assert_se(endswith("foobar", ""));
316         assert_se(endswith("foobar", "foobar"));
317         assert_se(endswith("", ""));
318
319         assert_se(!endswith("foobar", "foo"));
320         assert_se(!endswith("foobar", "foobarfoofoo"));
321 }
322
323 static void test_endswith_no_case(void) {
324         assert_se(endswith_no_case("fooBAR", "bar"));
325         assert_se(endswith_no_case("foobar", ""));
326         assert_se(endswith_no_case("foobar", "FOOBAR"));
327         assert_se(endswith_no_case("", ""));
328
329         assert_se(!endswith_no_case("foobar", "FOO"));
330         assert_se(!endswith_no_case("foobar", "FOOBARFOOFOO"));
331 }
332
333 #if 0 /// UNNEEDED by elogind
334 static void test_delete_chars(void) {
335         char *s, input[] = "   hello, waldo.   abc";
336
337         s = delete_chars(input, WHITESPACE);
338         assert_se(streq(s, "hello,waldo.abc"));
339         assert_se(s == input);
340 }
341 #endif // 0
342
343 static void test_delete_trailing_chars(void) {
344
345         char *s,
346                 input1[] = " \n \r k \n \r ",
347                 input2[] = "kkkkthiskkkiskkkaktestkkk",
348                 input3[] = "abcdef";
349
350         s = delete_trailing_chars(input1, WHITESPACE);
351         assert_se(streq(s, " \n \r k"));
352         assert_se(s == input1);
353
354         s = delete_trailing_chars(input2, "kt");
355         assert_se(streq(s, "kkkkthiskkkiskkkaktes"));
356         assert_se(s == input2);
357
358         s = delete_trailing_chars(input3, WHITESPACE);
359         assert_se(streq(s, "abcdef"));
360         assert_se(s == input3);
361
362         s = delete_trailing_chars(input3, "fe");
363         assert_se(streq(s, "abcd"));
364         assert_se(s == input3);
365 }
366
367 static void test_delete_trailing_slashes(void) {
368         char s1[] = "foobar//",
369              s2[] = "foobar/",
370              s3[] = "foobar",
371              s4[] = "";
372
373         assert_se(streq(delete_trailing_chars(s1, "_"), "foobar//"));
374         assert_se(streq(delete_trailing_chars(s1, "/"), "foobar"));
375         assert_se(streq(delete_trailing_chars(s2, "/"), "foobar"));
376         assert_se(streq(delete_trailing_chars(s3, "/"), "foobar"));
377         assert_se(streq(delete_trailing_chars(s4, "/"), ""));
378 }
379
380 #if 0 /// UNNEEDED by elogind
381 static void test_skip_leading_chars(void) {
382         char input1[] = " \n \r k \n \r ",
383                 input2[] = "kkkkthiskkkiskkkaktestkkk",
384                 input3[] = "abcdef";
385
386         assert_se(streq(skip_leading_chars(input1, WHITESPACE), "k \n \r "));
387         assert_se(streq(skip_leading_chars(input2, "k"), "thiskkkiskkkaktestkkk"));
388         assert_se(streq(skip_leading_chars(input2, "tk"), "hiskkkiskkkaktestkkk"));
389         assert_se(streq(skip_leading_chars(input3, WHITESPACE), "abcdef"));
390         assert_se(streq(skip_leading_chars(input3, "bcaef"), "def"));
391 }
392 #endif // 0
393
394 static void test_in_charset(void) {
395         assert_se(in_charset("dddaaabbbcccc", "abcd"));
396         assert_se(!in_charset("dddaaabbbcccc", "abc f"));
397 }
398
399 static void test_split_pair(void) {
400         _cleanup_free_ char *a = NULL, *b = NULL;
401
402         assert_se(split_pair("", "", &a, &b) == -EINVAL);
403         assert_se(split_pair("foo=bar", "", &a, &b) == -EINVAL);
404         assert_se(split_pair("", "=", &a, &b) == -EINVAL);
405         assert_se(split_pair("foo=bar", "=", &a, &b) >= 0);
406         assert_se(streq(a, "foo"));
407         assert_se(streq(b, "bar"));
408         free(a);
409         free(b);
410         assert_se(split_pair("==", "==", &a, &b) >= 0);
411         assert_se(streq(a, ""));
412         assert_se(streq(b, ""));
413         free(a);
414         free(b);
415
416         assert_se(split_pair("===", "==", &a, &b) >= 0);
417         assert_se(streq(a, ""));
418         assert_se(streq(b, "="));
419 }
420
421 static void test_first_word(void) {
422         assert_se(first_word("Hello", ""));
423         assert_se(first_word("Hello", "Hello"));
424         assert_se(first_word("Hello world", "Hello"));
425         assert_se(first_word("Hello\tworld", "Hello"));
426         assert_se(first_word("Hello\nworld", "Hello"));
427         assert_se(first_word("Hello\rworld", "Hello"));
428         assert_se(first_word("Hello ", "Hello"));
429
430         assert_se(!first_word("Hello", "Hellooo"));
431         assert_se(!first_word("Hello", "xxxxx"));
432         assert_se(!first_word("Hellooo", "Hello"));
433 }
434
435 static void test_strlen_ptr(void) {
436         assert_se(strlen_ptr("foo") == 3);
437         assert_se(strlen_ptr("") == 0);
438         assert_se(strlen_ptr(NULL) == 0);
439 }
440
441 static void test_memory_startswith(void) {
442         assert_se(streq(memory_startswith("", 0, ""), ""));
443         assert_se(streq(memory_startswith("", 1, ""), ""));
444         assert_se(streq(memory_startswith("x", 2, ""), "x"));
445         assert_se(!memory_startswith("", 1, "x"));
446         assert_se(!memory_startswith("", 1, "xxxxxxxx"));
447         assert_se(streq(memory_startswith("xxx", 4, "x"), "xx"));
448         assert_se(streq(memory_startswith("xxx", 4, "xx"), "x"));
449         assert_se(streq(memory_startswith("xxx", 4, "xxx"), ""));
450         assert_se(!memory_startswith("xxx", 4, "xxxx"));
451 }
452
453 int main(int argc, char *argv[]) {
454         test_string_erase();
455 #if 0 /// UNNEEDED by elogind
456         test_ascii_strcasecmp_n();
457         test_ascii_strcasecmp_nn();
458 #endif // 0
459         test_cellescape();
460         test_streq_ptr();
461         test_strstrip();
462         test_strextend();
463         test_strextend_with_separator();
464         test_strrep();
465         test_strappend();
466         test_string_has_cc();
467 #if 0 /// UNNEEDED by elogind
468         test_ascii_strlower();
469 #endif // 0
470         test_strshorten();
471         test_strjoina();
472         test_strcmp_ptr();
473         test_foreach_word();
474         test_foreach_word_quoted();
475         test_endswith();
476         test_endswith_no_case();
477 #if 0 /// UNNEEDED by elogind
478         test_delete_chars();
479 #endif // 0
480         test_delete_trailing_chars();
481         test_delete_trailing_slashes();
482 #if 0 /// UNNEEDED by elogind
483         test_skip_leading_chars();
484 #endif // 0
485         test_in_charset();
486         test_split_pair();
487         test_first_word();
488         test_strlen_ptr();
489         test_memory_startswith();
490
491         return 0;
492 }