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 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 7 | from test_support import verify, have_unicode |
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), |
| 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 | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 16 | ("Raw Data", ("binary"+chr(0)+"data"), REG_BINARY), |
| 17 | ] |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 18 | if have_unicode: |
| 19 | test_data+=[ |
| 20 | (unicode("Unicode Val"), unicode("A Unicode value"), REG_SZ,), |
| 21 | ("UnicodeExpand", unicode("The path is %path%"), REG_EXPAND_SZ), |
| 22 | ("Multi-unicode", [unicode("Lots"), unicode("of"), unicode("unicode"), unicode("values")], REG_MULTI_SZ), |
| 23 | ("Multi-mixed", [unicode("Unicode"), unicode("and"), "string", "values"],REG_MULTI_SZ), |
| 24 | ] |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 25 | |
| 26 | def WriteTestData(root_key): |
| 27 | # Set the default value for this key. |
| 28 | SetValue(root_key, test_key_name, REG_SZ, "Default value") |
| 29 | key = CreateKey(root_key, test_key_name) |
| 30 | # Create a sub-key |
| 31 | sub_key = CreateKey(key, "sub_key") |
| 32 | # Give the sub-key some named values |
| 33 | |
| 34 | for value_name, value_data, value_type in test_data: |
| 35 | SetValueEx(sub_key, value_name, 0, value_type, value_data) |
| 36 | |
| 37 | # Check we wrote as many items as we thought. |
| 38 | nkeys, nvalues, since_mod = QueryInfoKey(key) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 39 | verify(nkeys==1, "Not the correct number of sub keys") |
| 40 | verify(nvalues==1, "Not the correct number of values") |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 41 | nkeys, nvalues, since_mod = QueryInfoKey(sub_key) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 42 | verify(nkeys==0, "Not the correct number of sub keys") |
| 43 | verify(nvalues==len(test_data), "Not the correct number of values") |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 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: |
Paul Prescod | 99b84bd | 2000-07-04 03:38:10 +0000 | [diff] [blame] | 50 | QueryInfoKey(int_sub_key) |
| 51 | 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] | 52 | except EnvironmentError: |
Paul Prescod | 99b84bd | 2000-07-04 03:38:10 +0000 | [diff] [blame] | 53 | pass |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 54 | # ... and close that key that way :-) |
| 55 | int_key = int(key) |
| 56 | key.Close() |
| 57 | try: |
Paul Prescod | 99b84bd | 2000-07-04 03:38:10 +0000 | [diff] [blame] | 58 | QueryInfoKey(int_key) |
| 59 | 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] | 60 | except EnvironmentError: |
Paul Prescod | 99b84bd | 2000-07-04 03:38:10 +0000 | [diff] [blame] | 61 | pass |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 62 | |
| 63 | def ReadTestData(root_key): |
| 64 | # Check we can get default value for this key. |
| 65 | val = QueryValue(root_key, test_key_name) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 66 | verify(val=="Default value", "Registry didn't give back the correct value") |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 67 | |
| 68 | key = OpenKey(root_key, test_key_name) |
| 69 | # Read the sub-keys |
| 70 | sub_key = OpenKey(key, "sub_key") |
| 71 | # Check I can enumerate over the values. |
| 72 | index = 0 |
| 73 | while 1: |
| 74 | try: |
| 75 | data = EnumValue(sub_key, index) |
| 76 | except EnvironmentError: |
| 77 | break |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 78 | 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] | 79 | index = index + 1 |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 80 | 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] | 81 | # Check I can directly access each item |
| 82 | for value_name, value_data, value_type in test_data: |
| 83 | read_val, read_typ = QueryValueEx(sub_key, value_name) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 84 | verify(read_val==value_data and read_typ == value_type, \ |
| 85 | "Could not directly read the value" ) |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 86 | sub_key.Close() |
| 87 | # Enumerate our main key. |
| 88 | read_val = EnumKey(key, 0) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 89 | verify(read_val == "sub_key", "Read subkey value wrong") |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 90 | try: |
| 91 | EnumKey(key, 1) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 92 | 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] | 93 | except EnvironmentError: |
| 94 | pass |
| 95 | |
| 96 | key.Close() |
| 97 | |
| 98 | def DeleteTestData(root_key): |
| 99 | key = OpenKey(root_key, test_key_name, 0, KEY_ALL_ACCESS) |
| 100 | sub_key = OpenKey(key, "sub_key", 0, KEY_ALL_ACCESS) |
| 101 | # It is not necessary to delete the values before deleting |
| 102 | # the key (although subkeys must not exist). We delete them |
| 103 | # manually just to prove we can :-) |
| 104 | for value_name, value_data, value_type in test_data: |
| 105 | DeleteValue(sub_key, value_name) |
| 106 | |
| 107 | nkeys, nvalues, since_mod = QueryInfoKey(sub_key) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 108 | verify(nkeys==0 and nvalues==0, "subkey not empty before delete") |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 109 | sub_key.Close() |
| 110 | DeleteKey(key, "sub_key") |
| 111 | |
| 112 | try: |
| 113 | # Shouldnt be able to delete it twice! |
| 114 | DeleteKey(key, "sub_key") |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 115 | verify(0, "Deleting the key twice succeeded") |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 116 | except EnvironmentError: |
| 117 | pass |
| 118 | key.Close() |
| 119 | DeleteKey(root_key, test_key_name) |
| 120 | # Opening should now fail! |
| 121 | try: |
| 122 | key = OpenKey(root_key, test_key_name) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 123 | verify(0, "Could open the non-existent key") |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 124 | except WindowsError: # Use this error name this time |
| 125 | pass |
| 126 | |
| 127 | def TestAll(root_key): |
| 128 | WriteTestData(root_key) |
| 129 | ReadTestData(root_key) |
Fred Drake | d9b0f26 | 2000-04-01 05:25:57 +0000 | [diff] [blame] | 130 | DeleteTestData(root_key) |
Guido van Rossum | de59855 | 2000-03-28 20:36:51 +0000 | [diff] [blame] | 131 | |
| 132 | # Test on my local machine. |
| 133 | TestAll(HKEY_CURRENT_USER) |
| 134 | print "Local registry tests worked" |
| 135 | try: |
| 136 | remote_name = sys.argv[sys.argv.index("--remote")+1] |
| 137 | except (IndexError, ValueError): |
| 138 | remote_name = None |
| 139 | |
| 140 | if remote_name is not None: |
| 141 | try: |
| 142 | remote_key = ConnectRegistry(remote_name, HKEY_CURRENT_USER) |
| 143 | except EnvironmentError, exc: |
| 144 | print "Could not connect to the remote machine -", exc.strerror |
| 145 | remote_key = None |
| 146 | if remote_key is not None: |
| 147 | TestAll(remote_key) |
| 148 | print "Remote registry tests worked" |
| 149 | else: |
| 150 | print "Remote registry calls can be tested using", |
| 151 | print "'test_winreg.py --remote \\\\machine_name'" |