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
;
36 static bool wrap_ndr_pullpush_test(struct torture_context
*tctx
,
37 struct torture_tcase
*tcase
,
38 struct torture_test
*test
)
40 bool (*check_fn
) (struct torture_context
*ctx
, void *data
) = test
->fn
;
41 const struct ndr_pull_test_data
*data
= (const struct ndr_pull_test_data
*)test
->data
;
42 struct ndr_pull
*ndr
= ndr_pull_init_blob(&(data
->data
), tctx
);
43 void *ds
= talloc_zero_size(ndr
, data
->struct_size
);
46 ndr
->flags
|= LIBNDR_FLAG_REF_ALLOC
;
48 torture_assert_ndr_success(tctx
, data
->pull_fn(ndr
, data
->ndr_flags
, ds
),
51 torture_assert(tctx
, ndr
->offset
== ndr
->data_size
,
53 "%d unread bytes", ndr
->data_size
- ndr
->offset
));
55 if (check_fn
!= NULL
) {
56 ret
= check_fn(tctx
, ds
);
61 if (data
->push_fn
!= NULL
) {
63 torture_assert_ndr_success(tctx
, ndr_push_struct_blob(&outblob
, ndr
, ds
, data
->push_fn
), "pushing");
64 torture_assert_data_blob_equal(tctx
, outblob
, data
->data
, "ndr push compare");
71 _PUBLIC_
struct torture_test
*_torture_suite_add_ndr_pullpush_test(
72 struct torture_suite
*suite
,
74 ndr_pull_flags_fn_t pull_fn
,
75 ndr_push_flags_fn_t push_fn
,
79 bool (*check_fn
) (struct torture_context
*ctx
, void *data
))
81 struct torture_test
*test
;
82 struct torture_tcase
*tcase
;
83 struct ndr_pull_test_data
*data
;
85 tcase
= torture_suite_add_tcase(suite
, name
);
87 test
= talloc(tcase
, struct torture_test
);
89 test
->name
= talloc_strdup(test
, name
);
90 test
->description
= NULL
;
91 test
->run
= wrap_ndr_pullpush_test
;
93 data
= talloc(test
, struct ndr_pull_test_data
);
95 data
->ndr_flags
= ndr_flags
;
96 data
->struct_size
= struct_size
;
97 data
->pull_fn
= pull_fn
;
98 data
->push_fn
= push_fn
;
102 test
->dangerous
= false;
104 DLIST_ADD_END(tcase
->tests
, test
, struct torture_test
*);
110 static bool wrap_ndr_inout_pull_test(struct torture_context
*tctx
,
111 struct torture_tcase
*tcase
,
112 struct torture_test
*test
)
114 bool (*check_fn
) (struct torture_context
*ctx
, void *data
) = test
->fn
;
115 const struct ndr_pull_test_data
*data
= (const struct ndr_pull_test_data
*)test
->data
;
116 void *ds
= talloc_zero_size(tctx
, data
->struct_size
);
117 struct ndr_pull
*ndr
;
119 /* handle NDR_IN context */
121 ndr
= ndr_pull_init_blob(&(data
->data_context
), tctx
);
122 torture_assert(tctx
, ndr
, "ndr init failed");
124 ndr
->flags
|= LIBNDR_FLAG_REF_ALLOC
;
126 torture_assert_ndr_success(tctx
,
127 data
->pull_fn(ndr
, NDR_IN
, ds
),
128 "ndr pull of context failed");
130 torture_assert(tctx
, ndr
->offset
== ndr
->data_size
,
131 talloc_asprintf(tctx
, "%d unread bytes", ndr
->data_size
- ndr
->offset
));
137 ndr
= ndr_pull_init_blob(&(data
->data
), tctx
);
138 torture_assert(tctx
, ndr
, "ndr init failed");
140 ndr
->flags
|= LIBNDR_FLAG_REF_ALLOC
;
142 torture_assert_ndr_success(tctx
,
143 data
->pull_fn(ndr
, NDR_OUT
, ds
),
146 torture_assert(tctx
, ndr
->offset
== ndr
->data_size
,
147 talloc_asprintf(tctx
, "%d unread bytes", ndr
->data_size
- ndr
->offset
));
152 return check_fn(tctx
, ds
);
158 _PUBLIC_
struct torture_test
*_torture_suite_add_ndr_pull_inout_test(
159 struct torture_suite
*suite
,
160 const char *name
, ndr_pull_flags_fn_t pull_fn
,
164 bool (*check_fn
) (struct torture_context
*ctx
, void *data
))
166 struct torture_test
*test
;
167 struct torture_tcase
*tcase
;
168 struct ndr_pull_test_data
*data
;
170 tcase
= torture_suite_add_tcase(suite
, name
);
172 test
= talloc(tcase
, struct torture_test
);
174 test
->name
= talloc_strdup(test
, name
);
175 test
->description
= NULL
;
176 test
->run
= wrap_ndr_inout_pull_test
;
177 data
= talloc(test
, struct ndr_pull_test_data
);
179 data
->data_context
= db_in
;
181 data
->struct_size
= struct_size
;
182 data
->pull_fn
= pull_fn
;
185 test
->dangerous
= false;
187 DLIST_ADD_END(tcase
->tests
, test
, struct torture_test
*);
192 static bool test_check_string_terminator(struct torture_context
*tctx
)
194 struct ndr_pull
*ndr
;
196 TALLOC_CTX
*mem_ctx
= tctx
;
199 blob
= strhex_to_data_blob(tctx
, "0000");
201 ndr
= ndr_pull_init_blob(&blob
, mem_ctx
);
203 torture_assert_ndr_success(tctx
, ndr_check_string_terminator(ndr
, 1, 2),
204 "simple check_string_terminator test failed");
206 torture_assert(tctx
, ndr
->offset
== 0,
207 "check_string_terminator did not reset offset");
209 if (NDR_ERR_CODE_IS_SUCCESS(ndr_check_string_terminator(ndr
, 1, 3))) {
210 torture_fail(tctx
, "check_string_terminator checked beyond string boundaries");
213 torture_assert(tctx
, ndr
->offset
== 0,
214 "check_string_terminator did not reset offset");
218 blob
= strhex_to_data_blob(tctx
, "11220000");
219 ndr
= ndr_pull_init_blob(&blob
, mem_ctx
);
221 torture_assert_ndr_success(tctx
,
222 ndr_check_string_terminator(ndr
, 4, 1),
223 "check_string_terminator failed to recognize terminator");
225 torture_assert_ndr_success(tctx
,
226 ndr_check_string_terminator(ndr
, 3, 1),
227 "check_string_terminator failed to recognize terminator");
229 if (NDR_ERR_CODE_IS_SUCCESS(ndr_check_string_terminator(ndr
, 2, 1))) {
230 torture_fail(tctx
, "check_string_terminator erroneously reported terminator");
233 torture_assert(tctx
, ndr
->offset
== 0,
234 "check_string_terminator did not reset offset");
238 static bool test_guid_from_string_valid(struct torture_context
*tctx
)
244 static bool test_guid_from_string_null(struct torture_context
*tctx
)
247 torture_assert_ntstatus_equal(tctx
, NT_STATUS_INVALID_PARAMETER
,
248 GUID_from_string(NULL
, &guid
),
253 static bool test_guid_from_string_invalid(struct torture_context
*tctx
)
256 torture_assert_ntstatus_equal(tctx
, NT_STATUS_INVALID_PARAMETER
,
257 GUID_from_string("bla", &g1
),
258 "parameter not invalid");
262 static bool test_guid_from_string(struct torture_context
*tctx
)
265 torture_assert_ntstatus_ok(tctx
,
266 GUID_from_string("00000001-0002-0003-0405-060708090a0b", &g1
),
267 "invalid return code");
270 exp
.time_hi_and_version
= 3;
271 exp
.clock_seq
[0] = 4;
272 exp
.clock_seq
[1] = 5;
279 torture_assert(tctx
, GUID_equal(&g1
, &exp
), "UUID parsed incorrectly");
280 torture_assert_ntstatus_ok(tctx
,
281 GUID_from_string("{00000001-0002-0003-0405-060708090a0b}", &g1
),
282 "invalid return code");
283 torture_assert(tctx
, GUID_equal(&g1
, &exp
), "UUID parsed incorrectly");
288 static bool test_guid_string_valid(struct torture_context
*tctx
)
293 g
.time_hi_and_version
= 3;
302 torture_assert_str_equal(tctx
, "00000001-0002-0003-0405-060708090a0b",
303 GUID_string(tctx
, &g
),
304 "parsing guid failed");
308 static bool test_guid_string2_valid(struct torture_context
*tctx
)
313 g
.time_hi_and_version
= 3;
322 torture_assert_str_equal(tctx
, "{00000001-0002-0003-0405-060708090a0b}",
323 GUID_string2(tctx
, &g
),
324 "parsing guid failed");
328 static bool test_compare_uuid(struct torture_context
*tctx
)
331 ZERO_STRUCT(g1
); ZERO_STRUCT(g2
);
332 torture_assert_int_equal(tctx
, 0, GUID_compare(&g1
, &g2
),
335 torture_assert_int_equal(tctx
, 1, GUID_compare(&g1
, &g2
),
336 "GUID diff invalid");
339 torture_assert_int_equal(tctx
, 1, GUID_compare(&g1
, &g2
),
340 "GUID diff invalid");
343 g1
.clock_seq
[1] = 20;
344 torture_assert_int_equal(tctx
, 1, GUID_compare(&g1
, &g2
),
345 "GUID diff invalid");
348 torture_assert_int_equal(tctx
, 1, GUID_compare(&g1
, &g2
),
349 "GUID diff invalid");
353 torture_assert_int_equal(tctx
, -1, GUID_compare(&g1
, &g2
),
354 "GUID diff invalid");
358 struct torture_suite
*torture_local_ndr(TALLOC_CTX
*mem_ctx
)
360 struct torture_suite
*suite
= torture_suite_create(mem_ctx
, "ndr");
362 torture_suite_add_suite(suite
, ndr_winreg_suite(suite
));
363 torture_suite_add_suite(suite
, ndr_atsvc_suite(suite
));
364 torture_suite_add_suite(suite
, ndr_lsa_suite(suite
));
365 torture_suite_add_suite(suite
, ndr_epmap_suite(suite
));
366 torture_suite_add_suite(suite
, ndr_dfs_suite(suite
));
367 torture_suite_add_suite(suite
, ndr_dfsblob_suite(suite
));
368 torture_suite_add_suite(suite
, ndr_netlogon_suite(suite
));
369 torture_suite_add_suite(suite
, ndr_drsuapi_suite(suite
));
370 torture_suite_add_suite(suite
, ndr_spoolss_suite(suite
));
371 torture_suite_add_suite(suite
, ndr_samr_suite(suite
));
372 torture_suite_add_suite(suite
, ndr_drsblobs_suite(suite
));
373 torture_suite_add_suite(suite
, ndr_nbt_suite(suite
));
374 torture_suite_add_suite(suite
, ndr_ntlmssp_suite(suite
));
375 #ifdef SAMBA4_USES_HEIMDAL /* Add Heimdal-specific KDC test */
376 torture_suite_add_suite(suite
, ndr_backupkey_suite(suite
));
378 torture_suite_add_suite(suite
, ndr_string_suite(suite
));
380 torture_suite_add_simple_test(suite
, "string terminator",
381 test_check_string_terminator
);
383 torture_suite_add_simple_test(suite
, "guid_from_string_null",
384 test_guid_from_string_null
);
386 torture_suite_add_simple_test(suite
, "guid_from_string",
387 test_guid_from_string
);
389 torture_suite_add_simple_test(suite
, "guid_from_string_invalid",
390 test_guid_from_string_invalid
);
392 torture_suite_add_simple_test(suite
, "guid_string_valid",
393 test_guid_string_valid
);
395 torture_suite_add_simple_test(suite
, "guid_string2_valid",
396 test_guid_string2_valid
);
398 torture_suite_add_simple_test(suite
, "guid_from_string_valid",
399 test_guid_from_string_valid
);
401 torture_suite_add_simple_test(suite
, "compare_uuid",