From 47c2f938e3f32f73c4a0efc239ed218b44fc26ac Mon Sep 17 00:00:00 2001 From: gammazero Date: Mon, 24 Jun 2024 22:25:53 -0700 Subject: [PATCH] Move test utility functions to test repo 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. --- dagsync/ipnisync/head/signedhead_test.go | 4 +- dagsync/test/util.go | 4 +- dhash/dhash_test.go | 4 +- find/model/model_test.go | 4 +- go.mod | 1 + go.sum | 2 + ingest/model/ingest_request_test.go | 4 +- ingest/schema/envelope_test.go | 30 +++--- ingest/schema/types_test.go | 10 +- metadata/graphsync_filecoinv1_test.go | 4 +- metadata/metadata_test.go | 4 +- test/random.go | 115 ----------------------- 12 files changed, 37 insertions(+), 149 deletions(-) delete mode 100644 test/random.go diff --git a/dagsync/ipnisync/head/signedhead_test.go b/dagsync/ipnisync/head/signedhead_test.go index 4e6ebac..520a18a 100644 --- a/dagsync/ipnisync/head/signedhead_test.go +++ b/dagsync/ipnisync/head/signedhead_test.go @@ -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" @@ -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) diff --git a/dagsync/test/util.go b/dagsync/test/util.go index 62a25c2..4f5ee5c 100644 --- a/dagsync/test/util.go +++ b/dagsync/test/util.go @@ -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" @@ -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 { diff --git a/dhash/dhash_test.go b/dhash/dhash_test.go index 4b894be..16087f3 100644 --- a/dhash/dhash_test.go +++ b/dhash/dhash_test.go @@ -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" ) @@ -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() diff --git a/find/model/model_test.go b/find/model/model_test.go index 5458806..eabfbc7 100644 --- a/find/model/model_test.go +++ b/find/model/model_test.go @@ -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" ) @@ -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], diff --git a/go.mod b/go.mod index 7efe9d0..1a0edef 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index b279f52..9cd2728 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/ingest/model/ingest_request_test.go b/ingest/model/ingest_request_test.go index 63047b3..31eaf38 100644 --- a/ingest/model/ingest_request_test.go +++ b/ingest/model/ingest_request_test.go @@ -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") diff --git a/ingest/schema/envelope_test.go b/ingest/schema/envelope_test.go index 8e24e1f..608278d 100644 --- a/ingest/schema/envelope_test.go +++ b/ingest/schema/envelope_test.go @@ -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" @@ -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() @@ -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() @@ -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"), }, }, @@ -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() @@ -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(), @@ -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"), }, { @@ -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) }) @@ -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() @@ -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(), @@ -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"), }, }, @@ -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() @@ -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(), @@ -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"), }, }, diff --git a/ingest/schema/types_test.go b/ingest/schema/types_test.go index 3e6ced9..95f014d 100644 --- a/ingest/schema/types_test.go +++ b/ingest/schema/types_test.go @@ -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" @@ -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, @@ -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, @@ -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, @@ -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, diff --git a/metadata/graphsync_filecoinv1_test.go b/metadata/graphsync_filecoinv1_test.go index ba4b7f7..9d322f4 100644 --- a/metadata/graphsync_filecoinv1_test.go +++ b/metadata/graphsync_filecoinv1_test.go @@ -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], diff --git a/metadata/metadata_test.go b/metadata/metadata_test.go index 6d929f2..0d6285a 100644 --- a/metadata/metadata_test.go +++ b/metadata/metadata_test.go @@ -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 diff --git a/test/random.go b/test/random.go deleted file mode 100644 index 2c47dc5..0000000 --- a/test/random.go +++ /dev/null @@ -1,115 +0,0 @@ -package test - -import ( - "fmt" - "math/rand" - "sync/atomic" - - "github.com/ipfs/go-cid" - "github.com/ipni/go-libipni/ingest/schema" - "github.com/libp2p/go-libp2p/core/crypto" - "github.com/libp2p/go-libp2p/core/peer" - "github.com/libp2p/go-libp2p/core/test" - "github.com/multiformats/go-multiaddr" - "github.com/multiformats/go-multihash" -) - -var globalSeed atomic.Int64 - -// RandomAddrs returns a slice of n random unique addresses. -func RandomAddrs(n int) []string { - rng := rand.New(rand.NewSource(globalSeed.Add(1))) - addrs := make([]string, n) - addrSet := make(map[string]struct{}) - for i := 0; i < n; i++ { - addr := fmt.Sprintf("/ip4/%d.%d.%d.%d/tcp/%d", rng.Int()%255, rng.Intn(254)+1, rng.Intn(254)+1, rng.Intn(254)+1, rng.Intn(48157)+1024) - if _, ok := addrSet[addr]; ok { - i-- - continue - } - addrs[i] = addr - } - return addrs -} - -// RandomMultiaddrs returns a slice of n random unique Multiaddrs. -func RandomMultiaddrs(n int) []multiaddr.Multiaddr { - rng := rand.New(rand.NewSource(globalSeed.Add(1))) - maddrs := make([]multiaddr.Multiaddr, n) - addrSet := make(map[string]struct{}) - for i := 0; i < n; i++ { - addr := fmt.Sprintf("/ip4/%d.%d.%d.%d/tcp/%d", rng.Int()%255, rng.Intn(254)+1, rng.Intn(254)+1, rng.Intn(254)+1, rng.Intn(48157)+1024) - if _, ok := addrSet[addr]; ok { - i-- - continue - } - maddr, err := multiaddr.NewMultiaddr(addr) - if err != nil { - panic(err) - } - maddrs[i] = maddr - } - return maddrs -} - -// RandomCids returns a slice of n random unique CIDs. -func RandomCids(n int) []cid.Cid { - rng := rand.New(rand.NewSource(globalSeed.Add(1))) - prefix := schema.Linkproto.Prefix - cids := make([]cid.Cid, n) - set := make(map[string]struct{}) - for i := 0; i < n; i++ { - b := make([]byte, 10*n) - rng.Read(b) - if _, ok := set[string(b)]; ok { - i-- - continue - } - c, err := prefix.Sum(b) - if err != nil { - panic(err) - } - cids[i] = c - } - return cids -} - -// RandomIdentity returns a random unique peer ID, private key, and public key. -func RandomIdentity() (peer.ID, crypto.PrivKey, crypto.PubKey) { -retry: - privKey, pubKey, err := test.RandTestKeyPair(crypto.Ed25519, 256) - if err != nil { - panic(err) - } - peerID, err := peer.IDFromPublicKey(pubKey) - if err != nil { - panic(err) - } - idSet := make(map[peer.ID]struct{}) - if _, ok := idSet[peerID]; ok { - goto retry - } - return peerID, privKey, pubKey -} - -// RandomMultihashes returns a slice of n random unique Multihashes. -func RandomMultihashes(n int) []multihash.Multihash { - rng := rand.New(rand.NewSource(globalSeed.Add(1))) - prefix := schema.Linkproto.Prefix - set := make(map[string]struct{}) - mhashes := make([]multihash.Multihash, n) - for i := 0; i < n; i++ { - b := make([]byte, 10*n+16) - rng.Read(b) - if _, ok := set[string(b)]; ok { - i-- - continue - } - c, err := prefix.Sum(b) - if err != nil { - panic(err.Error()) - } - mhashes[i] = c.Hash() - } - return mhashes -}