-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder_test.go
236 lines (199 loc) · 5.31 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package netstring_test
import (
"bytes"
"io"
"testing"
"github.com/markdingo/netstring"
)
func newWith(s string) *netstring.Decoder {
return netstring.NewDecoder(bytes.NewBufferString(s))
}
func TestSimpleDecoder(t *testing.T) {
dc := newWith("3:abc,4:wxyz,")
v, e := dc.Decode()
if e != nil {
t.Fatal("Unexpected error", e)
}
if string(v) != "abc" {
t.Error("Expected 'abc' value, but got", len(v), string(v))
}
v, e = dc.Decode()
if e != nil {
t.Fatal("Unexpected error", e)
}
if string(v) != "wxyz" {
t.Error("Expected 'wxyz' value, but got", len(v), string(v))
}
}
func TestDecoderErrors(t *testing.T) {
type testCase struct {
input string
err error
}
testCases := []testCase{
{":abc,1:A,", netstring.ErrLengthNotDigit},
{"03:abc,1:A,", netstring.ErrLeadingZero},
{"999999999999:abc,1:A,", netstring.ErrLengthToLong},
{"3*abc,1:A,", netstring.ErrColonExpected},
{"3:abcZ1:A,", netstring.ErrCommaExpected},
}
for ix, tc := range testCases {
dc := newWith(tc.input)
_, err := dc.Decode()
if err == nil {
t.Error(ix, "Expected error return from", tc.input)
continue
}
if err != tc.err {
t.Error(ix, "Wrong error returned", err)
}
_, err = dc.Decode() // Second and subsequent should error
if err != tc.err {
t.Error(ix, "Wrong error returned", err)
}
}
}
type myReader struct {
ch chan []byte
buf []byte
}
func newMyReader() *myReader {
return &myReader{ch: make(chan []byte, 100)}
}
func (mr *myReader) set(p []byte) {
mr.ch <- p
}
func (mr *myReader) close() {
close(mr.ch)
}
func (mr *myReader) Read(p []byte) (n int, err error) {
if len(mr.buf) == 0 {
var ok bool
mr.buf, ok = <-mr.ch
if !ok {
return 0, io.EOF
}
}
n = copy(p, mr.buf)
mr.buf = mr.buf[n:]
return
}
func TestDecoderBuffer(t *testing.T) {
type ns struct {
key netstring.Key
value string
}
type testCase struct {
input string
expect []ns
}
testCases := []testCase{
// Partials
{"13:Xzeroefghzero,", []ns{{'X', "zeroefghzero"}}},
{"13:Xonedefghione", []ns{}},
{",", []ns{{'X', "onedefghione"}}},
{"13:Xtwodefghi", []ns{}},
{"two,", []ns{{'X', "twodefghitwo"}}},
{"13:Xt", []ns{}},
{"hreefgthree,", []ns{{'X', "threefgthree"}}},
{"13:X", []ns{}},
{"fourefghfour,", []ns{{'X', "fourefghfour"}}},
{"13", []ns{}},
{":Xfiveefghfive,", []ns{{'X', "fiveefghfive"}}},
{"1", []ns{}},
{"3:Xsixdefghisix,", []ns{{'X', "sixdefghisix"}}},
// Multiples
{"2:w1,3:x22,4:y333,5:z4444", []ns{{'w', "1"}, {'x', "22"}, {'y', "333"}}},
{",6:T55555,", []ns{{'z', "4444"}, {'T', "55555"}}},
}
mr := newMyReader()
dc := netstring.NewDecoder(mr)
for _, tc := range testCases { // Populate pipeline for multiple reads
mr.set([]byte(tc.input)) // to exerise decoder buffer management
}
mr.close()
for ix, tc := range testCases {
for nsx, nse := range tc.expect {
k, v, e := dc.DecodeKeyed()
if e != nil {
t.Fatal(ix, nsx, e)
}
if k != nse.key {
t.Error(ix, nsx, "Wrong key", string(k), "Expected", string(nse.key))
}
if string(v) != nse.value {
t.Error(ix, nsx, "Wrong value", string(v), "Expected", string(nse.value))
}
}
}
v, err := dc.Decode()
if v != nil {
t.Error("Expected nil value after DecodeKey() depleted", string(v), err)
}
}
func TestDecoderDecodeKeyedErrors(t *testing.T) {
dc := newWith("0:,")
_, _, e := dc.DecodeKeyed()
if e != netstring.ErrZeroKey {
t.Error("Expected ZeroKey error, not", e)
}
dc = newWith("2:@1,") // Not isalpha()
_, _, e = dc.DecodeKeyed()
if e != netstring.ErrInvalidKey {
t.Error("Expected InvalidKey error, not", e)
}
dc = newWith(string([]byte{'1', ':', 0, ','})) // NoKey as a keyed value is invalid
_, _, e = dc.DecodeKeyed()
if e != netstring.ErrInvalidKey {
t.Error("Expected InvalidKey error, not", e)
}
}
// Test that Write returns a perpetual error once one has been created by the parser.
func TestDecoderPerpetualWriteError(t *testing.T) {
dc := newWith("aa1:a,") // Invalid length
_, err := dc.Decode()
if err != netstring.ErrLengthNotDigit {
t.Fatal("Wrong first error returned", err)
}
_, err = dc.Decode() // Good netstring is irrelevant now
if err != netstring.ErrLengthNotDigit {
t.Fatal("Wrong second error returned", err)
}
}
// Test that Decode returns all valid netstrings upto the parsing error
func TestDecoderPerpetualError(t *testing.T) {
dc := newWith("1:a,2:bb,03:ccc,")
// Decode should return the first two netstrings then an error after that as "03:"
// is an invalid length.
val, err := dc.Decode()
if err != nil {
t.Fatal("Unexpected error on first Decode()", err)
}
if string(val) != "a" {
t.Fatal("Unexpected value", string(val))
}
val, err = dc.Decode()
if err != nil {
t.Fatal("Unexpected error on second Decode()", err)
}
if string(val) != "bb" {
t.Fatal("Unexpected value", string(val))
}
// Now we should get an error in perpetuity due to the leading '0' in 03:ccc,
_, err = dc.Decode()
if err != netstring.ErrLeadingZero {
t.Fatal("Expected error return due to leading zero, not", err)
}
// Make sure it's not a once-off
_, err = dc.Decode()
if err != netstring.ErrLeadingZero {
t.Fatal("Expected error return due to leading zero, not", err)
}
}
func TestDecodeKeyedWithNil(t *testing.T) {
dc := newWith("")
k, v, e := dc.DecodeKeyed()
if e != io.EOF {
t.Error("Expected EOF from empty parse but got", k, v, e)
}
}