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 "system/filesys.h"
30 NTSTATUS torture_temp_dir(struct torture_context *tctx, const char *prefix,
31 const char **tempdir);
34 * Test obtaining a predefined key.
36 static bool test_get_predefined(struct torture_context *tctx,
39 const struct registry_context *rctx = _data;
40 struct registry_key *root;
43 error = reg_get_predefined_key(rctx, HKEY_CLASSES_ROOT, &root);
44 torture_assert_werr_ok(tctx, error,
45 "getting predefined key failed");
50 * Test obtaining a predefined key.
52 static bool test_get_predefined_unknown(struct torture_context *tctx,
55 const struct registry_context *rctx = _data;
56 struct registry_key *root;
59 error = reg_get_predefined_key(rctx, 1337, &root);
60 torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
61 "getting predefined key failed");
65 static bool test_predef_key_by_name(struct torture_context *tctx,
68 const struct registry_context *rctx = _data;
69 struct registry_key *root;
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");
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");
83 static bool test_predef_key_by_name_invalid(struct torture_context *tctx,
86 const struct registry_context *rctx = _data;
87 struct registry_key *root;
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");
97 * Test creating a new subkey
99 static bool test_create_subkey(struct torture_context *tctx,
102 const struct registry_context *rctx = _data;
103 struct registry_key *root, *newkey;
106 error = reg_get_predefined_key(rctx, HKEY_CLASSES_ROOT, &root);
107 torture_assert_werr_ok(tctx, error,
108 "getting predefined key failed");
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");
118 * Test creating a new nested subkey
120 static bool test_create_nested_subkey(struct torture_context *tctx,
123 const struct registry_context *rctx = _data;
124 struct registry_key *root, *newkey1, *newkey2;
127 error = reg_get_predefined_key(rctx, HKEY_CLASSES_ROOT, &root);
128 torture_assert_werr_ok(tctx, error,
129 "getting predefined key failed");
131 error = reg_key_add_name(rctx, root, "Hamburg", NULL, NULL,
133 torture_assert_werr_ok(tctx, error, "Creating key return code");
134 torture_assert(tctx, newkey2 != NULL, "Creating new key");
136 error = reg_key_add_name(rctx, root, "Hamburg\\Hamburg", NULL, NULL,
138 torture_assert_werr_ok(tctx, error, "Creating key return code");
139 torture_assert(tctx, newkey2 != NULL, "Creating new key");
145 * Test creating a new subkey
147 static bool test_key_add_abs_top(struct torture_context *tctx,
150 const struct registry_context *rctx = _data;
151 struct registry_key *root;
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");
161 * Test creating a new subkey
163 static bool test_key_add_abs(struct torture_context *tctx,
167 const struct registry_context *rctx = _data;
168 struct registry_key *root, *result1, *result2;
170 error = reg_key_add_abs(tctx, rctx, "HKEY_CLASSES_ROOT\\bloe", 0, NULL, &result1);
171 torture_assert_werr_ok(tctx, error, "create lowest");
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");
176 error = reg_get_predefined_key(rctx, HKEY_CLASSES_ROOT, &root);
177 torture_assert_werr_ok(tctx, error,
178 "getting predefined key failed");
180 error = reg_open_key(tctx, root, "bloe", &result2);
181 torture_assert_werr_ok(tctx, error, "opening key");
183 error = reg_open_key(tctx, root, "bloe\\bla", &result2);
184 torture_assert_werr_ok(tctx, error, "opening key");
190 static bool test_del_key(struct torture_context *tctx, const void *_data)
192 const struct registry_context *rctx = _data;
193 struct registry_key *root, *newkey;
196 error = reg_get_predefined_key(rctx, HKEY_CLASSES_ROOT, &root);
197 torture_assert_werr_ok(tctx, error,
198 "getting predefined key failed");
200 error = reg_key_add_name(rctx, root, "Hamburg", NULL, NULL, &newkey);
202 torture_assert_werr_ok(tctx, error, "Creating key return code");
203 torture_assert(tctx, newkey != NULL, "Creating new key");
205 error = reg_key_del(root, "Hamburg");
206 torture_assert_werr_ok(tctx, error, "Delete key");
208 error = reg_key_del(root, "Hamburg");
209 torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
210 "Delete missing key");
216 * Convenience function for opening the HKEY_CLASSES_ROOT hive and
217 * creating a single key for testing purposes.
219 static bool create_test_key(struct torture_context *tctx,
220 const struct registry_context *rctx,
222 struct registry_key **root,
223 struct registry_key **subkey)
227 error = reg_get_predefined_key(rctx, HKEY_CLASSES_ROOT, root);
228 torture_assert_werr_ok(tctx, error,
229 "getting predefined key failed");
231 error = reg_key_add_name(rctx, *root, name, NULL, NULL, subkey);
232 torture_assert_werr_ok(tctx, error, "Creating key return code");
238 static bool test_flush_key(struct torture_context *tctx, const void *_data)
240 const struct registry_context *rctx = _data;
241 struct registry_key *root, *subkey;
244 if (!create_test_key(tctx, rctx, "Munchen", &root, &subkey))
247 error = reg_key_flush(subkey);
248 torture_assert_werr_ok(tctx, error, "flush key");
250 torture_assert_werr_equal(tctx, reg_key_flush(NULL),
251 WERR_INVALID_PARAM, "flush key");
256 static bool test_query_key(struct torture_context *tctx, const void *_data)
258 const struct registry_context *rctx = _data;
259 struct registry_key *root, *subkey;
261 NTTIME last_changed_time;
262 uint32_t num_subkeys, num_values;
263 const char *classname;
265 if (!create_test_key(tctx, rctx, "Munchen", &root, &subkey))
268 error = reg_key_get_info(tctx, subkey, &classname,
269 &num_subkeys, &num_values,
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");
280 static bool test_query_key_nums(struct torture_context *tctx, const void *_data)
282 const struct registry_context *rctx = _data;
283 struct registry_key *root, *subkey1, *subkey2;
285 uint32_t num_subkeys, num_values;
288 if (!create_test_key(tctx, rctx, "Berlin", &root, &subkey1))
291 error = reg_key_add_name(rctx, subkey1, "Bentheim", NULL, NULL, &subkey2);
292 torture_assert_werr_ok(tctx, error, "Creating key return code");
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");
298 error = reg_key_get_info(tctx, subkey1, NULL, &num_subkeys,
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");
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.
313 static bool test_list_subkeys(struct torture_context *tctx, const void *_data)
315 const struct registry_context *rctx = _data;
316 struct registry_key *subkey = NULL, *root;
318 NTTIME last_mod_time;
319 const char *classname, *name;
321 if (!create_test_key(tctx, rctx, "Goettingen", &root, &subkey))
324 error = reg_key_get_subkey_by_index(tctx, root, 0, &name, &classname,
327 torture_assert_werr_ok(tctx, error, "Enum keys return code");
328 torture_assert_str_equal(tctx, name, "Goettingen", "Enum keys data");
331 error = reg_key_get_subkey_by_index(tctx, root, 0, NULL, NULL, NULL);
333 torture_assert_werr_ok(tctx, error, "Enum keys with NULL arguments return code");
335 error = reg_key_get_subkey_by_index(tctx, root, 1, NULL, NULL, NULL);
337 torture_assert_werr_equal(tctx, error, WERR_NO_MORE_ITEMS,
338 "Invalid error for no more items");
340 error = reg_key_get_subkey_by_index(tctx, subkey, 0, NULL, NULL, NULL);
342 torture_assert_werr_equal(tctx, error, WERR_NO_MORE_ITEMS,
343 "Invalid error for no more items");
349 * Test setting a value
351 static bool test_set_value(struct torture_context *tctx, const void *_data)
353 const struct registry_context *rctx = _data;
354 struct registry_key *subkey = NULL, *root;
358 if (!create_test_key(tctx, rctx, "Dusseldorf", &root, &subkey))
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");
369 * Test getting a value
371 static bool test_get_value(struct torture_context *tctx, const void *_data)
373 const struct registry_context *rctx = _data;
374 struct registry_key *subkey = NULL, *root;
380 if (!create_test_key(tctx, rctx, "Duisburg", &root, &subkey))
383 error = reg_key_get_value_by_name(tctx, subkey, __FUNCTION__, &type,
385 torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
386 "getting missing value");
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");
392 error = reg_key_get_value_by_name(tctx, subkey, __FUNCTION__, &type,
394 torture_assert_werr_ok(tctx, error, "getting value");
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");
404 * Test unsetting a value
406 static bool test_del_value(struct torture_context *tctx, const void *_data)
408 const struct registry_context *rctx = _data;
409 struct registry_key *subkey = NULL, *root;
415 if (!create_test_key(tctx, rctx, "Duisburg", &root, &subkey))
418 error = reg_key_get_value_by_name(tctx, subkey, __FUNCTION__, &type,
420 torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
421 "getting missing value");
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");
427 error = reg_del_value(subkey, __FUNCTION__);
428 torture_assert_werr_ok (tctx, error, "unsetting value");
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");
438 * Test listing values
440 static bool test_list_values(struct torture_context *tctx, const void *_data)
442 const struct registry_context *rctx = _data;
443 struct registry_key *subkey = NULL, *root;
450 if (!create_test_key(tctx, rctx, "Bonn", &root, &subkey))
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");
457 error = reg_key_get_value_by_index(tctx, subkey, 0, &name, &type, &data);
458 torture_assert_werr_ok(tctx, error, "getting value");
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");
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");
472 static bool setup_local_registry(struct torture_context *tctx, void **data)
474 struct registry_context *rctx;
478 struct hive_key *hive_key;
480 error = reg_open_local(tctx, &rctx, NULL, NULL);
481 if (!W_ERROR_IS_OK(error))
484 status = torture_temp_dir(tctx, "registry-local", &tempdir);
485 if (!NT_STATUS_IS_OK(status))
488 error = reg_open_ldb_file(tctx,
489 talloc_asprintf(tctx, "%s/classes_root.ldb", tempdir),
493 if (!W_ERROR_IS_OK(error))
496 error = reg_mount_hive(rctx, hive_key, HKEY_CLASSES_ROOT, NULL);
497 if (!W_ERROR_IS_OK(error))
505 static void tcase_add_tests(struct torture_tcase *tcase)
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);
531 struct torture_suite *torture_registry_registry(TALLOC_CTX *mem_ctx)
533 struct torture_tcase *tcase;
534 struct torture_suite *suite = torture_suite_create(mem_ctx,
537 tcase = torture_suite_add_tcase(suite, "local");
538 torture_tcase_set_fixture(tcase, setup_local_registry, NULL);
539 tcase_add_tests(tcase);