Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/48.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add Hypothesis-based fuzz tests so prefix and Code parsing helpers never crash on random input.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dev = [
"bump-my-version>=1.2.0",
"mypy",
"pre-commit",
"hypothesis",
"pytest",
"pytest-runner",
"ruff",
Expand Down
41 changes: 41 additions & 0 deletions tests/test_fuzz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Property-based / fuzz tests for parsing helpers.

These tests assert that malformed or random input raises documented
exceptions (or returns a bool) rather than crashing with unexpected errors.
"""

from hypothesis import given
from hypothesis import strategies as st

from multicodec import Code, extract_prefix, get_codec, is_codec


@given(st.binary(max_size=100))
def test_extract_prefix_never_crashes(data: bytes) -> None:
"""extract_prefix should raise ValueError, not crash."""
try:
extract_prefix(data)
except ValueError:
pass


@given(st.binary(max_size=100))
def test_get_codec_never_crashes(data: bytes) -> None:
try:
get_codec(data)
except ValueError:
pass


@given(st.text(max_size=100))
def test_code_from_string_never_crashes(text: str) -> None:
try:
Code.from_string(text)
except ValueError:
pass


@given(st.text(max_size=100))
def test_is_codec_never_crashes(text: str) -> None:
result = is_codec(text)
assert isinstance(result, bool)
Loading