Skip to content

Add fuzz testing for prefix operations and Code parsing #48

Description

@sumanjeet0012

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

  1. 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)
  2. Add hypothesis to dev dependencies.

Related

  • Dev dependency to add: hypothesis
  • Test directory: tests/

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions