-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathframe_test.go
130 lines (120 loc) · 3.56 KB
/
frame_test.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
package smux
import (
"bytes"
"encoding/binary"
"testing"
)
func TestPacking(t *testing.T) {
streamId := uint32(1)
data := bytes.Repeat([]byte{1, 2, 3, 4, 5, 6, 7, 8}, 2048+1)
frames := packing(streamId, data, false) // no sealing
if len(frames) != 2 {
t.Errorf("packing should return 2 frames, but %d", len(frames))
}
for i := 0; i < len(frames); i++ {
l, t_, f, s := parseHeader(frames[i][0:NUM_BYTES_HEADER])
length := NUM_BYTES_MAX_PAYLOAD
if i == len(frames)-1 {
length = len(data) - (i * NUM_BYTES_MAX_PAYLOAD)
}
if l != uint16(length) {
t.Errorf("packing should return length of payload %d, but %d.", uint16(length), l)
}
if t_ != uint8(TYPE_DATA) {
t.Errorf("packing should return DATA type, but %d", t_)
}
if f != uint8(FLAG_DATA_NONE) {
t.Errorf("packing should return NONE flag, but %d", f)
}
if s != streamId {
t.Errorf("packing should return stream id %d, but %d", streamId, s)
}
payload := len(frames[i]) - NUM_BYTES_HEADER
if payload != length {
t.Errorf("packing should return payload %d, but %d.", length, payload)
}
}
}
func TestPackingAndSeal(t *testing.T) {
streamId := uint32(1)
data := bytes.Repeat([]byte{1, 2, 3, 4, 5, 6, 7, 8}, 2048+1)
frames := packing(streamId, data, true) // sealing
if len(frames) != 2 {
t.Errorf("packing should return 2 frames, but %d", len(frames))
}
for i := 0; i < len(frames); i++ {
l, t_, f, s := parseHeader(frames[i][0:NUM_BYTES_HEADER])
length := NUM_BYTES_MAX_PAYLOAD
if i == len(frames)-1 {
length = len(data) - (i * NUM_BYTES_MAX_PAYLOAD)
}
if l != uint16(length) {
t.Errorf("packing should return length of payload %d, but %d.", uint16(length), l)
}
if t_ != uint8(TYPE_DATA) {
t.Errorf("packing should return DATA type, but %d", t_)
}
if i == len(frames)-1 {
// end
if f != uint8(FLAG_DATA_END_STREAM) {
t.Errorf("packing should return END_STREAM flag, but %d", f)
}
} else {
// continuous
if f != uint8(FLAG_DATA_NONE) {
t.Errorf("packing should return NONE flag, but %d", f)
}
}
if s != streamId {
t.Errorf("packing should return stream id %d, but %d", streamId, s)
}
payload := len(frames[i]) - NUM_BYTES_HEADER
if payload != length {
t.Errorf("packing should return payload %d, but %d.", length, payload)
}
}
}
func TestSealing(t *testing.T) {
streamId := uint32(1)
endFrame := sealing(streamId)
if len(endFrame) != NUM_BYTES_HEADER {
t.Errorf("sealing should return only header frame.")
}
l, t_, f, s := parseHeader(endFrame)
if l != 0 {
t.Errorf("sealing should not return payload.")
}
if t_ != uint8(TYPE_DATA) {
t.Errorf("sealing should return DATA type, but %d", t_)
}
if f != uint8(FLAG_DATA_END_STREAM) {
t.Errorf("sealing should return END_STREAM flag, but %d", f)
}
if s != streamId {
t.Errorf("sealing should return stream id %d, but %d", streamId, s)
}
}
func TestParseHeader(t *testing.T) {
header := make([]byte, NUM_BYTES_HEADER)
length := uint16(NUM_BYTES_MAX_PAYLOAD)
typ := uint8(TYPE_DATA)
flag := uint8(FLAG_DATA_END_STREAM)
streamId := uint32(MAX_STREAM_ID)
binary.BigEndian.PutUint16(header[0:2], length)
header[2] = typ
header[3] = flag
binary.BigEndian.PutUint32(header[4:], streamId)
l, t_, f, s := parseHeader(header)
if l != length {
t.Errorf("parseHeader should return length %d, but %d", length, l)
}
if t_ != typ {
t.Errorf("parseHeader should return type %d, but %d", typ, t_)
}
if f != flag {
t.Errorf("parseHeader should return flag %d, but %d", flag, f)
}
if s != streamId {
t.Errorf("parseHeader should return streamId %d, but %d", streamId, s)
}
}