Chromium Code Reviews| 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 settings | |
| 8 | |
| 9 | 7 |
| 10 def VerifyRegistryEntries(entries): | 8 def VerifyRegistryEntries(entries): |
| 11 """Verifies that the current registry matches the specified criteria.""" | 9 """Verifies that the current registry matches the specified criteria. |
| 12 for key, entry in entries.iteritems(): | 10 |
| 13 # TODO(sukolsak): Use unittest framework instead of prints. | 11 Args: |
| 14 if VerifyRegistryEntry(key, entry): | 12 entries: A dictionary whose keys are registry keys and values are |
| 15 print 'Passed' | 13 expectation dictionaries. |
| 16 else: | 14 """ |
| 17 print 'Failed' | 15 for key, expectation in entries.iteritems(): |
| 16 VerifyRegistryEntry(key, expectation) | |
| 18 | 17 |
| 19 | 18 |
| 20 def RootKeyConstant(key): | 19 def RootKeyConstant(key): |
| 20 """Converts a root registry key string into a _winreg.HKEY_* constant.""" | |
| 21 if key == 'HKEY_CLASSES_ROOT': | 21 if key == 'HKEY_CLASSES_ROOT': |
| 22 return _winreg.HKEY_CLASSES_ROOT | 22 return _winreg.HKEY_CLASSES_ROOT |
| 23 if key == 'HKEY_CURRENT_USER': | 23 if key == 'HKEY_CURRENT_USER': |
| 24 return _winreg.HKEY_CURRENT_USER | 24 return _winreg.HKEY_CURRENT_USER |
| 25 if key == 'HKEY_LOCAL_MACHINE': | 25 if key == 'HKEY_LOCAL_MACHINE': |
| 26 return _winreg.HKEY_LOCAL_MACHINE | 26 return _winreg.HKEY_LOCAL_MACHINE |
| 27 if key == 'HKEY_USERS': | 27 if key == 'HKEY_USERS': |
| 28 return _winreg.HKEY_USERS | 28 return _winreg.HKEY_USERS |
| 29 # TODO(sukolsak): Use unittest framework instead of exceptions. | 29 raise KeyError("Unknown root registry key '%s'" % key) |
| 30 raise Exception('Unknown registry key') | |
| 31 | 30 |
| 32 | 31 |
| 33 def VerifyRegistryEntry(key, entry): | 32 def VerifyRegistryEntry(key, expectation): |
| 34 """Verifies that a registry entry exists or doesn't exist and has | 33 """Verifies a registry key according to the |expectation|. |
| 35 the specified value. | 34 |
| 35 The |expectation| specifies whether or not the registry key should exist | |
| 36 (under 'exists') and optionally specifies an expected 'value' for the key. | |
| 36 | 37 |
| 37 Args: | 38 Args: |
| 38 key: Name of the registry key. | 39 key: Name of the registry key. |
| 39 entry: A dictionary with the following keys and values: | 40 expectation: A dictionary with the following keys and values: |
| 40 'expected' a boolean indicating whether the registry entry exists. | 41 'exists' a boolean indicating whether the registry entry should exist. |
| 41 'value' (optional) a string representing the value of the registry entry. | 42 'value' (optional) a string representing the expected value for |
| 42 | 43 the key. |
| 43 Returns: | |
| 44 A boolean indicating whether the registry entry matches the criteria. | |
| 45 """ | 44 """ |
| 46 expected = entry['expected'] | |
| 47 # TODO(sukolsak): Debug prints to be removed later. | |
| 48 print settings.PRINT_VERIFIER_PREFIX + key, | |
| 49 if expected: | |
| 50 print 'exists...', | |
| 51 else: | |
| 52 print "doesn't exist...", | |
| 53 root_key, sub_key = key.split('\\', 1) | 45 root_key, sub_key = key.split('\\', 1) |
| 54 try: | 46 try: |
| 55 reg_key = _winreg.OpenKey(RootKeyConstant(root_key), | 47 # Query the Windows registry for the registry key. It will throw a |
| 56 sub_key, 0, _winreg.KEY_READ) | 48 # WindowsError if the key doesn't exist. |
| 49 _ = _winreg.OpenKey(RootKeyConstant(root_key), sub_key, 0, _winreg.KEY_READ) | |
| 57 except WindowsError: | 50 except WindowsError: |
| 58 return not expected | 51 # Key doesn't exist. See that it matches the expectation. |
| 59 if not expected: | 52 assert not expectation['exists'], 'Registry entry %s is missing' % key |
| 60 return False | 53 return |
| 61 if 'value' in entry: | 54 # The key exists, see that it matches the expectation. |
| 62 # TODO(sukolsak): implement value | 55 assert expectation['exists'], 'Registry entry %s exists' % key |
| 63 pass | 56 # TODO(sukolsak): Verify the expected value. |
|
robertshield
2013/08/12 19:59:17
Don't forget to do this in the next CL :-)
| |
| 64 return True | |
| OLD | NEW |