forked from summerwind/h2spec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
5_1.go
348 lines (278 loc) · 10.5 KB
/
5_1.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
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
341
342
343
344
345
346
347
348
package h2spec
import (
"github.com/bradfitz/http2"
)
func StreamStatesTestGroup(ctx *Context) *TestGroup {
tg := NewTestGroup("5.1", "Stream States")
tg.AddTestCase(NewTestCase(
"idle: Sends a DATA frame",
"The endpoint MUST treat this as a connection error (Section 5.4.1) of type PROTOCOL_ERROR.",
func(ctx *Context) (pass bool, expected []Result, actual Result) {
http2Conn := CreateHttp2Conn(ctx, true)
defer http2Conn.conn.Close()
http2Conn.fr.WriteData(1, true, []byte("test"))
actualCodes := []http2.ErrCode{http2.ErrCodeProtocol}
return TestConnectionError(ctx, http2Conn, actualCodes)
},
))
tg.AddTestCase(NewTestCase(
"idle: Sends a RST_STREAM frame",
"The endpoint MUST treat this as a connection error (Section 5.4.1) of type PROTOCOL_ERROR.",
func(ctx *Context) (pass bool, expected []Result, actual Result) {
http2Conn := CreateHttp2Conn(ctx, true)
defer http2Conn.conn.Close()
http2Conn.fr.WriteRSTStream(1, http2.ErrCodeCancel)
actualCodes := []http2.ErrCode{http2.ErrCodeProtocol}
return TestConnectionError(ctx, http2Conn, actualCodes)
},
))
tg.AddTestCase(NewTestCase(
"idle: Sends a WINDOW_UPDATE frame",
"The endpoint MUST treat this as a connection error (Section 5.4.1) of type PROTOCOL_ERROR.",
func(ctx *Context) (pass bool, expected []Result, actual Result) {
http2Conn := CreateHttp2Conn(ctx, true)
defer http2Conn.conn.Close()
http2Conn.fr.WriteWindowUpdate(1, 100)
actualCodes := []http2.ErrCode{http2.ErrCodeProtocol}
return TestConnectionError(ctx, http2Conn, actualCodes)
},
))
tg.AddTestCase(NewTestCase(
"idle: Sends a CONTINUATION frame",
"The endpoint MUST treat this as a connection error (Section 5.4.1) of type PROTOCOL_ERROR.",
func(ctx *Context) (pass bool, expected []Result, actual Result) {
http2Conn := CreateHttp2Conn(ctx, true)
defer http2Conn.conn.Close()
hdrs := commonHeaderFields(ctx)
blockFragment := http2Conn.EncodeHeader(hdrs)
http2Conn.fr.WriteContinuation(1, true, blockFragment)
actualCodes := []http2.ErrCode{http2.ErrCodeProtocol}
return TestConnectionError(ctx, http2Conn, actualCodes)
},
))
tg.AddTestCase(NewTestCase(
"half closed (remote): Sends a DATA frame",
"The endpoint MUST respond with a stream error (Section 5.4.2) of type STREAM_CLOSED.",
func(ctx *Context) (pass bool, expected []Result, actual Result) {
http2Conn := CreateHttp2Conn(ctx, true)
defer http2Conn.conn.Close()
hdrs := commonHeaderFields(ctx)
blockFragment := http2Conn.EncodeHeader(hdrs)
var hp http2.HeadersFrameParam
hp.StreamID = 1
hp.EndStream = true
hp.EndHeaders = true
hp.BlockFragment = blockFragment
http2Conn.fr.WriteHeaders(hp)
http2Conn.fr.WriteData(1, true, []byte("test"))
actualCodes := []http2.ErrCode{http2.ErrCodeStreamClosed}
return TestStreamError(ctx, http2Conn, actualCodes)
},
))
tg.AddTestCase(NewTestCase(
"half closed (remote): Sends a HEADERS frame",
"The endpoint MUST respond with a stream error (Section 5.4.2) of type STREAM_CLOSED.",
func(ctx *Context) (pass bool, expected []Result, actual Result) {
http2Conn := CreateHttp2Conn(ctx, true)
defer http2Conn.conn.Close()
hdrs := commonHeaderFields(ctx)
blockFragment := http2Conn.EncodeHeader(hdrs)
var hp1 http2.HeadersFrameParam
hp1.StreamID = 1
hp1.EndStream = true
hp1.EndHeaders = true
hp1.BlockFragment = blockFragment
http2Conn.fr.WriteHeaders(hp1)
var hp2 http2.HeadersFrameParam
hp2.StreamID = 1
hp2.EndStream = true
hp2.EndHeaders = true
hp2.BlockFragment = blockFragment
http2Conn.fr.WriteHeaders(hp2)
actualCodes := []http2.ErrCode{http2.ErrCodeStreamClosed}
return TestStreamError(ctx, http2Conn, actualCodes)
},
))
tg.AddTestCase(NewTestCase(
"half closed (remote): Sends a CONTINUATION frame",
"The endpoint MUST respond with a stream error (Section 5.4.2) of type STREAM_CLOSED.",
func(ctx *Context) (pass bool, expected []Result, actual Result) {
http2Conn := CreateHttp2Conn(ctx, true)
defer http2Conn.conn.Close()
hdrs := commonHeaderFields(ctx)
blockFragment := http2Conn.EncodeHeader(hdrs)
var hp http2.HeadersFrameParam
hp.StreamID = 1
hp.EndStream = true
hp.EndHeaders = true
hp.BlockFragment = blockFragment
http2Conn.fr.WriteHeaders(hp)
http2Conn.fr.WriteContinuation(1, true, blockFragment)
actualCodes := []http2.ErrCode{http2.ErrCodeStreamClosed, http2.ErrCodeProtocol}
return TestStreamError(ctx, http2Conn, actualCodes)
},
))
if ctx.Strict {
tg.AddTestCase(NewTestCase(
"closed: Sends a DATA frame",
"The endpoint MUST treat this as a stream error (Section 5.4.2) of type STREAM_CLOSED.",
func(ctx *Context) (pass bool, expected []Result, actual Result) {
http2Conn := CreateHttp2Conn(ctx, true)
defer http2Conn.conn.Close()
hdrs := commonHeaderFields(ctx)
blockFragment := http2Conn.EncodeHeader(hdrs)
var hp http2.HeadersFrameParam
hp.StreamID = 1
hp.EndStream = true
hp.EndHeaders = true
hp.BlockFragment = blockFragment
http2Conn.fr.WriteHeaders(hp)
pass, expected, actual = TestStreamClose(ctx, http2Conn)
if !pass {
return pass, expected, actual
}
http2Conn.fr.WriteData(1, true, []byte("test"))
actualCodes := []http2.ErrCode{http2.ErrCodeStreamClosed}
return TestStreamError(ctx, http2Conn, actualCodes)
},
))
}
if ctx.Strict {
tg.AddTestCase(NewTestCase(
"closed: Sends a HEADERS frame",
"The endpoint MUST treat this as a stream error (Section 5.4.2) of type STREAM_CLOSED.",
func(ctx *Context) (pass bool, expected []Result, actual Result) {
http2Conn := CreateHttp2Conn(ctx, true)
defer http2Conn.conn.Close()
hdrs := commonHeaderFields(ctx)
blockFragment := http2Conn.EncodeHeader(hdrs)
var hp http2.HeadersFrameParam
hp.StreamID = 1
hp.EndStream = true
hp.EndHeaders = true
hp.BlockFragment = blockFragment
http2Conn.fr.WriteHeaders(hp)
pass, expected, actual = TestStreamClose(ctx, http2Conn)
if !pass {
return pass, expected, actual
}
http2Conn.fr.WriteHeaders(hp)
actualCodes := []http2.ErrCode{http2.ErrCodeStreamClosed}
return TestStreamError(ctx, http2Conn, actualCodes)
},
))
}
tg.AddTestCase(NewTestCase(
"closed: Sends a CONTINUATION frame",
"The endpoint MUST treat this as a stream error (Section 5.4.2) of type STREAM_CLOSED.",
func(ctx *Context) (pass bool, expected []Result, actual Result) {
http2Conn := CreateHttp2Conn(ctx, true)
defer http2Conn.conn.Close()
hdrs := commonHeaderFields(ctx)
hdrs = append(hdrs, pair("x-dummy1", dummyData(10000)))
hdrs = append(hdrs, pair("x-dummy2", dummyData(10000)))
blockFragment := http2Conn.EncodeHeader(hdrs)
var hp http2.HeadersFrameParam
hp.StreamID = 1
hp.EndStream = true
hp.EndHeaders = false
hp.BlockFragment = blockFragment[0:16384]
http2Conn.fr.WriteHeaders(hp)
http2Conn.fr.WriteContinuation(1, true, blockFragment[16384:])
pass, expected, actual = TestStreamClose(ctx, http2Conn)
if !pass {
return pass, expected, actual
}
http2Conn.fr.WriteContinuation(1, true, blockFragment[16384:])
actualCodes := []http2.ErrCode{http2.ErrCodeStreamClosed, http2.ErrCodeProtocol}
return TestStreamError(ctx, http2Conn, actualCodes)
},
))
tg.AddTestGroup(StreamIdentifiersTestGroup(ctx))
tg.AddTestGroup(StreamConcurrencyTestGroup(ctx))
return tg
}
func StreamIdentifiersTestGroup(ctx *Context) *TestGroup {
tg := NewTestGroup("5.1.1", "Stream Identifiers")
tg.AddTestCase(NewTestCase(
"Sends even-numbered stream identifier",
"The endpoint MUST respond with a connection error of type PROTOCOL_ERROR.",
func(ctx *Context) (pass bool, expected []Result, actual Result) {
http2Conn := CreateHttp2Conn(ctx, true)
defer http2Conn.conn.Close()
hdrs := commonHeaderFields(ctx)
var hp http2.HeadersFrameParam
hp.StreamID = 2
hp.EndStream = true
hp.EndHeaders = true
hp.BlockFragment = http2Conn.EncodeHeader(hdrs)
http2Conn.fr.WriteHeaders(hp)
actualCodes := []http2.ErrCode{http2.ErrCodeProtocol}
return TestConnectionError(ctx, http2Conn, actualCodes)
},
))
if ctx.Strict {
tg.AddTestCase(NewTestCase(
"Sends stream identifier that is numerically smaller than previous",
"The endpoint MUST respond with a connection error of type PROTOCOL_ERROR.",
func(ctx *Context) (pass bool, expected []Result, actual Result) {
http2Conn := CreateHttp2Conn(ctx, true)
defer http2Conn.conn.Close()
hdrs := commonHeaderFields(ctx)
var hp1 http2.HeadersFrameParam
hp1.StreamID = 5
hp1.EndStream = true
hp1.EndHeaders = true
hp1.BlockFragment = http2Conn.EncodeHeader(hdrs)
http2Conn.fr.WriteHeaders(hp1)
var hp2 http2.HeadersFrameParam
hp2.StreamID = 3
hp2.EndStream = true
hp2.EndHeaders = true
hp2.BlockFragment = http2Conn.EncodeHeader(hdrs)
http2Conn.fr.WriteHeaders(hp2)
actualCodes := []http2.ErrCode{http2.ErrCodeProtocol}
return TestConnectionError(ctx, http2Conn, actualCodes)
},
))
}
return tg
}
func StreamConcurrencyTestGroup(ctx *Context) *TestGroup {
tg := NewTestGroup("5.1.2", "Stream Concurrency")
tg.AddTestCase(NewTestCase(
"Sends HEADERS frames that causes their advertised concurrent stream limit to be exceeded",
"The endpoint MUST treat this as a stream error (Section 5.4.2) of type PROTOCOL_ERROR or REFUSED_STREAM",
func(ctx *Context) (pass bool, expected []Result, actual Result) {
http2Conn := CreateHttp2Conn(ctx, true)
defer http2Conn.conn.Close()
// Skip this test when SETTINGS_MAX_CONCURRENT_STREAMS is unlimited.
_, ok := http2Conn.Settings[http2.SettingMaxConcurrentStreams]
if !ok {
actual = &ResultSkipped{"SETTINGS_MAX_CONCURRENT_STREAMS is unlimited."}
return true, nil, actual
}
// Set INITIAL_WINDOW_SIZE to zero to prevent the peer from closing the stream
settings := http2.Setting{http2.SettingInitialWindowSize, 0}
http2Conn.fr.WriteSettings(settings)
hdrs := commonHeaderFields(ctx)
hbf := http2Conn.EncodeHeader(hdrs)
var streamID uint32 = 1
for i := 0; i <= int(http2Conn.Settings[http2.SettingMaxConcurrentStreams]); i++ {
var hp http2.HeadersFrameParam
hp.StreamID = streamID
hp.EndStream = true
hp.EndHeaders = true
hp.BlockFragment = hbf
http2Conn.fr.WriteHeaders(hp)
streamID += 2
}
actualCodes := []http2.ErrCode{
http2.ErrCodeProtocol,
http2.ErrCodeRefusedStream,
}
return TestStreamError(ctx, http2Conn, actualCodes)
},
))
return tg
}