Skip to content

Commit

Permalink
Move test utility functions to test repo
Browse files Browse the repository at this point in the history
The test package contains ligic used in tests in various projects. This logic was moved into the test repo as it should be versioned separate from this module.
  • Loading branch information
gammazero committed Jun 25, 2024
1 parent d083517 commit 47c2f93
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 149 deletions.
4 changes: 2 additions & 2 deletions dagsync/ipnisync/head/signedhead_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/ipfs/go-cid"
_ "github.com/ipld/go-ipld-prime/codec/dagjson"
headschema "github.com/ipni/go-libipni/dagsync/ipnisync/head"
"github.com/ipni/go-libipni/test"
"github.com/ipni/test/random"
ic "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -69,7 +69,7 @@ func generateSignedHead() *headschema.SignedHead {
if err != nil {
panic(err.Error())
}
mhs := test.RandomMultihashes(1)
mhs := random.Multihashes(1)
headCid := cid.NewCidV1(cid.Raw, mhs[0])

sh, err := headschema.NewSignedHead(headCid, testTopic, privKey)
Expand Down
4 changes: 2 additions & 2 deletions dagsync/test/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/ipni/go-libipni/dagsync/ipnisync"
"github.com/ipni/go-libipni/ingest/schema"
"github.com/ipni/go-libipni/maurl"
"github.com/ipni/go-libipni/test"
"github.com/ipni/test/random"
"github.com/libp2p/go-libp2p"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/crypto"
Expand Down Expand Up @@ -170,7 +170,7 @@ func encode(lsys ipld.LinkSystem, n ipld.Node) (ipld.Node, ipld.Link) {
}

func RandomCids(n int) []cid.Cid {
return test.RandomCids(n)
return random.Cids(n)
}

func MkLinkSystem(ds datastore.Batching) ipld.LinkSystem {
Expand Down
4 changes: 2 additions & 2 deletions dhash/dhash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"math/rand"
"testing"

"github.com/ipni/go-libipni/test"
"github.com/ipni/test/random"
"github.com/multiformats/go-multihash"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -60,7 +60,7 @@ func TestCanDecryptEncryptedValue(t *testing.T) {
}

func TestSecondMultihash(t *testing.T) {
mh := test.RandomMultihashes(1)[0]
mh := random.Multihashes(1)[0]
smh := SecondMultihash(mh)

h := sha256.New()
Expand Down
4 changes: 2 additions & 2 deletions find/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/ipni/go-libipni/find/model"
"github.com/ipni/go-libipni/test"
"github.com/ipni/test/random"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
)
Expand All @@ -32,7 +32,7 @@ func TestMarshal(t *testing.T) {
MultihashResults: []model.MultihashResult{},
}

mhs := test.RandomMultihashes(3)
mhs := random.Multihashes(3)
for i := range mhs {
resp.MultihashResults = append(resp.MultihashResults, model.MultihashResult{
Multihash: mhs[i],
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/ipfs/go-ipld-format v0.6.0
github.com/ipfs/go-log/v2 v2.5.1
github.com/ipld/go-ipld-prime v0.21.0
github.com/ipni/test v0.0.2
github.com/libp2p/go-libp2p v0.35.1
github.com/libp2p/go-libp2p-pubsub v0.11.0
github.com/libp2p/go-msgio v0.3.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ github.com/ipfs/go-log/v2 v2.5.1 h1:1XdUzF7048prq4aBjDQQ4SL5RxftpRGdXhNRwKSAlcY=
github.com/ipfs/go-log/v2 v2.5.1/go.mod h1:prSpmC1Gpllc9UYWxDiZDreBYw7zp4Iqp1kOLU9U5UI=
github.com/ipld/go-ipld-prime v0.21.0 h1:n4JmcpOlPDIxBcY037SVfpd1G+Sj1nKZah0m6QH9C2E=
github.com/ipld/go-ipld-prime v0.21.0/go.mod h1:3RLqy//ERg/y5oShXXdx5YIp50cFGOanyMctpPjsvxQ=
github.com/ipni/test v0.0.2 h1:SMWVtxWuMpolc9Xff2Rl1unrTVfgiD4a9WoOreJNtnw=
github.com/ipni/test v0.0.2/go.mod h1:Gx72ruHbdlwtO2pU2EwwqVm7IxXHD524H22BRnJhxoI=
github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus=
github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
github.com/jbenet/go-cienv v0.1.0/go.mod h1:TqNnHUmJgXau0nCzC7kXWeotg3J9W34CUv5Djy1+FlA=
Expand Down
4 changes: 2 additions & 2 deletions ingest/model/ingest_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"bytes"
"testing"

"github.com/ipni/go-libipni/test"
"github.com/ipni/test/random"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
p2ptest "github.com/libp2p/go-libp2p/core/test"
)

func TestIngestRequest(t *testing.T) {
mhs := test.RandomMultihashes(1)
mhs := random.Multihashes(1)

metadata := []byte("test-metadata")

Expand Down
30 changes: 15 additions & 15 deletions ingest/schema/envelope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/storage/memstore"
stischema "github.com/ipni/go-libipni/ingest/schema"
"github.com/ipni/go-libipni/test"
"github.com/ipni/test/random"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
p2ptest "github.com/libp2p/go-libp2p/core/test"
Expand Down Expand Up @@ -44,7 +44,7 @@ func testSignAndVerify(t *testing.T, signer func(*stischema.Advertisement, crypt
require.NoError(t, err)

ec := stischema.EntryChunk{
Entries: test.RandomMultihashes(10),
Entries: random.Multihashes(10),
}

node, err := ec.ToNode()
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestSignShouldFailIfAdHasExtendedProviders(t *testing.T) {
require.NoError(t, err)

ec := stischema.EntryChunk{
Entries: test.RandomMultihashes(10),
Entries: random.Multihashes(10),
}

node, err := ec.ToNode()
Expand All @@ -115,7 +115,7 @@ func TestSignShouldFailIfAdHasExtendedProviders(t *testing.T) {
Providers: []stischema.Provider{
{
ID: ep1PeerID.String(),
Addresses: test.RandomAddrs(2),
Addresses: random.Addrs(2),
Metadata: []byte("ep1-metadata"),
},
},
Expand All @@ -132,7 +132,7 @@ func TestSignWithExtendedProviderAndVerify(t *testing.T) {
lsys.SetWriteStorage(store)

ec := stischema.EntryChunk{
Entries: test.RandomMultihashes(10),
Entries: random.Multihashes(10),
}

node, err := ec.ToNode()
Expand All @@ -143,7 +143,7 @@ func TestSignWithExtendedProviderAndVerify(t *testing.T) {
ep1Priv, ep1PeerID := generateIdentityAndKey(t)
ep2Priv, ep2PeerID := generateIdentityAndKey(t)
mpPriv, mpPeerID := generateIdentityAndKey(t)
mpAddrs := test.RandomAddrs(2)
mpAddrs := random.Addrs(2)

adv := stischema.Advertisement{
Provider: mpPeerID.String(),
Expand All @@ -155,12 +155,12 @@ func TestSignWithExtendedProviderAndVerify(t *testing.T) {
Providers: []stischema.Provider{
{
ID: ep1PeerID.String(),
Addresses: test.RandomAddrs(2),
Addresses: random.Addrs(2),
Metadata: []byte("ep1-metadata"),
},
{
ID: ep2PeerID.String(),
Addresses: test.RandomAddrs(2),
Addresses: random.Addrs(2),
Metadata: []byte("ep2-metadata"),
},
{
Expand Down Expand Up @@ -223,7 +223,7 @@ func TestSigVerificationFailsIfTheExtendedProviderMetadataIsIncorrect(t *testing

func TestSigVerificationFailsIfTheExtendedProviderAddrsAreIncorrect(t *testing.T) {
extendedSignatureTest(t, func(adv stischema.Advertisement) {
adv.ExtendedProvider.Providers[1].Addresses = test.RandomAddrs(10)
adv.ExtendedProvider.Providers[1].Addresses = random.Addrs(10)
_, err := adv.VerifySignature()
require.Error(t, err)
})
Expand Down Expand Up @@ -262,7 +262,7 @@ func TestSignFailsIfMainProviderIsNotInExtendedList(t *testing.T) {
lsys.SetWriteStorage(store)

ec := stischema.EntryChunk{
Entries: test.RandomMultihashes(10),
Entries: random.Multihashes(10),
}

node, err := ec.ToNode()
Expand All @@ -272,7 +272,7 @@ func TestSignFailsIfMainProviderIsNotInExtendedList(t *testing.T) {

ep1Priv, ep1PeerID := generateIdentityAndKey(t)
mpPriv, mpPeerID := generateIdentityAndKey(t)
mpAddrs := test.RandomAddrs(2)
mpAddrs := random.Addrs(2)

adv := stischema.Advertisement{
Provider: mpPeerID.String(),
Expand All @@ -284,7 +284,7 @@ func TestSignFailsIfMainProviderIsNotInExtendedList(t *testing.T) {
Providers: []stischema.Provider{
{
ID: ep1PeerID.String(),
Addresses: test.RandomAddrs(2),
Addresses: random.Addrs(2),
Metadata: []byte("ep1-metadata"),
},
},
Expand All @@ -310,7 +310,7 @@ func extendedSignatureTest(t *testing.T, testFunc func(adv stischema.Advertiseme
lsys.SetWriteStorage(store)

ec := stischema.EntryChunk{
Entries: test.RandomMultihashes(10),
Entries: random.Multihashes(10),
}

node, err := ec.ToNode()
Expand All @@ -320,7 +320,7 @@ func extendedSignatureTest(t *testing.T, testFunc func(adv stischema.Advertiseme

ep1Priv, ep1PeerID := generateIdentityAndKey(t)
mpPriv, mpPeerID := generateIdentityAndKey(t)
mpAddrs := test.RandomAddrs(2)
mpAddrs := random.Addrs(2)

adv := stischema.Advertisement{
Provider: mpPeerID.String(),
Expand All @@ -337,7 +337,7 @@ func extendedSignatureTest(t *testing.T, testFunc func(adv stischema.Advertiseme
},
{
ID: ep1PeerID.String(),
Addresses: test.RandomAddrs(2),
Addresses: random.Addrs(2),
Metadata: []byte("ep1-metadata"),
},
},
Expand Down
10 changes: 5 additions & 5 deletions ingest/schema/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
ipldSchema "github.com/ipld/go-ipld-prime/schema"
"github.com/ipld/go-ipld-prime/storage/memstore"
stischema "github.com/ipni/go-libipni/ingest/schema"
"github.com/ipni/go-libipni/test"
"github.com/ipni/test/random"
"github.com/multiformats/go-multicodec"
"github.com/multiformats/go-multihash"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -101,7 +101,7 @@ var newSchema []byte
func TestOldAdsCanBeReadWithNewStructs(t *testing.T) {
oldSchema := createTestSchema(t, oldSchema, (*OldAdvertisement)(nil))

mhs := test.RandomMultihashes(7)
mhs := random.Multihashes(7)
prev := ipld.Link(cidlink.Link{Cid: cid.NewCidV1(cid.Raw, mhs[0])})
oldAd := &OldAdvertisement{
PreviousID: prev,
Expand Down Expand Up @@ -146,7 +146,7 @@ func TestNewAdsCanBeReadWithOldStructs(t *testing.T) {
oldSchema := createTestSchema(t, oldSchema, (*OldAdvertisement)(nil))
newSchema := createTestSchema(t, newSchema, (*stischema.Advertisement)(nil))

mhs := test.RandomMultihashes(7)
mhs := random.Multihashes(7)
prev := ipld.Link(cidlink.Link{Cid: cid.NewCidV1(cid.Raw, mhs[0])})
newAd := &stischema.Advertisement{
PreviousID: prev,
Expand Down Expand Up @@ -267,7 +267,7 @@ func Test_LinkLoadNoEntries(t *testing.T) {
}

func generateAdvertisement() *stischema.Advertisement {
mhs := test.RandomMultihashes(7)
mhs := random.Multihashes(7)
prev := ipld.Link(cidlink.Link{Cid: cid.NewCidV1(cid.Raw, mhs[0])})
return &stischema.Advertisement{
PreviousID: prev,
Expand All @@ -284,7 +284,7 @@ func generateAdvertisement() *stischema.Advertisement {
}

func generateEntryChunk() *stischema.EntryChunk {
mhs := test.RandomMultihashes(100)
mhs := random.Multihashes(100)
next := ipld.Link(cidlink.Link{Cid: cid.NewCidV1(cid.Raw, mhs[0])})
return &stischema.EntryChunk{
Entries: mhs,
Expand Down
4 changes: 2 additions & 2 deletions metadata/graphsync_filecoinv1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"testing"

"github.com/ipni/go-libipni/metadata"
"github.com/ipni/go-libipni/test"
"github.com/ipni/test/random"
"github.com/multiformats/go-multicodec"
"github.com/multiformats/go-varint"
"github.com/stretchr/testify/require"
)

func TestRoundTripDataTransferFilecoin(t *testing.T) {
cids := test.RandomCids(4)
cids := random.Cids(4)
filecoinV1Datas := []*metadata.GraphsyncFilecoinV1{
{
PieceCID: cids[0],
Expand Down
4 changes: 2 additions & 2 deletions metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"testing"

"github.com/ipni/go-libipni/metadata"
"github.com/ipni/go-libipni/test"
"github.com/ipni/test/random"
"github.com/multiformats/go-multicodec"
"github.com/multiformats/go-varint"
"github.com/stretchr/testify/require"
)

func TestMetadata(t *testing.T) {
cids := test.RandomCids(4)
cids := random.Cids(4)
tests := []struct {
name string
givenTransports []metadata.Protocol
Expand Down
Loading

0 comments on commit 47c2f93

Please sign in to comment.