convert_test.go 3.05 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1
package net
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
2 3 4 5

import (
	"net"
	"testing"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6 7

	ma "github.com/jbenet/go-multiaddr"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
8 9
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10
type GenFunc func() (ma.Multiaddr, error)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11 12 13 14 15 16 17

func testConvert(t *testing.T, s string, gen GenFunc) {
	m, err := gen()
	if err != nil {
		t.Fatal("failed to generate.")
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
18
	if s2 := m.String(); err != nil || s2 != s {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
19 20 21 22
		t.Fatal("failed to convert: " + s + " != " + s2)
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
23
func testToNetAddr(t *testing.T, maddr, ntwk, addr string) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
24
	m, err := ma.NewMultiaddr(maddr)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
25 26 27 28 29 30 31 32 33 34 35 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
	if err != nil {
		t.Fatal("failed to generate.")
	}

	naddr, err := ToNetAddr(m)
	if addr == "" { // should fail
		if err == nil {
			t.Fatalf("failed to error: %s", m)
		}
		return
	}

	// shouldn't fail
	if err != nil {
		t.Fatalf("failed to convert to net addr: %s", m)
	}

	if naddr.String() != addr {
		t.Fatalf("naddr.Address() == %s != %s", naddr, addr)
	}

	if naddr.Network() != ntwk {
		t.Fatalf("naddr.Network() == %s != %s", naddr.Network(), ntwk)
	}

	// should convert properly
	switch ntwk {
	case "tcp":
		_ = naddr.(*net.TCPAddr)
	case "udp":
		_ = naddr.(*net.UDPAddr)
	case "ip":
		_ = naddr.(*net.IPAddr)
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
61
func TestFromIP4(t *testing.T) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
62
	testConvert(t, "/ip4/10.20.30.40", func() (ma.Multiaddr, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
63 64 65 66 67
		return FromIP(net.ParseIP("10.20.30.40"))
	})
}

func TestFromIP6(t *testing.T) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
68
	testConvert(t, "/ip6/2001:4860:0:2001::68", func() (ma.Multiaddr, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
69 70 71 72 73
		return FromIP(net.ParseIP("2001:4860:0:2001::68"))
	})
}

func TestFromTCP(t *testing.T) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
74
	testConvert(t, "/ip4/10.20.30.40/tcp/1234", func() (ma.Multiaddr, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
75 76 77 78 79 80 81 82
		return FromNetAddr(&net.TCPAddr{
			IP:   net.ParseIP("10.20.30.40"),
			Port: 1234,
		})
	})
}

func TestFromUDP(t *testing.T) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83
	testConvert(t, "/ip4/10.20.30.40/udp/1234", func() (ma.Multiaddr, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
84 85 86 87 88 89
		return FromNetAddr(&net.UDPAddr{
			IP:   net.ParseIP("10.20.30.40"),
			Port: 1234,
		})
	})
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
func TestThinWaist(t *testing.T) {
	addrs := map[string]bool{
		"/ip4/127.0.0.1/udp/1234":              true,
		"/ip4/127.0.0.1/tcp/1234":              true,
		"/ip4/127.0.0.1/udp/1234/tcp/1234":     true,
		"/ip4/127.0.0.1/tcp/12345/ip4/1.2.3.4": true,
		"/ip6/::1/tcp/80":                      true,
		"/ip6/::1/udp/80":                      true,
		"/ip6/::1":                             true,
		"/tcp/1234/ip4/1.2.3.4":                false,
		"/tcp/1234":                            false,
		"/tcp/1234/udp/1234":                   false,
		"/ip4/1.2.3.4/ip4/2.3.4.5":             true,
		"/ip6/::1/ip4/2.3.4.5":                 true,
	}

	for a, res := range addrs {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
108
		m, err := ma.NewMultiaddr(a)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
109 110 111 112
		if err != nil {
			t.Fatalf("failed to construct Multiaddr: %s", a)
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
113
		if IsThinWaist(m) != res {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
114 115 116 117 118
			t.Fatalf("IsThinWaist(%s) != %v", a, res)
		}
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
119
func TestDialArgs(t *testing.T) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
120
	m, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/1234")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	if err != nil {
		t.Fatal("failed to construct", "/ip4/127.0.0.1/udp/1234")
	}

	nw, host, err := DialArgs(m)
	if err != nil {
		t.Fatal("failed to get dial args", "/ip4/127.0.0.1/udp/1234", err)
	}

	if nw != "udp" {
		t.Error("failed to get udp network Dial Arg")
	}

	if host != "127.0.0.1:1234" {
		t.Error("failed to get host:port Dial Arg")
	}
}