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