Update CrOS OOBE throbber to MD throbber; delete old asset
[chromium-blink-merge.git] / chrome / test / mini_installer / registry_verifier.py
blobbd790d574823082202e3bc5c5eda561b9b2e1abb
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
3 # found in the LICENSE file.
5 import _winreg
7 import verifier
10 class RegistryVerifier(verifier.Verifier):
11 """Verifies that the current registry matches the specified criteria."""
13 def _RootKeyConstant(self, root_key):
14 """Converts a root registry key string into a _winreg.HKEY_* constant."""
15 root_key_mapping = {
16 'HKEY_CLASSES_ROOT': _winreg.HKEY_CLASSES_ROOT,
17 'HKEY_CURRENT_USER': _winreg.HKEY_CURRENT_USER,
18 'HKEY_LOCAL_MACHINE': _winreg.HKEY_LOCAL_MACHINE,
19 'HKEY_USERS': _winreg.HKEY_USERS,
21 if root_key not in root_key_mapping:
22 raise KeyError("Unknown root registry key '%s'" % root_key)
23 return root_key_mapping[root_key]
25 def _ValueTypeConstant(self, value_type):
26 """Converts a registry value type string into a _winreg.REG_* constant."""
27 value_type_mapping = {
28 'BINARY': _winreg.REG_BINARY,
29 'DWORD': _winreg.REG_DWORD,
30 'DWORD_LITTLE_ENDIAN': _winreg.REG_DWORD_LITTLE_ENDIAN,
31 'DWORD_BIG_ENDIAN': _winreg.REG_DWORD_BIG_ENDIAN,
32 'EXPAND_SZ': _winreg.REG_EXPAND_SZ,
33 'LINK': _winreg.REG_LINK,
34 'MULTI_SZ': _winreg.REG_MULTI_SZ,
35 'NONE': _winreg.REG_NONE,
36 'SZ': _winreg.REG_SZ,
38 if value_type not in value_type_mapping:
39 raise KeyError("Unknown registry value type '%s'" % value_type)
40 return value_type_mapping[value_type]
42 def _VerifyExpectation(self, expectation_name, expectation,
43 variable_expander):
44 """Overridden from verifier.Verifier.
46 Verifies a registry key according to the |expectation|.
48 Args:
49 expectation_name: The registry key being verified. It is expanded using
50 Expand.
51 expectation: A dictionary with the following keys and values:
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.
55 'values' (optional) a dictionary where each key is a registry value
56 and its associated value is a dictionary with the following key
57 and values:
58 'type' (optional) a string indicating the type of the registry
59 value. If not present, the corresponding value is expected
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.
63 variable_expander: A VariableExpander object.
64 """
65 key = variable_expander.Expand(expectation_name)
66 root_key, sub_key = key.split('\\', 1)
67 try:
68 # Query the Windows registry for the registry key. It will throw a
69 # WindowsError if the key doesn't exist.
70 key_handle = _winreg.OpenKey(self._RootKeyConstant(root_key), sub_key, 0,
71 _winreg.KEY_QUERY_VALUE)
72 except WindowsError:
73 # Key doesn't exist. See that it matches the expectation.
74 assert expectation['exists'] != 'required', ('Registry key %s is '
75 'missing' % key)
76 # Values are not checked if the missing key's existence is optional.
77 return
78 # The key exists, see that it matches the expectation.
79 assert expectation['exists'] != 'forbidden', ('Registry key %s exists' %
80 key)
82 # Verify the expected values.
83 if 'values' not in expectation:
84 return
85 for value, value_expectation in expectation['values'].iteritems():
86 # Query the value. It will throw a WindowsError if the value doesn't
87 # exist.
88 try:
89 data, value_type = _winreg.QueryValueEx(key_handle, value)
90 except WindowsError:
91 # The value does not exist. See that this matches the expectation.
92 assert 'type' not in value_expectation, ('Value %s of registry key %s '
93 'is missing' % (value, key))
94 continue
96 assert 'type' in value_expectation, ('Value %s of registry key %s exists '
97 'with value %s' % (value, key, data))
99 # Verify the type of the value.
100 expected_value_type = value_expectation['type']
101 assert self._ValueTypeConstant(expected_value_type) == value_type, \
102 "Value '%s' of registry key %s has unexpected type '%s'" % (
103 value, key, expected_value_type)
105 # Verify the associated data of the value.
106 expected_data = value_expectation['data']
107 if isinstance(expected_data, basestring):
108 expected_data = variable_expander.Expand(expected_data)
109 assert expected_data == data, \
110 ("Value '%s' of registry key %s has unexpected data.\n"
111 " Expected: %s\n"
112 " Actual: %s" % (value, key, expected_data, data))