blob: 18ce7a73c6d44530ba190866480f62fe11f0194d [file] [log] [blame]
Jeremy Hyltona8268e92000-10-16 17:33:50 +00001# Test the windows specific win32reg module.
2# Only win32reg functions not hit here: FlushKey, LoadKey and SaveKey
Guido van Rossumaad67612000-05-08 17:31:04 +00003
Jeremy Hyltona8268e92000-10-16 17:33:50 +00004from _winreg import *
5import os, sys
6
7test_key_name = "SOFTWARE\\Python Registry Test Key - Delete Me"
8
9test_data = [
10 ("Int Value", 45, REG_DWORD),
11 ("String Val", "A string value", REG_SZ,),
12 (u"Unicode Val", u"A Unicode value", REG_SZ,),
13 ("StringExpand", "The path is %path%", REG_EXPAND_SZ),
14 ("UnicodeExpand", u"The path is %path%", REG_EXPAND_SZ),
15 ("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ),
16 ("Multi-unicode", [u"Lots", u"of", u"unicode", u"values"], REG_MULTI_SZ),
17 ("Multi-mixed", [u"Unicode", u"and", "string", "values"],REG_MULTI_SZ),
18 ("Raw Data", ("binary"+chr(0)+"data"), REG_BINARY),
19]
20
21def WriteTestData(root_key):
22 # Set the default value for this key.
23 SetValue(root_key, test_key_name, REG_SZ, "Default value")
24 key = CreateKey(root_key, test_key_name)
25 # Create a sub-key
26 sub_key = CreateKey(key, "sub_key")
27 # Give the sub-key some named values
28
29 for value_name, value_data, value_type in test_data:
30 SetValueEx(sub_key, value_name, 0, value_type, value_data)
31
32 # Check we wrote as many items as we thought.
33 nkeys, nvalues, since_mod = QueryInfoKey(key)
34 assert nkeys==1, "Not the correct number of sub keys"
35 assert nvalues==1, "Not the correct number of values"
36 nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
37 assert nkeys==0, "Not the correct number of sub keys"
38 assert nvalues==len(test_data), "Not the correct number of values"
39 # Close this key this way...
40 # (but before we do, copy the key as an integer - this allows
41 # us to test that the key really gets closed).
42 int_sub_key = int(sub_key)
43 CloseKey(sub_key)
44 try:
45 QueryInfoKey(int_sub_key)
46 raise RuntimeError, "It appears the CloseKey() function does not close the actual key!"
47 except EnvironmentError:
48 pass
49 # ... and close that key that way :-)
50 int_key = int(key)
51 key.Close()
52 try:
53 QueryInfoKey(int_key)
54 raise RuntimeError, "It appears the key.Close() function does not close the actual key!"
55 except EnvironmentError:
56 pass
57
58def ReadTestData(root_key):
59 # Check we can get default value for this key.
60 val = QueryValue(root_key, test_key_name)
61 assert val=="Default value", "Registry didn't give back the correct value"
62
63 key = OpenKey(root_key, test_key_name)
64 # Read the sub-keys
65 sub_key = OpenKey(key, "sub_key")
66 # Check I can enumerate over the values.
67 index = 0
68 while 1:
69 try:
70 data = EnumValue(sub_key, index)
71 except EnvironmentError:
72 break
73 assert data in test_data, "Didn't read back the correct test data"
74 index = index + 1
75 assert index==len(test_data), "Didn't read the correct number of items"
76 # Check I can directly access each item
77 for value_name, value_data, value_type in test_data:
78 read_val, read_typ = QueryValueEx(sub_key, value_name)
79 assert read_val==value_data and read_typ == value_type, \
80 "Could not directly read the value"
81 sub_key.Close()
82 # Enumerate our main key.
83 read_val = EnumKey(key, 0)
84 assert read_val == "sub_key", "Read subkey value wrong"
85 try:
86 EnumKey(key, 1)
87 assert 0, "Was able to get a second key when I only have one!"
88 except EnvironmentError:
89 pass
90
91 key.Close()
92
93def DeleteTestData(root_key):
94 key = OpenKey(root_key, test_key_name, 0, KEY_ALL_ACCESS)
95 sub_key = OpenKey(key, "sub_key", 0, KEY_ALL_ACCESS)
96 # It is not necessary to delete the values before deleting
97 # the key (although subkeys must not exist). We delete them
98 # manually just to prove we can :-)
99 for value_name, value_data, value_type in test_data:
100 DeleteValue(sub_key, value_name)
101
102 nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
103 assert nkeys==0 and nvalues==0, "subkey not empty before delete"
104 sub_key.Close()
105 DeleteKey(key, "sub_key")
106
107 try:
108 # Shouldnt be able to delete it twice!
109 DeleteKey(key, "sub_key")
110 assert 0, "Deleting the key twice succeeded"
111 except EnvironmentError:
112 pass
113 key.Close()
114 DeleteKey(root_key, test_key_name)
115 # Opening should now fail!
116 try:
117 key = OpenKey(root_key, test_key_name)
118 assert 0, "Could open the non-existent key"
119 except WindowsError: # Use this error name this time
120 pass
121
122def TestAll(root_key):
123 WriteTestData(root_key)
124 ReadTestData(root_key)
125 DeleteTestData(root_key)
126
127# Test on my local machine.
128TestAll(HKEY_CURRENT_USER)
129print "Local registry tests worked"
130try:
131 remote_name = sys.argv[sys.argv.index("--remote")+1]
132except (IndexError, ValueError):
133 remote_name = None
134
135if remote_name is not None:
136 try:
137 remote_key = ConnectRegistry(remote_name, HKEY_CURRENT_USER)
138 except EnvironmentError, exc:
139 print "Could not connect to the remote machine -", exc.strerror
140 remote_key = None
141 if remote_key is not None:
142 TestAll(remote_key)
143 print "Remote registry tests worked"
144else:
145 print "Remote registry calls can be tested using",
146 print "'test_winreg.py --remote \\\\machine_name'"
Guido van Rossumaad67612000-05-08 17:31:04 +0000147