blob: e2ef790b92061c7565abbd28d80daf98932b2deb [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
Fredrik Lundhf7850422001-01-17 21:51:36 +00007
R. David Murray597ebab2009-03-31 18:32:17 +00008# Do this first so test will be skipped if module doesn't exist
R. David Murray59beec32009-03-30 19:04:00 +00009test_support.import_module('_winreg')
R. David Murray597ebab2009-03-31 18:32:17 +000010# Now import everything
R. David Murray59beec32009-03-30 19:04:00 +000011from _winreg import *
12
Guido van Rossumde598552000-03-28 20:36:51 +000013test_key_name = "SOFTWARE\\Python Registry Test Key - Delete Me"
14
15test_data = [
16 ("Int Value", 45, REG_DWORD),
Guido van Rossum0a185522003-11-30 22:46:18 +000017 ("String Val", "A string value", REG_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000018 ("StringExpand", "The path is %path%", REG_EXPAND_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000019 ("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000020 ("Raw Data", ("binary"+chr(0)+"data"), REG_BINARY),
Guido van Rossum291481b2003-12-03 15:24:02 +000021 ("Big String", "x"*(2**14-1), REG_SZ),
22 ("Big Binary", "x"*(2**14), REG_BINARY),
Guido van Rossumde598552000-03-28 20:36:51 +000023]
Georg Brandl3376a9a2007-08-24 17:38:49 +000024
25if test_support.have_unicode:
26 test_data += [
27 (unicode("Unicode Val"), unicode("A Unicode value"), REG_SZ,),
28 ("UnicodeExpand", unicode("The path is %path%"), REG_EXPAND_SZ),
29 ("Multi-unicode", [unicode("Lots"), unicode("of"), unicode("unicode"),
30 unicode("values")], REG_MULTI_SZ),
31 ("Multi-mixed", [unicode("Unicode"), unicode("and"), "string",
32 "values"], REG_MULTI_SZ),
Martin v. Löwis339d0f72001-08-17 18:39:25 +000033 ]
Guido van Rossumde598552000-03-28 20:36:51 +000034
Georg Brandl3376a9a2007-08-24 17:38:49 +000035class WinregTests(unittest.TestCase):
Guido van Rossumde598552000-03-28 20:36:51 +000036 remote_name = None
37
Benjamin Peterson64092a52009-06-07 23:12:44 +000038 def setUp(self):
39 # Make sure that the test key is absent when the test
40 # starts.
41 self.delete_tree(HKEY_CURRENT_USER, test_key_name)
42
43 def delete_tree(self, root, subkey):
44 try:
45 hkey = OpenKey(root, subkey, KEY_ALL_ACCESS)
46 except WindowsError:
47 # subkey does not exist
48 return
49 while True:
50 try:
51 subsubkey = EnumKey(hkey, 0)
52 except WindowsError:
53 # no more subkeys
54 break
55 self.delete_tree(hkey, subsubkey)
56 CloseKey(hkey)
57 DeleteKey(root, subkey)
58
Georg Brandl3376a9a2007-08-24 17:38:49 +000059 def WriteTestData(self, root_key):
60 # Set the default value for this key.
61 SetValue(root_key, test_key_name, REG_SZ, "Default value")
62 key = CreateKey(root_key, test_key_name)
63 # Create a sub-key
64 sub_key = CreateKey(key, "sub_key")
65 # Give the sub-key some named values
66
67 for value_name, value_data, value_type in test_data:
68 SetValueEx(sub_key, value_name, 0, value_type, value_data)
69
70 # Check we wrote as many items as we thought.
71 nkeys, nvalues, since_mod = QueryInfoKey(key)
72 self.assertEquals(nkeys, 1, "Not the correct number of sub keys")
73 self.assertEquals(nvalues, 1, "Not the correct number of values")
74 nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
75 self.assertEquals(nkeys, 0, "Not the correct number of sub keys")
76 self.assertEquals(nvalues, len(test_data),
77 "Not the correct number of values")
78 # Close this key this way...
79 # (but before we do, copy the key as an integer - this allows
80 # us to test that the key really gets closed).
81 int_sub_key = int(sub_key)
82 CloseKey(sub_key)
83 try:
84 QueryInfoKey(int_sub_key)
85 self.fail("It appears the CloseKey() function does "
86 "not close the actual key!")
87 except EnvironmentError:
88 pass
89 # ... and close that key that way :-)
90 int_key = int(key)
91 key.Close()
92 try:
93 QueryInfoKey(int_key)
94 self.fail("It appears the key.Close() function "
95 "does not close the actual key!")
96 except EnvironmentError:
97 pass
98
99 def ReadTestData(self, root_key):
100 # Check we can get default value for this key.
101 val = QueryValue(root_key, test_key_name)
102 self.assertEquals(val, "Default value",
103 "Registry didn't give back the correct value")
104
105 key = OpenKey(root_key, test_key_name)
106 # Read the sub-keys
Christian Heimesb39a7562008-01-08 15:46:10 +0000107 with OpenKey(key, "sub_key") as sub_key:
108 # Check I can enumerate over the values.
109 index = 0
110 while 1:
111 try:
112 data = EnumValue(sub_key, index)
113 except EnvironmentError:
114 break
Ezio Melottiaa980582010-01-23 23:04:36 +0000115 self.assertIn(data, test_data,
116 "Didn't read back the correct test data")
Christian Heimesb39a7562008-01-08 15:46:10 +0000117 index = index + 1
118 self.assertEquals(index, len(test_data),
119 "Didn't read the correct number of items")
120 # Check I can directly access each item
121 for value_name, value_data, value_type in test_data:
122 read_val, read_typ = QueryValueEx(sub_key, value_name)
123 self.assertEquals(read_val, value_data,
124 "Could not directly read the value")
125 self.assertEquals(read_typ, value_type,
126 "Could not directly read the value")
Georg Brandl3376a9a2007-08-24 17:38:49 +0000127 sub_key.Close()
128 # Enumerate our main key.
129 read_val = EnumKey(key, 0)
130 self.assertEquals(read_val, "sub_key", "Read subkey value wrong")
131 try:
132 EnumKey(key, 1)
133 self.fail("Was able to get a second key when I only have one!")
134 except EnvironmentError:
135 pass
136
137 key.Close()
138
139 def DeleteTestData(self, root_key):
140 key = OpenKey(root_key, test_key_name, 0, KEY_ALL_ACCESS)
141 sub_key = OpenKey(key, "sub_key", 0, KEY_ALL_ACCESS)
142 # It is not necessary to delete the values before deleting
143 # the key (although subkeys must not exist). We delete them
144 # manually just to prove we can :-)
145 for value_name, value_data, value_type in test_data:
146 DeleteValue(sub_key, value_name)
147
148 nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
149 self.assertEquals(nkeys, 0, "subkey not empty before delete")
150 self.assertEquals(nvalues, 0, "subkey not empty before delete")
151 sub_key.Close()
152 DeleteKey(key, "sub_key")
153
154 try:
155 # Shouldnt be able to delete it twice!
156 DeleteKey(key, "sub_key")
157 self.fail("Deleting the key twice succeeded")
158 except EnvironmentError:
159 pass
160 key.Close()
161 DeleteKey(root_key, test_key_name)
162 # Opening should now fail!
163 try:
164 key = OpenKey(root_key, test_key_name)
165 self.fail("Could open the non-existent key")
166 except WindowsError: # Use this error name this time
167 pass
168
169 def TestAll(self, root_key):
170 self.WriteTestData(root_key)
171 self.ReadTestData(root_key)
172 self.DeleteTestData(root_key)
173
174 def testLocalMachineRegistryWorks(self):
175 self.TestAll(HKEY_CURRENT_USER)
176
177 def testConnectRegistryToLocalMachineWorks(self):
178 # perform minimal ConnectRegistry test which just invokes it
179 h = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
180 h.Close()
181
182 def testRemoteMachineRegistryWorks(self):
183 if not self.remote_name:
Georg Brandl0226d852007-08-30 12:32:23 +0000184 return # remote machine name not specified
Georg Brandl3376a9a2007-08-24 17:38:49 +0000185 remote_key = ConnectRegistry(self.remote_name, HKEY_CURRENT_USER)
186 self.TestAll(remote_key)
187
Christian Heimesb39a7562008-01-08 15:46:10 +0000188 def testExpandEnvironmentStrings(self):
189 r = ExpandEnvironmentStrings(u"%windir%\\test")
190 self.assertEqual(type(r), unicode)
191 self.assertEqual(r, os.environ["windir"] + "\\test")
192
Georg Brandl3376a9a2007-08-24 17:38:49 +0000193def test_main():
194 test_support.run_unittest(WinregTests)
195
196if __name__ == "__main__":
Guido van Rossumde598552000-03-28 20:36:51 +0000197 try:
Georg Brandl3376a9a2007-08-24 17:38:49 +0000198 WinregTests.remote_name = sys.argv[sys.argv.index("--remote")+1]
199 except (IndexError, ValueError):
200 print "Remote registry calls can be tested using",
201 print "'test_winreg.py --remote \\\\machine_name'"
202 WinregTests.remote_name = None
203 test_main()