Source file
src/net/rawconn_test.go
1
2
3
4
5 package net
6
7 import (
8 "bytes"
9 "runtime"
10 "testing"
11 "time"
12 )
13
14 func TestRawConnReadWrite(t *testing.T) {
15 switch runtime.GOOS {
16 case "plan9", "js", "wasip1":
17 t.Skipf("not supported on %s", runtime.GOOS)
18 }
19
20 t.Run("TCP", func(t *testing.T) {
21 handler := func(ls *localServer, ln Listener) {
22 c, err := ln.Accept()
23 if err != nil {
24 t.Error(err)
25 return
26 }
27 defer c.Close()
28
29 cc, err := ln.(*TCPListener).SyscallConn()
30 if err != nil {
31 t.Fatal(err)
32 }
33 called := false
34 op := func(uintptr) bool {
35 called = true
36 return true
37 }
38 err = cc.Write(op)
39 if err == nil {
40 t.Error("Write should return an error")
41 }
42 if called {
43 t.Error("Write shouldn't call op")
44 }
45 called = false
46 err = cc.Read(op)
47 if err == nil {
48 t.Error("Read should return an error")
49 }
50 if called {
51 t.Error("Read shouldn't call op")
52 }
53
54 var b [32]byte
55 n, err := c.Read(b[:])
56 if err != nil {
57 t.Error(err)
58 return
59 }
60 if _, err := c.Write(b[:n]); err != nil {
61 t.Error(err)
62 return
63 }
64 }
65 ls := newLocalServer(t, "tcp")
66 defer ls.teardown()
67 if err := ls.buildup(handler); err != nil {
68 t.Fatal(err)
69 }
70
71 c, err := Dial(ls.Listener.Addr().Network(), ls.Listener.Addr().String())
72 if err != nil {
73 t.Fatal(err)
74 }
75 defer c.Close()
76
77 cc, err := c.(*TCPConn).SyscallConn()
78 if err != nil {
79 t.Fatal(err)
80 }
81 data := []byte("HELLO-R-U-THERE")
82 if err := writeRawConn(cc, data); err != nil {
83 t.Fatal(err)
84 }
85 var b [32]byte
86 n, err := readRawConn(cc, b[:])
87 if err != nil {
88 t.Fatal(err)
89 }
90 if bytes.Compare(b[:n], data) != 0 {
91 t.Fatalf("got %q; want %q", b[:n], data)
92 }
93 })
94 t.Run("Deadline", func(t *testing.T) {
95 switch runtime.GOOS {
96 case "windows":
97 t.Skipf("not supported on %s", runtime.GOOS)
98 }
99
100 ln := newLocalListener(t, "tcp")
101 defer ln.Close()
102
103 c, err := Dial(ln.Addr().Network(), ln.Addr().String())
104 if err != nil {
105 t.Fatal(err)
106 }
107 defer c.Close()
108
109 cc, err := c.(*TCPConn).SyscallConn()
110 if err != nil {
111 t.Fatal(err)
112 }
113 var b [1]byte
114
115 c.SetDeadline(noDeadline)
116 if err := c.SetDeadline(time.Now().Add(-1)); err != nil {
117 t.Fatal(err)
118 }
119 if err = writeRawConn(cc, b[:]); err == nil {
120 t.Fatal("Write should fail")
121 }
122 if perr := parseWriteError(err); perr != nil {
123 t.Error(perr)
124 }
125 if !isDeadlineExceeded(err) {
126 t.Errorf("got %v; want timeout", err)
127 }
128 if _, err = readRawConn(cc, b[:]); err == nil {
129 t.Fatal("Read should fail")
130 }
131 if perr := parseReadError(err); perr != nil {
132 t.Error(perr)
133 }
134 if !isDeadlineExceeded(err) {
135 t.Errorf("got %v; want timeout", err)
136 }
137
138 c.SetReadDeadline(noDeadline)
139 if err := c.SetReadDeadline(time.Now().Add(-1)); err != nil {
140 t.Fatal(err)
141 }
142 if _, err = readRawConn(cc, b[:]); err == nil {
143 t.Fatal("Read should fail")
144 }
145 if perr := parseReadError(err); perr != nil {
146 t.Error(perr)
147 }
148 if !isDeadlineExceeded(err) {
149 t.Errorf("got %v; want timeout", err)
150 }
151
152 c.SetWriteDeadline(noDeadline)
153 if err := c.SetWriteDeadline(time.Now().Add(-1)); err != nil {
154 t.Fatal(err)
155 }
156 if err = writeRawConn(cc, b[:]); err == nil {
157 t.Fatal("Write should fail")
158 }
159 if perr := parseWriteError(err); perr != nil {
160 t.Error(perr)
161 }
162 if !isDeadlineExceeded(err) {
163 t.Errorf("got %v; want timeout", err)
164 }
165 })
166 }
167
168 func TestRawConnControl(t *testing.T) {
169 switch runtime.GOOS {
170 case "plan9", "js", "wasip1":
171 t.Skipf("not supported on %s", runtime.GOOS)
172 }
173
174 t.Run("TCP", func(t *testing.T) {
175 ln := newLocalListener(t, "tcp")
176 defer ln.Close()
177
178 cc1, err := ln.(*TCPListener).SyscallConn()
179 if err != nil {
180 t.Fatal(err)
181 }
182 if err := controlRawConn(cc1, ln.Addr()); err != nil {
183 t.Fatal(err)
184 }
185
186 c, err := Dial(ln.Addr().Network(), ln.Addr().String())
187 if err != nil {
188 t.Fatal(err)
189 }
190 defer c.Close()
191
192 cc2, err := c.(*TCPConn).SyscallConn()
193 if err != nil {
194 t.Fatal(err)
195 }
196 if err := controlRawConn(cc2, c.LocalAddr()); err != nil {
197 t.Fatal(err)
198 }
199
200 ln.Close()
201 if err := controlRawConn(cc1, ln.Addr()); err == nil {
202 t.Fatal("Control after Close should fail")
203 }
204 c.Close()
205 if err := controlRawConn(cc2, c.LocalAddr()); err == nil {
206 t.Fatal("Control after Close should fail")
207 }
208 })
209 }
210
View as plain text