2012-03-27 23:13:14 +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"
|
2017-04-10 11:32:00 +00:00
|
|
|
"sync"
|
2012-03-27 23:13:14 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
// 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
|
2012-03-27 23:13:14 +00:00
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
var fileConnTests = []struct {
|
|
|
|
network string
|
|
|
|
}{
|
|
|
|
{"tcp"},
|
|
|
|
{"udp"},
|
|
|
|
{"unix"},
|
|
|
|
{"unixpacket"},
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
func TestFileConn(t *testing.T) {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "nacl", "plan9", "windows":
|
|
|
|
t.Skipf("not supported on %s", runtime.GOOS)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
for _, tt := range fileConnTests {
|
|
|
|
if !testableNetwork(tt.network) {
|
|
|
|
t.Logf("skipping %s test", tt.network)
|
|
|
|
continue
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
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()
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
c1, err := Dial(network, address)
|
|
|
|
if err != nil {
|
|
|
|
if perr := parseDialError(err); perr != nil {
|
|
|
|
t.Error(perr)
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
addr := c1.LocalAddr()
|
2014-09-21 17:33:12 +00:00
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
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)
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
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()
|
2014-09-21 17:33:12 +00:00
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
var fileListenerTests = []struct {
|
|
|
|
network string
|
|
|
|
}{
|
|
|
|
{"tcp"},
|
|
|
|
{"unix"},
|
|
|
|
{"unixpacket"},
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
|
2012-03-27 23:13:14 +00:00
|
|
|
func TestFileListener(t *testing.T) {
|
2014-09-21 17:33:12 +00:00
|
|
|
switch runtime.GOOS {
|
2017-04-10 11:32:00 +00:00
|
|
|
case "nacl", "plan9", "windows":
|
|
|
|
t.Skipf("not supported on %s", runtime.GOOS)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
|
|
|
|
for _, tt := range fileListenerTests {
|
2017-04-10 11:32:00 +00:00
|
|
|
if !testableNetwork(tt.network) {
|
|
|
|
t.Logf("skipping %s test", tt.network)
|
2014-09-21 17:33:12 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
|
|
|
|
ln1, err := newLocalListener(tt.network)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
switch tt.network {
|
|
|
|
case "unix", "unixpacket":
|
|
|
|
defer os.Remove(ln1.Addr().String())
|
|
|
|
}
|
|
|
|
addr := ln1.Addr()
|
2012-03-27 23:13:14 +00:00
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
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)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
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()
|
2012-03-27 23:13:14 +00:00
|
|
|
|
2017-04-10 11:32:00 +00:00
|
|
|
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)
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
var filePacketConnTests = []struct {
|
2017-04-10 11:32:00 +00:00
|
|
|
network string
|
2014-09-21 17:33:12 +00:00
|
|
|
}{
|
2017-04-10 11:32:00 +00:00
|
|
|
{"udp"},
|
|
|
|
{"unixgram"},
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
|
2012-03-27 23:13:14 +00:00
|
|
|
func TestFilePacketConn(t *testing.T) {
|
2014-09-21 17:33:12 +00:00
|
|
|
switch runtime.GOOS {
|
2015-08-28 15:33:40 +00:00
|
|
|
case "nacl", "plan9", "windows":
|
2017-04-10 11:32:00 +00:00
|
|
|
t.Skipf("not supported on %s", runtime.GOOS)
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
|
|
|
|
for _, tt := range filePacketConnTests {
|
2017-04-10 11:32:00 +00:00
|
|
|
if !testableNetwork(tt.network) {
|
|
|
|
t.Logf("skipping %s test", tt.network)
|
2014-09-21 17:33:12 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
|
|
|
|
c1, err := newLocalPacketListener(tt.network)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2015-08-28 15:33:40 +00:00
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if perr := parseCommonError(err); perr != nil {
|
|
|
|
t.Error(perr)
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
2017-04-10 11:32:00 +00:00
|
|
|
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)
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
}
|