1 | n/a | # Test the windows specific win32reg module. |
---|
2 | n/a | # Only win32reg functions not hit here: FlushKey, LoadKey and SaveKey |
---|
3 | n/a | |
---|
4 | n/a | import os, sys, errno |
---|
5 | n/a | import unittest |
---|
6 | n/a | from test import support |
---|
7 | n/a | threading = support.import_module("threading") |
---|
8 | n/a | from platform import machine |
---|
9 | n/a | |
---|
10 | n/a | # Do this first so test will be skipped if module doesn't exist |
---|
11 | n/a | support.import_module('winreg', required_on=['win']) |
---|
12 | n/a | # Now import everything |
---|
13 | n/a | from winreg import * |
---|
14 | n/a | |
---|
15 | n/a | try: |
---|
16 | n/a | REMOTE_NAME = sys.argv[sys.argv.index("--remote")+1] |
---|
17 | n/a | except (IndexError, ValueError): |
---|
18 | n/a | REMOTE_NAME = None |
---|
19 | n/a | |
---|
20 | n/a | # tuple of (major, minor) |
---|
21 | n/a | WIN_VER = sys.getwindowsversion()[:2] |
---|
22 | n/a | # Some tests should only run on 64-bit architectures where WOW64 will be. |
---|
23 | n/a | WIN64_MACHINE = True if machine() == "AMD64" else False |
---|
24 | n/a | |
---|
25 | n/a | # Starting with Windows 7 and Windows Server 2008 R2, WOW64 no longer uses |
---|
26 | n/a | # registry reflection and formerly reflected keys are shared instead. |
---|
27 | n/a | # Windows 7 and Windows Server 2008 R2 are version 6.1. Due to this, some |
---|
28 | n/a | # tests are only valid up until 6.1 |
---|
29 | n/a | HAS_REFLECTION = True if WIN_VER < (6, 1) else False |
---|
30 | n/a | |
---|
31 | n/a | # Use a per-process key to prevent concurrent test runs (buildbot!) from |
---|
32 | n/a | # stomping on each other. |
---|
33 | n/a | test_key_base = "Python Test Key [%d] - Delete Me" % (os.getpid(),) |
---|
34 | n/a | test_key_name = "SOFTWARE\\" + test_key_base |
---|
35 | n/a | # On OS'es that support reflection we should test with a reflected key |
---|
36 | n/a | test_reflect_key_name = "SOFTWARE\\Classes\\" + test_key_base |
---|
37 | n/a | |
---|
38 | n/a | test_data = [ |
---|
39 | n/a | ("Int Value", 45, REG_DWORD), |
---|
40 | n/a | ("Qword Value", 0x1122334455667788, REG_QWORD), |
---|
41 | n/a | ("String Val", "A string value", REG_SZ), |
---|
42 | n/a | ("StringExpand", "The path is %path%", REG_EXPAND_SZ), |
---|
43 | n/a | ("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ), |
---|
44 | n/a | ("Raw Data", b"binary\x00data", REG_BINARY), |
---|
45 | n/a | ("Big String", "x"*(2**14-1), REG_SZ), |
---|
46 | n/a | ("Big Binary", b"x"*(2**14), REG_BINARY), |
---|
47 | n/a | # Two and three kanjis, meaning: "Japan" and "Japanese") |
---|
48 | n/a | ("Japanese æ¥æ¬", "æ¥æ¬èª", REG_SZ), |
---|
49 | n/a | ] |
---|
50 | n/a | |
---|
51 | n/a | class BaseWinregTests(unittest.TestCase): |
---|
52 | n/a | |
---|
53 | n/a | def setUp(self): |
---|
54 | n/a | # Make sure that the test key is absent when the test |
---|
55 | n/a | # starts. |
---|
56 | n/a | self.delete_tree(HKEY_CURRENT_USER, test_key_name) |
---|
57 | n/a | |
---|
58 | n/a | def delete_tree(self, root, subkey): |
---|
59 | n/a | try: |
---|
60 | n/a | hkey = OpenKey(root, subkey, 0, KEY_ALL_ACCESS) |
---|
61 | n/a | except OSError: |
---|
62 | n/a | # subkey does not exist |
---|
63 | n/a | return |
---|
64 | n/a | while True: |
---|
65 | n/a | try: |
---|
66 | n/a | subsubkey = EnumKey(hkey, 0) |
---|
67 | n/a | except OSError: |
---|
68 | n/a | # no more subkeys |
---|
69 | n/a | break |
---|
70 | n/a | self.delete_tree(hkey, subsubkey) |
---|
71 | n/a | CloseKey(hkey) |
---|
72 | n/a | DeleteKey(root, subkey) |
---|
73 | n/a | |
---|
74 | n/a | def _write_test_data(self, root_key, subkeystr="sub_key", |
---|
75 | n/a | CreateKey=CreateKey): |
---|
76 | n/a | # Set the default value for this key. |
---|
77 | n/a | SetValue(root_key, test_key_name, REG_SZ, "Default value") |
---|
78 | n/a | key = CreateKey(root_key, test_key_name) |
---|
79 | n/a | self.assertTrue(key.handle != 0) |
---|
80 | n/a | # Create a sub-key |
---|
81 | n/a | sub_key = CreateKey(key, subkeystr) |
---|
82 | n/a | # Give the sub-key some named values |
---|
83 | n/a | |
---|
84 | n/a | for value_name, value_data, value_type in test_data: |
---|
85 | n/a | SetValueEx(sub_key, value_name, 0, value_type, value_data) |
---|
86 | n/a | |
---|
87 | n/a | # Check we wrote as many items as we thought. |
---|
88 | n/a | nkeys, nvalues, since_mod = QueryInfoKey(key) |
---|
89 | n/a | self.assertEqual(nkeys, 1, "Not the correct number of sub keys") |
---|
90 | n/a | self.assertEqual(nvalues, 1, "Not the correct number of values") |
---|
91 | n/a | nkeys, nvalues, since_mod = QueryInfoKey(sub_key) |
---|
92 | n/a | self.assertEqual(nkeys, 0, "Not the correct number of sub keys") |
---|
93 | n/a | self.assertEqual(nvalues, len(test_data), |
---|
94 | n/a | "Not the correct number of values") |
---|
95 | n/a | # Close this key this way... |
---|
96 | n/a | # (but before we do, copy the key as an integer - this allows |
---|
97 | n/a | # us to test that the key really gets closed). |
---|
98 | n/a | int_sub_key = int(sub_key) |
---|
99 | n/a | CloseKey(sub_key) |
---|
100 | n/a | try: |
---|
101 | n/a | QueryInfoKey(int_sub_key) |
---|
102 | n/a | self.fail("It appears the CloseKey() function does " |
---|
103 | n/a | "not close the actual key!") |
---|
104 | n/a | except OSError: |
---|
105 | n/a | pass |
---|
106 | n/a | # ... and close that key that way :-) |
---|
107 | n/a | int_key = int(key) |
---|
108 | n/a | key.Close() |
---|
109 | n/a | try: |
---|
110 | n/a | QueryInfoKey(int_key) |
---|
111 | n/a | self.fail("It appears the key.Close() function " |
---|
112 | n/a | "does not close the actual key!") |
---|
113 | n/a | except OSError: |
---|
114 | n/a | pass |
---|
115 | n/a | |
---|
116 | n/a | def _read_test_data(self, root_key, subkeystr="sub_key", OpenKey=OpenKey): |
---|
117 | n/a | # Check we can get default value for this key. |
---|
118 | n/a | val = QueryValue(root_key, test_key_name) |
---|
119 | n/a | self.assertEqual(val, "Default value", |
---|
120 | n/a | "Registry didn't give back the correct value") |
---|
121 | n/a | |
---|
122 | n/a | key = OpenKey(root_key, test_key_name) |
---|
123 | n/a | # Read the sub-keys |
---|
124 | n/a | with OpenKey(key, subkeystr) as sub_key: |
---|
125 | n/a | # Check I can enumerate over the values. |
---|
126 | n/a | index = 0 |
---|
127 | n/a | while 1: |
---|
128 | n/a | try: |
---|
129 | n/a | data = EnumValue(sub_key, index) |
---|
130 | n/a | except OSError: |
---|
131 | n/a | break |
---|
132 | n/a | self.assertEqual(data in test_data, True, |
---|
133 | n/a | "Didn't read back the correct test data") |
---|
134 | n/a | index = index + 1 |
---|
135 | n/a | self.assertEqual(index, len(test_data), |
---|
136 | n/a | "Didn't read the correct number of items") |
---|
137 | n/a | # Check I can directly access each item |
---|
138 | n/a | for value_name, value_data, value_type in test_data: |
---|
139 | n/a | read_val, read_typ = QueryValueEx(sub_key, value_name) |
---|
140 | n/a | self.assertEqual(read_val, value_data, |
---|
141 | n/a | "Could not directly read the value") |
---|
142 | n/a | self.assertEqual(read_typ, value_type, |
---|
143 | n/a | "Could not directly read the value") |
---|
144 | n/a | sub_key.Close() |
---|
145 | n/a | # Enumerate our main key. |
---|
146 | n/a | read_val = EnumKey(key, 0) |
---|
147 | n/a | self.assertEqual(read_val, subkeystr, "Read subkey value wrong") |
---|
148 | n/a | try: |
---|
149 | n/a | EnumKey(key, 1) |
---|
150 | n/a | self.fail("Was able to get a second key when I only have one!") |
---|
151 | n/a | except OSError: |
---|
152 | n/a | pass |
---|
153 | n/a | |
---|
154 | n/a | key.Close() |
---|
155 | n/a | |
---|
156 | n/a | def _delete_test_data(self, root_key, subkeystr="sub_key"): |
---|
157 | n/a | key = OpenKey(root_key, test_key_name, 0, KEY_ALL_ACCESS) |
---|
158 | n/a | sub_key = OpenKey(key, subkeystr, 0, KEY_ALL_ACCESS) |
---|
159 | n/a | # It is not necessary to delete the values before deleting |
---|
160 | n/a | # the key (although subkeys must not exist). We delete them |
---|
161 | n/a | # manually just to prove we can :-) |
---|
162 | n/a | for value_name, value_data, value_type in test_data: |
---|
163 | n/a | DeleteValue(sub_key, value_name) |
---|
164 | n/a | |
---|
165 | n/a | nkeys, nvalues, since_mod = QueryInfoKey(sub_key) |
---|
166 | n/a | self.assertEqual(nkeys, 0, "subkey not empty before delete") |
---|
167 | n/a | self.assertEqual(nvalues, 0, "subkey not empty before delete") |
---|
168 | n/a | sub_key.Close() |
---|
169 | n/a | DeleteKey(key, subkeystr) |
---|
170 | n/a | |
---|
171 | n/a | try: |
---|
172 | n/a | # Shouldn't be able to delete it twice! |
---|
173 | n/a | DeleteKey(key, subkeystr) |
---|
174 | n/a | self.fail("Deleting the key twice succeeded") |
---|
175 | n/a | except OSError: |
---|
176 | n/a | pass |
---|
177 | n/a | key.Close() |
---|
178 | n/a | DeleteKey(root_key, test_key_name) |
---|
179 | n/a | # Opening should now fail! |
---|
180 | n/a | try: |
---|
181 | n/a | key = OpenKey(root_key, test_key_name) |
---|
182 | n/a | self.fail("Could open the non-existent key") |
---|
183 | n/a | except OSError: # Use this error name this time |
---|
184 | n/a | pass |
---|
185 | n/a | |
---|
186 | n/a | def _test_all(self, root_key, subkeystr="sub_key"): |
---|
187 | n/a | self._write_test_data(root_key, subkeystr) |
---|
188 | n/a | self._read_test_data(root_key, subkeystr) |
---|
189 | n/a | self._delete_test_data(root_key, subkeystr) |
---|
190 | n/a | |
---|
191 | n/a | def _test_named_args(self, key, sub_key): |
---|
192 | n/a | with CreateKeyEx(key=key, sub_key=sub_key, reserved=0, |
---|
193 | n/a | access=KEY_ALL_ACCESS) as ckey: |
---|
194 | n/a | self.assertTrue(ckey.handle != 0) |
---|
195 | n/a | |
---|
196 | n/a | with OpenKeyEx(key=key, sub_key=sub_key, reserved=0, |
---|
197 | n/a | access=KEY_ALL_ACCESS) as okey: |
---|
198 | n/a | self.assertTrue(okey.handle != 0) |
---|
199 | n/a | |
---|
200 | n/a | |
---|
201 | n/a | class LocalWinregTests(BaseWinregTests): |
---|
202 | n/a | |
---|
203 | n/a | def test_registry_works(self): |
---|
204 | n/a | self._test_all(HKEY_CURRENT_USER) |
---|
205 | n/a | self._test_all(HKEY_CURRENT_USER, "æ¥æ¬-subkey") |
---|
206 | n/a | |
---|
207 | n/a | def test_registry_works_extended_functions(self): |
---|
208 | n/a | # Substitute the regular CreateKey and OpenKey calls with their |
---|
209 | n/a | # extended counterparts. |
---|
210 | n/a | # Note: DeleteKeyEx is not used here because it is platform dependent |
---|
211 | n/a | cke = lambda key, sub_key: CreateKeyEx(key, sub_key, 0, KEY_ALL_ACCESS) |
---|
212 | n/a | self._write_test_data(HKEY_CURRENT_USER, CreateKey=cke) |
---|
213 | n/a | |
---|
214 | n/a | oke = lambda key, sub_key: OpenKeyEx(key, sub_key, 0, KEY_READ) |
---|
215 | n/a | self._read_test_data(HKEY_CURRENT_USER, OpenKey=oke) |
---|
216 | n/a | |
---|
217 | n/a | self._delete_test_data(HKEY_CURRENT_USER) |
---|
218 | n/a | |
---|
219 | n/a | def test_named_arguments(self): |
---|
220 | n/a | self._test_named_args(HKEY_CURRENT_USER, test_key_name) |
---|
221 | n/a | # Use the regular DeleteKey to clean up |
---|
222 | n/a | # DeleteKeyEx takes named args and is tested separately |
---|
223 | n/a | DeleteKey(HKEY_CURRENT_USER, test_key_name) |
---|
224 | n/a | |
---|
225 | n/a | def test_connect_registry_to_local_machine_works(self): |
---|
226 | n/a | # perform minimal ConnectRegistry test which just invokes it |
---|
227 | n/a | h = ConnectRegistry(None, HKEY_LOCAL_MACHINE) |
---|
228 | n/a | self.assertNotEqual(h.handle, 0) |
---|
229 | n/a | h.Close() |
---|
230 | n/a | self.assertEqual(h.handle, 0) |
---|
231 | n/a | |
---|
232 | n/a | def test_inexistant_remote_registry(self): |
---|
233 | n/a | connect = lambda: ConnectRegistry("abcdefghijkl", HKEY_CURRENT_USER) |
---|
234 | n/a | self.assertRaises(OSError, connect) |
---|
235 | n/a | |
---|
236 | n/a | def testExpandEnvironmentStrings(self): |
---|
237 | n/a | r = ExpandEnvironmentStrings("%windir%\\test") |
---|
238 | n/a | self.assertEqual(type(r), str) |
---|
239 | n/a | self.assertEqual(r, os.environ["windir"] + "\\test") |
---|
240 | n/a | |
---|
241 | n/a | def test_context_manager(self): |
---|
242 | n/a | # ensure that the handle is closed if an exception occurs |
---|
243 | n/a | try: |
---|
244 | n/a | with ConnectRegistry(None, HKEY_LOCAL_MACHINE) as h: |
---|
245 | n/a | self.assertNotEqual(h.handle, 0) |
---|
246 | n/a | raise OSError |
---|
247 | n/a | except OSError: |
---|
248 | n/a | self.assertEqual(h.handle, 0) |
---|
249 | n/a | |
---|
250 | n/a | def test_changing_value(self): |
---|
251 | n/a | # Issue2810: A race condition in 2.6 and 3.1 may cause |
---|
252 | n/a | # EnumValue or QueryValue to raise "WindowsError: More data is |
---|
253 | n/a | # available" |
---|
254 | n/a | done = False |
---|
255 | n/a | |
---|
256 | n/a | class VeryActiveThread(threading.Thread): |
---|
257 | n/a | def run(self): |
---|
258 | n/a | with CreateKey(HKEY_CURRENT_USER, test_key_name) as key: |
---|
259 | n/a | use_short = True |
---|
260 | n/a | long_string = 'x'*2000 |
---|
261 | n/a | while not done: |
---|
262 | n/a | s = 'x' if use_short else long_string |
---|
263 | n/a | use_short = not use_short |
---|
264 | n/a | SetValue(key, 'changing_value', REG_SZ, s) |
---|
265 | n/a | |
---|
266 | n/a | thread = VeryActiveThread() |
---|
267 | n/a | thread.start() |
---|
268 | n/a | try: |
---|
269 | n/a | with CreateKey(HKEY_CURRENT_USER, |
---|
270 | n/a | test_key_name+'\\changing_value') as key: |
---|
271 | n/a | for _ in range(1000): |
---|
272 | n/a | num_subkeys, num_values, t = QueryInfoKey(key) |
---|
273 | n/a | for i in range(num_values): |
---|
274 | n/a | name = EnumValue(key, i) |
---|
275 | n/a | QueryValue(key, name[0]) |
---|
276 | n/a | finally: |
---|
277 | n/a | done = True |
---|
278 | n/a | thread.join() |
---|
279 | n/a | DeleteKey(HKEY_CURRENT_USER, test_key_name+'\\changing_value') |
---|
280 | n/a | DeleteKey(HKEY_CURRENT_USER, test_key_name) |
---|
281 | n/a | |
---|
282 | n/a | def test_long_key(self): |
---|
283 | n/a | # Issue2810, in 2.6 and 3.1 when the key name was exactly 256 |
---|
284 | n/a | # characters, EnumKey raised "WindowsError: More data is |
---|
285 | n/a | # available" |
---|
286 | n/a | name = 'x'*256 |
---|
287 | n/a | try: |
---|
288 | n/a | with CreateKey(HKEY_CURRENT_USER, test_key_name) as key: |
---|
289 | n/a | SetValue(key, name, REG_SZ, 'x') |
---|
290 | n/a | num_subkeys, num_values, t = QueryInfoKey(key) |
---|
291 | n/a | EnumKey(key, 0) |
---|
292 | n/a | finally: |
---|
293 | n/a | DeleteKey(HKEY_CURRENT_USER, '\\'.join((test_key_name, name))) |
---|
294 | n/a | DeleteKey(HKEY_CURRENT_USER, test_key_name) |
---|
295 | n/a | |
---|
296 | n/a | def test_dynamic_key(self): |
---|
297 | n/a | # Issue2810, when the value is dynamically generated, these |
---|
298 | n/a | # raise "WindowsError: More data is available" in 2.6 and 3.1 |
---|
299 | n/a | try: |
---|
300 | n/a | EnumValue(HKEY_PERFORMANCE_DATA, 0) |
---|
301 | n/a | except OSError as e: |
---|
302 | n/a | if e.errno in (errno.EPERM, errno.EACCES): |
---|
303 | n/a | self.skipTest("access denied to registry key " |
---|
304 | n/a | "(are you running in a non-interactive session?)") |
---|
305 | n/a | raise |
---|
306 | n/a | QueryValueEx(HKEY_PERFORMANCE_DATA, "") |
---|
307 | n/a | |
---|
308 | n/a | # Reflection requires XP x64/Vista at a minimum. XP doesn't have this stuff |
---|
309 | n/a | # or DeleteKeyEx so make sure their use raises NotImplementedError |
---|
310 | n/a | @unittest.skipUnless(WIN_VER < (5, 2), "Requires Windows XP") |
---|
311 | n/a | def test_reflection_unsupported(self): |
---|
312 | n/a | try: |
---|
313 | n/a | with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck: |
---|
314 | n/a | self.assertNotEqual(ck.handle, 0) |
---|
315 | n/a | |
---|
316 | n/a | key = OpenKey(HKEY_CURRENT_USER, test_key_name) |
---|
317 | n/a | self.assertNotEqual(key.handle, 0) |
---|
318 | n/a | |
---|
319 | n/a | with self.assertRaises(NotImplementedError): |
---|
320 | n/a | DisableReflectionKey(key) |
---|
321 | n/a | with self.assertRaises(NotImplementedError): |
---|
322 | n/a | EnableReflectionKey(key) |
---|
323 | n/a | with self.assertRaises(NotImplementedError): |
---|
324 | n/a | QueryReflectionKey(key) |
---|
325 | n/a | with self.assertRaises(NotImplementedError): |
---|
326 | n/a | DeleteKeyEx(HKEY_CURRENT_USER, test_key_name) |
---|
327 | n/a | finally: |
---|
328 | n/a | DeleteKey(HKEY_CURRENT_USER, test_key_name) |
---|
329 | n/a | |
---|
330 | n/a | def test_setvalueex_value_range(self): |
---|
331 | n/a | # Test for Issue #14420, accept proper ranges for SetValueEx. |
---|
332 | n/a | # Py2Reg, which gets called by SetValueEx, was using PyLong_AsLong, |
---|
333 | n/a | # thus raising OverflowError. The implementation now uses |
---|
334 | n/a | # PyLong_AsUnsignedLong to match DWORD's size. |
---|
335 | n/a | try: |
---|
336 | n/a | with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck: |
---|
337 | n/a | self.assertNotEqual(ck.handle, 0) |
---|
338 | n/a | SetValueEx(ck, "test_name", None, REG_DWORD, 0x80000000) |
---|
339 | n/a | finally: |
---|
340 | n/a | DeleteKey(HKEY_CURRENT_USER, test_key_name) |
---|
341 | n/a | |
---|
342 | n/a | def test_queryvalueex_return_value(self): |
---|
343 | n/a | # Test for Issue #16759, return unsigned int from QueryValueEx. |
---|
344 | n/a | # Reg2Py, which gets called by QueryValueEx, was returning a value |
---|
345 | n/a | # generated by PyLong_FromLong. The implementation now uses |
---|
346 | n/a | # PyLong_FromUnsignedLong to match DWORD's size. |
---|
347 | n/a | try: |
---|
348 | n/a | with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck: |
---|
349 | n/a | self.assertNotEqual(ck.handle, 0) |
---|
350 | n/a | test_val = 0x80000000 |
---|
351 | n/a | SetValueEx(ck, "test_name", None, REG_DWORD, test_val) |
---|
352 | n/a | ret_val, ret_type = QueryValueEx(ck, "test_name") |
---|
353 | n/a | self.assertEqual(ret_type, REG_DWORD) |
---|
354 | n/a | self.assertEqual(ret_val, test_val) |
---|
355 | n/a | finally: |
---|
356 | n/a | DeleteKey(HKEY_CURRENT_USER, test_key_name) |
---|
357 | n/a | |
---|
358 | n/a | def test_setvalueex_crash_with_none_arg(self): |
---|
359 | n/a | # Test for Issue #21151, segfault when None is passed to SetValueEx |
---|
360 | n/a | try: |
---|
361 | n/a | with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck: |
---|
362 | n/a | self.assertNotEqual(ck.handle, 0) |
---|
363 | n/a | test_val = None |
---|
364 | n/a | SetValueEx(ck, "test_name", 0, REG_BINARY, test_val) |
---|
365 | n/a | ret_val, ret_type = QueryValueEx(ck, "test_name") |
---|
366 | n/a | self.assertEqual(ret_type, REG_BINARY) |
---|
367 | n/a | self.assertEqual(ret_val, test_val) |
---|
368 | n/a | finally: |
---|
369 | n/a | DeleteKey(HKEY_CURRENT_USER, test_key_name) |
---|
370 | n/a | |
---|
371 | n/a | def test_read_string_containing_null(self): |
---|
372 | n/a | # Test for issue 25778: REG_SZ should not contain null characters |
---|
373 | n/a | try: |
---|
374 | n/a | with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck: |
---|
375 | n/a | self.assertNotEqual(ck.handle, 0) |
---|
376 | n/a | test_val = "A string\x00 with a null" |
---|
377 | n/a | SetValueEx(ck, "test_name", 0, REG_SZ, test_val) |
---|
378 | n/a | ret_val, ret_type = QueryValueEx(ck, "test_name") |
---|
379 | n/a | self.assertEqual(ret_type, REG_SZ) |
---|
380 | n/a | self.assertEqual(ret_val, "A string") |
---|
381 | n/a | finally: |
---|
382 | n/a | DeleteKey(HKEY_CURRENT_USER, test_key_name) |
---|
383 | n/a | |
---|
384 | n/a | |
---|
385 | n/a | @unittest.skipUnless(REMOTE_NAME, "Skipping remote registry tests") |
---|
386 | n/a | class RemoteWinregTests(BaseWinregTests): |
---|
387 | n/a | |
---|
388 | n/a | def test_remote_registry_works(self): |
---|
389 | n/a | remote_key = ConnectRegistry(REMOTE_NAME, HKEY_CURRENT_USER) |
---|
390 | n/a | self._test_all(remote_key) |
---|
391 | n/a | |
---|
392 | n/a | |
---|
393 | n/a | @unittest.skipUnless(WIN64_MACHINE, "x64 specific registry tests") |
---|
394 | n/a | class Win64WinregTests(BaseWinregTests): |
---|
395 | n/a | |
---|
396 | n/a | def test_named_arguments(self): |
---|
397 | n/a | self._test_named_args(HKEY_CURRENT_USER, test_key_name) |
---|
398 | n/a | # Clean up and also exercise the named arguments |
---|
399 | n/a | DeleteKeyEx(key=HKEY_CURRENT_USER, sub_key=test_key_name, |
---|
400 | n/a | access=KEY_ALL_ACCESS, reserved=0) |
---|
401 | n/a | |
---|
402 | n/a | def test_reflection_functions(self): |
---|
403 | n/a | # Test that we can call the query, enable, and disable functions |
---|
404 | n/a | # on a key which isn't on the reflection list with no consequences. |
---|
405 | n/a | with OpenKey(HKEY_LOCAL_MACHINE, "Software") as key: |
---|
406 | n/a | # HKLM\Software is redirected but not reflected in all OSes |
---|
407 | n/a | self.assertTrue(QueryReflectionKey(key)) |
---|
408 | n/a | self.assertIsNone(EnableReflectionKey(key)) |
---|
409 | n/a | self.assertIsNone(DisableReflectionKey(key)) |
---|
410 | n/a | self.assertTrue(QueryReflectionKey(key)) |
---|
411 | n/a | |
---|
412 | n/a | @unittest.skipUnless(HAS_REFLECTION, "OS doesn't support reflection") |
---|
413 | n/a | def test_reflection(self): |
---|
414 | n/a | # Test that we can create, open, and delete keys in the 32-bit |
---|
415 | n/a | # area. Because we are doing this in a key which gets reflected, |
---|
416 | n/a | # test the differences of 32 and 64-bit keys before and after the |
---|
417 | n/a | # reflection occurs (ie. when the created key is closed). |
---|
418 | n/a | try: |
---|
419 | n/a | with CreateKeyEx(HKEY_CURRENT_USER, test_reflect_key_name, 0, |
---|
420 | n/a | KEY_ALL_ACCESS | KEY_WOW64_32KEY) as created_key: |
---|
421 | n/a | self.assertNotEqual(created_key.handle, 0) |
---|
422 | n/a | |
---|
423 | n/a | # The key should now be available in the 32-bit area |
---|
424 | n/a | with OpenKey(HKEY_CURRENT_USER, test_reflect_key_name, 0, |
---|
425 | n/a | KEY_ALL_ACCESS | KEY_WOW64_32KEY) as key: |
---|
426 | n/a | self.assertNotEqual(key.handle, 0) |
---|
427 | n/a | |
---|
428 | n/a | # Write a value to what currently is only in the 32-bit area |
---|
429 | n/a | SetValueEx(created_key, "", 0, REG_SZ, "32KEY") |
---|
430 | n/a | |
---|
431 | n/a | # The key is not reflected until created_key is closed. |
---|
432 | n/a | # The 64-bit version of the key should not be available yet. |
---|
433 | n/a | open_fail = lambda: OpenKey(HKEY_CURRENT_USER, |
---|
434 | n/a | test_reflect_key_name, 0, |
---|
435 | n/a | KEY_READ | KEY_WOW64_64KEY) |
---|
436 | n/a | self.assertRaises(OSError, open_fail) |
---|
437 | n/a | |
---|
438 | n/a | # Now explicitly open the 64-bit version of the key |
---|
439 | n/a | with OpenKey(HKEY_CURRENT_USER, test_reflect_key_name, 0, |
---|
440 | n/a | KEY_ALL_ACCESS | KEY_WOW64_64KEY) as key: |
---|
441 | n/a | self.assertNotEqual(key.handle, 0) |
---|
442 | n/a | # Make sure the original value we set is there |
---|
443 | n/a | self.assertEqual("32KEY", QueryValue(key, "")) |
---|
444 | n/a | # Set a new value, which will get reflected to 32-bit |
---|
445 | n/a | SetValueEx(key, "", 0, REG_SZ, "64KEY") |
---|
446 | n/a | |
---|
447 | n/a | # Reflection uses a "last-writer wins policy, so the value we set |
---|
448 | n/a | # on the 64-bit key should be the same on 32-bit |
---|
449 | n/a | with OpenKey(HKEY_CURRENT_USER, test_reflect_key_name, 0, |
---|
450 | n/a | KEY_READ | KEY_WOW64_32KEY) as key: |
---|
451 | n/a | self.assertEqual("64KEY", QueryValue(key, "")) |
---|
452 | n/a | finally: |
---|
453 | n/a | DeleteKeyEx(HKEY_CURRENT_USER, test_reflect_key_name, |
---|
454 | n/a | KEY_WOW64_32KEY, 0) |
---|
455 | n/a | |
---|
456 | n/a | @unittest.skipUnless(HAS_REFLECTION, "OS doesn't support reflection") |
---|
457 | n/a | def test_disable_reflection(self): |
---|
458 | n/a | # Make use of a key which gets redirected and reflected |
---|
459 | n/a | try: |
---|
460 | n/a | with CreateKeyEx(HKEY_CURRENT_USER, test_reflect_key_name, 0, |
---|
461 | n/a | KEY_ALL_ACCESS | KEY_WOW64_32KEY) as created_key: |
---|
462 | n/a | # QueryReflectionKey returns whether or not the key is disabled |
---|
463 | n/a | disabled = QueryReflectionKey(created_key) |
---|
464 | n/a | self.assertEqual(type(disabled), bool) |
---|
465 | n/a | # HKCU\Software\Classes is reflected by default |
---|
466 | n/a | self.assertFalse(disabled) |
---|
467 | n/a | |
---|
468 | n/a | DisableReflectionKey(created_key) |
---|
469 | n/a | self.assertTrue(QueryReflectionKey(created_key)) |
---|
470 | n/a | |
---|
471 | n/a | # The key is now closed and would normally be reflected to the |
---|
472 | n/a | # 64-bit area, but let's make sure that didn't happen. |
---|
473 | n/a | open_fail = lambda: OpenKeyEx(HKEY_CURRENT_USER, |
---|
474 | n/a | test_reflect_key_name, 0, |
---|
475 | n/a | KEY_READ | KEY_WOW64_64KEY) |
---|
476 | n/a | self.assertRaises(OSError, open_fail) |
---|
477 | n/a | |
---|
478 | n/a | # Make sure the 32-bit key is actually there |
---|
479 | n/a | with OpenKeyEx(HKEY_CURRENT_USER, test_reflect_key_name, 0, |
---|
480 | n/a | KEY_READ | KEY_WOW64_32KEY) as key: |
---|
481 | n/a | self.assertNotEqual(key.handle, 0) |
---|
482 | n/a | finally: |
---|
483 | n/a | DeleteKeyEx(HKEY_CURRENT_USER, test_reflect_key_name, |
---|
484 | n/a | KEY_WOW64_32KEY, 0) |
---|
485 | n/a | |
---|
486 | n/a | def test_exception_numbers(self): |
---|
487 | n/a | with self.assertRaises(FileNotFoundError) as ctx: |
---|
488 | n/a | QueryValue(HKEY_CLASSES_ROOT, 'some_value_that_does_not_exist') |
---|
489 | n/a | |
---|
490 | n/a | def test_main(): |
---|
491 | n/a | support.run_unittest(LocalWinregTests, RemoteWinregTests, |
---|
492 | n/a | Win64WinregTests) |
---|
493 | n/a | |
---|
494 | n/a | if __name__ == "__main__": |
---|
495 | n/a | if not REMOTE_NAME: |
---|
496 | n/a | print("Remote registry calls can be tested using", |
---|
497 | n/a | "'test_winreg.py --remote \\\\machine_name'") |
---|
498 | n/a | test_main() |
---|