2016-03-01 15:57:46 -07:00
|
|
|
// Copyright 2012 The Go Authors. All rights reserved.
|
2012-10-21 15:17:51 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-02-19 08:02:01 -07:00
|
|
|
// This file implements API tests across platforms and will never have a build
|
|
|
|
// tag.
|
|
|
|
|
|
|
|
package net
|
2012-10-21 15:17:51 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-01-27 19:38:05 -07: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
|
|
|
|
|
2015-03-26 08:26:45 -06:00
|
|
|
func packetConnTestData(t *testing.T, network string) ([]byte, func()) {
|
|
|
|
if !testableNetwork(network) {
|
|
|
|
return nil, func() { t.Logf("skipping %s test", network) }
|
2013-08-26 03:36:58 -06:00
|
|
|
}
|
2015-03-26 08:26:45 -06:00
|
|
|
return []byte("PACKETCONN TEST"), nil
|
2013-08-26 03:36:58 -06:00
|
|
|
}
|
|
|
|
|
2012-10-21 15:17:51 -06:00
|
|
|
var packetConnTests = []struct {
|
|
|
|
net string
|
2014-04-03 20:45:53 -06:00
|
|
|
addr1 string
|
|
|
|
addr2 string
|
2012-10-21 15:17:51 -06:00
|
|
|
}{
|
2014-04-03 20:45:53 -06:00
|
|
|
{"udp", "127.0.0.1:0", "127.0.0.1:0"},
|
|
|
|
{"unixgram", testUnixAddr(), testUnixAddr()},
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPacketConn(t *testing.T) {
|
2013-02-19 08:02:01 -07:00
|
|
|
closer := func(c PacketConn, net, addr1, addr2 string) {
|
2012-12-20 22:19:33 -07:00
|
|
|
c.Close()
|
|
|
|
switch net {
|
|
|
|
case "unixgram":
|
|
|
|
os.Remove(addr1)
|
|
|
|
os.Remove(addr2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 08:26:45 -06:00
|
|
|
for _, tt := range packetConnTests {
|
|
|
|
wb, skipOrFatalFn := packetConnTestData(t, tt.net)
|
2013-08-26 03:36:58 -06:00
|
|
|
if skipOrFatalFn != nil {
|
|
|
|
skipOrFatalFn()
|
2012-10-21 15:17:51 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-04-03 20:45:53 -06:00
|
|
|
c1, err := ListenPacket(tt.net, tt.addr1)
|
2012-10-21 15:17:51 -06:00
|
|
|
if err != nil {
|
2015-04-30 21:38:42 -06:00
|
|
|
t.Fatal(err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
2015-03-26 08:26:45 -06:00
|
|
|
defer closer(c1, tt.net, tt.addr1, tt.addr2)
|
2012-10-21 15:17:51 -06:00
|
|
|
c1.LocalAddr()
|
2015-02-15 10:35:18 -07:00
|
|
|
c1.SetDeadline(time.Now().Add(500 * time.Millisecond))
|
|
|
|
c1.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
|
|
|
|
c1.SetWriteDeadline(time.Now().Add(500 * time.Millisecond))
|
2012-10-21 15:17:51 -06:00
|
|
|
|
2014-04-03 20:45:53 -06:00
|
|
|
c2, err := ListenPacket(tt.net, tt.addr2)
|
2012-10-21 15:17:51 -06:00
|
|
|
if err != nil {
|
2015-04-30 21:38:42 -06:00
|
|
|
t.Fatal(err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
2015-03-26 08:26:45 -06:00
|
|
|
defer closer(c2, tt.net, tt.addr1, tt.addr2)
|
2012-10-21 15:17:51 -06:00
|
|
|
c2.LocalAddr()
|
2015-02-15 10:35:18 -07:00
|
|
|
c2.SetDeadline(time.Now().Add(500 * time.Millisecond))
|
|
|
|
c2.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
|
|
|
|
c2.SetWriteDeadline(time.Now().Add(500 * time.Millisecond))
|
|
|
|
rb2 := make([]byte, 128)
|
2012-10-21 15:17:51 -06:00
|
|
|
|
|
|
|
if _, err := c1.WriteTo(wb, c2.LocalAddr()); err != nil {
|
2015-04-30 21:38:42 -06:00
|
|
|
t.Fatal(err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
|
|
|
if _, _, err := c2.ReadFrom(rb2); err != nil {
|
2015-04-30 21:38:42 -06:00
|
|
|
t.Fatal(err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
|
|
|
if _, err := c2.WriteTo(wb, c1.LocalAddr()); err != nil {
|
2015-04-30 21:38:42 -06:00
|
|
|
t.Fatal(err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
|
|
|
rb1 := make([]byte, 128)
|
|
|
|
if _, _, err := c1.ReadFrom(rb1); err != nil {
|
2015-04-30 21:38:42 -06:00
|
|
|
t.Fatal(err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConnAndPacketConn(t *testing.T) {
|
2013-03-02 23:59:53 -07:00
|
|
|
closer := func(c PacketConn, net, addr1, addr2 string) {
|
|
|
|
c.Close()
|
|
|
|
switch net {
|
|
|
|
case "unixgram":
|
|
|
|
os.Remove(addr1)
|
|
|
|
os.Remove(addr2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 08:26:45 -06:00
|
|
|
for _, tt := range packetConnTests {
|
2012-10-21 15:17:51 -06:00
|
|
|
var wb []byte
|
2015-03-26 08:26:45 -06:00
|
|
|
wb, skipOrFatalFn := packetConnTestData(t, tt.net)
|
2013-08-26 03:36:58 -06:00
|
|
|
if skipOrFatalFn != nil {
|
|
|
|
skipOrFatalFn()
|
2012-10-21 15:17:51 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-04-03 20:45:53 -06:00
|
|
|
c1, err := ListenPacket(tt.net, tt.addr1)
|
2012-10-21 15:17:51 -06:00
|
|
|
if err != nil {
|
2015-04-30 21:38:42 -06:00
|
|
|
t.Fatal(err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
2015-03-26 08:26:45 -06:00
|
|
|
defer closer(c1, tt.net, tt.addr1, tt.addr2)
|
2012-10-21 15:17:51 -06:00
|
|
|
c1.LocalAddr()
|
2015-02-15 10:35:18 -07:00
|
|
|
c1.SetDeadline(time.Now().Add(500 * time.Millisecond))
|
|
|
|
c1.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
|
|
|
|
c1.SetWriteDeadline(time.Now().Add(500 * time.Millisecond))
|
2012-10-21 15:17:51 -06:00
|
|
|
|
2013-02-19 08:02:01 -07:00
|
|
|
c2, err := Dial(tt.net, c1.LocalAddr().String())
|
2012-10-21 15:17:51 -06:00
|
|
|
if err != nil {
|
2015-04-30 21:38:42 -06:00
|
|
|
t.Fatal(err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
2013-03-02 23:59:53 -07:00
|
|
|
defer c2.Close()
|
2012-10-21 15:17:51 -06:00
|
|
|
c2.LocalAddr()
|
|
|
|
c2.RemoteAddr()
|
2015-02-15 10:35:18 -07:00
|
|
|
c2.SetDeadline(time.Now().Add(500 * time.Millisecond))
|
|
|
|
c2.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
|
|
|
|
c2.SetWriteDeadline(time.Now().Add(500 * time.Millisecond))
|
2012-10-21 15:17:51 -06:00
|
|
|
|
|
|
|
if _, err := c2.Write(wb); err != nil {
|
2015-04-30 21:38:42 -06:00
|
|
|
t.Fatal(err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
|
|
|
rb1 := make([]byte, 128)
|
|
|
|
if _, _, err := c1.ReadFrom(rb1); err != nil {
|
2015-04-30 21:38:42 -06:00
|
|
|
t.Fatal(err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
2013-02-19 08:02:01 -07:00
|
|
|
var dst Addr
|
2015-03-26 08:26:45 -06:00
|
|
|
switch tt.net {
|
2013-03-02 23:59:53 -07:00
|
|
|
case "unixgram":
|
|
|
|
continue
|
|
|
|
default:
|
2012-10-21 15:17:51 -06:00
|
|
|
dst = c2.LocalAddr()
|
|
|
|
}
|
|
|
|
if _, err := c1.WriteTo(wb, dst); err != nil {
|
2015-04-30 21:38:42 -06:00
|
|
|
t.Fatal(err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
|
|
|
rb2 := make([]byte, 128)
|
|
|
|
if _, err := c2.Read(rb2); err != nil {
|
2015-04-30 21:38:42 -06:00
|
|
|
t.Fatal(err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|