Skip to content

No spec vector tests validating sum() against official test vectors #53

Description

@sumanjeet0012

There are no tests that validate sum() output against the official multihash test vectors. go-multihash has TestSpecVectors that runs vectors from spec/multihash/tests/values/test_cases.csv.

Problem

The current test suite uses hardcoded test fixtures. There is no automated validation against the official test vectors that cover edge cases, truncation, and all supported hash functions.

go-multihash runs the official vectors:

func TestSpecVectors(t *testing.T) {
    file, _ := os.Open("spec/multihash/tests/values/test_cases.csv")
    // CSV format: algorithm, bits, input, multihash
    for _, testCase := range values[1:] {
        actual, _ := multihash.Sum([]byte(input), code, int(length/8))
        if actual.HexString() != expectedStr {
            t.Fatalf("got the wrong hash")
        }
    }
}

Proposed Solution

  1. Add the multihash spec submodule:

    git submodule add https://github.com/multiformats/multihash spec/multihash
  2. Create test:

    import csv
    import pytest
    from multihash import sum
    
    def test_spec_vectors():
        with open("spec/multihash/tests/values/test_cases.csv") as f:
            reader = csv.reader(f)
            header = next(reader)
            for algorithm, bits_str, input_str, expected_hex in reader:
                length_bits = int(bits_str)
                length_bytes = length_bits // 8
                try:
                    actual = sum(input_str.encode(), algorithm, length=length_bytes)
                except (KeyError, ValueError):
                    pytest.skip(f"Hash {algorithm} not supported")
                    continue
                assert actual.encode("hex").decode() == expected_hex

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