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/>.
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
{
30 DATA_BLOB data_context
;
32 ndr_pull_flags_fn_t pull_fn
;
33 ndr_push_flags_fn_t push_fn
;
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
)
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
);
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
);
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
),
73 if (ndr
->offset
> ndr
->relative_highest_offset
) {
74 highest_ofs
= ndr
->offset
;
76 highest_ofs
= ndr
->relative_highest_offset
;
79 torture_assert(tctx
, highest_ofs
== ndr
->data_size
,
81 "%d unread bytes", ndr
->data_size
- highest_ofs
));
83 if (check_fn
!= NULL
) {
84 ret
= check_fn(tctx
, ds
);
89 if (data
->push_fn
!= NULL
) {
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");
99 _PUBLIC_
struct torture_test
*_torture_suite_add_ndr_pullpush_test(
100 struct torture_suite
*suite
,
102 ndr_pull_flags_fn_t pull_fn
,
103 ndr_push_flags_fn_t push_fn
,
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
);
124 data
->ndr_flags
= ndr_flags
;
126 data
->struct_size
= struct_size
;
127 data
->pull_fn
= pull_fn
;
128 data
->push_fn
= push_fn
;
132 test
->dangerous
= false;
134 DLIST_ADD_END(tcase
->tests
, 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
;
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
));
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
),
183 if (ndr
->offset
> ndr
->relative_highest_offset
) {
184 highest_ofs
= ndr
->offset
;
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
));
195 return check_fn(tctx
, ds
);
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
,
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
);
222 data
->data_context
= db_in
;
224 data
->struct_size
= struct_size
;
225 data
->pull_fn
= pull_fn
;
228 test
->dangerous
= false;
230 DLIST_ADD_END(tcase
->tests
, test
);
235 static bool test_check_string_terminator(struct torture_context
*tctx
)
237 struct ndr_pull
*ndr
;
239 TALLOC_CTX
*mem_ctx
= tctx
;
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");
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");
281 static bool test_guid_from_string_valid(struct torture_context
*tctx
)
287 static bool test_guid_from_string_null(struct torture_context
*tctx
)
290 torture_assert_ntstatus_equal(tctx
, NT_STATUS_INVALID_PARAMETER
,
291 GUID_from_string(NULL
, &guid
),
296 static bool test_guid_from_string_invalid(struct torture_context
*tctx
)
301 const char *bad_guids
[] = {
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",
335 torture_fail(tctx
, "wrongly allowing invalid guids");
340 static bool test_guid_from_string(struct torture_context
*tctx
)
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 */
352 torture_assert_ntstatus_ok(tctx
,
353 GUID_from_string(guids
[0], &g1
),
354 "invalid return code");
357 exp
.time_hi_and_version
= 3;
358 exp
.clock_seq
[0] = 4;
359 exp
.clock_seq
[1] = 5;
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");
377 static bool test_guid_string_valid(struct torture_context
*tctx
)
382 g
.time_hi_and_version
= 3;
391 torture_assert_str_equal(tctx
, "00000001-0002-0003-0405-060708090a0b",
392 GUID_string(tctx
, &g
),
393 "parsing guid failed");
397 static bool test_guid_string2_valid(struct torture_context
*tctx
)
402 g
.time_hi_and_version
= 3;
411 torture_assert_str_equal(tctx
, "{00000001-0002-0003-0405-060708090a0b}",
412 GUID_string2(tctx
, &g
),
413 "parsing guid failed");
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,
424 0x8, 0x9, 0xa, 0xb };
425 DATA_BLOB exp
= data_blob_const(exp_guid
, 16);
427 { 0x0, 0x0, 0x0, 0x0,
430 0x0, 0x0, 0x0, 0x0 };
431 DATA_BLOB b
= data_blob_const(ndr_guid
, 16);
435 guid
.time_hi_and_version
= 3;
436 guid
.clock_seq
[0] = 4;
437 guid
.clock_seq
[1] = 5;
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
,
449 torture_assert_data_blob_equal(tctx
, b
, exp
,
450 "GUID packed wrongly");
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
;
460 { 0x0, 0x0, 0x0, 0x0,
463 0x0, 0x0, 0x0, 0x0, 0x0 };
464 DATA_BLOB b
= data_blob_const(ndr_guid
, 17);
468 guid
.time_hi_and_version
= 3;
469 guid
.clock_seq
[0] = 4;
470 guid
.clock_seq
[1] = 5;
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
,
487 static bool test_guid_into_short_blob(struct torture_context
*tctx
)
489 enum ndr_err_code ndr_err
;
491 { 0x0, 0x0, 0x0, 0x0,
495 DATA_BLOB b
= data_blob_const(ndr_guid
, 15);
499 guid
.time_hi_and_version
= 3;
500 guid
.clock_seq
[0] = 4;
501 guid
.clock_seq
[1] = 5;
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
,
517 static bool test_compare_uuid(struct torture_context
*tctx
)
520 ZERO_STRUCT(g1
); ZERO_STRUCT(g2
);
521 torture_assert_int_equal(tctx
, 0, GUID_compare(&g1
, &g2
),
524 torture_assert_int_equal(tctx
, 1, GUID_compare(&g1
, &g2
),
525 "GUID diff invalid");
528 torture_assert_int_equal(tctx
, 1, GUID_compare(&g1
, &g2
),
529 "GUID diff invalid");
532 g1
.clock_seq
[1] = 20;
533 torture_assert_int_equal(tctx
, 1, GUID_compare(&g1
, &g2
),
534 "GUID diff invalid");
537 torture_assert_int_equal(tctx
, 1, GUID_compare(&g1
, &g2
),
538 "GUID diff invalid");
542 torture_assert_int_equal(tctx
, -1, GUID_compare(&g1
, &g2
),
543 "GUID diff invalid");
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",
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
);