-
Notifications
You must be signed in to change notification settings - Fork 1
/
packets_test.go
98 lines (93 loc) · 2.54 KB
/
packets_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
package liter_test
import (
"bytes"
"testing"
. "github.com/kmcsr/go-liter"
)
func TestPacketForward(t *testing.T) {
data := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
var (
pb *PacketBuilder = NewPacket(V1_19_2, 987)
pr *PacketReader
buf = bytes.NewBuffer(nil)
buf2 = bytes.NewBuffer(nil)
err error
)
pb.ByteArray(data)
pb.WriteTo(buf)
if pr, err = ReadPacket(V1_19_2, bytes.NewReader(buf.Bytes()), false); err != nil {
t.Fatalf("Error when read packet: %v", err)
}
if pr.Protocol() != pb.Protocol() {
t.Fatalf("Protocol not match")
}
if pr.Id() != pb.Id() {
t.Fatalf("Id not match")
}
if !bytes.Equal(pr.Bytes(), data) {
t.Fatalf("Data not match, %v != %v", pr.Bytes(), data)
}
pb.Reset(pr.Protocol(), pr.Id()).ByteArray(pr.Bytes())
pb.WriteTo(buf2)
if !bytes.Equal(buf.Bytes(), buf2.Bytes()) {
t.Fatalf("Second bytes not match")
}
}
func TestPacketForwardNoCompressed(t *testing.T) {
data := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
var (
pb *PacketBuilder = NewPacket(V1_19_2, 987)
pr *PacketReader
buf = bytes.NewBuffer(nil)
buf2 = bytes.NewBuffer(nil)
err error
)
pb.ByteArray(data)
pb.WriteCompressedTo(buf, 10000)
if pr, err = ReadPacket(V1_19_2, bytes.NewReader(buf.Bytes()), true); err != nil {
t.Fatalf("Error when read packet: %v", err)
}
if pr.Protocol() != pb.Protocol() {
t.Fatalf("Protocol not match")
}
if pr.Id() != pb.Id() {
t.Fatalf("Id not match")
}
if !bytes.Equal(pr.Bytes(), data) {
t.Fatalf("Data not match, %v != %v", pr.Bytes(), data)
}
pb.Reset(pr.Protocol(), pr.Id()).ByteArray(pr.Bytes())
pb.WriteCompressedTo(buf2, 10000)
if !bytes.Equal(buf.Bytes(), buf2.Bytes()) {
t.Fatalf("Second bytes not match")
}
}
func TestPacketForwardCompressed(t *testing.T) {
data := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
var (
pb *PacketBuilder = NewPacket(V1_19_2, 987)
pr *PacketReader
buf = bytes.NewBuffer(nil)
buf2 = bytes.NewBuffer(nil)
err error
)
pb.ByteArray(data)
pb.WriteCompressedTo(buf, 0)
if pr, err = ReadPacket(V1_19_2, bytes.NewReader(buf.Bytes()), true); err != nil {
t.Fatalf("Error when read packet: %v", err)
}
if pr.Protocol() != pb.Protocol() {
t.Fatalf("Protocol not match")
}
if pr.Id() != pb.Id() {
t.Fatalf("Id not match")
}
if !bytes.Equal(pr.Bytes(), data) {
t.Fatalf("Data not match, %v != %v", pr.Bytes(), data)
}
pb.Reset(pr.Protocol(), pr.Id()).ByteArray(pr.Bytes())
pb.WriteCompressedTo(buf2, 0)
if !bytes.Equal(buf.Bytes(), buf2.Bytes()) {
t.Fatalf("Second bytes not match")
}
}