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 | |
Fred Drake | 252af9c | 2000-06-29 19:42:00 +0000 | [diff] [blame] | 4 | from _winreg import * |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 5 | import os, sys |
| 6 | |
Guido van Rossum | a8c360e | 2007-07-17 20:50:43 +0000 | [diff] [blame] | 7 | from test.test_support import verify |
Fredrik Lundh | f785042 | 2001-01-17 21:51:36 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 9 | test_key_name = "SOFTWARE\\Python Registry Test Key - Delete Me" |
| 10 | |
| 11 | test_data = [ |
| 12 | ("Int Value", 45, REG_DWORD), |
Guido van Rossum | 0a18552 | 2003-11-30 22:46:18 +0000 | [diff] [blame] | 13 | ("String Val", "A string value", REG_SZ), |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 14 | ("StringExpand", "The path is %path%", REG_EXPAND_SZ), |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 15 | ("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ), |
Guido van Rossum | a8c360e | 2007-07-17 20:50:43 +0000 | [diff] [blame] | 16 | ("Raw Data", bytes("binary"+chr(0)+"data"), REG_BINARY), |
Guido van Rossum | 291481b | 2003-12-03 15:24:02 +0000 | [diff] [blame] | 17 | ("Big String", "x"*(2**14-1), REG_SZ), |
Guido van Rossum | a8c360e | 2007-07-17 20:50:43 +0000 | [diff] [blame] | 18 | ("Big Binary", b"x"*(2**14), REG_BINARY), |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 19 | ] |
| 20 | |
| 21 | def 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é Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 34 | verify(nkeys==1, "Not the correct number of sub keys") |
| 35 | verify(nvalues==1, "Not the correct number of values") |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 36 | nkeys, nvalues, since_mod = QueryInfoKey(sub_key) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 37 | verify(nkeys==0, "Not the correct number of sub keys") |
| 38 | verify(nvalues==len(test_data), "Not the correct number of values") |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 39 | # 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 Prescod | 99b84bd | 2000-07-04 03:38:10 +0000 | [diff] [blame] | 45 | QueryInfoKey(int_sub_key) |
| 46 | raise RuntimeError, "It appears the CloseKey() function does not close the actual key!" |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 47 | except EnvironmentError: |
Paul Prescod | 99b84bd | 2000-07-04 03:38:10 +0000 | [diff] [blame] | 48 | pass |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 49 | # ... and close that key that way :-) |
| 50 | int_key = int(key) |
| 51 | key.Close() |
| 52 | try: |
Paul Prescod | 99b84bd | 2000-07-04 03:38:10 +0000 | [diff] [blame] | 53 | QueryInfoKey(int_key) |
| 54 | raise RuntimeError, "It appears the key.Close() function does not close the actual key!" |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 55 | except EnvironmentError: |
Paul Prescod | 99b84bd | 2000-07-04 03:38:10 +0000 | [diff] [blame] | 56 | pass |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 57 | |
| 58 | def ReadTestData(root_key): |
| 59 | # Check we can get default value for this key. |
| 60 | val = QueryValue(root_key, test_key_name) |
Guido van Rossum | a8c360e | 2007-07-17 20:50:43 +0000 | [diff] [blame] | 61 | verify(type(val) is str and val=="Default value", "Registry didn't give back the correct value") |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 62 | |
| 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é Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 73 | verify(data in test_data, "Didn't read back the correct test data") |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 74 | index = index + 1 |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 75 | verify(index==len(test_data), "Didn't read the correct number of items") |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 76 | # 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é Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 79 | verify(read_val==value_data and read_typ == value_type, \ |
| 80 | "Could not directly read the value" ) |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 81 | sub_key.Close() |
| 82 | # Enumerate our main key. |
| 83 | read_val = EnumKey(key, 0) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 84 | verify(read_val == "sub_key", "Read subkey value wrong") |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 85 | try: |
| 86 | EnumKey(key, 1) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 87 | verify(0, "Was able to get a second key when I only have one!") |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 88 | except EnvironmentError: |
| 89 | pass |
| 90 | |
| 91 | key.Close() |
| 92 | |
| 93 | def 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é Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 103 | verify(nkeys==0 and nvalues==0, "subkey not empty before delete") |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 104 | 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é Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 110 | verify(0, "Deleting the key twice succeeded") |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 111 | 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é Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 118 | verify(0, "Could open the non-existent key") |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 119 | except WindowsError: # Use this error name this time |
| 120 | pass |
| 121 | |
| 122 | def TestAll(root_key): |
| 123 | WriteTestData(root_key) |
| 124 | ReadTestData(root_key) |
Fred Drake | d9b0f26 | 2000-04-01 05:25:57 +0000 | [diff] [blame] | 125 | DeleteTestData(root_key) |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 126 | |
| 127 | # Test on my local machine. |
| 128 | TestAll(HKEY_CURRENT_USER) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 129 | print("Local registry tests worked") |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 130 | try: |
| 131 | remote_name = sys.argv[sys.argv.index("--remote")+1] |
| 132 | except (IndexError, ValueError): |
| 133 | remote_name = None |
| 134 | |
| 135 | if remote_name is not None: |
| 136 | try: |
| 137 | remote_key = ConnectRegistry(remote_name, HKEY_CURRENT_USER) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 138 | except EnvironmentError as exc: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 139 | print("Could not connect to the remote machine -", exc.strerror) |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 140 | remote_key = None |
| 141 | if remote_key is not None: |
| 142 | TestAll(remote_key) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 143 | print("Remote registry tests worked") |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 144 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 145 | print("Remote registry calls can be tested using", end=' ') |
| 146 | print("'test_winreg.py --remote \\\\machine_name'") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 147 | # perform minimal ConnectRegistry test which just invokes it |
| 148 | h = ConnectRegistry(None, HKEY_LOCAL_MACHINE) |
| 149 | h.Close() |