chiark / gitweb /
Fix service file to match installed elogind binary location
[elogind.git] / src / test / test-string-util.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2015 Lennart Poettering
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include "alloc-util.h"
21 #include "macro.h"
22 #include "string-util.h"
23 #include "strv.h"
24
25 static void test_string_erase(void) {
26         char *x;
27
28         x = strdupa("");
29         assert_se(streq(string_erase(x), ""));
30
31         x = strdupa("1");
32         assert_se(streq(string_erase(x), ""));
33
34         x = strdupa("123456789");
35         assert_se(streq(string_erase(x), ""));
36
37         assert_se(x[1] == '\0');
38         assert_se(x[2] == '\0');
39         assert_se(x[3] == '\0');
40         assert_se(x[4] == '\0');
41         assert_se(x[5] == '\0');
42         assert_se(x[6] == '\0');
43         assert_se(x[7] == '\0');
44         assert_se(x[8] == '\0');
45         assert_se(x[9] == '\0');
46 }
47
48 #if 0 /// UNNEEDED by elogind
49 static void test_ascii_strcasecmp_n(void) {
50
51         assert_se(ascii_strcasecmp_n("", "", 0) == 0);
52         assert_se(ascii_strcasecmp_n("", "", 1) == 0);
53         assert_se(ascii_strcasecmp_n("", "a", 1) < 0);
54         assert_se(ascii_strcasecmp_n("", "a", 2) < 0);
55         assert_se(ascii_strcasecmp_n("a", "", 1) > 0);
56         assert_se(ascii_strcasecmp_n("a", "", 2) > 0);
57         assert_se(ascii_strcasecmp_n("a", "a", 1) == 0);
58         assert_se(ascii_strcasecmp_n("a", "a", 2) == 0);
59         assert_se(ascii_strcasecmp_n("a", "b", 1) < 0);
60         assert_se(ascii_strcasecmp_n("a", "b", 2) < 0);
61         assert_se(ascii_strcasecmp_n("b", "a", 1) > 0);
62         assert_se(ascii_strcasecmp_n("b", "a", 2) > 0);
63         assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxYxxxx", 9) == 0);
64         assert_se(ascii_strcasecmp_n("xxxxxxxxx", "xxxxyxxxx", 9) < 0);
65         assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxyxxxx", 9) < 0);
66         assert_se(ascii_strcasecmp_n("xxxxxxxxx", "xxxxYxxxx", 9) < 0);
67         assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxYxxxx", 9) < 0);
68
69         assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxYxxxx", 9) == 0);
70         assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxxxxxx", 9) > 0);
71         assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxXxxxx", 9) > 0);
72         assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxxxxxx", 9) > 0);
73         assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxXxxxx", 9) > 0);
74 }
75
76 static void test_ascii_strcasecmp_nn(void) {
77         assert_se(ascii_strcasecmp_nn("", 0, "", 0) == 0);
78         assert_se(ascii_strcasecmp_nn("", 0, "", 1) < 0);
79         assert_se(ascii_strcasecmp_nn("", 1, "", 0) > 0);
80         assert_se(ascii_strcasecmp_nn("", 1, "", 1) == 0);
81
82         assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaAa", 4) == 0);
83         assert_se(ascii_strcasecmp_nn("aaa", 3, "aaAa", 4) < 0);
84         assert_se(ascii_strcasecmp_nn("aaa", 4, "aaAa", 4) < 0);
85         assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaA", 3) > 0);
86         assert_se(ascii_strcasecmp_nn("aaaa", 4, "AAA", 4) > 0);
87
88         assert_se(ascii_strcasecmp_nn("aaaa", 4, "bbbb", 4) < 0);
89         assert_se(ascii_strcasecmp_nn("aaAA", 4, "BBbb", 4) < 0);
90         assert_se(ascii_strcasecmp_nn("BBbb", 4, "aaaa", 4) > 0);
91 }
92 #endif // 0
93
94 static void test_streq_ptr(void) {
95         assert_se(streq_ptr(NULL, NULL));
96         assert_se(!streq_ptr("abc", "cdef"));
97 }
98
99 static void test_strstrip(void) {
100         char *r;
101         char input[] = "   hello, waldo.   ";
102
103         r = strstrip(input);
104         assert_se(streq(r, "hello, waldo."));
105 }
106
107 static void test_strextend(void) {
108         _cleanup_free_ char *str = strdup("0123");
109         strextend(&str, "456", "78", "9", NULL);
110         assert_se(streq(str, "0123456789"));
111 }
112
113 static void test_strrep(void) {
114         _cleanup_free_ char *one, *three, *zero;
115         one = strrep("waldo", 1);
116         three = strrep("waldo", 3);
117         zero = strrep("waldo", 0);
118
119         assert_se(streq(one, "waldo"));
120         assert_se(streq(three, "waldowaldowaldo"));
121         assert_se(streq(zero, ""));
122 }
123
124
125 static void test_strappend(void) {
126         _cleanup_free_ char *t1, *t2, *t3, *t4;
127
128         t1 = strappend(NULL, NULL);
129         assert_se(streq(t1, ""));
130
131         t2 = strappend(NULL, "suf");
132         assert_se(streq(t2, "suf"));
133
134         t3 = strappend("pre", NULL);
135         assert_se(streq(t3, "pre"));
136
137         t4 = strappend("pre", "suf");
138         assert_se(streq(t4, "presuf"));
139 }
140
141 static void test_string_has_cc(void) {
142         assert_se(string_has_cc("abc\1", NULL));
143         assert_se(string_has_cc("abc\x7f", NULL));
144         assert_se(string_has_cc("abc\x7f", NULL));
145         assert_se(string_has_cc("abc\t\x7f", "\t"));
146         assert_se(string_has_cc("abc\t\x7f", "\t"));
147         assert_se(string_has_cc("\x7f", "\t"));
148         assert_se(string_has_cc("\x7f", "\t\a"));
149
150         assert_se(!string_has_cc("abc\t\t", "\t"));
151         assert_se(!string_has_cc("abc\t\t\a", "\t\a"));
152         assert_se(!string_has_cc("a\ab\tc", "\t\a"));
153 }
154
155 #if 0 /// UNNEEDED by elogind
156 static void test_ascii_strlower(void) {
157         char a[] = "AabBcC Jk Ii Od LKJJJ kkd LK";
158         assert_se(streq(ascii_strlower(a), "aabbcc jk ii od lkjjj kkd lk"));
159 }
160 #endif // 0
161
162 static void test_strshorten(void) {
163         char s[] = "foobar";
164
165         assert_se(strlen(strshorten(s, 6)) == 6);
166         assert_se(strlen(strshorten(s, 12)) == 6);
167         assert_se(strlen(strshorten(s, 2)) == 2);
168         assert_se(strlen(strshorten(s, 0)) == 0);
169 }
170
171 static void test_strjoina(void) {
172         char *actual;
173
174         actual = strjoina("", "foo", "bar");
175         assert_se(streq(actual, "foobar"));
176
177         actual = strjoina("foo", "bar", "baz");
178         assert_se(streq(actual, "foobarbaz"));
179
180         actual = strjoina("foo", "", "bar", "baz");
181         assert_se(streq(actual, "foobarbaz"));
182
183         actual = strjoina("foo");
184         assert_se(streq(actual, "foo"));
185
186         actual = strjoina(NULL);
187         assert_se(streq(actual, ""));
188
189         actual = strjoina(NULL, "foo");
190         assert_se(streq(actual, ""));
191
192         actual = strjoina("foo", NULL, "bar");
193         assert_se(streq(actual, "foo"));
194 }
195
196 static void test_strcmp_ptr(void) {
197         assert_se(strcmp_ptr(NULL, NULL) == 0);
198         assert_se(strcmp_ptr("", NULL) > 0);
199         assert_se(strcmp_ptr("foo", NULL) > 0);
200         assert_se(strcmp_ptr(NULL, "") < 0);
201         assert_se(strcmp_ptr(NULL, "bar") < 0);
202         assert_se(strcmp_ptr("foo", "bar") > 0);
203         assert_se(strcmp_ptr("bar", "baz") < 0);
204         assert_se(strcmp_ptr("foo", "foo") == 0);
205         assert_se(strcmp_ptr("", "") == 0);
206 }
207
208 static void test_foreach_word(void) {
209         const char *word, *state;
210         size_t l;
211         int i = 0;
212         const char test[] = "test abc d\te   f   ";
213         const char * const expected[] = {
214                 "test",
215                 "abc",
216                 "d",
217                 "e",
218                 "f",
219                 "",
220                 NULL
221         };
222
223         FOREACH_WORD(word, l, test, state)
224                 assert_se(strneq(expected[i++], word, l));
225 }
226
227 static void check(const char *test, char** expected, bool trailing) {
228         int i = 0, r;
229
230         printf("<<<%s>>>\n", test);
231         for (;;) {
232                 _cleanup_free_ char *word = NULL;
233
234                 r = extract_first_word(&test, &word, NULL, EXTRACT_QUOTES);
235                 if (r == 0) {
236                         assert_se(!trailing);
237                         break;
238                 } else if (r < 0) {
239                         assert_se(trailing);
240                         break;
241                 }
242
243                 assert_se(streq(word, expected[i++]));
244                 printf("<%s>\n", word);
245         }
246         assert_se(expected[i] == NULL);
247 }
248
249 static void test_foreach_word_quoted(void) {
250         check("test a b c 'd' e '' '' hhh '' '' \"a b c\"",
251               STRV_MAKE("test",
252                         "a",
253                         "b",
254                         "c",
255                         "d",
256                         "e",
257                         "",
258                         "",
259                         "hhh",
260                         "",
261                         "",
262                         "a b c"),
263               false);
264
265         check("test \"xxx",
266               STRV_MAKE("test"),
267               true);
268
269         check("test\\",
270               STRV_MAKE_EMPTY,
271               true);
272 }
273
274 static void test_endswith(void) {
275         assert_se(endswith("foobar", "bar"));
276         assert_se(endswith("foobar", ""));
277         assert_se(endswith("foobar", "foobar"));
278         assert_se(endswith("", ""));
279
280         assert_se(!endswith("foobar", "foo"));
281         assert_se(!endswith("foobar", "foobarfoofoo"));
282 }
283
284 static void test_endswith_no_case(void) {
285         assert_se(endswith_no_case("fooBAR", "bar"));
286         assert_se(endswith_no_case("foobar", ""));
287         assert_se(endswith_no_case("foobar", "FOOBAR"));
288         assert_se(endswith_no_case("", ""));
289
290         assert_se(!endswith_no_case("foobar", "FOO"));
291         assert_se(!endswith_no_case("foobar", "FOOBARFOOFOO"));
292 }
293
294 #if 0 /// UNNEEDED by elogind
295 static void test_delete_chars(void) {
296         char *r;
297         char input[] = "   hello, waldo.   abc";
298
299         r = delete_chars(input, WHITESPACE);
300         assert_se(streq(r, "hello,waldo.abc"));
301 }
302 #endif // 0
303
304 static void test_in_charset(void) {
305         assert_se(in_charset("dddaaabbbcccc", "abcd"));
306         assert_se(!in_charset("dddaaabbbcccc", "abc f"));
307 }
308
309 static void test_split_pair(void) {
310         _cleanup_free_ char *a = NULL, *b = NULL;
311
312         assert_se(split_pair("", "", &a, &b) == -EINVAL);
313         assert_se(split_pair("foo=bar", "", &a, &b) == -EINVAL);
314         assert_se(split_pair("", "=", &a, &b) == -EINVAL);
315         assert_se(split_pair("foo=bar", "=", &a, &b) >= 0);
316         assert_se(streq(a, "foo"));
317         assert_se(streq(b, "bar"));
318         free(a);
319         free(b);
320         assert_se(split_pair("==", "==", &a, &b) >= 0);
321         assert_se(streq(a, ""));
322         assert_se(streq(b, ""));
323         free(a);
324         free(b);
325
326         assert_se(split_pair("===", "==", &a, &b) >= 0);
327         assert_se(streq(a, ""));
328         assert_se(streq(b, "="));
329 }
330
331 static void test_first_word(void) {
332         assert_se(first_word("Hello", ""));
333         assert_se(first_word("Hello", "Hello"));
334         assert_se(first_word("Hello world", "Hello"));
335         assert_se(first_word("Hello\tworld", "Hello"));
336         assert_se(first_word("Hello\nworld", "Hello"));
337         assert_se(first_word("Hello\rworld", "Hello"));
338         assert_se(first_word("Hello ", "Hello"));
339
340         assert_se(!first_word("Hello", "Hellooo"));
341         assert_se(!first_word("Hello", "xxxxx"));
342         assert_se(!first_word("Hellooo", "Hello"));
343 }
344
345 int main(int argc, char *argv[]) {
346         test_string_erase();
347 #if 0 /// UNNEEDED by elogind
348         test_ascii_strcasecmp_n();
349         test_ascii_strcasecmp_nn();
350 #endif // 0
351         test_streq_ptr();
352         test_strstrip();
353         test_strextend();
354         test_strrep();
355         test_strappend();
356         test_string_has_cc();
357 #if 0 /// UNNEEDED by elogind
358         test_ascii_strlower();
359 #endif // 0
360         test_strshorten();
361         test_strjoina();
362         test_strcmp_ptr();
363         test_foreach_word();
364         test_foreach_word_quoted();
365         test_endswith();
366         test_endswith_no_case();
367 #if 0 /// UNNEEDED by elogind
368         test_delete_chars();
369 #endif // 0
370         test_in_charset();
371         test_split_pair();
372         test_first_word();
373
374         return 0;
375 }