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"
|
2016-02-03 22:58:02 +01:00
|
|
|
"sync"
|
2011-03-30 17:33:16 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-02-03 22:58:02 +01: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
|
2011-03-30 17:33:16 +02:00
|
|
|
|
2016-02-03 22:58:02 +01:00
|
|
|
var fileConnTests = []struct {
|
|
|
|
network string
|
|
|
|
}{
|
|
|
|
{"tcp"},
|
|
|
|
{"udp"},
|
|
|
|
{"unix"},
|
|
|
|
{"unixpacket"},
|
2011-03-30 17:33:16 +02:00
|
|
|
}
|
|
|
|
|
2016-02-03 22:58:02 +01:00
|
|
|
func TestFileConn(t *testing.T) {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "nacl", "plan9", "windows":
|
|
|
|
t.Skipf("not supported on %s", runtime.GOOS)
|
2011-03-30 17:33:16 +02:00
|
|
|
}
|
|
|
|
|
2016-02-03 22:58:02 +01:00
|
|
|
for _, tt := range fileConnTests {
|
|
|
|
if !testableNetwork(tt.network) {
|
|
|
|
t.Logf("skipping %s test", tt.network)
|
|
|
|
continue
|
|
|
|
}
|
2012-03-30 23:27:11 +02:00
|
|
|
|
2016-02-03 22:58:02 +01: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()
|
|
|
|
}
|
2012-03-30 23:27:11 +02:00
|
|
|
|
2016-02-03 22:58:02 +01:00
|
|
|
c1, err := Dial(network, address)
|
|
|
|
if err != nil {
|
|
|
|
if perr := parseDialError(err); perr != nil {
|
|
|
|
t.Error(perr)
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
addr := c1.LocalAddr()
|
2012-03-30 23:27:11 +02:00
|
|
|
|
2016-02-03 22:58:02 +01: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 {
|
2017-09-14 19:11:35 +02:00
|
|
|
if perr := parseCloseError(err, false); perr != nil {
|
2016-02-03 22:58:02 +01:00
|
|
|
t.Error(perr)
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if perr := parseCommonError(err); perr != nil {
|
|
|
|
t.Error(perr)
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2012-03-30 23:27:11 +02:00
|
|
|
|
2016-02-03 22:58:02 +01: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()
|
2012-03-30 23:27:11 +02:00
|
|
|
|
2016-02-03 22:58:02 +01: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-30 23:27:11 +02:00
|
|
|
|
2016-02-03 22:58:02 +01:00
|
|
|
var fileListenerTests = []struct {
|
|
|
|
network string
|
|
|
|
}{
|
|
|
|
{"tcp"},
|
|
|
|
{"unix"},
|
|
|
|
{"unixpacket"},
|
2012-03-30 23:27:11 +02:00
|
|
|
}
|
|
|
|
|
2011-03-30 17:33:16 +02:00
|
|
|
func TestFileListener(t *testing.T) {
|
2012-03-30 23:27:11 +02:00
|
|
|
switch runtime.GOOS {
|
2016-02-03 22:58:02 +01:00
|
|
|
case "nacl", "plan9", "windows":
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Skipf("not supported on %s", runtime.GOOS)
|
2011-03-30 17:33:16 +02:00
|
|
|
}
|
2012-03-30 23:27:11 +02:00
|
|
|
|
|
|
|
for _, tt := range fileListenerTests {
|
2016-02-03 22:58:02 +01:00
|
|
|
if !testableNetwork(tt.network) {
|
|
|
|
t.Logf("skipping %s test", tt.network)
|
2012-03-30 23:27:11 +02:00
|
|
|
continue
|
|
|
|
}
|
2011-03-30 17:33:16 +02:00
|
|
|
|
2016-02-03 22:58:02 +01:00
|
|
|
ln1, err := newLocalListener(tt.network)
|
|
|
|
if err != nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatal(err)
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
2016-02-03 22:58:02 +01:00
|
|
|
switch tt.network {
|
|
|
|
case "unix", "unixpacket":
|
|
|
|
defer os.Remove(ln1.Addr().String())
|
|
|
|
}
|
|
|
|
addr := ln1.Addr()
|
2011-03-30 17:33:16 +02:00
|
|
|
|
2016-02-03 22:58:02 +01: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)
|
|
|
|
}
|
2011-03-30 17:33:16 +02:00
|
|
|
|
2016-02-03 22:58:02 +01: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()
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2011-03-30 17:33:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-30 23:27:11 +02:00
|
|
|
var filePacketConnTests = []struct {
|
2016-02-03 22:58:02 +01:00
|
|
|
network string
|
2012-03-30 23:27:11 +02:00
|
|
|
}{
|
2016-02-03 22:58:02 +01:00
|
|
|
{"udp"},
|
|
|
|
{"unixgram"},
|
2012-03-30 23:27:11 +02:00
|
|
|
}
|
|
|
|
|
2011-03-30 17:33:16 +02:00
|
|
|
func TestFilePacketConn(t *testing.T) {
|
2012-03-30 23:27:11 +02:00
|
|
|
switch runtime.GOOS {
|
2014-07-19 10:53:52 +02:00
|
|
|
case "nacl", "plan9", "windows":
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Skipf("not supported on %s", runtime.GOOS)
|
2011-03-30 17:33:16 +02:00
|
|
|
}
|
2012-03-30 23:27:11 +02:00
|
|
|
|
|
|
|
for _, tt := range filePacketConnTests {
|
2016-02-03 22:58:02 +01:00
|
|
|
if !testableNetwork(tt.network) {
|
|
|
|
t.Logf("skipping %s test", tt.network)
|
2012-03-30 23:27:11 +02:00
|
|
|
continue
|
|
|
|
}
|
2016-02-03 22:58:02 +01:00
|
|
|
|
|
|
|
c1, err := newLocalPacketListener(tt.network)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2014-06-07 00:37:27 +02:00
|
|
|
}
|
2016-02-03 22:58:02 +01: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 {
|
2017-09-14 19:11:35 +02:00
|
|
|
if perr := parseCloseError(err, false); perr != nil {
|
2016-02-03 22:58:02 +01:00
|
|
|
t.Error(perr)
|
2012-03-30 23:27:11 +02:00
|
|
|
}
|
2016-02-03 22:58:02 +01:00
|
|
|
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)
|
2012-03-30 23:27:11 +02:00
|
|
|
}
|
2011-03-30 17:33:16 +02:00
|
|
|
}
|
|
|
|
}
|