-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix hash mismatch error on matching link pointer
On load, the default IPLD Link System checks the hash of loaded data against the given hash to assure that they match. The actual hash is calculated using the given link's `LinkPrototype.BuildLink`, and then is directly compared with the given link via `==`. Because the comparison is done by value, if the given link is a pointer the comparison fails which will result in false-positive hash mismatch error. To address this issue, instead of directly comparing objects using `==` check that their `Binary` value, i.e. their densest possible encoding, is equal. Add tests that assert the issue is fixed and works with existing link representations.
- Loading branch information
Showing
3 changed files
with
77 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package linking_test | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"testing" | ||
|
||
qt "github.com/frankban/quicktest" | ||
"github.com/ipfs/go-cid" | ||
"github.com/ipld/go-ipld-prime" | ||
"github.com/ipld/go-ipld-prime/codec/dagcbor" | ||
"github.com/ipld/go-ipld-prime/datamodel" | ||
"github.com/ipld/go-ipld-prime/fluent" | ||
cidlink "github.com/ipld/go-ipld-prime/linking/cid" | ||
"github.com/ipld/go-ipld-prime/node/basicnode" | ||
"github.com/ipld/go-ipld-prime/storage/memstore" | ||
"github.com/multiformats/go-multicodec" | ||
) | ||
|
||
func TestLinkSystem_LoadHashMismatch(t *testing.T) { | ||
subject := cidlink.DefaultLinkSystem() | ||
storage := &memstore.Store{} | ||
subject.SetReadStorage(storage) | ||
subject.SetWriteStorage(storage) | ||
|
||
// Construct some test IPLD node. | ||
wantNode := fluent.MustBuildMap(basicnode.Prototype.Map, 1, func(na fluent.MapAssembler) { | ||
na.AssembleEntry("fish").AssignString("barreleye") | ||
}) | ||
|
||
// Encode as raw value to be used for testing LoadRaw | ||
var buf bytes.Buffer | ||
qt.Check(t, dagcbor.Encode(wantNode, &buf), qt.IsNil) | ||
wantNodeRaw := buf.Bytes() | ||
|
||
// Store the test IPLD node and get link back. | ||
lctx := ipld.LinkContext{Ctx: context.TODO()} | ||
gotLink, err := subject.Store(lctx, cidlink.LinkPrototype{ | ||
Prefix: cid.Prefix{ | ||
Version: 1, | ||
Codec: uint64(multicodec.DagCbor), | ||
MhType: uint64(multicodec.Sha2_256), | ||
MhLength: -1, | ||
}, | ||
}, wantNode) | ||
qt.Check(t, err, qt.IsNil) | ||
gotCidlink := gotLink.(cidlink.Link) | ||
|
||
// Assert all load variations return expected values for different link representations. | ||
for _, test := range []struct { | ||
name string | ||
link datamodel.Link | ||
}{ | ||
{"datamodel.Link", gotLink}, | ||
{"cidlink.Link", gotCidlink}, | ||
{"&cidlink.Link", &gotCidlink}, | ||
} { | ||
t.Run(test.name, func(t *testing.T) { | ||
gotNode, err := subject.Load(lctx, test.link, basicnode.Prototype.Any) | ||
qt.Check(t, err, qt.IsNil) | ||
qt.Check(t, ipld.DeepEqual(wantNode, gotNode), qt.IsTrue) | ||
|
||
gotNodeRaw, err := subject.LoadRaw(lctx, test.link) | ||
qt.Check(t, err, qt.IsNil) | ||
qt.Check(t, bytes.Equal(wantNodeRaw, gotNodeRaw), qt.IsTrue) | ||
|
||
gotNode, gotNodeRaw, err = subject.LoadPlusRaw(lctx, test.link, basicnode.Prototype.Any) | ||
qt.Check(t, err, qt.IsNil) | ||
qt.Check(t, ipld.DeepEqual(wantNode, gotNode), qt.IsTrue) | ||
qt.Check(t, bytes.Equal(wantNodeRaw, gotNodeRaw), qt.IsTrue) | ||
}) | ||
} | ||
} |