This repository was archived by the owner on Feb 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathchunk_test.go
More file actions
130 lines (121 loc) · 3.32 KB
/
chunk_test.go
File metadata and controls
130 lines (121 loc) · 3.32 KB
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 rtmp
import (
"bufio"
"bytes"
"reflect"
"testing"
)
func TestReadBasicHeader(t *testing.T) {
header := []byte{0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0x14, 0x00, 0x00, 0x00, 0x00}
in := bufio.NewReader(bytes.NewBuffer(header))
actual, err := readBasicHeader(in)
if err != nil {
t.Errorf("Should be nil, but got %s", err)
}
expected := &BasicHeader{
FMT: 0,
ChunkStreamID: 3,
}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Should be %#v, but got %#v", actual, expected)
}
}
func TestGenerateBasicHeader(t *testing.T) {
expected := []byte{0x03}
bh := &BasicHeader{
FMT: 0,
ChunkStreamID: 3,
}
actual, err := genBasicHeader(bh)
if err != nil {
t.Errorf("Should be nil, but got %s", err)
}
if bytes.Compare(actual, expected) != 0 {
t.Errorf("Should be %#v, but got %#v", actual, expected)
}
}
func TestReadMessageHeader(t *testing.T) {
header := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0x14, 0x01, 0x00, 0x00, 0x00}
in := bufio.NewReader(bytes.NewBuffer(header))
actual, err := readMessageHeader(in, 0)
if err != nil {
t.Errorf("Should be nil, but got %s", err)
}
expected := &MessageHeader{
Timestamp: 0,
MessageLength: 184,
MessageTypeID: 20,
MessageStreamID: 1,
}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Should be %#v, but got %#v", actual, expected)
}
}
func TestGenerateMessageHeader(t *testing.T) {
expected := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0x14, 0x01, 0x00, 0x00, 0x00}
mh := &MessageHeader{
Timestamp: 0,
MessageLength: 184,
MessageTypeID: 20,
MessageStreamID: 1,
}
actual, err := genMessageHeader(mh, 0)
if err != nil {
t.Errorf("Should be nil, but got %s", err)
}
if bytes.Compare(actual, expected) != 0 {
t.Errorf("Should be %#v, but got %#v", actual, expected)
}
}
func TestReadChunkHeader(t *testing.T) {
// FMT (2 bits) = 00
// chunk stream id (6 bits) = 000011
// timestamp (3bytes) = 0000 0000 0000 0000 0000 0000
// message length (3 bytes) = 0000 0000 0000 0000 1011 1000
// message type id (1 bytes) = 0001 0100
// message stream id (4 bytes) = 0000 0000 0000 0000 0000 0000 0000 0000
header := []byte{0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0x14, 0x00, 0x00, 0x00, 0x00}
in := bufio.NewReader(bytes.NewBuffer(header))
actual, err := readChunkHeader(in)
if err != nil {
t.Errorf("Should be nil, but got %s", err)
}
expected := &ChunkHeader{
BasicHeader: &BasicHeader{
FMT: 0,
ChunkStreamID: 3,
},
MessageHeader: &MessageHeader{
Timestamp: 0,
MessageLength: 184,
MessageTypeID: 20,
MessageStreamID: 0,
},
ExtendedTimestamp: 0,
}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Should be %#v, but got %#v", expected, actual)
}
}
func TestGenerateChunkHeader(t *testing.T) {
expected := []byte{0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0x14, 0x00, 0x00, 0x00, 0x00}
ch := &ChunkHeader{
BasicHeader: &BasicHeader{
FMT: 0,
ChunkStreamID: 3,
},
MessageHeader: &MessageHeader{
Timestamp: 0,
MessageLength: 184,
MessageTypeID: 20,
MessageStreamID: 0,
},
}
actual, err := genChunkHeader(ch)
if err != nil {
t.Errorf("Should be nil, but got %s", err)
}
if bytes.Compare(actual, expected) != 0 {
t.Errorf("Should be %#v, but got %#v", actual, expected)
}
}