Skip to content

Commit

Permalink
chore: expand testing to cover dag-pb identity CIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Sep 2, 2022
1 parent ffdf335 commit f8755fa
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 32 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ require (
github.com/ipfs/go-unixfs v0.3.1
github.com/ipld/go-car v0.4.0
github.com/ipld/go-car/v2 v2.4.1
github.com/ipld/go-codec-dagpb v1.3.1
github.com/ipld/go-ipld-prime v0.17.0
github.com/jbenet/go-random v0.0.0-20190219211222-123a90aedc0c
github.com/jpillora/backoff v1.0.0
Expand Down Expand Up @@ -100,7 +101,6 @@ require (
github.com/ipfs/go-peertaskqueue v0.7.1 // indirect
github.com/ipfs/go-unixfsnode v1.4.0 // indirect
github.com/ipfs/go-verifcid v0.0.1 // indirect
github.com/ipld/go-codec-dagpb v1.3.1 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/jbenet/goprocess v0.1.4 // indirect
github.com/klauspost/cpuid/v2 v2.0.12 // indirect
Expand Down
38 changes: 7 additions & 31 deletions retrievalmarket/impl/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ import (
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/namespace"
dss "github.com/ipfs/go-datastore/sync"
"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/qp"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
selectorparse "github.com/ipld/go-ipld-prime/traversal/selector/parse"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/multiformats/go-multicodec"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
cbg "github.com/whyrusleeping/cbor-gen"
Expand Down Expand Up @@ -645,26 +641,6 @@ func TestDynamicPricing(t *testing.T) {
}
}

func makeIdentityCidWith(cids []cid.Cid, padding ...[]byte) (cid.Cid, error) {
node, err := qp.BuildList(basicnode.Prototype.List, int64(len(cids)+len(padding)), func(la datamodel.ListAssembler) {
for _, cid := range cids {
qp.ListEntry(la, qp.Link(cidlink.Link{Cid: cid}))
}
for _, pad := range padding {
qp.ListEntry(la, qp.Bytes(pad))
}
})
if err != nil {
return cid.Undef, err
}
encoded, err := ipld.Encode(node, dagcbor.Encode)
if err != nil {
return cid.Undef, err
}
lp := cidlink.LinkPrototype{Prefix: cid.Prefix{Version: 1, Codec: 0x71, MhType: 0x0}}
return lp.BuildLink(encoded).(cidlink.Link).Cid, nil
}

func TestHandleQueryStream(t *testing.T) {
ctx := context.Background()

Expand All @@ -677,17 +653,17 @@ func TestHandleQueryStream(t *testing.T) {
paddedSize2 := uint64(2234)
expectedSize2 := uint64(abi.PaddedPieceSize(paddedSize2).Unpadded())

identityCidWith1, err := makeIdentityCidWith([]cid.Cid{payloadCID})
identityCidWith1, err := tut.MakeIdentityCidWith([]cid.Cid{payloadCID}, multicodec.DagPb)
require.NoError(t, err)
identityCidWithBoth, err := makeIdentityCidWith([]cid.Cid{payloadCID, payloadCID2}, []byte("and some padding"))
identityCidWithBoth, err := tut.MakeIdentityCidWith([]cid.Cid{payloadCID, payloadCID2}, multicodec.DagCbor, []byte("and some padding"))
require.NoError(t, err)
identityCidWithBothNested, err := makeIdentityCidWith([]cid.Cid{identityCidWith1, payloadCID2})
identityCidWithBothNested, err := tut.MakeIdentityCidWith([]cid.Cid{identityCidWith1, payloadCID2}, multicodec.DagPb)
require.NoError(t, err)
identityCidWithBogus, err := makeIdentityCidWith([]cid.Cid{payloadCID, tut.GenerateCids(1)[0]})
identityCidWithBogus, err := tut.MakeIdentityCidWith([]cid.Cid{payloadCID, tut.GenerateCids(1)[0]}, multicodec.DagPb)
require.NoError(t, err)
identityCidTooBig, err := makeIdentityCidWith([]cid.Cid{payloadCID}, tut.RandomBytes(2048))
identityCidTooBig, err := tut.MakeIdentityCidWith([]cid.Cid{payloadCID}, multicodec.DagCbor, tut.RandomBytes(2048))
require.NoError(t, err)
identityCidTooManyLinks, err := makeIdentityCidWith(tut.GenerateCids(33))
identityCidTooManyLinks, err := tut.MakeIdentityCidWith(tut.GenerateCids(33), multicodec.DagPb)
require.NoError(t, err)

expectedPieceCID := tut.GenerateCids(1)[0]
Expand Down
68 changes: 68 additions & 0 deletions shared_testutil/test_identitycid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package shared_testutil

import (
"github.com/ipfs/go-cid"
dagpb "github.com/ipld/go-codec-dagpb"
"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/qp"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
"github.com/multiformats/go-multicodec"
)

// MakeIdentityCidWith will create either a dag-pb or dag-cbor identity CID
// containing the provided list of CIDs and optional byte blocks.
// The dag-cbor identity CID will be a simple list with all items appended.
// This can also be used recursively to increase the ridiculousness.
func MakeIdentityCidWith(cids []cid.Cid, codec multicodec.Code, padding ...[]byte) (cid.Cid, error) {
var encoded []byte
var node datamodel.Node
var err error
switch codec {
case multicodec.DagPb:
if len(padding) > 1 {
panic("dag-pb only supports one bytes block")
}
node, err = qp.BuildMap(dagpb.Type.PBNode, 2, func(ma datamodel.MapAssembler) {
if len(padding) > 0 {
qp.MapEntry(ma, "Data", qp.Bytes(padding[0]))
}
qp.MapEntry(ma, "Links", qp.List(int64(len(cids)), func(la datamodel.ListAssembler) {
for _, cid := range cids {
qp.ListEntry(la, qp.Map(1, func(ma datamodel.MapAssembler) {
qp.MapEntry(ma, "Hash", qp.Link(cidlink.Link{Cid: cid}))
}))
}
}))
})
if err != nil {
return cid.Undef, err
}
encoded, err = ipld.Encode(node, dagpb.Encode)
case multicodec.DagCbor:
// for dag-cbor we'll just build a list and push everything into it
node, err = qp.BuildList(basicnode.Prototype.List, int64(len(cids)+len(padding)), func(la datamodel.ListAssembler) {
for _, cid := range cids {
qp.ListEntry(la, qp.Link(cidlink.Link{Cid: cid}))
}
for _, pad := range padding {
qp.ListEntry(la, qp.Bytes(pad))
}
})
if err != nil {
return cid.Undef, err
}
encoded, err = ipld.Encode(node, dagcbor.Encode)
default:
panic("unsupported codec")
}

if err != nil {
return cid.Undef, err
}

lp := cidlink.LinkPrototype{Prefix: cid.Prefix{Version: 1, Codec: uint64(codec), MhType: 0x0}}
return lp.BuildLink(encoded).(cidlink.Link).Cid, nil
}

0 comments on commit f8755fa

Please sign in to comment.