torture: Add tests for ndr_push_struct_into_fixed_blob()
[Samba.git] / source4 / torture / ndr / ndr.c
blob4541f9f08471d0015e6dbfc52779827e0888f43f
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 torture_assert_ntstatus_equal(tctx, NT_STATUS_INVALID_PARAMETER,
300 GUID_from_string("bla", &g1),
301 "parameter not invalid");
302 return true;
305 static bool test_guid_from_string(struct torture_context *tctx)
307 struct GUID g1, exp;
308 torture_assert_ntstatus_ok(tctx,
309 GUID_from_string("00000001-0002-0003-0405-060708090a0b", &g1),
310 "invalid return code");
311 exp.time_low = 1;
312 exp.time_mid = 2;
313 exp.time_hi_and_version = 3;
314 exp.clock_seq[0] = 4;
315 exp.clock_seq[1] = 5;
316 exp.node[0] = 6;
317 exp.node[1] = 7;
318 exp.node[2] = 8;
319 exp.node[3] = 9;
320 exp.node[4] = 10;
321 exp.node[5] = 11;
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");
328 return true;
331 static bool test_guid_string_valid(struct torture_context *tctx)
333 struct GUID g;
334 g.time_low = 1;
335 g.time_mid = 2;
336 g.time_hi_and_version = 3;
337 g.clock_seq[0] = 4;
338 g.clock_seq[1] = 5;
339 g.node[0] = 6;
340 g.node[1] = 7;
341 g.node[2] = 8;
342 g.node[3] = 9;
343 g.node[4] = 10;
344 g.node[5] = 11;
345 torture_assert_str_equal(tctx, "00000001-0002-0003-0405-060708090a0b",
346 GUID_string(tctx, &g),
347 "parsing guid failed");
348 return true;
351 static bool test_guid_string2_valid(struct torture_context *tctx)
353 struct GUID g;
354 g.time_low = 1;
355 g.time_mid = 2;
356 g.time_hi_and_version = 3;
357 g.clock_seq[0] = 4;
358 g.clock_seq[1] = 5;
359 g.node[0] = 6;
360 g.node[1] = 7;
361 g.node[2] = 8;
362 g.node[3] = 9;
363 g.node[4] = 10;
364 g.node[5] = 11;
365 torture_assert_str_equal(tctx, "{00000001-0002-0003-0405-060708090a0b}",
366 GUID_string2(tctx, &g),
367 "parsing guid failed");
368 return true;
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,
376 0x2, 0x0, 0x3, 0x0,
377 0x4, 0x5, 0x6, 0x7,
378 0x8, 0x9, 0xa, 0xb };
379 DATA_BLOB exp = data_blob_const(exp_guid, 16);
380 char ndr_guid[16] =
381 { 0x0, 0x0, 0x0, 0x0,
382 0x0, 0x0, 0x0, 0x0,
383 0x0, 0x0, 0x0, 0x0,
384 0x0, 0x0, 0x0, 0x0 };
385 DATA_BLOB b = data_blob_const(ndr_guid, 16);
386 struct GUID guid;
387 guid.time_low = 1;
388 guid.time_mid = 2;
389 guid.time_hi_and_version = 3;
390 guid.clock_seq[0] = 4;
391 guid.clock_seq[1] = 5;
392 guid.node[0] = 6;
393 guid.node[1] = 7;
394 guid.node[2] = 8;
395 guid.node[3] = 9;
396 guid.node[4] = 10;
397 guid.node[5] = 11;
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,
402 "wrong NDR error");
403 torture_assert_data_blob_equal(tctx, b, exp,
404 "GUID packed wrongly");
406 return true;
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;
413 char ndr_guid[17] =
414 { 0x0, 0x0, 0x0, 0x0,
415 0x0, 0x0, 0x0, 0x0,
416 0x0, 0x0, 0x0, 0x0,
417 0x0, 0x0, 0x0, 0x0, 0x0 };
418 DATA_BLOB b = data_blob_const(ndr_guid, 17);
419 struct GUID guid;
420 guid.time_low = 1;
421 guid.time_mid = 2;
422 guid.time_hi_and_version = 3;
423 guid.clock_seq[0] = 4;
424 guid.clock_seq[1] = 5;
425 guid.node[0] = 6;
426 guid.node[1] = 7;
427 guid.node[2] = 8;
428 guid.node[3] = 9;
429 guid.node[4] = 10;
430 guid.node[5] = 11;
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,
436 "wrong NDR error");
438 return true;
441 static bool test_guid_into_short_blob(struct torture_context *tctx)
443 enum ndr_err_code ndr_err;
444 char ndr_guid[15] =
445 { 0x0, 0x0, 0x0, 0x0,
446 0x0, 0x0, 0x0, 0x0,
447 0x0, 0x0, 0x0, 0x0,
448 0x0, 0x0, 0x0 };
449 DATA_BLOB b = data_blob_const(ndr_guid, 15);
450 struct GUID guid;
451 guid.time_low = 1;
452 guid.time_mid = 2;
453 guid.time_hi_and_version = 3;
454 guid.clock_seq[0] = 4;
455 guid.clock_seq[1] = 5;
456 guid.node[0] = 6;
457 guid.node[1] = 7;
458 guid.node[2] = 8;
459 guid.node[3] = 9;
460 guid.node[4] = 10;
461 guid.node[5] = 11;
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,
466 "wrong NDR error");
468 return true;
471 static bool test_compare_uuid(struct torture_context *tctx)
473 struct GUID g1, g2;
474 ZERO_STRUCT(g1); ZERO_STRUCT(g2);
475 torture_assert_int_equal(tctx, 0, GUID_compare(&g1, &g2),
476 "GUIDs not equal");
477 g1.time_low = 1;
478 torture_assert_int_equal(tctx, 1, GUID_compare(&g1, &g2),
479 "GUID diff invalid");
481 g1.time_low = 10;
482 torture_assert_int_equal(tctx, 1, GUID_compare(&g1, &g2),
483 "GUID diff invalid");
485 g1.time_low = 0;
486 g1.clock_seq[1] = 20;
487 torture_assert_int_equal(tctx, 1, GUID_compare(&g1, &g2),
488 "GUID diff invalid");
490 g1.time_low = ~0;
491 torture_assert_int_equal(tctx, 1, GUID_compare(&g1, &g2),
492 "GUID diff invalid");
494 g1.time_low = 0;
495 g2.time_low = ~0;
496 torture_assert_int_equal(tctx, -1, GUID_compare(&g1, &g2),
497 "GUID diff invalid");
498 return true;
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",
548 test_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);
559 return suite;