tcp_test.go 1.16 KB
Newer Older
1 2 3 4 5
package tcp

import (
	"testing"

6 7
	insecure "github.com/libp2p/go-conn-security/insecure"
	tptu "github.com/libp2p/go-libp2p-transport-upgrader"
8
	utils "github.com/libp2p/go-libp2p-transport/test"
Jeromy's avatar
Jeromy committed
9
	ma "github.com/multiformats/go-multiaddr"
10
	mplex "github.com/whyrusleeping/go-smux-multiplex"
11 12 13
)

func TestTcpTransport(t *testing.T) {
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
	for i := 0; i < 2; i++ {
		ta := NewTCPTransport(&tptu.Upgrader{
			Secure: insecure.New("peerA"),
			Muxer:  new(mplex.Transport),
		})
		tb := NewTCPTransport(&tptu.Upgrader{
			Secure: insecure.New("peerB"),
			Muxer:  new(mplex.Transport),
		})

		zero := "/ip4/127.0.0.1/tcp/0"
		utils.SubtestTransport(t, ta, tb, zero, "peerA")

		envReuseportVal = false
	}
	envReuseportVal = true
30 31 32
}

func TestTcpTransportCantListenUtp(t *testing.T) {
33 34 35 36 37
	for i := 0; i < 2; i++ {
		utpa, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/0/utp")
		if err != nil {
			t.Fatal(err)
		}
38

39 40 41 42
		tpt := NewTCPTransport(&tptu.Upgrader{
			Secure: insecure.New("peerB"),
			Muxer:  new(mplex.Transport),
		})
Jeromy's avatar
Jeromy committed
43

44 45 46 47
		_, err = tpt.Listen(utpa)
		if err == nil {
			t.Fatal("shouldnt be able to listen on utp addr with tcp transport")
		}
Jeromy's avatar
Jeromy committed
48

49
		envReuseportVal = false
Jeromy's avatar
Jeromy committed
50
	}
51
	envReuseportVal = true
Jeromy's avatar
Jeromy committed
52
}