2 Unix SMB/CIFS implementation.
4 local testing of registry library - hives
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 3 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 "torture/torture.h"
26 #include "librpc/gen_ndr/winreg.h"
27 #include "system/filesys.h"
28 #include "param/param.h"
29 #include "libcli/security/security.h"
31 static bool test_del_nonexistant_key(struct torture_context *tctx,
32 const void *test_data)
34 const struct hive_key *root = (const struct hive_key *)test_data;
35 WERROR error = hive_key_del(root, "bla");
36 torture_assert_werr_equal(tctx, error, WERR_BADFILE,
37 "invalid return code");
42 static bool test_keyinfo_root(struct torture_context *tctx,
43 const void *test_data)
45 uint32_t num_subkeys, num_values;
46 const struct hive_key *root = (const struct hive_key *)test_data;
49 /* This is a new backend. There should be no subkeys and no
51 error = hive_key_get_info(tctx, root, NULL, &num_subkeys, &num_values,
52 NULL, NULL, NULL, NULL);
53 torture_assert_werr_ok(tctx, error, "reg_key_num_subkeys()");
55 torture_assert_int_equal(tctx, num_subkeys, 0,
56 "New key has non-zero subkey count");
58 torture_assert_werr_ok(tctx, error, "reg_key_num_values");
60 torture_assert_int_equal(tctx, num_values, 0,
61 "New key has non-zero value count");
66 static bool test_keyinfo_nums(struct torture_context *tctx, void *test_data)
68 uint32_t num_subkeys, num_values;
69 struct hive_key *root = (struct hive_key *)test_data;
71 struct hive_key *subkey;
75 error = hive_key_add_name(tctx, root, "Nested Keyll", NULL,
77 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
79 error = hive_key_set_value(root, "Answer", REG_DWORD,
80 data_blob_talloc(tctx, data, sizeof(data)));
81 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
83 /* This is a new backend. There should be no subkeys and no
85 error = hive_key_get_info(tctx, root, NULL, &num_subkeys, &num_values,
86 NULL, NULL, NULL, NULL);
87 torture_assert_werr_ok(tctx, error, "reg_key_num_subkeys()");
89 torture_assert_int_equal(tctx, num_subkeys, 1, "subkey count");
91 torture_assert_werr_ok(tctx, error, "reg_key_num_values");
93 torture_assert_int_equal(tctx, num_values, 1, "value count");
98 static bool test_add_subkey(struct torture_context *tctx,
99 const void *test_data)
102 struct hive_key *subkey;
103 const struct hive_key *root = (const struct hive_key *)test_data;
104 TALLOC_CTX *mem_ctx = tctx;
106 error = hive_key_add_name(mem_ctx, root, "Nested Key", NULL,
108 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
110 error = hive_key_del(root, "Nested Key");
111 torture_assert_werr_ok(tctx, error, "reg_key_del");
116 static bool test_del_recursive(struct torture_context *tctx,
117 const void *test_data)
120 struct hive_key *subkey;
121 struct hive_key *subkey2;
122 const struct hive_key *root = (const struct hive_key *)test_data;
123 TALLOC_CTX *mem_ctx = tctx;
127 /* Create a new key under the root */
128 error = hive_key_add_name(mem_ctx, root, "Parent Key", NULL,
130 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
132 /* Create a new key under "Parent Key" */
133 error = hive_key_add_name(mem_ctx, subkey, "Child Key", NULL,
135 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
137 /* Create a new value under "Child Key" */
138 error = hive_key_set_value(subkey2, "Answer Recursive", REG_DWORD,
139 data_blob_talloc(mem_ctx, data, sizeof(data)));
140 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
142 /* Deleting "Parent Key" will also delete "Child Key" and the value. */
143 error = hive_key_del(root, "Parent Key");
144 torture_assert_werr_ok(tctx, error, "hive_key_del");
149 static bool test_flush_key(struct torture_context *tctx, void *test_data)
151 struct hive_key *root = (struct hive_key *)test_data;
153 torture_assert_werr_ok(tctx, hive_key_flush(root), "flush key");
158 static bool test_del_key(struct torture_context *tctx, const void *test_data)
161 struct hive_key *subkey;
162 const struct hive_key *root = (const struct hive_key *)test_data;
163 TALLOC_CTX *mem_ctx = tctx;
165 error = hive_key_add_name(mem_ctx, root, "Nested Key", NULL,
167 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
169 error = hive_key_del(root, "Nested Key");
170 torture_assert_werr_ok(tctx, error, "reg_key_del");
172 error = hive_key_del(root, "Nested Key");
173 torture_assert_werr_equal(tctx, error, WERR_BADFILE, "reg_key_del");
178 static bool test_set_value(struct torture_context *tctx,
179 const void *test_data)
182 struct hive_key *subkey;
183 const struct hive_key *root = (const struct hive_key *)test_data;
184 TALLOC_CTX *mem_ctx = tctx;
188 error = hive_key_add_name(mem_ctx, root, "YA Nested Key", NULL,
190 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
192 error = hive_key_set_value(subkey, "Answer", REG_DWORD,
193 data_blob_talloc(mem_ctx, data, sizeof(data)));
194 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
199 static bool test_get_value(struct torture_context *tctx, const void *test_data)
202 struct hive_key *subkey;
203 const struct hive_key *root = (const struct hive_key *)test_data;
204 TALLOC_CTX *mem_ctx = tctx;
211 error = hive_key_add_name(mem_ctx, root, "EYA Nested Key", NULL,
213 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
215 error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
216 torture_assert_werr_equal(tctx, error, WERR_BADFILE,
217 "getting missing value");
219 error = hive_key_set_value(subkey, "Answer", REG_DWORD,
220 data_blob_talloc(mem_ctx, data, sizeof(data)));
221 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
223 error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
224 torture_assert_werr_ok(tctx, error, "getting value");
226 torture_assert_int_equal(tctx, value.length, 4, "value length");
227 torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
229 torture_assert_mem_equal(tctx, &data, value.data, sizeof(uint32_t),
235 static bool test_del_value(struct torture_context *tctx, const void *test_data)
238 struct hive_key *subkey;
239 const struct hive_key *root = (const struct hive_key *)test_data;
240 TALLOC_CTX *mem_ctx = tctx;
247 error = hive_key_add_name(mem_ctx, root, "EEYA Nested Key", NULL,
249 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
251 error = hive_key_set_value(subkey, "Answer", REG_DWORD,
252 data_blob_talloc(mem_ctx, data, sizeof(data)));
253 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
255 error = hive_key_del_value(subkey, "Answer");
256 torture_assert_werr_ok(tctx, error, "deleting value");
258 error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
259 torture_assert_werr_equal(tctx, error, WERR_BADFILE, "getting value");
261 error = hive_key_del_value(subkey, "Answer");
262 torture_assert_werr_equal(tctx, error, WERR_BADFILE,
268 static bool test_list_values(struct torture_context *tctx,
269 const void *test_data)
272 struct hive_key *subkey;
273 const struct hive_key *root = (const struct hive_key *)test_data;
274 TALLOC_CTX *mem_ctx = tctx;
280 SIVAL(data, 0, data_val);
282 error = hive_key_add_name(mem_ctx, root, "AYAYA Nested Key", NULL,
284 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
286 error = hive_key_set_value(subkey, "Answer", REG_DWORD,
287 data_blob_talloc(mem_ctx, data, sizeof(data)));
288 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
290 error = hive_get_value_by_index(mem_ctx, subkey, 0, &name,
292 torture_assert_werr_ok(tctx, error, "getting value");
294 torture_assert_str_equal(tctx, name, "Answer", "value name");
296 torture_assert_int_equal(tctx, value.length, 4, "value length");
297 torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
300 torture_assert_int_equal(tctx, data_val, IVAL(value.data, 0), "value data");
302 error = hive_get_value_by_index(mem_ctx, subkey, 1, &name,
304 torture_assert_werr_equal(tctx, error, WERR_NO_MORE_ITEMS,
305 "getting missing value");
310 static bool test_hive_security(struct torture_context *tctx, const void *_data)
312 struct hive_key *subkey = NULL;
313 const struct hive_key *root = _data;
315 struct security_descriptor *osd, *nsd;
317 osd = security_descriptor_dacl_create(tctx,
320 SID_NT_AUTHENTICATED_USERS,
321 SEC_ACE_TYPE_ACCESS_ALLOWED,
323 SEC_ACE_FLAG_OBJECT_INHERIT,
327 error = hive_key_add_name(tctx, root, "SecurityKey", NULL,
329 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
331 error = hive_get_sec_desc(tctx, subkey, &nsd);
332 torture_assert_werr_ok (tctx, error, "getting security descriptor");
334 torture_assert(tctx, security_descriptor_equal(osd, nsd),
335 "security descriptor changed!");
337 /* Create a fresh security descriptor */
339 osd = security_descriptor_dacl_create(tctx,
342 SID_NT_AUTHENTICATED_USERS,
343 SEC_ACE_TYPE_ACCESS_ALLOWED,
345 SEC_ACE_FLAG_OBJECT_INHERIT,
348 error = hive_set_sec_desc(subkey, osd);
349 torture_assert_werr_ok(tctx, error, "setting security descriptor");
351 error = hive_get_sec_desc(tctx, subkey, &nsd);
352 torture_assert_werr_ok (tctx, error, "getting security descriptor");
354 torture_assert(tctx, security_descriptor_equal(osd, nsd),
355 "security descriptor changed!");
360 static void tcase_add_tests(struct torture_tcase *tcase)
362 torture_tcase_add_simple_test_const(tcase, "del_nonexistant_key",
363 test_del_nonexistant_key);
364 torture_tcase_add_simple_test_const(tcase, "add_subkey",
366 torture_tcase_add_simple_test(tcase, "flush_key",
368 /* test_del_recursive() test must run before test_keyinfo_root().
369 test_keyinfo_root() checks the number of subkeys, which verifies
370 the recursive delete worked properly. */
371 torture_tcase_add_simple_test_const(tcase, "del_recursive",
373 torture_tcase_add_simple_test_const(tcase, "get_info",
375 torture_tcase_add_simple_test(tcase, "get_info_nums",
377 torture_tcase_add_simple_test_const(tcase, "set_value",
379 torture_tcase_add_simple_test_const(tcase, "get_value",
381 torture_tcase_add_simple_test_const(tcase, "list_values",
383 torture_tcase_add_simple_test_const(tcase, "del_key",
385 torture_tcase_add_simple_test_const(tcase, "del_value",
387 torture_tcase_add_simple_test_const(tcase, "check hive security",
391 static bool hive_setup_dir(struct torture_context *tctx, void **data)
393 struct hive_key *key;
398 status = torture_temp_dir(tctx, "hive-dir", &dirname);
399 if (!NT_STATUS_IS_OK(status))
404 error = reg_create_directory(tctx, dirname, &key);
405 if (!W_ERROR_IS_OK(error)) {
406 fprintf(stderr, "Unable to initialize dir hive\n");
415 static bool hive_setup_ldb(struct torture_context *tctx, void **data)
417 struct hive_key *key;
422 status = torture_temp_dir(tctx, "hive-ldb", &dirname);
423 if (!NT_STATUS_IS_OK(status))
428 error = reg_open_ldb_file(tctx, dirname, NULL, NULL, tctx->ev, tctx->lp_ctx, &key);
429 if (!W_ERROR_IS_OK(error)) {
430 fprintf(stderr, "Unable to initialize ldb hive\n");
439 static bool hive_setup_regf(struct torture_context *tctx, void **data)
441 struct hive_key *key;
446 status = torture_temp_dir(tctx, "hive-regf", &dirname);
447 if (!NT_STATUS_IS_OK(status))
452 error = reg_create_regf_file(tctx, lp_iconv_convenience(tctx->lp_ctx),
454 if (!W_ERROR_IS_OK(error)) {
455 fprintf(stderr, "Unable to create new regf file\n");
464 static bool test_dir_refuses_null_location(struct torture_context *tctx)
466 torture_assert_werr_equal(tctx, WERR_INVALID_PARAM,
467 reg_open_directory(NULL, NULL, NULL),
468 "reg_open_directory accepts NULL location");
472 struct torture_suite *torture_registry_hive(TALLOC_CTX *mem_ctx)
474 struct torture_tcase *tcase;
475 struct torture_suite *suite = torture_suite_create(mem_ctx, "HIVE");
477 torture_suite_add_simple_test(suite, "dir-refuses-null-location",
478 test_dir_refuses_null_location);
480 tcase = torture_suite_add_tcase(suite, "dir");
481 torture_tcase_set_fixture(tcase, hive_setup_dir, NULL);
482 tcase_add_tests(tcase);
484 tcase = torture_suite_add_tcase(suite, "ldb");
485 torture_tcase_set_fixture(tcase, hive_setup_ldb, NULL);
486 tcase_add_tests(tcase);
488 tcase = torture_suite_add_tcase(suite, "regf");
489 torture_tcase_set_fixture(tcase, hive_setup_regf, NULL);
490 tcase_add_tests(tcase);