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"
25 #include "torture/local/proto.h"
26 #include "param/param.h"
28 struct test_list_element
{
29 const char *list_as_string
;
30 const char *separators
;
34 const struct test_list_element test_lists_strings
[] = {
40 .list_as_string
= "foo",
41 .list
= { "foo", NULL
}
44 .list_as_string
= "foo bar",
45 .list
= { "foo", "bar", NULL
}
48 .list_as_string
= "foo bar",
49 .list
= { "foo bar", NULL
},
53 .list_as_string
= "\"foo bar\"",
54 .list
= { "\"foo", "bar\"", NULL
}
57 .list_as_string
= "\"foo bar\",comma\ttab",
58 .list
= { "\"foo", "bar\"", "comma", "tab", NULL
}
61 .list_as_string
= "\"foo bar\",comma;semicolon",
62 .list
= { "\"foo bar\",comma", "semicolon", NULL
},
67 const struct test_list_element test_lists_shell_strings
[] = {
73 .list_as_string
= "foo",
74 .list
= { "foo", NULL
}
77 .list_as_string
= "foo bar",
78 .list
= { "foo", "bar", NULL
}
81 .list_as_string
= "foo bar",
82 .list
= { "foo bar", NULL
},
86 .list_as_string
= "\"foo bar\"",
87 .list
= { "foo bar", NULL
}
90 .list_as_string
= "foo bar \"bla \"",
91 .list
= { "foo", "bar", "bla ", NULL
}
94 .list_as_string
= "foo \"\" bla",
95 .list
= { "foo", "", "bla", NULL
},
98 .list_as_string
= "bla \"\"\"\" blie",
99 .list
= { "bla", "", "", "blie", NULL
},
103 static bool test_lists_shell(struct torture_context
*tctx
, const void *data
)
105 const struct test_list_element
*element
= data
;
107 char **ret1
, **ret2
, *tmp
;
109 TALLOC_CTX
*mem_ctx
= tctx
;
111 ret1
= str_list_make_shell(mem_ctx
, element
->list_as_string
, element
->separators
);
113 torture_assert(tctx
, ret1
, "str_list_make_shell() must not return NULL");
114 tmp
= str_list_join_shell(mem_ctx
, discard_const_p(const char *, ret1
),
115 element
->separators
? *element
->separators
: ' ');
116 ret2
= str_list_make_shell(mem_ctx
, tmp
, element
->separators
);
118 if ((ret1
== NULL
|| ret2
== NULL
) && ret2
!= ret1
) {
122 for (j
= 0; ret1
[j
] && ret2
[j
]; j
++) {
123 if (strcmp(ret1
[j
], ret2
[j
]) != 0) {
129 if (ret1
[j
] || ret2
[j
])
133 torture_assert(tctx
, match
, talloc_asprintf(tctx
,
134 "str_list_{make,join}_shell: Error double parsing, first run:\n%s\nSecond run: \n%s", element
->list_as_string
, tmp
));
135 torture_assert(tctx
, str_list_equal((const char * const *) ret1
,
137 talloc_asprintf(tctx
,
138 "str_list_make_shell(%s) failed to create correct list",
139 element
->list_as_string
));
144 static bool test_list_make(struct torture_context
*tctx
, const void *data
)
146 const struct test_list_element
*element
= data
;
149 result
= str_list_make(tctx
, element
->list_as_string
, element
->separators
);
150 torture_assert(tctx
, result
, "str_list_make() must not return NULL");
151 torture_assert(tctx
, str_list_equal((const char * const *) result
,
153 talloc_asprintf(tctx
,
154 "str_list_make(%s) failed to create correct list",
155 element
->list_as_string
));
159 static bool test_list_copy(struct torture_context
*tctx
)
162 const char *list
[] = { "foo", "bar", NULL
};
163 const char *empty_list
[] = { NULL
};
164 const char **null_list
= NULL
;
167 l
= str_list_copy(tctx
, list
);
168 result
= discard_const_p(const char *, l
);
169 torture_assert_int_equal(tctx
, str_list_length(result
), 2, "list length");
170 torture_assert_str_equal(tctx
, result
[0], "foo", "element 0");
171 torture_assert_str_equal(tctx
, result
[1], "bar", "element 1");
172 torture_assert_str_equal(tctx
, result
[2], NULL
, "element 2");
174 l
= str_list_copy(tctx
, empty_list
);
175 result
= discard_const_p(const char *, l
);
176 torture_assert_int_equal(tctx
, str_list_length(result
), 0, "list length");
177 torture_assert_str_equal(tctx
, result
[0], NULL
, "element 0");
179 l
= str_list_copy(tctx
, null_list
);
180 result
= discard_const_p(const char *, l
);
181 torture_assert(tctx
, result
== NULL
, "result NULL");
186 static bool test_list_make_empty(struct torture_context
*tctx
)
190 result
= str_list_make_empty(tctx
);
191 torture_assert(tctx
, result
, "str_list_make_empty() must not return NULL");
192 torture_assert(tctx
, result
[0] == NULL
, "first element in str_list_make_empty() result must be NULL");
194 result
= str_list_make(tctx
, NULL
, NULL
);
195 torture_assert(tctx
, result
, "str_list_make() must not return NULL");
196 torture_assert(tctx
, result
[0] == NULL
, "first element in str_list_make(ctx, NULL, NULL) result must be NULL");
198 result
= str_list_make(tctx
, "", NULL
);
199 torture_assert(tctx
, result
, "str_list_make() must not return NULL");
200 torture_assert(tctx
, result
[0] == NULL
, "first element in str_list_make(ctx, "", NULL) result must be NULL");
205 static bool test_list_make_single(struct torture_context
*tctx
)
209 result
= str_list_make_single(tctx
, "foo");
211 torture_assert(tctx
, result
, "str_list_make_single() must not return NULL");
212 torture_assert_str_equal(tctx
, result
[0], "foo", "element 0");
213 torture_assert(tctx
, result
[1] == NULL
, "second element in result must be NULL");
218 static bool test_list_copy_const(struct torture_context
*tctx
)
221 const char *list
[] = {
228 result
= str_list_copy_const(tctx
, list
);
229 torture_assert(tctx
, result
, "str_list_copy() must not return NULL");
230 torture_assert(tctx
, str_list_equal(result
, list
),
231 "str_list_copy() failed");
236 static bool test_list_length(struct torture_context
*tctx
)
238 const char *list
[] = {
245 const char *list2
[] = {
248 torture_assert_int_equal(tctx
, str_list_length(list
), 4,
249 "str_list_length() failed");
251 torture_assert_int_equal(tctx
, str_list_length(list2
), 0,
252 "str_list_length() failed");
254 torture_assert_int_equal(tctx
, str_list_length(NULL
), 0,
255 "str_list_length() failed");
260 static bool test_list_add(struct torture_context
*tctx
)
262 const char **result
, **result2
;
263 const char *list
[] = {
272 l
= str_list_make(tctx
, "element_0, element_1, element_2", NULL
);
273 result
= discard_const_p(const char *, l
);
274 torture_assert(tctx
, result
, "str_list_make() must not return NULL");
275 result2
= str_list_add(result
, "element_3");
276 torture_assert(tctx
, result2
, "str_list_add() must not return NULL");
277 torture_assert(tctx
, str_list_equal(result2
, list
),
278 "str_list_add() failed");
283 static bool test_list_add_const(struct torture_context
*tctx
)
285 const char **result
, **result2
;
286 const char *list
[] = {
295 l
= str_list_make(tctx
, "element_0, element_1, element_2", NULL
);
296 result
= discard_const_p(const char *, l
);
297 torture_assert(tctx
, result
, "str_list_make() must not return NULL");
298 result2
= str_list_add_const(result
, "element_3");
299 torture_assert(tctx
, result2
, "str_list_add_const() must not return NULL");
300 torture_assert(tctx
, str_list_equal(result2
, list
),
301 "str_list_add() failed");
306 static bool test_list_remove(struct torture_context
*tctx
)
309 const char *list
[] = {
317 l
= str_list_make(tctx
, "element_0, element_1, element_2, element_3", NULL
);
318 result
= discard_const_p(const char *, l
);
319 torture_assert(tctx
, result
, "str_list_make() must not return NULL");
320 str_list_remove(result
, "element_2");
321 torture_assert(tctx
, str_list_equal(result
, list
),
322 "str_list_remove() failed");
327 static bool test_list_check(struct torture_context
*tctx
)
329 const char *list
[] = {
335 torture_assert(tctx
, str_list_check(list
, "element_1"),
336 "str_list_check() failed");
341 static bool test_list_check_ci(struct torture_context
*tctx
)
343 const char *list
[] = {
349 torture_assert(tctx
, str_list_check_ci(list
, "ELEMENT_1"),
350 "str_list_check_ci() failed");
355 static bool test_list_unique(struct torture_context
*tctx
)
358 const char *list
[] = {
364 const char *list_dup
[] = {
377 l
= str_list_copy(tctx
, list_dup
);
378 result
= discard_const_p(const char *, l
);
379 /* We must copy the list, as str_list_unique does a talloc_realloc() on it's parameter */
380 result
= str_list_unique(result
);
381 torture_assert(tctx
, result
, "str_list_unique() must not return NULL");
383 torture_assert(tctx
, str_list_equal(list
, result
),
384 "str_list_unique() failed");
389 static bool test_list_unique_2(struct torture_context
*tctx
)
394 char **l1
= str_list_make_empty(tctx
);
395 char **l2
= str_list_make_empty(tctx
);
396 const char **list
= discard_const_p(const char *, l1
);
397 const char **list_dup
= discard_const_p(const char *, l2
);
400 count
= lpcfg_parm_int(tctx
->lp_ctx
, NULL
, "list_unique", "count", 9);
401 num_dups
= lpcfg_parm_int(tctx
->lp_ctx
, NULL
, "list_unique", "dups", 7);
402 torture_comment(tctx
, "test_list_unique_2() with %d elements and %d dups\n", count
, num_dups
);
404 for (i
= 0; i
< count
; i
++) {
405 list
= str_list_add_const(list
, (const char *)talloc_asprintf(tctx
, "element_%03d", i
));
408 for (i
= 0; i
< num_dups
; i
++) {
409 list_dup
= str_list_append(list_dup
, list
);
412 l
= str_list_copy(tctx
, list_dup
);
413 result
= discard_const_p(const char *, l
);
414 /* We must copy the list, as str_list_unique does a talloc_realloc() on it's parameter */
415 result
= str_list_unique(result
);
416 torture_assert(tctx
, result
, "str_list_unique() must not return NULL");
418 torture_assert(tctx
, str_list_equal(list
, result
),
419 "str_list_unique() failed");
424 static bool test_list_append(struct torture_context
*tctx
)
427 const char *list
[] = {
433 const char *list2
[] = {
439 const char *list_combined
[] = {
449 l
= str_list_copy(tctx
, list
);
450 result
= discard_const_p(const char *, l
);
451 torture_assert(tctx
, result
, "str_list_copy() must not return NULL");
452 result
= str_list_append(result
, list2
);
453 torture_assert(tctx
, result
, "str_list_append() must not return NULL");
454 torture_assert(tctx
, str_list_equal(list_combined
, result
),
455 "str_list_unique() failed");
460 static bool test_list_append_const(struct torture_context
*tctx
)
463 const char *list
[] = {
469 const char *list2
[] = {
475 const char *list_combined
[] = {
485 l
= str_list_copy(tctx
, list
);
486 result
= discard_const_p(const char *, l
);
487 torture_assert(tctx
, result
, "str_list_copy() must not return NULL");
488 result
= str_list_append_const(result
, list2
);
489 torture_assert(tctx
, result
, "str_list_append_const() must not return NULL");
490 torture_assert(tctx
, str_list_equal(list_combined
, result
),
491 "str_list_unique() failed");
496 static bool test_list_add_printf_NULL(struct torture_context
*tctx
)
499 str_list_add_printf(&list
, "x=%d", 1);
500 torture_assert(tctx
, list
==NULL
, "str_list_add_printf must keep NULL");
504 static bool test_list_add_printf(struct torture_context
*tctx
)
506 const char *list2
[] = { "foo", "bar=baz", NULL
};
507 char **list
= str_list_make_empty(tctx
);
508 str_list_add_printf(&list
, "foo");
509 str_list_add_printf(&list
, "bar=%s", "baz");
512 str_list_equal((const char * const *)list
, list2
),
513 "str_list_add_printf failed");
518 struct torture_suite
*torture_local_util_strlist(TALLOC_CTX
*mem_ctx
)
520 struct torture_suite
*suite
= torture_suite_create(mem_ctx
, "strlist");
523 for (i
= 0; i
< ARRAY_SIZE(test_lists_shell_strings
); i
++) {
525 name
= talloc_asprintf(suite
, "lists_shell(%s)",
526 test_lists_shell_strings
[i
].list_as_string
);
527 torture_suite_add_simple_tcase_const(suite
, name
,
528 test_lists_shell
, &test_lists_shell_strings
[i
]);
531 for (i
= 0; i
< ARRAY_SIZE(test_lists_strings
); i
++) {
533 name
= talloc_asprintf(suite
, "list_make(%s)",
534 test_lists_strings
[i
].list_as_string
);
535 torture_suite_add_simple_tcase_const(suite
, name
,
536 test_list_make
, &test_lists_strings
[i
]);
539 torture_suite_add_simple_test(suite
, "list_copy", test_list_copy
);
540 torture_suite_add_simple_test(suite
, "make_empty", test_list_make_empty
);
541 torture_suite_add_simple_test(suite
, "make_single", test_list_make_single
);
542 torture_suite_add_simple_test(suite
, "list_copy_const", test_list_copy_const
);
543 torture_suite_add_simple_test(suite
, "list_length", test_list_length
);
544 torture_suite_add_simple_test(suite
, "list_add", test_list_add
);
545 torture_suite_add_simple_test(suite
, "list_add_const", test_list_add_const
);
546 torture_suite_add_simple_test(suite
, "list_remove", test_list_remove
);
547 torture_suite_add_simple_test(suite
, "list_check", test_list_check
);
548 torture_suite_add_simple_test(suite
, "list_check_ci", test_list_check_ci
);
549 torture_suite_add_simple_test(suite
, "list_unique", test_list_unique
);
550 torture_suite_add_simple_test(suite
, "list_unique_2", test_list_unique_2
);
551 torture_suite_add_simple_test(suite
, "list_append", test_list_append
);
552 torture_suite_add_simple_test(suite
, "list_append_const", test_list_append_const
);
553 torture_suite_add_simple_test(
554 suite
, "list_add_printf_NULL", test_list_add_printf_NULL
);
555 torture_suite_add_simple_test(
556 suite
, "list_add_printf", test_list_add_printf
);