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 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Tests for the image export CLI tool."""
from __future__ import unicode_literals
import io
import os
import unittest
from dfvfs.lib import definitions as dfvfs_definitions
from dfvfs.path import factory as path_spec_factory
from dfvfs.resolver import resolver as path_spec_resolver
from plaso.cli import image_export_tool
from plaso.lib import errors
from tests import test_lib as shared_test_lib
from tests.cli import test_lib
class ImageExportToolTest(test_lib.CLIToolTestCase):
"""Tests for the image export CLI tool."""
# pylint: disable=protected-access
def _GetTestScanNode(self, scan_context):
"""Retrieves the scan node for testing.
Retrieves the first scan node, from the root upwards, with more or less
than 1 sub node.
Args:
scan_context (dfvfs.SourceScanContext): scan context.
Returns:
dfvfs.SourceScanNode: scan node.
"""
scan_node = scan_context.GetRootScanNode()
while len(scan_node.sub_nodes) == 1:
scan_node = scan_node.sub_nodes[0]
return scan_node
def _RecursiveList(self, path):
"""Recursively lists a file or directory.
Args:
path (str): path of the file or directory to list.
Returns:
list[str]: names of files and sub directories within the path.
"""
results = []
for sub_path, _, files in os.walk(path):
if sub_path != path:
results.append(sub_path)
for file_entry in files:
results.append(os.path.join(sub_path, file_entry))
return results
@shared_test_lib.skipUnlessHasTestFile(['ímynd.dd'])
def testCalculateDigestHash(self):
"""Tests the _CalculateDigestHash function."""
test_tool = image_export_tool.ImageExportTool()
test_path = self._GetTestFilePath(['ímynd.dd'])
os_path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_OS, location=test_path)
tsk_path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_TSK, inode=16,
location='/a_directory/another_file', parent=os_path_spec)
file_entry = path_spec_resolver.Resolver.OpenFileEntry(tsk_path_spec)
digest_hash = test_tool._CalculateDigestHash(file_entry, '')
expected_digest_hash = (
'c7fbc0e821c0871805a99584c6a384533909f68a6bbe9a2a687d28d9f3b10c16')
self.assertEqual(digest_hash, expected_digest_hash)
test_path = self._GetTestFilePath(['ímynd.dd'])
os_path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_OS, location=test_path)
tsk_path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_TSK, inode=12,
location='/a_directory', parent=os_path_spec)
file_entry = path_spec_resolver.Resolver.OpenFileEntry(tsk_path_spec)
with self.assertRaises(IOError):
test_tool._CalculateDigestHash(file_entry, '')
# TODO: add tests for _CreateSanitizedDestination.
# TODO: add tests for _Extract.
@shared_test_lib.skipUnlessHasTestFile(['ímynd.dd'])
def testExtractDataStream(self):
"""Tests the _ExtractDataStream function."""
output_writer = test_lib.TestOutputWriter(encoding='utf-8')
test_tool = image_export_tool.ImageExportTool()
test_path = self._GetTestFilePath(['ímynd.dd'])
os_path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_OS, location=test_path)
tsk_path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_TSK, inode=16,
location='/a_directory/another_file', parent=os_path_spec)
file_entry = path_spec_resolver.Resolver.OpenFileEntry(tsk_path_spec)
with shared_test_lib.TempDirectory() as temp_directory:
test_tool._ExtractDataStream(
file_entry, '', temp_directory, output_writer)
@shared_test_lib.skipUnlessHasTestFile(['ímynd.dd'])
def testExtractFileEntry(self):
"""Tests the _ExtractFileEntry function."""
output_writer = test_lib.TestOutputWriter(encoding='utf-8')
test_tool = image_export_tool.ImageExportTool()
test_path = self._GetTestFilePath(['ímynd.dd'])
os_path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_OS, location=test_path)
tsk_path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_TSK, inode=16,
location='/a_directory/another_file', parent=os_path_spec)
with shared_test_lib.TempDirectory() as temp_directory:
test_tool._ExtractFileEntry(
tsk_path_spec, temp_directory, output_writer)
# TODO: add tests for _ExtractWithFilter.
# TODO: add tests for _GetSourceFileSystem.
def testParseExtensionsString(self):
"""Tests the _ParseExtensionsString function."""
test_tool = image_export_tool.ImageExportTool()
test_tool._ParseExtensionsString('txt')
# TODO: add tests for _ParseFilterOptions.
def testParseNamesString(self):
"""Tests the _ParseNamesString function."""
test_tool = image_export_tool.ImageExportTool()
test_tool._ParseNamesString('another_file')
def testParseSignatureIdentifiers(self):
"""Tests the _ParseSignatureIdentifiers function."""
test_tool = image_export_tool.ImageExportTool()
test_tool._ParseSignatureIdentifiers(self._DATA_PATH, 'gzip')
with self.assertRaises(ValueError):
test_tool._ParseSignatureIdentifiers(None, 'gzip')
with self.assertRaises(IOError):
test_path = os.path.join(os.sep, 'bogus')
test_tool._ParseSignatureIdentifiers(test_path, 'gzip')
# TODO: add tests for _Preprocess.
# TODO: add tests for _ReadSpecificationFile.
@shared_test_lib.skipUnlessHasTestFile(['ímynd.dd'])
def testWriteFileEntry(self):
"""Tests the _WriteFileEntry function."""
test_tool = image_export_tool.ImageExportTool()
test_path = self._GetTestFilePath(['ímynd.dd'])
os_path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_OS, location=test_path)
tsk_path_spec = path_spec_factory.Factory.NewPathSpec(
dfvfs_definitions.TYPE_INDICATOR_TSK, inode=16,
location='/a_directory/another_file', parent=os_path_spec)
file_entry = path_spec_resolver.Resolver.OpenFileEntry(tsk_path_spec)
with shared_test_lib.TempDirectory() as temp_directory:
destination_path = os.path.join(temp_directory, 'another_file')
test_tool._WriteFileEntry(file_entry, '', destination_path)
# TODO: add tests for AddFilterOptions.
def testListSignatureIdentifiers(self):
"""Tests the ListSignatureIdentifiers function."""
output_writer = test_lib.TestOutputWriter(encoding='utf-8')
test_tool = image_export_tool.ImageExportTool(output_writer=output_writer)
test_tool._data_location = self._TEST_DATA_PATH
test_tool.ListSignatureIdentifiers()
expected_output = (
'Available signature identifiers:\n'
'7z, bzip2, esedb, evt, evtx, ewf_e01, ewf_l01, exe_mz, gzip, lnk, '
'msiecf, nk2,\n'
'olecf, olecf_beta, pdf, pff, qcow, rar, regf, tar, tar_old, '
'vhdi_footer,\n'
'vhdi_header, wtcdb_cache, wtcdb_index, zip\n'
'\n')
output = output_writer.ReadOutput()
# Compare the output as list of lines which makes it easier to spot
# differences.
self.assertEqual(output.split('\n'), expected_output.split('\n'))
test_tool._data_location = self._GetTestFilePath(['tmp'])
with self.assertRaises(errors.BadConfigOption):
test_tool.ListSignatureIdentifiers()
def testParseArguments(self):
"""Tests the ParseArguments function."""
output_writer = test_lib.TestOutputWriter(encoding='utf-8')
test_tool = image_export_tool.ImageExportTool(output_writer=output_writer)
result = test_tool.ParseArguments()
self.assertFalse(result)
# TODO: check output.
# TODO: improve test coverage.
def testParseOptions(self):
"""Tests the ParseOptions function."""
output_writer = test_lib.TestOutputWriter(encoding='utf-8')
test_tool = image_export_tool.ImageExportTool(output_writer=output_writer)
options = test_lib.TestOptions()
options.artifact_definitions_path = self._GetTestFilePath(['artifacts'])
options.image = self._GetTestFilePath(['image.qcow2'])
test_tool.ParseOptions(options)
options = test_lib.TestOptions()
with self.assertRaises(errors.BadConfigOption):
test_tool.ParseOptions(options)
# TODO: improve test coverage.
@shared_test_lib.skipUnlessHasTestFile(['image.qcow2'])
def testPrintFilterCollection(self):
"""Tests the PrintFilterCollection function."""
output_writer = test_lib.TestOutputWriter(encoding='utf-8')
test_tool = image_export_tool.ImageExportTool(output_writer=output_writer)
options = test_lib.TestOptions()
options.artifact_definitions_path = self._GetTestFilePath(['artifacts'])
options.date_filters = ['ctime,2012-05-25 15:59:00,2012-05-25 15:59:20']
options.image = self._GetTestFilePath(['image.qcow2'])
options.quiet = True
test_tool.ParseOptions(options)
test_tool.PrintFilterCollection()
expected_output = '\n'.join([
'Filters:',
('\tctime between 2012-05-25 15:59:00.000000 and '
'2012-05-25 15:59:20.000000'),
''])
output = output_writer.ReadOutput()
self.assertEqual(output, expected_output)
def testProcessSourcesWithImage(self):
"""Tests the ProcessSources function on a single partition image."""
output_writer = test_lib.TestOutputWriter(encoding='utf-8')
test_tool = image_export_tool.ImageExportTool(output_writer=output_writer)
options = test_lib.TestOptions()
options.artifact_definitions_path = self._GetTestFilePath(['artifacts'])
options.image = self._GetTestFilePath(['ímynd.dd'])
options.quiet = True
with shared_test_lib.TempDirectory() as temp_directory:
options.path = temp_directory
test_tool.ParseOptions(options)
test_tool.ProcessSources()
expected_output = '\n'.join([
'Export started.',
'Extracting file entries.',
'Export completed.',
'',
''])
output = output_writer.ReadOutput()
self.assertEqual(output, expected_output)
@shared_test_lib.skipUnlessHasTestFile(['image.qcow2'])
def testProcessSourcesExtractWithDateTimeFilter(self):
"""Tests the ProcessSources function with a date time filter."""
output_writer = test_lib.TestOutputWriter(encoding='utf-8')
test_tool = image_export_tool.ImageExportTool(output_writer=output_writer)
options = test_lib.TestOptions()
options.artifact_definitions_path = self._GetTestFilePath(['artifacts'])
options.date_filters = ['ctime,2012-05-25 15:59:00,2012-05-25 15:59:20']
options.image = self._GetTestFilePath(['image.qcow2'])
options.quiet = True
with shared_test_lib.TempDirectory() as temp_directory:
options.path = temp_directory
test_tool.ParseOptions(options)
test_tool.ProcessSources()
expected_extracted_files = sorted([
os.path.join(temp_directory, 'a_directory'),
os.path.join(temp_directory, 'a_directory', 'a_file')])
extracted_files = self._RecursiveList(temp_directory)
self.assertEqual(sorted(extracted_files), expected_extracted_files)
@shared_test_lib.skipUnlessHasTestFile(['image.qcow2'])
def testProcessSourcesExtractWithExtensionsFilter(self):
"""Tests the ProcessSources function with an extensions filter."""
output_writer = test_lib.TestOutputWriter(encoding='utf-8')
test_tool = image_export_tool.ImageExportTool(output_writer=output_writer)
options = test_lib.TestOptions()
options.artifact_definitions_path = self._GetTestFilePath(['artifacts'])
options.extensions_string = 'txt'
options.image = self._GetTestFilePath(['image.qcow2'])
options.quiet = True
with shared_test_lib.TempDirectory() as temp_directory:
options.path = temp_directory
test_tool.ParseOptions(options)
test_tool.ProcessSources()
expected_extracted_files = sorted([
os.path.join(temp_directory, 'passwords.txt')])
extracted_files = self._RecursiveList(temp_directory)
self.assertEqual(sorted(extracted_files), expected_extracted_files)
@shared_test_lib.skipUnlessHasTestFile(['image.qcow2'])
def testProcessSourcesExtractWithNamesFilter(self):
"""Tests the ProcessSources function with a names filter."""
output_writer = test_lib.TestOutputWriter(encoding='utf-8')
test_tool = image_export_tool.ImageExportTool(output_writer=output_writer)
options = test_lib.TestOptions()
options.artifact_definitions_path = self._GetTestFilePath(['artifacts'])
options.image = self._GetTestFilePath(['image.qcow2'])
options.names_string = 'another_file'
options.quiet = True
with shared_test_lib.TempDirectory() as temp_directory:
options.path = temp_directory
test_tool.ParseOptions(options)
test_tool.ProcessSources()
expected_extracted_files = sorted([
os.path.join(temp_directory, 'a_directory'),
os.path.join(temp_directory, 'a_directory', 'another_file')])
extracted_files = self._RecursiveList(temp_directory)
self.assertEqual(sorted(extracted_files), expected_extracted_files)
@shared_test_lib.skipUnlessHasTestFile(['image.qcow2'])
def testProcessSourcesExtractWithFilter(self):
"""Tests the ProcessSources function with a filter file."""
output_writer = test_lib.TestOutputWriter(encoding='utf-8')
test_tool = image_export_tool.ImageExportTool(output_writer=output_writer)
options = test_lib.TestOptions()
options.artifact_definitions_path = self._GetTestFilePath(['artifacts'])
options.image = self._GetTestFilePath(['image.qcow2'])
options.quiet = True
with shared_test_lib.TempDirectory() as temp_directory:
filter_file = os.path.join(temp_directory, 'filter.txt')
with io.open(filter_file, 'wt', encoding='utf-8') as file_object:
file_object.write('/a_directory/.+_file\n')
options.file_filter = filter_file
options.path = temp_directory
test_tool.ParseOptions(options)
test_tool.ProcessSources()
expected_extracted_files = sorted([
os.path.join(temp_directory, 'filter.txt'),
os.path.join(temp_directory, 'a_directory'),
os.path.join(temp_directory, 'a_directory', 'another_file'),
os.path.join(temp_directory, 'a_directory', 'a_file')])
extracted_files = self._RecursiveList(temp_directory)
self.assertEqual(sorted(extracted_files), expected_extracted_files)
@shared_test_lib.skipUnlessHasTestFile(['artifacts'])
@shared_test_lib.skipUnlessHasTestFile(['image.qcow2'])
def testProcessSourcesExtractWithArtifactsFilter(self):
"""Tests the ProcessSources function with a artifacts filter file."""
output_writer = test_lib.TestOutputWriter(encoding='utf-8')
test_tool = image_export_tool.ImageExportTool(output_writer=output_writer)
options = test_lib.TestOptions()
options.artifact_definitions_path = self._GetTestFilePath(['artifacts'])
options.image = self._GetTestFilePath(['image.qcow2'])
options.quiet = True
options.artifact_filter_string = 'TestFilesImageExport'
with shared_test_lib.TempDirectory() as temp_directory:
options.path = temp_directory
test_tool.ParseOptions(options)
test_tool.ProcessSources()
expected_extracted_files = sorted([
os.path.join(temp_directory, 'a_directory'),
os.path.join(temp_directory, 'a_directory', 'another_file'),
os.path.join(temp_directory, 'a_directory', 'a_file')])
extracted_files = self._RecursiveList(temp_directory)
self.assertEqual(sorted(extracted_files), expected_extracted_files)
@shared_test_lib.skipUnlessHasTestFile(['artifacts'])
@shared_test_lib.skipUnlessHasTestFile(['image.qcow2'])
def testProcessSourcesExtractWithArtifactsGroupFilter(self):
"""Tests the ProcessSources function with a group artifacts filter file."""
output_writer = test_lib.TestOutputWriter(encoding='utf-8')
test_tool = image_export_tool.ImageExportTool(output_writer=output_writer)
options = test_lib.TestOptions()
options.artifact_definitions_path = self._GetTestFilePath(['artifacts'])
options.image = self._GetTestFilePath(['image.qcow2'])
options.quiet = True
options.artifact_filter_string = 'TestGroupExport'
with shared_test_lib.TempDirectory() as temp_directory:
options.path = temp_directory
test_tool.ParseOptions(options)
test_tool.ProcessSources()
expected_extracted_files = sorted([
os.path.join(temp_directory, 'a_directory'),
os.path.join(temp_directory, 'a_directory', 'another_file'),
os.path.join(temp_directory, 'a_directory', 'a_file'),
os.path.join(temp_directory, 'passwords.txt')])
extracted_files = self._RecursiveList(temp_directory)
self.assertEqual(sorted(extracted_files), expected_extracted_files)
@shared_test_lib.skipUnlessHasTestFile(['syslog_image.dd'])
def testProcessSourcesExtractWithSignaturesFilter(self):
"""Tests the ProcessSources function with a signatures filter."""
output_writer = test_lib.TestOutputWriter(encoding='utf-8')
test_tool = image_export_tool.ImageExportTool(output_writer=output_writer)
options = test_lib.TestOptions()
options.artifact_definitions_path = self._GetTestFilePath(['artifacts'])
options.image = self._GetTestFilePath(['syslog_image.dd'])
options.quiet = True
options.signature_identifiers = 'gzip'
with shared_test_lib.TempDirectory() as temp_directory:
options.path = temp_directory
test_tool.ParseOptions(options)
test_tool.ProcessSources()
expected_extracted_files = sorted([
os.path.join(temp_directory, 'logs'),
os.path.join(temp_directory, 'logs', 'sys.tgz')])
extracted_files = self._RecursiveList(temp_directory)
self.assertEqual(sorted(extracted_files), expected_extracted_files)
if __name__ == '__main__':
unittest.main()
|