registry: Add an explicit test for recursive deletion.
[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 "torture/torture.h"
26 #include "librpc/gen_ndr/winreg.h"
27 #include "system/filesys.h"
28
29 static bool test_del_nonexistant_key(struct torture_context *tctx,
30                                      const void *test_data)
31 {
32         const struct hive_key *root = (const struct hive_key *)test_data;
33         WERROR error = hive_key_del(root, "bla");
34         torture_assert_werr_equal(tctx, error, WERR_BADFILE,
35                                   "invalid return code");
36
37         return true;
38 }
39
40 static bool test_keyinfo_root(struct torture_context *tctx,
41                               const void *test_data)
42 {
43         uint32_t num_subkeys, num_values;
44         const struct hive_key *root = (const struct hive_key *)test_data;
45         WERROR error;
46
47         /* This is a new backend. There should be no subkeys and no
48          * values */
49         error = hive_key_get_info(tctx, root, NULL, &num_subkeys, &num_values,
50                                   NULL, NULL, NULL, NULL);
51         torture_assert_werr_ok(tctx, error, "reg_key_num_subkeys()");
52
53         torture_assert_int_equal(tctx, num_subkeys, 0,
54                                  "New key has non-zero subkey count");
55
56         torture_assert_werr_ok(tctx, error, "reg_key_num_values");
57
58         torture_assert_int_equal(tctx, num_values, 0,
59                                  "New key has non-zero value count");
60
61         return true;
62 }
63
64 static bool test_keyinfo_nums(struct torture_context *tctx, void *test_data)
65 {
66         uint32_t num_subkeys, num_values;
67         struct hive_key *root = (struct hive_key *)test_data;
68         WERROR error;
69         struct hive_key *subkey;
70         uint32_t data = 42;
71
72         error = hive_key_add_name(tctx, root, "Nested Keyll", NULL,
73                                   NULL, &subkey);
74         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
75
76         error = hive_key_set_value(root, "Answer", REG_DWORD,
77                                data_blob_talloc(tctx, &data, sizeof(data)));
78         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
79
80         /* This is a new backend. There should be no subkeys and no
81          * values */
82         error = hive_key_get_info(tctx, root, NULL, &num_subkeys, &num_values,
83                                   NULL, NULL, NULL, NULL);
84         torture_assert_werr_ok(tctx, error, "reg_key_num_subkeys()");
85
86         torture_assert_int_equal(tctx, num_subkeys, 1, "subkey count");
87
88         torture_assert_werr_ok(tctx, error, "reg_key_num_values");
89
90         torture_assert_int_equal(tctx, num_values, 1, "value count");
91
92         return true;
93 }
94
95 static bool test_add_subkey(struct torture_context *tctx,
96                             const void *test_data)
97 {
98         WERROR error;
99         struct hive_key *subkey;
100         const struct hive_key *root = (const struct hive_key *)test_data;
101         TALLOC_CTX *mem_ctx = tctx;
102
103         error = hive_key_add_name(mem_ctx, root, "Nested Key", NULL,
104                                   NULL, &subkey);
105         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
106
107         error = hive_key_del(root, "Nested Key");
108         torture_assert_werr_ok(tctx, error, "reg_key_del");
109
110         return true;
111 }
112
113 static bool test_del_recursive(struct torture_context *tctx,
114                                const void *test_data)
115 {
116         WERROR error;
117         struct hive_key *subkey;
118         struct hive_key *subkey2;
119         const struct hive_key *root = (const struct hive_key *)test_data;
120         TALLOC_CTX *mem_ctx = tctx;
121         uint32_t data = 42;
122
123         /* Create a new key under the root */
124         error = hive_key_add_name(mem_ctx, root, "Parent Key", NULL,
125                                   NULL, &subkey);
126         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
127
128         /* Create a new key under "Parent Key" */
129         error = hive_key_add_name(mem_ctx, subkey, "Child Key", NULL,
130                                   NULL, &subkey2);
131         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
132
133         /* Create a new value under "Child Key" */
134         error = hive_key_set_value(subkey2, "Answer Recursive", REG_DWORD,
135                                data_blob_talloc(mem_ctx, &data, sizeof(data)));
136         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
137
138         /* Deleting "Parent Key" will also delete "Child Key" and the value. */
139         error = hive_key_del(root, "Parent Key");
140         torture_assert_werr_ok(tctx, error, "hive_key_del");
141
142         return true;
143 }
144
145 static bool test_flush_key(struct torture_context *tctx, void *test_data)
146 {
147         struct hive_key *root = (struct hive_key *)test_data;
148
149         torture_assert_werr_ok(tctx, hive_key_flush(root), "flush key");
150
151         return true;
152 }
153
154 static bool test_del_key(struct torture_context *tctx, const void *test_data)
155 {
156         WERROR error;
157         struct hive_key *subkey;
158         const struct hive_key *root = (const struct hive_key *)test_data;
159         TALLOC_CTX *mem_ctx = tctx;
160
161         error = hive_key_add_name(mem_ctx, root, "Nested Key", NULL,
162                                   NULL, &subkey);
163         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
164
165         error = hive_key_del(root, "Nested Key");
166         torture_assert_werr_ok(tctx, error, "reg_key_del");
167
168         error = hive_key_del(root, "Nested Key");
169         torture_assert_werr_equal(tctx, error, WERR_BADFILE, "reg_key_del");
170
171         return true;
172 }
173
174 static bool test_set_value(struct torture_context *tctx,
175                            const void *test_data)
176 {
177         WERROR error;
178         struct hive_key *subkey;
179         const struct hive_key *root = (const struct hive_key *)test_data;
180         TALLOC_CTX *mem_ctx = tctx;
181         uint32_t data = 42;
182
183         error = hive_key_add_name(mem_ctx, root, "YA Nested Key", NULL,
184                                   NULL, &subkey);
185         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
186
187         error = hive_key_set_value(subkey, "Answer", REG_DWORD,
188                                data_blob_talloc(mem_ctx, &data, sizeof(data)));
189         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
190
191         return true;
192 }
193
194 static bool test_get_value(struct torture_context *tctx, const void *test_data)
195 {
196         WERROR error;
197         struct hive_key *subkey;
198         const struct hive_key *root = (const struct hive_key *)test_data;
199         TALLOC_CTX *mem_ctx = tctx;
200         uint32_t data = 42;
201         uint32_t type;
202         DATA_BLOB value;
203
204         error = hive_key_add_name(mem_ctx, root, "EYA Nested Key", NULL,
205                                   NULL, &subkey);
206         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
207
208         error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
209         torture_assert_werr_equal(tctx, error, WERR_BADFILE,
210                                   "getting missing value");
211
212         error = hive_key_set_value(subkey, "Answer", REG_DWORD,
213                                data_blob_talloc(mem_ctx, &data, sizeof(data)));
214         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
215
216         error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
217         torture_assert_werr_ok(tctx, error, "getting value");
218
219         torture_assert_int_equal(tctx, value.length, 4, "value length");
220         torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
221
222         torture_assert_int_equal(tctx, data, IVAL(value.data, 0),
223                                  "value data");
224
225         return true;
226 }
227
228 static bool test_del_value(struct torture_context *tctx, const void *test_data)
229 {
230         WERROR error;
231         struct hive_key *subkey;
232         const struct hive_key *root = (const struct hive_key *)test_data;
233         TALLOC_CTX *mem_ctx = tctx;
234         uint32_t data = 42;
235         uint32_t type;
236         DATA_BLOB value;
237
238         error = hive_key_add_name(mem_ctx, root, "EEYA Nested Key", NULL,
239                                                          NULL, &subkey);
240         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
241
242         error = hive_key_set_value(subkey, "Answer", REG_DWORD,
243                                data_blob_talloc(mem_ctx, &data, sizeof(data)));
244         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
245
246         error = hive_key_del_value(subkey, "Answer");
247         torture_assert_werr_ok(tctx, error, "deleting value");
248
249         error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
250         torture_assert_werr_equal(tctx, error, WERR_BADFILE, "getting value");
251
252         error = hive_key_del_value(subkey, "Answer");
253         torture_assert_werr_equal(tctx, error, WERR_BADFILE,
254                                   "deleting value");
255
256         return true;
257 }
258
259 static bool test_list_values(struct torture_context *tctx,
260                              const void *test_data)
261 {
262         WERROR error;
263         struct hive_key *subkey;
264         const struct hive_key *root = (const struct hive_key *)test_data;
265         TALLOC_CTX *mem_ctx = tctx;
266         uint32_t data = 42;
267         uint32_t type;
268         DATA_BLOB value;
269         const char *name;
270
271         error = hive_key_add_name(mem_ctx, root, "AYAYA Nested Key", NULL,
272                                   NULL, &subkey);
273         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
274
275         error = hive_key_set_value(subkey, "Answer", REG_DWORD,
276                                data_blob_talloc(mem_ctx, &data, sizeof(data)));
277         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
278
279         error = hive_get_value_by_index(mem_ctx, subkey, 0, &name,
280                                         &type, &value);
281         torture_assert_werr_ok(tctx, error, "getting value");
282
283         torture_assert_str_equal(tctx, name, "Answer", "value name");
284
285         torture_assert_int_equal(tctx, value.length, 4, "value length");
286         torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
287         
288         
289         torture_assert_int_equal(tctx, data, IVAL(value.data, 0), "value data");
290
291         error = hive_get_value_by_index(mem_ctx, subkey, 1, &name,
292                                         &type, &value);
293         torture_assert_werr_equal(tctx, error, WERR_NO_MORE_ITEMS,
294                                   "getting missing value");
295
296         return true;
297 }
298
299 static void tcase_add_tests(struct torture_tcase *tcase)
300 {
301         torture_tcase_add_simple_test_const(tcase, "del_nonexistant_key",
302                                                 test_del_nonexistant_key);
303         torture_tcase_add_simple_test_const(tcase, "add_subkey",
304                                                 test_add_subkey);
305         torture_tcase_add_simple_test(tcase, "flush_key",
306                                                 test_flush_key);
307         /* test_del_recursive() test must run before test_keyinfo_root().
308            test_keyinfo_root() checks the number of subkeys, which verifies
309            the recursive delete worked properly. */
310         torture_tcase_add_simple_test_const(tcase, "del_recursive",
311                                                 test_del_recursive);
312         torture_tcase_add_simple_test_const(tcase, "get_info",
313                                                 test_keyinfo_root);
314         torture_tcase_add_simple_test(tcase, "get_info_nums",
315                                                 test_keyinfo_nums);
316         torture_tcase_add_simple_test_const(tcase, "set_value",
317                                                 test_set_value);
318         torture_tcase_add_simple_test_const(tcase, "get_value",
319                                                 test_get_value);
320         torture_tcase_add_simple_test_const(tcase, "list_values",
321                                                 test_list_values);
322         torture_tcase_add_simple_test_const(tcase, "del_key",
323                                                 test_del_key);
324         torture_tcase_add_simple_test_const(tcase, "del_value",
325                                                 test_del_value);
326 }
327
328 static bool hive_setup_dir(struct torture_context *tctx, void **data)
329 {
330         struct hive_key *key;
331         WERROR error;
332         char *dirname;
333         NTSTATUS status;
334
335         status = torture_temp_dir(tctx, "hive-dir", &dirname);
336         if (!NT_STATUS_IS_OK(status))
337                 return false;
338
339         rmdir(dirname);
340
341         error = reg_create_directory(tctx, dirname, &key);
342         if (!W_ERROR_IS_OK(error)) {
343                 fprintf(stderr, "Unable to initialize dir hive\n");
344                 return false;
345         }
346
347         *data = key;
348
349         return true;
350 }
351
352 static bool hive_setup_ldb(struct torture_context *tctx, void **data)
353 {
354         struct hive_key *key;
355         WERROR error;
356         char *dirname;
357         NTSTATUS status;
358
359         status = torture_temp_dir(tctx, "hive-ldb", &dirname);
360         if (!NT_STATUS_IS_OK(status))
361                 return false;
362
363         rmdir(dirname);
364
365         error = reg_open_ldb_file(tctx, dirname, NULL, NULL, tctx->lp_ctx, &key);
366         if (!W_ERROR_IS_OK(error)) {
367                 fprintf(stderr, "Unable to initialize ldb hive\n");
368                 return false;
369         }
370
371         *data = key;
372
373         return true;
374 }
375
376 static bool hive_setup_regf(struct torture_context *tctx, void **data)
377 {
378         struct hive_key *key;
379         WERROR error;
380         char *dirname;
381         NTSTATUS status;
382
383         status = torture_temp_dir(tctx, "hive-dir", &dirname);
384         if (!NT_STATUS_IS_OK(status))
385                 return false;
386
387         rmdir(dirname);
388
389         error = reg_create_regf_file(tctx, dirname, 5, &key);
390         if (!W_ERROR_IS_OK(error)) {
391                 fprintf(stderr, "Unable to create new regf file\n");
392                 return false;
393         }
394
395         *data = key;
396
397         return true;
398 }
399
400 static bool test_dir_refuses_null_location(struct torture_context *tctx)
401 {
402         torture_assert_werr_equal(tctx, WERR_INVALID_PARAM,
403                                   reg_open_directory(NULL, NULL, NULL),
404                                   "reg_open_directory accepts NULL location");
405         return true;
406 }
407
408 struct torture_suite *torture_registry_hive(TALLOC_CTX *mem_ctx)
409 {
410         struct torture_tcase *tcase;
411         struct torture_suite *suite = torture_suite_create(mem_ctx, "HIVE");
412
413         torture_suite_add_simple_test(suite, "dir-refuses-null-location",
414                                       test_dir_refuses_null_location);
415
416         tcase = torture_suite_add_tcase(suite, "dir");
417         torture_tcase_set_fixture(tcase, hive_setup_dir, NULL);
418         tcase_add_tests(tcase);
419
420         tcase = torture_suite_add_tcase(suite, "ldb");
421         torture_tcase_set_fixture(tcase, hive_setup_ldb, NULL);
422         tcase_add_tests(tcase);
423
424         tcase = torture_suite_add_tcase(suite, "regf");
425         torture_tcase_set_fixture(tcase, hive_setup_regf, NULL);
426         tcase_add_tests(tcase);
427
428         return suite;
429 }