55e24272105bffe9a1252f42a00d59eb1037417a
[samba.git] / source / 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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "torture/ndr/ndr.h"
24 #include "torture/ndr/proto.h"
25 #include "torture/ui.h"
26 #include "util/dlinklist.h"
27
28 struct ndr_pull_test_data {
29         DATA_BLOB data;
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 = 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_ntstatus_ok(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 test_check_string_terminator(struct torture_context *tctx)
93 {
94         struct ndr_pull *ndr;
95         DATA_BLOB blob;
96         TALLOC_CTX *mem_ctx = tctx;
97
98         /* Simple test */
99         blob = strhex_to_data_blob("0000");
100         
101         ndr = ndr_pull_init_blob(&blob, mem_ctx);
102
103         torture_assert_ntstatus_ok(tctx, ndr_check_string_terminator(ndr, 1, 2),
104                                                            "simple check_string_terminator test failed");
105
106         torture_assert(tctx, ndr->offset == 0,
107                 "check_string_terminator did not reset offset");
108
109         if (NT_STATUS_IS_OK(ndr_check_string_terminator(ndr, 1, 3))) {
110                 torture_fail(tctx, "check_string_terminator checked beyond string boundaries");
111         }
112
113         torture_assert(tctx, ndr->offset == 0, 
114                 "check_string_terminator did not reset offset");
115
116         talloc_free(ndr);
117
118         blob = strhex_to_data_blob("11220000");
119         ndr = ndr_pull_init_blob(&blob, mem_ctx);
120
121         torture_assert_ntstatus_ok(tctx, 
122                 ndr_check_string_terminator(ndr, 4, 1),
123                 "check_string_terminator failed to recognize terminator");
124
125         torture_assert_ntstatus_ok(tctx, 
126                 ndr_check_string_terminator(ndr, 3, 1),
127                 "check_string_terminator failed to recognize terminator");
128
129         if (NT_STATUS_IS_OK(ndr_check_string_terminator(ndr, 2, 1))) {
130                 torture_fail(tctx, 
131                                          "check_string_terminator erroneously reported terminator");
132         }
133
134         torture_assert(tctx, ndr->offset == 0,
135                 "check_string_terminator did not reset offset");
136         return true;
137 }
138
139 static bool test_guid_from_string_valid(struct torture_context *tctx)
140 {
141         /* FIXME */
142         return true;
143 }
144
145 static bool test_guid_from_string_null(struct torture_context *tctx)
146 {
147         struct GUID guid;
148         torture_assert_ntstatus_equal(tctx, NT_STATUS_INVALID_PARAMETER, 
149                                                                   GUID_from_string(NULL, &guid), 
150                                                                   "NULL failed");
151         return true;
152 }
153
154 static bool test_guid_from_string_invalid(struct torture_context *tctx)
155 {
156         struct GUID g1;
157         torture_assert_ntstatus_equal(tctx, NT_STATUS_INVALID_PARAMETER, 
158                                                                   GUID_from_string("bla", &g1),
159                                                                   "parameter not invalid");
160         return true;
161 }       
162
163 static bool test_guid_from_string(struct torture_context *tctx)
164 {
165         struct GUID g1, exp;
166         torture_assert_ntstatus_ok(tctx,
167                                                            GUID_from_string("00000001-0002-0003-0405-060708090a0b", &g1),
168                                                            "invalid return code");
169         exp.time_low = 1;
170         exp.time_mid = 2;
171         exp.time_hi_and_version = 3;
172         exp.clock_seq[0] = 4;
173         exp.clock_seq[1] = 5;
174         exp.node[0] = 6;
175         exp.node[1] = 7;
176         exp.node[2] = 8;
177         exp.node[3] = 9;
178         exp.node[4] = 10;
179         exp.node[5] = 11;
180         torture_assert(tctx, GUID_equal(&g1, &exp), "UUID parsed incorrectly");
181         torture_assert_ntstatus_ok(tctx,
182                                                            GUID_from_string("{00000001-0002-0003-0405-060708090a0b}", &g1),
183                                                            "invalid return code");
184         torture_assert(tctx, GUID_equal(&g1, &exp), "UUID parsed incorrectly");
185
186         return true;
187 }
188
189 static bool test_guid_string_valid(struct torture_context *tctx)
190 {
191         struct GUID g;
192         g.time_low = 1;
193         g.time_mid = 2;
194         g.time_hi_and_version = 3;
195         g.clock_seq[0] = 4;
196         g.clock_seq[1] = 5;
197         g.node[0] = 6;
198         g.node[1] = 7;
199         g.node[2] = 8;
200         g.node[3] = 9;
201         g.node[4] = 10;
202         g.node[5] = 11;
203         torture_assert_str_equal(tctx, "00000001-0002-0003-0405-060708090a0b", GUID_string(tctx, &g), 
204                                                          "parsing guid failed");
205         return true;
206 }
207
208 static bool test_guid_string2_valid(struct torture_context *tctx)
209 {
210         struct GUID g;
211         g.time_low = 1;
212         g.time_mid = 2;
213         g.time_hi_and_version = 3;
214         g.clock_seq[0] = 4;
215         g.clock_seq[1] = 5;
216         g.node[0] = 6;
217         g.node[1] = 7;
218         g.node[2] = 8;
219         g.node[3] = 9;
220         g.node[4] = 10;
221         g.node[5] = 11;
222         torture_assert_str_equal(tctx, "{00000001-0002-0003-0405-060708090a0b}", GUID_string2(tctx, &g), 
223                                                          "parsing guid failed");
224         return true;
225 }
226
227 static bool test_compare_uuid(struct torture_context *tctx)
228 {
229         struct GUID g1, g2;
230         ZERO_STRUCT(g1); ZERO_STRUCT(g2);
231         torture_assert_int_equal(tctx, 0, GUID_compare(&g1, &g2), 
232                                                          "GUIDs not equal");
233         g1.time_low = 1;
234         torture_assert_int_equal(tctx, 1, GUID_compare(&g1, &g2), 
235                                                          "GUID diff invalid");
236
237         g1.time_low = 10;
238         torture_assert_int_equal(tctx, 10, GUID_compare(&g1, &g2), 
239                                                          "GUID diff invalid");
240
241         g1.time_low = 0;
242         g1.clock_seq[1] = 20;
243         torture_assert_int_equal(tctx, 20, GUID_compare(&g1, &g2), 
244                                                          "GUID diff invalid");
245         return true;
246 }
247
248 struct torture_suite *torture_local_ndr(void)
249 {
250         struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "NDR");
251
252         torture_suite_add_suite(suite, ndr_winreg_suite(suite));
253         torture_suite_add_suite(suite, ndr_atsvc_suite(suite));
254         torture_suite_add_suite(suite, ndr_lsa_suite(suite));
255         torture_suite_add_suite(suite, ndr_epmap_suite(suite));
256         torture_suite_add_suite(suite, ndr_dfs_suite(suite));
257         torture_suite_add_suite(suite, ndr_netlogon_suite(suite));
258         torture_suite_add_suite(suite, ndr_drsuapi_suite(suite));
259         torture_suite_add_suite(suite, ndr_spoolss_suite(suite));
260
261         torture_suite_add_simple_test(suite, "string terminator", 
262                                                                    test_check_string_terminator);
263
264         torture_suite_add_simple_test(suite, "guid_from_string_null", 
265                                                                    test_guid_from_string_null);
266
267         torture_suite_add_simple_test(suite, "guid_from_string", 
268                                                                    test_guid_from_string);
269
270         torture_suite_add_simple_test(suite, "guid_from_string_invalid", 
271                                                                    test_guid_from_string_invalid);
272
273         torture_suite_add_simple_test(suite, "guid_string_valid", 
274                                                                    test_guid_string_valid);
275
276         torture_suite_add_simple_test(suite, "guid_string2_valid", 
277                                                                    test_guid_string2_valid);
278
279         torture_suite_add_simple_test(suite, "guid_from_string_valid", 
280                                                                    test_guid_from_string_valid);
281
282         torture_suite_add_simple_test(suite, "compare_uuid", 
283                                                                    test_compare_uuid);
284
285         return suite;
286 }
287