selftest: Use Python provision for Samba 4 by default.
[samba.git] / source / lib / registry / tests / hive.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    local testing of registry library - hives
5
6    Copyright (C) Jelmer Vernooij 2005-2007
7
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.
12
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.
17
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.
21 */
22
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
29 static bool test_del_nonexistant_key(struct torture_context *tctx,
30                                      const void *test_data)
31 {
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");
36
37         return true;
38 }
39
40 static bool test_keyinfo_root(struct torture_context *tctx,
41                               const void *test_data)
42 {
43         uint32_t num_subkeys, num_values;
44         const struct hive_key *root = (const struct hive_key *)test_data;
45         WERROR error;
46
47         /* This is a new backend. There should be no subkeys and no
48          * values */
49         error = hive_key_get_info(tctx, root, NULL, &num_subkeys, &num_values,
50                                   NULL, NULL, NULL, NULL);
51         torture_assert_werr_ok(tctx, error, "reg_key_num_subkeys()");
52
53         torture_assert_int_equal(tctx, num_subkeys, 0,
54                                  "New key has non-zero subkey count");
55
56         torture_assert_werr_ok(tctx, error, "reg_key_num_values");
57
58         torture_assert_int_equal(tctx, num_values, 0,
59                                  "New key has non-zero value count");
60
61         return true;
62 }
63
64 static bool test_keyinfo_nums(struct torture_context *tctx, void *test_data)
65 {
66         uint32_t num_subkeys, num_values;
67         struct hive_key *root = (struct hive_key *)test_data;
68         WERROR error;
69         struct hive_key *subkey;
70         uint32_t data = 42;
71
72         error = hive_key_add_name(tctx, root, "Nested Keyll", NULL,
73                                   NULL, &subkey);
74         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
75
76         error = hive_key_set_value(root, "Answer", REG_DWORD,
77                                data_blob_talloc(tctx, &data, sizeof(data)));
78         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
79
80         /* This is a new backend. There should be no subkeys and no
81          * values */
82         error = hive_key_get_info(tctx, root, NULL, &num_subkeys, &num_values,
83                                   NULL, NULL, NULL, NULL);
84         torture_assert_werr_ok(tctx, error, "reg_key_num_subkeys()");
85
86         torture_assert_int_equal(tctx, num_subkeys, 1, "subkey count");
87
88         torture_assert_werr_ok(tctx, error, "reg_key_num_values");
89
90         torture_assert_int_equal(tctx, num_values, 1, "value count");
91
92         return true;
93 }
94
95 static bool test_add_subkey(struct torture_context *tctx,
96                             const void *test_data)
97 {
98         WERROR error;
99         struct hive_key *subkey;
100         const struct hive_key *root = (const struct hive_key *)test_data;
101         TALLOC_CTX *mem_ctx = tctx;
102
103         error = hive_key_add_name(mem_ctx, root, "Nested Key", NULL,
104                                   NULL, &subkey);
105         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
106
107         error = hive_key_del(root, "Nested Key");
108         torture_assert_werr_ok(tctx, error, "reg_key_del");
109
110         return true;
111 }
112
113 static bool test_flush_key(struct torture_context *tctx, void *test_data)
114 {
115         struct hive_key *root = (struct hive_key *)test_data;
116
117         torture_assert_werr_ok(tctx, hive_key_flush(root), "flush key");
118
119         return true;
120 }
121
122 static bool test_del_key(struct torture_context *tctx, const void *test_data)
123 {
124         WERROR error;
125         struct hive_key *subkey;
126         const struct hive_key *root = (const struct hive_key *)test_data;
127         TALLOC_CTX *mem_ctx = tctx;
128
129         error = hive_key_add_name(mem_ctx, root, "Nested Key", NULL,
130                                   NULL, &subkey);
131         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
132
133         error = hive_key_del(root, "Nested Key");
134         torture_assert_werr_ok(tctx, error, "reg_key_del");
135
136         error = hive_key_del(root, "Nested Key");
137         torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND, "reg_key_del");
138
139         return true;
140 }
141
142 static bool test_set_value(struct torture_context *tctx,
143                            const void *test_data)
144 {
145         WERROR error;
146         struct hive_key *subkey;
147         const struct hive_key *root = (const struct hive_key *)test_data;
148         TALLOC_CTX *mem_ctx = tctx;
149         uint32_t data = 42;
150
151         error = hive_key_add_name(mem_ctx, root, "YA Nested Key", NULL,
152                                   NULL, &subkey);
153         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
154
155         error = hive_key_set_value(subkey, "Answer", REG_DWORD,
156                                data_blob_talloc(mem_ctx, &data, sizeof(data)));
157         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
158
159         return true;
160 }
161
162 static bool test_get_value(struct torture_context *tctx, const void *test_data)
163 {
164         WERROR error;
165         struct hive_key *subkey;
166         const struct hive_key *root = (const struct hive_key *)test_data;
167         TALLOC_CTX *mem_ctx = tctx;
168         uint32_t data = 42;
169         uint32_t type;
170         DATA_BLOB value;
171
172         error = hive_key_add_name(mem_ctx, root, "EYA Nested Key", NULL,
173                                   NULL, &subkey);
174         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
175
176         error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
177         torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
178                                   "getting missing value");
179
180         error = hive_key_set_value(subkey, "Answer", REG_DWORD,
181                                data_blob_talloc(mem_ctx, &data, sizeof(data)));
182         torture_assert_werr_ok(tctx, error, "hive_key_set_value");
183
184         error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
185         torture_assert_werr_ok(tctx, error, "getting value");
186
187         torture_assert_int_equal(tctx, value.length, 4, "value length");
188         torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
189
190         torture_assert_int_equal(tctx, data, IVAL(value.data, 0),
191                                  "value data");
192
193         return true;
194 }
195
196 static bool test_del_value(struct torture_context *tctx, const void *test_data)
197 {
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;
205
206         error = hive_key_add_name(mem_ctx, root, "EEYA Nested Key", NULL,
207                                                          NULL, &subkey);
208         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
209
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");
213
214         error = hive_key_del_value(subkey, "Answer");
215         torture_assert_werr_ok(tctx, error, "deleting value");
216
217         error = hive_get_value(mem_ctx, subkey, "Answer", &type, &value);
218         torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND, "getting value");
219
220         error = hive_key_del_value(subkey, "Answer");
221         torture_assert_werr_equal(tctx, error, WERR_NOT_FOUND,
222                                   "deleting value");
223
224         return true;
225 }
226
227 static bool test_list_values(struct torture_context *tctx,
228                              const void *test_data)
229 {
230         WERROR error;
231         struct hive_key *subkey;
232         const struct hive_key *root = (const struct hive_key *)test_data;
233         TALLOC_CTX *mem_ctx = tctx;
234         uint32_t data = 42;
235         uint32_t type;
236         DATA_BLOB value;
237         const char *name;
238
239         error = hive_key_add_name(mem_ctx, root, "AYAYA Nested Key", NULL,
240                                   NULL, &subkey);
241         torture_assert_werr_ok(tctx, error, "hive_key_add_name");
242
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");
246
247         error = hive_get_value_by_index(mem_ctx, subkey, 0, &name,
248                                         &type, &value);
249         torture_assert_werr_ok(tctx, error, "getting value");
250
251         torture_assert_str_equal(tctx, name, "Answer", "value name");
252
253         torture_assert_int_equal(tctx, value.length, 4, "value length");
254         torture_assert_int_equal(tctx, type, REG_DWORD, "value type");
255         
256         
257         torture_assert_int_equal(tctx, data, IVAL(value.data, 0), "value data");
258
259         error = hive_get_value_by_index(mem_ctx, subkey, 1, &name,
260                                         &type, &value);
261         torture_assert_werr_equal(tctx, error, WERR_NO_MORE_ITEMS,
262                                   "getting missing value");
263
264         return true;
265 }
266
267 static void tcase_add_tests(struct torture_tcase *tcase)
268 {
269         torture_tcase_add_simple_test_const(tcase, "del_nonexistant_key",
270                                                 test_del_nonexistant_key);
271         torture_tcase_add_simple_test_const(tcase, "add_subkey",
272                                                 test_add_subkey);
273         torture_tcase_add_simple_test(tcase, "flush_key",
274                                                 test_flush_key);
275         torture_tcase_add_simple_test_const(tcase, "get_info",
276                                                 test_keyinfo_root);
277         torture_tcase_add_simple_test(tcase, "get_info_nums",
278                                                 test_keyinfo_nums);
279         torture_tcase_add_simple_test_const(tcase, "set_value",
280                                                 test_set_value);
281         torture_tcase_add_simple_test_const(tcase, "get_value",
282                                                 test_get_value);
283         torture_tcase_add_simple_test_const(tcase, "list_values",
284                                                 test_list_values);
285         torture_tcase_add_simple_test_const(tcase, "del_key",
286                                                 test_del_key);
287         torture_tcase_add_simple_test_const(tcase, "del_value",
288                                                 test_del_value);
289 }
290
291 static bool hive_setup_dir(struct torture_context *tctx, void **data)
292 {
293         struct hive_key *key;
294         WERROR error;
295         char *dirname;
296         NTSTATUS status;
297
298         status = torture_temp_dir(tctx, "hive-dir", &dirname);
299         if (!NT_STATUS_IS_OK(status))
300                 return false;
301
302         rmdir(dirname);
303
304         error = reg_create_directory(tctx, dirname, &key);
305         if (!W_ERROR_IS_OK(error)) {
306                 fprintf(stderr, "Unable to initialize dir hive\n");
307                 return false;
308         }
309
310         *data = key;
311
312         return true;
313 }
314
315 static bool hive_setup_ldb(struct torture_context *tctx, void **data)
316 {
317         struct hive_key *key;
318         WERROR error;
319         char *dirname;
320         NTSTATUS status;
321
322         status = torture_temp_dir(tctx, "hive-ldb", &dirname);
323         if (!NT_STATUS_IS_OK(status))
324                 return false;
325
326         rmdir(dirname);
327
328         error = reg_open_ldb_file(tctx, dirname, NULL, NULL, tctx->lp_ctx, &key);
329         if (!W_ERROR_IS_OK(error)) {
330                 fprintf(stderr, "Unable to initialize ldb hive\n");
331                 return false;
332         }
333
334         *data = key;
335
336         return true;
337 }
338
339 static bool hive_setup_regf(struct torture_context *tctx, void **data)
340 {
341         struct hive_key *key;
342         WERROR error;
343         char *dirname;
344         NTSTATUS status;
345
346         status = torture_temp_dir(tctx, "hive-dir", &dirname);
347         if (!NT_STATUS_IS_OK(status))
348                 return false;
349
350         rmdir(dirname);
351
352         error = reg_create_regf_file(tctx, dirname, 5, &key);
353         if (!W_ERROR_IS_OK(error)) {
354                 fprintf(stderr, "Unable to create new regf file\n");
355                 return false;
356         }
357
358         *data = key;
359
360         return true;
361 }
362
363 static bool test_dir_refuses_null_location(struct torture_context *tctx)
364 {
365         torture_assert_werr_equal(tctx, WERR_INVALID_PARAM,
366                                   reg_open_directory(NULL, NULL, NULL),
367                                   "reg_open_directory accepts NULL location");
368         return true;
369 }
370
371 struct torture_suite *torture_registry_hive(TALLOC_CTX *mem_ctx)
372 {
373         struct torture_tcase *tcase;
374         struct torture_suite *suite = torture_suite_create(mem_ctx, "HIVE");
375
376         torture_suite_add_simple_test(suite, "dir-refuses-null-location",
377                                       test_dir_refuses_null_location);
378
379         tcase = torture_suite_add_tcase(suite, "dir");
380         torture_tcase_set_fixture(tcase, hive_setup_dir, NULL);
381         tcase_add_tests(tcase);
382
383         tcase = torture_suite_add_tcase(suite, "ldb");
384         torture_tcase_set_fixture(tcase, hive_setup_ldb, NULL);
385         tcase_add_tests(tcase);
386
387         tcase = torture_suite_add_tcase(suite, "regf");
388         torture_tcase_set_fixture(tcase, hive_setup_regf, NULL);
389         tcase_add_tests(tcase);
390
391         return suite;
392 }