libp2p_test.go 613 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package libp2p

import (
	"context"
	"fmt"
	"testing"

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

func TestNewHost(t *testing.T) {
	_, err := makeRandomHost(t, 9000)
	if err != nil {
		t.Fatal(err)
	}
}

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),
		Muxer(DefaultMuxer()),
		NATPortMap(),
	}

	return New(ctx, opts...)
}