Merge branch 'master' of ssh://git.samba.org/data/git/samba into regsrv
[kai/samba-autobuild/.git] / lib / util / tests / strlist.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    util_strlist testing
5
6    Copyright (C) Jelmer Vernooij 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "torture/torture.h"
24
25 static const char *test_lists_shell_strings[] = {
26         "",
27         "foo",
28         "foo bar",
29         "foo bar \"bla \"",
30         "foo \"\" bla",
31         "bla \"\"\"\" blie",
32         NULL
33 };
34
35 static bool test_lists_shell(struct torture_context *tctx,
36                                                          const void *test_data)
37 {
38         const char *data = (const char *)test_data;
39         const char **ret1, **ret2, *tmp;
40         bool match = true;
41         TALLOC_CTX *mem_ctx = tctx;
42
43         ret1 = str_list_make_shell(mem_ctx, data, " ");
44         tmp = str_list_join_shell(mem_ctx, ret1, ' ');
45         ret2 = str_list_make_shell(mem_ctx, tmp, " ");
46
47         if ((ret1 == NULL || ret2 == NULL) && ret2 != ret1) {
48                 match = false;
49         } else {
50                 int j;
51                 for (j = 0; ret1[j] && ret2[j]; j++) {
52                         if (strcmp(ret1[j], ret2[j]) != 0) {
53                                 match = false;
54                                 break;
55                         }
56                 }
57
58                 if (ret1[j] || ret2[j])
59                         match = false;
60         }
61
62         torture_assert(tctx, match, talloc_asprintf(tctx, 
63                 "str_list_{make,join}_shell: Error double parsing, first run:\n%s\nSecond run: \n%s", data, tmp));
64         return true;
65 }
66
67 static bool test_list_copy(struct torture_context *tctx)
68 {
69         const char **result;
70         const char *list[] = { "foo", "bar", NULL };
71         const char *empty_list[] = { NULL };
72         const char **null_list = NULL;
73
74         result = (const char **)str_list_copy(tctx, list);
75         torture_assert_int_equal(tctx, str_list_length(result), 2, "list length");
76         torture_assert_str_equal(tctx, result[0], "foo", "element 0");
77         torture_assert_str_equal(tctx, result[1], "bar", "element 1");
78         torture_assert_str_equal(tctx, result[2], NULL, "element 2");
79
80         result = (const char **)str_list_copy(tctx, empty_list);
81         torture_assert_int_equal(tctx, str_list_length(result), 0, "list length");
82         torture_assert_str_equal(tctx, result[0], NULL, "element 0");
83
84         result = (const char **)str_list_copy(tctx, null_list);
85         torture_assert(tctx, result == NULL, "result NULL");
86         
87         return true;
88 }
89
90 struct torture_suite *torture_local_util_strlist(TALLOC_CTX *mem_ctx)
91 {
92         struct torture_suite *suite = torture_suite_create(mem_ctx, "STRLIST");
93         int i;
94
95         for (i = 0; test_lists_shell_strings[i]; i++) {
96                 torture_suite_add_simple_tcase_const(suite, "lists_shell",
97                                 test_lists_shell, &test_lists_shell_strings[i]);
98         }
99
100         torture_suite_add_simple_test(suite, "list_copy", test_list_copy);
101
102         return suite;
103 }