blob: c1a3d188b97cce2d8a8bc711339a28c4bbfcdfe8 [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
Guido van Rossumde598552000-03-28 20:36:51 +00004import os, sys
Georg Brandl3376a9a2007-08-24 17:38:49 +00005import unittest
Georg Brandl3376a9a2007-08-24 17:38:49 +00006from test import test_support
Brian Curtine33fa882010-04-02 21:18:14 +00007from platform import machine
Fredrik Lundhf7850422001-01-17 21:51:36 +00008
R. David Murray597ebab2009-03-31 18:32:17 +00009# Do this first so test will be skipped if module doesn't exist
R. David Murray59beec32009-03-30 19:04:00 +000010test_support.import_module('_winreg')
R. David Murray597ebab2009-03-31 18:32:17 +000011# Now import everything
R. David Murray59beec32009-03-30 19:04:00 +000012from _winreg import *
13
Brian Curtine33fa882010-04-02 21:18:14 +000014try:
15 REMOTE_NAME = sys.argv[sys.argv.index("--remote")+1]
16except (IndexError, ValueError):
17 REMOTE_NAME = None
18
19# tuple of (major, minor)
20WIN_VER = sys.getwindowsversion()[:2]
21# Some tests should only run on 64-bit architectures where WOW64 will be.
22WIN64_MACHINE = True if machine() == "AMD64" else False
23
24# Starting with Windows 7 and Windows Server 2008 R2, WOW64 no longer uses
25# registry reflection and formerly reflected keys are shared instead.
26# Windows 7 and Windows Server 2008 R2 are version 6.1. Due to this, some
27# tests are only valid up until 6.1
28HAS_REFLECTION = True if WIN_VER < (6, 1) else False
29
Guido van Rossumde598552000-03-28 20:36:51 +000030test_key_name = "SOFTWARE\\Python Registry Test Key - Delete Me"
Brian Curtine33fa882010-04-02 21:18:14 +000031# On OS'es that support reflection we should test with a reflected key
32test_reflect_key_name = "SOFTWARE\\Classes\\Python Test Key - Delete Me"
Guido van Rossumde598552000-03-28 20:36:51 +000033
34test_data = [
35 ("Int Value", 45, REG_DWORD),
Guido van Rossum0a185522003-11-30 22:46:18 +000036 ("String Val", "A string value", REG_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000037 ("StringExpand", "The path is %path%", REG_EXPAND_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000038 ("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000039 ("Raw Data", ("binary"+chr(0)+"data"), REG_BINARY),
Guido van Rossum291481b2003-12-03 15:24:02 +000040 ("Big String", "x"*(2**14-1), REG_SZ),
41 ("Big Binary", "x"*(2**14), REG_BINARY),
Guido van Rossumde598552000-03-28 20:36:51 +000042]
Georg Brandl3376a9a2007-08-24 17:38:49 +000043
44if test_support.have_unicode:
45 test_data += [
46 (unicode("Unicode Val"), unicode("A Unicode value"), REG_SZ,),
47 ("UnicodeExpand", unicode("The path is %path%"), REG_EXPAND_SZ),
48 ("Multi-unicode", [unicode("Lots"), unicode("of"), unicode("unicode"),
49 unicode("values")], REG_MULTI_SZ),
50 ("Multi-mixed", [unicode("Unicode"), unicode("and"), "string",
51 "values"], REG_MULTI_SZ),
Martin v. Löwis339d0f72001-08-17 18:39:25 +000052 ]
Guido van Rossumde598552000-03-28 20:36:51 +000053
Brian Curtine33fa882010-04-02 21:18:14 +000054class BaseWinregTests(unittest.TestCase):
Guido van Rossumde598552000-03-28 20:36:51 +000055
Benjamin Peterson64092a52009-06-07 23:12:44 +000056 def setUp(self):
57 # Make sure that the test key is absent when the test
58 # starts.
59 self.delete_tree(HKEY_CURRENT_USER, test_key_name)
60
61 def delete_tree(self, root, subkey):
62 try:
63 hkey = OpenKey(root, subkey, KEY_ALL_ACCESS)
64 except WindowsError:
65 # subkey does not exist
66 return
67 while True:
68 try:
69 subsubkey = EnumKey(hkey, 0)
70 except WindowsError:
71 # no more subkeys
72 break
73 self.delete_tree(hkey, subsubkey)
74 CloseKey(hkey)
75 DeleteKey(root, subkey)
76
Brian Curtine33fa882010-04-02 21:18:14 +000077 def _write_test_data(self, root_key, CreateKey=CreateKey):
Georg Brandl3376a9a2007-08-24 17:38:49 +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)
81 # Create a sub-key
82 sub_key = CreateKey(key, "sub_key")
83 # Give the sub-key some named values
84
85 for value_name, value_data, value_type in test_data:
86 SetValueEx(sub_key, value_name, 0, value_type, value_data)
87
88 # Check we wrote as many items as we thought.
89 nkeys, nvalues, since_mod = QueryInfoKey(key)
90 self.assertEquals(nkeys, 1, "Not the correct number of sub keys")
91 self.assertEquals(nvalues, 1, "Not the correct number of values")
92 nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
93 self.assertEquals(nkeys, 0, "Not the correct number of sub keys")
94 self.assertEquals(nvalues, len(test_data),
95 "Not the correct number of values")
96 # Close this key this way...
97 # (but before we do, copy the key as an integer - this allows
98 # us to test that the key really gets closed).
99 int_sub_key = int(sub_key)
100 CloseKey(sub_key)
101 try:
102 QueryInfoKey(int_sub_key)
103 self.fail("It appears the CloseKey() function does "
104 "not close the actual key!")
105 except EnvironmentError:
106 pass
107 # ... and close that key that way :-)
108 int_key = int(key)
109 key.Close()
110 try:
111 QueryInfoKey(int_key)
112 self.fail("It appears the key.Close() function "
113 "does not close the actual key!")
114 except EnvironmentError:
115 pass
116
Brian Curtine33fa882010-04-02 21:18:14 +0000117 def _read_test_data(self, root_key, OpenKey=OpenKey):
Georg Brandl3376a9a2007-08-24 17:38:49 +0000118 # Check we can get default value for this key.
119 val = QueryValue(root_key, test_key_name)
120 self.assertEquals(val, "Default value",
121 "Registry didn't give back the correct value")
122
123 key = OpenKey(root_key, test_key_name)
124 # Read the sub-keys
Christian Heimesb39a7562008-01-08 15:46:10 +0000125 with OpenKey(key, "sub_key") as sub_key:
126 # Check I can enumerate over the values.
127 index = 0
128 while 1:
129 try:
130 data = EnumValue(sub_key, index)
131 except EnvironmentError:
132 break
Ezio Melottiaa980582010-01-23 23:04:36 +0000133 self.assertIn(data, test_data,
134 "Didn't read back the correct test data")
Christian Heimesb39a7562008-01-08 15:46:10 +0000135 index = index + 1
136 self.assertEquals(index, len(test_data),
137 "Didn't read the correct number of items")
138 # Check I can directly access each item
139 for value_name, value_data, value_type in test_data:
140 read_val, read_typ = QueryValueEx(sub_key, value_name)
141 self.assertEquals(read_val, value_data,
142 "Could not directly read the value")
143 self.assertEquals(read_typ, value_type,
144 "Could not directly read the value")
Georg Brandl3376a9a2007-08-24 17:38:49 +0000145 sub_key.Close()
146 # Enumerate our main key.
147 read_val = EnumKey(key, 0)
148 self.assertEquals(read_val, "sub_key", "Read subkey value wrong")
149 try:
150 EnumKey(key, 1)
151 self.fail("Was able to get a second key when I only have one!")
152 except EnvironmentError:
153 pass
154
155 key.Close()
156
Brian Curtine33fa882010-04-02 21:18:14 +0000157 def _delete_test_data(self, root_key):
Georg Brandl3376a9a2007-08-24 17:38:49 +0000158 key = OpenKey(root_key, test_key_name, 0, KEY_ALL_ACCESS)
159 sub_key = OpenKey(key, "sub_key", 0, KEY_ALL_ACCESS)
160 # It is not necessary to delete the values before deleting
161 # the key (although subkeys must not exist). We delete them
162 # manually just to prove we can :-)
163 for value_name, value_data, value_type in test_data:
164 DeleteValue(sub_key, value_name)
165
166 nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
167 self.assertEquals(nkeys, 0, "subkey not empty before delete")
168 self.assertEquals(nvalues, 0, "subkey not empty before delete")
169 sub_key.Close()
170 DeleteKey(key, "sub_key")
171
172 try:
173 # Shouldnt be able to delete it twice!
174 DeleteKey(key, "sub_key")
175 self.fail("Deleting the key twice succeeded")
176 except EnvironmentError:
177 pass
178 key.Close()
179 DeleteKey(root_key, test_key_name)
180 # Opening should now fail!
181 try:
182 key = OpenKey(root_key, test_key_name)
183 self.fail("Could open the non-existent key")
184 except WindowsError: # Use this error name this time
185 pass
186
Brian Curtine33fa882010-04-02 21:18:14 +0000187 def _test_all(self, root_key):
188 self._write_test_data(root_key)
189 self._read_test_data(root_key)
190 self._delete_test_data(root_key)
Georg Brandl3376a9a2007-08-24 17:38:49 +0000191
Brian Curtine33fa882010-04-02 21:18:14 +0000192class LocalWinregTests(BaseWinregTests):
Georg Brandl3376a9a2007-08-24 17:38:49 +0000193
Brian Curtine33fa882010-04-02 21:18:14 +0000194 def test_registry_works(self):
195 self._test_all(HKEY_CURRENT_USER)
196
197 def test_registry_works_extended_functions(self):
198 # Substitute the regular CreateKey and OpenKey calls with their
199 # extended counterparts.
200 # Note: DeleteKeyEx is not used here because it is platform dependent
201 cke = lambda key, sub_key: CreateKeyEx(key, sub_key, 0, KEY_ALL_ACCESS)
202 self._write_test_data(HKEY_CURRENT_USER, cke)
203
204 oke = lambda key, sub_key: OpenKeyEx(key, sub_key, 0, KEY_READ)
205 self._read_test_data(HKEY_CURRENT_USER, oke)
206
207 self._delete_test_data(HKEY_CURRENT_USER)
208
209 def test_connect_registry_to_local_machine_works(self):
Georg Brandl3376a9a2007-08-24 17:38:49 +0000210 # perform minimal ConnectRegistry test which just invokes it
211 h = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
Brian Curtine33fa882010-04-02 21:18:14 +0000212 self.assertNotEqual(h.handle, 0)
Georg Brandl3376a9a2007-08-24 17:38:49 +0000213 h.Close()
Brian Curtine33fa882010-04-02 21:18:14 +0000214 self.assertEqual(h.handle, 0)
Georg Brandl3376a9a2007-08-24 17:38:49 +0000215
Brian Curtine33fa882010-04-02 21:18:14 +0000216 def test_inexistant_remote_registry(self):
217 connect = lambda: ConnectRegistry("abcdefghijkl", HKEY_CURRENT_USER)
218 self.assertRaises(WindowsError, connect)
Georg Brandl3376a9a2007-08-24 17:38:49 +0000219
Brian Curtine33fa882010-04-02 21:18:14 +0000220 def test_expand_environment_strings(self):
Christian Heimesb39a7562008-01-08 15:46:10 +0000221 r = ExpandEnvironmentStrings(u"%windir%\\test")
222 self.assertEqual(type(r), unicode)
223 self.assertEqual(r, os.environ["windir"] + "\\test")
224
Brian Curtine33fa882010-04-02 21:18:14 +0000225 def test_context_manager(self):
226 # ensure that the handle is closed if an exception occurs
227 try:
228 with ConnectRegistry(None, HKEY_LOCAL_MACHINE) as h:
229 self.assertNotEqual(h.handle, 0)
230 raise WindowsError
231 except WindowsError:
232 self.assertEqual(h.handle, 0)
233
234 # Reflection requires XP x64/Vista at a minimum. XP doesn't have this stuff
235 # or DeleteKeyEx so make sure their use raises NotImplementedError
236 @unittest.skipUnless(WIN_VER < (5, 2), "Requires Windows XP")
237 def test_reflection_unsupported(self):
238 try:
239 with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck:
240 self.assertNotEqual(ck.handle, 0)
241
242 key = OpenKey(HKEY_CURRENT_USER, test_key_name)
243 self.assertNotEqual(key.handle, 0)
244
245 self.assertRaises(NotImplementedError, DisableReflectionKey(key))
246 self.assertRaises(NotImplementedError, EnableReflectionKey(key))
247 self.assertRaises(NotImplementedError, QueryReflectionKey(key))
248 self.assertRaises(NotImplementedError,
249 DeleteKeyEx(HKEY_CURRENT_USER, test_key_name))
250 finally:
251 DeleteKey(HKEY_CURRENT_USER, test_key_name)
252
253
254@unittest.skipUnless(REMOTE_NAME, "Skipping remote registry tests")
255class RemoteWinregTests(BaseWinregTests):
256
257 def test_remote_registry_works(self):
258 remote_key = ConnectRegistry(REMOTE_NAME, HKEY_CURRENT_USER)
259 self._test_all(remote_key)
260
261
262@unittest.skipUnless(WIN64_MACHINE, "x64 specific registry tests")
263class Win64WinregTests(BaseWinregTests):
264
265 def test_reflection_functions(self):
266 # Test that we can call the query, enable, and disable functions
267 # on a key which isn't on the reflection list with no consequences.
268 with OpenKey(HKEY_LOCAL_MACHINE, "Software") as key:
269 # HKLM\Software is redirected but not reflected in all OSes
270 self.assertTrue(QueryReflectionKey(key))
271 self.assertEquals(None, EnableReflectionKey(key))
272 self.assertEquals(None, DisableReflectionKey(key))
273 self.assertTrue(QueryReflectionKey(key))
274
275 @unittest.skipUnless(HAS_REFLECTION, "OS doesn't support reflection")
276 def test_reflection(self):
277 # Test that we can create, open, and delete keys in the 32-bit
278 # area. Because we are doing this in a key which gets reflected,
279 # test the differences of 32 and 64-bit keys before and after the
280 # reflection occurs (ie. when the created key is closed).
281 try:
282 with CreateKeyEx(HKEY_CURRENT_USER, test_reflect_key_name, 0,
283 KEY_ALL_ACCESS | KEY_WOW64_32KEY) as created_key:
284 self.assertNotEqual(created_key.handle, 0)
285
286 # The key should now be available in the 32-bit area
287 with OpenKey(HKEY_CURRENT_USER, test_reflect_key_name, 0,
288 KEY_ALL_ACCESS | KEY_WOW64_32KEY) as key:
289 self.assertNotEqual(key.handle, 0)
290
291 # Write a value to what currently is only in the 32-bit area
292 SetValueEx(created_key, "", 0, REG_SZ, "32KEY")
293
294 # The key is not reflected until created_key is closed.
295 # The 64-bit version of the key should not be available yet.
296 open_fail = lambda: OpenKey(HKEY_CURRENT_USER,
297 test_reflect_key_name, 0,
298 KEY_READ | KEY_WOW64_64KEY)
299 self.assertRaises(WindowsError, open_fail)
300
301 # Now explicitly open the 64-bit version of the key
302 with OpenKey(HKEY_CURRENT_USER, test_reflect_key_name, 0,
303 KEY_ALL_ACCESS | KEY_WOW64_64KEY) as key:
304 self.assertNotEqual(key.handle, 0)
305 # Make sure the original value we set is there
306 self.assertEqual("32KEY", QueryValue(key, ""))
307 # Set a new value, which will get reflected to 32-bit
308 SetValueEx(key, "", 0, REG_SZ, "64KEY")
309
310 # Reflection uses a "last-writer wins policy, so the value we set
311 # on the 64-bit key should be the same on 32-bit
312 with OpenKey(HKEY_CURRENT_USER, test_reflect_key_name, 0,
313 KEY_READ | KEY_WOW64_32KEY) as key:
314 self.assertEqual("64KEY", QueryValue(key, ""))
315 finally:
316 DeleteKeyEx(HKEY_CURRENT_USER, test_reflect_key_name,
317 KEY_WOW64_32KEY, 0)
318
319 @unittest.skipUnless(HAS_REFLECTION, "OS doesn't support reflection")
320 def test_disable_reflection(self):
321 # Make use of a key which gets redirected and reflected
322 try:
323 with CreateKeyEx(HKEY_CURRENT_USER, test_reflect_key_name, 0,
324 KEY_ALL_ACCESS | KEY_WOW64_32KEY) as created_key:
325 # QueryReflectionKey returns whether or not the key is disabled
326 disabled = QueryReflectionKey(created_key)
327 self.assertEqual(type(disabled), bool)
328 # HKCU\Software\Classes is reflected by default
329 self.assertFalse(disabled)
330
331 DisableReflectionKey(created_key)
332 self.assertTrue(QueryReflectionKey(created_key))
333
334 # The key is now closed and would normally be reflected to the
335 # 64-bit area, but let's make sure that didn't happen.
336 open_fail = lambda: OpenKeyEx(HKEY_CURRENT_USER,
337 test_reflect_key_name, 0,
338 KEY_READ | KEY_WOW64_64KEY)
339 self.assertRaises(WindowsError, open_fail)
340
341 # Make sure the 32-bit key is actually there
342 with OpenKeyEx(HKEY_CURRENT_USER, test_reflect_key_name, 0,
343 KEY_READ | KEY_WOW64_32KEY) as key:
344 self.assertNotEqual(key.handle, 0)
345 finally:
346 DeleteKeyEx(HKEY_CURRENT_USER, test_reflect_key_name,
347 KEY_WOW64_32KEY, 0)
348
349
Georg Brandl3376a9a2007-08-24 17:38:49 +0000350def test_main():
Brian Curtine33fa882010-04-02 21:18:14 +0000351 test_support.run_unittest(LocalWinregTests, RemoteWinregTests,
352 Win64WinregTests)
Georg Brandl3376a9a2007-08-24 17:38:49 +0000353
354if __name__ == "__main__":
Brian Curtine33fa882010-04-02 21:18:14 +0000355 if not REMOTE_NAME:
Georg Brandl3376a9a2007-08-24 17:38:49 +0000356 print "Remote registry calls can be tested using",
357 print "'test_winreg.py --remote \\\\machine_name'"
Georg Brandl3376a9a2007-08-24 17:38:49 +0000358 test_main()