-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetstring_test.go
285 lines (271 loc) · 7.13 KB
/
netstring_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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package netstring
import (
"bufio"
"bytes"
"errors"
"io"
"testing"
)
func TestParse(t *testing.T) {
cases := []struct {
desc string
stream []byte
err error
bytes []byte
}{
{
"Netstring is empty",
[]byte{},
io.EOF,
[]byte{},
},
{
"Netstring length is 0",
[]byte{0x30, prefixCh, suffixCh},
nil,
[]byte{},
},
{
"Netstring length starts with a leading 0",
[]byte{0x30, 0x38, prefixCh, 0x73, 0x75, 0x6e, 0x73, 0x68, 0x69, 0x6e, 0x65, suffixCh},
errors.New("leading zeros at the front of length are prohibited"),
[]byte{},
},
{
"Netstring is 0",
[]byte{0x30},
errors.New("leading zeros at the front of length are prohibited"),
[]byte{},
},
{
"Netstring length consists of 1 digit",
[]byte{
0x38, prefixCh, 0x73, 0x75, 0x6e, 0x73, 0x68, 0x69, 0x6e, 0x65, suffixCh,
},
nil,
[]byte("sunshine"),
},
{
"Netstring length consists of 2 digits",
[]byte{
0x31, 0x34, prefixCh, 0x70, 0x65, 0x72, 0x66, 0x65, 0x63, 0x74, 0x6c,
0x69, 0x62, 0x72, 0x61, 0x72, 0x79, suffixCh,
},
nil,
[]byte("perfectlibrary"),
},
{
"Netstring length consists of 3 digits",
[]byte{
0x31, 0x30, 0x35, prefixCh, 0x41, 0x20, 0x6e, 0x65, 0x74, 0x73, 0x74,
0x72, 0x69, 0x6e, 0x67, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x73, 0x65,
0x6c, 0x66, 0x2d, 0x64, 0x65, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x69, 0x6e,
0x67, 0x20, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x20, 0x6f,
0x66, 0x20, 0x61, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x2e, 0x20,
0x4e, 0x65, 0x74, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x73, 0x20, 0x61,
0x72, 0x65, 0x20, 0x76, 0x65, 0x72, 0x79, 0x20, 0x65, 0x61, 0x73, 0x79,
0x20, 0x74, 0x6f, 0x20, 0x67, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65,
0x20, 0x61, 0x6e, 0x64, 0x20, 0x74, 0x6f, 0x20, 0x70, 0x61, 0x72, 0x73,
0x65, 0x2e, suffixCh,
},
nil,
[]byte(
"A netstring is a self-delimiting encoding of a string. Netstrings " +
"are very easy to generate and to parse.",
),
},
{
"Netstring consists of digigts",
[]byte{
0x32, prefixCh, 0x31, 0x34, suffixCh,
},
nil,
[]byte("14"),
},
{
"Netstring length is shorter than the actual string",
[]byte{
0x30, prefixCh, 0x73, 0x75, 0x6e, 0x73, 0x68, 0x69, 0x6e, 0x65, suffixCh,
},
errors.New("unexpected suffix s, wanted ,"),
[]byte{},
},
{
"Netstring length is longer than the actual string",
[]byte{
0x31, 0x31, 0x31, prefixCh, 0x73, 0x75, 0x6e, 0x73, 0x68, 0x69, 0x6e, 0x65,
0x73, 0x75, 0x6e, 0x73, 0x68, 0x69, 0x6e, 0x65, prefixCh,
},
io.EOF,
[]byte{},
},
{
"Netstring is missing prefix :",
[]byte{
0x38, 0x73, 0x75, 0x6e, 0x73, 0x68, 0x69, 0x6e, 0x65, suffixCh,
},
errors.New("length number 67 is not in the range of 0-9"),
[]byte{},
},
{
"Netstring is missing suffix ,",
[]byte{
0x38, prefixCh, 0x73, 0x75, 0x6e, 0x73, 0x68, 0x69, 0x6e, 0x65,
},
io.EOF,
[]byte{},
},
{
"Netstring ends with other suffix than ,",
[]byte{
0x38, prefixCh, 0x73, 0x75, 0x6e, 0x73, 0x68, 0x69, 0x6e, 0x65, prefixCh,
},
errors.New("unexpected suffix :, wanted ,"),
[]byte{},
},
{
"Netstring includes non-digits in the length field",
[]byte{
0x6e, 0x65, prefixCh, 0x73, 0x75, 0x6e, 0x73, 0x68, 0x69, 0x6e, 0x65,
prefixCh,
},
errors.New("length number 62 is not in the range of 0-9"),
[]byte{},
},
{
"Netstring includes both digits and non-digits in the length field",
[]byte{
0x38, 0x65, prefixCh, 0x73, 0x75, 0x6e, 0x73, 0x68, 0x69, 0x6e, 0x65,
prefixCh,
},
errors.New("length number 53 is not in the range of 0-9"),
[]byte{},
},
}
for _, c := range cases {
t.Run(c.desc, func(t *testing.T) {
netstr, err := Parse(bufio.NewReader(bytes.NewReader(c.stream)))
if c.err == nil && err != nil {
t.Fatalf("unexpected error: '%q'", err)
return
}
if c.err != nil && err != nil {
if c.err.Error() != err.Error() {
t.Fatalf("expected error %q, got %q", c.err, err)
}
return
}
if c.err != nil && err == nil {
t.Errorf("expected error %q, got nothing", c.err)
}
if !bytes.Equal(netstr, c.bytes) {
t.Errorf("bytes: got %c, want %c", netstr, c.bytes)
}
})
}
}
func TestPack(t *testing.T) {
cases := []struct {
desc string
input string
err error
result []byte
}{
{
"Packs an empty string",
"",
nil,
[]byte{0x30, prefixCh, suffixCh},
},
{
"Packs a string with 1-digit length into a netstring",
"sunshine",
nil,
[]byte{
0x38, prefixCh, 0x73, 0x75, 0x6e, 0x73, 0x68, 0x69, 0x6e, 0x65,
suffixCh,
},
},
{
"Packs a string with 2-digit length into a netstring",
"perfectlibrary",
nil,
[]byte{
0x31, 0x34, prefixCh, 0x70, 0x65, 0x72, 0x66, 0x65, 0x63, 0x74, 0x6c,
0x69, 0x62, 0x72, 0x61, 0x72, 0x79, suffixCh,
},
},
{
"Packs a string with 3-digit length into a netstring",
"A netstring is a self-delimiting encoding of a string. Netstrings " +
"are very easy to generate and to parse.",
nil,
[]byte{
0x31, 0x30, 0x35, prefixCh, 0x41, 0x20, 0x6e, 0x65, 0x74, 0x73, 0x74,
0x72, 0x69, 0x6e, 0x67, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x73, 0x65,
0x6c, 0x66, 0x2d, 0x64, 0x65, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x69, 0x6e,
0x67, 0x20, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x20, 0x6f,
0x66, 0x20, 0x61, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x2e, 0x20,
0x4e, 0x65, 0x74, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x73, 0x20, 0x61,
0x72, 0x65, 0x20, 0x76, 0x65, 0x72, 0x79, 0x20, 0x65, 0x61, 0x73, 0x79,
0x20, 0x74, 0x6f, 0x20, 0x67, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65,
0x20, 0x61, 0x6e, 0x64, 0x20, 0x74, 0x6f, 0x20, 0x70, 0x61, 0x72, 0x73,
0x65, 0x2e, suffixCh,
},
},
}
for _, c := range cases {
t.Run(c.desc, func(t *testing.T) {
b, err := Pack([]byte(c.input))
if c.err == nil && err != nil {
t.Fatalf("unexpected error: '%q'", err)
return
}
if c.err != nil && err != nil {
if c.err.Error() != err.Error() {
t.Fatalf("expected error %q, got %q", c.err, err)
}
return
}
if c.err != nil && err == nil {
t.Errorf("expected error %q, got nothing", c.err)
}
if !bytes.Equal(b, c.result) {
t.Errorf("bytes: got %c, want %c", b, c.result)
}
})
}
}
func TestParseAndPackInteropability(t *testing.T) {
// 5:hello,
netstr := []byte{0x35, prefixCh, 0x68, 0x65, 0x6c, 0x6c, 0x6f, suffixCh}
str, err := Parse(bufio.NewReader(bytes.NewReader(netstr)))
if err != nil {
t.Fatalf("unexpected error: '%q'", err)
return
}
packed, err := Pack(str)
if err != nil {
t.Fatalf("unexpected error: '%q'", err)
return
}
if !bytes.Equal(netstr, packed) {
t.Errorf("wanted %c to be the same as %c", packed, netstr)
}
}
func TestPackAndParseInteropability(t *testing.T) {
str := []byte("sunshine")
packed, err := Pack(str)
if err != nil {
t.Fatalf("unexpected error: '%q'", err)
return
}
netstr, err := Parse(bufio.NewReader(bytes.NewReader(packed)))
if err != nil {
t.Fatalf("unexpected error: '%q'", err)
return
}
if !bytes.Equal(netstr, str) {
t.Errorf("wanted %c to be the same as %c", netstr, str)
}
}