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"
30 static bool test_del_nonexistant_key(struct torture_context *tctx,
31 const void *test_data)
33 const struct hive_key *root = (const struct hive_key *)test_data;
34 WERROR error = hive_key_del(root, "bla");
35 torture_assert_werr_equal(tctx, error, WERR_BADFILE,
36 "invalid return code");
41 static bool test_keyinfo_root(struct torture_context *tctx,
42 const void *test_data)
44 uint32_t num_subkeys, num_values;
45 const struct hive_key *root = (const struct hive_key *)test_data;
48 /* This is a new backend. There should be no subkeys and no
50 error = hive_key_get_info(tctx, root, NULL, &num_subkeys, &num_values,
51 NULL, NULL, NULL, NULL);
52 torture_assert_werr_ok(tctx, error, "reg_key_num_subkeys()");
54 torture_assert_int_equal(tctx, num_subkeys, 0,
55 "New key has non-zero subkey count");
57 torture_assert_werr_ok(tctx, error, "reg_key_num_values");
59 torture_assert_int_equal(tctx, num_values, 0,
60 "New key has non-zero value count");
65 static bool test_keyinfo_nums(struct torture_context *tctx, void *test_data)
67 uint32_t num_subkeys, num_values;
68 struct hive_key *root = (struct hive_key *)test_data;
70 struct hive_key *subkey;
74 error = hive_key_add_name(tctx, root, "Nested Keyll", NULL,
76 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
78 error = hive_key_set_value(root, "Answer", REG_DWORD,
79 data_blob_talloc(tctx, data, sizeof(data)));
80 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
82 /* This is a new backend. There should be no subkeys and no
84 error = hive_key_get_info(tctx, root, NULL, &num_subkeys, &num_values,
85 NULL, NULL, NULL, NULL);
86 torture_assert_werr_ok(tctx, error, "reg_key_num_subkeys()");
88 torture_assert_int_equal(tctx, num_subkeys, 1, "subkey count");
90 torture_assert_werr_ok(tctx, error, "reg_key_num_values");
92 torture_assert_int_equal(tctx, num_values, 1, "value count");
97 static bool test_add_subkey(struct torture_context *tctx,
98 const void *test_data)
101 struct hive_key *subkey;
102 const struct hive_key *root = (const struct hive_key *)test_data;
103 TALLOC_CTX *mem_ctx = tctx;
105 error = hive_key_add_name(mem_ctx, root, "Nested Key", NULL,
107 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
109 error = hive_key_del(root, "Nested Key");
110 torture_assert_werr_ok(tctx, error, "reg_key_del");
115 static bool test_del_recursive(struct torture_context *tctx,
116 const void *test_data)
119 struct hive_key *subkey;
120 struct hive_key *subkey2;
121 const struct hive_key *root = (const struct hive_key *)test_data;
122 TALLOC_CTX *mem_ctx = tctx;
126 /* Create a new key under the root */
127 error = hive_key_add_name(mem_ctx, root, "Parent Key", NULL,
129 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
131 /* Create a new key under "Parent Key" */
132 error = hive_key_add_name(mem_ctx, subkey, "Child Key", NULL,
134 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
136 /* Create a new value under "Child Key" */
137 error = hive_key_set_value(subkey2, "Answer Recursive", REG_DWORD,
138 data_blob_talloc(mem_ctx, data, sizeof(data)));
139 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
141 /* Deleting "Parent Key" will also delete "Child Key" and the value. */
142 error = hive_key_del(root, "Parent Key");
143 torture_assert_werr_ok(tctx, error, "hive_key_del");
148 static bool test_flush_key(struct torture_context *tctx, void *test_data)
150 struct hive_key *root = (struct hive_key *)test_data;
152 torture_assert_werr_ok(tctx, hive_key_flush(root), "flush key");
157 static bool test_del_key(struct torture_context *tctx, const void *test_data)
160 struct hive_key *subkey;
161 const struct hive_key *root = (const struct hive_key *)test_data;
162 TALLOC_CTX *mem_ctx = tctx;
164 error = hive_key_add_name(mem_ctx, root, "Nested Key", NULL,
166 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
168 error = hive_key_del(root, "Nested Key");
169 torture_assert_werr_ok(tctx, error, "reg_key_del");
171 error = hive_key_del(root, "Nested Key");
172 torture_assert_werr_equal(tctx, error, WERR_BADFILE, "reg_key_del");
177 static bool test_set_value(struct torture_context *tctx,
178 const void *test_data)
181 struct hive_key *subkey;
182 const struct hive_key *root = (const struct hive_key *)test_data;
183 TALLOC_CTX *mem_ctx = tctx;
187 error = hive_key_add_name(mem_ctx, root, "YA Nested Key", NULL,
189 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
191 error = hive_key_set_value(subkey, "Answer", REG_DWORD,
192 data_blob_talloc(mem_ctx, data, sizeof(data)));
193 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
198 static bool test_get_value(struct torture_context *tctx, const void *test_data)
201 struct hive_key *subkey;
202 const struct hive_key *root = (const struct hive_key *)test_data;
203 TALLOC_CTX *mem_ctx = tctx;
210 error = hive_key_add_name(mem_ctx, root, "EYA Nested Key", NULL,
212 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
214 error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
215 torture_assert_werr_equal(tctx, error, WERR_BADFILE,
216 "getting missing value");
218 error = hive_key_set_value(subkey, "Answer", REG_DWORD,
219 data_blob_talloc(mem_ctx, data, sizeof(data)));
220 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
222 error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
223 torture_assert_werr_ok(tctx, error, "getting value");
225 torture_assert_int_equal(tctx, value.length, 4, "value length");
226 torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
228 torture_assert_mem_equal(tctx, &data, value.data, sizeof(uint32_t),
234 static bool test_del_value(struct torture_context *tctx, const void *test_data)
237 struct hive_key *subkey;
238 const struct hive_key *root = (const struct hive_key *)test_data;
239 TALLOC_CTX *mem_ctx = tctx;
246 error = hive_key_add_name(mem_ctx, root, "EEYA Nested Key", NULL,
248 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
250 error = hive_key_set_value(subkey, "Answer", REG_DWORD,
251 data_blob_talloc(mem_ctx, data, sizeof(data)));
252 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
254 error = hive_key_del_value(subkey, "Answer");
255 torture_assert_werr_ok(tctx, error, "deleting value");
257 error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
258 torture_assert_werr_equal(tctx, error, WERR_BADFILE, "getting value");
260 error = hive_key_del_value(subkey, "Answer");
261 torture_assert_werr_equal(tctx, error, WERR_BADFILE,
267 static bool test_list_values(struct torture_context *tctx,
268 const void *test_data)
271 struct hive_key *subkey;
272 const struct hive_key *root = (const struct hive_key *)test_data;
273 TALLOC_CTX *mem_ctx = tctx;
279 SIVAL(data, 0, data_val);
281 error = hive_key_add_name(mem_ctx, root, "AYAYA Nested Key", NULL,
283 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
285 error = hive_key_set_value(subkey, "Answer", REG_DWORD,
286 data_blob_talloc(mem_ctx, data, sizeof(data)));
287 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
289 error = hive_get_value_by_index(mem_ctx, subkey, 0, &name,
291 torture_assert_werr_ok(tctx, error, "getting value");
293 torture_assert_str_equal(tctx, name, "Answer", "value name");
295 torture_assert_int_equal(tctx, value.length, 4, "value length");
296 torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
299 torture_assert_int_equal(tctx, data_val, IVAL(value.data, 0), "value data");
301 error = hive_get_value_by_index(mem_ctx, subkey, 1, &name,
303 torture_assert_werr_equal(tctx, error, WERR_NO_MORE_ITEMS,
304 "getting missing value");
309 static void tcase_add_tests(struct torture_tcase *tcase)
311 torture_tcase_add_simple_test_const(tcase, "del_nonexistant_key",
312 test_del_nonexistant_key);
313 torture_tcase_add_simple_test_const(tcase, "add_subkey",
315 torture_tcase_add_simple_test(tcase, "flush_key",
317 /* test_del_recursive() test must run before test_keyinfo_root().
318 test_keyinfo_root() checks the number of subkeys, which verifies
319 the recursive delete worked properly. */
320 torture_tcase_add_simple_test_const(tcase, "del_recursive",
322 torture_tcase_add_simple_test_const(tcase, "get_info",
324 torture_tcase_add_simple_test(tcase, "get_info_nums",
326 torture_tcase_add_simple_test_const(tcase, "set_value",
328 torture_tcase_add_simple_test_const(tcase, "get_value",
330 torture_tcase_add_simple_test_const(tcase, "list_values",
332 torture_tcase_add_simple_test_const(tcase, "del_key",
334 torture_tcase_add_simple_test_const(tcase, "del_value",
338 static bool hive_setup_dir(struct torture_context *tctx, void **data)
340 struct hive_key *key;
345 status = torture_temp_dir(tctx, "hive-dir", &dirname);
346 if (!NT_STATUS_IS_OK(status))
351 error = reg_create_directory(tctx, dirname, &key);
352 if (!W_ERROR_IS_OK(error)) {
353 fprintf(stderr, "Unable to initialize dir hive\n");
362 static bool hive_setup_ldb(struct torture_context *tctx, void **data)
364 struct hive_key *key;
369 status = torture_temp_dir(tctx, "hive-ldb", &dirname);
370 if (!NT_STATUS_IS_OK(status))
375 error = reg_open_ldb_file(tctx, dirname, NULL, NULL, tctx->ev, tctx->lp_ctx, &key);
376 if (!W_ERROR_IS_OK(error)) {
377 fprintf(stderr, "Unable to initialize ldb hive\n");
386 static bool hive_setup_regf(struct torture_context *tctx, void **data)
388 struct hive_key *key;
393 status = torture_temp_dir(tctx, "hive-dir", &dirname);
394 if (!NT_STATUS_IS_OK(status))
399 error = reg_create_regf_file(tctx, lp_iconv_convenience(tctx->lp_ctx),
401 if (!W_ERROR_IS_OK(error)) {
402 fprintf(stderr, "Unable to create new regf file\n");
411 static bool test_dir_refuses_null_location(struct torture_context *tctx)
413 torture_assert_werr_equal(tctx, WERR_INVALID_PARAM,
414 reg_open_directory(NULL, NULL, NULL),
415 "reg_open_directory accepts NULL location");
419 struct torture_suite *torture_registry_hive(TALLOC_CTX *mem_ctx)
421 struct torture_tcase *tcase;
422 struct torture_suite *suite = torture_suite_create(mem_ctx, "HIVE");
424 torture_suite_add_simple_test(suite, "dir-refuses-null-location",
425 test_dir_refuses_null_location);
427 tcase = torture_suite_add_tcase(suite, "dir");
428 torture_tcase_set_fixture(tcase, hive_setup_dir, NULL);
429 tcase_add_tests(tcase);
431 tcase = torture_suite_add_tcase(suite, "ldb");
432 torture_tcase_set_fixture(tcase, hive_setup_ldb, NULL);
433 tcase_add_tests(tcase);
435 tcase = torture_suite_add_tcase(suite, "regf");
436 torture_tcase_set_fixture(tcase, hive_setup_regf, NULL);
437 tcase_add_tests(tcase);