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