2012-10-21 15:17:51 -06:00
|
|
|
// Copyright 2012 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.
|
|
|
|
|
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"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"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
|
|
|
|
|
2013-08-26 03:36:58 -06:00
|
|
|
func packetConnTestData(t *testing.T, net string, i int) ([]byte, func()) {
|
|
|
|
switch net {
|
|
|
|
case "udp":
|
|
|
|
return []byte("UDP PACKETCONN TEST"), nil
|
|
|
|
case "unixgram":
|
|
|
|
switch runtime.GOOS {
|
2014-05-20 10:10:19 -06:00
|
|
|
case "nacl", "plan9", "windows":
|
2013-08-26 03:36:58 -06:00
|
|
|
return nil, func() {
|
|
|
|
t.Logf("skipping %q test on %q", net, runtime.GOOS)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return []byte("UNIXGRAM PACKETCONN TEST"), nil
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, func() {
|
|
|
|
t.Logf("skipping %q test", net)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-02 23:59:53 -07:00
|
|
|
for i, tt := range packetConnTests {
|
2012-10-21 15:17:51 -06:00
|
|
|
netstr := strings.Split(tt.net, ":")
|
2013-08-26 03:36:58 -06:00
|
|
|
wb, skipOrFatalFn := packetConnTestData(t, netstr[0], i)
|
|
|
|
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 {
|
2013-03-02 23:59:53 -07:00
|
|
|
t.Fatalf("ListenPacket failed: %v", err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
2014-04-03 20:45:53 -06:00
|
|
|
defer closer(c1, netstr[0], tt.addr1, tt.addr2)
|
2012-10-21 15:17:51 -06:00
|
|
|
c1.LocalAddr()
|
|
|
|
c1.SetDeadline(time.Now().Add(100 * time.Millisecond))
|
|
|
|
c1.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
|
|
|
|
c1.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
|
|
|
|
|
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 {
|
2013-03-02 23:59:53 -07:00
|
|
|
t.Fatalf("ListenPacket failed: %v", err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
2014-04-03 20:45:53 -06:00
|
|
|
defer closer(c2, netstr[0], tt.addr1, tt.addr2)
|
2012-10-21 15:17:51 -06:00
|
|
|
c2.LocalAddr()
|
|
|
|
c2.SetDeadline(time.Now().Add(100 * time.Millisecond))
|
|
|
|
c2.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
|
|
|
|
c2.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
|
|
|
|
|
|
|
|
if _, err := c1.WriteTo(wb, c2.LocalAddr()); err != nil {
|
2013-03-02 23:59:53 -07:00
|
|
|
t.Fatalf("PacketConn.WriteTo failed: %v", err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
|
|
|
rb2 := make([]byte, 128)
|
|
|
|
if _, _, err := c2.ReadFrom(rb2); err != nil {
|
2013-03-02 23:59:53 -07:00
|
|
|
t.Fatalf("PacketConn.ReadFrom failed: %v", err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
|
|
|
if _, err := c2.WriteTo(wb, c1.LocalAddr()); err != nil {
|
2013-03-02 23:59:53 -07:00
|
|
|
t.Fatalf("PacketConn.WriteTo failed: %v", err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
|
|
|
rb1 := make([]byte, 128)
|
|
|
|
if _, _, err := c1.ReadFrom(rb1); err != nil {
|
2013-03-02 23:59:53 -07:00
|
|
|
t.Fatalf("PacketConn.ReadFrom failed: %v", 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range packetConnTests {
|
2012-10-21 15:17:51 -06:00
|
|
|
var wb []byte
|
|
|
|
netstr := strings.Split(tt.net, ":")
|
2013-08-26 03:36:58 -06:00
|
|
|
wb, skipOrFatalFn := packetConnTestData(t, netstr[0], i)
|
|
|
|
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 {
|
2013-03-02 23:59:53 -07:00
|
|
|
t.Fatalf("ListenPacket failed: %v", err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
2014-04-03 20:45:53 -06:00
|
|
|
defer closer(c1, netstr[0], tt.addr1, tt.addr2)
|
2012-10-21 15:17:51 -06:00
|
|
|
c1.LocalAddr()
|
|
|
|
c1.SetDeadline(time.Now().Add(100 * time.Millisecond))
|
|
|
|
c1.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
|
|
|
|
c1.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
|
|
|
|
|
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 {
|
2013-03-02 23:59:53 -07:00
|
|
|
t.Fatalf("Dial failed: %v", 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()
|
|
|
|
c2.SetDeadline(time.Now().Add(100 * time.Millisecond))
|
|
|
|
c2.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
|
|
|
|
c2.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
|
|
|
|
|
|
|
|
if _, err := c2.Write(wb); err != nil {
|
2013-03-02 23:59:53 -07:00
|
|
|
t.Fatalf("Conn.Write failed: %v", err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
|
|
|
rb1 := make([]byte, 128)
|
|
|
|
if _, _, err := c1.ReadFrom(rb1); err != nil {
|
2013-10-09 07:44:11 -06:00
|
|
|
t.Fatalf("PacketConn.ReadFrom failed: %v", err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
2013-02-19 08:02:01 -07:00
|
|
|
var dst Addr
|
2013-03-02 23:59:53 -07:00
|
|
|
switch netstr[0] {
|
|
|
|
case "ip":
|
2013-02-19 08:02:01 -07:00
|
|
|
dst = &IPAddr{IP: IPv4(127, 0, 0, 1)}
|
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 {
|
2013-03-02 23:59:53 -07:00
|
|
|
t.Fatalf("PacketConn.WriteTo failed: %v", err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
|
|
|
rb2 := make([]byte, 128)
|
|
|
|
if _, err := c2.Read(rb2); err != nil {
|
2013-03-02 23:59:53 -07:00
|
|
|
t.Fatalf("Conn.Read failed: %v", err)
|
2012-10-21 15:17:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|