OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import _winreg | 5 import _winreg |
6 | 6 |
7 import verifier | 7 import verifier |
8 | 8 |
9 | 9 |
10 class RegistryVerifier(verifier.Verifier): | 10 class RegistryVerifier(verifier.Verifier): |
11 """Verifies that the current registry matches the specified criteria.""" | 11 """Verifies that the current registry matches the specified criteria.""" |
12 | 12 |
| 13 def __init__(self): |
| 14 self._root_key_value = None |
| 15 self._is_key_forbidden = None |
| 16 self._sub_key = None |
| 17 |
13 def _RootKeyConstant(self, root_key): | 18 def _RootKeyConstant(self, root_key): |
14 """Converts a root registry key string into a _winreg.HKEY_* constant.""" | 19 """Converts a root registry key string into a _winreg.HKEY_* constant.""" |
15 root_key_mapping = { | 20 root_key_mapping = { |
16 'HKEY_CLASSES_ROOT': _winreg.HKEY_CLASSES_ROOT, | 21 'HKEY_CLASSES_ROOT': _winreg.HKEY_CLASSES_ROOT, |
17 'HKEY_CURRENT_USER': _winreg.HKEY_CURRENT_USER, | 22 'HKEY_CURRENT_USER': _winreg.HKEY_CURRENT_USER, |
18 'HKEY_LOCAL_MACHINE': _winreg.HKEY_LOCAL_MACHINE, | 23 'HKEY_LOCAL_MACHINE': _winreg.HKEY_LOCAL_MACHINE, |
19 'HKEY_USERS': _winreg.HKEY_USERS, | 24 'HKEY_USERS': _winreg.HKEY_USERS, |
20 } | 25 } |
21 if root_key not in root_key_mapping: | 26 if root_key not in root_key_mapping: |
22 raise KeyError("Unknown root registry key '%s'" % root_key) | 27 raise KeyError("Unknown root registry key '%s'" % root_key) |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 and its associated value is a dictionary with the following key | 61 and its associated value is a dictionary with the following key |
57 and values: | 62 and values: |
58 'type' (optional) a string indicating the type of the registry | 63 'type' (optional) a string indicating the type of the registry |
59 value. If not present, the corresponding value is expected | 64 value. If not present, the corresponding value is expected |
60 to be absent in the registry. | 65 to be absent in the registry. |
61 'data' the associated data of the registry value if 'type' is | 66 'data' the associated data of the registry value if 'type' is |
62 specified. If it is a string, it is expanded using Expand. | 67 specified. If it is a string, it is expanded using Expand. |
63 variable_expander: A VariableExpander object. | 68 variable_expander: A VariableExpander object. |
64 """ | 69 """ |
65 key = variable_expander.Expand(expectation_name) | 70 key = variable_expander.Expand(expectation_name) |
66 root_key, sub_key = key.split('\\', 1) | 71 root_key, self._sub_key = key.split('\\', 1) |
| 72 self._root_key_value = self._RootKeyConstant(root_key) |
| 73 self._is_key_forbidden = (expectation['exists'] == 'forbidden') |
67 try: | 74 try: |
68 # Query the Windows registry for the registry key. It will throw a | 75 # Query the Windows registry for the registry key. It will throw a |
69 # WindowsError if the key doesn't exist. | 76 # WindowsError if the key doesn't exist. |
70 key_handle = _winreg.OpenKey(self._RootKeyConstant(root_key), sub_key, 0, | 77 key_handle = _winreg.OpenKey(self._root_key_value, self._sub_key, 0, |
71 _winreg.KEY_QUERY_VALUE) | 78 _winreg.KEY_QUERY_VALUE) |
72 except WindowsError: | 79 except WindowsError: |
73 # Key doesn't exist. See that it matches the expectation. | 80 # Key doesn't exist. See that it matches the expectation. |
74 assert expectation['exists'] != 'required', ('Registry key %s is ' | 81 self._Assert(expectation['exists'] != 'required', ('Registry key %s is ' |
75 'missing' % key) | 82 'missing' % key)) |
76 # Values are not checked if the missing key's existence is optional. | 83 # Values are not checked if the missing key's existence is optional. |
77 return | 84 return |
78 # The key exists, see that it matches the expectation. | 85 # The key exists, see that it matches the expectation. |
79 assert expectation['exists'] != 'forbidden', ('Registry key %s exists' % | 86 self._Assert(expectation['exists'] != 'forbidden', |
80 key) | 87 ('Registry key %s exists' % key)) |
81 | 88 |
82 # Verify the expected values. | 89 # Verify the expected values. |
83 if 'values' not in expectation: | 90 if 'values' not in expectation: |
84 return | 91 return |
85 for value, value_expectation in expectation['values'].iteritems(): | 92 for value, value_expectation in expectation['values'].iteritems(): |
86 # Query the value. It will throw a WindowsError if the value doesn't | 93 # Query the value. It will throw a WindowsError if the value doesn't |
87 # exist. | 94 # exist. |
88 try: | 95 try: |
89 data, value_type = _winreg.QueryValueEx(key_handle, value) | 96 data, value_type = _winreg.QueryValueEx(key_handle, value) |
90 except WindowsError: | 97 except WindowsError: |
(...skipping 12 matching lines...) Expand all Loading... |
103 value, key, expected_value_type) | 110 value, key, expected_value_type) |
104 | 111 |
105 # Verify the associated data of the value. | 112 # Verify the associated data of the value. |
106 expected_data = value_expectation['data'] | 113 expected_data = value_expectation['data'] |
107 if isinstance(expected_data, basestring): | 114 if isinstance(expected_data, basestring): |
108 expected_data = variable_expander.Expand(expected_data) | 115 expected_data = variable_expander.Expand(expected_data) |
109 assert expected_data == data, \ | 116 assert expected_data == data, \ |
110 ("Value '%s' of registry key %s has unexpected data.\n" | 117 ("Value '%s' of registry key %s has unexpected data.\n" |
111 " Expected: %s\n" | 118 " Expected: %s\n" |
112 " Actual: %s" % (value, key, expected_data, data)) | 119 " Actual: %s" % (value, key, expected_data, data)) |
OLD | NEW |