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