diff --git a/dagsync/http_test.go b/dagsync/http_test.go index b281952..f1096d8 100644 --- a/dagsync/http_test.go +++ b/dagsync/http_test.go @@ -9,6 +9,7 @@ import ( "github.com/ipfs/go-cid" "github.com/ipfs/go-datastore" dssync "github.com/ipfs/go-datastore/sync" + "github.com/ipfs/go-test/random" "github.com/ipld/go-ipld-prime/linking" cidlink "github.com/ipld/go-ipld-prime/linking/cid" "github.com/ipld/go-ipld-prime/node/basicnode" @@ -138,7 +139,7 @@ func TestSyncFnHttp(t *testing.T) { // Try to sync with a non-existing cid to chack that sync returns with err, // and SyncFinished watcher does not get event. - cids := test.RandomCids(1) + cids := random.Cids(1) ctx, syncncl := context.WithTimeout(context.Background(), time.Second) defer syncncl() diff --git a/dagsync/ipnisync/head/signedhead_test.go b/dagsync/ipnisync/head/signedhead_test.go index 4e6ebac..62fc678 100644 --- a/dagsync/ipnisync/head/signedhead_test.go +++ b/dagsync/ipnisync/head/signedhead_test.go @@ -7,9 +7,9 @@ import ( "testing" "github.com/ipfs/go-cid" + "github.com/ipfs/go-test/random" _ "github.com/ipld/go-ipld-prime/codec/dagjson" headschema "github.com/ipni/go-libipni/dagsync/ipnisync/head" - "github.com/ipni/go-libipni/test" 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/selector_test.go b/dagsync/selector_test.go index b7c9488..3941e03 100644 --- a/dagsync/selector_test.go +++ b/dagsync/selector_test.go @@ -4,13 +4,13 @@ import ( "testing" "github.com/ipfs/go-cid" + "github.com/ipfs/go-test/random" "github.com/ipld/go-ipld-prime" "github.com/ipld/go-ipld-prime/datamodel" cidlink "github.com/ipld/go-ipld-prime/linking/cid" basicnode "github.com/ipld/go-ipld-prime/node/basic" "github.com/ipld/go-ipld-prime/traversal/selector" selectorbuilder "github.com/ipld/go-ipld-prime/traversal/selector/builder" - "github.com/ipni/go-libipni/dagsync/test" "github.com/stretchr/testify/require" ) @@ -31,8 +31,8 @@ func TestGetStopNodeWhenNil(t *testing.T) { } func TestGetRecursionLimit(t *testing.T) { - testCid := test.RandomCids(1) - testStopLink := cidlink.Link{Cid: testCid[0]} + testCid := random.Cids(1)[0] + testStopLink := cidlink.Link{Cid: testCid} ssb := selectorbuilder.NewSelectorSpecBuilder(basicnode.Prototype.Any) tests := []struct { name string @@ -98,8 +98,8 @@ func TestGetRecursionLimit(t *testing.T) { } func TestWithRecursionLimit(t *testing.T) { - testCid := test.RandomCids(1) - testStopLink := cidlink.Link{Cid: testCid[0]} + testCid := random.Cids(1)[0] + testStopLink := cidlink.Link{Cid: testCid} ssb := selectorbuilder.NewSelectorSpecBuilder(basicnode.Prototype.Any) tests := []struct { name string diff --git a/dagsync/sync_test.go b/dagsync/sync_test.go index eeca396..3c0c429 100644 --- a/dagsync/sync_test.go +++ b/dagsync/sync_test.go @@ -9,6 +9,7 @@ import ( "github.com/ipfs/go-cid" "github.com/ipfs/go-datastore" dssync "github.com/ipfs/go-datastore/sync" + "github.com/ipfs/go-test/random" "github.com/ipld/go-ipld-prime" cidlink "github.com/ipld/go-ipld-prime/linking/cid" "github.com/ipni/go-libipni/announce" @@ -157,7 +158,7 @@ func TestSyncFn(t *testing.T) { // Try to sync with a non-existing cid to check that sync returns with err, // and SyncFinished watcher does not get event. - cids := test.RandomCids(1) + cids := random.Cids(1) ctx, syncncl := context.WithTimeout(context.Background(), updateTimeout) defer syncncl() peerInfo := peer.AddrInfo{ @@ -361,7 +362,7 @@ func TestLatestSyncFailure(t *testing.T) { Addrs: pub.Addrs(), } // Announce bad CID. - badCid := test.RandomCids(1)[0] + badCid := random.Cids(1)[0] err = sub.Announce(context.Background(), badCid, pubInfo) require.NoError(t, err) // Check for fetch failure. diff --git a/dagsync/test/util.go b/dagsync/test/util.go index 62a25c2..aa4bd8c 100644 --- a/dagsync/test/util.go +++ b/dagsync/test/util.go @@ -23,7 +23,6 @@ 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/libp2p/go-libp2p" pubsub "github.com/libp2p/go-libp2p-pubsub" "github.com/libp2p/go-libp2p/core/crypto" @@ -169,10 +168,6 @@ func encode(lsys ipld.LinkSystem, n ipld.Node) (ipld.Node, ipld.Link) { return n, lnk } -func RandomCids(n int) []cid.Cid { - return test.RandomCids(n) -} - func MkLinkSystem(ds datastore.Batching) ipld.LinkSystem { lsys := cidlink.DefaultLinkSystem() lsys.StorageReadOpener = func(_ ipld.LinkContext, lnk ipld.Link) (io.Reader, error) { diff --git a/dhash/dhash_test.go b/dhash/dhash_test.go index 4b894be..2041581 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/ipfs/go-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..78788bf 100644 --- a/find/model/model_test.go +++ b/find/model/model_test.go @@ -4,8 +4,8 @@ import ( "bytes" "testing" + "github.com/ipfs/go-test/random" "github.com/ipni/go-libipni/find/model" - "github.com/ipni/go-libipni/test" "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 5625e36..b9a187f 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/ipfs/go-datastore v0.6.0 github.com/ipfs/go-ipld-format v0.6.0 github.com/ipfs/go-log/v2 v2.5.1 + github.com/ipfs/go-test v0.0.2 github.com/ipld/go-ipld-prime v0.21.0 github.com/libp2p/go-libp2p v0.35.1 github.com/libp2p/go-libp2p-pubsub v0.11.0 diff --git a/go.sum b/go.sum index c3a13d0..def0754 100644 --- a/go.sum +++ b/go.sum @@ -139,6 +139,8 @@ github.com/ipfs/go-ipld-format v0.6.0 h1:VEJlA2kQ3LqFSIm5Vu6eIlSxD/Ze90xtc4Meten github.com/ipfs/go-ipld-format v0.6.0/go.mod h1:g4QVMTn3marU3qXchwjpKPKgJv+zF+OlaKMyhJ4LHPg= 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/ipfs/go-test v0.0.2 h1:Wdxl4bKEdjEM8SLiulXMHlAQwHYOhX3CSBoUoEvncmM= +github.com/ipfs/go-test v0.0.2/go.mod h1:qhIM1EluEfElKKM6fnWxGn822/z9knUGM1+I/OAQNKI= 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/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus= diff --git a/ingest/model/ingest_request_test.go b/ingest/model/ingest_request_test.go index 63047b3..11bf321 100644 --- a/ingest/model/ingest_request_test.go +++ b/ingest/model/ingest_request_test.go @@ -4,25 +4,13 @@ import ( "bytes" "testing" - "github.com/ipni/go-libipni/test" - "github.com/libp2p/go-libp2p/core/crypto" - "github.com/libp2p/go-libp2p/core/peer" - p2ptest "github.com/libp2p/go-libp2p/core/test" + "github.com/ipfs/go-test/random" ) func TestIngestRequest(t *testing.T) { - mhs := test.RandomMultihashes(1) - + mhs := random.Multihashes(1) metadata := []byte("test-metadata") - - privKey, pubKey, err := p2ptest.RandTestKeyPair(crypto.Ed25519, 256) - if err != nil { - t.Fatal(err) - } - peerID, err := peer.IDFromPublicKey(pubKey) - if err != nil { - t.Fatal(err) - } + peerID, privKey, _ := random.Identity() ctxID := []byte("test-context-id") address := "/ip4/127.0.0.1/tcp/7777" diff --git a/ingest/schema/envelope_test.go b/ingest/schema/envelope_test.go index 8e24e1f..c1d49b2 100644 --- a/ingest/schema/envelope_test.go +++ b/ingest/schema/envelope_test.go @@ -6,15 +6,14 @@ import ( "testing" "time" + "github.com/ipfs/go-test/random" "github.com/ipld/go-ipld-prime" _ "github.com/ipld/go-ipld-prime/codec/dagjson" 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/libp2p/go-libp2p/core/crypto" "github.com/libp2p/go-libp2p/core/peer" - p2ptest "github.com/libp2p/go-libp2p/core/test" "github.com/stretchr/testify/require" ) @@ -38,13 +37,10 @@ func testSignAndVerify(t *testing.T, signer func(*stischema.Advertisement, crypt lsys.SetReadStorage(store) lsys.SetWriteStorage(store) - priv, pub, err := p2ptest.RandTestKeyPair(crypto.Ed25519, 256) - require.NoError(t, err) - peerID, err := peer.IDFromPublicKey(pub) - require.NoError(t, err) + peerID, priv, _ := random.Identity() ec := stischema.EntryChunk{ - Entries: test.RandomMultihashes(10), + Entries: random.Multihashes(10), } node, err := ec.ToNode() @@ -87,13 +83,10 @@ func TestSignShouldFailIfAdHasExtendedProviders(t *testing.T) { lsys.SetReadStorage(store) lsys.SetWriteStorage(store) - priv, pub, err := p2ptest.RandTestKeyPair(crypto.Ed25519, 256) - require.NoError(t, err) - peerID, err := peer.IDFromPublicKey(pub) - require.NoError(t, err) + peerID, priv, _ := random.Identity() ec := stischema.EntryChunk{ - Entries: test.RandomMultihashes(10), + Entries: random.Multihashes(10), } node, err := ec.ToNode() @@ -101,7 +94,7 @@ func TestSignShouldFailIfAdHasExtendedProviders(t *testing.T) { elnk, err := lsys.Store(ipld.LinkContext{}, stischema.Linkproto, node) require.NoError(t, err) - _, ep1PeerID := generateIdentityAndKey(t) + ep1PeerID, _, _ := random.Identity() adv := stischema.Advertisement{ Provider: peerID.String(), @@ -115,7 +108,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 +125,7 @@ func TestSignWithExtendedProviderAndVerify(t *testing.T) { lsys.SetWriteStorage(store) ec := stischema.EntryChunk{ - Entries: test.RandomMultihashes(10), + Entries: random.Multihashes(10), } node, err := ec.ToNode() @@ -140,10 +133,10 @@ func TestSignWithExtendedProviderAndVerify(t *testing.T) { elnk, err := lsys.Store(ipld.LinkContext{}, stischema.Linkproto, node) require.NoError(t, err) - ep1Priv, ep1PeerID := generateIdentityAndKey(t) - ep2Priv, ep2PeerID := generateIdentityAndKey(t) - mpPriv, mpPeerID := generateIdentityAndKey(t) - mpAddrs := test.RandomAddrs(2) + ep1PeerID, ep1Priv, _ := random.Identity() + ep2PeerID, ep2Priv, _ := random.Identity() + mpPeerID, mpPriv, _ := random.Identity() + mpAddrs := random.Addrs(2) adv := stischema.Advertisement{ Provider: mpPeerID.String(), @@ -155,12 +148,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"), }, { @@ -193,7 +186,7 @@ func TestSignWithExtendedProviderAndVerify(t *testing.T) { func TestSigVerificationFailsIfTheAdProviderIdentityIsIncorrect(t *testing.T) { extendedSignatureTest(t, func(adv stischema.Advertisement) { - _, randomID := generateIdentityAndKey(t) + randomID, _, _ := random.Identity() adv.Provider = randomID.String() _, err := adv.VerifySignature() require.Error(t, err) @@ -203,7 +196,7 @@ func TestSigVerificationFailsIfTheAdProviderIdentityIsIncorrect(t *testing.T) { func TestSigVerificationFailsIfTheExtendedProviderIdentityIsIncorrect(t *testing.T) { extendedSignatureTest(t, func(adv stischema.Advertisement) { // main provider is the first one on the list - _, randomID := generateIdentityAndKey(t) + randomID, _, _ := random.Identity() adv.ExtendedProvider.Providers[1].ID = randomID.String() _, err := adv.VerifySignature() require.Error(t, err) @@ -223,7 +216,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 +255,7 @@ func TestSignFailsIfMainProviderIsNotInExtendedList(t *testing.T) { lsys.SetWriteStorage(store) ec := stischema.EntryChunk{ - Entries: test.RandomMultihashes(10), + Entries: random.Multihashes(10), } node, err := ec.ToNode() @@ -270,9 +263,9 @@ func TestSignFailsIfMainProviderIsNotInExtendedList(t *testing.T) { elnk, err := lsys.Store(ipld.LinkContext{}, stischema.Linkproto, node) require.NoError(t, err) - ep1Priv, ep1PeerID := generateIdentityAndKey(t) - mpPriv, mpPeerID := generateIdentityAndKey(t) - mpAddrs := test.RandomAddrs(2) + ep1PeerID, ep1Priv, _ := random.Identity() + mpPeerID, mpPriv, _ := random.Identity() + mpAddrs := random.Addrs(2) adv := stischema.Advertisement{ Provider: mpPeerID.String(), @@ -284,7 +277,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 +303,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() @@ -318,9 +311,9 @@ func extendedSignatureTest(t *testing.T, testFunc func(adv stischema.Advertiseme elnk, err := lsys.Store(ipld.LinkContext{}, stischema.Linkproto, node) require.NoError(t, err) - ep1Priv, ep1PeerID := generateIdentityAndKey(t) - mpPriv, mpPeerID := generateIdentityAndKey(t) - mpAddrs := test.RandomAddrs(2) + ep1PeerID, ep1Priv, _ := random.Identity() + mpPeerID, mpPriv, _ := random.Identity() + mpAddrs := random.Addrs(2) adv := stischema.Advertisement{ Provider: mpPeerID.String(), @@ -337,7 +330,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"), }, }, @@ -360,11 +353,3 @@ func extendedSignatureTest(t *testing.T, testFunc func(adv stischema.Advertiseme testFunc(adv) } - -func generateIdentityAndKey(t *testing.T) (crypto.PrivKey, peer.ID) { - priv, pub, err := p2ptest.RandTestKeyPair(crypto.Ed25519, 256) - require.NoError(t, err) - peerID, err := peer.IDFromPublicKey(pub) - require.NoError(t, err) - return priv, peerID -} diff --git a/ingest/schema/types_test.go b/ingest/schema/types_test.go index 3e6ced9..3ff41cf 100644 --- a/ingest/schema/types_test.go +++ b/ingest/schema/types_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/ipfs/go-cid" + "github.com/ipfs/go-test/random" "github.com/ipld/go-ipld-prime" _ "github.com/ipld/go-ipld-prime/codec/dagjson" cidlink "github.com/ipld/go-ipld-prime/linking/cid" @@ -14,7 +15,6 @@ 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/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..c9f60a9 100644 --- a/metadata/graphsync_filecoinv1_test.go +++ b/metadata/graphsync_filecoinv1_test.go @@ -3,15 +3,15 @@ package metadata_test import ( "testing" + "github.com/ipfs/go-test/random" "github.com/ipni/go-libipni/metadata" - "github.com/ipni/go-libipni/test" "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..36721bf 100644 --- a/metadata/metadata_test.go +++ b/metadata/metadata_test.go @@ -4,15 +4,15 @@ import ( "math/rand" "testing" + "github.com/ipfs/go-test/random" "github.com/ipni/go-libipni/metadata" - "github.com/ipni/go-libipni/test" "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 -}