Debian Codegraph @

tests/creg.py

raw
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 """Tests for the Windows 9x/Me Registry (CREG) back-end."""
4
5 import unittest
6
7 from dfwinreg import errors
8 from dfwinreg import creg
9
10 from tests import test_lib
11
12
13 class FakePyCREGKey(object):
14 """Fake pycreg key for testing."""
15
16 def __init__(self):
17 """Initializes a fake pycreg key."""
18 super(FakePyCREGKey, 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 pycreg.key: sub key, which will be None for testing.
36 """
37 return None
38
39
40 class FakePyCREGValue(object):
41 """Fake pycreg 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 pycreg value.
50
51 Args:
52 name (Optional[str]): name of the value.
53 value_type (Optional[str]): value type.
54 """
55 super(FakePyCREGValue, 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 CREGWinRegistryFileTest(test_lib.BaseTestCase):
68 """Tests for the CREG Windows Registry file."""
69
70 def testOpenClose(self):
71 """Tests the Open and Close functions."""
72 test_path = self._GetTestFilePath(['USER.DAT'])
73 self._SkipIfPathNotExists(test_path)
74
75 registry_file = creg.CREGWinRegistryFile()
76
77 with open(test_path, 'rb') as file_object:
78 registry_file.Open(file_object)
79 registry_file.Close()
80
81 def testGetRootKey(self):
82 """Tests the GetRootKey function."""
83 dat_test_path = self._GetTestFilePath(['USER.DAT'])
84 self._SkipIfPathNotExists(dat_test_path)
85
86 registry_file = creg.CREGWinRegistryFile()
87
88 with open(dat_test_path, 'rb') as file_object:
89 registry_file.Open(file_object)
90
91 try:
92 registry_key = registry_file.GetRootKey()
93 self.assertIsNotNone(registry_key)
94 self.assertEqual(registry_key.path, '\\')
95
96 finally:
97 registry_file.Close()
98
99 def testGetKeyByPath(self):
100 """Tests the GetKeyByPath function."""
101 test_path = self._GetTestFilePath(['USER.DAT'])
102 self._SkipIfPathNotExists(test_path)
103
104 registry_file = creg.CREGWinRegistryFile()
105
106 with open(test_path, 'rb') as file_object:
107 registry_file.Open(file_object)
108
109 try:
110 key_path = '\\'
111 registry_key = registry_file.GetKeyByPath(key_path)
112 self.assertIsNotNone(registry_key)
113 self.assertEqual(registry_key.path, key_path)
114
115 key_path = '\\Software'
116 registry_key = registry_file.GetKeyByPath(key_path)
117 self.assertIsNotNone(registry_key)
118 self.assertEqual(registry_key.path, key_path)
119
120 key_path = '\\Bogus'
121 registry_key = registry_file.GetKeyByPath(key_path)
122 self.assertIsNone(registry_key)
123
124 key_path = 'Bogus'
125 registry_key = registry_file.GetKeyByPath(key_path)
126 self.assertIsNone(registry_key)
127
128 finally:
129 registry_file.Close()
130
131 def testRecurseKeys(self):
132 """Tests the RecurseKeys function."""
133 dat_test_path = self._GetTestFilePath(['USER.DAT'])
134 self._SkipIfPathNotExists(dat_test_path)
135
136 registry_file = creg.CREGWinRegistryFile()
137
138 with open(dat_test_path, 'rb') as file_object:
139 registry_file.Open(file_object)
140
141 try:
142 registry_keys = list(registry_file.RecurseKeys())
143
144 finally:
145 registry_file.Close()
146
147 self.assertEqual(len(registry_keys), 788)
148
149
150 class CREGWinRegistryKeyTest(test_lib.BaseTestCase):
151 """Tests for the CREG Windows Registry key."""
152
153 # pylint: disable=protected-access
154
155 def testProperties(self):
156 """Tests the properties functions."""
157 test_path = self._GetTestFilePath(['USER.DAT'])
158 self._SkipIfPathNotExists(test_path)
159
160 registry_file = creg.CREGWinRegistryFile()
161
162 with open(test_path, 'rb') as file_object:
163 registry_file.Open(file_object)
164
165 try:
166 key_path = '\\Software'
167 registry_key = registry_file.GetKeyByPath(key_path)
168 self.assertIsNotNone(registry_key)
169 self.assertIsNone(registry_key.class_name)
170 self.assertEqual(registry_key.name, 'Software')
171 self.assertEqual(registry_key.number_of_subkeys, 1)
172 self.assertEqual(registry_key.number_of_values, 0)
173 self.assertEqual(registry_key.offset, 27948)
174 self.assertEqual(registry_key.path, key_path)
175
176 self.assertIsNotNone(registry_key.last_written_time)
177
178 date_time_string = registry_key.last_written_time.CopyToDateTimeString()
179 self.assertEqual(date_time_string, 'Not set')
180
181 registry_key._pycreg_key = FakePyCREGKey()
182 self.assertIsNotNone(registry_key.last_written_time)
183
184 date_time_string = registry_key.last_written_time.CopyToDateTimeString()
185 self.assertEqual(date_time_string, 'Not set')
186
187 finally:
188 registry_file.Close()
189
190 def testGetSubkeyByIndex(self):
191 """Tests the GetSubkeyByIndex function."""
192 test_path = self._GetTestFilePath(['USER.DAT'])
193 self._SkipIfPathNotExists(test_path)
194
195 registry_file = creg.CREGWinRegistryFile(
196 key_path_prefix='HKEY_CURRENT_USER')
197
198 with open(test_path, 'rb') as file_object:
199 registry_file.Open(file_object)
200
201 try:
202 registry_key = registry_file.GetRootKey()
203
204 key_name = '.DEFAULT'
205 sub_registry_key = registry_key.GetSubkeyByIndex(0)
206 self.assertIsNotNone(sub_registry_key)
207 self.assertEqual(sub_registry_key.name, key_name)
208
209 expected_key_path = 'HKEY_CURRENT_USER\\.DEFAULT'
210 self.assertEqual(sub_registry_key.path, expected_key_path)
211
212 with self.assertRaises(IndexError):
213 registry_key.GetSubkeyByIndex(-1)
214
215 finally:
216 registry_file.Close()
217
218 def testGetSubkeyByName(self):
219 """Tests the GetSubkeyByName function."""
220 test_path = self._GetTestFilePath(['USER.DAT'])
221 self._SkipIfPathNotExists(test_path)
222
223 registry_file = creg.CREGWinRegistryFile(
224 key_path_prefix='HKEY_CURRENT_USER')
225
226 with open(test_path, 'rb') as file_object:
227 registry_file.Open(file_object)
228
229 try:
230 registry_key = registry_file.GetRootKey()
231
232 key_name = 'Software'
233 sub_registry_key = registry_key.GetSubkeyByName(key_name)
234 self.assertIsNotNone(sub_registry_key)
235 self.assertEqual(sub_registry_key.name, key_name)
236
237 expected_key_path = 'HKEY_CURRENT_USER\\Software'
238 self.assertEqual(sub_registry_key.path, expected_key_path)
239
240 key_name = 'Bogus'
241 sub_registry_key = registry_key.GetSubkeyByName(key_name)
242 self.assertIsNone(sub_registry_key)
243
244 finally:
245 registry_file.Close()
246
247 def testGetSubkeyByPath(self):
248 """Tests the GetSubkeyByPath function."""
249 test_path = self._GetTestFilePath(['USER.DAT'])
250 self._SkipIfPathNotExists(test_path)
251
252 registry_file = creg.CREGWinRegistryFile(
253 key_path_prefix='HKEY_CURRENT_USER')
254
255 with open(test_path, 'rb') as file_object:
256 registry_file.Open(file_object)
257
258 try:
259 registry_key = registry_file.GetRootKey()
260
261 key_path = 'Software\\Microsoft'
262 sub_registry_key = registry_key.GetSubkeyByPath(key_path)
263 self.assertIsNotNone(sub_registry_key)
264 self.assertEqual(sub_registry_key.name, 'Microsoft')
265
266 expected_key_path = 'HKEY_CURRENT_USER\\Software\\Microsoft'
267 self.assertEqual(sub_registry_key.path, expected_key_path)
268
269 key_path = 'Software\\Bogus'
270 sub_registry_key = registry_key.GetSubkeyByPath(key_path)
271 self.assertIsNone(sub_registry_key)
272
273 finally:
274 registry_file.Close()
275
276 def testGetSubkeys(self):
277 """Tests the GetSubkeys function."""
278 test_path = self._GetTestFilePath(['USER.DAT'])
279 self._SkipIfPathNotExists(test_path)
280
281 registry_file = creg.CREGWinRegistryFile()
282
283 with open(test_path, 'rb') as file_object:
284 registry_file.Open(file_object)
285
286 try:
287 key_path = '\\Software'
288 registry_key = registry_file.GetKeyByPath(key_path)
289
290 sub_registry_keys = list(registry_key.GetSubkeys())
291 self.assertEqual(len(sub_registry_keys), 1)
292
293 finally:
294 registry_file.Close()
295
296 def testGetValueByName(self):
297 """Tests the GetValueByName function."""
298 test_path = self._GetTestFilePath(['USER.DAT'])
299 self._SkipIfPathNotExists(test_path)
300
301 registry_file = creg.CREGWinRegistryFile()
302
303 with open(test_path, 'rb') as file_object:
304 registry_file.Open(file_object)
305
306 try:
307 registry_key = registry_file.GetKeyByPath(
308 '\\.DEFAULT\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer'
309 '\\MountPoints\\A\\_Autorun')
310
311 value_name = 'LastUpdate'
312 registry_value = registry_key.GetValueByName(value_name)
313 self.assertIsNotNone(registry_value)
314 self.assertEqual(registry_value.name, value_name)
315
316 value_name = 'Bogus'
317 registry_value = registry_key.GetValueByName(value_name)
318 self.assertIsNone(registry_value)
319
320 # Test retrieving the default (or nameless) value.
321 registry_key = registry_file.GetKeyByPath(
322 '\\.DEFAULT\\AppEvents\\EventLabels\\.Default')
323
324 registry_value = registry_key.GetValueByName('')
325 self.assertIsNotNone(registry_value)
326 self.assertIsNone(registry_value.name)
327
328 finally:
329 registry_file.Close()
330
331 def testGetValues(self):
332 """Tests the GetValues function."""
333 test_path = self._GetTestFilePath(['USER.DAT'])
334 self._SkipIfPathNotExists(test_path)
335
336 registry_file = creg.CREGWinRegistryFile()
337
338 with open(test_path, 'rb') as file_object:
339 registry_file.Open(file_object)
340
341 try:
342 key_path = (
343 '\\.DEFAULT\\Software\\Microsoft\\Windows\\CurrentVersion'
344 '\\Explorer\\MountPoints\\A\\_Autorun')
345 registry_key = registry_file.GetKeyByPath(key_path)
346
347 values = list(registry_key.GetValues())
348 self.assertEqual(len(values), 3)
349
350 finally:
351 registry_file.Close()
352
353 def testRecurseKeys(self):
354 """Tests the RecurseKeys function."""
355 test_path = self._GetTestFilePath(['USER.DAT'])
356 self._SkipIfPathNotExists(test_path)
357
358 registry_file = creg.CREGWinRegistryFile()
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 registry_keys = list(registry_key.RecurseKeys())
367
368 finally:
369 registry_file.Close()
370
371 self.assertEqual(len(registry_keys), 6)
372
373
374 class CREGWinRegistryValueTest(test_lib.BaseTestCase):
375 """Tests for the CREG Windows Registry value."""
376
377 # pylint: disable=protected-access
378
379 _EXPECTED_BINARY_DATA_VALUE = bytes(bytearray([
380 0x04, 0x00, 0x00, 0x00, 0x1f, 0x30, 0x8c, 0xd3, 0x01, 0x00, 0x00, 0x00,
381 0x0d, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00,
382 0x12, 0x00, 0x00, 0x00, 0xf4, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
383 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xbc, 0x02, 0x00, 0x00,
384 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x72, 0x69, 0x61,
385 0x6c, 0x00, 0x17, 0x01, 0x50, 0x13, 0x78, 0x45, 0x00, 0x00, 0xc8, 0x45,
386 0x00, 0x00, 0x48, 0x7e, 0x00, 0x00, 0x30, 0x7e, 0x63, 0x00, 0x00, 0x00,
387 0x00, 0x00, 0x01, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00,
388 0xf7, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
389 0x00, 0x00, 0x00, 0x00, 0x90, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
390 0x00, 0x00, 0x00, 0x00, 0x4d, 0x53, 0x20, 0x53, 0x61, 0x6e, 0x73, 0x20,
391 0x53, 0x65, 0x72, 0x69, 0x66, 0x00, 0xc8, 0x45, 0x00, 0x00, 0x48, 0x7e,
392 0x00, 0x00, 0x30, 0x7e, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
393 0x12, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0xf5, 0xff, 0xff, 0xff,
394 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
395 0x90, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
396 0x4d, 0x53, 0x20, 0x53, 0x61, 0x6e, 0x73, 0x20, 0x53, 0x65, 0x72, 0x69,
397 0x66, 0x00, 0xc8, 0x45, 0x00, 0x00, 0x48, 0x7e, 0x00, 0x00, 0x30, 0x7e,
398 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0xf5, 0xff, 0xff, 0xff,
399 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
400 0x90, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
401 0x4d, 0x53, 0x20, 0x53, 0x61, 0x6e, 0x73, 0x20, 0x53, 0x65, 0x72, 0x69,
402 0x66, 0x00, 0xc8, 0x45, 0x00, 0x00, 0x48, 0x7e, 0x00, 0x00, 0x30, 0x7e,
403 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0xf5, 0xff, 0xff, 0xff,
404 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
405 0x90, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
406 0x4d, 0x53, 0x20, 0x53, 0x61, 0x6e, 0x73, 0x20, 0x53, 0x65, 0x72, 0x69,
407 0x66, 0x00, 0xc8, 0x45, 0x00, 0x00, 0x48, 0x7e, 0x00, 0x00, 0x30, 0x7e,
408 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0xf8, 0xff, 0xff, 0xff,
409 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
410 0x90, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
411 0x4d, 0x53, 0x20, 0x53, 0x61, 0x6e, 0x73, 0x20, 0x53, 0x65, 0x72, 0x69,
412 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
413 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe1, 0xe0, 0xd2, 0x02,
414 0x42, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x8d, 0x89, 0x61, 0x00,
415 0xc2, 0xbf, 0xa5, 0x02, 0xff, 0xff, 0xff, 0x02, 0x00, 0x00, 0x00, 0x02,
416 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0xe1, 0xe0, 0xd2, 0x02,
417 0xc2, 0xbf, 0xa5, 0x02, 0xc2, 0xbf, 0xa5, 0x02, 0xe1, 0xe0, 0xd2, 0x02,
418 0x8d, 0x89, 0x61, 0x02, 0xff, 0xff, 0xff, 0x02, 0xc2, 0xbf, 0xa5, 0x02,
419 0x8d, 0x89, 0x61, 0x02, 0x8d, 0x89, 0x61, 0x02, 0x00, 0x00, 0x00, 0x02,
420 0xe1, 0xe0, 0xd2, 0x02, 0xe1, 0xe0, 0xd2, 0x02, 0x00, 0x00, 0x00, 0x02,
421 0xc2, 0xbf, 0xa5, 0x02, 0x80, 0x00, 0x00, 0x02, 0xe1, 0xe0, 0xd2, 0x02,
422 0xba, 0xb7, 0x9a, 0x00, 0x00, 0x00, 0xff, 0x00, 0x80, 0x00, 0x00, 0x00,
423 0x8d, 0x89, 0x61, 0x00]))
424
425 def testProperties(self):
426 """Tests the properties functions on a USER.DAT file."""
427 test_path = self._GetTestFilePath(['USER.DAT'])
428 self._SkipIfPathNotExists(test_path)
429
430 registry_file = creg.CREGWinRegistryFile()
431
432 with open(test_path, 'rb') as file_object:
433 registry_file.Open(file_object)
434
435 try:
436 registry_key = registry_file.GetKeyByPath(
437 '\\.DEFAULT\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer'
438 '\\MountPoints\\A\\_Autorun')
439 value_name = 'LastUpdate'
440 registry_value = registry_key.GetValueByName(value_name)
441 expected_data = b'\x8e\x7d\x02\x00'
442
443 self.assertIsNotNone(registry_value)
444 self.assertEqual(registry_value.data_type, 4)
445 self.assertEqual(registry_value.data_type_string, 'REG_DWORD_LE')
446 self.assertEqual(registry_value.GetDataAsObject(), 163214)
447 self.assertEqual(registry_value.name, value_name)
448 self.assertEqual(registry_value.offset, 90258)
449 self.assertEqual(registry_value.data, expected_data)
450
451 registry_key = registry_file.GetKeyByPath(
452 '\\.DEFAULT\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer'
453 '\\Shell Folders')
454 value_name = 'Personal'
455 registry_value = registry_key.GetValueByName(value_name)
456 expected_data = (
457 b'\x43\x3a\x5c\x4d\x79\x20\x44\x6f\x63\x75\x6d\x65\x6e\x74\x73')
458
459 self.assertIsNotNone(registry_value)
460 self.assertEqual(registry_value.data_type, 1)
461 self.assertEqual(registry_value.data_type_string, 'REG_SZ')
462 self.assertEqual(registry_value.GetDataAsObject(), 'C:\\My Documents')
463 self.assertEqual(registry_value.name, value_name)
464 self.assertEqual(registry_value.offset, 57547)
465 self.assertEqual(registry_value.data, expected_data)
466
467 registry_key = registry_file.GetKeyByPath(
468 '\\.DEFAULT\\Control Panel\\Appearance\\Schemes')
469 value_name = 'Brick'
470 registry_value = registry_key.GetValueByName(value_name)
471 expected_data = self._EXPECTED_BINARY_DATA_VALUE
472
473 self.assertIsNotNone(registry_value)
474 self.assertEqual(registry_value.data_type, 3)
475 self.assertEqual(registry_value.data_type_string, 'REG_BINARY')
476 self.assertEqual(registry_value.GetDataAsObject(), expected_data)
477 self.assertEqual(registry_value.name, value_name)
478 self.assertEqual(registry_value.offset, 30271)
479 self.assertEqual(registry_value.data, expected_data)
480
481 registry_value._pycreg_value = FakePyCREGValue()
482 with self.assertRaises(errors.WinRegistryValueError):
483 _ = registry_value.data
484
485 finally:
486 registry_file.Close()
487
488 def testGetDataAsObject(self):
489 """Tests the GetDataAsObject function."""
490 test_path = self._GetTestFilePath(['USER.DAT'])
491 self._SkipIfPathNotExists(test_path)
492
493 registry_file = creg.CREGWinRegistryFile()
494
495 with open(test_path, 'rb') as file_object:
496 registry_file.Open(file_object)
497
498 try:
499 registry_key = registry_file.GetKeyByPath(
500 '\\.DEFAULT\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer'
501 '\\MountPoints\\A\\_Autorun')
502 registry_value = registry_key.GetValueByName('LastUpdate')
503
504 data_object = registry_value.GetDataAsObject()
505 self.assertEqual(data_object, 163214)
506
507 registry_value._pycreg_value = FakePyCREGValue(value_type='REG_SZ')
508 with self.assertRaises(errors.WinRegistryValueError):
509 registry_value.GetDataAsObject()
510
511 registry_value._pycreg_value = FakePyCREGValue(
512 value_type='REG_DWORD_LE')
513 with self.assertRaises(errors.WinRegistryValueError):
514 registry_value.GetDataAsObject()
515
516 registry_value._pycreg_value = FakePyCREGValue(
517 value_type='REG_MULTI_SZ')
518 with self.assertRaises(errors.WinRegistryValueError):
519 registry_value.GetDataAsObject()
520
521 finally:
522 registry_file.Close()
523
524
525 if __name__ == '__main__':
526 unittest.main()