blob: 37ec2bc664cde1571c434b4b04689aac68ba458b [file] [log] [blame]
Martin v. Löwisf82d9b52007-09-03 07:43:05 +00001# -*- coding: utf-8 -*-
Guido van Rossumde598552000-03-28 20:36:51 +00002# Test the windows specific win32reg module.
3# Only win32reg functions not hit here: FlushKey, LoadKey and SaveKey
4
Guido van Rossumde598552000-03-28 20:36:51 +00005import os, sys
Thomas Woutersed03b412007-08-28 21:37:11 +00006import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00007from test import support
Brian Curtin9c9d8782010-05-26 18:09:32 +00008threading = support.import_module("threading")
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
11support.import_module('winreg')
12# Now import everything
13from winreg import *
14
Guido van Rossumde598552000-03-28 20:36:51 +000015test_key_name = "SOFTWARE\\Python Registry Test Key - Delete Me"
16
17test_data = [
18 ("Int Value", 45, REG_DWORD),
Guido van Rossum0a185522003-11-30 22:46:18 +000019 ("String Val", "A string value", REG_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000020 ("StringExpand", "The path is %path%", REG_EXPAND_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000021 ("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ),
Thomas Woutersed03b412007-08-28 21:37:11 +000022 ("Raw Data", b"binary\x00data", REG_BINARY),
Guido van Rossum291481b2003-12-03 15:24:02 +000023 ("Big String", "x"*(2**14-1), REG_SZ),
Guido van Rossuma8c360e2007-07-17 20:50:43 +000024 ("Big Binary", b"x"*(2**14), REG_BINARY),
Martin v. Löwisf82d9b52007-09-03 07:43:05 +000025 # Two and three kanjis, meaning: "Japan" and "Japanese")
26 ("Japanese 日本", "日本語", REG_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000027]
28
Thomas Woutersed03b412007-08-28 21:37:11 +000029class WinregTests(unittest.TestCase):
Guido van Rossumde598552000-03-28 20:36:51 +000030 remote_name = None
31
Martin v. Löwisb7a51562009-06-07 17:55:17 +000032 def setUp(self):
33 # Make sure that the test key is absent when the test
34 # starts.
35 self.delete_tree(HKEY_CURRENT_USER, test_key_name)
36
37 def delete_tree(self, root, subkey):
38 try:
39 hkey = OpenKey(root, subkey, KEY_ALL_ACCESS)
40 except WindowsError:
41 # subkey does not exist
42 return
43 while True:
44 try:
45 subsubkey = EnumKey(hkey, 0)
46 except WindowsError:
47 # no more subkeys
48 break
49 self.delete_tree(hkey, subsubkey)
50 CloseKey(hkey)
51 DeleteKey(root, subkey)
52
Martin v. Löwisf82d9b52007-09-03 07:43:05 +000053 def WriteTestData(self, root_key, subkeystr="sub_key"):
Thomas Woutersed03b412007-08-28 21:37:11 +000054 # Set the default value for this key.
55 SetValue(root_key, test_key_name, REG_SZ, "Default value")
56 key = CreateKey(root_key, test_key_name)
Georg Brandlab91fde2009-08-13 08:51:18 +000057 self.assertTrue(key.handle != 0)
Thomas Woutersed03b412007-08-28 21:37:11 +000058 # Create a sub-key
Martin v. Löwisf82d9b52007-09-03 07:43:05 +000059 sub_key = CreateKey(key, subkeystr)
Thomas Woutersed03b412007-08-28 21:37:11 +000060 # Give the sub-key some named values
61
62 for value_name, value_data, value_type in test_data:
63 SetValueEx(sub_key, value_name, 0, value_type, value_data)
64
65 # Check we wrote as many items as we thought.
66 nkeys, nvalues, since_mod = QueryInfoKey(key)
67 self.assertEquals(nkeys, 1, "Not the correct number of sub keys")
68 self.assertEquals(nvalues, 1, "Not the correct number of values")
69 nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
70 self.assertEquals(nkeys, 0, "Not the correct number of sub keys")
71 self.assertEquals(nvalues, len(test_data),
72 "Not the correct number of values")
73 # Close this key this way...
74 # (but before we do, copy the key as an integer - this allows
75 # us to test that the key really gets closed).
76 int_sub_key = int(sub_key)
77 CloseKey(sub_key)
78 try:
79 QueryInfoKey(int_sub_key)
80 self.fail("It appears the CloseKey() function does "
81 "not close the actual key!")
82 except EnvironmentError:
83 pass
84 # ... and close that key that way :-)
85 int_key = int(key)
86 key.Close()
87 try:
88 QueryInfoKey(int_key)
89 self.fail("It appears the key.Close() function "
90 "does not close the actual key!")
91 except EnvironmentError:
92 pass
93
Martin v. Löwisf82d9b52007-09-03 07:43:05 +000094 def ReadTestData(self, root_key, subkeystr="sub_key"):
Thomas Woutersed03b412007-08-28 21:37:11 +000095 # Check we can get default value for this key.
96 val = QueryValue(root_key, test_key_name)
97 self.assertEquals(val, "Default value",
98 "Registry didn't give back the correct value")
99
100 key = OpenKey(root_key, test_key_name)
101 # Read the sub-keys
Thomas Hellere83ebd92008-01-24 10:31:31 +0000102 with OpenKey(key, subkeystr) as sub_key:
Christian Heimes2380ac72008-01-09 00:17:24 +0000103 # Check I can enumerate over the values.
104 index = 0
105 while 1:
106 try:
107 data = EnumValue(sub_key, index)
108 except EnvironmentError:
109 break
110 self.assertEquals(data in test_data, True,
111 "Didn't read back the correct test data")
112 index = index + 1
113 self.assertEquals(index, len(test_data),
114 "Didn't read the correct number of items")
115 # Check I can directly access each item
116 for value_name, value_data, value_type in test_data:
117 read_val, read_typ = QueryValueEx(sub_key, value_name)
118 self.assertEquals(read_val, value_data,
119 "Could not directly read the value")
120 self.assertEquals(read_typ, value_type,
121 "Could not directly read the value")
Thomas Woutersed03b412007-08-28 21:37:11 +0000122 sub_key.Close()
123 # Enumerate our main key.
124 read_val = EnumKey(key, 0)
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000125 self.assertEquals(read_val, subkeystr, "Read subkey value wrong")
Thomas Woutersed03b412007-08-28 21:37:11 +0000126 try:
127 EnumKey(key, 1)
128 self.fail("Was able to get a second key when I only have one!")
129 except EnvironmentError:
130 pass
131
132 key.Close()
133
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000134 def DeleteTestData(self, root_key, subkeystr="sub_key"):
Thomas Woutersed03b412007-08-28 21:37:11 +0000135 key = OpenKey(root_key, test_key_name, 0, KEY_ALL_ACCESS)
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000136 sub_key = OpenKey(key, subkeystr, 0, KEY_ALL_ACCESS)
Thomas Woutersed03b412007-08-28 21:37:11 +0000137 # It is not necessary to delete the values before deleting
138 # the key (although subkeys must not exist). We delete them
139 # manually just to prove we can :-)
140 for value_name, value_data, value_type in test_data:
141 DeleteValue(sub_key, value_name)
142
143 nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
144 self.assertEquals(nkeys, 0, "subkey not empty before delete")
145 self.assertEquals(nvalues, 0, "subkey not empty before delete")
146 sub_key.Close()
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000147 DeleteKey(key, subkeystr)
Thomas Woutersed03b412007-08-28 21:37:11 +0000148
149 try:
150 # Shouldnt be able to delete it twice!
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000151 DeleteKey(key, subkeystr)
Thomas Woutersed03b412007-08-28 21:37:11 +0000152 self.fail("Deleting the key twice succeeded")
153 except EnvironmentError:
154 pass
155 key.Close()
156 DeleteKey(root_key, test_key_name)
157 # Opening should now fail!
158 try:
159 key = OpenKey(root_key, test_key_name)
160 self.fail("Could open the non-existent key")
161 except WindowsError: # Use this error name this time
162 pass
163
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000164 def TestAll(self, root_key, subkeystr="sub_key"):
165 self.WriteTestData(root_key, subkeystr)
166 self.ReadTestData(root_key, subkeystr)
167 self.DeleteTestData(root_key, subkeystr)
Thomas Woutersed03b412007-08-28 21:37:11 +0000168
169 def testLocalMachineRegistryWorks(self):
170 self.TestAll(HKEY_CURRENT_USER)
Martin v. Löwisf82d9b52007-09-03 07:43:05 +0000171 self.TestAll(HKEY_CURRENT_USER, "日本-subkey")
Thomas Woutersed03b412007-08-28 21:37:11 +0000172
173 def testConnectRegistryToLocalMachineWorks(self):
174 # perform minimal ConnectRegistry test which just invokes it
175 h = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
176 h.Close()
177
178 def testRemoteMachineRegistryWorks(self):
179 if not self.remote_name:
Thomas Wouters47b49bf2007-08-30 22:15:33 +0000180 return # remote machine name not specified
Thomas Woutersed03b412007-08-28 21:37:11 +0000181 remote_key = ConnectRegistry(self.remote_name, HKEY_CURRENT_USER)
182 self.TestAll(remote_key)
183
Christian Heimes2380ac72008-01-09 00:17:24 +0000184 def testExpandEnvironmentStrings(self):
185 r = ExpandEnvironmentStrings("%windir%\\test")
186 self.assertEqual(type(r), str)
187 self.assertEqual(r, os.environ["windir"] + "\\test")
188
Brian Curtin9c9d8782010-05-26 18:09:32 +0000189 def test_changing_value(self):
190 # Issue2810: A race condition in 2.6 and 3.1 may cause
191 # EnumValue or QueryValue to throw "WindowsError: More data is
192 # available"
193 done = False
194
195 class VeryActiveThread(threading.Thread):
196 def run(self):
197 with CreateKey(HKEY_CURRENT_USER, test_key_name) as key:
198 use_short = True
199 long_string = 'x'*2000
200 while not done:
201 s = 'x' if use_short else long_string
202 use_short = not use_short
203 SetValue(key, 'changing_value', REG_SZ, s)
204
205 thread = VeryActiveThread()
206 thread.start()
207 try:
208 with CreateKey(HKEY_CURRENT_USER,
209 test_key_name+'\\changing_value') as key:
210 for _ in range(1000):
211 num_subkeys, num_values, t = QueryInfoKey(key)
212 for i in range(num_values):
213 name = EnumValue(key, i)
214 QueryValue(key, name[0])
215 finally:
216 done = True
217 thread.join()
218 DeleteKey(HKEY_CURRENT_USER, test_key_name+'\\changing_value')
219 DeleteKey(HKEY_CURRENT_USER, test_key_name)
220
221 def test_long_key(self):
222 # Issue2810, in 2.6 and 3.1 when the key name was exactly 256
223 # characters, EnumKey threw "WindowsError: More data is
224 # available"
225 name = 'x'*256
226 try:
227 with CreateKey(HKEY_CURRENT_USER, test_key_name) as key:
228 SetValue(key, name, REG_SZ, 'x')
229 num_subkeys, num_values, t = QueryInfoKey(key)
230 EnumKey(key, 0)
231 finally:
232 DeleteKey(HKEY_CURRENT_USER, '\\'.join((test_key_name, name)))
233 DeleteKey(HKEY_CURRENT_USER, test_key_name)
234
235 def test_dynamic_key(self):
236 # Issue2810, when the value is dynamically generated, these
237 # throw "WindowsError: More data is available" in 2.6 and 3.1
238 EnumValue(HKEY_PERFORMANCE_DATA, 0)
239 QueryValueEx(HKEY_PERFORMANCE_DATA, "")
240
241
Thomas Woutersed03b412007-08-28 21:37:11 +0000242def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000243 support.run_unittest(WinregTests)
Thomas Woutersed03b412007-08-28 21:37:11 +0000244
245if __name__ == "__main__":
Guido van Rossumde598552000-03-28 20:36:51 +0000246 try:
Thomas Woutersed03b412007-08-28 21:37:11 +0000247 WinregTests.remote_name = sys.argv[sys.argv.index("--remote")+1]
248 except (IndexError, ValueError):
249 print("Remote registry calls can be tested using",
250 "'test_winreg.py --remote \\\\machine_name'")
251 WinregTests.remote_name = None
252 test_main()