From 375ec9b7a2e7c7d394713c981b3b0c63fa9e8733 Mon Sep 17 00:00:00 2001 From: acul71 Date: Mon, 7 Sep 2026 23:06:24 +0200 Subject: [PATCH] Add tests rejecting invalid alphabet input per encoding Cover decode() with invalid suffixes across supported encodings so malformed payloads raise DecodingError instead of being silently accepted. Co-authored-by: Cursor --- newsfragments/41.misc.rst | 1 + tests/test_roundtrip.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 newsfragments/41.misc.rst create mode 100644 tests/test_roundtrip.py diff --git a/newsfragments/41.misc.rst b/newsfragments/41.misc.rst new file mode 100644 index 0000000..1efcffe --- /dev/null +++ b/newsfragments/41.misc.rst @@ -0,0 +1 @@ +Added tests that verify invalid alphabet input is rejected for every encoding. diff --git a/tests/test_roundtrip.py b/tests/test_roundtrip.py new file mode 100644 index 0000000..4e10f2a --- /dev/null +++ b/tests/test_roundtrip.py @@ -0,0 +1,32 @@ +"""Round-trip and invalid-input rejection tests for multibase encodings.""" + +import pytest + +from multibase import ENCODINGS, DecodingError, decode + +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, + ids=[label for _, label in INVALID_SUFFIXES], +) +def test_invalid_input_rejected(encoding_info, suffix, label): + """Decoding invalid alphabet characters should raise DecodingError.""" + if encoding_info.encoding == "identity": + pytest.skip("identity accepts all bytes") + if encoding_info.encoding == "base256emoji": + pytest.skip("base256emoji has its own alphabet; emoji suffixes are not reliably invalid") + + prefix = encoding_info.code.decode("utf-8") + invalid_data = prefix + suffix + + with pytest.raises(DecodingError): + decode(invalid_data)