forked from inconshreveable/muxado
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_test.go
272 lines (215 loc) · 6.15 KB
/
stream_test.go
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package muxado
import (
"io"
"io/ioutil"
"testing"
"github.com/inconshreveable/muxado/frame"
)
func TestCloseWrite(t *testing.T) {
t.Parallel()
local, remote := newFakeConnPair()
s := Client(local, &Config{NewFramer: debugFramer("CLIENT")})
done := make(chan int)
go func() {
fr := debugFramer("SERVER")(remote, remote)
readFrame := func(ftype frame.Type) frame.Frame {
f, err := fr.ReadFrame()
if err != nil {
t.Errorf("Failed to read next frame: %v", err)
return nil
}
if f.Type() != ftype {
t.Errorf("Wrong frame type. Got %v, expected %v", f.Type(), ftype)
return nil
}
return f
}
f := readFrame(frame.TypeData)
if f.Length() != 10 {
t.Errorf("Wrong data length. Got %d, expected %d", f.Length(), 10)
return
}
n, _ := io.Copy(ioutil.Discard, f.(*frame.Data).Reader())
if n != 10 {
t.Errorf("Wrong read size. Got %d, expected %d", n, 10)
return
}
f = readFrame(frame.TypeData)
if f.Length() != 0 {
t.Errorf("Wrong data length. Got %d, expected %d", f.Length(), 0)
return
}
if !f.(*frame.Data).Fin() {
t.Errorf("Expected fin flag on final data frame.")
return
}
// io.Pipe is weird and apparently sometimes doesn't acknowledge a write completion
// until the next read
remote.Read([]byte{})
close(done)
}()
str, err := s.OpenStream()
if err != nil {
t.Fatalf("Failed to open stream: %v", err)
}
_, err = str.Write(make([]byte, 10))
if err != nil {
t.Fatalf("Failed to write data: %v", err)
}
err = str.CloseWrite()
if err != nil {
t.Fatalf("Failed to close write")
}
<-done
}
/*
func TestDataAfterRst(t *testing.T) {
local, remote := newFakeConnPair()
_ = NewSession(local, NewStream, false, []Extension{})
trans := frame.NewBasicTransport(remote)
// make sure that we get an RST STREAM_CLOSED
done := make(chan int)
go func() {
defer func() { done <- 1 }()
f, err := trans.ReadFrame()
if err != nil {
t.Errorf("Failed to read frame sent from session: %v", err)
return
}
fr, ok := f.(*frame.RStreamRst)
if !ok {
t.Errorf("Frame is not STREAM_RST: %v", f)
return
}
if fr.ErrorCode() != frame.StreamClosed {
t.Errorf("Error code on STREAM_RST is not STREAM_CLOSED. Got %d, expected %d", fr.ErrorCode(), frame.StreamClosed)
return
}
}()
fSyn := frame.NewWStreamSyn()
if err := fSyn.Set(301, 0, 0, false); err != nil {
t.Fatalf("Failed to make syn frame: %v", err)
}
if err := trans.WriteFrame(fSyn); err != nil {
t.Fatalf("Failed to send syn: %v", err)
}
fRst := frame.NewWStreamRst()
if err := fRst.Set(301, frame.Cancel); err != nil {
t.Fatal("Failed to make rst frame: %v", err)
}
if err := trans.WriteFrame(fRst); err != nil {
t.Fatalf("Failed to write rst frame: %v", err)
}
fData := frame.NewWStreamData()
if err := fData.Set(301, []byte{0xa, 0xFF}, false); err != nil {
t.Fatalf("Failed to set data frame")
}
trans.WriteFrame(fData)
<-done
}
func TestFlowControlError(t *testing.T) {
local, remote := newFakeConnPair()
s := NewSession(local, NewStream, false, []Extension{})
s.(*Session).defaultWindowSize = 10
trans := frame.NewBasicTransport(remote)
// make sure that we get an RST FLOW_CONTROL_ERROR
done := make(chan int)
go func() {
defer func() { done <- 1 }()
f, err := trans.ReadFrame()
if err != nil {
t.Errorf("Failed to read frame sent from session: %v", err)
return
}
fr, ok := f.(*frame.RStreamRst)
if !ok {
t.Errorf("Frame is not STREAM_RST: %v", f)
return
}
if fr.ErrorCode() != frame.FlowControlError {
t.Errorf("Error code on STREAM_RST is not FLOW_CONTROL_ERROR. Got %d, expected %d", fr.ErrorCode(), frame.FlowControlError)
return
}
}()
fSyn := frame.NewWStreamSyn()
if err := fSyn.Set(301, 0, 0, false); err != nil {
t.Fatalf("Failed to make syn frame: %v", err)
}
if err := trans.WriteFrame(fSyn); err != nil {
t.Fatalf("Failed to send syn: %v", err)
}
fData := frame.NewWStreamData()
if err := fData.Set(301, make([]byte, 11), false); err != nil {
t.Fatalf("Failed to set data frame")
}
trans.WriteFrame(fData)
<-done
}
func TestTolerateLateFrameAfterRst(t *testing.T) {
local, remote := newFakeConnPair()
s := NewSession(local, NewStream, false, []Extension{})
trans := frame.NewBasicTransport(remote)
// make sure that we don't get any error on a late frame
done := make(chan int)
go func() {
defer func() { done <- 1 }()
// read syn
trans.ReadFrame()
// read rst
trans.ReadFrame()
// should block
if f, err := trans.ReadFrame(); err != nil {
t.Errorf("Error reading frame: %v", err)
} else {
t.Errorf("Got frame that we shouldn't have read: %v. Type: %v", f, f.Type())
}
}()
str, err := s.Open()
if err != nil {
t.Fatalf("failed to open stream")
}
str.(*Stream).resetWith(frame.Cancel, fmt.Errorf("cancel"))
fData := frame.NewWStreamData()
if err := fData.Set(str.Id(), []byte{0x1, 0x2, 0x3}, false); err != nil {
t.Fatalf("Failed to set data frame")
}
trans.WriteFrame(fData)
select {
case <-done:
t.Fatalf("Stream sent response to late DATA frame")
case <-time.After(1 * time.Second):
// ok
}
}
// Test that we remove a stream from the session if both sides half-close
func TestRemoveAfterHalfClose(t *testing.T) {
local, remote := newFakeConnPair()
remote.Discard()
s := NewSession(local, NewStream, false, []Extension{})
trans := frame.NewBasicTransport(remote)
// open stream
str, err := s.Open()
if err != nil {
t.Fatalf("failed to open stream")
}
// half close remote side (true means half-close)
fData := frame.NewWStreamData()
if err := fData.Set(str.Id(), []byte{0x1, 0x2, 0x3}, true); err != nil {
t.Fatalf("Failed to set data frame")
}
if err := trans.WriteFrame(fData); err != nil {
t.Fatalf("Failed to write data frame")
}
// half-close local side
str.HalfClose([]byte{0xFF, 0xFE, 0xFD, 0xFC})
// yield so the stream can process
time.Sleep(0)
// verify stream is removed
if stream, ok := s.(*Session).streams.Get(str.Id()); ok {
t.Fatalf("Expected stream %d to be removed after both sides half-closed, but found: %v!", str.Id(), stream)
}
}
// Test that we get a RST if we send a DATA frame after we send a DATA frame with a FIN
func TestDataAfterFin(t *testing.T) {
}
*/