blob: 85a0aacb95d8594c5136a18681c36bb56d616214 [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
Fred Drake252af9c2000-06-29 19:42:00 +00004from _winreg import *
Guido van Rossumde598552000-03-28 20:36:51 +00005import os, sys
Georg Brandl3376a9a2007-08-24 17:38:49 +00006import unittest
Guido van Rossumde598552000-03-28 20:36:51 +00007
Georg Brandl3376a9a2007-08-24 17:38:49 +00008from test import test_support
Fredrik Lundhf7850422001-01-17 21:51:36 +00009
Guido van Rossumde598552000-03-28 20:36:51 +000010test_key_name = "SOFTWARE\\Python Registry Test Key - Delete Me"
11
12test_data = [
13 ("Int Value", 45, REG_DWORD),
Guido van Rossum0a185522003-11-30 22:46:18 +000014 ("String Val", "A string value", REG_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000015 ("StringExpand", "The path is %path%", REG_EXPAND_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000016 ("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000017 ("Raw Data", ("binary"+chr(0)+"data"), REG_BINARY),
Guido van Rossum291481b2003-12-03 15:24:02 +000018 ("Big String", "x"*(2**14-1), REG_SZ),
19 ("Big Binary", "x"*(2**14), REG_BINARY),
Guido van Rossumde598552000-03-28 20:36:51 +000020]
Georg Brandl3376a9a2007-08-24 17:38:49 +000021
22if test_support.have_unicode:
23 test_data += [
24 (unicode("Unicode Val"), unicode("A Unicode value"), REG_SZ,),
25 ("UnicodeExpand", unicode("The path is %path%"), REG_EXPAND_SZ),
26 ("Multi-unicode", [unicode("Lots"), unicode("of"), unicode("unicode"),
27 unicode("values")], REG_MULTI_SZ),
28 ("Multi-mixed", [unicode("Unicode"), unicode("and"), "string",
29 "values"], REG_MULTI_SZ),
Martin v. Löwis339d0f72001-08-17 18:39:25 +000030 ]
Guido van Rossumde598552000-03-28 20:36:51 +000031
Georg Brandl3376a9a2007-08-24 17:38:49 +000032class WinregTests(unittest.TestCase):
Guido van Rossumde598552000-03-28 20:36:51 +000033 remote_name = None
34
Georg Brandl3376a9a2007-08-24 17:38:49 +000035 def WriteTestData(self, root_key):
36 # Set the default value for this key.
37 SetValue(root_key, test_key_name, REG_SZ, "Default value")
38 key = CreateKey(root_key, test_key_name)
39 # Create a sub-key
40 sub_key = CreateKey(key, "sub_key")
41 # Give the sub-key some named values
42
43 for value_name, value_data, value_type in test_data:
44 SetValueEx(sub_key, value_name, 0, value_type, value_data)
45
46 # Check we wrote as many items as we thought.
47 nkeys, nvalues, since_mod = QueryInfoKey(key)
48 self.assertEquals(nkeys, 1, "Not the correct number of sub keys")
49 self.assertEquals(nvalues, 1, "Not the correct number of values")
50 nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
51 self.assertEquals(nkeys, 0, "Not the correct number of sub keys")
52 self.assertEquals(nvalues, len(test_data),
53 "Not the correct number of values")
54 # Close this key this way...
55 # (but before we do, copy the key as an integer - this allows
56 # us to test that the key really gets closed).
57 int_sub_key = int(sub_key)
58 CloseKey(sub_key)
59 try:
60 QueryInfoKey(int_sub_key)
61 self.fail("It appears the CloseKey() function does "
62 "not close the actual key!")
63 except EnvironmentError:
64 pass
65 # ... and close that key that way :-)
66 int_key = int(key)
67 key.Close()
68 try:
69 QueryInfoKey(int_key)
70 self.fail("It appears the key.Close() function "
71 "does not close the actual key!")
72 except EnvironmentError:
73 pass
74
75 def ReadTestData(self, root_key):
76 # Check we can get default value for this key.
77 val = QueryValue(root_key, test_key_name)
78 self.assertEquals(val, "Default value",
79 "Registry didn't give back the correct value")
80
81 key = OpenKey(root_key, test_key_name)
82 # Read the sub-keys
83 sub_key = OpenKey(key, "sub_key")
84 # Check I can enumerate over the values.
85 index = 0
86 while 1:
87 try:
88 data = EnumValue(sub_key, index)
89 except EnvironmentError:
90 break
91 self.assertEquals(data in test_data, True,
92 "Didn't read back the correct test data")
93 index = index + 1
94 self.assertEquals(index, len(test_data),
95 "Didn't read the correct number of items")
96 # Check I can directly access each item
97 for value_name, value_data, value_type in test_data:
98 read_val, read_typ = QueryValueEx(sub_key, value_name)
99 self.assertEquals(read_val, value_data,
100 "Could not directly read the value")
101 self.assertEquals(read_typ, value_type,
102 "Could not directly read the value")
103 sub_key.Close()
104 # Enumerate our main key.
105 read_val = EnumKey(key, 0)
106 self.assertEquals(read_val, "sub_key", "Read subkey value wrong")
107 try:
108 EnumKey(key, 1)
109 self.fail("Was able to get a second key when I only have one!")
110 except EnvironmentError:
111 pass
112
113 key.Close()
114
115 def DeleteTestData(self, root_key):
116 key = OpenKey(root_key, test_key_name, 0, KEY_ALL_ACCESS)
117 sub_key = OpenKey(key, "sub_key", 0, KEY_ALL_ACCESS)
118 # It is not necessary to delete the values before deleting
119 # the key (although subkeys must not exist). We delete them
120 # manually just to prove we can :-)
121 for value_name, value_data, value_type in test_data:
122 DeleteValue(sub_key, value_name)
123
124 nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
125 self.assertEquals(nkeys, 0, "subkey not empty before delete")
126 self.assertEquals(nvalues, 0, "subkey not empty before delete")
127 sub_key.Close()
128 DeleteKey(key, "sub_key")
129
130 try:
131 # Shouldnt be able to delete it twice!
132 DeleteKey(key, "sub_key")
133 self.fail("Deleting the key twice succeeded")
134 except EnvironmentError:
135 pass
136 key.Close()
137 DeleteKey(root_key, test_key_name)
138 # Opening should now fail!
139 try:
140 key = OpenKey(root_key, test_key_name)
141 self.fail("Could open the non-existent key")
142 except WindowsError: # Use this error name this time
143 pass
144
145 def TestAll(self, root_key):
146 self.WriteTestData(root_key)
147 self.ReadTestData(root_key)
148 self.DeleteTestData(root_key)
149
150 def testLocalMachineRegistryWorks(self):
151 self.TestAll(HKEY_CURRENT_USER)
152
153 def testConnectRegistryToLocalMachineWorks(self):
154 # perform minimal ConnectRegistry test which just invokes it
155 h = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
156 h.Close()
157
158 def testRemoteMachineRegistryWorks(self):
159 if not self.remote_name:
Georg Brandl0226d852007-08-30 12:32:23 +0000160 return # remote machine name not specified
Georg Brandl3376a9a2007-08-24 17:38:49 +0000161 remote_key = ConnectRegistry(self.remote_name, HKEY_CURRENT_USER)
162 self.TestAll(remote_key)
163
164def test_main():
165 test_support.run_unittest(WinregTests)
166
167if __name__ == "__main__":
Guido van Rossumde598552000-03-28 20:36:51 +0000168 try:
Georg Brandl3376a9a2007-08-24 17:38:49 +0000169 WinregTests.remote_name = sys.argv[sys.argv.index("--remote")+1]
170 except (IndexError, ValueError):
171 print "Remote registry calls can be tested using",
172 print "'test_winreg.py --remote \\\\machine_name'"
173 WinregTests.remote_name = None
174 test_main()