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