Skip to content

Commit

Permalink
adding blake3 tests and fixing an incorrect error message. (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
laudiacay committed Jul 26, 2022
1 parent 89bf86c commit f62cf07
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
15 changes: 9 additions & 6 deletions multihash.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ var (

// ErrInconsistentLen is returned when a decoded multihash has an inconsistent length
type ErrInconsistentLen struct {
dm *DecodedMultihash
dm *DecodedMultihash
lengthFound int
}

func (e ErrInconsistentLen) Error() string {
return fmt.Sprintf("multihash length inconsistent: expected %d, got %d", e.dm.Length, len(e.dm.Digest))
return fmt.Sprintf("multihash length inconsistent: expected len(hdig)=%d, got rlen=%d", e.dm.Length, e.lengthFound)
}

// constants
Expand Down Expand Up @@ -143,6 +144,7 @@ var Codes = map[uint64]string{
MD5: "md5",
}

// reads a varint from buf and returns bytes read.
func uvarint(buf []byte) (uint64, []byte, error) {
n, c, err := varint.FromUvarint(buf)
if err != nil {
Expand Down Expand Up @@ -233,7 +235,7 @@ func Decode(buf []byte) (*DecodedMultihash, error) {
}

if len(buf) != rlen {
return nil, ErrInconsistentLen{dm}
return nil, ErrInconsistentLen{dm, rlen}
}

return dm, nil
Expand Down Expand Up @@ -264,8 +266,8 @@ func EncodeName(buf []byte, name string) ([]byte, error) {
// Note: the returned digest is a slice over the passed in data and should be
// copied if the buffer will be reused
func readMultihashFromBuf(buf []byte) (int, uint64, []byte, error) {
bufl := len(buf)
if bufl < 2 {
initBufLength := len(buf)
if initBufLength < 2 {
return 0, 0, nil, ErrTooShort
}

Expand All @@ -289,7 +291,8 @@ func readMultihashFromBuf(buf []byte) (int, uint64, []byte, error) {
return 0, 0, nil, errors.New("length greater than remaining number of bytes in buffer")
}

rlen := (bufl - len(buf)) + int(length)
// rlen is the advertised size of the CID
rlen := (initBufLength - len(buf)) + int(length)
return rlen, code, buf[:length], nil
}

Expand Down
1 change: 1 addition & 0 deletions multihash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ var testCases = []TestCase{
{"d41d8cd98f00b204e9800998ecf8427e", 0xd5, "md5"},
{"14fcb37dc45fa9a3c492557121bd4d461c0db40e5dcfcaa98498bd238486c307", 0x1012, "sha2-256-trunc254-padded"},
{"14fcb37dc45fa9a3c492557121bd4d461c0db40e5dcfcaa98498bd238486c307", 0xb401, "poseidon-bls12_381-a2-fc1"},
{"04e0bb39f30b1a3feb89f536c93be15055482df748674b00d26e5a75777702e9", 0x1e, "blake3"},
}

func (tc TestCase) Multihash() (Multihash, error) {
Expand Down
1 change: 1 addition & 0 deletions sum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var sumTestCases = []SumTestCase{
{multihash.SHAKE_128, 32, "foo", "1820f84e95cb5fbd2038863ab27d3cdeac295ad2d4ab96ad1f4b070c0bf36078ef08"},
{multihash.SHAKE_256, 64, "foo", "19401af97f7818a28edfdfce5ec66dbdc7e871813816d7d585fe1f12475ded5b6502b7723b74e2ee36f2651a10a8eaca72aa9148c3c761aaceac8f6d6cc64381ed39"},
{multihash.MD5, -1, "foo", "d50110acbd18db4cc2f85cedef654fccc4a4d8"},
{multihash.BLAKE3, 32, "foo", "1e2004e0bb39f30b1a3feb89f536c93be15055482df748674b00d26e5a75777702e9"},
}

func TestSum(t *testing.T) {
Expand Down

0 comments on commit f62cf07

Please sign in to comment.