summaryrefslogtreecommitdiff
path: root/source4/lib/registry/tests/hive.c
blob: fdb72823952af3e34e4ef7e1a8da19cf9c819277 (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
/*
   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 2 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"

static bool test_del_nonexistant_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(root, "bla");
	torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
				  "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);
	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,
			      const void *test_data)
{
	uint32_t num_subkeys, num_values;
	const struct hive_key *root = (const struct hive_key *)test_data;
	WERROR error;
	struct hive_key *subkey;
	uint32_t data = 42;

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

	error = hive_set_value(root, "Answer", REG_DWORD,
			       data_blob_talloc(tctx, &data, sizeof(data)));
	torture_assert_werr_ok(tctx, error, "hive_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);
	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(root, "Nested Key");
	torture_assert_werr_ok(tctx, error, "reg_key_del");

	return true;
}

static bool test_flush_key(struct torture_context *tctx,
			   const void *test_data)
{
	const struct hive_key *root = (const 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(root, "Nested Key");
	torture_assert_werr_ok(tctx, error, "reg_key_del");

	error = hive_key_del(root, "Nested Key");
	torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND, "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;
	uint32_t data = 42;

	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_set_value(subkey, "Answer", REG_DWORD,
			       data_blob_talloc(mem_ctx, &data, sizeof(data)));
	torture_assert_werr_ok(tctx, error, "hive_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 data = 42;
	uint32_t type;
	DATA_BLOB value;

	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, &value);
	torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
				  "getting missing value");

	error = hive_set_value(subkey, "Answer", REG_DWORD,
			       data_blob_talloc(mem_ctx, &data, sizeof(data)));
	torture_assert_werr_ok(tctx, error, "hive_set_value");

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

	torture_assert_int_equal(tctx, value.length, 4, "value length");
	torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
	torture_assert(tctx, memcmp(value.data, &data, 4) == 0, "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 data = 42;
	uint32_t type;
	DATA_BLOB value;

	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_set_value(subkey, "Answer", REG_DWORD,
			       data_blob_talloc(mem_ctx, &data, sizeof(data)));
	torture_assert_werr_ok(tctx, error, "hive_set_value");

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

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

	error = hive_del_value(subkey, "Answer");
	torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
				  "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 data = 42;
	uint32_t type;
	DATA_BLOB value;
	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_set_value(subkey, "Answer", REG_DWORD,
			       data_blob_talloc(mem_ctx, &data, sizeof(data)));
	torture_assert_werr_ok(tctx, error, "hive_set_value");

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

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

	torture_assert_int_equal(tctx, value.length, 4, "value length");
	torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
	torture_assert(tctx, memcmp(value.data, &data, 4) == 0, "value data");

	error = hive_get_value_by_index(mem_ctx, subkey, 1, &name,
					&type, &value);
	torture_assert_werr_equal(tctx, error, WERR_NO_MORE_ITEMS,
				  "getting missing value");

	return true;
}

static void tcase_add_tests(struct torture_tcase *tcase)
{
	torture_tcase_add_simple_test(tcase, "del_nonexistant_key",
				      test_del_nonexistant_key);
	torture_tcase_add_simple_test(tcase, "add_subkey",
				      test_add_subkey);
	torture_tcase_add_simple_test(tcase, "flush_key",
				      test_flush_key);
	torture_tcase_add_simple_test(tcase, "get_info",
				      test_keyinfo_root);
	torture_tcase_add_simple_test(tcase, "get_info_nums",
				      test_keyinfo_nums);
	torture_tcase_add_simple_test(tcase, "set_value",
				      test_set_value);
	torture_tcase_add_simple_test(tcase, "get_value",
				      test_get_value);
	torture_tcase_add_simple_test(tcase, "list_values",
				      test_list_values);
	torture_tcase_add_simple_test(tcase, "del_key",
				      test_del_key);
	torture_tcase_add_simple_test(tcase, "del_value",
				      test_del_value);
}

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

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

	rmdir(dirname);

	error = reg_create_directory(tctx, dirname, &key);
	if (!W_ERROR_IS_OK(error)) {
		fprintf(stderr, "Unable to initialize dir hive\n");
		return false;
	}

	*data = key;

	return true;
}

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->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-dir", &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;
}

static bool test_dir_refuses_null_location(struct torture_context *tctx)
{
	torture_assert_werr_equal(tctx, WERR_INVALID_PARAM,
				  reg_open_directory(NULL, NULL, NULL),
				  "reg_open_directory accepts NULL location");
	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");

	torture_suite_add_simple_test(suite, "dir-refuses-null-location",
				      test_dir_refuses_null_location);

	tcase = torture_suite_add_tcase(suite, "dir");
	torture_tcase_set_fixture(tcase, hive_setup_dir, NULL);
	tcase_add_tests(tcase);

	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;
}