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 func strfunc(s
string) func() string {
19 return func() string {
24 var packetConnTests
= []struct {
29 {"udp", strfunc("127.0.0.1:0"), strfunc("127.0.0.1:0")},
30 {"ip:icmp", strfunc("127.0.0.1"), strfunc("127.0.0.1")},
31 {"unixgram", testUnixAddr
, testUnixAddr
},
34 func TestPacketConn(t
*testing
.T
) {
35 closer
:= func(c PacketConn
, net
, addr1
, addr2
string) {
44 for i
, tt
:= range packetConnTests
{
46 netstr
:= strings
.Split(tt
.net
, ":")
49 wb
= []byte("UDP PACKETCONN TEST")
59 wb
, err
= (&icmpMessage
{
60 Type
: icmpv4EchoRequest
, Code
: 0,
62 ID
: os
.Getpid() & 0xffff, Seq
: i
+ 1,
63 Data
: []byte("IP PACKETCONN TEST"),
67 t
.Fatalf("icmpMessage.Marshal failed: %v", err
)
71 case "plan9", "windows":
74 wb
= []byte("UNIXGRAM PACKETCONN TEST")
79 addr1
, addr2
:= tt
.addr1(), tt
.addr2()
80 c1
, err
:= ListenPacket(tt
.net
, addr1
)
82 t
.Fatalf("ListenPacket failed: %v", err
)
84 defer closer(c1
, netstr
[0], addr1
, addr2
)
86 c1
.SetDeadline(time
.Now().Add(100 * time
.Millisecond
))
87 c1
.SetReadDeadline(time
.Now().Add(100 * time
.Millisecond
))
88 c1
.SetWriteDeadline(time
.Now().Add(100 * time
.Millisecond
))
90 c2
, err
:= ListenPacket(tt
.net
, addr2
)
92 t
.Fatalf("ListenPacket failed: %v", err
)
94 defer closer(c2
, netstr
[0], addr1
, addr2
)
96 c2
.SetDeadline(time
.Now().Add(100 * time
.Millisecond
))
97 c2
.SetReadDeadline(time
.Now().Add(100 * time
.Millisecond
))
98 c2
.SetWriteDeadline(time
.Now().Add(100 * time
.Millisecond
))
100 if _
, err
:= c1
.WriteTo(wb
, c2
.LocalAddr()); err
!= nil {
101 t
.Fatalf("PacketConn.WriteTo failed: %v", err
)
103 rb2
:= make([]byte, 128)
104 if _
, _
, err
:= c2
.ReadFrom(rb2
); err
!= nil {
105 t
.Fatalf("PacketConn.ReadFrom failed: %v", err
)
107 if _
, err
:= c2
.WriteTo(wb
, c1
.LocalAddr()); err
!= nil {
108 t
.Fatalf("PacketConn.WriteTo failed: %v", err
)
110 rb1
:= make([]byte, 128)
111 if _
, _
, err
:= c1
.ReadFrom(rb1
); err
!= nil {
112 t
.Fatalf("PacketConn.ReadFrom failed: %v", err
)
117 func TestConnAndPacketConn(t
*testing
.T
) {
118 closer
:= func(c PacketConn
, net
, addr1
, addr2
string) {
127 for i
, tt
:= range packetConnTests
{
129 netstr
:= strings
.Split(tt
.net
, ":")
132 wb
= []byte("UDP PACKETCONN TEST")
134 switch runtime
.GOOS
{
138 if os
.Getuid() != 0 {
142 wb
, err
= (&icmpMessage
{
143 Type
: icmpv4EchoRequest
, Code
: 0,
145 ID
: os
.Getpid() & 0xffff, Seq
: i
+ 1,
146 Data
: []byte("IP PACKETCONN TEST"),
150 t
.Fatalf("icmpMessage.Marshal failed: %v", err
)
153 switch runtime
.GOOS
{
154 case "plan9", "windows":
157 wb
= []byte("UNIXGRAM PACKETCONN TEST")
162 addr1
, addr2
:= tt
.addr1(), tt
.addr2()
163 c1
, err
:= ListenPacket(tt
.net
, addr1
)
165 t
.Fatalf("ListenPacket failed: %v", err
)
167 defer closer(c1
, netstr
[0], addr1
, addr2
)
169 c1
.SetDeadline(time
.Now().Add(100 * time
.Millisecond
))
170 c1
.SetReadDeadline(time
.Now().Add(100 * time
.Millisecond
))
171 c1
.SetWriteDeadline(time
.Now().Add(100 * time
.Millisecond
))
173 c2
, err
:= Dial(tt
.net
, c1
.LocalAddr().String())
175 t
.Fatalf("Dial failed: %v", err
)
180 c2
.SetDeadline(time
.Now().Add(100 * time
.Millisecond
))
181 c2
.SetReadDeadline(time
.Now().Add(100 * time
.Millisecond
))
182 c2
.SetWriteDeadline(time
.Now().Add(100 * time
.Millisecond
))
184 if _
, err
:= c2
.Write(wb
); err
!= nil {
185 t
.Fatalf("Conn.Write failed: %v", err
)
187 rb1
:= make([]byte, 128)
188 if _
, _
, err
:= c1
.ReadFrom(rb1
); err
!= nil {
189 t
.Fatalf("PacetConn.ReadFrom failed: %v", err
)
194 dst
= &IPAddr
{IP
: IPv4(127, 0, 0, 1)}
200 if _
, err
:= c1
.WriteTo(wb
, dst
); err
!= nil {
201 t
.Fatalf("PacketConn.WriteTo failed: %v", err
)
203 rb2
:= make([]byte, 128)
204 if _
, err
:= c2
.Read(rb2
); err
!= nil {
205 t
.Fatalf("Conn.Read failed: %v", err
)