/
6_7.go
98 lines (84 loc) · 2.67 KB
/
6_7.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
package h2spec
import (
"fmt"
"golang.org/x/net/http2"
"io"
"net"
"syscall"
)
func PingTestGroup(ctx *Context) *TestGroup {
tg := NewTestGroup("6.7", "PING")
tg.AddTestCase(NewTestCase(
"Sends a PING frame",
"The endpoint MUST sends a PING frame with ACK, with an identical payload.",
func(ctx *Context) (pass bool, expected []Result, actual Result) {
pass = false
expected = []Result{
&ResultFrame{8, http2.FramePing, http2.FlagPingAck, ErrCodeDefault},
}
http2Conn := CreateHttp2Conn(ctx, true)
defer http2Conn.conn.Close()
data := [8]byte{'h', '2', 's', 'p', 'e', 'c'}
http2Conn.fr.WritePing(false, data)
loop:
for {
f, err := http2Conn.ReadFrame(ctx.Timeout)
if err != nil {
opErr, ok := err.(*net.OpError)
if err == io.EOF || (ok && opErr.Err == syscall.ECONNRESET) {
rf, ok := actual.(*ResultFrame)
if actual == nil || (ok && rf.Type != http2.FrameGoAway) {
actual = &ResultConnectionClose{}
}
} else if err == TIMEOUT {
if actual == nil {
actual = &ResultTestTimeout{}
}
} else {
actual = &ResultError{err}
}
break loop
}
switch f := f.(type) {
case *http2.PingFrame:
actual = CreateResultFrame(f)
if f.FrameHeader.Flags.Has(http2.FlagPingAck) && f.Data == data {
pass = true
break loop
}
case *http2.GoAwayFrame:
actual = CreateResultFrame(f)
break loop
default:
actual = CreateResultFrame(f)
}
}
return pass, expected, actual
},
))
tg.AddTestCase(NewTestCase(
"Sends a PING frame with the stream identifier that is not 0x0",
"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()
fmt.Fprintf(http2Conn.conn, "\x00\x00\x08\x06\x00\x00\x00\x00\x03")
fmt.Fprintf(http2Conn.conn, "\x00\x00\x00\x00\x00\x00\x00\x00")
actualCodes := []http2.ErrCode{http2.ErrCodeProtocol}
return TestConnectionError(ctx, http2Conn, actualCodes)
},
))
tg.AddTestCase(NewTestCase(
"Sends a PING frame with a length field value other than 8",
"The endpoint MUST respond with a connection error of type FRAME_SIZE_ERROR.",
func(ctx *Context) (pass bool, expected []Result, actual Result) {
http2Conn := CreateHttp2Conn(ctx, true)
defer http2Conn.conn.Close()
fmt.Fprintf(http2Conn.conn, "\x00\x00\x06\x06\x00\x00\x00\x00\x00")
fmt.Fprintf(http2Conn.conn, "\x00\x00\x00\x00\x00\x00")
actualCodes := []http2.ErrCode{http2.ErrCodeFrameSize}
return TestConnectionError(ctx, http2Conn, actualCodes)
},
))
return tg
}