| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
"""This file contains the tests for the Windows Registry library.""" |
| 4 |
|
| 5 |
import os |
| 6 |
import unittest |
| 7 |
|
| 8 |
from dfwinreg import definitions |
| 9 |
from dfwinreg import fake |
| 10 |
from dfwinreg import interface |
| 11 |
from dfwinreg import regf |
| 12 |
from dfwinreg import registry |
| 13 |
|
| 14 |
from tests import test_lib |
| 15 |
|
| 16 |
|
| 17 |
class TestWinRegistry(registry.WinRegistry): |
| 18 |
"""Windows Registry for testing.""" |
| 19 |
|
| 20 |
def GetKeyByPath(self, key_path): |
| 21 |
"""Retrieves the key for a specific path. |
| 22 |
|
| 23 |
Args: |
| 24 |
key_path (str): Windows Registry key path. |
| 25 |
|
| 26 |
Returns: |
| 27 |
WinRegistryKey: Windows Registry key or None if not available. |
| 28 |
|
| 29 |
Raises: |
| 30 |
RuntimeError: if the root key is not supported. |
| 31 |
""" |
| 32 |
if key_path == 'HKEY_LOCAL_MACHINE\\System\\Select': |
| 33 |
registry_key = fake.FakeWinRegistryKey( |
| 34 |
'Select', key_path_prefix='HKEY_LOCAL_MACHINE\\System', |
| 35 |
last_written_time=0, relative_key_path='') |
| 36 |
|
| 37 |
registry_value = fake.FakeWinRegistryValue( |
| 38 |
'Current', data=b'DATA', data_type=definitions.REG_BINARY) |
| 39 |
registry_key.AddValue(registry_value) |
| 40 |
|
| 41 |
registry_value = fake.FakeWinRegistryValue( |
| 42 |
'Default', data=b'\xff\xff\xff\xff', data_type=definitions.REG_DWORD) |
| 43 |
registry_key.AddValue(registry_value) |
| 44 |
|
| 45 |
registry_value = fake.FakeWinRegistryValue( |
| 46 |
'LastKnownGood', data=b'\x01\x00\x00\x00', |
| 47 |
data_type=definitions.REG_DWORD) |
| 48 |
registry_key.AddValue(registry_value) |
| 49 |
|
| 50 |
return registry_key |
| 51 |
|
| 52 |
return super(TestWinRegistry, self).GetKeyByPath(key_path) |
| 53 |
|
| 54 |
|
| 55 |
class TestWinRegistryKeyPathPrefixMismatch(registry.WinRegistry): |
| 56 |
"""Windows Registry for testing key path prefix mismatch.""" |
| 57 |
|
| 58 |
def _GetFileByPath(self, key_path_upper): |
| 59 |
"""Retrieves a Windows Registry file for a specific path. |
| 60 |
|
| 61 |
Args: |
| 62 |
key_path_upper (str): Windows Registry key path, in upper case with |
| 63 |
a resolved root key alias. |
| 64 |
|
| 65 |
Returns: |
| 66 |
tuple: consists: |
| 67 |
|
| 68 |
str: upper case key path prefix |
| 69 |
WinRegistryFile: corresponding Windows Registry file or None if not |
| 70 |
available. |
| 71 |
""" |
| 72 |
_, registry_file = super( |
| 73 |
TestWinRegistryKeyPathPrefixMismatch, self)._GetFileByPath( |
| 74 |
key_path_upper) |
| 75 |
return 'BOGUS', registry_file |
| 76 |
|
| 77 |
|
| 78 |
class TestREGFWinRegistryFileReader(interface.WinRegistryFileReader): |
| 79 |
"""Windows Registry file reader that reads a single test file.""" |
| 80 |
|
| 81 |
def __init__(self): |
| 82 |
"""Initializes a file Windows Registry file reader.""" |
| 83 |
super(TestREGFWinRegistryFileReader, self).__init__() |
| 84 |
self._file_objects = [] |
| 85 |
|
| 86 |
def __del__(self): |
| 87 |
"""Cleans up the Windows Registry file reader.""" |
| 88 |
while self._file_objects: |
| 89 |
file_object = self._file_objects.pop(0) |
| 90 |
file_object.close() |
| 91 |
|
| 92 |
def Open(self, path, ascii_codepage='cp1252'): |
| 93 |
"""Opens the Windows Registry file specified by the path. |
| 94 |
|
| 95 |
Args: |
| 96 |
path (str): path of the Windows Registry file. |
| 97 |
ascii_codepage (Optional[str]): ASCII string codepage. |
| 98 |
|
| 99 |
Returns: |
| 100 |
WinRegistryFile: Windows Registry file or None. |
| 101 |
""" |
| 102 |
if not os.path.exists(path): |
| 103 |
return None |
| 104 |
|
| 105 |
registry_file = regf.REGFWinRegistryFile(ascii_codepage=ascii_codepage) |
| 106 |
|
| 107 |
file_object = open(path, 'rb') # pylint: disable=consider-using-with |
| 108 |
|
| 109 |
try: |
| 110 |
# If open is successful Registry file will manage the file object. |
| 111 |
registry_file.Open(file_object) |
| 112 |
except IOError: |
| 113 |
file_object.close() |
| 114 |
file_object = None |
| 115 |
registry_file = None |
| 116 |
|
| 117 |
self._file_objects.append(file_object) |
| 118 |
|
| 119 |
return registry_file |
| 120 |
|
| 121 |
|
| 122 |
class TestREGFWinRegistryFileReaderMapped(TestREGFWinRegistryFileReader): |
| 123 |
"""Windows Registry file reader that maps Windows paths to test files.""" |
| 124 |
|
| 125 |
_MAPPED_PATHS = { |
| 126 |
'%SystemRoot%\\System32\\config\\SAM': ( |
| 127 |
os.path.join(test_lib.TEST_DATA_PATH, 'SAM')), |
| 128 |
'%SystemRoot%\\System32\\config\\SECURITY': ( |
| 129 |
os.path.join(test_lib.TEST_DATA_PATH, 'SECURITY')), |
| 130 |
'%SystemRoot%\\System32\\config\\SOFTWARE': ( |
| 131 |
os.path.join(test_lib.TEST_DATA_PATH, 'SOFTWARE')), |
| 132 |
'%SystemRoot%\\System32\\config\\SYSTEM': ( |
| 133 |
os.path.join(test_lib.TEST_DATA_PATH, 'SYSTEM')), |
| 134 |
'%UserProfile%\\AppData\\Local\\Microsoft\\Windows\\UsrClass.dat': ( |
| 135 |
os.path.join(test_lib.TEST_DATA_PATH, 'UsrClass.dat')), |
| 136 |
'%UserProfile%\\NTUSER.DAT': ( |
| 137 |
os.path.join(test_lib.TEST_DATA_PATH, 'NTUSER.DAT'))} |
| 138 |
|
| 139 |
def Open(self, path, ascii_codepage='cp1252'): |
| 140 |
"""Opens the Windows Registry file specified by the path. |
| 141 |
|
| 142 |
Args: |
| 143 |
path (str): path of the Windows Registry file. |
| 144 |
ascii_codepage (Optional[str]): ASCII string codepage. |
| 145 |
|
| 146 |
Returns: |
| 147 |
WinRegistryFile: Windows Registry file or None. |
| 148 |
|
| 149 |
Raises: |
| 150 |
SkipTest: if the Windows Registry file does not exist and the test |
| 151 |
should be skipped. |
| 152 |
""" |
| 153 |
test_file_path = self._MAPPED_PATHS.get(path, None) |
| 154 |
if test_file_path is None: |
| 155 |
return None |
| 156 |
|
| 157 |
return super(TestREGFWinRegistryFileReaderMapped, self).Open( |
| 158 |
test_file_path, ascii_codepage=ascii_codepage) |
| 159 |
|
| 160 |
|
| 161 |
class RegistryTest(test_lib.BaseTestCase): |
| 162 |
"""Tests for the Windows Registry library.""" |
| 163 |
|
| 164 |
# pylint: disable=protected-access |
| 165 |
|
| 166 |
def testGetCachedFileByPath(self): |
| 167 |
"""Tests the _GetCachedFileByPath function.""" |
| 168 |
test_path = self._GetTestFilePath(['SYSTEM']) |
| 169 |
self._SkipIfPathNotExists(test_path) |
| 170 |
|
| 171 |
win_registry = registry.WinRegistry() |
| 172 |
|
| 173 |
# Note that _GetCachedFileByPath expects the key path to be in upper case. |
| 174 |
key_path = 'HKEY_LOCAL_MACHINE\\SYSTEM' |
| 175 |
|
| 176 |
key_path_prefix, registry_file = win_registry._GetCachedFileByPath(key_path) |
| 177 |
self.assertIsNone(key_path_prefix) |
| 178 |
self.assertIsNone(registry_file) |
| 179 |
|
| 180 |
win_registry = registry.WinRegistry( |
| 181 |
registry_file_reader=TestREGFWinRegistryFileReader()) |
| 182 |
win_registry.OpenAndMapFile(test_path) |
| 183 |
|
| 184 |
key_path_prefix, registry_file = win_registry._GetCachedFileByPath(key_path) |
| 185 |
self.assertEqual(key_path_prefix, key_path) |
| 186 |
self.assertIsNotNone(registry_file) |
| 187 |
|
| 188 |
# TODO: add tests for _GetCachedUserFileByPath |
| 189 |
|
| 190 |
def testGetCandidateFileMappingsByPath(self): |
| 191 |
"""Tests the _GetCandidateFileMappingsByPath function.""" |
| 192 |
win_registry = registry.WinRegistry() |
| 193 |
|
| 194 |
# Note that _GetCandidateFileMappingsByPath expects the key path to be in |
| 195 |
# upper case. |
| 196 |
key_path = 'HKEY_USERS' |
| 197 |
mappings = list(win_registry._GetCandidateFileMappingsByPath(key_path)) |
| 198 |
self.assertEqual(len(mappings), 1) |
| 199 |
|
| 200 |
key_path = 'HKEY_BOGUS\\SYSTEM' |
| 201 |
mappings = list(win_registry._GetCandidateFileMappingsByPath(key_path)) |
| 202 |
self.assertEqual(len(mappings), 0) |
| 203 |
|
| 204 |
key_path = 'HKEY_CURRENT_USER\\SOFTWARE\\CLASSES' |
| 205 |
mappings = list(win_registry._GetCandidateFileMappingsByPath(key_path)) |
| 206 |
self.assertEqual(len(mappings), 3) |
| 207 |
|
| 208 |
def testGetKeyByPathFromFile(self): |
| 209 |
"""Tests the _GetKeyByPathFromFile function.""" |
| 210 |
test_path = self._GetTestFilePath(['NTUSER.DAT']) |
| 211 |
self._SkipIfPathNotExists(test_path) |
| 212 |
|
| 213 |
win_registry = registry.WinRegistry( |
| 214 |
registry_file_reader=TestREGFWinRegistryFileReader()) |
| 215 |
win_registry.OpenAndMapFile(test_path) |
| 216 |
|
| 217 |
# Test an existing key. |
| 218 |
registry_key = win_registry._GetKeyByPathFromFile( |
| 219 |
'HKEY_CURRENT_USER\\Software\\Microsoft') |
| 220 |
self.assertIsNotNone(registry_key) |
| 221 |
|
| 222 |
# Test a non-existing key. |
| 223 |
registry_key = win_registry._GetKeyByPathFromFile( |
| 224 |
'HKEY_CURRENT_USER\\Software\\Bogus') |
| 225 |
self.assertIsNone(registry_key) |
| 226 |
|
| 227 |
# Test a non-existing key outside the Registry file. |
| 228 |
registry_key = win_registry._GetKeyByPathFromFile( |
| 229 |
'HKEY_LOCAL_MACHINE\\System\\ControlSet001') |
| 230 |
self.assertIsNone(registry_key) |
| 231 |
|
| 232 |
def testGetUsersVirtualKey(self): |
| 233 |
"""Tests the _GetUsersVirtualKey function.""" |
| 234 |
ntuser_test_path = self._GetTestFilePath(['NTUSER.DAT']) |
| 235 |
self._SkipIfPathNotExists(ntuser_test_path) |
| 236 |
|
| 237 |
software_test_path = self._GetTestFilePath(['SOFTWARE']) |
| 238 |
self._SkipIfPathNotExists(software_test_path) |
| 239 |
|
| 240 |
win_registry = registry.WinRegistry() |
| 241 |
|
| 242 |
registry_key = win_registry._GetUsersVirtualKey('\\S-1-5-18') |
| 243 |
self.assertIsNone(registry_key) |
| 244 |
|
| 245 |
win_registry = registry.WinRegistry( |
| 246 |
registry_file_reader=TestREGFWinRegistryFileReader()) |
| 247 |
|
| 248 |
registry_file = win_registry._OpenFile(ntuser_test_path) |
| 249 |
profile_path = '%SystemRoot%\\System32\\config\\systemprofile\\NTUSER.DAT' |
| 250 |
win_registry.MapUserFile(profile_path, registry_file) |
| 251 |
|
| 252 |
win_registry.OpenAndMapFile(software_test_path) |
| 253 |
|
| 254 |
registry_key = win_registry._GetUsersVirtualKey('\\S-1-5-18') |
| 255 |
self.assertIsNotNone(registry_key) |
| 256 |
|
| 257 |
expected_key_path = 'HKEY_USERS\\S-1-5-18' |
| 258 |
self.assertEqual(registry_key.path, expected_key_path) |
| 259 |
|
| 260 |
registry_key = win_registry._GetUsersVirtualKey( |
| 261 |
'\\S-1-5-18\\Software\\Microsoft\\Windows\\CurrentVersion') |
| 262 |
self.assertIsNotNone(registry_key) |
| 263 |
|
| 264 |
expected_key_path = ( |
| 265 |
'HKEY_USERS\\S-1-5-18\\Software\\Microsoft\\Windows\\CurrentVersion') |
| 266 |
self.assertEqual(registry_key.path, expected_key_path) |
| 267 |
|
| 268 |
registry_key = win_registry._GetUsersVirtualKey('\\.DEFAULT') |
| 269 |
self.assertIsNotNone(registry_key) |
| 270 |
|
| 271 |
expected_key_path = 'HKEY_USERS\\.DEFAULT' |
| 272 |
self.assertEqual(registry_key.path, expected_key_path) |
| 273 |
|
| 274 |
with self.assertRaises(RuntimeError): |
| 275 |
win_registry._GetUsersVirtualKey('.DEFAULT') |
| 276 |
|
| 277 |
def testGetFileByPath(self): |
| 278 |
"""Tests the _GetFileByPath function.""" |
| 279 |
test_path = self._GetTestFilePath(['SYSTEM']) |
| 280 |
self._SkipIfPathNotExists(test_path) |
| 281 |
|
| 282 |
# Test mapped file with key path prefix. |
| 283 |
win_registry = registry.WinRegistry( |
| 284 |
registry_file_reader=TestREGFWinRegistryFileReader()) |
| 285 |
win_registry.OpenAndMapFile(test_path) |
| 286 |
|
| 287 |
key_path = 'HKEY_LOCAL_MACHINE\\SYSTEM' |
| 288 |
|
| 289 |
key_path_prefix, registry_file = win_registry._GetFileByPath(key_path) |
| 290 |
self.assertEqual(key_path_prefix, key_path) |
| 291 |
self.assertIsNotNone(registry_file) |
| 292 |
|
| 293 |
# Test mapped file without key path prefix. |
| 294 |
win_registry = registry.WinRegistry( |
| 295 |
registry_file_reader=TestREGFWinRegistryFileReader()) |
| 296 |
|
| 297 |
registry_file = win_registry._OpenFile(test_path) |
| 298 |
win_registry.MapFile('', registry_file) |
| 299 |
|
| 300 |
key_path = 'HKEY_LOCAL_MACHINE\\SYSTEM' |
| 301 |
|
| 302 |
key_path_prefix, registry_file = win_registry._GetFileByPath(key_path) |
| 303 |
self.assertEqual(key_path_prefix, key_path) |
| 304 |
self.assertIsNone(registry_file) |
| 305 |
|
| 306 |
# Test without mapped file. |
| 307 |
win_registry = registry.WinRegistry() |
| 308 |
|
| 309 |
key_path = 'HKEY_LOCAL_MACHINE\\SYSTEM' |
| 310 |
|
| 311 |
key_path_prefix, registry_file = win_registry._GetFileByPath(key_path) |
| 312 |
self.assertEqual(key_path_prefix, key_path) |
| 313 |
self.assertIsNone(registry_file) |
| 314 |
|
| 315 |
# Tests open file based on predefined mapping. |
| 316 |
win_registry = registry.WinRegistry( |
| 317 |
registry_file_reader=TestREGFWinRegistryFileReaderMapped()) |
| 318 |
|
| 319 |
key_path = 'HKEY_LOCAL_MACHINE\\SYSTEM' |
| 320 |
|
| 321 |
key_path_prefix, registry_file = win_registry._GetFileByPath(key_path) |
| 322 |
self.assertEqual(key_path_prefix, key_path) |
| 323 |
self.assertIsNotNone(registry_file) |
| 324 |
|
| 325 |
key_path = 'HKEY_CURRENT_USER' |
| 326 |
|
| 327 |
key_path_prefix, registry_file = win_registry._GetFileByPath(key_path) |
| 328 |
self.assertEqual(key_path_prefix, key_path) |
| 329 |
self.assertIsNotNone(registry_file) |
| 330 |
|
| 331 |
key_path = 'HKEY_CURRENT_USER\\SOFTWARE\\CLASSES' |
| 332 |
|
| 333 |
key_path_prefix, registry_file = win_registry._GetFileByPath(key_path) |
| 334 |
self.assertEqual(key_path_prefix, key_path) |
| 335 |
self.assertIsNotNone(registry_file) |
| 336 |
|
| 337 |
# Tests open file based on predefined mapping in reversed order. |
| 338 |
win_registry = registry.WinRegistry( |
| 339 |
registry_file_reader=TestREGFWinRegistryFileReaderMapped()) |
| 340 |
|
| 341 |
key_path = 'HKEY_CURRENT_USER\\SOFTWARE\\CLASSES' |
| 342 |
|
| 343 |
key_path_prefix, registry_file = win_registry._GetFileByPath(key_path) |
| 344 |
self.assertEqual(key_path_prefix, key_path) |
| 345 |
self.assertIsNotNone(registry_file) |
| 346 |
|
| 347 |
key_path = 'HKEY_CURRENT_USER' |
| 348 |
|
| 349 |
key_path_prefix, registry_file = win_registry._GetFileByPath(key_path) |
| 350 |
self.assertEqual(key_path_prefix, key_path) |
| 351 |
self.assertIsNotNone(registry_file) |
| 352 |
|
| 353 |
def testGetKeyByPathOnNTUserDat(self): |
| 354 |
"""Tests the GetKeyByPath function on a NTUSER.DAT file.""" |
| 355 |
test_path = self._GetTestFilePath(['NTUSER.DAT']) |
| 356 |
self._SkipIfPathNotExists(test_path) |
| 357 |
|
| 358 |
win_registry = registry.WinRegistry( |
| 359 |
registry_file_reader=TestREGFWinRegistryFileReader()) |
| 360 |
win_registry.OpenAndMapFile(test_path) |
| 361 |
|
| 362 |
# Test an existing key. |
| 363 |
registry_key = win_registry.GetKeyByPath( |
| 364 |
'HKEY_CURRENT_USER\\Software\\Microsoft') |
| 365 |
self.assertIsNotNone(registry_key) |
| 366 |
|
| 367 |
# Test a non-existing key. |
| 368 |
registry_key = win_registry.GetKeyByPath( |
| 369 |
'HKEY_CURRENT_USER\\Software\\Bogus') |
| 370 |
self.assertIsNone(registry_key) |
| 371 |
|
| 372 |
# Test a non-existing root. |
| 373 |
with self.assertRaises(RuntimeError): |
| 374 |
win_registry.GetKeyByPath('HKEY_BOGUS\\Software\\Bogus') |
| 375 |
|
| 376 |
# Test a non-existing key outside the Registry file. |
| 377 |
registry_key = win_registry.GetKeyByPath( |
| 378 |
'HKEY_LOCAL_MACHINE\\System\\ControlSet001') |
| 379 |
self.assertIsNone(registry_key) |
| 380 |
|
| 381 |
def testGetKeyByPathOnSystem(self): |
| 382 |
"""Tests the GetKeyByPath function on a SYSTEM file.""" |
| 383 |
win_registry = registry.WinRegistry( |
| 384 |
registry_file_reader=TestREGFWinRegistryFileReaderMapped()) |
| 385 |
|
| 386 |
# Test an existing key. |
| 387 |
registry_key = win_registry.GetKeyByPath( |
| 388 |
'HKEY_LOCAL_MACHINE\\System\\ControlSet001') |
| 389 |
self.assertIsNotNone(registry_key) |
| 390 |
|
| 391 |
self.assertEqual( |
| 392 |
registry_key.path, 'HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001') |
| 393 |
|
| 394 |
registry_key = win_registry.GetKeyByPath( |
| 395 |
'HKEY_LOCAL_MACHINE\\System\\ControlSet001\\Enum') |
| 396 |
self.assertIsNotNone(registry_key) |
| 397 |
|
| 398 |
self.assertEqual( |
| 399 |
registry_key.path, 'HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Enum') |
| 400 |
|
| 401 |
# Test a virtual key. |
| 402 |
registry_key = win_registry.GetKeyByPath( |
| 403 |
'HKEY_LOCAL_MACHINE\\System\\CurrentControlSet') |
| 404 |
self.assertIsNotNone(registry_key) |
| 405 |
|
| 406 |
self.assertEqual( |
| 407 |
registry_key.path, 'HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet') |
| 408 |
|
| 409 |
# Test a subkey of a virtual key. |
| 410 |
registry_key = win_registry.GetKeyByPath( |
| 411 |
'HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Enum') |
| 412 |
self.assertIsNotNone(registry_key) |
| 413 |
|
| 414 |
self.assertEqual( |
| 415 |
registry_key.path, |
| 416 |
'HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum') |
| 417 |
|
| 418 |
# Test a non-existing key. |
| 419 |
registry_key = win_registry.GetKeyByPath( |
| 420 |
'HKEY_LOCAL_MACHINE\\System\\Bogus') |
| 421 |
self.assertIsNone(registry_key) |
| 422 |
|
| 423 |
# Tests Current value is not an integer. |
| 424 |
win_registry = TestWinRegistryKeyPathPrefixMismatch( |
| 425 |
registry_file_reader=TestREGFWinRegistryFileReaderMapped()) |
| 426 |
|
| 427 |
with self.assertRaises(RuntimeError): |
| 428 |
win_registry.GetKeyByPath('HKEY_LOCAL_MACHINE\\System\\ControlSet001') |
| 429 |
|
| 430 |
def testGetRegistryFileMappingOnNTUserDat(self): |
| 431 |
"""Tests the GetRegistryFileMapping function on a NTUSER.DAT file.""" |
| 432 |
dat_test_path = self._GetTestFilePath(['NTUSER.DAT']) |
| 433 |
self._SkipIfPathNotExists(dat_test_path) |
| 434 |
|
| 435 |
log_test_path = self._GetTestFilePath(['NTUSER.DAT.LOG']) |
| 436 |
self._SkipIfPathNotExists(log_test_path) |
| 437 |
|
| 438 |
win_registry = registry.WinRegistry( |
| 439 |
registry_file_reader=TestREGFWinRegistryFileReader()) |
| 440 |
|
| 441 |
registry_file = win_registry._OpenFile(dat_test_path) |
| 442 |
|
| 443 |
try: |
| 444 |
key_path_prefix = win_registry.GetRegistryFileMapping(registry_file) |
| 445 |
self.assertEqual(key_path_prefix, 'HKEY_CURRENT_USER') |
| 446 |
|
| 447 |
finally: |
| 448 |
registry_file.Close() |
| 449 |
|
| 450 |
registry_file = win_registry._OpenFile(log_test_path) |
| 451 |
|
| 452 |
try: |
| 453 |
key_path_prefix = win_registry.GetRegistryFileMapping(registry_file) |
| 454 |
self.assertEqual(key_path_prefix, '') |
| 455 |
|
| 456 |
finally: |
| 457 |
registry_file.Close() |
| 458 |
|
| 459 |
key_path_prefix = win_registry.GetRegistryFileMapping(None) |
| 460 |
self.assertEqual(key_path_prefix, '') |
| 461 |
|
| 462 |
def testGetRegistryFileMappingOnSystem(self): |
| 463 |
"""Tests the GetRegistryFileMapping function on a SYSTEM file.""" |
| 464 |
test_path = self._GetTestFilePath(['SYSTEM']) |
| 465 |
self._SkipIfPathNotExists(test_path) |
| 466 |
|
| 467 |
win_registry = registry.WinRegistry( |
| 468 |
registry_file_reader=TestREGFWinRegistryFileReader()) |
| 469 |
|
| 470 |
registry_file = win_registry._OpenFile(test_path) |
| 471 |
|
| 472 |
try: |
| 473 |
key_path_prefix = win_registry.GetRegistryFileMapping(registry_file) |
| 474 |
self.assertEqual(key_path_prefix, 'HKEY_LOCAL_MACHINE\\System') |
| 475 |
|
| 476 |
finally: |
| 477 |
registry_file.Close() |
| 478 |
|
| 479 |
# TODO: add GetRegistryFileMapping on UsrClass file test. |
| 480 |
|
| 481 |
def testGetRootKey(self): |
| 482 |
"""Tests the GetRootKey function.""" |
| 483 |
test_path = self._GetTestFilePath(['SYSTEM']) |
| 484 |
self._SkipIfPathNotExists(test_path) |
| 485 |
|
| 486 |
win_registry = registry.WinRegistry( |
| 487 |
registry_file_reader=TestREGFWinRegistryFileReader()) |
| 488 |
win_registry.OpenAndMapFile(test_path) |
| 489 |
|
| 490 |
root_key = win_registry.GetRootKey() |
| 491 |
self.assertIsNotNone(root_key) |
| 492 |
|
| 493 |
expected_subkey_names = [ |
| 494 |
'HKEY_CLASSES_ROOT', |
| 495 |
'HKEY_CURRENT_CONFIG', |
| 496 |
'HKEY_CURRENT_USER', |
| 497 |
'HKEY_DYN_DATA', |
| 498 |
'HKEY_LOCAL_MACHINE', |
| 499 |
'HKEY_PERFORMANCE_DATA', |
| 500 |
'HKEY_USERS'] |
| 501 |
|
| 502 |
subkey_names = sorted([subkey.name for subkey in root_key.GetSubkeys()]) |
| 503 |
self.assertEqual(subkey_names, expected_subkey_names) |
| 504 |
|
| 505 |
def testMapFile(self): |
| 506 |
"""Tests the MapFile function.""" |
| 507 |
test_path = self._GetTestFilePath(['SYSTEM']) |
| 508 |
self._SkipIfPathNotExists(test_path) |
| 509 |
|
| 510 |
win_registry = registry.WinRegistry( |
| 511 |
registry_file_reader=TestREGFWinRegistryFileReader()) |
| 512 |
|
| 513 |
registry_file = win_registry._OpenFile(test_path) |
| 514 |
|
| 515 |
win_registry = registry.WinRegistry() |
| 516 |
key_path_prefix = win_registry.GetRegistryFileMapping(registry_file) |
| 517 |
win_registry.MapFile(key_path_prefix, registry_file) |
| 518 |
|
| 519 |
def testMapUserFile(self): |
| 520 |
"""Tests the MapUserFile function.""" |
| 521 |
test_path = self._GetTestFilePath(['NTUSER.DAT']) |
| 522 |
self._SkipIfPathNotExists(test_path) |
| 523 |
|
| 524 |
win_registry = registry.WinRegistry( |
| 525 |
registry_file_reader=TestREGFWinRegistryFileReader()) |
| 526 |
|
| 527 |
registry_file = win_registry._OpenFile(test_path) |
| 528 |
|
| 529 |
win_registry = registry.WinRegistry() |
| 530 |
profile_path = '%SystemRoot%\\System32\\config\\systemprofile' |
| 531 |
win_registry.MapUserFile(profile_path, registry_file) |
| 532 |
|
| 533 |
# TODO: add tests for SplitKeyPath |
| 534 |
|
| 535 |
|
| 536 |
if __name__ == '__main__': |
| 537 |
unittest.main() |