2010-12-03 05:34:57 +01: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 (
|
|
|
|
"bytes"
|
2013-07-16 08:54:42 +02:00
|
|
|
"fmt"
|
2010-12-03 05:34:57 +01:00
|
|
|
"os"
|
2012-12-13 00:13:29 +01:00
|
|
|
"reflect"
|
2013-11-06 20:49:01 +01:00
|
|
|
"runtime"
|
2010-12-03 05:34:57 +01:00
|
|
|
"testing"
|
2012-01-25 22:54:22 +01:00
|
|
|
"time"
|
2010-12-03 05:34:57 +01:00
|
|
|
)
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
type resolveIPAddrTest struct {
|
2013-11-06 20:49:01 +01:00
|
|
|
net string
|
|
|
|
litAddrOrName string
|
|
|
|
addr *IPAddr
|
|
|
|
err error
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var resolveIPAddrTests = []resolveIPAddrTest{
|
2012-12-13 00:13:29 +01:00
|
|
|
{"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},
|
|
|
|
|
|
|
|
{"ip", "::1", &IPAddr{IP: ParseIP("::1")}, nil},
|
|
|
|
{"ip6", "::1", &IPAddr{IP: ParseIP("::1")}, nil},
|
2013-07-16 08:54:42 +02:00
|
|
|
{"ip6:ipv6-icmp", "::1", &IPAddr{IP: ParseIP("::1")}, nil},
|
2013-11-06 20:49:01 +01:00
|
|
|
{"ip6:IPv6-ICMP", "::1", &IPAddr{IP: ParseIP("::1")}, nil},
|
2013-07-16 08:54:42 +02:00
|
|
|
|
|
|
|
{"ip", "::1%en0", &IPAddr{IP: ParseIP("::1"), Zone: "en0"}, nil},
|
|
|
|
{"ip6", "::1%911", &IPAddr{IP: ParseIP("::1"), Zone: "911"}, nil},
|
2012-12-13 00:13:29 +01:00
|
|
|
|
|
|
|
{"", "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
|
|
|
|
|
|
|
|
{"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")},
|
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
func init() {
|
|
|
|
if ifi := loopbackInterface(); ifi != nil {
|
|
|
|
index := fmt.Sprintf("%v", ifi.Index)
|
|
|
|
resolveIPAddrTests = append(resolveIPAddrTests, []resolveIPAddrTest{
|
|
|
|
{"ip6", "fe80::1%" + ifi.Name, &IPAddr{IP: ParseIP("fe80::1"), Zone: zoneToString(ifi.Index)}, nil},
|
|
|
|
{"ip6", "fe80::1%" + index, &IPAddr{IP: ParseIP("fe80::1"), Zone: index}, nil},
|
|
|
|
}...)
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
if ips, err := LookupIP("localhost"); err == nil && len(ips) > 1 && supportsIPv4 && supportsIPv6 {
|
|
|
|
resolveIPAddrTests = append(resolveIPAddrTests, []resolveIPAddrTest{
|
|
|
|
{"ip", "localhost", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
|
|
|
|
{"ip4", "localhost", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
|
|
|
|
{"ip6", "localhost", &IPAddr{IP: IPv6loopback}, nil},
|
|
|
|
}...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func skipRawSocketTest(t *testing.T) (skip bool, skipmsg string) {
|
|
|
|
skip, skipmsg, err := skipRawSocketTests()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return skip, skipmsg
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
|
2012-12-13 00:13:29 +01:00
|
|
|
func TestResolveIPAddr(t *testing.T) {
|
|
|
|
for _, tt := range resolveIPAddrTests {
|
2013-11-06 20:49:01 +01:00
|
|
|
addr, err := ResolveIPAddr(tt.net, tt.litAddrOrName)
|
2012-12-13 00:13:29 +01:00
|
|
|
if err != tt.err {
|
2013-11-06 20:49:01 +01:00
|
|
|
t.Fatalf("ResolveIPAddr(%v, %v) failed: %v", tt.net, tt.litAddrOrName, err)
|
2013-07-16 08:54:42 +02:00
|
|
|
} else if !reflect.DeepEqual(addr, tt.addr) {
|
2012-12-13 00:13:29 +01:00
|
|
|
t.Fatalf("got %#v; expected %#v", addr, tt.addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
var icmpEchoTests = []struct {
|
2012-02-01 20:26:59 +01:00
|
|
|
net string
|
|
|
|
laddr string
|
|
|
|
raddr string
|
|
|
|
}{
|
2013-07-16 08:54:42 +02:00
|
|
|
{"ip4:icmp", "0.0.0.0", "127.0.0.1"},
|
|
|
|
{"ip6:ipv6-icmp", "::", "::1"},
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
func TestConnICMPEcho(t *testing.T) {
|
2013-11-06 20:49:01 +01:00
|
|
|
if skip, skipmsg := skipRawSocketTest(t); skip {
|
|
|
|
t.Skip(skipmsg)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
for i, tt := range icmpEchoTests {
|
|
|
|
net, _, err := parseNetwork(tt.net)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("parseNetwork failed: %v", err)
|
|
|
|
}
|
|
|
|
if net == "ip6" && !supportsIPv6 {
|
2012-02-01 20:26:59 +01:00
|
|
|
continue
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
c, err := Dial(tt.net, tt.raddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Dial failed: %v", err)
|
|
|
|
}
|
|
|
|
c.SetDeadline(time.Now().Add(100 * time.Millisecond))
|
|
|
|
defer c.Close()
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
typ := icmpv4EchoRequest
|
|
|
|
if net == "ip6" {
|
|
|
|
typ = icmpv6EchoRequest
|
|
|
|
}
|
|
|
|
xid, xseq := os.Getpid()&0xffff, i+1
|
2013-11-06 20:49:01 +01:00
|
|
|
wb, err := (&icmpMessage{
|
2013-07-16 08:54:42 +02:00
|
|
|
Type: typ, Code: 0,
|
|
|
|
Body: &icmpEcho{
|
|
|
|
ID: xid, Seq: xseq,
|
|
|
|
Data: bytes.Repeat([]byte("Go Go Gadget Ping!!!"), 3),
|
|
|
|
},
|
|
|
|
}).Marshal()
|
2012-02-01 20:26:59 +01:00
|
|
|
if err != nil {
|
2013-07-16 08:54:42 +02:00
|
|
|
t.Fatalf("icmpMessage.Marshal failed: %v", err)
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
if _, err := c.Write(wb); err != nil {
|
2013-07-16 08:54:42 +02:00
|
|
|
t.Fatalf("Conn.Write failed: %v", err)
|
|
|
|
}
|
|
|
|
var m *icmpMessage
|
2013-11-06 20:49:01 +01:00
|
|
|
rb := make([]byte, 20+len(wb))
|
2013-07-16 08:54:42 +02:00
|
|
|
for {
|
2013-11-06 20:49:01 +01:00
|
|
|
if _, err := c.Read(rb); err != nil {
|
2013-07-16 08:54:42 +02:00
|
|
|
t.Fatalf("Conn.Read failed: %v", err)
|
|
|
|
}
|
|
|
|
if net == "ip4" {
|
2013-11-06 20:49:01 +01:00
|
|
|
rb = ipv4Payload(rb)
|
2012-03-02 21:01:37 +01:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
if m, err = parseICMPMessage(rb); err != nil {
|
2013-07-16 08:54:42 +02:00
|
|
|
t.Fatalf("parseICMPMessage failed: %v", err)
|
|
|
|
}
|
|
|
|
switch m.Type {
|
|
|
|
case icmpv4EchoRequest, icmpv6EchoRequest:
|
2012-03-02 21:01:37 +01:00
|
|
|
continue
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
break
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
switch p := m.Body.(type) {
|
|
|
|
case *icmpEcho:
|
|
|
|
if p.ID != xid || p.Seq != xseq {
|
|
|
|
t.Fatalf("got id=%v, seqnum=%v; expected id=%v, seqnum=%v", p.ID, p.Seq, xid, xseq)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatalf("got type=%v, code=%v; expected type=%v, code=%v", m.Type, m.Code, typ, 0)
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
func TestPacketConnICMPEcho(t *testing.T) {
|
2013-11-06 20:49:01 +01:00
|
|
|
if skip, skipmsg := skipRawSocketTest(t); skip {
|
|
|
|
t.Skip(skipmsg)
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
for i, tt := range icmpEchoTests {
|
|
|
|
net, _, err := parseNetwork(tt.net)
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
2013-07-16 08:54:42 +02:00
|
|
|
t.Fatalf("parseNetwork failed: %v", err)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
if net == "ip6" && !supportsIPv6 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := ListenPacket(tt.net, tt.laddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ListenPacket failed: %v", err)
|
|
|
|
}
|
|
|
|
c.SetDeadline(time.Now().Add(100 * time.Millisecond))
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
ra, err := ResolveIPAddr(tt.net, tt.raddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ResolveIPAddr failed: %v", err)
|
|
|
|
}
|
|
|
|
typ := icmpv4EchoRequest
|
|
|
|
if net == "ip6" {
|
|
|
|
typ = icmpv6EchoRequest
|
|
|
|
}
|
|
|
|
xid, xseq := os.Getpid()&0xffff, i+1
|
2013-11-06 20:49:01 +01:00
|
|
|
wb, err := (&icmpMessage{
|
2013-07-16 08:54:42 +02:00
|
|
|
Type: typ, Code: 0,
|
|
|
|
Body: &icmpEcho{
|
|
|
|
ID: xid, Seq: xseq,
|
|
|
|
Data: bytes.Repeat([]byte("Go Go Gadget Ping!!!"), 3),
|
|
|
|
},
|
|
|
|
}).Marshal()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("icmpMessage.Marshal failed: %v", err)
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
if _, err := c.WriteTo(wb, ra); err != nil {
|
2013-07-16 08:54:42 +02:00
|
|
|
t.Fatalf("PacketConn.WriteTo failed: %v", err)
|
|
|
|
}
|
|
|
|
var m *icmpMessage
|
2013-11-06 20:49:01 +01:00
|
|
|
rb := make([]byte, 20+len(wb))
|
2013-07-16 08:54:42 +02:00
|
|
|
for {
|
2013-11-06 20:49:01 +01:00
|
|
|
if _, _, err := c.ReadFrom(rb); err != nil {
|
2013-07-16 08:54:42 +02:00
|
|
|
t.Fatalf("PacketConn.ReadFrom failed: %v", err)
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
// See BUG section.
|
2013-07-16 08:54:42 +02:00
|
|
|
//if net == "ip4" {
|
2013-11-06 20:49:01 +01:00
|
|
|
// rb = ipv4Payload(rb)
|
2013-07-16 08:54:42 +02:00
|
|
|
//}
|
2013-11-06 20:49:01 +01:00
|
|
|
if m, err = parseICMPMessage(rb); err != nil {
|
2013-07-16 08:54:42 +02:00
|
|
|
t.Fatalf("parseICMPMessage failed: %v", err)
|
2012-03-02 21:01:37 +01:00
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
switch m.Type {
|
|
|
|
case icmpv4EchoRequest, icmpv6EchoRequest:
|
2012-03-02 21:01:37 +01:00
|
|
|
continue
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
switch p := m.Body.(type) {
|
|
|
|
case *icmpEcho:
|
|
|
|
if p.ID != xid || p.Seq != xseq {
|
|
|
|
t.Fatalf("got id=%v, seqnum=%v; expected id=%v, seqnum=%v", p.ID, p.Seq, xid, xseq)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatalf("got type=%v, code=%v; expected type=%v, code=%v", m.Type, m.Code, typ, 0)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
2012-02-01 20:26:59 +01:00
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
func ipv4Payload(b []byte) []byte {
|
|
|
|
if len(b) < 20 {
|
|
|
|
return b
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
hdrlen := int(b[0]&0x0f) << 2
|
|
|
|
return b[hdrlen:]
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
|
2012-11-21 08:03:38 +01:00
|
|
|
var ipConnLocalNameTests = []struct {
|
|
|
|
net string
|
|
|
|
laddr *IPAddr
|
|
|
|
}{
|
|
|
|
{"ip4:icmp", &IPAddr{IP: IPv4(127, 0, 0, 1)}},
|
|
|
|
{"ip4:icmp", &IPAddr{}},
|
|
|
|
{"ip4:icmp", nil},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIPConnLocalName(t *testing.T) {
|
2013-11-06 20:49:01 +01:00
|
|
|
switch runtime.GOOS {
|
2014-07-19 10:53:52 +02:00
|
|
|
case "nacl", "plan9", "windows":
|
2013-11-06 20:49:01 +01:00
|
|
|
t.Skipf("skipping test on %q", runtime.GOOS)
|
|
|
|
default:
|
|
|
|
if os.Getuid() != 0 {
|
|
|
|
t.Skip("skipping test; must be root")
|
|
|
|
}
|
2012-11-21 08:03:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range ipConnLocalNameTests {
|
|
|
|
c, err := ListenIP(tt.net, tt.laddr)
|
|
|
|
if err != nil {
|
2013-07-16 08:54:42 +02:00
|
|
|
t.Fatalf("ListenIP failed: %v", err)
|
2012-11-21 08:03:38 +01:00
|
|
|
}
|
|
|
|
defer c.Close()
|
2013-07-16 08:54:42 +02:00
|
|
|
if la := c.LocalAddr(); la == nil {
|
|
|
|
t.Fatal("IPConn.LocalAddr failed")
|
2012-11-21 08:03:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
|
|
|
|
func TestIPConnRemoteName(t *testing.T) {
|
2013-11-06 20:49:01 +01:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "plan9", "windows":
|
|
|
|
t.Skipf("skipping test on %q", runtime.GOOS)
|
|
|
|
default:
|
|
|
|
if os.Getuid() != 0 {
|
|
|
|
t.Skip("skipping test; must be root")
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
raddr := &IPAddr{IP: IPv4(127, 0, 0, 1).To4()}
|
2013-07-16 08:54:42 +02:00
|
|
|
c, err := DialIP("ip:tcp", &IPAddr{IP: IPv4(127, 0, 0, 1)}, raddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("DialIP failed: %v", err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
if !reflect.DeepEqual(raddr, c.RemoteAddr()) {
|
|
|
|
t.Fatalf("got %#v, expected %#v", c.RemoteAddr(), raddr)
|
|
|
|
}
|
|
|
|
}
|