1
# Test the windows specific win32reg module.
2
# Only win32reg functions not hit here: FlushKey, LoadKey and SaveKey
7
from test.test_support import verify, have_unicode
9
test_key_name = "SOFTWARE\\Python Registry Test Key - Delete Me"
12
("Int Value", 45, REG_DWORD),
13
("String Val", "A string value", REG_SZ),
14
("StringExpand", "The path is %path%", REG_EXPAND_SZ),
15
("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ),
16
("Raw Data", ("binary"+chr(0)+"data"), REG_BINARY),
17
("Big String", "x"*(2**14-1), REG_SZ),
18
("Big Binary", "x"*(2**14), REG_BINARY),
22
(unicode("Unicode Val"), unicode("A Unicode value"), REG_SZ,),
23
("UnicodeExpand", unicode("The path is %path%"), REG_EXPAND_SZ),
24
("Multi-unicode", [unicode("Lots"), unicode("of"), unicode("unicode"), unicode("values")], REG_MULTI_SZ),
25
("Multi-mixed", [unicode("Unicode"), unicode("and"), "string", "values"],REG_MULTI_SZ),
28
def WriteTestData(root_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)
33
sub_key = CreateKey(key, "sub_key")
34
# Give the sub-key some named values
36
for value_name, value_data, value_type in test_data:
37
SetValueEx(sub_key, value_name, 0, value_type, value_data)
39
# Check we wrote as many items as we thought.
40
nkeys, nvalues, since_mod = QueryInfoKey(key)
41
verify(nkeys==1, "Not the correct number of sub keys")
42
verify(nvalues==1, "Not the correct number of values")
43
nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
44
verify(nkeys==0, "Not the correct number of sub keys")
45
verify(nvalues==len(test_data), "Not the correct number of values")
46
# Close this key this way...
47
# (but before we do, copy the key as an integer - this allows
48
# us to test that the key really gets closed).
49
int_sub_key = int(sub_key)
52
QueryInfoKey(int_sub_key)
53
raise RuntimeError, "It appears the CloseKey() function does not close the actual key!"
54
except EnvironmentError:
56
# ... and close that key that way :-)
61
raise RuntimeError, "It appears the key.Close() function does not close the actual key!"
62
except EnvironmentError:
65
def ReadTestData(root_key):
66
# Check we can get default value for this key.
67
val = QueryValue(root_key, test_key_name)
68
verify(val=="Default value", "Registry didn't give back the correct value")
70
key = OpenKey(root_key, test_key_name)
72
sub_key = OpenKey(key, "sub_key")
73
# Check I can enumerate over the values.
77
data = EnumValue(sub_key, index)
78
except EnvironmentError:
80
verify(data in test_data, "Didn't read back the correct test data")
82
verify(index==len(test_data), "Didn't read the correct number of items")
83
# Check I can directly access each item
84
for value_name, value_data, value_type in test_data:
85
read_val, read_typ = QueryValueEx(sub_key, value_name)
86
verify(read_val==value_data and read_typ == value_type, \
87
"Could not directly read the value" )
89
# Enumerate our main key.
90
read_val = EnumKey(key, 0)
91
verify(read_val == "sub_key", "Read subkey value wrong")
94
verify(0, "Was able to get a second key when I only have one!")
95
except EnvironmentError:
100
def DeleteTestData(root_key):
101
key = OpenKey(root_key, test_key_name, 0, KEY_ALL_ACCESS)
102
sub_key = OpenKey(key, "sub_key", 0, KEY_ALL_ACCESS)
103
# It is not necessary to delete the values before deleting
104
# the key (although subkeys must not exist). We delete them
105
# manually just to prove we can :-)
106
for value_name, value_data, value_type in test_data:
107
DeleteValue(sub_key, value_name)
109
nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
110
verify(nkeys==0 and nvalues==0, "subkey not empty before delete")
112
DeleteKey(key, "sub_key")
115
# Shouldnt be able to delete it twice!
116
DeleteKey(key, "sub_key")
117
verify(0, "Deleting the key twice succeeded")
118
except EnvironmentError:
121
DeleteKey(root_key, test_key_name)
122
# Opening should now fail!
124
key = OpenKey(root_key, test_key_name)
125
verify(0, "Could open the non-existent key")
126
except WindowsError: # Use this error name this time
129
def TestAll(root_key):
130
WriteTestData(root_key)
131
ReadTestData(root_key)
132
DeleteTestData(root_key)
134
# Test on my local machine.
135
TestAll(HKEY_CURRENT_USER)
136
print "Local registry tests worked"
138
remote_name = sys.argv[sys.argv.index("--remote")+1]
139
except (IndexError, ValueError):
142
if remote_name is not None:
144
remote_key = ConnectRegistry(remote_name, HKEY_CURRENT_USER)
145
except EnvironmentError, exc:
146
print "Could not connect to the remote machine -", exc.strerror
148
if remote_key is not None:
150
print "Remote registry tests worked"
152
print "Remote registry calls can be tested using",
153
print "'test_winreg.py --remote \\\\machine_name'"