2016-10-02 16:41:58 +03:00
|
|
|
package spec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-01-22 04:56:47 +03:00
|
|
|
"reflect"
|
2016-10-02 16:41:58 +03:00
|
|
|
|
|
|
|
"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)"
|
|
|
|
)
|
|
|
|
|
2016-12-24 16:10:42 +03:00
|
|
|
// VerifyConnectionClose verifies whether the connection was closed.
|
2016-10-02 16:41:58 +03:00
|
|
|
func VerifyConnectionClose(conn *Conn) error {
|
|
|
|
var actual Event
|
|
|
|
|
|
|
|
passed := false
|
|
|
|
for !conn.Closed {
|
2016-11-05 17:47:07 +03:00
|
|
|
event := conn.WaitEvent()
|
|
|
|
|
|
|
|
switch ev := event.(type) {
|
2016-12-31 17:41:23 +03:00
|
|
|
case ConnectionClosedEvent:
|
2016-11-05 17:47:07 +03:00
|
|
|
passed = true
|
2016-12-31 17:41:23 +03:00
|
|
|
case TimeoutEvent:
|
2016-11-05 17:47:07 +03:00
|
|
|
if actual == nil {
|
|
|
|
actual = ev
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
actual = ev
|
|
|
|
}
|
|
|
|
|
2016-10-02 16:41:58 +03:00
|
|
|
if passed {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !passed {
|
|
|
|
return &TestError{
|
|
|
|
Expected: []string{ExpectedConnectionClosed},
|
|
|
|
Actual: actual.String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-24 16:10:42 +03:00
|
|
|
// VerifyConnectionError verifies whether a connection error of HTTP/2
|
|
|
|
// has occurred.
|
2016-10-02 16:41:58 +03:00
|
|
|
func VerifyConnectionError(conn *Conn, codes ...http2.ErrCode) error {
|
|
|
|
var actual Event
|
|
|
|
|
|
|
|
passed := false
|
|
|
|
for !conn.Closed {
|
2016-11-05 17:47:07 +03:00
|
|
|
ev := conn.WaitEvent()
|
2016-10-02 16:41:58 +03:00
|
|
|
|
2016-11-05 17:47:07 +03:00
|
|
|
switch event := ev.(type) {
|
2016-12-31 17:41:23 +03:00
|
|
|
case ConnectionClosedEvent:
|
2016-10-02 16:41:58 +03:00
|
|
|
passed = true
|
2016-12-31 17:41:23 +03:00
|
|
|
case GoAwayFrameEvent:
|
2016-11-05 17:47:07 +03:00
|
|
|
passed = VerifyErrorCode(codes, event.ErrCode)
|
2016-12-31 17:41:23 +03:00
|
|
|
case TimeoutEvent:
|
2016-11-05 17:47:07 +03:00
|
|
|
if actual == nil {
|
|
|
|
actual = event
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
actual = event
|
2016-10-02 16:41:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-12-24 16:10:42 +03:00
|
|
|
// VerifyStreamError verifies whether a stream error of HTTP/2
|
|
|
|
// has occurred.
|
2016-10-02 16:41:58 +03:00
|
|
|
func VerifyStreamError(conn *Conn, codes ...http2.ErrCode) error {
|
|
|
|
var actual Event
|
|
|
|
|
|
|
|
passed := false
|
|
|
|
for !conn.Closed {
|
2016-11-05 17:47:07 +03:00
|
|
|
ev := conn.WaitEvent()
|
2016-10-02 16:41:58 +03:00
|
|
|
|
2016-11-05 17:47:07 +03:00
|
|
|
switch event := ev.(type) {
|
2016-12-31 17:41:23 +03:00
|
|
|
case ConnectionClosedEvent:
|
2016-10-02 16:41:58 +03:00
|
|
|
passed = true
|
2016-12-31 17:41:23 +03:00
|
|
|
case GoAwayFrameEvent:
|
2016-11-05 17:47:07 +03:00
|
|
|
passed = VerifyErrorCode(codes, event.ErrCode)
|
2016-12-31 17:41:23 +03:00
|
|
|
case RSTStreamFrameEvent:
|
2016-11-05 17:47:07 +03:00
|
|
|
passed = VerifyErrorCode(codes, event.ErrCode)
|
2016-12-31 17:41:23 +03:00
|
|
|
case TimeoutEvent:
|
2016-11-05 17:47:07 +03:00
|
|
|
if actual == nil {
|
|
|
|
actual = event
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
actual = event
|
2016-10-02 16:41:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-12-24 16:10:42 +03:00
|
|
|
// VerifyStreamClose verifies whether a stream close of HTTP/2
|
|
|
|
// has occurred.
|
2016-10-02 16:41:58 +03:00
|
|
|
func VerifyStreamClose(conn *Conn) error {
|
|
|
|
var actual Event
|
|
|
|
|
|
|
|
passed := false
|
|
|
|
for !conn.Closed {
|
2016-11-05 17:47:07 +03:00
|
|
|
ev := conn.WaitEvent()
|
2016-10-02 16:41:58 +03:00
|
|
|
|
2016-11-05 17:47:07 +03:00
|
|
|
switch event := ev.(type) {
|
2016-12-31 17:41:23 +03:00
|
|
|
case DataFrameEvent:
|
2016-11-05 17:47:07 +03:00
|
|
|
if event.StreamEnded() {
|
2016-10-02 16:41:58 +03:00
|
|
|
passed = true
|
|
|
|
}
|
2016-12-31 17:41:23 +03:00
|
|
|
case HeadersFrameEvent:
|
2016-11-05 17:47:07 +03:00
|
|
|
if event.StreamEnded() {
|
2016-10-02 16:41:58 +03:00
|
|
|
passed = true
|
|
|
|
}
|
2017-01-22 08:47:08 +03:00
|
|
|
case RSTStreamFrameEvent:
|
|
|
|
if event.ErrCode == http2.ErrCodeNo {
|
|
|
|
passed = true
|
|
|
|
}
|
2016-12-31 17:41:23 +03:00
|
|
|
case TimeoutEvent:
|
2016-11-05 17:47:07 +03:00
|
|
|
if actual == nil {
|
|
|
|
actual = event
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
actual = event
|
2016-10-02 16:41:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if passed {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !passed {
|
|
|
|
return &TestError{
|
|
|
|
Expected: []string{ExpectedStreamClosed},
|
|
|
|
Actual: actual.String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-22 04:56:47 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2017-01-03 16:19:09 +03:00
|
|
|
// VerifyEventType verifies whether a frame with specified type
|
2016-12-24 17:23:17 +03:00
|
|
|
// has received.
|
2017-01-03 16:19:09 +03:00
|
|
|
func VerifyEventType(conn *Conn, et EventType) error {
|
2016-12-24 17:23:17 +03:00
|
|
|
var actual Event
|
|
|
|
|
2017-01-03 16:19:09 +03:00
|
|
|
actual, passed := conn.WaitEventByType(et)
|
2016-12-24 17:23:17 +03:00
|
|
|
|
|
|
|
if !passed {
|
|
|
|
return &TestError{
|
2017-01-03 16:19:09 +03:00
|
|
|
Expected: []string{et.String()},
|
2016-12-24 17:23:17 +03:00
|
|
|
Actual: actual.String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-24 16:10:42 +03:00
|
|
|
// VerifyErrorCode verifies whether the specified error code is
|
|
|
|
// the expected error code.
|
2016-10-02 16:41:58 +03:00
|
|
|
func VerifyErrorCode(codes []http2.ErrCode, code http2.ErrCode) bool {
|
|
|
|
for _, c := range codes {
|
|
|
|
if c == code {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|