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