diff --git a/p2p/discovery/mdns_test.go b/p2p/discovery/mdns_test.go index 8c5250048e2723f1157fccd1e4c862135a04b894..ea2633ebde4e074b71a2ab757610350da3665586 100644 --- a/p2p/discovery/mdns_test.go +++ b/p2p/discovery/mdns_test.go @@ -5,8 +5,10 @@ import ( "testing" "time" + bhost "github.com/libp2p/go-libp2p/p2p/host/basic" + host "github.com/libp2p/go-libp2p-host" - netutil "github.com/libp2p/go-libp2p/p2p/test/util" + netutil "github.com/libp2p/go-libp2p-netutil" pstore "github.com/libp2p/go-libp2p-peerstore" ) @@ -23,8 +25,8 @@ func TestMdnsDiscovery(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - a := netutil.GenHostSwarm(t, ctx) - b := netutil.GenHostSwarm(t, ctx) + a := bhost.New(netutil.GenSwarmNetwork(t, ctx)) + b := bhost.New(netutil.GenSwarmNetwork(t, ctx)) sa, err := NewMdnsService(ctx, a, time.Second) if err != nil { diff --git a/p2p/host/basic/basic_host_test.go b/p2p/host/basic/basic_host_test.go index da12989f00e95ad36c6ebbb2a194145922f44da7..0c7dea29fefd06ebd022420c983266cb143c2bdc 100644 --- a/p2p/host/basic/basic_host_test.go +++ b/p2p/host/basic/basic_host_test.go @@ -1,4 +1,4 @@ -package basichost_test +package basichost import ( "bytes" @@ -9,15 +9,15 @@ import ( host "github.com/libp2p/go-libp2p-host" inet "github.com/libp2p/go-libp2p-net" + testutil "github.com/libp2p/go-libp2p-netutil" protocol "github.com/libp2p/go-libp2p-protocol" - testutil "github.com/libp2p/go-libp2p/p2p/test/util" ) func TestHostSimple(t *testing.T) { ctx := context.Background() - h1 := testutil.GenHostSwarm(t, ctx) - h2 := testutil.GenHostSwarm(t, ctx) + h1 := New(testutil.GenSwarmNetwork(t, ctx)) + h2 := New(testutil.GenSwarmNetwork(t, ctx)) defer h1.Close() defer h2.Close() @@ -64,8 +64,8 @@ func TestHostSimple(t *testing.T) { } func getHostPair(ctx context.Context, t *testing.T) (host.Host, host.Host) { - h1 := testutil.GenHostSwarm(t, ctx) - h2 := testutil.GenHostSwarm(t, ctx) + h1 := New(testutil.GenSwarmNetwork(t, ctx)) + h2 := New(testutil.GenSwarmNetwork(t, ctx)) h2pi := h2.Peerstore().PeerInfo(h2.ID()) if err := h1.Connect(ctx, h2pi); err != nil { @@ -170,8 +170,8 @@ func TestHostProtoPreknowledge(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - h1 := testutil.GenHostSwarm(t, ctx) - h2 := testutil.GenHostSwarm(t, ctx) + h1 := New(testutil.GenSwarmNetwork(t, ctx)) + h2 := New(testutil.GenSwarmNetwork(t, ctx)) conn := make(chan protocol.ID, 16) handler := func(s inet.Stream) { diff --git a/p2p/net/mock/mock_net.go b/p2p/net/mock/mock_net.go index 0a20601788c461708cd9db0c9173ac459a09a38c..fb12a61a56582ac4ede5b84c93462663a88a969e 100644 --- a/p2p/net/mock/mock_net.go +++ b/p2p/net/mock/mock_net.go @@ -8,12 +8,12 @@ import ( host "github.com/libp2p/go-libp2p-host" bhost "github.com/libp2p/go-libp2p/p2p/host/basic" - p2putil "github.com/libp2p/go-libp2p/p2p/test/util" "github.com/jbenet/goprocess" goprocessctx "github.com/jbenet/goprocess/context" ic "github.com/libp2p/go-libp2p-crypto" inet "github.com/libp2p/go-libp2p-net" + p2putil "github.com/libp2p/go-libp2p-netutil" peer "github.com/libp2p/go-libp2p-peer" pstore "github.com/libp2p/go-libp2p-peerstore" testutil "github.com/libp2p/go-testutil" diff --git a/p2p/protocol/identify/id_test.go b/p2p/protocol/identify/id_test.go index a7a87bc85773e9922015297eb6c20b2159ba57af..d46340bf10071f82002a67694d3335b6fafc24a8 100644 --- a/p2p/protocol/identify/id_test.go +++ b/p2p/protocol/identify/id_test.go @@ -6,24 +6,27 @@ import ( "time" ic "github.com/libp2p/go-libp2p-crypto" + testutil "github.com/libp2p/go-libp2p-netutil" peer "github.com/libp2p/go-libp2p-peer" identify "github.com/libp2p/go-libp2p/p2p/protocol/identify" - testutil "github.com/libp2p/go-libp2p/p2p/test/util" + blhost "github.com/libp2p/go-libp2p-blankhost" host "github.com/libp2p/go-libp2p-host" ma "github.com/multiformats/go-multiaddr" ) func subtestIDService(t *testing.T, postDialWait time.Duration) { - // the generated networks should have the id service wired in. ctx := context.Background() - h1 := testutil.GenHostSwarm(t, ctx) - h2 := testutil.GenHostSwarm(t, ctx) + h1 := blhost.NewBlankHost(testutil.GenSwarmNetwork(t, ctx)) + h2 := blhost.NewBlankHost(testutil.GenSwarmNetwork(t, ctx)) h1p := h1.ID() h2p := h2.ID() + ids1 := identify.NewIDService(h1) + ids2 := identify.NewIDService(h2) + testKnowsAddrs(t, h1, h2p, []ma.Multiaddr{}) // nothing testKnowsAddrs(t, h2, h1p, []ma.Multiaddr{}) // nothing @@ -32,11 +35,13 @@ func subtestIDService(t *testing.T, postDialWait time.Duration) { t.Fatal(err) } - // we need to wait here if Dial returns before ID service is finished. - if postDialWait > 0 { - <-time.After(postDialWait) + h1t2c := h1.Network().ConnsToPeer(h2p) + if len(h1t2c) == 0 { + t.Fatal("should have a conn here") } + ids1.IdentifyConn(h1t2c[0]) + // the IDService should be opened automatically, by the network. // what we should see now is that both peers know about each others listen addresses. t.Log("test peer1 has peer2 addrs correctly") @@ -50,7 +55,7 @@ func subtestIDService(t *testing.T, postDialWait time.Duration) { if len(c) < 1 { t.Fatal("should have connection by now at least.") } - <-h2.IDService().IdentifyWait(c[0]) + ids2.IdentifyConn(c[0]) addrs := h1.Peerstore().Addrs(h1p) addrs = append(addrs, c[0].RemoteMultiaddr()) diff --git a/p2p/protocol/ping/ping_test.go b/p2p/protocol/ping/ping_test.go index 1c420ca7ef5955ff45d75d3a77975a17fc6d419d..a260cb81d45caf411505c9e32ccd341a0063af15 100644 --- a/p2p/protocol/ping/ping_test.go +++ b/p2p/protocol/ping/ping_test.go @@ -1,21 +1,21 @@ package ping import ( + "context" "testing" "time" - netutil "github.com/libp2p/go-libp2p/p2p/test/util" - - "context" + netutil "github.com/libp2p/go-libp2p-netutil" peer "github.com/libp2p/go-libp2p-peer" pstore "github.com/libp2p/go-libp2p-peerstore" + bhost "github.com/libp2p/go-libp2p/p2p/host/basic" ) func TestPing(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - h1 := netutil.GenHostSwarm(t, ctx) - h2 := netutil.GenHostSwarm(t, ctx) + h1 := bhost.New(netutil.GenSwarmNetwork(t, ctx)) + h2 := bhost.New(netutil.GenSwarmNetwork(t, ctx)) err := h1.Connect(ctx, pstore.PeerInfo{ ID: h2.ID(), diff --git a/p2p/protocol/relay/relay_test.go b/p2p/protocol/relay/relay_test.go index fbc1fc104d04d87c4b7a750c585b6746b98b677c..2c24d3f7d09deeaa49a6dea7fa26184e16373bd6 100644 --- a/p2p/protocol/relay/relay_test.go +++ b/p2p/protocol/relay/relay_test.go @@ -1,15 +1,17 @@ package relay_test import ( + "context" "io" "testing" - "context" + bhost "github.com/libp2p/go-libp2p/p2p/host/basic" + relay "github.com/libp2p/go-libp2p/p2p/protocol/relay" + logging "github.com/ipfs/go-log" inet "github.com/libp2p/go-libp2p-net" + testutil "github.com/libp2p/go-libp2p-netutil" protocol "github.com/libp2p/go-libp2p-protocol" - relay "github.com/libp2p/go-libp2p/p2p/protocol/relay" - testutil "github.com/libp2p/go-libp2p/p2p/test/util" msmux "github.com/whyrusleeping/go-multistream" ) @@ -20,9 +22,9 @@ func TestRelaySimple(t *testing.T) { ctx := context.Background() // these networks have the relay service wired in already. - n1 := testutil.GenHostSwarm(t, ctx) - n2 := testutil.GenHostSwarm(t, ctx) - n3 := testutil.GenHostSwarm(t, ctx) + n1 := bhost.New(testutil.GenSwarmNetwork(t, ctx)) + n2 := bhost.New(testutil.GenSwarmNetwork(t, ctx)) + n3 := bhost.New(testutil.GenSwarmNetwork(t, ctx)) n1p := n1.ID() n2p := n2.ID() @@ -103,11 +105,11 @@ func TestRelayAcrossFour(t *testing.T) { ctx := context.Background() // these networks have the relay service wired in already. - n1 := testutil.GenHostSwarm(t, ctx) - n2 := testutil.GenHostSwarm(t, ctx) - n3 := testutil.GenHostSwarm(t, ctx) - n4 := testutil.GenHostSwarm(t, ctx) - n5 := testutil.GenHostSwarm(t, ctx) + n1 := bhost.New(testutil.GenSwarmNetwork(t, ctx)) + n2 := bhost.New(testutil.GenSwarmNetwork(t, ctx)) + n3 := bhost.New(testutil.GenSwarmNetwork(t, ctx)) + n4 := bhost.New(testutil.GenSwarmNetwork(t, ctx)) + n5 := bhost.New(testutil.GenSwarmNetwork(t, ctx)) n1p := n1.ID() n2p := n2.ID() @@ -215,9 +217,9 @@ func TestRelayStress(t *testing.T) { ctx := context.Background() // these networks have the relay service wired in already. - n1 := testutil.GenHostSwarm(t, ctx) - n2 := testutil.GenHostSwarm(t, ctx) - n3 := testutil.GenHostSwarm(t, ctx) + n1 := bhost.New(testutil.GenSwarmNetwork(t, ctx)) + n2 := bhost.New(testutil.GenSwarmNetwork(t, ctx)) + n3 := bhost.New(testutil.GenSwarmNetwork(t, ctx)) n1p := n1.ID() n2p := n2.ID() diff --git a/p2p/test/backpressure/backpressure_test.go b/p2p/test/backpressure/backpressure_test.go index c23944c2fda48587c14331e23e17d4fb1d85cfa3..163c7051284f5e11f372575f3e96868a61478d40 100644 --- a/p2p/test/backpressure/backpressure_test.go +++ b/p2p/test/backpressure/backpressure_test.go @@ -1,19 +1,21 @@ package backpressure_tests import ( + "context" "io" "math/rand" "testing" "time" - "context" + bhost "github.com/libp2p/go-libp2p/p2p/host/basic" + u "github.com/ipfs/go-ipfs-util" logging "github.com/ipfs/go-log" host "github.com/libp2p/go-libp2p-host" inet "github.com/libp2p/go-libp2p-net" + testutil "github.com/libp2p/go-libp2p-netutil" peer "github.com/libp2p/go-libp2p-peer" protocol "github.com/libp2p/go-libp2p-protocol" - testutil "github.com/libp2p/go-libp2p/p2p/test/util" ) var log = logging.Logger("backpressure") @@ -135,8 +137,8 @@ a problem. // ok that's enough setup. let's do it! ctx := context.Background() - h1 := testutil.GenHostSwarm(t, ctx) - h2 := testutil.GenHostSwarm(t, ctx) + h1 := bhost.New(testutil.GenSwarmNetwork(t, ctx)) + h2 := bhost.New(testutil.GenSwarmNetwork(t, ctx)) // setup receiver handler h1.SetStreamHandler(protocol.TestingID, receiver) @@ -272,8 +274,8 @@ func TestStBackpressureStreamWrite(t *testing.T) { // setup the networks ctx := context.Background() - h1 := testutil.GenHostSwarm(t, ctx) - h2 := testutil.GenHostSwarm(t, ctx) + h1 := bhost.New(testutil.GenSwarmNetwork(t, ctx)) + h2 := bhost.New(testutil.GenSwarmNetwork(t, ctx)) // setup sender handler on 1 h1.SetStreamHandler(protocol.TestingID, sender) diff --git a/p2p/test/reconnects/reconnect_test.go b/p2p/test/reconnects/reconnect_test.go index ed3ed9710093bc4c6a03ad1d5f7f5c6207d4d5fa..a79ac71e7e5babbb677898284b615cb0103b2772 100644 --- a/p2p/test/reconnects/reconnect_test.go +++ b/p2p/test/reconnects/reconnect_test.go @@ -8,13 +8,15 @@ import ( "testing" "time" + bhost "github.com/libp2p/go-libp2p/p2p/host/basic" + u "github.com/ipfs/go-ipfs-util" logging "github.com/ipfs/go-log" host "github.com/libp2p/go-libp2p-host" inet "github.com/libp2p/go-libp2p-net" + testutil "github.com/libp2p/go-libp2p-netutil" protocol "github.com/libp2p/go-libp2p-protocol" swarm "github.com/libp2p/go-libp2p-swarm" - testutil "github.com/libp2p/go-libp2p/p2p/test/util" ps "github.com/libp2p/go-peerstream" ) @@ -103,8 +105,8 @@ func newSender() (chan sendChans, func(s inet.Stream)) { // TestReconnect tests whether hosts are able to disconnect and reconnect. func TestReconnect2(t *testing.T) { ctx := context.Background() - h1 := testutil.GenHostSwarm(t, ctx) - h2 := testutil.GenHostSwarm(t, ctx) + h1 := bhost.New(testutil.GenSwarmNetwork(t, ctx)) + h2 := bhost.New(testutil.GenSwarmNetwork(t, ctx)) hosts := []host.Host{h1, h2} h1.SetStreamHandler(protocol.TestingID, EchoStreamHandler) @@ -123,11 +125,11 @@ func TestReconnect2(t *testing.T) { // TestReconnect tests whether hosts are able to disconnect and reconnect. func TestReconnect5(t *testing.T) { ctx := context.Background() - h1 := testutil.GenHostSwarm(t, ctx) - h2 := testutil.GenHostSwarm(t, ctx) - h3 := testutil.GenHostSwarm(t, ctx) - h4 := testutil.GenHostSwarm(t, ctx) - h5 := testutil.GenHostSwarm(t, ctx) + h1 := bhost.New(testutil.GenSwarmNetwork(t, ctx)) + h2 := bhost.New(testutil.GenSwarmNetwork(t, ctx)) + h3 := bhost.New(testutil.GenSwarmNetwork(t, ctx)) + h4 := bhost.New(testutil.GenSwarmNetwork(t, ctx)) + h5 := bhost.New(testutil.GenSwarmNetwork(t, ctx)) hosts := []host.Host{h1, h2, h3, h4, h5} h1.SetStreamHandler(protocol.TestingID, EchoStreamHandler) diff --git a/p2p/test/util/key.go b/p2p/test/util/key.go deleted file mode 100644 index 501d6dd3ae8432690946af27042679d197273cea..0000000000000000000000000000000000000000 --- a/p2p/test/util/key.go +++ /dev/null @@ -1,164 +0,0 @@ -package testutil - -import ( - "bytes" - "io" - "testing" - - u "github.com/ipfs/go-ipfs-util" - logging "github.com/ipfs/go-log" - ic "github.com/libp2p/go-libp2p-crypto" - peer "github.com/libp2p/go-libp2p-peer" - testutil "github.com/libp2p/go-testutil" - - ma "github.com/multiformats/go-multiaddr" -) - -var log = logging.Logger("boguskey") - -// TestBogusPrivateKey is a key used for testing (to avoid expensive keygen) -type TestBogusPrivateKey []byte - -// TestBogusPublicKey is a key used for testing (to avoid expensive keygen) -type TestBogusPublicKey []byte - -func (pk TestBogusPublicKey) Verify(data, sig []byte) (bool, error) { - log.Errorf("TestBogusPublicKey.Verify -- this better be a test!") - return bytes.Equal(data, reverse(sig)), nil -} - -func (pk TestBogusPublicKey) Bytes() ([]byte, error) { - return []byte(pk), nil -} - -func (pk TestBogusPublicKey) Encrypt(b []byte) ([]byte, error) { - log.Errorf("TestBogusPublicKey.Encrypt -- this better be a test!") - return reverse(b), nil -} - -// Equals checks whether this key is equal to another -func (pk TestBogusPublicKey) Equals(k ic.Key) bool { - return ic.KeyEqual(pk, k) -} - -func (pk TestBogusPublicKey) Hash() ([]byte, error) { - return ic.KeyHash(pk) -} - -func (sk TestBogusPrivateKey) GenSecret() []byte { - return []byte(sk) -} - -func (sk TestBogusPrivateKey) Sign(message []byte) ([]byte, error) { - log.Errorf("TestBogusPrivateKey.Sign -- this better be a test!") - return reverse(message), nil -} - -func (sk TestBogusPrivateKey) GetPublic() ic.PubKey { - return TestBogusPublicKey(sk) -} - -func (sk TestBogusPrivateKey) Decrypt(b []byte) ([]byte, error) { - log.Errorf("TestBogusPrivateKey.Decrypt -- this better be a test!") - return reverse(b), nil -} - -func (sk TestBogusPrivateKey) Bytes() ([]byte, error) { - return []byte(sk), nil -} - -// Equals checks whether this key is equal to another -func (sk TestBogusPrivateKey) Equals(k ic.Key) bool { - return ic.KeyEqual(sk, k) -} - -func (sk TestBogusPrivateKey) Hash() ([]byte, error) { - return ic.KeyHash(sk) -} - -func RandTestBogusPrivateKey() (TestBogusPrivateKey, error) { - r := u.NewTimeSeededRand() - k := make([]byte, 5) - if _, err := io.ReadFull(r, k); err != nil { - return nil, err - } - return TestBogusPrivateKey(k), nil -} - -func RandTestBogusPublicKey() (TestBogusPublicKey, error) { - k, err := RandTestBogusPrivateKey() - return TestBogusPublicKey(k), err -} - -func RandTestBogusPrivateKeyOrFatal(t *testing.T) TestBogusPrivateKey { - k, err := RandTestBogusPrivateKey() - if err != nil { - t.Fatal(err) - } - return k -} - -func RandTestBogusPublicKeyOrFatal(t *testing.T) TestBogusPublicKey { - k, err := RandTestBogusPublicKey() - if err != nil { - t.Fatal(err) - } - return k -} - -func RandTestBogusIdentity() (testutil.Identity, error) { - k, err := RandTestBogusPrivateKey() - if err != nil { - return nil, err - } - - id, err := peer.IDFromPrivateKey(k) - if err != nil { - return nil, err - } - - return &identity{ - k: k, - id: id, - a: testutil.RandLocalTCPAddress(), - }, nil -} - -func RandTestBogusIdentityOrFatal(t *testing.T) testutil.Identity { - k, err := RandTestBogusIdentity() - if err != nil { - t.Fatal(err) - } - return k -} - -// identity is a temporary shim to delay binding of PeerNetParams. -type identity struct { - k TestBogusPrivateKey - id peer.ID - a ma.Multiaddr -} - -func (p *identity) ID() peer.ID { - return p.id -} - -func (p *identity) Address() ma.Multiaddr { - return p.a -} - -func (p *identity) PrivateKey() ic.PrivKey { - return p.k -} - -func (p *identity) PublicKey() ic.PubKey { - return p.k.GetPublic() -} - -func reverse(a []byte) []byte { - b := make([]byte, len(a)) - for i := 0; i < len(a); i++ { - b[i] = a[len(a)-1-i] - } - return b -} diff --git a/p2p/test/util/util.go b/p2p/test/util/util.go deleted file mode 100644 index efe3d0d082f51eb02fe925304e9173acce5e8aa2..0000000000000000000000000000000000000000 --- a/p2p/test/util/util.go +++ /dev/null @@ -1,41 +0,0 @@ -package testutil - -import ( - "context" - "testing" - - bhost "github.com/libp2p/go-libp2p/p2p/host/basic" - - metrics "github.com/libp2p/go-libp2p-metrics" - inet "github.com/libp2p/go-libp2p-net" - pstore "github.com/libp2p/go-libp2p-peerstore" - swarm "github.com/libp2p/go-libp2p-swarm" - tu "github.com/libp2p/go-testutil" - ma "github.com/multiformats/go-multiaddr" -) - -func GenSwarmNetwork(t *testing.T, ctx context.Context) *swarm.Network { - p := tu.RandPeerNetParamsOrFatal(t) - ps := pstore.NewPeerstore() - ps.AddPubKey(p.ID, p.PubKey) - ps.AddPrivKey(p.ID, p.PrivKey) - n, err := swarm.NewNetwork(ctx, []ma.Multiaddr{p.Addr}, p.ID, ps, metrics.NewBandwidthCounter()) - if err != nil { - t.Fatal(err) - } - ps.AddAddrs(p.ID, n.ListenAddresses(), pstore.PermanentAddrTTL) - return n -} - -func DivulgeAddresses(a, b inet.Network) { - id := a.LocalPeer() - addrs := a.Peerstore().Addrs(id) - b.Peerstore().AddAddrs(id, addrs, pstore.PermanentAddrTTL) -} - -func GenHostSwarm(t *testing.T, ctx context.Context) *bhost.BasicHost { - n := GenSwarmNetwork(t, ctx) - return bhost.New(n) -} - -var RandPeerID = tu.RandPeerID diff --git a/package.json b/package.json index 467ecd1093dd20b80eea6ed700da41a5350ac2b9..972595723df1db2480ccaaa175aac236963bb204 100644 --- a/package.json +++ b/package.json @@ -226,6 +226,18 @@ "hash": "QmPpncQ3L4bC3rnwLBrgEomygs5RbnFejb68GgsecxbMiL", "name": "go-libp2p-nat", "version": "0.0.0" + }, + { + "author": "whyrusleeping", + "hash": "QmcDTquYLTYirqj71RRWKUWEEw3nJt11Awzun5ep8kfY7W", + "name": "go-libp2p-netutil", + "version": "0.1.0" + }, + { + "author": "whyrusleeping", + "hash": "QmYjDhB1VkuP5ATkqjdnPHfA2huyfcydNMXoGdtEvTNcYL", + "name": "go-libp2p-blankhost", + "version": "0.1.0" } ], "gxVersion": "0.4.0", @@ -235,3 +247,4 @@ "releaseCmd": "git commit -a -m \"gx publish $VERSION\"", "version": "4.1.0" } +