-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsijsop_test.go
318 lines (256 loc) · 6.76 KB
/
sijsop_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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package sijsop
// this is currently tested only on the happy path; the error handling is
// assumed to be correct for prototyping purposes.
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"reflect"
"strings"
"testing"
)
func TestJSONProtocolHappyPath(t *testing.T) {
// "in" from the point of view of the JSON protocol
incoming := []byte{
// the type is one byte
'1', 10,
// which is capital A,
'A', 10,
'1', '3', 10, //payload is 13 bytes
}
inJSON := `{"a":1,"b":2}`
incoming = append(incoming, []byte(inJSON)...)
incoming = append(incoming, 10)
in := bytes.NewBuffer(incoming)
var out bytes.Buffer
rw := &ReadWriter{in, &out}
protocol := &Definition{}
protocol.Register(&TestMessage{})
sp := protocol.Wrap(rw)
// verify that we can read the incoming messages
val, err := sp.ReceiveNext()
if err != nil {
t.Fatal("Can't read out of the JSON message stream:", err.Error())
}
shouldBe := &TestMessage{1, 2}
if !reflect.DeepEqual(val, shouldBe) {
t.Fatal("Incorrect value read out of the JSON protocol")
}
_, err = sp.ReceiveNext()
if err != io.EOF {
t.Fatal("jsonprotocol doesn't notice the end of the stream")
}
_ = sp.Close()
err1 := sp.Send(shouldBe)
_, err2 := sp.ReceiveNext()
err3 := sp.Unmarshal(&TestMessage{})
if err1 != ErrClosed || err2 != ErrClosed || err3 != ErrClosed {
t.Fatal("The closed handling is incorrect")
}
in = bytes.NewBuffer(incoming)
rw = &ReadWriter{in, &out}
sp = protocol.Wrap(rw)
// now we try to read the same value via Unmarshal. Note lack of
// registration.
target := &TestMessage{}
_ = sp.Unmarshal(target)
if !reflect.DeepEqual(target, shouldBe) {
t.Fatal("Unmarshal doesn't seem to work correctly.")
}
_ = sp.Send(target)
if !reflect.DeepEqual(out.Bytes(),
[]byte{
'1', 10,
'A', 10,
'1', '3', 10,
123, 34, 97, 34, 58, 49, 44, 34, 98, 34, 58, 50, 125, 10,
}) {
t.Fatal("Does not send the correct data values")
}
}
func TestCoverage(t *testing.T) {
// this at least ensures they don't crash, and removes the noise from
// the coverage chart
_ = ErrWrongType{"A", "B"}.Error()
_ = ErrUnknownType{"A"}.Error()
_ = ErrJSONTooLarge{1, 1}.Error()
}
func TestBadSends(t *testing.T) {
sp := &Definition{}
sp.Register(&TestMessage{}, &BadJSON{}, &StringMessage{})
badWriterErr := errors.New("bad writer strikes again")
bw := &BadWriter{0, badWriterErr}
writer := sp.Writer(bw)
err := writer.Send(&TestMessage{})
if err != badWriterErr {
t.Fatal("Can write to the badwriter at zero bytes!")
}
// This involves sending a long enough message that the buffer will
// flush during the buf.Write at the end of Send(), resulting in the error case
// being hit in the middle of the for loop, rather than the Flush at
// the end triggering it. This is just to figure out how much space the
// bufio uses by default:
bufio := bufio.NewWriter(&BadWriter{0, nil})
size := bufio.Available()
writer = sp.Writer(&BadWriter{0, badWriterErr})
err = writer.Send(&StringMessage{strings.Repeat("a", size*2)})
if err != badWriterErr {
t.Fatal("can write to bad writers")
}
// this checks the JSON marshaling clause
buf := &bytes.Buffer{}
writer = sp.Writer(buf)
err = writer.Send(&BadJSON{})
if err == nil {
t.Fatal("can write bad json types without error")
}
// check the thresholding
buf = &bytes.Buffer{}
writer = sp.Writer(buf)
writer.SizeLimit = 4
err = writer.Send(&TestMessage{})
if !reflect.DeepEqual(ErrJSONTooLarge{13, 4}, err) {
t.Fatal("Threshold detection doesn't work")
}
}
func TestOtherErrors(t *testing.T) {
in := &ReaderCloser{&bytes.Buffer{}, nil}
out := &WriterCloser{&bytes.Buffer{}, nil}
rw := &RWWithClose{in, out}
protocol := &Definition{}
protocol.Register(&TestMessage{})
sp := protocol.Wrap(rw)
err := sp.Unmarshal(nil)
if err != ErrNoUnmarshalTarget {
t.Fatal("Can unmarshal into nil")
}
err = sp.Close()
if err != nil {
t.Fatal("Unexpected error: " + err.Error())
}
testErr := errors.New("test")
in = &ReaderCloser{&bytes.Buffer{}, nil}
out = &WriterCloser{&bytes.Buffer{}, testErr}
rw = &RWWithClose{in, out}
sp = protocol.Wrap(rw)
err = sp.Close()
if err != testErr {
t.Fatal("Did not propagate close errors correctly.")
}
in = &ReaderCloser{&bytes.Buffer{}, testErr}
out = &WriterCloser{&bytes.Buffer{}, nil}
rw = &RWWithClose{in, out}
sp = protocol.Wrap(rw)
err = sp.Close()
if err != testErr {
t.Fatal("Did not propagate close errors correctly.")
}
}
func TestReadErrors(t *testing.T) {
// this test produces various malformed inputs and ensures that they
// are handled properly in the reading code
protocol := &Definition{}
protocol.Register(&TestMessage{})
for idx, in := range []io.Reader{
bytes.NewBuffer([]byte{}),
// dies in the middle of the type string
bytes.NewBuffer([]byte{4, 65}),
// dies in the middle of the length specification
bytes.NewBuffer([]byte{1, 65, 0, 0, 0}),
// dies in the middle of the JSON
bytes.NewBuffer([]byte{1, 65, 0, 0, 0, 13, '{'}),
// bad type
bytes.NewBuffer([]byte{1, 66, 0, 0, 0, 0}),
// bad json
bytes.NewBuffer([]byte{1, 65, 0, 0, 0, 1, 'T'}),
} {
r := protocol.Reader(in)
_, err := r.ReceiveNext()
if err == nil {
t.Fatal(fmt.Sprintf("Can't detect end of stream in test case %d", idx))
}
}
readMsg := append([]byte{'1', 10, 'A', 10, '1', '3', 10},
[]byte(`{"a":1,"b":2}`)...)
readMsg = append(readMsg, 10)
in := bytes.NewBuffer(readMsg)
r := protocol.Reader(in)
// wrong type
sm := &StringMessage{}
err := r.Unmarshal(sm)
if !reflect.DeepEqual(ErrWrongType{"string", "A"}, err) {
t.Fatal("Can unmarshal the wrong type")
}
}
type ReadWriter struct {
io.Reader
io.Writer
}
type RWWithClose struct {
io.ReadCloser
io.WriteCloser
}
func (rw *RWWithClose) Close() error {
err1 := rw.ReadCloser.Close()
err2 := rw.WriteCloser.Close()
if err1 != nil {
return err1
}
return err2
}
type TestMessage struct {
A int `json:"a"`
B int `json:"b"`
}
func (tm *TestMessage) SijsopType() string {
return "A"
}
func (tm *TestMessage) New() Message {
return &TestMessage{}
}
type BadJSON struct {
Channel chan struct{} `json:"channel"`
}
func (b *BadJSON) SijsopType() string {
return "string"
}
func (b *BadJSON) New() Message {
return &BadJSON{}
}
type StringMessage struct {
Message string `json:"message"`
}
func (sm *StringMessage) SijsopType() string {
return "string"
}
func (sm *StringMessage) New() Message {
return &StringMessage{}
}
type BadWriter struct {
ErrOnByte int
Error error
}
func (bw *BadWriter) Write(b []byte) (int, error) {
l := len(b)
bw.ErrOnByte -= l
if bw.ErrOnByte <= 0 {
return 0, bw.Error
}
return l, nil
}
type WriterCloser struct {
io.Writer
error
}
func (wc *WriterCloser) Close() error {
return wc.error
}
type ReaderCloser struct {
io.Reader
error
}
func (rc *ReaderCloser) Close() error {
return rc.error
}