s4-torture: Always compile backupkey ndr testsuite.
[obnox/samba/samba-obnox.git] / source4 / torture / ndr / ndr.c
1 /*
2    Unix SMB/CIFS implementation.
3    test suite for winreg ndr operations
4
5    Copyright (C) Jelmer Vernooij 2007
6
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.
11
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.
16
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/>.
19 */
20
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
27 struct ndr_pull_test_data {
28         DATA_BLOB data;
29         DATA_BLOB data_context;
30         size_t struct_size;
31         ndr_pull_flags_fn_t pull_fn;
32         ndr_push_flags_fn_t push_fn;
33         int ndr_flags;
34         int flags;
35 };
36
37 static bool wrap_ndr_pullpush_test(struct torture_context *tctx,
38                                    struct torture_tcase *tcase,
39                                    struct torture_test *test)
40 {
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);
45         bool ret;
46         uint32_t highest_ofs;
47
48         ndr->flags |= data->flags;
49
50         ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
51
52         torture_assert_ndr_success(tctx, data->pull_fn(ndr, data->ndr_flags, ds),
53                                    "pulling");
54
55         if (ndr->offset > ndr->relative_highest_offset) {
56                 highest_ofs = ndr->offset;
57         } else {
58                 highest_ofs = ndr->relative_highest_offset;
59         }
60
61         torture_assert(tctx, highest_ofs == ndr->data_size,
62                                    talloc_asprintf(tctx,
63                                            "%d unread bytes", ndr->data_size - highest_ofs));
64
65         if (check_fn != NULL) {
66                 ret = check_fn(tctx, ds);
67         } else {
68                 ret = true;
69         }
70
71         if (data->push_fn != NULL) {
72                 DATA_BLOB outblob;
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");
75         }
76
77         talloc_free(ndr);
78         return ret;
79 }
80
81 _PUBLIC_ struct torture_test *_torture_suite_add_ndr_pullpush_test(
82         struct torture_suite *suite,
83         const char *name,
84         ndr_pull_flags_fn_t pull_fn,
85         ndr_push_flags_fn_t push_fn,
86         DATA_BLOB db,
87         size_t struct_size,
88         int ndr_flags,
89         int flags,
90         bool (*check_fn) (struct torture_context *ctx, void *data))
91 {
92         struct torture_test *test;
93         struct torture_tcase *tcase;
94         struct ndr_pull_test_data *data;
95
96         tcase = torture_suite_add_tcase(suite, name);
97
98         test = talloc(tcase, struct torture_test);
99
100         test->name = talloc_strdup(test, name);
101         test->description = NULL;
102         test->run = wrap_ndr_pullpush_test;
103
104         data = talloc(test, struct ndr_pull_test_data);
105         data->data = db;
106         data->ndr_flags = ndr_flags;
107         data->flags = flags;
108         data->struct_size = struct_size;
109         data->pull_fn = pull_fn;
110         data->push_fn = push_fn;
111
112         test->data = data;
113         test->fn = check_fn;
114         test->dangerous = false;
115
116         DLIST_ADD_END(tcase->tests, test, struct torture_test *);
117
118         return test;
119 }
120
121
122 static bool wrap_ndr_inout_pull_test(struct torture_context *tctx,
123                                      struct torture_tcase *tcase,
124                                      struct torture_test *test)
125 {
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;
131
132         /* handle NDR_IN context */
133
134         ndr = ndr_pull_init_blob(&(data->data_context), tctx);
135         torture_assert(tctx, ndr, "ndr init failed");
136
137         ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
138
139         torture_assert_ndr_success(tctx,
140                 data->pull_fn(ndr, NDR_IN, ds),
141                 "ndr pull of context failed");
142
143         if (ndr->offset > ndr->relative_highest_offset) {
144                 highest_ofs = ndr->offset;
145         } else {
146                 highest_ofs = ndr->relative_highest_offset;
147         }
148
149         torture_assert(tctx, highest_ofs == ndr->data_size,
150                 talloc_asprintf(tctx, "%d unread bytes", ndr->data_size - highest_ofs));
151
152         talloc_free(ndr);
153
154         /* handle NDR_OUT */
155
156         ndr = ndr_pull_init_blob(&(data->data), tctx);
157         torture_assert(tctx, ndr, "ndr init failed");
158
159         ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
160
161         torture_assert_ndr_success(tctx,
162                 data->pull_fn(ndr, NDR_OUT, ds),
163                 "ndr pull failed");
164
165         if (ndr->offset > ndr->relative_highest_offset) {
166                 highest_ofs = ndr->offset;
167         } else {
168                 highest_ofs = ndr->relative_highest_offset;
169         }
170
171         torture_assert(tctx, highest_ofs == ndr->data_size,
172                 talloc_asprintf(tctx, "%d unread bytes", ndr->data_size - highest_ofs));
173
174         talloc_free(ndr);
175
176         if (check_fn) {
177                 return check_fn(tctx, ds);
178         } else {
179                 return true;
180         }
181 }
182
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,
186                                         DATA_BLOB db_in,
187                                         DATA_BLOB db_out,
188                                         size_t struct_size,
189                                         bool (*check_fn) (struct torture_context *ctx, void *data))
190 {
191         struct torture_test *test;
192         struct torture_tcase *tcase;
193         struct ndr_pull_test_data *data;
194
195         tcase = torture_suite_add_tcase(suite, name);
196
197         test = talloc(tcase, struct torture_test);
198
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);
203         data->data = db_out;
204         data->data_context = db_in;
205         data->ndr_flags = 0;
206         data->struct_size = struct_size;
207         data->pull_fn = pull_fn;
208         test->data = data;
209         test->fn = check_fn;
210         test->dangerous = false;
211
212         DLIST_ADD_END(tcase->tests, test, struct torture_test *);
213
214         return test;
215 }
216
217 static bool test_check_string_terminator(struct torture_context *tctx)
218 {
219         struct ndr_pull *ndr;
220         DATA_BLOB blob;
221         TALLOC_CTX *mem_ctx = tctx;
222
223         /* Simple test */
224         blob = strhex_to_data_blob(tctx, "0000");
225
226         ndr = ndr_pull_init_blob(&blob, mem_ctx);
227
228         torture_assert_ndr_success(tctx, ndr_check_string_terminator(ndr, 1, 2),
229                                    "simple check_string_terminator test failed");
230
231         torture_assert(tctx, ndr->offset == 0,
232                 "check_string_terminator did not reset offset");
233
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");
236         }
237
238         torture_assert(tctx, ndr->offset == 0,
239                 "check_string_terminator did not reset offset");
240
241         talloc_free(ndr);
242
243         blob = strhex_to_data_blob(tctx, "11220000");
244         ndr = ndr_pull_init_blob(&blob, mem_ctx);
245
246         torture_assert_ndr_success(tctx,
247                 ndr_check_string_terminator(ndr, 4, 1),
248                 "check_string_terminator failed to recognize terminator");
249
250         torture_assert_ndr_success(tctx,
251                 ndr_check_string_terminator(ndr, 3, 1),
252                 "check_string_terminator failed to recognize terminator");
253
254         if (NDR_ERR_CODE_IS_SUCCESS(ndr_check_string_terminator(ndr, 2, 1))) {
255                 torture_fail(tctx, "check_string_terminator erroneously reported terminator");
256         }
257
258         torture_assert(tctx, ndr->offset == 0,
259                 "check_string_terminator did not reset offset");
260         return true;
261 }
262
263 static bool test_guid_from_string_valid(struct torture_context *tctx)
264 {
265         /* FIXME */
266         return true;
267 }
268
269 static bool test_guid_from_string_null(struct torture_context *tctx)
270 {
271         struct GUID guid;
272         torture_assert_ntstatus_equal(tctx, NT_STATUS_INVALID_PARAMETER,
273                                       GUID_from_string(NULL, &guid),
274                                       "NULL failed");
275         return true;
276 }
277
278 static bool test_guid_from_string_invalid(struct torture_context *tctx)
279 {
280         struct GUID g1;
281         torture_assert_ntstatus_equal(tctx, NT_STATUS_INVALID_PARAMETER,
282                                       GUID_from_string("bla", &g1),
283                                       "parameter not invalid");
284         return true;
285 }
286
287 static bool test_guid_from_string(struct torture_context *tctx)
288 {
289         struct GUID g1, exp;
290         torture_assert_ntstatus_ok(tctx,
291                                    GUID_from_string("00000001-0002-0003-0405-060708090a0b", &g1),
292                                    "invalid return code");
293         exp.time_low = 1;
294         exp.time_mid = 2;
295         exp.time_hi_and_version = 3;
296         exp.clock_seq[0] = 4;
297         exp.clock_seq[1] = 5;
298         exp.node[0] = 6;
299         exp.node[1] = 7;
300         exp.node[2] = 8;
301         exp.node[3] = 9;
302         exp.node[4] = 10;
303         exp.node[5] = 11;
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");
309
310         return true;
311 }
312
313 static bool test_guid_string_valid(struct torture_context *tctx)
314 {
315         struct GUID g;
316         g.time_low = 1;
317         g.time_mid = 2;
318         g.time_hi_and_version = 3;
319         g.clock_seq[0] = 4;
320         g.clock_seq[1] = 5;
321         g.node[0] = 6;
322         g.node[1] = 7;
323         g.node[2] = 8;
324         g.node[3] = 9;
325         g.node[4] = 10;
326         g.node[5] = 11;
327         torture_assert_str_equal(tctx, "00000001-0002-0003-0405-060708090a0b",
328                                  GUID_string(tctx, &g),
329                                  "parsing guid failed");
330         return true;
331 }
332
333 static bool test_guid_string2_valid(struct torture_context *tctx)
334 {
335         struct GUID g;
336         g.time_low = 1;
337         g.time_mid = 2;
338         g.time_hi_and_version = 3;
339         g.clock_seq[0] = 4;
340         g.clock_seq[1] = 5;
341         g.node[0] = 6;
342         g.node[1] = 7;
343         g.node[2] = 8;
344         g.node[3] = 9;
345         g.node[4] = 10;
346         g.node[5] = 11;
347         torture_assert_str_equal(tctx, "{00000001-0002-0003-0405-060708090a0b}",
348                                  GUID_string2(tctx, &g),
349                                  "parsing guid failed");
350         return true;
351 }
352
353 static bool test_compare_uuid(struct torture_context *tctx)
354 {
355         struct GUID g1, g2;
356         ZERO_STRUCT(g1); ZERO_STRUCT(g2);
357         torture_assert_int_equal(tctx, 0, GUID_compare(&g1, &g2),
358                                  "GUIDs not equal");
359         g1.time_low = 1;
360         torture_assert_int_equal(tctx, 1, GUID_compare(&g1, &g2),
361                                  "GUID diff invalid");
362
363         g1.time_low = 10;
364         torture_assert_int_equal(tctx, 1, GUID_compare(&g1, &g2),
365                                  "GUID diff invalid");
366
367         g1.time_low = 0;
368         g1.clock_seq[1] = 20;
369         torture_assert_int_equal(tctx, 1, GUID_compare(&g1, &g2),
370                                  "GUID diff invalid");
371
372         g1.time_low = ~0;
373         torture_assert_int_equal(tctx, 1, GUID_compare(&g1, &g2),
374                                  "GUID diff invalid");
375
376         g1.time_low = 0;
377         g2.time_low = ~0;
378         torture_assert_int_equal(tctx, -1, GUID_compare(&g1, &g2),
379                                  "GUID diff invalid");
380         return true;
381 }
382
383 struct torture_suite *torture_local_ndr(TALLOC_CTX *mem_ctx)
384 {
385         struct torture_suite *suite = torture_suite_create(mem_ctx, "ndr");
386
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));
403
404         torture_suite_add_simple_test(suite, "string terminator",
405                                       test_check_string_terminator);
406
407         torture_suite_add_simple_test(suite, "guid_from_string_null",
408                                       test_guid_from_string_null);
409
410         torture_suite_add_simple_test(suite, "guid_from_string",
411                                       test_guid_from_string);
412
413         torture_suite_add_simple_test(suite, "guid_from_string_invalid",
414                                       test_guid_from_string_invalid);
415
416         torture_suite_add_simple_test(suite, "guid_string_valid",
417                                       test_guid_string_valid);
418
419         torture_suite_add_simple_test(suite, "guid_string2_valid",
420                                       test_guid_string2_valid);
421
422         torture_suite_add_simple_test(suite, "guid_from_string_valid",
423                                       test_guid_from_string_valid);
424
425         torture_suite_add_simple_test(suite, "compare_uuid",
426                                       test_compare_uuid);
427
428         return suite;
429 }
430