s3: Fix Coverity ID 242705 Dereference before null check
[Samba/gebeck_regimport.git] / source4 / torture / ndr / ndr.c
blobb4462420492b1e6be7464602194e544d79493715
1 /*
2 Unix SMB/CIFS implementation.
3 test suite for winreg ndr operations
5 Copyright (C) Jelmer Vernooij 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "torture/ndr/ndr.h"
23 #include "torture/ndr/proto.h"
24 #include "../lib/util/dlinklist.h"
25 #include "param/param.h"
27 struct ndr_pull_test_data {
28 DATA_BLOB data;
29 DATA_BLOB data_context;
30 size_t struct_size;
31 ndr_pull_flags_fn_t pull_fn;
32 ndr_push_flags_fn_t push_fn;
33 int ndr_flags;
36 static bool wrap_ndr_pullpush_test(struct torture_context *tctx,
37 struct torture_tcase *tcase,
38 struct torture_test *test)
40 bool (*check_fn) (struct torture_context *ctx, void *data) = test->fn;
41 const struct ndr_pull_test_data *data = (const struct ndr_pull_test_data *)test->data;
42 struct ndr_pull *ndr = ndr_pull_init_blob(&(data->data), tctx);
43 void *ds = talloc_zero_size(ndr, data->struct_size);
44 bool ret;
46 ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
48 torture_assert_ndr_success(tctx, data->pull_fn(ndr, data->ndr_flags, ds),
49 "pulling");
51 torture_assert(tctx, ndr->offset == ndr->data_size,
52 talloc_asprintf(tctx,
53 "%d unread bytes", ndr->data_size - ndr->offset));
55 if (check_fn != NULL) {
56 ret = check_fn(tctx, ds);
57 } else {
58 ret = true;
61 if (data->push_fn != NULL) {
62 DATA_BLOB outblob;
63 torture_assert_ndr_success(tctx, ndr_push_struct_blob(&outblob, ndr, ds, data->push_fn), "pushing");
64 torture_assert_data_blob_equal(tctx, outblob, data->data, "ndr push compare");
67 talloc_free(ndr);
68 return ret;
71 _PUBLIC_ struct torture_test *_torture_suite_add_ndr_pullpush_test(
72 struct torture_suite *suite,
73 const char *name,
74 ndr_pull_flags_fn_t pull_fn,
75 ndr_push_flags_fn_t push_fn,
76 DATA_BLOB db,
77 size_t struct_size,
78 int ndr_flags,
79 bool (*check_fn) (struct torture_context *ctx, void *data))
81 struct torture_test *test;
82 struct torture_tcase *tcase;
83 struct ndr_pull_test_data *data;
85 tcase = torture_suite_add_tcase(suite, name);
87 test = talloc(tcase, struct torture_test);
89 test->name = talloc_strdup(test, name);
90 test->description = NULL;
91 test->run = wrap_ndr_pullpush_test;
93 data = talloc(test, struct ndr_pull_test_data);
94 data->data = db;
95 data->ndr_flags = ndr_flags;
96 data->struct_size = struct_size;
97 data->pull_fn = pull_fn;
98 data->push_fn = push_fn;
100 test->data = data;
101 test->fn = check_fn;
102 test->dangerous = false;
104 DLIST_ADD_END(tcase->tests, test, struct torture_test *);
106 return test;
110 static bool wrap_ndr_inout_pull_test(struct torture_context *tctx,
111 struct torture_tcase *tcase,
112 struct torture_test *test)
114 bool (*check_fn) (struct torture_context *ctx, void *data) = test->fn;
115 const struct ndr_pull_test_data *data = (const struct ndr_pull_test_data *)test->data;
116 void *ds = talloc_zero_size(tctx, data->struct_size);
117 struct ndr_pull *ndr;
119 /* handle NDR_IN context */
121 ndr = ndr_pull_init_blob(&(data->data_context), tctx);
122 torture_assert(tctx, ndr, "ndr init failed");
124 ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
126 torture_assert_ndr_success(tctx,
127 data->pull_fn(ndr, NDR_IN, ds),
128 "ndr pull of context failed");
130 torture_assert(tctx, ndr->offset == ndr->data_size,
131 talloc_asprintf(tctx, "%d unread bytes", ndr->data_size - ndr->offset));
133 talloc_free(ndr);
135 /* handle NDR_OUT */
137 ndr = ndr_pull_init_blob(&(data->data), tctx);
138 torture_assert(tctx, ndr, "ndr init failed");
140 ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
142 torture_assert_ndr_success(tctx,
143 data->pull_fn(ndr, NDR_OUT, ds),
144 "ndr pull failed");
146 torture_assert(tctx, ndr->offset == ndr->data_size,
147 talloc_asprintf(tctx, "%d unread bytes", ndr->data_size - ndr->offset));
149 talloc_free(ndr);
151 if (check_fn) {
152 return check_fn(tctx, ds);
153 } else {
154 return true;
158 _PUBLIC_ struct torture_test *_torture_suite_add_ndr_pull_inout_test(
159 struct torture_suite *suite,
160 const char *name, ndr_pull_flags_fn_t pull_fn,
161 DATA_BLOB db_in,
162 DATA_BLOB db_out,
163 size_t struct_size,
164 bool (*check_fn) (struct torture_context *ctx, void *data))
166 struct torture_test *test;
167 struct torture_tcase *tcase;
168 struct ndr_pull_test_data *data;
170 tcase = torture_suite_add_tcase(suite, name);
172 test = talloc(tcase, struct torture_test);
174 test->name = talloc_strdup(test, name);
175 test->description = NULL;
176 test->run = wrap_ndr_inout_pull_test;
177 data = talloc(test, struct ndr_pull_test_data);
178 data->data = db_out;
179 data->data_context = db_in;
180 data->ndr_flags = 0;
181 data->struct_size = struct_size;
182 data->pull_fn = pull_fn;
183 test->data = data;
184 test->fn = check_fn;
185 test->dangerous = false;
187 DLIST_ADD_END(tcase->tests, test, struct torture_test *);
189 return test;
192 static bool test_check_string_terminator(struct torture_context *tctx)
194 struct ndr_pull *ndr;
195 DATA_BLOB blob;
196 TALLOC_CTX *mem_ctx = tctx;
198 /* Simple test */
199 blob = strhex_to_data_blob(tctx, "0000");
201 ndr = ndr_pull_init_blob(&blob, mem_ctx);
203 torture_assert_ndr_success(tctx, ndr_check_string_terminator(ndr, 1, 2),
204 "simple check_string_terminator test failed");
206 torture_assert(tctx, ndr->offset == 0,
207 "check_string_terminator did not reset offset");
209 if (NDR_ERR_CODE_IS_SUCCESS(ndr_check_string_terminator(ndr, 1, 3))) {
210 torture_fail(tctx, "check_string_terminator checked beyond string boundaries");
213 torture_assert(tctx, ndr->offset == 0,
214 "check_string_terminator did not reset offset");
216 talloc_free(ndr);
218 blob = strhex_to_data_blob(tctx, "11220000");
219 ndr = ndr_pull_init_blob(&blob, mem_ctx);
221 torture_assert_ndr_success(tctx,
222 ndr_check_string_terminator(ndr, 4, 1),
223 "check_string_terminator failed to recognize terminator");
225 torture_assert_ndr_success(tctx,
226 ndr_check_string_terminator(ndr, 3, 1),
227 "check_string_terminator failed to recognize terminator");
229 if (NDR_ERR_CODE_IS_SUCCESS(ndr_check_string_terminator(ndr, 2, 1))) {
230 torture_fail(tctx, "check_string_terminator erroneously reported terminator");
233 torture_assert(tctx, ndr->offset == 0,
234 "check_string_terminator did not reset offset");
235 return true;
238 static bool test_guid_from_string_valid(struct torture_context *tctx)
240 /* FIXME */
241 return true;
244 static bool test_guid_from_string_null(struct torture_context *tctx)
246 struct GUID guid;
247 torture_assert_ntstatus_equal(tctx, NT_STATUS_INVALID_PARAMETER,
248 GUID_from_string(NULL, &guid),
249 "NULL failed");
250 return true;
253 static bool test_guid_from_string_invalid(struct torture_context *tctx)
255 struct GUID g1;
256 torture_assert_ntstatus_equal(tctx, NT_STATUS_INVALID_PARAMETER,
257 GUID_from_string("bla", &g1),
258 "parameter not invalid");
259 return true;
262 static bool test_guid_from_string(struct torture_context *tctx)
264 struct GUID g1, exp;
265 torture_assert_ntstatus_ok(tctx,
266 GUID_from_string("00000001-0002-0003-0405-060708090a0b", &g1),
267 "invalid return code");
268 exp.time_low = 1;
269 exp.time_mid = 2;
270 exp.time_hi_and_version = 3;
271 exp.clock_seq[0] = 4;
272 exp.clock_seq[1] = 5;
273 exp.node[0] = 6;
274 exp.node[1] = 7;
275 exp.node[2] = 8;
276 exp.node[3] = 9;
277 exp.node[4] = 10;
278 exp.node[5] = 11;
279 torture_assert(tctx, GUID_equal(&g1, &exp), "UUID parsed incorrectly");
280 torture_assert_ntstatus_ok(tctx,
281 GUID_from_string("{00000001-0002-0003-0405-060708090a0b}", &g1),
282 "invalid return code");
283 torture_assert(tctx, GUID_equal(&g1, &exp), "UUID parsed incorrectly");
285 return true;
288 static bool test_guid_string_valid(struct torture_context *tctx)
290 struct GUID g;
291 g.time_low = 1;
292 g.time_mid = 2;
293 g.time_hi_and_version = 3;
294 g.clock_seq[0] = 4;
295 g.clock_seq[1] = 5;
296 g.node[0] = 6;
297 g.node[1] = 7;
298 g.node[2] = 8;
299 g.node[3] = 9;
300 g.node[4] = 10;
301 g.node[5] = 11;
302 torture_assert_str_equal(tctx, "00000001-0002-0003-0405-060708090a0b",
303 GUID_string(tctx, &g),
304 "parsing guid failed");
305 return true;
308 static bool test_guid_string2_valid(struct torture_context *tctx)
310 struct GUID g;
311 g.time_low = 1;
312 g.time_mid = 2;
313 g.time_hi_and_version = 3;
314 g.clock_seq[0] = 4;
315 g.clock_seq[1] = 5;
316 g.node[0] = 6;
317 g.node[1] = 7;
318 g.node[2] = 8;
319 g.node[3] = 9;
320 g.node[4] = 10;
321 g.node[5] = 11;
322 torture_assert_str_equal(tctx, "{00000001-0002-0003-0405-060708090a0b}",
323 GUID_string2(tctx, &g),
324 "parsing guid failed");
325 return true;
328 static bool test_compare_uuid(struct torture_context *tctx)
330 struct GUID g1, g2;
331 ZERO_STRUCT(g1); ZERO_STRUCT(g2);
332 torture_assert_int_equal(tctx, 0, GUID_compare(&g1, &g2),
333 "GUIDs not equal");
334 g1.time_low = 1;
335 torture_assert_int_equal(tctx, 1, GUID_compare(&g1, &g2),
336 "GUID diff invalid");
338 g1.time_low = 10;
339 torture_assert_int_equal(tctx, 1, GUID_compare(&g1, &g2),
340 "GUID diff invalid");
342 g1.time_low = 0;
343 g1.clock_seq[1] = 20;
344 torture_assert_int_equal(tctx, 1, GUID_compare(&g1, &g2),
345 "GUID diff invalid");
347 g1.time_low = ~0;
348 torture_assert_int_equal(tctx, 1, GUID_compare(&g1, &g2),
349 "GUID diff invalid");
351 g1.time_low = 0;
352 g2.time_low = ~0;
353 torture_assert_int_equal(tctx, -1, GUID_compare(&g1, &g2),
354 "GUID diff invalid");
355 return true;
358 struct torture_suite *torture_local_ndr(TALLOC_CTX *mem_ctx)
360 struct torture_suite *suite = torture_suite_create(mem_ctx, "ndr");
362 torture_suite_add_suite(suite, ndr_winreg_suite(suite));
363 torture_suite_add_suite(suite, ndr_atsvc_suite(suite));
364 torture_suite_add_suite(suite, ndr_lsa_suite(suite));
365 torture_suite_add_suite(suite, ndr_epmap_suite(suite));
366 torture_suite_add_suite(suite, ndr_dfs_suite(suite));
367 torture_suite_add_suite(suite, ndr_dfsblob_suite(suite));
368 torture_suite_add_suite(suite, ndr_netlogon_suite(suite));
369 torture_suite_add_suite(suite, ndr_drsuapi_suite(suite));
370 torture_suite_add_suite(suite, ndr_spoolss_suite(suite));
371 torture_suite_add_suite(suite, ndr_samr_suite(suite));
372 torture_suite_add_suite(suite, ndr_drsblobs_suite(suite));
373 torture_suite_add_suite(suite, ndr_nbt_suite(suite));
374 torture_suite_add_suite(suite, ndr_ntlmssp_suite(suite));
375 #ifdef SAMBA4_USES_HEIMDAL /* Add Heimdal-specific KDC test */
376 torture_suite_add_suite(suite, ndr_backupkey_suite(suite));
377 #endif
378 torture_suite_add_suite(suite, ndr_string_suite(suite));
380 torture_suite_add_simple_test(suite, "string terminator",
381 test_check_string_terminator);
383 torture_suite_add_simple_test(suite, "guid_from_string_null",
384 test_guid_from_string_null);
386 torture_suite_add_simple_test(suite, "guid_from_string",
387 test_guid_from_string);
389 torture_suite_add_simple_test(suite, "guid_from_string_invalid",
390 test_guid_from_string_invalid);
392 torture_suite_add_simple_test(suite, "guid_string_valid",
393 test_guid_string_valid);
395 torture_suite_add_simple_test(suite, "guid_string2_valid",
396 test_guid_string2_valid);
398 torture_suite_add_simple_test(suite, "guid_from_string_valid",
399 test_guid_from_string_valid);
401 torture_suite_add_simple_test(suite, "compare_uuid",
402 test_compare_uuid);
404 return suite;