rst_test.go 2.64 KB
Newer Older
Jeromy's avatar
Jeromy committed
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
package frame

import (
	"reflect"
	"testing"
)

type RstTestParams struct {
	streamId  StreamId
	errorCode ErrorCode
}

func TestSerializeRst(t *testing.T) {
	t.Parallel()

	cases := []struct {
		params   RstTestParams
		expected []byte
	}{
		{
			RstTestParams{0x49a1bb00, ProtocolError},
			[]byte{0x0, 0x4, 0x0, TypeStreamRst, 0x49, 0xa1, 0xbb, 0x00, 0x0, 0x0, 0x0, ProtocolError},
		},
		{
			RstTestParams{0x0, FlowControlError},
			[]byte{0x0, 0x4, 0x0, TypeStreamRst, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, FlowControlError},
		},
		{
			RstTestParams{streamMask, RefusedStream},
			[]byte{0x00, 0x4, 0x0, TypeStreamRst, 0x7F, 0xFF, 0xFF, 0xFF, 0x0, 0x0, 0x0, RefusedStream},
		},
	}

	for _, tcase := range cases {
		buf, trans := loadedTrans([]byte{})
		var f *WStreamRst = NewWStreamRst()

		if err := f.Set(tcase.params.streamId, tcase.params.errorCode); err != nil {
			t.Fatalf("Error while setting params %v!", tcase.params)
		}

		if err := f.writeTo(trans); err != nil {
			t.Fatalf("Error while writing %v!", tcase.params)
		}

		if !reflect.DeepEqual(tcase.expected, buf.Bytes()) {
			t.Errorf("Failed to serialize STREAM_RST, expected: %v got %v", tcase.expected, buf.Bytes())
		}
	}
}

func TestDeserializeRst(t *testing.T) {
	t.Parallel()

	_, trans := loadedTrans([]byte{0x00, rstBodySize, 0x0, TypeStreamRst, 0x7F, 0xFF, 0xFF, 0xFF, 0x0, 0x0, 0x0, RefusedStream})
	h := newHeader()
	if err := h.readFrom(trans); err != nil {
		t.Fatalf("Failed to read header: %v", err)
	}
	var f RStreamRst
	f.Header = h
	if err := f.readFrom(trans); err != nil {
		t.Fatalf("Error while reading rst frame: %v", err)
	}

	if f.ErrorCode() != RefusedStream {
		t.Errorf("Expected error code %d but got %d", RefusedStream, f.ErrorCode())
	}
}

// test a bad frame length of rstBodySize+1
func TestBadLengthRst(t *testing.T) {
	t.Parallel()

	_, trans := loadedTrans([]byte{0x00, rstBodySize + 1, 0x0, TypeStreamRst, 0x7F, 0xFF, 0xFF, 0xFF, 0x0, 0x0, 0x0, 0x0})
	h := newHeader()
	if err := h.readFrom(trans); err != nil {
		t.Fatalf("Failed to read header: %v", err)
	}
	var f RStreamRst
	f.Header = h
	if err := f.readFrom(trans); err == nil {
		t.Errorf("Expected error when setting bad rst frame length, got none.")
	}
}

// test fewer than rstBodySize bytes available after header
func TestShortReadRst(t *testing.T) {
	t.Parallel()

	_, trans := loadedTrans([]byte{0x00, rstBodySize, 0x0, TypeStreamRst, 0x7F, 0xFF, 0xFF, 0xFF, 0x1})
	h := newHeader()
	if err := h.readFrom(trans); err != nil {
		t.Fatalf("Failed to read header: %v", err)
	}
	var f RStreamRst
	f.Header = h
	if err := f.readFrom(trans); err == nil {
		t.Errorf("Expected error when reading incomplete frame, got none.")
	}
}