Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-wsgi
[bbaumbach/samba-autobuild/.git] / source4 / lib / registry / tests / diff.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    local testing of registry diff functionality
5
6    Copyright (C) Jelmer Vernooij 2007
7    Copyright (C) Wilco Baan Hofman 2008
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
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 "param/param.h"
28
29 struct diff_tcase_data {
30         struct registry_context *r1_ctx;
31         struct registry_context *r2_ctx;
32         struct reg_diff_callbacks *callbacks;
33         void *callback_data;
34         char *tempdir;
35         char *filename;
36 };
37
38 static bool test_generate_diff(struct torture_context *tctx, void *tcase_data)
39 {
40         WERROR error;
41         struct diff_tcase_data *td = tcase_data;
42
43         error = reg_generate_diff(td->r1_ctx, td->r2_ctx, 
44                         td->callbacks,
45                         td->callback_data);
46         torture_assert_werr_ok(tctx, error, "reg_generate_diff");
47
48         return true;
49 }
50
51 #if 0
52 static bool test_diff_load(struct torture_context *tctx, void *tcase_data)
53 {
54         struct diff_tcase_data *td = tcase_data;
55         struct smb_iconv_convenience *ic;
56         struct reg_diff_callbacks *callbacks;
57         void *data;
58         WERROR error;
59
60         ic = lp_iconv_convenience(tctx->lp_ctx);
61
62         error = reg_diff_load(td->filename, iconv_convenience, callbacks, data);
63         torture_assert_werr_ok(tctx, error, "reg_diff_load");
64
65         return true;
66 }
67 #endif
68 static bool test_diff_apply(struct torture_context *tctx, void *tcase_data)
69 {
70         struct diff_tcase_data *td = tcase_data;
71         struct registry_key *key;
72         WERROR error;
73
74         error = reg_diff_apply(td->r1_ctx, td->filename);
75         torture_assert_werr_ok(tctx, error, "reg_diff_apply");
76
77         error = td->r1_ctx->ops->get_predefined_key(td->r1_ctx, HKEY_LOCAL_MACHINE, &key);
78         torture_assert_werr_ok(tctx, error, "Opening HKEY_LOCAL_MACHINE failed");
79
80         /* If this generates an error it could be that the apply doesn't work,
81          * but also that the reg_generate_diff didn't work. */
82         error = td->r1_ctx->ops->open_key(td->r1_ctx, key, "Software", &key);
83         torture_assert_werr_ok(tctx, error, "Opening HKLM\\Software failed");
84         error = td->r1_ctx->ops->open_key(td->r1_ctx, key, "Microsoft", &key);
85         torture_assert_werr_ok(tctx, error, "Opening HKLM\\Software\\Microsoft failed");
86         error = td->r1_ctx->ops->open_key(td->r1_ctx, key, "Windows", &key);
87         torture_assert_werr_ok(tctx, error, "Opening HKLM\\..\\Microsoft\\Windows failed");
88         error = td->r1_ctx->ops->open_key(td->r1_ctx, key, "CurrentVersion", &key);
89         torture_assert_werr_ok(tctx, error, "Opening HKLM\\..\\Windows\\CurrentVersion failed");
90         error = td->r1_ctx->ops->open_key(td->r1_ctx, key, "Policies", &key);
91         torture_assert_werr_ok(tctx, error, "Opening HKLM\\..\\CurrentVersion\\Policies failed");
92         error = td->r1_ctx->ops->open_key(td->r1_ctx, key, "Explorer", &key);
93         torture_assert_werr_ok(tctx, error, "Opening HKLM\\..\\Policies\\Explorer failed");
94
95         return true;
96 }
97
98 static const char *added_key = NULL;
99
100 static WERROR test_add_key(void *callback_data, const char *key_name)
101 {
102         added_key = talloc_strdup(callback_data, key_name);
103
104         return WERR_OK;
105 }
106
107 static bool test_generate_diff_key_add(struct torture_context *tctx, void *tcase_data)
108 {
109         struct reg_diff_callbacks cb;
110         struct registry_key rk;
111
112         return true;
113
114         ZERO_STRUCT(cb);
115
116         cb.add_key = test_add_key;
117
118         if (W_ERROR_IS_OK(reg_generate_diff_key(&rk, NULL, "bla", &cb, tctx)))
119                 return false;
120
121         torture_assert_str_equal(tctx, added_key, "bla", "key added");
122
123         return true;
124 }
125
126 static bool test_generate_diff_key_null(struct torture_context *tctx, void *tcase_data)
127 {
128         struct reg_diff_callbacks cb;
129
130         ZERO_STRUCT(cb);
131
132         if (!W_ERROR_IS_OK(reg_generate_diff_key(NULL, NULL, "", &cb, NULL)))
133                 return false;
134
135         return true;
136 }
137
138 static void tcase_add_tests (struct torture_tcase *tcase) 
139 {
140         torture_tcase_add_simple_test(tcase, "test_generate_diff_key_add",
141                         test_generate_diff_key_add);
142         torture_tcase_add_simple_test(tcase, "test_generate_diff_key_null",
143                         test_generate_diff_key_null);
144         torture_tcase_add_simple_test(tcase, "test_generate_diff",
145                         test_generate_diff);
146         torture_tcase_add_simple_test(tcase, "test_diff_apply",
147                         test_diff_apply);
148 /*      torture_tcase_add_simple_test(tcase, "test_diff_load",
149                         test_diff_load);
150 */
151 }
152
153 static bool diff_setup_tcase(struct torture_context *tctx, void **data)
154 {
155         struct registry_context *r1_ctx, *r2_ctx;
156         WERROR error;
157         NTSTATUS status;
158         struct hive_key *r1_hklm, *r1_hkcu;
159         struct hive_key *r2_hklm, *r2_hkcu;
160         const char *filename;
161         struct diff_tcase_data *td;
162         struct registry_key *key, *newkey;
163         DATA_BLOB blob;
164
165         td = talloc(tctx, struct diff_tcase_data);
166
167         /* Create two registry contexts */
168         error = reg_open_local(tctx, &r1_ctx);
169         torture_assert_werr_ok(tctx, error, "Opening registry 1 for patch tests failed");
170         
171         error = reg_open_local(tctx, &r2_ctx);
172         torture_assert_werr_ok(tctx, error, "Opening registry 2 for patch tests failed");
173
174         /* Create temp directory */
175         status = torture_temp_dir(tctx, "patchfile", &td->tempdir);
176         torture_assert_ntstatus_ok(tctx, status, "Creating temp dir failed");
177
178         /* Create and mount HKLM and HKCU hives for registry 1 */
179         filename = talloc_asprintf(tctx, "%s/r1_local_machine.ldb", td->tempdir);
180         error = reg_open_ldb_file(tctx, filename, NULL, NULL, tctx->ev, tctx->lp_ctx, &r1_hklm);
181         torture_assert_werr_ok(tctx, error, "Opening local machine file failed");
182
183         error = reg_mount_hive(r1_ctx, r1_hklm, HKEY_LOCAL_MACHINE, NULL);
184         torture_assert_werr_ok(tctx, error, "Mounting hive failed");
185         
186         filename = talloc_asprintf(tctx, "%s/r1_current_user.ldb", td->tempdir);
187         error = reg_open_ldb_file(tctx, filename, NULL, NULL, tctx->ev, tctx->lp_ctx, &r1_hkcu);
188         torture_assert_werr_ok(tctx, error, "Opening current user file failed");
189
190         error = reg_mount_hive(r1_ctx, r1_hkcu, HKEY_CURRENT_USER, NULL);
191         torture_assert_werr_ok(tctx, error, "Mounting hive failed");
192         
193         /* Create and mount HKLM and HKCU hives for registry 2 */
194         filename = talloc_asprintf(tctx, "%s/r2_local_machine.ldb", td->tempdir);
195         error = reg_open_ldb_file(tctx, filename, NULL, NULL, tctx->ev, tctx->lp_ctx, &r2_hklm);
196         torture_assert_werr_ok(tctx, error, "Opening local machine file failed");
197
198         error = reg_mount_hive(r2_ctx, r2_hklm, HKEY_LOCAL_MACHINE, NULL);
199         torture_assert_werr_ok(tctx, error, "Mounting hive failed");
200         
201         filename = talloc_asprintf(tctx, "%s/r2_current_user.ldb", td->tempdir);
202         error = reg_open_ldb_file(tctx, filename, NULL, NULL, tctx->ev, tctx->lp_ctx, &r2_hkcu);
203         torture_assert_werr_ok(tctx, error, "Opening current user file failed");
204         
205         error = reg_mount_hive(r2_ctx, r2_hkcu, HKEY_CURRENT_USER, NULL);
206         torture_assert_werr_ok(tctx, error, "Mounting hive failed");
207
208         error = r1_ctx->ops->get_predefined_key(r1_ctx, HKEY_CURRENT_USER, &key);
209         torture_assert_werr_ok(tctx, error, "Opening HKEY_CURRENT_USER failed");
210         error = r1_ctx->ops->create_key(r1_ctx, key, "Network", NULL, NULL, &newkey);
211         torture_assert_werr_ok(tctx, error, "Opening HKCU\\Network failed");
212         error = r1_ctx->ops->create_key(r1_ctx, newkey, "L", NULL, NULL, &newkey);
213         torture_assert_werr_ok(tctx, error, "Opening HKCU\\Network\\L failed");
214
215         error = r2_ctx->ops->get_predefined_key(r2_ctx, HKEY_LOCAL_MACHINE, &key);
216         torture_assert_werr_ok(tctx, error, "Opening HKEY_LOCAL_MACHINE failed");
217         error = r2_ctx->ops->create_key(r2_ctx, key, "Software", NULL, NULL, &newkey);
218         torture_assert_werr_ok(tctx, error, "Creating HKLM\\Sofware failed");
219         error = r2_ctx->ops->create_key(r2_ctx, newkey, "Microsoft", NULL, NULL, &newkey);
220         torture_assert_werr_ok(tctx, error, "Creating HKLM\\Software\\Microsoft failed");
221         error = r2_ctx->ops->create_key(r2_ctx, newkey, "Windows", NULL, NULL, &newkey);
222         torture_assert_werr_ok(tctx, error, "Creating HKLM\\Software\\Microsoft\\Windows failed");
223         error = r2_ctx->ops->create_key(r2_ctx, newkey, "CurrentVersion", NULL, NULL, &newkey);
224         torture_assert_werr_ok(tctx, error, "Creating HKLM\\..\\Windows\\CurrentVersion failed");
225         error = r2_ctx->ops->create_key(r2_ctx, newkey, "Policies", NULL, NULL, &newkey);
226         torture_assert_werr_ok(tctx, error, "Creating HKLM\\..\\CurrentVersion\\Policies failed");
227         error = r2_ctx->ops->create_key(r2_ctx, newkey, "Explorer", NULL, NULL, &newkey);
228         torture_assert_werr_ok(tctx, error, "Creating HKLM\\..\\Policies\\Explorer failed");
229
230
231         blob.data = (void *)talloc(r2_ctx, uint32_t);
232         SIVAL(blob.data, 0, 0x03ffffff);
233         blob.length = sizeof(uint32_t);
234
235         r1_ctx->ops->set_value(newkey, "NoDrives", REG_DWORD, blob);
236
237         /* Set test case data */
238         td->r1_ctx = r1_ctx;
239         td->r2_ctx = r2_ctx;
240
241         *data = td;
242
243         return true;
244 }
245
246 static bool diff_setup_preg_tcase (struct torture_context *tctx, void **data)
247 {
248         struct diff_tcase_data *td;
249         struct smb_iconv_convenience *ic;
250         WERROR error;
251
252         diff_setup_tcase(tctx, data);
253         td = *data;
254
255         ic = lp_iconv_convenience(tctx->lp_ctx);
256
257         td->filename = talloc_asprintf(tctx, "%s/test.pol", td->tempdir);
258         error = reg_preg_diff_save(tctx, td->filename, ic, &td->callbacks, &td->callback_data);
259         torture_assert_werr_ok(tctx, error, "reg_preg_diff_save");
260
261         return true;
262 }
263
264 static bool diff_setup_dotreg_tcase (struct torture_context *tctx, void **data)
265 {
266         struct diff_tcase_data *td;
267         struct smb_iconv_convenience *ic;
268         WERROR error;
269
270         diff_setup_tcase(tctx, data);
271         td = *data;
272
273         ic = lp_iconv_convenience(tctx->lp_ctx);
274         
275         td->filename = talloc_asprintf(tctx, "%s/test.reg", td->tempdir);
276         error = reg_dotreg_diff_save(tctx, td->filename, ic, &td->callbacks, &td->callback_data);
277         torture_assert_werr_ok(tctx, error, "reg_dotreg_diff_save");
278
279         return true;
280 }
281
282 struct torture_suite *torture_registry_diff(TALLOC_CTX *mem_ctx)
283 {
284         struct torture_tcase *tcase;
285         struct torture_suite *suite = torture_suite_create(mem_ctx, "DIFF");
286
287         tcase = torture_suite_add_tcase(suite, "PReg");
288         torture_tcase_set_fixture(tcase, diff_setup_preg_tcase, NULL);
289         tcase_add_tests(tcase);
290
291         tcase = torture_suite_add_tcase(suite, "dotreg");
292         torture_tcase_set_fixture(tcase, diff_setup_dotreg_tcase, NULL);
293         tcase_add_tests(tcase);
294
295         return suite;
296 }