| 1 |
import io |
| 2 |
import os |
| 3 |
import pathlib |
| 4 |
import sys |
| 5 |
|
| 6 |
from hypothesis import given |
| 7 |
from hypothesis import strategies as st |
| 8 |
from pytest import fixture |
| 9 |
|
| 10 |
import multivolumefile as MV |
| 11 |
|
| 12 |
counter = 0 |
| 13 |
|
| 14 |
|
| 15 |
@fixture(scope="session") |
| 16 |
def basepath(tmp_path_factory): |
| 17 |
return tmp_path_factory.getbasetemp() |
| 18 |
|
| 19 |
|
| 20 |
def numbered_tempdir(base: pathlib.Path, common, suffix): |
| 21 |
global counter |
| 22 |
counter += 1 |
| 23 |
tmpdir = base.joinpath(common).joinpath("{0}{1}".format(suffix, counter)) |
| 24 |
os.makedirs(tmpdir, exist_ok=True) |
| 25 |
return tmpdir |
| 26 |
|
| 27 |
|
| 28 |
@given( |
| 29 |
obj=st.binary(min_size=10, max_size=10 << 10), |
| 30 |
volume=st.integers(min_value=5, max_value=1 << 10), |
| 31 |
) |
| 32 |
def test_fuzzer_once(obj, volume, basepath): |
| 33 |
target = numbered_tempdir(basepath, "test_fuzzer", "once").joinpath("target.bin") |
| 34 |
with MV.open(target, mode="wb", volume=volume) as f: |
| 35 |
f.write(obj) |
| 36 |
with MV.open(target, mode="rb") as f: |
| 37 |
result = f.read() |
| 38 |
assert result == obj |
| 39 |
|
| 40 |
|
| 41 |
@given( |
| 42 |
obj=st.binary(min_size=10, max_size=10 << 10), |
| 43 |
volume=st.integers(min_value=5, max_value=1 << 10), |
| 44 |
wblock=st.integers(min_value=5, max_value=5 << 9), |
| 45 |
rblock=st.integers(min_value=5, max_value=5 << 9), |
| 46 |
) |
| 47 |
def test_fuzzer_block(obj, volume, wblock, rblock, basepath): |
| 48 |
target = numbered_tempdir(basepath, "test_fuzzer", "block").joinpath("target.bin") |
| 49 |
src = io.BytesIO(obj) |
| 50 |
result = b"" |
| 51 |
with MV.open(target, mode="wb", volume=volume) as f: |
| 52 |
data = src.read(wblock) |
| 53 |
while len(data) > 0: |
| 54 |
f.write(data) |
| 55 |
data = src.read(wblock) |
| 56 |
with MV.open(target, mode="rb") as f: |
| 57 |
data = f.read(rblock) |
| 58 |
while len(data) > 0: |
| 59 |
result += data |
| 60 |
data = f.read(rblock) |
| 61 |
assert result == obj |
| 62 |
|
| 63 |
|
| 64 |
if __name__ == "__main__": |
| 65 |
import atheris |
| 66 |
|
| 67 |
atheris.Setup(sys.argv, test_fuzzer_block.hypothesis.fuzz_one_input) |
| 68 |
atheris.Fuzz() |