-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetstring_test.go
More file actions
239 lines (195 loc) · 6.18 KB
/
Copy pathnetstring_test.go
File metadata and controls
239 lines (195 loc) · 6.18 KB
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
// SPDX-License-Identifier: MPL-2.0
//
// Copyright (c) 2016-2026 Alex Hermann, Hexla. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package netstring_test
import (
"bufio"
"bytes"
"io"
"testing"
"github.com/stretchr/testify/assert"
"go.hexla.nl/netstring"
)
func TestEncode(t *testing.T) {
var buf bytes.Buffer
enc := netstring.NewEncoder(&buf)
err := enc.Encode([]byte("hello"))
assert.NoError(t, err)
assert.Equal(t, "5:hello,", buf.String())
}
func TestEncodeEmpty(t *testing.T) {
var buf bytes.Buffer
enc := netstring.NewEncoder(&buf)
err := enc.Encode(nil)
assert.NoError(t, err)
assert.Equal(t, "0:,", buf.String())
}
func TestDecode(t *testing.T) {
input := bytes.NewBufferString("5:hello,")
dec := netstring.NewDecoder(input)
b, err := dec.Decode()
assert.NoError(t, err)
assert.Equal(t, []byte("hello"), b)
}
func TestDecodeEmpty(t *testing.T) {
input := bytes.NewBufferString("0:,")
dec := netstring.NewDecoder(input)
b, err := dec.Decode()
assert.NoError(t, err)
assert.Empty(t, b)
}
func TestDecodeMultiple(t *testing.T) {
input := bytes.NewBufferString("5:hello,5:world,")
dec := netstring.NewDecoder(input)
b1, err := dec.Decode()
assert.NoError(t, err)
assert.Equal(t, []byte("hello"), b1)
b2, err := dec.Decode()
assert.NoError(t, err)
assert.Equal(t, []byte("world"), b2)
}
func TestDecodeEmbeddedColonComma(t *testing.T) {
input := bytes.NewBufferString("12:hello:world,,")
dec := netstring.NewDecoder(input)
b, err := dec.Decode()
assert.NoError(t, err)
assert.Equal(t, []byte("hello:world,"), b)
}
func TestDecodeZeroCopy(t *testing.T) {
input := bytes.NewBufferString("5:hello,")
dec := netstring.NewDecoder(input)
b, err := dec.DecodeZeroCopy()
assert.NoError(t, err)
assert.Equal(t, []byte("hello"), b)
}
func TestDecodeZeroCopyValidity(t *testing.T) {
input := bytes.NewBufferString("5:hello,5:world,")
dec := netstring.NewDecoder(input)
b1, err := dec.DecodeZeroCopy()
assert.NoError(t, err)
assert.Equal(t, []byte("hello"), b1)
// Next call invalidates previous slice
b2, err := dec.DecodeZeroCopy()
assert.NoError(t, err)
assert.Equal(t, []byte("world"), b2)
}
func TestDecodeUnexpectedEOF(t *testing.T) {
input := bytes.NewBufferString("5:hel")
dec := netstring.NewDecoder(input)
_, err := dec.Decode()
assert.ErrorIs(t, err, netstring.ErrInvalidNetstring)
assert.ErrorIs(t, err, io.ErrUnexpectedEOF)
}
func TestRoundTrip(t *testing.T) {
var buf bytes.Buffer
enc := netstring.NewEncoder(&buf)
dec := netstring.NewDecoder(&buf)
payloads := [][]byte{
[]byte("hello"),
[]byte(""),
[]byte("netstring"),
[]byte("🔥"),
}
for _, p := range payloads {
assert.NoError(t, enc.Encode(p))
}
for _, expected := range payloads {
b, err := dec.Decode()
assert.NoError(t, err)
assert.Equal(t, expected, b)
}
}
func TestDecodeHeaderNoColon(t *testing.T) {
input := bytes.NewBuffer(bytes.Repeat([]byte("1"), 5000))
dec := netstring.NewDecoder(input)
_, err := dec.Decode()
assert.ErrorIs(t, err, netstring.ErrInvalidNetstring)
}
func TestDecodeInvalid(t *testing.T) {
tests := []struct {
name string
input string
}{
{"zero length, non empty", "0:hello,"},
{"plus sign", "+5:hello,"},
{"negative sign", "-5:hello,"},
{"leading space", " 5:hello,"},
{"trailing space", "5 :hello,"},
{"empty length", ":hello,"},
{"non digit length", "x:hello,"},
{"length overlow", "18446744073709551615:hello,"},
{"missing comma", "5:hello."},
{"missing delimiter", "5:hello"},
{"empty", ""},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
dec := netstring.NewDecoder(bytes.NewBufferString(tc.input))
_, err := dec.Decode()
assert.ErrorIs(t, err, netstring.ErrInvalidNetstring)
})
}
}
func TestDecodeMaxPayloadSize(t *testing.T) {
tests := []struct {
name string
maxPayloadSize int
input string
expect []byte
err error
}{
{"too large for default", 0, "20971520:hello,", nil, netstring.ErrPayloadTooLarge},
{"too large for custom", 5, "6:hello!,", nil, netstring.ErrPayloadTooLarge},
{"no limit valid", -1, "6:hello!,", []byte("hello!"), nil},
{"no limit invalid", -1, "20971520:hello,", nil, netstring.ErrInvalidNetstring},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
dec := netstring.NewDecoder(bytes.NewBufferString(tc.input))
dec.MaxPayloadSize = tc.maxPayloadSize
b, err := dec.Decode()
assert.ErrorIs(t, err, tc.err)
assert.Equal(t, tc.expect, b)
})
}
}
func TestDecodeZeroCopyBufferTooSmall(t *testing.T) {
// Since bufio.Reader's default buffer size is 4096 bytes,
// peeking at a netstring larger than 4096 should return ErrBufferTooSmall.
payload := bytes.Repeat([]byte("a"), 5000)
var buf bytes.Buffer
enc := netstring.NewEncoder(&buf)
err := enc.Encode(payload)
assert.NoError(t, err)
dec := netstring.NewDecoder(&buf)
_, err = dec.DecodeZeroCopy()
assert.ErrorIs(t, err, netstring.ErrBufferTooSmall)
}
func TestPackageLevelDecodeDiscardsExtraData(t *testing.T) {
// Demonstrates that package-level Decode is unsafe on multi-message streams.
input := bytes.NewBufferString("5:hello,5:world,")
b1, err := netstring.Decode(input)
assert.NoError(t, err)
assert.Equal(t, []byte("hello"), b1)
// Since the first call to Decode wrapped input in a bufio.Reader and buffered the rest,
// calling Decode(input) again on the underlying raw stream will lose "5:world," and hit EOF.
_, err = netstring.Decode(input)
assert.ErrorIs(t, err, netstring.ErrInvalidNetstring)
assert.ErrorIs(t, err, io.EOF)
}
func TestPackageLevelDecodeKeepsBufferedData(t *testing.T) {
// Demonstrates that package-level Decode is safe on multi-message streams when using a buffered Reader.
buf := bytes.NewBufferString("5:hello,5:world,")
input := bufio.NewReader(buf)
b1, err := netstring.Decode(input)
assert.NoError(t, err)
assert.Equal(t, []byte("hello"), b1)
// Calling Decode(input) again on the underlying buffer stream will return "5:world,".
b2, err := netstring.Decode(input)
assert.NoError(t, err)
assert.Equal(t, []byte("world"), b2)
}