1
0
mirror of https://github.com/golang/go synced 2024-11-18 03:04:45 -07:00

net: add missing File{Conn,Listener,PacketConn} read/write tests

At present, the series of File{Conn,Listener,PacketConn} APIs are the
only way to configure platform-specific socket options such as
SO_REUSE{ADDR,PORT}, TCP_FASTOPEN. This change adds missing test cases
that test read and write operations on connections created by File APIs
and removes redundant parameter tests which are already tested in
server_test.go.

Also adds comment on full stack test cases for IPConn.

Fixes #10730.

Change-Id: I67abb083781b602e876f72a6775a593c0f363c38
Reviewed-on: https://go-review.googlesource.com/17476
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Mikio Hara 2015-12-07 17:45:19 +09:00
parent a028892882
commit 6a1c2a5989

View File

@ -8,158 +8,222 @@ import (
"os"
"reflect"
"runtime"
"sync"
"testing"
)
type listenerFile interface {
Listener
File() (f *os.File, err error)
// The full stack test cases for IPConn have been moved to the
// following:
// golang.org/x/net/ipv4
// golang.org/x/net/ipv6
// golang.org/x/net/icmp
var fileConnTests = []struct {
network string
}{
{"tcp"},
{"udp"},
{"unix"},
{"unixpacket"},
}
type packetConnFile interface {
PacketConn
File() (f *os.File, err error)
}
func TestFileConn(t *testing.T) {
switch runtime.GOOS {
case "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
type connFile interface {
Conn
File() (f *os.File, err error)
}
for _, tt := range fileConnTests {
if !testableNetwork(tt.network) {
t.Logf("skipping %s test", tt.network)
continue
}
func testFileListener(t *testing.T, net, laddr string) {
l, err := Listen(net, laddr)
if err != nil {
t.Fatal(err)
}
defer l.Close()
lf := l.(listenerFile)
f, err := lf.File()
if err != nil {
t.Fatal(err)
}
c, err := FileListener(f)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(l.Addr(), c.Addr()) {
t.Fatalf("got %#v; want%#v", l.Addr(), c.Addr())
}
if err := c.Close(); err != nil {
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
var network, address string
switch tt.network {
case "udp":
c, err := newLocalPacketListener(tt.network)
if err != nil {
t.Fatal(err)
}
defer c.Close()
network = c.LocalAddr().Network()
address = c.LocalAddr().String()
default:
handler := func(ls *localServer, ln Listener) {
c, err := ln.Accept()
if err != nil {
return
}
defer c.Close()
var b [1]byte
c.Read(b[:])
}
ls, err := newLocalServer(tt.network)
if err != nil {
t.Fatal(err)
}
defer ls.teardown()
if err := ls.buildup(handler); err != nil {
t.Fatal(err)
}
network = ls.Listener.Addr().Network()
address = ls.Listener.Addr().String()
}
c1, err := Dial(network, address)
if err != nil {
if perr := parseDialError(err); perr != nil {
t.Error(perr)
}
t.Fatal(err)
}
addr := c1.LocalAddr()
var f *os.File
switch c1 := c1.(type) {
case *TCPConn:
f, err = c1.File()
case *UDPConn:
f, err = c1.File()
case *UnixConn:
f, err = c1.File()
}
if err := c1.Close(); err != nil {
if perr := parseCloseError(err); perr != nil {
t.Error(perr)
}
t.Error(err)
}
if err != nil {
if perr := parseCommonError(err); perr != nil {
t.Error(perr)
}
t.Fatal(err)
}
c2, err := FileConn(f)
if err := f.Close(); err != nil {
t.Error(err)
}
if err != nil {
if perr := parseCommonError(err); perr != nil {
t.Error(perr)
}
t.Fatal(err)
}
defer c2.Close()
if _, err := c2.Write([]byte("FILECONN TEST")); err != nil {
if perr := parseWriteError(err); perr != nil {
t.Error(perr)
}
t.Fatal(err)
}
if !reflect.DeepEqual(c2.LocalAddr(), addr) {
t.Fatalf("got %#v; want %#v", c2.LocalAddr(), addr)
}
}
}
var fileListenerTests = []struct {
net string
laddr string
network string
}{
{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"},
{net: "tcp", laddr: "127.0.0.1:0"},
{net: "tcp", laddr: "[::ffff:127.0.0.1]:0"},
{net: "tcp", laddr: "[::1]:0"},
{net: "tcp4", laddr: ":0"},
{net: "tcp4", laddr: "0.0.0.0:0"},
{net: "tcp4", laddr: "[::ffff:0.0.0.0]:0"},
{net: "tcp4", laddr: "127.0.0.1:0"},
{net: "tcp4", laddr: "[::ffff:127.0.0.1]:0"},
{net: "tcp6", laddr: ":0"},
{net: "tcp6", laddr: "[::]:0"},
{net: "tcp6", laddr: "[::1]:0"},
{net: "unix", laddr: "@gotest/net"},
{net: "unixpacket", laddr: "@gotest/net"},
{"tcp"},
{"unix"},
{"unixpacket"},
}
func TestFileListener(t *testing.T) {
switch runtime.GOOS {
case "nacl", "windows":
case "nacl", "plan9", "windows":
t.Skipf("not supported on %s", runtime.GOOS)
}
for _, tt := range fileListenerTests {
if !testableListenArgs(tt.net, tt.laddr, "") {
t.Logf("skipping %s test", tt.net+" "+tt.laddr)
if !testableNetwork(tt.network) {
t.Logf("skipping %s test", tt.network)
continue
}
testFileListener(t, tt.net, tt.laddr)
}
}
func testFilePacketConn(t *testing.T, pcf packetConnFile, listen bool) {
f, err := pcf.File()
if err != nil {
t.Fatal(err)
}
c, err := FilePacketConn(f)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(pcf.LocalAddr(), c.LocalAddr()) {
t.Fatalf("got %#v; want %#v", pcf.LocalAddr(), c.LocalAddr())
}
if listen {
if _, err := c.WriteTo([]byte{}, c.LocalAddr()); err != nil {
ln1, err := newLocalListener(tt.network)
if err != nil {
t.Fatal(err)
}
}
if err := c.Close(); err != nil {
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
}
}
switch tt.network {
case "unix", "unixpacket":
defer os.Remove(ln1.Addr().String())
}
addr := ln1.Addr()
func testFilePacketConnListen(t *testing.T, net, laddr string) {
l, err := ListenPacket(net, laddr)
if err != nil {
t.Fatal(err)
}
testFilePacketConn(t, l.(packetConnFile), true)
if err := l.Close(); err != nil {
t.Fatal(err)
}
}
var f *os.File
switch ln1 := ln1.(type) {
case *TCPListener:
f, err = ln1.File()
case *UnixListener:
f, err = ln1.File()
}
switch tt.network {
case "unix", "unixpacket":
defer ln1.Close() // UnixListener.Close calls syscall.Unlink internally
default:
if err := ln1.Close(); err != nil {
t.Error(err)
}
}
if err != nil {
if perr := parseCommonError(err); perr != nil {
t.Error(perr)
}
t.Fatal(err)
}
func testFilePacketConnDial(t *testing.T, net, raddr string) {
c, err := Dial(net, raddr)
if err != nil {
t.Fatal(err)
}
testFilePacketConn(t, c.(packetConnFile), false)
if err := c.Close(); err != nil {
t.Fatal(err)
ln2, err := FileListener(f)
if err := f.Close(); err != nil {
t.Error(err)
}
if err != nil {
if perr := parseCommonError(err); perr != nil {
t.Error(perr)
}
t.Fatal(err)
}
defer ln2.Close()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
c, err := Dial(ln2.Addr().Network(), ln2.Addr().String())
if err != nil {
if perr := parseDialError(err); perr != nil {
t.Error(perr)
}
t.Error(err)
return
}
c.Close()
}()
c, err := ln2.Accept()
if err != nil {
if perr := parseAcceptError(err); perr != nil {
t.Error(perr)
}
t.Fatal(err)
}
c.Close()
wg.Wait()
if !reflect.DeepEqual(ln2.Addr(), addr) {
t.Fatalf("got %#v; want %#v", ln2.Addr(), addr)
}
}
}
var filePacketConnTests = []struct {
net string
addr string
network string
}{
{net: "udp", addr: "127.0.0.1:0"},
{net: "udp", addr: "[::ffff:127.0.0.1]:0"},
{net: "udp", addr: "[::1]:0"},
{net: "udp4", addr: "127.0.0.1:0"},
{net: "udp4", addr: "[::ffff:127.0.0.1]:0"},
{net: "udp6", addr: "[::1]:0"},
// TODO(mikioh,bradfitz): reenable once 10730 is fixed
// {net: "ip4:icmp", addr: "127.0.0.1"},
{net: "unixgram", addr: "@gotest3/net"},
{"udp"},
{"unixgram"},
}
func TestFilePacketConn(t *testing.T) {
@ -169,25 +233,61 @@ func TestFilePacketConn(t *testing.T) {
}
for _, tt := range filePacketConnTests {
if !testableListenArgs(tt.net, tt.addr, "") {
t.Logf("skipping %s test", tt.net+" "+tt.addr)
if !testableNetwork(tt.network) {
t.Logf("skipping %s test", tt.network)
continue
}
if os.Getuid() != 0 && tt.net == "ip4:icmp" {
t.Log("skipping test; must be root")
continue
c1, err := newLocalPacketListener(tt.network)
if err != nil {
t.Fatal(err)
}
testFilePacketConnListen(t, tt.net, tt.addr)
switch tt.net {
case "udp", "udp4", "udp6":
host, _, err := SplitHostPort(tt.addr)
if err != nil {
t.Error(err)
continue
switch tt.network {
case "unixgram":
defer os.Remove(c1.LocalAddr().String())
}
addr := c1.LocalAddr()
var f *os.File
switch c1 := c1.(type) {
case *UDPConn:
f, err = c1.File()
case *UnixConn:
f, err = c1.File()
}
if err := c1.Close(); err != nil {
if perr := parseCloseError(err); perr != nil {
t.Error(perr)
}
testFilePacketConnDial(t, tt.net, JoinHostPort(host, "12345"))
case "ip4:icmp":
testFilePacketConnDial(t, tt.net, tt.addr)
t.Error(err)
}
if err != nil {
if perr := parseCommonError(err); perr != nil {
t.Error(perr)
}
t.Fatal(err)
}
c2, err := FilePacketConn(f)
if err := f.Close(); err != nil {
t.Error(err)
}
if err != nil {
if perr := parseCommonError(err); perr != nil {
t.Error(perr)
}
t.Fatal(err)
}
defer c2.Close()
if _, err := c2.WriteTo([]byte("FILEPACKETCONN TEST"), addr); err != nil {
if perr := parseWriteError(err); perr != nil {
t.Error(perr)
}
t.Fatal(err)
}
if !reflect.DeepEqual(c2.LocalAddr(), addr) {
t.Fatalf("got %#v; want %#v", c2.LocalAddr(), addr)
}
}
}