2011-03-30 17:33:16 +02:00
|
|
|
// Copyright 2011 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 (
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type listenerFile interface {
|
|
|
|
Listener
|
2011-12-03 03:17:34 +01:00
|
|
|
File() (f *os.File, err error)
|
2011-03-30 17:33:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type packetConnFile interface {
|
|
|
|
PacketConn
|
2011-12-03 03:17:34 +01:00
|
|
|
File() (f *os.File, err error)
|
2011-03-30 17:33:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type connFile interface {
|
|
|
|
Conn
|
2011-12-03 03:17:34 +01:00
|
|
|
File() (f *os.File, err error)
|
2011-03-30 17:33:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func testFileListener(t *testing.T, net, laddr string) {
|
2012-03-30 23:27:11 +02:00
|
|
|
switch net {
|
|
|
|
case "tcp", "tcp4", "tcp6":
|
2011-03-30 17:33:16 +02:00
|
|
|
laddr += ":0" // any available port
|
|
|
|
}
|
|
|
|
l, err := Listen(net, laddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Listen failed: %v", err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
lf := l.(listenerFile)
|
|
|
|
f, err := lf.File()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("File failed: %v", err)
|
|
|
|
}
|
|
|
|
c, err := FileListener(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("FileListener failed: %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(l.Addr(), c.Addr()) {
|
|
|
|
t.Fatalf("Addrs not equal: %#v != %#v", l.Addr(), c.Addr())
|
|
|
|
}
|
|
|
|
if err := c.Close(); err != nil {
|
|
|
|
t.Fatalf("Close failed: %v", err)
|
|
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
t.Fatalf("Close failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-30 23:27:11 +02:00
|
|
|
var fileListenerTests = []struct {
|
|
|
|
net string
|
|
|
|
laddr string
|
|
|
|
ipv6 bool // test with underlying AF_INET6 socket
|
|
|
|
linux bool // test with abstract unix domain socket, a Linux-ism
|
|
|
|
}{
|
|
|
|
{net: "tcp", laddr: ""},
|
|
|
|
{net: "tcp", laddr: "0.0.0.0"},
|
|
|
|
{net: "tcp", laddr: "[::ffff:0.0.0.0]"},
|
|
|
|
{net: "tcp", laddr: "[::]", ipv6: true},
|
|
|
|
|
|
|
|
{net: "tcp", laddr: "127.0.0.1"},
|
|
|
|
{net: "tcp", laddr: "[::ffff:127.0.0.1]"},
|
|
|
|
{net: "tcp", laddr: "[::1]", ipv6: true},
|
|
|
|
|
|
|
|
{net: "tcp4", laddr: ""},
|
|
|
|
{net: "tcp4", laddr: "0.0.0.0"},
|
|
|
|
{net: "tcp4", laddr: "[::ffff:0.0.0.0]"},
|
|
|
|
|
|
|
|
{net: "tcp4", laddr: "127.0.0.1"},
|
|
|
|
{net: "tcp4", laddr: "[::ffff:127.0.0.1]"},
|
|
|
|
|
|
|
|
{net: "tcp6", laddr: "", ipv6: true},
|
|
|
|
{net: "tcp6", laddr: "[::]", ipv6: true},
|
|
|
|
|
|
|
|
{net: "tcp6", laddr: "[::1]", ipv6: true},
|
|
|
|
|
|
|
|
{net: "unix", laddr: "@gotest/net", linux: true},
|
|
|
|
{net: "unixpacket", laddr: "@gotest/net", linux: true},
|
|
|
|
}
|
|
|
|
|
2011-03-30 17:33:16 +02:00
|
|
|
func TestFileListener(t *testing.T) {
|
2012-03-30 23:27:11 +02:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "plan9", "windows":
|
2013-01-29 21:52:43 +01:00
|
|
|
t.Skipf("skipping test on %q", runtime.GOOS)
|
2011-03-30 17:33:16 +02:00
|
|
|
}
|
2012-03-30 23:27:11 +02:00
|
|
|
|
|
|
|
for _, tt := range fileListenerTests {
|
|
|
|
if skipServerTest(tt.net, "unix", tt.laddr, tt.ipv6, false, tt.linux) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if skipServerTest(tt.net, "unixpacket", tt.laddr, tt.ipv6, false, tt.linux) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
testFileListener(t, tt.net, tt.laddr)
|
2011-03-30 17:33:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
func testFilePacketConn(t *testing.T, pcf packetConnFile, listen bool) {
|
2011-03-30 17:33:16 +02:00
|
|
|
f, err := pcf.File()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("File failed: %v", err)
|
|
|
|
}
|
|
|
|
c, err := FilePacketConn(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("FilePacketConn failed: %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(pcf.LocalAddr(), c.LocalAddr()) {
|
|
|
|
t.Fatalf("LocalAddrs not equal: %#v != %#v", pcf.LocalAddr(), c.LocalAddr())
|
|
|
|
}
|
2011-10-27 01:57:58 +02:00
|
|
|
if listen {
|
|
|
|
if _, err := c.WriteTo([]byte{}, c.LocalAddr()); err != nil {
|
|
|
|
t.Fatalf("WriteTo failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2011-03-30 17:33:16 +02:00
|
|
|
if err := c.Close(); err != nil {
|
|
|
|
t.Fatalf("Close failed: %v", err)
|
|
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
t.Fatalf("Close failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFilePacketConnListen(t *testing.T, net, laddr string) {
|
2012-03-30 23:27:11 +02:00
|
|
|
switch net {
|
|
|
|
case "udp", "udp4", "udp6":
|
|
|
|
laddr += ":0" // any available port
|
|
|
|
}
|
2011-03-30 17:33:16 +02:00
|
|
|
l, err := ListenPacket(net, laddr)
|
|
|
|
if err != nil {
|
2012-03-30 23:27:11 +02:00
|
|
|
t.Fatalf("ListenPacket failed: %v", err)
|
2011-03-30 17:33:16 +02:00
|
|
|
}
|
2011-10-27 01:57:58 +02:00
|
|
|
testFilePacketConn(t, l.(packetConnFile), true)
|
2011-03-30 17:33:16 +02:00
|
|
|
if err := l.Close(); err != nil {
|
|
|
|
t.Fatalf("Close failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFilePacketConnDial(t *testing.T, net, raddr string) {
|
2012-03-30 23:27:11 +02:00
|
|
|
switch net {
|
|
|
|
case "udp", "udp4", "udp6":
|
|
|
|
raddr += ":12345"
|
|
|
|
}
|
2011-03-30 17:33:16 +02:00
|
|
|
c, err := Dial(net, raddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Dial failed: %v", err)
|
|
|
|
}
|
2011-10-27 01:57:58 +02:00
|
|
|
testFilePacketConn(t, c.(packetConnFile), false)
|
2011-03-30 17:33:16 +02:00
|
|
|
if err := c.Close(); err != nil {
|
|
|
|
t.Fatalf("Close failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-30 23:27:11 +02:00
|
|
|
var filePacketConnTests = []struct {
|
|
|
|
net string
|
|
|
|
addr string
|
|
|
|
ipv6 bool // test with underlying AF_INET6 socket
|
|
|
|
linux bool // test with abstract unix domain socket, a Linux-ism
|
|
|
|
}{
|
|
|
|
{net: "udp", addr: "127.0.0.1"},
|
|
|
|
{net: "udp", addr: "[::ffff:127.0.0.1]"},
|
|
|
|
{net: "udp", addr: "[::1]", ipv6: true},
|
|
|
|
|
|
|
|
{net: "udp4", addr: "127.0.0.1"},
|
|
|
|
{net: "udp4", addr: "[::ffff:127.0.0.1]"},
|
|
|
|
|
|
|
|
{net: "udp6", addr: "[::1]", ipv6: true},
|
|
|
|
|
|
|
|
{net: "unixgram", addr: "@gotest3/net", linux: true},
|
|
|
|
}
|
|
|
|
|
2011-03-30 17:33:16 +02:00
|
|
|
func TestFilePacketConn(t *testing.T) {
|
2012-03-30 23:27:11 +02:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "plan9", "windows":
|
2013-01-29 21:52:43 +01:00
|
|
|
t.Skipf("skipping test on %q", runtime.GOOS)
|
2011-03-30 17:33:16 +02:00
|
|
|
}
|
2012-03-30 23:27:11 +02:00
|
|
|
|
|
|
|
for _, tt := range filePacketConnTests {
|
|
|
|
if skipServerTest(tt.net, "unixgram", tt.addr, tt.ipv6, false, tt.linux) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
testFilePacketConnListen(t, tt.net, tt.addr)
|
|
|
|
switch tt.addr {
|
|
|
|
case "", "0.0.0.0", "[::ffff:0.0.0.0]", "[::]":
|
|
|
|
default:
|
|
|
|
if tt.net != "unixgram" {
|
|
|
|
testFilePacketConnDial(t, tt.net, tt.addr)
|
|
|
|
}
|
|
|
|
}
|
2011-03-30 17:33:16 +02:00
|
|
|
}
|
|
|
|
}
|