Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix inline CIDs generated by Prefix.Sum #84

Merged
merged 1 commit into from
Feb 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
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