py:dcerpc/raw_testcase: pass auth_context and stub_len to parse_auth() in order to...
[Samba.git] / source4 / torture / ndr / ndr.c
blob17c3b1915d6d43a67ea3213d377f533077e5ed4f
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"
26 #include "librpc/gen_ndr/ndr_misc.h"
28 struct ndr_pull_test_data {
29 DATA_BLOB data;
30 DATA_BLOB data_context;
31 size_t struct_size;
32 ndr_pull_flags_fn_t pull_fn;
33 ndr_push_flags_fn_t push_fn;
34 int ndr_flags;
35 int flags;
38 static enum ndr_err_code torture_ndr_push_struct_blob_flags(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, uint32_t flags, uint32_t ndr_flags, const void *p, ndr_push_flags_fn_t fn)
40 struct ndr_push *ndr;
41 ndr = ndr_push_init_ctx(mem_ctx);
42 NDR_ERR_HAVE_NO_MEMORY(ndr);
44 ndr->flags |= ndr_flags;
46 NDR_CHECK(fn(ndr, flags, p));
48 *blob = ndr_push_blob(ndr);
49 talloc_steal(mem_ctx, blob->data);
50 talloc_free(ndr);
52 return NDR_ERR_SUCCESS;
55 static bool wrap_ndr_pullpush_test(struct torture_context *tctx,
56 struct torture_tcase *tcase,
57 struct torture_test *test)
59 bool (*check_fn) (struct torture_context *ctx, void *data) = test->fn;
60 const struct ndr_pull_test_data *data = (const struct ndr_pull_test_data *)test->data;
61 struct ndr_pull *ndr = ndr_pull_init_blob(&(data->data), tctx);
62 void *ds = talloc_zero_size(ndr, data->struct_size);
63 bool ret;
64 uint32_t highest_ofs;
66 ndr->flags |= data->flags;
68 ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
70 torture_assert_ndr_success(tctx, data->pull_fn(ndr, data->ndr_flags, ds),
71 "pulling");
73 if (ndr->offset > ndr->relative_highest_offset) {
74 highest_ofs = ndr->offset;
75 } else {
76 highest_ofs = ndr->relative_highest_offset;
79 torture_assert(tctx, highest_ofs == ndr->data_size,
80 talloc_asprintf(tctx,
81 "%d unread bytes", ndr->data_size - highest_ofs));
83 if (check_fn != NULL) {
84 ret = check_fn(tctx, ds);
85 } else {
86 ret = true;
89 if (data->push_fn != NULL) {
90 DATA_BLOB outblob;
91 torture_assert_ndr_success(tctx, torture_ndr_push_struct_blob_flags(&outblob, ndr, data->ndr_flags, ndr->flags, ds, data->push_fn), "pushing");
92 torture_assert_data_blob_equal(tctx, outblob, data->data, "ndr push compare");
95 talloc_free(ndr);
96 return ret;
99 _PUBLIC_ struct torture_test *_torture_suite_add_ndr_pullpush_test(
100 struct torture_suite *suite,
101 const char *name,
102 ndr_pull_flags_fn_t pull_fn,
103 ndr_push_flags_fn_t push_fn,
104 DATA_BLOB db,
105 size_t struct_size,
106 int ndr_flags,
107 int flags,
108 bool (*check_fn) (struct torture_context *ctx, void *data))
110 struct torture_test *test;
111 struct torture_tcase *tcase;
112 struct ndr_pull_test_data *data;
114 tcase = torture_suite_add_tcase(suite, name);
116 test = talloc(tcase, struct torture_test);
118 test->name = talloc_strdup(test, name);
119 test->description = NULL;
120 test->run = wrap_ndr_pullpush_test;
122 data = talloc(test, struct ndr_pull_test_data);
123 data->data = db;
124 data->ndr_flags = ndr_flags;
125 data->flags = flags;
126 data->struct_size = struct_size;
127 data->pull_fn = pull_fn;
128 data->push_fn = push_fn;
130 test->data = data;
131 test->fn = check_fn;
132 test->dangerous = false;
134 DLIST_ADD_END(tcase->tests, test);
136 return test;
140 static bool wrap_ndr_inout_pull_test(struct torture_context *tctx,
141 struct torture_tcase *tcase,
142 struct torture_test *test)
144 bool (*check_fn) (struct torture_context *ctx, void *data) = test->fn;
145 const struct ndr_pull_test_data *data = (const struct ndr_pull_test_data *)test->data;
146 void *ds = talloc_zero_size(tctx, data->struct_size);
147 struct ndr_pull *ndr;
148 uint32_t highest_ofs;
150 /* handle NDR_IN context */
152 ndr = ndr_pull_init_blob(&(data->data_context), tctx);
153 torture_assert(tctx, ndr, "ndr init failed");
155 ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
157 torture_assert_ndr_success(tctx,
158 data->pull_fn(ndr, NDR_IN, ds),
159 "ndr pull of context failed");
161 if (ndr->offset > ndr->relative_highest_offset) {
162 highest_ofs = ndr->offset;
163 } else {
164 highest_ofs = ndr->relative_highest_offset;
167 torture_assert(tctx, highest_ofs == ndr->data_size,
168 talloc_asprintf(tctx, "%d unread bytes", ndr->data_size - highest_ofs));
170 talloc_free(ndr);
172 /* handle NDR_OUT */
174 ndr = ndr_pull_init_blob(&(data->data), tctx);
175 torture_assert(tctx, ndr, "ndr init failed");
177 ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
179 torture_assert_ndr_success(tctx,
180 data->pull_fn(ndr, NDR_OUT, ds),
181 "ndr pull failed");
183 if (ndr->offset > ndr->relative_highest_offset) {
184 highest_ofs = ndr->offset;
185 } else {
186 highest_ofs = ndr->relative_highest_offset;
189 torture_assert(tctx, highest_ofs == ndr->data_size,
190 talloc_asprintf(tctx, "%d unread bytes", ndr->data_size - highest_ofs));
192 talloc_free(ndr);
194 if (check_fn) {
195 return check_fn(tctx, ds);
196 } else {
197 return true;
201 _PUBLIC_ struct torture_test *_torture_suite_add_ndr_pull_inout_test(
202 struct torture_suite *suite,
203 const char *name, ndr_pull_flags_fn_t pull_fn,
204 DATA_BLOB db_in,
205 DATA_BLOB db_out,
206 size_t struct_size,
207 bool (*check_fn) (struct torture_context *ctx, void *data))
209 struct torture_test *test;
210 struct torture_tcase *tcase;
211 struct ndr_pull_test_data *data;
213 tcase = torture_suite_add_tcase(suite, name);
215 test = talloc(tcase, struct torture_test);
217 test->name = talloc_strdup(test, name);
218 test->description = NULL;
219 test->run = wrap_ndr_inout_pull_test;
220 data = talloc(test, struct ndr_pull_test_data);
221 data->data = db_out;
222 data->data_context = db_in;
223 data->ndr_flags = 0;
224 data->struct_size = struct_size;
225 data->pull_fn = pull_fn;
226 test->data = data;
227 test->fn = check_fn;
228 test->dangerous = false;
230 DLIST_ADD_END(tcase->tests, test);
232 return test;
235 static bool test_check_string_terminator(struct torture_context *tctx)
237 struct ndr_pull *ndr;
238 DATA_BLOB blob;
239 TALLOC_CTX *mem_ctx = tctx;
241 /* Simple test */
242 blob = strhex_to_data_blob(tctx, "0000");
244 ndr = ndr_pull_init_blob(&blob, mem_ctx);
246 torture_assert_ndr_success(tctx, ndr_check_string_terminator(ndr, 1, 2),
247 "simple check_string_terminator test failed");
249 torture_assert(tctx, ndr->offset == 0,
250 "check_string_terminator did not reset offset");
252 if (NDR_ERR_CODE_IS_SUCCESS(ndr_check_string_terminator(ndr, 1, 3))) {
253 torture_fail(tctx, "check_string_terminator checked beyond string boundaries");
256 torture_assert(tctx, ndr->offset == 0,
257 "check_string_terminator did not reset offset");
259 talloc_free(ndr);
261 blob = strhex_to_data_blob(tctx, "11220000");
262 ndr = ndr_pull_init_blob(&blob, mem_ctx);
264 torture_assert_ndr_success(tctx,
265 ndr_check_string_terminator(ndr, 4, 1),
266 "check_string_terminator failed to recognize terminator");
268 torture_assert_ndr_success(tctx,
269 ndr_check_string_terminator(ndr, 3, 1),
270 "check_string_terminator failed to recognize terminator");
272 if (NDR_ERR_CODE_IS_SUCCESS(ndr_check_string_terminator(ndr, 2, 1))) {
273 torture_fail(tctx, "check_string_terminator erroneously reported terminator");
276 torture_assert(tctx, ndr->offset == 0,
277 "check_string_terminator did not reset offset");
278 return true;
281 static bool test_guid_from_string_valid(struct torture_context *tctx)
283 /* FIXME */
284 return true;
287 static bool test_guid_from_string_null(struct torture_context *tctx)
289 struct GUID guid;
290 torture_assert_ntstatus_equal(tctx, NT_STATUS_INVALID_PARAMETER,
291 GUID_from_string(NULL, &guid),
292 "NULL failed");
293 return true;
296 static bool test_guid_from_string_invalid(struct torture_context *tctx)
298 struct GUID g1;
299 bool failed = false;
300 int i;
301 const char *bad_guids[] = {
302 "bla",
305 "00000001-0002-0003-0405-060708090a0b", correct
307 "00000001-0002-0003-0405-060708090a0b1", /* too long */
308 "00000001-0002-0003-0405-060708090a0", /* too short */
309 "00000001-0002-0003-0405--060708090a0", /* negative */
310 "00000001-0002-0003--0405-060708090a0", /* negative */
311 "-0000001-0002-0003-0405-060708090a0b", /* negative */
312 "-0000001-0002-0003-04-5-060708090a0b", /* negative */
313 "d0000001-0002-0003-0405-060708090a-b", /* negative */
314 "00000001- -2-0003-0405-060708090a0b", /* negative, space */
315 "00000001-0002-0003-0405- 060708090a0", /* whitespace */
316 " 0000001-0002-0003--0405-060708090a0", /* whitespace */
317 "00000001-0002-0003--0405-060708090a ", /* whitespace */
318 "0000001-00002-0003-04050-60708090a0b", /* misshapen */
319 "00000010-0002-0003-04050-60708090a0b", /* misshapen */
320 "00000001-0002-0003-0405-0z0708090a0b", /* bad char */
321 "00000001-00x2-0x03-0405-060708090a0b", /* bad char (00x) */
322 "0x000001-0002-0003-0405-060708090a0b", /* 0x char */
323 "00000001-0x02-0x03-0405-060708090a0b", /* 0x char */
326 for (i = 0; i < ARRAY_SIZE(bad_guids); i++) {
327 NTSTATUS status = GUID_from_string(bad_guids[i], &g1);
328 if (! NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
329 torture_comment(tctx, "bad guid %s parsed as OK\n",
330 bad_guids[i]);
331 failed = true;
334 if (failed) {
335 torture_fail(tctx, "wrongly allowing invalid guids");
337 return true;
340 static bool test_guid_from_string(struct torture_context *tctx)
342 struct GUID g1, exp;
343 /* we are asserting all these guids are valid and equal */
344 const char *guids[4] = {
345 "00000001-0002-0003-0405-060708090a0b",
346 "{00000001-0002-0003-0405-060708090a0b}",
347 "{00000001-0002-0003-0405-060708090a0B}", /* mixed */
348 "00000001-0002-0003-0405-060708090A0B", /* upper */
350 int i;
352 torture_assert_ntstatus_ok(tctx,
353 GUID_from_string(guids[0], &g1),
354 "invalid return code");
355 exp.time_low = 1;
356 exp.time_mid = 2;
357 exp.time_hi_and_version = 3;
358 exp.clock_seq[0] = 4;
359 exp.clock_seq[1] = 5;
360 exp.node[0] = 6;
361 exp.node[1] = 7;
362 exp.node[2] = 8;
363 exp.node[3] = 9;
364 exp.node[4] = 10;
365 exp.node[5] = 11;
367 for (i = 1; i < ARRAY_SIZE(guids); i++) {
368 torture_assert_ntstatus_ok(tctx,
369 GUID_from_string(guids[i], &g1),
370 "invalid return code");
371 torture_assert(tctx, GUID_equal(&g1, &exp),
372 "UUID parsed incorrectly");
374 return true;
377 static bool test_guid_string_valid(struct torture_context *tctx)
379 struct GUID g;
380 g.time_low = 1;
381 g.time_mid = 2;
382 g.time_hi_and_version = 3;
383 g.clock_seq[0] = 4;
384 g.clock_seq[1] = 5;
385 g.node[0] = 6;
386 g.node[1] = 7;
387 g.node[2] = 8;
388 g.node[3] = 9;
389 g.node[4] = 10;
390 g.node[5] = 11;
391 torture_assert_str_equal(tctx, "00000001-0002-0003-0405-060708090a0b",
392 GUID_string(tctx, &g),
393 "parsing guid failed");
394 return true;
397 static bool test_guid_string2_valid(struct torture_context *tctx)
399 struct GUID g;
400 g.time_low = 1;
401 g.time_mid = 2;
402 g.time_hi_and_version = 3;
403 g.clock_seq[0] = 4;
404 g.clock_seq[1] = 5;
405 g.node[0] = 6;
406 g.node[1] = 7;
407 g.node[2] = 8;
408 g.node[3] = 9;
409 g.node[4] = 10;
410 g.node[5] = 11;
411 torture_assert_str_equal(tctx, "{00000001-0002-0003-0405-060708090a0b}",
412 GUID_string2(tctx, &g),
413 "parsing guid failed");
414 return true;
417 static bool test_guid_into_blob(struct torture_context *tctx)
419 enum ndr_err_code ndr_err;
420 static const char exp_guid[16] =
421 { 0x1, 0x0, 0x0, 0x0,
422 0x2, 0x0, 0x3, 0x0,
423 0x4, 0x5, 0x6, 0x7,
424 0x8, 0x9, 0xa, 0xb };
425 DATA_BLOB exp = data_blob_const(exp_guid, 16);
426 char ndr_guid[16] =
427 { 0x0, 0x0, 0x0, 0x0,
428 0x0, 0x0, 0x0, 0x0,
429 0x0, 0x0, 0x0, 0x0,
430 0x0, 0x0, 0x0, 0x0 };
431 DATA_BLOB b = data_blob_const(ndr_guid, 16);
432 struct GUID guid;
433 guid.time_low = 1;
434 guid.time_mid = 2;
435 guid.time_hi_and_version = 3;
436 guid.clock_seq[0] = 4;
437 guid.clock_seq[1] = 5;
438 guid.node[0] = 6;
439 guid.node[1] = 7;
440 guid.node[2] = 8;
441 guid.node[3] = 9;
442 guid.node[4] = 10;
443 guid.node[5] = 11;
445 ndr_err = ndr_push_struct_into_fixed_blob(&b, &guid,
446 (ndr_push_flags_fn_t)ndr_push_GUID);
447 torture_assert_ndr_err_equal(tctx, ndr_err, NDR_ERR_SUCCESS,
448 "wrong NDR error");
449 torture_assert_data_blob_equal(tctx, b, exp,
450 "GUID packed wrongly");
452 return true;
455 /* Really a test of ndr_push_struct_into_fixed_blob error handling */
456 static bool test_guid_into_long_blob(struct torture_context *tctx)
458 enum ndr_err_code ndr_err;
459 char ndr_guid[17] =
460 { 0x0, 0x0, 0x0, 0x0,
461 0x0, 0x0, 0x0, 0x0,
462 0x0, 0x0, 0x0, 0x0,
463 0x0, 0x0, 0x0, 0x0, 0x0 };
464 DATA_BLOB b = data_blob_const(ndr_guid, 17);
465 struct GUID guid;
466 guid.time_low = 1;
467 guid.time_mid = 2;
468 guid.time_hi_and_version = 3;
469 guid.clock_seq[0] = 4;
470 guid.clock_seq[1] = 5;
471 guid.node[0] = 6;
472 guid.node[1] = 7;
473 guid.node[2] = 8;
474 guid.node[3] = 9;
475 guid.node[4] = 10;
476 guid.node[5] = 11;
478 torture_assert(tctx, b.data != NULL, "data_blob_talloc failed");
479 ndr_err = ndr_push_struct_into_fixed_blob(
480 &b, &guid, (ndr_push_flags_fn_t)ndr_push_GUID);
481 torture_assert_ndr_err_equal(tctx, ndr_err, NDR_ERR_BUFSIZE,
482 "wrong NDR error");
484 return true;
487 static bool test_guid_into_short_blob(struct torture_context *tctx)
489 enum ndr_err_code ndr_err;
490 char ndr_guid[15] =
491 { 0x0, 0x0, 0x0, 0x0,
492 0x0, 0x0, 0x0, 0x0,
493 0x0, 0x0, 0x0, 0x0,
494 0x0, 0x0, 0x0 };
495 DATA_BLOB b = data_blob_const(ndr_guid, 15);
496 struct GUID guid;
497 guid.time_low = 1;
498 guid.time_mid = 2;
499 guid.time_hi_and_version = 3;
500 guid.clock_seq[0] = 4;
501 guid.clock_seq[1] = 5;
502 guid.node[0] = 6;
503 guid.node[1] = 7;
504 guid.node[2] = 8;
505 guid.node[3] = 9;
506 guid.node[4] = 10;
507 guid.node[5] = 11;
509 ndr_err = ndr_push_struct_into_fixed_blob(
510 &b, &guid, (ndr_push_flags_fn_t)ndr_push_GUID);
511 torture_assert_ndr_err_equal(tctx, ndr_err, NDR_ERR_BUFSIZE,
512 "wrong NDR error");
514 return true;
517 static bool test_compare_uuid(struct torture_context *tctx)
519 struct GUID g1, g2;
520 ZERO_STRUCT(g1); ZERO_STRUCT(g2);
521 torture_assert_int_equal(tctx, 0, GUID_compare(&g1, &g2),
522 "GUIDs not equal");
523 g1.time_low = 1;
524 torture_assert_int_equal(tctx, 1, GUID_compare(&g1, &g2),
525 "GUID diff invalid");
527 g1.time_low = 10;
528 torture_assert_int_equal(tctx, 1, GUID_compare(&g1, &g2),
529 "GUID diff invalid");
531 g1.time_low = 0;
532 g1.clock_seq[1] = 20;
533 torture_assert_int_equal(tctx, 1, GUID_compare(&g1, &g2),
534 "GUID diff invalid");
536 g1.time_low = ~0;
537 torture_assert_int_equal(tctx, 1, GUID_compare(&g1, &g2),
538 "GUID diff invalid");
540 g1.time_low = 0;
541 g2.time_low = ~0;
542 torture_assert_int_equal(tctx, -1, GUID_compare(&g1, &g2),
543 "GUID diff invalid");
544 return true;
547 struct torture_suite *torture_local_ndr(TALLOC_CTX *mem_ctx)
549 struct torture_suite *suite = torture_suite_create(mem_ctx, "ndr");
551 torture_suite_add_suite(suite, ndr_winreg_suite(suite));
552 torture_suite_add_suite(suite, ndr_atsvc_suite(suite));
553 torture_suite_add_suite(suite, ndr_lsa_suite(suite));
554 torture_suite_add_suite(suite, ndr_epmap_suite(suite));
555 torture_suite_add_suite(suite, ndr_dfs_suite(suite));
556 torture_suite_add_suite(suite, ndr_dfsblob_suite(suite));
557 torture_suite_add_suite(suite, ndr_netlogon_suite(suite));
558 torture_suite_add_suite(suite, ndr_drsuapi_suite(suite));
559 torture_suite_add_suite(suite, ndr_spoolss_suite(suite));
560 torture_suite_add_suite(suite, ndr_winspool_suite(suite));
561 torture_suite_add_suite(suite, ndr_ntprinting_suite(suite));
562 torture_suite_add_suite(suite, ndr_samr_suite(suite));
563 torture_suite_add_suite(suite, ndr_drsblobs_suite(suite));
564 torture_suite_add_suite(suite, ndr_nbt_suite(suite));
565 torture_suite_add_suite(suite, ndr_ntlmssp_suite(suite));
566 torture_suite_add_suite(suite, ndr_backupkey_suite(suite));
567 torture_suite_add_suite(suite, ndr_witness_suite(suite));
568 torture_suite_add_suite(suite, ndr_clusapi_suite(suite));
569 torture_suite_add_suite(suite, ndr_negoex_suite(suite));
570 torture_suite_add_suite(suite, ndr_string_suite(suite));
571 torture_suite_add_suite(suite, ndr_krb5pac_suite(suite));
572 torture_suite_add_suite(suite, ndr_cabinet_suite(suite));
573 torture_suite_add_suite(suite, ndr_charset_suite(suite));
575 torture_suite_add_simple_test(suite, "string terminator",
576 test_check_string_terminator);
578 torture_suite_add_simple_test(suite, "guid_from_string_null",
579 test_guid_from_string_null);
581 torture_suite_add_simple_test(suite, "guid_from_string",
582 test_guid_from_string);
584 torture_suite_add_simple_test(suite, "guid_from_string_invalid",
585 test_guid_from_string_invalid);
587 torture_suite_add_simple_test(suite, "guid_string_valid",
588 test_guid_string_valid);
590 torture_suite_add_simple_test(suite, "guid_string2_valid",
591 test_guid_string2_valid);
593 torture_suite_add_simple_test(suite, "guid_from_string_valid",
594 test_guid_from_string_valid);
596 torture_suite_add_simple_test(suite, "compare_uuid",
597 test_compare_uuid);
599 torture_suite_add_simple_test(suite, "guid_into_blob",
600 test_guid_into_blob);
602 torture_suite_add_simple_test(suite, "guid_into_short_blob",
603 test_guid_into_short_blob);
605 torture_suite_add_simple_test(suite, "guid_into_long_blob",
606 test_guid_into_long_blob);
608 return suite;