/
verifier.go
360 lines (303 loc) · 6.88 KB
/
verifier.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
349
350
351
352
353
354
355
356
357
358
359
360
package spec
import (
"fmt"
"reflect"
"golang.org/x/net/http2"
)
const (
ExpectedConnectionClosed = "Connection closed"
ExpectedStreamClosed = "Stream closed"
ExpectedGoAwayFrame = "GOAWAY Frame (Error Code: %s)"
ExpectedRSTStreamFrame = "RST_STREAM Frame (Error Code: %s)"
)
// VerifyConnectionClose verifies whether the connection was closed.
func VerifyConnectionClose(conn *Conn) error {
var actual Event
passed := false
for !conn.Closed {
event := conn.WaitEvent()
switch ev := event.(type) {
case ConnectionClosedEvent:
passed = true
case TimeoutEvent:
if actual == nil {
actual = ev
}
default:
actual = ev
}
if passed {
break
}
}
if !passed {
return &TestError{
Expected: []string{ExpectedConnectionClosed},
Actual: actual.String(),
}
}
return nil
}
// VerifyConnectionError verifies whether a connection error of HTTP/2
// has occurred.
func VerifyConnectionError(conn *Conn, codes ...http2.ErrCode) error {
var actual Event
passed := false
for !conn.Closed {
ev := conn.WaitEvent()
switch event := ev.(type) {
case ConnectionClosedEvent:
passed = true
case GoAwayFrameEvent:
passed = VerifyErrorCode(codes, event.ErrCode)
case TimeoutEvent:
if actual == nil {
actual = event
}
default:
actual = event
}
if passed {
break
}
}
if !passed {
expected := []string{}
for _, code := range codes {
expected = append(expected, fmt.Sprintf(ExpectedGoAwayFrame, code))
}
expected = append(expected, ExpectedConnectionClosed)
return &TestError{
Expected: expected,
Actual: actual.String(),
}
}
return nil
}
// VerifyStreamError verifies whether a stream error of HTTP/2
// has occurred.
func VerifyStreamError(conn *Conn, codes ...http2.ErrCode) error {
var actual Event
passed := false
for !conn.Closed {
ev := conn.WaitEvent()
switch event := ev.(type) {
case ConnectionClosedEvent:
passed = true
case GoAwayFrameEvent:
passed = VerifyErrorCode(codes, event.ErrCode)
case RSTStreamFrameEvent:
passed = VerifyErrorCode(codes, event.ErrCode)
case TimeoutEvent:
if actual == nil {
actual = event
}
default:
actual = event
}
if passed {
break
}
}
if !passed {
expected := []string{}
for _, code := range codes {
expected = append(expected, fmt.Sprintf(ExpectedGoAwayFrame, code))
expected = append(expected, fmt.Sprintf(ExpectedRSTStreamFrame, code))
}
expected = append(expected, ExpectedConnectionClosed)
return &TestError{
Expected: expected,
Actual: actual.String(),
}
}
return nil
}
// VerifyStreamClose verifies whether a stream close of HTTP/2
// has occurred.
func VerifyStreamClose(conn *Conn) error {
var actual Event
passed := false
for !conn.Closed {
ev := conn.WaitEvent()
switch event := ev.(type) {
case DataFrameEvent:
if event.StreamEnded() {
passed = true
}
case HeadersFrameEvent:
if event.StreamEnded() {
passed = true
}
case RSTStreamFrameEvent:
if event.ErrCode == http2.ErrCodeNo {
passed = true
}
case TimeoutEvent:
if actual == nil {
actual = event
}
default:
actual = event
}
if passed {
break
}
}
if !passed {
return &TestError{
Expected: []string{ExpectedStreamClosed},
Actual: actual.String(),
}
}
return nil
}
// VerifyHeadersFrame verifies whether a HEADERS frame with specified
// stream ID has received.
func VerifyHeadersFrame(conn *Conn, streamID uint32) error {
actual, passed := conn.WaitEventByType(EventHeadersFrame)
switch event := actual.(type) {
case HeadersFrameEvent:
passed = (event.Header().StreamID == streamID)
default:
passed = false
}
if !passed {
expected := []string{
fmt.Sprintf("HEADERS Frame (stream_id:%d)", streamID),
}
return &TestError{
Expected: expected,
Actual: actual.String(),
}
}
return nil
}
// VerifySettingsFrameWithAck verifies whether a SETTINGS frame with
// ACK flag has received.
func VerifySettingsFrameWithAck(conn *Conn) error {
actual, passed := conn.WaitEventByType(EventSettingsFrame)
switch event := actual.(type) {
case SettingsFrameEvent:
passed = event.IsAck()
default:
passed = false
}
if !passed {
expected := []string{
"SETTINGS Frame (length:0, flags:0x01, stream_id:0)",
}
return &TestError{
Expected: expected,
Actual: actual.String(),
}
}
return nil
}
// VerifyPingFrameWithAck verifies whether a PING frame with ACK flag
// has received.
func VerifyPingFrameWithAck(conn *Conn, data [8]byte) error {
actual, passed := conn.WaitEventByType(EventPingFrame)
switch event := actual.(type) {
case PingFrameEvent:
passed = event.IsAck() && reflect.DeepEqual(event.Data, data)
default:
passed = false
}
if !passed {
var actualStr string
expected := []string{
fmt.Sprintf("PING Frame (length:8, flags:0x01, stream_id:0, opaque_data:%s)", data),
}
f, ok := actual.(PingFrameEvent)
if ok {
header := f.Header()
actualStr = fmt.Sprintf(
"PING Frame ((length:%d, flags:0x%02x, stream_id:%d, opaque_data: %s)",
header.Length,
header.Flags,
header.StreamID,
f.Data,
)
} else {
actualStr = actual.String()
}
return &TestError{
Expected: expected,
Actual: actualStr,
}
}
return nil
}
// VerifyPingFrameOrConnectionClose verifies whether a PING frame with
// ACK flag has received or the connection was closed.
func VerifyPingFrameOrConnectionClose(conn *Conn, data [8]byte) error {
var actual Event
passed := false
for !conn.Closed {
event := conn.WaitEvent()
switch ev := event.(type) {
case ConnectionClosedEvent:
passed = true
case PingFrameEvent:
passed = ev.IsAck() && reflect.DeepEqual(ev.Data, data)
case TimeoutEvent:
if actual == nil {
actual = ev
}
default:
actual = ev
}
if passed {
break
}
}
if !passed {
var actualStr string
expected := []string{
ExpectedConnectionClosed,
fmt.Sprintf("PING Frame (length:8, flags:0x01, stream_id:0, opaque_data:%s)", data),
}
f, ok := actual.(PingFrameEvent)
if ok {
header := f.Header()
actualStr = fmt.Sprintf(
"PING Frame ((length:%d, flags:0x%02x, stream_id:%d, opaque_data: %s)",
header.Length,
header.Flags,
header.StreamID,
f.Data,
)
} else {
actualStr = actual.String()
}
return &TestError{
Expected: expected,
Actual: actualStr,
}
}
return nil
}
// VerifyEventType verifies whether a frame with specified type
// has received.
func VerifyEventType(conn *Conn, et EventType) error {
var actual Event
actual, passed := conn.WaitEventByType(et)
if !passed {
return &TestError{
Expected: []string{et.String()},
Actual: actual.String(),
}
}
return nil
}
// VerifyErrorCode verifies whether the specified error code is
// the expected error code.
func VerifyErrorCode(codes []http2.ErrCode, code http2.ErrCode) bool {
for _, c := range codes {
if c == code {
return true
}
}
return false
}