diff --git a/newsfragments/72.feature.rst b/newsfragments/72.feature.rst new file mode 100644 index 0000000..70a2d7f --- /dev/null +++ b/newsfragments/72.feature.rst @@ -0,0 +1 @@ +Add property-based fuzz testing using `hypothesis` to ensure CID parsing and encoding are robust against arbitrary input and do not crash. diff --git a/tests/test_fuzz.py b/tests/test_fuzz.py new file mode 100644 index 0000000..a3a5983 --- /dev/null +++ b/tests/test_fuzz.py @@ -0,0 +1,30 @@ +from hypothesis import ( + given, + settings, + strategies as st, +) + +from cid import make_cid + + +@given(st.binary(max_size=200)) +@settings(max_examples=1000) +def test_from_bytes_never_crashes(data): + """from_bytes should raise ValueError, KeyError, or TypeError, not crash.""" + try: + make_cid(data) + except (ValueError, KeyError, TypeError): + pass # Any exception is fine, just no crashes + + +@given(st.binary(min_size=34, max_size=100)) +@settings(max_examples=500) +def test_cid_roundtrip_never_crashes(data): + """CID encode/decode should never crash.""" + try: + cid = make_cid(data) + _ = cid.encode() + _ = str(cid) + _ = cid.prefix() + except (ValueError, KeyError, TypeError): + pass