blob: 1eaa93047e79208cd3bcab862f163c080405ed07 [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
Georg Brandl3376a9a2007-08-24 17:38:49 +000038 def WriteTestData(self, root_key):
39 # Set the default value for this key.
40 SetValue(root_key, test_key_name, REG_SZ, "Default value")
41 key = CreateKey(root_key, test_key_name)
42 # Create a sub-key
43 sub_key = CreateKey(key, "sub_key")
44 # Give the sub-key some named values
45
46 for value_name, value_data, value_type in test_data:
47 SetValueEx(sub_key, value_name, 0, value_type, value_data)
48
49 # Check we wrote as many items as we thought.
50 nkeys, nvalues, since_mod = QueryInfoKey(key)
51 self.assertEquals(nkeys, 1, "Not the correct number of sub keys")
52 self.assertEquals(nvalues, 1, "Not the correct number of values")
53 nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
54 self.assertEquals(nkeys, 0, "Not the correct number of sub keys")
55 self.assertEquals(nvalues, len(test_data),
56 "Not the correct number of values")
57 # Close this key this way...
58 # (but before we do, copy the key as an integer - this allows
59 # us to test that the key really gets closed).
60 int_sub_key = int(sub_key)
61 CloseKey(sub_key)
62 try:
63 QueryInfoKey(int_sub_key)
64 self.fail("It appears the CloseKey() function does "
65 "not close the actual key!")
66 except EnvironmentError:
67 pass
68 # ... and close that key that way :-)
69 int_key = int(key)
70 key.Close()
71 try:
72 QueryInfoKey(int_key)
73 self.fail("It appears the key.Close() function "
74 "does not close the actual key!")
75 except EnvironmentError:
76 pass
77
78 def ReadTestData(self, root_key):
79 # Check we can get default value for this key.
80 val = QueryValue(root_key, test_key_name)
81 self.assertEquals(val, "Default value",
82 "Registry didn't give back the correct value")
83
84 key = OpenKey(root_key, test_key_name)
85 # Read the sub-keys
Christian Heimesb39a7562008-01-08 15:46:10 +000086 with OpenKey(key, "sub_key") as sub_key:
87 # Check I can enumerate over the values.
88 index = 0
89 while 1:
90 try:
91 data = EnumValue(sub_key, index)
92 except EnvironmentError:
93 break
94 self.assertEquals(data in test_data, True,
95 "Didn't read back the correct test data")
96 index = index + 1
97 self.assertEquals(index, len(test_data),
98 "Didn't read the correct number of items")
99 # Check I can directly access each item
100 for value_name, value_data, value_type in test_data:
101 read_val, read_typ = QueryValueEx(sub_key, value_name)
102 self.assertEquals(read_val, value_data,
103 "Could not directly read the value")
104 self.assertEquals(read_typ, value_type,
105 "Could not directly read the value")
Georg Brandl3376a9a2007-08-24 17:38:49 +0000106 sub_key.Close()
107 # Enumerate our main key.
108 read_val = EnumKey(key, 0)
109 self.assertEquals(read_val, "sub_key", "Read subkey value wrong")
110 try:
111 EnumKey(key, 1)
112 self.fail("Was able to get a second key when I only have one!")
113 except EnvironmentError:
114 pass
115
116 key.Close()
117
118 def DeleteTestData(self, root_key):
119 key = OpenKey(root_key, test_key_name, 0, KEY_ALL_ACCESS)
120 sub_key = OpenKey(key, "sub_key", 0, KEY_ALL_ACCESS)
121 # It is not necessary to delete the values before deleting
122 # the key (although subkeys must not exist). We delete them
123 # manually just to prove we can :-)
124 for value_name, value_data, value_type in test_data:
125 DeleteValue(sub_key, value_name)
126
127 nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
128 self.assertEquals(nkeys, 0, "subkey not empty before delete")
129 self.assertEquals(nvalues, 0, "subkey not empty before delete")
130 sub_key.Close()
131 DeleteKey(key, "sub_key")
132
133 try:
134 # Shouldnt be able to delete it twice!
135 DeleteKey(key, "sub_key")
136 self.fail("Deleting the key twice succeeded")
137 except EnvironmentError:
138 pass
139 key.Close()
140 DeleteKey(root_key, test_key_name)
141 # Opening should now fail!
142 try:
143 key = OpenKey(root_key, test_key_name)
144 self.fail("Could open the non-existent key")
145 except WindowsError: # Use this error name this time
146 pass
147
148 def TestAll(self, root_key):
149 self.WriteTestData(root_key)
150 self.ReadTestData(root_key)
151 self.DeleteTestData(root_key)
152
153 def testLocalMachineRegistryWorks(self):
154 self.TestAll(HKEY_CURRENT_USER)
155
156 def testConnectRegistryToLocalMachineWorks(self):
157 # perform minimal ConnectRegistry test which just invokes it
158 h = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
159 h.Close()
160
161 def testRemoteMachineRegistryWorks(self):
162 if not self.remote_name:
Georg Brandl0226d852007-08-30 12:32:23 +0000163 return # remote machine name not specified
Georg Brandl3376a9a2007-08-24 17:38:49 +0000164 remote_key = ConnectRegistry(self.remote_name, HKEY_CURRENT_USER)
165 self.TestAll(remote_key)
166
Christian Heimesb39a7562008-01-08 15:46:10 +0000167 def testExpandEnvironmentStrings(self):
168 r = ExpandEnvironmentStrings(u"%windir%\\test")
169 self.assertEqual(type(r), unicode)
170 self.assertEqual(r, os.environ["windir"] + "\\test")
171
Georg Brandl3376a9a2007-08-24 17:38:49 +0000172def test_main():
173 test_support.run_unittest(WinregTests)
174
175if __name__ == "__main__":
Guido van Rossumde598552000-03-28 20:36:51 +0000176 try:
Georg Brandl3376a9a2007-08-24 17:38:49 +0000177 WinregTests.remote_name = sys.argv[sys.argv.index("--remote")+1]
178 except (IndexError, ValueError):
179 print "Remote registry calls can be tested using",
180 print "'test_winreg.py --remote \\\\machine_name'"
181 WinregTests.remote_name = None
182 test_main()