From cff07e787a94d97ca150ce848675c0ab9fe28783 Mon Sep 17 00:00:00 2001 From: sumanjeet0012 Date: Mon, 13 Jul 2026 22:48:07 +0530 Subject: [PATCH 1/3] Add fuzz testing --- newsfragments/72.feature.rst | 1 + tests/test_fuzz.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 newsfragments/72.feature.rst create mode 100644 tests/test_fuzz.py 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..a0e23aa --- /dev/null +++ b/tests/test_fuzz.py @@ -0,0 +1,24 @@ +import pytest +from hypothesis import given, strategies as st, settings +from cid import make_cid, CIDv1, CIDv0 + +@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 From 89618f1bc28f65b1d9c78ab9e71f1e24a92e3f1e Mon Sep 17 00:00:00 2001 From: sumanjeet0012 Date: Mon, 13 Jul 2026 22:53:23 +0530 Subject: [PATCH 2/3] Fix lint errors in test_fuzz.py --- tests/test_fuzz.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/test_fuzz.py b/tests/test_fuzz.py index a0e23aa..a10c650 100644 --- a/tests/test_fuzz.py +++ b/tests/test_fuzz.py @@ -1,6 +1,11 @@ -import pytest -from hypothesis import given, strategies as st, settings -from cid import make_cid, CIDv1, CIDv0 +from hypothesis import ( + given, + settings, + strategies as st, +) + +from cid import make_cid + @given(st.binary(max_size=200)) @settings(max_examples=1000) From b69adbe5230f38edd86e8bc94e5f41c07dc6b977 Mon Sep 17 00:00:00 2001 From: sumanjeet0012 Date: Mon, 13 Jul 2026 22:55:49 +0530 Subject: [PATCH 3/3] Fix formatting issues --- tests/test_fuzz.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_fuzz.py b/tests/test_fuzz.py index a10c650..a3a5983 100644 --- a/tests/test_fuzz.py +++ b/tests/test_fuzz.py @@ -16,6 +16,7 @@ def test_from_bytes_never_crashes(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):