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/>.
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
,
38 "strv_count() on NULL failed");
40 /* NULL strv has no next entry */
42 strv_next(NULL
, NULL
) == NULL
,
43 "strv_next() on NULL failed");
48 static bool test_strv_single(struct torture_context
*tctx
)
50 const char *data
= "foo";
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
,
62 "strv_count() failed");
64 /* Is the expected item the first one? */
65 t
= strv_next(strv
, NULL
);
68 "strv_next() failed");
70 /* Can the expected item be found? */
71 t
= strv_find(strv
, data
);
74 "strv_next() failed");
77 strv_delete(&strv
, t
);
79 /* Should have no items */
80 torture_assert_int_equal(tctx
,
82 "strv_count() failed");
86 static bool test_strv_multi(struct torture_context
*tctx
)
88 const char *data
[5] = { "foo", "bar", "", "samba", "x"};
92 const int num
= sizeof(data
) / sizeof(data
[0]);
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 */
106 for (i
= 0; i
< num
; i
++) {
107 t
= strv_next(strv
, t
);
109 strcmp(t
, data
[i
]) == 0,
110 "strv_next() failed");
114 /* Check that strv_next() finds the expected values */
116 for (i
= 0; i
< num
; i
++) {
117 t
= strv_next(strv
, t
);
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
]);
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");
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
]);
149 strcmp(t
, data
[i
]) == 0,
150 "strv_next() failed");
151 strv_delete(&strv
, t
);
152 torture_assert_int_equal(tctx
,
154 "strv_delete() failed");
160 struct torture_suite
*torture_local_util_strv(TALLOC_CTX
*mem_ctx
)
162 struct torture_suite
*suite
= torture_suite_create(mem_ctx
, "strv");
164 torture_suite_add_simple_test(suite
, "strv_empty", test_strv_empty
);
165 torture_suite_add_simple_test(suite
, "strv_single", test_strv_single
);
166 torture_suite_add_simple_test(suite
, "strv_multi", test_strv_multi
);