| 1 |
import hashlib |
| 2 |
import os |
| 3 |
import pathlib |
| 4 |
import zipfile |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
import inflate64 |
| 9 |
|
| 10 |
testdata_path = pathlib.Path(os.path.dirname(__file__)).joinpath("data") |
| 11 |
testdata = testdata_path.joinpath("test-file.zip") |
| 12 |
largedata = testdata_path.joinpath("large-file.zip") |
| 13 |
srcdata = testdata_path.joinpath("src.zip") |
| 14 |
|
| 15 |
|
| 16 |
@pytest.mark.parametrize( |
| 17 |
"fname,offset,length", |
| 18 |
[ |
| 19 |
("test-file.1", 41, 3096), |
| 20 |
("test-file.2", 36434, 3112), |
| 21 |
("test-file.3", 42984, 3125), |
| 22 |
("test-file.4", 46150, 3143), |
| 23 |
("test-file.5", 49334, 3156), |
| 24 |
("test-file.6", 52531, 3169), |
| 25 |
("test-file.7", 55741, 3186), |
| 26 |
("test-file.8", 58968, 3198), |
| 27 |
("test-file.9", 62207, 3210), |
| 28 |
("test-file.10", 3179, 3227), |
| 29 |
("test-file.11", 6448, 3237), |
| 30 |
("test-file.12", 9727, 3249), |
| 31 |
("test-file.13", 13018, 3266), |
| 32 |
("test-file.14", 16326, 3277), |
| 33 |
("test-file.15", 19645, 3289), |
| 34 |
("test-file.16", 22976, 3304), |
| 35 |
("test-file.17", 26322, 3316), |
| 36 |
("test-file.18", 29680, 3328), |
| 37 |
("test-file.19", 33050, 3343), |
| 38 |
("test-file.20", 39588, 3355), |
| 39 |
], |
| 40 |
) |
| 41 |
def test_decompress(fname, offset, length): |
| 42 |
""" |
| 43 |
Test with various size of data. |
| 44 |
:param fname: file name of test file. |
| 45 |
:param offset: data offset in target zip file. |
| 46 |
:param length: compressed size of data. |
| 47 |
""" |
| 48 |
with testdata.open("rb") as f: |
| 49 |
_ = f.seek(offset, os.SEEK_SET) |
| 50 |
data = f.read(length) |
| 51 |
with zipfile.ZipFile(srcdata) as z: |
| 52 |
expected = z.read(fname) |
| 53 |
decompressor = inflate64.Inflater() |
| 54 |
result = decompressor.inflate(data) |
| 55 |
assert len(result) == len(expected) |
| 56 |
assert result == expected |
| 57 |
|
| 58 |
|
| 59 |
def test_decompress_larger(): |
| 60 |
""" |
| 61 |
Test with larger file with looping inflate() method. |
| 62 |
""" |
| 63 |
fname = "10000SalesRecords.csv" |
| 64 |
offset = 51 |
| 65 |
remaining = 351298 |
| 66 |
BS = 8192 |
| 67 |
|
| 68 |
m = hashlib.sha256() |
| 69 |
with zipfile.ZipFile(srcdata) as z: |
| 70 |
m.update(z.read(fname)) |
| 71 |
expected = m.digest() |
| 72 |
|
| 73 |
m = hashlib.sha256() |
| 74 |
decompressor = inflate64.Inflater() |
| 75 |
assert not decompressor.eof |
| 76 |
with largedata.open("rb") as f: |
| 77 |
_ = f.seek(offset, os.SEEK_SET) |
| 78 |
while remaining > 0: |
| 79 |
length = min(BS, remaining) |
| 80 |
data = f.read(length) |
| 81 |
m.update(decompressor.inflate(data)) |
| 82 |
remaining -= len(data) |
| 83 |
assert decompressor.eof |
| 84 |
assert m.digest() == expected |