Skip to content

Commit 64ffd94

Browse files
authored
Merge pull request #115 from multiformats/feat/spec-test
feat: improve tests
2 parents e1cec6a + 1c9c846 commit 64ffd94

File tree

8 files changed

+180
-8
lines changed

8 files changed

+180
-8
lines changed

.gitmodules

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[submodule "spec/multicodec"]
2+
path = spec/multicodec
3+
url = https://github.com/multiformats/multicodec.git
4+
[submodule "spec/multihash"]
5+
path = spec/multihash
6+
url = https://github.com/multiformats/multihash.git

multihash.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ const (
6464

6565
DBL_SHA2_256 = 0x56
6666

67-
MURMUR3 = 0x22
67+
MURMUR3_128 = 0x22
68+
// Deprecated: use MURMUR3_128
69+
MURMUR3 = MURMUR3_128
6870

6971
X11 = 0x1100
7072
)
@@ -101,7 +103,7 @@ var Names = map[string]uint64{
101103
"sha3-384": SHA3_384,
102104
"sha3-512": SHA3_512,
103105
"dbl-sha2-256": DBL_SHA2_256,
104-
"murmur3": MURMUR3,
106+
"murmur3-128": MURMUR3_128,
105107
"keccak-224": KECCAK_224,
106108
"keccak-256": KECCAK_256,
107109
"keccak-384": KECCAK_384,
@@ -123,7 +125,7 @@ var Codes = map[uint64]string{
123125
SHA3_384: "sha3-384",
124126
SHA3_512: "sha3-512",
125127
DBL_SHA2_256: "dbl-sha2-256",
126-
MURMUR3: "murmur3",
128+
MURMUR3_128: "murmur3-128",
127129
KECCAK_224: "keccak-224",
128130
KECCAK_256: "keccak-256",
129131
KECCAK_384: "keccak-384",
@@ -147,7 +149,7 @@ var DefaultLengths = map[uint64]int{
147149
DBL_SHA2_256: 32,
148150
KECCAK_224: 28,
149151
KECCAK_256: 32,
150-
MURMUR3: 4,
152+
MURMUR3_128: 4,
151153
KECCAK_384: 48,
152154
KECCAK_512: 64,
153155
SHAKE_128: 32,

multihash_test.go

+31-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/binary"
66
"encoding/hex"
77
"fmt"
8+
"strings"
89
"testing"
910
)
1011

@@ -20,7 +21,7 @@ var tCodes = map[uint64]string{
2021
0x16: "sha3-256",
2122
0x17: "sha3-224",
2223
0x56: "dbl-sha2-256",
23-
0x22: "murmur3",
24+
0x22: "murmur3-128",
2425
0x1A: "keccak-224",
2526
0x1B: "keccak-256",
2627
0x1C: "keccak-384",
@@ -45,7 +46,7 @@ var testCases = []TestCase{
4546
{"2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae", 0x12, "sha2-256"},
4647
{"2c26b46b", 0x12, "sha2-256"},
4748
{"2c26b46b68ffc68ff99b453c1d30413413", 0xb240, "blake2b-512"},
48-
{"243ddb9e", 0x22, "murmur3"},
49+
{"243ddb9e", 0x22, "murmur3-128"},
4950
{"f00ba4", 0x1b, "keccak-256"},
5051
{"f84e95cb5fbd2038863ab27d3cdeac295ad2d4ab96ad1f4b070c0bf36078ef08", 0x18, "shake-128"},
5152
{"1af97f7818a28edfdfce5ec66dbdc7e871813816d7d585fe1f12475ded5b6502b7723b74e2ee36f2651a10a8eaca72aa9148c3c761aaceac8f6d6cc64381ed39", 0x19, "shake-256"},
@@ -175,6 +176,34 @@ func TestTable(t *testing.T) {
175176
t.Error("Table mismatch: ", Names[v], k)
176177
}
177178
}
179+
180+
for name, code := range Names {
181+
if tCodes[code] != name {
182+
if strings.HasPrefix(name, "blake") {
183+
// skip these
184+
continue
185+
}
186+
if name == "sha3" {
187+
// tested as "sha3-512"
188+
continue
189+
}
190+
t.Error("missing test case for: ", name)
191+
}
192+
}
193+
194+
for code, name := range Codes {
195+
if tCodes[code] != name {
196+
if strings.HasPrefix(name, "blake") {
197+
// skip these
198+
continue
199+
}
200+
if name == "sha3" {
201+
// tested as "sha3-512"
202+
continue
203+
}
204+
t.Error("missing test case for: ", name)
205+
}
206+
}
178207
}
179208

180209
func ExampleDecode() {

spec/multicodec

Submodule multicodec added at ba90699

spec/multihash

Submodule multihash added at cde1aef

spec_test.go

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package multihash
2+
3+
import (
4+
"encoding/csv"
5+
"fmt"
6+
"os"
7+
"strconv"
8+
"strings"
9+
"testing"
10+
)
11+
12+
func TestSpec(t *testing.T) {
13+
file, err := os.Open("spec/multicodec/table.csv")
14+
if err != nil {
15+
t.Fatal(err)
16+
}
17+
defer file.Close()
18+
19+
reader := csv.NewReader(file)
20+
reader.LazyQuotes = false
21+
reader.FieldsPerRecord = 4
22+
reader.TrimLeadingSpace = true
23+
24+
values, err := reader.ReadAll()
25+
if err != nil {
26+
t.Error(err)
27+
}
28+
expectedFunctions := make(map[uint64]string, len(values)-1)
29+
if values[0][0] != "name" || values[0][1] != "tag" || values[0][2] != "code" {
30+
t.Fatal("table format has changed")
31+
}
32+
33+
for _, v := range values[1:] {
34+
name := v[0]
35+
tag := v[1]
36+
codeStr := v[2]
37+
38+
if tag != "multihash" {
39+
// not a multihash function
40+
continue
41+
}
42+
43+
var code uint64
44+
if !strings.HasPrefix(codeStr, "0x") {
45+
t.Errorf("invalid multicodec code %q (%s)", codeStr, name)
46+
continue
47+
}
48+
49+
i, err := strconv.ParseUint(codeStr[2:], 16, 64)
50+
if err != nil {
51+
t.Errorf("invalid multibase code %q (%s)", codeStr, name)
52+
continue
53+
}
54+
code = uint64(i)
55+
expectedFunctions[code] = name
56+
}
57+
58+
for code, name := range Codes {
59+
expectedName, ok := expectedFunctions[code]
60+
if !ok {
61+
t.Errorf("multihash %q (%x) not defined in the spec", name, code)
62+
continue
63+
}
64+
if expectedName != name {
65+
t.Errorf("encoding %q (%x) has unexpected name %q", expectedName, code, name)
66+
}
67+
}
68+
}
69+
70+
func TestSpecVectors(t *testing.T) {
71+
file, err := os.Open("spec/multihash/tests/values/test_cases.csv")
72+
if err != nil {
73+
t.Error(err)
74+
return
75+
}
76+
defer file.Close()
77+
78+
reader := csv.NewReader(file)
79+
reader.LazyQuotes = false
80+
reader.FieldsPerRecord = 4
81+
reader.TrimLeadingSpace = true
82+
83+
values, err := reader.ReadAll()
84+
if err != nil {
85+
t.Error(err)
86+
}
87+
if len(values) == 0 {
88+
t.Error("no test values")
89+
return
90+
}
91+
92+
// check the header.
93+
if values[0][0] != "algorithm" ||
94+
values[0][1] != "bits" ||
95+
values[0][2] != "input" ||
96+
values[0][3] != "multihash" {
97+
t.Fatal("table format has changed")
98+
}
99+
100+
for i, testCase := range values[1:] {
101+
function := testCase[0]
102+
lengthStr := testCase[1]
103+
input := testCase[2]
104+
expectedStr := testCase[3]
105+
106+
t.Run(fmt.Sprintf("%d/%s/%s", i, function, lengthStr), func(t *testing.T) {
107+
code, ok := Names[function]
108+
if !ok {
109+
t.Skipf("skipping %s: not supported", function)
110+
return
111+
}
112+
113+
length, err := strconv.ParseInt(lengthStr, 10, 64)
114+
if err != nil {
115+
t.Fatalf("failed to decode length: %s", err)
116+
}
117+
118+
if length%8 != 0 {
119+
t.Fatal("expected the length to be a multiple of 8")
120+
}
121+
122+
actual, err := Sum([]byte(input), code, int(length/8))
123+
if err != nil {
124+
t.Fatalf("failed to hash: %s", err)
125+
}
126+
actualStr := actual.HexString()
127+
if actualStr != expectedStr {
128+
t.Fatalf("got the wrong hash: expected %s, got %s", expectedStr, actualStr)
129+
}
130+
})
131+
132+
}
133+
}

sum.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func registerNonStdlibHashFuncs() {
185185
RegisterHashFunc(SHA3_384, sumSHA3_384)
186186
RegisterHashFunc(SHA3_512, sumSHA3_512)
187187

188-
RegisterHashFunc(MURMUR3, sumMURMUR3)
188+
RegisterHashFunc(MURMUR3_128, sumMURMUR3)
189189

190190
RegisterHashFunc(SHAKE_128, sumSHAKE128)
191191
RegisterHashFunc(SHAKE_256, sumSHAKE256)

sum_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ var sumTestCases = []SumTestCase{
4747
{BLAKE2B_MIN, -1, "foo", "81e4020152"},
4848
{BLAKE2B_MIN, 1, "foo", "81e4020152"},
4949
{BLAKE2S_MAX, 32, "foo", "e0e4022008d6cad88075de8f192db097573d0e829411cd91eb6ec65e8fc16c017edfdb74"},
50-
{MURMUR3, 4, "beep boop", "2204243ddb9e"},
50+
{MURMUR3_128, 4, "beep boop", "2204243ddb9e"},
5151
{KECCAK_256, 32, "foo", "1b2041b1a0649752af1b28b3dc29a1556eee781e4a4c3a1f7f53f90fa834de098c4d"},
5252
{KECCAK_512, -1, "beep boop", "1d40e161c54798f78eba3404ac5e7e12d27555b7b810e7fd0db3f25ffa0c785c438331b0fbb6156215f69edf403c642e5280f4521da9bd767296ec81f05100852e78"},
5353
{SHAKE_128, 32, "foo", "1820f84e95cb5fbd2038863ab27d3cdeac295ad2d4ab96ad1f4b070c0bf36078ef08"},

0 commit comments

Comments
 (0)