Skip to content

Commit

Permalink
Worked on tests (#635)
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimmetz authored Jan 14, 2022
1 parent db2f73d commit a28741f
Show file tree
Hide file tree
Showing 15 changed files with 327 additions and 171 deletions.
Binary file modified test_data/apfs.raw
Binary file not shown.
Binary file modified test_data/hfsplus.raw
Binary file not shown.
Binary file modified test_data/hfsplus.sparseimage
Binary file not shown.
Binary file modified test_data/hfsplus_zlib.dmg
Binary file not shown.
4 changes: 2 additions & 2 deletions tests/file_io/apfs_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
class APFSFileTest(shared_test_lib.BaseTestCase):
"""Tests the file-like object implementation using pyfsapfs.file_entry."""

_IDENTIFIER_ANOTHER_FILE = 21
_IDENTIFIER_PASSWORDS_TXT = 20
_IDENTIFIER_ANOTHER_FILE = 19
_IDENTIFIER_PASSWORDS_TXT = 18

def setUp(self):
"""Sets up the needed objects used throughout the test."""
Expand Down
72 changes: 13 additions & 59 deletions tests/file_io/hfs_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# -*- coding: utf-8 -*-
"""Tests for the Extended File System (HFS) file-like object."""

import os
import unittest

from dfvfs.file_io import hfs_file_io
Expand All @@ -11,14 +10,14 @@
from dfvfs.path import factory as path_spec_factory
from dfvfs.resolver import context

from tests import test_lib as shared_test_lib
from tests.file_io import test_lib


class HFSFileTest(shared_test_lib.BaseTestCase):
class HFSFileTest(test_lib.HFSImageFileTestCase):
"""Tests the file-like object implementation using pyfshfs.file_entry."""

_IDENTIFIER_ANOTHER_FILE = 23
_IDENTIFIER_PASSWORDS_TXT = 22
_IDENTIFIER_ANOTHER_FILE = 21
_IDENTIFIER_PASSWORDS_TXT = 20

def setUp(self):
"""Sets up the needed objects used throughout the test."""
Expand All @@ -40,31 +39,27 @@ def testOpenCloseIdentifier(self):
"""Test the open and close functionality using an identifier."""
path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_HFS,
identifier=self._IDENTIFIER_PASSWORDS_TXT,
parent=self._raw_path_spec)
identifier=self._IDENTIFIER_PASSWORDS_TXT, parent=self._raw_path_spec)
file_object = hfs_file_io.HFSFile(self._resolver_context, path_spec)

file_object.Open()
self.assertEqual(file_object.get_size(), 116)

# TODO: add a failing scenario.
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_HFS, location='/passwords.txt',
definitions.TYPE_INDICATOR_HFS,
identifier=self._IDENTIFIER_PASSWORDS_TXT, location='/passwords.txt',
parent=self._raw_path_spec)
file_object = hfs_file_io.HFSFile(self._resolver_context, path_spec)

file_object.Open()
self.assertEqual(file_object.get_size(), 116)
self._TestOpenCloseLocation(file_object)

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

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

def testSeek(self):
"""Test the seek functionality."""
Expand All @@ -74,36 +69,7 @@ def testSeek(self):
parent=self._raw_path_spec)
file_object = hfs_file_io.HFSFile(self._resolver_context, path_spec)

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)
self._TestSeek(file_object)

def testRead(self):
"""Test the read functionality."""
Expand All @@ -113,19 +79,7 @@ def testRead(self):
parent=self._raw_path_spec)
file_object = hfs_file_io.HFSFile(self._resolver_context, path_spec)

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.
self._TestRead(file_object)


if __name__ == '__main__':
Expand Down
95 changes: 69 additions & 26 deletions tests/file_io/modi_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import unittest

from dfvfs.file_io import hfs_file_io
from dfvfs.lib import definitions
from dfvfs.lib import errors
from dfvfs.path import factory as path_spec_factory
Expand All @@ -24,41 +25,62 @@ def setUp(self):
definitions.TYPE_INDICATOR_OS, location=test_path)
self._modi_path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_MODI, parent=test_os_path_spec)
self._tsk_partition_path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_TSK_PARTITION, location='/p1',
self._gpt_path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_GPT, location='/p1',
parent=self._modi_path_spec)

def testOpenCloseInode(self):
"""Test the open and close functionality using an inode."""
self._TestOpenCloseInode(self._tsk_partition_path_spec)
def testOpenCloseIdentifier(self):
"""Test the open and close functionality using an identifier."""
path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_HFS,
identifier=self._IDENTIFIER_PASSWORDS_TXT, parent=self._gpt_path_spec)
file_object = hfs_file_io.HFSFile(self._resolver_context, path_spec)

self._TestOpenCloseIdentifier(file_object)

def testOpenCloseLocation(self):
"""Test the open and close functionality using a location."""
self._TestOpenCloseLocation(self._tsk_partition_path_spec)
path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_HFS,
identifier=self._IDENTIFIER_PASSWORDS_TXT, location='/passwords.txt',
parent=self._gpt_path_spec)
file_object = hfs_file_io.HFSFile(self._resolver_context, path_spec)

self._TestOpenCloseLocation(file_object)

# Try open with a path specification that has no parent.
path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_TSK_PARTITION, location='/p1',
parent=self._modi_path_spec)
path_spec.parent = None
file_object = hfs_file_io.HFSFile(self._resolver_context, path_spec)

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

def testSeek(self):
"""Test the seek functionality."""
self._TestSeek(self._tsk_partition_path_spec)
path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_HFS,
identifier=self._IDENTIFIER_ANOTHER_FILE,
location='/a_directory/another_file', parent=self._gpt_path_spec)
file_object = hfs_file_io.HFSFile(self._resolver_context, path_spec)

self._TestSeek(file_object)

def testRead(self):
"""Test the read functionality."""
self._TestRead(self._tsk_partition_path_spec)
path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_HFS,
identifier=self._IDENTIFIER_PASSWORDS_TXT, location='/passwords.txt',
parent=self._gpt_path_spec)
file_object = hfs_file_io.HFSFile(self._resolver_context, path_spec)

self._TestRead(file_object)


class ZlibCompressedUDIFMODIFileTest(test_lib.HFSImageFileTestCase):
"""Tests the MODI file-like object on a zlib compressed UDIF image file."""

_IDENTIFIER_ANOTHER_FILE = 20
_IDENTIFIER_PASSWORDS_TXT = 22
_IDENTIFIER_ANOTHER_FILE = 21
_IDENTIFIER_PASSWORDS_TXT = 23

def setUp(self):
"""Sets up the needed objects used throughout the test."""
Expand All @@ -70,34 +92,55 @@ def setUp(self):
definitions.TYPE_INDICATOR_OS, location=test_path)
self._modi_path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_MODI, parent=test_os_path_spec)
self._tsk_partition_path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_TSK_PARTITION, location='/p1',
self._gpt_path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_GPT, location='/p1',
parent=self._modi_path_spec)

def testOpenCloseInode(self):
"""Test the open and close functionality using an inode."""
self._TestOpenCloseInode(self._tsk_partition_path_spec)
def testOpenCloseIdentifier(self):
"""Test the open and close functionality using an identifier."""
path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_HFS,
identifier=self._IDENTIFIER_PASSWORDS_TXT, parent=self._gpt_path_spec)
file_object = hfs_file_io.HFSFile(self._resolver_context, path_spec)

self._TestOpenCloseIdentifier(file_object)

def testOpenCloseLocation(self):
"""Test the open and close functionality using a location."""
self._TestOpenCloseLocation(self._tsk_partition_path_spec)
path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_HFS,
identifier=self._IDENTIFIER_PASSWORDS_TXT, location='/passwords.txt',
parent=self._gpt_path_spec)
file_object = hfs_file_io.HFSFile(self._resolver_context, path_spec)

self._TestOpenCloseLocation(file_object)

# Try open with a path specification that has no parent.
path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_TSK_PARTITION, location='/p1',
parent=self._modi_path_spec)
path_spec.parent = None
file_object = hfs_file_io.HFSFile(self._resolver_context, path_spec)

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

def testSeek(self):
"""Test the seek functionality."""
self._TestSeek(self._tsk_partition_path_spec)
path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_HFS,
identifier=self._IDENTIFIER_ANOTHER_FILE,
location='/a_directory/another_file', parent=self._gpt_path_spec)
file_object = hfs_file_io.HFSFile(self._resolver_context, path_spec)

self._TestSeek(file_object)

def testRead(self):
"""Test the read functionality."""
self._TestRead(self._tsk_partition_path_spec)
path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_HFS,
identifier=self._IDENTIFIER_PASSWORDS_TXT, location='/passwords.txt',
parent=self._gpt_path_spec)
file_object = hfs_file_io.HFSFile(self._resolver_context, path_spec)

self._TestRead(file_object)


if __name__ == '__main__':
Expand Down
41 changes: 32 additions & 9 deletions tests/file_io/phdi_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import unittest

from dfvfs.file_io import hfs_file_io
from dfvfs.lib import definitions
from dfvfs.lib import errors
from dfvfs.path import factory as path_spec_factory
Expand Down Expand Up @@ -31,29 +32,51 @@ def setUp(self):
definitions.TYPE_INDICATOR_GPT, location='/p1',
parent=self._phdi_path_spec)

def testOpenCloseInode(self):
"""Test the open and close functionality using an inode."""
self._TestOpenCloseInode(self._gpt_path_spec)
def testOpenCloseIdentifier(self):
"""Test the open and close functionality using an identifier."""
path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_HFS,
identifier=self._IDENTIFIER_PASSWORDS_TXT, parent=self._gpt_path_spec)
file_object = hfs_file_io.HFSFile(self._resolver_context, path_spec)

self._TestOpenCloseIdentifier(file_object)

def testOpenCloseLocation(self):
"""Test the open and close functionality using a location."""
self._TestOpenCloseLocation(self._gpt_path_spec)
path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_HFS,
identifier=self._IDENTIFIER_PASSWORDS_TXT, location='/passwords.txt',
parent=self._gpt_path_spec)
file_object = hfs_file_io.HFSFile(self._resolver_context, path_spec)

self._TestOpenCloseLocation(file_object)

# Try open with a path specification that has no parent.
path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_PHDI, parent=self._phdi_path_spec)
path_spec.parent = None
file_object = hfs_file_io.HFSFile(self._resolver_context, path_spec)

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

def testSeek(self):
"""Test the seek functionality."""
self._TestSeek(self._gpt_path_spec)
path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_HFS,
identifier=self._IDENTIFIER_ANOTHER_FILE,
location='/a_directory/another_file', parent=self._gpt_path_spec)
file_object = hfs_file_io.HFSFile(self._resolver_context, path_spec)

self._TestSeek(file_object)

def testRead(self):
"""Test the read functionality."""
self._TestRead(self._gpt_path_spec)
path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_HFS,
identifier=self._IDENTIFIER_PASSWORDS_TXT, location='/passwords.txt',
parent=self._gpt_path_spec)
file_object = hfs_file_io.HFSFile(self._resolver_context, path_spec)

self._TestRead(file_object)


if __name__ == '__main__':
Expand Down
Loading

0 comments on commit a28741f

Please sign in to comment.