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"
|
|
|
|
|
2020-05-20 17:37:51 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/connmgr"
|
2019-05-26 21:55:46 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/crypto"
|
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2020-05-20 17:37:51 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/transport"
|
2018-07-25 19:38:20 +00:00
|
|
|
"github.com/libp2p/go-tcp-transport"
|
2020-05-20 17:37:51 +00:00
|
|
|
|
|
|
|
tptu "github.com/libp2p/go-libp2p-transport-upgrader"
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:37:51 +00:00
|
|
|
func TestTransportConstructor(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
ctor := func(
|
|
|
|
h host.Host,
|
|
|
|
_ connmgr.ConnectionGater,
|
|
|
|
upgrader *tptu.Upgrader,
|
|
|
|
) transport.Transport {
|
|
|
|
return tcp.NewTCPTransport(upgrader)
|
|
|
|
}
|
|
|
|
h, err := New(ctx, Transport(ctor))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
h.Close()
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
2019-05-26 21:55:46 +00:00
|
|
|
err = a.Connect(ctx, peer.AddrInfo{
|
2018-08-16 20:20:17 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-03-24 23:48:50 +00:00
|
|
|
func TestAutoNATService(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
h, err := New(ctx, EnableNATService())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
h.Close()
|
|
|
|
}
|
|
|
|
|
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-09-28 08:30:45 +00:00
|
|
|
re2 := regexp.MustCompile("/p2p-circuit")
|
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() {
|
2018-09-28 08:30:45 +00:00
|
|
|
if re.FindStringSubmatchIndex(addr.String()) == nil &&
|
|
|
|
re2.FindStringSubmatchIndex(addr.String()) == nil {
|
|
|
|
t.Error("expected ip4 or ip6 or relay interface")
|
2018-07-27 20:38:45 +00:00
|
|
|
}
|
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()
|
|
|
|
|
2018-09-28 08:30:45 +00:00
|
|
|
// Test 2: Listen addr only include relay if user defined transport is passed.
|
2018-07-26 20:56:29 +00:00
|
|
|
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-09-28 08:30:45 +00:00
|
|
|
if len(h.Network().ListenAddresses()) != 1 {
|
|
|
|
t.Error("expected one listen addr with user defined transport")
|
|
|
|
}
|
|
|
|
if re2.FindStringSubmatchIndex(h.Network().ListenAddresses()[0].String()) == nil {
|
|
|
|
t.Error("expected relay address")
|
2018-07-25 19:38:20 +00:00
|
|
|
}
|
|
|
|
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...)
|
|
|
|
}
|
2019-04-17 23:20:02 +00:00
|
|
|
|
|
|
|
func TestChainOptions(t *testing.T) {
|
|
|
|
var cfg Config
|
|
|
|
var optsRun []int
|
|
|
|
optcount := 0
|
|
|
|
newOpt := func() Option {
|
|
|
|
index := optcount
|
|
|
|
optcount++
|
|
|
|
return func(c *Config) error {
|
|
|
|
optsRun = append(optsRun, index)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2019-04-18 01:05:08 +00:00
|
|
|
|
2019-04-17 23:20:02 +00:00
|
|
|
if err := cfg.Apply(newOpt(), nil, ChainOptions(newOpt(), newOpt(), ChainOptions(), ChainOptions(nil, newOpt()))); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-04-18 01:05:08 +00:00
|
|
|
|
|
|
|
// Make sure we ran all options.
|
|
|
|
if optcount != 4 {
|
2019-04-17 23:20:02 +00:00
|
|
|
t.Errorf("expected to have handled %d options, handled %d", optcount, len(optsRun))
|
|
|
|
}
|
2019-04-18 01:05:08 +00:00
|
|
|
|
|
|
|
// Make sure we ran the options in-order.
|
2019-04-17 23:20:02 +00:00
|
|
|
for i, x := range optsRun {
|
|
|
|
if i != x {
|
|
|
|
t.Errorf("expected opt %d, got opt %d", i, x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|