Skip to content

Commit 927d52b

Browse files
Feat: Add fuzz testing (#82)
* Add fuzz testing * Fix lint errors in test_fuzz.py * Fix formatting issues --------- Co-authored-by: acul71 <34693171+acul71@users.noreply.github.com>
1 parent ff69fa5 commit 927d52b

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

newsfragments/72.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add property-based fuzz testing using `hypothesis` to ensure CID parsing and encoding are robust against arbitrary input and do not crash.

tests/test_fuzz.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from hypothesis import (
2+
given,
3+
settings,
4+
strategies as st,
5+
)
6+
7+
from cid import make_cid
8+
9+
10+
@given(st.binary(max_size=200))
11+
@settings(max_examples=1000)
12+
def test_from_bytes_never_crashes(data):
13+
"""from_bytes should raise ValueError, KeyError, or TypeError, not crash."""
14+
try:
15+
make_cid(data)
16+
except (ValueError, KeyError, TypeError):
17+
pass # Any exception is fine, just no crashes
18+
19+
20+
@given(st.binary(min_size=34, max_size=100))
21+
@settings(max_examples=500)
22+
def test_cid_roundtrip_never_crashes(data):
23+
"""CID encode/decode should never crash."""
24+
try:
25+
cid = make_cid(data)
26+
_ = cid.encode()
27+
_ = str(cid)
28+
_ = cid.prefix()
29+
except (ValueError, KeyError, TypeError):
30+
pass

0 commit comments

Comments
 (0)