Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed Ext2ImageFileTestCase to use preferred ext back-end #464 #496

Merged
merged 2 commits into from
Nov 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion dfvfs/resolver/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

from __future__ import unicode_literals

import collections
import itertools

from dfvfs.lib import errors


Expand Down Expand Up @@ -62,7 +65,7 @@ def __init__(self, maximum_number_of_cached_values):

super(ObjectsCache, self).__init__()
self._maximum_number_of_cached_values = maximum_number_of_cached_values
self._values = {}
self._values = collections.OrderedDict()

def CacheObject(self, identifier, vfs_object):
"""Caches a VFS object.
Expand Down Expand Up @@ -129,6 +132,22 @@ def GetCacheValueByObject(self, vfs_object):

return None, None

def GetLastObject(self):
"""Retrieves the last cached object.

This method ignores the cache value reference count.

Returns:
object: the last cached VFS object or None if the cache is empty.
"""
if not self._values:
return None

# Get the last (or most recent added) cache value.
cache_value = next(itertools.islice(
self._values.values(), len(self._values) - 1, None))
return cache_value.vfs_object

def GetObject(self, identifier):
"""Retrieves a cached object based on the identifier.

Expand Down
5 changes: 5 additions & 0 deletions dfvfs/resolver/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ def CacheFileSystem(self, path_spec, file_system):

def Empty(self):
"""Empties the caches."""
file_object = self._file_object_cache.GetLastObject()
while file_object:
file_object.close()
file_object = self._file_object_cache.GetLastObject()

self._file_object_cache.Empty()
self._file_system_cache.Empty()

Expand Down
10 changes: 7 additions & 3 deletions tests/file_io/apfs_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
from dfvfs.path import factory as path_spec_factory
from dfvfs.resolver import context

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


class APFSFileTest(test_lib.ImageFileTestCase):
class APFSFileTest(shared_test_lib.BaseTestCase):
"""Tests the file-like object implementation using pyfsapfs.file_entry."""

_IDENTIFIER_ANOTHER_FILE = 21
Expand All @@ -37,6 +37,10 @@ def setUp(self):
definitions.TYPE_INDICATOR_APFS_CONTAINER, location='/apfs1',
parent=test_raw_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(
Expand Down Expand Up @@ -66,7 +70,7 @@ def testOpenCloseLocation(self):
path_spec.parent = None

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

def testSeek(self):
"""Test the seek functionality."""
Expand Down
16 changes: 16 additions & 0 deletions tests/file_io/compressed_stream_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def setUp(self):
compression_method=definitions.COMPRESSION_METHOD_BZIP2,
parent=self._os_path_spec))

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

def testOpenCloseFileObject(self):
"""Test the open and close functionality using a file-like object."""
os_file_object = os_file_io.OSFile(self._resolver_context)
Expand Down Expand Up @@ -101,6 +105,10 @@ def setUp(self):
compression_method=definitions.COMPRESSION_METHOD_LZMA,
parent=self._os_path_spec))

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

def testOpenCloseFileObject(self):
"""Test the open and close functionality using a file-like object."""
os_file_object = os_file_io.OSFile(self._resolver_context)
Expand Down Expand Up @@ -170,6 +178,10 @@ def setUp(self):
compression_method=definitions.COMPRESSION_METHOD_XZ,
parent=self._os_path_spec))

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

def testOpenCloseFileObject(self):
"""Test the open and close functionality using a file-like object."""
os_file_object = os_file_io.OSFile(self._resolver_context)
Expand Down Expand Up @@ -239,6 +251,10 @@ def setUp(self):
compression_method=definitions.COMPRESSION_METHOD_ZLIB,
parent=self._os_path_spec))

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

def testOpenCloseFileObject(self):
"""Test the open and close functionality using a file-like object."""
os_file_object = os_file_io.OSFile(self._resolver_context)
Expand Down
16 changes: 16 additions & 0 deletions tests/file_io/cpio_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def setUp(self):
self._cpio_path_spec = cpio_path_spec.CPIOPathSpec(
location='/syslog', parent=path_spec)

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

def testOpenClosePathSpec(self):
"""Test the open and close functionality using a path specification."""
file_object = cpio_file_io.CPIOFile(self._resolver_context)
Expand Down Expand Up @@ -70,6 +74,10 @@ def setUp(self):
self._cpio_path_spec = cpio_path_spec.CPIOPathSpec(
location='/syslog', parent=path_spec)

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

def testOpenClosePathSpec(self):
"""Test the open and close functionality using a path specification."""
file_object = cpio_file_io.CPIOFile(self._resolver_context)
Expand Down Expand Up @@ -112,6 +120,10 @@ def setUp(self):
self._cpio_path_spec = cpio_path_spec.CPIOPathSpec(
location='/syslog', parent=path_spec)

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

def testOpenClosePathSpec(self):
"""Test the open and close functionality using a path specification."""
file_object = cpio_file_io.CPIOFile(self._resolver_context)
Expand Down Expand Up @@ -154,6 +166,10 @@ def setUp(self):
self._cpio_path_spec = cpio_path_spec.CPIOPathSpec(
location='/syslog', parent=path_spec)

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

def testOpenClosePathSpec(self):
"""Test the open and close functionality using a path specification."""
file_object = cpio_file_io.CPIOFile(self._resolver_context)
Expand Down
4 changes: 4 additions & 0 deletions tests/file_io/data_range_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def setUp(self):
self._data_range_path_spec = data_range_path_spec.DataRangePathSpec(
range_offset=167, range_size=1080, parent=self._os_path_spec)

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

def testOpenCloseFileObject(self):
"""Test the open and close functionality using a file-like object."""
os_file_object = os_file_io.OSFile(self._resolver_context)
Expand Down
12 changes: 12 additions & 0 deletions tests/file_io/encoded_stream_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def setUp(self):
encoding_method=definitions.ENCODING_METHOD_BASE16,
parent=self._os_path_spec))

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

def testOpenCloseFileObject(self):
"""Test the open and close functionality using a file-like object."""
os_file_object = os_file_io.OSFile(self._resolver_context)
Expand Down Expand Up @@ -101,6 +105,10 @@ def setUp(self):
encoding_method=definitions.ENCODING_METHOD_BASE32,
parent=self._os_path_spec))

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

def testOpenCloseFileObject(self):
"""Test the open and close functionality using a file-like object."""
os_file_object = os_file_io.OSFile(self._resolver_context)
Expand Down Expand Up @@ -170,6 +178,10 @@ def setUp(self):
encoding_method=definitions.ENCODING_METHOD_BASE64,
parent=self._os_path_spec))

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

def testOpenCloseFileObject(self):
"""Test the open and close functionality using a file-like object."""
os_file_object = os_file_io.OSFile(self._resolver_context)
Expand Down
20 changes: 20 additions & 0 deletions tests/file_io/encrypted_stream_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def setUp(self):
self._encrypted_stream_path_spec, 'cipher_mode', self._AES_MODE)
self.padding_size = 1

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

def testOpenCloseFileObject(self):
"""Test the open and close functionality using a file-like object."""
os_file_object = os_file_io.OSFile(self._resolver_context)
Expand Down Expand Up @@ -126,6 +130,10 @@ def setUp(self):
key=self._AES_KEY, parent=self._os_path_spec))
self.padding_size = 1

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

def testOpenCloseFileObject(self):
"""Test the open and close functionality using a file-like object."""
os_file_object = os_file_io.OSFile(self._resolver_context)
Expand Down Expand Up @@ -209,6 +217,10 @@ def setUp(self):
self._encrypted_stream_path_spec, 'cipher_mode', self._BLOWFISH_MODE)
self.padding_size = 1

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

def testOpenCloseFileObject(self):
"""Test the open and close functionality using a file-like object."""
os_file_object = os_file_io.OSFile(self._resolver_context)
Expand Down Expand Up @@ -292,6 +304,10 @@ def setUp(self):
self._encrypted_stream_path_spec, 'cipher_mode', self._DES3_MODE)
self.padding_size = 1

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

def testOpenCloseFileObject(self):
"""Test the open and close functionality using a file-like object."""
os_file_object = os_file_io.OSFile(self._resolver_context)
Expand Down Expand Up @@ -367,6 +383,10 @@ def setUp(self):
resolver.Resolver.key_chain.SetCredential(
self._encrypted_stream_path_spec, 'key', self._RC4_KEY)

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

def testOpenCloseFileObject(self):
"""Test the open and close functionality using a file-like object."""
os_file_object = os_file_io.OSFile(self._resolver_context)
Expand Down
10 changes: 7 additions & 3 deletions tests/file_io/ext_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
from dfvfs.path import factory as path_spec_factory
from dfvfs.resolver import context

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


class EXTFileTest(test_lib.ImageFileTestCase):
class EXTFileTest(shared_test_lib.BaseTestCase):
"""Tests the file-like object implementation using pyfsext.file_entry."""

_IDENTIFIER_ANOTHER_FILE = 15
Expand All @@ -34,6 +34,10 @@ def setUp(self):
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 inode."""
path_spec = path_spec_factory.Factory.NewPathSpec(
Expand Down Expand Up @@ -63,7 +67,7 @@ def testOpenCloseLocation(self):
path_spec.parent = None

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

def testSeek(self):
"""Test the seek functionality."""
Expand Down
4 changes: 4 additions & 0 deletions tests/file_io/fake_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ 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 testOpenClosePathSpec(self):
"""Test the Open and Close functions with a path specification."""
test_file = '/test_data/password.txt'
Expand Down
4 changes: 4 additions & 0 deletions tests/file_io/gzip_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def setUp(self):
self._gzip_path_spec = path_spec_factory.Factory.NewPathSpec(
definitions.TYPE_INDICATOR_GZIP, parent=test_os_path_spec)

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

def testOpenClosePathSpec(self):
"""Test the open and close functionality using a path specification."""
file_object = gzip_file_io.GzipFile(self._resolver_context)
Expand Down
10 changes: 7 additions & 3 deletions tests/file_io/hfs_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
from dfvfs.path import factory as path_spec_factory
from dfvfs.resolver import context

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


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

_IDENTIFIER_ANOTHER_FILE = 21
Expand All @@ -34,6 +34,10 @@ def setUp(self):
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(
Expand Down Expand Up @@ -63,7 +67,7 @@ def testOpenCloseLocation(self):
path_spec.parent = None

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

def testSeek(self):
"""Test the seek functionality."""
Expand Down
4 changes: 4 additions & 0 deletions tests/file_io/lvm_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def setUp(self):
path_spec = os_path_spec.OSPathSpec(location=test_file)
self._raw_path_spec = raw_path_spec.RawPathSpec(parent=path_spec)

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

def testOpenClose(self):
"""Test the open and close functionality."""
path_spec = lvm_path_spec.LVMPathSpec(
Expand Down
4 changes: 4 additions & 0 deletions tests/file_io/os_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def setUp(self):

self._path_spec2 = os_path_spec.OSPathSpec(location=test_file)

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

def testOpenClosePathSpec(self):
"""Test the open and close functionality using a path specification."""
file_object = os_file_io.OSFile(self._resolver_context)
Expand Down
Loading