1
# Test the windows specific win32reg module.
2
# Only win32reg functions not hit here: FlushKey, LoadKey and SaveKey
8
from test import test_support
10
test_key_name = "SOFTWARE\\Python Registry Test Key - Delete Me"
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),
22
if test_support.have_unicode:
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),
32
class WinregTests(unittest.TestCase):
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)
40
sub_key = CreateKey(key, "sub_key")
41
# Give the sub-key some named values
43
for value_name, value_data, value_type in test_data:
44
SetValueEx(sub_key, value_name, 0, value_type, value_data)
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)
60
QueryInfoKey(int_sub_key)
61
self.fail("It appears the CloseKey() function does "
62
"not close the actual key!")
63
except EnvironmentError:
65
# ... and close that key that way :-)
70
self.fail("It appears the key.Close() function "
71
"does not close the actual key!")
72
except EnvironmentError:
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")
81
key = OpenKey(root_key, test_key_name)
83
with OpenKey(key, "sub_key") as sub_key:
84
# Check I can enumerate over the values.
88
data = EnumValue(sub_key, index)
89
except EnvironmentError:
91
self.assertEquals(data in test_data, True,
92
"Didn't read back the correct test data")
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")
104
# Enumerate our main key.
105
read_val = EnumKey(key, 0)
106
self.assertEquals(read_val, "sub_key", "Read subkey value wrong")
109
self.fail("Was able to get a second key when I only have one!")
110
except EnvironmentError:
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)
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")
128
DeleteKey(key, "sub_key")
131
# Shouldnt be able to delete it twice!
132
DeleteKey(key, "sub_key")
133
self.fail("Deleting the key twice succeeded")
134
except EnvironmentError:
137
DeleteKey(root_key, test_key_name)
138
# Opening should now fail!
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
145
def TestAll(self, root_key):
146
self.WriteTestData(root_key)
147
self.ReadTestData(root_key)
148
self.DeleteTestData(root_key)
150
def testLocalMachineRegistryWorks(self):
151
self.TestAll(HKEY_CURRENT_USER)
153
def testConnectRegistryToLocalMachineWorks(self):
154
# perform minimal ConnectRegistry test which just invokes it
155
h = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
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)
164
def testExpandEnvironmentStrings(self):
165
r = ExpandEnvironmentStrings(u"%windir%\\test")
166
self.assertEqual(type(r), unicode)
167
self.assertEqual(r, os.environ["windir"] + "\\test")
170
test_support.run_unittest(WinregTests)
172
if __name__ == "__main__":
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