2016-07-22 20:15:38 +02:00
|
|
|
// Copyright 2012 The Go Authors. All rights reserved.
|
2012-02-01 20:26:59 +01:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2016-07-22 20:15:38 +02:00
|
|
|
"internal/testenv"
|
2012-12-13 00:13:29 +01:00
|
|
|
"reflect"
|
2012-02-01 20:26:59 +01:00
|
|
|
"runtime"
|
|
|
|
"testing"
|
2015-01-15 01:27:56 +01:00
|
|
|
"time"
|
2012-02-01 20:26:59 +01:00
|
|
|
)
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
func BenchmarkUDP6LinkLocalUnicast(b *testing.B) {
|
|
|
|
testHookUninstaller.Do(uninstallTestHooks)
|
|
|
|
|
2017-09-14 19:11:35 +02:00
|
|
|
if !supportsIPv6() {
|
2016-07-22 20:15:38 +02:00
|
|
|
b.Skip("IPv6 is not supported")
|
|
|
|
}
|
|
|
|
ifi := loopbackInterface()
|
|
|
|
if ifi == nil {
|
|
|
|
b.Skip("loopback interface not found")
|
|
|
|
}
|
|
|
|
lla := ipv6LinkLocalUnicastAddr(ifi)
|
|
|
|
if lla == "" {
|
|
|
|
b.Skip("IPv6 link-local unicast address not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
c1, err := ListenPacket("udp6", JoinHostPort(lla+"%"+ifi.Name, "0"))
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer c1.Close()
|
|
|
|
c2, err := ListenPacket("udp6", JoinHostPort(lla+"%"+ifi.Name, "0"))
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer c2.Close()
|
|
|
|
|
|
|
|
var buf [1]byte
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if _, err := c1.WriteTo(buf[:], c2.LocalAddr()); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, _, err := c2.ReadFrom(buf[:]); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
type resolveUDPAddrTest struct {
|
|
|
|
network string
|
|
|
|
litAddrOrName string
|
|
|
|
addr *UDPAddr
|
|
|
|
err error
|
2012-12-13 00:13:29 +01:00
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
var resolveUDPAddrTests = []resolveUDPAddrTest{
|
|
|
|
{"udp", "127.0.0.1:0", &UDPAddr{IP: IPv4(127, 0, 0, 1), Port: 0}, nil},
|
|
|
|
{"udp4", "127.0.0.1:65535", &UDPAddr{IP: IPv4(127, 0, 0, 1), Port: 65535}, nil},
|
2015-01-15 01:27:56 +01:00
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
{"udp", "[::1]:0", &UDPAddr{IP: ParseIP("::1"), Port: 0}, nil},
|
|
|
|
{"udp6", "[::1]:65535", &UDPAddr{IP: ParseIP("::1"), Port: 65535}, nil},
|
2015-01-15 01:27:56 +01:00
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
{"udp", "[::1%en0]:1", &UDPAddr{IP: ParseIP("::1"), Port: 1, Zone: "en0"}, nil},
|
|
|
|
{"udp6", "[::1%911]:2", &UDPAddr{IP: ParseIP("::1"), Port: 2, Zone: "911"}, nil},
|
2015-01-15 01:27:56 +01:00
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
{"", "127.0.0.1:0", &UDPAddr{IP: IPv4(127, 0, 0, 1), Port: 0}, nil}, // Go 1.0 behavior
|
|
|
|
{"", "[::1]:0", &UDPAddr{IP: ParseIP("::1"), Port: 0}, nil}, // Go 1.0 behavior
|
2015-01-15 01:27:56 +01:00
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
{"udp", ":12345", &UDPAddr{Port: 12345}, nil},
|
2015-01-15 01:27:56 +01:00
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
{"http", "127.0.0.1:0", nil, UnknownNetworkError("http")},
|
2017-01-14 01:05:42 +01:00
|
|
|
|
|
|
|
{"udp", "127.0.0.1:domain", &UDPAddr{IP: ParseIP("127.0.0.1"), Port: 53}, nil},
|
|
|
|
{"udp", "[::ffff:127.0.0.1]:domain", &UDPAddr{IP: ParseIP("::ffff:127.0.0.1"), Port: 53}, nil},
|
|
|
|
{"udp", "[2001:db8::1]:domain", &UDPAddr{IP: ParseIP("2001:db8::1"), Port: 53}, nil},
|
|
|
|
{"udp4", "127.0.0.1:domain", &UDPAddr{IP: ParseIP("127.0.0.1"), Port: 53}, nil},
|
|
|
|
{"udp4", "[::ffff:127.0.0.1]:domain", &UDPAddr{IP: ParseIP("127.0.0.1"), Port: 53}, nil},
|
|
|
|
{"udp6", "[2001:db8::1]:domain", &UDPAddr{IP: ParseIP("2001:db8::1"), Port: 53}, nil},
|
|
|
|
|
|
|
|
{"udp4", "[2001:db8::1]:domain", nil, &AddrError{Err: errNoSuitableAddress.Error(), Addr: "2001:db8::1"}},
|
|
|
|
{"udp6", "127.0.0.1:domain", nil, &AddrError{Err: errNoSuitableAddress.Error(), Addr: "127.0.0.1"}},
|
|
|
|
{"udp6", "[::ffff:127.0.0.1]:domain", nil, &AddrError{Err: errNoSuitableAddress.Error(), Addr: "::ffff:127.0.0.1"}},
|
2015-10-31 01:59:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestResolveUDPAddr(t *testing.T) {
|
|
|
|
origTestHookLookupIP := testHookLookupIP
|
|
|
|
defer func() { testHookLookupIP = origTestHookLookupIP }()
|
|
|
|
testHookLookupIP = lookupLocalhost
|
|
|
|
|
2017-01-14 01:05:42 +01:00
|
|
|
for _, tt := range resolveUDPAddrTests {
|
2015-10-31 01:59:47 +01:00
|
|
|
addr, err := ResolveUDPAddr(tt.network, tt.litAddrOrName)
|
2017-01-14 01:05:42 +01:00
|
|
|
if !reflect.DeepEqual(addr, tt.addr) || !reflect.DeepEqual(err, tt.err) {
|
|
|
|
t.Errorf("ResolveUDPAddr(%q, %q) = %#v, %v, want %#v, %v", tt.network, tt.litAddrOrName, addr, err, tt.addr, tt.err)
|
2015-10-31 01:59:47 +01:00
|
|
|
continue
|
|
|
|
}
|
2017-01-14 01:05:42 +01:00
|
|
|
if err == nil {
|
|
|
|
addr2, err := ResolveUDPAddr(addr.Network(), addr.String())
|
|
|
|
if !reflect.DeepEqual(addr2, tt.addr) || err != tt.err {
|
|
|
|
t.Errorf("(%q, %q): ResolveUDPAddr(%q, %q) = %#v, %v, want %#v, %v", tt.network, tt.litAddrOrName, addr.Network(), addr.String(), addr2, err, tt.addr, tt.err)
|
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
}
|
2015-01-15 01:27:56 +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":
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Skipf("not supported on %s", runtime.GOOS)
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
c, err := ListenPacket("udp", "127.0.0.1:0")
|
2012-02-01 20:26:59 +01:00
|
|
|
if err != nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatal(err)
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
defer c.Close()
|
2012-02-01 20:26:59 +01:00
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
testWriteToConn(t, c.LocalAddr().String())
|
|
|
|
testWriteToPacketConn(t, c.LocalAddr().String())
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func testWriteToConn(t *testing.T, raddr string) {
|
|
|
|
c, err := Dial("udp", raddr)
|
|
|
|
if err != nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatal(err)
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
ra, err := ResolveUDPAddr("udp", raddr)
|
|
|
|
if err != nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatal(err)
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
b := []byte("CONNECTED-MODE SOCKET")
|
|
|
|
_, err = c.(*UDPConn).WriteToUDP(b, ra)
|
2012-02-01 20:26:59 +01:00
|
|
|
if err == nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatal("should fail")
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
if err != nil && err.(*OpError).Err != ErrWriteToConnected {
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatalf("should fail as ErrWriteToConnected: %v", err)
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
_, err = c.(*UDPConn).WriteTo(b, ra)
|
2012-02-01 20:26:59 +01:00
|
|
|
if err == nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatal("should fail")
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
if err != nil && err.(*OpError).Err != ErrWriteToConnected {
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatalf("should fail as ErrWriteToConnected: %v", err)
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
_, err = c.Write(b)
|
2012-02-01 20:26:59 +01:00
|
|
|
if err != nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, _, err = c.(*UDPConn).WriteMsgUDP(b, nil, ra)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should fail")
|
|
|
|
}
|
|
|
|
if err != nil && err.(*OpError).Err != ErrWriteToConnected {
|
|
|
|
t.Fatalf("should fail as ErrWriteToConnected: %v", err)
|
|
|
|
}
|
|
|
|
_, _, err = c.(*UDPConn).WriteMsgUDP(b, nil, nil)
|
|
|
|
switch runtime.GOOS {
|
2018-01-09 02:23:08 +01:00
|
|
|
case "nacl": // see golang.org/issue/9252
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Skipf("not implemented yet on %s", runtime.GOOS)
|
|
|
|
default:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testWriteToPacketConn(t *testing.T, raddr string) {
|
|
|
|
c, err := ListenPacket("udp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatal(err)
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
ra, err := ResolveUDPAddr("udp", raddr)
|
|
|
|
if err != nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatal(err)
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
b := []byte("UNCONNECTED-MODE SOCKET")
|
|
|
|
_, err = c.(*UDPConn).WriteToUDP(b, ra)
|
2012-02-01 20:26:59 +01:00
|
|
|
if err != nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatal(err)
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
_, err = c.WriteTo(b, ra)
|
2012-02-01 20:26:59 +01:00
|
|
|
if err != nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatal(err)
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
_, err = c.(*UDPConn).Write(b)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should fail")
|
|
|
|
}
|
|
|
|
_, _, err = c.(*UDPConn).WriteMsgUDP(b, nil, nil)
|
2012-02-01 20:26:59 +01:00
|
|
|
if err == nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatal("should fail")
|
|
|
|
}
|
|
|
|
if err != nil && err.(*OpError).Err != errMissingAddress {
|
|
|
|
t.Fatalf("should fail as errMissingAddress: %v", err)
|
|
|
|
}
|
|
|
|
_, _, err = c.(*UDPConn).WriteMsgUDP(b, nil, ra)
|
|
|
|
switch runtime.GOOS {
|
2018-01-09 02:23:08 +01:00
|
|
|
case "nacl": // see golang.org/issue/9252
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Skipf("not implemented yet on %s", runtime.GOOS)
|
|
|
|
default:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
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) {
|
2016-07-22 20:15:38 +02:00
|
|
|
testenv.MustHaveExternalNetwork(t)
|
2012-11-21 08:03:38 +01:00
|
|
|
|
|
|
|
for _, tt := range udpConnLocalNameTests {
|
|
|
|
c, err := ListenUDP(tt.net, tt.laddr)
|
|
|
|
if err != nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatal(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 {
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatal(err)
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
defer c1.Close()
|
|
|
|
|
|
|
|
var la *UDPAddr
|
|
|
|
if laddr != "" {
|
|
|
|
var err error
|
|
|
|
if la, err = ResolveUDPAddr("udp", laddr); err != nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatal(err)
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
c2, err := DialUDP("udp", la, c1.LocalAddr().(*UDPAddr))
|
|
|
|
if err != nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatal(err)
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
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) {
|
2016-07-22 20:15:38 +02:00
|
|
|
testenv.MustHaveExternalNetwork(t)
|
|
|
|
|
2017-09-14 19:11:35 +02:00
|
|
|
if !supportsIPv6() {
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Skip("IPv6 is not supported")
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
for i, tt := range ipv6LinkLocalUnicastUDPTests {
|
|
|
|
c1, err := ListenPacket(tt.network, tt.address)
|
2013-07-16 08:54:42 +02:00
|
|
|
if err != nil {
|
|
|
|
// It might return "LookupHost returned no
|
|
|
|
// suitable address" error on some platforms.
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Log(err)
|
2013-07-16 08:54:42 +02:00
|
|
|
continue
|
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
ls, err := (&packetListener{PacketConn: c1}).newLocalServer()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer ls.teardown()
|
|
|
|
ch := make(chan error, 1)
|
|
|
|
handler := func(ls *localPacketServer, c PacketConn) { packetTransponder(c, ch) }
|
|
|
|
if err := ls.buildup(handler); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
if la, ok := c1.LocalAddr().(*UDPAddr); !ok || !tt.nameLookup && la.Zone == "" {
|
|
|
|
t.Fatalf("got %v; expected a proper address with zone identifier", la)
|
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
c2, err := Dial(tt.network, ls.PacketConn.LocalAddr().String())
|
2013-07-16 08:54:42 +02:00
|
|
|
if err != nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatal(err)
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
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 {
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatal(err)
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
b := make([]byte, 32)
|
2015-10-31 01:59:47 +01:00
|
|
|
if _, err := c2.Read(b); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for err := range ch {
|
|
|
|
t.Errorf("#%d: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUDPZeroBytePayload(t *testing.T) {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "nacl", "plan9":
|
|
|
|
t.Skipf("not supported on %s", runtime.GOOS)
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := newLocalPacketListener("udp")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
for _, genericRead := range []bool{false, true} {
|
|
|
|
n, err := c.WriteTo(nil, c.LocalAddr())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if n != 0 {
|
|
|
|
t.Errorf("got %d; want 0", n)
|
|
|
|
}
|
|
|
|
c.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
|
|
|
|
var b [1]byte
|
|
|
|
if genericRead {
|
|
|
|
_, err = c.(Conn).Read(b[:])
|
2013-07-16 08:54:42 +02:00
|
|
|
} else {
|
2015-10-31 01:59:47 +01:00
|
|
|
_, _, err = c.ReadFrom(b[:])
|
|
|
|
}
|
|
|
|
switch err {
|
|
|
|
case nil: // ReadFrom succeeds
|
|
|
|
default: // Read may timeout, it depends on the platform
|
|
|
|
if nerr, ok := err.(Error); !ok || !nerr.Timeout() {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUDPZeroByteBuffer(t *testing.T) {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "nacl", "plan9":
|
|
|
|
t.Skipf("not supported on %s", runtime.GOOS)
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := newLocalPacketListener("udp")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
b := []byte("UDP ZERO BYTE BUFFER TEST")
|
|
|
|
for _, genericRead := range []bool{false, true} {
|
|
|
|
n, err := c.WriteTo(b, c.LocalAddr())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if n != len(b) {
|
|
|
|
t.Errorf("got %d; want %d", n, len(b))
|
|
|
|
}
|
|
|
|
c.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
|
|
|
|
if genericRead {
|
|
|
|
_, err = c.(Conn).Read(nil)
|
|
|
|
} else {
|
|
|
|
_, _, err = c.ReadFrom(nil)
|
|
|
|
}
|
|
|
|
switch err {
|
|
|
|
case nil: // ReadFrom succeeds
|
|
|
|
default: // Read may timeout, it depends on the platform
|
2016-07-22 20:15:38 +02:00
|
|
|
if nerr, ok := err.(Error); (!ok || !nerr.Timeout()) && runtime.GOOS != "windows" { // Windows returns WSAEMSGSIZ
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatal(err)
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
2012-11-21 08:03:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|