| 1 |
package frame |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
type RstTestParams struct { |
| 9 |
streamId StreamId |
| 10 |
errorCode ErrorCode |
| 11 |
} |
| 12 |
|
| 13 |
func TestSerializeRst(t *testing.T) { |
| 14 |
t.Parallel() |
| 15 |
|
| 16 |
cases := []struct { |
| 17 |
params RstTestParams |
| 18 |
expected []byte |
| 19 |
}{ |
| 20 |
{ |
| 21 |
RstTestParams{0x49a1bb00, ProtocolError}, |
| 22 |
[]byte{0x0, 0x4, 0x0, TypeStreamRst, 0x49, 0xa1, 0xbb, 0x00, 0x0, 0x0, 0x0, ProtocolError}, |
| 23 |
}, |
| 24 |
{ |
| 25 |
RstTestParams{0x0, FlowControlError}, |
| 26 |
[]byte{0x0, 0x4, 0x0, TypeStreamRst, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, FlowControlError}, |
| 27 |
}, |
| 28 |
{ |
| 29 |
RstTestParams{streamMask, RefusedStream}, |
| 30 |
[]byte{0x00, 0x4, 0x0, TypeStreamRst, 0x7F, 0xFF, 0xFF, 0xFF, 0x0, 0x0, 0x0, RefusedStream}, |
| 31 |
}, |
| 32 |
} |
| 33 |
|
| 34 |
for _, tcase := range cases { |
| 35 |
buf, trans := loadedTrans([]byte{}) |
| 36 |
var f *WStreamRst = NewWStreamRst() |
| 37 |
|
| 38 |
if err := f.Set(tcase.params.streamId, tcase.params.errorCode); err != nil { |
| 39 |
t.Fatalf("Error while setting params %v!", tcase.params) |
| 40 |
} |
| 41 |
|
| 42 |
if err := f.writeTo(trans); err != nil { |
| 43 |
t.Fatalf("Error while writing %v!", tcase.params) |
| 44 |
} |
| 45 |
|
| 46 |
if !reflect.DeepEqual(tcase.expected, buf.Bytes()) { |
| 47 |
t.Errorf("Failed to serialize STREAM_RST, expected: %v got %v", tcase.expected, buf.Bytes()) |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
func TestDeserializeRst(t *testing.T) { |
| 53 |
t.Parallel() |
| 54 |
|
| 55 |
_, trans := loadedTrans([]byte{0x00, rstBodySize, 0x0, TypeStreamRst, 0x7F, 0xFF, 0xFF, 0xFF, 0x0, 0x0, 0x0, RefusedStream}) |
| 56 |
h := newHeader() |
| 57 |
if err := h.readFrom(trans); err != nil { |
| 58 |
t.Fatalf("Failed to read header: %v", err) |
| 59 |
} |
| 60 |
var f RStreamRst |
| 61 |
f.Header = h |
| 62 |
if err := f.readFrom(trans); err != nil { |
| 63 |
t.Fatalf("Error while reading rst frame: %v", err) |
| 64 |
} |
| 65 |
|
| 66 |
if f.ErrorCode() != RefusedStream { |
| 67 |
t.Errorf("Expected error code %d but got %d", RefusedStream, f.ErrorCode()) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
// test a bad frame length of rstBodySize+1 |
| 72 |
func TestBadLengthRst(t *testing.T) { |
| 73 |
t.Parallel() |
| 74 |
|
| 75 |
_, trans := loadedTrans([]byte{0x00, rstBodySize + 1, 0x0, TypeStreamRst, 0x7F, 0xFF, 0xFF, 0xFF, 0x0, 0x0, 0x0, 0x0}) |
| 76 |
h := newHeader() |
| 77 |
if err := h.readFrom(trans); err != nil { |
| 78 |
t.Fatalf("Failed to read header: %v", err) |
| 79 |
} |
| 80 |
var f RStreamRst |
| 81 |
f.Header = h |
| 82 |
if err := f.readFrom(trans); err == nil { |
| 83 |
t.Errorf("Expected error when setting bad rst frame length, got none.") |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// test fewer than rstBodySize bytes available after header |
| 88 |
func TestShortReadRst(t *testing.T) { |
| 89 |
t.Parallel() |
| 90 |
|
| 91 |
_, trans := loadedTrans([]byte{0x00, rstBodySize, 0x0, TypeStreamRst, 0x7F, 0xFF, 0xFF, 0xFF, 0x1}) |
| 92 |
h := newHeader() |
| 93 |
if err := h.readFrom(trans); err != nil { |
| 94 |
t.Fatalf("Failed to read header: %v", err) |
| 95 |
} |
| 96 |
var f RStreamRst |
| 97 |
f.Header = h |
| 98 |
if err := f.readFrom(trans); err == nil { |
| 99 |
t.Errorf("Expected error when reading incomplete frame, got none.") |
| 100 |
} |
| 101 |
} |