Line data Source code
1 : /*
2 : * Unix SMB/CIFS implementation.
3 : *
4 : * Copyright (C) 2018 Andreas Schneider <asn@samba.org>
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 : #include <stdarg.h>
21 : #include <stddef.h>
22 : #include <stdint.h>
23 : #include <setjmp.h>
24 : #include <cmocka.h>
25 :
26 : #include <ldb.h>
27 : #include "ldb_private.h"
28 :
29 1 : static void test_ldb_dn_add_child_fmt(void **state)
30 : {
31 1 : struct ldb_context *ldb = ldb_init(NULL, NULL);
32 :
33 1 : struct ldb_dn *dn = ldb_dn_new(ldb, ldb, "dc=samba,dc=org");
34 :
35 1 : assert_true(ldb_dn_add_child_fmt(dn,
36 : "DC=X"));
37 :
38 1 : assert_string_equal("DC=X,dc=samba,dc=org",
39 : ldb_dn_get_linearized(dn));
40 :
41 1 : assert_string_equal("DC=X,DC=SAMBA,DC=ORG",
42 : ldb_dn_get_casefold(dn));
43 :
44 1 : }
45 :
46 1 : static void test_ldb_dn_add_child_fmt2(void **state)
47 : {
48 1 : struct ldb_context *ldb = ldb_init(NULL, NULL);
49 :
50 1 : struct ldb_dn *dn = ldb_dn_new(ldb, ldb, "dc=samba,dc=org");
51 :
52 1 : assert_true(ldb_dn_add_child_fmt(dn,
53 : "DC=X,DC=Y"));
54 :
55 1 : assert_string_equal("DC=X,DC=Y,dc=samba,dc=org",
56 : ldb_dn_get_linearized(dn));
57 :
58 1 : assert_string_equal("DC=X,DC=Y,DC=SAMBA,DC=ORG",
59 : ldb_dn_get_casefold(dn));
60 :
61 1 : assert_int_equal(4,
62 : ldb_dn_get_comp_num(dn));
63 :
64 1 : }
65 :
66 1 : static void test_ldb_dn_add_child_val(void **state)
67 : {
68 1 : struct ldb_context *ldb = ldb_init(NULL, NULL);
69 :
70 1 : struct ldb_dn *dn = ldb_dn_new(ldb, ldb, "dc=samba,dc=org");
71 1 : struct ldb_val name = {.data = discard_const("X"),
72 : .length = 1
73 : };
74 :
75 1 : assert_true(ldb_dn_add_child_val(dn,
76 : "DC", name));
77 :
78 1 : assert_string_equal("DC=X,dc=samba,dc=org",
79 : ldb_dn_get_linearized(dn));
80 :
81 1 : assert_string_equal("DC=X,DC=SAMBA,DC=ORG",
82 : ldb_dn_get_casefold(dn));
83 :
84 1 : }
85 :
86 1 : static void test_ldb_dn_add_child_val2(void **state)
87 : {
88 1 : struct ldb_context *ldb = ldb_init(NULL, NULL);
89 :
90 1 : struct ldb_dn *dn = ldb_dn_new(ldb, ldb, "dc=samba,dc=org");
91 :
92 1 : struct ldb_val name = {.data = discard_const("X,DC=Y"),
93 : .length = 6
94 : };
95 :
96 1 : assert_true(ldb_dn_add_child_val(dn,
97 : "DC", name));
98 :
99 1 : assert_string_equal("DC=X\\,DC\\3DY,dc=samba,dc=org",
100 : ldb_dn_get_linearized(dn));
101 :
102 1 : assert_string_equal("DC=X\\,DC\\3DY,DC=SAMBA,DC=ORG",
103 : ldb_dn_get_casefold(dn));
104 :
105 1 : assert_int_equal(3,
106 : ldb_dn_get_comp_num(dn));
107 :
108 1 : }
109 :
110 : struct explode_test {
111 : const char *strdn;
112 : int comp_num;
113 : int ext_comp_num;
114 : bool special;
115 : bool invalid;
116 : const char *linearized;
117 : const char *ext_linearized_1;
118 : bool explode_result;
119 : };
120 :
121 2 : static int extended_dn_read_ID(struct ldb_context *ldb, void *mem_ctx,
122 : const struct ldb_val *in, struct ldb_val *out)
123 : {
124 :
125 : /* Allow to check we can cope with validity checks */
126 2 : if (in->length != 4) {
127 : return -1;
128 : }
129 :
130 1 : *out = *in;
131 1 : out->data = talloc_memdup(mem_ctx, in->data, in->length);
132 1 : if (out->data == NULL) {
133 0 : return -1;
134 : }
135 :
136 : return 0;
137 : }
138 :
139 : /* write out (reused for both HEX and clear for now) */
140 1 : static int extended_dn_write_ID(struct ldb_context *ldb, void *mem_ctx,
141 : const struct ldb_val *in, struct ldb_val *out)
142 : {
143 1 : *out = *in;
144 :
145 1 : out->data = talloc_memdup(mem_ctx, in->data, in->length);
146 1 : if (out->data == NULL) {
147 0 : return -1;
148 : }
149 : return 0;
150 : }
151 :
152 :
153 1 : static void test_ldb_dn_explode(void **state)
154 : {
155 1 : size_t i;
156 1 : struct ldb_context *ldb = ldb_init(NULL, NULL);
157 1 : struct explode_test tests[] = {
158 : {"A=B", 1, 0, false, false, "A=B", "A=B", true},
159 : {"", 0, 0, false, false, "", "", true},
160 : {" ", -1, -1, false, false, " ", " ", false},
161 : {"<>", 0, 0, false, false, "", NULL, true},
162 : {"<", 0, 0, false, false, "", NULL, true},
163 : {"<><", 0, 0, false, false, "", NULL, true},
164 : {"<><>", 0, 0, false, false, "", NULL, true},
165 : {"A=B,C=D", 2, 0, false, false, "A=B,C=D", "A=B,C=D", true},
166 : {"<X=Y>A=B,C=D", -1, -1, false, false, "", NULL, false},
167 : {"<X=Y>;A=B,C=D", -1, -1, false, false, "A=B,C=D", NULL, false},
168 : {"<ID=ABC>;A=B,C=D", -1, -1, false, true, "A=B,C=D", NULL, false},
169 : {"<ID=ABCD>;A=B,C=D", 2, 1, false, false, "A=B,C=D", "<ID=ABCD>;A=B,C=D", true},
170 : {"x=🔥", 1, 0, false, false, "x=🔥", "x=🔥", true},
171 : {"@FOO", 0, 0, true, false, "@FOO", "@FOO", true},
172 : };
173 :
174 1 : struct ldb_dn_extended_syntax syntax = {
175 : .name = "ID",
176 : .read_fn = extended_dn_read_ID,
177 : .write_clear_fn = extended_dn_write_ID,
178 : .write_hex_fn = extended_dn_write_ID
179 : };
180 :
181 1 : ldb_dn_extended_add_syntax(ldb, 0, &syntax);
182 :
183 16 : for (i = 0; i < ARRAY_SIZE(tests); i++) {
184 14 : bool result;
185 14 : const char *linear;
186 14 : const char *ext_linear;
187 14 : struct ldb_dn *dn = ldb_dn_new(ldb, ldb, tests[i].strdn);
188 :
189 : /*
190 : * special, invalid, linear, and ext_linear are set before
191 : * explode
192 : */
193 14 : linear = ldb_dn_get_linearized(dn);
194 14 : assert_true((linear == NULL) == (tests[i].linearized == NULL));
195 14 : assert_string_equal(linear,
196 : tests[i].linearized);
197 :
198 14 : ext_linear = ldb_dn_get_extended_linearized(ldb, dn, 1);
199 14 : assert_true((ext_linear == NULL) ==
200 : (tests[i].ext_linearized_1 == NULL));
201 :
202 14 : if (tests[i].ext_linearized_1 != NULL) {
203 7 : assert_string_equal(ext_linear,
204 : tests[i].ext_linearized_1);
205 : }
206 14 : assert_true(ldb_dn_is_special(dn) == tests[i].special);
207 14 : assert_true(ldb_dn_is_valid(dn) != tests[i].invalid);
208 :
209 : /* comp nums are set by explode */
210 14 : result = ldb_dn_validate(dn);
211 14 : print_error("string under test (%zu) «%s»: res %i lin «%s» ext «%s»\n",
212 : i, tests[i].strdn, result, linear, ext_linear);
213 :
214 14 : assert_true(result == tests[i].explode_result);
215 14 : assert_int_equal(ldb_dn_get_comp_num(dn),
216 : tests[i].comp_num);
217 14 : assert_int_equal(ldb_dn_get_extended_comp_num(dn),
218 : tests[i].ext_comp_num);
219 : }
220 1 : }
221 :
222 1 : int main(void) {
223 1 : const struct CMUnitTest tests[] = {
224 : cmocka_unit_test(test_ldb_dn_add_child_fmt),
225 : cmocka_unit_test(test_ldb_dn_add_child_fmt2),
226 : cmocka_unit_test(test_ldb_dn_add_child_val),
227 : cmocka_unit_test(test_ldb_dn_add_child_val2),
228 : cmocka_unit_test(test_ldb_dn_explode),
229 : };
230 :
231 1 : cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
232 :
233 1 : return cmocka_run_group_tests(tests, NULL, NULL);
234 : }
|