blob: dc2b46e42521f3577c4fb3647c0c2be886e31378 [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 Pitrou2bb30212012-07-13 22:46:41 +02004import os, sys, errno
Thomas Woutersed03b412007-08-28 21:37:11 +00005import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00006from test import support
Antoine Pitrou88c60c92017-09-18 23:50:44 +02007import threading
Paul Monson62dfd7d2019-04-25 11:36:45 -07008from platform import machine, win32_edition
Fredrik Lundhf7850422001-01-17 21:51:36 +00009
R. David Murraya21e4ca2009-03-31 23:16:50 +000010# Do this first so test will be skipped if module doesn't exist
Brett Cannond1877262012-11-17 20:46:26 -050011support.import_module('winreg', required_on=['win'])
R. David Murraya21e4ca2009-03-31 23:16:50 +000012# Now import everything
13from winreg import *
14
Brian Curtin3035c392010-04-21 23:56:21 +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 Murray4140fb52013-04-21 10:08:02 -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 Curtin3035c392010-04-21 23:56:21 +000035# On OS'es that support reflection we should test with a reflected key
R David Murray4140fb52013-04-21 10:08:02 -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),
Steve Dower80ac11d2016-05-24 15:42:04 -070040 ("Qword Value", 0x1122334455667788, REG_QWORD),
Guido van Rossum0a185522003-11-30 22:46:18 +000041 ("String Val", "A string value", REG_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000042 ("StringExpand", "The path is %path%", REG_EXPAND_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000043 ("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ),
Thomas Woutersed03b412007-08-28 21:37:11 +000044 ("Raw Data", b"binary\x00data", REG_BINARY),
Guido van Rossum291481b2003-12-03 15:24:02 +000045 ("Big String", "x"*(2**14-1), REG_SZ),
Guido van Rossuma8c360e2007-07-17 20:50:43 +000046 ("Big Binary", b"x"*(2**14), REG_BINARY),
Martin v. Löwisf82d9b52007-09-03 07:43:05 +000047 # Two and three kanjis, meaning: "Japan" and "Japanese")
48 ("Japanese 日本", "日本語", REG_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000049]
50
Brian Curtin3035c392010-04-21 23:56:21 +000051class BaseWinregTests(unittest.TestCase):
Guido van Rossumde598552000-03-28 20:36:51 +000052
Martin v. Löwisb7a51562009-06-07 17:55:17 +000053 def setUp(self):
54 # Make sure that the test key is absent when the test
55 # starts.
56 self.delete_tree(HKEY_CURRENT_USER, test_key_name)
57
58 def delete_tree(self, root, subkey):
59 try:
Steve Dower40fa2662016-12-17 13:30:27 -080060 hkey = OpenKey(root, subkey, 0, KEY_ALL_ACCESS)
Andrew Svetlov2606a6f2012-12-19 14:33:35 +020061 except OSError:
Martin v. Löwisb7a51562009-06-07 17:55:17 +000062 # subkey does not exist
63 return
64 while True:
65 try:
66 subsubkey = EnumKey(hkey, 0)
Andrew Svetlov2606a6f2012-12-19 14:33:35 +020067 except OSError:
Martin v. Löwisb7a51562009-06-07 17:55:17 +000068 # no more subkeys
69 break
70 self.delete_tree(hkey, subsubkey)
71 CloseKey(hkey)
72 DeleteKey(root, subkey)
73
Brian Curtin3035c392010-04-21 23:56:21 +000074 def _write_test_data(self, root_key, subkeystr="sub_key",
75 CreateKey=CreateKey):
Thomas Woutersed03b412007-08-28 21:37:11 +000076 # Set the default value for this key.
77 SetValue(root_key, test_key_name, REG_SZ, "Default value")
78 key = CreateKey(root_key, test_key_name)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000079 self.assertTrue(key.handle != 0)
Thomas Woutersed03b412007-08-28 21:37:11 +000080 # Create a sub-key
Martin v. Löwisf82d9b52007-09-03 07:43:05 +000081 sub_key = CreateKey(key, subkeystr)
Thomas Woutersed03b412007-08-28 21:37:11 +000082 # Give the sub-key some named values
83
84 for value_name, value_data, value_type in test_data:
85 SetValueEx(sub_key, value_name, 0, value_type, value_data)
86
87 # Check we wrote as many items as we thought.
88 nkeys, nvalues, since_mod = QueryInfoKey(key)
Ezio Melottib3aedd42010-11-20 19:04:17 +000089 self.assertEqual(nkeys, 1, "Not the correct number of sub keys")
90 self.assertEqual(nvalues, 1, "Not the correct number of values")
Thomas Woutersed03b412007-08-28 21:37:11 +000091 nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
Ezio Melottib3aedd42010-11-20 19:04:17 +000092 self.assertEqual(nkeys, 0, "Not the correct number of sub keys")
93 self.assertEqual(nvalues, len(test_data),
94 "Not the correct number of values")
Thomas Woutersed03b412007-08-28 21:37:11 +000095 # Close this key this way...
96 # (but before we do, copy the key as an integer - this allows
97 # us to test that the key really gets closed).
98 int_sub_key = int(sub_key)
99 CloseKey(sub_key)
100 try:
101 QueryInfoKey(int_sub_key)
102 self.fail("It appears the CloseKey() function does "
103 "not close the actual key!")
Andrew Svetlov3438fa42012-12-17 23:35:18 +0200104 except OSError:
Thomas Woutersed03b412007-08-28 21:37:11 +0000105 pass
106 # ... and close that key that way :-)
107 int_key = int(key)
108 key.Close()
109 try:
110 QueryInfoKey(int_key)
111 self.fail("It appears the key.Close() function "
112 "does not close the actual key!")
Andrew Svetlov3438fa42012-12-17 23:35:18 +0200113 except OSError:
Thomas Woutersed03b412007-08-28 21:37:11 +0000114 pass
115
Brian Curtin3035c392010-04-21 23:56:21 +0000116 def _read_test_data(self, root_key, subkeystr="sub_key", OpenKey=OpenKey):
Thomas Woutersed03b412007-08-28 21:37:11 +0000117 # Check we can get default value for this key.
118 val = QueryValue(root_key, test_key_name)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000119 self.assertEqual(val, "Default value",
120 "Registry didn't give back the correct value")
Thomas Woutersed03b412007-08-28 21:37:11 +0000121
122 key = OpenKey(root_key, test_key_name)
123 # Read the sub-keys
Thomas Hellere83ebd92008-01-24 10:31:31 +0000124 with OpenKey(key, subkeystr) as sub_key:
Christian Heimes2380ac72008-01-09 00:17:24 +0000125 # Check I can enumerate over the values.
126 index = 0
127 while 1:
128 try:
129 data = EnumValue(sub_key, index)
Andrew Svetlov3438fa42012-12-17 23:35:18 +0200130 except OSError:
Christian Heimes2380ac72008-01-09 00:17:24 +0000131 break
Ezio Melottib3aedd42010-11-20 19:04:17 +0000132 self.assertEqual(data in test_data, True,
133 "Didn't read back the correct test data")
Christian Heimes2380ac72008-01-09 00:17:24 +0000134 index = index + 1
Ezio Melottib3aedd42010-11-20 19:04:17 +0000135 self.assertEqual(index, len(test_data),
136 "Didn't read the correct number of items")
Christian Heimes2380ac72008-01-09 00:17:24 +0000137 # Check I can directly access each item
138 for value_name, value_data, value_type in test_data:
139 read_val, read_typ = QueryValueEx(sub_key, value_name)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000140 self.assertEqual(read_val, value_data,
141 "Could not directly read the value")
142 self.assertEqual(read_typ, value_type,
143 "Could not directly read the value")
Thomas Woutersed03b412007-08-28 21:37:11 +0000144 sub_key.Close()
145 # Enumerate our main key.
146 read_val = EnumKey(key, 0)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000147 self.assertEqual(read_val, subkeystr, "Read subkey value wrong")
Thomas Woutersed03b412007-08-28 21:37:11 +0000148 try:
149 EnumKey(key, 1)
150 self.fail("Was able to get a second key when I only have one!")
Andrew Svetlov3438fa42012-12-17 23:35:18 +0200151 except OSError:
Thomas Woutersed03b412007-08-28 21:37:11 +0000152 pass
153
154 key.Close()
155
Brian Curtin3035c392010-04-21 23:56:21 +0000156 def _delete_test_data(self, root_key, subkeystr="sub_key"):
Thomas Woutersed03b412007-08-28 21:37:11 +0000157 key = OpenKey(root_key, test_key_name, 0, KEY_ALL_ACCESS)
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000158 sub_key = OpenKey(key, subkeystr, 0, KEY_ALL_ACCESS)
Thomas Woutersed03b412007-08-28 21:37:11 +0000159 # It is not necessary to delete the values before deleting
160 # the key (although subkeys must not exist). We delete them
161 # manually just to prove we can :-)
162 for value_name, value_data, value_type in test_data:
163 DeleteValue(sub_key, value_name)
164
165 nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000166 self.assertEqual(nkeys, 0, "subkey not empty before delete")
167 self.assertEqual(nvalues, 0, "subkey not empty before delete")
Thomas Woutersed03b412007-08-28 21:37:11 +0000168 sub_key.Close()
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000169 DeleteKey(key, subkeystr)
Thomas Woutersed03b412007-08-28 21:37:11 +0000170
171 try:
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700172 # Shouldn't be able to delete it twice!
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000173 DeleteKey(key, subkeystr)
Thomas Woutersed03b412007-08-28 21:37:11 +0000174 self.fail("Deleting the key twice succeeded")
Andrew Svetlov3438fa42012-12-17 23:35:18 +0200175 except OSError:
Thomas Woutersed03b412007-08-28 21:37:11 +0000176 pass
177 key.Close()
178 DeleteKey(root_key, test_key_name)
179 # Opening should now fail!
180 try:
181 key = OpenKey(root_key, test_key_name)
182 self.fail("Could open the non-existent key")
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200183 except OSError: # Use this error name this time
Thomas Woutersed03b412007-08-28 21:37:11 +0000184 pass
185
Brian Curtin3035c392010-04-21 23:56:21 +0000186 def _test_all(self, root_key, subkeystr="sub_key"):
187 self._write_test_data(root_key, subkeystr)
188 self._read_test_data(root_key, subkeystr)
189 self._delete_test_data(root_key, subkeystr)
Thomas Woutersed03b412007-08-28 21:37:11 +0000190
Brian Curtin1771b542010-09-27 17:56:36 +0000191 def _test_named_args(self, key, sub_key):
192 with CreateKeyEx(key=key, sub_key=sub_key, reserved=0,
193 access=KEY_ALL_ACCESS) as ckey:
194 self.assertTrue(ckey.handle != 0)
195
196 with OpenKeyEx(key=key, sub_key=sub_key, reserved=0,
197 access=KEY_ALL_ACCESS) as okey:
198 self.assertTrue(okey.handle != 0)
199
200
Brian Curtin3035c392010-04-21 23:56:21 +0000201class LocalWinregTests(BaseWinregTests):
Thomas Woutersed03b412007-08-28 21:37:11 +0000202
Brian Curtin3035c392010-04-21 23:56:21 +0000203 def test_registry_works(self):
204 self._test_all(HKEY_CURRENT_USER)
205 self._test_all(HKEY_CURRENT_USER, "日本-subkey")
206
207 def test_registry_works_extended_functions(self):
208 # Substitute the regular CreateKey and OpenKey calls with their
209 # extended counterparts.
210 # Note: DeleteKeyEx is not used here because it is platform dependent
211 cke = lambda key, sub_key: CreateKeyEx(key, sub_key, 0, KEY_ALL_ACCESS)
212 self._write_test_data(HKEY_CURRENT_USER, CreateKey=cke)
213
214 oke = lambda key, sub_key: OpenKeyEx(key, sub_key, 0, KEY_READ)
215 self._read_test_data(HKEY_CURRENT_USER, OpenKey=oke)
216
217 self._delete_test_data(HKEY_CURRENT_USER)
218
Brian Curtin1771b542010-09-27 17:56:36 +0000219 def test_named_arguments(self):
220 self._test_named_args(HKEY_CURRENT_USER, test_key_name)
221 # Use the regular DeleteKey to clean up
222 # DeleteKeyEx takes named args and is tested separately
223 DeleteKey(HKEY_CURRENT_USER, test_key_name)
224
Brian Curtin3035c392010-04-21 23:56:21 +0000225 def test_connect_registry_to_local_machine_works(self):
Thomas Woutersed03b412007-08-28 21:37:11 +0000226 # perform minimal ConnectRegistry test which just invokes it
227 h = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
Brian Curtin3035c392010-04-21 23:56:21 +0000228 self.assertNotEqual(h.handle, 0)
Thomas Woutersed03b412007-08-28 21:37:11 +0000229 h.Close()
Brian Curtin3035c392010-04-21 23:56:21 +0000230 self.assertEqual(h.handle, 0)
Thomas Woutersed03b412007-08-28 21:37:11 +0000231
Brian Curtin3035c392010-04-21 23:56:21 +0000232 def test_inexistant_remote_registry(self):
233 connect = lambda: ConnectRegistry("abcdefghijkl", HKEY_CURRENT_USER)
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200234 self.assertRaises(OSError, connect)
Thomas Woutersed03b412007-08-28 21:37:11 +0000235
Christian Heimes2380ac72008-01-09 00:17:24 +0000236 def testExpandEnvironmentStrings(self):
237 r = ExpandEnvironmentStrings("%windir%\\test")
238 self.assertEqual(type(r), str)
239 self.assertEqual(r, os.environ["windir"] + "\\test")
240
Brian Curtin3035c392010-04-21 23:56:21 +0000241 def test_context_manager(self):
242 # ensure that the handle is closed if an exception occurs
243 try:
244 with ConnectRegistry(None, HKEY_LOCAL_MACHINE) as h:
245 self.assertNotEqual(h.handle, 0)
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200246 raise OSError
247 except OSError:
Brian Curtin3035c392010-04-21 23:56:21 +0000248 self.assertEqual(h.handle, 0)
249
Brian Curtin60853212010-05-26 17:43:50 +0000250 def test_changing_value(self):
251 # Issue2810: A race condition in 2.6 and 3.1 may cause
Andrew Svetlov737fb892012-12-18 21:14:22 +0200252 # EnumValue or QueryValue to raise "WindowsError: More data is
Brian Curtin60853212010-05-26 17:43:50 +0000253 # available"
254 done = False
255
256 class VeryActiveThread(threading.Thread):
257 def run(self):
258 with CreateKey(HKEY_CURRENT_USER, test_key_name) as key:
259 use_short = True
260 long_string = 'x'*2000
261 while not done:
262 s = 'x' if use_short else long_string
263 use_short = not use_short
264 SetValue(key, 'changing_value', REG_SZ, s)
265
266 thread = VeryActiveThread()
267 thread.start()
268 try:
269 with CreateKey(HKEY_CURRENT_USER,
270 test_key_name+'\\changing_value') as key:
271 for _ in range(1000):
272 num_subkeys, num_values, t = QueryInfoKey(key)
273 for i in range(num_values):
274 name = EnumValue(key, i)
275 QueryValue(key, name[0])
276 finally:
277 done = True
278 thread.join()
279 DeleteKey(HKEY_CURRENT_USER, test_key_name+'\\changing_value')
280 DeleteKey(HKEY_CURRENT_USER, test_key_name)
281
282 def test_long_key(self):
283 # Issue2810, in 2.6 and 3.1 when the key name was exactly 256
Andrew Svetlov7bd61cb2012-12-19 22:49:25 +0200284 # characters, EnumKey raised "WindowsError: More data is
Brian Curtin60853212010-05-26 17:43:50 +0000285 # available"
286 name = 'x'*256
287 try:
288 with CreateKey(HKEY_CURRENT_USER, test_key_name) as key:
289 SetValue(key, name, REG_SZ, 'x')
290 num_subkeys, num_values, t = QueryInfoKey(key)
291 EnumKey(key, 0)
292 finally:
293 DeleteKey(HKEY_CURRENT_USER, '\\'.join((test_key_name, name)))
294 DeleteKey(HKEY_CURRENT_USER, test_key_name)
295
296 def test_dynamic_key(self):
297 # Issue2810, when the value is dynamically generated, these
Andrew Svetlov737fb892012-12-18 21:14:22 +0200298 # raise "WindowsError: More data is available" in 2.6 and 3.1
Antoine Pitrou2bb30212012-07-13 22:46:41 +0200299 try:
300 EnumValue(HKEY_PERFORMANCE_DATA, 0)
301 except OSError as e:
302 if e.errno in (errno.EPERM, errno.EACCES):
303 self.skipTest("access denied to registry key "
304 "(are you running in a non-interactive session?)")
305 raise
Brian Curtin60853212010-05-26 17:43:50 +0000306 QueryValueEx(HKEY_PERFORMANCE_DATA, "")
307
Brian Curtin3035c392010-04-21 23:56:21 +0000308 # Reflection requires XP x64/Vista at a minimum. XP doesn't have this stuff
309 # or DeleteKeyEx so make sure their use raises NotImplementedError
310 @unittest.skipUnless(WIN_VER < (5, 2), "Requires Windows XP")
311 def test_reflection_unsupported(self):
312 try:
313 with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck:
314 self.assertNotEqual(ck.handle, 0)
315
316 key = OpenKey(HKEY_CURRENT_USER, test_key_name)
317 self.assertNotEqual(key.handle, 0)
318
319 with self.assertRaises(NotImplementedError):
320 DisableReflectionKey(key)
321 with self.assertRaises(NotImplementedError):
322 EnableReflectionKey(key)
323 with self.assertRaises(NotImplementedError):
324 QueryReflectionKey(key)
325 with self.assertRaises(NotImplementedError):
326 DeleteKeyEx(HKEY_CURRENT_USER, test_key_name)
327 finally:
328 DeleteKey(HKEY_CURRENT_USER, test_key_name)
329
Brian Curtin12706f22012-12-27 10:12:45 -0600330 def test_setvalueex_value_range(self):
331 # Test for Issue #14420, accept proper ranges for SetValueEx.
332 # Py2Reg, which gets called by SetValueEx, was using PyLong_AsLong,
333 # thus raising OverflowError. The implementation now uses
334 # PyLong_AsUnsignedLong to match DWORD's size.
335 try:
336 with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck:
337 self.assertNotEqual(ck.handle, 0)
338 SetValueEx(ck, "test_name", None, REG_DWORD, 0x80000000)
339 finally:
340 DeleteKey(HKEY_CURRENT_USER, test_key_name)
341
Brian Curtin172e4222012-12-27 14:04:42 -0600342 def test_queryvalueex_return_value(self):
343 # Test for Issue #16759, return unsigned int from QueryValueEx.
344 # Reg2Py, which gets called by QueryValueEx, was returning a value
Zachary Waread4690f2014-07-03 10:58:06 -0500345 # generated by PyLong_FromLong. The implementation now uses
Brian Curtin172e4222012-12-27 14:04:42 -0600346 # PyLong_FromUnsignedLong to match DWORD's size.
347 try:
348 with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck:
349 self.assertNotEqual(ck.handle, 0)
350 test_val = 0x80000000
351 SetValueEx(ck, "test_name", None, REG_DWORD, test_val)
352 ret_val, ret_type = QueryValueEx(ck, "test_name")
353 self.assertEqual(ret_type, REG_DWORD)
354 self.assertEqual(ret_val, test_val)
355 finally:
356 DeleteKey(HKEY_CURRENT_USER, test_key_name)
357
Zachary Waread4690f2014-07-03 10:58:06 -0500358 def test_setvalueex_crash_with_none_arg(self):
359 # Test for Issue #21151, segfault when None is passed to SetValueEx
360 try:
361 with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck:
362 self.assertNotEqual(ck.handle, 0)
363 test_val = None
364 SetValueEx(ck, "test_name", 0, REG_BINARY, test_val)
365 ret_val, ret_type = QueryValueEx(ck, "test_name")
366 self.assertEqual(ret_type, REG_BINARY)
367 self.assertEqual(ret_val, test_val)
368 finally:
369 DeleteKey(HKEY_CURRENT_USER, test_key_name)
370
Steve Dower40fa2662016-12-17 13:30:27 -0800371 def test_read_string_containing_null(self):
372 # Test for issue 25778: REG_SZ should not contain null characters
373 try:
374 with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck:
375 self.assertNotEqual(ck.handle, 0)
376 test_val = "A string\x00 with a null"
377 SetValueEx(ck, "test_name", 0, REG_SZ, test_val)
378 ret_val, ret_type = QueryValueEx(ck, "test_name")
379 self.assertEqual(ret_type, REG_SZ)
380 self.assertEqual(ret_val, "A string")
381 finally:
382 DeleteKey(HKEY_CURRENT_USER, test_key_name)
Brian Curtin172e4222012-12-27 14:04:42 -0600383
Brian Curtin3035c392010-04-21 23:56:21 +0000384
385@unittest.skipUnless(REMOTE_NAME, "Skipping remote registry tests")
386class RemoteWinregTests(BaseWinregTests):
387
388 def test_remote_registry_works(self):
389 remote_key = ConnectRegistry(REMOTE_NAME, HKEY_CURRENT_USER)
390 self._test_all(remote_key)
391
392
393@unittest.skipUnless(WIN64_MACHINE, "x64 specific registry tests")
394class Win64WinregTests(BaseWinregTests):
395
Brian Curtin1771b542010-09-27 17:56:36 +0000396 def test_named_arguments(self):
397 self._test_named_args(HKEY_CURRENT_USER, test_key_name)
398 # Clean up and also exercise the named arguments
399 DeleteKeyEx(key=HKEY_CURRENT_USER, sub_key=test_key_name,
400 access=KEY_ALL_ACCESS, reserved=0)
401
Paul Monson62dfd7d2019-04-25 11:36:45 -0700402 @unittest.skipIf(win32_edition() in ('WindowsCoreHeadless', 'IoTEdgeOS'), "APIs not available on WindowsCoreHeadless")
Brian Curtin3035c392010-04-21 23:56:21 +0000403 def test_reflection_functions(self):
404 # Test that we can call the query, enable, and disable functions
405 # on a key which isn't on the reflection list with no consequences.
406 with OpenKey(HKEY_LOCAL_MACHINE, "Software") as key:
407 # HKLM\Software is redirected but not reflected in all OSes
408 self.assertTrue(QueryReflectionKey(key))
Raymond Hettinger7beae8a2011-01-06 05:34:17 +0000409 self.assertIsNone(EnableReflectionKey(key))
410 self.assertIsNone(DisableReflectionKey(key))
Brian Curtin3035c392010-04-21 23:56:21 +0000411 self.assertTrue(QueryReflectionKey(key))
412
413 @unittest.skipUnless(HAS_REFLECTION, "OS doesn't support reflection")
414 def test_reflection(self):
415 # Test that we can create, open, and delete keys in the 32-bit
416 # area. Because we are doing this in a key which gets reflected,
417 # test the differences of 32 and 64-bit keys before and after the
418 # reflection occurs (ie. when the created key is closed).
419 try:
420 with CreateKeyEx(HKEY_CURRENT_USER, test_reflect_key_name, 0,
421 KEY_ALL_ACCESS | KEY_WOW64_32KEY) as created_key:
422 self.assertNotEqual(created_key.handle, 0)
423
424 # The key should now be available in the 32-bit area
425 with OpenKey(HKEY_CURRENT_USER, test_reflect_key_name, 0,
426 KEY_ALL_ACCESS | KEY_WOW64_32KEY) as key:
427 self.assertNotEqual(key.handle, 0)
428
429 # Write a value to what currently is only in the 32-bit area
430 SetValueEx(created_key, "", 0, REG_SZ, "32KEY")
431
432 # The key is not reflected until created_key is closed.
433 # The 64-bit version of the key should not be available yet.
434 open_fail = lambda: OpenKey(HKEY_CURRENT_USER,
435 test_reflect_key_name, 0,
436 KEY_READ | KEY_WOW64_64KEY)
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200437 self.assertRaises(OSError, open_fail)
Brian Curtin3035c392010-04-21 23:56:21 +0000438
439 # Now explicitly open the 64-bit version of the key
440 with OpenKey(HKEY_CURRENT_USER, test_reflect_key_name, 0,
441 KEY_ALL_ACCESS | KEY_WOW64_64KEY) as key:
442 self.assertNotEqual(key.handle, 0)
443 # Make sure the original value we set is there
444 self.assertEqual("32KEY", QueryValue(key, ""))
445 # Set a new value, which will get reflected to 32-bit
446 SetValueEx(key, "", 0, REG_SZ, "64KEY")
447
448 # Reflection uses a "last-writer wins policy, so the value we set
449 # on the 64-bit key should be the same on 32-bit
450 with OpenKey(HKEY_CURRENT_USER, test_reflect_key_name, 0,
451 KEY_READ | KEY_WOW64_32KEY) as key:
452 self.assertEqual("64KEY", QueryValue(key, ""))
453 finally:
454 DeleteKeyEx(HKEY_CURRENT_USER, test_reflect_key_name,
455 KEY_WOW64_32KEY, 0)
456
457 @unittest.skipUnless(HAS_REFLECTION, "OS doesn't support reflection")
458 def test_disable_reflection(self):
459 # Make use of a key which gets redirected and reflected
460 try:
461 with CreateKeyEx(HKEY_CURRENT_USER, test_reflect_key_name, 0,
462 KEY_ALL_ACCESS | KEY_WOW64_32KEY) as created_key:
463 # QueryReflectionKey returns whether or not the key is disabled
464 disabled = QueryReflectionKey(created_key)
465 self.assertEqual(type(disabled), bool)
466 # HKCU\Software\Classes is reflected by default
467 self.assertFalse(disabled)
468
469 DisableReflectionKey(created_key)
470 self.assertTrue(QueryReflectionKey(created_key))
471
472 # The key is now closed and would normally be reflected to the
473 # 64-bit area, but let's make sure that didn't happen.
474 open_fail = lambda: OpenKeyEx(HKEY_CURRENT_USER,
475 test_reflect_key_name, 0,
476 KEY_READ | KEY_WOW64_64KEY)
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200477 self.assertRaises(OSError, open_fail)
Brian Curtin3035c392010-04-21 23:56:21 +0000478
479 # Make sure the 32-bit key is actually there
480 with OpenKeyEx(HKEY_CURRENT_USER, test_reflect_key_name, 0,
481 KEY_READ | KEY_WOW64_32KEY) as key:
482 self.assertNotEqual(key.handle, 0)
483 finally:
484 DeleteKeyEx(HKEY_CURRENT_USER, test_reflect_key_name,
485 KEY_WOW64_32KEY, 0)
486
Ezio Melotti28faf032013-05-04 17:46:23 +0300487 def test_exception_numbers(self):
488 with self.assertRaises(FileNotFoundError) as ctx:
489 QueryValue(HKEY_CLASSES_ROOT, 'some_value_that_does_not_exist')
Brian Curtin3035c392010-04-21 23:56:21 +0000490
Thomas Woutersed03b412007-08-28 21:37:11 +0000491def test_main():
Brian Curtin3035c392010-04-21 23:56:21 +0000492 support.run_unittest(LocalWinregTests, RemoteWinregTests,
493 Win64WinregTests)
Thomas Woutersed03b412007-08-28 21:37:11 +0000494
495if __name__ == "__main__":
Brian Curtin3035c392010-04-21 23:56:21 +0000496 if not REMOTE_NAME:
Thomas Woutersed03b412007-08-28 21:37:11 +0000497 print("Remote registry calls can be tested using",
498 "'test_winreg.py --remote \\\\machine_name'")
Thomas Woutersed03b412007-08-28 21:37:11 +0000499 test_main()