-
Notifications
You must be signed in to change notification settings - Fork 161
/
header_test.go
223 lines (215 loc) · 5.35 KB
/
header_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package gortmp
import (
"bytes"
"testing"
)
type TestBaseHeaderCase struct {
name string
data []byte
fmt uint8
csi uint32
}
var testBaseHeaderCases = []TestBaseHeaderCase{
{"Chunk strea ID(64 - 319)", []byte{byte(0x00 | 0x00), 0x00}, 0x00, 64},
{"Chunk strea ID(64 - 319)", []byte{byte(0x00 | 0x00), 0x01}, 0x00, 65},
{"Chunk strea ID(64 - 319)", []byte{byte(0x00 | 0x00), 0xff}, 0x00, 319},
{"Chunk strea ID(64 - 65599)", []byte{byte(0x00 | 0x01), 0x00, 0x01}, 0x00, 320},
{"Chunk strea ID(64 - 65599)", []byte{byte(0x00 | 0x01), 0xff, 0xff}, 0x00, 65599},
{"Chunk strea ID 2", []byte{byte(0x00 | 0x02)}, 0x00, 2},
{"Chunk strea ID(3 - 63)", []byte{byte(0x00 | 0x03)}, 0x00, 3},
{"Chunk strea ID(3 - 63)", []byte{byte(0x00 | 0x3f)}, 0x00, 63},
{"fmt 1", []byte{byte(0x40 | 0x03)}, 0x01, 3},
{"fmt 2", []byte{byte(0x80 | 0x03)}, 0x02, 3},
{"fmt 3", []byte{byte(0xC0 | 0x03)}, 0x03, 3},
}
func TestReadBaseHeader(t *testing.T) {
InitTestLogger()
for _, c := range testBaseHeaderCases {
buf := bytes.NewReader(c.data)
n, fmt, csi, err := ReadBaseHeader(buf)
if err != nil {
t.Errorf("TestReadBaseHeader(%s - fmt: %d, csi: %d) error: %s", c.name, c.fmt, c.csi, err.Error())
continue
}
if fmt != c.fmt || csi != c.csi || n != len(c.data) {
t.Errorf("TestReadBaseHeader(%s - n: %d, fmt: %d, csi: %d) got: n: %d, fmt: %d, csi: %d",
len(c.data), c.name, c.fmt, c.csi, n, fmt, csi)
}
}
}
type TestHeaderCase struct {
name string
data []byte
baseHeader []byte
header Header
absolute bool
}
var testHeaderCases = []TestHeaderCase{
{
"Type 0",
[]byte{
0x00, 0x00, 0x01, // Timestamp
0x00, 0x00, 0x02, // Message Length
0x03, // Message Type ID
0x04, 0x00, 0x00, 0x00, // Message Stream ID
},
[]byte{0x03},
Header{
Fmt: 0x00,
ChunkStreamID: 3,
Timestamp: 1,
MessageLength: 2,
MessageTypeID: 3,
MessageStreamID: 4,
ExtendedTimestamp: 0,
},
true,
},
{
"Type 0 - with externed timestamp",
[]byte{
0xff, 0xff, 0xff, // Timestamp
0x00, 0x00, 0x02, // Message Length
0x03, // Message Type ID
0x04, 0x00, 0x00, 0x00, // Message Stream ID
0x10, 0x00, 0x00, 0x00, // Externed timestamp
},
[]byte{0x03},
Header{
Fmt: 0x00,
ChunkStreamID: 3,
Timestamp: 0xffffff,
MessageLength: 2,
MessageTypeID: 3,
MessageStreamID: 4,
ExtendedTimestamp: 0x10000000,
},
true,
},
{
"Type 1",
[]byte{
0x00, 0x00, 0x11, // Timestamp
0x00, 0x00, 0x12, // Message Length
0x13, // Message Type ID
},
[]byte{0x43},
Header{
Fmt: 0x01,
ChunkStreamID: 3,
Timestamp: 0x11,
MessageLength: 0x12,
MessageTypeID: 0x13,
MessageStreamID: 0x04,
ExtendedTimestamp: 0,
},
false,
},
{
"Type 1 - with externed timestamp",
[]byte{
0xff, 0xff, 0xff, // Timestamp
0x00, 0x00, 0x12, // Message Length
0x13, // Message Type ID
0x11, 0x00, 0x00, 0x00, // Externed timestamp
},
[]byte{0x43},
Header{
Fmt: 0x01,
ChunkStreamID: 3,
Timestamp: 0xffffff,
MessageLength: 0x12,
MessageTypeID: 0x13,
MessageStreamID: 0x04,
ExtendedTimestamp: 0x11000000,
},
false,
},
{
"Type 2",
[]byte{
0x00, 0x00, 0x21, // Timestamp
},
[]byte{0x83},
Header{
Fmt: 0x02,
ChunkStreamID: 3,
Timestamp: 0x21,
MessageLength: 0x12,
MessageTypeID: 0x13,
MessageStreamID: 0x04,
ExtendedTimestamp: 0,
},
false,
},
{
"Type 3",
[]byte{}, // No data
[]byte{0xc3},
Header{
Fmt: 0x03,
ChunkStreamID: 3,
Timestamp: 0x21,
MessageLength: 0x12,
MessageTypeID: 0x13,
MessageStreamID: 0x04,
ExtendedTimestamp: 0,
},
false,
},
{
"Type 2 - with externed timestamp",
[]byte{
0xff, 0xff, 0xff, // Timestamp
0x21, 0x00, 0x00, 0x00, // Externed timestamp
},
[]byte{0x83},
Header{
Fmt: 0x02,
ChunkStreamID: 3,
Timestamp: 0xffffff,
MessageLength: 0x12,
MessageTypeID: 0x13,
MessageStreamID: 0x04,
ExtendedTimestamp: 0x21000000,
},
false,
},
}
func TestReadHeader(t *testing.T) {
InitTestLogger()
header := &Header{}
for _, c := range testHeaderCases {
buf := bytes.NewReader(c.data)
n, err := header.ReadHeader(buf, c.header.Fmt, c.header.ChunkStreamID, nil)
if err != nil {
t.Errorf("TestReadHeader(%s)\n\t%v\nerror: %s", c.name, c.header, err.Error())
continue
}
if *header != c.header {
t.Errorf("TestReadHeader(%s)\n\texpect: %v\n\tgot: %v", c.name, c.header, *header)
}
if n != len(c.data) {
t.Errorf("TestReadHeader(%s)\n\texpect n: %d\n\tgot n: %d", c.name, len(c.data), n)
}
}
}
func TestWriteHeader(t *testing.T) {
InitTestLogger()
for _, c := range testHeaderCases {
buf := new(bytes.Buffer)
n, err := c.header.Write(buf)
if err != nil {
t.Errorf("TestWriteHeader(%s)\n\t%v\nerror: %s", c.name, c.header, err.Error())
continue
}
expect := append(c.baseHeader, c.data...)
if n != len(expect) {
t.Errorf("TestWriteHeader(%s)\n\texpect length: %d\n\tn:%d", c.name, len(expect), n)
}
got := buf.Bytes()
if !bytes.Equal(expect, got) {
t.Errorf("TestWriteHeader(%s)\n\texpect: % x\n\tgot: % x", c.name, expect, got)
}
}
}