2011-03-30 15:33:16 +00: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 02:17:34 +00:00
|
|
|
File() (f *os.File, err error)
|
2011-03-30 15:33:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type packetConnFile interface {
|
|
|
|
PacketConn
|
2011-12-03 02:17:34 +00:00
|
|
|
File() (f *os.File, err error)
|
2011-03-30 15:33:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type connFile interface {
|
|
|
|
Conn
|
2011-12-03 02:17:34 +00:00
|
|
|
File() (f *os.File, err error)
|
2011-03-30 15:33:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testFileListener(t *testing.T, net, laddr string) {
|
|
|
|
l, err := Listen(net, laddr)
|
|
|
|
if err != nil {
|
2015-10-31 00:59:47 +00:00
|
|
|
t.Fatal(err)
|
2011-03-30 15:33:16 +00:00
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
lf := l.(listenerFile)
|
|
|
|
f, err := lf.File()
|
|
|
|
if err != nil {
|
2015-10-31 00:59:47 +00:00
|
|
|
t.Fatal(err)
|
2011-03-30 15:33:16 +00:00
|
|
|
}
|
|
|
|
c, err := FileListener(f)
|
|
|
|
if err != nil {
|
2015-10-31 00:59:47 +00:00
|
|
|
t.Fatal(err)
|
2011-03-30 15:33:16 +00:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(l.Addr(), c.Addr()) {
|
2015-10-31 00:59:47 +00:00
|
|
|
t.Fatalf("got %#v; want%#v", l.Addr(), c.Addr())
|
2011-03-30 15:33:16 +00:00
|
|
|
}
|
|
|
|
if err := c.Close(); err != nil {
|
2015-10-31 00:59:47 +00:00
|
|
|
t.Fatal(err)
|
2011-03-30 15:33:16 +00:00
|
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
2015-10-31 00:59:47 +00:00
|
|
|
t.Fatal(err)
|
2011-03-30 15:33:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-30 21:27:11 +00:00
|
|
|
var fileListenerTests = []struct {
|
|
|
|
net string
|
|
|
|
laddr string
|
|
|
|
}{
|
2015-10-31 00:59:47 +00:00
|
|
|
{net: "tcp", laddr: ":0"},
|
|
|
|
{net: "tcp", laddr: "0.0.0.0:0"},
|
|
|
|
{net: "tcp", laddr: "[::ffff:0.0.0.0]:0"},
|
|
|
|
{net: "tcp", laddr: "[::]:0"},
|
2012-03-30 21:27:11 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
{net: "tcp", laddr: "127.0.0.1:0"},
|
|
|
|
{net: "tcp", laddr: "[::ffff:127.0.0.1]:0"},
|
|
|
|
{net: "tcp", laddr: "[::1]:0"},
|
2012-03-30 21:27:11 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
{net: "tcp4", laddr: ":0"},
|
|
|
|
{net: "tcp4", laddr: "0.0.0.0:0"},
|
|
|
|
{net: "tcp4", laddr: "[::ffff:0.0.0.0]:0"},
|
2012-03-30 21:27:11 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
{net: "tcp4", laddr: "127.0.0.1:0"},
|
|
|
|
{net: "tcp4", laddr: "[::ffff:127.0.0.1]:0"},
|
2012-03-30 21:27:11 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
{net: "tcp6", laddr: ":0"},
|
|
|
|
{net: "tcp6", laddr: "[::]:0"},
|
2012-03-30 21:27:11 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
{net: "tcp6", laddr: "[::1]:0"},
|
2012-03-30 21:27:11 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
{net: "unix", laddr: "@gotest/net"},
|
|
|
|
{net: "unixpacket", laddr: "@gotest/net"},
|
2012-03-30 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2011-03-30 15:33:16 +00:00
|
|
|
func TestFileListener(t *testing.T) {
|
2012-03-30 21:27:11 +00:00
|
|
|
switch runtime.GOOS {
|
2015-01-15 00:27:56 +00:00
|
|
|
case "nacl", "windows":
|
2015-10-31 00:59:47 +00:00
|
|
|
t.Skipf("not supported on %s", runtime.GOOS)
|
2011-03-30 15:33:16 +00:00
|
|
|
}
|
2012-03-30 21:27:11 +00:00
|
|
|
|
|
|
|
for _, tt := range fileListenerTests {
|
2015-10-31 00:59:47 +00:00
|
|
|
if !testableListenArgs(tt.net, tt.laddr, "") {
|
|
|
|
t.Logf("skipping %s test", tt.net+" "+tt.laddr)
|
2012-03-30 21:27:11 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
testFileListener(t, tt.net, tt.laddr)
|
2011-03-30 15:33:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-26 23:57:58 +00:00
|
|
|
func testFilePacketConn(t *testing.T, pcf packetConnFile, listen bool) {
|
2011-03-30 15:33:16 +00:00
|
|
|
f, err := pcf.File()
|
|
|
|
if err != nil {
|
2015-10-31 00:59:47 +00:00
|
|
|
t.Fatal(err)
|
2011-03-30 15:33:16 +00:00
|
|
|
}
|
|
|
|
c, err := FilePacketConn(f)
|
|
|
|
if err != nil {
|
2015-10-31 00:59:47 +00:00
|
|
|
t.Fatal(err)
|
2011-03-30 15:33:16 +00:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(pcf.LocalAddr(), c.LocalAddr()) {
|
2015-10-31 00:59:47 +00:00
|
|
|
t.Fatalf("got %#v; want %#v", pcf.LocalAddr(), c.LocalAddr())
|
2011-03-30 15:33:16 +00:00
|
|
|
}
|
2011-10-26 23:57:58 +00:00
|
|
|
if listen {
|
|
|
|
if _, err := c.WriteTo([]byte{}, c.LocalAddr()); err != nil {
|
2015-10-31 00:59:47 +00:00
|
|
|
t.Fatal(err)
|
2011-10-26 23:57:58 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-30 15:33:16 +00:00
|
|
|
if err := c.Close(); err != nil {
|
2015-10-31 00:59:47 +00:00
|
|
|
t.Fatal(err)
|
2011-03-30 15:33:16 +00:00
|
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
2015-10-31 00:59:47 +00:00
|
|
|
t.Fatal(err)
|
2011-03-30 15:33:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFilePacketConnListen(t *testing.T, net, laddr string) {
|
|
|
|
l, err := ListenPacket(net, laddr)
|
|
|
|
if err != nil {
|
2015-10-31 00:59:47 +00:00
|
|
|
t.Fatal(err)
|
2011-03-30 15:33:16 +00:00
|
|
|
}
|
2011-10-26 23:57:58 +00:00
|
|
|
testFilePacketConn(t, l.(packetConnFile), true)
|
2011-03-30 15:33:16 +00:00
|
|
|
if err := l.Close(); err != nil {
|
2015-10-31 00:59:47 +00:00
|
|
|
t.Fatal(err)
|
2011-03-30 15:33:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFilePacketConnDial(t *testing.T, net, raddr string) {
|
|
|
|
c, err := Dial(net, raddr)
|
|
|
|
if err != nil {
|
2015-10-31 00:59:47 +00:00
|
|
|
t.Fatal(err)
|
2011-03-30 15:33:16 +00:00
|
|
|
}
|
2011-10-26 23:57:58 +00:00
|
|
|
testFilePacketConn(t, c.(packetConnFile), false)
|
2011-03-30 15:33:16 +00:00
|
|
|
if err := c.Close(); err != nil {
|
2015-10-31 00:59:47 +00:00
|
|
|
t.Fatal(err)
|
2011-03-30 15:33:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-30 21:27:11 +00:00
|
|
|
var filePacketConnTests = []struct {
|
2015-10-31 00:59:47 +00:00
|
|
|
net string
|
|
|
|
addr string
|
2012-03-30 21:27:11 +00:00
|
|
|
}{
|
2015-10-31 00:59:47 +00:00
|
|
|
{net: "udp", addr: "127.0.0.1:0"},
|
|
|
|
{net: "udp", addr: "[::ffff:127.0.0.1]:0"},
|
|
|
|
{net: "udp", addr: "[::1]:0"},
|
2012-03-30 21:27:11 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
{net: "udp4", addr: "127.0.0.1:0"},
|
|
|
|
{net: "udp4", addr: "[::ffff:127.0.0.1]:0"},
|
2012-03-30 21:27:11 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
{net: "udp6", addr: "[::1]:0"},
|
2012-03-30 21:27:11 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
// TODO(mikioh,bradfitz): reenable once 10730 is fixed
|
|
|
|
// {net: "ip4:icmp", addr: "127.0.0.1"},
|
2014-06-06 22:37:27 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
{net: "unixgram", addr: "@gotest3/net"},
|
2012-03-30 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2011-03-30 15:33:16 +00:00
|
|
|
func TestFilePacketConn(t *testing.T) {
|
2012-03-30 21:27:11 +00:00
|
|
|
switch runtime.GOOS {
|
2014-07-19 08:53:52 +00:00
|
|
|
case "nacl", "plan9", "windows":
|
2015-10-31 00:59:47 +00:00
|
|
|
t.Skipf("not supported on %s", runtime.GOOS)
|
2011-03-30 15:33:16 +00:00
|
|
|
}
|
2012-03-30 21:27:11 +00:00
|
|
|
|
|
|
|
for _, tt := range filePacketConnTests {
|
2015-10-31 00:59:47 +00:00
|
|
|
if !testableListenArgs(tt.net, tt.addr, "") {
|
|
|
|
t.Logf("skipping %s test", tt.net+" "+tt.addr)
|
2012-03-30 21:27:11 +00:00
|
|
|
continue
|
|
|
|
}
|
2014-06-06 22:37:27 +00:00
|
|
|
if os.Getuid() != 0 && tt.net == "ip4:icmp" {
|
|
|
|
t.Log("skipping test; must be root")
|
|
|
|
continue
|
|
|
|
}
|
2012-03-30 21:27:11 +00:00
|
|
|
testFilePacketConnListen(t, tt.net, tt.addr)
|
2015-10-31 00:59:47 +00:00
|
|
|
switch tt.net {
|
|
|
|
case "udp", "udp4", "udp6":
|
|
|
|
host, _, err := SplitHostPort(tt.addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
2012-03-30 21:27:11 +00:00
|
|
|
}
|
2015-10-31 00:59:47 +00:00
|
|
|
testFilePacketConnDial(t, tt.net, JoinHostPort(host, "12345"))
|
|
|
|
case "ip4:icmp":
|
|
|
|
testFilePacketConnDial(t, tt.net, tt.addr)
|
2012-03-30 21:27:11 +00:00
|
|
|
}
|
2011-03-30 15:33:16 +00:00
|
|
|
}
|
|
|
|
}
|