There is no fuzz testing for extract_prefix(), get_codec(), Code.from_string(), or other parsing functions. go-multicodec includes a fuzz target that exercises Cast(), String(), Prefix(), and JSON round-trips with random input.
Problem
Parsing functions like extract_prefix() and Code.from_string() accept arbitrary input but are only tested with valid or specifically-crafted invalid inputs. Random/malformed input could trigger unhandled exceptions, infinite loops, or excessive memory allocation.
go-multicodec has a fuzz target (cid_fuzz.go) that:
func Fuzz(data []byte) int {
cid, err := Cast(data)
if err != nil { return 0 }
_ = cid.Bytes()
_ = cid.String()
// ... exercises all methods with random input
}
Proposed Solution
-
Create tests/test_fuzz.py using hypothesis for property-based testing:
from hypothesis import given, 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):
"""extract_prefix should raise ValueError, not crash."""
try:
extract_prefix(data)
except ValueError:
pass # Expected
@given(st.binary(max_size=100))
def test_get_codec_never_crashes(data):
try:
get_codec(data)
except ValueError:
pass
@given(st.text(max_size=100))
def test_code_from_string_never_crashes(text):
try:
Code.from_string(text)
except ValueError:
pass
@given(st.text(max_size=100))
def test_is_codec_never_crashes(text):
result = is_codec(text)
assert isinstance(result, bool)
-
Add hypothesis to dev dependencies.
Related
- Dev dependency to add:
hypothesis
- Test directory:
tests/
There is no fuzz testing for
extract_prefix(),get_codec(),Code.from_string(), or other parsing functions. go-multicodec includes a fuzz target that exercisesCast(),String(),Prefix(), and JSON round-trips with random input.Problem
Parsing functions like
extract_prefix()andCode.from_string()accept arbitrary input but are only tested with valid or specifically-crafted invalid inputs. Random/malformed input could trigger unhandled exceptions, infinite loops, or excessive memory allocation.go-multicodec has a fuzz target (
cid_fuzz.go) that:Proposed Solution
Create
tests/test_fuzz.pyusinghypothesisfor property-based testing:Add
hypothesisto dev dependencies.Related
hypothesistests/