]>
Commit | Line | Data |
---|---|---|
f90cf44c LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
539ad707 | 7 | Copyright 2013 Thomas H.P. Andersen |
f90cf44c LP |
8 | |
9 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
10 | under the terms of the GNU Lesser General Public License as published by |
11 | the Free Software Foundation; either version 2.1 of the License, or | |
f90cf44c LP |
12 | (at your option) any later version. |
13 | ||
14 | systemd is distributed in the hope that it will be useful, but | |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 17 | Lesser General Public License for more details. |
f90cf44c | 18 | |
5430f7f2 | 19 | You should have received a copy of the GNU Lesser General Public License |
f90cf44c LP |
20 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
21 | ***/ | |
22 | ||
23 | #include <string.h> | |
2c4b304e | 24 | |
f90cf44c | 25 | #include "util.h" |
2c4b304e | 26 | #include "specifier.h" |
682cfdff | 27 | #include "strv.h" |
f90cf44c | 28 | |
3a7719d3 | 29 | static void test_specifier_printf(void) { |
9f316366 | 30 | _cleanup_free_ char *w = NULL; |
3a7719d3 | 31 | |
2c4b304e LP |
32 | const Specifier table[] = { |
33 | { 'a', specifier_string, (char*) "AAAA" }, | |
34 | { 'b', specifier_string, (char*) "BBBB" }, | |
35 | { 0, NULL, NULL } | |
36 | }; | |
37 | ||
3a7719d3 | 38 | w = specifier_printf("xxx a=%a b=%b yyy", table, NULL); |
9f316366 DB |
39 | puts(w); |
40 | ||
41 | assert_se(w); | |
42 | assert_se(streq(w, "xxx a=AAAA b=BBBB yyy")); | |
3a7719d3 DB |
43 | } |
44 | ||
a6fde353 ZJS |
45 | static const char* const input_table_multiple[] = { |
46 | "one", | |
47 | "two", | |
48 | "three", | |
49 | NULL, | |
50 | }; | |
51 | ||
52 | static const char* const input_table_one[] = { | |
53 | "one", | |
54 | NULL, | |
55 | }; | |
56 | ||
57 | static const char* const input_table_none[] = { | |
58 | NULL, | |
59 | }; | |
60 | ||
61 | static const char* const input_table_quotes[] = { | |
62 | "\"", | |
63 | "'", | |
64 | "\"\"", | |
65 | "\\", | |
66 | "\\\\", | |
67 | NULL, | |
68 | }; | |
69 | #define QUOTES_STRING \ | |
70 | "\"\\\"\" " \ | |
71 | "\"\\\'\" " \ | |
72 | "\"\\\"\\\"\" " \ | |
73 | "\"\\\\\" " \ | |
74 | "\"\\\\\\\\\"" | |
75 | ||
76 | static const char * const input_table_spaces[] = { | |
77 | " ", | |
78 | "' '", | |
79 | "\" ", | |
80 | " \"", | |
81 | " \\\\ ", | |
82 | NULL, | |
83 | }; | |
84 | #define SPACES_STRING \ | |
85 | "\" \" " \ | |
86 | "\"\\' \\'\" " \ | |
87 | "\"\\\" \" " \ | |
88 | "\" \\\"\" " \ | |
89 | "\" \\\\\\\\ \"" | |
539ad707 | 90 | |
a6fde353 ZJS |
91 | static void test_strv_find(void) { |
92 | assert_se(strv_find((char **)input_table_multiple, "three")); | |
93 | assert_se(!strv_find((char **)input_table_multiple, "four")); | |
539ad707 TA |
94 | } |
95 | ||
96 | static void test_strv_find_prefix(void) { | |
a6fde353 ZJS |
97 | assert_se(strv_find_prefix((char **)input_table_multiple, "o")); |
98 | assert_se(strv_find_prefix((char **)input_table_multiple, "one")); | |
99 | assert_se(strv_find_prefix((char **)input_table_multiple, "")); | |
100 | assert_se(!strv_find_prefix((char **)input_table_multiple, "xxx")); | |
101 | assert_se(!strv_find_prefix((char **)input_table_multiple, "onee")); | |
3a7719d3 | 102 | } |
f90cf44c | 103 | |
682cfdff | 104 | static void test_strv_join(void) { |
539ad707 | 105 | _cleanup_free_ char *p = NULL, *q = NULL, *r = NULL, *s = NULL, *t = NULL; |
682cfdff | 106 | |
539ad707 | 107 | p = strv_join((char **)input_table_multiple, ", "); |
04045d84 | 108 | assert_se(p); |
7b68d618 | 109 | assert_se(streq(p, "one, two, three")); |
682cfdff | 110 | |
539ad707 | 111 | q = strv_join((char **)input_table_multiple, ";"); |
04045d84 | 112 | assert_se(q); |
7b68d618 | 113 | assert_se(streq(q, "one;two;three")); |
682cfdff DB |
114 | |
115 | r = strv_join((char **)input_table_multiple, NULL); | |
04045d84 | 116 | assert_se(r); |
7b68d618 | 117 | assert_se(streq(r, "one two three")); |
539ad707 TA |
118 | |
119 | s = strv_join((char **)input_table_one, ", "); | |
04045d84 | 120 | assert_se(s); |
7b68d618 | 121 | assert_se(streq(s, "one")); |
539ad707 TA |
122 | |
123 | t = strv_join((char **)input_table_none, ", "); | |
04045d84 | 124 | assert_se(t); |
7b68d618 | 125 | assert_se(streq(t, "")); |
682cfdff DB |
126 | } |
127 | ||
a6fde353 ZJS |
128 | static void test_strv_quote_unquote(const char* const *split, const char *quoted) { |
129 | _cleanup_free_ char *p; | |
130 | _cleanup_strv_free_ char **s; | |
131 | char **t; | |
132 | ||
133 | p = strv_join_quoted((char **)split); | |
134 | printf("-%s- --- -%s-\n", p, quoted); /* fprintf deals with NULL, puts does not */ | |
135 | assert_se(p); | |
136 | assert_se(streq(p, quoted)); | |
137 | ||
138 | s = strv_split_quoted(quoted); | |
139 | assert_se(s); | |
140 | STRV_FOREACH(t, s) { | |
141 | assert_se(*t); | |
142 | assert_se(streq(*t, *split)); | |
143 | split++; | |
144 | } | |
145 | } | |
146 | ||
2f213f74 DB |
147 | static void test_strv_split_nulstr(void) { |
148 | _cleanup_strv_free_ char **l = NULL; | |
149 | const char nulstr[] = "str0\0str1\0str2\0str3\0"; | |
150 | ||
151 | l = strv_split_nulstr (nulstr); | |
04045d84 | 152 | assert_se(l); |
2f213f74 DB |
153 | |
154 | assert_se(streq(l[0], "str0")); | |
155 | assert_se(streq(l[1], "str1")); | |
156 | assert_se(streq(l[2], "str2")); | |
157 | assert_se(streq(l[3], "str3")); | |
158 | } | |
159 | ||
10ddd913 TA |
160 | static void test_strv_parse_nulstr(void) { |
161 | _cleanup_strv_free_ char **l = NULL; | |
162 | const char nulstr[] = "fuck\0fuck2\0fuck3\0\0fuck5\0\0xxx"; | |
163 | ||
164 | l = strv_parse_nulstr(nulstr, sizeof(nulstr)-1); | |
04045d84 | 165 | assert_se(l); |
10ddd913 TA |
166 | puts("Parse nulstr:"); |
167 | strv_print(l); | |
168 | ||
7b68d618 DB |
169 | assert_se(streq(l[0], "fuck")); |
170 | assert_se(streq(l[1], "fuck2")); | |
171 | assert_se(streq(l[2], "fuck3")); | |
172 | assert_se(streq(l[3], "")); | |
173 | assert_se(streq(l[4], "fuck5")); | |
174 | assert_se(streq(l[5], "")); | |
175 | assert_se(streq(l[6], "xxx")); | |
10ddd913 TA |
176 | } |
177 | ||
539ad707 TA |
178 | static void test_strv_overlap(void) { |
179 | const char * const input_table[] = { | |
180 | "one", | |
181 | "two", | |
182 | "three", | |
183 | NULL | |
184 | }; | |
185 | const char * const input_table_overlap[] = { | |
186 | "two", | |
187 | NULL | |
188 | }; | |
189 | const char * const input_table_unique[] = { | |
190 | "four", | |
191 | "five", | |
192 | "six", | |
193 | NULL | |
194 | }; | |
195 | ||
7b68d618 DB |
196 | assert_se(strv_overlap((char **)input_table, (char**)input_table_overlap)); |
197 | assert_se(!strv_overlap((char **)input_table, (char**)input_table_unique)); | |
539ad707 TA |
198 | } |
199 | ||
200 | static void test_strv_sort(void) { | |
1e64bbc1 | 201 | const char* input_table[] = { |
539ad707 TA |
202 | "durian", |
203 | "apple", | |
204 | "citrus", | |
205 | "CAPITAL LETTERS FIRST", | |
206 | "banana", | |
207 | NULL | |
208 | }; | |
209 | ||
210 | strv_sort((char **)input_table); | |
211 | ||
7b68d618 DB |
212 | assert_se(streq(input_table[0], "CAPITAL LETTERS FIRST")); |
213 | assert_se(streq(input_table[1], "apple")); | |
214 | assert_se(streq(input_table[2], "banana")); | |
215 | assert_se(streq(input_table[3], "citrus")); | |
216 | assert_se(streq(input_table[4], "durian")); | |
3a7719d3 | 217 | } |
e3aa71c3 | 218 | |
343a8969 | 219 | static void test_strv_merge_concat(void) { |
7b68d618 | 220 | _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL; |
343a8969 | 221 | |
7b68d618 DB |
222 | a = strv_new("without", "suffix", NULL); |
223 | b = strv_new("with", "suffix", NULL); | |
04045d84 DB |
224 | assert_se(a); |
225 | assert_se(b); | |
343a8969 | 226 | |
7b68d618 | 227 | c = strv_merge_concat(a, b, "_suffix"); |
04045d84 | 228 | assert_se(c); |
343a8969 | 229 | |
7b68d618 DB |
230 | assert_se(streq(c[0], "without")); |
231 | assert_se(streq(c[1], "suffix")); | |
232 | assert_se(streq(c[2], "with_suffix")); | |
233 | assert_se(streq(c[3], "suffix_suffix")); | |
343a8969 DB |
234 | } |
235 | ||
a1022300 DB |
236 | static void test_strv_merge(void) { |
237 | _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL; | |
238 | ||
7b68d618 DB |
239 | a = strv_new("abc", "def", "ghi", NULL); |
240 | b = strv_new("jkl", "mno", "pqr", NULL); | |
04045d84 DB |
241 | assert_se(a); |
242 | assert_se(b); | |
a1022300 | 243 | |
7b68d618 | 244 | c = strv_merge(a, b); |
04045d84 | 245 | assert_se(c); |
a1022300 | 246 | |
7b68d618 DB |
247 | assert_se(streq(c[0], "abc")); |
248 | assert_se(streq(c[1], "def")); | |
249 | assert_se(streq(c[2], "ghi")); | |
250 | assert_se(streq(c[3], "jkl")); | |
251 | assert_se(streq(c[4], "mno")); | |
252 | assert_se(streq(c[5], "pqr")); | |
a1022300 | 253 | |
7b68d618 | 254 | assert_se(strv_length(c) == 6); |
a1022300 DB |
255 | } |
256 | ||
40857008 DB |
257 | static void test_strv_append(void) { |
258 | _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL; | |
259 | ||
260 | a = strv_new("test", "test1", NULL); | |
04045d84 | 261 | assert_se(a); |
40857008 DB |
262 | b = strv_append(a, "test2"); |
263 | c = strv_append(NULL, "test3"); | |
04045d84 DB |
264 | assert_se(b); |
265 | assert_se(c); | |
40857008 | 266 | |
7b68d618 DB |
267 | assert_se(streq(b[0], "test")); |
268 | assert_se(streq(b[1], "test1")); | |
269 | assert_se(streq(b[2], "test2")); | |
270 | assert_se(streq(c[0], "test3")); | |
40857008 DB |
271 | } |
272 | ||
4c325b2e DB |
273 | static void test_strv_foreach_pair(void) { |
274 | _cleanup_strv_free_ char **a = NULL; | |
275 | char **x, **y; | |
276 | ||
277 | a = strv_new("pair_one", "pair_one", | |
278 | "pair_two", "pair_two", | |
279 | "pair_three", "pair_three", | |
280 | NULL); | |
281 | ||
282 | STRV_FOREACH_PAIR(x, y, a) { | |
283 | assert_se(streq(*x, *y)); | |
284 | } | |
285 | } | |
286 | ||
3a7719d3 | 287 | int main(int argc, char *argv[]) { |
3a7719d3 | 288 | test_specifier_printf(); |
4c325b2e | 289 | test_strv_foreach_pair(); |
539ad707 TA |
290 | test_strv_find(); |
291 | test_strv_find_prefix(); | |
682cfdff | 292 | test_strv_join(); |
a6fde353 ZJS |
293 | |
294 | test_strv_quote_unquote(input_table_multiple, "\"one\" \"two\" \"three\""); | |
295 | test_strv_quote_unquote(input_table_one, "\"one\""); | |
296 | test_strv_quote_unquote(input_table_none, ""); | |
297 | test_strv_quote_unquote(input_table_quotes, QUOTES_STRING); | |
298 | test_strv_quote_unquote(input_table_spaces, SPACES_STRING); | |
299 | ||
2f213f74 | 300 | test_strv_split_nulstr(); |
10ddd913 | 301 | test_strv_parse_nulstr(); |
539ad707 TA |
302 | test_strv_overlap(); |
303 | test_strv_sort(); | |
a1022300 | 304 | test_strv_merge(); |
343a8969 | 305 | test_strv_merge_concat(); |
40857008 | 306 | test_strv_append(); |
2c4b304e | 307 | |
f90cf44c LP |
308 | return 0; |
309 | } |