Source file
src/net/packetconn_test.go
Documentation: net
1
2
3
4
5
6
7
8
9
10
11 package net
12
13 import (
14 "os"
15 "testing"
16 "time"
17 )
18
19
20
21
22
23
24
25 func packetConnTestData(t *testing.T, network string) ([]byte, func()) {
26 if !testableNetwork(network) {
27 return nil, func() { t.Logf("skipping %s test", network) }
28 }
29 return []byte("PACKETCONN TEST"), nil
30 }
31
32 var packetConnTests = []struct {
33 net string
34 addr1 string
35 addr2 string
36 }{
37 {"udp", "127.0.0.1:0", "127.0.0.1:0"},
38 {"unixgram", testUnixAddr(), testUnixAddr()},
39 }
40
41 func TestPacketConn(t *testing.T) {
42 closer := func(c PacketConn, net, addr1, addr2 string) {
43 c.Close()
44 switch net {
45 case "unixgram":
46 os.Remove(addr1)
47 os.Remove(addr2)
48 }
49 }
50
51 for _, tt := range packetConnTests {
52 wb, skipOrFatalFn := packetConnTestData(t, tt.net)
53 if skipOrFatalFn != nil {
54 skipOrFatalFn()
55 continue
56 }
57
58 c1, err := ListenPacket(tt.net, tt.addr1)
59 if err != nil {
60 t.Fatal(err)
61 }
62 defer closer(c1, tt.net, tt.addr1, tt.addr2)
63 c1.LocalAddr()
64 c1.SetDeadline(time.Now().Add(500 * time.Millisecond))
65 c1.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
66 c1.SetWriteDeadline(time.Now().Add(500 * time.Millisecond))
67
68 c2, err := ListenPacket(tt.net, tt.addr2)
69 if err != nil {
70 t.Fatal(err)
71 }
72 defer closer(c2, tt.net, tt.addr1, tt.addr2)
73 c2.LocalAddr()
74 c2.SetDeadline(time.Now().Add(500 * time.Millisecond))
75 c2.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
76 c2.SetWriteDeadline(time.Now().Add(500 * time.Millisecond))
77 rb2 := make([]byte, 128)
78
79 if _, err := c1.WriteTo(wb, c2.LocalAddr()); err != nil {
80 t.Fatal(err)
81 }
82 if _, _, err := c2.ReadFrom(rb2); err != nil {
83 t.Fatal(err)
84 }
85 if _, err := c2.WriteTo(wb, c1.LocalAddr()); err != nil {
86 t.Fatal(err)
87 }
88 rb1 := make([]byte, 128)
89 if _, _, err := c1.ReadFrom(rb1); err != nil {
90 t.Fatal(err)
91 }
92 }
93 }
94
95 func TestConnAndPacketConn(t *testing.T) {
96 closer := func(c PacketConn, net, addr1, addr2 string) {
97 c.Close()
98 switch net {
99 case "unixgram":
100 os.Remove(addr1)
101 os.Remove(addr2)
102 }
103 }
104
105 for _, tt := range packetConnTests {
106 var wb []byte
107 wb, skipOrFatalFn := packetConnTestData(t, tt.net)
108 if skipOrFatalFn != nil {
109 skipOrFatalFn()
110 continue
111 }
112
113 c1, err := ListenPacket(tt.net, tt.addr1)
114 if err != nil {
115 t.Fatal(err)
116 }
117 defer closer(c1, tt.net, tt.addr1, tt.addr2)
118 c1.LocalAddr()
119 c1.SetDeadline(time.Now().Add(500 * time.Millisecond))
120 c1.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
121 c1.SetWriteDeadline(time.Now().Add(500 * time.Millisecond))
122
123 c2, err := Dial(tt.net, c1.LocalAddr().String())
124 if err != nil {
125 t.Fatal(err)
126 }
127 defer c2.Close()
128 c2.LocalAddr()
129 c2.RemoteAddr()
130 c2.SetDeadline(time.Now().Add(500 * time.Millisecond))
131 c2.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
132 c2.SetWriteDeadline(time.Now().Add(500 * time.Millisecond))
133
134 if _, err := c2.Write(wb); err != nil {
135 t.Fatal(err)
136 }
137 rb1 := make([]byte, 128)
138 if _, _, err := c1.ReadFrom(rb1); err != nil {
139 t.Fatal(err)
140 }
141 var dst Addr
142 switch tt.net {
143 case "unixgram":
144 continue
145 default:
146 dst = c2.LocalAddr()
147 }
148 if _, err := c1.WriteTo(wb, dst); err != nil {
149 t.Fatal(err)
150 }
151 rb2 := make([]byte, 128)
152 if _, err := c2.Read(rb2); err != nil {
153 t.Fatal(err)
154 }
155 }
156 }
157
View as plain text