| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
"""Tests for the Virtual Windows Registry key implementation.""" |
| 4 |
|
| 5 |
import unittest |
| 6 |
|
| 7 |
from dfwinreg import registry |
| 8 |
from dfwinreg import virtual |
| 9 |
|
| 10 |
from tests import registry as test_registry |
| 11 |
from tests import test_lib |
| 12 |
|
| 13 |
|
| 14 |
class TestWinRegistry(object): |
| 15 |
"""Windows Registry for testing.""" |
| 16 |
|
| 17 |
# pylint: disable=redundant-returns-doc,unused-argument |
| 18 |
|
| 19 |
def GetKeyByPath(self, key_path): |
| 20 |
"""Retrieves the key for a specific path. |
| 21 |
|
| 22 |
Args: |
| 23 |
key_path (str): Windows Registry key path. |
| 24 |
|
| 25 |
Returns: |
| 26 |
WinRegistryKey: Windows Registry key or None if not available. |
| 27 |
|
| 28 |
Raises: |
| 29 |
RuntimeError: if the root key is not supported. |
| 30 |
""" |
| 31 |
return None |
| 32 |
|
| 33 |
|
| 34 |
class ErrorWinRegistry(object): |
| 35 |
"""Windows Registry for testing that fails.""" |
| 36 |
|
| 37 |
# pylint: disable=redundant-returns-doc,unused-argument |
| 38 |
|
| 39 |
def GetKeyByPath(self, key_path): |
| 40 |
"""Retrieves the key for a specific path. |
| 41 |
|
| 42 |
Args: |
| 43 |
key_path (str): Windows Registry key path. |
| 44 |
|
| 45 |
Returns: |
| 46 |
WinRegistryKey: Windows Registry key or None if not available. |
| 47 |
|
| 48 |
Raises: |
| 49 |
RuntimeError: if the root key is not supported. |
| 50 |
""" |
| 51 |
raise RuntimeError('Not supported for testing') |
| 52 |
|
| 53 |
|
| 54 |
class VirtualWinRegistryKeyTest(test_lib.BaseTestCase): |
| 55 |
"""Tests for a virtual Windows Registry key.""" |
| 56 |
|
| 57 |
# pylint: disable=protected-access |
| 58 |
|
| 59 |
def _CreateTestKey(self): |
| 60 |
"""Creates a virtual Windows Registry key. |
| 61 |
|
| 62 |
Returns: |
| 63 |
VirtualWinRegistryKey: virtual Windows Registry key. |
| 64 |
""" |
| 65 |
registry_key = virtual.VirtualWinRegistryKey( |
| 66 |
'HKEY_LOCAL_MACHINE', relative_key_path='HKEY_LOCAL_MACHINE') |
| 67 |
|
| 68 |
sub_registry_key = virtual.VirtualWinRegistryKey('System') |
| 69 |
registry_key.AddSubkey(sub_registry_key.name, sub_registry_key) |
| 70 |
|
| 71 |
sub_registry_key = virtual.VirtualWinRegistryKey('Software') |
| 72 |
registry_key.AddSubkey(sub_registry_key.name, sub_registry_key) |
| 73 |
|
| 74 |
test_registry_key = virtual.VirtualWinRegistryKey('Classes') |
| 75 |
sub_registry_key.AddSubkey(test_registry_key.name, test_registry_key) |
| 76 |
|
| 77 |
return registry_key |
| 78 |
|
| 79 |
def _CreateTestRegistryWithMappedFile(self): |
| 80 |
"""Creates a Windows Registry with a mapped file. |
| 81 |
|
| 82 |
Returns: |
| 83 |
WinRegistry: Windows Registry. |
| 84 |
""" |
| 85 |
test_path = self._GetTestFilePath(['SYSTEM']) |
| 86 |
self._SkipIfPathNotExists(test_path) |
| 87 |
|
| 88 |
win_registry = registry.WinRegistry( |
| 89 |
registry_file_reader=test_registry.TestREGFWinRegistryFileReader()) |
| 90 |
win_registry.OpenAndMapFile(test_path) |
| 91 |
|
| 92 |
return win_registry |
| 93 |
|
| 94 |
def testLastWrittenTime(self): |
| 95 |
"""Tests the last_written_time property.""" |
| 96 |
win_registry = self._CreateTestRegistryWithMappedFile() |
| 97 |
|
| 98 |
mapped_key = win_registry.GetKeyByPath('HKEY_LOCAL_MACHINE\\System') |
| 99 |
self.assertIsNotNone(mapped_key) |
| 100 |
self.assertIsNotNone(mapped_key.last_written_time) |
| 101 |
|
| 102 |
def testNumberOfSubkeys(self): |
| 103 |
"""Tests the number_of_subkeys property.""" |
| 104 |
win_registry = self._CreateTestRegistryWithMappedFile() |
| 105 |
|
| 106 |
mapped_key = win_registry.GetKeyByPath('HKEY_LOCAL_MACHINE\\System') |
| 107 |
self.assertIsNotNone(mapped_key) |
| 108 |
self.assertEqual(mapped_key.number_of_subkeys, 9) |
| 109 |
|
| 110 |
def testNumberOfValues(self): |
| 111 |
"""Tests the number_of_values property.""" |
| 112 |
win_registry = self._CreateTestRegistryWithMappedFile() |
| 113 |
|
| 114 |
mapped_key = win_registry.GetKeyByPath( |
| 115 |
'HKEY_LOCAL_MACHINE\\System') |
| 116 |
self.assertIsNotNone(mapped_key) |
| 117 |
self.assertEqual(mapped_key.number_of_values, 0) |
| 118 |
|
| 119 |
# Test virtual key fallback. |
| 120 |
registry_key = self._CreateTestKey() |
| 121 |
self.assertIsNotNone(registry_key) |
| 122 |
|
| 123 |
self.assertEqual(registry_key.number_of_values, 0) |
| 124 |
|
| 125 |
def testOffset(self): |
| 126 |
"""Tests the offset property.""" |
| 127 |
win_registry = self._CreateTestRegistryWithMappedFile() |
| 128 |
|
| 129 |
mapped_key = win_registry.GetKeyByPath('HKEY_LOCAL_MACHINE\\System') |
| 130 |
self.assertIsNotNone(mapped_key) |
| 131 |
self.assertEqual(mapped_key.offset, 4132) |
| 132 |
|
| 133 |
def testPropertiesWithMappedRegistry(self): |
| 134 |
"""Tests the properties with a mapped registry.""" |
| 135 |
win_registry = self._CreateTestRegistryWithMappedFile() |
| 136 |
|
| 137 |
mapped_key = win_registry.GetKeyByPath('HKEY_LOCAL_MACHINE\\System') |
| 138 |
self.assertIsNotNone(mapped_key) |
| 139 |
|
| 140 |
self.assertIsNone(mapped_key.class_name) |
| 141 |
self.assertIsNotNone(mapped_key.last_written_time) |
| 142 |
timestamp = mapped_key.last_written_time.timestamp |
| 143 |
self.assertEqual(timestamp, 131243722185199451) |
| 144 |
|
| 145 |
self.assertEqual(mapped_key.number_of_subkeys, 9) |
| 146 |
self.assertEqual(mapped_key.number_of_values, 0) |
| 147 |
self.assertEqual(mapped_key.offset, 4132) |
| 148 |
|
| 149 |
def testGetKeyFromRegistry(self): |
| 150 |
"""Tests the _GetKeyFromRegistry function.""" |
| 151 |
registry_key = virtual.VirtualWinRegistryKey( |
| 152 |
'HKEY_LOCAL_MACHINE', relative_key_path='HKEY_LOCAL_MACHINE') |
| 153 |
registry_key._GetKeyFromRegistry() |
| 154 |
|
| 155 |
test_win_registry = TestWinRegistry() |
| 156 |
registry_key = virtual.VirtualWinRegistryKey( |
| 157 |
'HKEY_LOCAL_MACHINE', relative_key_path='HKEY_LOCAL_MACHINE', |
| 158 |
registry=test_win_registry) |
| 159 |
registry_key._GetKeyFromRegistry() |
| 160 |
|
| 161 |
test_win_registry = ErrorWinRegistry() |
| 162 |
registry_key = virtual.VirtualWinRegistryKey( |
| 163 |
'HKEY_LOCAL_MACHINE', relative_key_path='HKEY_LOCAL_MACHINE', |
| 164 |
registry=test_win_registry) |
| 165 |
registry_key._GetKeyFromRegistry() |
| 166 |
|
| 167 |
def testJoinKeyPath(self): |
| 168 |
"""Tests the _JoinKeyPath function.""" |
| 169 |
registry_key = virtual.VirtualWinRegistryKey( |
| 170 |
'HKEY_LOCAL_MACHINE', relative_key_path='HKEY_LOCAL_MACHINE') |
| 171 |
|
| 172 |
expected_path = 'HKEY_LOCAL_MACHINE\\Software' |
| 173 |
path = registry_key._JoinKeyPath(['HKEY_LOCAL_MACHINE', 'Software']) |
| 174 |
self.assertEqual(path, expected_path) |
| 175 |
|
| 176 |
def testAddSubkey(self): |
| 177 |
"""Tests the AddSubkey function.""" |
| 178 |
registry_key = virtual.VirtualWinRegistryKey( |
| 179 |
'HKEY_LOCAL_MACHINE', relative_key_path='HKEY_LOCAL_MACHINE') |
| 180 |
|
| 181 |
sub_registry_key = virtual.VirtualWinRegistryKey( |
| 182 |
'System', relative_key_path='HKEY_LOCAL_MACHINE') |
| 183 |
|
| 184 |
registry_key.AddSubkey(sub_registry_key.name, sub_registry_key) |
| 185 |
|
| 186 |
with self.assertRaises(KeyError): |
| 187 |
registry_key.AddSubkey(sub_registry_key.name, sub_registry_key) |
| 188 |
|
| 189 |
def testGetSubkeyByIndex(self): |
| 190 |
"""Tests the GetSubkeyByIndex function.""" |
| 191 |
registry_key = self._CreateTestKey() |
| 192 |
|
| 193 |
sub_registry_key = registry_key.GetSubkeyByIndex(0) |
| 194 |
self.assertIsNotNone(sub_registry_key) |
| 195 |
|
| 196 |
expected_key_path = 'HKEY_LOCAL_MACHINE\\System' |
| 197 |
self.assertEqual(sub_registry_key.path, expected_key_path) |
| 198 |
|
| 199 |
with self.assertRaises(IndexError): |
| 200 |
registry_key.GetSubkeyByIndex(-1) |
| 201 |
|
| 202 |
def testGetSubkeyByIndexWithMappedRegistry(self): |
| 203 |
"""Tests the GetSubkeyByIndex function with a mapped registry.""" |
| 204 |
win_registry = self._CreateTestRegistryWithMappedFile() |
| 205 |
|
| 206 |
mapped_key = win_registry.GetKeyByPath('HKEY_LOCAL_MACHINE\\System') |
| 207 |
self.assertIsNotNone(mapped_key) |
| 208 |
|
| 209 |
sub_registry_key = mapped_key.GetSubkeyByIndex(0) |
| 210 |
self.assertIsNotNone(sub_registry_key) |
| 211 |
|
| 212 |
def testGetSubkeyByName(self): |
| 213 |
"""Tests the GetSubkeyByName function.""" |
| 214 |
registry_key = self._CreateTestKey() |
| 215 |
|
| 216 |
sub_registry_key = registry_key.GetSubkeyByName('Software') |
| 217 |
self.assertIsNotNone(sub_registry_key) |
| 218 |
|
| 219 |
expected_key_path = 'HKEY_LOCAL_MACHINE\\Software' |
| 220 |
self.assertEqual(sub_registry_key.path, expected_key_path) |
| 221 |
|
| 222 |
sub_registry_key = registry_key.GetSubkeyByName('Bogus') |
| 223 |
self.assertIsNone(sub_registry_key) |
| 224 |
|
| 225 |
def testGetSubkeyByNameWithMappedRegistry(self): |
| 226 |
"""Tests the GetSubkeyByName function with a mapped registry.""" |
| 227 |
win_registry = self._CreateTestRegistryWithMappedFile() |
| 228 |
|
| 229 |
mapped_key = win_registry.GetKeyByPath('HKEY_LOCAL_MACHINE\\System') |
| 230 |
self.assertIsNotNone(mapped_key) |
| 231 |
|
| 232 |
sub_registry_key = mapped_key.GetSubkeyByName('ControlSet001') |
| 233 |
self.assertIsNotNone(sub_registry_key) |
| 234 |
|
| 235 |
def testGetSubkeyByPath(self): |
| 236 |
"""Tests the GetSubkeyByPath function.""" |
| 237 |
registry_key = self._CreateTestKey() |
| 238 |
|
| 239 |
key_path = 'Software\\Classes' |
| 240 |
sub_registry_key = registry_key.GetSubkeyByPath(key_path) |
| 241 |
self.assertIsNotNone(sub_registry_key) |
| 242 |
self.assertEqual(sub_registry_key.name, 'Classes') |
| 243 |
|
| 244 |
expected_key_path = ( |
| 245 |
'HKEY_LOCAL_MACHINE\\Software\\Classes') |
| 246 |
self.assertEqual(sub_registry_key.path, expected_key_path) |
| 247 |
|
| 248 |
key_path = 'Software\\Bogus' |
| 249 |
sub_registry_key = registry_key.GetSubkeyByPath(key_path) |
| 250 |
self.assertIsNone(sub_registry_key) |
| 251 |
|
| 252 |
def testGetSubkeyByPathWithMappedRegistry(self): |
| 253 |
"""Tests the GetSubkeyByPath function with a mapped registry.""" |
| 254 |
win_registry = self._CreateTestRegistryWithMappedFile() |
| 255 |
|
| 256 |
mapped_key = win_registry.GetKeyByPath('HKEY_LOCAL_MACHINE\\System') |
| 257 |
self.assertIsNotNone(mapped_key) |
| 258 |
|
| 259 |
sub_registry_key = mapped_key.GetSubkeyByPath('ControlSet001\\Control') |
| 260 |
self.assertIsNotNone(sub_registry_key) |
| 261 |
|
| 262 |
def testGetSubkeys(self): |
| 263 |
"""Tests the GetSubkeys function.""" |
| 264 |
registry_key = self._CreateTestKey() |
| 265 |
|
| 266 |
sub_registry_keys = list(registry_key.GetSubkeys()) |
| 267 |
self.assertEqual(len(sub_registry_keys), 2) |
| 268 |
|
| 269 |
def testGetSubkeysWithMappedRegistry(self): |
| 270 |
"""Tests the GetSubkeys function with a mapped registry.""" |
| 271 |
win_registry = self._CreateTestRegistryWithMappedFile() |
| 272 |
|
| 273 |
mapped_key = win_registry.GetKeyByPath('HKEY_LOCAL_MACHINE\\System') |
| 274 |
self.assertIsNotNone(mapped_key) |
| 275 |
|
| 276 |
sub_registry_keys = list(mapped_key.GetSubkeys()) |
| 277 |
self.assertEqual(len(sub_registry_keys), 9) |
| 278 |
|
| 279 |
def testGetValueByName(self): |
| 280 |
"""Tests the GetValueByName function.""" |
| 281 |
registry_key = self._CreateTestKey() |
| 282 |
|
| 283 |
registry_value = registry_key.GetValueByName('') |
| 284 |
self.assertIsNone(registry_value) |
| 285 |
|
| 286 |
def testGetValueByNameWithMappedRegistry(self): |
| 287 |
"""Tests the GetValueByName function with a mapped registry.""" |
| 288 |
win_registry = self._CreateTestRegistryWithMappedFile() |
| 289 |
|
| 290 |
mapped_key = win_registry.GetKeyByPath('HKEY_LOCAL_MACHINE\\System') |
| 291 |
self.assertIsNotNone(mapped_key) |
| 292 |
|
| 293 |
registry_value = mapped_key.GetValueByName('') |
| 294 |
self.assertIsNone(registry_value) |
| 295 |
|
| 296 |
def testGetValues(self): |
| 297 |
"""Tests the GetValues function.""" |
| 298 |
registry_key = self._CreateTestKey() |
| 299 |
|
| 300 |
values = list(registry_key.GetValues()) |
| 301 |
self.assertEqual(len(values), 0) |
| 302 |
|
| 303 |
def testGetValuesWithMappedRegistry(self): |
| 304 |
"""Tests the GetValues function with a mapped registry.""" |
| 305 |
win_registry = self._CreateTestRegistryWithMappedFile() |
| 306 |
|
| 307 |
mapped_key = win_registry.GetKeyByPath('HKEY_LOCAL_MACHINE\\System') |
| 308 |
self.assertIsNotNone(mapped_key) |
| 309 |
|
| 310 |
values = list(mapped_key.GetValues()) |
| 311 |
self.assertEqual(len(values), 0) |
| 312 |
|
| 313 |
|
| 314 |
if __name__ == '__main__': |
| 315 |
unittest.main() |