r21415: Add tests for spoolss and drsuapi. Still need to add validators.
[Samba/ekacnet.git] / source / torture / ndr / ndr.c
blob919ea2ce49c23786cbaf85c1a4a38fa73728af27
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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
23 #include "torture/ndr/ndr.h"
24 #include "torture/ndr/proto.h"
25 #include "util/dlinklist.h"
27 struct ndr_pull_test_data {
28 DATA_BLOB data;
29 size_t struct_size;
30 ndr_pull_flags_fn_t pull_fn;
31 int ndr_flags;
34 static bool wrap_ndr_pull_test(struct torture_context *tctx,
35 struct torture_tcase *tcase,
36 struct torture_test *test)
38 bool (*check_fn) (struct torture_context *ctx, void *data) = test->fn;
39 const struct ndr_pull_test_data *data = test->data;
40 void *ds = talloc_zero_size(tctx, data->struct_size);
41 struct ndr_pull *ndr = ndr_pull_init_blob(&(data->data), tctx);
43 ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
45 torture_assert_ntstatus_ok(tctx, data->pull_fn(ndr, data->ndr_flags, ds),
46 "pulling");
48 if (check_fn != NULL)
49 return check_fn(tctx, ds);
50 else
51 return true;
54 _PUBLIC_ struct torture_test *_torture_suite_add_ndr_pull_test(
55 struct torture_suite *suite,
56 const char *name, ndr_pull_flags_fn_t pull_fn,
57 DATA_BLOB db,
58 size_t struct_size,
59 int ndr_flags,
60 bool (*check_fn) (struct torture_context *ctx, void *data))
62 struct torture_test *test;
63 struct torture_tcase *tcase;
64 struct ndr_pull_test_data *data;
66 tcase = torture_suite_add_tcase(suite, name);
68 test = talloc(tcase, struct torture_test);
70 test->name = talloc_strdup(test, name);
71 test->description = NULL;
72 test->run = wrap_ndr_pull_test;
73 data = talloc(test, struct ndr_pull_test_data);
74 data->data = db;
75 data->ndr_flags = ndr_flags;
76 data->struct_size = struct_size;
77 data->pull_fn = pull_fn;
78 test->data = data;
79 test->fn = check_fn;
80 test->dangerous = false;
82 DLIST_ADD_END(tcase->tests, test, struct torture_test *);
84 return test;
87 static bool test_check_string_terminator(struct torture_context *tctx)
89 struct ndr_pull *ndr;
90 DATA_BLOB blob;
91 TALLOC_CTX *mem_ctx = tctx;
93 /* Simple test */
94 blob = strhex_to_data_blob("0000");
96 ndr = ndr_pull_init_blob(&blob, mem_ctx);
98 torture_assert_ntstatus_ok(tctx, ndr_check_string_terminator(ndr, 1, 2),
99 "simple check_string_terminator test failed");
101 torture_assert(tctx, ndr->offset == 0,
102 "check_string_terminator did not reset offset");
104 if (NT_STATUS_IS_OK(ndr_check_string_terminator(ndr, 1, 3))) {
105 torture_fail(tctx, "check_string_terminator checked beyond string boundaries");
108 torture_assert(tctx, ndr->offset == 0,
109 "check_string_terminator did not reset offset");
111 talloc_free(ndr);
113 blob = strhex_to_data_blob("11220000");
114 ndr = ndr_pull_init_blob(&blob, mem_ctx);
116 torture_assert_ntstatus_ok(tctx,
117 ndr_check_string_terminator(ndr, 4, 1),
118 "check_string_terminator failed to recognize terminator");
120 torture_assert_ntstatus_ok(tctx,
121 ndr_check_string_terminator(ndr, 3, 1),
122 "check_string_terminator failed to recognize terminator");
124 if (NT_STATUS_IS_OK(ndr_check_string_terminator(ndr, 2, 1))) {
125 torture_fail(tctx,
126 "check_string_terminator erroneously reported terminator");
129 torture_assert(tctx, ndr->offset == 0,
130 "check_string_terminator did not reset offset");
131 return true;
134 static bool test_guid_from_string_valid(struct torture_context *tctx)
136 /* FIXME */
137 return true;
140 static bool test_guid_from_string_null(struct torture_context *tctx)
142 struct GUID guid;
143 torture_assert_ntstatus_equal(tctx, NT_STATUS_INVALID_PARAMETER,
144 GUID_from_string(NULL, &guid),
145 "NULL failed");
146 return true;
149 static bool test_guid_from_string_invalid(struct torture_context *tctx)
151 struct GUID g1;
152 torture_assert_ntstatus_equal(tctx, NT_STATUS_INVALID_PARAMETER,
153 GUID_from_string("bla", &g1),
154 "parameter not invalid");
155 return true;
158 static bool test_guid_from_string(struct torture_context *tctx)
160 struct GUID g1, exp;
161 torture_assert_ntstatus_ok(tctx,
162 GUID_from_string("00000001-0002-0003-0405-060708090a0b", &g1),
163 "invalid return code");
164 exp.time_low = 1;
165 exp.time_mid = 2;
166 exp.time_hi_and_version = 3;
167 exp.clock_seq[0] = 4;
168 exp.clock_seq[1] = 5;
169 exp.node[0] = 6;
170 exp.node[1] = 7;
171 exp.node[2] = 8;
172 exp.node[3] = 9;
173 exp.node[4] = 10;
174 exp.node[5] = 11;
175 torture_assert(tctx, GUID_equal(&g1, &exp), "UUID parsed incorrectly");
176 torture_assert_ntstatus_ok(tctx,
177 GUID_from_string("{00000001-0002-0003-0405-060708090a0b}", &g1),
178 "invalid return code");
179 torture_assert(tctx, GUID_equal(&g1, &exp), "UUID parsed incorrectly");
181 return true;
184 static bool test_guid_string_valid(struct torture_context *tctx)
186 struct GUID g;
187 g.time_low = 1;
188 g.time_mid = 2;
189 g.time_hi_and_version = 3;
190 g.clock_seq[0] = 4;
191 g.clock_seq[1] = 5;
192 g.node[0] = 6;
193 g.node[1] = 7;
194 g.node[2] = 8;
195 g.node[3] = 9;
196 g.node[4] = 10;
197 g.node[5] = 11;
198 torture_assert_str_equal(tctx, "00000001-0002-0003-0405-060708090a0b", GUID_string(tctx, &g),
199 "parsing guid failed");
200 return true;
203 static bool test_guid_string2_valid(struct torture_context *tctx)
205 struct GUID g;
206 g.time_low = 1;
207 g.time_mid = 2;
208 g.time_hi_and_version = 3;
209 g.clock_seq[0] = 4;
210 g.clock_seq[1] = 5;
211 g.node[0] = 6;
212 g.node[1] = 7;
213 g.node[2] = 8;
214 g.node[3] = 9;
215 g.node[4] = 10;
216 g.node[5] = 11;
217 torture_assert_str_equal(tctx, "{00000001-0002-0003-0405-060708090a0b}", GUID_string2(tctx, &g),
218 "parsing guid failed");
219 return true;
222 static bool test_compare_uuid(struct torture_context *tctx)
224 struct GUID g1, g2;
225 ZERO_STRUCT(g1); ZERO_STRUCT(g2);
226 torture_assert_int_equal(tctx, 0, GUID_compare(&g1, &g2),
227 "GUIDs not equal");
228 g1.time_low = 1;
229 torture_assert_int_equal(tctx, 1, GUID_compare(&g1, &g2),
230 "GUID diff invalid");
232 g1.time_low = 10;
233 torture_assert_int_equal(tctx, 10, GUID_compare(&g1, &g2),
234 "GUID diff invalid");
236 g1.time_low = 0;
237 g1.clock_seq[1] = 20;
238 torture_assert_int_equal(tctx, 20, GUID_compare(&g1, &g2),
239 "GUID diff invalid");
240 return true;
243 struct torture_suite *torture_local_ndr(void)
245 struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "NDR");
247 torture_suite_add_suite(suite, ndr_winreg_suite(suite));
248 torture_suite_add_suite(suite, ndr_atsvc_suite(suite));
249 torture_suite_add_suite(suite, ndr_lsa_suite(suite));
250 torture_suite_add_suite(suite, ndr_epmap_suite(suite));
251 torture_suite_add_suite(suite, ndr_dfs_suite(suite));
252 torture_suite_add_suite(suite, ndr_netlogon_suite(suite));
253 torture_suite_add_suite(suite, ndr_drsuapi_suite(suite));
254 torture_suite_add_suite(suite, ndr_spoolss_suite(suite));
256 torture_suite_add_simple_test(suite, "string terminator",
257 test_check_string_terminator);
259 torture_suite_add_simple_test(suite, "guid_from_string_null",
260 test_guid_from_string_null);
262 torture_suite_add_simple_test(suite, "guid_from_string",
263 test_guid_from_string);
265 torture_suite_add_simple_test(suite, "guid_from_string_invalid",
266 test_guid_from_string_invalid);
268 torture_suite_add_simple_test(suite, "guid_string_valid",
269 test_guid_string_valid);
271 torture_suite_add_simple_test(suite, "guid_string2_valid",
272 test_guid_string2_valid);
274 torture_suite_add_simple_test(suite, "guid_from_string_valid",
275 test_guid_from_string_valid);
277 torture_suite_add_simple_test(suite, "compare_uuid",
278 test_compare_uuid);
280 return suite;