Skip to content

Commit

Permalink
test nonozero mac validation
Browse files Browse the repository at this point in the history
  • Loading branch information
mdekstrand committed Feb 18, 2024
1 parent a37d3e4 commit 7ed364f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion binpickle/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def decode(cls, buf: bytes | bytearray | memoryview, *, verify: bool = True) ->
"""
off, len, ck, mac = TRAILER_FORMAT.unpack(buf)
if verify and mac != b"\0" * 32:
raise ValueError("nonzero MACs not supported")
raise FormatError("nonzero MACs not supported")
return cls(off, len, ck, mac)


Expand Down
29 changes: 28 additions & 1 deletion tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,38 @@
import pytest

from binpickle import BinPickleFile, dump
from binpickle.errors import IntegrityError
from binpickle.errors import FormatError, IntegrityError

_log = logging.getLogger(__name__)


def test_verfy_unsupported_mac(tmp_path, rng: np.random.Generator):
"Nonzero MAC should fail"
file = tmp_path / "data.bpk"

df = pd.DataFrame(
{
"key": np.arange(0, 5000),
"count": rng.integers(0, 1000, 5000),
"score": rng.normal(10, 2, 5000),
}
)

dump(df, file, codecs=["lz4"])

# corrupt the file
stat = os.stat(file)
_log.info("%s: length %d", file, stat.st_size)
with open(file, "r+b") as f:
f.seek(stat.st_size - 4)
f.write(b"XX")

# try to read the file
with pytest.raises(FormatError, match=r"nonzero MACs"):
with BinPickleFile(file) as _bpf:
pass


def test_verfy_index(tmp_path, rng: np.random.Generator):
"Index hash mismatch should fail"
file = tmp_path / "data.bpk"
Expand Down

0 comments on commit 7ed364f

Please sign in to comment.