2012-02-01 20:26:59 +01:00
|
|
|
// Copyright 2012 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 (
|
2012-12-13 00:13:29 +01:00
|
|
|
"reflect"
|
2012-02-01 20:26:59 +01:00
|
|
|
"runtime"
|
2013-11-06 20:49:01 +01:00
|
|
|
"strings"
|
2012-02-01 20:26:59 +01:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2012-12-13 00:13:29 +01:00
|
|
|
func TestResolveUDPAddr(t *testing.T) {
|
2013-11-06 20:49:01 +01:00
|
|
|
for _, tt := range resolveTCPAddrTests {
|
|
|
|
net := strings.Replace(tt.net, "tcp", "udp", -1)
|
|
|
|
addr, err := ResolveUDPAddr(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("ResolveUDPAddr(%q, %q) failed: %v", net, tt.litAddrOrName, err)
|
2012-12-13 00:13:29 +01:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
if !reflect.DeepEqual(addr, (*UDPAddr)(tt.addr)) {
|
|
|
|
t.Fatalf("ResolveUDPAddr(%q, %q) = %#v, want %#v", net, tt.litAddrOrName, addr, tt.addr)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
str := addr.String()
|
|
|
|
addr1, err := ResolveUDPAddr(net, str)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ResolveUDPAddr(%q, %q) [from %q]: %v", net, str, tt.litAddrOrName, err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(addr1, addr) {
|
|
|
|
t.Fatalf("ResolveUDPAddr(%q, %q) [from %q] = %#v, want %#v", net, str, tt.litAddrOrName, addr1, addr)
|
|
|
|
}
|
2012-12-13 00:13:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-01 20:26:59 +01:00
|
|
|
func TestWriteToUDP(t *testing.T) {
|
2012-03-30 23:27:11 +02:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "plan9":
|
2013-01-29 21:52:43 +01:00
|
|
|
t.Skipf("skipping test on %q", runtime.GOOS)
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
l, err := ListenPacket("udp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Listen failed: %v", err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
testWriteToConn(t, l.LocalAddr().String())
|
|
|
|
testWriteToPacketConn(t, l.LocalAddr().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func testWriteToConn(t *testing.T, raddr string) {
|
|
|
|
c, err := Dial("udp", raddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Dial failed: %v", err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
ra, err := ResolveUDPAddr("udp", raddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ResolveUDPAddr failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.(*UDPConn).WriteToUDP([]byte("Connection-oriented mode socket"), ra)
|
|
|
|
if err == nil {
|
2012-03-06 18:57:23 +01:00
|
|
|
t.Fatal("WriteToUDP should fail")
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
if err != nil && err.(*OpError).Err != ErrWriteToConnected {
|
2012-03-06 18:57:23 +01:00
|
|
|
t.Fatalf("WriteToUDP should fail as ErrWriteToConnected: %v", err)
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.(*UDPConn).WriteTo([]byte("Connection-oriented mode socket"), ra)
|
|
|
|
if err == nil {
|
2012-03-06 18:57:23 +01:00
|
|
|
t.Fatal("WriteTo should fail")
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
if err != nil && err.(*OpError).Err != ErrWriteToConnected {
|
2012-03-06 18:57:23 +01:00
|
|
|
t.Fatalf("WriteTo should fail as ErrWriteToConnected: %v", err)
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.Write([]byte("Connection-oriented mode socket"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Write failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testWriteToPacketConn(t *testing.T, raddr string) {
|
|
|
|
c, err := ListenPacket("udp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ListenPacket failed: %v", err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
ra, err := ResolveUDPAddr("udp", raddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ResolveUDPAddr failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.(*UDPConn).WriteToUDP([]byte("Connection-less mode socket"), ra)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("WriteToUDP failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.WriteTo([]byte("Connection-less mode socket"), ra)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("WriteTo failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.(*UDPConn).Write([]byte("Connection-less mode socket"))
|
|
|
|
if err == nil {
|
2012-03-06 18:57:23 +01:00
|
|
|
t.Fatal("Write should fail")
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
}
|
2012-11-21 08:03:38 +01:00
|
|
|
|
|
|
|
var udpConnLocalNameTests = []struct {
|
|
|
|
net string
|
|
|
|
laddr *UDPAddr
|
|
|
|
}{
|
|
|
|
{"udp4", &UDPAddr{IP: IPv4(127, 0, 0, 1)}},
|
|
|
|
{"udp4", &UDPAddr{}},
|
|
|
|
{"udp4", nil},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUDPConnLocalName(t *testing.T) {
|
|
|
|
if testing.Short() || !*testExternal {
|
2013-01-29 21:52:43 +01:00
|
|
|
t.Skip("skipping test to avoid external network")
|
2012-11-21 08:03:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range udpConnLocalNameTests {
|
|
|
|
c, err := ListenUDP(tt.net, tt.laddr)
|
|
|
|
if err != nil {
|
2013-07-16 08:54:42 +02:00
|
|
|
t.Fatalf("ListenUDP failed: %v", err)
|
2012-11-21 08:03:38 +01:00
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
la := c.LocalAddr()
|
|
|
|
if a, ok := la.(*UDPAddr); !ok || a.Port == 0 {
|
2013-07-16 08:54:42 +02:00
|
|
|
t.Fatalf("got %v; expected a proper address with non-zero port number", la)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUDPConnLocalAndRemoteNames(t *testing.T) {
|
|
|
|
for _, laddr := range []string{"", "127.0.0.1:0"} {
|
|
|
|
c1, err := ListenPacket("udp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ListenUDP failed: %v", err)
|
|
|
|
}
|
|
|
|
defer c1.Close()
|
|
|
|
|
|
|
|
var la *UDPAddr
|
|
|
|
if laddr != "" {
|
|
|
|
var err error
|
|
|
|
if la, err = ResolveUDPAddr("udp", laddr); err != nil {
|
|
|
|
t.Fatalf("ResolveUDPAddr failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c2, err := DialUDP("udp", la, c1.LocalAddr().(*UDPAddr))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("DialUDP failed: %v", err)
|
|
|
|
}
|
|
|
|
defer c2.Close()
|
|
|
|
|
|
|
|
var connAddrs = [4]struct {
|
|
|
|
got Addr
|
|
|
|
ok bool
|
|
|
|
}{
|
|
|
|
{c1.LocalAddr(), true},
|
|
|
|
{c1.(*UDPConn).RemoteAddr(), false},
|
|
|
|
{c2.LocalAddr(), true},
|
|
|
|
{c2.RemoteAddr(), true},
|
|
|
|
}
|
|
|
|
for _, ca := range connAddrs {
|
|
|
|
if a, ok := ca.got.(*UDPAddr); ok != ca.ok || ok && a.Port == 0 {
|
|
|
|
t.Fatalf("got %v; expected a proper address with non-zero port number", ca.got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIPv6LinkLocalUnicastUDP(t *testing.T) {
|
|
|
|
if testing.Short() || !*testExternal {
|
|
|
|
t.Skip("skipping test to avoid external network")
|
|
|
|
}
|
|
|
|
if !supportsIPv6 {
|
|
|
|
t.Skip("ipv6 is not supported")
|
|
|
|
}
|
|
|
|
ifi := loopbackInterface()
|
|
|
|
if ifi == nil {
|
|
|
|
t.Skip("loopback interface not found")
|
|
|
|
}
|
|
|
|
laddr := ipv6LinkLocalUnicastAddr(ifi)
|
|
|
|
if laddr == "" {
|
|
|
|
t.Skip("ipv6 unicast address on loopback not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
type test struct {
|
|
|
|
net, addr string
|
|
|
|
nameLookup bool
|
|
|
|
}
|
|
|
|
var tests = []test{
|
|
|
|
{"udp", "[" + laddr + "%" + ifi.Name + "]:0", false},
|
|
|
|
{"udp6", "[" + laddr + "%" + ifi.Name + "]:0", false},
|
|
|
|
}
|
2014-07-19 10:53:52 +02:00
|
|
|
// The first udp test fails on DragonFly - see issue 7473.
|
|
|
|
if runtime.GOOS == "dragonfly" {
|
|
|
|
tests = tests[1:]
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
switch runtime.GOOS {
|
2013-11-06 20:49:01 +01:00
|
|
|
case "darwin", "dragonfly", "freebsd", "openbsd", "netbsd":
|
2013-07-16 08:54:42 +02:00
|
|
|
tests = append(tests, []test{
|
|
|
|
{"udp", "[localhost%" + ifi.Name + "]:0", true},
|
|
|
|
{"udp6", "[localhost%" + ifi.Name + "]:0", true},
|
|
|
|
}...)
|
|
|
|
case "linux":
|
|
|
|
tests = append(tests, []test{
|
|
|
|
{"udp", "[ip6-localhost%" + ifi.Name + "]:0", true},
|
|
|
|
{"udp6", "[ip6-localhost%" + ifi.Name + "]:0", true},
|
|
|
|
}...)
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
c1, err := ListenPacket(tt.net, tt.addr)
|
|
|
|
if err != nil {
|
|
|
|
// It might return "LookupHost returned no
|
|
|
|
// suitable address" error on some platforms.
|
|
|
|
t.Logf("ListenPacket failed: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
defer c1.Close()
|
|
|
|
if la, ok := c1.LocalAddr().(*UDPAddr); !ok || !tt.nameLookup && la.Zone == "" {
|
|
|
|
t.Fatalf("got %v; expected a proper address with zone identifier", la)
|
|
|
|
}
|
|
|
|
|
|
|
|
c2, err := Dial(tt.net, c1.LocalAddr().String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Dial failed: %v", err)
|
|
|
|
}
|
|
|
|
defer c2.Close()
|
|
|
|
if la, ok := c2.LocalAddr().(*UDPAddr); !ok || !tt.nameLookup && la.Zone == "" {
|
|
|
|
t.Fatalf("got %v; expected a proper address with zone identifier", la)
|
|
|
|
}
|
|
|
|
if ra, ok := c2.RemoteAddr().(*UDPAddr); !ok || !tt.nameLookup && ra.Zone == "" {
|
|
|
|
t.Fatalf("got %v; expected a proper address with zone identifier", ra)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := c2.Write([]byte("UDP OVER IPV6 LINKLOCAL TEST")); err != nil {
|
|
|
|
t.Fatalf("Conn.Write failed: %v", err)
|
|
|
|
}
|
|
|
|
b := make([]byte, 32)
|
|
|
|
if _, from, err := c1.ReadFrom(b); err != nil {
|
|
|
|
t.Fatalf("PacketConn.ReadFrom failed: %v", err)
|
|
|
|
} else {
|
|
|
|
if ra, ok := from.(*UDPAddr); !ok || !tt.nameLookup && ra.Zone == "" {
|
|
|
|
t.Fatalf("got %v; expected a proper address with zone identifier", ra)
|
|
|
|
}
|
2012-11-21 08:03:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|