7bd3631ad0dc3bed8cbf2265f9f998f3e8619dd5
[ira/wip.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         size_t struct_size;
30         ndr_pull_flags_fn_t pull_fn;
31         int ndr_flags;
32 };
33
34 static bool wrap_ndr_pull_test(struct torture_context *tctx,
35                                                            struct torture_tcase *tcase,
36                                                            struct torture_test *test)
37 {
38         bool (*check_fn) (struct torture_context *ctx, void *data) = test->fn;
39         const struct ndr_pull_test_data *data = (const struct ndr_pull_test_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, lp_iconv_convenience(tctx->lp_ctx));
42
43         ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
44
45         torture_assert_ndr_success(tctx, data->pull_fn(ndr, data->ndr_flags, ds),
46                                    "pulling");
47
48         torture_assert(tctx, ndr->offset == ndr->data_size, 
49                                    talloc_asprintf(tctx, 
50                                            "%d unread bytes", ndr->data_size - ndr->offset));
51
52         if (check_fn != NULL) 
53                 return check_fn(tctx, ds);
54         else
55                 return true;
56 }
57
58 _PUBLIC_ struct torture_test *_torture_suite_add_ndr_pull_test(
59                                         struct torture_suite *suite, 
60                                         const char *name, ndr_pull_flags_fn_t pull_fn,
61                                         DATA_BLOB db, 
62                                         size_t struct_size,
63                                         int ndr_flags,
64                                         bool (*check_fn) (struct torture_context *ctx, void *data))
65 {
66         struct torture_test *test; 
67         struct torture_tcase *tcase;
68         struct ndr_pull_test_data *data;
69         
70         tcase = torture_suite_add_tcase(suite, name);
71
72         test = talloc(tcase, struct torture_test);
73
74         test->name = talloc_strdup(test, name);
75         test->description = NULL;
76         test->run = wrap_ndr_pull_test;
77         data = talloc(test, struct ndr_pull_test_data);
78         data->data = db;
79         data->ndr_flags = ndr_flags;
80         data->struct_size = struct_size;
81         data->pull_fn = pull_fn;
82         test->data = data;
83         test->fn = check_fn;
84         test->dangerous = false;
85
86         DLIST_ADD_END(tcase->tests, test, struct torture_test *);
87
88         return test;
89 }
90
91 static bool test_check_string_terminator(struct torture_context *tctx)
92 {
93         struct ndr_pull *ndr;
94         DATA_BLOB blob;
95         TALLOC_CTX *mem_ctx = tctx;
96
97         /* Simple test */
98         blob = strhex_to_data_blob(tctx, "0000");
99         
100         ndr = ndr_pull_init_blob(&blob, mem_ctx, lp_iconv_convenience(tctx->lp_ctx));
101
102         torture_assert_ndr_success(tctx, ndr_check_string_terminator(ndr, 1, 2),
103                                    "simple check_string_terminator test failed");
104
105         torture_assert(tctx, ndr->offset == 0,
106                 "check_string_terminator did not reset offset");
107
108         if (NDR_ERR_CODE_IS_SUCCESS(ndr_check_string_terminator(ndr, 1, 3))) {
109                 torture_fail(tctx, "check_string_terminator checked beyond string boundaries");
110         }
111
112         torture_assert(tctx, ndr->offset == 0, 
113                 "check_string_terminator did not reset offset");
114
115         talloc_free(ndr);
116
117         blob = strhex_to_data_blob(tctx, "11220000");
118         ndr = ndr_pull_init_blob(&blob, mem_ctx, lp_iconv_convenience(tctx->lp_ctx));
119
120         torture_assert_ndr_success(tctx,
121                 ndr_check_string_terminator(ndr, 4, 1),
122                 "check_string_terminator failed to recognize terminator");
123
124         torture_assert_ndr_success(tctx,
125                 ndr_check_string_terminator(ndr, 3, 1),
126                 "check_string_terminator failed to recognize terminator");
127
128         if (NDR_ERR_CODE_IS_SUCCESS(ndr_check_string_terminator(ndr, 2, 1))) {
129                 torture_fail(tctx, "check_string_terminator erroneously reported terminator");
130         }
131
132         torture_assert(tctx, ndr->offset == 0,
133                 "check_string_terminator did not reset offset");
134         return true;
135 }
136
137 static bool test_guid_from_string_valid(struct torture_context *tctx)
138 {
139         /* FIXME */
140         return true;
141 }
142
143 static bool test_guid_from_string_null(struct torture_context *tctx)
144 {
145         struct GUID guid;
146         torture_assert_ntstatus_equal(tctx, NT_STATUS_INVALID_PARAMETER, 
147                                                                   GUID_from_string(NULL, &guid), 
148                                                                   "NULL failed");
149         return true;
150 }
151
152 static bool test_guid_from_string_invalid(struct torture_context *tctx)
153 {
154         struct GUID g1;
155         torture_assert_ntstatus_equal(tctx, NT_STATUS_INVALID_PARAMETER, 
156                                                                   GUID_from_string("bla", &g1),
157                                                                   "parameter not invalid");
158         return true;
159 }       
160
161 static bool test_guid_from_string(struct torture_context *tctx)
162 {
163         struct GUID g1, exp;
164         torture_assert_ntstatus_ok(tctx,
165                                                            GUID_from_string("00000001-0002-0003-0405-060708090a0b", &g1),
166                                                            "invalid return code");
167         exp.time_low = 1;
168         exp.time_mid = 2;
169         exp.time_hi_and_version = 3;
170         exp.clock_seq[0] = 4;
171         exp.clock_seq[1] = 5;
172         exp.node[0] = 6;
173         exp.node[1] = 7;
174         exp.node[2] = 8;
175         exp.node[3] = 9;
176         exp.node[4] = 10;
177         exp.node[5] = 11;
178         torture_assert(tctx, GUID_equal(&g1, &exp), "UUID parsed incorrectly");
179         torture_assert_ntstatus_ok(tctx,
180                                                            GUID_from_string("{00000001-0002-0003-0405-060708090a0b}", &g1),
181                                                            "invalid return code");
182         torture_assert(tctx, GUID_equal(&g1, &exp), "UUID parsed incorrectly");
183
184         return true;
185 }
186
187 static bool test_guid_string_valid(struct torture_context *tctx)
188 {
189         struct GUID g;
190         g.time_low = 1;
191         g.time_mid = 2;
192         g.time_hi_and_version = 3;
193         g.clock_seq[0] = 4;
194         g.clock_seq[1] = 5;
195         g.node[0] = 6;
196         g.node[1] = 7;
197         g.node[2] = 8;
198         g.node[3] = 9;
199         g.node[4] = 10;
200         g.node[5] = 11;
201         torture_assert_str_equal(tctx, "00000001-0002-0003-0405-060708090a0b", GUID_string(tctx, &g), 
202                                                          "parsing guid failed");
203         return true;
204 }
205
206 static bool test_guid_string2_valid(struct torture_context *tctx)
207 {
208         struct GUID g;
209         g.time_low = 1;
210         g.time_mid = 2;
211         g.time_hi_and_version = 3;
212         g.clock_seq[0] = 4;
213         g.clock_seq[1] = 5;
214         g.node[0] = 6;
215         g.node[1] = 7;
216         g.node[2] = 8;
217         g.node[3] = 9;
218         g.node[4] = 10;
219         g.node[5] = 11;
220         torture_assert_str_equal(tctx, "{00000001-0002-0003-0405-060708090a0b}", GUID_string2(tctx, &g), 
221                                                          "parsing guid failed");
222         return true;
223 }
224
225 static bool test_compare_uuid(struct torture_context *tctx)
226 {
227         struct GUID g1, g2;
228         ZERO_STRUCT(g1); ZERO_STRUCT(g2);
229         torture_assert_int_equal(tctx, 0, GUID_compare(&g1, &g2), 
230                                                          "GUIDs not equal");
231         g1.time_low = 1;
232         torture_assert_int_equal(tctx, 1, GUID_compare(&g1, &g2), 
233                                                          "GUID diff invalid");
234
235         g1.time_low = 10;
236         torture_assert_int_equal(tctx, 10, GUID_compare(&g1, &g2), 
237                                                          "GUID diff invalid");
238
239         g1.time_low = 0;
240         g1.clock_seq[1] = 20;
241         torture_assert_int_equal(tctx, 20, GUID_compare(&g1, &g2), 
242                                                          "GUID diff invalid");
243         return true;
244 }
245
246 struct torture_suite *torture_local_ndr(TALLOC_CTX *mem_ctx)
247 {
248         struct torture_suite *suite = torture_suite_create(mem_ctx, "NDR");
249
250         torture_suite_add_suite(suite, ndr_winreg_suite(suite));
251         torture_suite_add_suite(suite, ndr_atsvc_suite(suite));
252         torture_suite_add_suite(suite, ndr_lsa_suite(suite));
253         torture_suite_add_suite(suite, ndr_epmap_suite(suite));
254         torture_suite_add_suite(suite, ndr_dfs_suite(suite));
255         torture_suite_add_suite(suite, ndr_netlogon_suite(suite));
256         torture_suite_add_suite(suite, ndr_drsuapi_suite(suite));
257         torture_suite_add_suite(suite, ndr_spoolss_suite(suite));
258         torture_suite_add_suite(suite, ndr_samr_suite(suite));
259
260         torture_suite_add_simple_test(suite, "string terminator", 
261                                                                    test_check_string_terminator);
262
263         torture_suite_add_simple_test(suite, "guid_from_string_null", 
264                                                                    test_guid_from_string_null);
265
266         torture_suite_add_simple_test(suite, "guid_from_string", 
267                                                                    test_guid_from_string);
268
269         torture_suite_add_simple_test(suite, "guid_from_string_invalid", 
270                                                                    test_guid_from_string_invalid);
271
272         torture_suite_add_simple_test(suite, "guid_string_valid", 
273                                                                    test_guid_string_valid);
274
275         torture_suite_add_simple_test(suite, "guid_string2_valid", 
276                                                                    test_guid_string2_valid);
277
278         torture_suite_add_simple_test(suite, "guid_from_string_valid", 
279                                                                    test_guid_from_string_valid);
280
281         torture_suite_add_simple_test(suite, "compare_uuid", 
282                                                                    test_compare_uuid);
283
284         return suite;
285 }
286