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 "torture/torture.h"
26 #include "librpc/gen_ndr/winreg.h"
27 #include "system/filesys.h"
29 static bool test_del_nonexistant_key(struct torture_context *tctx,
30 const void *test_data)
32 const struct hive_key *root = (const struct hive_key *)test_data;
33 WERROR error = hive_key_del(root, "bla");
34 torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
35 "invalid return code");
40 static bool test_keyinfo_root(struct torture_context *tctx,
41 const void *test_data)
43 uint32_t num_subkeys, num_values;
44 const struct hive_key *root = (const struct hive_key *)test_data;
47 /* This is a new backend. There should be no subkeys and no
49 error = hive_key_get_info(tctx, root, NULL, &num_subkeys, &num_values,
51 torture_assert_werr_ok(tctx, error, "reg_key_num_subkeys()");
53 torture_assert_int_equal(tctx, num_subkeys, 0,
54 "New key has non-zero subkey count");
56 torture_assert_werr_ok(tctx, error, "reg_key_num_values");
58 torture_assert_int_equal(tctx, num_values, 0,
59 "New key has non-zero value count");
64 static bool test_keyinfo_nums(struct torture_context *tctx,
65 const void *test_data)
67 uint32_t num_subkeys, num_values;
68 const struct hive_key *root = (const 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,
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_flush_key(struct torture_context *tctx,
115 const void *test_data)
117 const struct hive_key *root = (const struct hive_key *)test_data;
119 torture_assert_werr_ok(tctx, hive_key_flush(root), "flush key");
124 static bool test_del_key(struct torture_context *tctx, const void *test_data)
127 struct hive_key *subkey;
128 const struct hive_key *root = (const struct hive_key *)test_data;
129 TALLOC_CTX *mem_ctx = tctx;
131 error = hive_key_add_name(mem_ctx, root, "Nested Key", NULL,
133 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
135 error = hive_key_del(root, "Nested Key");
136 torture_assert_werr_ok(tctx, error, "reg_key_del");
138 error = hive_key_del(root, "Nested Key");
139 torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND, "reg_key_del");
144 static bool test_set_value(struct torture_context *tctx,
145 const void *test_data)
148 struct hive_key *subkey;
149 const struct hive_key *root = (const struct hive_key *)test_data;
150 TALLOC_CTX *mem_ctx = tctx;
153 error = hive_key_add_name(mem_ctx, root, "YA Nested Key", NULL,
155 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
157 error = hive_key_set_value(subkey, "Answer", REG_DWORD,
158 data_blob_talloc(mem_ctx, &data, sizeof(data)));
159 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
164 static bool test_get_value(struct torture_context *tctx, const void *test_data)
167 struct hive_key *subkey;
168 const struct hive_key *root = (const struct hive_key *)test_data;
169 TALLOC_CTX *mem_ctx = tctx;
174 error = hive_key_add_name(mem_ctx, root, "EYA Nested Key", NULL,
176 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
178 error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
179 torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
180 "getting missing value");
182 error = hive_key_set_value(subkey, "Answer", REG_DWORD,
183 data_blob_talloc(mem_ctx, &data, sizeof(data)));
184 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
186 error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
187 torture_assert_werr_ok(tctx, error, "getting value");
189 torture_assert_int_equal(tctx, value.length, 4, "value length");
190 torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
191 torture_assert(tctx, memcmp(value.data, &data, 4) == 0, "value data");
196 static bool test_del_value(struct torture_context *tctx, const void *test_data)
199 struct hive_key *subkey;
200 const struct hive_key *root = (const struct hive_key *)test_data;
201 TALLOC_CTX *mem_ctx = tctx;
206 error = hive_key_add_name(mem_ctx, root, "EEYA Nested Key", NULL,
208 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
210 error = hive_key_set_value(subkey, "Answer", REG_DWORD,
211 data_blob_talloc(mem_ctx, &data, sizeof(data)));
212 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
214 error = hive_key_del_value(subkey, "Answer");
215 torture_assert_werr_ok(tctx, error, "deleting value");
217 error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
218 torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND, "getting value");
220 error = hive_key_del_value(subkey, "Answer");
221 torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
227 static bool test_list_values(struct torture_context *tctx,
228 const void *test_data)
231 struct hive_key *subkey;
232 const struct hive_key *root = (const struct hive_key *)test_data;
233 TALLOC_CTX *mem_ctx = tctx;
239 error = hive_key_add_name(mem_ctx, root, "AYAYA 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_get_value_by_index(mem_ctx, subkey, 0, &name,
249 torture_assert_werr_ok(tctx, error, "getting value");
251 torture_assert_str_equal(tctx, name, "Answer", "value name");
253 torture_assert_int_equal(tctx, value.length, 4, "value length");
254 torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
255 torture_assert(tctx, memcmp(value.data, &data, 4) == 0, "value data");
257 error = hive_get_value_by_index(mem_ctx, subkey, 1, &name,
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_const(tcase, "del_nonexistant_key",
268 test_del_nonexistant_key);
269 torture_tcase_add_simple_test_const(tcase, "add_subkey",
271 torture_tcase_add_simple_test_const(tcase, "flush_key",
273 torture_tcase_add_simple_test_const(tcase, "get_info",
275 torture_tcase_add_simple_test_const(tcase, "get_info_nums",
277 torture_tcase_add_simple_test_const(tcase, "set_value",
279 torture_tcase_add_simple_test_const(tcase, "get_value",
281 torture_tcase_add_simple_test_const(tcase, "list_values",
283 torture_tcase_add_simple_test_const(tcase, "del_key",
285 torture_tcase_add_simple_test_const(tcase, "del_value",
289 static bool hive_setup_dir(struct torture_context *tctx, void **data)
291 struct hive_key *key;
296 status = torture_temp_dir(tctx, "hive-dir", &dirname);
297 if (!NT_STATUS_IS_OK(status))
302 error = reg_create_directory(tctx, dirname, &key);
303 if (!W_ERROR_IS_OK(error)) {
304 fprintf(stderr, "Unable to initialize dir hive\n");
313 static bool hive_setup_ldb(struct torture_context *tctx, void **data)
315 struct hive_key *key;
320 status = torture_temp_dir(tctx, "hive-ldb", &dirname);
321 if (!NT_STATUS_IS_OK(status))
326 error = reg_open_ldb_file(tctx, dirname, NULL, NULL, tctx->lp_ctx, &key);
327 if (!W_ERROR_IS_OK(error)) {
328 fprintf(stderr, "Unable to initialize ldb hive\n");
337 static bool hive_setup_regf(struct torture_context *tctx, void **data)
339 struct hive_key *key;
344 status = torture_temp_dir(tctx, "hive-dir", &dirname);
345 if (!NT_STATUS_IS_OK(status))
350 error = reg_create_regf_file(tctx, dirname, 5, &key);
351 if (!W_ERROR_IS_OK(error)) {
352 fprintf(stderr, "Unable to create new regf file\n");
361 static bool test_dir_refuses_null_location(struct torture_context *tctx)
363 torture_assert_werr_equal(tctx, WERR_INVALID_PARAM,
364 reg_open_directory(NULL, NULL, NULL),
365 "reg_open_directory accepts NULL location");
369 struct torture_suite *torture_registry_hive(TALLOC_CTX *mem_ctx)
371 struct torture_tcase *tcase;
372 struct torture_suite *suite = torture_suite_create(mem_ctx, "HIVE");
374 torture_suite_add_simple_test(suite, "dir-refuses-null-location",
375 test_dir_refuses_null_location);
377 tcase = torture_suite_add_tcase(suite, "dir");
378 torture_tcase_set_fixture(tcase, hive_setup_dir, NULL);
379 tcase_add_tests(tcase);
381 tcase = torture_suite_add_tcase(suite, "ldb");
382 torture_tcase_set_fixture(tcase, hive_setup_ldb, NULL);
383 tcase_add_tests(tcase);
385 tcase = torture_suite_add_tcase(suite, "regf");
386 torture_tcase_set_fixture(tcase, hive_setup_regf, NULL);
387 tcase_add_tests(tcase);