|
| 1 | +package linking_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "testing" |
| 7 | + |
| 8 | + qt "github.com/frankban/quicktest" |
| 9 | + "github.com/ipfs/go-cid" |
| 10 | + "github.com/ipld/go-ipld-prime" |
| 11 | + "github.com/ipld/go-ipld-prime/codec/dagcbor" |
| 12 | + "github.com/ipld/go-ipld-prime/datamodel" |
| 13 | + "github.com/ipld/go-ipld-prime/fluent" |
| 14 | + cidlink "github.com/ipld/go-ipld-prime/linking/cid" |
| 15 | + "github.com/ipld/go-ipld-prime/node/basicnode" |
| 16 | + "github.com/ipld/go-ipld-prime/storage/memstore" |
| 17 | + "github.com/multiformats/go-multicodec" |
| 18 | +) |
| 19 | + |
| 20 | +func TestLinkSystem_LoadHashMismatch(t *testing.T) { |
| 21 | + subject := cidlink.DefaultLinkSystem() |
| 22 | + storage := &memstore.Store{} |
| 23 | + subject.SetReadStorage(storage) |
| 24 | + subject.SetWriteStorage(storage) |
| 25 | + |
| 26 | + // Construct some test IPLD node. |
| 27 | + wantNode := fluent.MustBuildMap(basicnode.Prototype.Map, 1, func(na fluent.MapAssembler) { |
| 28 | + na.AssembleEntry("fish").AssignString("barreleye") |
| 29 | + }) |
| 30 | + |
| 31 | + // Encode as raw value to be used for testing LoadRaw |
| 32 | + var buf bytes.Buffer |
| 33 | + qt.Check(t, dagcbor.Encode(wantNode, &buf), qt.IsNil) |
| 34 | + wantNodeRaw := buf.Bytes() |
| 35 | + |
| 36 | + // Store the test IPLD node and get link back. |
| 37 | + lctx := ipld.LinkContext{Ctx: context.TODO()} |
| 38 | + gotLink, err := subject.Store(lctx, cidlink.LinkPrototype{ |
| 39 | + Prefix: cid.Prefix{ |
| 40 | + Version: 1, |
| 41 | + Codec: uint64(multicodec.DagCbor), |
| 42 | + MhType: uint64(multicodec.Sha2_256), |
| 43 | + MhLength: -1, |
| 44 | + }, |
| 45 | + }, wantNode) |
| 46 | + qt.Check(t, err, qt.IsNil) |
| 47 | + gotCidlink := gotLink.(cidlink.Link) |
| 48 | + |
| 49 | + // Assert all load variations return expected values for different link representations. |
| 50 | + for _, test := range []struct { |
| 51 | + name string |
| 52 | + link datamodel.Link |
| 53 | + }{ |
| 54 | + {"datamodel.Link", gotLink}, |
| 55 | + {"cidlink.Link", gotCidlink}, |
| 56 | + {"&cidlink.Link", &gotCidlink}, |
| 57 | + } { |
| 58 | + t.Run(test.name, func(t *testing.T) { |
| 59 | + gotNode, err := subject.Load(lctx, test.link, basicnode.Prototype.Any) |
| 60 | + qt.Check(t, err, qt.IsNil) |
| 61 | + qt.Check(t, ipld.DeepEqual(wantNode, gotNode), qt.IsTrue) |
| 62 | + |
| 63 | + gotNodeRaw, err := subject.LoadRaw(lctx, test.link) |
| 64 | + qt.Check(t, err, qt.IsNil) |
| 65 | + qt.Check(t, bytes.Equal(wantNodeRaw, gotNodeRaw), qt.IsTrue) |
| 66 | + |
| 67 | + gotNode, gotNodeRaw, err = subject.LoadPlusRaw(lctx, test.link, basicnode.Prototype.Any) |
| 68 | + qt.Check(t, err, qt.IsNil) |
| 69 | + qt.Check(t, ipld.DeepEqual(wantNode, gotNode), qt.IsTrue) |
| 70 | + qt.Check(t, bytes.Equal(wantNodeRaw, gotNodeRaw), qt.IsTrue) |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
0 commit comments