Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 2 | # Test the windows specific win32reg module. |
| 3 | # Only win32reg functions not hit here: FlushKey, LoadKey and SaveKey |
| 4 | |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 5 | import os, sys |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 6 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 7 | from test import support |
Brian Curtin | 9c9d878 | 2010-05-26 18:09:32 +0000 | [diff] [blame] | 8 | threading = support.import_module("threading") |
Fredrik Lundh | f785042 | 2001-01-17 21:51:36 +0000 | [diff] [blame] | 9 | |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 10 | # Do this first so test will be skipped if module doesn't exist |
| 11 | support.import_module('winreg') |
| 12 | # Now import everything |
| 13 | from winreg import * |
| 14 | |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 15 | test_key_name = "SOFTWARE\\Python Registry Test Key - Delete Me" |
| 16 | |
| 17 | test_data = [ |
| 18 | ("Int Value", 45, REG_DWORD), |
Guido van Rossum | 0a18552 | 2003-11-30 22:46:18 +0000 | [diff] [blame] | 19 | ("String Val", "A string value", REG_SZ), |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 20 | ("StringExpand", "The path is %path%", REG_EXPAND_SZ), |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 21 | ("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ), |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 22 | ("Raw Data", b"binary\x00data", REG_BINARY), |
Guido van Rossum | 291481b | 2003-12-03 15:24:02 +0000 | [diff] [blame] | 23 | ("Big String", "x"*(2**14-1), REG_SZ), |
Guido van Rossum | a8c360e | 2007-07-17 20:50:43 +0000 | [diff] [blame] | 24 | ("Big Binary", b"x"*(2**14), REG_BINARY), |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 25 | # Two and three kanjis, meaning: "Japan" and "Japanese") |
| 26 | ("Japanese 日本", "日本語", REG_SZ), |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 27 | ] |
| 28 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 29 | class WinregTests(unittest.TestCase): |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 30 | remote_name = None |
| 31 | |
Martin v. Löwis | b7a5156 | 2009-06-07 17:55:17 +0000 | [diff] [blame] | 32 | def setUp(self): |
| 33 | # Make sure that the test key is absent when the test |
| 34 | # starts. |
| 35 | self.delete_tree(HKEY_CURRENT_USER, test_key_name) |
| 36 | |
| 37 | def delete_tree(self, root, subkey): |
| 38 | try: |
| 39 | hkey = OpenKey(root, subkey, KEY_ALL_ACCESS) |
| 40 | except WindowsError: |
| 41 | # subkey does not exist |
| 42 | return |
| 43 | while True: |
| 44 | try: |
| 45 | subsubkey = EnumKey(hkey, 0) |
| 46 | except WindowsError: |
| 47 | # no more subkeys |
| 48 | break |
| 49 | self.delete_tree(hkey, subsubkey) |
| 50 | CloseKey(hkey) |
| 51 | DeleteKey(root, subkey) |
| 52 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 53 | def WriteTestData(self, root_key, subkeystr="sub_key"): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 54 | # Set the default value for this key. |
| 55 | SetValue(root_key, test_key_name, REG_SZ, "Default value") |
| 56 | key = CreateKey(root_key, test_key_name) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 57 | self.assertTrue(key.handle != 0) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 58 | # Create a sub-key |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 59 | sub_key = CreateKey(key, subkeystr) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 60 | # Give the sub-key some named values |
| 61 | |
| 62 | for value_name, value_data, value_type in test_data: |
| 63 | SetValueEx(sub_key, value_name, 0, value_type, value_data) |
| 64 | |
| 65 | # Check we wrote as many items as we thought. |
| 66 | nkeys, nvalues, since_mod = QueryInfoKey(key) |
| 67 | self.assertEquals(nkeys, 1, "Not the correct number of sub keys") |
| 68 | self.assertEquals(nvalues, 1, "Not the correct number of values") |
| 69 | nkeys, nvalues, since_mod = QueryInfoKey(sub_key) |
| 70 | self.assertEquals(nkeys, 0, "Not the correct number of sub keys") |
| 71 | self.assertEquals(nvalues, len(test_data), |
| 72 | "Not the correct number of values") |
| 73 | # Close this key this way... |
| 74 | # (but before we do, copy the key as an integer - this allows |
| 75 | # us to test that the key really gets closed). |
| 76 | int_sub_key = int(sub_key) |
| 77 | CloseKey(sub_key) |
| 78 | try: |
| 79 | QueryInfoKey(int_sub_key) |
| 80 | self.fail("It appears the CloseKey() function does " |
| 81 | "not close the actual key!") |
| 82 | except EnvironmentError: |
| 83 | pass |
| 84 | # ... and close that key that way :-) |
| 85 | int_key = int(key) |
| 86 | key.Close() |
| 87 | try: |
| 88 | QueryInfoKey(int_key) |
| 89 | self.fail("It appears the key.Close() function " |
| 90 | "does not close the actual key!") |
| 91 | except EnvironmentError: |
| 92 | pass |
| 93 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 94 | def ReadTestData(self, root_key, subkeystr="sub_key"): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 95 | # Check we can get default value for this key. |
| 96 | val = QueryValue(root_key, test_key_name) |
| 97 | self.assertEquals(val, "Default value", |
| 98 | "Registry didn't give back the correct value") |
| 99 | |
| 100 | key = OpenKey(root_key, test_key_name) |
| 101 | # Read the sub-keys |
Thomas Heller | e83ebd9 | 2008-01-24 10:31:31 +0000 | [diff] [blame] | 102 | with OpenKey(key, subkeystr) as sub_key: |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 103 | # Check I can enumerate over the values. |
| 104 | index = 0 |
| 105 | while 1: |
| 106 | try: |
| 107 | data = EnumValue(sub_key, index) |
| 108 | except EnvironmentError: |
| 109 | break |
| 110 | self.assertEquals(data in test_data, True, |
| 111 | "Didn't read back the correct test data") |
| 112 | index = index + 1 |
| 113 | self.assertEquals(index, len(test_data), |
| 114 | "Didn't read the correct number of items") |
| 115 | # Check I can directly access each item |
| 116 | for value_name, value_data, value_type in test_data: |
| 117 | read_val, read_typ = QueryValueEx(sub_key, value_name) |
| 118 | self.assertEquals(read_val, value_data, |
| 119 | "Could not directly read the value") |
| 120 | self.assertEquals(read_typ, value_type, |
| 121 | "Could not directly read the value") |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 122 | sub_key.Close() |
| 123 | # Enumerate our main key. |
| 124 | read_val = EnumKey(key, 0) |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 125 | self.assertEquals(read_val, subkeystr, "Read subkey value wrong") |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 126 | try: |
| 127 | EnumKey(key, 1) |
| 128 | self.fail("Was able to get a second key when I only have one!") |
| 129 | except EnvironmentError: |
| 130 | pass |
| 131 | |
| 132 | key.Close() |
| 133 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 134 | def DeleteTestData(self, root_key, subkeystr="sub_key"): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 135 | key = OpenKey(root_key, test_key_name, 0, KEY_ALL_ACCESS) |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 136 | sub_key = OpenKey(key, subkeystr, 0, KEY_ALL_ACCESS) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 137 | # It is not necessary to delete the values before deleting |
| 138 | # the key (although subkeys must not exist). We delete them |
| 139 | # manually just to prove we can :-) |
| 140 | for value_name, value_data, value_type in test_data: |
| 141 | DeleteValue(sub_key, value_name) |
| 142 | |
| 143 | nkeys, nvalues, since_mod = QueryInfoKey(sub_key) |
| 144 | self.assertEquals(nkeys, 0, "subkey not empty before delete") |
| 145 | self.assertEquals(nvalues, 0, "subkey not empty before delete") |
| 146 | sub_key.Close() |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 147 | DeleteKey(key, subkeystr) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 148 | |
| 149 | try: |
| 150 | # Shouldnt be able to delete it twice! |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 151 | DeleteKey(key, subkeystr) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 152 | self.fail("Deleting the key twice succeeded") |
| 153 | except EnvironmentError: |
| 154 | pass |
| 155 | key.Close() |
| 156 | DeleteKey(root_key, test_key_name) |
| 157 | # Opening should now fail! |
| 158 | try: |
| 159 | key = OpenKey(root_key, test_key_name) |
| 160 | self.fail("Could open the non-existent key") |
| 161 | except WindowsError: # Use this error name this time |
| 162 | pass |
| 163 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 164 | def TestAll(self, root_key, subkeystr="sub_key"): |
| 165 | self.WriteTestData(root_key, subkeystr) |
| 166 | self.ReadTestData(root_key, subkeystr) |
| 167 | self.DeleteTestData(root_key, subkeystr) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 168 | |
| 169 | def testLocalMachineRegistryWorks(self): |
| 170 | self.TestAll(HKEY_CURRENT_USER) |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 171 | self.TestAll(HKEY_CURRENT_USER, "日本-subkey") |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 172 | |
| 173 | def testConnectRegistryToLocalMachineWorks(self): |
| 174 | # perform minimal ConnectRegistry test which just invokes it |
| 175 | h = ConnectRegistry(None, HKEY_LOCAL_MACHINE) |
| 176 | h.Close() |
| 177 | |
| 178 | def testRemoteMachineRegistryWorks(self): |
| 179 | if not self.remote_name: |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 180 | return # remote machine name not specified |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 181 | remote_key = ConnectRegistry(self.remote_name, HKEY_CURRENT_USER) |
| 182 | self.TestAll(remote_key) |
| 183 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 184 | def testExpandEnvironmentStrings(self): |
| 185 | r = ExpandEnvironmentStrings("%windir%\\test") |
| 186 | self.assertEqual(type(r), str) |
| 187 | self.assertEqual(r, os.environ["windir"] + "\\test") |
| 188 | |
Brian Curtin | 9c9d878 | 2010-05-26 18:09:32 +0000 | [diff] [blame] | 189 | def test_changing_value(self): |
| 190 | # Issue2810: A race condition in 2.6 and 3.1 may cause |
| 191 | # EnumValue or QueryValue to throw "WindowsError: More data is |
| 192 | # available" |
| 193 | done = False |
| 194 | |
| 195 | class VeryActiveThread(threading.Thread): |
| 196 | def run(self): |
| 197 | with CreateKey(HKEY_CURRENT_USER, test_key_name) as key: |
| 198 | use_short = True |
| 199 | long_string = 'x'*2000 |
| 200 | while not done: |
| 201 | s = 'x' if use_short else long_string |
| 202 | use_short = not use_short |
| 203 | SetValue(key, 'changing_value', REG_SZ, s) |
| 204 | |
| 205 | thread = VeryActiveThread() |
| 206 | thread.start() |
| 207 | try: |
| 208 | with CreateKey(HKEY_CURRENT_USER, |
| 209 | test_key_name+'\\changing_value') as key: |
| 210 | for _ in range(1000): |
| 211 | num_subkeys, num_values, t = QueryInfoKey(key) |
| 212 | for i in range(num_values): |
| 213 | name = EnumValue(key, i) |
| 214 | QueryValue(key, name[0]) |
| 215 | finally: |
| 216 | done = True |
| 217 | thread.join() |
| 218 | DeleteKey(HKEY_CURRENT_USER, test_key_name+'\\changing_value') |
| 219 | DeleteKey(HKEY_CURRENT_USER, test_key_name) |
| 220 | |
| 221 | def test_long_key(self): |
| 222 | # Issue2810, in 2.6 and 3.1 when the key name was exactly 256 |
| 223 | # characters, EnumKey threw "WindowsError: More data is |
| 224 | # available" |
| 225 | name = 'x'*256 |
| 226 | try: |
| 227 | with CreateKey(HKEY_CURRENT_USER, test_key_name) as key: |
| 228 | SetValue(key, name, REG_SZ, 'x') |
| 229 | num_subkeys, num_values, t = QueryInfoKey(key) |
| 230 | EnumKey(key, 0) |
| 231 | finally: |
| 232 | DeleteKey(HKEY_CURRENT_USER, '\\'.join((test_key_name, name))) |
| 233 | DeleteKey(HKEY_CURRENT_USER, test_key_name) |
| 234 | |
| 235 | def test_dynamic_key(self): |
| 236 | # Issue2810, when the value is dynamically generated, these |
| 237 | # throw "WindowsError: More data is available" in 2.6 and 3.1 |
| 238 | EnumValue(HKEY_PERFORMANCE_DATA, 0) |
| 239 | QueryValueEx(HKEY_PERFORMANCE_DATA, "") |
| 240 | |
| 241 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 242 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 243 | support.run_unittest(WinregTests) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 244 | |
| 245 | if __name__ == "__main__": |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 246 | try: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 247 | WinregTests.remote_name = sys.argv[sys.argv.index("--remote")+1] |
| 248 | except (IndexError, ValueError): |
| 249 | print("Remote registry calls can be tested using", |
| 250 | "'test_winreg.py --remote \\\\machine_name'") |
| 251 | WinregTests.remote_name = None |
| 252 | test_main() |