forked from zhangpeihao/goamf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amf3_test.go
407 lines (382 loc) · 10.2 KB
/
amf3_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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package amf
import (
"bytes"
"testing"
)
type testU29Case struct {
value uint32
expect []byte
}
var testU29Cases = []testU29Case{
{1, []byte{0x01}},
{2, []byte{0x02}},
{127, []byte{0x7F}},
{128, []byte{0x81, 0x00}},
{255, []byte{0x81, 0x7F}},
{256, []byte{0x82, 0x00}},
{0x3FFF, []byte{0xFF, 0x7F}},
{0x4000, []byte{0x81, 0x80, 0x00}},
{0x7FFF, []byte{0x81, 0xFF, 0x7F}},
{0x8000, []byte{0x82, 0x80, 0x00}},
{0x1FFFFF, []byte{0xFF, 0xFF, 0x7F}},
{0x200000, []byte{0x80, 0xC0, 0x80, 0x00}},
{0x3FFFFF, []byte{0x80, 0xFF, 0xFF, 0xFF}},
{0x400000, []byte{0x81, 0x80, 0x80, 0x00}},
{0x0FFFFFFF, []byte{0xBF, 0xFF, 0xFF, 0xFF}},
}
func TestWriteU29(t *testing.T) {
for _, c := range testU29Cases {
buf := new(bytes.Buffer)
n, err := AMF3_WriteU29(buf, c.value)
if err != nil {
t.Errorf("AMF3_WriteU29(%d) error: %s", c.value, err)
} else {
if n != len(c.expect) {
t.Errorf("AMF3_WriteU29 expect n %x got %x", len(c.expect), n)
}
got := buf.Bytes()
if !bytes.Equal(c.expect, got) {
t.Errorf("AMF3_WriteU29 expect buffer %x got %x", c.expect, got)
}
}
}
}
func TestAMF3_WriteUTF8(t *testing.T) {
buf := new(bytes.Buffer)
n, err := AMF3_WriteUTF8(buf, "foo")
if err != nil {
t.Errorf("test for %s error: %s", "foo", err)
} else {
if n != 4 {
t.Errorf("AMF3_WriteU29 expect n %x got %x", 4, n)
}
expect := []byte{0x07, 'f', 'o', 'o'}
got := buf.Bytes()
if !bytes.Equal(expect, got) {
t.Errorf("AMF3_WriteUTF8 expect buffer %x got %x", expect, got)
}
}
buf = new(bytes.Buffer)
n, err = AMF3_WriteUTF8(buf, "你好")
if err != nil {
t.Errorf("test for %s error: %s", "你好", err)
} else {
if n != 7 {
t.Errorf("AMF3_WriteU29 expect n %x got %x", 7, n)
}
expect := []byte{0x0D, 0xe4, 0xbd, 0xa0, 0xe5, 0xa5, 0xbd}
got := buf.Bytes()
if !bytes.Equal(expect, got) {
t.Errorf("AMF3_WriteUTF8 expect buffer %x got %x", expect, got)
}
}
}
func TestAMF3_EncodeString(t *testing.T) {
buf := new(bytes.Buffer)
n, err := AMF3_WriteString(buf, "foo")
if err != nil {
t.Fatalf("AMF3_WriteString error: %s", err)
}
if n != 5 {
t.Errorf("AMF3_WriteString return n: %d\n", n)
}
expect := []byte{0x06, 0x07, 'f', 'o', 'o'}
got := buf.Bytes()
if !bytes.Equal(expect, got) {
t.Errorf("AMF3_WriteString expect %x got %x", expect, got)
}
}
func TestAMF3_EncodeFloat(t *testing.T) {
buf := new(bytes.Buffer)
n, err := AMF3_WriteDouble(buf, float64(1.2))
if err != nil {
t.Fatalf("AMF3_WriteDouble error: %s", err)
}
if n != 9 {
t.Errorf("AMF3_WriteDouble return n: %d\n", n)
}
if buf.Len() != 9 {
t.Errorf("AMF3_WriteDouble writen buffer len: %d\n", buf.Len())
}
expect := []byte{0x05, 0x3f, 0xf3, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33}
got := buf.Bytes()
if !bytes.Equal(expect, got) {
t.Fatalf("AMF3_WriteDouble expect % x got % x", expect, got)
}
}
func TestAMF3_EncodeBoolean(t *testing.T) {
buf := new(bytes.Buffer)
n, err := AMF3_WriteBoolean(buf, true)
if err != nil {
t.Fatalf("AMF3_WriteBoolean error: %s", err)
}
if n != 1 {
t.Errorf("AMF3_WriteBoolean return n: %d\n", n)
}
expect := []byte{0x03}
got := buf.Bytes()
if !bytes.Equal(expect, got) {
t.Errorf("AMF3_WriteBoolean(true) expect %x got %x", expect, got)
}
buf = new(bytes.Buffer)
n, err = AMF3_WriteBoolean(buf, false)
if err != nil {
t.Fatalf("AMF3_WriteBoolean error: %s", err)
}
if n != 1 {
t.Errorf("AMF3_WriteBoolean return n: %d\n", n)
}
expect = []byte{0x02}
got = buf.Bytes()
if !bytes.Equal(expect, got) {
t.Errorf("AMF3_WriteBoolean(false) expect %x got %x", expect, got)
}
}
func TestAMF3_EncodeNull(t *testing.T) {
buf := new(bytes.Buffer)
n, err := AMF3_WriteNull(buf)
if err != nil {
t.Fatalf("AMF3_WriteNull error: %s", err)
}
if n != 1 {
t.Errorf("AMF3_WriteNull return n: %d\n", n)
}
expect := []byte{0x01}
got := buf.Bytes()
if !bytes.Equal(expect, got) {
t.Errorf("AMF3_WriteNull expect %x got %x", expect, got)
}
}
func TestAMF3_EncodeUndefined(t *testing.T) {
buf := new(bytes.Buffer)
n, err := AMF3_WriteUndefined(buf)
if err != nil {
t.Fatalf("AMF3_WriteUndefined error: %s", err)
}
if n != 1 {
t.Errorf("AMF3_WriteUndefined return n: %d\n", n)
}
expect := []byte{0x00}
got := buf.Bytes()
if !bytes.Equal(expect, got) {
t.Errorf("AMF3_WriteUndefined expect %x got %x", expect, got)
}
}
func TestAMF3_EncodeObject(t *testing.T) {
buf := new(bytes.Buffer)
obj := Object{"0": "a", "1": "b", "2": "c"}
n, err := AMF3_WriteObject(buf, obj)
if err != nil {
t.Fatalf("AMF3_WriteObject %s", err)
}
if n != 19 {
t.Errorf("AMF3_WriteObject return n: %d\n", n)
}
if buf.Len() != 19 {
t.Errorf("AMF3_WriteDouble writen buffer len: %d\n", buf.Len())
}
/*
expect := []byte{0x0A, 0x0B, 0x01
0x03, '0', 0x06, 0x03, 'a',
0x03, '1', 0x06, 0x03, 'b',
0x03, '2', 0x06, 0x03, 'c',
0x01,
}
*/
got := buf.Bytes()
if got[0] != 0x0A || got[1] != 0x0B || got[2] != 0x01 {
t.Errorf("AMF3_WriteObject got[:3]: %x, expect %x\n", got[:3], []byte{0x0A, 0x0B, 0x01})
}
names := make([]string, len(obj))
i := 0
for k, _ := range obj {
names[i] = k
i++
}
for i = 0; i < 3; i++ {
if got[3+i*5] != 0x03 {
t.Errorf("AMF3_WriteObject item[%d] name len: 0x%x, expect 0x03\n", i, got[3+i*5])
}
name := string(got[3+i*5+1 : 3+i*5+2])
found := false
for index, n := range names {
if n == name {
names = append(names[:index], names[index+1:]...)
found = true
break
}
}
if !found {
t.Errorf("AMF3_WriteObject item[%d] name: %s not in expected slices: %v\n", i, name, names)
}
if got[3+i*5+2] != 0x06 || got[3+i*5+3] != 0x03 {
t.Errorf("AMF3_WriteObject item[%d] marker: %x, expect 0603\n", i, got[(3+i*5+2):(3+i*5+4)])
}
value := string(got[(3 + i*5 + 4):(3 + i*5 + 5)])
if obj[name] != value {
t.Errorf("AMF3_WriteObject item[%d] value: %s, expect: %s\n", i, value, obj[name])
}
}
}
func TestAMF3_EncodeByteArray(t *testing.T) {
buf := new(bytes.Buffer)
b := []byte("foo")
n, err := AMF3_WriteValue(buf, b)
if err != nil {
t.Fatalf("TestAMF3_EncodeByteArray error: %s", err)
}
if n != 5 {
t.Errorf("TestAMF3_EncodeByteArray return n: %d\n", n)
}
expect := []byte{0x0c, 0x07, 'f', 'o', 'o'}
got := buf.Bytes()
if !bytes.Equal(expect, got) {
t.Errorf("TestAMF3_EncodeByteArray expect %x got %x", expect, got)
}
}
func TestAMF3_EncodeArray(t *testing.T) {
buf := new(bytes.Buffer)
a := []string{
"1", "2", "3", "4", "5", "6", "7", "8", "9",
}
n, err := AMF3_WriteValue(buf, a)
if err != nil {
t.Fatalf("TestAMF3_EncodeArray error: %s", err)
}
if n != 30 {
t.Errorf("TestAMF3_EncodeArray return n: %d\n", n)
}
expect := []byte{0x09, 0x13, 0x01,
0x06, 0x03, '1',
0x06, 0x03, '2',
0x06, 0x03, '3',
0x06, 0x03, '4',
0x06, 0x03, '5',
0x06, 0x03, '6',
0x06, 0x03, '7',
0x06, 0x03, '8',
0x06, 0x03, '9',
}
got := buf.Bytes()
if !bytes.Equal(expect, got) {
t.Errorf("TestAMF3_EncodeArray expect %x got %x", expect, got)
}
}
//-------------------------------------------------------
func TestAMF3_ReadU29(t *testing.T) {
for _, c := range testU29Cases {
buf := bytes.NewBuffer(c.expect)
n, err := AMF3_ReadU29(buf)
if err != nil {
t.Errorf("AMF3_ReadU29(%d) error: %s", c.value, err)
} else {
if n != c.value {
t.Errorf("AMF3_WriteU29 expect n %x got %x", c.value, n)
}
}
}
}
func TestAMF3_ReadUTF8(t *testing.T) {
expect := "foo"
buf := bytes.NewBuffer([]byte{0x07, 'f', 'o', 'o'})
got, err := AMF3_ReadUTF8(buf)
if err != nil {
t.Errorf("TestAMF3_ReadUTF8 test for %s error: %s", expect, err)
} else {
if got != expect {
t.Errorf("TestAMF3_ReadUTF8 expect %s got %s", expect, got)
}
}
}
func TestAMF3_ReadString(t *testing.T) {
expect := "foo"
buf := bytes.NewBuffer([]byte{0x06, 0x07, 'f', 'o', 'o'})
got, err := AMF3_ReadString(buf)
if err != nil {
t.Errorf("TestAMF3_ReadString test for %s error: %s", expect, err)
} else {
if got != expect {
t.Errorf("TestAMF3_ReadString expect %s got %s", expect, got)
}
}
}
func TestAMF3_DecodeDouble(t *testing.T) {
expect := float64(1.2)
buf := bytes.NewBuffer([]byte{0x05, 0x3f, 0xf3, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33})
got, err := AMF3_ReadDouble(buf)
if err != nil {
t.Fatalf("TestAMF3_DecodeDouble error: %s", err)
}
if got != expect {
t.Errorf("TestAMF3_DecodeDouble got %v, expect %v\n", got, expect)
}
}
var testAMF3_DecodeCases = []TestEncodeValueCase{
{"1.2", 1.2, []byte{0x05, 0x3f, 0xf3, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33}},
{"float64(1.2)", float64(1.2), []byte{0x05, 0x3f, 0xf3, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33}},
{"1", uint32(1), []byte{0x04, 0x01}},
{"foo", "foo", []byte{0x06, 0x07, 'f', 'o', 'o'}},
{"empty string", "", []byte{0x06, 0x01}},
{"false", false, []byte{0x02}},
{"true", true, []byte{0x03}},
{"null", nil, []byte{0x01}},
}
func TestAMF3_DecodeValue(t *testing.T) {
for _, c := range testAMF3_DecodeCases {
buf := bytes.NewReader(c.expect)
value, err := AMF3_ReadValue(buf)
if err != nil {
t.Errorf("AMF3_ReadValue(%s) error: %s", c.name, err.Error())
continue
}
if value != c.v {
t.Errorf("AMF3_ReadValue(%s) return %v, expect %v\n", c.name, value, c.v)
}
}
}
func TestAMF3_DecodeObject(t *testing.T) {
buf := bytes.NewReader(
[]byte{0x0A, 0x0B, 0x01,
0x03, '0', 0x06, 0x03, 'a',
0x03, '1', 0x06, 0x03, 'b',
0x03, '2', 0x06, 0x03, 'c',
0x01,
})
obj, err := AMF3_ReadObject(buf)
if err != nil {
t.Fatalf("TestAMF3_DecodeObject error: %s", err)
}
if len(obj) != 3 {
t.Errorf("TestAMF3_DecodeObject return len :%d, expect 3\n", len(obj))
}
expect := Object{
"0": "a",
"1": "b",
"2": "c",
}
for k, v := range obj {
if expectV, ok := expect[k]; ok {
if v != expectV {
t.Errorf("TestAMF3_DecodeObject item[%s] value unmatck! got %s, exprct %s", k, v, expectV)
}
delete(expect, k)
} else {
t.Errorf("TestAMF3_DecodeObject not found key: %s", k)
}
}
if len(expect) != 0 {
t.Errorf("TestAMF3_DecodeObject loss some items: %v", expect)
}
}
func TestAMF3_DecodeByteArray(t *testing.T) {
buf := bytes.NewReader(
[]byte{0x0c, 0x07, 'f', 'o', 'o'})
got, err := AMF3_ReadByteArray(buf)
if err != nil {
t.Fatalf("AMF3_ReadByteArray error: %s", err)
}
expect := []byte("foo")
if !bytes.Equal(expect, got) {
t.Errorf("AMF3_ReadByteArray expect %x got %x", expect, got)
}
}