blob: 487abcd8d9531ad765356813f499d28e9704ad9f [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
Hai Shif7ba40b2020-06-25 18:38:51 +08007from test.support import import_helper
Antoine Pitrou88c60c92017-09-18 23:50:44 +02008import threading
Paul Monson62dfd7d2019-04-25 11:36:45 -07009from platform import machine, win32_edition
Fredrik Lundhf7850422001-01-17 21:51:36 +000010
R. David Murraya21e4ca2009-03-31 23:16:50 +000011# Do this first so test will be skipped if module doesn't exist
Hai Shif7ba40b2020-06-25 18:38:51 +080012import_helper.import_module('winreg', required_on=['win'])
R. David Murraya21e4ca2009-03-31 23:16:50 +000013# Now import everything
14from winreg import *
15
Brian Curtin3035c392010-04-21 23:56:21 +000016try:
17 REMOTE_NAME = sys.argv[sys.argv.index("--remote")+1]
18except (IndexError, ValueError):
19 REMOTE_NAME = None
20
21# tuple of (major, minor)
22WIN_VER = sys.getwindowsversion()[:2]
23# Some tests should only run on 64-bit architectures where WOW64 will be.
24WIN64_MACHINE = True if machine() == "AMD64" else False
25
26# Starting with Windows 7 and Windows Server 2008 R2, WOW64 no longer uses
27# registry reflection and formerly reflected keys are shared instead.
28# Windows 7 and Windows Server 2008 R2 are version 6.1. Due to this, some
29# tests are only valid up until 6.1
30HAS_REFLECTION = True if WIN_VER < (6, 1) else False
31
R David Murray4140fb52013-04-21 10:08:02 -040032# Use a per-process key to prevent concurrent test runs (buildbot!) from
33# stomping on each other.
34test_key_base = "Python Test Key [%d] - Delete Me" % (os.getpid(),)
35test_key_name = "SOFTWARE\\" + test_key_base
Brian Curtin3035c392010-04-21 23:56:21 +000036# On OS'es that support reflection we should test with a reflected key
R David Murray4140fb52013-04-21 10:08:02 -040037test_reflect_key_name = "SOFTWARE\\Classes\\" + test_key_base
Guido van Rossumde598552000-03-28 20:36:51 +000038
39test_data = [
40 ("Int Value", 45, REG_DWORD),
Steve Dower80ac11d2016-05-24 15:42:04 -070041 ("Qword Value", 0x1122334455667788, REG_QWORD),
Guido van Rossum0a185522003-11-30 22:46:18 +000042 ("String Val", "A string value", REG_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000043 ("StringExpand", "The path is %path%", REG_EXPAND_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000044 ("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ),
Zackery Spytze223ba12019-09-09 03:26:15 -060045 ("Multi-nul", ["", "", "", ""], REG_MULTI_SZ),
Thomas Woutersed03b412007-08-28 21:37:11 +000046 ("Raw Data", b"binary\x00data", REG_BINARY),
Guido van Rossum291481b2003-12-03 15:24:02 +000047 ("Big String", "x"*(2**14-1), REG_SZ),
Guido van Rossuma8c360e2007-07-17 20:50:43 +000048 ("Big Binary", b"x"*(2**14), REG_BINARY),
Martin v. Löwisf82d9b52007-09-03 07:43:05 +000049 # Two and three kanjis, meaning: "Japan" and "Japanese")
50 ("Japanese 日本", "日本語", REG_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000051]
52
Brian Curtin3035c392010-04-21 23:56:21 +000053class BaseWinregTests(unittest.TestCase):
Guido van Rossumde598552000-03-28 20:36:51 +000054
Martin v. Löwisb7a51562009-06-07 17:55:17 +000055 def setUp(self):
56 # Make sure that the test key is absent when the test
57 # starts.
58 self.delete_tree(HKEY_CURRENT_USER, test_key_name)
59
60 def delete_tree(self, root, subkey):
61 try:
Steve Dower40fa2662016-12-17 13:30:27 -080062 hkey = OpenKey(root, subkey, 0, KEY_ALL_ACCESS)
Andrew Svetlov2606a6f2012-12-19 14:33:35 +020063 except OSError:
Martin v. Löwisb7a51562009-06-07 17:55:17 +000064 # subkey does not exist
65 return
66 while True:
67 try:
68 subsubkey = EnumKey(hkey, 0)
Andrew Svetlov2606a6f2012-12-19 14:33:35 +020069 except OSError:
Martin v. Löwisb7a51562009-06-07 17:55:17 +000070 # no more subkeys
71 break
72 self.delete_tree(hkey, subsubkey)
73 CloseKey(hkey)
74 DeleteKey(root, subkey)
75
Brian Curtin3035c392010-04-21 23:56:21 +000076 def _write_test_data(self, root_key, subkeystr="sub_key",
77 CreateKey=CreateKey):
Thomas Woutersed03b412007-08-28 21:37:11 +000078 # Set the default value for this key.
79 SetValue(root_key, test_key_name, REG_SZ, "Default value")
80 key = CreateKey(root_key, test_key_name)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000081 self.assertTrue(key.handle != 0)
Thomas Woutersed03b412007-08-28 21:37:11 +000082 # Create a sub-key
Martin v. Löwisf82d9b52007-09-03 07:43:05 +000083 sub_key = CreateKey(key, subkeystr)
Thomas Woutersed03b412007-08-28 21:37:11 +000084 # Give the sub-key some named values
85
86 for value_name, value_data, value_type in test_data:
87 SetValueEx(sub_key, value_name, 0, value_type, value_data)
88
89 # Check we wrote as many items as we thought.
90 nkeys, nvalues, since_mod = QueryInfoKey(key)
Ezio Melottib3aedd42010-11-20 19:04:17 +000091 self.assertEqual(nkeys, 1, "Not the correct number of sub keys")
92 self.assertEqual(nvalues, 1, "Not the correct number of values")
Thomas Woutersed03b412007-08-28 21:37:11 +000093 nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
Ezio Melottib3aedd42010-11-20 19:04:17 +000094 self.assertEqual(nkeys, 0, "Not the correct number of sub keys")
95 self.assertEqual(nvalues, len(test_data),
96 "Not the correct number of values")
Thomas Woutersed03b412007-08-28 21:37:11 +000097 # Close this key this way...
98 # (but before we do, copy the key as an integer - this allows
99 # us to test that the key really gets closed).
100 int_sub_key = int(sub_key)
101 CloseKey(sub_key)
102 try:
103 QueryInfoKey(int_sub_key)
104 self.fail("It appears the CloseKey() function does "
105 "not close the actual key!")
Andrew Svetlov3438fa42012-12-17 23:35:18 +0200106 except OSError:
Thomas Woutersed03b412007-08-28 21:37:11 +0000107 pass
108 # ... and close that key that way :-)
109 int_key = int(key)
110 key.Close()
111 try:
112 QueryInfoKey(int_key)
113 self.fail("It appears the key.Close() function "
114 "does not close the actual key!")
Andrew Svetlov3438fa42012-12-17 23:35:18 +0200115 except OSError:
Thomas Woutersed03b412007-08-28 21:37:11 +0000116 pass
117
Brian Curtin3035c392010-04-21 23:56:21 +0000118 def _read_test_data(self, root_key, subkeystr="sub_key", OpenKey=OpenKey):
Thomas Woutersed03b412007-08-28 21:37:11 +0000119 # Check we can get default value for this key.
120 val = QueryValue(root_key, test_key_name)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000121 self.assertEqual(val, "Default value",
122 "Registry didn't give back the correct value")
Thomas Woutersed03b412007-08-28 21:37:11 +0000123
124 key = OpenKey(root_key, test_key_name)
125 # Read the sub-keys
Thomas Hellere83ebd92008-01-24 10:31:31 +0000126 with OpenKey(key, subkeystr) as sub_key:
Christian Heimes2380ac72008-01-09 00:17:24 +0000127 # Check I can enumerate over the values.
128 index = 0
129 while 1:
130 try:
131 data = EnumValue(sub_key, index)
Andrew Svetlov3438fa42012-12-17 23:35:18 +0200132 except OSError:
Christian Heimes2380ac72008-01-09 00:17:24 +0000133 break
Ezio Melottib3aedd42010-11-20 19:04:17 +0000134 self.assertEqual(data in test_data, True,
135 "Didn't read back the correct test data")
Christian Heimes2380ac72008-01-09 00:17:24 +0000136 index = index + 1
Ezio Melottib3aedd42010-11-20 19:04:17 +0000137 self.assertEqual(index, len(test_data),
138 "Didn't read the correct number of items")
Christian Heimes2380ac72008-01-09 00:17:24 +0000139 # Check I can directly access each item
140 for value_name, value_data, value_type in test_data:
141 read_val, read_typ = QueryValueEx(sub_key, value_name)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000142 self.assertEqual(read_val, value_data,
143 "Could not directly read the value")
144 self.assertEqual(read_typ, value_type,
145 "Could not directly read the value")
Thomas Woutersed03b412007-08-28 21:37:11 +0000146 sub_key.Close()
147 # Enumerate our main key.
148 read_val = EnumKey(key, 0)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000149 self.assertEqual(read_val, subkeystr, "Read subkey value wrong")
Thomas Woutersed03b412007-08-28 21:37:11 +0000150 try:
151 EnumKey(key, 1)
152 self.fail("Was able to get a second key when I only have one!")
Andrew Svetlov3438fa42012-12-17 23:35:18 +0200153 except OSError:
Thomas Woutersed03b412007-08-28 21:37:11 +0000154 pass
155
156 key.Close()
157
Brian Curtin3035c392010-04-21 23:56:21 +0000158 def _delete_test_data(self, root_key, subkeystr="sub_key"):
Thomas Woutersed03b412007-08-28 21:37:11 +0000159 key = OpenKey(root_key, test_key_name, 0, KEY_ALL_ACCESS)
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000160 sub_key = OpenKey(key, subkeystr, 0, KEY_ALL_ACCESS)
Thomas Woutersed03b412007-08-28 21:37:11 +0000161 # It is not necessary to delete the values before deleting
162 # the key (although subkeys must not exist). We delete them
163 # manually just to prove we can :-)
164 for value_name, value_data, value_type in test_data:
165 DeleteValue(sub_key, value_name)
166
167 nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000168 self.assertEqual(nkeys, 0, "subkey not empty before delete")
169 self.assertEqual(nvalues, 0, "subkey not empty before delete")
Thomas Woutersed03b412007-08-28 21:37:11 +0000170 sub_key.Close()
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000171 DeleteKey(key, subkeystr)
Thomas Woutersed03b412007-08-28 21:37:11 +0000172
173 try:
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700174 # Shouldn't be able to delete it twice!
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000175 DeleteKey(key, subkeystr)
Thomas Woutersed03b412007-08-28 21:37:11 +0000176 self.fail("Deleting the key twice succeeded")
Andrew Svetlov3438fa42012-12-17 23:35:18 +0200177 except OSError:
Thomas Woutersed03b412007-08-28 21:37:11 +0000178 pass
179 key.Close()
180 DeleteKey(root_key, test_key_name)
181 # Opening should now fail!
182 try:
183 key = OpenKey(root_key, test_key_name)
184 self.fail("Could open the non-existent key")
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200185 except OSError: # Use this error name this time
Thomas Woutersed03b412007-08-28 21:37:11 +0000186 pass
187
Brian Curtin3035c392010-04-21 23:56:21 +0000188 def _test_all(self, root_key, subkeystr="sub_key"):
189 self._write_test_data(root_key, subkeystr)
190 self._read_test_data(root_key, subkeystr)
191 self._delete_test_data(root_key, subkeystr)
Thomas Woutersed03b412007-08-28 21:37:11 +0000192
Brian Curtin1771b542010-09-27 17:56:36 +0000193 def _test_named_args(self, key, sub_key):
194 with CreateKeyEx(key=key, sub_key=sub_key, reserved=0,
195 access=KEY_ALL_ACCESS) as ckey:
196 self.assertTrue(ckey.handle != 0)
197
198 with OpenKeyEx(key=key, sub_key=sub_key, reserved=0,
199 access=KEY_ALL_ACCESS) as okey:
200 self.assertTrue(okey.handle != 0)
201
202
Brian Curtin3035c392010-04-21 23:56:21 +0000203class LocalWinregTests(BaseWinregTests):
Thomas Woutersed03b412007-08-28 21:37:11 +0000204
Brian Curtin3035c392010-04-21 23:56:21 +0000205 def test_registry_works(self):
206 self._test_all(HKEY_CURRENT_USER)
207 self._test_all(HKEY_CURRENT_USER, "日本-subkey")
208
209 def test_registry_works_extended_functions(self):
210 # Substitute the regular CreateKey and OpenKey calls with their
211 # extended counterparts.
212 # Note: DeleteKeyEx is not used here because it is platform dependent
213 cke = lambda key, sub_key: CreateKeyEx(key, sub_key, 0, KEY_ALL_ACCESS)
214 self._write_test_data(HKEY_CURRENT_USER, CreateKey=cke)
215
216 oke = lambda key, sub_key: OpenKeyEx(key, sub_key, 0, KEY_READ)
217 self._read_test_data(HKEY_CURRENT_USER, OpenKey=oke)
218
219 self._delete_test_data(HKEY_CURRENT_USER)
220
Brian Curtin1771b542010-09-27 17:56:36 +0000221 def test_named_arguments(self):
222 self._test_named_args(HKEY_CURRENT_USER, test_key_name)
223 # Use the regular DeleteKey to clean up
224 # DeleteKeyEx takes named args and is tested separately
225 DeleteKey(HKEY_CURRENT_USER, test_key_name)
226
Brian Curtin3035c392010-04-21 23:56:21 +0000227 def test_connect_registry_to_local_machine_works(self):
Thomas Woutersed03b412007-08-28 21:37:11 +0000228 # perform minimal ConnectRegistry test which just invokes it
229 h = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
Brian Curtin3035c392010-04-21 23:56:21 +0000230 self.assertNotEqual(h.handle, 0)
Thomas Woutersed03b412007-08-28 21:37:11 +0000231 h.Close()
Brian Curtin3035c392010-04-21 23:56:21 +0000232 self.assertEqual(h.handle, 0)
Thomas Woutersed03b412007-08-28 21:37:11 +0000233
Min ho Kim39d87b52019-08-31 06:21:19 +1000234 def test_nonexistent_remote_registry(self):
Brian Curtin3035c392010-04-21 23:56:21 +0000235 connect = lambda: ConnectRegistry("abcdefghijkl", HKEY_CURRENT_USER)
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200236 self.assertRaises(OSError, connect)
Thomas Woutersed03b412007-08-28 21:37:11 +0000237
Christian Heimes2380ac72008-01-09 00:17:24 +0000238 def testExpandEnvironmentStrings(self):
239 r = ExpandEnvironmentStrings("%windir%\\test")
240 self.assertEqual(type(r), str)
241 self.assertEqual(r, os.environ["windir"] + "\\test")
242
Brian Curtin3035c392010-04-21 23:56:21 +0000243 def test_context_manager(self):
244 # ensure that the handle is closed if an exception occurs
245 try:
246 with ConnectRegistry(None, HKEY_LOCAL_MACHINE) as h:
247 self.assertNotEqual(h.handle, 0)
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200248 raise OSError
249 except OSError:
Brian Curtin3035c392010-04-21 23:56:21 +0000250 self.assertEqual(h.handle, 0)
251
Brian Curtin60853212010-05-26 17:43:50 +0000252 def test_changing_value(self):
253 # Issue2810: A race condition in 2.6 and 3.1 may cause
Andrew Svetlov737fb892012-12-18 21:14:22 +0200254 # EnumValue or QueryValue to raise "WindowsError: More data is
Brian Curtin60853212010-05-26 17:43:50 +0000255 # available"
256 done = False
257
258 class VeryActiveThread(threading.Thread):
259 def run(self):
260 with CreateKey(HKEY_CURRENT_USER, test_key_name) as key:
261 use_short = True
262 long_string = 'x'*2000
263 while not done:
264 s = 'x' if use_short else long_string
265 use_short = not use_short
266 SetValue(key, 'changing_value', REG_SZ, s)
267
268 thread = VeryActiveThread()
269 thread.start()
270 try:
271 with CreateKey(HKEY_CURRENT_USER,
272 test_key_name+'\\changing_value') as key:
273 for _ in range(1000):
274 num_subkeys, num_values, t = QueryInfoKey(key)
275 for i in range(num_values):
276 name = EnumValue(key, i)
277 QueryValue(key, name[0])
278 finally:
279 done = True
280 thread.join()
281 DeleteKey(HKEY_CURRENT_USER, test_key_name+'\\changing_value')
282 DeleteKey(HKEY_CURRENT_USER, test_key_name)
283
284 def test_long_key(self):
285 # Issue2810, in 2.6 and 3.1 when the key name was exactly 256
Andrew Svetlov7bd61cb2012-12-19 22:49:25 +0200286 # characters, EnumKey raised "WindowsError: More data is
Brian Curtin60853212010-05-26 17:43:50 +0000287 # available"
288 name = 'x'*256
289 try:
290 with CreateKey(HKEY_CURRENT_USER, test_key_name) as key:
291 SetValue(key, name, REG_SZ, 'x')
292 num_subkeys, num_values, t = QueryInfoKey(key)
293 EnumKey(key, 0)
294 finally:
295 DeleteKey(HKEY_CURRENT_USER, '\\'.join((test_key_name, name)))
296 DeleteKey(HKEY_CURRENT_USER, test_key_name)
297
298 def test_dynamic_key(self):
299 # Issue2810, when the value is dynamically generated, these
Andrew Svetlov737fb892012-12-18 21:14:22 +0200300 # raise "WindowsError: More data is available" in 2.6 and 3.1
Antoine Pitrou2bb30212012-07-13 22:46:41 +0200301 try:
302 EnumValue(HKEY_PERFORMANCE_DATA, 0)
303 except OSError as e:
304 if e.errno in (errno.EPERM, errno.EACCES):
305 self.skipTest("access denied to registry key "
306 "(are you running in a non-interactive session?)")
307 raise
Brian Curtin60853212010-05-26 17:43:50 +0000308 QueryValueEx(HKEY_PERFORMANCE_DATA, "")
309
Brian Curtin3035c392010-04-21 23:56:21 +0000310 # Reflection requires XP x64/Vista at a minimum. XP doesn't have this stuff
311 # or DeleteKeyEx so make sure their use raises NotImplementedError
312 @unittest.skipUnless(WIN_VER < (5, 2), "Requires Windows XP")
313 def test_reflection_unsupported(self):
314 try:
315 with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck:
316 self.assertNotEqual(ck.handle, 0)
317
318 key = OpenKey(HKEY_CURRENT_USER, test_key_name)
319 self.assertNotEqual(key.handle, 0)
320
321 with self.assertRaises(NotImplementedError):
322 DisableReflectionKey(key)
323 with self.assertRaises(NotImplementedError):
324 EnableReflectionKey(key)
325 with self.assertRaises(NotImplementedError):
326 QueryReflectionKey(key)
327 with self.assertRaises(NotImplementedError):
328 DeleteKeyEx(HKEY_CURRENT_USER, test_key_name)
329 finally:
330 DeleteKey(HKEY_CURRENT_USER, test_key_name)
331
Brian Curtin12706f22012-12-27 10:12:45 -0600332 def test_setvalueex_value_range(self):
333 # Test for Issue #14420, accept proper ranges for SetValueEx.
334 # Py2Reg, which gets called by SetValueEx, was using PyLong_AsLong,
335 # thus raising OverflowError. The implementation now uses
336 # PyLong_AsUnsignedLong to match DWORD's size.
337 try:
338 with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck:
339 self.assertNotEqual(ck.handle, 0)
340 SetValueEx(ck, "test_name", None, REG_DWORD, 0x80000000)
341 finally:
342 DeleteKey(HKEY_CURRENT_USER, test_key_name)
343
Brian Curtin172e4222012-12-27 14:04:42 -0600344 def test_queryvalueex_return_value(self):
345 # Test for Issue #16759, return unsigned int from QueryValueEx.
346 # Reg2Py, which gets called by QueryValueEx, was returning a value
Zachary Waread4690f2014-07-03 10:58:06 -0500347 # generated by PyLong_FromLong. The implementation now uses
Brian Curtin172e4222012-12-27 14:04:42 -0600348 # PyLong_FromUnsignedLong to match DWORD's size.
349 try:
350 with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck:
351 self.assertNotEqual(ck.handle, 0)
352 test_val = 0x80000000
353 SetValueEx(ck, "test_name", None, REG_DWORD, test_val)
354 ret_val, ret_type = QueryValueEx(ck, "test_name")
355 self.assertEqual(ret_type, REG_DWORD)
356 self.assertEqual(ret_val, test_val)
357 finally:
358 DeleteKey(HKEY_CURRENT_USER, test_key_name)
359
Zachary Waread4690f2014-07-03 10:58:06 -0500360 def test_setvalueex_crash_with_none_arg(self):
361 # Test for Issue #21151, segfault when None is passed to SetValueEx
362 try:
363 with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck:
364 self.assertNotEqual(ck.handle, 0)
365 test_val = None
366 SetValueEx(ck, "test_name", 0, REG_BINARY, test_val)
367 ret_val, ret_type = QueryValueEx(ck, "test_name")
368 self.assertEqual(ret_type, REG_BINARY)
369 self.assertEqual(ret_val, test_val)
370 finally:
371 DeleteKey(HKEY_CURRENT_USER, test_key_name)
372
Steve Dower40fa2662016-12-17 13:30:27 -0800373 def test_read_string_containing_null(self):
374 # Test for issue 25778: REG_SZ should not contain null characters
375 try:
376 with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck:
377 self.assertNotEqual(ck.handle, 0)
378 test_val = "A string\x00 with a null"
379 SetValueEx(ck, "test_name", 0, REG_SZ, test_val)
380 ret_val, ret_type = QueryValueEx(ck, "test_name")
381 self.assertEqual(ret_type, REG_SZ)
382 self.assertEqual(ret_val, "A string")
383 finally:
384 DeleteKey(HKEY_CURRENT_USER, test_key_name)
Brian Curtin172e4222012-12-27 14:04:42 -0600385
Brian Curtin3035c392010-04-21 23:56:21 +0000386
387@unittest.skipUnless(REMOTE_NAME, "Skipping remote registry tests")
388class RemoteWinregTests(BaseWinregTests):
389
390 def test_remote_registry_works(self):
391 remote_key = ConnectRegistry(REMOTE_NAME, HKEY_CURRENT_USER)
392 self._test_all(remote_key)
393
394
395@unittest.skipUnless(WIN64_MACHINE, "x64 specific registry tests")
396class Win64WinregTests(BaseWinregTests):
397
Brian Curtin1771b542010-09-27 17:56:36 +0000398 def test_named_arguments(self):
399 self._test_named_args(HKEY_CURRENT_USER, test_key_name)
400 # Clean up and also exercise the named arguments
401 DeleteKeyEx(key=HKEY_CURRENT_USER, sub_key=test_key_name,
402 access=KEY_ALL_ACCESS, reserved=0)
403
Paul Monson62dfd7d2019-04-25 11:36:45 -0700404 @unittest.skipIf(win32_edition() in ('WindowsCoreHeadless', 'IoTEdgeOS'), "APIs not available on WindowsCoreHeadless")
Brian Curtin3035c392010-04-21 23:56:21 +0000405 def test_reflection_functions(self):
406 # Test that we can call the query, enable, and disable functions
407 # on a key which isn't on the reflection list with no consequences.
408 with OpenKey(HKEY_LOCAL_MACHINE, "Software") as key:
409 # HKLM\Software is redirected but not reflected in all OSes
410 self.assertTrue(QueryReflectionKey(key))
Raymond Hettinger7beae8a2011-01-06 05:34:17 +0000411 self.assertIsNone(EnableReflectionKey(key))
412 self.assertIsNone(DisableReflectionKey(key))
Brian Curtin3035c392010-04-21 23:56:21 +0000413 self.assertTrue(QueryReflectionKey(key))
414
415 @unittest.skipUnless(HAS_REFLECTION, "OS doesn't support reflection")
416 def test_reflection(self):
417 # Test that we can create, open, and delete keys in the 32-bit
418 # area. Because we are doing this in a key which gets reflected,
419 # test the differences of 32 and 64-bit keys before and after the
420 # reflection occurs (ie. when the created key is closed).
421 try:
422 with CreateKeyEx(HKEY_CURRENT_USER, test_reflect_key_name, 0,
423 KEY_ALL_ACCESS | KEY_WOW64_32KEY) as created_key:
424 self.assertNotEqual(created_key.handle, 0)
425
426 # The key should now be available in the 32-bit area
427 with OpenKey(HKEY_CURRENT_USER, test_reflect_key_name, 0,
428 KEY_ALL_ACCESS | KEY_WOW64_32KEY) as key:
429 self.assertNotEqual(key.handle, 0)
430
431 # Write a value to what currently is only in the 32-bit area
432 SetValueEx(created_key, "", 0, REG_SZ, "32KEY")
433
434 # The key is not reflected until created_key is closed.
435 # The 64-bit version of the key should not be available yet.
436 open_fail = lambda: OpenKey(HKEY_CURRENT_USER,
437 test_reflect_key_name, 0,
438 KEY_READ | KEY_WOW64_64KEY)
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200439 self.assertRaises(OSError, open_fail)
Brian Curtin3035c392010-04-21 23:56:21 +0000440
441 # Now explicitly open the 64-bit version of the key
442 with OpenKey(HKEY_CURRENT_USER, test_reflect_key_name, 0,
443 KEY_ALL_ACCESS | KEY_WOW64_64KEY) as key:
444 self.assertNotEqual(key.handle, 0)
445 # Make sure the original value we set is there
446 self.assertEqual("32KEY", QueryValue(key, ""))
447 # Set a new value, which will get reflected to 32-bit
448 SetValueEx(key, "", 0, REG_SZ, "64KEY")
449
450 # Reflection uses a "last-writer wins policy, so the value we set
451 # on the 64-bit key should be the same on 32-bit
452 with OpenKey(HKEY_CURRENT_USER, test_reflect_key_name, 0,
453 KEY_READ | KEY_WOW64_32KEY) as key:
454 self.assertEqual("64KEY", QueryValue(key, ""))
455 finally:
456 DeleteKeyEx(HKEY_CURRENT_USER, test_reflect_key_name,
457 KEY_WOW64_32KEY, 0)
458
459 @unittest.skipUnless(HAS_REFLECTION, "OS doesn't support reflection")
460 def test_disable_reflection(self):
461 # Make use of a key which gets redirected and reflected
462 try:
463 with CreateKeyEx(HKEY_CURRENT_USER, test_reflect_key_name, 0,
464 KEY_ALL_ACCESS | KEY_WOW64_32KEY) as created_key:
465 # QueryReflectionKey returns whether or not the key is disabled
466 disabled = QueryReflectionKey(created_key)
467 self.assertEqual(type(disabled), bool)
468 # HKCU\Software\Classes is reflected by default
469 self.assertFalse(disabled)
470
471 DisableReflectionKey(created_key)
472 self.assertTrue(QueryReflectionKey(created_key))
473
474 # The key is now closed and would normally be reflected to the
475 # 64-bit area, but let's make sure that didn't happen.
476 open_fail = lambda: OpenKeyEx(HKEY_CURRENT_USER,
477 test_reflect_key_name, 0,
478 KEY_READ | KEY_WOW64_64KEY)
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200479 self.assertRaises(OSError, open_fail)
Brian Curtin3035c392010-04-21 23:56:21 +0000480
481 # Make sure the 32-bit key is actually there
482 with OpenKeyEx(HKEY_CURRENT_USER, test_reflect_key_name, 0,
483 KEY_READ | KEY_WOW64_32KEY) as key:
484 self.assertNotEqual(key.handle, 0)
485 finally:
486 DeleteKeyEx(HKEY_CURRENT_USER, test_reflect_key_name,
487 KEY_WOW64_32KEY, 0)
488
Ezio Melotti28faf032013-05-04 17:46:23 +0300489 def test_exception_numbers(self):
490 with self.assertRaises(FileNotFoundError) as ctx:
491 QueryValue(HKEY_CLASSES_ROOT, 'some_value_that_does_not_exist')
Brian Curtin3035c392010-04-21 23:56:21 +0000492
Thomas Woutersed03b412007-08-28 21:37:11 +0000493def test_main():
Brian Curtin3035c392010-04-21 23:56:21 +0000494 support.run_unittest(LocalWinregTests, RemoteWinregTests,
495 Win64WinregTests)
Thomas Woutersed03b412007-08-28 21:37:11 +0000496
497if __name__ == "__main__":
Brian Curtin3035c392010-04-21 23:56:21 +0000498 if not REMOTE_NAME:
Thomas Woutersed03b412007-08-28 21:37:11 +0000499 print("Remote registry calls can be tested using",
500 "'test_winreg.py --remote \\\\machine_name'")
Thomas Woutersed03b412007-08-28 21:37:11 +0000501 test_main()