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"
29 #include "libcli/security/security.h"
30 #include "lib/registry/tests/proto.h"
32 static bool test_del_nonexistent_key(struct torture_context
*tctx
,
33 const void *test_data
)
35 const struct hive_key
*root
= (const struct hive_key
*)test_data
;
36 WERROR error
= hive_key_del(tctx
, root
, "bla");
37 torture_assert_werr_equal(tctx
, error
, WERR_BADFILE
,
38 "invalid return code");
43 static bool test_keyinfo_root(struct torture_context
*tctx
,
44 const void *test_data
)
46 uint32_t num_subkeys
, num_values
;
47 const struct hive_key
*root
= (const struct hive_key
*)test_data
;
50 /* This is a new backend. There should be no subkeys and no
52 error
= hive_key_get_info(tctx
, root
, NULL
, &num_subkeys
, &num_values
,
53 NULL
, NULL
, NULL
, NULL
);
54 torture_assert_werr_ok(tctx
, error
, "reg_key_num_subkeys()");
56 torture_assert_int_equal(tctx
, num_subkeys
, 0,
57 "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,
62 "New key has non-zero value count");
67 static bool test_keyinfo_nums(struct torture_context
*tctx
, void *test_data
)
69 uint32_t num_subkeys
, num_values
;
70 struct hive_key
*root
= (struct hive_key
*)test_data
;
72 struct hive_key
*subkey
;
73 uint8_t d
[] = { 0x42, 0x00, 0x00, 0x00 };
74 DATA_BLOB db
= { d
, 4 };
76 error
= hive_key_add_name(tctx
, root
, "Nested Keyll", NULL
,
78 torture_assert_werr_ok(tctx
, error
, "hive_key_add_name");
80 error
= hive_key_set_value(root
, "Answer", REG_DWORD
, db
);
81 torture_assert_werr_ok(tctx
, error
, "hive_key_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
,
86 NULL
, NULL
, NULL
, NULL
);
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
= (const struct hive_key
*)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(mem_ctx
, root
, "Nested Key");
111 torture_assert_werr_ok(tctx
, error
, "reg_key_del");
116 static bool test_del_recursive(struct torture_context
*tctx
,
117 const void *test_data
)
120 struct hive_key
*subkey
;
121 struct hive_key
*subkey2
;
122 const struct hive_key
*root
= (const struct hive_key
*)test_data
;
123 TALLOC_CTX
*mem_ctx
= tctx
;
124 uint8_t d
[] = { 0x42, 0x00, 0x00, 0x00 };
125 DATA_BLOB db
= { d
, 4 };
127 /* Create a new key under the root */
128 error
= hive_key_add_name(mem_ctx
, root
, "Parent Key", NULL
,
130 torture_assert_werr_ok(tctx
, error
, "hive_key_add_name");
132 /* Create a new key under "Parent Key" */
133 error
= hive_key_add_name(mem_ctx
, subkey
, "Child Key", NULL
,
135 torture_assert_werr_ok(tctx
, error
, "hive_key_add_name");
137 /* Create a new value under "Child Key" */
138 error
= hive_key_set_value(subkey2
, "Answer Recursive", REG_DWORD
, db
);
139 torture_assert_werr_ok(tctx
, error
, "hive_key_set_value");
141 /* Deleting "Parent Key" will also delete "Child Key" and the value. */
142 error
= hive_key_del(mem_ctx
, root
, "Parent Key");
143 torture_assert_werr_ok(tctx
, error
, "hive_key_del");
148 static bool test_flush_key(struct torture_context
*tctx
, void *test_data
)
150 struct hive_key
*root
= (struct hive_key
*)test_data
;
152 torture_assert_werr_ok(tctx
, hive_key_flush(root
), "flush key");
157 static bool test_del_key(struct torture_context
*tctx
, const void *test_data
)
160 struct hive_key
*subkey
;
161 const struct hive_key
*root
= (const struct hive_key
*)test_data
;
162 TALLOC_CTX
*mem_ctx
= tctx
;
164 error
= hive_key_add_name(mem_ctx
, root
, "Nested Key", NULL
,
166 torture_assert_werr_ok(tctx
, error
, "hive_key_add_name");
168 error
= hive_key_del(mem_ctx
, root
, "Nested Key");
169 torture_assert_werr_ok(tctx
, error
, "reg_key_del");
171 error
= hive_key_del(mem_ctx
, root
, "Nested Key");
172 torture_assert_werr_equal(tctx
, error
, WERR_BADFILE
, "reg_key_del");
177 static bool test_set_value(struct torture_context
*tctx
,
178 const void *test_data
)
181 struct hive_key
*subkey
;
182 const struct hive_key
*root
= (const struct hive_key
*)test_data
;
183 TALLOC_CTX
*mem_ctx
= tctx
;
184 uint8_t d
[] = { 0x42, 0x00, 0x00, 0x00 };
185 DATA_BLOB db
= { d
, 4 };
187 error
= hive_key_add_name(mem_ctx
, root
, "YA Nested Key", NULL
,
189 torture_assert_werr_ok(tctx
, error
, "hive_key_add_name");
191 error
= hive_key_set_value(subkey
, "Answer", REG_DWORD
, db
);
192 torture_assert_werr_ok(tctx
, error
, "hive_key_set_value");
197 static bool test_get_value(struct torture_context
*tctx
, const void *test_data
)
200 struct hive_key
*subkey
;
201 const struct hive_key
*root
= (const struct hive_key
*)test_data
;
202 TALLOC_CTX
*mem_ctx
= tctx
;
204 uint8_t d
[] = { 0x42, 0x00, 0x00, 0x00 };
205 DATA_BLOB db
= { d
, 4 }, data
;
207 error
= hive_key_add_name(mem_ctx
, root
, "EYA Nested Key", NULL
,
209 torture_assert_werr_ok(tctx
, error
, "hive_key_add_name");
211 error
= hive_get_value(mem_ctx
, subkey
, "Answer", &type
, &data
);
212 torture_assert_werr_equal(tctx
, error
, WERR_BADFILE
,
213 "getting missing value");
215 error
= hive_key_set_value(subkey
, "Answer", REG_DWORD
, db
);
216 torture_assert_werr_ok(tctx
, error
, "hive_key_set_value");
218 error
= hive_get_value(mem_ctx
, subkey
, "Answer", &type
, &data
);
219 torture_assert_werr_ok(tctx
, error
, "getting value");
221 torture_assert_int_equal(tctx
, data
.length
, 4, "value length");
222 torture_assert_int_equal(tctx
, type
, REG_DWORD
, "value type");
224 torture_assert_mem_equal(tctx
, data
.data
, db
.data
, 4, "value data");
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
;
236 uint8_t d
[] = { 0x42, 0x00, 0x00, 0x00 };
237 DATA_BLOB db
= { d
, 4 };
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
, db
);
244 torture_assert_werr_ok(tctx
, error
, "hive_key_set_value");
246 error
= hive_key_del_value(mem_ctx
, subkey
, "Answer");
247 torture_assert_werr_ok(tctx
, error
, "deleting value");
249 error
= hive_get_value(mem_ctx
, subkey
, "Answer", &type
, &db
);
250 torture_assert_werr_equal(tctx
, error
, WERR_BADFILE
, "getting value");
252 error
= hive_key_del_value(mem_ctx
, subkey
, "Answer");
253 torture_assert_werr_equal(tctx
, error
, WERR_BADFILE
,
259 static bool test_list_values(struct torture_context
*tctx
,
260 const void *test_data
)
263 struct hive_key
*subkey
;
264 const struct hive_key
*root
= (const struct hive_key
*)test_data
;
265 TALLOC_CTX
*mem_ctx
= tctx
;
267 uint8_t d
[] = { 0x42, 0x00, 0x00, 0x00 };
268 DATA_BLOB db
= { d
, 4 }, data
;
271 error
= hive_key_add_name(mem_ctx
, root
, "AYAYA Nested Key", NULL
,
273 torture_assert_werr_ok(tctx
, error
, "hive_key_add_name");
275 error
= hive_key_set_value(subkey
, "Answer", REG_DWORD
, db
);
276 torture_assert_werr_ok(tctx
, error
, "hive_key_set_value");
278 error
= hive_get_value_by_index(mem_ctx
, subkey
, 0, &name
,
280 torture_assert_werr_ok(tctx
, error
, "getting value");
282 torture_assert_str_equal(tctx
, name
, "Answer", "value name");
284 torture_assert_int_equal(tctx
, data
.length
, 4, "value length");
285 torture_assert_int_equal(tctx
, type
, REG_DWORD
, "value type");
287 torture_assert_mem_equal(tctx
, data
.data
, db
.data
, 4, "value data");
289 error
= hive_get_value_by_index(mem_ctx
, subkey
, 1, &name
,
291 torture_assert_werr_equal(tctx
, error
, WERR_NO_MORE_ITEMS
,
292 "getting missing value");
297 static bool test_hive_security(struct torture_context
*tctx
, const void *_data
)
299 struct hive_key
*subkey
= NULL
;
300 const struct hive_key
*root
= _data
;
302 struct security_descriptor
*osd
, *nsd
;
304 osd
= security_descriptor_dacl_create(tctx
,
307 SID_NT_AUTHENTICATED_USERS
,
308 SEC_ACE_TYPE_ACCESS_ALLOWED
,
310 SEC_ACE_FLAG_OBJECT_INHERIT
,
314 error
= hive_key_add_name(tctx
, root
, "SecurityKey", NULL
,
316 torture_assert_werr_ok(tctx
, error
, "hive_key_add_name");
318 error
= hive_get_sec_desc(tctx
, subkey
, &nsd
);
319 torture_assert_werr_ok (tctx
, error
, "getting security descriptor");
321 torture_assert(tctx
, security_descriptor_equal(osd
, nsd
),
322 "security descriptor changed!");
324 /* Create a fresh security descriptor */
326 osd
= security_descriptor_dacl_create(tctx
,
329 SID_NT_AUTHENTICATED_USERS
,
330 SEC_ACE_TYPE_ACCESS_ALLOWED
,
332 SEC_ACE_FLAG_OBJECT_INHERIT
,
335 error
= hive_set_sec_desc(subkey
, osd
);
336 torture_assert_werr_ok(tctx
, error
, "setting security descriptor");
338 error
= hive_get_sec_desc(tctx
, subkey
, &nsd
);
339 torture_assert_werr_ok (tctx
, error
, "getting security descriptor");
341 torture_assert(tctx
, security_descriptor_equal(osd
, nsd
),
342 "security descriptor changed!");
347 static void tcase_add_tests(struct torture_tcase
*tcase
)
349 torture_tcase_add_simple_test_const(tcase
, "del_nonexistent_key",
350 test_del_nonexistent_key
);
351 torture_tcase_add_simple_test_const(tcase
, "add_subkey",
353 torture_tcase_add_simple_test(tcase
, "flush_key",
355 /* test_del_recursive() test must run before test_keyinfo_root().
356 test_keyinfo_root() checks the number of subkeys, which verifies
357 the recursive delete worked properly. */
358 torture_tcase_add_simple_test_const(tcase
, "del_recursive",
360 torture_tcase_add_simple_test_const(tcase
, "get_info",
362 torture_tcase_add_simple_test(tcase
, "get_info_nums",
364 torture_tcase_add_simple_test_const(tcase
, "set_value",
366 torture_tcase_add_simple_test_const(tcase
, "get_value",
368 torture_tcase_add_simple_test_const(tcase
, "list_values",
370 torture_tcase_add_simple_test_const(tcase
, "del_key",
372 torture_tcase_add_simple_test_const(tcase
, "del_value",
374 torture_tcase_add_simple_test_const(tcase
, "check hive security",
378 static bool hive_setup_ldb(struct torture_context
*tctx
, void **data
)
380 struct hive_key
*key
;
385 status
= torture_temp_dir(tctx
, "hive-ldb", &dirname
);
386 if (!NT_STATUS_IS_OK(status
))
391 error
= reg_open_ldb_file(tctx
, dirname
, NULL
, NULL
, tctx
->ev
, tctx
->lp_ctx
, &key
);
392 if (!W_ERROR_IS_OK(error
)) {
393 fprintf(stderr
, "Unable to initialize ldb hive\n");
402 static bool hive_setup_regf(struct torture_context
*tctx
, void **data
)
404 struct hive_key
*key
;
409 status
= torture_temp_dir(tctx
, "hive-regf", &dirname
);
410 if (!NT_STATUS_IS_OK(status
))
415 error
= reg_create_regf_file(tctx
, dirname
, 5, &key
);
416 if (!W_ERROR_IS_OK(error
)) {
417 fprintf(stderr
, "Unable to create new regf file\n");
426 struct torture_suite
*torture_registry_hive(TALLOC_CTX
*mem_ctx
)
428 struct torture_tcase
*tcase
;
429 struct torture_suite
*suite
= torture_suite_create(mem_ctx
, "hive");
431 tcase
= torture_suite_add_tcase(suite
, "ldb");
432 torture_tcase_set_fixture(tcase
, hive_setup_ldb
, NULL
);
433 tcase_add_tests(tcase
);
435 tcase
= torture_suite_add_tcase(suite
, "regf");
436 torture_tcase_set_fixture(tcase
, hive_setup_regf
, NULL
);
437 tcase_add_tests(tcase
);