File: analyzer.py

package info (click to toggle)
dfvfs 20201219-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 284,900 kB
  • sloc: python: 30,025; vhdl: 1,921; sh: 465; makefile: 16
file content (362 lines) | stat: -rw-r--r-- 14,541 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Tests for the format analyzer."""

from __future__ import unicode_literals

import unittest

from dfvfs.analyzer import analyzer
from dfvfs.analyzer import analyzer_helper
from dfvfs.analyzer import specification
from dfvfs.lib import definitions
from dfvfs.path import gzip_path_spec
from dfvfs.path import os_path_spec
from dfvfs.path import qcow_path_spec
from dfvfs.path import raw_path_spec
from dfvfs.path import tsk_partition_path_spec
from dfvfs.path import vshadow_path_spec

from tests import test_lib as shared_test_lib


class TestAnalyzerHelper(analyzer_helper.AnalyzerHelper):
  """Analyzer helper for testing."""

  FORMAT_CATEGORIES = frozenset([
      definitions.FORMAT_CATEGORY_VOLUME_SYSTEM])

  TYPE_INDICATOR = 'test'

  # pylint: disable=redundant-returns-doc,unused-argument
  def AnalyzeFileObject(self, file_object):
    """Retrieves the format specification.

    This is the fall through implementation that raises a RuntimeError.

    Args:
      file_object (FileIO): file-like object.

    Returns:
      str: type indicator if the file-like object contains a supported format
          or None otherwise.
    """
    return None


class AnalyzerTest(shared_test_lib.BaseTestCase):
  """Format analyzer tests."""

  # pylint: disable=protected-access

  def testFlushCache(self):
    """Tests the _FlushCache function."""
    analyzer.Analyzer._FlushCache(definitions.FORMAT_CATEGORIES)

  def testGetSignatureScanner(self):
    """Tests the _GetSignatureScanner function."""
    specification_store = specification.FormatSpecificationStore()

    signature_scanner = analyzer.Analyzer._GetSignatureScanner(
        specification_store)
    self.assertIsNotNone(signature_scanner)

  def testGetSpecificationStore(self):
    """Tests the _GetSpecificationStore function."""
    specification_store = analyzer.Analyzer._GetSpecificationStore(
        definitions.FORMAT_CATEGORY_VOLUME_SYSTEM)
    self.assertIsNotNone(specification_store)

  # TODO: add tests for _GetTypeIndicators

  def testHelperRegistration(self):
    """Tests the DeregisterHelper and RegisterHelper functions."""
    test_helper = TestAnalyzerHelper()

    number_of_helpers = len(analyzer.Analyzer._analyzer_helpers)

    analyzer.Analyzer.RegisterHelper(test_helper)
    self.assertEqual(
        len(analyzer.Analyzer._analyzer_helpers), number_of_helpers + 1)

    with self.assertRaises(KeyError):
      analyzer.Analyzer.RegisterHelper(test_helper)

    analyzer.Analyzer.DeregisterHelper(test_helper)
    self.assertEqual(
        len(analyzer.Analyzer._analyzer_helpers), number_of_helpers)

  def testGetArchiveTypeIndicatorsTAR(self):
    """Tests the GetArchiveTypeIndicators function on a .tar file."""
    test_file = self._GetTestFilePath(['syslog.tar'])
    self._SkipIfPathNotExists(test_file)

    path_spec = os_path_spec.OSPathSpec(location=test_file)

    expected_type_indicators = [definitions.TYPE_INDICATOR_TAR]
    type_indicators = analyzer.Analyzer.GetArchiveTypeIndicators(
        path_spec)
    self.assertEqual(type_indicators, expected_type_indicators)

  def testGetArchiveTypeIndicatorsZIP(self):
    """Tests the GetArchiveTypeIndicators function on a .zip file."""
    test_file = self._GetTestFilePath(['syslog.zip'])
    self._SkipIfPathNotExists(test_file)

    path_spec = os_path_spec.OSPathSpec(location=test_file)

    expected_type_indicators = [definitions.TYPE_INDICATOR_ZIP]
    type_indicators = analyzer.Analyzer.GetArchiveTypeIndicators(
        path_spec)
    self.assertEqual(type_indicators, expected_type_indicators)

  def testGetCompressedStreamTypeIndicatorsBZIP2(self):
    """Tests the GetCompressedStreamTypeIndicators function on a .bz2 file."""
    test_file = self._GetTestFilePath(['syslog.bz2'])
    self._SkipIfPathNotExists(test_file)

    path_spec = os_path_spec.OSPathSpec(location=test_file)

    expected_type_indicators = [definitions.TYPE_INDICATOR_BZIP2]
    type_indicators = analyzer.Analyzer.GetCompressedStreamTypeIndicators(
        path_spec)
    self.assertEqual(type_indicators, expected_type_indicators)

  def testGetCompressedStreamTypeIndicatorsGZIP(self):
    """Tests the GetCompressedStreamTypeIndicators function on a .gz file."""
    test_file = self._GetTestFilePath(['syslog.gz'])
    self._SkipIfPathNotExists(test_file)

    path_spec = os_path_spec.OSPathSpec(location=test_file)

    expected_type_indicators = [definitions.TYPE_INDICATOR_GZIP]
    type_indicators = analyzer.Analyzer.GetCompressedStreamTypeIndicators(
        path_spec)
    self.assertEqual(type_indicators, expected_type_indicators)

  def testGetCompressedStreamTypeIndicatorsXZ(self):
    """Tests the GetCompressedStreamTypeIndicators function on a .xz file."""
    test_file = self._GetTestFilePath(['syslog.xz'])
    self._SkipIfPathNotExists(test_file)

    path_spec = os_path_spec.OSPathSpec(location=test_file)

    expected_type_indicators = [definitions.TYPE_INDICATOR_XZ]
    type_indicators = analyzer.Analyzer.GetCompressedStreamTypeIndicators(
        path_spec)
    self.assertEqual(type_indicators, expected_type_indicators)

  def testGetCompressedArchiveTypeIndicators(self):
    """Tests the GetCompressedStreamTypeIndicators function on a .tgz file."""
    test_file = self._GetTestFilePath(['syslog.tgz'])
    self._SkipIfPathNotExists(test_file)

    path_spec = os_path_spec.OSPathSpec(location=test_file)

    expected_type_indicators = [definitions.TYPE_INDICATOR_GZIP]
    type_indicators = analyzer.Analyzer.GetCompressedStreamTypeIndicators(
        path_spec)
    self.assertEqual(type_indicators, expected_type_indicators)

    path_spec = gzip_path_spec.GzipPathSpec(parent=path_spec)

    expected_type_indicators = [definitions.TYPE_INDICATOR_TAR]
    type_indicators = analyzer.Analyzer.GetArchiveTypeIndicators(
        path_spec)
    self.assertEqual(type_indicators, expected_type_indicators)

  def testGetFileSystemTypeIndicators(self):
    """Tests the GetFileSystemTypeIndicators function on a .qcow2 file."""
    test_file = self._GetTestFilePath(['vsstest.qcow2'])
    self._SkipIfPathNotExists(test_file)

    path_spec = os_path_spec.OSPathSpec(location=test_file)
    path_spec = qcow_path_spec.QCOWPathSpec(parent=path_spec)
    path_spec = vshadow_path_spec.VShadowPathSpec(
        store_index=1, parent=path_spec)

    expected_type_indicators = [definitions.PREFERRED_NTFS_BACK_END]
    type_indicators = analyzer.Analyzer.GetFileSystemTypeIndicators(
        path_spec)
    self.assertEqual(type_indicators, expected_type_indicators)

  def testGetFileSystemTypeIndicatorsEXT2(self):
    """Tests the GetFileSystemTypeIndicators function on an ext2 file system."""
    test_file = self._GetTestFilePath(['ext2.raw'])
    self._SkipIfPathNotExists(test_file)

    path_spec = os_path_spec.OSPathSpec(location=test_file)
    path_spec = raw_path_spec.RawPathSpec(parent=path_spec)

    expected_type_indicators = [definitions.PREFERRED_EXT_BACK_END]
    type_indicators = analyzer.Analyzer.GetFileSystemTypeIndicators(path_spec)
    self.assertEqual(type_indicators, expected_type_indicators)

  def testGetFileSystemTypeIndicatorsHFSPlus(self):
    """Tests the GetFileSystemTypeIndicators function on a HFS+ file system."""
    test_file = self._GetTestFilePath(['hfsplus.raw'])
    self._SkipIfPathNotExists(test_file)

    path_spec = os_path_spec.OSPathSpec(location=test_file)
    path_spec = raw_path_spec.RawPathSpec(parent=path_spec)

    expected_type_indicators = [definitions.PREFERRED_HFS_BACK_END]
    type_indicators = analyzer.Analyzer.GetFileSystemTypeIndicators(path_spec)
    self.assertEqual(type_indicators, expected_type_indicators)

  def testGetFileSystemTypeIndicatorsNTFS(self):
    """Tests the GetFileSystemTypeIndicators function on a NTFS file system."""
    test_file = self._GetTestFilePath(['ntfs.raw'])
    self._SkipIfPathNotExists(test_file)

    path_spec = os_path_spec.OSPathSpec(location=test_file)
    path_spec = raw_path_spec.RawPathSpec(parent=path_spec)

    expected_type_indicators = [definitions.PREFERRED_NTFS_BACK_END]
    type_indicators = analyzer.Analyzer.GetFileSystemTypeIndicators(path_spec)
    self.assertEqual(type_indicators, expected_type_indicators)

  def testGetFileSystemTypeIndicatorsXFS(self):
    """Tests the GetFileSystemTypeIndicators function on an XFS file system."""
    test_file = self._GetTestFilePath(['xfs.raw'])
    self._SkipIfPathNotExists(test_file)

    path_spec = os_path_spec.OSPathSpec(location=test_file)
    path_spec = raw_path_spec.RawPathSpec(parent=path_spec)

    expected_type_indicators = [definitions.TYPE_INDICATOR_XFS]
    type_indicators = analyzer.Analyzer.GetFileSystemTypeIndicators(path_spec)
    self.assertEqual(type_indicators, expected_type_indicators)

  def testGetStorageMediaImageTypeIndicatorsEWF(self):
    """Tests the GetStorageMediaImageTypeIndicator function on a .E01 file."""
    test_file = self._GetTestFilePath(['ext2.E01'])
    self._SkipIfPathNotExists(test_file)

    path_spec = os_path_spec.OSPathSpec(location=test_file)

    expected_type_indicators = [definitions.TYPE_INDICATOR_EWF]
    type_indicators = analyzer.Analyzer.GetStorageMediaImageTypeIndicators(
        path_spec)
    self.assertEqual(type_indicators, expected_type_indicators)

  def testGetStorageMediaImageTypeIndicatorsQCOW(self):
    """Tests the GetStorageMediaImageTypeIndicator function on a .qcow2 file."""
    test_file = self._GetTestFilePath(['ext2.qcow2'])
    self._SkipIfPathNotExists(test_file)

    path_spec = os_path_spec.OSPathSpec(location=test_file)

    expected_type_indicators = [definitions.TYPE_INDICATOR_QCOW]
    type_indicators = analyzer.Analyzer.GetStorageMediaImageTypeIndicators(
        path_spec)
    self.assertEqual(type_indicators, expected_type_indicators)

  def testGetStorageMediaImageTypeIndicatorsVHDI(self):
    """Tests the GetStorageMediaImageTypeIndicator function on .vhd[x] files."""
    test_file = self._GetTestFilePath(['ext2.vhd'])
    self._SkipIfPathNotExists(test_file)

    path_spec = os_path_spec.OSPathSpec(location=test_file)

    expected_type_indicators = [definitions.TYPE_INDICATOR_VHDI]
    type_indicators = analyzer.Analyzer.GetStorageMediaImageTypeIndicators(
        path_spec)
    self.assertEqual(type_indicators, expected_type_indicators)

    test_file = self._GetTestFilePath(['ext2.vhdx'])
    self._SkipIfPathNotExists(test_file)

    path_spec = os_path_spec.OSPathSpec(location=test_file)

    expected_type_indicators = [definitions.TYPE_INDICATOR_VHDI]
    type_indicators = analyzer.Analyzer.GetStorageMediaImageTypeIndicators(
        path_spec)
    self.assertEqual(type_indicators, expected_type_indicators)

  def testGetStorageMediaImageTypeIndicatorsVMDK(self):
    """Tests the GetStorageMediaImageTypeIndicator function on a .vmdk file."""
    test_file = self._GetTestFilePath(['ext2.vmdk'])
    self._SkipIfPathNotExists(test_file)

    path_spec = os_path_spec.OSPathSpec(location=test_file)

    expected_type_indicators = [definitions.TYPE_INDICATOR_VMDK]
    type_indicators = analyzer.Analyzer.GetStorageMediaImageTypeIndicators(
        path_spec)
    self.assertEqual(type_indicators, expected_type_indicators)

  def testGetStorageMediaImageTypeIndicatorsBodyFile(self):
    """Tests the GetStorageMediaImageTypeIndicator function on a bodyfile."""
    test_file = self._GetTestFilePath(['mactime.body'])
    self._SkipIfPathNotExists(test_file)

    path_spec = os_path_spec.OSPathSpec(location=test_file)

    expected_type_indicators = []
    type_indicators = analyzer.Analyzer.GetStorageMediaImageTypeIndicators(
        path_spec)
    self.assertEqual(type_indicators, expected_type_indicators)

  def testGetVolumeSystemTypeIndicatorsTSK(self):
    """Tests the GetVolumeSystemTypeIndicators function on partitions."""
    test_file = self._GetTestFilePath(['mbr.raw'])
    self._SkipIfPathNotExists(test_file)

    path_spec = os_path_spec.OSPathSpec(location=test_file)

    expected_type_indicators = [definitions.TYPE_INDICATOR_TSK_PARTITION]
    type_indicators = analyzer.Analyzer.GetVolumeSystemTypeIndicators(path_spec)
    self.assertEqual(type_indicators, expected_type_indicators)

  def testGetVolumeSystemTypeIndicatorsVSS(self):
    """Tests the GetVolumeSystemTypeIndicators function on a VSS volume."""
    test_file = self._GetTestFilePath(['vsstest.qcow2'])
    self._SkipIfPathNotExists(test_file)

    path_spec = os_path_spec.OSPathSpec(location=test_file)
    path_spec = qcow_path_spec.QCOWPathSpec(parent=path_spec)

    expected_type_indicators = [definitions.TYPE_INDICATOR_VSHADOW]
    type_indicators = analyzer.Analyzer.GetVolumeSystemTypeIndicators(path_spec)
    self.assertEqual(type_indicators, expected_type_indicators)

  def testGetVolumeSystemTypeIndicatorsBDE(self):
    """Tests the GetVolumeSystemTypeIndicators function on a BDE ToGo drive."""
    test_file = self._GetTestFilePath(['bdetogo.raw'])
    self._SkipIfPathNotExists(test_file)

    path_spec = os_path_spec.OSPathSpec(location=test_file)

    expected_type_indicators = [definitions.TYPE_INDICATOR_BDE]
    type_indicators = analyzer.Analyzer.GetVolumeSystemTypeIndicators(path_spec)
    self.assertEqual(type_indicators, expected_type_indicators)

  def testGetVolumeSystemTypeIndicatorsFVDE(self):
    """Tests the GetVolumeSystemTypeIndicators function on a FVDE volume."""
    test_file = self._GetTestFilePath(['fvdetest.qcow2'])
    self._SkipIfPathNotExists(test_file)

    path_spec = os_path_spec.OSPathSpec(location=test_file)
    path_spec = qcow_path_spec.QCOWPathSpec(parent=path_spec)
    path_spec = tsk_partition_path_spec.TSKPartitionPathSpec(
        location='/p1', parent=path_spec)

    expected_type_indicators = [definitions.TYPE_INDICATOR_FVDE]
    type_indicators = analyzer.Analyzer.GetVolumeSystemTypeIndicators(path_spec)
    self.assertEqual(type_indicators, expected_type_indicators)

  def testGetVolumeSystemTypeIndicatorsLUKS(self):
    """Tests the GetVolumeSystemTypeIndicators function on a LUKS volume."""
    test_file = self._GetTestFilePath(['luks1.raw'])
    self._SkipIfPathNotExists(test_file)

    path_spec = os_path_spec.OSPathSpec(location=test_file)

    expected_type_indicators = [definitions.TYPE_INDICATOR_LUKSDE]
    type_indicators = analyzer.Analyzer.GetVolumeSystemTypeIndicators(path_spec)
    self.assertEqual(type_indicators, expected_type_indicators)


if __name__ == '__main__':
  unittest.main()