-
Notifications
You must be signed in to change notification settings - Fork 8
/
packet_test.go
159 lines (130 loc) · 3.58 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
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
package rcon
import (
"bytes"
"encoding/binary"
"errors"
"io"
"testing"
)
func TestNewPacket(t *testing.T) {
body := []byte("testdata")
packet := NewPacket(SERVERDATA_RESPONSE_VALUE, 42, string(body))
if packet.Body() != string(body) {
t.Errorf("%q, want %q", packet.Body(), body)
}
want := int32(len([]byte(body))) + PacketHeaderSize + PacketPaddingSize
if packet.Size != want {
t.Errorf("got %d, want %d", packet.Size, want)
}
}
func TestPacket_WriteTo(t *testing.T) {
t.Run("check bytes written", func(t *testing.T) {
body := []byte("testdata")
packet := NewPacket(SERVERDATA_RESPONSE_VALUE, 42, string(body))
var buffer bytes.Buffer
n, err := packet.WriteTo(&buffer)
if err != nil {
t.Fatal(err)
}
wantN := packet.Size + 4
if n != int64(wantN) {
t.Errorf("got %d, want %d", n, int64(wantN))
}
})
}
func TestPacket_ReadFrom(t *testing.T) {
t.Run("check read", func(t *testing.T) {
body := []byte("testdata")
packetWant := NewPacket(SERVERDATA_RESPONSE_VALUE, 42, string(body))
var buffer bytes.Buffer
nWant, err := packetWant.WriteTo(&buffer)
if err != nil {
t.Fatal(err)
}
packetGot := new(Packet)
nGot, err := packetGot.ReadFrom(&buffer)
if err != nil {
t.Fatal(err)
}
if nGot != nWant {
t.Fatalf("got %d, want %d", nGot, nWant)
}
if packetGot.Body() != packetWant.Body() {
t.Fatalf("got %q, want %q", packetGot.body, packetWant.body)
}
})
t.Run("EOF", func(t *testing.T) {
var buffer bytes.Buffer
packetGot := new(Packet)
nGot, err := packetGot.ReadFrom(&buffer)
if !errors.Is(err, io.EOF) {
t.Fatalf("got %q, want %q", err, io.EOF)
}
if nGot != 0 {
t.Fatalf("got %d, want %d", nGot, 0)
}
})
t.Run("response too small", func(t *testing.T) {
var buffer bytes.Buffer
packetGot := new(Packet)
binary.Write(&buffer, binary.LittleEndian, packetGot.Size)
_, err := packetGot.ReadFrom(&buffer)
if !errors.Is(err, ErrResponseTooSmall) {
t.Fatalf("got %q, want %q", err, ErrResponseTooSmall)
}
})
t.Run("EOF 2", func(t *testing.T) {
var buffer bytes.Buffer
binary.Write(&buffer, binary.LittleEndian, int32(18))
packetGot := new(Packet)
nGot, err := packetGot.ReadFrom(&buffer)
if !errors.Is(err, io.EOF) {
t.Fatalf("got %q, want %q", err, io.EOF)
}
if nGot != 4 {
t.Fatalf("got %d, want %d", nGot, 4)
}
})
t.Run("EOF 3", func(t *testing.T) {
var buffer bytes.Buffer
binary.Write(&buffer, binary.LittleEndian, int32(18))
binary.Write(&buffer, binary.LittleEndian, int32(42))
packetGot := new(Packet)
nGot, err := packetGot.ReadFrom(&buffer)
if !errors.Is(err, io.EOF) {
t.Fatalf("got %q, want %q", err, io.EOF)
}
if nGot != 8 {
t.Fatalf("got %d, want %d", nGot, 8)
}
})
t.Run("padding", func(t *testing.T) {
body := []byte("testdata")
packetWant := NewPacket(SERVERDATA_RESPONSE_VALUE, 42, string(body))
packetWant.Size = 10
var buffer bytes.Buffer
_, err := packetWant.WriteTo(&buffer)
if err != nil {
t.Fatal(err)
}
packetGot := new(Packet)
_, err = packetGot.ReadFrom(&buffer)
if !errors.Is(err, ErrInvalidPacketPadding) {
t.Fatalf("got %q, want %q", err, ErrInvalidPacketPadding)
}
})
t.Run("EOF 4", func(t *testing.T) {
var buffer bytes.Buffer
binary.Write(&buffer, binary.LittleEndian, int32(18))
binary.Write(&buffer, binary.LittleEndian, int32(42))
buffer.Write(append([]byte("testdata"), 0x00, 0x00))
packetGot := new(Packet)
nGot, err := packetGot.ReadFrom(&buffer)
if !errors.Is(err, io.EOF) {
t.Fatalf("got %q, want %q", err, io.EOF)
}
if nGot != 18 {
t.Fatalf("got %d, want %d", nGot, 18)
}
})
}