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

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

	crypto "github.com/libp2p/go-libp2p-crypto"
	host "github.com/libp2p/go-libp2p-host"
)

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

func TestInsecure(t *testing.T) {
	ctx := context.Background()
	h, err := New(ctx, NoSecurity)
	if err != nil {
		t.Fatal(err)
	}
	h.Close()
40
41
42
43
44
45
46
47
48
49
50
51
}

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
52
53
54
		DefaultTransports,
		DefaultMuxers,
		DefaultSecurity,
55
56
57
58
59
		NATPortMap(),
	}

	return New(ctx, opts...)
}