blob: 8e3cdf3637011bf18a18e2c88b2816cffe590e7e [file] [log] [blame]
Guido van Rossumde598552000-03-28 20:36:51 +00001# Test the windows specific win32reg module.
2# Only win32reg functions not hit here: FlushKey, LoadKey and SaveKey
3
Antoine Pitrouf5262972012-07-13 22:46:41 +02004import os, sys, errno
Georg Brandl3376a9a2007-08-24 17:38:49 +00005import unittest
Georg Brandl3376a9a2007-08-24 17:38:49 +00006from test import test_support
Brian Curtinb64c89b2010-05-25 15:06:15 +00007threading = test_support.import_module("threading")
Brian Curtine33fa882010-04-02 21:18:14 +00008from platform import machine
Fredrik Lundhf7850422001-01-17 21:51:36 +00009
R. David Murray597ebab2009-03-31 18:32:17 +000010# Do this first so test will be skipped if module doesn't exist
R. David Murray59beec32009-03-30 19:04:00 +000011test_support.import_module('_winreg')
R. David Murray597ebab2009-03-31 18:32:17 +000012# Now import everything
R. David Murray59beec32009-03-30 19:04:00 +000013from _winreg import *
14
Brian Curtine33fa882010-04-02 21:18:14 +000015try:
16 REMOTE_NAME = sys.argv[sys.argv.index("--remote")+1]
17except (IndexError, ValueError):
18 REMOTE_NAME = None
19
20# tuple of (major, minor)
21WIN_VER = sys.getwindowsversion()[:2]
22# Some tests should only run on 64-bit architectures where WOW64 will be.
23WIN64_MACHINE = True if machine() == "AMD64" else False
24
25# Starting with Windows 7 and Windows Server 2008 R2, WOW64 no longer uses
26# registry reflection and formerly reflected keys are shared instead.
27# Windows 7 and Windows Server 2008 R2 are version 6.1. Due to this, some
28# tests are only valid up until 6.1
29HAS_REFLECTION = True if WIN_VER < (6, 1) else False
30
R David Murray5cbff512013-04-21 10:13:43 -040031# Use a per-process key to prevent concurrent test runs (buildbot!) from
32# stomping on each other.
33test_key_base = "Python Test Key [%d] - Delete Me" % (os.getpid(),)
34test_key_name = "SOFTWARE\\" + test_key_base
Brian Curtine33fa882010-04-02 21:18:14 +000035# On OS'es that support reflection we should test with a reflected key
R David Murray5cbff512013-04-21 10:13:43 -040036test_reflect_key_name = "SOFTWARE\\Classes\\" + test_key_base
Guido van Rossumde598552000-03-28 20:36:51 +000037
38test_data = [
39 ("Int Value", 45, REG_DWORD),
Guido van Rossum0a185522003-11-30 22:46:18 +000040 ("String Val", "A string value", REG_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000041 ("StringExpand", "The path is %path%", REG_EXPAND_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000042 ("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000043 ("Raw Data", ("binary"+chr(0)+"data"), REG_BINARY),
Guido van Rossum291481b2003-12-03 15:24:02 +000044 ("Big String", "x"*(2**14-1), REG_SZ),
45 ("Big Binary", "x"*(2**14), REG_BINARY),
Guido van Rossumde598552000-03-28 20:36:51 +000046]
Georg Brandl3376a9a2007-08-24 17:38:49 +000047
48if test_support.have_unicode:
49 test_data += [
50 (unicode("Unicode Val"), unicode("A Unicode value"), REG_SZ,),
51 ("UnicodeExpand", unicode("The path is %path%"), REG_EXPAND_SZ),
52 ("Multi-unicode", [unicode("Lots"), unicode("of"), unicode("unicode"),
53 unicode("values")], REG_MULTI_SZ),
54 ("Multi-mixed", [unicode("Unicode"), unicode("and"), "string",
55 "values"], REG_MULTI_SZ),
Martin v. Löwis339d0f72001-08-17 18:39:25 +000056 ]
Guido van Rossumde598552000-03-28 20:36:51 +000057
Brian Curtine33fa882010-04-02 21:18:14 +000058class BaseWinregTests(unittest.TestCase):
Guido van Rossumde598552000-03-28 20:36:51 +000059
Benjamin Peterson64092a52009-06-07 23:12:44 +000060 def setUp(self):
61 # Make sure that the test key is absent when the test
62 # starts.
63 self.delete_tree(HKEY_CURRENT_USER, test_key_name)
64
65 def delete_tree(self, root, subkey):
66 try:
67 hkey = OpenKey(root, subkey, KEY_ALL_ACCESS)
68 except WindowsError:
69 # subkey does not exist
70 return
71 while True:
72 try:
73 subsubkey = EnumKey(hkey, 0)
74 except WindowsError:
75 # no more subkeys
76 break
77 self.delete_tree(hkey, subsubkey)
78 CloseKey(hkey)
79 DeleteKey(root, subkey)
80
Brian Curtine33fa882010-04-02 21:18:14 +000081 def _write_test_data(self, root_key, CreateKey=CreateKey):
Georg Brandl3376a9a2007-08-24 17:38:49 +000082 # Set the default value for this key.
83 SetValue(root_key, test_key_name, REG_SZ, "Default value")
84 key = CreateKey(root_key, test_key_name)
85 # Create a sub-key
86 sub_key = CreateKey(key, "sub_key")
87 # Give the sub-key some named values
88
89 for value_name, value_data, value_type in test_data:
90 SetValueEx(sub_key, value_name, 0, value_type, value_data)
91
92 # Check we wrote as many items as we thought.
93 nkeys, nvalues, since_mod = QueryInfoKey(key)
Ezio Melotti2623a372010-11-21 13:34:58 +000094 self.assertEqual(nkeys, 1, "Not the correct number of sub keys")
95 self.assertEqual(nvalues, 1, "Not the correct number of values")
Georg Brandl3376a9a2007-08-24 17:38:49 +000096 nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
Ezio Melotti2623a372010-11-21 13:34:58 +000097 self.assertEqual(nkeys, 0, "Not the correct number of sub keys")
98 self.assertEqual(nvalues, len(test_data),
Georg Brandl3376a9a2007-08-24 17:38:49 +000099 "Not the correct number of values")
100 # Close this key this way...
101 # (but before we do, copy the key as an integer - this allows
102 # us to test that the key really gets closed).
103 int_sub_key = int(sub_key)
104 CloseKey(sub_key)
105 try:
106 QueryInfoKey(int_sub_key)
107 self.fail("It appears the CloseKey() function does "
108 "not close the actual key!")
109 except EnvironmentError:
110 pass
111 # ... and close that key that way :-)
112 int_key = int(key)
113 key.Close()
114 try:
115 QueryInfoKey(int_key)
116 self.fail("It appears the key.Close() function "
117 "does not close the actual key!")
118 except EnvironmentError:
119 pass
120
Brian Curtine33fa882010-04-02 21:18:14 +0000121 def _read_test_data(self, root_key, OpenKey=OpenKey):
Georg Brandl3376a9a2007-08-24 17:38:49 +0000122 # Check we can get default value for this key.
123 val = QueryValue(root_key, test_key_name)
Ezio Melotti2623a372010-11-21 13:34:58 +0000124 self.assertEqual(val, "Default value",
125 "Registry didn't give back the correct value")
Georg Brandl3376a9a2007-08-24 17:38:49 +0000126
127 key = OpenKey(root_key, test_key_name)
128 # Read the sub-keys
Christian Heimesb39a7562008-01-08 15:46:10 +0000129 with OpenKey(key, "sub_key") as sub_key:
130 # Check I can enumerate over the values.
131 index = 0
132 while 1:
133 try:
134 data = EnumValue(sub_key, index)
135 except EnvironmentError:
136 break
Ezio Melottiaa980582010-01-23 23:04:36 +0000137 self.assertIn(data, test_data,
138 "Didn't read back the correct test data")
Christian Heimesb39a7562008-01-08 15:46:10 +0000139 index = index + 1
Ezio Melotti2623a372010-11-21 13:34:58 +0000140 self.assertEqual(index, len(test_data),
141 "Didn't read the correct number of items")
Christian Heimesb39a7562008-01-08 15:46:10 +0000142 # Check I can directly access each item
143 for value_name, value_data, value_type in test_data:
144 read_val, read_typ = QueryValueEx(sub_key, value_name)
Ezio Melotti2623a372010-11-21 13:34:58 +0000145 self.assertEqual(read_val, value_data,
146 "Could not directly read the value")
147 self.assertEqual(read_typ, value_type,
148 "Could not directly read the value")
Georg Brandl3376a9a2007-08-24 17:38:49 +0000149 sub_key.Close()
150 # Enumerate our main key.
151 read_val = EnumKey(key, 0)
Ezio Melotti2623a372010-11-21 13:34:58 +0000152 self.assertEqual(read_val, "sub_key", "Read subkey value wrong")
Georg Brandl3376a9a2007-08-24 17:38:49 +0000153 try:
154 EnumKey(key, 1)
155 self.fail("Was able to get a second key when I only have one!")
156 except EnvironmentError:
157 pass
158
159 key.Close()
160
Brian Curtine33fa882010-04-02 21:18:14 +0000161 def _delete_test_data(self, root_key):
Georg Brandl3376a9a2007-08-24 17:38:49 +0000162 key = OpenKey(root_key, test_key_name, 0, KEY_ALL_ACCESS)
163 sub_key = OpenKey(key, "sub_key", 0, KEY_ALL_ACCESS)
164 # It is not necessary to delete the values before deleting
165 # the key (although subkeys must not exist). We delete them
166 # manually just to prove we can :-)
167 for value_name, value_data, value_type in test_data:
168 DeleteValue(sub_key, value_name)
169
170 nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
Ezio Melotti2623a372010-11-21 13:34:58 +0000171 self.assertEqual(nkeys, 0, "subkey not empty before delete")
172 self.assertEqual(nvalues, 0, "subkey not empty before delete")
Georg Brandl3376a9a2007-08-24 17:38:49 +0000173 sub_key.Close()
174 DeleteKey(key, "sub_key")
175
176 try:
177 # Shouldnt be able to delete it twice!
178 DeleteKey(key, "sub_key")
179 self.fail("Deleting the key twice succeeded")
180 except EnvironmentError:
181 pass
182 key.Close()
183 DeleteKey(root_key, test_key_name)
184 # Opening should now fail!
185 try:
186 key = OpenKey(root_key, test_key_name)
187 self.fail("Could open the non-existent key")
188 except WindowsError: # Use this error name this time
189 pass
190
Brian Curtine33fa882010-04-02 21:18:14 +0000191 def _test_all(self, root_key):
192 self._write_test_data(root_key)
193 self._read_test_data(root_key)
194 self._delete_test_data(root_key)
Georg Brandl3376a9a2007-08-24 17:38:49 +0000195
Brian Curtine33fa882010-04-02 21:18:14 +0000196class LocalWinregTests(BaseWinregTests):
Georg Brandl3376a9a2007-08-24 17:38:49 +0000197
Brian Curtine33fa882010-04-02 21:18:14 +0000198 def test_registry_works(self):
199 self._test_all(HKEY_CURRENT_USER)
200
201 def test_registry_works_extended_functions(self):
202 # Substitute the regular CreateKey and OpenKey calls with their
203 # extended counterparts.
204 # Note: DeleteKeyEx is not used here because it is platform dependent
205 cke = lambda key, sub_key: CreateKeyEx(key, sub_key, 0, KEY_ALL_ACCESS)
206 self._write_test_data(HKEY_CURRENT_USER, cke)
207
208 oke = lambda key, sub_key: OpenKeyEx(key, sub_key, 0, KEY_READ)
209 self._read_test_data(HKEY_CURRENT_USER, oke)
210
211 self._delete_test_data(HKEY_CURRENT_USER)
212
213 def test_connect_registry_to_local_machine_works(self):
Georg Brandl3376a9a2007-08-24 17:38:49 +0000214 # perform minimal ConnectRegistry test which just invokes it
215 h = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
Brian Curtine33fa882010-04-02 21:18:14 +0000216 self.assertNotEqual(h.handle, 0)
Georg Brandl3376a9a2007-08-24 17:38:49 +0000217 h.Close()
Brian Curtine33fa882010-04-02 21:18:14 +0000218 self.assertEqual(h.handle, 0)
Georg Brandl3376a9a2007-08-24 17:38:49 +0000219
Brian Curtine33fa882010-04-02 21:18:14 +0000220 def test_inexistant_remote_registry(self):
221 connect = lambda: ConnectRegistry("abcdefghijkl", HKEY_CURRENT_USER)
222 self.assertRaises(WindowsError, connect)
Georg Brandl3376a9a2007-08-24 17:38:49 +0000223
Brian Curtine33fa882010-04-02 21:18:14 +0000224 def test_expand_environment_strings(self):
Christian Heimesb39a7562008-01-08 15:46:10 +0000225 r = ExpandEnvironmentStrings(u"%windir%\\test")
226 self.assertEqual(type(r), unicode)
227 self.assertEqual(r, os.environ["windir"] + "\\test")
228
Brian Curtine33fa882010-04-02 21:18:14 +0000229 def test_context_manager(self):
230 # ensure that the handle is closed if an exception occurs
231 try:
232 with ConnectRegistry(None, HKEY_LOCAL_MACHINE) as h:
233 self.assertNotEqual(h.handle, 0)
234 raise WindowsError
235 except WindowsError:
236 self.assertEqual(h.handle, 0)
237
Brian Curtinb64c89b2010-05-25 15:06:15 +0000238 def test_changing_value(self):
239 # Issue2810: A race condition in 2.6 and 3.1 may cause
Andrew Svetlov4bb142b2012-12-18 21:27:37 +0200240 # EnumValue or QueryValue to raise "WindowsError: More data is
Brian Curtinb64c89b2010-05-25 15:06:15 +0000241 # available"
242 done = False
243
244 class VeryActiveThread(threading.Thread):
245 def run(self):
246 with CreateKey(HKEY_CURRENT_USER, test_key_name) as key:
247 use_short = True
248 long_string = 'x'*2000
249 while not done:
250 s = 'x' if use_short else long_string
251 use_short = not use_short
252 SetValue(key, 'changing_value', REG_SZ, s)
253
254 thread = VeryActiveThread()
255 thread.start()
256 try:
257 with CreateKey(HKEY_CURRENT_USER,
258 test_key_name+'\\changing_value') as key:
259 for _ in range(1000):
260 num_subkeys, num_values, t = QueryInfoKey(key)
261 for i in range(num_values):
262 name = EnumValue(key, i)
263 QueryValue(key, name[0])
264 finally:
265 done = True
266 thread.join()
Hirokazu Yamamotoecdead62010-10-22 11:47:07 +0000267 with OpenKey(HKEY_CURRENT_USER, test_key_name, 0, KEY_ALL_ACCESS) as key:
268 DeleteKey(key, 'changing_value')
Brian Curtinb64c89b2010-05-25 15:06:15 +0000269 DeleteKey(HKEY_CURRENT_USER, test_key_name)
270
271 def test_long_key(self):
272 # Issue2810, in 2.6 and 3.1 when the key name was exactly 256
Andrew Svetlovbacf1bf2012-12-19 22:49:01 +0200273 # characters, EnumKey raised "WindowsError: More data is
Brian Curtinb64c89b2010-05-25 15:06:15 +0000274 # available"
275 name = 'x'*256
276 try:
277 with CreateKey(HKEY_CURRENT_USER, test_key_name) as key:
278 SetValue(key, name, REG_SZ, 'x')
279 num_subkeys, num_values, t = QueryInfoKey(key)
280 EnumKey(key, 0)
281 finally:
Hirokazu Yamamotoecdead62010-10-22 11:47:07 +0000282 with OpenKey(HKEY_CURRENT_USER, test_key_name, 0, KEY_ALL_ACCESS) as key:
283 DeleteKey(key, name)
Brian Curtinb64c89b2010-05-25 15:06:15 +0000284 DeleteKey(HKEY_CURRENT_USER, test_key_name)
285
286 def test_dynamic_key(self):
287 # Issue2810, when the value is dynamically generated, these
Andrew Svetlov4bb142b2012-12-18 21:27:37 +0200288 # raise "WindowsError: More data is available" in 2.6 and 3.1
Antoine Pitrouf5262972012-07-13 22:46:41 +0200289 try:
290 EnumValue(HKEY_PERFORMANCE_DATA, 0)
291 except OSError as e:
292 if e.errno in (errno.EPERM, errno.EACCES):
293 self.skipTest("access denied to registry key "
294 "(are you running in a non-interactive session?)")
295 raise
Brian Curtinb64c89b2010-05-25 15:06:15 +0000296 QueryValueEx(HKEY_PERFORMANCE_DATA, None)
297
Brian Curtine33fa882010-04-02 21:18:14 +0000298 # Reflection requires XP x64/Vista at a minimum. XP doesn't have this stuff
299 # or DeleteKeyEx so make sure their use raises NotImplementedError
300 @unittest.skipUnless(WIN_VER < (5, 2), "Requires Windows XP")
301 def test_reflection_unsupported(self):
302 try:
303 with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck:
304 self.assertNotEqual(ck.handle, 0)
305
306 key = OpenKey(HKEY_CURRENT_USER, test_key_name)
307 self.assertNotEqual(key.handle, 0)
308
Brian Curtine9da81c2010-04-03 00:59:32 +0000309 with self.assertRaises(NotImplementedError):
310 DisableReflectionKey(key)
311 with self.assertRaises(NotImplementedError):
312 EnableReflectionKey(key)
313 with self.assertRaises(NotImplementedError):
314 QueryReflectionKey(key)
315 with self.assertRaises(NotImplementedError):
316 DeleteKeyEx(HKEY_CURRENT_USER, test_key_name)
Brian Curtine33fa882010-04-02 21:18:14 +0000317 finally:
318 DeleteKey(HKEY_CURRENT_USER, test_key_name)
319
Brian Curtin0e091b02012-12-27 12:28:51 -0600320 def test_setvalueex_value_range(self):
321 # Test for Issue #14420, accept proper ranges for SetValueEx.
322 # Py2Reg, which gets called by SetValueEx, was using PyLong_AsLong,
323 # thus raising OverflowError. The implementation now uses
324 # PyLong_AsUnsignedLong to match DWORD's size.
325 try:
326 with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck:
327 self.assertNotEqual(ck.handle, 0)
328 SetValueEx(ck, "test_name", None, REG_DWORD, 0x80000000)
329 finally:
330 DeleteKey(HKEY_CURRENT_USER, test_key_name)
331
Tim Golden3c603f32014-04-26 15:47:08 +0100332 def test_setvalueex_with_memoryview(self):
333 try:
334 with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck:
335 self.assertNotEqual(ck.handle, 0)
336 with self.assertRaises(TypeError):
337 SetValueEx(ck, "test_name", None, REG_BINARY, memoryview('val'))
338 finally:
339 DeleteKey(HKEY_CURRENT_USER, test_key_name)
340
Brian Curtin33e05e72012-12-27 14:37:06 -0600341 def test_queryvalueex_return_value(self):
342 # Test for Issue #16759, return unsigned int from QueryValueEx.
343 # Reg2Py, which gets called by QueryValueEx, was returning a value
Zachary Ware6690eed2014-07-03 10:57:44 -0500344 # generated by PyLong_FromLong. The implementation now uses
Brian Curtin33e05e72012-12-27 14:37:06 -0600345 # PyLong_FromUnsignedLong to match DWORD's size.
346 try:
347 with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck:
348 self.assertNotEqual(ck.handle, 0)
349 test_val = 0x80000000
350 SetValueEx(ck, "test_name", None, REG_DWORD, test_val)
351 ret_val, ret_type = QueryValueEx(ck, "test_name")
352 self.assertEqual(ret_type, REG_DWORD)
353 self.assertEqual(ret_val, test_val)
354 finally:
355 DeleteKey(HKEY_CURRENT_USER, test_key_name)
356
Zachary Ware6690eed2014-07-03 10:57:44 -0500357 def test_setvalueex_crash_with_none_arg(self):
358 # Test for Issue #21151, segfault when None is passed to SetValueEx
359 try:
360 with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck:
361 self.assertNotEqual(ck.handle, 0)
362 test_val = None
363 SetValueEx(ck, "test_name", 0, REG_BINARY, test_val)
364 ret_val, ret_type = QueryValueEx(ck, "test_name")
365 self.assertEqual(ret_type, REG_BINARY)
366 self.assertEqual(ret_val, test_val)
367 finally:
368 DeleteKey(HKEY_CURRENT_USER, test_key_name)
369
Brian Curtin33e05e72012-12-27 14:37:06 -0600370
Brian Curtine33fa882010-04-02 21:18:14 +0000371
372@unittest.skipUnless(REMOTE_NAME, "Skipping remote registry tests")
373class RemoteWinregTests(BaseWinregTests):
374
375 def test_remote_registry_works(self):
376 remote_key = ConnectRegistry(REMOTE_NAME, HKEY_CURRENT_USER)
377 self._test_all(remote_key)
378
379
380@unittest.skipUnless(WIN64_MACHINE, "x64 specific registry tests")
381class Win64WinregTests(BaseWinregTests):
382
383 def test_reflection_functions(self):
384 # Test that we can call the query, enable, and disable functions
385 # on a key which isn't on the reflection list with no consequences.
386 with OpenKey(HKEY_LOCAL_MACHINE, "Software") as key:
387 # HKLM\Software is redirected but not reflected in all OSes
388 self.assertTrue(QueryReflectionKey(key))
Ezio Melotti2623a372010-11-21 13:34:58 +0000389 self.assertEqual(None, EnableReflectionKey(key))
390 self.assertEqual(None, DisableReflectionKey(key))
Brian Curtine33fa882010-04-02 21:18:14 +0000391 self.assertTrue(QueryReflectionKey(key))
392
393 @unittest.skipUnless(HAS_REFLECTION, "OS doesn't support reflection")
394 def test_reflection(self):
395 # Test that we can create, open, and delete keys in the 32-bit
396 # area. Because we are doing this in a key which gets reflected,
397 # test the differences of 32 and 64-bit keys before and after the
398 # reflection occurs (ie. when the created key is closed).
399 try:
400 with CreateKeyEx(HKEY_CURRENT_USER, test_reflect_key_name, 0,
401 KEY_ALL_ACCESS | KEY_WOW64_32KEY) as created_key:
402 self.assertNotEqual(created_key.handle, 0)
403
404 # The key should now be available in the 32-bit area
405 with OpenKey(HKEY_CURRENT_USER, test_reflect_key_name, 0,
406 KEY_ALL_ACCESS | KEY_WOW64_32KEY) as key:
407 self.assertNotEqual(key.handle, 0)
408
409 # Write a value to what currently is only in the 32-bit area
410 SetValueEx(created_key, "", 0, REG_SZ, "32KEY")
411
412 # The key is not reflected until created_key is closed.
413 # The 64-bit version of the key should not be available yet.
414 open_fail = lambda: OpenKey(HKEY_CURRENT_USER,
415 test_reflect_key_name, 0,
416 KEY_READ | KEY_WOW64_64KEY)
417 self.assertRaises(WindowsError, open_fail)
418
419 # Now explicitly open the 64-bit version of the key
420 with OpenKey(HKEY_CURRENT_USER, test_reflect_key_name, 0,
421 KEY_ALL_ACCESS | KEY_WOW64_64KEY) as key:
422 self.assertNotEqual(key.handle, 0)
423 # Make sure the original value we set is there
424 self.assertEqual("32KEY", QueryValue(key, ""))
425 # Set a new value, which will get reflected to 32-bit
426 SetValueEx(key, "", 0, REG_SZ, "64KEY")
427
428 # Reflection uses a "last-writer wins policy, so the value we set
429 # on the 64-bit key should be the same on 32-bit
430 with OpenKey(HKEY_CURRENT_USER, test_reflect_key_name, 0,
431 KEY_READ | KEY_WOW64_32KEY) as key:
432 self.assertEqual("64KEY", QueryValue(key, ""))
433 finally:
434 DeleteKeyEx(HKEY_CURRENT_USER, test_reflect_key_name,
435 KEY_WOW64_32KEY, 0)
436
437 @unittest.skipUnless(HAS_REFLECTION, "OS doesn't support reflection")
438 def test_disable_reflection(self):
439 # Make use of a key which gets redirected and reflected
440 try:
441 with CreateKeyEx(HKEY_CURRENT_USER, test_reflect_key_name, 0,
442 KEY_ALL_ACCESS | KEY_WOW64_32KEY) as created_key:
443 # QueryReflectionKey returns whether or not the key is disabled
444 disabled = QueryReflectionKey(created_key)
445 self.assertEqual(type(disabled), bool)
446 # HKCU\Software\Classes is reflected by default
447 self.assertFalse(disabled)
448
449 DisableReflectionKey(created_key)
450 self.assertTrue(QueryReflectionKey(created_key))
451
452 # The key is now closed and would normally be reflected to the
453 # 64-bit area, but let's make sure that didn't happen.
454 open_fail = lambda: OpenKeyEx(HKEY_CURRENT_USER,
455 test_reflect_key_name, 0,
456 KEY_READ | KEY_WOW64_64KEY)
457 self.assertRaises(WindowsError, open_fail)
458
459 # Make sure the 32-bit key is actually there
460 with OpenKeyEx(HKEY_CURRENT_USER, test_reflect_key_name, 0,
461 KEY_READ | KEY_WOW64_32KEY) as key:
462 self.assertNotEqual(key.handle, 0)
463 finally:
464 DeleteKeyEx(HKEY_CURRENT_USER, test_reflect_key_name,
465 KEY_WOW64_32KEY, 0)
466
Ezio Melotti5cea09d2013-05-04 17:59:03 +0300467 def test_exception_numbers(self):
468 with self.assertRaises(WindowsError) as ctx:
469 QueryValue(HKEY_CLASSES_ROOT, 'some_value_that_does_not_exist')
470
471 self.assertEqual(ctx.exception.errno, 2)
Brian Curtine33fa882010-04-02 21:18:14 +0000472
Georg Brandl3376a9a2007-08-24 17:38:49 +0000473def test_main():
Brian Curtine33fa882010-04-02 21:18:14 +0000474 test_support.run_unittest(LocalWinregTests, RemoteWinregTests,
475 Win64WinregTests)
Georg Brandl3376a9a2007-08-24 17:38:49 +0000476
477if __name__ == "__main__":
Brian Curtine33fa882010-04-02 21:18:14 +0000478 if not REMOTE_NAME:
Georg Brandl3376a9a2007-08-24 17:38:49 +0000479 print "Remote registry calls can be tested using",
480 print "'test_winreg.py --remote \\\\machine_name'"
Georg Brandl3376a9a2007-08-24 17:38:49 +0000481 test_main()