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 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 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_NOT_FOUND,
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,
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,
66 const void *test_data)
68 uint32_t num_subkeys, num_values;
69 const struct hive_key *root = (const struct hive_key *)test_data;
71 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_set_value(root, "Answer", REG_DWORD,
79 data_blob_talloc(tctx, &data, sizeof(data)));
80 torture_assert_werr_ok(tctx, error, "hive_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,
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_flush_key(struct torture_context *tctx,
116 const void *test_data)
118 const struct hive_key *root = (const struct hive_key *)test_data;
120 torture_assert_werr_ok(tctx, hive_key_flush(root), "flush key");
125 static bool test_del_key(struct torture_context *tctx, const void *test_data)
128 struct hive_key *subkey;
129 const struct hive_key *root = (const struct hive_key *)test_data;
130 TALLOC_CTX *mem_ctx = tctx;
132 error = hive_key_add_name(mem_ctx, root, "Nested Key", NULL,
134 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
136 error = hive_key_del(root, "Nested Key");
137 torture_assert_werr_ok(tctx, error, "reg_key_del");
139 error = hive_key_del(root, "Nested Key");
140 torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND, "reg_key_del");
145 static bool test_set_value(struct torture_context *tctx,
146 const void *test_data)
149 struct hive_key *subkey;
150 const struct hive_key *root = (const struct hive_key *)test_data;
151 TALLOC_CTX *mem_ctx = tctx;
154 error = hive_key_add_name(mem_ctx, root, "YA Nested Key", NULL,
156 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
158 error = hive_set_value(subkey, "Answer", REG_DWORD,
159 data_blob_talloc(mem_ctx, &data, sizeof(data)));
160 torture_assert_werr_ok(tctx, error, "hive_set_value");
165 static bool test_get_value(struct torture_context *tctx, const void *test_data)
168 struct hive_key *subkey;
169 const struct hive_key *root = (const struct hive_key *)test_data;
170 TALLOC_CTX *mem_ctx = tctx;
175 error = hive_key_add_name(mem_ctx, root, "EYA Nested Key", NULL,
177 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
179 error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
180 torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
181 "getting missing value");
183 error = hive_set_value(subkey, "Answer", REG_DWORD,
184 data_blob_talloc(mem_ctx, &data, sizeof(data)));
185 torture_assert_werr_ok(tctx, error, "hive_set_value");
187 error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
188 torture_assert_werr_ok(tctx, error, "getting value");
190 torture_assert(tctx, memcmp(value.data, &data, 4) == 0, "value data");
192 torture_assert_int_equal(tctx, value.length, 4, "value length");
193 torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
198 static bool test_del_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;
208 error = hive_key_add_name(mem_ctx, root, "EEYA Nested Key", NULL,
210 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
212 error = hive_set_value(subkey, "Answer", REG_DWORD,
213 data_blob_talloc(mem_ctx, &data, sizeof(data)));
214 torture_assert_werr_ok(tctx, error, "hive_set_value");
216 error = hive_del_value(subkey, "Answer");
217 torture_assert_werr_ok(tctx, error, "deleting value");
219 error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
220 torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND, "getting value");
222 error = hive_del_value(subkey, "Answer");
223 torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
229 static bool test_list_values(struct torture_context *tctx,
230 const void *test_data)
233 struct hive_key *subkey;
234 const struct hive_key *root = (const struct hive_key *)test_data;
235 TALLOC_CTX *mem_ctx = tctx;
241 error = hive_key_add_name(mem_ctx, root, "AYAYA Nested Key", NULL,
243 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
245 error = hive_set_value(subkey, "Answer", REG_DWORD,
246 data_blob_talloc(mem_ctx, &data, sizeof(data)));
247 torture_assert_werr_ok(tctx, error, "hive_set_value");
249 error = hive_get_value_by_index(mem_ctx, subkey, 0, &name,
251 torture_assert_werr_ok(tctx, error, "getting value");
253 torture_assert_str_equal(tctx, name, "Answer", "value name");
254 torture_assert(tctx, memcmp(value.data, &data, 4) == 0, "value data");
256 torture_assert_int_equal(tctx, value.length, 4, "value length");
257 torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
259 error = hive_get_value_by_index(mem_ctx, subkey, 1, &name,
261 torture_assert_werr_equal(tctx, error, WERR_NO_MORE_ITEMS,
262 "getting missing value");
267 static void tcase_add_tests(struct torture_tcase *tcase)
269 torture_tcase_add_simple_test(tcase, "del_nonexistant_key",
270 test_del_nonexistant_key);
271 torture_tcase_add_simple_test(tcase, "add_subkey",
273 torture_tcase_add_simple_test(tcase, "flush_key",
275 torture_tcase_add_simple_test(tcase, "get_info",
277 torture_tcase_add_simple_test(tcase, "get_info_nums",
279 torture_tcase_add_simple_test(tcase, "set_value",
281 torture_tcase_add_simple_test(tcase, "get_value",
283 torture_tcase_add_simple_test(tcase, "list_values",
285 torture_tcase_add_simple_test(tcase, "del_key",
287 torture_tcase_add_simple_test(tcase, "del_value",
291 static bool hive_setup_dir(struct torture_context *tctx, void **data)
293 struct hive_key *key;
298 status = torture_temp_dir(tctx, "hive-dir", &dirname);
299 if (!NT_STATUS_IS_OK(status))
304 error = reg_create_directory(tctx, dirname, &key);
305 if (!W_ERROR_IS_OK(error)) {
306 fprintf(stderr, "Unable to initialize dir hive\n");
315 static bool hive_setup_ldb(struct torture_context *tctx, void **data)
317 struct hive_key *key;
322 status = torture_temp_dir(tctx, "hive-ldb", &dirname);
323 if (!NT_STATUS_IS_OK(status))
328 error = reg_open_ldb_file(tctx, dirname, NULL, NULL, &key);
329 if (!W_ERROR_IS_OK(error)) {
330 fprintf(stderr, "Unable to initialize ldb hive\n");
339 static bool hive_setup_regf(struct torture_context *tctx, void **data)
341 struct hive_key *key;
346 status = torture_temp_dir(tctx, "hive-dir", &dirname);
347 if (!NT_STATUS_IS_OK(status))
352 error = reg_create_regf_file(tctx, dirname, 5, &key);
353 if (!W_ERROR_IS_OK(error)) {
354 fprintf(stderr, "Unable to create new regf file\n");
363 static bool test_dir_refuses_null_location(struct torture_context *tctx)
365 torture_assert_werr_equal(tctx, WERR_INVALID_PARAM,
366 reg_open_directory(NULL, NULL, NULL),
367 "reg_open_directory accepts NULL location");
371 struct torture_suite *torture_registry_hive(TALLOC_CTX *mem_ctx)
373 struct torture_tcase *tcase;
374 struct torture_suite *suite = torture_suite_create(mem_ctx, "HIVE");
376 torture_suite_add_simple_test(suite, "dir-refuses-null-location",
377 test_dir_refuses_null_location);
379 tcase = torture_suite_add_tcase(suite, "dir");
380 torture_tcase_set_fixture(tcase, hive_setup_dir, NULL);
381 tcase_add_tests(tcase);
383 tcase = torture_suite_add_tcase(suite, "ldb");
384 torture_tcase_set_fixture(tcase, hive_setup_ldb, NULL);
385 tcase_add_tests(tcase);
387 tcase = torture_suite_add_tcase(suite, "regf");
388 torture_tcase_set_fixture(tcase, hive_setup_regf, NULL);
389 tcase_add_tests(tcase);