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