r24683: Add two more tests.
[kai/samba-autobuild/.git] / source4 / lib / registry / tests / registry.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    local testing of registry library - registry backend
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 /**
34  * Test obtaining a predefined key.
35  */
36 static bool test_get_predefined(struct torture_context *tctx,
37                                                                 const void *_data)
38 {
39         const struct registry_context *rctx = _data;
40         struct registry_key *root;
41         WERROR error;
42
43         error = reg_get_predefined_key(rctx, HKEY_CLASSES_ROOT, &root);
44         torture_assert_werr_ok(tctx, error, 
45                                                    "getting predefined key failed");
46         return true;
47 }
48
49 /**
50  * Test obtaining a predefined key.
51  */
52 static bool test_get_predefined_unknown(struct torture_context *tctx,
53                                                                 const void *_data)
54 {
55         const struct registry_context *rctx = _data;
56         struct registry_key *root;
57         WERROR error;
58
59         error = reg_get_predefined_key(rctx, 1337, &root);
60         torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
61                                                    "getting predefined key failed");
62         return true;
63 }
64
65 static bool test_predef_key_by_name(struct torture_context *tctx,
66                                                                 const void *_data)
67 {
68         const struct registry_context *rctx = _data;
69         struct registry_key *root;
70         WERROR error;
71
72         error = reg_get_predefined_key_by_name(rctx, "HKEY_CLASSES_ROOT", &root);
73         torture_assert_werr_ok(tctx, error, 
74                                                    "getting predefined key failed");
75
76         error = reg_get_predefined_key_by_name(rctx, "HKEY_classes_ROOT", &root);
77         torture_assert_werr_ok(tctx, error, 
78                                                    "getting predefined key case insensitively failed");
79
80         return true;
81 }
82
83 static bool test_predef_key_by_name_invalid(struct torture_context *tctx,
84                                                                 const void *_data)
85 {
86         const struct registry_context *rctx = _data;
87         struct registry_key *root;
88         WERROR error;
89
90         error = reg_get_predefined_key_by_name(tctx, "BLA", &root);
91         torture_assert_werr_equal(tctx, error, WERR_BADFILE,
92                                                    "getting predefined key failed");
93         return true;
94 }
95
96 /**
97  * Test creating a new subkey
98  */
99 static bool test_create_subkey(struct torture_context *tctx,
100                                                       const void *_data)
101 {
102         const struct registry_context *rctx = _data;
103         struct registry_key *root, *newkey;
104         WERROR error;
105
106         error = reg_get_predefined_key(rctx, HKEY_CLASSES_ROOT, &root);
107         torture_assert_werr_ok(tctx, error, 
108                                                    "getting predefined key failed");
109
110         error = reg_key_add_name(rctx, root, "Bad Bentheim", NULL, NULL, &newkey);
111         torture_assert_werr_ok(tctx, error, "Creating key return code");
112         torture_assert(tctx, newkey != NULL, "Creating new key");
113
114         return true;
115 }
116
117 /**
118  * Test creating a new nested subkey
119  */
120 static bool test_create_nested_subkey(struct torture_context *tctx,
121                                                       const void *_data)
122 {
123         const struct registry_context *rctx = _data;
124         struct registry_key *root, *newkey1, *newkey2;
125         WERROR error;
126
127         error = reg_get_predefined_key(rctx, HKEY_CLASSES_ROOT, &root);
128         torture_assert_werr_ok(tctx, error, 
129                                                    "getting predefined key failed");
130
131         error = reg_key_add_name(rctx, root, "Hamburg", NULL, NULL, 
132                                                          &newkey1);
133         torture_assert_werr_ok(tctx, error, "Creating key return code");
134         torture_assert(tctx, newkey2 != NULL, "Creating new key");
135
136         error = reg_key_add_name(rctx, root, "Hamburg\\Hamburg", NULL, NULL, 
137                                                          &newkey2);
138         torture_assert_werr_ok(tctx, error, "Creating key return code");
139         torture_assert(tctx, newkey2 != NULL, "Creating new key");
140
141         return true;
142 }
143
144 /**
145  * Test creating a new subkey
146  */
147 static bool test_key_add_abs_top(struct torture_context *tctx,
148                                                          const void *_data)
149 {
150         const struct registry_context *rctx = _data;
151         struct registry_key *root;
152         WERROR error;
153
154         error = reg_key_add_abs(tctx, rctx, "HKEY_CLASSES_ROOT", 0, NULL, &root);
155         torture_assert_werr_equal(tctx, error, WERR_ALREADY_EXISTS, "create top level");
156
157         return true;
158 }
159
160 /**
161  * Test creating a new subkey
162  */
163 static bool test_key_add_abs(struct torture_context *tctx,
164                                                          const void *_data)
165 {
166         WERROR error;
167         const struct registry_context *rctx = _data;
168         struct registry_key *root, *result1, *result2;
169
170         error = reg_key_add_abs(tctx, rctx,  "HKEY_CLASSES_ROOT\\bloe", 0, NULL, &result1);
171         torture_assert_werr_ok(tctx, error, "create lowest");
172
173         error = reg_key_add_abs(tctx, rctx,  "HKEY_CLASSES_ROOT\\bloe\\bla", 0, NULL, &result1);
174         torture_assert_werr_ok(tctx, error, "create nested");
175
176         error = reg_get_predefined_key(rctx, HKEY_CLASSES_ROOT, &root);
177         torture_assert_werr_ok(tctx, error, 
178                                                    "getting predefined key failed");
179
180         error = reg_open_key(tctx, root, "bloe", &result2);
181         torture_assert_werr_ok(tctx, error, "opening key");
182
183         error = reg_open_key(tctx, root, "bloe\\bla", &result2);
184         torture_assert_werr_ok(tctx, error, "opening key");
185
186         return true;
187 }
188
189
190 static bool test_del_key(struct torture_context *tctx, const void *_data)
191 {
192         const struct registry_context *rctx = _data;
193         struct registry_key *root, *newkey;
194         WERROR error;
195
196         error = reg_get_predefined_key(rctx, HKEY_CLASSES_ROOT, &root);
197         torture_assert_werr_ok(tctx, error, 
198                                                    "getting predefined key failed");
199
200         error = reg_key_add_name(rctx, root, "Hamburg", NULL, NULL, &newkey);
201
202         torture_assert_werr_ok(tctx, error, "Creating key return code");
203         torture_assert(tctx, newkey != NULL, "Creating new key");
204
205         error = reg_key_del(root, "Hamburg");
206         torture_assert_werr_ok(tctx, error, "Delete key");
207
208         error = reg_key_del(root, "Hamburg");
209         torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND, 
210                                                           "Delete missing key");
211
212         return true;
213 }
214
215 /**
216  * Convenience function for opening the HKEY_CLASSES_ROOT hive and 
217  * creating a single key for testing purposes.
218  */
219 static bool create_test_key(struct torture_context *tctx, 
220                                                         const struct registry_context *rctx,
221                                                         const char *name, 
222                                                         struct registry_key **root,
223                                                         struct registry_key **subkey)
224 {
225         WERROR error;
226
227         error = reg_get_predefined_key(rctx, HKEY_CLASSES_ROOT, root);
228         torture_assert_werr_ok(tctx, error, 
229                                                    "getting predefined key failed");
230
231         error = reg_key_add_name(rctx, *root, name, NULL, NULL, subkey);
232         torture_assert_werr_ok(tctx, error, "Creating key return code");
233
234         return true;
235 }
236
237
238 static bool test_flush_key(struct torture_context *tctx, const void *_data)
239 {
240         const struct registry_context *rctx = _data;
241         struct registry_key *root, *subkey;
242         WERROR error;
243
244         if (!create_test_key(tctx, rctx, "Munchen", &root, &subkey))
245                 return false;
246
247         error = reg_key_flush(subkey);
248         torture_assert_werr_ok(tctx, error, "flush key");
249
250         torture_assert_werr_equal(tctx, reg_key_flush(NULL), 
251                                                           WERR_INVALID_PARAM, "flush key");
252
253         return true;
254 }
255
256 static bool test_query_key(struct torture_context *tctx, const void *_data)
257 {
258         const struct registry_context *rctx = _data;
259         struct registry_key *root, *subkey;
260         WERROR error;
261         NTTIME last_changed_time;
262         uint32_t num_subkeys, num_values;
263         const char *classname;
264
265         if (!create_test_key(tctx, rctx, "Munchen", &root, &subkey))
266                 return false;
267
268         error = reg_key_get_info(tctx, subkey, &classname,
269                                                          &num_subkeys, &num_values,
270                                                          &last_changed_time);
271
272         torture_assert_werr_ok(tctx, error, "get info key");
273         torture_assert(tctx, classname == NULL, "classname");
274         torture_assert_int_equal(tctx, num_subkeys, 0, "num subkeys");
275         torture_assert_int_equal(tctx, num_values, 0, "num values");
276
277         return true;
278 }
279
280 static bool test_query_key_nums(struct torture_context *tctx, const void *_data)
281 {
282         const struct registry_context *rctx = _data;
283         struct registry_key *root, *subkey1, *subkey2;
284         WERROR error;
285         uint32_t num_subkeys, num_values;
286         uint32_t data = 42;
287
288         if (!create_test_key(tctx, rctx, "Berlin", &root, &subkey1))
289                 return false;
290
291         error = reg_key_add_name(rctx, subkey1, "Bentheim", NULL, NULL, &subkey2);
292         torture_assert_werr_ok(tctx, error, "Creating key return code");
293
294         error = reg_val_set(subkey1, "Answer", REG_DWORD, 
295                                                 data_blob_talloc(tctx, &data, sizeof(data)));
296         torture_assert_werr_ok(tctx, error, "set value");
297
298         error = reg_key_get_info(tctx, subkey1, NULL, &num_subkeys,
299                                                          &num_values, NULL);
300
301         torture_assert_werr_ok(tctx, error, "get info key");
302         torture_assert_int_equal(tctx, num_subkeys, 1, "num subkeys");
303         torture_assert_int_equal(tctx, num_values, 1, "num values");
304
305         return true;
306 }
307
308 /**
309  * Test that the subkeys of a key can be enumerated, that 
310  * the returned parameters for get_subkey_by_index are optional and 
311  * that enumerating the parents of a non-top-level node works.
312  */
313 static bool test_list_subkeys(struct torture_context *tctx, const void *_data)
314 {
315         const struct registry_context *rctx = _data;
316         struct registry_key *subkey = NULL, *root;
317         WERROR error;
318         NTTIME last_mod_time;
319         const char *classname, *name;
320
321         if (!create_test_key(tctx, rctx, "Goettingen", &root, &subkey))
322                 return false;
323
324         error = reg_key_get_subkey_by_index(tctx, root, 0, &name, &classname, 
325                                                                 &last_mod_time);
326
327         torture_assert_werr_ok(tctx, error, "Enum keys return code");
328         torture_assert_str_equal(tctx, name, "Goettingen", "Enum keys data");
329
330
331         error = reg_key_get_subkey_by_index(tctx, root, 0, NULL, NULL, NULL);
332
333         torture_assert_werr_ok(tctx, error, "Enum keys with NULL arguments return code");
334
335         error = reg_key_get_subkey_by_index(tctx, root, 1, NULL, NULL, NULL);
336         
337         torture_assert_werr_equal(tctx, error, WERR_NO_MORE_ITEMS, 
338                                                           "Invalid error for no more items");
339
340         error = reg_key_get_subkey_by_index(tctx, subkey, 0, NULL, NULL, NULL);
341         
342         torture_assert_werr_equal(tctx, error, WERR_NO_MORE_ITEMS, 
343                                                           "Invalid error for no more items");
344
345         return true;
346 }
347
348 /**
349  * Test setting a value
350  */
351 static bool test_set_value(struct torture_context *tctx, const void *_data)
352 {
353         const struct registry_context *rctx = _data;
354         struct registry_key *subkey = NULL, *root;
355         WERROR error;
356         uint32_t data = 42;
357
358         if (!create_test_key(tctx, rctx, "Dusseldorf", &root, &subkey))
359                 return false;
360
361         error = reg_val_set(subkey, "Answer", REG_DWORD, 
362                                                 data_blob_talloc(tctx, &data, sizeof(data)));
363         torture_assert_werr_ok (tctx, error, "setting value");
364
365         return true;
366 }
367
368 /**
369  * Test getting a value
370  */
371 static bool test_get_value(struct torture_context *tctx, const void *_data)
372 {
373         const struct registry_context *rctx = _data;
374         struct registry_key *subkey = NULL, *root;
375         WERROR error;
376         DATA_BLOB data;
377         uint32_t value = 42;
378         uint32_t type;
379
380         if (!create_test_key(tctx, rctx, "Duisburg", &root, &subkey))
381                 return false;
382
383         error = reg_key_get_value_by_name(tctx, subkey, __FUNCTION__, &type, 
384                                                                           &data);
385         torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND, 
386                                                           "getting missing value");
387
388         error = reg_val_set(subkey, __FUNCTION__, REG_DWORD, 
389                                                 data_blob_talloc(tctx, &value, 4));
390         torture_assert_werr_ok (tctx, error, "setting value");
391
392         error = reg_key_get_value_by_name(tctx, subkey, __FUNCTION__, &type, 
393                                                                           &data);
394         torture_assert_werr_ok(tctx, error, "getting value");
395
396         torture_assert_int_equal(tctx, 4, data.length, "value length ok");
397         torture_assert(tctx, memcmp(data.data, &value, 4) == 0, "value content ok");
398         torture_assert_int_equal(tctx, REG_DWORD, type, "value type");
399
400         return true;
401 }
402
403 /**
404  * Test unsetting a value
405  */
406 static bool test_del_value(struct torture_context *tctx, const void *_data)
407 {
408         const struct registry_context *rctx = _data;
409         struct registry_key *subkey = NULL, *root;
410         WERROR error;
411         DATA_BLOB data;
412         uint32_t value = 42;
413         uint32_t type;
414
415         if (!create_test_key(tctx, rctx, "Duisburg", &root, &subkey))
416                 return false;
417
418         error = reg_key_get_value_by_name(tctx, subkey, __FUNCTION__, &type, 
419                                                                           &data);
420         torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND, 
421                                                           "getting missing value");
422
423         error = reg_val_set(subkey, __FUNCTION__, REG_DWORD, 
424                                                 data_blob_talloc(tctx, &value, 4));
425         torture_assert_werr_ok (tctx, error, "setting value");
426
427         error = reg_del_value(subkey, __FUNCTION__);
428         torture_assert_werr_ok (tctx, error, "unsetting value");
429
430         error = reg_key_get_value_by_name(tctx, subkey, __FUNCTION__, &type, &data);
431         torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND, 
432                                                           "getting missing value");
433
434         return true;
435 }
436
437 /**
438  * Test listing values
439  */
440 static bool test_list_values(struct torture_context *tctx, const void *_data)
441 {
442         const struct registry_context *rctx = _data;
443         struct registry_key *subkey = NULL, *root;
444         WERROR error;
445         DATA_BLOB data;
446         uint32_t value = 42;
447         uint32_t type;
448         const char *name;
449
450         if (!create_test_key(tctx, rctx, "Bonn", &root, &subkey))
451                 return false;
452
453         error = reg_val_set(subkey, "bar", REG_DWORD, 
454                                                 data_blob_talloc(tctx, &value, 4));
455         torture_assert_werr_ok (tctx, error, "setting value");
456
457         error = reg_key_get_value_by_index(tctx, subkey, 0, &name, &type, &data);
458         torture_assert_werr_ok(tctx, error, "getting value");
459
460         torture_assert_str_equal(tctx, name, "bar", "value name");
461         torture_assert_int_equal(tctx, 4, data.length, "value length");
462         torture_assert(tctx, memcmp(data.data, &value, 4) == 0, "value content");
463         torture_assert_int_equal(tctx, REG_DWORD, type, "value type");
464
465         error = reg_key_get_value_by_index(tctx, subkey, 1, &name, &type, &data);
466         torture_assert_werr_equal(tctx, error, WERR_NO_MORE_ITEMS, 
467                                                           "getting missing value");
468
469         return true;
470 }
471
472 static bool setup_local_registry(struct torture_context *tctx, void **data)
473 {
474         struct registry_context *rctx;
475         WERROR error;
476         const char *tempdir;
477         NTSTATUS status;
478         struct hive_key *hive_key;
479
480         error = reg_open_local(tctx, &rctx, NULL, NULL);
481         if (!W_ERROR_IS_OK(error))
482                 return false;
483
484         status = torture_temp_dir(tctx, "registry-local", &tempdir);
485         if (!NT_STATUS_IS_OK(status))
486                 return false;
487
488         error = reg_open_ldb_file(tctx, 
489                                           talloc_asprintf(tctx, "%s/classes_root.ldb", tempdir),
490                                           NULL,
491                                           NULL,
492                                           &hive_key);
493         if (!W_ERROR_IS_OK(error))
494                 return false;
495
496         error = reg_mount_hive(rctx, hive_key, HKEY_CLASSES_ROOT, NULL);
497         if (!W_ERROR_IS_OK(error))
498                 return false;
499
500         *data = rctx;
501
502         return true;
503 }
504
505 static void tcase_add_tests(struct torture_tcase *tcase)
506 {
507         torture_tcase_add_simple_test(tcase, "list_subkeys", test_list_subkeys);
508         torture_tcase_add_simple_test(tcase, "get_predefined_key",
509                                                                         test_get_predefined);
510         torture_tcase_add_simple_test(tcase, "get_predefined_key",
511                                                                         test_get_predefined_unknown);
512         torture_tcase_add_simple_test(tcase, "create_key", test_create_subkey);
513         torture_tcase_add_simple_test(tcase, "create_key", 
514                                                                   test_create_nested_subkey);
515         torture_tcase_add_simple_test(tcase, "key_add_abs", test_key_add_abs);
516         torture_tcase_add_simple_test(tcase, "key_add_abs_top", test_key_add_abs_top);
517         torture_tcase_add_simple_test(tcase, "set_value", test_set_value);
518         torture_tcase_add_simple_test(tcase, "get_value", test_get_value);
519         torture_tcase_add_simple_test(tcase, "list_values", test_list_values);
520         torture_tcase_add_simple_test(tcase, "del_key", test_del_key);
521         torture_tcase_add_simple_test(tcase, "del_value", test_del_value);
522         torture_tcase_add_simple_test(tcase, "flush_key", test_flush_key);
523         torture_tcase_add_simple_test(tcase, "query_key", test_query_key);
524         torture_tcase_add_simple_test(tcase, "query_key_nums", test_query_key_nums);
525         torture_tcase_add_simple_test(tcase, "test_predef_key_by_name", 
526                                                                   test_predef_key_by_name);
527         torture_tcase_add_simple_test(tcase, "test_predef_key_by_name_invalid", 
528                                                                   test_predef_key_by_name_invalid);
529 }
530
531 struct torture_suite *torture_registry_registry(TALLOC_CTX *mem_ctx) 
532 {
533         struct torture_tcase *tcase;
534         struct torture_suite *suite = torture_suite_create(mem_ctx, 
535                                                                                                            "REGISTRY");
536         
537         tcase = torture_suite_add_tcase(suite, "local");
538         torture_tcase_set_fixture(tcase, setup_local_registry, NULL);
539         tcase_add_tests(tcase);
540
541         return suite;
542 }