multiformats / multiformats/py-multibase
No test verifying invalid input is rejected for every encoding
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 25
- Forks
- 9
- PR merge metrics
- No merged PRs in 30d
Description
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
- Go test: go-multibase
multibase_test.goTestRoundTrip
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in tests/test_roundtrip.py by reviewing ENCODINGS and the existing round-trip tests, then inspect the decode exceptions used there. Add parametrized coverage for the listed invalid suffixes, preserving the documented skips for identity and base256emoji; done means applicable encodings reject each invalid input.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 82/100