blob: 9984af1dd91b85505467546950c6f2aa36391286 [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
Guido van Rossuma8c360e2007-07-17 20:50:43 +00007from test.test_support import verify
Fredrik Lundhf7850422001-01-17 21:51:36 +00008
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),
Guido van Rossum0a185522003-11-30 22:46:18 +000013 ("String Val", "A string value", REG_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000014 ("StringExpand", "The path is %path%", REG_EXPAND_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000015 ("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ),
Guido van Rossuma8c360e2007-07-17 20:50:43 +000016 ("Raw Data", bytes("binary"+chr(0)+"data"), REG_BINARY),
Guido van Rossum291481b2003-12-03 15:24:02 +000017 ("Big String", "x"*(2**14-1), REG_SZ),
Guido van Rossuma8c360e2007-07-17 20:50:43 +000018 ("Big Binary", b"x"*(2**14), REG_BINARY),
Guido van Rossumde598552000-03-28 20:36:51 +000019]
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)
Marc-André Lemburg36619082001-01-17 19:11:13 +000034 verify(nkeys==1, "Not the correct number of sub keys")
35 verify(nvalues==1, "Not the correct number of values")
Guido van Rossumde598552000-03-28 20:36:51 +000036 nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
Marc-André Lemburg36619082001-01-17 19:11:13 +000037 verify(nkeys==0, "Not the correct number of sub keys")
38 verify(nvalues==len(test_data), "Not the correct number of values")
Guido van Rossumde598552000-03-28 20:36:51 +000039 # 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:
Paul Prescod99b84bd2000-07-04 03:38:10 +000045 QueryInfoKey(int_sub_key)
46 raise RuntimeError, "It appears the CloseKey() function does not close the actual key!"
Guido van Rossumde598552000-03-28 20:36:51 +000047 except EnvironmentError:
Paul Prescod99b84bd2000-07-04 03:38:10 +000048 pass
Guido van Rossumde598552000-03-28 20:36:51 +000049 # ... and close that key that way :-)
50 int_key = int(key)
51 key.Close()
52 try:
Paul Prescod99b84bd2000-07-04 03:38:10 +000053 QueryInfoKey(int_key)
54 raise RuntimeError, "It appears the key.Close() function does not close the actual key!"
Guido van Rossumde598552000-03-28 20:36:51 +000055 except EnvironmentError:
Paul Prescod99b84bd2000-07-04 03:38:10 +000056 pass
Guido van Rossumde598552000-03-28 20:36:51 +000057
58def ReadTestData(root_key):
59 # Check we can get default value for this key.
60 val = QueryValue(root_key, test_key_name)
Guido van Rossuma8c360e2007-07-17 20:50:43 +000061 verify(type(val) is str and val=="Default value", "Registry didn't give back the correct value")
Guido van Rossumde598552000-03-28 20:36:51 +000062
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
Marc-André Lemburg36619082001-01-17 19:11:13 +000073 verify(data in test_data, "Didn't read back the correct test data")
Guido van Rossumde598552000-03-28 20:36:51 +000074 index = index + 1
Marc-André Lemburg36619082001-01-17 19:11:13 +000075 verify(index==len(test_data), "Didn't read the correct number of items")
Guido van Rossumde598552000-03-28 20:36:51 +000076 # 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)
Marc-André Lemburg36619082001-01-17 19:11:13 +000079 verify(read_val==value_data and read_typ == value_type, \
80 "Could not directly read the value" )
Guido van Rossumde598552000-03-28 20:36:51 +000081 sub_key.Close()
82 # Enumerate our main key.
83 read_val = EnumKey(key, 0)
Marc-André Lemburg36619082001-01-17 19:11:13 +000084 verify(read_val == "sub_key", "Read subkey value wrong")
Guido van Rossumde598552000-03-28 20:36:51 +000085 try:
86 EnumKey(key, 1)
Marc-André Lemburg36619082001-01-17 19:11:13 +000087 verify(0, "Was able to get a second key when I only have one!")
Guido van Rossumde598552000-03-28 20:36:51 +000088 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)
Marc-André Lemburg36619082001-01-17 19:11:13 +0000103 verify(nkeys==0 and nvalues==0, "subkey not empty before delete")
Guido van Rossumde598552000-03-28 20:36:51 +0000104 sub_key.Close()
105 DeleteKey(key, "sub_key")
106
107 try:
108 # Shouldnt be able to delete it twice!
109 DeleteKey(key, "sub_key")
Marc-André Lemburg36619082001-01-17 19:11:13 +0000110 verify(0, "Deleting the key twice succeeded")
Guido van Rossumde598552000-03-28 20:36:51 +0000111 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)
Marc-André Lemburg36619082001-01-17 19:11:13 +0000118 verify(0, "Could open the non-existent key")
Guido van Rossumde598552000-03-28 20:36:51 +0000119 except WindowsError: # Use this error name this time
120 pass
121
122def TestAll(root_key):
123 WriteTestData(root_key)
124 ReadTestData(root_key)
Fred Draked9b0f262000-04-01 05:25:57 +0000125 DeleteTestData(root_key)
Guido van Rossumde598552000-03-28 20:36:51 +0000126
127# Test on my local machine.
128TestAll(HKEY_CURRENT_USER)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000129print("Local registry tests worked")
Guido van Rossumde598552000-03-28 20:36:51 +0000130try:
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)
Guido van Rossumb940e112007-01-10 16:19:56 +0000138 except EnvironmentError as exc:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000139 print("Could not connect to the remote machine -", exc.strerror)
Guido van Rossumde598552000-03-28 20:36:51 +0000140 remote_key = None
141 if remote_key is not None:
142 TestAll(remote_key)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000143 print("Remote registry tests worked")
Guido van Rossumde598552000-03-28 20:36:51 +0000144else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000145 print("Remote registry calls can be tested using", end=' ')
146 print("'test_winreg.py --remote \\\\machine_name'")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000147 # perform minimal ConnectRegistry test which just invokes it
148 h = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
149 h.Close()