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
)
299 torture_assert_ntstatus_equal(tctx
, NT_STATUS_INVALID_PARAMETER
,
300 GUID_from_string("bla", &g1
),
301 "parameter not invalid");
305 static bool test_guid_from_string(struct torture_context
*tctx
)
308 torture_assert_ntstatus_ok(tctx
,
309 GUID_from_string("00000001-0002-0003-0405-060708090a0b", &g1
),
310 "invalid return code");
313 exp
.time_hi_and_version
= 3;
314 exp
.clock_seq
[0] = 4;
315 exp
.clock_seq
[1] = 5;
322 torture_assert(tctx
, GUID_equal(&g1
, &exp
), "UUID parsed incorrectly");
323 torture_assert_ntstatus_ok(tctx
,
324 GUID_from_string("{00000001-0002-0003-0405-060708090a0b}", &g1
),
325 "invalid return code");
326 torture_assert(tctx
, GUID_equal(&g1
, &exp
), "UUID parsed incorrectly");
331 static bool test_guid_string_valid(struct torture_context
*tctx
)
336 g
.time_hi_and_version
= 3;
345 torture_assert_str_equal(tctx
, "00000001-0002-0003-0405-060708090a0b",
346 GUID_string(tctx
, &g
),
347 "parsing guid failed");
351 static bool test_guid_string2_valid(struct torture_context
*tctx
)
356 g
.time_hi_and_version
= 3;
365 torture_assert_str_equal(tctx
, "{00000001-0002-0003-0405-060708090a0b}",
366 GUID_string2(tctx
, &g
),
367 "parsing guid failed");
371 static bool test_guid_into_blob(struct torture_context
*tctx
)
373 enum ndr_err_code ndr_err
;
374 static const char exp_guid
[16] =
375 { 0x1, 0x0, 0x0, 0x0,
378 0x8, 0x9, 0xa, 0xb };
379 DATA_BLOB exp
= data_blob_const(exp_guid
, 16);
381 { 0x0, 0x0, 0x0, 0x0,
384 0x0, 0x0, 0x0, 0x0 };
385 DATA_BLOB b
= data_blob_const(ndr_guid
, 16);
389 guid
.time_hi_and_version
= 3;
390 guid
.clock_seq
[0] = 4;
391 guid
.clock_seq
[1] = 5;
399 ndr_err
= ndr_push_struct_into_fixed_blob(&b
, &guid
,
400 (ndr_push_flags_fn_t
)ndr_push_GUID
);
401 torture_assert_ndr_err_equal(tctx
, ndr_err
, NDR_ERR_SUCCESS
,
403 torture_assert_data_blob_equal(tctx
, b
, exp
,
404 "GUID packed wrongly");
409 /* Really a test of ndr_push_struct_into_fixed_blob error handling */
410 static bool test_guid_into_long_blob(struct torture_context
*tctx
)
412 enum ndr_err_code ndr_err
;
414 { 0x0, 0x0, 0x0, 0x0,
417 0x0, 0x0, 0x0, 0x0, 0x0 };
418 DATA_BLOB b
= data_blob_const(ndr_guid
, 17);
422 guid
.time_hi_and_version
= 3;
423 guid
.clock_seq
[0] = 4;
424 guid
.clock_seq
[1] = 5;
432 torture_assert(tctx
, b
.data
!= NULL
, "data_blob_talloc failed");
433 ndr_err
= ndr_push_struct_into_fixed_blob(
434 &b
, &guid
, (ndr_push_flags_fn_t
)ndr_push_GUID
);
435 torture_assert_ndr_err_equal(tctx
, ndr_err
, NDR_ERR_BUFSIZE
,
441 static bool test_guid_into_short_blob(struct torture_context
*tctx
)
443 enum ndr_err_code ndr_err
;
445 { 0x0, 0x0, 0x0, 0x0,
449 DATA_BLOB b
= data_blob_const(ndr_guid
, 15);
453 guid
.time_hi_and_version
= 3;
454 guid
.clock_seq
[0] = 4;
455 guid
.clock_seq
[1] = 5;
463 ndr_err
= ndr_push_struct_into_fixed_blob(
464 &b
, &guid
, (ndr_push_flags_fn_t
)ndr_push_GUID
);
465 torture_assert_ndr_err_equal(tctx
, ndr_err
, NDR_ERR_BUFSIZE
,
471 static bool test_compare_uuid(struct torture_context
*tctx
)
474 ZERO_STRUCT(g1
); ZERO_STRUCT(g2
);
475 torture_assert_int_equal(tctx
, 0, GUID_compare(&g1
, &g2
),
478 torture_assert_int_equal(tctx
, 1, GUID_compare(&g1
, &g2
),
479 "GUID diff invalid");
482 torture_assert_int_equal(tctx
, 1, GUID_compare(&g1
, &g2
),
483 "GUID diff invalid");
486 g1
.clock_seq
[1] = 20;
487 torture_assert_int_equal(tctx
, 1, GUID_compare(&g1
, &g2
),
488 "GUID diff invalid");
491 torture_assert_int_equal(tctx
, 1, GUID_compare(&g1
, &g2
),
492 "GUID diff invalid");
496 torture_assert_int_equal(tctx
, -1, GUID_compare(&g1
, &g2
),
497 "GUID diff invalid");
501 struct torture_suite
*torture_local_ndr(TALLOC_CTX
*mem_ctx
)
503 struct torture_suite
*suite
= torture_suite_create(mem_ctx
, "ndr");
505 torture_suite_add_suite(suite
, ndr_winreg_suite(suite
));
506 torture_suite_add_suite(suite
, ndr_atsvc_suite(suite
));
507 torture_suite_add_suite(suite
, ndr_lsa_suite(suite
));
508 torture_suite_add_suite(suite
, ndr_epmap_suite(suite
));
509 torture_suite_add_suite(suite
, ndr_dfs_suite(suite
));
510 torture_suite_add_suite(suite
, ndr_dfsblob_suite(suite
));
511 torture_suite_add_suite(suite
, ndr_netlogon_suite(suite
));
512 torture_suite_add_suite(suite
, ndr_drsuapi_suite(suite
));
513 torture_suite_add_suite(suite
, ndr_spoolss_suite(suite
));
514 torture_suite_add_suite(suite
, ndr_ntprinting_suite(suite
));
515 torture_suite_add_suite(suite
, ndr_samr_suite(suite
));
516 torture_suite_add_suite(suite
, ndr_drsblobs_suite(suite
));
517 torture_suite_add_suite(suite
, ndr_nbt_suite(suite
));
518 torture_suite_add_suite(suite
, ndr_ntlmssp_suite(suite
));
519 torture_suite_add_suite(suite
, ndr_backupkey_suite(suite
));
520 torture_suite_add_suite(suite
, ndr_witness_suite(suite
));
521 torture_suite_add_suite(suite
, ndr_clusapi_suite(suite
));
522 torture_suite_add_suite(suite
, ndr_negoex_suite(suite
));
523 torture_suite_add_suite(suite
, ndr_string_suite(suite
));
524 torture_suite_add_suite(suite
, ndr_krb5pac_suite(suite
));
526 torture_suite_add_simple_test(suite
, "string terminator",
527 test_check_string_terminator
);
529 torture_suite_add_simple_test(suite
, "guid_from_string_null",
530 test_guid_from_string_null
);
532 torture_suite_add_simple_test(suite
, "guid_from_string",
533 test_guid_from_string
);
535 torture_suite_add_simple_test(suite
, "guid_from_string_invalid",
536 test_guid_from_string_invalid
);
538 torture_suite_add_simple_test(suite
, "guid_string_valid",
539 test_guid_string_valid
);
541 torture_suite_add_simple_test(suite
, "guid_string2_valid",
542 test_guid_string2_valid
);
544 torture_suite_add_simple_test(suite
, "guid_from_string_valid",
545 test_guid_from_string_valid
);
547 torture_suite_add_simple_test(suite
, "compare_uuid",
550 torture_suite_add_simple_test(suite
, "guid_into_blob",
551 test_guid_into_blob
);
553 torture_suite_add_simple_test(suite
, "guid_into_short_blob",
554 test_guid_into_short_blob
);
556 torture_suite_add_simple_test(suite
, "guid_into_long_blob",
557 test_guid_into_long_blob
);