|
1 # Test the windows specific win32reg module. |
|
2 # Only win32reg functions not hit here: FlushKey, LoadKey and SaveKey |
|
3 |
|
4 from _winreg import * |
|
5 import os, sys |
|
6 import unittest |
|
7 |
|
8 from test import test_support |
|
9 |
|
10 test_key_name = "SOFTWARE\\Python Registry Test Key - Delete Me" |
|
11 |
|
12 test_data = [ |
|
13 ("Int Value", 45, REG_DWORD), |
|
14 ("String Val", "A string value", REG_SZ), |
|
15 ("StringExpand", "The path is %path%", REG_EXPAND_SZ), |
|
16 ("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ), |
|
17 ("Raw Data", ("binary"+chr(0)+"data"), REG_BINARY), |
|
18 ("Big String", "x"*(2**14-1), REG_SZ), |
|
19 ("Big Binary", "x"*(2**14), REG_BINARY), |
|
20 ] |
|
21 |
|
22 if 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), |
|
30 ] |
|
31 |
|
32 class WinregTests(unittest.TestCase): |
|
33 remote_name = None |
|
34 |
|
35 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 with OpenKey(key, "sub_key") as 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: |
|
160 return # remote machine name not specified |
|
161 remote_key = ConnectRegistry(self.remote_name, HKEY_CURRENT_USER) |
|
162 self.TestAll(remote_key) |
|
163 |
|
164 def testExpandEnvironmentStrings(self): |
|
165 r = ExpandEnvironmentStrings(u"%windir%\\test") |
|
166 self.assertEqual(type(r), unicode) |
|
167 self.assertEqual(r, os.environ["windir"] + "\\test") |
|
168 |
|
169 def test_main(): |
|
170 test_support.run_unittest(WinregTests) |
|
171 |
|
172 if __name__ == "__main__": |
|
173 try: |
|
174 WinregTests.remote_name = sys.argv[sys.argv.index("--remote")+1] |
|
175 except (IndexError, ValueError): |
|
176 print "Remote registry calls can be tested using", |
|
177 print "'test_winreg.py --remote \\\\machine_name'" |
|
178 WinregTests.remote_name = None |
|
179 test_main() |