2018-03-04 05:35:18 +00:00
|
|
|
package libp2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2018-07-27 20:38:45 +00:00
|
|
|
"regexp"
|
2018-03-10 01:56:02 +00:00
|
|
|
"strings"
|
2018-03-04 05:35:18 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
crypto "github.com/libp2p/go-libp2p-crypto"
|
|
|
|
host "github.com/libp2p/go-libp2p-host"
|
2018-08-16 20:20:17 +00:00
|
|
|
pstore "github.com/libp2p/go-libp2p-peerstore"
|
2018-07-25 19:38:20 +00:00
|
|
|
"github.com/libp2p/go-tcp-transport"
|
2018-03-04 05:35:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewHost(t *testing.T) {
|
2018-03-10 01:56:02 +00:00
|
|
|
h, err := makeRandomHost(t, 9000)
|
2018-03-04 05:35:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-03-10 01:56:02 +00:00
|
|
|
h.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBadTransportConstructor(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
h, err := New(ctx, Transport(func() {}))
|
|
|
|
if err == nil {
|
|
|
|
h.Close()
|
|
|
|
t.Fatal("expected an error")
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "libp2p_test.go") {
|
|
|
|
t.Error("expected error to contain debugging info")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 20:20:17 +00:00
|
|
|
func TestNoListenAddrs(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
h, err := New(ctx, NoListenAddrs)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer h.Close()
|
|
|
|
if len(h.Addrs()) != 0 {
|
|
|
|
t.Fatal("expected no addresses")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoTransports(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
a, err := New(ctx, NoTransports)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer a.Close()
|
|
|
|
|
|
|
|
b, err := New(ctx, ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer b.Close()
|
|
|
|
|
|
|
|
err = a.Connect(ctx, pstore.PeerInfo{
|
|
|
|
ID: b.ID(),
|
|
|
|
Addrs: b.Addrs(),
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
t.Error("dial should have failed as no transports have been configured")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-10 01:56:02 +00:00
|
|
|
func TestInsecure(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
h, err := New(ctx, NoSecurity)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
h.Close()
|
2018-03-04 05:35:18 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 19:38:20 +00:00
|
|
|
func TestDefaultListenAddrs(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-07-27 21:18:09 +00:00
|
|
|
re := regexp.MustCompile("/(ip)[4|6]/((0.0.0.0)|(::))/tcp/")
|
2018-07-27 20:38:45 +00:00
|
|
|
|
2018-07-26 20:56:29 +00:00
|
|
|
// Test 1: Setting the correct listen addresses if userDefined.Transport == nil && userDefined.ListenAddrs == nil
|
|
|
|
h, err := New(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-07-27 20:38:45 +00:00
|
|
|
for _, addr := range h.Network().ListenAddresses() {
|
|
|
|
if re.FindStringSubmatchIndex(addr.String()) == nil {
|
|
|
|
t.Error("expected ip4 or ip6 interface")
|
|
|
|
}
|
2018-07-26 20:56:29 +00:00
|
|
|
}
|
2018-07-27 20:38:45 +00:00
|
|
|
|
2018-07-26 20:56:29 +00:00
|
|
|
h.Close()
|
|
|
|
|
|
|
|
// Test 2: Listen addr should not set if user defined transport is passed.
|
|
|
|
h, err = New(
|
2018-07-25 19:38:20 +00:00
|
|
|
ctx,
|
2018-07-25 19:44:09 +00:00
|
|
|
Transport(tcp.NewTCPTransport),
|
2018-07-25 19:38:20 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-07-27 20:38:45 +00:00
|
|
|
if len(h.Network().ListenAddresses()) != 0 {
|
2018-07-25 19:38:20 +00:00
|
|
|
t.Error("expected zero listen addrs as none is set with user defined transport")
|
|
|
|
}
|
|
|
|
h.Close()
|
|
|
|
}
|
|
|
|
|
2018-03-04 05:35:18 +00:00
|
|
|
func makeRandomHost(t *testing.T, port int) (host.Host, error) {
|
|
|
|
ctx := context.Background()
|
|
|
|
priv, _, err := crypto.GenerateKeyPair(crypto.RSA, 2048)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := []Option{
|
|
|
|
ListenAddrStrings(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port)),
|
|
|
|
Identity(priv),
|
2018-03-10 01:56:02 +00:00
|
|
|
DefaultTransports,
|
|
|
|
DefaultMuxers,
|
|
|
|
DefaultSecurity,
|
2018-03-04 05:35:18 +00:00
|
|
|
NATPortMap(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return New(ctx, opts...)
|
|
|
|
}
|