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"
27 struct ndr_pull_test_data {
29 DATA_BLOB data_context;
31 ndr_pull_flags_fn_t pull_fn;
32 ndr_push_flags_fn_t push_fn;
37 static bool wrap_ndr_pullpush_test(struct torture_context *tctx,
38 struct torture_tcase *tcase,
39 struct torture_test *test)
41 bool (*check_fn) (struct torture_context *ctx, void *data) = test->fn;
42 const struct ndr_pull_test_data *data = (const struct ndr_pull_test_data *)test->data;
43 struct ndr_pull *ndr = ndr_pull_init_blob(&(data->data), tctx);
44 void *ds = talloc_zero_size(ndr, data->struct_size);
48 ndr->flags |= data->flags;
50 ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
52 torture_assert_ndr_success(tctx, data->pull_fn(ndr, data->ndr_flags, ds),
55 if (ndr->offset > ndr->relative_highest_offset) {
56 highest_ofs = ndr->offset;
58 highest_ofs = ndr->relative_highest_offset;
61 torture_assert(tctx, highest_ofs == ndr->data_size,
63 "%d unread bytes", ndr->data_size - highest_ofs));
65 if (check_fn != NULL) {
66 ret = check_fn(tctx, ds);
71 if (data->push_fn != NULL) {
73 torture_assert_ndr_success(tctx, ndr_push_struct_blob(&outblob, ndr, ds, data->push_fn), "pushing");
74 torture_assert_data_blob_equal(tctx, outblob, data->data, "ndr push compare");
81 _PUBLIC_ struct torture_test *_torture_suite_add_ndr_pullpush_test(
82 struct torture_suite *suite,
84 ndr_pull_flags_fn_t pull_fn,
85 ndr_push_flags_fn_t push_fn,
90 bool (*check_fn) (struct torture_context *ctx, void *data))
92 struct torture_test *test;
93 struct torture_tcase *tcase;
94 struct ndr_pull_test_data *data;
96 tcase = torture_suite_add_tcase(suite, name);
98 test = talloc(tcase, struct torture_test);
100 test->name = talloc_strdup(test, name);
101 test->description = NULL;
102 test->run = wrap_ndr_pullpush_test;
104 data = talloc(test, struct ndr_pull_test_data);
106 data->ndr_flags = ndr_flags;
108 data->struct_size = struct_size;
109 data->pull_fn = pull_fn;
110 data->push_fn = push_fn;
114 test->dangerous = false;
116 DLIST_ADD_END(tcase->tests, test, struct torture_test *);
122 static bool wrap_ndr_inout_pull_test(struct torture_context *tctx,
123 struct torture_tcase *tcase,
124 struct torture_test *test)
126 bool (*check_fn) (struct torture_context *ctx, void *data) = test->fn;
127 const struct ndr_pull_test_data *data = (const struct ndr_pull_test_data *)test->data;
128 void *ds = talloc_zero_size(tctx, data->struct_size);
129 struct ndr_pull *ndr;
130 uint32_t highest_ofs;
132 /* handle NDR_IN context */
134 ndr = ndr_pull_init_blob(&(data->data_context), tctx);
135 torture_assert(tctx, ndr, "ndr init failed");
137 ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
139 torture_assert_ndr_success(tctx,
140 data->pull_fn(ndr, NDR_IN, ds),
141 "ndr pull of context failed");
143 if (ndr->offset > ndr->relative_highest_offset) {
144 highest_ofs = ndr->offset;
146 highest_ofs = ndr->relative_highest_offset;
149 torture_assert(tctx, highest_ofs == ndr->data_size,
150 talloc_asprintf(tctx, "%d unread bytes", ndr->data_size - highest_ofs));
156 ndr = ndr_pull_init_blob(&(data->data), tctx);
157 torture_assert(tctx, ndr, "ndr init failed");
159 ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
161 torture_assert_ndr_success(tctx,
162 data->pull_fn(ndr, NDR_OUT, ds),
165 if (ndr->offset > ndr->relative_highest_offset) {
166 highest_ofs = ndr->offset;
168 highest_ofs = ndr->relative_highest_offset;
171 torture_assert(tctx, highest_ofs == ndr->data_size,
172 talloc_asprintf(tctx, "%d unread bytes", ndr->data_size - highest_ofs));
177 return check_fn(tctx, ds);
183 _PUBLIC_ struct torture_test *_torture_suite_add_ndr_pull_inout_test(
184 struct torture_suite *suite,
185 const char *name, ndr_pull_flags_fn_t pull_fn,
189 bool (*check_fn) (struct torture_context *ctx, void *data))
191 struct torture_test *test;
192 struct torture_tcase *tcase;
193 struct ndr_pull_test_data *data;
195 tcase = torture_suite_add_tcase(suite, name);
197 test = talloc(tcase, struct torture_test);
199 test->name = talloc_strdup(test, name);
200 test->description = NULL;
201 test->run = wrap_ndr_inout_pull_test;
202 data = talloc(test, struct ndr_pull_test_data);
204 data->data_context = db_in;
206 data->struct_size = struct_size;
207 data->pull_fn = pull_fn;
210 test->dangerous = false;
212 DLIST_ADD_END(tcase->tests, test, struct torture_test *);
217 static bool test_check_string_terminator(struct torture_context *tctx)
219 struct ndr_pull *ndr;
221 TALLOC_CTX *mem_ctx = tctx;
224 blob = strhex_to_data_blob(tctx, "0000");
226 ndr = ndr_pull_init_blob(&blob, mem_ctx);
228 torture_assert_ndr_success(tctx, ndr_check_string_terminator(ndr, 1, 2),
229 "simple check_string_terminator test failed");
231 torture_assert(tctx, ndr->offset == 0,
232 "check_string_terminator did not reset offset");
234 if (NDR_ERR_CODE_IS_SUCCESS(ndr_check_string_terminator(ndr, 1, 3))) {
235 torture_fail(tctx, "check_string_terminator checked beyond string boundaries");
238 torture_assert(tctx, ndr->offset == 0,
239 "check_string_terminator did not reset offset");
243 blob = strhex_to_data_blob(tctx, "11220000");
244 ndr = ndr_pull_init_blob(&blob, mem_ctx);
246 torture_assert_ndr_success(tctx,
247 ndr_check_string_terminator(ndr, 4, 1),
248 "check_string_terminator failed to recognize terminator");
250 torture_assert_ndr_success(tctx,
251 ndr_check_string_terminator(ndr, 3, 1),
252 "check_string_terminator failed to recognize terminator");
254 if (NDR_ERR_CODE_IS_SUCCESS(ndr_check_string_terminator(ndr, 2, 1))) {
255 torture_fail(tctx, "check_string_terminator erroneously reported terminator");
258 torture_assert(tctx, ndr->offset == 0,
259 "check_string_terminator did not reset offset");
263 static bool test_guid_from_string_valid(struct torture_context *tctx)
269 static bool test_guid_from_string_null(struct torture_context *tctx)
272 torture_assert_ntstatus_equal(tctx, NT_STATUS_INVALID_PARAMETER,
273 GUID_from_string(NULL, &guid),
278 static bool test_guid_from_string_invalid(struct torture_context *tctx)
281 torture_assert_ntstatus_equal(tctx, NT_STATUS_INVALID_PARAMETER,
282 GUID_from_string("bla", &g1),
283 "parameter not invalid");
287 static bool test_guid_from_string(struct torture_context *tctx)
290 torture_assert_ntstatus_ok(tctx,
291 GUID_from_string("00000001-0002-0003-0405-060708090a0b", &g1),
292 "invalid return code");
295 exp.time_hi_and_version = 3;
296 exp.clock_seq[0] = 4;
297 exp.clock_seq[1] = 5;
304 torture_assert(tctx, GUID_equal(&g1, &exp), "UUID parsed incorrectly");
305 torture_assert_ntstatus_ok(tctx,
306 GUID_from_string("{00000001-0002-0003-0405-060708090a0b}", &g1),
307 "invalid return code");
308 torture_assert(tctx, GUID_equal(&g1, &exp), "UUID parsed incorrectly");
313 static bool test_guid_string_valid(struct torture_context *tctx)
318 g.time_hi_and_version = 3;
327 torture_assert_str_equal(tctx, "00000001-0002-0003-0405-060708090a0b",
328 GUID_string(tctx, &g),
329 "parsing guid failed");
333 static bool test_guid_string2_valid(struct torture_context *tctx)
338 g.time_hi_and_version = 3;
347 torture_assert_str_equal(tctx, "{00000001-0002-0003-0405-060708090a0b}",
348 GUID_string2(tctx, &g),
349 "parsing guid failed");
353 static bool test_compare_uuid(struct torture_context *tctx)
356 ZERO_STRUCT(g1); ZERO_STRUCT(g2);
357 torture_assert_int_equal(tctx, 0, GUID_compare(&g1, &g2),
360 torture_assert_int_equal(tctx, 1, GUID_compare(&g1, &g2),
361 "GUID diff invalid");
364 torture_assert_int_equal(tctx, 1, GUID_compare(&g1, &g2),
365 "GUID diff invalid");
368 g1.clock_seq[1] = 20;
369 torture_assert_int_equal(tctx, 1, GUID_compare(&g1, &g2),
370 "GUID diff invalid");
373 torture_assert_int_equal(tctx, 1, GUID_compare(&g1, &g2),
374 "GUID diff invalid");
378 torture_assert_int_equal(tctx, -1, GUID_compare(&g1, &g2),
379 "GUID diff invalid");
383 struct torture_suite *torture_local_ndr(TALLOC_CTX *mem_ctx)
385 struct torture_suite *suite = torture_suite_create(mem_ctx, "ndr");
387 torture_suite_add_suite(suite, ndr_winreg_suite(suite));
388 torture_suite_add_suite(suite, ndr_atsvc_suite(suite));
389 torture_suite_add_suite(suite, ndr_lsa_suite(suite));
390 torture_suite_add_suite(suite, ndr_epmap_suite(suite));
391 torture_suite_add_suite(suite, ndr_dfs_suite(suite));
392 torture_suite_add_suite(suite, ndr_dfsblob_suite(suite));
393 torture_suite_add_suite(suite, ndr_netlogon_suite(suite));
394 torture_suite_add_suite(suite, ndr_drsuapi_suite(suite));
395 torture_suite_add_suite(suite, ndr_spoolss_suite(suite));
396 torture_suite_add_suite(suite, ndr_ntprinting_suite(suite));
397 torture_suite_add_suite(suite, ndr_samr_suite(suite));
398 torture_suite_add_suite(suite, ndr_drsblobs_suite(suite));
399 torture_suite_add_suite(suite, ndr_nbt_suite(suite));
400 torture_suite_add_suite(suite, ndr_ntlmssp_suite(suite));
401 torture_suite_add_suite(suite, ndr_backupkey_suite(suite));
402 torture_suite_add_suite(suite, ndr_string_suite(suite));
404 torture_suite_add_simple_test(suite, "string terminator",
405 test_check_string_terminator);
407 torture_suite_add_simple_test(suite, "guid_from_string_null",
408 test_guid_from_string_null);
410 torture_suite_add_simple_test(suite, "guid_from_string",
411 test_guid_from_string);
413 torture_suite_add_simple_test(suite, "guid_from_string_invalid",
414 test_guid_from_string_invalid);
416 torture_suite_add_simple_test(suite, "guid_string_valid",
417 test_guid_string_valid);
419 torture_suite_add_simple_test(suite, "guid_string2_valid",
420 test_guid_string2_valid);
422 torture_suite_add_simple_test(suite, "guid_from_string_valid",
423 test_guid_from_string_valid);
425 torture_suite_add_simple_test(suite, "compare_uuid",