Retro68/gcc/libgo/go/net/ipraw_test.go

117 lines
3.0 KiB
Go
Raw Normal View History

2012-03-27 23:13:14 +00:00
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package net
import (
2014-09-21 17:33:12 +00:00
"reflect"
2012-03-27 23:13:14 +00:00
"testing"
)
2017-04-10 11:32:00 +00:00
// The full stack test cases for IPConn have been moved to the
// following:
// golang.org/x/net/ipv4
// golang.org/x/net/ipv6
// golang.org/x/net/icmp
2014-09-21 17:33:12 +00:00
type resolveIPAddrTest struct {
2017-04-10 11:32:00 +00:00
network string
2014-09-21 17:33:12 +00:00
litAddrOrName string
addr *IPAddr
err error
2012-03-27 23:13:14 +00:00
}
2014-09-21 17:33:12 +00:00
var resolveIPAddrTests = []resolveIPAddrTest{
{"ip", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
{"ip4", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
{"ip4:icmp", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
2012-03-27 23:13:14 +00:00
2014-09-21 17:33:12 +00:00
{"ip", "::1", &IPAddr{IP: ParseIP("::1")}, nil},
{"ip6", "::1", &IPAddr{IP: ParseIP("::1")}, nil},
{"ip6:ipv6-icmp", "::1", &IPAddr{IP: ParseIP("::1")}, nil},
{"ip6:IPv6-ICMP", "::1", &IPAddr{IP: ParseIP("::1")}, nil},
{"ip", "::1%en0", &IPAddr{IP: ParseIP("::1"), Zone: "en0"}, nil},
{"ip6", "::1%911", &IPAddr{IP: ParseIP("::1"), Zone: "911"}, nil},
{"", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil}, // Go 1.0 behavior
{"", "::1", &IPAddr{IP: ParseIP("::1")}, nil}, // Go 1.0 behavior
2017-04-10 11:32:00 +00:00
{"ip4:icmp", "", &IPAddr{}, nil},
2014-09-21 17:33:12 +00:00
{"l2tp", "127.0.0.1", nil, UnknownNetworkError("l2tp")},
{"l2tp:gre", "127.0.0.1", nil, UnknownNetworkError("l2tp:gre")},
{"tcp", "1.2.3.4:123", nil, UnknownNetworkError("tcp")},
}
func TestResolveIPAddr(t *testing.T) {
2017-04-10 11:32:00 +00:00
if !testableNetwork("ip+nopriv") {
t.Skip("ip+nopriv test")
2015-08-28 15:33:40 +00:00
}
2017-04-10 11:32:00 +00:00
origTestHookLookupIP := testHookLookupIP
defer func() { testHookLookupIP = origTestHookLookupIP }()
testHookLookupIP = lookupLocalhost
for i, tt := range resolveIPAddrTests {
addr, err := ResolveIPAddr(tt.network, tt.litAddrOrName)
2014-09-21 17:33:12 +00:00
if err != tt.err {
2017-04-10 11:32:00 +00:00
t.Errorf("#%d: %v", i, err)
2014-09-21 17:33:12 +00:00
} else if !reflect.DeepEqual(addr, tt.addr) {
2017-04-10 11:32:00 +00:00
t.Errorf("#%d: got %#v; want %#v", i, addr, tt.addr)
2014-09-21 17:33:12 +00:00
}
2012-03-27 23:13:14 +00:00
if err != nil {
2014-09-21 17:33:12 +00:00
continue
}
2017-04-10 11:32:00 +00:00
rtaddr, err := ResolveIPAddr(addr.Network(), addr.String())
2014-09-21 17:33:12 +00:00
if err != nil {
2017-04-10 11:32:00 +00:00
t.Errorf("#%d: %v", i, err)
} else if !reflect.DeepEqual(rtaddr, addr) {
t.Errorf("#%d: got %#v; want %#v", i, rtaddr, addr)
2012-03-27 23:13:14 +00:00
}
}
}
2014-09-21 17:33:12 +00:00
var ipConnLocalNameTests = []struct {
net string
laddr *IPAddr
}{
{"ip4:icmp", &IPAddr{IP: IPv4(127, 0, 0, 1)}},
{"ip4:icmp", &IPAddr{}},
{"ip4:icmp", nil},
2012-03-27 23:13:14 +00:00
}
2014-09-21 17:33:12 +00:00
func TestIPConnLocalName(t *testing.T) {
for _, tt := range ipConnLocalNameTests {
2017-04-10 11:32:00 +00:00
if !testableNetwork(tt.net) {
t.Logf("skipping %s test", tt.net)
continue
}
2014-09-21 17:33:12 +00:00
c, err := ListenIP(tt.net, tt.laddr)
if err != nil {
2017-04-10 11:32:00 +00:00
t.Fatal(err)
2014-09-21 17:33:12 +00:00
}
defer c.Close()
if la := c.LocalAddr(); la == nil {
2017-04-10 11:32:00 +00:00
t.Fatal("should not fail")
2014-09-21 17:33:12 +00:00
}
2012-03-27 23:13:14 +00:00
}
}
2014-09-21 17:33:12 +00:00
func TestIPConnRemoteName(t *testing.T) {
2017-04-10 11:32:00 +00:00
if !testableNetwork("ip:tcp") {
t.Skip("ip:tcp test")
2014-09-21 17:33:12 +00:00
}
2012-03-27 23:13:14 +00:00
2015-08-28 15:33:40 +00:00
raddr := &IPAddr{IP: IPv4(127, 0, 0, 1).To4()}
2014-09-21 17:33:12 +00:00
c, err := DialIP("ip:tcp", &IPAddr{IP: IPv4(127, 0, 0, 1)}, raddr)
if err != nil {
2017-04-10 11:32:00 +00:00
t.Fatal(err)
2012-03-27 23:13:14 +00:00
}
2014-09-21 17:33:12 +00:00
defer c.Close()
if !reflect.DeepEqual(raddr, c.RemoteAddr()) {
2017-04-10 11:32:00 +00:00
t.Fatalf("got %#v; want %#v", c.RemoteAddr(), raddr)
2012-03-27 23:13:14 +00:00
}
}