Skip to content

Commit

Permalink
Merge pull request #27 from miurahr/patch-multiblock-timeout
Browse files Browse the repository at this point in the history
Add new test and timeout
  • Loading branch information
miurahr authored Sep 29, 2019
2 parents 23045b6 + 20faf80 commit fc83ef6
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 39 deletions.
4 changes: 4 additions & 0 deletions py7zr/compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ def decompress(self, fp: BinaryIO, folder, fileish: Handler,
read_size = min(Configuration.get('read_blocksize'), decompressor.remaining_size)
inp = fp.read(read_size)
tmp = decompressor.decompress(inp, max_length)
if len(tmp) == 0:
for _ in range(out_remaining):
fileish.write(b'\x00')
break
else:
tmp = decompressor.decompress(b'', max_length)
if out_remaining >= len(tmp):
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ texttable
pytest
pytest-benchmark[histogram]
pytest-cov
pytest-timeout
coverage
coveralls
flake8
Expand Down
File renamed without changes.
File renamed without changes.
Binary file added tests/data/mblock_3.7z
Binary file not shown.
File renamed without changes.
40 changes: 1 addition & 39 deletions tests/test_advanced.py → tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import py7zr
import pytest
from py7zr import UnsupportedCompressionMethodError, unpack_7zarchive
from py7zr import unpack_7zarchive

from . import check_archive, decode_all

Expand All @@ -31,13 +31,6 @@ def test_solid():
check_archive(archive)


@pytest.mark.files
@pytest.mark.xfail(raises=UnsupportedCompressionMethodError)
def test_copy():
""" test loading of copy compressed files.(help wanted)"""
check_archive(py7zr.SevenZipFile(open(os.path.join(testdata_path, 'copy.7z'), 'rb')))


@pytest.mark.files
def test_empty():
# decompress empty archive
Expand All @@ -56,18 +49,6 @@ def test_github_14():
shutil.rmtree(tmpdir)


@pytest.mark.files
def test_github_14_multi():
""" multiple unnamed objects."""
archive = py7zr.SevenZipFile(open(os.path.join(testdata_path, 'github_14_multi.7z'), 'rb'))
assert archive.getnames() == ['github_14_multi', 'github_14_multi']
tmpdir = tempfile.mkdtemp()
archive.extractall(path=tmpdir)
with open(os.path.join(tmpdir, 'github_14_multi'), 'rb') as f:
assert f.read() == bytes('Hello GitHub issue #14 2/2.\n', 'ascii')
shutil.rmtree(tmpdir)


@pytest.mark.files
def _test_umlaut_archive(filename):
archive = py7zr.SevenZipFile(open(os.path.join(testdata_path, filename), 'rb'))
Expand Down Expand Up @@ -153,25 +134,6 @@ def test_zerosize():
shutil.rmtree(tmpdir)


@pytest.mark.files
def test_zerosize2():
archive = py7zr.SevenZipFile(open(os.path.join(testdata_path, 'test_6.7z'), 'rb'))
tmpdir = tempfile.mkdtemp()
archive.extractall(path=tmpdir)
shutil.rmtree(tmpdir)


@pytest.mark.files
def test_multiblock():
archive = py7zr.SevenZipFile(open(os.path.join(testdata_path, 'mblock.7z'), 'rb'))
tmpdir = tempfile.mkdtemp()
archive.extractall(path=tmpdir)
m = hashlib.sha256()
m.update(open(os.path.join(tmpdir, 'bin/7zdec.exe'), 'rb').read())
assert m.digest() == binascii.unhexlify('e14d8201c5c0d1049e717a63898a3b1c7ce4054a24871daebaa717da64dcaff5')
shutil.rmtree(tmpdir, onerror=rmtree_onerror)


@pytest.mark.api
def test_register_unpack_archive():
shutil.register_unpack_format('7zip', ['.7z'], unpack_7zarchive)
Expand Down
17 changes: 17 additions & 0 deletions tests/test_files_lzmamethods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os

import py7zr
import pytest
from py7zr import UnsupportedCompressionMethodError

from . import check_archive

testdata_path = os.path.join(os.path.dirname(__file__), 'data')
os.umask(0o022)


@pytest.mark.files
@pytest.mark.xfail(raises=UnsupportedCompressionMethodError)
def test_copy():
""" test loading of copy compressed files.(help wanted)"""
check_archive(py7zr.SevenZipFile(open(os.path.join(testdata_path, 'copy.7z'), 'rb')))
61 changes: 61 additions & 0 deletions tests/test_files_multi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import binascii
import hashlib
import os
import shutil
import stat
import tempfile

import py7zr
import pytest

testdata_path = os.path.join(os.path.dirname(__file__), 'data')
os.umask(0o022)


def rmtree_onerror(func, path, exc_info):
if not os.access(path, os.W_OK):
os.chmod(path, stat.S_IWUSR)
func(path)
else:
raise


@pytest.mark.files
def test_github_14_multi():
""" multiple unnamed objects."""
archive = py7zr.SevenZipFile(open(os.path.join(testdata_path, 'github_14_multi.7z'), 'rb'))
assert archive.getnames() == ['github_14_multi', 'github_14_multi']
tmpdir = tempfile.mkdtemp()
archive.extractall(path=tmpdir)
with open(os.path.join(tmpdir, 'github_14_multi'), 'rb') as f:
assert f.read() == bytes('Hello GitHub issue #14 2/2.\n', 'ascii')
shutil.rmtree(tmpdir)


@pytest.mark.files
def test_multiblock():
archive = py7zr.SevenZipFile(open(os.path.join(testdata_path, 'mblock_1.7z'), 'rb'))
tmpdir = tempfile.mkdtemp()
archive.extractall(path=tmpdir)
m = hashlib.sha256()
m.update(open(os.path.join(tmpdir, 'bin/7zdec.exe'), 'rb').read())
assert m.digest() == binascii.unhexlify('e14d8201c5c0d1049e717a63898a3b1c7ce4054a24871daebaa717da64dcaff5')
shutil.rmtree(tmpdir, onerror=rmtree_onerror)


@pytest.mark.files
def test_multiblock_zerosize():
archive = py7zr.SevenZipFile(open(os.path.join(testdata_path, 'mblock_2.7z'), 'rb'))
tmpdir = tempfile.mkdtemp()
archive.extractall(path=tmpdir)
shutil.rmtree(tmpdir)


@pytest.mark.files
@pytest.mark.timeout(5, method='thread')
@pytest.mark.xfail()
def test_multiblock_last_padding():
archive = py7zr.SevenZipFile(open(os.path.join(testdata_path, 'mblock_3.7z'), 'rb'))
tmpdir = tempfile.mkdtemp()
archive.extractall(path=tmpdir)
shutil.rmtree(tmpdir)
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ basepython =
{clean,check,docs,report,coveralls}: {env:TOXPYTHON:python3}
python_files = test*.py
addopts = --benchmark-autosave --cov-report=term-missing:skip-covered --cov-append --cov=py7zr
timeout = 300
markers =
basic: mark a test as a basic feature test.
files: mark a test as a test with actual files.
Expand Down

0 comments on commit fc83ef6

Please sign in to comment.