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): |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 def _VerifyExpectation(self, expectation_name, expectation, | 42 def _VerifyExpectation(self, expectation_name, expectation, |
43 variable_expander): | 43 variable_expander): |
44 """Overridden from verifier.Verifier. | 44 """Overridden from verifier.Verifier. |
45 | 45 |
46 Verifies a registry key according to the |expectation|. | 46 Verifies a registry key according to the |expectation|. |
47 | 47 |
48 Args: | 48 Args: |
49 expectation_name: The registry key being verified. It is expanded using | 49 expectation_name: The registry key being verified. It is expanded using |
50 Expand. | 50 Expand. |
51 expectation: A dictionary with the following keys and values: | 51 expectation: A dictionary with the following keys and values: |
52 'exists' a boolean indicating whether the registry key should exist. | 52 'exists' a string indicating whether the registry key's existence is |
| 53 'required', 'optional', or 'forbidden'. Values are not checked if |
| 54 an 'optional' key is not present in the registry. |
53 'values' (optional) a dictionary where each key is a registry value | 55 'values' (optional) a dictionary where each key is a registry value |
54 and its associated value is a dictionary with the following key | 56 and its associated value is a dictionary with the following key |
55 and values: | 57 and values: |
56 'type' a string indicating the type of the registry value. | 58 'type' (optional) a string indicating the type of the registry |
57 'data' the associated data of the registry value. If it is a | 59 value. If not present, the corresponding value is expected |
58 string, it is expanded using Expand. | 60 to be absent in the registry. |
| 61 'data' the associated data of the registry value if 'type' is |
| 62 specified. If it is a string, it is expanded using Expand. |
59 variable_expander: A VariableExpander object. | 63 variable_expander: A VariableExpander object. |
60 """ | 64 """ |
61 key = variable_expander.Expand(expectation_name) | 65 key = variable_expander.Expand(expectation_name) |
62 root_key, sub_key = key.split('\\', 1) | 66 root_key, sub_key = key.split('\\', 1) |
63 try: | 67 try: |
64 # Query the Windows registry for the registry key. It will throw a | 68 # Query the Windows registry for the registry key. It will throw a |
65 # WindowsError if the key doesn't exist. | 69 # WindowsError if the key doesn't exist. |
66 key_handle = _winreg.OpenKey(self._RootKeyConstant(root_key), sub_key, 0, | 70 key_handle = _winreg.OpenKey(self._RootKeyConstant(root_key), sub_key, 0, |
67 _winreg.KEY_QUERY_VALUE) | 71 _winreg.KEY_QUERY_VALUE) |
68 except WindowsError: | 72 except WindowsError: |
69 # Key doesn't exist. See that it matches the expectation. | 73 # Key doesn't exist. See that it matches the expectation. |
70 assert not expectation['exists'], ('Registry key %s is missing' % | 74 assert expectation['exists'] is not 'required', ('Registry key %s is ' |
71 key) | 75 'missing' % key) |
| 76 # Values are not checked if the missing key's existence is optional. |
72 return | 77 return |
73 # The key exists, see that it matches the expectation. | 78 # The key exists, see that it matches the expectation. |
74 assert expectation['exists'], ('Registry key %s exists' % key) | 79 assert expectation['exists'] is not 'forbidden', ('Registry key %s exists' % |
| 80 key) |
75 | 81 |
76 # Verify the expected values. | 82 # Verify the expected values. |
77 if 'values' not in expectation: | 83 if 'values' not in expectation: |
78 return | 84 return |
79 for value, value_expectation in expectation['values'].iteritems(): | 85 for value, value_expectation in expectation['values'].iteritems(): |
80 # Query the value. It will throw a WindowsError if the value doesn't | 86 # Query the value. It will throw a WindowsError if the value doesn't |
81 # exist. | 87 # exist. |
82 try: | 88 try: |
83 data, value_type = _winreg.QueryValueEx(key_handle, value) | 89 data, value_type = _winreg.QueryValueEx(key_handle, value) |
84 except WindowsError: | 90 except WindowsError: |
85 raise KeyError("Value '%s' of registry key %s is missing" % ( | 91 # The value does not exist. See that this matches the expectation. |
86 value, key)) | 92 assert 'type' not in value_expectation, ('Value %s of registry key %s ' |
| 93 'is missing' % (value, key)) |
| 94 continue |
| 95 |
| 96 assert 'type' in value_expectation, ('Value %s of registry key %s exists ' |
| 97 'with value %s' % (value, key, data)) |
87 | 98 |
88 # Verify the type of the value. | 99 # Verify the type of the value. |
89 expected_value_type = value_expectation['type'] | 100 expected_value_type = value_expectation['type'] |
90 assert self._ValueTypeConstant(expected_value_type) == value_type, \ | 101 assert self._ValueTypeConstant(expected_value_type) == value_type, \ |
91 "Value '%s' of registry key %s has unexpected type '%s'" % ( | 102 "Value '%s' of registry key %s has unexpected type '%s'" % ( |
92 value, key, expected_value_type) | 103 value, key, expected_value_type) |
93 | 104 |
94 # Verify the associated data of the value. | 105 # Verify the associated data of the value. |
95 expected_data = value_expectation['data'] | 106 expected_data = value_expectation['data'] |
96 if isinstance(expected_data, basestring): | 107 if isinstance(expected_data, basestring): |
97 expected_data = variable_expander.Expand(expected_data) | 108 expected_data = variable_expander.Expand(expected_data) |
98 assert expected_data == data, \ | 109 assert expected_data == data, \ |
99 ("Value '%s' of registry key %s has unexpected data.\n" | 110 ("Value '%s' of registry key %s has unexpected data.\n" |
100 " Expected: %s\n" | 111 " Expected: %s\n" |
101 " Actual: %s" % (value, key, expected_data, data)) | 112 " Actual: %s" % (value, key, expected_data, data)) |
OLD | NEW |