1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // This file implements API tests across platforms and will never have a build
18 // The full stack test cases for IPConn have been moved to the
20 // golang.org/x/net/ipv4
21 // golang.org/x/net/ipv6
22 // golang.org/x/net/icmp
24 func packetConnTestData(t
*testing
.T
, network
string) ([]byte, func()) {
25 if !testableNetwork(network
) {
26 return nil, func() { t
.Logf("skipping %s test", network
) }
28 return []byte("PACKETCONN TEST"), nil
31 var packetConnTests
= []struct {
36 {"udp", "127.0.0.1:0", "127.0.0.1:0"},
37 {"unixgram", testUnixAddr(), testUnixAddr()},
40 func TestPacketConn(t
*testing
.T
) {
41 closer
:= func(c PacketConn
, net
, addr1
, addr2
string) {
50 for _
, tt
:= range packetConnTests
{
51 wb
, skipOrFatalFn
:= packetConnTestData(t
, tt
.net
)
52 if skipOrFatalFn
!= nil {
57 c1
, err
:= ListenPacket(tt
.net
, tt
.addr1
)
61 defer closer(c1
, tt
.net
, tt
.addr1
, tt
.addr2
)
63 c1
.SetDeadline(time
.Now().Add(500 * time
.Millisecond
))
64 c1
.SetReadDeadline(time
.Now().Add(500 * time
.Millisecond
))
65 c1
.SetWriteDeadline(time
.Now().Add(500 * time
.Millisecond
))
67 c2
, err
:= ListenPacket(tt
.net
, tt
.addr2
)
71 defer closer(c2
, tt
.net
, tt
.addr1
, tt
.addr2
)
73 c2
.SetDeadline(time
.Now().Add(500 * time
.Millisecond
))
74 c2
.SetReadDeadline(time
.Now().Add(500 * time
.Millisecond
))
75 c2
.SetWriteDeadline(time
.Now().Add(500 * time
.Millisecond
))
76 rb2
:= make([]byte, 128)
78 if _
, err
:= c1
.WriteTo(wb
, c2
.LocalAddr()); err
!= nil {
81 if _
, _
, err
:= c2
.ReadFrom(rb2
); err
!= nil {
84 if _
, err
:= c2
.WriteTo(wb
, c1
.LocalAddr()); err
!= nil {
87 rb1
:= make([]byte, 128)
88 if _
, _
, err
:= c1
.ReadFrom(rb1
); err
!= nil {
94 func TestConnAndPacketConn(t
*testing
.T
) {
95 closer
:= func(c PacketConn
, net
, addr1
, addr2
string) {
104 for _
, tt
:= range packetConnTests
{
106 wb
, skipOrFatalFn
:= packetConnTestData(t
, tt
.net
)
107 if skipOrFatalFn
!= nil {
112 c1
, err
:= ListenPacket(tt
.net
, tt
.addr1
)
116 defer closer(c1
, tt
.net
, tt
.addr1
, tt
.addr2
)
118 c1
.SetDeadline(time
.Now().Add(500 * time
.Millisecond
))
119 c1
.SetReadDeadline(time
.Now().Add(500 * time
.Millisecond
))
120 c1
.SetWriteDeadline(time
.Now().Add(500 * time
.Millisecond
))
122 c2
, err
:= Dial(tt
.net
, c1
.LocalAddr().String())
129 c2
.SetDeadline(time
.Now().Add(500 * time
.Millisecond
))
130 c2
.SetReadDeadline(time
.Now().Add(500 * time
.Millisecond
))
131 c2
.SetWriteDeadline(time
.Now().Add(500 * time
.Millisecond
))
133 if _
, err
:= c2
.Write(wb
); err
!= nil {
136 rb1
:= make([]byte, 128)
137 if _
, _
, err
:= c1
.ReadFrom(rb1
); err
!= nil {
147 if _
, err
:= c1
.WriteTo(wb
, dst
); err
!= nil {
150 rb2
:= make([]byte, 128)
151 if _
, err
:= c2
.Read(rb2
); err
!= nil {