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) {
|
|
|
|
if net == "tcp" {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileListener(t *testing.T) {
|
2011-09-16 17:47:21 +02:00
|
|
|
if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
|
2011-03-30 17:33:16 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
testFileListener(t, "tcp", "127.0.0.1")
|
|
|
|
testFileListener(t, "tcp", "127.0.0.1")
|
2011-09-16 17:47:21 +02:00
|
|
|
if supportsIPv6 && supportsIPv4map {
|
2011-03-30 17:33:16 +02:00
|
|
|
testFileListener(t, "tcp", "[::ffff:127.0.0.1]")
|
|
|
|
testFileListener(t, "tcp", "127.0.0.1")
|
|
|
|
testFileListener(t, "tcp", "[::ffff:127.0.0.1]")
|
|
|
|
}
|
2012-01-25 21:56:26 +01:00
|
|
|
if runtime.GOOS == "linux" {
|
2011-03-30 17:33:16 +02:00
|
|
|
testFileListener(t, "unix", "@gotest/net")
|
|
|
|
testFileListener(t, "unixpacket", "@gotest/net")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
l, err := ListenPacket(net, laddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Listen failed: %v", err)
|
|
|
|
}
|
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) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilePacketConn(t *testing.T) {
|
2011-09-16 17:47:21 +02:00
|
|
|
if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
|
2011-03-30 17:33:16 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
testFilePacketConnListen(t, "udp", "127.0.0.1:0")
|
|
|
|
testFilePacketConnDial(t, "udp", "127.0.0.1:12345")
|
2011-09-16 17:47:21 +02:00
|
|
|
if supportsIPv6 {
|
2011-03-30 17:33:16 +02:00
|
|
|
testFilePacketConnListen(t, "udp", "[::1]:0")
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
if supportsIPv6 && supportsIPv4map {
|
2011-03-30 17:33:16 +02:00
|
|
|
testFilePacketConnDial(t, "udp", "[::ffff:127.0.0.1]:12345")
|
|
|
|
}
|
2012-01-25 21:56:26 +01:00
|
|
|
if runtime.GOOS == "linux" {
|
2011-03-30 17:33:16 +02:00
|
|
|
testFilePacketConnListen(t, "unixgram", "@gotest1/net")
|
|
|
|
}
|
|
|
|
}
|