session_test.go 8.73 KB
Newer Older
Jeromy's avatar
Jeromy committed
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package proto

import (
	"QmfEm573cZeq3LpgccZMpngV6dXbm5gfU23F5nNUuhSxxJ/muxado/proto/frame"
	"io"
	"io/ioutil"
	"net"
	"net/http"
	"testing"
	"time"
)

func fakeStreamFactory(id frame.StreamId, priority frame.StreamPriority, streamType frame.StreamType, finLocal bool, finRemote bool, windowSize uint32, sess session) stream {
	return new(fakeStream)
}

type fakeStream struct {
}

func (s *fakeStream) Write([]byte) (int, error)               { return 0, nil }
func (s *fakeStream) Read([]byte) (int, error)                { return 0, nil }
func (s *fakeStream) Close() error                            { return nil }
func (s *fakeStream) SetDeadline(time.Time) error             { return nil }
func (s *fakeStream) SetReadDeadline(time.Time) error         { return nil }
func (s *fakeStream) SetWriteDeadline(time.Time) error        { return nil }
func (s *fakeStream) HalfClose([]byte) (int, error)           { return 0, nil }
func (s *fakeStream) Id() frame.StreamId                      { return 0 }
func (s *fakeStream) StreamType() frame.StreamType            { return 0 }
func (s *fakeStream) Session() ISession                       { return nil }
func (s *fakeStream) RemoteAddr() net.Addr                    { return nil }
func (s *fakeStream) LocalAddr() net.Addr                     { return nil }
func (s *fakeStream) handleStreamData(*frame.RStreamData)     {}
func (s *fakeStream) handleStreamWndInc(*frame.RStreamWndInc) {}
func (s *fakeStream) handleStreamRst(*frame.RStreamRst)       {}
func (s *fakeStream) closeWith(error)                         {}

type fakeConn struct {
	in     *io.PipeReader
	out    *io.PipeWriter
	closed bool
}

func (c *fakeConn) SetDeadline(time.Time) error      { return nil }
func (c *fakeConn) SetReadDeadline(time.Time) error  { return nil }
func (c *fakeConn) SetWriteDeadline(time.Time) error { return nil }
func (c *fakeConn) LocalAddr() net.Addr              { return nil }
func (c *fakeConn) RemoteAddr() net.Addr             { return nil }
func (c *fakeConn) Close() error                     { c.closed = true; c.in.Close(); return c.out.Close() }
func (c *fakeConn) Read(p []byte) (int, error)       { return c.in.Read(p) }
func (c *fakeConn) Write(p []byte) (int, error)      { return c.out.Write(p) }
func (c *fakeConn) Discard()                         { go io.Copy(ioutil.Discard, c.in) }

func newFakeConnPair() (local *fakeConn, remote *fakeConn) {
	local, remote = new(fakeConn), new(fakeConn)
	local.in, remote.out = io.Pipe()
	remote.in, local.out = io.Pipe()
	return
}

func TestFailWrongClientParity(t *testing.T) {
	t.Parallel()

	local, remote := newFakeConnPair()

	// don't need the remote output
	remote.Discard()

	// false for a server session
	s := NewSession(local, fakeStreamFactory, false, []Extension{})

	// 300 is even, and only servers send even stream ids
	f := frame.NewWStreamSyn()
	f.Set(300, 0, 0, false)

	// send the frame into the session
	trans := frame.NewBasicTransport(remote)
	trans.WriteFrame(f)

	// wait for failure
	code, err, _ := s.Wait()

	if code != frame.ProtocolError {
		t.Errorf("Session not terminated with protocol error. Got %d, expected %d. Session error: %v", code, frame.ProtocolError, err)
	}

	if !local.closed {
		t.Errorf("Session transport not closed after protocol failure.")
	}
}

func TestWrongServerParity(t *testing.T) {
	t.Parallel()

	local, remote := newFakeConnPair()

	// true for a client session
	s := NewSession(local, fakeStreamFactory, true, []Extension{})

	// don't need the remote output
	remote.Discard()

	// 300 is even, and only servers send even stream ids
	f := frame.NewWStreamSyn()
	f.Set(301, 0, 0, false)

	// send the frame into the session
	trans := frame.NewBasicTransport(remote)
	trans.WriteFrame(f)

	// wait for failure
	code, err, _ := s.Wait()

	if code != frame.ProtocolError {
		t.Errorf("Session not terminated with protocol error. Got %d, expected %d. Session error: %v", code, frame.ProtocolError, err)
	}

	if !local.closed {
		t.Errorf("Session transport not closed after protocol failure.")
	}
}

func TestAcceptStream(t *testing.T) {
	t.Parallel()

	local, remote := newFakeConnPair()

	// don't need the remote output
	remote.Discard()

	// true for a client session
	s := NewSession(local, NewStream, true, []Extension{})
	defer s.Close()

	f := frame.NewWStreamSyn()
	f.Set(300, 0, 0, false)

	// send the frame into the session
	trans := frame.NewBasicTransport(remote)
	trans.WriteFrame(f)

	done := make(chan int)
	go func() {
		defer func() { done <- 1 }()

		// wait for accept
		str, err := s.Accept()

		if err != nil {
			t.Errorf("Error accepting stream: %v", err)
			return
		}

		if str.Id() != frame.StreamId(300) {
			t.Errorf("Stream has wrong id. Expected %d, got %d", str.Id(), 300)
		}
	}()

	select {
	case <-time.After(time.Second):
		t.Fatalf("Timed out!")
	case <-done:
	}
}

func TestSynLowId(t *testing.T) {
	t.Parallel()

	local, remote := newFakeConnPair()

	// don't need the remote output
	remote.Discard()

	// true for a client session
	s := NewSession(local, fakeStreamFactory, true, []Extension{})

	// Start a stream
	f := frame.NewWStreamSyn()
	f.Set(302, 0, 0, false)

	// send the frame into the session
	trans := frame.NewBasicTransport(remote)
	trans.WriteFrame(f)

	// accept it
	s.Accept()

	// Start a closed stream at a lower id number
	f.Set(300, 0, 0, false)

	// send the frame into the session
	trans.WriteFrame(f)

	code, err, _ := s.Wait()
	if code != frame.ProtocolError {
		t.Errorf("Session not terminated with protocol error, got %d expected %d. Error: %v", code, frame.ProtocolError, err)
	}
}

// Check that sending a frame of the wrong size responds with FRAME_SIZE_ERROR
func TestFrameSizeError(t *testing.T) {
}

// Check that we get a protocol error for sending STREAM_DATA on a stream id that was never opened
func TestDataOnClosed(t *testing.T) {
}

// Check that we get nothing for sending STREAM_WND_INC on a stream id that was never opened
func TestWndIncOnClosed(t *testing.T) {
}

// Check that we get nothing for sending STREAM_RST on a stream id that was never opened
func TestRstOnClosed(t *testing.T) {
}

func TestGoAway(t *testing.T) {
}

func TestCloseGoAway(t *testing.T) {
}

func TestKill(t *testing.T) {
}

// make sure we get a valid syn frame from opening a new stream
func TestOpen(t *testing.T) {
}

// test opening a new stream that is immediately half-closed
func TestOpenWithFin(t *testing.T) {
}

// validate that a session fulfills the net.Listener interface
// compile-only check
func TestNetListener(t *testing.T) {
	t.Parallel()

	_ = func() {
		s := NewSession(new(fakeConn), NewStream, false, []Extension{})
		http.Serve(s.NetListener(), nil)
	}
}

func TestNetListenerAccept(t *testing.T) {
	t.Parallel()
	local, remote := newFakeConnPair()

	sLocal := NewSession(local, NewStream, false, []Extension{})
	sRemote := NewSession(remote, NewStream, true, []Extension{})

	go func() {
		_, err := sRemote.Open()
		if err != nil {
			t.Errorf("Failed to open stream: %v", err)
			return
		}
	}()

	l := sLocal.NetListener()

	_, err := l.Accept()
	if err != nil {
		t.Fatalf("Failed to accept stream: %v", err)
	}
}

// set up a fake extension which tries to accept a stream.
// we're testing to make sure that when the remote side closes the connection
// that the extension actually gets an error back from its accept() method
type fakeExt struct {
	closeOk chan int
}

func (e *fakeExt) Start(sess ISession, accept ExtAccept) frame.StreamType {
	go func() {
		_, err := accept()
		if err != nil {
			// we should get an error when the session close
			e.closeOk <- 1
		}
	}()

	return MinExtensionType
}

func TestExtensionCleanupAccept(t *testing.T) {
	t.Parallel()
	local, remote := newFakeConnPair()

	closeOk := make(chan int)
	_ = NewSession(local, NewStream, false, []Extension{&fakeExt{closeOk}})
	sRemote := NewSession(remote, NewStream, true, []Extension{})

	sRemote.Close()

	select {
	case <-time.After(time.Second):
		t.Fatalf("Timed out!")
	case <-closeOk:
	}
}

func TestWriteAfterClose(t *testing.T) {
	t.Parallel()
	local, remote := newFakeConnPair()
	sLocal := NewSession(local, NewStream, false, []Extension{})
	sRemote := NewSession(remote, NewStream, true, []Extension{})

	closed := make(chan int)
	go func() {
		stream, err := sRemote.Open()
		if err != nil {
			t.Errorf("Failed to open stream: %v", err)
			return
		}

		<-closed
		if _, err = stream.Write([]byte("test!")); err != nil {
			t.Errorf("Failed to write test data: %v", err)
			return
		}

		if _, err := sRemote.Open(); err != nil {
			t.Errorf("Failed to open second stream: %v", err)
			return
		}
	}()

	stream, err := sLocal.Accept()
	if err != nil {
		t.Fatalf("Failed to accept stream!")
	}

	// tell the other side that we closed so they can write late
	stream.Close()
	closed <- 1

	if _, err = sLocal.Accept(); err != nil {
		t.Fatalf("Failed to accept second connection: %v", err)
	}
}