1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
// 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.
// This file implements API tests across platforms and will never have a build
// tag.
package net
import (
"os"
"runtime"
"strings"
"testing"
"time"
)
func strfunc(s string) func() string {
return func() string {
return s
}
}
func packetConnTestData(t *testing.T, net string, i int) ([]byte, func()) {
switch net {
case "udp":
return []byte("UDP PACKETCONN TEST"), nil
case "ip":
if skip, skipmsg := skipRawSocketTest(t); skip {
return nil, func() {
t.Logf(skipmsg)
}
}
b, err := (&icmpMessage{
Type: icmpv4EchoRequest, Code: 0,
Body: &icmpEcho{
ID: os.Getpid() & 0xffff, Seq: i + 1,
Data: []byte("IP PACKETCONN TEST"),
},
}).Marshal()
if err != nil {
return nil, func() {
t.Fatalf("icmpMessage.Marshal failed: %v", err)
}
}
return b, nil
case "unixgram":
switch runtime.GOOS {
case "plan9", "windows":
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)
}
}
}
var packetConnTests = []struct {
net string
addr1 func() string
addr2 func() string
}{
{"udp", strfunc("127.0.0.1:0"), strfunc("127.0.0.1:0")},
{"ip:icmp", strfunc("127.0.0.1"), strfunc("127.0.0.1")},
{"unixgram", testUnixAddr, testUnixAddr},
}
func TestPacketConn(t *testing.T) {
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 {
netstr := strings.Split(tt.net, ":")
wb, skipOrFatalFn := packetConnTestData(t, netstr[0], i)
if skipOrFatalFn != nil {
skipOrFatalFn()
continue
}
addr1, addr2 := tt.addr1(), tt.addr2()
c1, err := ListenPacket(tt.net, addr1)
if err != nil {
t.Fatalf("ListenPacket failed: %v", err)
}
defer closer(c1, netstr[0], addr1, addr2)
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))
c2, err := ListenPacket(tt.net, addr2)
if err != nil {
t.Fatalf("ListenPacket failed: %v", err)
}
defer closer(c2, netstr[0], addr1, addr2)
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 {
t.Fatalf("PacketConn.WriteTo failed: %v", err)
}
rb2 := make([]byte, 128)
if _, _, err := c2.ReadFrom(rb2); err != nil {
t.Fatalf("PacketConn.ReadFrom failed: %v", err)
}
if _, err := c2.WriteTo(wb, c1.LocalAddr()); err != nil {
t.Fatalf("PacketConn.WriteTo failed: %v", err)
}
rb1 := make([]byte, 128)
if _, _, err := c1.ReadFrom(rb1); err != nil {
t.Fatalf("PacketConn.ReadFrom failed: %v", err)
}
}
}
func TestConnAndPacketConn(t *testing.T) {
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 {
var wb []byte
netstr := strings.Split(tt.net, ":")
wb, skipOrFatalFn := packetConnTestData(t, netstr[0], i)
if skipOrFatalFn != nil {
skipOrFatalFn()
continue
}
addr1, addr2 := tt.addr1(), tt.addr2()
c1, err := ListenPacket(tt.net, addr1)
if err != nil {
t.Fatalf("ListenPacket failed: %v", err)
}
defer closer(c1, netstr[0], addr1, addr2)
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))
c2, err := Dial(tt.net, c1.LocalAddr().String())
if err != nil {
t.Fatalf("Dial failed: %v", err)
}
defer c2.Close()
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 {
t.Fatalf("Conn.Write failed: %v", err)
}
rb1 := make([]byte, 128)
if _, _, err := c1.ReadFrom(rb1); err != nil {
t.Fatalf("PacketConn.ReadFrom failed: %v", err)
}
var dst Addr
switch netstr[0] {
case "ip":
dst = &IPAddr{IP: IPv4(127, 0, 0, 1)}
case "unixgram":
continue
default:
dst = c2.LocalAddr()
}
if _, err := c1.WriteTo(wb, dst); err != nil {
t.Fatalf("PacketConn.WriteTo failed: %v", err)
}
rb2 := make([]byte, 128)
if _, err := c2.Read(rb2); err != nil {
t.Fatalf("Conn.Read failed: %v", err)
}
}
}
|