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 NTSTATUS torture_temp_dir(struct torture_context *tctx, const char *prefix,
31 const char **tempdir);
33 static bool test_del_nonexistant_key(struct torture_context *tctx,
34 const void *test_data)
36 const struct hive_key *root = test_data;
37 WERROR error = hive_key_del(root, "bla");
38 torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
39 "invalid return code");
44 static bool test_keyinfo_root(struct torture_context *tctx,
45 const void *test_data)
47 uint32_t num_subkeys, num_values;
48 const struct hive_key *root = test_data;
51 /* This is a new backend. There should be no subkeys and no
53 error = hive_key_get_info(tctx, root, NULL, &num_subkeys, &num_values,
55 torture_assert_werr_ok(tctx, error, "reg_key_num_subkeys()");
57 torture_assert_int_equal(tctx, num_subkeys, 0, "New key has non-zero subkey count");
59 torture_assert_werr_ok(tctx, error, "reg_key_num_values");
61 torture_assert_int_equal(tctx, num_values, 0, "New key has non-zero value count");
66 static bool test_keyinfo_nums(struct torture_context *tctx,
67 const void *test_data)
69 uint32_t num_subkeys, num_values;
70 const struct hive_key *root = test_data;
72 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_set_value(root, "Answer", REG_DWORD,
80 data_blob_talloc(tctx, &data, sizeof(data)));
81 torture_assert_werr_ok(tctx, error, "hive_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,
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 = 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_flush_key(struct torture_context *tctx,
117 const void *test_data)
119 const struct hive_key *root = test_data;
121 torture_assert_werr_ok(tctx, hive_key_flush(root), "flush key");
126 static bool test_del_key(struct torture_context *tctx, const void *test_data)
129 struct hive_key *subkey;
130 const struct hive_key *root = test_data;
131 TALLOC_CTX *mem_ctx = tctx;
133 error = hive_key_add_name(mem_ctx, root, "Nested Key", NULL,
135 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
137 error = hive_key_del(root, "Nested Key");
138 torture_assert_werr_ok(tctx, error, "reg_key_del");
140 error = hive_key_del(root, "Nested Key");
141 torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND, "reg_key_del");
146 static bool test_set_value(struct torture_context *tctx,
147 const void *test_data)
150 struct hive_key *subkey;
151 const struct hive_key *root = test_data;
152 TALLOC_CTX *mem_ctx = tctx;
155 error = hive_key_add_name(mem_ctx, root, "YA Nested Key", NULL,
157 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
159 error = hive_set_value(subkey, "Answer", REG_DWORD,
160 data_blob_talloc(mem_ctx, &data, sizeof(data)));
161 torture_assert_werr_ok(tctx, error, "hive_set_value");
166 static bool test_get_value(struct torture_context *tctx, const void *test_data)
169 struct hive_key *subkey;
170 const struct hive_key *root = test_data;
171 TALLOC_CTX *mem_ctx = tctx;
176 error = hive_key_add_name(mem_ctx, root, "EYA Nested Key", NULL,
178 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
180 error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
181 torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
182 "getting missing value");
184 error = hive_set_value(subkey, "Answer", REG_DWORD,
185 data_blob_talloc(mem_ctx, &data, sizeof(data)));
186 torture_assert_werr_ok(tctx, error, "hive_set_value");
188 error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
189 torture_assert_werr_ok(tctx, error, "getting value");
191 torture_assert(tctx, memcmp(value.data, &data, 4) == 0, "value data");
193 torture_assert_int_equal(tctx, value.length, 4, "value length");
194 torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
199 static bool test_del_value(struct torture_context *tctx, const void *test_data)
202 struct hive_key *subkey;
203 const struct hive_key *root = test_data;
204 TALLOC_CTX *mem_ctx = tctx;
209 error = hive_key_add_name(mem_ctx, root, "EEYA Nested Key", NULL,
211 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
213 error = hive_set_value(subkey, "Answer", REG_DWORD,
214 data_blob_talloc(mem_ctx, &data, sizeof(data)));
215 torture_assert_werr_ok(tctx, error, "hive_set_value");
217 error = hive_del_value(subkey, "Answer");
218 torture_assert_werr_ok(tctx, error, "deleting value");
220 error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
221 torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND, "getting value");
223 error = hive_del_value(subkey, "Answer");
224 torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND, "deleting value");
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 = 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, &type, &value);
250 torture_assert_werr_ok(tctx, error, "getting value");
252 torture_assert_str_equal(tctx, name, "Answer", "value name");
253 torture_assert(tctx, memcmp(value.data, &data, 4) == 0, "value data");
255 torture_assert_int_equal(tctx, value.length, 4, "value length");
256 torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
258 error = hive_get_value_by_index(mem_ctx, subkey, 1, &name, &type, &value);
259 torture_assert_werr_equal(tctx, error, WERR_NO_MORE_ITEMS,
260 "getting missing value");
265 static void tcase_add_tests(struct torture_tcase *tcase)
267 torture_tcase_add_simple_test(tcase, "del_nonexistant_key",
268 test_del_nonexistant_key);
269 torture_tcase_add_simple_test(tcase, "add_subkey", test_add_subkey);
270 torture_tcase_add_simple_test(tcase, "flush_key", test_flush_key);
271 torture_tcase_add_simple_test(tcase, "get_info", test_keyinfo_root);
272 torture_tcase_add_simple_test(tcase, "get_info_nums", test_keyinfo_nums);
273 torture_tcase_add_simple_test(tcase, "set_value", test_set_value);
274 torture_tcase_add_simple_test(tcase, "get_value", test_get_value);
275 torture_tcase_add_simple_test(tcase, "list_values", test_list_values);
276 torture_tcase_add_simple_test(tcase, "del_key", test_del_key);
277 torture_tcase_add_simple_test(tcase, "del_value", test_del_value);
280 static bool hive_setup_dir(struct torture_context *tctx, void **data)
282 struct hive_key *key;
287 status = torture_temp_dir(tctx, "hive-dir", &dirname);
288 if (!NT_STATUS_IS_OK(status))
293 error = reg_create_directory(tctx, dirname, &key);
294 if (!W_ERROR_IS_OK(error)) {
295 fprintf(stderr, "Unable to initialize dir hive\n");
304 static bool hive_setup_ldb(struct torture_context *tctx, void **data)
306 struct hive_key *key;
311 status = torture_temp_dir(tctx, "hive-ldb", &dirname);
312 if (!NT_STATUS_IS_OK(status))
317 error = reg_open_ldb_file(tctx, dirname, NULL, NULL, &key);
318 if (!W_ERROR_IS_OK(error)) {
319 fprintf(stderr, "Unable to initialize ldb hive\n");
328 static bool hive_setup_regf(struct torture_context *tctx, void **data)
330 struct hive_key *key;
335 status = torture_temp_dir(tctx, "hive-dir", &dirname);
336 if (!NT_STATUS_IS_OK(status))
341 error = reg_create_regf_file(tctx, dirname, 5, &key);
342 if (!W_ERROR_IS_OK(error)) {
343 fprintf(stderr, "Unable to create new regf file\n");
352 static bool test_dir_refuses_null_location(struct torture_context *tctx)
354 torture_assert_werr_equal(tctx, WERR_INVALID_PARAM,
355 reg_open_directory(NULL, NULL, NULL),
356 "reg_open_directory accepts NULL location");
360 struct torture_suite *torture_registry_hive(TALLOC_CTX *mem_ctx)
362 struct torture_tcase *tcase;
363 struct torture_suite *suite = torture_suite_create(mem_ctx,
366 torture_suite_add_simple_test(suite, "dir-refuses-null-location",
367 test_dir_refuses_null_location);
370 tcase = torture_suite_add_tcase(suite, "dir");
371 torture_tcase_set_fixture(tcase, hive_setup_dir, NULL);
372 tcase_add_tests(tcase);
374 tcase = torture_suite_add_tcase(suite, "ldb");
375 torture_tcase_set_fixture(tcase, hive_setup_ldb, NULL);
376 tcase_add_tests(tcase);
378 tcase = torture_suite_add_tcase(suite, "regf");
379 torture_tcase_set_fixture(tcase, hive_setup_regf, NULL);
380 tcase_add_tests(tcase);