Skip to content

Commit

Permalink
Code review: 323820043: Fixed negative seeks in encrypted, encoded, a…
Browse files Browse the repository at this point in the history
…nd compressed streams log2timeline#183
  • Loading branch information
dc3-plaso authored and joachimmetz committed Jun 25, 2017
1 parent 25ccf9c commit a889480
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 0 deletions.
8 changes: 8 additions & 0 deletions dfvfs/file_io/compressed_stream_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,18 @@ def seek(self, offset, whence=os.SEEK_SET):

if whence == os.SEEK_CUR:
offset += self._current_offset

elif whence == os.SEEK_END:
if self._uncompressed_stream_size is None:
self._uncompressed_stream_size = self._GetUncompressedStreamSize()
if self._uncompressed_stream_size is None:
raise IOError(u'Invalid uncompressed stream size.')

offset += self._uncompressed_stream_size

elif whence != os.SEEK_SET:
raise IOError(u'Unsupported whence.')

if offset < 0:
raise IOError(u'Invalid offset value less than zero.')

Expand Down
8 changes: 8 additions & 0 deletions dfvfs/file_io/encoded_stream_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,18 @@ def seek(self, offset, whence=os.SEEK_SET):

if whence == os.SEEK_CUR:
offset += self._current_offset

elif whence == os.SEEK_END:
if self._decoded_stream_size is None:
self._decoded_stream_size = self._GetDecodedStreamSize()
if self._decoded_stream_size is None:
raise IOError(u'Invalid decoded stream size.')

offset += self._decoded_stream_size

elif whence != os.SEEK_SET:
raise IOError(u'Unsupported whence.')

if offset < 0:
raise IOError(u'Invalid offset value less than zero.')

Expand Down
8 changes: 8 additions & 0 deletions dfvfs/file_io/encrypted_stream_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,18 @@ def seek(self, offset, whence=os.SEEK_SET):

if whence == os.SEEK_CUR:
offset += self._current_offset

elif whence == os.SEEK_END:
if self._decrypted_stream_size is None:
self._decrypted_stream_size = self._GetDecryptedStreamSize()
if self._decrypted_stream_size is None:
raise IOError(u'Invalid decrypted stream size.')

offset += self._decrypted_stream_size

elif whence != os.SEEK_SET:
raise IOError(u'Unsupported whence.')

if offset < 0:
raise IOError(u'Invalid offset value less than zero.')

Expand Down
45 changes: 45 additions & 0 deletions tests/file_io/compressed_stream_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
except ImportError:
lzma = None

import os
import unittest

from dfvfs.file_io import compressed_stream_io
Expand Down Expand Up @@ -67,6 +68,17 @@ def testSeek(self):

file_object.close()

# TODO: Test SEEK_CUR after open.

# Test SEEK_END after open.
file_object = compressed_stream_io.CompressedStream(self._resolver_context)
file_object.open(path_spec=self._compressed_stream_path_spec)

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

file_object.close()

def testRead(self):
"""Test the read functionality."""
file_object = compressed_stream_io.CompressedStream(self._resolver_context)
Expand Down Expand Up @@ -125,6 +137,17 @@ def testSeek(self):

file_object.close()

# TODO: Test SEEK_CUR after open.

# Test SEEK_END after open.
file_object = compressed_stream_io.CompressedStream(self._resolver_context)
file_object.open(path_spec=self._compressed_stream_path_spec)

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

file_object.close()

def testRead(self):
"""Test the read functionality."""
file_object = compressed_stream_io.CompressedStream(self._resolver_context)
Expand Down Expand Up @@ -183,6 +206,17 @@ def testSeek(self):

file_object.close()

# TODO: Test SEEK_CUR after open.

# Test SEEK_END after open.
file_object = compressed_stream_io.CompressedStream(self._resolver_context)
file_object.open(path_spec=self._compressed_stream_path_spec)

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

file_object.close()

def testRead(self):
"""Test the read functionality."""
file_object = compressed_stream_io.CompressedStream(self._resolver_context)
Expand Down Expand Up @@ -240,6 +274,17 @@ def testSeek(self):

file_object.close()

# TODO: Test SEEK_CUR after open.

# Test SEEK_END after open.
file_object = compressed_stream_io.CompressedStream(self._resolver_context)
file_object.open(path_spec=self._compressed_stream_path_spec)

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

file_object.close()

def testRead(self):
"""Test the read functionality."""
file_object = compressed_stream_io.CompressedStream(self._resolver_context)
Expand Down
34 changes: 34 additions & 0 deletions tests/file_io/encoded_stream_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
"""Tests for the encoded stream file-like object."""

import os
import unittest

from dfvfs.file_io import encoded_stream_io
Expand Down Expand Up @@ -62,6 +63,17 @@ def testSeek(self):

file_object.close()

# TODO: Test SEEK_CUR after open.

# Test SEEK_END after open.
file_object = encoded_stream_io.EncodedStream(self._resolver_context)
file_object.open(path_spec=self._encoded_stream_path_spec)

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

file_object.close()

def testRead(self):
"""Test the read functionality."""
file_object = encoded_stream_io.EncodedStream(self._resolver_context)
Expand Down Expand Up @@ -119,6 +131,17 @@ def testSeek(self):

file_object.close()

# TODO: Test SEEK_CUR after open.

# Test SEEK_END after open.
file_object = encoded_stream_io.EncodedStream(self._resolver_context)
file_object.open(path_spec=self._encoded_stream_path_spec)

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

file_object.close()

def testRead(self):
"""Test the read functionality."""
file_object = encoded_stream_io.EncodedStream(self._resolver_context)
Expand Down Expand Up @@ -176,6 +199,17 @@ def testSeek(self):

file_object.close()

# TODO: Test SEEK_CUR after open.

# Test SEEK_END after open.
file_object = encoded_stream_io.EncodedStream(self._resolver_context)
file_object.open(path_spec=self._encoded_stream_path_spec)

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

file_object.close()

def testRead(self):
"""Test the read functionality."""
file_object = encoded_stream_io.EncodedStream(self._resolver_context)
Expand Down
56 changes: 56 additions & 0 deletions tests/file_io/encrypted_stream_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
"""Tests for the encrypted stream file-like object."""

import os
import unittest

from dfvfs.file_io import encrypted_stream_io
Expand Down Expand Up @@ -77,6 +78,17 @@ def testSeek(self):

file_object.close()

# TODO: Test SEEK_CUR after open.

# Test SEEK_END after open.
file_object = encrypted_stream_io.EncryptedStream(self._resolver_context)
file_object.open(path_spec=self._encrypted_stream_path_spec)

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

file_object.close()

def testRead(self):
"""Test the read functionality."""
file_object = encrypted_stream_io.EncryptedStream(self._resolver_context)
Expand Down Expand Up @@ -144,6 +156,17 @@ def testSeek(self):

file_object.close()

# TODO: Test SEEK_CUR after open.

# Test SEEK_END after open.
file_object = encrypted_stream_io.EncryptedStream(self._resolver_context)
file_object.open(path_spec=self._encrypted_stream_path_spec)

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

file_object.close()

def testRead(self):
"""Test the read functionality."""
file_object = encrypted_stream_io.EncryptedStream(self._resolver_context)
Expand Down Expand Up @@ -215,6 +238,17 @@ def testSeek(self):

file_object.close()

# TODO: Test SEEK_CUR after open.

# Test SEEK_END after open.
file_object = encrypted_stream_io.EncryptedStream(self._resolver_context)
file_object.open(path_spec=self._encrypted_stream_path_spec)

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

file_object.close()

def testRead(self):
"""Test the read functionality."""
file_object = encrypted_stream_io.EncryptedStream(self._resolver_context)
Expand Down Expand Up @@ -286,6 +320,17 @@ def testSeek(self):

file_object.close()

# TODO: Test SEEK_CUR after open.

# Test SEEK_END after open.
file_object = encrypted_stream_io.EncryptedStream(self._resolver_context)
file_object.open(path_spec=self._encrypted_stream_path_spec)

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

file_object.close()

def testRead(self):
"""Test the read functionality."""
file_object = encrypted_stream_io.EncryptedStream(self._resolver_context)
Expand Down Expand Up @@ -349,6 +394,17 @@ def testSeek(self):

file_object.close()

# TODO: Test SEEK_CUR after open.

# Test SEEK_END after open.
file_object = encrypted_stream_io.EncryptedStream(self._resolver_context)
file_object.open(path_spec=self._encrypted_stream_path_spec)

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

file_object.close()

def testRead(self):
"""Test the read functionality."""
file_object = encrypted_stream_io.EncryptedStream(self._resolver_context)
Expand Down

0 comments on commit a889480

Please sign in to comment.