2010-12-03 04:34:57 +00:00
|
|
|
// Copyright 2009 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 (
|
2011-12-03 02:17:34 +00:00
|
|
|
"io"
|
2012-10-23 04:31:11 +00:00
|
|
|
"os"
|
2011-12-02 19:34:41 +00:00
|
|
|
"runtime"
|
2010-12-03 04:34:57 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
func TestCloseRead(t *testing.T) {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "nacl", "plan9":
|
|
|
|
t.Skipf("not supported on %s", runtime.GOOS)
|
2011-12-02 19:34:41 +00:00
|
|
|
}
|
2015-10-31 00:59:47 +00:00
|
|
|
|
|
|
|
for _, network := range []string{"tcp", "unix", "unixpacket"} {
|
|
|
|
if !testableNetwork(network) {
|
|
|
|
t.Logf("skipping %s test", network)
|
|
|
|
continue
|
2011-10-26 23:57:58 +00:00
|
|
|
}
|
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
ln, err := newLocalListener(network)
|
2011-10-26 23:57:58 +00:00
|
|
|
if err != nil {
|
2015-10-31 00:59:47 +00:00
|
|
|
t.Fatal(err)
|
2011-10-26 23:57:58 +00:00
|
|
|
}
|
2015-10-31 00:59:47 +00:00
|
|
|
switch network {
|
|
|
|
case "unix", "unixpacket":
|
|
|
|
defer os.Remove(ln.Addr().String())
|
2011-10-26 23:57:58 +00:00
|
|
|
}
|
2015-10-31 00:59:47 +00:00
|
|
|
defer ln.Close()
|
2011-10-26 23:57:58 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
c, err := Dial(ln.Addr().Network(), ln.Addr().String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
switch network {
|
|
|
|
case "unix", "unixpacket":
|
|
|
|
defer os.Remove(c.LocalAddr().String())
|
|
|
|
}
|
|
|
|
defer c.Close()
|
2011-10-26 23:57:58 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
switch c := c.(type) {
|
|
|
|
case *TCPConn:
|
|
|
|
err = c.CloseRead()
|
|
|
|
case *UnixConn:
|
|
|
|
err = c.CloseRead()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if perr := parseCloseError(err); perr != nil {
|
|
|
|
t.Error(perr)
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var b [1]byte
|
|
|
|
n, err := c.Read(b[:])
|
|
|
|
if n != 0 || err == nil {
|
|
|
|
t.Fatalf("got (%d, %v); want (0, error)", n, err)
|
|
|
|
}
|
2011-10-26 23:57:58 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-02 16:38:43 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
func TestCloseWrite(t *testing.T) {
|
2012-10-23 04:31:11 +00:00
|
|
|
switch runtime.GOOS {
|
2015-10-31 00:59:47 +00:00
|
|
|
case "nacl", "plan9":
|
|
|
|
t.Skipf("not supported on %s", runtime.GOOS)
|
2012-10-23 04:31:11 +00:00
|
|
|
}
|
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
handler := func(ls *localServer, ln Listener) {
|
2012-10-23 04:31:11 +00:00
|
|
|
c, err := ln.Accept()
|
|
|
|
if err != nil {
|
2015-10-31 00:59:47 +00:00
|
|
|
t.Error(err)
|
2014-07-19 08:53:52 +00:00
|
|
|
return
|
2012-10-23 04:31:11 +00:00
|
|
|
}
|
2015-10-31 00:59:47 +00:00
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
var b [1]byte
|
|
|
|
n, err := c.Read(b[:])
|
2012-10-23 04:31:11 +00:00
|
|
|
if n != 0 || err != io.EOF {
|
2015-10-31 00:59:47 +00:00
|
|
|
t.Errorf("got (%d, %v); want (0, io.EOF)", n, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch c := c.(type) {
|
|
|
|
case *TCPConn:
|
|
|
|
err = c.CloseWrite()
|
|
|
|
case *UnixConn:
|
|
|
|
err = c.CloseWrite()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if perr := parseCloseError(err); perr != nil {
|
|
|
|
t.Error(perr)
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n, err = c.Write(b[:])
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("got (%d, %v); want (any, error)", n, err)
|
2014-07-19 08:53:52 +00:00
|
|
|
return
|
2012-10-23 04:31:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
for _, network := range []string{"tcp", "unix", "unixpacket"} {
|
|
|
|
if !testableNetwork(network) {
|
|
|
|
t.Logf("skipping %s test", network)
|
|
|
|
continue
|
|
|
|
}
|
2012-03-02 16:38:43 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
ls, err := newLocalServer(network)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer ls.teardown()
|
|
|
|
if err := ls.buildup(handler); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2012-03-02 16:38:43 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
c, err := Dial(ls.Listener.Addr().Network(), ls.Listener.Addr().String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
switch network {
|
|
|
|
case "unix", "unixpacket":
|
|
|
|
defer os.Remove(c.LocalAddr().String())
|
|
|
|
}
|
|
|
|
defer c.Close()
|
2012-03-02 16:38:43 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
switch c := c.(type) {
|
|
|
|
case *TCPConn:
|
|
|
|
err = c.CloseWrite()
|
|
|
|
case *UnixConn:
|
|
|
|
err = c.CloseWrite()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if perr := parseCloseError(err); perr != nil {
|
|
|
|
t.Error(perr)
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var b [1]byte
|
|
|
|
n, err := c.Read(b[:])
|
|
|
|
if n != 0 || err != io.EOF {
|
|
|
|
t.Fatalf("got (%d, %v); want (0, io.EOF)", n, err)
|
|
|
|
}
|
|
|
|
n, err = c.Write(b[:])
|
2012-03-02 16:38:43 +00:00
|
|
|
if err == nil {
|
2015-10-31 00:59:47 +00:00
|
|
|
t.Fatalf("got (%d, %v); want (any, error)", n, err)
|
|
|
|
}
|
2012-03-02 16:38:43 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-21 07:03:38 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
func TestConnClose(t *testing.T) {
|
|
|
|
for _, network := range []string{"tcp", "unix", "unixpacket"} {
|
|
|
|
if !testableNetwork(network) {
|
|
|
|
t.Logf("skipping %s test", network)
|
|
|
|
continue
|
|
|
|
}
|
2012-11-21 07:03:38 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
ln, err := newLocalListener(network)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
switch network {
|
|
|
|
case "unix", "unixpacket":
|
|
|
|
defer os.Remove(ln.Addr().String())
|
|
|
|
}
|
|
|
|
defer ln.Close()
|
2012-11-21 07:03:38 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
c, err := Dial(ln.Addr().Network(), ln.Addr().String())
|
2012-11-21 07:03:38 +00:00
|
|
|
if err != nil {
|
2015-10-31 00:59:47 +00:00
|
|
|
t.Fatal(err)
|
2012-11-21 07:03:38 +00:00
|
|
|
}
|
2015-10-31 00:59:47 +00:00
|
|
|
switch network {
|
|
|
|
case "unix", "unixpacket":
|
|
|
|
defer os.Remove(c.LocalAddr().String())
|
|
|
|
}
|
|
|
|
defer c.Close()
|
2012-11-21 07:03:38 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
if err := c.Close(); err != nil {
|
|
|
|
if perr := parseCloseError(err); perr != nil {
|
|
|
|
t.Error(perr)
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var b [1]byte
|
|
|
|
n, err := c.Read(b[:])
|
|
|
|
if n != 0 || err == nil {
|
|
|
|
t.Fatalf("got (%d, %v); want (0, error)", n, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-21 07:03:38 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
func TestListenerClose(t *testing.T) {
|
|
|
|
for _, network := range []string{"tcp", "unix", "unixpacket"} {
|
|
|
|
if !testableNetwork(network) {
|
|
|
|
t.Logf("skipping %s test", network)
|
|
|
|
continue
|
|
|
|
}
|
2012-11-21 07:03:38 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
ln, err := newLocalListener(network)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
switch network {
|
|
|
|
case "unix", "unixpacket":
|
|
|
|
defer os.Remove(ln.Addr().String())
|
|
|
|
}
|
|
|
|
defer ln.Close()
|
2012-11-21 07:03:38 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
if err := ln.Close(); err != nil {
|
|
|
|
if perr := parseCloseError(err); perr != nil {
|
|
|
|
t.Error(perr)
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
c, err := ln.Accept()
|
|
|
|
if err == nil {
|
|
|
|
c.Close()
|
|
|
|
t.Fatal("should fail")
|
|
|
|
}
|
2012-11-21 07:03:38 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-06 19:49:01 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
func TestPacketConnClose(t *testing.T) {
|
|
|
|
for _, network := range []string{"udp", "unixgram"} {
|
|
|
|
if !testableNetwork(network) {
|
|
|
|
t.Logf("skipping %s test", network)
|
|
|
|
continue
|
|
|
|
}
|
2013-11-06 19:49:01 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
c, err := newLocalPacketListener(network)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
switch network {
|
|
|
|
case "unixgram":
|
|
|
|
defer os.Remove(c.LocalAddr().String())
|
|
|
|
}
|
|
|
|
defer c.Close()
|
2013-11-06 19:49:01 +00:00
|
|
|
|
2015-10-31 00:59:47 +00:00
|
|
|
if err := c.Close(); err != nil {
|
|
|
|
if perr := parseCloseError(err); perr != nil {
|
|
|
|
t.Error(perr)
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var b [1]byte
|
|
|
|
n, _, err := c.ReadFrom(b[:])
|
|
|
|
if n != 0 || err == nil {
|
|
|
|
t.Fatalf("got (%d, %v); want (0, error)", n, err)
|
|
|
|
}
|
2013-11-06 19:49:01 +00:00
|
|
|
}
|
|
|
|
}
|