r25613: verify the length and type before checking the value,
[ira/wip.git] / source4 / lib / registry / tests / hive.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    local testing of registry library - hives
5
6    Copyright (C) Jelmer Vernooij 2005-2007
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "lib/registry/registry.h"
25 #include "lib/cmdline/popt_common.h"
26 #include "torture/torture.h"
27 #include "librpc/gen_ndr/winreg.h"
28 #include "system/filesys.h"
29
30 static bool test_del_nonexistant_key(struct torture_context *tctx,
31                                      const void *test_data)
32 {
33         const struct hive_key *root = (const struct hive_key *)test_data;
34         WERROR error = hive_key_del(root, "bla");
35         torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
36                                   "invalid return code");
37
38         return true;
39 }
40
41 static bool test_keyinfo_root(struct torture_context *tctx,
42                               const void *test_data)
43 {
44         uint32_t num_subkeys, num_values;
45         const struct hive_key *root = (const struct hive_key *)test_data;
46         WERROR error;
47
48         /* This is a new backend. There should be no subkeys and no
49          * values */
50         error = hive_key_get_info(tctx, root, NULL, &num_subkeys, &num_values,
51                                   NULL);
52         torture_assert_werr_ok(tctx, error, "reg_key_num_subkeys()");
53
54         torture_assert_int_equal(tctx, num_subkeys, 0,
55                                  "New key has non-zero subkey count");
56
57         torture_assert_werr_ok(tctx, error, "reg_key_num_values");
58
59         torture_assert_int_equal(tctx, num_values, 0,
60                                  "New key has non-zero value count");
61
62         return true;
63 }
64
65 static bool test_keyinfo_nums(struct torture_context *tctx,
66                               const void *test_data)
67 {
68         uint32_t num_subkeys, num_values;
69         const struct hive_key *root = (const struct hive_key *)test_data;
70         WERROR error;
71         struct hive_key *subkey;
72         uint32_t data = 42;
73
74         error = hive_key_add_name(tctx, root, "Nested Keyll", NULL,
75                                   NULL, &subkey);
76         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
77
78         error = hive_set_value(root, "Answer", REG_DWORD,
79                                data_blob_talloc(tctx, &data, sizeof(data)));
80         torture_assert_werr_ok(tctx, error, "hive_set_value");
81
82         /* This is a new backend. There should be no subkeys and no
83          * values */
84         error = hive_key_get_info(tctx, root, NULL, &num_subkeys, &num_values,
85                                   NULL);
86         torture_assert_werr_ok(tctx, error, "reg_key_num_subkeys()");
87
88         torture_assert_int_equal(tctx, num_subkeys, 1, "subkey count");
89
90         torture_assert_werr_ok(tctx, error, "reg_key_num_values");
91
92         torture_assert_int_equal(tctx, num_values, 1, "value count");
93
94         return true;
95 }
96
97 static bool test_add_subkey(struct torture_context *tctx,
98                             const void *test_data)
99 {
100         WERROR error;
101         struct hive_key *subkey;
102         const struct hive_key *root = (const struct hive_key *)test_data;
103         TALLOC_CTX *mem_ctx = tctx;
104
105         error = hive_key_add_name(mem_ctx, root, "Nested Key", NULL,
106                                   NULL, &subkey);
107         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
108
109         error = hive_key_del(root, "Nested Key");
110         torture_assert_werr_ok(tctx, error, "reg_key_del");
111
112         return true;
113 }
114
115 static bool test_flush_key(struct torture_context *tctx,
116                            const void *test_data)
117 {
118         const struct hive_key *root = (const struct hive_key *)test_data;
119
120         torture_assert_werr_ok(tctx, hive_key_flush(root), "flush key");
121
122         return true;
123 }
124
125 static bool test_del_key(struct torture_context *tctx, const void *test_data)
126 {
127         WERROR error;
128         struct hive_key *subkey;
129         const struct hive_key *root = (const struct hive_key *)test_data;
130         TALLOC_CTX *mem_ctx = tctx;
131
132         error = hive_key_add_name(mem_ctx, root, "Nested Key", NULL,
133                                   NULL, &subkey);
134         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
135
136         error = hive_key_del(root, "Nested Key");
137         torture_assert_werr_ok(tctx, error, "reg_key_del");
138
139         error = hive_key_del(root, "Nested Key");
140         torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND, "reg_key_del");
141
142         return true;
143 }
144
145 static bool test_set_value(struct torture_context *tctx,
146                            const void *test_data)
147 {
148         WERROR error;
149         struct hive_key *subkey;
150         const struct hive_key *root = (const struct hive_key *)test_data;
151         TALLOC_CTX *mem_ctx = tctx;
152         uint32_t data = 42;
153
154         error = hive_key_add_name(mem_ctx, root, "YA Nested Key", NULL,
155                                   NULL, &subkey);
156         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
157
158         error = hive_set_value(subkey, "Answer", REG_DWORD,
159                                data_blob_talloc(mem_ctx, &data, sizeof(data)));
160         torture_assert_werr_ok(tctx, error, "hive_set_value");
161
162         return true;
163 }
164
165 static bool test_get_value(struct torture_context *tctx, const void *test_data)
166 {
167         WERROR error;
168         struct hive_key *subkey;
169         const struct hive_key *root = (const struct hive_key *)test_data;
170         TALLOC_CTX *mem_ctx = tctx;
171         uint32_t data = 42;
172         uint32_t type;
173         DATA_BLOB value;
174
175         error = hive_key_add_name(mem_ctx, root, "EYA Nested Key", NULL,
176                                   NULL, &subkey);
177         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
178
179         error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
180         torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
181                                   "getting missing value");
182
183         error = hive_set_value(subkey, "Answer", REG_DWORD,
184                                data_blob_talloc(mem_ctx, &data, sizeof(data)));
185         torture_assert_werr_ok(tctx, error, "hive_set_value");
186
187         error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
188         torture_assert_werr_ok(tctx, error, "getting value");
189
190         torture_assert_int_equal(tctx, value.length, 4, "value length");
191         torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
192         torture_assert(tctx, memcmp(value.data, &data, 4) == 0, "value data");
193
194         return true;
195 }
196
197 static bool test_del_value(struct torture_context *tctx, const void *test_data)
198 {
199         WERROR error;
200         struct hive_key *subkey;
201         const struct hive_key *root = (const struct hive_key *)test_data;
202         TALLOC_CTX *mem_ctx = tctx;
203         uint32_t data = 42;
204         uint32_t type;
205         DATA_BLOB value;
206
207         error = hive_key_add_name(mem_ctx, root, "EEYA Nested Key", NULL,
208                                                          NULL, &subkey);
209         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
210
211         error = hive_set_value(subkey, "Answer", REG_DWORD,
212                                data_blob_talloc(mem_ctx, &data, sizeof(data)));
213         torture_assert_werr_ok(tctx, error, "hive_set_value");
214
215         error = hive_del_value(subkey, "Answer");
216         torture_assert_werr_ok(tctx, error, "deleting value");
217
218         error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
219         torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND, "getting value");
220
221         error = hive_del_value(subkey, "Answer");
222         torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
223                                   "deleting value");
224
225         return true;
226 }
227
228 static bool test_list_values(struct torture_context *tctx,
229                              const void *test_data)
230 {
231         WERROR error;
232         struct hive_key *subkey;
233         const struct hive_key *root = (const struct hive_key *)test_data;
234         TALLOC_CTX *mem_ctx = tctx;
235         uint32_t data = 42;
236         uint32_t type;
237         DATA_BLOB value;
238         const char *name;
239
240         error = hive_key_add_name(mem_ctx, root, "AYAYA Nested Key", NULL,
241                                   NULL, &subkey);
242         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
243
244         error = hive_set_value(subkey, "Answer", REG_DWORD,
245                                data_blob_talloc(mem_ctx, &data, sizeof(data)));
246         torture_assert_werr_ok(tctx, error, "hive_set_value");
247
248         error = hive_get_value_by_index(mem_ctx, subkey, 0, &name,
249                                         &type, &value);
250         torture_assert_werr_ok(tctx, error, "getting value");
251
252         torture_assert_str_equal(tctx, name, "Answer", "value name");
253
254         torture_assert_int_equal(tctx, value.length, 4, "value length");
255         torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
256         torture_assert(tctx, memcmp(value.data, &data, 4) == 0, "value data");
257
258         error = hive_get_value_by_index(mem_ctx, subkey, 1, &name,
259                                         &type, &value);
260         torture_assert_werr_equal(tctx, error, WERR_NO_MORE_ITEMS,
261                                   "getting missing value");
262
263         return true;
264 }
265
266 static void tcase_add_tests(struct torture_tcase *tcase)
267 {
268         torture_tcase_add_simple_test(tcase, "del_nonexistant_key",
269                                       test_del_nonexistant_key);
270         torture_tcase_add_simple_test(tcase, "add_subkey",
271                                       test_add_subkey);
272         torture_tcase_add_simple_test(tcase, "flush_key",
273                                       test_flush_key);
274         torture_tcase_add_simple_test(tcase, "get_info",
275                                       test_keyinfo_root);
276         torture_tcase_add_simple_test(tcase, "get_info_nums",
277                                       test_keyinfo_nums);
278         torture_tcase_add_simple_test(tcase, "set_value",
279                                       test_set_value);
280         torture_tcase_add_simple_test(tcase, "get_value",
281                                       test_get_value);
282         torture_tcase_add_simple_test(tcase, "list_values",
283                                       test_list_values);
284         torture_tcase_add_simple_test(tcase, "del_key",
285                                       test_del_key);
286         torture_tcase_add_simple_test(tcase, "del_value",
287                                       test_del_value);
288 }
289
290 static bool hive_setup_dir(struct torture_context *tctx, void **data)
291 {
292         struct hive_key *key;
293         WERROR error;
294         const char *dirname;
295         NTSTATUS status;
296
297         status = torture_temp_dir(tctx, "hive-dir", &dirname);
298         if (!NT_STATUS_IS_OK(status))
299                 return false;
300
301         rmdir(dirname);
302
303         error = reg_create_directory(tctx, dirname, &key);
304         if (!W_ERROR_IS_OK(error)) {
305                 fprintf(stderr, "Unable to initialize dir hive\n");
306                 return false;
307         }
308
309         *data = key;
310
311         return true;
312 }
313
314 static bool hive_setup_ldb(struct torture_context *tctx, void **data)
315 {
316         struct hive_key *key;
317         WERROR error;
318         const char *dirname;
319         NTSTATUS status;
320
321         status = torture_temp_dir(tctx, "hive-ldb", &dirname);
322         if (!NT_STATUS_IS_OK(status))
323                 return false;
324
325         rmdir(dirname);
326
327         error = reg_open_ldb_file(tctx, dirname, NULL, NULL, &key);
328         if (!W_ERROR_IS_OK(error)) {
329                 fprintf(stderr, "Unable to initialize ldb hive\n");
330                 return false;
331         }
332
333         *data = key;
334
335         return true;
336 }
337
338 static bool hive_setup_regf(struct torture_context *tctx, void **data)
339 {
340         struct hive_key *key;
341         WERROR error;
342         const char *dirname;
343         NTSTATUS status;
344
345         status = torture_temp_dir(tctx, "hive-dir", &dirname);
346         if (!NT_STATUS_IS_OK(status))
347                 return false;
348
349         rmdir(dirname);
350
351         error = reg_create_regf_file(tctx, dirname, 5, &key);
352         if (!W_ERROR_IS_OK(error)) {
353                 fprintf(stderr, "Unable to create new regf file\n");
354                 return false;
355         }
356
357         *data = key;
358
359         return true;
360 }
361
362 static bool test_dir_refuses_null_location(struct torture_context *tctx)
363 {
364         torture_assert_werr_equal(tctx, WERR_INVALID_PARAM,
365                                   reg_open_directory(NULL, NULL, NULL),
366                                   "reg_open_directory accepts NULL location");
367         return true;
368 }
369
370 struct torture_suite *torture_registry_hive(TALLOC_CTX *mem_ctx)
371 {
372         struct torture_tcase *tcase;
373         struct torture_suite *suite = torture_suite_create(mem_ctx, "HIVE");
374
375         torture_suite_add_simple_test(suite, "dir-refuses-null-location",
376                                       test_dir_refuses_null_location);
377
378         tcase = torture_suite_add_tcase(suite, "dir");
379         torture_tcase_set_fixture(tcase, hive_setup_dir, NULL);
380         tcase_add_tests(tcase);
381
382         tcase = torture_suite_add_tcase(suite, "ldb");
383         torture_tcase_set_fixture(tcase, hive_setup_ldb, NULL);
384         tcase_add_tests(tcase);
385
386         tcase = torture_suite_add_tcase(suite, "regf");
387         torture_tcase_set_fixture(tcase, hive_setup_regf, NULL);
388         tcase_add_tests(tcase);
389
390         return suite;
391 }