summaryrefslogtreecommitdiff
path: root/source4/lib/registry/tests/hive.c
blob: 2bc2e57b687f4b38bc72349c6e5030f14c567d12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
   Unix SMB/CIFS implementation.

   local testing of registry library - hives

   Copyright (C) Jelmer Vernooij 2005-2007

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "lib/registry/registry.h"
#include "torture/torture.h"
#include "librpc/gen_ndr/winreg.h"
#include "system/filesys.h"
#include "param/param.h"
#include "libcli/security/security.h"
#include "lib/registry/tests/proto.h"

static bool test_del_nonexistent_key(struct torture_context *tctx,
				     const void *test_data)
{
	const struct hive_key *root = (const struct hive_key *)test_data;
	WERROR error = hive_key_del(tctx, root, "bla");
	torture_assert_werr_equal(tctx, error, WERR_BADFILE,
				  "invalid return code");

	return true;
}

static bool test_keyinfo_root(struct torture_context *tctx,
			      const void *test_data)
{
	uint32_t num_subkeys, num_values;
	const struct hive_key *root = (const struct hive_key *)test_data;
	WERROR error;

	/* This is a new backend. There should be no subkeys and no
	 * values */
	error = hive_key_get_info(tctx, root, NULL, &num_subkeys, &num_values,
				  NULL, NULL, NULL, NULL);
	torture_assert_werr_ok(tctx, error, "reg_key_num_subkeys()");

	torture_assert_int_equal(tctx, num_subkeys, 0,
				 "New key has non-zero subkey count");

	torture_assert_werr_ok(tctx, error, "reg_key_num_values");

	torture_assert_int_equal(tctx, num_values, 0,
				 "New key has non-zero value count");

	return true;
}

static bool test_keyinfo_nums(struct torture_context *tctx, void *test_data)
{
	uint32_t num_subkeys, num_values;
	struct hive_key *root = (struct hive_key *)test_data;
	WERROR error;
	struct hive_key *subkey;
	uint8_t d[] = { 0x42, 0x00, 0x00, 0x00 };
	DATA_BLOB db = { d, 4 };

	error = hive_key_add_name(tctx, root, "Nested Keyll", NULL,
				  NULL, &subkey);
	torture_assert_werr_ok(tctx, error, "hive_key_add_name");

	error = hive_key_set_value(root, "Answer", REG_DWORD, db);
	torture_assert_werr_ok(tctx, error, "hive_key_set_value");

	/* This is a new backend. There should be no subkeys and no
	 * values */
	error = hive_key_get_info(tctx, root, NULL, &num_subkeys, &num_values,
				  NULL, NULL, NULL, NULL);
	torture_assert_werr_ok(tctx, error, "reg_key_num_subkeys()");

	torture_assert_int_equal(tctx, num_subkeys, 1, "subkey count");

	torture_assert_werr_ok(tctx, error, "reg_key_num_values");

	torture_assert_int_equal(tctx, num_values, 1, "value count");

	return true;
}

static bool test_add_subkey(struct torture_context *tctx,
			    const void *test_data)
{
	WERROR error;
	struct hive_key *subkey;
	const struct hive_key *root = (const struct hive_key *)test_data;
	TALLOC_CTX *mem_ctx = tctx;

	error = hive_key_add_name(mem_ctx, root, "Nested Key", NULL,
				  NULL, &subkey);
	torture_assert_werr_ok(tctx, error, "hive_key_add_name");

	error = hive_key_del(mem_ctx, root, "Nested Key");
	torture_assert_werr_ok(tctx, error, "reg_key_del");

	return true;
}

static bool test_del_recursive(struct torture_context *tctx,
			       const void *test_data)
{
	WERROR error;
	struct hive_key *subkey;
	struct hive_key *subkey2;
	const struct hive_key *root = (const struct hive_key *)test_data;
	TALLOC_CTX *mem_ctx = tctx;
	uint8_t d[] = { 0x42, 0x00, 0x00, 0x00 };
	DATA_BLOB db = { d, 4 };

	/* Create a new key under the root */
	error = hive_key_add_name(mem_ctx, root, "Parent Key", NULL,
				  NULL, &subkey);
	torture_assert_werr_ok(tctx, error, "hive_key_add_name");

	/* Create a new key under "Parent Key" */
	error = hive_key_add_name(mem_ctx, subkey, "Child Key", NULL,
				  NULL, &subkey2);
	torture_assert_werr_ok(tctx, error, "hive_key_add_name");

	/* Create a new value under "Child Key" */
	error = hive_key_set_value(subkey2, "Answer Recursive", REG_DWORD, db);
	torture_assert_werr_ok(tctx, error, "hive_key_set_value");

	/* Deleting "Parent Key" will also delete "Child Key" and the value. */
	error = hive_key_del(mem_ctx, root, "Parent Key");
	torture_assert_werr_ok(tctx, error, "hive_key_del");

	return true;
}

static bool test_flush_key(struct torture_context *tctx, void *test_data)
{
	struct hive_key *root = (struct hive_key *)test_data;

	torture_assert_werr_ok(tctx, hive_key_flush(root), "flush key");

	return true;
}

static bool test_del_key(struct torture_context *tctx, const void *test_data)
{
	WERROR error;
	struct hive_key *subkey;
	const struct hive_key *root = (const struct hive_key *)test_data;
	TALLOC_CTX *mem_ctx = tctx;

	error = hive_key_add_name(mem_ctx, root, "Nested Key", NULL,
				  NULL, &subkey);
	torture_assert_werr_ok(tctx, error, "hive_key_add_name");

	error = hive_key_del(mem_ctx, root, "Nested Key");
	torture_assert_werr_ok(tctx, error, "reg_key_del");

	error = hive_key_del(mem_ctx, root, "Nested Key");
	torture_assert_werr_equal(tctx, error, WERR_BADFILE, "reg_key_del");

	return true;
}

static bool test_set_value(struct torture_context *tctx,
			   const void *test_data)
{
	WERROR error;
	struct hive_key *subkey;
	const struct hive_key *root = (const struct hive_key *)test_data;
	TALLOC_CTX *mem_ctx = tctx;
	uint8_t d[] = { 0x42, 0x00, 0x00, 0x00 };
	DATA_BLOB db = { d, 4 };

	error = hive_key_add_name(mem_ctx, root, "YA Nested Key", NULL,
				  NULL, &subkey);
	torture_assert_werr_ok(tctx, error, "hive_key_add_name");

	error = hive_key_set_value(subkey, "Answer", REG_DWORD, db);
	torture_assert_werr_ok(tctx, error, "hive_key_set_value");

	return true;
}

static bool test_get_value(struct torture_context *tctx, const void *test_data)
{
	WERROR error;
	struct hive_key *subkey;
	const struct hive_key *root = (const struct hive_key *)test_data;
	TALLOC_CTX *mem_ctx = tctx;
	uint32_t type;
	uint8_t d[] = { 0x42, 0x00, 0x00, 0x00 };
	DATA_BLOB db = { d, 4 }, data;

	error = hive_key_add_name(mem_ctx, root, "EYA Nested Key", NULL,
				  NULL, &subkey);
	torture_assert_werr_ok(tctx, error, "hive_key_add_name");

	error = hive_get_value(mem_ctx, subkey, "Answer", &type, &data);
	torture_assert_werr_equal(tctx, error, WERR_BADFILE,
				  "getting missing value");

	error = hive_key_set_value(subkey, "Answer", REG_DWORD, db);
	torture_assert_werr_ok(tctx, error, "hive_key_set_value");

	error = hive_get_value(mem_ctx, subkey, "Answer", &type, &data);
	torture_assert_werr_ok(tctx, error, "getting value");

	torture_assert_int_equal(tctx, data.length, 4, "value length");
	torture_assert_int_equal(tctx, type, REG_DWORD, "value type");

	torture_assert_mem_equal(tctx, data.data, db.data, 4, "value data");

	return true;
}

static bool test_del_value(struct torture_context *tctx, const void *test_data)
{
	WERROR error;
	struct hive_key *subkey;
	const struct hive_key *root = (const struct hive_key *)test_data;
	TALLOC_CTX *mem_ctx = tctx;
	uint32_t type;
	uint8_t d[] = { 0x42, 0x00, 0x00, 0x00 };
	DATA_BLOB db = { d, 4 };

	error = hive_key_add_name(mem_ctx, root, "EEYA Nested Key", NULL,
							 NULL, &subkey);
	torture_assert_werr_ok(tctx, error, "hive_key_add_name");

	error = hive_key_set_value(subkey, "Answer", REG_DWORD, db);
	torture_assert_werr_ok(tctx, error, "hive_key_set_value");

	error = hive_key_del_value(mem_ctx, subkey, "Answer");
	torture_assert_werr_ok(tctx, error, "deleting value");

	error = hive_get_value(mem_ctx, subkey, "Answer", &type, &db);
	torture_assert_werr_equal(tctx, error, WERR_BADFILE, "getting value");

	error = hive_key_del_value(mem_ctx, subkey, "Answer");
	torture_assert_werr_equal(tctx, error, WERR_BADFILE,
				  "deleting value");

	return true;
}

static bool test_list_values(struct torture_context *tctx,
			     const void *test_data)
{
	WERROR error;
	struct hive_key *subkey;
	const struct hive_key *root = (const struct hive_key *)test_data;
	TALLOC_CTX *mem_ctx = tctx;
	uint32_t type;
	uint8_t d[] = { 0x42, 0x00, 0x00, 0x00 };
	DATA_BLOB db = { d, 4 }, data;
	const char *name;

	error = hive_key_add_name(mem_ctx, root, "AYAYA Nested Key", NULL,
				  NULL, &subkey);
	torture_assert_werr_ok(tctx, error, "hive_key_add_name");

	error = hive_key_set_value(subkey, "Answer", REG_DWORD, db);
	torture_assert_werr_ok(tctx, error, "hive_key_set_value");

	error = hive_get_value_by_index(mem_ctx, subkey, 0, &name,
					&type, &data);
	torture_assert_werr_ok(tctx, error, "getting value");

	torture_assert_str_equal(tctx, name, "Answer", "value name");

	torture_assert_int_equal(tctx, data.length, 4, "value length");
	torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
	
	torture_assert_mem_equal(tctx, data.data, db.data, 4, "value data");
	
	error = hive_get_value_by_index(mem_ctx, subkey, 1, &name,
					&type, &data);
	torture_assert_werr_equal(tctx, error, WERR_NO_MORE_ITEMS,
				  "getting missing value");

	return true;
}

static bool test_hive_security(struct torture_context *tctx, const void *_data)
{
	struct hive_key *subkey = NULL;
        const struct hive_key *root = _data;
	WERROR error;
	struct security_descriptor *osd, *nsd;
	
	osd = security_descriptor_dacl_create(tctx,
					 0,
					 NULL, NULL,
					 SID_NT_AUTHENTICATED_USERS,
					 SEC_ACE_TYPE_ACCESS_ALLOWED,
					 SEC_GENERIC_ALL,
					 SEC_ACE_FLAG_OBJECT_INHERIT,
					 NULL);


	error = hive_key_add_name(tctx, root, "SecurityKey", NULL,
				  osd, &subkey);
	torture_assert_werr_ok(tctx, error, "hive_key_add_name");

	error = hive_get_sec_desc(tctx, subkey, &nsd);
	torture_assert_werr_ok (tctx, error, "getting security descriptor");

	torture_assert(tctx, security_descriptor_equal(osd, nsd),
		       "security descriptor changed!");

	/* Create a fresh security descriptor */	
	talloc_free(osd);
	osd = security_descriptor_dacl_create(tctx,
					 0,
					 NULL, NULL,
					 SID_NT_AUTHENTICATED_USERS,
					 SEC_ACE_TYPE_ACCESS_ALLOWED,
					 SEC_GENERIC_ALL,
					 SEC_ACE_FLAG_OBJECT_INHERIT,
					 NULL);

	error = hive_set_sec_desc(subkey, osd);
	torture_assert_werr_ok(tctx, error, "setting security descriptor");
	
	error = hive_get_sec_desc(tctx, subkey, &nsd);
	torture_assert_werr_ok (tctx, error, "getting security descriptor");
	
	torture_assert(tctx, security_descriptor_equal(osd, nsd),
		       "security descriptor changed!");

	return true;
}

static void tcase_add_tests(struct torture_tcase *tcase)
{
	torture_tcase_add_simple_test_const(tcase, "del_nonexistent_key",
						test_del_nonexistent_key);
	torture_tcase_add_simple_test_const(tcase, "add_subkey",
						test_add_subkey);
	torture_tcase_add_simple_test(tcase, "flush_key",
						test_flush_key);
	/* test_del_recursive() test must run before test_keyinfo_root().
	   test_keyinfo_root() checks the number of subkeys, which verifies
	   the recursive delete worked properly. */
	torture_tcase_add_simple_test_const(tcase, "del_recursive",
						test_del_recursive);
	torture_tcase_add_simple_test_const(tcase, "get_info",
						test_keyinfo_root);
	torture_tcase_add_simple_test(tcase, "get_info_nums",
						test_keyinfo_nums);
	torture_tcase_add_simple_test_const(tcase, "set_value",
						test_set_value);
	torture_tcase_add_simple_test_const(tcase, "get_value",
						test_get_value);
	torture_tcase_add_simple_test_const(tcase, "list_values",
						test_list_values);
	torture_tcase_add_simple_test_const(tcase, "del_key",
						test_del_key);
	torture_tcase_add_simple_test_const(tcase, "del_value",
						test_del_value);
	torture_tcase_add_simple_test_const(tcase, "check hive security",
						test_hive_security);
}

static bool hive_setup_ldb(struct torture_context *tctx, void **data)
{
	struct hive_key *key;
	WERROR error;
	char *dirname;
	NTSTATUS status;

	status = torture_temp_dir(tctx, "hive-ldb", &dirname);
	if (!NT_STATUS_IS_OK(status))
		return false;

	rmdir(dirname);

	error = reg_open_ldb_file(tctx, dirname, NULL, NULL, tctx->ev, tctx->lp_ctx, &key);
	if (!W_ERROR_IS_OK(error)) {
		fprintf(stderr, "Unable to initialize ldb hive\n");
		return false;
	}

	*data = key;

	return true;
}

static bool hive_setup_regf(struct torture_context *tctx, void **data)
{
	struct hive_key *key;
	WERROR error;
	char *dirname;
	NTSTATUS status;

	status = torture_temp_dir(tctx, "hive-regf", &dirname);
	if (!NT_STATUS_IS_OK(status))
		return false;

	rmdir(dirname);

	error = reg_create_regf_file(tctx, dirname, 5, &key);
	if (!W_ERROR_IS_OK(error)) {
		fprintf(stderr, "Unable to create new regf file\n");
		return false;
	}

	*data = key;

	return true;
}

struct torture_suite *torture_registry_hive(TALLOC_CTX *mem_ctx)
{
	struct torture_tcase *tcase;
	struct torture_suite *suite = torture_suite_create(mem_ctx, "hive");

	tcase = torture_suite_add_tcase(suite, "ldb");
	torture_tcase_set_fixture(tcase, hive_setup_ldb, NULL);
	tcase_add_tests(tcase);

	tcase = torture_suite_add_tcase(suite, "regf");
	torture_tcase_set_fixture(tcase, hive_setup_regf, NULL);
	tcase_add_tests(tcase);

	return suite;
}