2008-09-17 14:49:23 -06: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.
|
|
|
|
|
2008-12-18 16:42:39 -07:00
|
|
|
package net
|
2008-09-17 14:49:23 -06:00
|
|
|
|
|
|
|
import (
|
2009-12-15 16:35:38 -07:00
|
|
|
"os"
|
|
|
|
"testing"
|
2008-09-17 14:49:23 -06:00
|
|
|
)
|
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
var tcpServerTests = []struct {
|
|
|
|
snet, saddr string // server endpoint
|
|
|
|
tnet, taddr string // target endpoint for client
|
2012-03-05 17:43:45 -07:00
|
|
|
}{
|
2015-04-21 07:10:09 -06:00
|
|
|
{snet: "tcp", saddr: ":0", tnet: "tcp", taddr: "127.0.0.1"},
|
|
|
|
{snet: "tcp", saddr: "0.0.0.0:0", tnet: "tcp", taddr: "127.0.0.1"},
|
|
|
|
{snet: "tcp", saddr: "[::ffff:0.0.0.0]:0", tnet: "tcp", taddr: "127.0.0.1"},
|
|
|
|
{snet: "tcp", saddr: "[::]:0", tnet: "tcp", taddr: "::1"},
|
2008-09-17 14:49:23 -06:00
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
{snet: "tcp", saddr: ":0", tnet: "tcp", taddr: "::1"},
|
|
|
|
{snet: "tcp", saddr: "0.0.0.0:0", tnet: "tcp", taddr: "::1"},
|
|
|
|
{snet: "tcp", saddr: "[::ffff:0.0.0.0]:0", tnet: "tcp", taddr: "::1"},
|
|
|
|
{snet: "tcp", saddr: "[::]:0", tnet: "tcp", taddr: "127.0.0.1"},
|
2012-03-05 17:43:45 -07:00
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
{snet: "tcp", saddr: ":0", tnet: "tcp4", taddr: "127.0.0.1"},
|
|
|
|
{snet: "tcp", saddr: "0.0.0.0:0", tnet: "tcp4", taddr: "127.0.0.1"},
|
|
|
|
{snet: "tcp", saddr: "[::ffff:0.0.0.0]:0", tnet: "tcp4", taddr: "127.0.0.1"},
|
|
|
|
{snet: "tcp", saddr: "[::]:0", tnet: "tcp6", taddr: "::1"},
|
2012-03-05 17:43:45 -07:00
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
{snet: "tcp", saddr: ":0", tnet: "tcp6", taddr: "::1"},
|
|
|
|
{snet: "tcp", saddr: "0.0.0.0:0", tnet: "tcp6", taddr: "::1"},
|
|
|
|
{snet: "tcp", saddr: "[::ffff:0.0.0.0]:0", tnet: "tcp6", taddr: "::1"},
|
|
|
|
{snet: "tcp", saddr: "[::]:0", tnet: "tcp4", taddr: "127.0.0.1"},
|
2012-03-05 17:43:45 -07:00
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
{snet: "tcp", saddr: "127.0.0.1:0", tnet: "tcp", taddr: "127.0.0.1"},
|
|
|
|
{snet: "tcp", saddr: "[::ffff:127.0.0.1]:0", tnet: "tcp", taddr: "127.0.0.1"},
|
|
|
|
{snet: "tcp", saddr: "[::1]:0", tnet: "tcp", taddr: "::1"},
|
2012-03-05 17:43:45 -07:00
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
{snet: "tcp4", saddr: ":0", tnet: "tcp4", taddr: "127.0.0.1"},
|
|
|
|
{snet: "tcp4", saddr: "0.0.0.0:0", tnet: "tcp4", taddr: "127.0.0.1"},
|
|
|
|
{snet: "tcp4", saddr: "[::ffff:0.0.0.0]:0", tnet: "tcp4", taddr: "127.0.0.1"},
|
2012-03-05 17:43:45 -07:00
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
{snet: "tcp4", saddr: "127.0.0.1:0", tnet: "tcp4", taddr: "127.0.0.1"},
|
2012-03-05 17:43:45 -07:00
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
{snet: "tcp6", saddr: ":0", tnet: "tcp6", taddr: "::1"},
|
|
|
|
{snet: "tcp6", saddr: "[::]:0", tnet: "tcp6", taddr: "::1"},
|
2012-03-05 17:43:45 -07:00
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
{snet: "tcp6", saddr: "[::1]:0", tnet: "tcp6", taddr: "::1"},
|
2012-03-05 17:43:45 -07:00
|
|
|
}
|
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
// TestTCPServer tests concurrent accept-read-write servers.
|
|
|
|
func TestTCPServer(t *testing.T) {
|
|
|
|
const N = 3
|
|
|
|
|
|
|
|
for i, tt := range tcpServerTests {
|
|
|
|
if !testableListenArgs(tt.snet, tt.saddr, tt.taddr) {
|
net: Make Listen(":port") use IPv6 when IPv4 is not supported.
When running an experimental kernel with IPv4 disabled, Listen(":port")
currently tries to create an AF_INET socket, and fails. Instead, it
should see !supportsIPv4, and use an AF_INET6 socket.
This sort of environment is quite esoteric at the moment, but I can
force the tests to fail on regular Linux using the following tweaks:
- net/net.go: supportsIPv4, supportsIPv6, supportsIPv4map = false, true, false
- net/sockopt_linux.go: ipv6only=true
- net/ipsock_posix.go: Revert this fix
- ./make.bash && ../bin/go test net
Also, make the arrows in server_test.go point to the left, because
server<-client is easier to read.
Fixes #12510
Change-Id: I0cc3b6b08d5e6908d2fbf8594f652ba19815aa4b
Reviewed-on: https://go-review.googlesource.com/14334
Run-TryBot: Paul Marks <pmarks@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-09-04 19:33:35 -06:00
|
|
|
t.Logf("skipping %s test", tt.snet+" "+tt.saddr+"<-"+tt.taddr)
|
2012-03-05 17:43:45 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
ln, err := Listen(tt.snet, tt.saddr)
|
|
|
|
if err != nil {
|
|
|
|
if perr := parseDialError(err); perr != nil {
|
|
|
|
t.Error(perr)
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
2012-03-05 17:43:45 -07:00
|
|
|
}
|
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
var lss []*localServer
|
|
|
|
var tpchs []chan error
|
2015-04-25 16:52:28 -06:00
|
|
|
defer func() {
|
|
|
|
for _, ls := range lss {
|
|
|
|
ls.teardown()
|
|
|
|
}
|
|
|
|
}()
|
2015-04-21 07:10:09 -06:00
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
ls, err := (&streamListener{Listener: ln}).newLocalServer()
|
2012-03-05 17:43:45 -07:00
|
|
|
if err != nil {
|
2015-04-21 07:10:09 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
lss = append(lss, ls)
|
|
|
|
tpchs = append(tpchs, make(chan error, 1))
|
|
|
|
}
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
ch := tpchs[i]
|
|
|
|
handler := func(ls *localServer, ln Listener) { transponder(ln, ch) }
|
|
|
|
if err := lss[i].buildup(handler); err != nil {
|
|
|
|
t.Fatal(err)
|
2012-03-05 17:43:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
var trchs []chan error
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
_, port, err := SplitHostPort(lss[i].Listener.Addr().String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
d := Dialer{Timeout: someTimeout}
|
|
|
|
c, err := d.Dial(tt.tnet, JoinHostPort(tt.taddr, port))
|
|
|
|
if err != nil {
|
|
|
|
if perr := parseDialError(err); perr != nil {
|
|
|
|
t.Error(perr)
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
trchs = append(trchs, make(chan error, 1))
|
|
|
|
go transceiver(c, []byte("TCP SERVER TEST"), trchs[i])
|
|
|
|
}
|
2012-03-05 17:43:45 -07:00
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
for _, ch := range trchs {
|
|
|
|
for err := range ch {
|
|
|
|
t.Errorf("#%d: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, ch := range tpchs {
|
|
|
|
for err := range ch {
|
|
|
|
t.Errorf("#%d: %v", i, err)
|
|
|
|
}
|
2012-03-05 17:43:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
var unixAndUnixpacketServerTests = []struct {
|
|
|
|
network, address string
|
2012-03-05 17:43:45 -07:00
|
|
|
}{
|
2015-04-21 07:10:09 -06:00
|
|
|
{"unix", testUnixAddr()},
|
|
|
|
{"unix", "@nettest/go/unix"},
|
|
|
|
|
|
|
|
{"unixpacket", testUnixAddr()},
|
|
|
|
{"unixpacket", "@nettest/go/unixpacket"},
|
2012-03-05 17:43:45 -07:00
|
|
|
}
|
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
// TestUnixAndUnixpacketServer tests concurrent accept-read-write
|
|
|
|
// servers
|
|
|
|
func TestUnixAndUnixpacketServer(t *testing.T) {
|
|
|
|
const N = 3
|
|
|
|
|
|
|
|
for i, tt := range unixAndUnixpacketServerTests {
|
|
|
|
if !testableListenArgs(tt.network, tt.address, "") {
|
2015-04-30 21:38:42 -06:00
|
|
|
t.Logf("skipping %s test", tt.network+" "+tt.address)
|
2014-04-02 04:43:39 -06:00
|
|
|
continue
|
|
|
|
}
|
2012-03-05 17:43:45 -07:00
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
ln, err := Listen(tt.network, tt.address)
|
|
|
|
if err != nil {
|
|
|
|
if perr := parseDialError(err); perr != nil {
|
|
|
|
t.Error(perr)
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
2008-09-17 14:49:23 -06:00
|
|
|
}
|
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
var lss []*localServer
|
|
|
|
var tpchs []chan error
|
2015-04-25 16:52:28 -06:00
|
|
|
defer func() {
|
|
|
|
for _, ls := range lss {
|
|
|
|
ls.teardown()
|
|
|
|
}
|
|
|
|
}()
|
2015-04-21 07:10:09 -06:00
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
ls, err := (&streamListener{Listener: ln}).newLocalServer()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2012-03-05 17:43:45 -07:00
|
|
|
}
|
2015-04-21 07:10:09 -06:00
|
|
|
lss = append(lss, ls)
|
|
|
|
tpchs = append(tpchs, make(chan error, 1))
|
2012-03-05 17:43:45 -07:00
|
|
|
}
|
2015-04-21 07:10:09 -06:00
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
ch := tpchs[i]
|
|
|
|
handler := func(ls *localServer, ln Listener) { transponder(ln, ch) }
|
|
|
|
if err := lss[i].buildup(handler); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2008-09-17 14:49:23 -06:00
|
|
|
}
|
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
var trchs []chan error
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
d := Dialer{Timeout: someTimeout}
|
|
|
|
c, err := d.Dial(lss[i].Listener.Addr().Network(), lss[i].Listener.Addr().String())
|
|
|
|
if err != nil {
|
|
|
|
if perr := parseDialError(err); perr != nil {
|
|
|
|
t.Error(perr)
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(c.LocalAddr().String())
|
|
|
|
defer c.Close()
|
|
|
|
trchs = append(trchs, make(chan error, 1))
|
|
|
|
go transceiver(c, []byte("UNIX AND UNIXPACKET SERVER TEST"), trchs[i])
|
|
|
|
}
|
2011-01-26 13:46:15 -07:00
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
for _, ch := range trchs {
|
|
|
|
for err := range ch {
|
|
|
|
t.Errorf("#%d: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, ch := range tpchs {
|
|
|
|
for err := range ch {
|
|
|
|
t.Errorf("#%d: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
2011-01-26 13:46:15 -07:00
|
|
|
}
|
2008-09-17 14:49:23 -06:00
|
|
|
}
|
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
var udpServerTests = []struct {
|
|
|
|
snet, saddr string // server endpoint
|
|
|
|
tnet, taddr string // target endpoint for client
|
|
|
|
dial bool // test with Dial
|
2012-03-05 17:43:45 -07:00
|
|
|
}{
|
2015-04-21 07:10:09 -06:00
|
|
|
{snet: "udp", saddr: ":0", tnet: "udp", taddr: "127.0.0.1"},
|
|
|
|
{snet: "udp", saddr: "0.0.0.0:0", tnet: "udp", taddr: "127.0.0.1"},
|
|
|
|
{snet: "udp", saddr: "[::ffff:0.0.0.0]:0", tnet: "udp", taddr: "127.0.0.1"},
|
|
|
|
{snet: "udp", saddr: "[::]:0", tnet: "udp", taddr: "::1"},
|
2012-03-05 17:43:45 -07:00
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
{snet: "udp", saddr: ":0", tnet: "udp", taddr: "::1"},
|
|
|
|
{snet: "udp", saddr: "0.0.0.0:0", tnet: "udp", taddr: "::1"},
|
|
|
|
{snet: "udp", saddr: "[::ffff:0.0.0.0]:0", tnet: "udp", taddr: "::1"},
|
|
|
|
{snet: "udp", saddr: "[::]:0", tnet: "udp", taddr: "127.0.0.1"},
|
2012-03-05 17:43:45 -07:00
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
{snet: "udp", saddr: ":0", tnet: "udp4", taddr: "127.0.0.1"},
|
|
|
|
{snet: "udp", saddr: "0.0.0.0:0", tnet: "udp4", taddr: "127.0.0.1"},
|
|
|
|
{snet: "udp", saddr: "[::ffff:0.0.0.0]:0", tnet: "udp4", taddr: "127.0.0.1"},
|
|
|
|
{snet: "udp", saddr: "[::]:0", tnet: "udp6", taddr: "::1"},
|
2012-03-05 17:43:45 -07:00
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
{snet: "udp", saddr: ":0", tnet: "udp6", taddr: "::1"},
|
|
|
|
{snet: "udp", saddr: "0.0.0.0:0", tnet: "udp6", taddr: "::1"},
|
|
|
|
{snet: "udp", saddr: "[::ffff:0.0.0.0]:0", tnet: "udp6", taddr: "::1"},
|
|
|
|
{snet: "udp", saddr: "[::]:0", tnet: "udp4", taddr: "127.0.0.1"},
|
2012-03-05 17:43:45 -07:00
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
{snet: "udp", saddr: "127.0.0.1:0", tnet: "udp", taddr: "127.0.0.1"},
|
|
|
|
{snet: "udp", saddr: "[::ffff:127.0.0.1]:0", tnet: "udp", taddr: "127.0.0.1"},
|
|
|
|
{snet: "udp", saddr: "[::1]:0", tnet: "udp", taddr: "::1"},
|
2012-03-05 17:43:45 -07:00
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
{snet: "udp4", saddr: ":0", tnet: "udp4", taddr: "127.0.0.1"},
|
|
|
|
{snet: "udp4", saddr: "0.0.0.0:0", tnet: "udp4", taddr: "127.0.0.1"},
|
|
|
|
{snet: "udp4", saddr: "[::ffff:0.0.0.0]:0", tnet: "udp4", taddr: "127.0.0.1"},
|
2012-03-05 17:43:45 -07:00
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
{snet: "udp4", saddr: "127.0.0.1:0", tnet: "udp4", taddr: "127.0.0.1"},
|
2012-03-05 17:43:45 -07:00
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
{snet: "udp6", saddr: ":0", tnet: "udp6", taddr: "::1"},
|
|
|
|
{snet: "udp6", saddr: "[::]:0", tnet: "udp6", taddr: "::1"},
|
2012-03-05 17:43:45 -07:00
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
{snet: "udp6", saddr: "[::1]:0", tnet: "udp6", taddr: "::1"},
|
2012-03-05 17:43:45 -07:00
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
{snet: "udp", saddr: "127.0.0.1:0", tnet: "udp", taddr: "127.0.0.1", dial: true},
|
2012-03-05 17:43:45 -07:00
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
{snet: "udp", saddr: "[::1]:0", tnet: "udp", taddr: "::1", dial: true},
|
2008-09-17 14:49:23 -06:00
|
|
|
}
|
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
func TestUDPServer(t *testing.T) {
|
|
|
|
for i, tt := range udpServerTests {
|
|
|
|
if !testableListenArgs(tt.snet, tt.saddr, tt.taddr) {
|
net: Make Listen(":port") use IPv6 when IPv4 is not supported.
When running an experimental kernel with IPv4 disabled, Listen(":port")
currently tries to create an AF_INET socket, and fails. Instead, it
should see !supportsIPv4, and use an AF_INET6 socket.
This sort of environment is quite esoteric at the moment, but I can
force the tests to fail on regular Linux using the following tweaks:
- net/net.go: supportsIPv4, supportsIPv6, supportsIPv4map = false, true, false
- net/sockopt_linux.go: ipv6only=true
- net/ipsock_posix.go: Revert this fix
- ./make.bash && ../bin/go test net
Also, make the arrows in server_test.go point to the left, because
server<-client is easier to read.
Fixes #12510
Change-Id: I0cc3b6b08d5e6908d2fbf8594f652ba19815aa4b
Reviewed-on: https://go-review.googlesource.com/14334
Run-TryBot: Paul Marks <pmarks@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-09-04 19:33:35 -06:00
|
|
|
t.Logf("skipping %s test", tt.snet+" "+tt.saddr+"<-"+tt.taddr)
|
2015-04-21 07:10:09 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
c1, err := ListenPacket(tt.snet, tt.saddr)
|
|
|
|
if err != nil {
|
|
|
|
if perr := parseDialError(err); perr != nil {
|
|
|
|
t.Error(perr)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2015-04-21 07:10:09 -06:00
|
|
|
t.Fatal(err)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2015-04-21 07:10:09 -06:00
|
|
|
|
|
|
|
ls, err := (&packetListener{PacketConn: c1}).newLocalServer()
|
2009-11-02 19:37:30 -07:00
|
|
|
if err != nil {
|
2015-04-21 07:10:09 -06:00
|
|
|
t.Fatal(err)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2015-04-21 07:10:09 -06:00
|
|
|
defer ls.teardown()
|
|
|
|
tpch := make(chan error, 1)
|
|
|
|
handler := func(ls *localPacketServer, c PacketConn) { packetTransponder(c, tpch) }
|
|
|
|
if err := ls.buildup(handler); err != nil {
|
|
|
|
t.Fatal(err)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
trch := make(chan error, 1)
|
|
|
|
_, port, err := SplitHostPort(ls.PacketConn.LocalAddr().String())
|
2012-03-05 17:43:45 -07:00
|
|
|
if err != nil {
|
2015-04-21 07:10:09 -06:00
|
|
|
t.Fatal(err)
|
2012-03-05 17:43:45 -07:00
|
|
|
}
|
2015-04-21 07:10:09 -06:00
|
|
|
if tt.dial {
|
|
|
|
d := Dialer{Timeout: someTimeout}
|
|
|
|
c2, err := d.Dial(tt.tnet, JoinHostPort(tt.taddr, port))
|
|
|
|
if err != nil {
|
|
|
|
if perr := parseDialError(err); perr != nil {
|
|
|
|
t.Error(perr)
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer c2.Close()
|
|
|
|
go transceiver(c2, []byte("UDP SERVER TEST"), trch)
|
|
|
|
} else {
|
|
|
|
c2, err := ListenPacket(tt.tnet, JoinHostPort(tt.taddr, "0"))
|
|
|
|
if err != nil {
|
|
|
|
if perr := parseDialError(err); perr != nil {
|
|
|
|
t.Error(perr)
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer c2.Close()
|
|
|
|
dst, err := ResolveUDPAddr(tt.tnet, JoinHostPort(tt.taddr, port))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
go packetTransceiver(c2, []byte("UDP SERVER TEST"), dst, trch)
|
2012-03-05 17:43:45 -07:00
|
|
|
}
|
2009-11-02 19:37:30 -07:00
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
for err := range trch {
|
|
|
|
t.Errorf("#%d: %v", i, err)
|
|
|
|
}
|
|
|
|
for err := range tpch {
|
|
|
|
t.Errorf("#%d: %v", i, err)
|
|
|
|
}
|
2010-02-22 21:38:56 -07:00
|
|
|
}
|
2015-04-21 07:10:09 -06:00
|
|
|
}
|
2012-03-05 17:43:45 -07:00
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
var unixgramServerTests = []struct {
|
|
|
|
saddr string // server endpoint
|
|
|
|
caddr string // client endpoint
|
|
|
|
dial bool // test with Dial
|
|
|
|
}{
|
|
|
|
{saddr: testUnixAddr(), caddr: testUnixAddr()},
|
|
|
|
{saddr: testUnixAddr(), caddr: testUnixAddr(), dial: true},
|
|
|
|
|
|
|
|
{saddr: "@nettest/go/unixgram/server", caddr: "@nettest/go/unixgram/client"},
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
func TestUnixgramServer(t *testing.T) {
|
|
|
|
for i, tt := range unixgramServerTests {
|
|
|
|
if !testableListenArgs("unixgram", tt.saddr, "") {
|
net: Make Listen(":port") use IPv6 when IPv4 is not supported.
When running an experimental kernel with IPv4 disabled, Listen(":port")
currently tries to create an AF_INET socket, and fails. Instead, it
should see !supportsIPv4, and use an AF_INET6 socket.
This sort of environment is quite esoteric at the moment, but I can
force the tests to fail on regular Linux using the following tweaks:
- net/net.go: supportsIPv4, supportsIPv6, supportsIPv4map = false, true, false
- net/sockopt_linux.go: ipv6only=true
- net/ipsock_posix.go: Revert this fix
- ./make.bash && ../bin/go test net
Also, make the arrows in server_test.go point to the left, because
server<-client is easier to read.
Fixes #12510
Change-Id: I0cc3b6b08d5e6908d2fbf8594f652ba19815aa4b
Reviewed-on: https://go-review.googlesource.com/14334
Run-TryBot: Paul Marks <pmarks@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-09-04 19:33:35 -06:00
|
|
|
t.Logf("skipping %s test", "unixgram "+tt.saddr+"<-"+tt.caddr)
|
2015-04-21 07:10:09 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
c1, err := ListenPacket("unixgram", tt.saddr)
|
2012-03-05 17:43:45 -07:00
|
|
|
if err != nil {
|
2015-04-21 07:10:09 -06:00
|
|
|
if perr := parseDialError(err); perr != nil {
|
|
|
|
t.Error(perr)
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
2012-03-05 17:43:45 -07:00
|
|
|
}
|
2015-04-21 07:10:09 -06:00
|
|
|
|
|
|
|
ls, err := (&packetListener{PacketConn: c1}).newLocalServer()
|
2012-03-05 17:43:45 -07:00
|
|
|
if err != nil {
|
2015-04-21 07:10:09 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer ls.teardown()
|
|
|
|
tpch := make(chan error, 1)
|
|
|
|
handler := func(ls *localPacketServer, c PacketConn) { packetTransponder(c, tpch) }
|
|
|
|
if err := ls.buildup(handler); err != nil {
|
|
|
|
t.Fatal(err)
|
2012-03-05 17:43:45 -07:00
|
|
|
}
|
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
trch := make(chan error, 1)
|
|
|
|
if tt.dial {
|
|
|
|
d := Dialer{Timeout: someTimeout, LocalAddr: &UnixAddr{Net: "unixgram", Name: tt.caddr}}
|
|
|
|
c2, err := d.Dial("unixgram", ls.PacketConn.LocalAddr().String())
|
|
|
|
if err != nil {
|
|
|
|
if perr := parseDialError(err); perr != nil {
|
|
|
|
t.Error(perr)
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(c2.LocalAddr().String())
|
|
|
|
defer c2.Close()
|
|
|
|
go transceiver(c2, []byte(c2.LocalAddr().String()), trch)
|
|
|
|
} else {
|
|
|
|
c2, err := ListenPacket("unixgram", tt.caddr)
|
|
|
|
if err != nil {
|
|
|
|
if perr := parseDialError(err); perr != nil {
|
|
|
|
t.Error(perr)
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(c2.LocalAddr().String())
|
|
|
|
defer c2.Close()
|
|
|
|
go packetTransceiver(c2, []byte("UNIXGRAM SERVER TEST"), ls.PacketConn.LocalAddr(), trch)
|
|
|
|
}
|
2012-03-05 17:43:45 -07:00
|
|
|
|
2015-04-21 07:10:09 -06:00
|
|
|
for err := range trch {
|
|
|
|
t.Errorf("#%d: %v", i, err)
|
|
|
|
}
|
|
|
|
for err := range tpch {
|
|
|
|
t.Errorf("#%d: %v", i, err)
|
|
|
|
}
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
}
|