auth/credentials: don't ignore "client use kerberos" and --use-kerberos for machine...
[Samba.git] / lib / util / tests / strv.c
blobc79ed5c33f26c83878401d3639cceda372dffadf
1 /*
2 * Tests for strv
4 * Copyright Martin Schwenke <martin@meltin.net> 2016
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.
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.
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/>.
21 #include <talloc.h>
23 #include "replace.h"
25 #include "libcli/util/ntstatus.h"
26 #include "torture/torture.h"
27 #include "lib/util/data_blob.h"
28 #include "torture/local/proto.h"
30 #include "lib/util/strv.h"
32 static bool test_strv_empty(struct torture_context *tctx)
34 /* NULL strv contains 0 entries */
35 torture_assert_int_equal(tctx,
36 strv_count(NULL),
38 "strv_count() on NULL failed");
40 /* NULL strv has no next entry */
41 torture_assert(tctx,
42 strv_next(NULL, NULL) == NULL,
43 "strv_next() on NULL failed");
45 return true;
48 static bool test_strv_single(struct torture_context *tctx)
50 const char *data = "foo";
51 char *strv = NULL;
52 char *t;
53 int ret;
55 /* Add an item */
56 ret = strv_add(tctx, &strv, data);
57 torture_assert(tctx, ret == 0, "strv_add() failed");
59 /* Is there 1 item? */
60 torture_assert_int_equal(tctx,
61 strv_count(strv), 1,
62 "strv_count() failed");
64 /* Is the expected item the first one? */
65 t = strv_next(strv, NULL);
66 torture_assert(tctx,
67 strcmp(t, data) == 0,
68 "strv_next() failed");
70 /* Can the expected item be found? */
71 t = strv_find(strv, data);
72 torture_assert(tctx,
73 strcmp(t, data) == 0,
74 "strv_next() failed");
76 /* Delete it */
77 strv_delete(&strv, t);
79 /* Should have no items */
80 torture_assert_int_equal(tctx,
81 strv_count(strv), 0,
82 "strv_count() failed");
83 return true;
86 static bool test_strv_multi(struct torture_context *tctx)
88 const char *data[] = { "foo", "bar", "", "samba", "x"};
89 char *strv = NULL;
90 char *t;
91 int i, ret;
92 const int num = ARRAY_SIZE(data);
94 /* Add items */
95 for (i = 0; i < num; i++) {
96 ret = strv_add(tctx, &strv, data[i]);
97 torture_assert(tctx, ret == 0, "strv_add() failed");
100 torture_assert_int_equal(tctx,
101 strv_count(strv), num,
102 "strv_count() failed");
104 /* Check that strv_next() finds the expected values */
105 t = NULL;
106 for (i = 0; i < num; i++) {
107 t = strv_next(strv, t);
108 torture_assert(tctx,
109 strcmp(t, data[i]) == 0,
110 "strv_next() failed");
114 /* Check that strv_next() finds the expected values */
115 t = NULL;
116 for (i = 0; i < num; i++) {
117 t = strv_next(strv, t);
118 torture_assert(tctx,
119 strcmp(t, data[i]) == 0,
120 "strv_next() failed");
123 /* Find each item, delete it, check count */
124 for (i = 0; i < num; i++) {
125 t = strv_find(strv, data[i]);
126 torture_assert(tctx,
127 strcmp(t, data[i]) == 0,
128 "strv_next() failed");
129 strv_delete(&strv, t);
130 torture_assert_int_equal(tctx,
131 strv_count(strv), num - i - 1,
132 "strv_delete() failed");
135 /* Add items */
136 for (i = 0; i < num; i++) {
137 ret = strv_add(tctx, &strv, data[i]);
138 torture_assert(tctx, ret == 0, "strv_add() failed");
141 torture_assert_int_equal(tctx,
142 strv_count(strv), num,
143 "strv_count() failed");
145 /* Find items in reverse, delete, check count */
146 for (i = num - 1; i >= 0; i--) {
147 t = strv_find(strv, data[i]);
148 torture_assert(tctx,
149 strcmp(t, data[i]) == 0,
150 "strv_next() failed");
151 strv_delete(&strv, t);
152 torture_assert_int_equal(tctx,
153 strv_count(strv), i,
154 "strv_delete() failed");
157 return true;
160 /* Similar to above but only add/check first 2 chars of each string */
161 static bool test_strv_addn(struct torture_context *tctx)
163 const char *data[] = { "foo", "bar", "samba" };
164 char *strv = NULL;
165 char *t;
166 int i, ret;
167 const int num = ARRAY_SIZE(data);
169 /* Add first 2 chars of each item */
170 for (i = 0; i < num; i++) {
171 ret = strv_addn(tctx, &strv, data[i], 2);
172 torture_assert(tctx, ret == 0, "strv_add() failed");
175 torture_assert_int_equal(tctx,
176 strv_count(strv), num,
177 "strv_count() failed");
179 /* Check that strv_next() finds the expected values */
180 t = NULL;
181 for (i = 0; i < num; i++) {
182 t = strv_next(strv, t);
183 torture_assert(tctx,
184 strncmp(t, data[i], 2) == 0,
185 "strv_next() failed");
188 return true;
191 struct torture_suite *torture_local_util_strv(TALLOC_CTX *mem_ctx)
193 struct torture_suite *suite = torture_suite_create(mem_ctx, "strv");
195 torture_suite_add_simple_test(suite, "strv_empty", test_strv_empty);
196 torture_suite_add_simple_test(suite, "strv_single", test_strv_single);
197 torture_suite_add_simple_test(suite, "strv_multi", test_strv_multi);
198 torture_suite_add_simple_test(suite, "strv_addn", test_strv_addn);
200 return suite;