Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 1 | # Test the windows specific win32reg module. |
| 2 | # Only win32reg functions not hit here: FlushKey, LoadKey and SaveKey |
| 3 | |
Antoine Pitrou | 2bb3021 | 2012-07-13 22:46:41 +0200 | [diff] [blame] | 4 | import os, sys, errno |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 5 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 6 | from test import support |
Hai Shi | f7ba40b | 2020-06-25 18:38:51 +0800 | [diff] [blame] | 7 | from test.support import import_helper |
Antoine Pitrou | 88c60c9 | 2017-09-18 23:50:44 +0200 | [diff] [blame] | 8 | import threading |
Paul Monson | 62dfd7d | 2019-04-25 11:36:45 -0700 | [diff] [blame] | 9 | from platform import machine, win32_edition |
Fredrik Lundh | f785042 | 2001-01-17 21:51:36 +0000 | [diff] [blame] | 10 | |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 11 | # Do this first so test will be skipped if module doesn't exist |
Hai Shi | f7ba40b | 2020-06-25 18:38:51 +0800 | [diff] [blame] | 12 | import_helper.import_module('winreg', required_on=['win']) |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 13 | # Now import everything |
| 14 | from winreg import * |
| 15 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 16 | try: |
| 17 | REMOTE_NAME = sys.argv[sys.argv.index("--remote")+1] |
| 18 | except (IndexError, ValueError): |
| 19 | REMOTE_NAME = None |
| 20 | |
| 21 | # tuple of (major, minor) |
| 22 | WIN_VER = sys.getwindowsversion()[:2] |
| 23 | # Some tests should only run on 64-bit architectures where WOW64 will be. |
| 24 | WIN64_MACHINE = True if machine() == "AMD64" else False |
| 25 | |
| 26 | # Starting with Windows 7 and Windows Server 2008 R2, WOW64 no longer uses |
| 27 | # registry reflection and formerly reflected keys are shared instead. |
| 28 | # Windows 7 and Windows Server 2008 R2 are version 6.1. Due to this, some |
| 29 | # tests are only valid up until 6.1 |
| 30 | HAS_REFLECTION = True if WIN_VER < (6, 1) else False |
| 31 | |
R David Murray | 4140fb5 | 2013-04-21 10:08:02 -0400 | [diff] [blame] | 32 | # Use a per-process key to prevent concurrent test runs (buildbot!) from |
| 33 | # stomping on each other. |
| 34 | test_key_base = "Python Test Key [%d] - Delete Me" % (os.getpid(),) |
| 35 | test_key_name = "SOFTWARE\\" + test_key_base |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 36 | # On OS'es that support reflection we should test with a reflected key |
R David Murray | 4140fb5 | 2013-04-21 10:08:02 -0400 | [diff] [blame] | 37 | test_reflect_key_name = "SOFTWARE\\Classes\\" + test_key_base |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 38 | |
| 39 | test_data = [ |
| 40 | ("Int Value", 45, REG_DWORD), |
Steve Dower | 80ac11d | 2016-05-24 15:42:04 -0700 | [diff] [blame] | 41 | ("Qword Value", 0x1122334455667788, REG_QWORD), |
Guido van Rossum | 0a18552 | 2003-11-30 22:46:18 +0000 | [diff] [blame] | 42 | ("String Val", "A string value", REG_SZ), |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 43 | ("StringExpand", "The path is %path%", REG_EXPAND_SZ), |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 44 | ("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ), |
Zackery Spytz | e223ba1 | 2019-09-09 03:26:15 -0600 | [diff] [blame] | 45 | ("Multi-nul", ["", "", "", ""], REG_MULTI_SZ), |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 46 | ("Raw Data", b"binary\x00data", REG_BINARY), |
Guido van Rossum | 291481b | 2003-12-03 15:24:02 +0000 | [diff] [blame] | 47 | ("Big String", "x"*(2**14-1), REG_SZ), |
Guido van Rossum | a8c360e | 2007-07-17 20:50:43 +0000 | [diff] [blame] | 48 | ("Big Binary", b"x"*(2**14), REG_BINARY), |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 49 | # Two and three kanjis, meaning: "Japan" and "Japanese") |
| 50 | ("Japanese 日本", "日本語", REG_SZ), |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 51 | ] |
| 52 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 53 | class BaseWinregTests(unittest.TestCase): |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 54 | |
Martin v. Löwis | b7a5156 | 2009-06-07 17:55:17 +0000 | [diff] [blame] | 55 | def setUp(self): |
| 56 | # Make sure that the test key is absent when the test |
| 57 | # starts. |
| 58 | self.delete_tree(HKEY_CURRENT_USER, test_key_name) |
| 59 | |
| 60 | def delete_tree(self, root, subkey): |
| 61 | try: |
Steve Dower | 40fa266 | 2016-12-17 13:30:27 -0800 | [diff] [blame] | 62 | hkey = OpenKey(root, subkey, 0, KEY_ALL_ACCESS) |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 63 | except OSError: |
Martin v. Löwis | b7a5156 | 2009-06-07 17:55:17 +0000 | [diff] [blame] | 64 | # subkey does not exist |
| 65 | return |
| 66 | while True: |
| 67 | try: |
| 68 | subsubkey = EnumKey(hkey, 0) |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 69 | except OSError: |
Martin v. Löwis | b7a5156 | 2009-06-07 17:55:17 +0000 | [diff] [blame] | 70 | # no more subkeys |
| 71 | break |
| 72 | self.delete_tree(hkey, subsubkey) |
| 73 | CloseKey(hkey) |
| 74 | DeleteKey(root, subkey) |
| 75 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 76 | def _write_test_data(self, root_key, subkeystr="sub_key", |
| 77 | CreateKey=CreateKey): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 78 | # Set the default value for this key. |
| 79 | SetValue(root_key, test_key_name, REG_SZ, "Default value") |
| 80 | key = CreateKey(root_key, test_key_name) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 81 | self.assertTrue(key.handle != 0) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 82 | # Create a sub-key |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 83 | sub_key = CreateKey(key, subkeystr) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 84 | # Give the sub-key some named values |
| 85 | |
| 86 | for value_name, value_data, value_type in test_data: |
| 87 | SetValueEx(sub_key, value_name, 0, value_type, value_data) |
| 88 | |
| 89 | # Check we wrote as many items as we thought. |
| 90 | nkeys, nvalues, since_mod = QueryInfoKey(key) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 91 | self.assertEqual(nkeys, 1, "Not the correct number of sub keys") |
| 92 | self.assertEqual(nvalues, 1, "Not the correct number of values") |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 93 | nkeys, nvalues, since_mod = QueryInfoKey(sub_key) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 94 | self.assertEqual(nkeys, 0, "Not the correct number of sub keys") |
| 95 | self.assertEqual(nvalues, len(test_data), |
| 96 | "Not the correct number of values") |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 97 | # Close this key this way... |
| 98 | # (but before we do, copy the key as an integer - this allows |
| 99 | # us to test that the key really gets closed). |
| 100 | int_sub_key = int(sub_key) |
| 101 | CloseKey(sub_key) |
| 102 | try: |
| 103 | QueryInfoKey(int_sub_key) |
| 104 | self.fail("It appears the CloseKey() function does " |
| 105 | "not close the actual key!") |
Andrew Svetlov | 3438fa4 | 2012-12-17 23:35:18 +0200 | [diff] [blame] | 106 | except OSError: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 107 | pass |
| 108 | # ... and close that key that way :-) |
| 109 | int_key = int(key) |
| 110 | key.Close() |
| 111 | try: |
| 112 | QueryInfoKey(int_key) |
| 113 | self.fail("It appears the key.Close() function " |
| 114 | "does not close the actual key!") |
Andrew Svetlov | 3438fa4 | 2012-12-17 23:35:18 +0200 | [diff] [blame] | 115 | except OSError: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 116 | pass |
| 117 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 118 | def _read_test_data(self, root_key, subkeystr="sub_key", OpenKey=OpenKey): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 119 | # Check we can get default value for this key. |
| 120 | val = QueryValue(root_key, test_key_name) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 121 | self.assertEqual(val, "Default value", |
| 122 | "Registry didn't give back the correct value") |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 123 | |
| 124 | key = OpenKey(root_key, test_key_name) |
| 125 | # Read the sub-keys |
Thomas Heller | e83ebd9 | 2008-01-24 10:31:31 +0000 | [diff] [blame] | 126 | with OpenKey(key, subkeystr) as sub_key: |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 127 | # Check I can enumerate over the values. |
| 128 | index = 0 |
| 129 | while 1: |
| 130 | try: |
| 131 | data = EnumValue(sub_key, index) |
Andrew Svetlov | 3438fa4 | 2012-12-17 23:35:18 +0200 | [diff] [blame] | 132 | except OSError: |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 133 | break |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 134 | self.assertEqual(data in test_data, True, |
| 135 | "Didn't read back the correct test data") |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 136 | index = index + 1 |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 137 | self.assertEqual(index, len(test_data), |
| 138 | "Didn't read the correct number of items") |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 139 | # Check I can directly access each item |
| 140 | for value_name, value_data, value_type in test_data: |
| 141 | read_val, read_typ = QueryValueEx(sub_key, value_name) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 142 | self.assertEqual(read_val, value_data, |
| 143 | "Could not directly read the value") |
| 144 | self.assertEqual(read_typ, value_type, |
| 145 | "Could not directly read the value") |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 146 | sub_key.Close() |
| 147 | # Enumerate our main key. |
| 148 | read_val = EnumKey(key, 0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 149 | self.assertEqual(read_val, subkeystr, "Read subkey value wrong") |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 150 | try: |
| 151 | EnumKey(key, 1) |
| 152 | self.fail("Was able to get a second key when I only have one!") |
Andrew Svetlov | 3438fa4 | 2012-12-17 23:35:18 +0200 | [diff] [blame] | 153 | except OSError: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 154 | pass |
| 155 | |
| 156 | key.Close() |
| 157 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 158 | def _delete_test_data(self, root_key, subkeystr="sub_key"): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 159 | 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] | 160 | sub_key = OpenKey(key, subkeystr, 0, KEY_ALL_ACCESS) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 161 | # It is not necessary to delete the values before deleting |
| 162 | # the key (although subkeys must not exist). We delete them |
| 163 | # manually just to prove we can :-) |
| 164 | for value_name, value_data, value_type in test_data: |
| 165 | DeleteValue(sub_key, value_name) |
| 166 | |
| 167 | nkeys, nvalues, since_mod = QueryInfoKey(sub_key) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 168 | self.assertEqual(nkeys, 0, "subkey not empty before delete") |
| 169 | self.assertEqual(nvalues, 0, "subkey not empty before delete") |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 170 | sub_key.Close() |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 171 | DeleteKey(key, subkeystr) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 172 | |
| 173 | try: |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 174 | # Shouldn't be able to delete it twice! |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 175 | DeleteKey(key, subkeystr) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 176 | self.fail("Deleting the key twice succeeded") |
Andrew Svetlov | 3438fa4 | 2012-12-17 23:35:18 +0200 | [diff] [blame] | 177 | except OSError: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 178 | pass |
| 179 | key.Close() |
| 180 | DeleteKey(root_key, test_key_name) |
| 181 | # Opening should now fail! |
| 182 | try: |
| 183 | key = OpenKey(root_key, test_key_name) |
| 184 | self.fail("Could open the non-existent key") |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 185 | except OSError: # Use this error name this time |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 186 | pass |
| 187 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 188 | def _test_all(self, root_key, subkeystr="sub_key"): |
| 189 | self._write_test_data(root_key, subkeystr) |
| 190 | self._read_test_data(root_key, subkeystr) |
| 191 | self._delete_test_data(root_key, subkeystr) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 192 | |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 193 | def _test_named_args(self, key, sub_key): |
| 194 | with CreateKeyEx(key=key, sub_key=sub_key, reserved=0, |
| 195 | access=KEY_ALL_ACCESS) as ckey: |
| 196 | self.assertTrue(ckey.handle != 0) |
| 197 | |
| 198 | with OpenKeyEx(key=key, sub_key=sub_key, reserved=0, |
| 199 | access=KEY_ALL_ACCESS) as okey: |
| 200 | self.assertTrue(okey.handle != 0) |
| 201 | |
| 202 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 203 | class LocalWinregTests(BaseWinregTests): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 204 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 205 | def test_registry_works(self): |
| 206 | self._test_all(HKEY_CURRENT_USER) |
| 207 | self._test_all(HKEY_CURRENT_USER, "日本-subkey") |
| 208 | |
| 209 | def test_registry_works_extended_functions(self): |
| 210 | # Substitute the regular CreateKey and OpenKey calls with their |
| 211 | # extended counterparts. |
| 212 | # Note: DeleteKeyEx is not used here because it is platform dependent |
| 213 | cke = lambda key, sub_key: CreateKeyEx(key, sub_key, 0, KEY_ALL_ACCESS) |
| 214 | self._write_test_data(HKEY_CURRENT_USER, CreateKey=cke) |
| 215 | |
| 216 | oke = lambda key, sub_key: OpenKeyEx(key, sub_key, 0, KEY_READ) |
| 217 | self._read_test_data(HKEY_CURRENT_USER, OpenKey=oke) |
| 218 | |
| 219 | self._delete_test_data(HKEY_CURRENT_USER) |
| 220 | |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 221 | def test_named_arguments(self): |
| 222 | self._test_named_args(HKEY_CURRENT_USER, test_key_name) |
| 223 | # Use the regular DeleteKey to clean up |
| 224 | # DeleteKeyEx takes named args and is tested separately |
| 225 | DeleteKey(HKEY_CURRENT_USER, test_key_name) |
| 226 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 227 | def test_connect_registry_to_local_machine_works(self): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 228 | # perform minimal ConnectRegistry test which just invokes it |
| 229 | h = ConnectRegistry(None, HKEY_LOCAL_MACHINE) |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 230 | self.assertNotEqual(h.handle, 0) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 231 | h.Close() |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 232 | self.assertEqual(h.handle, 0) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 233 | |
Min ho Kim | 39d87b5 | 2019-08-31 06:21:19 +1000 | [diff] [blame] | 234 | def test_nonexistent_remote_registry(self): |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 235 | connect = lambda: ConnectRegistry("abcdefghijkl", HKEY_CURRENT_USER) |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 236 | self.assertRaises(OSError, connect) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 237 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 238 | def testExpandEnvironmentStrings(self): |
| 239 | r = ExpandEnvironmentStrings("%windir%\\test") |
| 240 | self.assertEqual(type(r), str) |
| 241 | self.assertEqual(r, os.environ["windir"] + "\\test") |
| 242 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 243 | def test_context_manager(self): |
| 244 | # ensure that the handle is closed if an exception occurs |
| 245 | try: |
| 246 | with ConnectRegistry(None, HKEY_LOCAL_MACHINE) as h: |
| 247 | self.assertNotEqual(h.handle, 0) |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 248 | raise OSError |
| 249 | except OSError: |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 250 | self.assertEqual(h.handle, 0) |
| 251 | |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 252 | def test_changing_value(self): |
| 253 | # Issue2810: A race condition in 2.6 and 3.1 may cause |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 254 | # EnumValue or QueryValue to raise "WindowsError: More data is |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 255 | # available" |
| 256 | done = False |
| 257 | |
| 258 | class VeryActiveThread(threading.Thread): |
| 259 | def run(self): |
| 260 | with CreateKey(HKEY_CURRENT_USER, test_key_name) as key: |
| 261 | use_short = True |
| 262 | long_string = 'x'*2000 |
| 263 | while not done: |
| 264 | s = 'x' if use_short else long_string |
| 265 | use_short = not use_short |
| 266 | SetValue(key, 'changing_value', REG_SZ, s) |
| 267 | |
| 268 | thread = VeryActiveThread() |
| 269 | thread.start() |
| 270 | try: |
| 271 | with CreateKey(HKEY_CURRENT_USER, |
| 272 | test_key_name+'\\changing_value') as key: |
| 273 | for _ in range(1000): |
| 274 | num_subkeys, num_values, t = QueryInfoKey(key) |
| 275 | for i in range(num_values): |
| 276 | name = EnumValue(key, i) |
| 277 | QueryValue(key, name[0]) |
| 278 | finally: |
| 279 | done = True |
| 280 | thread.join() |
| 281 | DeleteKey(HKEY_CURRENT_USER, test_key_name+'\\changing_value') |
| 282 | DeleteKey(HKEY_CURRENT_USER, test_key_name) |
| 283 | |
| 284 | def test_long_key(self): |
| 285 | # Issue2810, in 2.6 and 3.1 when the key name was exactly 256 |
Andrew Svetlov | 7bd61cb | 2012-12-19 22:49:25 +0200 | [diff] [blame] | 286 | # characters, EnumKey raised "WindowsError: More data is |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 287 | # available" |
| 288 | name = 'x'*256 |
| 289 | try: |
| 290 | with CreateKey(HKEY_CURRENT_USER, test_key_name) as key: |
| 291 | SetValue(key, name, REG_SZ, 'x') |
| 292 | num_subkeys, num_values, t = QueryInfoKey(key) |
| 293 | EnumKey(key, 0) |
| 294 | finally: |
| 295 | DeleteKey(HKEY_CURRENT_USER, '\\'.join((test_key_name, name))) |
| 296 | DeleteKey(HKEY_CURRENT_USER, test_key_name) |
| 297 | |
| 298 | def test_dynamic_key(self): |
| 299 | # Issue2810, when the value is dynamically generated, these |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 300 | # raise "WindowsError: More data is available" in 2.6 and 3.1 |
Antoine Pitrou | 2bb3021 | 2012-07-13 22:46:41 +0200 | [diff] [blame] | 301 | try: |
| 302 | EnumValue(HKEY_PERFORMANCE_DATA, 0) |
| 303 | except OSError as e: |
| 304 | if e.errno in (errno.EPERM, errno.EACCES): |
| 305 | self.skipTest("access denied to registry key " |
| 306 | "(are you running in a non-interactive session?)") |
| 307 | raise |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 308 | QueryValueEx(HKEY_PERFORMANCE_DATA, "") |
| 309 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 310 | # Reflection requires XP x64/Vista at a minimum. XP doesn't have this stuff |
| 311 | # or DeleteKeyEx so make sure their use raises NotImplementedError |
| 312 | @unittest.skipUnless(WIN_VER < (5, 2), "Requires Windows XP") |
| 313 | def test_reflection_unsupported(self): |
| 314 | try: |
| 315 | with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck: |
| 316 | self.assertNotEqual(ck.handle, 0) |
| 317 | |
| 318 | key = OpenKey(HKEY_CURRENT_USER, test_key_name) |
| 319 | self.assertNotEqual(key.handle, 0) |
| 320 | |
| 321 | with self.assertRaises(NotImplementedError): |
| 322 | DisableReflectionKey(key) |
| 323 | with self.assertRaises(NotImplementedError): |
| 324 | EnableReflectionKey(key) |
| 325 | with self.assertRaises(NotImplementedError): |
| 326 | QueryReflectionKey(key) |
| 327 | with self.assertRaises(NotImplementedError): |
| 328 | DeleteKeyEx(HKEY_CURRENT_USER, test_key_name) |
| 329 | finally: |
| 330 | DeleteKey(HKEY_CURRENT_USER, test_key_name) |
| 331 | |
Brian Curtin | 12706f2 | 2012-12-27 10:12:45 -0600 | [diff] [blame] | 332 | def test_setvalueex_value_range(self): |
| 333 | # Test for Issue #14420, accept proper ranges for SetValueEx. |
| 334 | # Py2Reg, which gets called by SetValueEx, was using PyLong_AsLong, |
| 335 | # thus raising OverflowError. The implementation now uses |
| 336 | # PyLong_AsUnsignedLong to match DWORD's size. |
| 337 | try: |
| 338 | with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck: |
| 339 | self.assertNotEqual(ck.handle, 0) |
| 340 | SetValueEx(ck, "test_name", None, REG_DWORD, 0x80000000) |
| 341 | finally: |
| 342 | DeleteKey(HKEY_CURRENT_USER, test_key_name) |
| 343 | |
Brian Curtin | 172e422 | 2012-12-27 14:04:42 -0600 | [diff] [blame] | 344 | def test_queryvalueex_return_value(self): |
| 345 | # Test for Issue #16759, return unsigned int from QueryValueEx. |
| 346 | # Reg2Py, which gets called by QueryValueEx, was returning a value |
Zachary Ware | ad4690f | 2014-07-03 10:58:06 -0500 | [diff] [blame] | 347 | # generated by PyLong_FromLong. The implementation now uses |
Brian Curtin | 172e422 | 2012-12-27 14:04:42 -0600 | [diff] [blame] | 348 | # PyLong_FromUnsignedLong to match DWORD's size. |
| 349 | try: |
| 350 | with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck: |
| 351 | self.assertNotEqual(ck.handle, 0) |
| 352 | test_val = 0x80000000 |
| 353 | SetValueEx(ck, "test_name", None, REG_DWORD, test_val) |
| 354 | ret_val, ret_type = QueryValueEx(ck, "test_name") |
| 355 | self.assertEqual(ret_type, REG_DWORD) |
| 356 | self.assertEqual(ret_val, test_val) |
| 357 | finally: |
| 358 | DeleteKey(HKEY_CURRENT_USER, test_key_name) |
| 359 | |
Zachary Ware | ad4690f | 2014-07-03 10:58:06 -0500 | [diff] [blame] | 360 | def test_setvalueex_crash_with_none_arg(self): |
| 361 | # Test for Issue #21151, segfault when None is passed to SetValueEx |
| 362 | try: |
| 363 | with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck: |
| 364 | self.assertNotEqual(ck.handle, 0) |
| 365 | test_val = None |
| 366 | SetValueEx(ck, "test_name", 0, REG_BINARY, test_val) |
| 367 | ret_val, ret_type = QueryValueEx(ck, "test_name") |
| 368 | self.assertEqual(ret_type, REG_BINARY) |
| 369 | self.assertEqual(ret_val, test_val) |
| 370 | finally: |
| 371 | DeleteKey(HKEY_CURRENT_USER, test_key_name) |
| 372 | |
Steve Dower | 40fa266 | 2016-12-17 13:30:27 -0800 | [diff] [blame] | 373 | def test_read_string_containing_null(self): |
| 374 | # Test for issue 25778: REG_SZ should not contain null characters |
| 375 | try: |
| 376 | with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck: |
| 377 | self.assertNotEqual(ck.handle, 0) |
| 378 | test_val = "A string\x00 with a null" |
| 379 | SetValueEx(ck, "test_name", 0, REG_SZ, test_val) |
| 380 | ret_val, ret_type = QueryValueEx(ck, "test_name") |
| 381 | self.assertEqual(ret_type, REG_SZ) |
| 382 | self.assertEqual(ret_val, "A string") |
| 383 | finally: |
| 384 | DeleteKey(HKEY_CURRENT_USER, test_key_name) |
Brian Curtin | 172e422 | 2012-12-27 14:04:42 -0600 | [diff] [blame] | 385 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 386 | |
| 387 | @unittest.skipUnless(REMOTE_NAME, "Skipping remote registry tests") |
| 388 | class RemoteWinregTests(BaseWinregTests): |
| 389 | |
| 390 | def test_remote_registry_works(self): |
| 391 | remote_key = ConnectRegistry(REMOTE_NAME, HKEY_CURRENT_USER) |
| 392 | self._test_all(remote_key) |
| 393 | |
| 394 | |
| 395 | @unittest.skipUnless(WIN64_MACHINE, "x64 specific registry tests") |
| 396 | class Win64WinregTests(BaseWinregTests): |
| 397 | |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 398 | def test_named_arguments(self): |
| 399 | self._test_named_args(HKEY_CURRENT_USER, test_key_name) |
| 400 | # Clean up and also exercise the named arguments |
| 401 | DeleteKeyEx(key=HKEY_CURRENT_USER, sub_key=test_key_name, |
| 402 | access=KEY_ALL_ACCESS, reserved=0) |
| 403 | |
Paul Monson | 62dfd7d | 2019-04-25 11:36:45 -0700 | [diff] [blame] | 404 | @unittest.skipIf(win32_edition() in ('WindowsCoreHeadless', 'IoTEdgeOS'), "APIs not available on WindowsCoreHeadless") |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 405 | def test_reflection_functions(self): |
| 406 | # Test that we can call the query, enable, and disable functions |
| 407 | # on a key which isn't on the reflection list with no consequences. |
| 408 | with OpenKey(HKEY_LOCAL_MACHINE, "Software") as key: |
| 409 | # HKLM\Software is redirected but not reflected in all OSes |
| 410 | self.assertTrue(QueryReflectionKey(key)) |
Raymond Hettinger | 7beae8a | 2011-01-06 05:34:17 +0000 | [diff] [blame] | 411 | self.assertIsNone(EnableReflectionKey(key)) |
| 412 | self.assertIsNone(DisableReflectionKey(key)) |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 413 | self.assertTrue(QueryReflectionKey(key)) |
| 414 | |
| 415 | @unittest.skipUnless(HAS_REFLECTION, "OS doesn't support reflection") |
| 416 | def test_reflection(self): |
| 417 | # Test that we can create, open, and delete keys in the 32-bit |
| 418 | # area. Because we are doing this in a key which gets reflected, |
| 419 | # test the differences of 32 and 64-bit keys before and after the |
| 420 | # reflection occurs (ie. when the created key is closed). |
| 421 | try: |
| 422 | with CreateKeyEx(HKEY_CURRENT_USER, test_reflect_key_name, 0, |
| 423 | KEY_ALL_ACCESS | KEY_WOW64_32KEY) as created_key: |
| 424 | self.assertNotEqual(created_key.handle, 0) |
| 425 | |
| 426 | # The key should now be available in the 32-bit area |
| 427 | with OpenKey(HKEY_CURRENT_USER, test_reflect_key_name, 0, |
| 428 | KEY_ALL_ACCESS | KEY_WOW64_32KEY) as key: |
| 429 | self.assertNotEqual(key.handle, 0) |
| 430 | |
| 431 | # Write a value to what currently is only in the 32-bit area |
| 432 | SetValueEx(created_key, "", 0, REG_SZ, "32KEY") |
| 433 | |
| 434 | # The key is not reflected until created_key is closed. |
| 435 | # The 64-bit version of the key should not be available yet. |
| 436 | open_fail = lambda: OpenKey(HKEY_CURRENT_USER, |
| 437 | test_reflect_key_name, 0, |
| 438 | KEY_READ | KEY_WOW64_64KEY) |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 439 | self.assertRaises(OSError, open_fail) |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 440 | |
| 441 | # Now explicitly open the 64-bit version of the key |
| 442 | with OpenKey(HKEY_CURRENT_USER, test_reflect_key_name, 0, |
| 443 | KEY_ALL_ACCESS | KEY_WOW64_64KEY) as key: |
| 444 | self.assertNotEqual(key.handle, 0) |
| 445 | # Make sure the original value we set is there |
| 446 | self.assertEqual("32KEY", QueryValue(key, "")) |
| 447 | # Set a new value, which will get reflected to 32-bit |
| 448 | SetValueEx(key, "", 0, REG_SZ, "64KEY") |
| 449 | |
| 450 | # Reflection uses a "last-writer wins policy, so the value we set |
| 451 | # on the 64-bit key should be the same on 32-bit |
| 452 | with OpenKey(HKEY_CURRENT_USER, test_reflect_key_name, 0, |
| 453 | KEY_READ | KEY_WOW64_32KEY) as key: |
| 454 | self.assertEqual("64KEY", QueryValue(key, "")) |
| 455 | finally: |
| 456 | DeleteKeyEx(HKEY_CURRENT_USER, test_reflect_key_name, |
| 457 | KEY_WOW64_32KEY, 0) |
| 458 | |
| 459 | @unittest.skipUnless(HAS_REFLECTION, "OS doesn't support reflection") |
| 460 | def test_disable_reflection(self): |
| 461 | # Make use of a key which gets redirected and reflected |
| 462 | try: |
| 463 | with CreateKeyEx(HKEY_CURRENT_USER, test_reflect_key_name, 0, |
| 464 | KEY_ALL_ACCESS | KEY_WOW64_32KEY) as created_key: |
| 465 | # QueryReflectionKey returns whether or not the key is disabled |
| 466 | disabled = QueryReflectionKey(created_key) |
| 467 | self.assertEqual(type(disabled), bool) |
| 468 | # HKCU\Software\Classes is reflected by default |
| 469 | self.assertFalse(disabled) |
| 470 | |
| 471 | DisableReflectionKey(created_key) |
| 472 | self.assertTrue(QueryReflectionKey(created_key)) |
| 473 | |
| 474 | # The key is now closed and would normally be reflected to the |
| 475 | # 64-bit area, but let's make sure that didn't happen. |
| 476 | open_fail = lambda: OpenKeyEx(HKEY_CURRENT_USER, |
| 477 | test_reflect_key_name, 0, |
| 478 | KEY_READ | KEY_WOW64_64KEY) |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 479 | self.assertRaises(OSError, open_fail) |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 480 | |
| 481 | # Make sure the 32-bit key is actually there |
| 482 | with OpenKeyEx(HKEY_CURRENT_USER, test_reflect_key_name, 0, |
| 483 | KEY_READ | KEY_WOW64_32KEY) as key: |
| 484 | self.assertNotEqual(key.handle, 0) |
| 485 | finally: |
| 486 | DeleteKeyEx(HKEY_CURRENT_USER, test_reflect_key_name, |
| 487 | KEY_WOW64_32KEY, 0) |
| 488 | |
Ezio Melotti | 28faf03 | 2013-05-04 17:46:23 +0300 | [diff] [blame] | 489 | def test_exception_numbers(self): |
| 490 | with self.assertRaises(FileNotFoundError) as ctx: |
| 491 | QueryValue(HKEY_CLASSES_ROOT, 'some_value_that_does_not_exist') |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 492 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 493 | def test_main(): |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 494 | support.run_unittest(LocalWinregTests, RemoteWinregTests, |
| 495 | Win64WinregTests) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 496 | |
| 497 | if __name__ == "__main__": |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 498 | if not REMOTE_NAME: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 499 | print("Remote registry calls can be tested using", |
| 500 | "'test_winreg.py --remote \\\\machine_name'") |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 501 | test_main() |