Skip to content

No round-trip tests with random data and leading zeros #40

Description

@sumanjeet0012

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

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