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 | |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 4 | import os, sys |
Georg Brandl | 3376a9a | 2007-08-24 17:38:49 +0000 | [diff] [blame] | 5 | import unittest |
Georg Brandl | 3376a9a | 2007-08-24 17:38:49 +0000 | [diff] [blame] | 6 | from test import test_support |
Fredrik Lundh | f785042 | 2001-01-17 21:51:36 +0000 | [diff] [blame] | 7 | |
R. David Murray | 597ebab | 2009-03-31 18:32:17 +0000 | [diff] [blame^] | 8 | # Do this first so test will be skipped if module doesn't exist |
R. David Murray | 59beec3 | 2009-03-30 19:04:00 +0000 | [diff] [blame] | 9 | test_support.import_module('_winreg') |
R. David Murray | 597ebab | 2009-03-31 18:32:17 +0000 | [diff] [blame^] | 10 | # Now import everything |
R. David Murray | 59beec3 | 2009-03-30 19:04:00 +0000 | [diff] [blame] | 11 | from _winreg import * |
| 12 | |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 13 | test_key_name = "SOFTWARE\\Python Registry Test Key - Delete Me" |
| 14 | |
| 15 | test_data = [ |
| 16 | ("Int Value", 45, REG_DWORD), |
Guido van Rossum | 0a18552 | 2003-11-30 22:46:18 +0000 | [diff] [blame] | 17 | ("String Val", "A string value", REG_SZ), |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 18 | ("StringExpand", "The path is %path%", REG_EXPAND_SZ), |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 19 | ("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ), |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 20 | ("Raw Data", ("binary"+chr(0)+"data"), REG_BINARY), |
Guido van Rossum | 291481b | 2003-12-03 15:24:02 +0000 | [diff] [blame] | 21 | ("Big String", "x"*(2**14-1), REG_SZ), |
| 22 | ("Big Binary", "x"*(2**14), REG_BINARY), |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 23 | ] |
Georg Brandl | 3376a9a | 2007-08-24 17:38:49 +0000 | [diff] [blame] | 24 | |
| 25 | if test_support.have_unicode: |
| 26 | test_data += [ |
| 27 | (unicode("Unicode Val"), unicode("A Unicode value"), REG_SZ,), |
| 28 | ("UnicodeExpand", unicode("The path is %path%"), REG_EXPAND_SZ), |
| 29 | ("Multi-unicode", [unicode("Lots"), unicode("of"), unicode("unicode"), |
| 30 | unicode("values")], REG_MULTI_SZ), |
| 31 | ("Multi-mixed", [unicode("Unicode"), unicode("and"), "string", |
| 32 | "values"], REG_MULTI_SZ), |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 33 | ] |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 34 | |
Georg Brandl | 3376a9a | 2007-08-24 17:38:49 +0000 | [diff] [blame] | 35 | class WinregTests(unittest.TestCase): |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 36 | remote_name = None |
| 37 | |
Georg Brandl | 3376a9a | 2007-08-24 17:38:49 +0000 | [diff] [blame] | 38 | def WriteTestData(self, root_key): |
| 39 | # Set the default value for this key. |
| 40 | SetValue(root_key, test_key_name, REG_SZ, "Default value") |
| 41 | key = CreateKey(root_key, test_key_name) |
| 42 | # Create a sub-key |
| 43 | sub_key = CreateKey(key, "sub_key") |
| 44 | # Give the sub-key some named values |
| 45 | |
| 46 | for value_name, value_data, value_type in test_data: |
| 47 | SetValueEx(sub_key, value_name, 0, value_type, value_data) |
| 48 | |
| 49 | # Check we wrote as many items as we thought. |
| 50 | nkeys, nvalues, since_mod = QueryInfoKey(key) |
| 51 | self.assertEquals(nkeys, 1, "Not the correct number of sub keys") |
| 52 | self.assertEquals(nvalues, 1, "Not the correct number of values") |
| 53 | nkeys, nvalues, since_mod = QueryInfoKey(sub_key) |
| 54 | self.assertEquals(nkeys, 0, "Not the correct number of sub keys") |
| 55 | self.assertEquals(nvalues, len(test_data), |
| 56 | "Not the correct number of values") |
| 57 | # Close this key this way... |
| 58 | # (but before we do, copy the key as an integer - this allows |
| 59 | # us to test that the key really gets closed). |
| 60 | int_sub_key = int(sub_key) |
| 61 | CloseKey(sub_key) |
| 62 | try: |
| 63 | QueryInfoKey(int_sub_key) |
| 64 | self.fail("It appears the CloseKey() function does " |
| 65 | "not close the actual key!") |
| 66 | except EnvironmentError: |
| 67 | pass |
| 68 | # ... and close that key that way :-) |
| 69 | int_key = int(key) |
| 70 | key.Close() |
| 71 | try: |
| 72 | QueryInfoKey(int_key) |
| 73 | self.fail("It appears the key.Close() function " |
| 74 | "does not close the actual key!") |
| 75 | except EnvironmentError: |
| 76 | pass |
| 77 | |
| 78 | def ReadTestData(self, root_key): |
| 79 | # Check we can get default value for this key. |
| 80 | val = QueryValue(root_key, test_key_name) |
| 81 | self.assertEquals(val, "Default value", |
| 82 | "Registry didn't give back the correct value") |
| 83 | |
| 84 | key = OpenKey(root_key, test_key_name) |
| 85 | # Read the sub-keys |
Christian Heimes | b39a756 | 2008-01-08 15:46:10 +0000 | [diff] [blame] | 86 | with OpenKey(key, "sub_key") as sub_key: |
| 87 | # Check I can enumerate over the values. |
| 88 | index = 0 |
| 89 | while 1: |
| 90 | try: |
| 91 | data = EnumValue(sub_key, index) |
| 92 | except EnvironmentError: |
| 93 | break |
| 94 | self.assertEquals(data in test_data, True, |
| 95 | "Didn't read back the correct test data") |
| 96 | index = index + 1 |
| 97 | self.assertEquals(index, len(test_data), |
| 98 | "Didn't read the correct number of items") |
| 99 | # Check I can directly access each item |
| 100 | for value_name, value_data, value_type in test_data: |
| 101 | read_val, read_typ = QueryValueEx(sub_key, value_name) |
| 102 | self.assertEquals(read_val, value_data, |
| 103 | "Could not directly read the value") |
| 104 | self.assertEquals(read_typ, value_type, |
| 105 | "Could not directly read the value") |
Georg Brandl | 3376a9a | 2007-08-24 17:38:49 +0000 | [diff] [blame] | 106 | sub_key.Close() |
| 107 | # Enumerate our main key. |
| 108 | read_val = EnumKey(key, 0) |
| 109 | self.assertEquals(read_val, "sub_key", "Read subkey value wrong") |
| 110 | try: |
| 111 | EnumKey(key, 1) |
| 112 | self.fail("Was able to get a second key when I only have one!") |
| 113 | except EnvironmentError: |
| 114 | pass |
| 115 | |
| 116 | key.Close() |
| 117 | |
| 118 | def DeleteTestData(self, root_key): |
| 119 | key = OpenKey(root_key, test_key_name, 0, KEY_ALL_ACCESS) |
| 120 | sub_key = OpenKey(key, "sub_key", 0, KEY_ALL_ACCESS) |
| 121 | # It is not necessary to delete the values before deleting |
| 122 | # the key (although subkeys must not exist). We delete them |
| 123 | # manually just to prove we can :-) |
| 124 | for value_name, value_data, value_type in test_data: |
| 125 | DeleteValue(sub_key, value_name) |
| 126 | |
| 127 | nkeys, nvalues, since_mod = QueryInfoKey(sub_key) |
| 128 | self.assertEquals(nkeys, 0, "subkey not empty before delete") |
| 129 | self.assertEquals(nvalues, 0, "subkey not empty before delete") |
| 130 | sub_key.Close() |
| 131 | DeleteKey(key, "sub_key") |
| 132 | |
| 133 | try: |
| 134 | # Shouldnt be able to delete it twice! |
| 135 | DeleteKey(key, "sub_key") |
| 136 | self.fail("Deleting the key twice succeeded") |
| 137 | except EnvironmentError: |
| 138 | pass |
| 139 | key.Close() |
| 140 | DeleteKey(root_key, test_key_name) |
| 141 | # Opening should now fail! |
| 142 | try: |
| 143 | key = OpenKey(root_key, test_key_name) |
| 144 | self.fail("Could open the non-existent key") |
| 145 | except WindowsError: # Use this error name this time |
| 146 | pass |
| 147 | |
| 148 | def TestAll(self, root_key): |
| 149 | self.WriteTestData(root_key) |
| 150 | self.ReadTestData(root_key) |
| 151 | self.DeleteTestData(root_key) |
| 152 | |
| 153 | def testLocalMachineRegistryWorks(self): |
| 154 | self.TestAll(HKEY_CURRENT_USER) |
| 155 | |
| 156 | def testConnectRegistryToLocalMachineWorks(self): |
| 157 | # perform minimal ConnectRegistry test which just invokes it |
| 158 | h = ConnectRegistry(None, HKEY_LOCAL_MACHINE) |
| 159 | h.Close() |
| 160 | |
| 161 | def testRemoteMachineRegistryWorks(self): |
| 162 | if not self.remote_name: |
Georg Brandl | 0226d85 | 2007-08-30 12:32:23 +0000 | [diff] [blame] | 163 | return # remote machine name not specified |
Georg Brandl | 3376a9a | 2007-08-24 17:38:49 +0000 | [diff] [blame] | 164 | remote_key = ConnectRegistry(self.remote_name, HKEY_CURRENT_USER) |
| 165 | self.TestAll(remote_key) |
| 166 | |
Christian Heimes | b39a756 | 2008-01-08 15:46:10 +0000 | [diff] [blame] | 167 | def testExpandEnvironmentStrings(self): |
| 168 | r = ExpandEnvironmentStrings(u"%windir%\\test") |
| 169 | self.assertEqual(type(r), unicode) |
| 170 | self.assertEqual(r, os.environ["windir"] + "\\test") |
| 171 | |
Georg Brandl | 3376a9a | 2007-08-24 17:38:49 +0000 | [diff] [blame] | 172 | def test_main(): |
| 173 | test_support.run_unittest(WinregTests) |
| 174 | |
| 175 | if __name__ == "__main__": |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 176 | try: |
Georg Brandl | 3376a9a | 2007-08-24 17:38:49 +0000 | [diff] [blame] | 177 | WinregTests.remote_name = sys.argv[sys.argv.index("--remote")+1] |
| 178 | except (IndexError, ValueError): |
| 179 | print "Remote registry calls can be tested using", |
| 180 | print "'test_winreg.py --remote \\\\machine_name'" |
| 181 | WinregTests.remote_name = None |
| 182 | test_main() |