forked from fastred/go-imap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_test.go
536 lines (469 loc) · 13.5 KB
/
read_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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
package imap_test
import (
"bytes"
"io"
"io/ioutil"
"reflect"
"testing"
"github.com/emersion/go-imap"
)
func TestParseNumber(t *testing.T) {
tests := []struct {
f interface{}
n uint32
err bool
}{
{f: "42", n: 42},
{f: "0", n: 0},
{f: "-1", err: true},
{f: "1.2", err: true},
{f: nil, err: true},
{f: bytes.NewBufferString("cc"), err: true},
}
for _, test := range tests {
n, err := imap.ParseNumber(test.f)
if err != nil {
if !test.err {
t.Errorf("Cannot parse number %v", test.f)
}
} else {
if test.err {
t.Errorf("Parsed invalid number %v", test.f)
} else if n != test.n {
t.Errorf("Invalid parsed number: got %v but expected %v", n, test.n)
}
}
}
}
func TestParseStringList(t *testing.T) {
tests := []struct {
field interface{}
list []string
}{
{
field: []interface{}{"a", "b", "c", "d"},
list: []string{"a", "b", "c", "d"},
},
{
field: []interface{}{"a"},
list: []string{"a"},
},
{
field: []interface{}{},
list: []string{},
},
{
field: []interface{}{"a", 42, "c", "d"},
list: nil,
},
{
field: []interface{}{"a", nil, "c", "d"},
list: nil,
},
{
field: "Asuka FTW",
list: nil,
},
}
for _, test := range tests {
list, err := imap.ParseStringList(test.field)
if err != nil {
if test.list != nil {
t.Errorf("Cannot parse string list: %v", err)
}
} else if !reflect.DeepEqual(list, test.list) {
t.Errorf("Invalid parsed string list: got \n%+v\n but expected \n%+v", list, test.list)
}
}
}
func newReader(s string) (b *bytes.Buffer, r *imap.Reader) {
b = bytes.NewBuffer([]byte(s))
r = imap.NewReader(b)
return
}
func TestReader_ReadSp(t *testing.T) {
b, r := newReader(" ")
if err := r.ReadSp(); err != nil {
t.Error(err)
}
if b.Len() > 0 {
t.Error("Buffer is not empty after read")
}
_, r = newReader("")
if err := r.ReadSp(); err == nil {
t.Error("Invalid read didn't fail")
}
}
func TestReader_ReadCrlf(t *testing.T) {
b, r := newReader("\r\n")
if err := r.ReadCrlf(); err != nil {
t.Error(err)
}
if b.Len() > 0 {
t.Error("Buffer is not empty after read")
}
_, r = newReader("")
if err := r.ReadCrlf(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("\n")
if err := r.ReadCrlf(); err != nil {
t.Error(err)
}
_, r = newReader("\r")
if err := r.ReadCrlf(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("\r42")
if err := r.ReadCrlf(); err == nil {
t.Error("Invalid read didn't fail")
}
}
func TestReader_ReadAtom(t *testing.T) {
b, r := newReader("NIL\r\n")
if atom, err := r.ReadAtom(); err != nil {
t.Error(err)
} else if atom != nil {
t.Error("NIL atom is not nil:", atom)
} else {
if err := r.ReadCrlf(); err != nil && err != io.EOF {
t.Error("Cannot read CRLF after atom:", err)
}
if b.Len() > 0 {
t.Error("Buffer is not empty after read")
}
}
b, r = newReader("atom\r\n")
if atom, err := r.ReadAtom(); err != nil {
t.Error(err)
} else if s, ok := atom.(string); !ok || s != "atom" {
t.Error("String atom has not the expected value:", atom)
} else {
if err := r.ReadCrlf(); err != nil && err != io.EOF {
t.Error("Cannot read CRLF after atom:", err)
}
if b.Len() > 0 {
t.Error("Buffer is not empty after read")
}
}
_, r = newReader("")
if _, err := r.ReadAtom(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("(hi there)\r\n")
if _, err := r.ReadAtom(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("{42}\r\n")
if _, err := r.ReadAtom(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("\"\r\n")
if _, err := r.ReadAtom(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("abc]")
if _, err := r.ReadAtom(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("[abc]def]ghi")
if _, err := r.ReadAtom(); err == nil {
t.Error("Invalid read didn't fail")
}
}
func TestReader_ReadLiteral_NonSync(t *testing.T) {
// For synchronizing literal we should send continuation request.
b := bytes.NewBuffer([]byte("{7}\r\nabcdefg"))
cont := make(chan bool, 5)
r := imap.NewServerReader(b, cont)
if litr, err := r.ReadLiteral(); err != nil {
t.Error(err)
} else if litr.Len() != 7 {
t.Error("Invalid literal length")
} else {
if len(cont) != 1 {
t.Error("Missing continuation rejqest")
}
<-cont
}
b = bytes.NewBuffer([]byte("{7+}\r\nabcdefg"))
r = imap.NewServerReader(b, cont)
if litr, err := r.ReadLiteral(); err != nil {
t.Error(err)
} else if litr.Len() != 7 {
t.Error("Invalid literal length")
} else {
if len(cont) != 0 {
t.Error("Unexpected continuation rejqest")
}
if contents, err := ioutil.ReadAll(litr); err != nil {
t.Error(err)
} else if string(contents) != "abcdefg" {
t.Error("Literal has not the expected value:", string(contents))
} else if b.Len() > 0 {
t.Error("Buffer is not empty after read")
}
}
}
func TestReader_ReadLiteral(t *testing.T) {
b, r := newReader("{7}\r\nabcdefg")
if literal, err := r.ReadLiteral(); err != nil {
t.Error(err)
} else if literal.Len() != 7 {
t.Error("Invalid literal length:", literal.Len())
} else {
if contents, err := ioutil.ReadAll(literal); err != nil {
t.Error(err)
} else if string(contents) != "abcdefg" {
t.Error("Literal has not the expected value:", string(contents))
} else if b.Len() > 0 {
t.Error("Buffer is not empty after read")
}
}
_, r = newReader("")
if _, err := r.ReadLiteral(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("[7}\r\nabcdefg")
if _, err := r.ReadLiteral(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("{7]\r\nabcdefg")
if _, err := r.ReadLiteral(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("{7.4}\r\nabcdefg")
if _, err := r.ReadLiteral(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("{7}abcdefg")
if _, err := r.ReadLiteral(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("{7}\rabcdefg")
if _, err := r.ReadLiteral(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("{7}\nabcdefg")
if _, err := r.ReadLiteral(); err != nil {
t.Error(err)
}
_, r = newReader("{7}\r\nabcd")
if _, err := r.ReadLiteral(); err == nil {
t.Error("Invalid read didn't fail")
}
}
func TestReader_ReadQuotedString(t *testing.T) {
b, r := newReader("\"hello gopher\"\r\n")
if s, err := r.ReadQuotedString(); err != nil {
t.Error(err)
} else if s != "hello gopher" {
t.Error("Quoted string has not the expected value:", s)
} else {
if err := r.ReadCrlf(); err != nil && err != io.EOF {
t.Error("Cannot read CRLF after quoted string:", err)
}
if b.Len() > 0 {
t.Error("Buffer is not empty after read")
}
}
_, r = newReader("\"here's a backslash: \\\\, and here's a double quote: \\\" !\"\r\n")
if s, err := r.ReadQuotedString(); err != nil {
t.Error(err)
} else if s != "here's a backslash: \\, and here's a double quote: \" !" {
t.Error("Quoted string has not the expected value:", s)
}
_, r = newReader("")
if _, err := r.ReadQuotedString(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("hello gopher\"\r\n")
if _, err := r.ReadQuotedString(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("\"hello gopher\r\n")
if _, err := r.ReadQuotedString(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("\"hello \\gopher\"\r\n")
if _, err := r.ReadQuotedString(); err == nil {
t.Error("Invalid read didn't fail")
}
}
func TestReader_ReadFields(t *testing.T) {
b, r := newReader("field1 \"field2\"\r\n")
if fields, err := r.ReadFields(); err != nil {
t.Error(err)
} else if len(fields) != 2 {
t.Error("Expected 2 fields, but got", len(fields))
} else if s, ok := fields[0].(string); !ok || s != "field1" {
t.Error("Field 1 has not the expected value:", fields[0])
} else if s, ok := fields[1].(string); !ok || s != "field2" {
t.Error("Field 2 has not the expected value:", fields[1])
} else {
if err := r.ReadCrlf(); err != nil && err != io.EOF {
t.Error("Cannot read CRLF after fields:", err)
}
if b.Len() > 0 {
t.Error("Buffer is not empty after read")
}
}
_, r = newReader("")
if _, err := r.ReadFields(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("fi\"eld1 \"field2\"\r\n")
if _, err := r.ReadFields(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("field1 ")
if _, err := r.ReadFields(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("field1 (")
if _, err := r.ReadFields(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("field1\"field2\"\r\n")
if _, err := r.ReadFields(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("\"field1\"\"field2\"\r\n")
if _, err := r.ReadFields(); err == nil {
t.Error("Invalid read didn't fail")
}
}
func TestReader_ReadList(t *testing.T) {
b, r := newReader("(field1 \"field2\" {6}\r\nfield3 field4)")
if fields, err := r.ReadList(); err != nil {
t.Error(err)
} else if len(fields) != 4 {
t.Error("Expected 2 fields, but got", len(fields))
} else if s, ok := fields[0].(string); !ok || s != "field1" {
t.Error("Field 1 has not the expected value:", fields[0])
} else if s, ok := fields[1].(string); !ok || s != "field2" {
t.Error("Field 2 has not the expected value:", fields[1])
} else if literal, ok := fields[2].(imap.Literal); !ok {
t.Error("Field 3 has not the expected value:", fields[2])
} else if contents, err := ioutil.ReadAll(literal); err != nil {
t.Error(err)
} else if string(contents) != "field3" {
t.Error("Field 3 has not the expected value:", string(contents))
} else if s, ok := fields[3].(string); !ok || s != "field4" {
t.Error("Field 4 has not the expected value:", fields[3])
} else if b.Len() > 0 {
t.Error("Buffer is not empty after read")
}
b, r = newReader("()")
if fields, err := r.ReadList(); err != nil {
t.Error(err)
} else if len(fields) != 0 {
t.Error("Expected 0 fields, but got", len(fields))
} else if b.Len() > 0 {
t.Error("Buffer is not empty after read")
}
_, r = newReader("")
if _, err := r.ReadList(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("[field1 field2 field3)")
if _, err := r.ReadList(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("(field1 fie\"ld2 field3)")
if _, err := r.ReadList(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("(field1 field2 field3\r\n")
if _, err := r.ReadList(); err == nil {
t.Error("Invalid read didn't fail")
}
}
func TestReader_ReadLine(t *testing.T) {
b, r := newReader("field1 field2\r\n")
if fields, err := r.ReadLine(); err != nil {
t.Error(err)
} else if len(fields) != 2 {
t.Error("Expected 2 fields, but got", len(fields))
} else if s, ok := fields[0].(string); !ok || s != "field1" {
t.Error("Field 1 has not the expected value:", fields[0])
} else if s, ok := fields[1].(string); !ok || s != "field2" {
t.Error("Field 2 has not the expected value:", fields[1])
} else if b.Len() > 0 {
t.Error("Buffer is not empty after read")
}
_, r = newReader("")
if _, err := r.ReadLine(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("field1 field2\rabc")
if _, err := r.ReadLine(); err == nil {
t.Error("Invalid read didn't fail")
}
}
func TestReader_ReadRespCode(t *testing.T) {
b, r := newReader("[CAPABILITY NOOP STARTTLS]")
if code, fields, err := r.ReadRespCode(); err != nil {
t.Error(err)
} else if code != imap.CodeCapability {
t.Error("Response code has not the expected value:", code)
} else if len(fields) != 2 {
t.Error("Expected 2 fields, but got", len(fields))
} else if s, ok := fields[0].(string); !ok || s != "NOOP" {
t.Error("Field 1 has not the expected value:", fields[0])
} else if s, ok := fields[1].(string); !ok || s != "STARTTLS" {
t.Error("Field 2 has not the expected value:", fields[1])
} else if b.Len() > 0 {
t.Error("Buffer is not empty after read")
}
_, r = newReader("")
if _, _, err := r.ReadRespCode(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("{CAPABILITY NOOP STARTTLS]")
if _, _, err := r.ReadRespCode(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("[CAPABILITY NO\"OP STARTTLS]")
if _, _, err := r.ReadRespCode(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("[]")
if _, _, err := r.ReadRespCode(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("[{3}\r\nabc]")
if _, _, err := r.ReadRespCode(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("[CAPABILITY NOOP STARTTLS\r\n")
if _, _, err := r.ReadRespCode(); err == nil {
t.Error("Invalid read didn't fail")
}
}
func TestReader_ReadInfo(t *testing.T) {
b, r := newReader("I love potatoes.\r\n")
if info, err := r.ReadInfo(); err != nil {
t.Error(err)
} else if info != "I love potatoes." {
t.Error("Info has not the expected value:", info)
} else if b.Len() > 0 {
t.Error("Buffer is not empty after read")
}
_, r = newReader("I love potatoes.")
if _, err := r.ReadInfo(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("I love potatoes.\r")
if _, err := r.ReadInfo(); err == nil {
t.Error("Invalid read didn't fail")
}
_, r = newReader("I love potatoes.\n")
if _, err := r.ReadInfo(); err != nil {
t.Error(err)
}
_, r = newReader("I love potatoes.\rabc")
if _, err := r.ReadInfo(); err == nil {
t.Error("Invalid read didn't fail")
}
}