2 Unix SMB/CIFS implementation.
4 local testing of registry library - registry backend
6 Copyright (C) Jelmer Vernooij 2005-2007
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.
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.
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.
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"
31 NTSTATUS torture_temp_dir(struct torture_context *tctx, const char *prefix,
32 const char **tempdir);
35 * Test obtaining a predefined key.
37 static bool test_get_predefined(struct torture_context *tctx,
40 const struct registry_context *rctx =
41 (const struct registry_context *)_data;
42 struct registry_key *root;
45 error = reg_get_predefined_key(rctx, HKEY_CLASSES_ROOT, &root);
46 torture_assert_werr_ok(tctx, error,
47 "getting predefined key failed");
52 * Test obtaining a predefined key.
54 static bool test_get_predefined_unknown(struct torture_context *tctx,
57 const struct registry_context *rctx = _data;
58 struct registry_key *root;
61 error = reg_get_predefined_key(rctx, 1337, &root);
62 torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
63 "getting predefined key failed");
67 static bool test_predef_key_by_name(struct torture_context *tctx,
70 const struct registry_context *rctx =
71 (const struct registry_context *)_data;
72 struct registry_key *root;
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");
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");
86 static bool test_predef_key_by_name_invalid(struct torture_context *tctx,
89 const struct registry_context *rctx =
90 (const struct registry_context *)_data;
91 struct registry_key *root;
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");
101 * Test creating a new subkey
103 static bool test_create_subkey(struct torture_context *tctx,
106 const struct registry_context *rctx =
107 (const struct registry_context *)_data;
108 struct registry_key *root, *newkey;
111 error = reg_get_predefined_key(rctx, HKEY_CLASSES_ROOT, &root);
112 torture_assert_werr_ok(tctx, error,
113 "getting predefined key failed");
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");
123 * Test creating a new nested subkey
125 static bool test_create_nested_subkey(struct torture_context *tctx,
128 const struct registry_context *rctx =
129 (const struct registry_context *)_data;
130 struct registry_key *root, *newkey1, *newkey2;
133 error = reg_get_predefined_key(rctx, HKEY_CLASSES_ROOT, &root);
134 torture_assert_werr_ok(tctx, error,
135 "getting predefined key failed");
137 error = reg_key_add_name(rctx, root, "Hamburg", NULL, NULL,
139 torture_assert_werr_ok(tctx, error, "Creating key return code");
140 torture_assert(tctx, newkey1 != NULL, "Creating new key");
142 error = reg_key_add_name(rctx, root, "Hamburg\\Hamburg", NULL, NULL,
144 torture_assert_werr_ok(tctx, error, "Creating key return code");
145 torture_assert(tctx, newkey2 != NULL, "Creating new key");
151 * Test creating a new subkey
153 static bool test_key_add_abs_top(struct torture_context *tctx,
156 const struct registry_context *rctx =
157 (const struct registry_context *)_data;
158 struct registry_key *root;
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");
168 * Test creating a new subkey
170 static bool test_key_add_abs(struct torture_context *tctx,
174 const struct registry_context *rctx =
175 (const struct registry_context *)_data;
176 struct registry_key *root, *result1, *result2;
178 error = reg_key_add_abs(tctx, rctx, "HKEY_CLASSES_ROOT\\bloe", 0, NULL, &result1);
179 torture_assert_werr_ok(tctx, error, "create lowest");
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");
184 error = reg_get_predefined_key(rctx, HKEY_CLASSES_ROOT, &root);
185 torture_assert_werr_ok(tctx, error,
186 "getting predefined key failed");
188 error = reg_open_key(tctx, root, "bloe", &result2);
189 torture_assert_werr_ok(tctx, error, "opening key");
191 error = reg_open_key(tctx, root, "bloe\\bla", &result2);
192 torture_assert_werr_ok(tctx, error, "opening key");
198 static bool test_del_key(struct torture_context *tctx, const void *_data)
200 const struct registry_context *rctx =
201 (const struct registry_context *)_data;
202 struct registry_key *root, *newkey;
205 error = reg_get_predefined_key(rctx, HKEY_CLASSES_ROOT, &root);
206 torture_assert_werr_ok(tctx, error,
207 "getting predefined key failed");
209 error = reg_key_add_name(rctx, root, "Hamburg", NULL, NULL, &newkey);
211 torture_assert_werr_ok(tctx, error, "Creating key return code");
212 torture_assert(tctx, newkey != NULL, "Creating new key");
214 error = reg_key_del(root, "Hamburg");
215 torture_assert_werr_ok(tctx, error, "Delete key");
217 error = reg_key_del(root, "Hamburg");
218 torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
219 "Delete missing key");
225 * Convenience function for opening the HKEY_CLASSES_ROOT hive and
226 * creating a single key for testing purposes.
228 static bool create_test_key(struct torture_context *tctx,
229 const struct registry_context *rctx,
231 struct registry_key **root,
232 struct registry_key **subkey)
236 error = reg_get_predefined_key(rctx, HKEY_CLASSES_ROOT, root);
237 torture_assert_werr_ok(tctx, error,
238 "getting predefined key failed");
240 error = reg_key_add_name(rctx, *root, name, NULL, NULL, subkey);
241 torture_assert_werr_ok(tctx, error, "Creating key return code");
247 static bool test_flush_key(struct torture_context *tctx, const void *_data)
249 const struct registry_context *rctx =
250 (const struct registry_context *)_data;
251 struct registry_key *root, *subkey;
254 if (!create_test_key(tctx, rctx, "Munchen", &root, &subkey))
257 error = reg_key_flush(subkey);
258 torture_assert_werr_ok(tctx, error, "flush key");
260 torture_assert_werr_equal(tctx, reg_key_flush(NULL),
261 WERR_INVALID_PARAM, "flush key");
266 static bool test_query_key(struct torture_context *tctx, const void *_data)
268 const struct registry_context *rctx =
269 (const struct registry_context *)_data;
270 struct registry_key *root, *subkey;
272 NTTIME last_changed_time;
273 uint32_t num_subkeys, num_values;
274 const char *classname;
276 if (!create_test_key(tctx, rctx, "Munchen", &root, &subkey))
279 error = reg_key_get_info(tctx, subkey, &classname,
280 &num_subkeys, &num_values,
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");
291 static bool test_query_key_nums(struct torture_context *tctx, const void *_data)
293 const struct registry_context *rctx =
294 (const struct registry_context *)_data;
295 struct registry_key *root, *subkey1, *subkey2;
297 uint32_t num_subkeys, num_values;
300 if (!create_test_key(tctx, rctx, "Berlin", &root, &subkey1))
303 error = reg_key_add_name(rctx, subkey1, "Bentheim", NULL, NULL, &subkey2);
304 torture_assert_werr_ok(tctx, error, "Creating key return code");
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");
310 error = reg_key_get_info(tctx, subkey1, NULL, &num_subkeys,
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");
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.
325 static bool test_list_subkeys(struct torture_context *tctx, const void *_data)
327 const struct registry_context *rctx =
328 (const struct registry_context *)_data;
329 struct registry_key *subkey = NULL, *root;
331 NTTIME last_mod_time;
332 const char *classname, *name;
334 if (!create_test_key(tctx, rctx, "Goettingen", &root, &subkey))
337 error = reg_key_get_subkey_by_index(tctx, root, 0, &name, &classname,
340 torture_assert_werr_ok(tctx, error, "Enum keys return code");
341 torture_assert_str_equal(tctx, name, "Goettingen", "Enum keys data");
344 error = reg_key_get_subkey_by_index(tctx, root, 0, NULL, NULL, NULL);
346 torture_assert_werr_ok(tctx, error, "Enum keys with NULL arguments return code");
348 error = reg_key_get_subkey_by_index(tctx, root, 1, NULL, NULL, NULL);
350 torture_assert_werr_equal(tctx, error, WERR_NO_MORE_ITEMS,
351 "Invalid error for no more items");
353 error = reg_key_get_subkey_by_index(tctx, subkey, 0, NULL, NULL, NULL);
355 torture_assert_werr_equal(tctx, error, WERR_NO_MORE_ITEMS,
356 "Invalid error for no more items");
362 * Test setting a value
364 static bool test_set_value(struct torture_context *tctx, const void *_data)
366 const struct registry_context *rctx =
367 (const struct registry_context *)_data;
368 struct registry_key *subkey = NULL, *root;
372 if (!create_test_key(tctx, rctx, "Dusseldorf", &root, &subkey))
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");
383 * Test getting/setting security descriptors
385 static bool test_security(struct torture_context *tctx, const void *_data)
387 const struct registry_context *rctx =
388 (const struct registry_context *)_data;
389 struct registry_key *subkey = NULL, *root;
391 struct security_descriptor *osd, *nsd;
393 if (!create_test_key(tctx, rctx, "Düsseldorf", &root, &subkey))
396 osd = security_descriptor_create(tctx,
398 SID_NT_AUTHENTICATED_USERS,
399 SEC_ACE_TYPE_ACCESS_ALLOWED,
401 SEC_ACE_FLAG_OBJECT_INHERIT,
404 error = reg_set_security(subkey, osd);
405 torture_assert_werr_ok(tctx, error, "setting security");
407 error = reg_get_security(tctx, subkey, &nsd);
408 torture_assert_werr_ok (tctx, error, "setting security");
410 torture_assert(tctx, security_descriptor_equal(osd, nsd),
411 "security descriptor changed!");
417 * Test getting a value
419 static bool test_get_value(struct torture_context *tctx, const void *_data)
421 const struct registry_context *rctx =
422 (const struct registry_context *)_data;
423 struct registry_key *subkey = NULL, *root;
429 if (!create_test_key(tctx, rctx, "Duisburg", &root, &subkey))
432 error = reg_key_get_value_by_name(tctx, subkey, __FUNCTION__, &type,
434 torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
435 "getting missing value");
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");
441 error = reg_key_get_value_by_name(tctx, subkey, __FUNCTION__, &type,
443 torture_assert_werr_ok(tctx, error, "getting value");
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");
453 * Test unsetting a value
455 static bool test_del_value(struct torture_context *tctx, const void *_data)
457 const struct registry_context *rctx =
458 (const struct registry_context *)_data;
459 struct registry_key *subkey = NULL, *root;
465 if (!create_test_key(tctx, rctx, "Duisburg", &root, &subkey))
468 error = reg_key_get_value_by_name(tctx, subkey, __FUNCTION__, &type,
470 torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
471 "getting missing value");
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");
477 error = reg_del_value(subkey, __FUNCTION__);
478 torture_assert_werr_ok (tctx, error, "unsetting value");
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");
488 * Test listing values
490 static bool test_list_values(struct torture_context *tctx, const void *_data)
492 const struct registry_context *rctx =
493 (const struct registry_context *)_data;
494 struct registry_key *subkey = NULL, *root;
501 if (!create_test_key(tctx, rctx, "Bonn", &root, &subkey))
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");
508 error = reg_key_get_value_by_index(tctx, subkey, 0, &name, &type, &data);
509 torture_assert_werr_ok(tctx, error, "getting value");
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");
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");
523 static bool setup_local_registry(struct torture_context *tctx, void **data)
525 struct registry_context *rctx;
529 struct hive_key *hive_key;
530 const char *filename;
532 error = reg_open_local(tctx, &rctx, NULL, NULL);
533 torture_assert_werr_ok(tctx, error, "Opening local registry failed");
535 status = torture_temp_dir(tctx, "registry-local", &tempdir);
536 torture_assert_ntstatus_ok(tctx, status, "Creating temp dir failed");
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");
542 error = reg_mount_hive(rctx, hive_key, HKEY_CLASSES_ROOT, NULL);
543 torture_assert_werr_ok(tctx, error, "Mounting hive failed");
550 static void tcase_add_tests(struct torture_tcase *tcase)
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);
574 struct torture_suite *torture_registry_registry(TALLOC_CTX *mem_ctx)
576 struct torture_tcase *tcase;
577 struct torture_suite *suite = torture_suite_create(mem_ctx, "REGISTRY");
579 tcase = torture_suite_add_tcase(suite, "local");
580 torture_tcase_set_fixture(tcase, setup_local_registry, NULL);
581 tcase_add_tests(tcase);