blob: 02bc749f2428507f88d8a15006b7b6fb2d33752b [file] [log] [blame]
Guido van Rossumde598552000-03-28 20:36:51 +00001# Test the windows specific win32reg module.
2# Only win32reg functions not hit here: FlushKey, LoadKey and SaveKey
3
Fred Drake252af9c2000-06-29 19:42:00 +00004from _winreg import *
Guido van Rossumde598552000-03-28 20:36:51 +00005import os, sys
6
Fredrik Lundhf7850422001-01-17 21:51:36 +00007from test_support import verify
8
Guido van Rossumde598552000-03-28 20:36:51 +00009test_key_name = "SOFTWARE\\Python Registry Test Key - Delete Me"
10
11test_data = [
12 ("Int Value", 45, REG_DWORD),
13 ("String Val", "A string value", REG_SZ,),
14 (u"Unicode Val", u"A Unicode value", REG_SZ,),
15 ("StringExpand", "The path is %path%", REG_EXPAND_SZ),
16 ("UnicodeExpand", u"The path is %path%", REG_EXPAND_SZ),
17 ("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ),
18 ("Multi-unicode", [u"Lots", u"of", u"unicode", u"values"], REG_MULTI_SZ),
19 ("Multi-mixed", [u"Unicode", u"and", "string", "values"],REG_MULTI_SZ),
20 ("Raw Data", ("binary"+chr(0)+"data"), REG_BINARY),
21]
22
23def WriteTestData(root_key):
24 # Set the default value for this key.
25 SetValue(root_key, test_key_name, REG_SZ, "Default value")
26 key = CreateKey(root_key, test_key_name)
27 # Create a sub-key
28 sub_key = CreateKey(key, "sub_key")
29 # Give the sub-key some named values
30
31 for value_name, value_data, value_type in test_data:
32 SetValueEx(sub_key, value_name, 0, value_type, value_data)
33
34 # Check we wrote as many items as we thought.
35 nkeys, nvalues, since_mod = QueryInfoKey(key)
Marc-André Lemburg36619082001-01-17 19:11:13 +000036 verify(nkeys==1, "Not the correct number of sub keys")
37 verify(nvalues==1, "Not the correct number of values")
Guido van Rossumde598552000-03-28 20:36:51 +000038 nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
Marc-André Lemburg36619082001-01-17 19:11:13 +000039 verify(nkeys==0, "Not the correct number of sub keys")
40 verify(nvalues==len(test_data), "Not the correct number of values")
Guido van Rossumde598552000-03-28 20:36:51 +000041 # Close this key this way...
42 # (but before we do, copy the key as an integer - this allows
43 # us to test that the key really gets closed).
44 int_sub_key = int(sub_key)
45 CloseKey(sub_key)
46 try:
Paul Prescod99b84bd2000-07-04 03:38:10 +000047 QueryInfoKey(int_sub_key)
48 raise RuntimeError, "It appears the CloseKey() function does not close the actual key!"
Guido van Rossumde598552000-03-28 20:36:51 +000049 except EnvironmentError:
Paul Prescod99b84bd2000-07-04 03:38:10 +000050 pass
Guido van Rossumde598552000-03-28 20:36:51 +000051 # ... and close that key that way :-)
52 int_key = int(key)
53 key.Close()
54 try:
Paul Prescod99b84bd2000-07-04 03:38:10 +000055 QueryInfoKey(int_key)
56 raise RuntimeError, "It appears the key.Close() function does not close the actual key!"
Guido van Rossumde598552000-03-28 20:36:51 +000057 except EnvironmentError:
Paul Prescod99b84bd2000-07-04 03:38:10 +000058 pass
Guido van Rossumde598552000-03-28 20:36:51 +000059
60def ReadTestData(root_key):
61 # Check we can get default value for this key.
62 val = QueryValue(root_key, test_key_name)
Marc-André Lemburg36619082001-01-17 19:11:13 +000063 verify(val=="Default value", "Registry didn't give back the correct value")
Guido van Rossumde598552000-03-28 20:36:51 +000064
65 key = OpenKey(root_key, test_key_name)
66 # Read the sub-keys
67 sub_key = OpenKey(key, "sub_key")
68 # Check I can enumerate over the values.
69 index = 0
70 while 1:
71 try:
72 data = EnumValue(sub_key, index)
73 except EnvironmentError:
74 break
Marc-André Lemburg36619082001-01-17 19:11:13 +000075 verify(data in test_data, "Didn't read back the correct test data")
Guido van Rossumde598552000-03-28 20:36:51 +000076 index = index + 1
Marc-André Lemburg36619082001-01-17 19:11:13 +000077 verify(index==len(test_data), "Didn't read the correct number of items")
Guido van Rossumde598552000-03-28 20:36:51 +000078 # Check I can directly access each item
79 for value_name, value_data, value_type in test_data:
80 read_val, read_typ = QueryValueEx(sub_key, value_name)
Marc-André Lemburg36619082001-01-17 19:11:13 +000081 verify(read_val==value_data and read_typ == value_type, \
82 "Could not directly read the value" )
Guido van Rossumde598552000-03-28 20:36:51 +000083 sub_key.Close()
84 # Enumerate our main key.
85 read_val = EnumKey(key, 0)
Marc-André Lemburg36619082001-01-17 19:11:13 +000086 verify(read_val == "sub_key", "Read subkey value wrong")
Guido van Rossumde598552000-03-28 20:36:51 +000087 try:
88 EnumKey(key, 1)
Marc-André Lemburg36619082001-01-17 19:11:13 +000089 verify(0, "Was able to get a second key when I only have one!")
Guido van Rossumde598552000-03-28 20:36:51 +000090 except EnvironmentError:
91 pass
92
93 key.Close()
94
95def DeleteTestData(root_key):
96 key = OpenKey(root_key, test_key_name, 0, KEY_ALL_ACCESS)
97 sub_key = OpenKey(key, "sub_key", 0, KEY_ALL_ACCESS)
98 # It is not necessary to delete the values before deleting
99 # the key (although subkeys must not exist). We delete them
100 # manually just to prove we can :-)
101 for value_name, value_data, value_type in test_data:
102 DeleteValue(sub_key, value_name)
103
104 nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
Marc-André Lemburg36619082001-01-17 19:11:13 +0000105 verify(nkeys==0 and nvalues==0, "subkey not empty before delete")
Guido van Rossumde598552000-03-28 20:36:51 +0000106 sub_key.Close()
107 DeleteKey(key, "sub_key")
108
109 try:
110 # Shouldnt be able to delete it twice!
111 DeleteKey(key, "sub_key")
Marc-André Lemburg36619082001-01-17 19:11:13 +0000112 verify(0, "Deleting the key twice succeeded")
Guido van Rossumde598552000-03-28 20:36:51 +0000113 except EnvironmentError:
114 pass
115 key.Close()
116 DeleteKey(root_key, test_key_name)
117 # Opening should now fail!
118 try:
119 key = OpenKey(root_key, test_key_name)
Marc-André Lemburg36619082001-01-17 19:11:13 +0000120 verify(0, "Could open the non-existent key")
Guido van Rossumde598552000-03-28 20:36:51 +0000121 except WindowsError: # Use this error name this time
122 pass
123
124def TestAll(root_key):
125 WriteTestData(root_key)
126 ReadTestData(root_key)
Fred Draked9b0f262000-04-01 05:25:57 +0000127 DeleteTestData(root_key)
Guido van Rossumde598552000-03-28 20:36:51 +0000128
129# Test on my local machine.
130TestAll(HKEY_CURRENT_USER)
131print "Local registry tests worked"
132try:
133 remote_name = sys.argv[sys.argv.index("--remote")+1]
134except (IndexError, ValueError):
135 remote_name = None
136
137if remote_name is not None:
138 try:
139 remote_key = ConnectRegistry(remote_name, HKEY_CURRENT_USER)
140 except EnvironmentError, exc:
141 print "Could not connect to the remote machine -", exc.strerror
142 remote_key = None
143 if remote_key is not None:
144 TestAll(remote_key)
145 print "Remote registry tests worked"
146else:
147 print "Remote registry calls can be tested using",
148 print "'test_winreg.py --remote \\\\machine_name'"