Commit 32c2e408 authored by Steven Allen's avatar Steven Allen

test ipv6

parent 0e2041a9
......@@ -8,10 +8,13 @@ import (
manet "github.com/multiformats/go-multiaddr-net"
)
var loopback, _ = ma.NewMultiaddr("/ip4/127.0.0.1/tcp/0")
var unspec, _ = ma.NewMultiaddr("/ip4/0.0.0.0/tcp/0")
var loopbackV4, _ = ma.NewMultiaddr("/ip4/127.0.0.1/tcp/0")
var loopbackV6, _ = ma.NewMultiaddr("/ip6/::1/tcp/0")
var unspecV6, _ = ma.NewMultiaddr("/ip6/::/tcp/0")
var unspecV4, _ = ma.NewMultiaddr("/ip4/0.0.0.0/tcp/0")
var global ma.Multiaddr
var globalV4 ma.Multiaddr
var globalV6 ma.Multiaddr
func init() {
addrs, err := manet.InterfaceMultiaddrs()
......@@ -21,8 +24,16 @@ func init() {
for _, addr := range addrs {
if !manet.IsIP6LinkLocal(addr) && !manet.IsIPLoopback(addr) {
tcp, _ := ma.NewMultiaddr("/tcp/0")
global = addr.Encapsulate(tcp)
return
switch addr.Protocols()[0].Code {
case ma.P_IP4:
if globalV4 == nil {
globalV4 = addr.Encapsulate(tcp)
}
case ma.P_IP6:
if globalV6 == nil {
globalV6 = addr.Encapsulate(tcp)
}
}
}
}
}
......@@ -68,7 +79,7 @@ func dialOne(t *testing.T, tr *Transport, listener manet.Listener, expected ...i
func TestNoneAndSingle(t *testing.T) {
var trA Transport
var trB Transport
listenerA, err := trA.Listen(loopback)
listenerA, err := trA.Listen(loopbackV4)
if err != nil {
t.Fatal(err)
}
......@@ -76,7 +87,7 @@ func TestNoneAndSingle(t *testing.T) {
dialOne(t, &trB, listenerA)
listenerB, err := trB.Listen(loopback)
listenerB, err := trB.Listen(loopbackV4)
if err != nil {
t.Fatal(err)
}
......@@ -88,19 +99,19 @@ func TestNoneAndSingle(t *testing.T) {
func TestTwoLocal(t *testing.T) {
var trA Transport
var trB Transport
listenerA, err := trA.Listen(loopback)
listenerA, err := trA.Listen(loopbackV4)
if err != nil {
t.Fatal(err)
}
defer listenerA.Close()
listenerB1, err := trB.Listen(loopback)
listenerB1, err := trB.Listen(loopbackV4)
if err != nil {
t.Fatal(err)
}
defer listenerB1.Close()
listenerB2, err := trB.Listen(loopback)
listenerB2, err := trB.Listen(loopbackV4)
if err != nil {
t.Fatal(err)
}
......@@ -111,20 +122,33 @@ func TestTwoLocal(t *testing.T) {
listenerB2.Addr().(*net.TCPAddr).Port)
}
func TestGlobalPreference(t *testing.T) {
if global == nil {
t.Skip("no global addresses configured")
func TestGlobalPreferenceV4(t *testing.T) {
if globalV4 == nil {
t.Skip("no global IPv4 addresses configured")
return
}
testPrefer(t, loopback, loopback, global)
testPrefer(t, loopback, unspec, global)
testPrefer(t, loopbackV4, loopbackV4, globalV4)
testPrefer(t, loopbackV4, unspecV4, globalV4)
testPrefer(t, global, unspec, global)
testPrefer(t, global, unspec, loopback)
testPrefer(t, globalV4, unspecV4, globalV4)
testPrefer(t, globalV4, unspecV4, loopbackV4)
}
func TestGlobalPreferenceV6(t *testing.T) {
if globalV6 == nil {
t.Skip("no global IPv6 addresses configured")
return
}
testPrefer(t, loopbackV6, loopbackV6, globalV6)
testPrefer(t, loopbackV6, unspecV6, globalV6)
testPrefer(t, globalV6, unspecV6, globalV6)
testPrefer(t, globalV6, unspecV6, loopbackV6)
}
func TestLoopbackPreference(t *testing.T) {
testPrefer(t, loopback, loopback, unspec)
testPrefer(t, loopbackV4, loopbackV4, unspecV4)
testPrefer(t, loopbackV6, loopbackV6, unspecV6)
}
func testPrefer(t *testing.T, listen, prefer, avoid ma.Multiaddr) {
......@@ -158,21 +182,30 @@ func testPrefer(t *testing.T, listen, prefer, avoid ma.Multiaddr) {
dialOne(t, &trB, listenerA, listenerB1.Addr().(*net.TCPAddr).Port)
}
func TestV6V4(t *testing.T) {
testUseFirst(t, loopbackV4, loopbackV4, loopbackV6)
testUseFirst(t, loopbackV6, loopbackV6, loopbackV4)
}
func TestGlobalToGlobal(t *testing.T) {
if global == nil {
t.Skip("no global addresses configured")
if globalV4 == nil {
t.Skip("no globalV4 addresses configured")
return
}
testUseFirst(t, globalV4, globalV4, loopbackV4)
testUseFirst(t, globalV6, globalV6, loopbackV6)
}
func testUseFirst(t *testing.T, listen, use, never ma.Multiaddr) {
var trA Transport
var trB Transport
listenerA, err := trA.Listen(global)
listenerA, err := trA.Listen(globalV4)
if err != nil {
t.Fatal(err)
}
defer listenerA.Close()
listenerB1, err := trB.Listen(loopback)
listenerB1, err := trB.Listen(loopbackV4)
if err != nil {
t.Fatal(err)
}
......@@ -181,13 +214,13 @@ func TestGlobalToGlobal(t *testing.T) {
// It works (random port)
dialOne(t, &trB, listenerA)
listenerB2, err := trB.Listen(global)
listenerB2, err := trB.Listen(globalV4)
if err != nil {
t.Fatal(err)
}
defer listenerB2.Close()
// Uses global port.
// Uses globalV4 port.
dialOne(t, &trB, listenerA, listenerB2.Addr().(*net.TCPAddr).Port)
// Closing the listener should reset the dialer.
......@@ -198,26 +231,26 @@ func TestGlobalToGlobal(t *testing.T) {
}
func TestDuplicateGlobal(t *testing.T) {
if global == nil {
t.Skip("no global addresses configured")
if globalV4 == nil {
t.Skip("no globalV4 addresses configured")
return
}
var trA Transport
var trB Transport
listenerA, err := trA.Listen(global)
listenerA, err := trA.Listen(globalV4)
if err != nil {
t.Fatal(err)
}
defer listenerA.Close()
listenerB1, err := trB.Listen(global)
listenerB1, err := trB.Listen(globalV4)
if err != nil {
t.Fatal(err)
}
defer listenerB1.Close()
listenerB2, err := trB.Listen(global)
listenerB2, err := trB.Listen(globalV4)
if err != nil {
t.Fatal(err)
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment