Skip to content

No test verifying invalid input is rejected for every encoding #41

Description

@sumanjeet0012

There are no tests verifying that decoding invalid characters (non-alphabet characters, emoji, control characters) fails for every encoding. go-multibase tests this systematically in TestRoundTrip.

Problem

The current test suite only tests that valid encoded data decodes correctly. It doesn't test that invalid data is properly rejected. This means a converter could silently accept garbage input.

go-multibase tests invalid input rejection:

func TestRoundTrip(t *testing.T) {
    for base := range EncodingToStr {
        // These should all fail to decode
        _, _, err := Decode(string(rune(base)) + "\u00A0")    // low-unicode
        if err == nil { t.Fatal("should fail on low-unicode") }

        _, _, err = Decode(string(rune(base)) + "\u1F4A8")    // emoji
        if err == nil { t.Fatal("should fail on emoji") }

        _, _, err = Decode(string(rune(base)) + "!")          // punctuation
        if err == nil { t.Fatal("should fail on punctuation") }

        _, _, err = Decode(string(rune(base)) + "\xA0")       // high-latin1
        if err == nil { t.Fatal("should fail on high-latin1") }
    }
}

Proposed Solution

Add tests to tests/test_roundtrip.py:

INVALID_SUFFIXES = [
    ("\u00A0", "low-unicode"),
    ("\U0001F4A8", "emoji"),
    ("!", "punctuation"),
    ("\u00FF", "high-latin1"),
]

@pytest.mark.parametrize("encoding_info", ENCODINGS, ids=lambda e: e.encoding)
@pytest.mark.parametrize("suffix,label", INVALID_SUFFIXES)
def test_invalid_input_rejected(encoding_info, suffix, label):
    """Decoding invalid characters should raise an error."""
    if encoding_info.encoding == "identity":
        pytest.skip("identity accepts all bytes")
    if encoding_info.encoding == "base256emoji":
        pytest.skip("base256emoji has its own alphabet")

    prefix = encoding_info.code.decode("utf-8") if isinstance(encoding_info.code, bytes) else encoding_info.code
    invalid_data = prefix + suffix

    with pytest.raises((DecodingError, InvalidMultibaseStringError, ValueError)):
        decode(invalid_data)

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions