Commit 9e26e06f authored by Mikio Hara's avatar Mikio Hara Committed by Brad Fitzpatrick

internal/netreflect: use of internal/nettest package

Change-Id: Ia77cabda5ae189dd91c3e528b9769ba93581f00d
Reviewed-on: https://go-review.googlesource.com/34732
Run-TryBot: Mikio Hara <mikioh.mikioh@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarMatt Layher <mdlayher@gmail.com>
Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 6c39fadc
......@@ -5,61 +5,15 @@
package netreflect_test
import (
"fmt"
"io/ioutil"
"net"
"os"
"runtime"
"testing"
"golang.org/x/net/internal/netreflect"
"golang.org/x/net/internal/nettest"
)
func localPath() string {
f, err := ioutil.TempFile("", "netreflect")
if err != nil {
panic(err)
}
path := f.Name()
f.Close()
os.Remove(path)
return path
}
func newLocalListener(network string) (net.Listener, error) {
switch network {
case "tcp":
if ln, err := net.Listen("tcp4", "127.0.0.1:0"); err == nil {
return ln, nil
}
return net.Listen("tcp6", "[::1]:0")
case "tcp4":
return net.Listen("tcp4", "127.0.0.1:0")
case "tcp6":
return net.Listen("tcp6", "[::1]:0")
case "unix", "unixpacket":
return net.Listen(network, localPath())
}
return nil, fmt.Errorf("%s is not supported", network)
}
func newLocalPacketListener(network string) (net.PacketConn, error) {
switch network {
case "udp":
if c, err := net.ListenPacket("udp4", "127.0.0.1:0"); err == nil {
return c, nil
}
return net.ListenPacket("udp6", "[::1]:0")
case "udp4":
return net.ListenPacket("udp4", "127.0.0.1:0")
case "udp6":
return net.ListenPacket("udp6", "[::1]:0")
case "unixgram":
return net.ListenPacket(network, localPath())
}
return nil, fmt.Errorf("%s is not supported", network)
}
func TestSocketOf(t *testing.T) {
for _, network := range []string{"tcp", "unix", "unixpacket"} {
switch runtime.GOOS {
......@@ -74,7 +28,7 @@ func TestSocketOf(t *testing.T) {
continue
}
}
ln, err := newLocalListener(network)
ln, err := nettest.NewLocalListener(network)
if err != nil {
t.Error(err)
continue
......@@ -109,7 +63,7 @@ func TestPacketSocketOf(t *testing.T) {
continue
}
}
c, err := newLocalPacketListener(network)
c, err := nettest.NewLocalPacketListener(network)
if err != nil {
t.Error(err)
continue
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment