| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
"""Tests for the Windows Registry searcher.""" |
| 4 |
|
| 5 |
import unittest |
| 6 |
|
| 7 |
from dfwinreg import fake |
| 8 |
from dfwinreg import registry |
| 9 |
from dfwinreg import registry_searcher |
| 10 |
|
| 11 |
from tests import registry as test_registry |
| 12 |
from tests import test_lib |
| 13 |
|
| 14 |
|
| 15 |
class FindSpecTest(test_lib.BaseTestCase): |
| 16 |
"""Tests for the find specification.""" |
| 17 |
|
| 18 |
# pylint: disable=protected-access |
| 19 |
|
| 20 |
def testInitialize(self): |
| 21 |
"""Tests the __init__ function.""" |
| 22 |
find_spec = registry_searcher.FindSpec() |
| 23 |
self.assertIsNotNone(find_spec) |
| 24 |
|
| 25 |
find_spec = registry_searcher.FindSpec( |
| 26 |
key_path='HKEY_CURRENT_USER\\Software\\Microsoft') |
| 27 |
self.assertIsNotNone(find_spec) |
| 28 |
|
| 29 |
find_spec = registry_searcher.FindSpec( |
| 30 |
key_path=['HKEY_CURRENT_USER', 'Software', 'Microsoft']) |
| 31 |
self.assertIsNotNone(find_spec) |
| 32 |
|
| 33 |
find_spec = registry_searcher.FindSpec( |
| 34 |
key_path_glob='HKEY_CURRENT_USER\\*\\Microsoft') |
| 35 |
self.assertIsNotNone(find_spec) |
| 36 |
|
| 37 |
find_spec = registry_searcher.FindSpec( |
| 38 |
key_path_glob=['HKEY_CURRENT_USER', '*', 'Microsoft']) |
| 39 |
self.assertIsNotNone(find_spec) |
| 40 |
|
| 41 |
find_spec = registry_searcher.FindSpec( |
| 42 |
key_path_regex='HKEY_CURRENT_USER\\.*\\Microsoft') |
| 43 |
self.assertIsNotNone(find_spec) |
| 44 |
|
| 45 |
find_spec = registry_searcher.FindSpec( |
| 46 |
key_path_regex=['HKEY_CURRENT_USER', '.*', 'Microsoft']) |
| 47 |
self.assertIsNotNone(find_spec) |
| 48 |
|
| 49 |
with self.assertRaises(TypeError): |
| 50 |
registry_searcher.FindSpec(key_path=('bogus', 0)) |
| 51 |
|
| 52 |
with self.assertRaises(TypeError): |
| 53 |
registry_searcher.FindSpec(key_path_glob=('bogus', 0)) |
| 54 |
|
| 55 |
with self.assertRaises(TypeError): |
| 56 |
registry_searcher.FindSpec(key_path_regex=('bogus', 0)) |
| 57 |
|
| 58 |
with self.assertRaises(ValueError): |
| 59 |
registry_searcher.FindSpec( |
| 60 |
key_path='HKEY_CURRENT_USER\\Software\\Microsoft', |
| 61 |
key_path_glob='HKEY_CURRENT_USER\\*\\Microsoft') |
| 62 |
|
| 63 |
def testCheckKeyPath(self): |
| 64 |
"""Tests the _CheckKeyPath function.""" |
| 65 |
find_spec = registry_searcher.FindSpec( |
| 66 |
key_path='HKEY_CURRENT_USER\\Software\\Microsoft') |
| 67 |
|
| 68 |
registry_key = fake.FakeWinRegistryKey( |
| 69 |
'Microsoft', key_path_prefix='HKEY_CURRENT_USER\\Software', |
| 70 |
relative_key_path='Microsoft') |
| 71 |
|
| 72 |
result = find_spec._CheckKeyPath(registry_key, 3) |
| 73 |
self.assertTrue(result) |
| 74 |
|
| 75 |
result = find_spec._CheckKeyPath(registry_key, 0) |
| 76 |
self.assertTrue(result) |
| 77 |
|
| 78 |
# Test incorrect search depth. |
| 79 |
result = find_spec._CheckKeyPath(registry_key, 1) |
| 80 |
self.assertFalse(result) |
| 81 |
|
| 82 |
# Test invalid search depth. |
| 83 |
result = find_spec._CheckKeyPath(registry_key, -1) |
| 84 |
self.assertFalse(result) |
| 85 |
|
| 86 |
result = find_spec._CheckKeyPath(registry_key, 99) |
| 87 |
self.assertFalse(result) |
| 88 |
|
| 89 |
# Test find specification with regular expression. |
| 90 |
find_spec = registry_searcher.FindSpec( |
| 91 |
key_path_regex=['HKEY_CURRENT_USER', 'Software', 'Microsoft']) |
| 92 |
|
| 93 |
registry_key = fake.FakeWinRegistryKey( |
| 94 |
'Microsoft', key_path_prefix='HKEY_CURRENT_USER\\Software', |
| 95 |
relative_key_path='Microsoft') |
| 96 |
|
| 97 |
result = find_spec._CheckKeyPath(registry_key, 3) |
| 98 |
self.assertTrue(result) |
| 99 |
|
| 100 |
# Test find specification without key_path_segments. |
| 101 |
find_spec._key_path_segments = None |
| 102 |
result = find_spec._CheckKeyPath(registry_key, 3) |
| 103 |
self.assertFalse(result) |
| 104 |
|
| 105 |
# Test find specification with invalid regular expression. |
| 106 |
find_spec = registry_searcher.FindSpec( |
| 107 |
key_path_regex=['HKEY_CURRENT_USER', 'Software', 'Mi(rosoft']) |
| 108 |
|
| 109 |
registry_key = fake.FakeWinRegistryKey( |
| 110 |
'Microsoft', key_path_prefix='HKEY_CURRENT_USER\\Software', |
| 111 |
relative_key_path='Microsoft') |
| 112 |
|
| 113 |
result = find_spec._CheckKeyPath(registry_key, 3) |
| 114 |
self.assertFalse(result) |
| 115 |
|
| 116 |
def testCompareWithKeyPathSegment(self): |
| 117 |
"""Test the _CompareWithKeyPathSegment function.""" |
| 118 |
find_spec = registry_searcher.FindSpec( |
| 119 |
key_path='HKEY_CURRENT_USER\\Software\\Microsoft') |
| 120 |
|
| 121 |
result = find_spec._CompareWithKeyPathSegment('Microsoft', 2) |
| 122 |
self.assertTrue(result) |
| 123 |
|
| 124 |
result = find_spec._CompareWithKeyPathSegment('Microsoft', 1) |
| 125 |
self.assertFalse(result) |
| 126 |
|
| 127 |
find_spec = registry_searcher.FindSpec( |
| 128 |
key_path='HKEY_CURRENT_USER\\Software\\Bogus') |
| 129 |
|
| 130 |
result = find_spec._CompareWithKeyPathSegment('Microsoft', 2) |
| 131 |
self.assertFalse(result) |
| 132 |
|
| 133 |
def testAtLastKeyPathSegment(self): |
| 134 |
"""Test the AtLastKeyPathSegment function.""" |
| 135 |
find_spec = registry_searcher.FindSpec() |
| 136 |
|
| 137 |
result = find_spec.AtLastKeyPathSegment(2) |
| 138 |
self.assertFalse(result) |
| 139 |
|
| 140 |
find_spec = registry_searcher.FindSpec( |
| 141 |
key_path='HKEY_CURRENT_USER\\Software\\Microsoft') |
| 142 |
|
| 143 |
result = find_spec.AtLastKeyPathSegment(0) |
| 144 |
self.assertFalse(result) |
| 145 |
|
| 146 |
result = find_spec.AtLastKeyPathSegment(2) |
| 147 |
self.assertTrue(result) |
| 148 |
|
| 149 |
result = find_spec.AtLastKeyPathSegment(5) |
| 150 |
self.assertTrue(result) |
| 151 |
|
| 152 |
def testAtMaximumDepth(self): |
| 153 |
"""Tests the AtMaximumDepth function.""" |
| 154 |
find_spec = registry_searcher.FindSpec( |
| 155 |
key_path='HKEY_CURRENT_USER\\Software\\Microsoft') |
| 156 |
|
| 157 |
result = find_spec.AtMaximumDepth(1) |
| 158 |
self.assertFalse(result) |
| 159 |
|
| 160 |
result = find_spec.AtMaximumDepth(5) |
| 161 |
self.assertTrue(result) |
| 162 |
|
| 163 |
def testCompareKeyPath(self): |
| 164 |
"""Test the CompareKeyPath function.""" |
| 165 |
registry_key = fake.FakeWinRegistryKey( |
| 166 |
'Microsoft', key_path_prefix='HKEY_CURRENT_USER\\Software', |
| 167 |
relative_key_path='Microsoft') |
| 168 |
|
| 169 |
find_spec = registry_searcher.FindSpec( |
| 170 |
key_path='HKEY_CURRENT_USER\\Software\\Microsoft') |
| 171 |
|
| 172 |
result = find_spec.CompareKeyPath(registry_key) |
| 173 |
self.assertTrue(result) |
| 174 |
|
| 175 |
find_spec = registry_searcher.FindSpec( |
| 176 |
key_path='HKEY_CURRENT_USER\\Software\\Bogus') |
| 177 |
|
| 178 |
result = find_spec.CompareKeyPath(registry_key) |
| 179 |
self.assertFalse(result) |
| 180 |
|
| 181 |
def testCompareNameWithKeyPathSegment(self): |
| 182 |
"""Test the CompareNameWithKeyPathSegment function.""" |
| 183 |
registry_key = fake.FakeWinRegistryKey( |
| 184 |
'Microsoft', key_path_prefix='HKEY_CURRENT_USER\\Software', |
| 185 |
relative_key_path='Microsoft') |
| 186 |
|
| 187 |
find_spec = registry_searcher.FindSpec( |
| 188 |
key_path='HKEY_CURRENT_USER\\Software\\Microsoft') |
| 189 |
|
| 190 |
result = find_spec.CompareNameWithKeyPathSegment(registry_key, 2) |
| 191 |
self.assertTrue(result) |
| 192 |
|
| 193 |
result = find_spec.CompareNameWithKeyPathSegment(registry_key, 1) |
| 194 |
self.assertFalse(result) |
| 195 |
|
| 196 |
find_spec = registry_searcher.FindSpec( |
| 197 |
key_path='HKEY_CURRENT_USER\\Software\\Bogus') |
| 198 |
|
| 199 |
result = find_spec.CompareNameWithKeyPathSegment(registry_key, 2) |
| 200 |
self.assertFalse(result) |
| 201 |
|
| 202 |
def testHasKeyPath(self): |
| 203 |
"""Test the HasKeyPath function.""" |
| 204 |
find_spec = registry_searcher.FindSpec() |
| 205 |
|
| 206 |
result = find_spec.HasKeyPath() |
| 207 |
self.assertFalse(result) |
| 208 |
|
| 209 |
find_spec = registry_searcher.FindSpec( |
| 210 |
key_path='HKEY_CURRENT_USER\\Software\\Microsoft') |
| 211 |
|
| 212 |
result = find_spec.HasKeyPath() |
| 213 |
self.assertTrue(result) |
| 214 |
|
| 215 |
def testIsLastKeyPathSegment(self): |
| 216 |
"""Test the IsLastKeyPathSegment function.""" |
| 217 |
find_spec = registry_searcher.FindSpec( |
| 218 |
key_path='HKEY_CURRENT_USER\\Software\\Microsoft') |
| 219 |
|
| 220 |
result = find_spec.IsLastKeyPathSegment(0) |
| 221 |
self.assertFalse(result) |
| 222 |
|
| 223 |
result = find_spec.IsLastKeyPathSegment(2) |
| 224 |
self.assertTrue(result) |
| 225 |
|
| 226 |
result = find_spec.IsLastKeyPathSegment(5) |
| 227 |
self.assertFalse(result) |
| 228 |
|
| 229 |
def testMatches(self): |
| 230 |
"""Tests the Matches function.""" |
| 231 |
find_spec = registry_searcher.FindSpec( |
| 232 |
key_path='HKEY_CURRENT_USER\\Software\\Microsoft') |
| 233 |
|
| 234 |
registry_key = fake.FakeWinRegistryKey( |
| 235 |
'Microsoft', key_path_prefix='HKEY_CURRENT_USER\\Software', |
| 236 |
relative_key_path='Microsoft') |
| 237 |
|
| 238 |
result = find_spec.Matches(registry_key, 3) |
| 239 |
self.assertEqual(result, (True, True)) |
| 240 |
|
| 241 |
result = find_spec.Matches(registry_key, 1) |
| 242 |
self.assertEqual(result, (False, False)) |
| 243 |
|
| 244 |
result = find_spec.Matches(registry_key, 0) |
| 245 |
self.assertEqual(result, (False, True)) |
| 246 |
|
| 247 |
# Test find specification without key_path_segments. |
| 248 |
find_spec._key_path_segments = None |
| 249 |
result = find_spec.Matches(registry_key, 3) |
| 250 |
self.assertEqual(result, (True, None)) |
| 251 |
|
| 252 |
|
| 253 |
class WinRegistrySearcherTest(test_lib.BaseTestCase): |
| 254 |
"""Tests for the Windows Registry searcher.""" |
| 255 |
|
| 256 |
# pylint: disable=protected-access |
| 257 |
|
| 258 |
def testInitialize(self): |
| 259 |
"""Tests the __init__ function.""" |
| 260 |
with self.assertRaises(ValueError): |
| 261 |
registry_searcher.WinRegistrySearcher(None) |
| 262 |
|
| 263 |
# TODO: add tests for _FindInKey |
| 264 |
|
| 265 |
def testFind(self): |
| 266 |
"""Tests the Find function.""" |
| 267 |
test_path = self._GetTestFilePath(['SYSTEM']) |
| 268 |
self._SkipIfPathNotExists(test_path) |
| 269 |
|
| 270 |
win_registry = registry.WinRegistry( |
| 271 |
registry_file_reader=test_registry.TestREGFWinRegistryFileReader()) |
| 272 |
win_registry.OpenAndMapFile(test_path) |
| 273 |
|
| 274 |
searcher = registry_searcher.WinRegistrySearcher(win_registry) |
| 275 |
|
| 276 |
# Test with key path. |
| 277 |
find_spec = registry_searcher.FindSpec( |
| 278 |
key_path='HKEY_LOCAL_MACHINE\\System\\ControlSet001') |
| 279 |
|
| 280 |
expected_key_paths = ['HKEY_LOCAL_MACHINE\\System\\ControlSet001'] |
| 281 |
key_paths = list(searcher.Find(find_specs=[find_spec])) |
| 282 |
self.assertEqual(key_paths, expected_key_paths) |
| 283 |
|
| 284 |
# Test with key path glob. |
| 285 |
find_spec = registry_searcher.FindSpec( |
| 286 |
key_path_glob='HKEY_LOCAL_MACHINE\\System\\ControlSet001\\*') |
| 287 |
|
| 288 |
expected_key_paths = [ |
| 289 |
'HKEY_LOCAL_MACHINE\\System\\ControlSet001\\Control', |
| 290 |
'HKEY_LOCAL_MACHINE\\System\\ControlSet001\\Enum', |
| 291 |
'HKEY_LOCAL_MACHINE\\System\\ControlSet001\\Hardware Profiles', |
| 292 |
'HKEY_LOCAL_MACHINE\\System\\ControlSet001\\Policies', |
| 293 |
'HKEY_LOCAL_MACHINE\\System\\ControlSet001\\Services'] |
| 294 |
key_paths = list(searcher.Find(find_specs=[find_spec])) |
| 295 |
self.assertEqual(key_paths, expected_key_paths) |
| 296 |
|
| 297 |
# Test with key path regular expression. |
| 298 |
find_spec = registry_searcher.FindSpec( |
| 299 |
key_path_regex=['HKEY_LOCAL_MACHINE', 'System', 'ControlSet001', '.*']) |
| 300 |
|
| 301 |
expected_key_paths = [ |
| 302 |
'HKEY_LOCAL_MACHINE\\System\\ControlSet001\\Control', |
| 303 |
'HKEY_LOCAL_MACHINE\\System\\ControlSet001\\Enum', |
| 304 |
'HKEY_LOCAL_MACHINE\\System\\ControlSet001\\Hardware Profiles', |
| 305 |
'HKEY_LOCAL_MACHINE\\System\\ControlSet001\\Policies', |
| 306 |
'HKEY_LOCAL_MACHINE\\System\\ControlSet001\\Services'] |
| 307 |
key_paths = list(searcher.Find(find_specs=[find_spec])) |
| 308 |
self.assertEqual(key_paths, expected_key_paths) |
| 309 |
|
| 310 |
# Test with CurrentControlSet. |
| 311 |
find_spec = registry_searcher.FindSpec( |
| 312 |
key_path_glob='HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\*') |
| 313 |
|
| 314 |
expected_key_paths = [ |
| 315 |
'HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control', |
| 316 |
'HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Enum', |
| 317 |
'HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Hardware Profiles', |
| 318 |
'HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Policies', |
| 319 |
'HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Services'] |
| 320 |
key_paths = list(searcher.Find(find_specs=[find_spec])) |
| 321 |
self.assertEqual(key_paths, expected_key_paths) |
| 322 |
|
| 323 |
# Test without find specifications. |
| 324 |
win_registry = registry.WinRegistry( |
| 325 |
registry_file_reader=test_registry.TestREGFWinRegistryFileReader()) |
| 326 |
win_registry.OpenAndMapFile(test_path) |
| 327 |
|
| 328 |
searcher = registry_searcher.WinRegistrySearcher(win_registry) |
| 329 |
key_paths = list(searcher.Find()) |
| 330 |
self.assertEqual(len(key_paths), 31353) |
| 331 |
|
| 332 |
def testGetKeyByPath(self): |
| 333 |
"""Tests the GetKeyByPath function.""" |
| 334 |
test_path = self._GetTestFilePath(['SYSTEM']) |
| 335 |
self._SkipIfPathNotExists(test_path) |
| 336 |
|
| 337 |
win_registry = registry.WinRegistry( |
| 338 |
registry_file_reader=test_registry.TestREGFWinRegistryFileReader()) |
| 339 |
win_registry.OpenAndMapFile(test_path) |
| 340 |
|
| 341 |
searcher = registry_searcher.WinRegistrySearcher(win_registry) |
| 342 |
|
| 343 |
registry_key = searcher.GetKeyByPath( |
| 344 |
'HKEY_LOCAL_MACHINE\\System\\ControlSet001\\Control') |
| 345 |
self.assertIsNotNone(registry_key) |
| 346 |
|
| 347 |
registry_key = searcher.GetKeyByPath( |
| 348 |
'HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control') |
| 349 |
self.assertIsNotNone(registry_key) |
| 350 |
|
| 351 |
def testSplitKeyPath(self): |
| 352 |
"""Tests the SplitKeyPath function.""" |
| 353 |
test_path = self._GetTestFilePath(['SYSTEM']) |
| 354 |
self._SkipIfPathNotExists(test_path) |
| 355 |
|
| 356 |
win_registry = registry.WinRegistry( |
| 357 |
registry_file_reader=test_registry.TestREGFWinRegistryFileReader()) |
| 358 |
win_registry.OpenAndMapFile(test_path) |
| 359 |
|
| 360 |
searcher = registry_searcher.WinRegistrySearcher(win_registry) |
| 361 |
|
| 362 |
path_segments = searcher.SplitKeyPath( |
| 363 |
'HKEY_LOCAL_MACHINE\\System\\ControlSet001\\Control') |
| 364 |
self.assertEqual(len(path_segments), 4) |
| 365 |
|
| 366 |
|
| 367 |
if __name__ == '__main__': |
| 368 |
unittest.main() |