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;
73 error = hive_key_add_name(tctx, root, "Nested Keyll", NULL,
75 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
77 error = hive_key_set_value(root, "Answer", REG_DWORD,
78 data_blob_talloc(tctx, &data, sizeof(data)));
79 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
81 /* This is a new backend. There should be no subkeys and no
83 error = hive_key_get_info(tctx, root, NULL, &num_subkeys, &num_values,
84 NULL, NULL, NULL, NULL);
85 torture_assert_werr_ok(tctx, error, "reg_key_num_subkeys()");
87 torture_assert_int_equal(tctx, num_subkeys, 1, "subkey count");
89 torture_assert_werr_ok(tctx, error, "reg_key_num_values");
91 torture_assert_int_equal(tctx, num_values, 1, "value count");
96 static bool test_add_subkey(struct torture_context *tctx,
97 const void *test_data)
100 struct hive_key *subkey;
101 const struct hive_key *root = (const struct hive_key *)test_data;
102 TALLOC_CTX *mem_ctx = tctx;
104 error = hive_key_add_name(mem_ctx, root, "Nested Key", NULL,
106 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
108 error = hive_key_del(root, "Nested Key");
109 torture_assert_werr_ok(tctx, error, "reg_key_del");
114 static bool test_del_recursive(struct torture_context *tctx,
115 const void *test_data)
118 struct hive_key *subkey;
119 struct hive_key *subkey2;
120 const struct hive_key *root = (const struct hive_key *)test_data;
121 TALLOC_CTX *mem_ctx = tctx;
124 /* Create a new key under the root */
125 error = hive_key_add_name(mem_ctx, root, "Parent Key", NULL,
127 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
129 /* Create a new key under "Parent Key" */
130 error = hive_key_add_name(mem_ctx, subkey, "Child Key", NULL,
132 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
134 /* Create a new value under "Child Key" */
135 error = hive_key_set_value(subkey2, "Answer Recursive", REG_DWORD,
136 data_blob_talloc(mem_ctx, &data, sizeof(data)));
137 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
139 /* Deleting "Parent Key" will also delete "Child Key" and the value. */
140 error = hive_key_del(root, "Parent Key");
141 torture_assert_werr_ok(tctx, error, "hive_key_del");
146 static bool test_flush_key(struct torture_context *tctx, void *test_data)
148 struct hive_key *root = (struct hive_key *)test_data;
150 torture_assert_werr_ok(tctx, hive_key_flush(root), "flush key");
155 static bool test_del_key(struct torture_context *tctx, const void *test_data)
158 struct hive_key *subkey;
159 const struct hive_key *root = (const struct hive_key *)test_data;
160 TALLOC_CTX *mem_ctx = tctx;
162 error = hive_key_add_name(mem_ctx, root, "Nested Key", NULL,
164 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
166 error = hive_key_del(root, "Nested Key");
167 torture_assert_werr_ok(tctx, error, "reg_key_del");
169 error = hive_key_del(root, "Nested Key");
170 torture_assert_werr_equal(tctx, error, WERR_BADFILE, "reg_key_del");
175 static bool test_set_value(struct torture_context *tctx,
176 const void *test_data)
179 struct hive_key *subkey;
180 const struct hive_key *root = (const struct hive_key *)test_data;
181 TALLOC_CTX *mem_ctx = tctx;
184 error = hive_key_add_name(mem_ctx, root, "YA Nested Key", NULL,
186 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
188 error = hive_key_set_value(subkey, "Answer", REG_DWORD,
189 data_blob_talloc(mem_ctx, &data, sizeof(data)));
190 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
195 static bool test_get_value(struct torture_context *tctx, const void *test_data)
198 struct hive_key *subkey;
199 const struct hive_key *root = (const struct hive_key *)test_data;
200 TALLOC_CTX *mem_ctx = tctx;
205 error = hive_key_add_name(mem_ctx, root, "EYA Nested Key", NULL,
207 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
209 error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
210 torture_assert_werr_equal(tctx, error, WERR_BADFILE,
211 "getting missing value");
213 error = hive_key_set_value(subkey, "Answer", REG_DWORD,
214 data_blob_talloc(mem_ctx, &data, sizeof(data)));
215 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
217 error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
218 torture_assert_werr_ok(tctx, error, "getting value");
220 torture_assert_int_equal(tctx, value.length, 4, "value length");
221 torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
223 torture_assert_mem_equal(tctx, &data, value.data, sizeof(uint32_t),
229 static bool test_del_value(struct torture_context *tctx, const void *test_data)
232 struct hive_key *subkey;
233 const struct hive_key *root = (const struct hive_key *)test_data;
234 TALLOC_CTX *mem_ctx = tctx;
239 error = hive_key_add_name(mem_ctx, root, "EEYA Nested Key", NULL,
241 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
243 error = hive_key_set_value(subkey, "Answer", REG_DWORD,
244 data_blob_talloc(mem_ctx, &data, sizeof(data)));
245 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
247 error = hive_key_del_value(subkey, "Answer");
248 torture_assert_werr_ok(tctx, error, "deleting value");
250 error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
251 torture_assert_werr_equal(tctx, error, WERR_BADFILE, "getting value");
253 error = hive_key_del_value(subkey, "Answer");
254 torture_assert_werr_equal(tctx, error, WERR_BADFILE,
260 static bool test_list_values(struct torture_context *tctx,
261 const void *test_data)
264 struct hive_key *subkey;
265 const struct hive_key *root = (const struct hive_key *)test_data;
266 TALLOC_CTX *mem_ctx = tctx;
272 error = hive_key_add_name(mem_ctx, root, "AYAYA Nested Key", NULL,
274 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
276 error = hive_key_set_value(subkey, "Answer", REG_DWORD,
277 data_blob_talloc(mem_ctx, &data, sizeof(data)));
278 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
280 error = hive_get_value_by_index(mem_ctx, subkey, 0, &name,
282 torture_assert_werr_ok(tctx, error, "getting value");
284 torture_assert_str_equal(tctx, name, "Answer", "value name");
286 torture_assert_int_equal(tctx, value.length, 4, "value length");
287 torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
290 torture_assert_int_equal(tctx, data, IVAL(value.data, 0), "value data");
292 error = hive_get_value_by_index(mem_ctx, subkey, 1, &name,
294 torture_assert_werr_equal(tctx, error, WERR_NO_MORE_ITEMS,
295 "getting missing value");
300 static void tcase_add_tests(struct torture_tcase *tcase)
302 torture_tcase_add_simple_test_const(tcase, "del_nonexistant_key",
303 test_del_nonexistant_key);
304 torture_tcase_add_simple_test_const(tcase, "add_subkey",
306 torture_tcase_add_simple_test(tcase, "flush_key",
308 /* test_del_recursive() test must run before test_keyinfo_root().
309 test_keyinfo_root() checks the number of subkeys, which verifies
310 the recursive delete worked properly. */
311 torture_tcase_add_simple_test_const(tcase, "del_recursive",
313 torture_tcase_add_simple_test_const(tcase, "get_info",
315 torture_tcase_add_simple_test(tcase, "get_info_nums",
317 torture_tcase_add_simple_test_const(tcase, "set_value",
319 torture_tcase_add_simple_test_const(tcase, "get_value",
321 torture_tcase_add_simple_test_const(tcase, "list_values",
323 torture_tcase_add_simple_test_const(tcase, "del_key",
325 torture_tcase_add_simple_test_const(tcase, "del_value",
329 static bool hive_setup_dir(struct torture_context *tctx, void **data)
331 struct hive_key *key;
336 status = torture_temp_dir(tctx, "hive-dir", &dirname);
337 if (!NT_STATUS_IS_OK(status))
342 error = reg_create_directory(tctx, dirname, &key);
343 if (!W_ERROR_IS_OK(error)) {
344 fprintf(stderr, "Unable to initialize dir hive\n");
353 static bool hive_setup_ldb(struct torture_context *tctx, void **data)
355 struct hive_key *key;
360 status = torture_temp_dir(tctx, "hive-ldb", &dirname);
361 if (!NT_STATUS_IS_OK(status))
366 error = reg_open_ldb_file(tctx, dirname, NULL, NULL, tctx->lp_ctx, &key);
367 if (!W_ERROR_IS_OK(error)) {
368 fprintf(stderr, "Unable to initialize ldb hive\n");
377 static bool hive_setup_regf(struct torture_context *tctx, void **data)
379 struct hive_key *key;
384 status = torture_temp_dir(tctx, "hive-dir", &dirname);
385 if (!NT_STATUS_IS_OK(status))
390 error = reg_create_regf_file(tctx, lp_iconv_convenience(tctx->lp_ctx),
392 if (!W_ERROR_IS_OK(error)) {
393 fprintf(stderr, "Unable to create new regf file\n");
402 static bool test_dir_refuses_null_location(struct torture_context *tctx)
404 torture_assert_werr_equal(tctx, WERR_INVALID_PARAM,
405 reg_open_directory(NULL, NULL, NULL),
406 "reg_open_directory accepts NULL location");
410 struct torture_suite *torture_registry_hive(TALLOC_CTX *mem_ctx)
412 struct torture_tcase *tcase;
413 struct torture_suite *suite = torture_suite_create(mem_ctx, "HIVE");
415 torture_suite_add_simple_test(suite, "dir-refuses-null-location",
416 test_dir_refuses_null_location);
418 tcase = torture_suite_add_tcase(suite, "dir");
419 torture_tcase_set_fixture(tcase, hive_setup_dir, NULL);
420 tcase_add_tests(tcase);
422 tcase = torture_suite_add_tcase(suite, "ldb");
423 torture_tcase_set_fixture(tcase, hive_setup_ldb, NULL);
424 tcase_add_tests(tcase);
426 tcase = torture_suite_add_tcase(suite, "regf");
427 torture_tcase_set_fixture(tcase, hive_setup_regf, NULL);
428 tcase_add_tests(tcase);