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: recursive
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
Expand Down Expand Up @@ -50,6 +52,8 @@ jobs:
fail-fast: false
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- 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 "spec/multihash"]
path = spec/multihash
url = https://github.com/multiformats/multihash.git
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ recursive-exclude * __pycache__
recursive-exclude * *.py[co]

recursive-include docs *.rst conf.py Makefile make.bat *.jpg *.png *.gif
graft spec
1 change: 1 addition & 0 deletions newsfragments/53.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add official multihash spec vector tests validating :func:`~multihash.sum` against ``spec/multihash`` test cases.
1 change: 1 addition & 0 deletions spec/multihash
Submodule multihash added at b43ec1
49 changes: 49 additions & 0 deletions tests/test_spec_vectors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Official multihash spec vector tests."""

import csv
from binascii import hexlify
from pathlib import Path

import pytest

from multihash import FuncReg, HashComputationError, sum

SPEC_VECTORS = Path(__file__).resolve().parents[1] / "spec" / "multihash" / "tests" / "values" / "test_cases.csv"

# Spec CSV uses "sha3" as an alias for sha3-512 (same as go-multihash).
_ALGORITHM_ALIASES = {
"sha3": "sha3-512",
}


def _load_cases():
if not SPEC_VECTORS.is_file():
pytest.skip(f"spec vectors not found at {SPEC_VECTORS} (init git submodules?)")
with SPEC_VECTORS.open(newline="") as f:
reader = csv.reader(f)
next(reader) # header
return list(reader)


@pytest.mark.parametrize("algorithm,bits_str,input_hex,expected_hex", _load_cases())
def test_spec_vectors(algorithm, bits_str, input_hex, expected_hex):
"""Validate sum() against official multihash test vectors.

The CSV ``input`` column is hashed as UTF-8/ASCII of the hex string itself
(not as decoded binary), matching go-multihash ``TestSpecVectors``.
"""
algorithm = _ALGORITHM_ALIASES.get(algorithm, algorithm)
length_bytes = int(bits_str) // 8
data = input_hex.encode()

try:
FuncReg.get(algorithm)
except KeyError:
pytest.skip(f"hash {algorithm} not registered")

try:
actual = sum(data, algorithm, length=length_bytes)
except (HashComputationError, KeyError, ValueError) as exc:
pytest.skip(f"hash {algorithm} unavailable: {exc}")

assert hexlify(actual.encode()).decode() == expected_hex
Loading