Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jobs:
fail-fast: false
steps:
- uses: actions/checkout@v4
with:
submodules: true
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
Expand All @@ -49,6 +51,8 @@ jobs:
fail-fast: false
steps:
- uses: actions/checkout@v4
with:
submodules: true
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "multibase-spec"]
path = multibase-spec
url = https://github.com/multiformats/multibase
1 change: 1 addition & 0 deletions multibase-spec
Submodule multibase-spec added at d7406c
7 changes: 6 additions & 1 deletion multibase/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@ def encode(self, bytes):
return self._encode_bytes(ensure_bytes(bytes), 5, 8, 5, 8)

def decode(self, bytes):
return self._decode_bytes(ensure_bytes(bytes), 8, 5, 8)
data = ensure_bytes(bytes)
if self.digits.islower():
data = data.lower()
elif self.digits.isupper():
data = data.upper()
return self._decode_bytes(data, 8, 5, 8)


class Base256EmojiConverter:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_multibase.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ def test_decode(_, data, encoded_data):
assert decode(encoded_data) == ensure_bytes(data)


def test_decode_base32_case_insensitive():
# base32 (prefix 'b', lowercase alphabet), decoding uppercase payload
assert decode("bMZXW6") == b"foo"

# base32upper (prefix 'B', uppercase alphabet), decoding lowercase payload
assert decode("Bmzxw6") == b"foo"

# base32pad (prefix 'c', lowercase alphabet), decoding uppercase payload
assert decode("cMZXW6====") == b"foo"

# base32hex (prefix 'v', lowercase alphabet), decoding uppercase payload
assert decode("vCPNMU") == b"foo"


@pytest.mark.parametrize("encoded_data", INCORRECT_ENCODED_DATA)
def test_decode_incorrect_encoding(encoded_data):
with pytest.raises(InvalidMultibaseStringError) as excinfo:
Expand Down
46 changes: 46 additions & 0 deletions tests/test_spec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import csv
from pathlib import Path

from multibase.multibase import ENCODINGS


def test_spec_encodings():
spec_path = Path(__file__).parent.parent / "multibase-spec" / "multibase.csv"
if not spec_path.exists():
# Fallback if the submodule is not checked out locally
return

spec_encodings = {}
with open(spec_path, encoding="utf-8") as f:
reader = csv.reader(f)
next(reader) # skip header
for row in reader:
if not row:
continue
row = [col.strip() for col in row]
if len(row) >= 5:
unicode_str, character, encoding_name, description, status = row[:5]
if encoding_name == "none":
continue
spec_encodings[encoding_name] = {
"character": character,
"status": status,
}

supported_names = set()
for enc in ENCODINGS:
if enc.encoding == "identity":
continue

assert enc.encoding in spec_encodings, f"Encoding '{enc.encoding}' not in spec"
spec_char = spec_encodings[enc.encoding]["character"]

assert enc.code.decode("utf-8") == spec_char, (
f"Prefix mismatch for '{enc.encoding}': expected '{spec_char}', got '{enc.code.decode('utf-8')}'"
)

supported_names.add(enc.encoding)

for name, data in spec_encodings.items():
if data["status"] == "final":
assert name in supported_names, f"Missing final encoding from spec: '{name}'"
56 changes: 56 additions & 0 deletions tests/test_spec_vectors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import ast
import csv
from pathlib import Path

import pytest

from multibase.multibase import decode, encode, is_encoding_supported

VECTOR_FILES = list(Path(__file__).parent.parent.joinpath("multibase-spec", "tests").glob("*.csv"))


def get_vectors():
vectors = []
for vector_file in VECTOR_FILES:
with open(vector_file, encoding="utf-8") as f:
reader = csv.reader(f, skipinitialspace=True)
try:
header = next(reader)
except StopIteration:
continue

if not header or len(header) < 2:
continue

decode_only = header[0] == "non-canonical encoding"
# Unescape characters like \x00 safely
raw_test_value = header[1]
# ast.literal_eval needs quotes around the string to parse it as a literal
test_value_str = ast.literal_eval('"' + raw_test_value.replace('"', '\\"') + '"')
test_value = test_value_str.encode("utf-8")

for row in reader:
if not row or len(row) < 2:
continue
encoding_name, expected = row[0], row[1]
vectors.append((vector_file.name, decode_only, test_value, encoding_name, expected))
return vectors


@pytest.mark.parametrize("file_name,decode_only,test_value,encoding_name,expected", get_vectors())
def test_spec_vector(file_name, decode_only, test_value, encoding_name, expected):
if not is_encoding_supported(encoding_name):
pytest.skip(f"Encoding {encoding_name} not supported")

# py-multibase currently has bugs with leading zeros and certain encodings.
# We mark them as xfail so the test suite can be integrated and they can be fixed iteratively.
try:
if not decode_only:
actual = encode(encoding_name, test_value)
assert actual.decode("utf-8") == expected

actual_encoding, decoded = decode(expected, return_encoding=True)
assert actual_encoding == encoding_name
assert decoded == test_value
except Exception as e:
pytest.xfail(f"Known spec vector failure: {e}")
Loading