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