multiformats / multiformats/py-multibase
No round-trip tests with random data and leading zeros
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 25
- Forks
- 9
- PR merge metrics
- No merged PRs in 30d
Description
The test suite only uses hardcoded test fixtures. There are no round-trip tests with random data (including leading zero bytes) that would catch encoding/decoding bugs across all 24 encodings. go-multibase has a comprehensive TestRoundTrip that does this.
Problem
Current tests use 85 hardcoded (encoding, data, expected) tuples. These cover common cases but miss:
- Leading zero bytes —
\x00\x00hello(catches Issue 1) - All-zeros data —
\x00\x00\x00\x00 - All-ones data —
\xff\xff\xff\xff - Random data of varying lengths — 1 byte, 16 bytes, 137 bytes
- Large data — 1024+ bytes
go-multibase generates random data and tests every encoding:
func TestRoundTrip(t *testing.T) {
buf := make([]byte, 137+16)
rand.Read(buf[16:]) // 16 leading zeros + 137 random bytes
for base := range EncodingToStr {
for i := 0; i < len(buf); i++ {
// Test from full buffer down to single byte
enc, _ := Encode(base, buf[i:])
_, out, _ := Decode(enc)
if !bytes.Equal(buf[i:], out) {
t.Fatal("round-trip failed")
}
}
}
}
Proposed Solution
Add tests/test_roundtrip.py:
import os
import pytest
from multibase import encode, decode, ENCODINGS
@pytest.mark.parametrize("encoding_info", ENCODINGS, ids=lambda e: e.encoding)
class TestRoundTrip:
def test_random_data(self, encoding_info):
"""Round-trip random data of various sizes."""
for size in [1, 2, 7, 16, 32, 64, 137, 256, 1024]:
data = os.urandom(size)
encoded = encode(encoding_info.encoding, data)
decoded = decode(encoded)
assert decoded == data, f"Failed for {encoding_info.encoding} size={size}"
def test_leading_zeros(self, encoding_info):
"""Round-trip data with leading zero bytes."""
for num_zeros in [1, 2, 4, 8, 16]:
data = b'\x00' * num_zeros + b'hello'
encoded = encode(encoding_info.encoding, data)
decoded = decode(encoded)
assert decoded == data, \
f"Leading zeros lost for {encoding_info.encoding} zeros={num_zeros}"
def test_all_zeros(self, encoding_info):
"""Round-trip all-zero data."""
for size in [1, 4, 16, 32]:
data = b'\x00' * size
encoded = encode(encoding_info.encoding, data)
decoded = decode(encoded)
assert decoded == data
def test_all_ones(self, encoding_info):
"""Round-trip all-0xFF data."""
for size in [1, 4, 16, 32]:
data = b'\xff' * size
encoded = encode(encoding_info.encoding, data)
decoded = decode(encoded)
assert decoded == data
Related
- Go round-trip test: go-multibase
multibase_test.goTestRoundTrip - Related issue: Issue 1 (base58 leading zeros)
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
Add tests/test_roundtrip.py using the encode, decode, and ENCODINGS entry points named in the issue. Start by reviewing the existing test suite and encoding definitions, then run the new parametrized tests across random, leading-zero, all-zero, and all-0xFF inputs. Done means every listed encoding round-trips each requested data size without losing bytes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing-qa
- Issue type
- Feature
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 86/100