Remove debug stuff.
[Samba/gebeck_regimport.git] / source4 / lib / registry / tests / hive.c
blob474704b5170bb3093c270225b51182334a2c7d86
1 /*
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.
23 #include "includes.h"
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"
29 #include "libcli/security/security.h"
31 static bool test_del_nonexistant_key(struct torture_context *tctx,
32 const void *test_data)
34 const struct hive_key *root = (const struct hive_key *)test_data;
35 WERROR error = hive_key_del(root, "bla");
36 torture_assert_werr_equal(tctx, error, WERR_BADFILE,
37 "invalid return code");
39 return true;
42 static bool test_keyinfo_root(struct torture_context *tctx,
43 const void *test_data)
45 uint32_t num_subkeys, num_values;
46 const struct hive_key *root = (const struct hive_key *)test_data;
47 WERROR error;
49 /* This is a new backend. There should be no subkeys and no
50 * values */
51 error = hive_key_get_info(tctx, root, NULL, &num_subkeys, &num_values,
52 NULL, NULL, NULL, NULL);
53 torture_assert_werr_ok(tctx, error, "reg_key_num_subkeys()");
55 torture_assert_int_equal(tctx, num_subkeys, 0,
56 "New key has non-zero subkey count");
58 torture_assert_werr_ok(tctx, error, "reg_key_num_values");
60 torture_assert_int_equal(tctx, num_values, 0,
61 "New key has non-zero value count");
63 return true;
66 static bool test_keyinfo_nums(struct torture_context *tctx, void *test_data)
68 uint32_t num_subkeys, num_values;
69 struct hive_key *root = (struct hive_key *)test_data;
70 WERROR error;
71 struct hive_key *subkey;
72 uint32_t data = 42;
74 error = hive_key_add_name(tctx, root, "Nested Keyll", NULL,
75 NULL, &subkey);
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
83 * values */
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");
94 return true;
97 static bool test_add_subkey(struct torture_context *tctx,
98 const void *test_data)
100 WERROR error;
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,
106 NULL, &subkey);
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");
112 return true;
115 static bool test_del_recursive(struct torture_context *tctx,
116 const void *test_data)
118 WERROR error;
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;
123 uint32_t data = 42;
125 /* Create a new key under the root */
126 error = hive_key_add_name(mem_ctx, root, "Parent Key", NULL,
127 NULL, &subkey);
128 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
130 /* Create a new key under "Parent Key" */
131 error = hive_key_add_name(mem_ctx, subkey, "Child Key", NULL,
132 NULL, &subkey2);
133 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
135 /* Create a new value under "Child Key" */
136 error = hive_key_set_value(subkey2, "Answer Recursive", REG_DWORD,
137 data_blob_talloc(mem_ctx, &data, sizeof(data)));
138 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
140 /* Deleting "Parent Key" will also delete "Child Key" and the value. */
141 error = hive_key_del(root, "Parent Key");
142 torture_assert_werr_ok(tctx, error, "hive_key_del");
144 return true;
147 static bool test_flush_key(struct torture_context *tctx, void *test_data)
149 struct hive_key *root = (struct hive_key *)test_data;
151 torture_assert_werr_ok(tctx, hive_key_flush(root), "flush key");
153 return true;
156 static bool test_del_key(struct torture_context *tctx, const void *test_data)
158 WERROR error;
159 struct hive_key *subkey;
160 const struct hive_key *root = (const struct hive_key *)test_data;
161 TALLOC_CTX *mem_ctx = tctx;
163 error = hive_key_add_name(mem_ctx, root, "Nested Key", NULL,
164 NULL, &subkey);
165 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
167 error = hive_key_del(root, "Nested Key");
168 torture_assert_werr_ok(tctx, error, "reg_key_del");
170 error = hive_key_del(root, "Nested Key");
171 torture_assert_werr_equal(tctx, error, WERR_BADFILE, "reg_key_del");
173 return true;
176 static bool test_set_value(struct torture_context *tctx,
177 const void *test_data)
179 WERROR error;
180 struct hive_key *subkey;
181 const struct hive_key *root = (const struct hive_key *)test_data;
182 TALLOC_CTX *mem_ctx = tctx;
183 uint32_t data = 42;
185 error = hive_key_add_name(mem_ctx, root, "YA Nested Key", NULL,
186 NULL, &subkey);
187 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
189 error = hive_key_set_value(subkey, "Answer", REG_DWORD,
190 data_blob_talloc(mem_ctx, &data, sizeof(data)));
191 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
193 return true;
196 static bool test_get_value(struct torture_context *tctx, const void *test_data)
198 WERROR error;
199 struct hive_key *subkey;
200 const struct hive_key *root = (const struct hive_key *)test_data;
201 TALLOC_CTX *mem_ctx = tctx;
202 uint32_t data = 42;
203 uint32_t type;
204 DATA_BLOB value;
206 error = hive_key_add_name(mem_ctx, root, "EYA Nested Key", NULL,
207 NULL, &subkey);
208 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
210 error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
211 torture_assert_werr_equal(tctx, error, WERR_BADFILE,
212 "getting missing value");
214 error = hive_key_set_value(subkey, "Answer", REG_DWORD,
215 data_blob_talloc(mem_ctx, &data, sizeof(data)));
216 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
218 error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
219 torture_assert_werr_ok(tctx, error, "getting value");
221 torture_assert_int_equal(tctx, value.length, 4, "value length");
222 torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
224 torture_assert_mem_equal(tctx, &data, value.data, sizeof(uint32_t),
225 "value data");
227 return true;
230 static bool test_del_value(struct torture_context *tctx, const void *test_data)
232 WERROR error;
233 struct hive_key *subkey;
234 const struct hive_key *root = (const struct hive_key *)test_data;
235 TALLOC_CTX *mem_ctx = tctx;
236 uint32_t data = 42;
237 uint32_t type;
238 DATA_BLOB value;
240 error = hive_key_add_name(mem_ctx, root, "EEYA Nested Key", NULL,
241 NULL, &subkey);
242 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
244 error = hive_key_set_value(subkey, "Answer", REG_DWORD,
245 data_blob_talloc(mem_ctx, &data, sizeof(data)));
246 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
248 error = hive_key_del_value(subkey, "Answer");
249 torture_assert_werr_ok(tctx, error, "deleting value");
251 error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
252 torture_assert_werr_equal(tctx, error, WERR_BADFILE, "getting value");
254 error = hive_key_del_value(subkey, "Answer");
255 torture_assert_werr_equal(tctx, error, WERR_BADFILE,
256 "deleting value");
258 return true;
261 static bool test_list_values(struct torture_context *tctx,
262 const void *test_data)
264 WERROR error;
265 struct hive_key *subkey;
266 const struct hive_key *root = (const struct hive_key *)test_data;
267 TALLOC_CTX *mem_ctx = tctx;
268 uint32_t data = 42;
269 uint32_t type;
270 DATA_BLOB value;
271 const char *name;
273 error = hive_key_add_name(mem_ctx, root, "AYAYA Nested Key", NULL,
274 NULL, &subkey);
275 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
277 error = hive_key_set_value(subkey, "Answer", REG_DWORD,
278 data_blob_talloc(mem_ctx, &data, sizeof(data)));
279 torture_assert_werr_ok(tctx, error, "hive_key_set_value");
281 error = hive_get_value_by_index(mem_ctx, subkey, 0, &name,
282 &type, &value);
283 torture_assert_werr_ok(tctx, error, "getting value");
285 torture_assert_str_equal(tctx, name, "Answer", "value name");
287 torture_assert_int_equal(tctx, value.length, 4, "value length");
288 torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
291 torture_assert_int_equal(tctx, data, IVAL(value.data, 0), "value data");
293 error = hive_get_value_by_index(mem_ctx, subkey, 1, &name,
294 &type, &value);
295 torture_assert_werr_equal(tctx, error, WERR_NO_MORE_ITEMS,
296 "getting missing value");
298 return true;
301 static bool test_hive_security(struct torture_context *tctx, const void *_data)
303 struct hive_key *subkey = NULL;
304 const struct hive_key *root = _data;
305 WERROR error;
306 struct security_descriptor *osd, *nsd;
308 osd = security_descriptor_dacl_create(tctx,
310 NULL, NULL,
311 SID_NT_AUTHENTICATED_USERS,
312 SEC_ACE_TYPE_ACCESS_ALLOWED,
313 SEC_GENERIC_ALL,
314 SEC_ACE_FLAG_OBJECT_INHERIT,
315 NULL);
318 error = hive_key_add_name(tctx, root, "SecurityKey", NULL,
319 osd, &subkey);
320 torture_assert_werr_ok(tctx, error, "hive_key_add_name");
322 error = hive_get_sec_desc(tctx, subkey, &nsd);
323 torture_assert_werr_ok (tctx, error, "getting security descriptor");
325 torture_assert(tctx, security_descriptor_equal(osd, nsd),
326 "security descriptor changed!");
328 /* Create a fresh security descriptor */
329 talloc_free(osd);
330 osd = security_descriptor_dacl_create(tctx,
332 NULL, NULL,
333 SID_NT_AUTHENTICATED_USERS,
334 SEC_ACE_TYPE_ACCESS_ALLOWED,
335 SEC_GENERIC_ALL,
336 SEC_ACE_FLAG_OBJECT_INHERIT,
337 NULL);
339 error = hive_set_sec_desc(subkey, osd);
340 torture_assert_werr_ok(tctx, error, "setting security descriptor");
342 error = hive_get_sec_desc(tctx, subkey, &nsd);
343 torture_assert_werr_ok (tctx, error, "getting security descriptor");
345 torture_assert(tctx, security_descriptor_equal(osd, nsd),
346 "security descriptor changed!");
348 return true;
351 static void tcase_add_tests(struct torture_tcase *tcase)
353 torture_tcase_add_simple_test_const(tcase, "del_nonexistant_key",
354 test_del_nonexistant_key);
355 torture_tcase_add_simple_test_const(tcase, "add_subkey",
356 test_add_subkey);
357 torture_tcase_add_simple_test(tcase, "flush_key",
358 test_flush_key);
359 /* test_del_recursive() test must run before test_keyinfo_root().
360 test_keyinfo_root() checks the number of subkeys, which verifies
361 the recursive delete worked properly. */
362 torture_tcase_add_simple_test_const(tcase, "del_recursive",
363 test_del_recursive);
364 torture_tcase_add_simple_test_const(tcase, "get_info",
365 test_keyinfo_root);
366 torture_tcase_add_simple_test(tcase, "get_info_nums",
367 test_keyinfo_nums);
368 torture_tcase_add_simple_test_const(tcase, "set_value",
369 test_set_value);
370 torture_tcase_add_simple_test_const(tcase, "get_value",
371 test_get_value);
372 torture_tcase_add_simple_test_const(tcase, "list_values",
373 test_list_values);
374 torture_tcase_add_simple_test_const(tcase, "del_key",
375 test_del_key);
376 torture_tcase_add_simple_test_const(tcase, "del_value",
377 test_del_value);
378 torture_tcase_add_simple_test_const(tcase, "check hive security",
379 test_hive_security);
382 static bool hive_setup_dir(struct torture_context *tctx, void **data)
384 struct hive_key *key;
385 WERROR error;
386 char *dirname;
387 NTSTATUS status;
389 status = torture_temp_dir(tctx, "hive-dir", &dirname);
390 if (!NT_STATUS_IS_OK(status))
391 return false;
393 rmdir(dirname);
395 error = reg_create_directory(tctx, dirname, &key);
396 if (!W_ERROR_IS_OK(error)) {
397 fprintf(stderr, "Unable to initialize dir hive\n");
398 return false;
401 *data = key;
403 return true;
406 static bool hive_setup_ldb(struct torture_context *tctx, void **data)
408 struct hive_key *key;
409 WERROR error;
410 char *dirname;
411 NTSTATUS status;
413 status = torture_temp_dir(tctx, "hive-ldb", &dirname);
414 if (!NT_STATUS_IS_OK(status))
415 return false;
417 rmdir(dirname);
419 error = reg_open_ldb_file(tctx, dirname, NULL, NULL, tctx->lp_ctx, &key);
420 if (!W_ERROR_IS_OK(error)) {
421 fprintf(stderr, "Unable to initialize ldb hive\n");
422 return false;
425 *data = key;
427 return true;
430 static bool hive_setup_regf(struct torture_context *tctx, void **data)
432 struct hive_key *key;
433 WERROR error;
434 char *dirname;
435 NTSTATUS status;
437 status = torture_temp_dir(tctx, "hive-regf", &dirname);
438 if (!NT_STATUS_IS_OK(status))
439 return false;
441 rmdir(dirname);
443 error = reg_create_regf_file(tctx, lp_iconv_convenience(tctx->lp_ctx),
444 dirname, 5, &key);
445 if (!W_ERROR_IS_OK(error)) {
446 fprintf(stderr, "Unable to create new regf file\n");
447 return false;
450 *data = key;
452 return true;
455 static bool test_dir_refuses_null_location(struct torture_context *tctx)
457 torture_assert_werr_equal(tctx, WERR_INVALID_PARAM,
458 reg_open_directory(NULL, NULL, NULL),
459 "reg_open_directory accepts NULL location");
460 return true;
463 struct torture_suite *torture_registry_hive(TALLOC_CTX *mem_ctx)
465 struct torture_tcase *tcase;
466 struct torture_suite *suite = torture_suite_create(mem_ctx, "HIVE");
468 torture_suite_add_simple_test(suite, "dir-refuses-null-location",
469 test_dir_refuses_null_location);
471 tcase = torture_suite_add_tcase(suite, "dir");
472 torture_tcase_set_fixture(tcase, hive_setup_dir, NULL);
473 tcase_add_tests(tcase);
475 tcase = torture_suite_add_tcase(suite, "ldb");
476 torture_tcase_set_fixture(tcase, hive_setup_ldb, NULL);
477 tcase_add_tests(tcase);
479 tcase = torture_suite_add_tcase(suite, "regf");
480 torture_tcase_set_fixture(tcase, hive_setup_regf, NULL);
481 tcase_add_tests(tcase);
483 return suite;