| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
"""Tests for the REGF Windows Registry back-end.""" |
| 4 |
|
| 5 |
import unittest |
| 6 |
|
| 7 |
from dfwinreg import errors |
| 8 |
from dfwinreg import regf |
| 9 |
|
| 10 |
from tests import test_lib |
| 11 |
|
| 12 |
|
| 13 |
class FakePyREGFKey(object): |
| 14 |
"""Fake pyregf key for testing.""" |
| 15 |
|
| 16 |
def __init__(self): |
| 17 |
"""Initializes a fake pyregf key.""" |
| 18 |
super(FakePyREGFKey, self).__init__() |
| 19 |
self.number_of_sub_keys = 1 |
| 20 |
|
| 21 |
# pylint: disable=invalid-name,redundant-returns-doc,unused-argument |
| 22 |
|
| 23 |
def get_last_written_time_as_integer(self): |
| 24 |
"""Retrieves the last written time as an integer. |
| 25 |
|
| 26 |
Returns: |
| 27 |
int: last written time, which will be 0 for testing. |
| 28 |
""" |
| 29 |
return 0 |
| 30 |
|
| 31 |
def get_sub_key(self, sub_key_index): |
| 32 |
"""Retrieves a specific sub key. |
| 33 |
|
| 34 |
Returns: |
| 35 |
pyregf.key: sub key, which will be None for testing. |
| 36 |
""" |
| 37 |
return None |
| 38 |
|
| 39 |
|
| 40 |
class FakePyREGFValue(object): |
| 41 |
"""Fake pyregf value for testing. |
| 42 |
|
| 43 |
Attributes: |
| 44 |
name (str): name of the value. |
| 45 |
type (str): value type. |
| 46 |
""" |
| 47 |
|
| 48 |
def __init__(self, name='Test', value_type='REG_SZ'): |
| 49 |
"""Initializes a fake pyregf value. |
| 50 |
|
| 51 |
Args: |
| 52 |
name (Optional[str]): name of the value. |
| 53 |
value_type (Optional[str]): value type. |
| 54 |
""" |
| 55 |
super(FakePyREGFValue, self).__init__() |
| 56 |
self.name = name |
| 57 |
self.type = value_type |
| 58 |
|
| 59 |
# pylint: disable=missing-raises-doc |
| 60 |
|
| 61 |
@property |
| 62 |
def data(self): |
| 63 |
"""bytes: value data.""" |
| 64 |
raise IOError('raised for testing purposes.') |
| 65 |
|
| 66 |
|
| 67 |
class REGFWinRegistryFileTest(test_lib.BaseTestCase): |
| 68 |
"""Tests for the REGF Windows Registry file.""" |
| 69 |
|
| 70 |
# pylint: disable=protected-access |
| 71 |
|
| 72 |
# TODO: add tests for _CreateKey |
| 73 |
# TODO: add tests for _GetCurrentControlSetKeyPath |
| 74 |
# TODO: add tests for _GetKeyByPathFromFile |
| 75 |
# TODO: add tests for AddVirtualKey |
| 76 |
|
| 77 |
def testOpenClose(self): |
| 78 |
"""Tests the Open and Close functions.""" |
| 79 |
test_path = self._GetTestFilePath(['NTUSER.DAT']) |
| 80 |
self._SkipIfPathNotExists(test_path) |
| 81 |
|
| 82 |
registry_file = regf.REGFWinRegistryFile() |
| 83 |
|
| 84 |
with open(test_path, 'rb') as file_object: |
| 85 |
registry_file.Open(file_object) |
| 86 |
registry_file.Close() |
| 87 |
|
| 88 |
def testGetRootKey(self): |
| 89 |
"""Tests the GetRootKey function.""" |
| 90 |
# Test GetRootKey on NTUSER.DAT file |
| 91 |
test_path = self._GetTestFilePath(['NTUSER.DAT']) |
| 92 |
self._SkipIfPathNotExists(test_path) |
| 93 |
|
| 94 |
registry_file = regf.REGFWinRegistryFile() |
| 95 |
|
| 96 |
with open(test_path, 'rb') as file_object: |
| 97 |
registry_file.Open(file_object) |
| 98 |
|
| 99 |
try: |
| 100 |
registry_key = registry_file.GetRootKey() |
| 101 |
self.assertIsNotNone(registry_key) |
| 102 |
self.assertIsInstance(registry_key, regf.REGFWinRegistryKey) |
| 103 |
self.assertEqual(registry_key.path, '\\') |
| 104 |
|
| 105 |
finally: |
| 106 |
registry_file.Close() |
| 107 |
|
| 108 |
# Test GetRootKey on SYSTEM file |
| 109 |
test_path = self._GetTestFilePath(['SYSTEM']) |
| 110 |
self._SkipIfPathNotExists(test_path) |
| 111 |
|
| 112 |
registry_file = regf.REGFWinRegistryFile() |
| 113 |
|
| 114 |
with open(test_path, 'rb') as file_object: |
| 115 |
registry_file.Open(file_object) |
| 116 |
registry_file.AddCurrentControlSetKey() |
| 117 |
|
| 118 |
try: |
| 119 |
registry_key = registry_file.GetRootKey() |
| 120 |
self.assertIsNotNone(registry_key) |
| 121 |
self.assertIsInstance(registry_key, regf.VirtualREGFWinRegistryKey) |
| 122 |
self.assertEqual(registry_key.path, '\\') |
| 123 |
|
| 124 |
finally: |
| 125 |
registry_file.Close() |
| 126 |
|
| 127 |
# Test GetRootKey on NTUSER.DAT.LOG file |
| 128 |
registry_file = regf.REGFWinRegistryFile() |
| 129 |
|
| 130 |
test_path = self._GetTestFilePath(['NTUSER.DAT.LOG']) |
| 131 |
self._SkipIfPathNotExists(test_path) |
| 132 |
|
| 133 |
with open(test_path, 'rb') as file_object: |
| 134 |
registry_file.Open(file_object) |
| 135 |
|
| 136 |
try: |
| 137 |
root_key = registry_file.GetRootKey() |
| 138 |
self.assertIsNone(root_key) |
| 139 |
|
| 140 |
finally: |
| 141 |
registry_file.Close() |
| 142 |
|
| 143 |
def testGetKeyByPath(self): |
| 144 |
"""Tests the GetKeyByPath function.""" |
| 145 |
test_path = self._GetTestFilePath(['SYSTEM']) |
| 146 |
self._SkipIfPathNotExists(test_path) |
| 147 |
|
| 148 |
registry_file = regf.REGFWinRegistryFile() |
| 149 |
|
| 150 |
with open(test_path, 'rb') as file_object: |
| 151 |
registry_file.Open(file_object) |
| 152 |
registry_file.AddCurrentControlSetKey() |
| 153 |
|
| 154 |
try: |
| 155 |
key_path = '\\' |
| 156 |
registry_key = registry_file.GetKeyByPath(key_path) |
| 157 |
self.assertIsNotNone(registry_key) |
| 158 |
self.assertEqual(registry_key.name, '') |
| 159 |
self.assertEqual(registry_key.path, key_path) |
| 160 |
|
| 161 |
key_path = '\\ControlSet001' |
| 162 |
registry_key = registry_file.GetKeyByPath(key_path) |
| 163 |
self.assertIsNotNone(registry_key) |
| 164 |
self.assertEqual(registry_key.name, 'ControlSet001') |
| 165 |
self.assertEqual(registry_key.path, key_path) |
| 166 |
|
| 167 |
registry_key = registry_file.GetKeyByPath('ControlSet001') |
| 168 |
self.assertIsNotNone(registry_key) |
| 169 |
self.assertEqual(registry_key.name, 'ControlSet001') |
| 170 |
self.assertEqual(registry_key.path, key_path) |
| 171 |
|
| 172 |
key_path = '\\CurrentControlSet' |
| 173 |
registry_key = registry_file.GetKeyByPath(key_path) |
| 174 |
self.assertIsNotNone(registry_key) |
| 175 |
self.assertEqual(registry_key.name, 'CurrentControlSet') |
| 176 |
self.assertEqual(registry_key.path, key_path) |
| 177 |
|
| 178 |
key_path = '\\CurrentControlSet\\Enum' |
| 179 |
registry_key = registry_file.GetKeyByPath(key_path) |
| 180 |
self.assertIsNotNone(registry_key) |
| 181 |
self.assertEqual(registry_key.name, 'Enum') |
| 182 |
self.assertEqual(registry_key.path, key_path) |
| 183 |
|
| 184 |
key_path = '\\Bogus' |
| 185 |
registry_key = registry_file.GetKeyByPath(key_path) |
| 186 |
self.assertIsNone(registry_key) |
| 187 |
|
| 188 |
finally: |
| 189 |
registry_file.Close() |
| 190 |
|
| 191 |
def testRecurseKeys(self): |
| 192 |
"""Tests the RecurseKeys function.""" |
| 193 |
dat_test_path = self._GetTestFilePath(['NTUSER.DAT']) |
| 194 |
self._SkipIfPathNotExists(dat_test_path) |
| 195 |
|
| 196 |
log_test_path = self._GetTestFilePath(['NTUSER.DAT.LOG']) |
| 197 |
self._SkipIfPathNotExists(log_test_path) |
| 198 |
|
| 199 |
registry_file = regf.REGFWinRegistryFile() |
| 200 |
|
| 201 |
with open(dat_test_path, 'rb') as file_object: |
| 202 |
registry_file.Open(file_object) |
| 203 |
|
| 204 |
try: |
| 205 |
registry_keys = list(registry_file.RecurseKeys()) |
| 206 |
|
| 207 |
finally: |
| 208 |
registry_file.Close() |
| 209 |
|
| 210 |
self.assertEqual(len(registry_keys), 1597) |
| 211 |
|
| 212 |
registry_file = regf.REGFWinRegistryFile() |
| 213 |
|
| 214 |
with open(log_test_path, 'rb') as file_object: |
| 215 |
registry_file.Open(file_object) |
| 216 |
|
| 217 |
try: |
| 218 |
registry_keys = list(registry_file.RecurseKeys()) |
| 219 |
|
| 220 |
finally: |
| 221 |
registry_file.Close() |
| 222 |
|
| 223 |
self.assertEqual(len(registry_keys), 0) |
| 224 |
|
| 225 |
|
| 226 |
class REGFWinRegistryKeyTest(test_lib.BaseTestCase): |
| 227 |
"""Tests for the REGF Windows Registry key.""" |
| 228 |
|
| 229 |
# pylint: disable=protected-access |
| 230 |
|
| 231 |
def testProperties(self): |
| 232 |
"""Tests the properties functions.""" |
| 233 |
test_path = self._GetTestFilePath(['NTUSER.DAT']) |
| 234 |
self._SkipIfPathNotExists(test_path) |
| 235 |
|
| 236 |
registry_file = regf.REGFWinRegistryFile() |
| 237 |
|
| 238 |
with open(test_path, 'rb') as file_object: |
| 239 |
registry_file.Open(file_object) |
| 240 |
|
| 241 |
try: |
| 242 |
key_path = '\\Software' |
| 243 |
registry_key = registry_file.GetKeyByPath(key_path) |
| 244 |
self.assertIsNotNone(registry_key) |
| 245 |
self.assertIsNone(registry_key.class_name) |
| 246 |
self.assertEqual(registry_key.name, 'Software') |
| 247 |
self.assertEqual(registry_key.number_of_subkeys, 7) |
| 248 |
self.assertEqual(registry_key.number_of_values, 0) |
| 249 |
self.assertEqual(registry_key.offset, 4372) |
| 250 |
self.assertEqual(registry_key.path, key_path) |
| 251 |
|
| 252 |
self.assertIsNotNone(registry_key.last_written_time) |
| 253 |
timestamp = registry_key.last_written_time.timestamp |
| 254 |
self.assertEqual(timestamp, 131205170396534120) |
| 255 |
|
| 256 |
registry_key._pyregf_key = FakePyREGFKey() |
| 257 |
self.assertIsNotNone(registry_key.last_written_time) |
| 258 |
|
| 259 |
date_time_string = ( |
| 260 |
registry_key.last_written_time.CopyToDateTimeString()) |
| 261 |
self.assertEqual(date_time_string, 'Not set') |
| 262 |
|
| 263 |
finally: |
| 264 |
registry_file.Close() |
| 265 |
|
| 266 |
def testGetSubkeyByIndex(self): |
| 267 |
"""Tests the GetSubkeyByIndex function.""" |
| 268 |
test_path = self._GetTestFilePath(['NTUSER.DAT']) |
| 269 |
self._SkipIfPathNotExists(test_path) |
| 270 |
|
| 271 |
registry_file = regf.REGFWinRegistryFile( |
| 272 |
key_path_prefix='HKEY_CURRENT_USER') |
| 273 |
|
| 274 |
with open(test_path, 'rb') as file_object: |
| 275 |
registry_file.Open(file_object) |
| 276 |
|
| 277 |
try: |
| 278 |
registry_key = registry_file.GetRootKey() |
| 279 |
|
| 280 |
key_name = 'AppEvents' |
| 281 |
sub_registry_key = registry_key.GetSubkeyByIndex(0) |
| 282 |
self.assertIsNotNone(sub_registry_key) |
| 283 |
self.assertEqual(sub_registry_key.name, key_name) |
| 284 |
|
| 285 |
expected_key_path = 'HKEY_CURRENT_USER\\AppEvents' |
| 286 |
self.assertEqual(sub_registry_key.path, expected_key_path) |
| 287 |
|
| 288 |
with self.assertRaises(IndexError): |
| 289 |
registry_key.GetSubkeyByIndex(-1) |
| 290 |
|
| 291 |
finally: |
| 292 |
registry_file.Close() |
| 293 |
|
| 294 |
def testGetSubkeyByName(self): |
| 295 |
"""Tests the GetSubkeyByName function.""" |
| 296 |
test_path = self._GetTestFilePath(['NTUSER.DAT']) |
| 297 |
self._SkipIfPathNotExists(test_path) |
| 298 |
|
| 299 |
registry_file = regf.REGFWinRegistryFile( |
| 300 |
key_path_prefix='HKEY_CURRENT_USER') |
| 301 |
|
| 302 |
with open(test_path, 'rb') as file_object: |
| 303 |
registry_file.Open(file_object) |
| 304 |
|
| 305 |
try: |
| 306 |
registry_key = registry_file.GetRootKey() |
| 307 |
self.assertIsNotNone(registry_key) |
| 308 |
|
| 309 |
key_name = 'Software' |
| 310 |
sub_registry_key = registry_key.GetSubkeyByName(key_name) |
| 311 |
self.assertIsNotNone(sub_registry_key) |
| 312 |
self.assertEqual(sub_registry_key.name, key_name) |
| 313 |
|
| 314 |
expected_key_path = 'HKEY_CURRENT_USER\\Software' |
| 315 |
self.assertEqual(sub_registry_key.path, expected_key_path) |
| 316 |
|
| 317 |
key_name = 'Bogus' |
| 318 |
sub_registry_key = registry_key.GetSubkeyByName(key_name) |
| 319 |
self.assertIsNone(sub_registry_key) |
| 320 |
|
| 321 |
finally: |
| 322 |
registry_file.Close() |
| 323 |
|
| 324 |
def testGetSubkeyByPath(self): |
| 325 |
"""Tests the GetSubkeyByPath function.""" |
| 326 |
test_path = self._GetTestFilePath(['NTUSER.DAT']) |
| 327 |
self._SkipIfPathNotExists(test_path) |
| 328 |
|
| 329 |
registry_file = regf.REGFWinRegistryFile( |
| 330 |
key_path_prefix='HKEY_CURRENT_USER') |
| 331 |
|
| 332 |
with open(test_path, 'rb') as file_object: |
| 333 |
registry_file.Open(file_object) |
| 334 |
|
| 335 |
try: |
| 336 |
registry_key = registry_file.GetRootKey() |
| 337 |
|
| 338 |
key_path = 'Software\\Microsoft' |
| 339 |
sub_registry_key = registry_key.GetSubkeyByPath(key_path) |
| 340 |
self.assertIsNotNone(sub_registry_key) |
| 341 |
self.assertEqual(sub_registry_key.name, 'Microsoft') |
| 342 |
|
| 343 |
expected_key_path = 'HKEY_CURRENT_USER\\Software\\Microsoft' |
| 344 |
self.assertEqual(sub_registry_key.path, expected_key_path) |
| 345 |
|
| 346 |
key_path = 'Software\\Bogus' |
| 347 |
sub_registry_key = registry_key.GetSubkeyByPath(key_path) |
| 348 |
self.assertIsNone(sub_registry_key) |
| 349 |
|
| 350 |
finally: |
| 351 |
registry_file.Close() |
| 352 |
|
| 353 |
def testGetSubkeys(self): |
| 354 |
"""Tests the GetSubkeys function.""" |
| 355 |
test_path = self._GetTestFilePath(['NTUSER.DAT']) |
| 356 |
self._SkipIfPathNotExists(test_path) |
| 357 |
|
| 358 |
registry_file = regf.REGFWinRegistryFile() |
| 359 |
|
| 360 |
with open(test_path, 'rb') as file_object: |
| 361 |
registry_file.Open(file_object) |
| 362 |
|
| 363 |
try: |
| 364 |
key_path = '\\Software' |
| 365 |
registry_key = registry_file.GetKeyByPath(key_path) |
| 366 |
self.assertIsNotNone(registry_key) |
| 367 |
|
| 368 |
sub_registry_keys = list(registry_key.GetSubkeys()) |
| 369 |
self.assertEqual(len(sub_registry_keys), 7) |
| 370 |
|
| 371 |
finally: |
| 372 |
registry_file.Close() |
| 373 |
|
| 374 |
def testGetValueByName(self): |
| 375 |
"""Tests the GetValueByName function.""" |
| 376 |
test_path = self._GetTestFilePath(['NTUSER.DAT']) |
| 377 |
self._SkipIfPathNotExists(test_path) |
| 378 |
|
| 379 |
registry_file = regf.REGFWinRegistryFile() |
| 380 |
|
| 381 |
with open(test_path, 'rb') as file_object: |
| 382 |
registry_file.Open(file_object) |
| 383 |
|
| 384 |
try: |
| 385 |
registry_key = registry_file.GetKeyByPath('\\Console') |
| 386 |
self.assertIsNotNone(registry_key) |
| 387 |
|
| 388 |
value_name = 'ColorTable14' |
| 389 |
registry_value = registry_key.GetValueByName(value_name) |
| 390 |
self.assertIsNotNone(registry_value) |
| 391 |
self.assertEqual(registry_value.name, value_name) |
| 392 |
|
| 393 |
value_name = 'Bogus' |
| 394 |
registry_value = registry_key.GetValueByName(value_name) |
| 395 |
self.assertIsNone(registry_value) |
| 396 |
|
| 397 |
# Test retrieving the default (or nameless) value. |
| 398 |
registry_key = registry_file.GetKeyByPath( |
| 399 |
'\\AppEvents\\EventLabels\\.Default') |
| 400 |
self.assertIsNotNone(registry_key) |
| 401 |
|
| 402 |
registry_value = registry_key.GetValueByName('') |
| 403 |
self.assertIsNotNone(registry_value) |
| 404 |
self.assertIsNone(registry_value.name) |
| 405 |
|
| 406 |
finally: |
| 407 |
registry_file.Close() |
| 408 |
|
| 409 |
def testGetValues(self): |
| 410 |
"""Tests the GetValues function.""" |
| 411 |
test_path = self._GetTestFilePath(['NTUSER.DAT']) |
| 412 |
self._SkipIfPathNotExists(test_path) |
| 413 |
|
| 414 |
registry_file = regf.REGFWinRegistryFile() |
| 415 |
|
| 416 |
with open(test_path, 'rb') as file_object: |
| 417 |
registry_file.Open(file_object) |
| 418 |
|
| 419 |
try: |
| 420 |
key_path = '\\Console' |
| 421 |
registry_key = registry_file.GetKeyByPath(key_path) |
| 422 |
self.assertIsNotNone(registry_key) |
| 423 |
|
| 424 |
values = list(registry_key.GetValues()) |
| 425 |
self.assertEqual(len(values), 37) |
| 426 |
|
| 427 |
finally: |
| 428 |
registry_file.Close() |
| 429 |
|
| 430 |
def testRecurseKeys(self): |
| 431 |
"""Tests the RecurseKeys function.""" |
| 432 |
test_path = self._GetTestFilePath(['NTUSER.DAT']) |
| 433 |
self._SkipIfPathNotExists(test_path) |
| 434 |
|
| 435 |
registry_file = regf.REGFWinRegistryFile() |
| 436 |
|
| 437 |
with open(test_path, 'rb') as file_object: |
| 438 |
registry_file.Open(file_object) |
| 439 |
|
| 440 |
try: |
| 441 |
key_path = '\\Software' |
| 442 |
registry_key = registry_file.GetKeyByPath(key_path) |
| 443 |
self.assertIsNotNone(registry_key) |
| 444 |
|
| 445 |
registry_keys = list(registry_key.RecurseKeys()) |
| 446 |
|
| 447 |
finally: |
| 448 |
registry_file.Close() |
| 449 |
|
| 450 |
self.assertEqual(len(registry_keys), 1219) |
| 451 |
|
| 452 |
|
| 453 |
class VirtualREGFWinRegistryKeyTest(test_lib.BaseTestCase): |
| 454 |
"""Tests for the virtual REGF Windows Registry key.""" |
| 455 |
|
| 456 |
# pylint: disable=protected-access |
| 457 |
|
| 458 |
# TODO: add tests for _GetVirtualSubKeyByName |
| 459 |
# TODO: add tests for AddVirtualSubKey |
| 460 |
|
| 461 |
def testGetSubkeyByIndex(self): |
| 462 |
"""Tests the GetSubkeyByIndex function.""" |
| 463 |
test_path = self._GetTestFilePath(['SYSTEM']) |
| 464 |
self._SkipIfPathNotExists(test_path) |
| 465 |
|
| 466 |
registry_file = regf.REGFWinRegistryFile() |
| 467 |
|
| 468 |
with open(test_path, 'rb') as file_object: |
| 469 |
registry_file.Open(file_object) |
| 470 |
registry_file.AddCurrentControlSetKey() |
| 471 |
|
| 472 |
try: |
| 473 |
key_path = '\\' |
| 474 |
registry_key = registry_file.GetKeyByPath(key_path) |
| 475 |
self.assertIsNotNone(registry_key) |
| 476 |
self.assertEqual(registry_key.path, key_path) |
| 477 |
self.assertIsInstance(registry_key, regf.VirtualREGFWinRegistryKey) |
| 478 |
|
| 479 |
self.assertEqual(registry_key.number_of_subkeys, 9) |
| 480 |
|
| 481 |
sub_registry_key = registry_key.GetSubkeyByIndex(0) |
| 482 |
self.assertIsNotNone(sub_registry_key) |
| 483 |
self.assertEqual(sub_registry_key.name, 'ControlSet001') |
| 484 |
self.assertIsInstance(sub_registry_key, regf.REGFWinRegistryKey) |
| 485 |
|
| 486 |
sub_registry_key = registry_key.GetSubkeyByIndex(8) |
| 487 |
self.assertIsNotNone(sub_registry_key) |
| 488 |
self.assertEqual(sub_registry_key.name, 'CurrentControlSet') |
| 489 |
self.assertIsInstance(sub_registry_key, regf.VirtualREGFWinRegistryKey) |
| 490 |
|
| 491 |
self.assertEqual(sub_registry_key.number_of_subkeys, 5) |
| 492 |
|
| 493 |
with self.assertRaises(IndexError): |
| 494 |
registry_key.GetSubkeyByIndex(9) |
| 495 |
|
| 496 |
finally: |
| 497 |
registry_file.Close() |
| 498 |
|
| 499 |
def testGetSubkeyByName(self): |
| 500 |
"""Tests the GetSubkeyByName function.""" |
| 501 |
test_path = self._GetTestFilePath(['SYSTEM']) |
| 502 |
self._SkipIfPathNotExists(test_path) |
| 503 |
|
| 504 |
registry_file = regf.REGFWinRegistryFile() |
| 505 |
|
| 506 |
with open(test_path, 'rb') as file_object: |
| 507 |
registry_file.Open(file_object) |
| 508 |
registry_file.AddCurrentControlSetKey() |
| 509 |
|
| 510 |
try: |
| 511 |
key_path = '\\' |
| 512 |
registry_key = registry_file.GetKeyByPath(key_path) |
| 513 |
self.assertIsNotNone(registry_key) |
| 514 |
self.assertEqual(registry_key.path, key_path) |
| 515 |
self.assertIsInstance(registry_key, regf.VirtualREGFWinRegistryKey) |
| 516 |
|
| 517 |
self.assertEqual(registry_key.number_of_subkeys, 9) |
| 518 |
|
| 519 |
sub_registry_key = registry_key.GetSubkeyByName('ControlSet001') |
| 520 |
self.assertIsNotNone(sub_registry_key) |
| 521 |
self.assertEqual(sub_registry_key.name, 'ControlSet001') |
| 522 |
self.assertIsInstance(sub_registry_key, regf.REGFWinRegistryKey) |
| 523 |
|
| 524 |
sub_registry_key = registry_key.GetSubkeyByName('CurrentControlSet') |
| 525 |
self.assertIsNotNone(sub_registry_key) |
| 526 |
self.assertEqual(sub_registry_key.name, 'CurrentControlSet') |
| 527 |
self.assertIsInstance(sub_registry_key, regf.VirtualREGFWinRegistryKey) |
| 528 |
|
| 529 |
self.assertEqual(sub_registry_key.number_of_subkeys, 5) |
| 530 |
|
| 531 |
# Ensure the virtual CurrentControlSet key does not return another copy |
| 532 |
# of itself. |
| 533 |
test_registry_key = sub_registry_key.GetSubkeyByName( |
| 534 |
'CurrentControlSet') |
| 535 |
self.assertIsNone(test_registry_key) |
| 536 |
|
| 537 |
finally: |
| 538 |
registry_file.Close() |
| 539 |
|
| 540 |
def testGetSubkeyByPath(self): |
| 541 |
"""Tests the GetSubkeyByPath function.""" |
| 542 |
test_path = self._GetTestFilePath(['SYSTEM']) |
| 543 |
self._SkipIfPathNotExists(test_path) |
| 544 |
|
| 545 |
registry_file = regf.REGFWinRegistryFile() |
| 546 |
|
| 547 |
with open(test_path, 'rb') as file_object: |
| 548 |
registry_file.Open(file_object) |
| 549 |
registry_file.AddCurrentControlSetKey() |
| 550 |
|
| 551 |
try: |
| 552 |
key_path = '\\' |
| 553 |
registry_key = registry_file.GetKeyByPath(key_path) |
| 554 |
self.assertIsNotNone(registry_key) |
| 555 |
self.assertEqual(registry_key.path, key_path) |
| 556 |
self.assertIsInstance(registry_key, regf.VirtualREGFWinRegistryKey) |
| 557 |
|
| 558 |
self.assertEqual(registry_key.number_of_subkeys, 9) |
| 559 |
|
| 560 |
sub_registry_key = registry_key.GetSubkeyByPath('\\ControlSet001') |
| 561 |
self.assertIsNotNone(sub_registry_key) |
| 562 |
self.assertEqual(sub_registry_key.name, 'ControlSet001') |
| 563 |
self.assertIsInstance(sub_registry_key, regf.REGFWinRegistryKey) |
| 564 |
|
| 565 |
sub_registry_key = registry_key.GetSubkeyByPath( |
| 566 |
'\\ControlSet001\\Enum') |
| 567 |
self.assertIsNotNone(sub_registry_key) |
| 568 |
self.assertEqual(sub_registry_key.name, 'Enum') |
| 569 |
self.assertEqual(sub_registry_key.path, '\\ControlSet001\\Enum') |
| 570 |
self.assertIsInstance(sub_registry_key, regf.REGFWinRegistryKey) |
| 571 |
|
| 572 |
sub_registry_key = registry_key.GetSubkeyByPath('\\CurrentControlSet') |
| 573 |
self.assertIsNotNone(sub_registry_key) |
| 574 |
self.assertEqual(sub_registry_key.name, 'CurrentControlSet') |
| 575 |
self.assertIsInstance(sub_registry_key, regf.VirtualREGFWinRegistryKey) |
| 576 |
|
| 577 |
sub_registry_key = registry_key.GetSubkeyByPath( |
| 578 |
'\\CurrentControlSet\\Enum') |
| 579 |
self.assertIsNotNone(sub_registry_key) |
| 580 |
self.assertEqual(sub_registry_key.name, 'Enum') |
| 581 |
self.assertEqual(sub_registry_key.path, '\\CurrentControlSet\\Enum') |
| 582 |
self.assertIsInstance(sub_registry_key, regf.REGFWinRegistryKey) |
| 583 |
|
| 584 |
finally: |
| 585 |
registry_file.Close() |
| 586 |
|
| 587 |
def testGetSubkeys(self): |
| 588 |
"""Tests the GetSubkeys function.""" |
| 589 |
test_path = self._GetTestFilePath(['SYSTEM']) |
| 590 |
self._SkipIfPathNotExists(test_path) |
| 591 |
|
| 592 |
registry_file = regf.REGFWinRegistryFile() |
| 593 |
|
| 594 |
with open(test_path, 'rb') as file_object: |
| 595 |
registry_file.Open(file_object) |
| 596 |
registry_file.AddCurrentControlSetKey() |
| 597 |
|
| 598 |
try: |
| 599 |
key_path = '\\' |
| 600 |
registry_key = registry_file.GetKeyByPath(key_path) |
| 601 |
self.assertIsNotNone(registry_key) |
| 602 |
self.assertEqual(registry_key.path, key_path) |
| 603 |
self.assertIsInstance(registry_key, regf.VirtualREGFWinRegistryKey) |
| 604 |
|
| 605 |
self.assertEqual(registry_key.number_of_subkeys, 9) |
| 606 |
|
| 607 |
expected_subkey_names = [ |
| 608 |
'ControlSet001', |
| 609 |
'DriverDatabase', |
| 610 |
'HardwareConfig', |
| 611 |
'MountedDevices', |
| 612 |
'RNG', |
| 613 |
'Select', |
| 614 |
'Setup', |
| 615 |
'WPA', |
| 616 |
'CurrentControlSet'] |
| 617 |
|
| 618 |
subkey_names = [subkey.name for subkey in registry_key.GetSubkeys()] |
| 619 |
self.assertEqual(subkey_names, expected_subkey_names) |
| 620 |
|
| 621 |
sub_registry_key = registry_key.GetSubkeyByPath('\\CurrentControlSet') |
| 622 |
self.assertIsNotNone(sub_registry_key) |
| 623 |
|
| 624 |
self.assertEqual(sub_registry_key.number_of_subkeys, 5) |
| 625 |
|
| 626 |
expected_subkey_names = [ |
| 627 |
'Control', |
| 628 |
'Enum', |
| 629 |
'Hardware Profiles', |
| 630 |
'Policies', |
| 631 |
'Services'] |
| 632 |
|
| 633 |
subkey_names = [subkey.name for subkey in sub_registry_key.GetSubkeys()] |
| 634 |
self.assertEqual(subkey_names, expected_subkey_names) |
| 635 |
|
| 636 |
finally: |
| 637 |
registry_file.Close() |
| 638 |
|
| 639 |
|
| 640 |
class REGFWinRegistryValueTest(test_lib.BaseTestCase): |
| 641 |
"""Tests for the REGF Windows Registry value.""" |
| 642 |
|
| 643 |
# pylint: disable=protected-access |
| 644 |
|
| 645 |
def testProperties(self): |
| 646 |
"""Tests the properties functions on a NTUSER.DAT file.""" |
| 647 |
test_path = self._GetTestFilePath(['NTUSER.DAT']) |
| 648 |
self._SkipIfPathNotExists(test_path) |
| 649 |
|
| 650 |
registry_file = regf.REGFWinRegistryFile() |
| 651 |
|
| 652 |
with open(test_path, 'rb') as file_object: |
| 653 |
registry_file.Open(file_object) |
| 654 |
|
| 655 |
try: |
| 656 |
registry_key = registry_file.GetKeyByPath('\\Console') |
| 657 |
self.assertIsNotNone(registry_key) |
| 658 |
|
| 659 |
value_name = 'ColorTable14' |
| 660 |
registry_value = registry_key.GetValueByName(value_name) |
| 661 |
expected_data = b'\xff\xff\x00\x00' |
| 662 |
|
| 663 |
self.assertIsNotNone(registry_value) |
| 664 |
self.assertEqual(registry_value.data_type, 4) |
| 665 |
self.assertEqual(registry_value.data_type_string, 'REG_DWORD_LE') |
| 666 |
self.assertEqual(registry_value.GetDataAsObject(), 65535) |
| 667 |
self.assertEqual(registry_value.name, value_name) |
| 668 |
self.assertEqual(registry_value.offset, 105212) |
| 669 |
self.assertEqual(registry_value.data, expected_data) |
| 670 |
|
| 671 |
registry_key = registry_file.GetKeyByPath( |
| 672 |
'\\AppEvents\\EventLabels\\CriticalBatteryAlarm') |
| 673 |
self.assertIsNotNone(registry_key) |
| 674 |
|
| 675 |
value_name = 'DispFileName' |
| 676 |
registry_value = registry_key.GetValueByName(value_name) |
| 677 |
expected_data = ( |
| 678 |
b'@\x00m\x00m\x00r\x00e\x00s\x00.\x00d\x00l\x00l\x00,\x00-\x005' |
| 679 |
b'\x008\x002\x007\x00\x00\x00') |
| 680 |
|
| 681 |
self.assertIsNotNone(registry_value) |
| 682 |
self.assertEqual(registry_value.data_type, 1) |
| 683 |
self.assertEqual(registry_value.data_type_string, 'REG_SZ') |
| 684 |
self.assertEqual(registry_value.GetDataAsObject(), '@mmres.dll,-5827') |
| 685 |
self.assertEqual(registry_value.name, value_name) |
| 686 |
self.assertEqual(registry_value.offset, 62028) |
| 687 |
self.assertEqual(registry_value.data, expected_data) |
| 688 |
|
| 689 |
registry_key = registry_file.GetKeyByPath('\\Control Panel\\Appearance') |
| 690 |
self.assertIsNotNone(registry_key) |
| 691 |
|
| 692 |
value_name = 'SchemeLangID' |
| 693 |
registry_value = registry_key.GetValueByName(value_name) |
| 694 |
expected_data = b'\x00\x00' |
| 695 |
|
| 696 |
self.assertIsNotNone(registry_value) |
| 697 |
self.assertEqual(registry_value.data_type, 3) |
| 698 |
self.assertEqual(registry_value.data_type_string, 'REG_BINARY') |
| 699 |
self.assertEqual(registry_value.GetDataAsObject(), expected_data) |
| 700 |
self.assertEqual(registry_value.name, value_name) |
| 701 |
self.assertEqual(registry_value.offset, 46468) |
| 702 |
self.assertEqual(registry_value.data, expected_data) |
| 703 |
|
| 704 |
registry_value._pyregf_value = FakePyREGFValue() |
| 705 |
with self.assertRaises(errors.WinRegistryValueError): |
| 706 |
_ = registry_value.data |
| 707 |
|
| 708 |
finally: |
| 709 |
registry_file.Close() |
| 710 |
|
| 711 |
def testGetDataAsObject(self): |
| 712 |
"""Tests the GetDataAsObject function.""" |
| 713 |
test_path = self._GetTestFilePath(['NTUSER.DAT']) |
| 714 |
self._SkipIfPathNotExists(test_path) |
| 715 |
|
| 716 |
registry_file = regf.REGFWinRegistryFile() |
| 717 |
|
| 718 |
with open(test_path, 'rb') as file_object: |
| 719 |
registry_file.Open(file_object) |
| 720 |
|
| 721 |
try: |
| 722 |
registry_key = registry_file.GetKeyByPath('\\Console') |
| 723 |
self.assertIsNotNone(registry_key) |
| 724 |
|
| 725 |
registry_value = registry_key.GetValueByName('ColorTable14') |
| 726 |
self.assertIsNotNone(registry_value) |
| 727 |
|
| 728 |
data_object = registry_value.GetDataAsObject() |
| 729 |
self.assertEqual(data_object, 65535) |
| 730 |
|
| 731 |
registry_value._pyregf_value = FakePyREGFValue(value_type='REG_SZ') |
| 732 |
with self.assertRaises(errors.WinRegistryValueError): |
| 733 |
registry_value.GetDataAsObject() |
| 734 |
|
| 735 |
registry_value._pyregf_value = FakePyREGFValue( |
| 736 |
value_type='REG_DWORD_LE') |
| 737 |
with self.assertRaises(errors.WinRegistryValueError): |
| 738 |
registry_value.GetDataAsObject() |
| 739 |
|
| 740 |
registry_value._pyregf_value = FakePyREGFValue( |
| 741 |
value_type='REG_MULTI_SZ') |
| 742 |
with self.assertRaises(errors.WinRegistryValueError): |
| 743 |
registry_value.GetDataAsObject() |
| 744 |
|
| 745 |
# Test REG_MULTI_SZ without additional empty string. |
| 746 |
registry_key = registry_file.GetKeyByPath( |
| 747 |
'\\Control Panel\\International\\User Profile') |
| 748 |
self.assertIsNotNone(registry_key) |
| 749 |
|
| 750 |
registry_value = registry_key.GetValueByName('Languages') |
| 751 |
self.assertIsNotNone(registry_value) |
| 752 |
|
| 753 |
data_object = registry_value.GetDataAsObject() |
| 754 |
self.assertEqual(len(data_object), 1) |
| 755 |
|
| 756 |
# Test REG_MULTI_SZ with additional empty string. |
| 757 |
registry_key = registry_file.GetKeyByPath( |
| 758 |
'\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\' |
| 759 |
'Discardable\\PostSetup\\ShellNew') |
| 760 |
self.assertIsNotNone(registry_key) |
| 761 |
|
| 762 |
registry_value = registry_key.GetValueByName('Classes') |
| 763 |
self.assertIsNotNone(registry_value) |
| 764 |
|
| 765 |
data_object = registry_value.GetDataAsObject() |
| 766 |
self.assertEqual(len(data_object), 9) |
| 767 |
|
| 768 |
finally: |
| 769 |
registry_file.Close() |
| 770 |
|
| 771 |
|
| 772 |
if __name__ == '__main__': |
| 773 |
unittest.main() |