-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdecoder_test.go
215 lines (187 loc) · 7.05 KB
/
decoder_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
package xivnet_test
import (
"bytes"
"github.com/ff14wed/xivnet/v3"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gstruct"
"github.com/onsi/gomega/types"
)
func matchExpectedFrame(expectedFrame xivnet.Frame) types.GomegaMatcher {
return gstruct.PointTo(gstruct.MatchFields(gstruct.IgnoreExtras, gstruct.Fields{
"Preamble": Equal(expectedFrame.Preamble),
"Time": Equal(expectedFrame.Time),
"Length": Equal(expectedFrame.Length),
"ConnectionType": Equal(expectedFrame.ConnectionType),
"Count": Equal(expectedFrame.Count),
"Reserved1": Equal(expectedFrame.Reserved1),
"Compression": Equal(expectedFrame.Compression),
"Reserved2": Equal(expectedFrame.Reserved2),
"DecompressedLength": Equal(expectedFrame.DecompressedLength),
"Blocks": Equal(expectedFrame.Blocks),
}))
}
var _ = Describe("Decoder", func() {
Describe("Decode", func() {
Context("with zlib compressed blocks", func() {
It("properly decodes a packet into the correct structures", func() {
buf := bytes.NewReader(zlibPacket)
d := xivnet.NewDecoder(buf, 32768)
f, err := d.NextFrame()
Expect(err).ToNot(HaveOccurred())
Expect(f).To(matchExpectedFrame(expectedZlibFrame))
})
})
Context("with a zero-block zlib compressed frame", func() {
It("correct returns a frame with no blocks", func() {
byteBuf := bytes.NewBuffer(zeroBlockPacket)
d := xivnet.NewDecoder(byteBuf, 32768)
f, err := d.NextFrame()
Expect(err).ToNot(HaveOccurred())
Expect(f.Count).To(BeZero())
Expect(f.Blocks).To(BeEmpty())
})
})
Context("with multiple frames on the buffer", func() {
It("decodes the packets until there is nothing left on the buffer", func() {
buf := bytes.NewBuffer(append(zlibPacket, zlibPacket...))
d := xivnet.NewDecoder(buf, 32768)
for i := 0; i < 2; i++ {
f, err := d.NextFrame()
Expect(err).ToNot(HaveOccurred())
Expect(f).To(matchExpectedFrame(expectedZlibFrame))
}
_, err := d.NextFrame()
Expect(err).To(HaveOccurred())
})
})
Context("with a non-zlib compressed packet and short block data", func() {
It("properly decodes a packet into the correct structures", func() {
buf := bytes.NewBuffer(nonZlibPacket)
d := xivnet.NewDecoder(buf, 32768)
frame, err := d.NextFrame()
Expect(err).ToNot(HaveOccurred())
Expect(frame).To(matchExpectedFrame(expectedNonZlibFrame))
})
})
Context("with incomplete data on the buffer", func() {
It("eventually is able to read the frame", func() {
byteBuf := bytes.NewBuffer(nil)
_, err := byteBuf.Write(zlibPacket[:69])
Expect(err).ToNot(HaveOccurred())
d := xivnet.NewDecoder(byteBuf, 32768)
_, err = d.NextFrame()
Expect(err).To(MatchError("peeking data failed reading 148 bytes from buffer: EOF"))
_, err = byteBuf.Write(zlibPacket[69:])
Expect(err).ToNot(HaveOccurred())
f, err := d.NextFrame()
Expect(err).ToNot(HaveOccurred())
Expect(f).To(matchExpectedFrame(expectedZlibFrame))
})
})
Context("with a decoder that has too small a buffer", func() {
It("returns an error", func() {
buf := bytes.NewReader(zlibPacket)
d := xivnet.NewDecoder(buf, 16)
_, err := d.NextFrame()
Expect(err).To(MatchError("invalid frame length: 28 (max 16)"))
})
Context("with a slightly larger but still too small of a buffer", func() {
It("returns an error", func() {
buf := bytes.NewReader(zlibPacket)
d := xivnet.NewDecoder(buf, 40)
_, err := d.NextFrame()
Expect(err).To(MatchError("invalid frame length: 148 (max 40)"))
})
})
})
Context("with an empty buffer", func() {
It("returns an error", func() {
byteBuf := bytes.NewBuffer(nil)
d := xivnet.NewDecoder(byteBuf, 32768)
_, err := d.NextFrame()
Expect(err).To(MatchError("peeking header failed reading 28 bytes from buffer: EOF"))
})
})
Context("with an invalid header at the head of the buffer", func() {
It("returns an error", func() {
byteBuf := bytes.NewBuffer(invalidHeaderPacket)
d := xivnet.NewDecoder(byteBuf, 32768)
_, err := d.NextFrame()
Expect(err).To(MatchError("invalid header: 52520000ff5d46e27f2a644d7b99c475e6f693da590100008a000000"))
})
})
Context("with corrupt data on the buffer", func() {
It("reads the first non-corrupted frame on the buffer", func() {
byteBuf := bytes.NewBuffer(nil)
_, err := byteBuf.Write(zlibPacket[:40])
Expect(err).ToNot(HaveOccurred())
_, err = byteBuf.Write(zlibPacket)
Expect(err).ToNot(HaveOccurred())
d := xivnet.NewDecoder(byteBuf, 32768)
_, err = d.NextFrame()
Expect(err.Error()).To(ContainSubstring("error decompressing data: zlib: invalid header"))
f, err := d.NextFrame()
Expect(err).ToNot(HaveOccurred())
Expect(f).To(matchExpectedFrame(expectedZlibFrame))
})
})
Context("with a block that specifies an invalid length", func() {
It("returns an error", func() {
byteBuf := bytes.NewBuffer(invalidBlockPacket)
d := xivnet.NewDecoder(byteBuf, 32768)
_, err := d.NextFrame()
Expect(err).To(MatchError(
"error decoding frame: error decoding blocks: not enough data: expected 32 bytes, got 24\n" +
"Data: 000000000000000000000000000000000000000000000000400000000000010000000000000000002000000000000000000000000800000015cd5b0742e08958",
))
})
})
})
Describe("CheckHeader", func() {
Context("with an empty buffer", func() {
It("returns an error", func() {
byteBuf := bytes.NewBuffer(nil)
d := xivnet.NewDecoder(byteBuf, 32768)
_, err := d.CheckHeader()
Expect(err).To(MatchError("peeking header failed reading 28 bytes from buffer: EOF"))
})
})
Context("with an invalid header at the head of the buffer", func() {
It("returns an error", func() {
byteBuf := bytes.NewBuffer(invalidHeaderPacket)
d := xivnet.NewDecoder(byteBuf, 32768)
_, err := d.CheckHeader()
Expect(err).To(MatchError("invalid header: 52520000ff5d46e27f2a644d7b99c475e6f693da590100008a000000"))
})
})
})
Describe("DiscardInvalidData", func() {
Context("with an empty buffer", func() {
It("does nothing to the buffer", func() {
byteBuf := bytes.NewBuffer(nil)
d := xivnet.NewDecoder(byteBuf, 32768)
d.DiscardDataUntilValid()
Expect(byteBuf.Len()).To(Equal(0))
})
})
Context("with an invalid header at the head of the buffer", func() {
It("discards the invalid data and allows the next decode operation to succeed with valid data", func() {
byteBuf := bytes.NewBuffer(append(invalidHeaderPacket, zeroBlockPacket...))
d := xivnet.NewDecoder(byteBuf, 32768)
d.DiscardDataUntilValid()
f, err := d.NextFrame()
Expect(err).ToNot(HaveOccurred())
Expect(f.Length).To(Equal(uint32(48)))
})
})
It("does nothing when the header is already valid", func() {
byteBuf := bytes.NewBuffer(zeroBlockPacket)
d := xivnet.NewDecoder(byteBuf, 32768)
d.DiscardDataUntilValid()
f, err := d.NextFrame()
Expect(err).ToNot(HaveOccurred())
Expect(f.Length).To(Equal(uint32(48)))
})
})
})