1
# -*- coding: utf-8 -*-
2
# Test the windows specific win32reg module.
3
# Only win32reg functions not hit here: FlushKey, LoadKey and SaveKey
9
from test import support
11
test_key_name = "SOFTWARE\\Python Registry Test Key - Delete Me"
14
("Int Value", 45, REG_DWORD),
15
("String Val", "A string value", REG_SZ),
16
("StringExpand", "The path is %path%", REG_EXPAND_SZ),
17
("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ),
18
("Raw Data", b"binary\x00data", REG_BINARY),
19
("Big String", "x"*(2**14-1), REG_SZ),
20
("Big Binary", b"x"*(2**14), REG_BINARY),
21
# Two and three kanjis, meaning: "Japan" and "Japanese")
22
("Japanese 日本", "日本語", REG_SZ),
25
class WinregTests(unittest.TestCase):
28
def WriteTestData(self, root_key, subkeystr="sub_key"):
29
# Set the default value for this key.
30
SetValue(root_key, test_key_name, REG_SZ, "Default value")
31
key = CreateKey(root_key, test_key_name)
32
self.assert_(key.handle != 0)
34
sub_key = CreateKey(key, subkeystr)
35
# Give the sub-key some named values
37
for value_name, value_data, value_type in test_data:
38
SetValueEx(sub_key, value_name, 0, value_type, value_data)
40
# Check we wrote as many items as we thought.
41
nkeys, nvalues, since_mod = QueryInfoKey(key)
42
self.assertEquals(nkeys, 1, "Not the correct number of sub keys")
43
self.assertEquals(nvalues, 1, "Not the correct number of values")
44
nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
45
self.assertEquals(nkeys, 0, "Not the correct number of sub keys")
46
self.assertEquals(nvalues, len(test_data),
47
"Not the correct number of values")
48
# Close this key this way...
49
# (but before we do, copy the key as an integer - this allows
50
# us to test that the key really gets closed).
51
int_sub_key = int(sub_key)
54
QueryInfoKey(int_sub_key)
55
self.fail("It appears the CloseKey() function does "
56
"not close the actual key!")
57
except EnvironmentError:
59
# ... and close that key that way :-)
64
self.fail("It appears the key.Close() function "
65
"does not close the actual key!")
66
except EnvironmentError:
69
def ReadTestData(self, root_key, subkeystr="sub_key"):
70
# Check we can get default value for this key.
71
val = QueryValue(root_key, test_key_name)
72
self.assertEquals(val, "Default value",
73
"Registry didn't give back the correct value")
75
key = OpenKey(root_key, test_key_name)
77
with OpenKey(key, subkeystr) as sub_key:
78
# Check I can enumerate over the values.
82
data = EnumValue(sub_key, index)
83
except EnvironmentError:
85
self.assertEquals(data in test_data, True,
86
"Didn't read back the correct test data")
88
self.assertEquals(index, len(test_data),
89
"Didn't read the correct number of items")
90
# Check I can directly access each item
91
for value_name, value_data, value_type in test_data:
92
read_val, read_typ = QueryValueEx(sub_key, value_name)
93
self.assertEquals(read_val, value_data,
94
"Could not directly read the value")
95
self.assertEquals(read_typ, value_type,
96
"Could not directly read the value")
98
# Enumerate our main key.
99
read_val = EnumKey(key, 0)
100
self.assertEquals(read_val, subkeystr, "Read subkey value wrong")
103
self.fail("Was able to get a second key when I only have one!")
104
except EnvironmentError:
109
def DeleteTestData(self, root_key, subkeystr="sub_key"):
110
key = OpenKey(root_key, test_key_name, 0, KEY_ALL_ACCESS)
111
sub_key = OpenKey(key, subkeystr, 0, KEY_ALL_ACCESS)
112
# It is not necessary to delete the values before deleting
113
# the key (although subkeys must not exist). We delete them
114
# manually just to prove we can :-)
115
for value_name, value_data, value_type in test_data:
116
DeleteValue(sub_key, value_name)
118
nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
119
self.assertEquals(nkeys, 0, "subkey not empty before delete")
120
self.assertEquals(nvalues, 0, "subkey not empty before delete")
122
DeleteKey(key, subkeystr)
125
# Shouldnt be able to delete it twice!
126
DeleteKey(key, subkeystr)
127
self.fail("Deleting the key twice succeeded")
128
except EnvironmentError:
131
DeleteKey(root_key, test_key_name)
132
# Opening should now fail!
134
key = OpenKey(root_key, test_key_name)
135
self.fail("Could open the non-existent key")
136
except WindowsError: # Use this error name this time
139
def TestAll(self, root_key, subkeystr="sub_key"):
140
self.WriteTestData(root_key, subkeystr)
141
self.ReadTestData(root_key, subkeystr)
142
self.DeleteTestData(root_key, subkeystr)
144
def testLocalMachineRegistryWorks(self):
145
self.TestAll(HKEY_CURRENT_USER)
146
self.TestAll(HKEY_CURRENT_USER, "日本-subkey")
148
def testConnectRegistryToLocalMachineWorks(self):
149
# perform minimal ConnectRegistry test which just invokes it
150
h = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
153
def testRemoteMachineRegistryWorks(self):
154
if not self.remote_name:
155
return # remote machine name not specified
156
remote_key = ConnectRegistry(self.remote_name, HKEY_CURRENT_USER)
157
self.TestAll(remote_key)
159
def testExpandEnvironmentStrings(self):
160
r = ExpandEnvironmentStrings("%windir%\\test")
161
self.assertEqual(type(r), str)
162
self.assertEqual(r, os.environ["windir"] + "\\test")
165
support.run_unittest(WinregTests)
167
if __name__ == "__main__":
169
WinregTests.remote_name = sys.argv[sys.argv.index("--remote")+1]
170
except (IndexError, ValueError):
171
print("Remote registry calls can be tested using",
172
"'test_winreg.py --remote \\\\machine_name'")
173
WinregTests.remote_name = None