Skip to content

Commit

Permalink
Worked on tests and code clean up (log2timeline#671)
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimmetz authored Jul 29, 2022
1 parent de43a87 commit 55e01ab
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dfvfs/file_io/hfs_file_io.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
"""The Extended File System (HFS) file-like object implementation."""
"""The Hierarchical File System (HFS) file-like object implementation."""

import os

Expand Down
2 changes: 1 addition & 1 deletion tests/file_io/hfs_file_io.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Tests for the Extended File System (HFS) file-like object."""
"""Tests for the Hierarchical File System (HFS) file-like object."""

import unittest

Expand Down
92 changes: 92 additions & 0 deletions tests/file_io/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,98 @@ def _TestRead(self, parent_path_spec):
# TODO: add boundary scenarios.


class FAT12ImageFileTestCase(shared_test_lib.BaseTestCase):
"""Shared functionality for storage media image with a FAT-12 file system."""

_IDENTIFIER_ANOTHER_FILE = 584
_IDENTIFIER_PASSWORDS_TXT = 7

def setUp(self):
"""Sets up the needed objects used throughout the test."""
self._resolver_context = context.Context()

def tearDown(self):
"""Cleans up the needed objects used throughout the test."""
self._resolver_context.Empty()

def _TestOpenCloseIdentifier(self, file_object):
"""Test the open and close functionality using an identifier.
Args:
file_object (FileIO): file-like object.
"""
file_object.Open()
self.assertEqual(file_object.get_size(), 116)

# TODO: add a failing scenario.

def _TestOpenCloseLocation(self, file_object):
"""Test the open and close functionality using a location.
Args:
file_object (FileIO): file-like object.
"""
file_object.Open()
self.assertEqual(file_object.get_size(), 116)

def _TestSeek(self, file_object):
"""Test the seek functionality.
Args:
file_object (FileIO): file-like object.
"""
file_object.Open()
self.assertEqual(file_object.get_size(), 22)

file_object.seek(10)
self.assertEqual(file_object.read(5), b'other')
self.assertEqual(file_object.get_offset(), 15)

file_object.seek(-10, os.SEEK_END)
self.assertEqual(file_object.read(5), b'her f')

file_object.seek(2, os.SEEK_CUR)
self.assertEqual(file_object.read(2), b'e.')

# Conforming to the POSIX seek the offset can exceed the file size
# but reading will result in no data being returned.
file_object.seek(300, os.SEEK_SET)
self.assertEqual(file_object.get_offset(), 300)
self.assertEqual(file_object.read(2), b'')

with self.assertRaises(IOError):
file_object.seek(-10, os.SEEK_SET)

# On error the offset should not change.
self.assertEqual(file_object.get_offset(), 300)

with self.assertRaises(IOError):
file_object.seek(10, 5)

# On error the offset should not change.
self.assertEqual(file_object.get_offset(), 300)

def _TestRead(self, file_object):
"""Test the read functionality.
Args:
file_object (FileIO): file-like object.
"""
file_object.Open()
read_buffer = file_object.read()

expected_buffer = (
b'place,user,password\n'
b'bank,joesmith,superrich\n'
b'alarm system,-,1234\n'
b'treasure chest,-,1111\n'
b'uber secret laire,admin,admin\n')

self.assertEqual(read_buffer, expected_buffer)

# TODO: add boundary scenarios.


class HFSImageFileTestCase(shared_test_lib.BaseTestCase):
"""Shared functionality for storage media image with a HFS file system."""

Expand Down
66 changes: 66 additions & 0 deletions tests/file_io/tsk_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,72 @@ def testRead(self):
# TODO: add boundary scenarios.


class TSKFileTestFAT12(test_lib.FAT12ImageFileTestCase):
"""Tests the SleuthKit (TSK) file-like object on FAT-12."""

_IDENTIFIER_ANOTHER_FILE = 584
_IDENTIFIER_PASSWORDS_TXT = 7

def setUp(self):
"""Sets up the needed objects used throughout the test."""
super(TSKFileTestFAT12, self).setUp()
self._resolver_context = context.Context()
test_path = self._GetTestFilePath(['fat12.raw'])
self._SkipIfPathNotExists(test_path)

test_os_path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_OS, location=test_path)
self._raw_path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_RAW, parent=test_os_path_spec)

def tearDown(self):
"""Cleans up the needed objects used throughout the test."""
self._resolver_context.Empty()

def testOpenCloseIdentifier(self):
"""Test the open and close functionality using an identifier."""
path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_TSK, inode=self._IDENTIFIER_PASSWORDS_TXT,
parent=self._raw_path_spec)
file_object = tsk_file_io.TSKFile(self._resolver_context, path_spec)

self._TestOpenCloseIdentifier(file_object)

def testOpenCloseLocation(self):
"""Test the open and close functionality using a location."""
path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_TSK, location='/passwords.txt',
parent=self._raw_path_spec)
file_object = tsk_file_io.TSKFile(self._resolver_context, path_spec)

self._TestOpenCloseLocation(file_object)

# Try open with a path specification that has no parent.
path_spec.parent = None
file_object = tsk_file_io.TSKFile(self._resolver_context, path_spec)

with self.assertRaises(errors.PathSpecError):
self._TestOpenCloseLocation(file_object)

def testSeek(self):
"""Test the seek functionality."""
path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_TSK, location='/a_directory/another_file',
inode=self._IDENTIFIER_ANOTHER_FILE, parent=self._raw_path_spec)
file_object = tsk_file_io.TSKFile(self._resolver_context, path_spec)

self._TestSeek(file_object)

def testRead(self):
"""Test the read functionality."""
path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_TSK, location='/passwords.txt',
inode=self._IDENTIFIER_PASSWORDS_TXT, parent=self._raw_path_spec)
file_object = tsk_file_io.TSKFile(self._resolver_context, path_spec)

self._TestRead(file_object)


class TSKFileTestHFS(test_lib.HFSImageFileTestCase):
"""Tests the SleuthKit (TSK) file-like object on HFS."""

Expand Down

0 comments on commit 55e01ab

Please sign in to comment.