2 Unix SMB/CIFS implementation.
6 Copyright (C) Jelmer Vernooij 2005
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2009
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "torture/torture.h"
26 struct test_list_element
{
27 const char *list_as_string
;
28 const char *seperators
;
32 struct test_list_element test_lists_strings
[] = {
38 .list_as_string
= "foo",
39 .list
= { "foo", NULL
}
42 .list_as_string
= "foo bar",
43 .list
= { "foo", "bar", NULL
}
46 .list_as_string
= "foo bar",
47 .list
= { "foo bar", NULL
},
51 .list_as_string
= "\"foo bar\"",
52 .list
= { "\"foo", "bar\"", NULL
}
55 .list_as_string
= "\"foo bar\",comma\ttab",
56 .list
= { "\"foo", "bar\"", "comma", "tab", NULL
}
59 .list_as_string
= "\"foo bar\",comma;semicolon",
60 .list
= { "\"foo bar\",comma", "semicolon", NULL
},
65 struct test_list_element test_lists_shell_strings
[] = {
71 .list_as_string
= "foo",
72 .list
= { "foo", NULL
}
75 .list_as_string
= "foo bar",
76 .list
= { "foo", "bar", NULL
}
79 .list_as_string
= "foo bar",
80 .list
= { "foo bar", NULL
},
84 .list_as_string
= "\"foo bar\"",
85 .list
= { "foo bar", NULL
}
88 .list_as_string
= "foo bar \"bla \"",
89 .list
= { "foo", "bar", "bla ", NULL
}
92 .list_as_string
= "foo \"\" bla",
93 .list
= { "foo", "", "bla", NULL
},
96 .list_as_string
= "bla \"\"\"\" blie",
97 .list
= { "bla", "", "", "blie", NULL
},
101 static bool test_lists_shell(struct torture_context
*tctx
,
104 const struct test_list_element
*element
= data
;
105 const char **ret1
, **ret2
, *tmp
;
107 TALLOC_CTX
*mem_ctx
= tctx
;
109 ret1
= str_list_make_shell(mem_ctx
, element
->list_as_string
, element
->seperators
);
111 torture_assert(tctx
, ret1
, "str_list_make_shell() must not return NULL");
112 tmp
= str_list_join_shell(mem_ctx
, ret1
, element
->seperators
? *element
->seperators
: ' ');
113 ret2
= str_list_make_shell(mem_ctx
, tmp
, element
->seperators
);
115 if ((ret1
== NULL
|| ret2
== NULL
) && ret2
!= ret1
) {
119 for (j
= 0; ret1
[j
] && ret2
[j
]; j
++) {
120 if (strcmp(ret1
[j
], ret2
[j
]) != 0) {
126 if (ret1
[j
] || ret2
[j
])
130 torture_assert(tctx
, match
, talloc_asprintf(tctx
,
131 "str_list_{make,join}_shell: Error double parsing, first run:\n%s\nSecond run: \n%s", element
->list_as_string
, tmp
));
132 torture_assert(tctx
, str_list_equal(ret1
, element
->list
),
133 talloc_asprintf(tctx
,
134 "str_list_make_shell(%s) failed to create correct list",
135 element
->list_as_string
));
140 static bool test_list_make(struct torture_context
*tctx
, const void *data
)
142 const struct test_list_element
*element
= data
;
144 result
= str_list_make(tctx
, element
->list_as_string
, element
->seperators
);
145 torture_assert(tctx
, result
, "str_list_make() must not return NULL");
146 torture_assert(tctx
, str_list_equal((const char **)result
, element
->list
),
147 talloc_asprintf(tctx
,
148 "str_list_make(%s) failed to create correct list",
149 element
->list_as_string
));
153 static bool test_list_copy(struct torture_context
*tctx
)
156 const char *list
[] = { "foo", "bar", NULL
};
157 const char *empty_list
[] = { NULL
};
158 const char **null_list
= NULL
;
160 result
= (const char **)str_list_copy(tctx
, list
);
161 torture_assert_int_equal(tctx
, str_list_length(result
), 2, "list length");
162 torture_assert_str_equal(tctx
, result
[0], "foo", "element 0");
163 torture_assert_str_equal(tctx
, result
[1], "bar", "element 1");
164 torture_assert_str_equal(tctx
, result
[2], NULL
, "element 2");
166 result
= (const char **)str_list_copy(tctx
, empty_list
);
167 torture_assert_int_equal(tctx
, str_list_length(result
), 0, "list length");
168 torture_assert_str_equal(tctx
, result
[0], NULL
, "element 0");
170 result
= (const char **)str_list_copy(tctx
, null_list
);
171 torture_assert(tctx
, result
== NULL
, "result NULL");
176 static bool test_list_make_empty(struct torture_context
*tctx
)
180 result
= str_list_make_empty(tctx
);
181 torture_assert(tctx
, result
, "str_list_make_empty() must not return NULL");
182 torture_assert(tctx
, result
[0] == NULL
, "first element in str_list_make_empty() result must be NULL");
184 result
= str_list_make(tctx
, NULL
, NULL
);
185 torture_assert(tctx
, result
, "str_list_make() must not return NULL");
186 torture_assert(tctx
, result
[0] == NULL
, "first element in str_list_make(ctx, NULL, NULL) result must be NULL");
188 result
= str_list_make(tctx
, "", NULL
);
189 torture_assert(tctx
, result
, "str_list_make() must not return NULL");
190 torture_assert(tctx
, result
[0] == NULL
, "first element in str_list_make(ctx, "", NULL) result must be NULL");
195 static bool test_list_make_single(struct torture_context
*tctx
)
199 result
= str_list_make_single(tctx
, "foo");
201 torture_assert(tctx
, result
, "str_list_make_single() must not return NULL");
202 torture_assert_str_equal(tctx
, result
[0], "foo", "element 0");
203 torture_assert(tctx
, result
[1] == NULL
, "second element in result must be NULL");
208 static bool test_list_copy_const(struct torture_context
*tctx
)
211 const char *list
[] = {
218 result
= str_list_copy_const(tctx
, list
);
219 torture_assert(tctx
, result
, "str_list_copy() must not return NULL");
220 torture_assert(tctx
, str_list_equal(result
, list
),
221 "str_list_copy() failed");
226 static bool test_list_length(struct torture_context
*tctx
)
228 const char *list
[] = {
235 const char *list2
[] = {
238 torture_assert_int_equal(tctx
, str_list_length(list
), 4,
239 "str_list_length() failed");
241 torture_assert_int_equal(tctx
, str_list_length(list2
), 0,
242 "str_list_length() failed");
244 torture_assert_int_equal(tctx
, str_list_length(NULL
), 0,
245 "str_list_length() failed");
250 static bool test_list_add(struct torture_context
*tctx
)
252 char **result
, **result2
;
253 const char *list
[] = {
260 result
= str_list_make(tctx
, "element_0, element_1, element_2", NULL
);
261 torture_assert(tctx
, result
, "str_list_make() must not return NULL");
262 result2
= str_list_add(result
, "element_3");
263 torture_assert(tctx
, result2
, "str_list_add() must not return NULL");
264 torture_assert(tctx
, str_list_equal(result2
, list
),
265 "str_list_add() failed");
270 static bool test_list_add_const(struct torture_context
*tctx
)
272 char **result
, **result2
;
273 const char *list
[] = {
280 result
= str_list_make(tctx
, "element_0, element_1, element_2", NULL
);
281 torture_assert(tctx
, result
, "str_list_make() must not return NULL");
282 result2
= str_list_add_const(result
, "element_3");
283 torture_assert(tctx
, result2
, "str_list_add_const() must not return NULL");
284 torture_assert(tctx
, str_list_equal(result2
, list
),
285 "str_list_add() failed");
290 static bool test_list_remove(struct torture_context
*tctx
)
293 const char *list
[] = {
299 result
= str_list_make(tctx
, "element_0, element_1, element_2, element_3", NULL
);
300 torture_assert(tctx
, result
, "str_list_make() must not return NULL");
301 str_list_remove(result
, "element_2");
302 torture_assert(tctx
, str_list_equal(result
, list
),
303 "str_list_remove() failed");
308 static bool test_list_check(struct torture_context
*tctx
)
310 const char *list
[] = {
316 torture_assert(tctx
, str_list_check(list
, "element_1"),
317 "str_list_check() failed");
322 static bool test_list_check_ci(struct torture_context
*tctx
)
324 const char *list
[] = {
330 torture_assert(tctx
, str_list_check_ci(list
, "ELEMENT_1"),
331 "str_list_check_ci() failed");
336 static bool test_list_unique(struct torture_context
*tctx
)
339 const char *list
[] = {
345 const char *list_dup
[] = {
356 result
= str_list_copy(tctx
, list_dup
);
357 /* We must copy the list, as str_list_unique does a talloc_realloc() on it's parameter */
358 result
= str_list_unique(result
);
359 torture_assert(tctx
, result
, "str_list_unique() must not return NULL");
361 torture_assert(tctx
, str_list_equal(list
, result
),
362 "str_list_unique() failed");
367 static bool test_list_append(struct torture_context
*tctx
)
370 const char *list
[] = {
376 const char *list2
[] = {
382 const char *list_combined
[] = {
391 result
= str_list_copy(tctx
, list
);
392 torture_assert(tctx
, result
, "str_list_copy() must not return NULL");
393 result
= str_list_append(result
, list2
);
394 torture_assert(tctx
, result
, "str_list_append() must not return NULL");
395 torture_assert(tctx
, str_list_equal(list_combined
, result
),
396 "str_list_unique() failed");
401 static bool test_list_append_const(struct torture_context
*tctx
)
404 const char *list
[] = {
410 const char *list2
[] = {
416 const char *list_combined
[] = {
425 result
= str_list_copy(tctx
, list
);
426 torture_assert(tctx
, result
, "str_list_copy() must not return NULL");
427 result
= str_list_append_const(result
, list2
);
428 torture_assert(tctx
, result
, "str_list_append_const() must not return NULL");
429 torture_assert(tctx
, str_list_equal(list_combined
, result
),
430 "str_list_unique() failed");
435 struct torture_suite
*torture_local_util_strlist(TALLOC_CTX
*mem_ctx
)
437 struct torture_suite
*suite
= torture_suite_create(mem_ctx
, "STRLIST");
440 for (i
= 0; i
< ARRAY_SIZE(test_lists_shell_strings
); i
++) {
441 torture_suite_add_simple_tcase_const(suite
, "lists_shell",
442 test_lists_shell
, &test_lists_shell_strings
[i
]);
445 for (i
= 0; i
< ARRAY_SIZE(test_lists_strings
); i
++) {
446 torture_suite_add_simple_tcase_const(suite
, "lists",
447 test_list_make
, &test_lists_strings
[i
]);
450 torture_suite_add_simple_test(suite
, "list_copy", test_list_copy
);
451 torture_suite_add_simple_test(suite
, "make_empty", test_list_make_empty
);
452 torture_suite_add_simple_test(suite
, "make_single", test_list_make_single
);
453 torture_suite_add_simple_test(suite
, "list_copy_const", test_list_copy_const
);
454 torture_suite_add_simple_test(suite
, "list_length", test_list_length
);
455 torture_suite_add_simple_test(suite
, "list_add", test_list_add
);
456 torture_suite_add_simple_test(suite
, "list_add_const", test_list_add_const
);
457 torture_suite_add_simple_test(suite
, "list_remove", test_list_remove
);
458 torture_suite_add_simple_test(suite
, "list_check", test_list_check
);
459 torture_suite_add_simple_test(suite
, "list_check_ci", test_list_check_ci
);
460 torture_suite_add_simple_test(suite
, "list_unique", test_list_unique
);
461 torture_suite_add_simple_test(suite
, "list_append", test_list_append
);
462 torture_suite_add_simple_test(suite
, "list_append_const", test_list_append_const
);