Skip to content

Commit

Permalink
fix inline CIDs generated by Prefix.Sum
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Feb 21, 2019
1 parent c9e99b3 commit e55c11c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cid.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,12 @@ type Prefix struct {
// Sum uses the information in a prefix to perform a multihash.Sum()
// and return a newly constructed Cid with the resulting multihash.
func (p Prefix) Sum(data []byte) (Cid, error) {
hash, err := mh.Sum(data, p.MhType, p.MhLength)
length := p.MhLength
if p.MhType == mh.ID {
length = -1
}

hash, err := mh.Sum(data, p.MhType, length)
if err != nil {
return Undef, err
}
Expand Down
28 changes: 28 additions & 0 deletions cid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,34 @@ func TestTableForV0(t *testing.T) {
}
}

func TestPrefixSum(t *testing.T) {
// Test creating CIDs both manually and with Prefix.
// Tests: https://github.com/ipfs/go-cid/issues/83
for _, hashfun := range []uint64{
mh.ID, mh.SHA3, mh.SHA2_256,
} {
h1, err := mh.Sum([]byte("TEST"), hashfun, -1)
if err != nil {
t.Fatal(err)
}
c1 := NewCidV1(Raw, h1)

h2, err := mh.Sum([]byte("foobar"), hashfun, -1)
if err != nil {
t.Fatal(err)
}
c2 := NewCidV1(Raw, h2)

c3, err := c1.Prefix().Sum([]byte("foobar"))
if err != nil {
t.Fatal(err)
}
if !c2.Equals(c3) {
t.Fatal("expected CIDs to be equal")
}
}
}

func TestBasicMarshaling(t *testing.T) {
h, err := mh.Sum([]byte("TEST"), mh.SHA3, 4)
if err != nil {
Expand Down

0 comments on commit e55c11c

Please sign in to comment.