libp2p_test.go 2.82 KB
Newer Older
1
2
3
4
5
package libp2p

import (
	"context"
	"fmt"
6
	"regexp"
Steven Allen's avatar
Steven Allen committed
7
	"strings"
8
9
10
11
	"testing"

	crypto "github.com/libp2p/go-libp2p-crypto"
	host "github.com/libp2p/go-libp2p-host"
12
	pstore "github.com/libp2p/go-libp2p-peerstore"
13
	"github.com/libp2p/go-tcp-transport"
14
15
16
)

func TestNewHost(t *testing.T) {
Steven Allen's avatar
Steven Allen committed
17
	h, err := makeRandomHost(t, 9000)
18
19
20
	if err != nil {
		t.Fatal(err)
	}
Steven Allen's avatar
Steven Allen committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
	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")
	}
}

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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")
	}
}

Steven Allen's avatar
Steven Allen committed
71
72
73
74
75
76
77
func TestInsecure(t *testing.T) {
	ctx := context.Background()
	h, err := New(ctx, NoSecurity)
	if err != nil {
		t.Fatal(err)
	}
	h.Close()
78
79
}

80
81
82
func TestDefaultListenAddrs(t *testing.T) {
	ctx := context.Background()

Abhishek Upperwal's avatar
Abhishek Upperwal committed
83
	re := regexp.MustCompile("/(ip)[4|6]/((0.0.0.0)|(::))/tcp/")
vyzo's avatar
vyzo committed
84
	re2 := regexp.MustCompile("/p2p-circuit")
85

86
87
88
89
90
	// Test 1: Setting the correct listen addresses if userDefined.Transport == nil && userDefined.ListenAddrs == nil
	h, err := New(ctx)
	if err != nil {
		t.Fatal(err)
	}
91
	for _, addr := range h.Network().ListenAddresses() {
vyzo's avatar
vyzo committed
92
93
94
		if re.FindStringSubmatchIndex(addr.String()) == nil &&
			re2.FindStringSubmatchIndex(addr.String()) == nil {
			t.Error("expected ip4 or ip6 or relay interface")
95
		}
96
	}
97

98
99
	h.Close()

vyzo's avatar
vyzo committed
100
	// Test 2: Listen addr only include relay if user defined transport is passed.
101
	h, err = New(
102
		ctx,
Abhishek Upperwal's avatar
Abhishek Upperwal committed
103
		Transport(tcp.NewTCPTransport),
104
105
106
107
108
	)
	if err != nil {
		t.Fatal(err)
	}

vyzo's avatar
vyzo committed
109
110
111
112
113
	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")
114
115
116
117
	}
	h.Close()
}

118
119
120
121
122
123
124
125
126
127
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),
Steven Allen's avatar
Steven Allen committed
128
129
130
		DefaultTransports,
		DefaultMuxers,
		DefaultSecurity,
131
132
133
134
135
		NATPortMap(),
	}

	return New(ctx, opts...)
}