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"
28 #include "lib/registry/tests/proto.h"
30 struct diff_tcase_data
{
31 struct registry_context
*r1_ctx
;
32 struct registry_context
*r2_ctx
;
33 struct reg_diff_callbacks
*callbacks
;
39 static bool test_generate_diff(struct torture_context
*tctx
, void *tcase_data
)
42 struct diff_tcase_data
*td
= tcase_data
;
44 error
= reg_generate_diff(td
->r1_ctx
, td
->r2_ctx
,
47 torture_assert_werr_ok(tctx
, error
, "reg_generate_diff");
53 static bool test_diff_load(struct torture_context
*tctx
, void *tcase_data
)
55 struct diff_tcase_data
*td
= tcase_data
;
56 struct reg_diff_callbacks
*callbacks
;
60 error
= reg_diff_load(td
->filename
, callbacks
, data
);
61 torture_assert_werr_ok(tctx
, error
, "reg_diff_load");
66 static bool test_diff_apply(struct torture_context
*tctx
, void *tcase_data
)
68 struct diff_tcase_data
*td
= tcase_data
;
69 struct registry_key
*key
;
72 error
= reg_diff_apply(td
->r1_ctx
, td
->filename
);
73 torture_assert_werr_ok(tctx
, error
, "reg_diff_apply");
75 error
= td
->r1_ctx
->ops
->get_predefined_key(td
->r1_ctx
, HKEY_LOCAL_MACHINE
, &key
);
76 torture_assert_werr_ok(tctx
, error
, "Opening HKEY_LOCAL_MACHINE failed");
78 /* If this generates an error it could be that the apply doesn't work,
79 * but also that the reg_generate_diff didn't work. */
80 error
= td
->r1_ctx
->ops
->open_key(td
->r1_ctx
, key
, "Software", &key
);
81 torture_assert_werr_ok(tctx
, error
, "Opening HKLM\\Software failed");
82 error
= td
->r1_ctx
->ops
->open_key(td
->r1_ctx
, key
, "Microsoft", &key
);
83 torture_assert_werr_ok(tctx
, error
, "Opening HKLM\\Software\\Microsoft failed");
84 error
= td
->r1_ctx
->ops
->open_key(td
->r1_ctx
, key
, "Windows", &key
);
85 torture_assert_werr_ok(tctx
, error
, "Opening HKLM\\..\\Microsoft\\Windows failed");
86 error
= td
->r1_ctx
->ops
->open_key(td
->r1_ctx
, key
, "CurrentVersion", &key
);
87 torture_assert_werr_ok(tctx
, error
, "Opening HKLM\\..\\Windows\\CurrentVersion failed");
88 error
= td
->r1_ctx
->ops
->open_key(td
->r1_ctx
, key
, "Policies", &key
);
89 torture_assert_werr_ok(tctx
, error
, "Opening HKLM\\..\\CurrentVersion\\Policies failed");
90 error
= td
->r1_ctx
->ops
->open_key(td
->r1_ctx
, key
, "Explorer", &key
);
91 torture_assert_werr_ok(tctx
, error
, "Opening HKLM\\..\\Policies\\Explorer failed");
96 static const char *added_key
= NULL
;
98 static WERROR
test_add_key(void *callback_data
, const char *key_name
)
100 added_key
= talloc_strdup(callback_data
, key_name
);
105 static bool test_generate_diff_key_add(struct torture_context
*tctx
, void *tcase_data
)
107 struct reg_diff_callbacks cb
;
108 struct registry_key rk
;
114 cb
.add_key
= test_add_key
;
116 if (W_ERROR_IS_OK(reg_generate_diff_key(&rk
, NULL
, "bla", &cb
, tctx
)))
119 torture_assert_str_equal(tctx
, added_key
, "bla", "key added");
124 static bool test_generate_diff_key_null(struct torture_context
*tctx
, void *tcase_data
)
126 struct reg_diff_callbacks cb
;
130 if (!W_ERROR_IS_OK(reg_generate_diff_key(NULL
, NULL
, "", &cb
, NULL
)))
136 static void tcase_add_tests (struct torture_tcase
*tcase
)
138 torture_tcase_add_simple_test(tcase
, "test_generate_diff_key_add",
139 test_generate_diff_key_add
);
140 torture_tcase_add_simple_test(tcase
, "test_generate_diff_key_null",
141 test_generate_diff_key_null
);
142 torture_tcase_add_simple_test(tcase
, "test_generate_diff",
144 torture_tcase_add_simple_test(tcase
, "test_diff_apply",
146 /* torture_tcase_add_simple_test(tcase, "test_diff_load",
151 static bool diff_setup_tcase(struct torture_context
*tctx
, void **data
)
153 struct registry_context
*r1_ctx
, *r2_ctx
;
156 struct hive_key
*r1_hklm
, *r1_hkcu
;
157 struct hive_key
*r2_hklm
, *r2_hkcu
;
158 const char *filename
;
159 struct diff_tcase_data
*td
;
160 struct registry_key
*key
, *newkey
;
163 td
= talloc(tctx
, struct diff_tcase_data
);
165 /* Create two registry contexts */
166 error
= reg_open_local(tctx
, &r1_ctx
);
167 torture_assert_werr_ok(tctx
, error
, "Opening registry 1 for patch tests failed");
169 error
= reg_open_local(tctx
, &r2_ctx
);
170 torture_assert_werr_ok(tctx
, error
, "Opening registry 2 for patch tests failed");
172 /* Create temp directory */
173 status
= torture_temp_dir(tctx
, "patchfile", &td
->tempdir
);
174 torture_assert_ntstatus_ok(tctx
, status
, "Creating temp dir failed");
176 /* Create and mount HKLM and HKCU hives for registry 1 */
177 filename
= talloc_asprintf(tctx
, "%s/r1_local_machine.ldb", td
->tempdir
);
178 error
= reg_open_ldb_file(tctx
, filename
, NULL
, NULL
, tctx
->ev
, tctx
->lp_ctx
, &r1_hklm
);
179 torture_assert_werr_ok(tctx
, error
, "Opening local machine file failed");
181 error
= reg_mount_hive(r1_ctx
, r1_hklm
, HKEY_LOCAL_MACHINE
, NULL
);
182 torture_assert_werr_ok(tctx
, error
, "Mounting hive failed");
184 filename
= talloc_asprintf(tctx
, "%s/r1_current_user.ldb", td
->tempdir
);
185 error
= reg_open_ldb_file(tctx
, filename
, NULL
, NULL
, tctx
->ev
, tctx
->lp_ctx
, &r1_hkcu
);
186 torture_assert_werr_ok(tctx
, error
, "Opening current user file failed");
188 error
= reg_mount_hive(r1_ctx
, r1_hkcu
, HKEY_CURRENT_USER
, NULL
);
189 torture_assert_werr_ok(tctx
, error
, "Mounting hive failed");
191 /* Create and mount HKLM and HKCU hives for registry 2 */
192 filename
= talloc_asprintf(tctx
, "%s/r2_local_machine.ldb", td
->tempdir
);
193 error
= reg_open_ldb_file(tctx
, filename
, NULL
, NULL
, tctx
->ev
, tctx
->lp_ctx
, &r2_hklm
);
194 torture_assert_werr_ok(tctx
, error
, "Opening local machine file failed");
196 error
= reg_mount_hive(r2_ctx
, r2_hklm
, HKEY_LOCAL_MACHINE
, NULL
);
197 torture_assert_werr_ok(tctx
, error
, "Mounting hive failed");
199 filename
= talloc_asprintf(tctx
, "%s/r2_current_user.ldb", td
->tempdir
);
200 error
= reg_open_ldb_file(tctx
, filename
, NULL
, NULL
, tctx
->ev
, tctx
->lp_ctx
, &r2_hkcu
);
201 torture_assert_werr_ok(tctx
, error
, "Opening current user file failed");
203 error
= reg_mount_hive(r2_ctx
, r2_hkcu
, HKEY_CURRENT_USER
, NULL
);
204 torture_assert_werr_ok(tctx
, error
, "Mounting hive failed");
206 error
= r1_ctx
->ops
->get_predefined_key(r1_ctx
, HKEY_CURRENT_USER
, &key
);
207 torture_assert_werr_ok(tctx
, error
, "Opening HKEY_CURRENT_USER failed");
208 error
= r1_ctx
->ops
->create_key(r1_ctx
, key
, "Network", NULL
, NULL
, &newkey
);
209 torture_assert_werr_ok(tctx
, error
, "Opening HKCU\\Network failed");
210 error
= r1_ctx
->ops
->create_key(r1_ctx
, newkey
, "L", NULL
, NULL
, &newkey
);
211 torture_assert_werr_ok(tctx
, error
, "Opening HKCU\\Network\\L failed");
213 error
= r2_ctx
->ops
->get_predefined_key(r2_ctx
, HKEY_LOCAL_MACHINE
, &key
);
214 torture_assert_werr_ok(tctx
, error
, "Opening HKEY_LOCAL_MACHINE failed");
215 error
= r2_ctx
->ops
->create_key(r2_ctx
, key
, "Software", NULL
, NULL
, &newkey
);
216 torture_assert_werr_ok(tctx
, error
, "Creating HKLM\\Software failed");
217 error
= r2_ctx
->ops
->create_key(r2_ctx
, newkey
, "Microsoft", NULL
, NULL
, &newkey
);
218 torture_assert_werr_ok(tctx
, error
, "Creating HKLM\\Software\\Microsoft failed");
219 error
= r2_ctx
->ops
->create_key(r2_ctx
, newkey
, "Windows", NULL
, NULL
, &newkey
);
220 torture_assert_werr_ok(tctx
, error
, "Creating HKLM\\Software\\Microsoft\\Windows failed");
221 error
= r2_ctx
->ops
->create_key(r2_ctx
, newkey
, "CurrentVersion", NULL
, NULL
, &newkey
);
222 torture_assert_werr_ok(tctx
, error
, "Creating HKLM\\..\\Windows\\CurrentVersion failed");
223 error
= r2_ctx
->ops
->create_key(r2_ctx
, newkey
, "Policies", NULL
, NULL
, &newkey
);
224 torture_assert_werr_ok(tctx
, error
, "Creating HKLM\\..\\CurrentVersion\\Policies failed");
225 error
= r2_ctx
->ops
->create_key(r2_ctx
, newkey
, "Explorer", NULL
, NULL
, &newkey
);
226 torture_assert_werr_ok(tctx
, error
, "Creating HKLM\\..\\Policies\\Explorer failed");
228 blob
.data
= talloc_array(r2_ctx
, uint8_t, 4);
229 /* set "0x03FFFFFF" in little endian format */
230 blob
.data
[0] = 0xFF; blob
.data
[1] = 0xFF;
231 blob
.data
[2] = 0xFF; blob
.data
[3] = 0x03;
234 r1_ctx
->ops
->set_value(newkey
, "NoDrives", REG_DWORD
, blob
);
236 /* Set test case data */
245 static bool diff_setup_preg_tcase (struct torture_context
*tctx
, void **data
)
247 struct diff_tcase_data
*td
;
250 diff_setup_tcase(tctx
, data
);
253 td
->filename
= talloc_asprintf(tctx
, "%s/test.pol", td
->tempdir
);
254 error
= reg_preg_diff_save(tctx
, td
->filename
, &td
->callbacks
,
256 torture_assert_werr_ok(tctx
, error
, "reg_preg_diff_save");
261 static bool diff_setup_dotreg_tcase (struct torture_context
*tctx
, void **data
)
263 struct diff_tcase_data
*td
;
266 diff_setup_tcase(tctx
, data
);
269 td
->filename
= talloc_asprintf(tctx
, "%s/test.reg", td
->tempdir
);
270 error
= reg_dotreg_diff_save(tctx
, td
->filename
, &td
->callbacks
,
272 torture_assert_werr_ok(tctx
, error
, "reg_dotreg_diff_save");
277 struct torture_suite
*torture_registry_diff(TALLOC_CTX
*mem_ctx
)
279 struct torture_tcase
*tcase
;
280 struct torture_suite
*suite
= torture_suite_create(mem_ctx
, "diff");
282 tcase
= torture_suite_add_tcase(suite
, "PReg");
283 torture_tcase_set_fixture(tcase
, diff_setup_preg_tcase
, NULL
);
284 tcase_add_tests(tcase
);
286 tcase
= torture_suite_add_tcase(suite
, "dotreg");
287 torture_tcase_set_fixture(tcase
, diff_setup_dotreg_tcase
, NULL
);
288 tcase_add_tests(tcase
);