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'. | |
53 'values' (optional) a dictionary where each key is a registry value | 54 'values' (optional) a dictionary where each key is a registry value |
54 and its associated value is a dictionary with the following key | 55 and its associated value is a dictionary with the following key |
55 and values: | 56 and values: |
56 'type' a string indicating the type of the registry value. | 57 'type' (optional) a string indicating the type of the registry |
57 'data' the associated data of the registry value. If it is a | 58 value. If not present, the corresponding value is expected |
58 string, it is expanded using Expand. | 59 to be absent in the registry. |
60 'data' the associated data of the registry value if 'type' is | |
61 specified. If it is a string, it is expanded using Expand. | |
59 variable_expander: A VariableExpander object. | 62 variable_expander: A VariableExpander object. |
60 """ | 63 """ |
61 key = variable_expander.Expand(expectation_name) | 64 key = variable_expander.Expand(expectation_name) |
62 root_key, sub_key = key.split('\\', 1) | 65 root_key, sub_key = key.split('\\', 1) |
63 try: | 66 try: |
64 # Query the Windows registry for the registry key. It will throw a | 67 # Query the Windows registry for the registry key. It will throw a |
65 # WindowsError if the key doesn't exist. | 68 # WindowsError if the key doesn't exist. |
66 key_handle = _winreg.OpenKey(self._RootKeyConstant(root_key), sub_key, 0, | 69 key_handle = _winreg.OpenKey(self._RootKeyConstant(root_key), sub_key, 0, |
67 _winreg.KEY_QUERY_VALUE) | 70 _winreg.KEY_QUERY_VALUE) |
68 except WindowsError: | 71 except WindowsError: |
69 # Key doesn't exist. See that it matches the expectation. | 72 # Key doesn't exist. See that it matches the expectation. |
70 assert not expectation['exists'], ('Registry key %s is missing' % | 73 assert expectation['exists'] is not 'required', ('Registry key %s is ' |
71 key) | 74 'missing' % key) |
75 # Values are not checked if the missing key's existence is optional. | |
robertshield
2014/09/02 15:45:27
Can you reference this in the method comment?
grt (UTC plus 2)
2014/09/02 16:37:11
Done.
| |
72 return | 76 return |
73 # The key exists, see that it matches the expectation. | 77 # The key exists, see that it matches the expectation. |
74 assert expectation['exists'], ('Registry key %s exists' % key) | 78 assert expectation['exists'] is not 'forbidden', ('Registry key %s exists' % |
79 key) | |
75 | 80 |
76 # Verify the expected values. | 81 # Verify the expected values. |
77 if 'values' not in expectation: | 82 if 'values' not in expectation: |
78 return | 83 return |
79 for value, value_expectation in expectation['values'].iteritems(): | 84 for value, value_expectation in expectation['values'].iteritems(): |
80 # Query the value. It will throw a WindowsError if the value doesn't | 85 # Query the value. It will throw a WindowsError if the value doesn't |
81 # exist. | 86 # exist. |
82 try: | 87 try: |
83 data, value_type = _winreg.QueryValueEx(key_handle, value) | 88 data, value_type = _winreg.QueryValueEx(key_handle, value) |
84 except WindowsError: | 89 except WindowsError: |
85 raise KeyError("Value '%s' of registry key %s is missing" % ( | 90 # The value does not exist. See that this matches the expectation. |
86 value, key)) | 91 assert 'type' not in value_expectation, ('Value %s of registry key %s ' |
92 'is missing' % (value, key)) | |
93 continue | |
94 | |
95 assert 'type' in value_expectation, ('Value %s of registry key %s exists ' | |
96 'with value %s' % (value, key, data)) | |
87 | 97 |
88 # Verify the type of the value. | 98 # Verify the type of the value. |
89 expected_value_type = value_expectation['type'] | 99 expected_value_type = value_expectation['type'] |
90 assert self._ValueTypeConstant(expected_value_type) == value_type, \ | 100 assert self._ValueTypeConstant(expected_value_type) == value_type, \ |
91 "Value '%s' of registry key %s has unexpected type '%s'" % ( | 101 "Value '%s' of registry key %s has unexpected type '%s'" % ( |
92 value, key, expected_value_type) | 102 value, key, expected_value_type) |
93 | 103 |
94 # Verify the associated data of the value. | 104 # Verify the associated data of the value. |
95 expected_data = value_expectation['data'] | 105 expected_data = value_expectation['data'] |
96 if isinstance(expected_data, basestring): | 106 if isinstance(expected_data, basestring): |
97 expected_data = variable_expander.Expand(expected_data) | 107 expected_data = variable_expander.Expand(expected_data) |
98 assert expected_data == data, \ | 108 assert expected_data == data, \ |
99 ("Value '%s' of registry key %s has unexpected data.\n" | 109 ("Value '%s' of registry key %s has unexpected data.\n" |
100 " Expected: %s\n" | 110 " Expected: %s\n" |
101 " Actual: %s" % (value, key, expected_data, data)) | 111 " Actual: %s" % (value, key, expected_data, data)) |
OLD | NEW |