-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
codec_test.go
239 lines (221 loc) · 5.59 KB
/
codec_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
package capnp
import (
"bytes"
"errors"
"io"
"testing"
"github.com/stretchr/testify/require"
)
func TestEncoder(t *testing.T) {
t.Parallel()
for i, test := range serializeTests {
if test.decodeFails {
continue
}
msg := &Message{Arena: test.arena()}
var buf bytes.Buffer
enc := NewEncoder(&buf)
err := enc.Encode(msg)
out := buf.Bytes()
if err != nil {
if !test.encodeFails {
t.Errorf("serializeTests[%d] - %s: Encode error: %v", i, test.name, err)
}
continue
}
if test.encodeFails {
t.Errorf("serializeTests[%d] - %s: Encode success; want error", i, test.name)
continue
}
if !bytes.Equal(out, test.out) {
t.Errorf("serializeTests[%d] - %s: Encode = % 02x; want % 02x", i, test.name, out, test.out)
}
}
}
func TestDecoder(t *testing.T) {
t.Parallel()
for i, test := range serializeTests {
if test.encodeFails {
continue
}
msg, err := NewDecoder(bytes.NewReader(test.out)).Decode()
if err != nil {
if !test.decodeFails {
t.Errorf("serializeTests[%d] - %s: Decode error: %v", i, test.name, err)
}
if test.decodeError != nil && err != test.decodeError {
t.Errorf("serializeTests[%d] - %s: Decode error: %v; want %v", i, test.name, err, test.decodeError)
}
continue
}
if test.decodeFails {
t.Errorf("serializeTests[%d] - %s: Decode success; want error", i, test.name)
continue
}
if msg.NumSegments() != int64(len(test.segs)) {
t.Errorf("serializeTests[%d] - %s: Decode NumSegments() = %d; want %d", i, test.name, msg.NumSegments(), len(test.segs))
continue
}
for j := range test.segs {
seg, err := msg.Segment(SegmentID(j))
if err != nil {
t.Errorf("serializeTests[%d] - %s: Decode Segment(%d) error: %v", i, test.name, j, err)
continue
}
if !bytes.Equal(seg.Data(), test.segs[j]) {
t.Errorf("serializeTests[%d] - %s: Decode Segment(%d) = % 02x; want % 02x", i, test.name, j, seg.Data(), test.segs[j])
}
}
}
}
type tooManySegsArena struct {
data []byte
}
func (t *tooManySegsArena) NumSegments() int64 { return 1<<32 + 1 }
func (t *tooManySegsArena) Segment(id SegmentID) *Segment {
return nil
}
func (t *tooManySegsArena) Allocate(minsz Size, msg *Message, seg *Segment) (*Segment, address, error) {
return nil, 0, errors.New("cannot allocate")
}
func (t *tooManySegsArena) Release() {}
// TestEncoderTooManySegments verifies attempting to encode an arena that has
// more segments than possible.
func TestEncoderTooManySegments(t *testing.T) {
t.Parallel()
zeroWord := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
arena := &tooManySegsArena{data: zeroWord}
// Setup via field because NewMessage checks arena has > 1 segments.
var msg Message
msg.Arena = arena
var buf bytes.Buffer
enc := NewEncoder(&buf)
err := enc.Encode(&msg)
// Encoding should error with a specific error.
require.ErrorIs(t, err, errTooManySegments)
}
func TestDecoder_MaxMessageSize(t *testing.T) {
t.Parallel()
zeroWord := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
tests := []struct {
name string
maxSize uint64
r io.Reader
ok bool
}{
{
name: "header too big",
maxSize: 15,
r: bytes.NewReader([]byte{
0x02, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
}),
},
{
name: "header at limit",
maxSize: 16,
r: bytes.NewReader([]byte{
0x02, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
}),
ok: true,
},
{
name: "body too large",
maxSize: 64,
r: io.MultiReader(
bytes.NewReader([]byte{
0x00, 0x00, 0x00, 0x00,
0x09, 0x00, 0x00, 0x00,
}),
bytes.NewReader(bytes.Repeat(zeroWord, 9)),
),
},
{
name: "body plus header too large",
maxSize: 64,
r: io.MultiReader(
bytes.NewReader([]byte{
0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00,
}),
bytes.NewReader(bytes.Repeat(zeroWord, 8)),
),
},
{
name: "body plus header at limit",
maxSize: 72,
r: io.MultiReader(
bytes.NewReader([]byte{
0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00,
}),
bytes.NewReader(bytes.Repeat(zeroWord, 8)),
),
ok: true,
},
}
for _, test := range tests {
d := NewDecoder(test.r)
d.MaxMessageSize = test.maxSize
_, err := d.Decode()
switch {
case err != nil && test.ok:
t.Errorf("%s test: Decode error: %v", test.name, err)
case err == nil && !test.ok:
t.Errorf("%s test: Decode success; want error", test.name)
}
}
}
// TestStreamHeaderPadding is a regression test for
// stream header padding.
//
// Encoder reuses a buffer for stream header marshalling,
// this test ensures that the padding is explicitly
// zeroed. This was not done in previous versions and
// resulted in the padding being garbage.
func TestStreamHeaderPadding(t *testing.T) {
t.Parallel()
msg := &Message{
Arena: MultiSegment([][]byte{
incrementingData(8),
incrementingData(8),
incrementingData(8),
}),
}
var buf bytes.Buffer
enc := NewEncoder(&buf)
err := enc.Encode(msg)
buf.Reset()
if err != nil {
t.Fatalf("Encode error: %v", err)
}
msg = &Message{
Arena: MultiSegment([][]byte{
incrementingData(8),
incrementingData(8),
}),
}
err = enc.Encode(msg)
out := buf.Bytes()
if err != nil {
t.Fatalf("Encode error: %v", err)
}
want := []byte{
0x01, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x02, 0x03,
0x04, 0x05, 0x06, 0x07,
0x00, 0x01, 0x02, 0x03,
0x04, 0x05, 0x06, 0x07,
}
if !bytes.Equal(out, want) {
t.Errorf("Encode = % 02x; want % 02x", out, want)
}
}