Skip to content

No spec compliance test validating hash codes against multicodec/table.csv #52

Description

@sumanjeet0012

There is no test that validates the hash function codes in constants.py and funcs.py against the authoritative upstream multicodec table.csv. go-multihash has a TestSpec that does this validation.

Problem

The HASH_CODES dict in constants.py and the Func IntEnum in funcs.py are manually maintained. There is no automated check that:

  • Every multihash-tagged entry in the CSV is present
  • The codes match the CSV
  • The names match the CSV
  • No extra entries exist

go-multihash validates against the spec:

func TestSpec(t *testing.T) {
    file, _ := os.Open("spec/multicodec/table.csv")
    // ... reads CSV, filters for tag == "multihash"
    for code, name := range multihash.Codes {
        expectedName, ok := expectedFunctions[code]
        if !ok {
            t.Errorf("multihash %q (%x) not defined in the spec", name, code)
        }
    }
}

Proposed Solution

  1. Add the multicodec spec as a git submodule:

    git submodule add https://github.com/multiformats/multicodec spec/multicodec
  2. Create tests/test_spec.py:

    import csv
    from multihash.constants import HASH_CODES, CODE_HASHES
    
    def test_spec_table_completeness():
        """Every multihash entry in table.csv should be in HASH_CODES."""
        with open("spec/multicodec/table.csv") as f:
            reader = csv.DictReader(f, skipinitialspace=True)
            for row in reader:
                if row["tag"].strip() != "multihash":
                    continue
                name = row["name"].strip()
                code = int(row["code"].strip(), 16)
                assert name in HASH_CODES, f"Missing hash: {name} (0x{code:x})"
                assert HASH_CODES[name] == code, f"Code mismatch for {name}"

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