Merge pull request #84 from ipfs/fix/83

fix inline CIDs generated by Prefix.Sum
This commit is contained in:
Steven Allen
2019-02-20 20:11:24 -08:00
committed by GitHub
2 changed files with 34 additions and 1 deletions

7
cid.go
View File

@@ -536,7 +536,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
}

View File

@@ -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 {