-
Notifications
You must be signed in to change notification settings - Fork 1
/
packet_test.go
96 lines (84 loc) · 2.33 KB
/
packet_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
// (c) MASSIVE ART WebServices GmbH
//
// This source file is subject to the MIT license that is bundled
// with this source code in the file LICENSE.
package engineio
import (
"bytes"
"testing"
)
func TestPacketDecode(t *testing.T) {
data := []byte("8:4aaaaaaa")
packets, err := decode(data)
if err != nil {
t.Fatalf("decode 1: %v", err)
}
if len(packets) != 1 {
t.Fatalf("decode 1: expect 1 packet, got %d", len(packets))
}
if packets[0].Type != "4" {
t.Fatalf("decode 1: expect packet type \"4\", got %q", packets[0].Type)
}
if bytes.Compare(packets[0].Data, []byte("aaaaaaa")) != 0 {
t.Fatalf("decode 1: expect packet data \"aaaaaaa\", got %q", packets[0].Data)
}
data = []byte("8:4aaaaaaa10:4xxxxxxxxx4:2bbb")
packets, err = decode(data)
if err != nil {
t.Fatalf("decode 3: %v", err)
}
if len(packets) != 3 {
t.Fatalf("decode 3: expect 3 packets, got %d", len(packets))
}
for i, ty := range []string{"4", "4", "2"} {
if packets[i].Type != ty {
t.Fatalf("decode: expect packet type %q, got %q", ty, packets[i].Type)
}
}
for i, ty := range []string{"aaaaaaa", "xxxxxxxxx", "bbb"} {
if bytes.Compare(packets[0].Data, []byte("aaaaaaa")) != 0 {
t.Fatalf("decode: expect packet data %q, got %q", ty, packets[i].Data)
}
}
data = []byte(`15:4{"test":"aaa"}`)
packets, err = decode(data)
if err != nil {
t.Fatalf("decode 4: %v", err)
}
if len(packets) != 1 {
t.Fatalf("decode 4: expect 1 packet, got %d", len(packets))
}
if packets[0].Type != "4" {
t.Fatalf("decode 4: expect packet type \"4\", got %q", packets[0].Type)
}
if bytes.Compare(packets[0].Data, []byte(`{"test":"aaa"}`)) != 0 {
t.Fatalf("decode 4: expect packet data \"{\"test\":\"aaa\"}\", got %q", packets[0].Data)
}
}
func TestInvalidPacket(t *testing.T) {
data := []byte("8:4aaa")
_, err := decode(data)
if err == nil {
t.Fatalf("invalid 1: expected non nil err")
}
data = []byte("2:4aaaaaaaaaa")
_, err = decode(data)
if err == nil {
t.Fatalf("invalid 2: expected non nil err")
}
data = []byte("8:4aaa34:4aaa")
_, err = decode(data)
if err == nil {
t.Fatalf("invalid 3: expected non nil err")
}
data = []byte("3:4::::::::::")
_, err = decode(data)
if err == nil {
t.Fatalf("invalid 4: expected non nil err")
}
data = []byte("3:4")
_, err = decode(data)
if err == nil {
t.Fatalf("invalid 5: expected non nil err")
}
}