| 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 | 7 |
| 8 def VerifyRegistryEntries(entries, testcase): |
| 9 """Verifies that the current registry matches the specified criteria.""" |
| 10 for key, entry in entries.iteritems(): |
| 11 VerifyRegistryEntry(key, entry, testcase) |
| 8 | 12 |
| 9 | 13 |
| 10 def VerifyRegistryEntries(entries): | 14 def RootKeyConstant(key, testcase): |
| 11 """Verifies that the current registry matches the specified criteria.""" | |
| 12 for key, entry in entries.iteritems(): | |
| 13 # TODO(sukolsak): Use unittest framework instead of prints. | |
| 14 if VerifyRegistryEntry(key, entry): | |
| 15 print 'Passed' | |
| 16 else: | |
| 17 print 'Failed' | |
| 18 | |
| 19 | |
| 20 def RootKeyConstant(key): | |
| 21 if key == 'HKEY_CLASSES_ROOT': | 15 if key == 'HKEY_CLASSES_ROOT': |
| 22 return _winreg.HKEY_CLASSES_ROOT | 16 return _winreg.HKEY_CLASSES_ROOT |
| 23 if key == 'HKEY_CURRENT_USER': | 17 if key == 'HKEY_CURRENT_USER': |
| 24 return _winreg.HKEY_CURRENT_USER | 18 return _winreg.HKEY_CURRENT_USER |
| 25 if key == 'HKEY_LOCAL_MACHINE': | 19 if key == 'HKEY_LOCAL_MACHINE': |
| 26 return _winreg.HKEY_LOCAL_MACHINE | 20 return _winreg.HKEY_LOCAL_MACHINE |
| 27 if key == 'HKEY_USERS': | 21 if key == 'HKEY_USERS': |
| 28 return _winreg.HKEY_USERS | 22 return _winreg.HKEY_USERS |
| 29 # TODO(sukolsak): Use unittest framework instead of exceptions. | 23 testcase.fail('Unknown registry key') |
| 30 raise Exception('Unknown registry key') | |
| 31 | 24 |
| 32 | 25 |
| 33 def VerifyRegistryEntry(key, entry): | 26 def VerifyRegistryEntry(key, entry, testcase): |
| 34 """Verifies that a registry entry exists or doesn't exist and has | 27 """Verifies that a registry entry exists or doesn't exist and has |
| 35 the specified value. | 28 the specified value. |
| 36 | 29 |
| 37 Args: | 30 Args: |
| 38 key: Name of the registry key. | 31 key: Name of the registry key. |
| 39 entry: A dictionary with the following keys and values: | 32 entry: A dictionary with the following keys and values: |
| 40 'expected' a boolean indicating whether the registry entry exists. | 33 'expected' a boolean indicating whether the registry entry exists. |
| 41 'value' (optional) a string representing the value of the registry entry. | 34 'value' (optional) a string representing the value of the registry entry. |
| 35 testcase: A TestCase instance. |
| 42 | 36 |
| 43 Returns: | 37 Returns: |
| 44 A boolean indicating whether the registry entry matches the criteria. | 38 A boolean indicating whether the registry entry matches the criteria. |
| 45 """ | 39 """ |
| 46 expected = entry['expected'] | 40 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) | 41 root_key, sub_key = key.split('\\', 1) |
| 54 try: | 42 try: |
| 55 reg_key = _winreg.OpenKey(RootKeyConstant(root_key), | 43 reg_key = _winreg.OpenKey(RootKeyConstant(root_key, testcase), sub_key, 0, |
| 56 sub_key, 0, _winreg.KEY_READ) | 44 _winreg.KEY_READ) |
| 57 except WindowsError: | 45 except WindowsError: |
| 58 return not expected | 46 testcase.assertFalse(expected, 'Registry entry %s should not exist' % key) |
| 59 if not expected: | 47 return |
| 60 return False | 48 testcase.assertTrue(expected, 'Registry entry %s should exist' % key) |
| 61 if 'value' in entry: | 49 if 'value' in entry: |
| 62 # TODO(sukolsak): implement value | 50 # TODO(sukolsak): implement value |
| 63 pass | 51 pass |
| 64 return True | |
| OLD | NEW |