Line data Source code
1 : /*
2 : * Tests for strv
3 : *
4 : * Copyright Martin Schwenke <martin@meltin.net> 2016
5 : *
6 : * This program is free software; you can redistribute it and/or modify
7 : * it under the terms of the GNU General Public License as published by
8 : * the Free Software Foundation; either version 3 of the License, or
9 : * (at your option) any later version.
10 : *
11 : * This program is distributed in the hope that it will be useful,
12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : * GNU General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU General Public License
17 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 : */
19 :
20 :
21 : #include <talloc.h>
22 :
23 : #include "replace.h"
24 :
25 : #include "libcli/util/ntstatus.h"
26 : #include "torture/torture.h"
27 : #include "lib/util/data_blob.h"
28 : #include "torture/local/proto.h"
29 :
30 : #include "lib/util/strv.h"
31 :
32 0 : static bool test_strv_empty(struct torture_context *tctx)
33 : {
34 : /* NULL strv contains 0 entries */
35 0 : torture_assert_int_equal(tctx,
36 : strv_count(NULL),
37 : 0,
38 : "strv_count() on NULL failed");
39 :
40 : /* NULL strv has no next entry */
41 0 : torture_assert(tctx,
42 : strv_next(NULL, NULL) == NULL,
43 : "strv_next() on NULL failed");
44 :
45 0 : return true;
46 : }
47 :
48 0 : static bool test_strv_single(struct torture_context *tctx)
49 : {
50 0 : const char *data = "foo";
51 0 : char *strv = NULL;
52 : char *t;
53 : int ret;
54 :
55 : /* Add an item */
56 0 : ret = strv_add(tctx, &strv, data);
57 0 : torture_assert(tctx, ret == 0, "strv_add() failed");
58 :
59 : /* Is there 1 item? */
60 0 : torture_assert_int_equal(tctx,
61 : strv_count(strv), 1,
62 : "strv_count() failed");
63 :
64 : /* Is the expected item the first one? */
65 0 : t = strv_next(strv, NULL);
66 0 : torture_assert(tctx,
67 : strcmp(t, data) == 0,
68 : "strv_next() failed");
69 :
70 : /* Can the expected item be found? */
71 0 : t = strv_find(strv, data);
72 0 : torture_assert(tctx,
73 : strcmp(t, data) == 0,
74 : "strv_next() failed");
75 :
76 : /* Delete it */
77 0 : strv_delete(&strv, t);
78 :
79 : /* Should have no items */
80 0 : torture_assert_int_equal(tctx,
81 : strv_count(strv), 0,
82 : "strv_count() failed");
83 0 : return true;
84 : }
85 :
86 0 : static bool test_strv_multi(struct torture_context *tctx)
87 : {
88 0 : const char *data[] = { "foo", "bar", "", "samba", "x"};
89 0 : char *strv = NULL;
90 : char *t;
91 : int i, ret;
92 0 : const int num = ARRAY_SIZE(data);
93 :
94 : /* Add items */
95 0 : for (i = 0; i < num; i++) {
96 0 : ret = strv_add(tctx, &strv, data[i]);
97 0 : torture_assert(tctx, ret == 0, "strv_add() failed");
98 : }
99 :
100 0 : torture_assert_int_equal(tctx,
101 : strv_count(strv), num,
102 : "strv_count() failed");
103 :
104 : /* Check that strv_next() finds the expected values */
105 0 : t = NULL;
106 0 : for (i = 0; i < num; i++) {
107 0 : t = strv_next(strv, t);
108 0 : torture_assert(tctx,
109 : strcmp(t, data[i]) == 0,
110 : "strv_next() failed");
111 : }
112 :
113 :
114 : /* Check that strv_next() finds the expected values */
115 0 : t = NULL;
116 0 : for (i = 0; i < num; i++) {
117 0 : t = strv_next(strv, t);
118 0 : torture_assert(tctx,
119 : strcmp(t, data[i]) == 0,
120 : "strv_next() failed");
121 : }
122 :
123 : /* Find each item, delete it, check count */
124 0 : for (i = 0; i < num; i++) {
125 0 : t = strv_find(strv, data[i]);
126 0 : torture_assert(tctx,
127 : strcmp(t, data[i]) == 0,
128 : "strv_next() failed");
129 0 : strv_delete(&strv, t);
130 0 : torture_assert_int_equal(tctx,
131 : strv_count(strv), num - i - 1,
132 : "strv_delete() failed");
133 : }
134 :
135 : /* Add items */
136 0 : for (i = 0; i < num; i++) {
137 0 : ret = strv_add(tctx, &strv, data[i]);
138 0 : torture_assert(tctx, ret == 0, "strv_add() failed");
139 : }
140 :
141 0 : torture_assert_int_equal(tctx,
142 : strv_count(strv), num,
143 : "strv_count() failed");
144 :
145 : /* Find items in reverse, delete, check count */
146 0 : for (i = num - 1; i >= 0; i--) {
147 0 : t = strv_find(strv, data[i]);
148 0 : torture_assert(tctx,
149 : strcmp(t, data[i]) == 0,
150 : "strv_next() failed");
151 0 : strv_delete(&strv, t);
152 0 : torture_assert_int_equal(tctx,
153 : strv_count(strv), i,
154 : "strv_delete() failed");
155 : }
156 :
157 0 : return true;
158 : }
159 :
160 : /* Similar to above but only add/check first 2 chars of each string */
161 0 : static bool test_strv_addn(struct torture_context *tctx)
162 : {
163 0 : const char *data[] = { "foo", "bar", "samba" };
164 0 : char *strv = NULL;
165 : char *t;
166 : int i, ret;
167 0 : const int num = ARRAY_SIZE(data);
168 :
169 : /* Add first 2 chars of each item */
170 0 : for (i = 0; i < num; i++) {
171 0 : ret = strv_addn(tctx, &strv, data[i], 2);
172 0 : torture_assert(tctx, ret == 0, "strv_add() failed");
173 : }
174 :
175 0 : torture_assert_int_equal(tctx,
176 : strv_count(strv), num,
177 : "strv_count() failed");
178 :
179 : /* Check that strv_next() finds the expected values */
180 0 : t = NULL;
181 0 : for (i = 0; i < num; i++) {
182 0 : t = strv_next(strv, t);
183 0 : torture_assert(tctx,
184 : strncmp(t, data[i], 2) == 0,
185 : "strv_next() failed");
186 : }
187 :
188 0 : return true;
189 : }
190 :
191 964 : struct torture_suite *torture_local_util_strv(TALLOC_CTX *mem_ctx)
192 : {
193 964 : struct torture_suite *suite = torture_suite_create(mem_ctx, "strv");
194 :
195 964 : torture_suite_add_simple_test(suite, "strv_empty", test_strv_empty);
196 964 : torture_suite_add_simple_test(suite, "strv_single", test_strv_single);
197 964 : torture_suite_add_simple_test(suite, "strv_multi", test_strv_multi);
198 964 : torture_suite_add_simple_test(suite, "strv_addn", test_strv_addn);
199 :
200 964 : return suite;
201 : }
|