Skip to content

Commit 6525a7a

Browse files
laudiacayJorropo
authored andcommitted
feat: adding tests and finish variable sized functions
1 parent 1f2c018 commit 6525a7a

File tree

4 files changed

+74
-55
lines changed

4 files changed

+74
-55
lines changed

core/registry.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ import (
1010
)
1111

1212
// registry is a simple map which maps a multihash indicator number
13-
// to a standard golang Hash interface.
13+
// to a function : (size:int) -> ((hasher:hash.Hash), (bool:success))
14+
// The function may error (i.e., return (nil, false)) to signify that the hasher can't return that many bytes.
1415
//
1516
// Multihash indicator numbers are reserved and described in
1617
// https://github.com/multiformats/multicodec/blob/master/table.csv .
1718
// The keys used in this map must match those reservations.
1819
//
1920
// Hashers which are available in the golang stdlib will be registered automatically.
2021
// Others can be added using the Register function.
21-
var registry = make(map[uint64]func(int) (hash.Hash, bool))
22+
var registry = make(map[uint64]func(int) (h hash.Hash, ok bool))
2223

2324
// Register adds a new hash to the set available from GetHasher and Sum.
2425
//

multihash.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type ErrInconsistentLen struct {
3232
}
3333

3434
func (e ErrInconsistentLen) Error() string {
35-
return fmt.Sprintf("multihash length inconsistent: expected len(hdig)=%d, got rlen=%d", e.dm.Length, e.lengthFound)
35+
return fmt.Sprintf("multihash length inconsistent: expected %d; got %d", e.dm.Length, e.lengthFound)
3636
}
3737

3838
// constants

register/blake3/multihash_blake3.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,17 @@ import (
1717
"github.com/multiformats/go-multihash/core"
1818
)
1919

20-
func init() {
21-
multihash.Register(multihash.BLAKE3, func() hash.Hash { h := blake3.New(32, nil); return h })
20+
const DefaultSize = 32
21+
const MaxSize = 128
2222

23+
func init() {
24+
multihash.RegisterVariableSize(multihash.BLAKE3, func(size int) (hash.Hash, bool) {
25+
if size == -1 {
26+
size = DefaultSize
27+
} else if size > MaxSize || size <= 0 {
28+
return nil, false
29+
}
30+
h := blake3.New(size, nil)
31+
return h, true
32+
})
2333
}

sum_test.go

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,57 @@ import (
1313
)
1414

1515
type SumTestCase struct {
16-
code uint64
17-
length int
18-
input string
19-
hex string
16+
code uint64
17+
length int
18+
input string
19+
hex string
20+
expectedSumError error
2021
}
2122

2223
var sumTestCases = []SumTestCase{
23-
{multihash.ID, 3, "foo", "0003666f6f"},
24-
{multihash.ID, -1, "foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo", "0030666f6f666f6f666f6f666f6f666f6f666f6f666f6f666f6f666f6f666f6f666f6f666f6f666f6f666f6f666f6f666f6f"},
25-
{multihash.SHA1, -1, "foo", "11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"},
26-
{multihash.SHA1, 10, "foo", "110a0beec7b5ea3f0fdbc95d"},
27-
{multihash.SHA2_256, -1, "foo", "12202c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"},
28-
{multihash.SHA2_256, 31, "foo", "121f2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7"},
29-
{multihash.SHA2_256, 32, "foo", "12202c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"},
30-
{multihash.SHA2_256, 16, "foo", "12102c26b46b68ffc68ff99b453c1d304134"},
31-
{multihash.SHA2_512, -1, "foo", "1340f7fbba6e0636f890e56fbbf3283e524c6fa3204ae298382d624741d0dc6638326e282c41be5e4254d8820772c5518a2c5a8c0c7f7eda19594a7eb539453e1ed7"},
32-
{multihash.SHA2_512, 32, "foo", "1320f7fbba6e0636f890e56fbbf3283e524c6fa3204ae298382d624741d0dc663832"},
33-
{multihash.SHA3, 32, "foo", "14204bca2b137edc580fe50a88983ef860ebaca36c857b1f492839d6d7392452a63c"},
34-
{multihash.SHA3_512, 16, "foo", "14104bca2b137edc580fe50a88983ef860eb"},
35-
{multihash.SHA3_512, -1, "foo", "14404bca2b137edc580fe50a88983ef860ebaca36c857b1f492839d6d7392452a63c82cbebc68e3b70a2a1480b4bb5d437a7cba6ecf9d89f9ff3ccd14cd6146ea7e7"},
36-
{multihash.SHA3_224, -1, "beep boop", "171c0da73a89549018df311c0a63250e008f7be357f93ba4e582aaea32b8"},
37-
{multihash.SHA3_224, 16, "beep boop", "17100da73a89549018df311c0a63250e008f"},
38-
{multihash.SHA3_256, -1, "beep boop", "1620828705da60284b39de02e3599d1f39e6c1df001f5dbf63c9ec2d2c91a95a427f"},
39-
{multihash.SHA3_256, 16, "beep boop", "1610828705da60284b39de02e3599d1f39e6"},
40-
{multihash.SHA3_384, -1, "beep boop", "153075a9cff1bcfbe8a7025aa225dd558fb002769d4bf3b67d2aaf180459172208bea989804aefccf060b583e629e5f41e8d"},
41-
{multihash.SHA3_384, 16, "beep boop", "151075a9cff1bcfbe8a7025aa225dd558fb0"},
42-
{multihash.DBL_SHA2_256, 32, "foo", "5620c7ade88fc7a21498a6a5e5c385e1f68bed822b72aa63c4a9a48a02c2466ee29e"},
43-
{multihash.BLAKE2B_MAX, -1, "foo", "c0e40240ca002330e69d3e6b84a46a56a6533fd79d51d97a3bb7cad6c2ff43b354185d6dc1e723fb3db4ae0737e120378424c714bb982d9dc5bbd7a0ab318240ddd18f8d"},
44-
{multihash.BLAKE2B_MAX, 64, "foo", "c0e40240ca002330e69d3e6b84a46a56a6533fd79d51d97a3bb7cad6c2ff43b354185d6dc1e723fb3db4ae0737e120378424c714bb982d9dc5bbd7a0ab318240ddd18f8d"},
45-
{multihash.BLAKE2B_MAX - 32, -1, "foo", "a0e40220b8fe9f7f6255a6fa08f668ab632a8d081ad87983c77cd274e48ce450f0b349fd"},
46-
{multihash.BLAKE2B_MAX - 32, 32, "foo", "a0e40220b8fe9f7f6255a6fa08f668ab632a8d081ad87983c77cd274e48ce450f0b349fd"},
47-
{multihash.BLAKE2B_MAX - 19, -1, "foo", "ade4022dca82ab956d5885e3f5db10cca94182f01a6ca2c47f9f4228497dcc9f4a0121c725468b852a71ec21fcbeb725df"},
48-
{multihash.BLAKE2B_MAX - 19, 45, "foo", "ade4022dca82ab956d5885e3f5db10cca94182f01a6ca2c47f9f4228497dcc9f4a0121c725468b852a71ec21fcbeb725df"},
49-
{multihash.BLAKE2B_MAX - 16, -1, "foo", "b0e40230e629ee880953d32c8877e479e3b4cb0a4c9d5805e2b34c675b5a5863c4ad7d64bb2a9b8257fac9d82d289b3d39eb9cc2"},
50-
{multihash.BLAKE2B_MAX - 16, 48, "foo", "b0e40230e629ee880953d32c8877e479e3b4cb0a4c9d5805e2b34c675b5a5863c4ad7d64bb2a9b8257fac9d82d289b3d39eb9cc2"},
51-
{multihash.BLAKE2B_MIN + 19, -1, "foo", "94e40214983ceba2afea8694cc933336b27b907f90c53a88"},
52-
{multihash.BLAKE2B_MIN + 19, 20, "foo", "94e40214983ceba2afea8694cc933336b27b907f90c53a88"},
53-
{multihash.BLAKE2B_MIN, -1, "foo", "81e4020152"},
54-
{multihash.BLAKE2B_MIN, 1, "foo", "81e4020152"},
55-
{multihash.BLAKE2S_MAX, 32, "foo", "e0e4022008d6cad88075de8f192db097573d0e829411cd91eb6ec65e8fc16c017edfdb74"},
56-
{multihash.KECCAK_256, 32, "foo", "1b2041b1a0649752af1b28b3dc29a1556eee781e4a4c3a1f7f53f90fa834de098c4d"},
57-
{multihash.KECCAK_512, -1, "beep boop", "1d40e161c54798f78eba3404ac5e7e12d27555b7b810e7fd0db3f25ffa0c785c438331b0fbb6156215f69edf403c642e5280f4521da9bd767296ec81f05100852e78"},
58-
{multihash.SHAKE_128, 32, "foo", "1820f84e95cb5fbd2038863ab27d3cdeac295ad2d4ab96ad1f4b070c0bf36078ef08"},
59-
{multihash.SHAKE_256, 64, "foo", "19401af97f7818a28edfdfce5ec66dbdc7e871813816d7d585fe1f12475ded5b6502b7723b74e2ee36f2651a10a8eaca72aa9148c3c761aaceac8f6d6cc64381ed39"},
60-
{multihash.MD5, -1, "foo", "d50110acbd18db4cc2f85cedef654fccc4a4d8"},
61-
{multihash.BLAKE3, 32, "foo", "1e2004e0bb39f30b1a3feb89f536c93be15055482df748674b00d26e5a75777702e9"},
24+
{multihash.IDENTITY, 3, "foo", "0003666f6f", nil},
25+
{multihash.IDENTITY, -1, "foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo", "0030666f6f666f6f666f6f666f6f666f6f666f6f666f6f666f6f666f6f666f6f666f6f666f6f666f6f666f6f666f6f666f6f", nil},
26+
{multihash.SHA1, -1, "foo", "11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33", nil},
27+
{multihash.SHA1, 10, "foo", "110a0beec7b5ea3f0fdbc95d", nil},
28+
{multihash.SHA2_256, -1, "foo", "12202c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae", nil},
29+
{multihash.SHA2_256, 31, "foo", "121f2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7", nil},
30+
{multihash.SHA2_256, 32, "foo", "12202c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae", nil},
31+
{multihash.SHA2_256, 16, "foo", "12102c26b46b68ffc68ff99b453c1d304134", nil},
32+
{multihash.SHA2_512, -1, "foo", "1340f7fbba6e0636f890e56fbbf3283e524c6fa3204ae298382d624741d0dc6638326e282c41be5e4254d8820772c5518a2c5a8c0c7f7eda19594a7eb539453e1ed7", nil},
33+
{multihash.SHA2_512, 32, "foo", "1320f7fbba6e0636f890e56fbbf3283e524c6fa3204ae298382d624741d0dc663832", nil},
34+
{multihash.SHA3, 32, "foo", "14204bca2b137edc580fe50a88983ef860ebaca36c857b1f492839d6d7392452a63c", nil},
35+
{multihash.SHA3_512, 16, "foo", "14104bca2b137edc580fe50a88983ef860eb", nil},
36+
{multihash.SHA3_512, -1, "foo", "14404bca2b137edc580fe50a88983ef860ebaca36c857b1f492839d6d7392452a63c82cbebc68e3b70a2a1480b4bb5d437a7cba6ecf9d89f9ff3ccd14cd6146ea7e7", nil},
37+
{multihash.SHA3_224, -1, "beep boop", "171c0da73a89549018df311c0a63250e008f7be357f93ba4e582aaea32b8", nil},
38+
{multihash.SHA3_224, 16, "beep boop", "17100da73a89549018df311c0a63250e008f", nil},
39+
{multihash.SHA3_256, -1, "beep boop", "1620828705da60284b39de02e3599d1f39e6c1df001f5dbf63c9ec2d2c91a95a427f", nil},
40+
{multihash.SHA3_256, 16, "beep boop", "1610828705da60284b39de02e3599d1f39e6", nil},
41+
{multihash.SHA3_384, -1, "beep boop", "153075a9cff1bcfbe8a7025aa225dd558fb002769d4bf3b67d2aaf180459172208bea989804aefccf060b583e629e5f41e8d", nil},
42+
{multihash.SHA3_384, 16, "beep boop", "151075a9cff1bcfbe8a7025aa225dd558fb0", nil},
43+
{multihash.DBL_SHA2_256, 32, "foo", "5620c7ade88fc7a21498a6a5e5c385e1f68bed822b72aa63c4a9a48a02c2466ee29e", nil},
44+
{multihash.BLAKE2B_MAX, -1, "foo", "c0e40240ca002330e69d3e6b84a46a56a6533fd79d51d97a3bb7cad6c2ff43b354185d6dc1e723fb3db4ae0737e120378424c714bb982d9dc5bbd7a0ab318240ddd18f8d", nil},
45+
{multihash.BLAKE2B_MAX, 64, "foo", "c0e40240ca002330e69d3e6b84a46a56a6533fd79d51d97a3bb7cad6c2ff43b354185d6dc1e723fb3db4ae0737e120378424c714bb982d9dc5bbd7a0ab318240ddd18f8d", nil},
46+
{multihash.BLAKE2B_MAX - 32, -1, "foo", "a0e40220b8fe9f7f6255a6fa08f668ab632a8d081ad87983c77cd274e48ce450f0b349fd", nil},
47+
{multihash.BLAKE2B_MAX - 32, 32, "foo", "a0e40220b8fe9f7f6255a6fa08f668ab632a8d081ad87983c77cd274e48ce450f0b349fd", nil},
48+
{multihash.BLAKE2B_MAX - 19, -1, "foo", "ade4022dca82ab956d5885e3f5db10cca94182f01a6ca2c47f9f4228497dcc9f4a0121c725468b852a71ec21fcbeb725df", nil},
49+
{multihash.BLAKE2B_MAX - 19, 45, "foo", "ade4022dca82ab956d5885e3f5db10cca94182f01a6ca2c47f9f4228497dcc9f4a0121c725468b852a71ec21fcbeb725df", nil},
50+
{multihash.BLAKE2B_MAX - 16, -1, "foo", "b0e40230e629ee880953d32c8877e479e3b4cb0a4c9d5805e2b34c675b5a5863c4ad7d64bb2a9b8257fac9d82d289b3d39eb9cc2", nil},
51+
{multihash.BLAKE2B_MAX - 16, 48, "foo", "b0e40230e629ee880953d32c8877e479e3b4cb0a4c9d5805e2b34c675b5a5863c4ad7d64bb2a9b8257fac9d82d289b3d39eb9cc2", nil},
52+
{multihash.BLAKE2B_MIN + 19, -1, "foo", "94e40214983ceba2afea8694cc933336b27b907f90c53a88", nil},
53+
{multihash.BLAKE2B_MIN + 19, 20, "foo", "94e40214983ceba2afea8694cc933336b27b907f90c53a88", nil},
54+
{multihash.BLAKE2B_MIN, -1, "foo", "81e4020152", nil},
55+
{multihash.BLAKE2B_MIN, 1, "foo", "81e4020152", nil},
56+
{multihash.BLAKE2S_MAX, 32, "foo", "e0e4022008d6cad88075de8f192db097573d0e829411cd91eb6ec65e8fc16c017edfdb74", nil},
57+
{multihash.KECCAK_256, 32, "foo", "1b2041b1a0649752af1b28b3dc29a1556eee781e4a4c3a1f7f53f90fa834de098c4d", nil},
58+
{multihash.KECCAK_512, -1, "beep boop", "1d40e161c54798f78eba3404ac5e7e12d27555b7b810e7fd0db3f25ffa0c785c438331b0fbb6156215f69edf403c642e5280f4521da9bd767296ec81f05100852e78", nil},
59+
{multihash.SHAKE_128, 32, "foo", "1820f84e95cb5fbd2038863ab27d3cdeac295ad2d4ab96ad1f4b070c0bf36078ef08", nil},
60+
{multihash.SHAKE_256, 64, "foo", "19401af97f7818a28edfdfce5ec66dbdc7e871813816d7d585fe1f12475ded5b6502b7723b74e2ee36f2651a10a8eaca72aa9148c3c761aaceac8f6d6cc64381ed39", nil},
61+
{multihash.MD5, -1, "foo", "d50110acbd18db4cc2f85cedef654fccc4a4d8", nil},
62+
{multihash.BLAKE3, 32, "foo", "1e2004e0bb39f30b1a3feb89f536c93be15055482df748674b00d26e5a75777702e9", nil},
63+
{multihash.BLAKE3, 64, "foo", "1e4004e0bb39f30b1a3feb89f536c93be15055482df748674b00d26e5a75777702e9791074b7511b59d31c71c62f5a745689fa6c9497f68bdf1061fe07f518d410c0", nil},
64+
{multihash.BLAKE3, 128, "foo", "1e800104e0bb39f30b1a3feb89f536c93be15055482df748674b00d26e5a75777702e9791074b7511b59d31c71c62f5a745689fa6c9497f68bdf1061fe07f518d410c0b0c27f41b3cf083f8a7fdc67a877e21790515762a754a45dcb8a356722698a7af5ed2bb608983d5aa75d4d61691ef132efe8631ce0afc15553a08fffc60ee936", nil},
65+
{multihash.BLAKE3, -1, "foo", "1e2004e0bb39f30b1a3feb89f536c93be15055482df748674b00d26e5a75777702e9", nil},
66+
{multihash.BLAKE3, 129, "foo", "1e810104e0bb39f30b1a3feb89f536c93be15055482df748674b00d26e5a75777702e9791074b7511b59d31c71c62f5a745689fa6c9497f68bdf1061fe07f518d410c0b0c27f41b3cf083f8a7fdc67a877e21790515762a754a45dcb8a356722698a7af5ed2bb608983d5aa75d4d61691ef132efe8631ce0afc15553a08fffc60ee9369b", multihash.ErrLenTooLarge},
6267
}
6368

6469
func TestSum(t *testing.T) {
@@ -70,8 +75,11 @@ func TestSum(t *testing.T) {
7075
}
7176

7277
m2, err := multihash.Sum([]byte(tc.input), tc.code, tc.length)
73-
if err != nil {
74-
t.Error(tc.code, "sum failed.", err)
78+
if err != tc.expectedSumError {
79+
t.Error(tc.code, "sum failed or succeeded unexpectedly.", err)
80+
continue
81+
} else if err != nil {
82+
// test case was expected to fail at the sum, and failed as expected. stop the test.
7583
continue
7684
}
7785

@@ -100,7 +108,7 @@ func TestSum(t *testing.T) {
100108
func BenchmarkSum(b *testing.B) {
101109
tc := sumTestCases[0]
102110
for i := 0; i < b.N; i++ {
103-
multihash.Sum([]byte(tc.input), tc.code, tc.length)
111+
_, _ = multihash.Sum([]byte(tc.input), tc.code, tc.length)
104112
}
105113
}
106114

@@ -130,20 +138,20 @@ func TestSmallerLengthHashID(t *testing.T) {
130138
dataLength := len(data)
131139

132140
// Normal case: `length == len(data)`.
133-
_, err := multihash.Sum(data, multihash.ID, dataLength)
141+
_, err := multihash.Sum(data, multihash.IDENTITY, dataLength)
134142
if err != nil {
135143
t.Fatal(err)
136144
}
137145

138146
// Unconstrained length (-1): also allowed.
139-
_, err = multihash.Sum(data, multihash.ID, -1)
147+
_, err = multihash.Sum(data, multihash.IDENTITY, -1)
140148
if err != nil {
141149
t.Fatal(err)
142150
}
143151

144152
// Any other variation of those two scenarios should fail.
145-
for l := (dataLength - 1); l >= 0; l-- {
146-
_, err = multihash.Sum(data, multihash.ID, l)
153+
for l := dataLength - 1; l >= 0; l-- {
154+
_, err = multihash.Sum(data, multihash.IDENTITY, l)
147155
if err == nil {
148156
t.Fatalf("identity hash of length %d smaller than data length %d didn't fail",
149157
l, dataLength)

0 commit comments

Comments
 (0)