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/>.
24 #include "libcli/util/ntstatus.h"
25 #include "torture/torture.h"
26 #include "lib/util/data_blob.h"
27 #include "torture/local/proto.h"
29 #include "lib/util/strv.h"
30 #include "lib/util/strv_util.h"
32 static bool test_strv_split_none(struct torture_context
*tctx
)
37 /* NULL has 0 entries */
38 ret
= strv_split(tctx
, &strv
, NULL
, " ");
39 torture_assert(tctx
, ret
== 0, "strv_split() on NULL failed");
40 torture_assert_int_equal(tctx
,
43 "strv_split() on NULL failed");
46 /* Empty string has 0 entries */
47 ret
= strv_split(tctx
, &strv
, "", " ");
48 torture_assert(tctx
, ret
== 0, "strv_split() on NULL failed");
49 torture_assert_int_equal(tctx
,
52 "strv_split() on \"\" failed");
55 /* String containing only separators has 0 entries */
56 ret
= strv_split(tctx
, &strv
, "abcabcabc", "cba ");
57 torture_assert(tctx
, ret
== 0, "strv_split() on NULL failed");
58 torture_assert_int_equal(tctx
,
61 "strv_split() on seps-only failed");
67 struct test_str_split_data
{
70 const char *out
[10]; /* Hardcoded maximum! */
73 static bool test_strv_split_some(struct torture_context
*tctx
)
75 const struct test_str_split_data data
[] = {
83 /* Single string, single leading separator */
89 /* Single string, single trailing separator */
95 /* Single string, lots of separators */
101 /* Multiple strings, many separators */
102 .in
= " \t foo bar\t\tx\t samba\t ",
104 .out
= { "foo", "bar", "x", "samba" }
111 for (j
= 0; j
< ARRAY_SIZE(data
); j
++) {
114 const struct test_str_split_data
*d
= &data
[j
];
117 while (num
< ARRAY_SIZE(d
->out
) && d
->out
[num
] != NULL
) {
120 ret
= strv_split(tctx
, &strv
, d
->in
, d
->sep
);
121 torture_assert(tctx
, ret
== 0, "strv_split() on NULL failed");
122 torture_assert_int_equal(tctx
,
125 "strv_split() failed");
127 for (i
= 0; i
< num
; i
++) {
128 t
= strv_next(strv
, t
);
130 strcmp(t
, d
->out
[i
]) == 0,
131 "strv_split() failed");
138 struct torture_suite
*torture_local_util_strv_util(TALLOC_CTX
*mem_ctx
)
140 struct torture_suite
*suite
=
141 torture_suite_create(mem_ctx
, "strv_util");
143 torture_suite_add_simple_test(suite
,
145 test_strv_split_none
);
146 torture_suite_add_simple_test(suite
,
148 test_strv_split_some
);