Finish removal of iconv_convenience in public API's.
[amitay/samba.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 reg_diff_callbacks *callbacks;
56         void *data;
57         WERROR error;
58
59         error = reg_diff_load(td->filename, callbacks, data);
60         torture_assert_werr_ok(tctx, error, "reg_diff_load");
61
62         return true;
63 }
64 #endif
65 static bool test_diff_apply(struct torture_context *tctx, void *tcase_data)
66 {
67         struct diff_tcase_data *td = tcase_data;
68         struct registry_key *key;
69         WERROR error;
70
71         error = reg_diff_apply(td->r1_ctx, td->filename);
72         torture_assert_werr_ok(tctx, error, "reg_diff_apply");
73
74         error = td->r1_ctx->ops->get_predefined_key(td->r1_ctx, HKEY_LOCAL_MACHINE, &key);
75         torture_assert_werr_ok(tctx, error, "Opening HKEY_LOCAL_MACHINE failed");
76
77         /* If this generates an error it could be that the apply doesn't work,
78          * but also that the reg_generate_diff didn't work. */
79         error = td->r1_ctx->ops->open_key(td->r1_ctx, key, "Software", &key);
80         torture_assert_werr_ok(tctx, error, "Opening HKLM\\Software failed");
81         error = td->r1_ctx->ops->open_key(td->r1_ctx, key, "Microsoft", &key);
82         torture_assert_werr_ok(tctx, error, "Opening HKLM\\Software\\Microsoft failed");
83         error = td->r1_ctx->ops->open_key(td->r1_ctx, key, "Windows", &key);
84         torture_assert_werr_ok(tctx, error, "Opening HKLM\\..\\Microsoft\\Windows failed");
85         error = td->r1_ctx->ops->open_key(td->r1_ctx, key, "CurrentVersion", &key);
86         torture_assert_werr_ok(tctx, error, "Opening HKLM\\..\\Windows\\CurrentVersion failed");
87         error = td->r1_ctx->ops->open_key(td->r1_ctx, key, "Policies", &key);
88         torture_assert_werr_ok(tctx, error, "Opening HKLM\\..\\CurrentVersion\\Policies failed");
89         error = td->r1_ctx->ops->open_key(td->r1_ctx, key, "Explorer", &key);
90         torture_assert_werr_ok(tctx, error, "Opening HKLM\\..\\Policies\\Explorer failed");
91
92         return true;
93 }
94
95 static const char *added_key = NULL;
96
97 static WERROR test_add_key(void *callback_data, const char *key_name)
98 {
99         added_key = talloc_strdup(callback_data, key_name);
100
101         return WERR_OK;
102 }
103
104 static bool test_generate_diff_key_add(struct torture_context *tctx, void *tcase_data)
105 {
106         struct reg_diff_callbacks cb;
107         struct registry_key rk;
108
109         return true;
110
111         ZERO_STRUCT(cb);
112
113         cb.add_key = test_add_key;
114
115         if (W_ERROR_IS_OK(reg_generate_diff_key(&rk, NULL, "bla", &cb, tctx)))
116                 return false;
117
118         torture_assert_str_equal(tctx, added_key, "bla", "key added");
119
120         return true;
121 }
122
123 static bool test_generate_diff_key_null(struct torture_context *tctx, void *tcase_data)
124 {
125         struct reg_diff_callbacks cb;
126
127         ZERO_STRUCT(cb);
128
129         if (!W_ERROR_IS_OK(reg_generate_diff_key(NULL, NULL, "", &cb, NULL)))
130                 return false;
131
132         return true;
133 }
134
135 static void tcase_add_tests (struct torture_tcase *tcase) 
136 {
137         torture_tcase_add_simple_test(tcase, "test_generate_diff_key_add",
138                         test_generate_diff_key_add);
139         torture_tcase_add_simple_test(tcase, "test_generate_diff_key_null",
140                         test_generate_diff_key_null);
141         torture_tcase_add_simple_test(tcase, "test_generate_diff",
142                         test_generate_diff);
143         torture_tcase_add_simple_test(tcase, "test_diff_apply",
144                         test_diff_apply);
145 /*      torture_tcase_add_simple_test(tcase, "test_diff_load",
146                         test_diff_load);
147 */
148 }
149
150 static bool diff_setup_tcase(struct torture_context *tctx, void **data)
151 {
152         struct registry_context *r1_ctx, *r2_ctx;
153         WERROR error;
154         NTSTATUS status;
155         struct hive_key *r1_hklm, *r1_hkcu;
156         struct hive_key *r2_hklm, *r2_hkcu;
157         const char *filename;
158         struct diff_tcase_data *td;
159         struct registry_key *key, *newkey;
160         DATA_BLOB blob;
161
162         td = talloc(tctx, struct diff_tcase_data);
163
164         /* Create two registry contexts */
165         error = reg_open_local(tctx, &r1_ctx);
166         torture_assert_werr_ok(tctx, error, "Opening registry 1 for patch tests failed");
167         
168         error = reg_open_local(tctx, &r2_ctx);
169         torture_assert_werr_ok(tctx, error, "Opening registry 2 for patch tests failed");
170
171         /* Create temp directory */
172         status = torture_temp_dir(tctx, "patchfile", &td->tempdir);
173         torture_assert_ntstatus_ok(tctx, status, "Creating temp dir failed");
174
175         /* Create and mount HKLM and HKCU hives for registry 1 */
176         filename = talloc_asprintf(tctx, "%s/r1_local_machine.ldb", td->tempdir);
177         error = reg_open_ldb_file(tctx, filename, NULL, NULL, tctx->ev, tctx->lp_ctx, &r1_hklm);
178         torture_assert_werr_ok(tctx, error, "Opening local machine file failed");
179
180         error = reg_mount_hive(r1_ctx, r1_hklm, HKEY_LOCAL_MACHINE, NULL);
181         torture_assert_werr_ok(tctx, error, "Mounting hive failed");
182         
183         filename = talloc_asprintf(tctx, "%s/r1_current_user.ldb", td->tempdir);
184         error = reg_open_ldb_file(tctx, filename, NULL, NULL, tctx->ev, tctx->lp_ctx, &r1_hkcu);
185         torture_assert_werr_ok(tctx, error, "Opening current user file failed");
186
187         error = reg_mount_hive(r1_ctx, r1_hkcu, HKEY_CURRENT_USER, NULL);
188         torture_assert_werr_ok(tctx, error, "Mounting hive failed");
189         
190         /* Create and mount HKLM and HKCU hives for registry 2 */
191         filename = talloc_asprintf(tctx, "%s/r2_local_machine.ldb", td->tempdir);
192         error = reg_open_ldb_file(tctx, filename, NULL, NULL, tctx->ev, tctx->lp_ctx, &r2_hklm);
193         torture_assert_werr_ok(tctx, error, "Opening local machine file failed");
194
195         error = reg_mount_hive(r2_ctx, r2_hklm, HKEY_LOCAL_MACHINE, NULL);
196         torture_assert_werr_ok(tctx, error, "Mounting hive failed");
197         
198         filename = talloc_asprintf(tctx, "%s/r2_current_user.ldb", td->tempdir);
199         error = reg_open_ldb_file(tctx, filename, NULL, NULL, tctx->ev, tctx->lp_ctx, &r2_hkcu);
200         torture_assert_werr_ok(tctx, error, "Opening current user file failed");
201         
202         error = reg_mount_hive(r2_ctx, r2_hkcu, HKEY_CURRENT_USER, NULL);
203         torture_assert_werr_ok(tctx, error, "Mounting hive failed");
204
205         error = r1_ctx->ops->get_predefined_key(r1_ctx, HKEY_CURRENT_USER, &key);
206         torture_assert_werr_ok(tctx, error, "Opening HKEY_CURRENT_USER failed");
207         error = r1_ctx->ops->create_key(r1_ctx, key, "Network", NULL, NULL, &newkey);
208         torture_assert_werr_ok(tctx, error, "Opening HKCU\\Network failed");
209         error = r1_ctx->ops->create_key(r1_ctx, newkey, "L", NULL, NULL, &newkey);
210         torture_assert_werr_ok(tctx, error, "Opening HKCU\\Network\\L failed");
211
212         error = r2_ctx->ops->get_predefined_key(r2_ctx, HKEY_LOCAL_MACHINE, &key);
213         torture_assert_werr_ok(tctx, error, "Opening HKEY_LOCAL_MACHINE failed");
214         error = r2_ctx->ops->create_key(r2_ctx, key, "Software", NULL, NULL, &newkey);
215         torture_assert_werr_ok(tctx, error, "Creating HKLM\\Sofware failed");
216         error = r2_ctx->ops->create_key(r2_ctx, newkey, "Microsoft", NULL, NULL, &newkey);
217         torture_assert_werr_ok(tctx, error, "Creating HKLM\\Software\\Microsoft failed");
218         error = r2_ctx->ops->create_key(r2_ctx, newkey, "Windows", NULL, NULL, &newkey);
219         torture_assert_werr_ok(tctx, error, "Creating HKLM\\Software\\Microsoft\\Windows failed");
220         error = r2_ctx->ops->create_key(r2_ctx, newkey, "CurrentVersion", NULL, NULL, &newkey);
221         torture_assert_werr_ok(tctx, error, "Creating HKLM\\..\\Windows\\CurrentVersion failed");
222         error = r2_ctx->ops->create_key(r2_ctx, newkey, "Policies", NULL, NULL, &newkey);
223         torture_assert_werr_ok(tctx, error, "Creating HKLM\\..\\CurrentVersion\\Policies failed");
224         error = r2_ctx->ops->create_key(r2_ctx, newkey, "Explorer", NULL, NULL, &newkey);
225         torture_assert_werr_ok(tctx, error, "Creating HKLM\\..\\Policies\\Explorer failed");
226
227         blob.data = talloc_array(r2_ctx, uint8_t, 4);
228         /* set "0x03FFFFFF" in little endian format */
229         blob.data[0] = 0xFF; blob.data[1] = 0xFF;
230         blob.data[2] = 0xFF; blob.data[3] = 0x03;
231         blob.length = 4;
232
233         r1_ctx->ops->set_value(newkey, "NoDrives", REG_DWORD, blob);
234
235         /* Set test case data */
236         td->r1_ctx = r1_ctx;
237         td->r2_ctx = r2_ctx;
238
239         *data = td;
240
241         return true;
242 }
243
244 static bool diff_setup_preg_tcase (struct torture_context *tctx, void **data)
245 {
246         struct diff_tcase_data *td;
247         WERROR error;
248
249         diff_setup_tcase(tctx, data);
250         td = *data;
251
252         td->filename = talloc_asprintf(tctx, "%s/test.pol", td->tempdir);
253         error = reg_preg_diff_save(tctx, td->filename,  &td->callbacks,
254                                                            &td->callback_data);
255         torture_assert_werr_ok(tctx, error, "reg_preg_diff_save");
256
257         return true;
258 }
259
260 static bool diff_setup_dotreg_tcase (struct torture_context *tctx, void **data)
261 {
262         struct diff_tcase_data *td;
263         WERROR error;
264
265         diff_setup_tcase(tctx, data);
266         td = *data;
267
268         td->filename = talloc_asprintf(tctx, "%s/test.reg", td->tempdir);
269         error = reg_dotreg_diff_save(tctx, td->filename, &td->callbacks,
270                                                                  &td->callback_data);
271         torture_assert_werr_ok(tctx, error, "reg_dotreg_diff_save");
272
273         return true;
274 }
275
276 struct torture_suite *torture_registry_diff(TALLOC_CTX *mem_ctx)
277 {
278         struct torture_tcase *tcase;
279         struct torture_suite *suite = torture_suite_create(mem_ctx, "DIFF");
280
281         tcase = torture_suite_add_tcase(suite, "PReg");
282         torture_tcase_set_fixture(tcase, diff_setup_preg_tcase, NULL);
283         tcase_add_tests(tcase);
284
285         tcase = torture_suite_add_tcase(suite, "dotreg");
286         torture_tcase_set_fixture(tcase, diff_setup_dotreg_tcase, NULL);
287         tcase_add_tests(tcase);
288
289         return suite;
290 }