-
Notifications
You must be signed in to change notification settings - Fork 54
/
packet_buffer_test.go
211 lines (192 loc) · 4.7 KB
/
packet_buffer_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
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package rtcp
import (
"bytes"
"errors"
"fmt"
"testing"
)
func TestWrite(t *testing.T) {
type Subtree struct {
SubA uint32
SubB []uint8
}
s := struct {
A uint8
Z uint32 `encoding:"omit"`
B uint16
C uint32
D uint64
_ uint8
E []uint16
F Subtree
G []Subtree
}{
0xf8,
0x01234567,
0x1234,
0x56789ABC,
0x0102030405060708,
0x12,
[]uint16{0x0E, 0x02FF},
Subtree{0x11223344, []uint8{9, 8, 7, 6, 5, 4, 3, 2, 1}},
[]Subtree{{0x01, []uint8{1, 2, 3, 4}}, {0x02, []uint8{5, 6, 7, 8}}},
}
expected := []byte{
0xf8,
0x12, 0x34,
0x56, 0x78, 0x9A, 0xBC,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x00,
0x00, 0x0E, 0x02, 0xFF,
0x11, 0x22, 0x33, 0x44, 9, 8, 7, 6, 5, 4, 3, 2, 1,
0x00, 0x00, 0x00, 0x01, 1, 2, 3, 4, 0x00, 0x00, 0x00, 0x02, 5, 6, 7, 8,
}
size := wireSize(s)
if size != len(expected) {
t.Fatalf("wireSize() returned unexpected value. Expected %v, got %v", len(expected), size)
}
raw := make([]byte, len(expected))
buffer := packetBuffer{bytes: raw}
err := buffer.write(s)
if err != nil {
t.Fatalf("Serialization failed. Err = %v", err)
}
if !bytes.Equal(raw, expected) {
t.Fatalf("Serialization failed. Wanted %v, got %v", expected, raw)
}
// Check for overflow
raw = make([]byte, len(expected)-1)
buffer = packetBuffer{bytes: raw}
err = buffer.write(s)
if !errors.Is(err, errWrongMarshalSize) {
t.Fatalf("Serialization failed. Err = %v", err)
}
}
func TestReadUint8(t *testing.T) {
const expected = 0x01
raw := []byte{expected}
output := uint8(0)
buffer := packetBuffer{bytes: raw}
err := buffer.read(&output)
if err != nil {
t.Fatalf("Value parsing failed. Err = %v", err)
}
if output != expected {
t.Fatalf("Reading uint8 failed. Wanted %X, got %X", expected, output)
}
}
func TestReadUint16(t *testing.T) {
const expected = 0x0102
raw := []byte{0x01, 0x02}
output := uint16(0)
buffer := packetBuffer{bytes: raw}
err := buffer.read(&output)
if err != nil {
t.Fatalf("Value parsing failed. Err = %v", err)
}
if output != expected {
t.Fatalf("Reading uint16 failed. Wanted %X, got %X", expected, output)
}
}
func TestReadUint32(t *testing.T) {
const expected = 0x01020304
raw := []byte{0x01, 0x02, 0x03, 0x04}
output := uint32(0)
buffer := packetBuffer{bytes: raw}
err := buffer.read(&output)
if err != nil {
t.Fatalf("Value parsing failed. Err = %v", err)
}
if output != expected {
t.Fatalf("Reading uint32 failed. Wanted %X, got %X", expected, output)
}
}
func TestReadUint64(t *testing.T) {
expected := uint64(0x0102030405060708)
raw := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
output := uint64(0)
buffer := packetBuffer{bytes: raw}
err := buffer.read(&output)
if err != nil {
t.Fatalf("Value parsing failed. Err = %v", err)
}
if output != expected {
t.Fatalf("Reading uint64 failed. Wanted %X, got %X", expected, output)
}
}
func TestReadStruct(t *testing.T) {
type S struct {
A uint8
B uint16
C uint32
D uint64
}
expected := S{0x01, 0x0203, 0x04050607, 0x08090A0B0C0D0E0F}
raw := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}
var output S
buffer := packetBuffer{bytes: raw}
err := buffer.read(&output)
if err != nil {
t.Fatalf("Struct parsing failed. Err = %v", err)
}
if output != expected {
t.Fatalf("Reading struct failed. Wanted %v, got %v", expected, output)
}
}
func TestReadSlice(t *testing.T) {
expected := []uint16{0x0102, 0x0304, 0x0506, 0x0708}
raw := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
var output []uint16
buffer := packetBuffer{bytes: raw}
err := buffer.read(&output)
if err != nil {
t.Fatalf("Slice parsing failed. Err = %v", err)
}
if fmt.Sprintf("%x", output) != fmt.Sprintf("%x", expected) {
t.Fatalf("Reading struct failed. Wanted %v, got %v", expected, output)
}
}
func TestReadComplex(t *testing.T) {
raw := []byte{
0xf8,
0x12, 0x34,
0x56, 0x78, 0x9A, 0xBC,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x12,
0x11, 0x22, 0x33, 0x44, 9,
0x00, 0x00, 0x00, 0x01, 1, 0x00, 0x00, 0x00, 0x02, 5,
}
type Subtree struct {
SubA uint32
SubB uint8
}
type Tree struct {
A uint8
B uint16
C uint32
D uint64
_ uint8
F Subtree
G []Subtree
}
expected := Tree{
0xf8,
0x1234,
0x56789ABC,
0x0102030405060708,
0x00,
Subtree{0x11223344, 9},
[]Subtree{{0x01, 1}, {0x02, 5}},
}
var output Tree
buffer := packetBuffer{bytes: raw}
err := buffer.read(&output)
if err != nil {
t.Fatalf("Complex parsing failed. Err = %v", err)
}
if fmt.Sprintf("%x", output) != fmt.Sprintf("%x", expected) {
t.Fatalf("Reading struct failed. Wanted %v, got %v", expected, output)
}
}