2 Unix SMB/CIFS implementation.
4 local testing of registry diff functionality
6 Copyright (C) Jelmer Vernooij 2007
7 Copyright (C) Wilco Baan Hofman 2008
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.
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.
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/>.
24 #include "lib/registry/registry.h"
25 #include "torture/torture.h"
26 #include "librpc/gen_ndr/winreg.h"
27 #include "param/param.h"
29 struct diff_tcase_data {
30 struct registry_context *r1_ctx;
31 struct registry_context *r2_ctx;
32 struct reg_diff_callbacks *callbacks;
38 static bool test_generate_diff(struct torture_context *tctx, void *tcase_data)
41 struct diff_tcase_data *td = tcase_data;
43 error = reg_generate_diff(td->r1_ctx, td->r2_ctx,
46 torture_assert_werr_ok(tctx, error, "reg_generate_diff");
52 static bool test_diff_load(struct torture_context *tctx, void *tcase_data)
54 struct diff_tcase_data *td = tcase_data;
55 struct smb_iconv_convenience *ic;
56 struct reg_diff_callbacks *callbacks;
60 ic = lp_iconv_convenience(tctx->lp_ctx);
62 error = reg_diff_load(td->filename, iconv_convenience, callbacks, data);
63 torture_assert_werr_ok(tctx, error, "reg_diff_load");
68 static bool test_diff_apply(struct torture_context *tctx, void *tcase_data)
70 struct diff_tcase_data *td = tcase_data;
71 struct registry_key *key;
74 error = reg_diff_apply(td->r1_ctx, td->filename);
75 torture_assert_werr_ok(tctx, error, "reg_diff_apply");
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");
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");
98 static const char *added_key = NULL;
100 static WERROR test_add_key(void *callback_data, const char *key_name)
102 added_key = talloc_strdup(callback_data, key_name);
107 static bool test_generate_diff_key_add(struct torture_context *tctx, void *tcase_data)
109 struct reg_diff_callbacks cb;
110 struct registry_key rk;
116 cb.add_key = test_add_key;
118 if (W_ERROR_IS_OK(reg_generate_diff_key(&rk, NULL, "bla", &cb, tctx)))
121 torture_assert_str_equal(tctx, added_key, "bla", "key added");
126 static bool test_generate_diff_key_null(struct torture_context *tctx, void *tcase_data)
128 struct reg_diff_callbacks cb;
132 if (!W_ERROR_IS_OK(reg_generate_diff_key(NULL, NULL, "", &cb, NULL)))
138 static void tcase_add_tests (struct torture_tcase *tcase)
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",
146 torture_tcase_add_simple_test(tcase, "test_diff_apply",
148 /* torture_tcase_add_simple_test(tcase, "test_diff_load",
153 static bool diff_setup_tcase(struct torture_context *tctx, void **data)
155 struct registry_context *r1_ctx, *r2_ctx;
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;
165 td = talloc(tctx, struct diff_tcase_data);
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");
171 error = reg_open_local(tctx, &r2_ctx);
172 torture_assert_werr_ok(tctx, error, "Opening registry 2 for patch tests failed");
174 /* Create temp directory */
175 status = torture_temp_dir(tctx, "patchfile", &td->tempdir);
176 torture_assert_ntstatus_ok(tctx, status, "Creating temp dir failed");
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");
183 error = reg_mount_hive(r1_ctx, r1_hklm, HKEY_LOCAL_MACHINE, NULL);
184 torture_assert_werr_ok(tctx, error, "Mounting hive failed");
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");
190 error = reg_mount_hive(r1_ctx, r1_hkcu, HKEY_CURRENT_USER, NULL);
191 torture_assert_werr_ok(tctx, error, "Mounting hive failed");
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");
198 error = reg_mount_hive(r2_ctx, r2_hklm, HKEY_LOCAL_MACHINE, NULL);
199 torture_assert_werr_ok(tctx, error, "Mounting hive failed");
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");
205 error = reg_mount_hive(r2_ctx, r2_hkcu, HKEY_CURRENT_USER, NULL);
206 torture_assert_werr_ok(tctx, error, "Mounting hive failed");
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");
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");
231 blob.data = (void *)talloc(r2_ctx, uint32_t);
232 SIVAL(blob.data, 0, 0x03ffffff);
233 blob.length = sizeof(uint32_t);
235 r1_ctx->ops->set_value(newkey, "NoDrives", REG_DWORD, blob);
237 /* Set test case data */
246 static bool diff_setup_preg_tcase (struct torture_context *tctx, void **data)
248 struct diff_tcase_data *td;
249 struct smb_iconv_convenience *ic;
252 diff_setup_tcase(tctx, data);
255 ic = lp_iconv_convenience(tctx->lp_ctx);
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");
264 static bool diff_setup_dotreg_tcase (struct torture_context *tctx, void **data)
266 struct diff_tcase_data *td;
267 struct smb_iconv_convenience *ic;
270 diff_setup_tcase(tctx, data);
273 ic = lp_iconv_convenience(tctx->lp_ctx);
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");
282 struct torture_suite *torture_registry_diff(TALLOC_CTX *mem_ctx)
284 struct torture_tcase *tcase;
285 struct torture_suite *suite = torture_suite_create(mem_ctx, "DIFF");
287 tcase = torture_suite_add_tcase(suite, "PReg");
288 torture_tcase_set_fixture(tcase, diff_setup_preg_tcase, NULL);
289 tcase_add_tests(tcase);
291 tcase = torture_suite_add_tcase(suite, "dotreg");
292 torture_tcase_set_fixture(tcase, diff_setup_dotreg_tcase, NULL);
293 tcase_add_tests(tcase);