Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-test
[metze/samba/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 3 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 #include "param/param.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_BADFILE,
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, NULL, NULL, 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, void *test_data)
66 {
67         uint32_t num_subkeys, num_values;
68         struct hive_key *root = (struct hive_key *)test_data;
69         WERROR error;
70         struct hive_key *subkey;
71         char data[4];
72         SIVAL(data, 0, 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_key_set_value(root, "Answer", REG_DWORD,
79                                data_blob_talloc(tctx, data, sizeof(data)));
80         torture_assert_werr_ok(tctx, error, "hive_key_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, NULL, NULL, 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_del_recursive(struct torture_context *tctx,
116                                const void *test_data)
117 {
118         WERROR error;
119         struct hive_key *subkey;
120         struct hive_key *subkey2;
121         const struct hive_key *root = (const struct hive_key *)test_data;
122         TALLOC_CTX *mem_ctx = tctx;
123         char data[4];
124         SIVAL(data, 0, 42);
125
126         /* Create a new key under the root */
127         error = hive_key_add_name(mem_ctx, root, "Parent Key", NULL,
128                                   NULL, &subkey);
129         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
130
131         /* Create a new key under "Parent Key" */
132         error = hive_key_add_name(mem_ctx, subkey, "Child Key", NULL,
133                                   NULL, &subkey2);
134         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
135
136         /* Create a new value under "Child Key" */
137         error = hive_key_set_value(subkey2, "Answer Recursive", REG_DWORD,
138                                data_blob_talloc(mem_ctx, data, sizeof(data)));
139         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
140
141         /* Deleting "Parent Key" will also delete "Child Key" and the value. */
142         error = hive_key_del(root, "Parent Key");
143         torture_assert_werr_ok(tctx, error, "hive_key_del");
144
145         return true;
146 }
147
148 static bool test_flush_key(struct torture_context *tctx, void *test_data)
149 {
150         struct hive_key *root = (struct hive_key *)test_data;
151
152         torture_assert_werr_ok(tctx, hive_key_flush(root), "flush key");
153
154         return true;
155 }
156
157 static bool test_del_key(struct torture_context *tctx, const void *test_data)
158 {
159         WERROR error;
160         struct hive_key *subkey;
161         const struct hive_key *root = (const struct hive_key *)test_data;
162         TALLOC_CTX *mem_ctx = tctx;
163
164         error = hive_key_add_name(mem_ctx, root, "Nested Key", NULL,
165                                   NULL, &subkey);
166         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
167
168         error = hive_key_del(root, "Nested Key");
169         torture_assert_werr_ok(tctx, error, "reg_key_del");
170
171         error = hive_key_del(root, "Nested Key");
172         torture_assert_werr_equal(tctx, error, WERR_BADFILE, "reg_key_del");
173
174         return true;
175 }
176
177 static bool test_set_value(struct torture_context *tctx,
178                            const void *test_data)
179 {
180         WERROR error;
181         struct hive_key *subkey;
182         const struct hive_key *root = (const struct hive_key *)test_data;
183         TALLOC_CTX *mem_ctx = tctx;
184         char data[4];
185         SIVAL(data, 0, 42);
186
187         error = hive_key_add_name(mem_ctx, root, "YA Nested Key", NULL,
188                                   NULL, &subkey);
189         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
190
191         error = hive_key_set_value(subkey, "Answer", REG_DWORD,
192                                data_blob_talloc(mem_ctx, data, sizeof(data)));
193         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
194
195         return true;
196 }
197
198 static bool test_get_value(struct torture_context *tctx, const void *test_data)
199 {
200         WERROR error;
201         struct hive_key *subkey;
202         const struct hive_key *root = (const struct hive_key *)test_data;
203         TALLOC_CTX *mem_ctx = tctx;
204         char data[4];
205         uint32_t type;
206         DATA_BLOB value;
207
208         SIVAL(data, 0, 42);
209
210         error = hive_key_add_name(mem_ctx, root, "EYA Nested Key", NULL,
211                                   NULL, &subkey);
212         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
213
214         error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
215         torture_assert_werr_equal(tctx, error, WERR_BADFILE,
216                                   "getting missing value");
217
218         error = hive_key_set_value(subkey, "Answer", REG_DWORD,
219                                data_blob_talloc(mem_ctx, data, sizeof(data)));
220         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
221
222         error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
223         torture_assert_werr_ok(tctx, error, "getting value");
224
225         torture_assert_int_equal(tctx, value.length, 4, "value length");
226         torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
227
228         torture_assert_mem_equal(tctx, &data, value.data, sizeof(uint32_t),
229                                  "value data");
230
231         return true;
232 }
233
234 static bool test_del_value(struct torture_context *tctx, const void *test_data)
235 {
236         WERROR error;
237         struct hive_key *subkey;
238         const struct hive_key *root = (const struct hive_key *)test_data;
239         TALLOC_CTX *mem_ctx = tctx;
240         char data[4];
241         uint32_t type;
242         DATA_BLOB value;
243
244         SIVAL(data, 0, 42);
245
246         error = hive_key_add_name(mem_ctx, root, "EEYA Nested Key", NULL,
247                                                          NULL, &subkey);
248         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
249
250         error = hive_key_set_value(subkey, "Answer", REG_DWORD,
251                                data_blob_talloc(mem_ctx, data, sizeof(data)));
252         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
253
254         error = hive_key_del_value(subkey, "Answer");
255         torture_assert_werr_ok(tctx, error, "deleting value");
256
257         error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
258         torture_assert_werr_equal(tctx, error, WERR_BADFILE, "getting value");
259
260         error = hive_key_del_value(subkey, "Answer");
261         torture_assert_werr_equal(tctx, error, WERR_BADFILE,
262                                   "deleting value");
263
264         return true;
265 }
266
267 static bool test_list_values(struct torture_context *tctx,
268                              const void *test_data)
269 {
270         WERROR error;
271         struct hive_key *subkey;
272         const struct hive_key *root = (const struct hive_key *)test_data;
273         TALLOC_CTX *mem_ctx = tctx;
274         char data[4];
275         uint32_t type;
276         DATA_BLOB value;
277         const char *name;
278         int data_val = 42;
279         SIVAL(data, 0, data_val);
280
281         error = hive_key_add_name(mem_ctx, root, "AYAYA Nested Key", NULL,
282                                   NULL, &subkey);
283         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
284
285         error = hive_key_set_value(subkey, "Answer", REG_DWORD,
286                                data_blob_talloc(mem_ctx, data, sizeof(data)));
287         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
288
289         error = hive_get_value_by_index(mem_ctx, subkey, 0, &name,
290                                         &type, &value);
291         torture_assert_werr_ok(tctx, error, "getting value");
292
293         torture_assert_str_equal(tctx, name, "Answer", "value name");
294
295         torture_assert_int_equal(tctx, value.length, 4, "value length");
296         torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
297         
298         
299         torture_assert_int_equal(tctx, data_val, IVAL(value.data, 0), "value data");
300
301         error = hive_get_value_by_index(mem_ctx, subkey, 1, &name,
302                                         &type, &value);
303         torture_assert_werr_equal(tctx, error, WERR_NO_MORE_ITEMS,
304                                   "getting missing value");
305
306         return true;
307 }
308
309 static void tcase_add_tests(struct torture_tcase *tcase)
310 {
311         torture_tcase_add_simple_test_const(tcase, "del_nonexistant_key",
312                                                 test_del_nonexistant_key);
313         torture_tcase_add_simple_test_const(tcase, "add_subkey",
314                                                 test_add_subkey);
315         torture_tcase_add_simple_test(tcase, "flush_key",
316                                                 test_flush_key);
317         /* test_del_recursive() test must run before test_keyinfo_root().
318            test_keyinfo_root() checks the number of subkeys, which verifies
319            the recursive delete worked properly. */
320         torture_tcase_add_simple_test_const(tcase, "del_recursive",
321                                                 test_del_recursive);
322         torture_tcase_add_simple_test_const(tcase, "get_info",
323                                                 test_keyinfo_root);
324         torture_tcase_add_simple_test(tcase, "get_info_nums",
325                                                 test_keyinfo_nums);
326         torture_tcase_add_simple_test_const(tcase, "set_value",
327                                                 test_set_value);
328         torture_tcase_add_simple_test_const(tcase, "get_value",
329                                                 test_get_value);
330         torture_tcase_add_simple_test_const(tcase, "list_values",
331                                                 test_list_values);
332         torture_tcase_add_simple_test_const(tcase, "del_key",
333                                                 test_del_key);
334         torture_tcase_add_simple_test_const(tcase, "del_value",
335                                                 test_del_value);
336 }
337
338 static bool hive_setup_dir(struct torture_context *tctx, void **data)
339 {
340         struct hive_key *key;
341         WERROR error;
342         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_directory(tctx, dirname, &key);
352         if (!W_ERROR_IS_OK(error)) {
353                 fprintf(stderr, "Unable to initialize dir hive\n");
354                 return false;
355         }
356
357         *data = key;
358
359         return true;
360 }
361
362 static bool hive_setup_ldb(struct torture_context *tctx, void **data)
363 {
364         struct hive_key *key;
365         WERROR error;
366         char *dirname;
367         NTSTATUS status;
368
369         status = torture_temp_dir(tctx, "hive-ldb", &dirname);
370         if (!NT_STATUS_IS_OK(status))
371                 return false;
372
373         rmdir(dirname);
374
375         error = reg_open_ldb_file(tctx, dirname, NULL, NULL, tctx->ev, tctx->lp_ctx, &key);
376         if (!W_ERROR_IS_OK(error)) {
377                 fprintf(stderr, "Unable to initialize ldb hive\n");
378                 return false;
379         }
380
381         *data = key;
382
383         return true;
384 }
385
386 static bool hive_setup_regf(struct torture_context *tctx, void **data)
387 {
388         struct hive_key *key;
389         WERROR error;
390         char *dirname;
391         NTSTATUS status;
392
393         status = torture_temp_dir(tctx, "hive-dir", &dirname);
394         if (!NT_STATUS_IS_OK(status))
395                 return false;
396
397         rmdir(dirname);
398
399         error = reg_create_regf_file(tctx, lp_iconv_convenience(tctx->lp_ctx),
400                                      dirname, 5, &key);
401         if (!W_ERROR_IS_OK(error)) {
402                 fprintf(stderr, "Unable to create new regf file\n");
403                 return false;
404         }
405
406         *data = key;
407
408         return true;
409 }
410
411 static bool test_dir_refuses_null_location(struct torture_context *tctx)
412 {
413         torture_assert_werr_equal(tctx, WERR_INVALID_PARAM,
414                                   reg_open_directory(NULL, NULL, NULL),
415                                   "reg_open_directory accepts NULL location");
416         return true;
417 }
418
419 struct torture_suite *torture_registry_hive(TALLOC_CTX *mem_ctx)
420 {
421         struct torture_tcase *tcase;
422         struct torture_suite *suite = torture_suite_create(mem_ctx, "HIVE");
423
424         torture_suite_add_simple_test(suite, "dir-refuses-null-location",
425                                       test_dir_refuses_null_location);
426
427         tcase = torture_suite_add_tcase(suite, "dir");
428         torture_tcase_set_fixture(tcase, hive_setup_dir, NULL);
429         tcase_add_tests(tcase);
430
431         tcase = torture_suite_add_tcase(suite, "ldb");
432         torture_tcase_set_fixture(tcase, hive_setup_ldb, NULL);
433         tcase_add_tests(tcase);
434
435         tcase = torture_suite_add_tcase(suite, "regf");
436         torture_tcase_set_fixture(tcase, hive_setup_regf, NULL);
437         tcase_add_tests(tcase);
438
439         return suite;
440 }