blob: fc898b8cc4ae05327d98e43caddc0e083ea88639 [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
Thomas Woutersed03b412007-08-28 21:37:11 +00006import unittest
Guido van Rossumde598552000-03-28 20:36:51 +00007
Thomas Woutersed03b412007-08-28 21:37:11 +00008from test import test_support
Fredrik Lundhf7850422001-01-17 21:51:36 +00009
Guido van Rossumde598552000-03-28 20:36:51 +000010test_key_name = "SOFTWARE\\Python Registry Test Key - Delete Me"
11
12test_data = [
13 ("Int Value", 45, REG_DWORD),
Guido van Rossum0a185522003-11-30 22:46:18 +000014 ("String Val", "A string value", REG_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000015 ("StringExpand", "The path is %path%", REG_EXPAND_SZ),
Guido van Rossumde598552000-03-28 20:36:51 +000016 ("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ),
Thomas Woutersed03b412007-08-28 21:37:11 +000017 ("Raw Data", b"binary\x00data", REG_BINARY),
Guido van Rossum291481b2003-12-03 15:24:02 +000018 ("Big String", "x"*(2**14-1), REG_SZ),
Guido van Rossuma8c360e2007-07-17 20:50:43 +000019 ("Big Binary", b"x"*(2**14), REG_BINARY),
Guido van Rossumde598552000-03-28 20:36:51 +000020]
21
Thomas Woutersed03b412007-08-28 21:37:11 +000022class WinregTests(unittest.TestCase):
Guido van Rossumde598552000-03-28 20:36:51 +000023 remote_name = None
24
Thomas Woutersed03b412007-08-28 21:37:11 +000025 def WriteTestData(self, root_key):
26 # Set the default value for this key.
27 SetValue(root_key, test_key_name, REG_SZ, "Default value")
28 key = CreateKey(root_key, test_key_name)
29 # Create a sub-key
30 sub_key = CreateKey(key, "sub_key")
31 # Give the sub-key some named values
32
33 for value_name, value_data, value_type in test_data:
34 SetValueEx(sub_key, value_name, 0, value_type, value_data)
35
36 # Check we wrote as many items as we thought.
37 nkeys, nvalues, since_mod = QueryInfoKey(key)
38 self.assertEquals(nkeys, 1, "Not the correct number of sub keys")
39 self.assertEquals(nvalues, 1, "Not the correct number of values")
40 nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
41 self.assertEquals(nkeys, 0, "Not the correct number of sub keys")
42 self.assertEquals(nvalues, len(test_data),
43 "Not the correct number of values")
44 # Close this key this way...
45 # (but before we do, copy the key as an integer - this allows
46 # us to test that the key really gets closed).
47 int_sub_key = int(sub_key)
48 CloseKey(sub_key)
49 try:
50 QueryInfoKey(int_sub_key)
51 self.fail("It appears the CloseKey() function does "
52 "not close the actual key!")
53 except EnvironmentError:
54 pass
55 # ... and close that key that way :-)
56 int_key = int(key)
57 key.Close()
58 try:
59 QueryInfoKey(int_key)
60 self.fail("It appears the key.Close() function "
61 "does not close the actual key!")
62 except EnvironmentError:
63 pass
64
65 def ReadTestData(self, root_key):
66 # Check we can get default value for this key.
67 val = QueryValue(root_key, test_key_name)
68 self.assertEquals(val, "Default value",
69 "Registry didn't give back the correct value")
70
71 key = OpenKey(root_key, test_key_name)
72 # Read the sub-keys
73 sub_key = OpenKey(key, "sub_key")
74 # Check I can enumerate over the values.
75 index = 0
76 while 1:
77 try:
78 data = EnumValue(sub_key, index)
79 except EnvironmentError:
80 break
81 self.assertEquals(data in test_data, True,
82 "Didn't read back the correct test data")
83 index = index + 1
84 self.assertEquals(index, len(test_data),
85 "Didn't read the correct number of items")
86 # Check I can directly access each item
87 for value_name, value_data, value_type in test_data:
88 read_val, read_typ = QueryValueEx(sub_key, value_name)
89 self.assertEquals(read_val, value_data,
90 "Could not directly read the value")
91 self.assertEquals(read_typ, value_type,
92 "Could not directly read the value")
93 sub_key.Close()
94 # Enumerate our main key.
95 read_val = EnumKey(key, 0)
96 self.assertEquals(read_val, "sub_key", "Read subkey value wrong")
97 try:
98 EnumKey(key, 1)
99 self.fail("Was able to get a second key when I only have one!")
100 except EnvironmentError:
101 pass
102
103 key.Close()
104
105 def DeleteTestData(self, root_key):
106 key = OpenKey(root_key, test_key_name, 0, KEY_ALL_ACCESS)
107 sub_key = OpenKey(key, "sub_key", 0, KEY_ALL_ACCESS)
108 # It is not necessary to delete the values before deleting
109 # the key (although subkeys must not exist). We delete them
110 # manually just to prove we can :-)
111 for value_name, value_data, value_type in test_data:
112 DeleteValue(sub_key, value_name)
113
114 nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
115 self.assertEquals(nkeys, 0, "subkey not empty before delete")
116 self.assertEquals(nvalues, 0, "subkey not empty before delete")
117 sub_key.Close()
118 DeleteKey(key, "sub_key")
119
120 try:
121 # Shouldnt be able to delete it twice!
122 DeleteKey(key, "sub_key")
123 self.fail("Deleting the key twice succeeded")
124 except EnvironmentError:
125 pass
126 key.Close()
127 DeleteKey(root_key, test_key_name)
128 # Opening should now fail!
129 try:
130 key = OpenKey(root_key, test_key_name)
131 self.fail("Could open the non-existent key")
132 except WindowsError: # Use this error name this time
133 pass
134
135 def TestAll(self, root_key):
136 self.WriteTestData(root_key)
137 self.ReadTestData(root_key)
138 self.DeleteTestData(root_key)
139
140 def testLocalMachineRegistryWorks(self):
141 self.TestAll(HKEY_CURRENT_USER)
142
143 def testConnectRegistryToLocalMachineWorks(self):
144 # perform minimal ConnectRegistry test which just invokes it
145 h = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
146 h.Close()
147
148 def testRemoteMachineRegistryWorks(self):
149 if not self.remote_name:
150 raise test_support.TestSkipped("Remote machine name "
151 "not specified.")
152 remote_key = ConnectRegistry(self.remote_name, HKEY_CURRENT_USER)
153 self.TestAll(remote_key)
154
155def test_main():
156 test_support.run_unittest(WinregTests)
157
158if __name__ == "__main__":
Guido van Rossumde598552000-03-28 20:36:51 +0000159 try:
Thomas Woutersed03b412007-08-28 21:37:11 +0000160 WinregTests.remote_name = sys.argv[sys.argv.index("--remote")+1]
161 except (IndexError, ValueError):
162 print("Remote registry calls can be tested using",
163 "'test_winreg.py --remote \\\\machine_name'")
164 WinregTests.remote_name = None
165 test_main()