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