Merge pull request #155 from libp2p/deps/extract-netutil

Extract netutil to its own package
This commit is contained in:
Jeromy Johnson 2016-11-17 12:09:37 -08:00 committed by GitHub
commit 6f7efeabfe
11 changed files with 79 additions and 258 deletions

View File

@ -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 {

View File

@ -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) {

View File

@ -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"

View File

@ -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())

View File

@ -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(),

View File

@ -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()

View File

@ -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)

View File

@ -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)

View File

@ -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
}

View File

@ -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

View File

@ -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"
}