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 reg_diff_callbacks *callbacks;
59 error = reg_diff_load(td->filename, callbacks, data);
60 torture_assert_werr_ok(tctx, error, "reg_diff_load");
65 static bool test_diff_apply(struct torture_context *tctx, void *tcase_data)
67 struct diff_tcase_data *td = tcase_data;
68 struct registry_key *key;
71 error = reg_diff_apply(td->r1_ctx, td->filename);
72 torture_assert_werr_ok(tctx, error, "reg_diff_apply");
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");
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");
95 static const char *added_key = NULL;
97 static WERROR test_add_key(void *callback_data, const char *key_name)
99 added_key = talloc_strdup(callback_data, key_name);
104 static bool test_generate_diff_key_add(struct torture_context *tctx, void *tcase_data)
106 struct reg_diff_callbacks cb;
107 struct registry_key rk;
113 cb.add_key = test_add_key;
115 if (W_ERROR_IS_OK(reg_generate_diff_key(&rk, NULL, "bla", &cb, tctx)))
118 torture_assert_str_equal(tctx, added_key, "bla", "key added");
123 static bool test_generate_diff_key_null(struct torture_context *tctx, void *tcase_data)
125 struct reg_diff_callbacks cb;
129 if (!W_ERROR_IS_OK(reg_generate_diff_key(NULL, NULL, "", &cb, NULL)))
135 static void tcase_add_tests (struct torture_tcase *tcase)
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",
143 torture_tcase_add_simple_test(tcase, "test_diff_apply",
145 /* torture_tcase_add_simple_test(tcase, "test_diff_load",
150 static bool diff_setup_tcase(struct torture_context *tctx, void **data)
152 struct registry_context *r1_ctx, *r2_ctx;
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;
162 td = talloc(tctx, struct diff_tcase_data);
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");
168 error = reg_open_local(tctx, &r2_ctx);
169 torture_assert_werr_ok(tctx, error, "Opening registry 2 for patch tests failed");
171 /* Create temp directory */
172 status = torture_temp_dir(tctx, "patchfile", &td->tempdir);
173 torture_assert_ntstatus_ok(tctx, status, "Creating temp dir failed");
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");
180 error = reg_mount_hive(r1_ctx, r1_hklm, HKEY_LOCAL_MACHINE, NULL);
181 torture_assert_werr_ok(tctx, error, "Mounting hive failed");
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");
187 error = reg_mount_hive(r1_ctx, r1_hkcu, HKEY_CURRENT_USER, NULL);
188 torture_assert_werr_ok(tctx, error, "Mounting hive failed");
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");
195 error = reg_mount_hive(r2_ctx, r2_hklm, HKEY_LOCAL_MACHINE, NULL);
196 torture_assert_werr_ok(tctx, error, "Mounting hive failed");
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");
202 error = reg_mount_hive(r2_ctx, r2_hkcu, HKEY_CURRENT_USER, NULL);
203 torture_assert_werr_ok(tctx, error, "Mounting hive failed");
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");
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");
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;
233 r1_ctx->ops->set_value(newkey, "NoDrives", REG_DWORD, blob);
235 /* Set test case data */
244 static bool diff_setup_preg_tcase (struct torture_context *tctx, void **data)
246 struct diff_tcase_data *td;
249 diff_setup_tcase(tctx, data);
252 td->filename = talloc_asprintf(tctx, "%s/test.pol", td->tempdir);
253 error = reg_preg_diff_save(tctx, td->filename, &td->callbacks,
255 torture_assert_werr_ok(tctx, error, "reg_preg_diff_save");
260 static bool diff_setup_dotreg_tcase (struct torture_context *tctx, void **data)
262 struct diff_tcase_data *td;
265 diff_setup_tcase(tctx, data);
268 td->filename = talloc_asprintf(tctx, "%s/test.reg", td->tempdir);
269 error = reg_dotreg_diff_save(tctx, td->filename, &td->callbacks,
271 torture_assert_werr_ok(tctx, error, "reg_dotreg_diff_save");
276 struct torture_suite *torture_registry_diff(TALLOC_CTX *mem_ctx)
278 struct torture_tcase *tcase;
279 struct torture_suite *suite = torture_suite_create(mem_ctx, "DIFF");
281 tcase = torture_suite_add_tcase(suite, "PReg");
282 torture_tcase_set_fixture(tcase, diff_setup_preg_tcase, NULL);
283 tcase_add_tests(tcase);
285 tcase = torture_suite_add_tcase(suite, "dotreg");
286 torture_tcase_set_fixture(tcase, diff_setup_dotreg_tcase, NULL);
287 tcase_add_tests(tcase);