forked from fastred/go-imap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_test.go
749 lines (683 loc) · 18.5 KB
/
message_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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
package imap
import (
"bytes"
"fmt"
"reflect"
"testing"
"time"
)
func TestCanonicalFlag(t *testing.T) {
if got := CanonicalFlag("\\SEEN"); got != SeenFlag {
t.Errorf("Invalid canonical flag: expected %q but got %q", SeenFlag, got)
}
if got := CanonicalFlag("Junk"); got != "junk" {
t.Errorf("Invalid canonical flag: expected %q but got %q", "junk", got)
}
}
func TestNewMessage(t *testing.T) {
msg := NewMessage(42, []FetchItem{FetchBodyStructure, FetchFlags})
expected := &Message{
SeqNum: 42,
Items: map[FetchItem]interface{}{FetchBodyStructure: nil, FetchFlags: nil},
Body: make(map[*BodySectionName]Literal),
itemsOrder: []FetchItem{FetchBodyStructure, FetchFlags},
}
if !reflect.DeepEqual(expected, msg) {
t.Errorf("Invalid message: expected \n%+v\n but got \n%+v", expected, msg)
}
}
func formatFields(fields []interface{}) (string, error) {
b := &bytes.Buffer{}
w := NewWriter(b)
if err := w.writeList(fields); err != nil {
return "", fmt.Errorf("Cannot format \n%+v\n got error: \n%v", fields, err)
}
return b.String(), nil
}
var messageTests = []struct {
message *Message
fields []interface{}
}{
{
message: &Message{
Items: map[FetchItem]interface{}{
FetchEnvelope: nil,
FetchBody: nil,
FetchFlags: nil,
FetchRFC822Size: nil,
FetchUid: nil,
},
Body: map[*BodySectionName]Literal{},
Envelope: envelopeTests[0].envelope,
BodyStructure: bodyStructureTests[0].bodyStructure,
Flags: []string{SeenFlag, AnsweredFlag},
Size: 4242,
Uid: 2424,
itemsOrder: []FetchItem{FetchEnvelope, FetchBody, FetchFlags, FetchRFC822Size, FetchUid},
},
fields: []interface{}{
RawString("ENVELOPE"), envelopeTests[0].fields,
RawString("BODY"), bodyStructureTests[0].fields,
RawString("FLAGS"), []interface{}{RawString(SeenFlag), RawString(AnsweredFlag)},
RawString("RFC822.SIZE"), RawString("4242"),
RawString("UID"), RawString("2424"),
},
},
}
func TestMessage_Parse(t *testing.T) {
for i, test := range messageTests {
m := &Message{}
if err := m.Parse(test.fields); err != nil {
t.Errorf("Cannot parse message for #%v: %v", i, err)
} else if !reflect.DeepEqual(m, test.message) {
t.Errorf("Invalid parsed message for #%v: got \n%+v\n but expected \n%+v", i, m, test.message)
}
}
}
func TestMessage_Format(t *testing.T) {
for i, test := range messageTests {
fields := test.message.Format()
got, err := formatFields(fields)
if err != nil {
t.Error(err)
continue
}
expected, _ := formatFields(test.fields)
if got != expected {
t.Errorf("Invalid message fields for #%v: got \n%v\n but expected \n%v", i, got, expected)
}
}
}
var bodySectionNameTests = []struct {
raw string
parsed *BodySectionName
formatted string
}{
{
raw: "BODY[]",
parsed: &BodySectionName{BodyPartName: BodyPartName{}},
},
{
raw: "RFC822",
parsed: &BodySectionName{BodyPartName: BodyPartName{}},
formatted: "BODY[]",
},
{
raw: "BODY[HEADER]",
parsed: &BodySectionName{BodyPartName: BodyPartName{Specifier: HeaderSpecifier}},
},
{
raw: "BODY.PEEK[]",
parsed: &BodySectionName{BodyPartName: BodyPartName{}, Peek: true},
},
{
raw: "BODY[TEXT]",
parsed: &BodySectionName{BodyPartName: BodyPartName{Specifier: TextSpecifier}},
},
{
raw: "RFC822.TEXT",
parsed: &BodySectionName{BodyPartName: BodyPartName{Specifier: TextSpecifier}},
formatted: "BODY[TEXT]",
},
{
raw: "RFC822.HEADER",
parsed: &BodySectionName{BodyPartName: BodyPartName{Specifier: HeaderSpecifier}, Peek: true},
formatted: "BODY.PEEK[HEADER]",
},
{
raw: "BODY[]<0.512>",
parsed: &BodySectionName{BodyPartName: BodyPartName{}, Partial: []int{0, 512}},
},
{
raw: "BODY[]<512>",
parsed: &BodySectionName{BodyPartName: BodyPartName{}, Partial: []int{512}},
},
{
raw: "BODY[1.2.3]",
parsed: &BodySectionName{BodyPartName: BodyPartName{Path: []int{1, 2, 3}}},
},
{
raw: "BODY[1.2.3.HEADER]",
parsed: &BodySectionName{BodyPartName: BodyPartName{Specifier: HeaderSpecifier, Path: []int{1, 2, 3}}},
},
{
raw: "BODY[5.MIME]",
parsed: &BodySectionName{BodyPartName: BodyPartName{Specifier: MIMESpecifier, Path: []int{5}}},
},
{
raw: "BODY[HEADER.FIELDS (From To)]",
parsed: &BodySectionName{BodyPartName: BodyPartName{Specifier: HeaderSpecifier, Fields: []string{"From", "To"}}},
},
{
raw: "BODY[HEADER.FIELDS.NOT (Content-Id)]",
parsed: &BodySectionName{BodyPartName: BodyPartName{Specifier: HeaderSpecifier, Fields: []string{"Content-Id"}, NotFields: true}},
},
}
func TestNewBodySectionName(t *testing.T) {
for i, test := range bodySectionNameTests {
bsn, err := ParseBodySectionName(FetchItem(test.raw))
if err != nil {
t.Errorf("Cannot parse #%v: %v", i, err)
continue
}
if !reflect.DeepEqual(bsn.BodyPartName, test.parsed.BodyPartName) {
t.Errorf("Invalid body part name for #%v: %#+v", i, bsn.BodyPartName)
} else if bsn.Peek != test.parsed.Peek {
t.Errorf("Invalid peek value for #%v: %#+v", i, bsn.Peek)
} else if !reflect.DeepEqual(bsn.Partial, test.parsed.Partial) {
t.Errorf("Invalid partial for #%v: %#+v", i, bsn.Partial)
}
}
}
func TestBodySectionName_String(t *testing.T) {
for i, test := range bodySectionNameTests {
s := string(test.parsed.FetchItem())
expected := test.formatted
if expected == "" {
expected = test.raw
}
if expected != s {
t.Errorf("Invalid body section name for #%v: got %v but expected %v", i, s, expected)
}
}
}
func TestBodySectionName_ExtractPartial(t *testing.T) {
tests := []struct {
bsn string
whole string
partial string
}{
{
bsn: "BODY[]",
whole: "Hello World!",
partial: "Hello World!",
},
{
bsn: "BODY[]<6.5>",
whole: "Hello World!",
partial: "World",
},
{
bsn: "BODY[]<6.1000>",
whole: "Hello World!",
partial: "World!",
},
{
bsn: "BODY[]<0.1>",
whole: "Hello World!",
partial: "H",
},
{
bsn: "BODY[]<1000.2000>",
whole: "Hello World!",
partial: "",
},
}
for i, test := range tests {
bsn, err := ParseBodySectionName(FetchItem(test.bsn))
if err != nil {
t.Errorf("Cannot parse body section name #%v: %v", i, err)
continue
}
partial := string(bsn.ExtractPartial([]byte(test.whole)))
if partial != test.partial {
t.Errorf("Invalid partial for #%v: got %v but expected %v", i, partial, test.partial)
}
}
}
var t = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.FixedZone("", -6*60*60))
var envelopeTests = []struct {
envelope *Envelope
fields []interface{}
}{
{
envelope: &Envelope{
Date: t,
Subject: "Hello World!",
From: []*Address{addrTests[0].addr},
Sender: nil,
ReplyTo: nil,
To: nil,
Cc: nil,
Bcc: nil,
InReplyTo: "[email protected]",
MessageId: "[email protected]",
},
fields: []interface{}{
"Tue, 10 Nov 2009 23:00:00 -0600",
"Hello World!",
[]interface{}{addrTests[0].fields},
nil,
nil,
nil,
nil,
nil,
},
},
}
func TestEnvelope_Parse(t *testing.T) {
for i, test := range envelopeTests {
e := &Envelope{}
if err := e.Parse(test.fields); err != nil {
t.Error("Error parsing envelope:", err)
} else if !reflect.DeepEqual(e, test.envelope) {
t.Errorf("Invalid envelope for #%v: got %v but expected %v", i, e, test.envelope)
}
}
}
func TestEnvelope_Parse_literal(t *testing.T) {
subject := "Hello World!"
l := bytes.NewBufferString(subject)
fields := []interface{}{
"Tue, 10 Nov 2009 23:00:00 -0600",
l,
nil,
nil,
nil,
nil,
nil,
nil,
}
e := &Envelope{}
if err := e.Parse(fields); err != nil {
t.Error("Error parsing envelope:", err)
} else if e.Subject != subject {
t.Errorf("Invalid envelope subject: got %v but expected %v", e.Subject, subject)
}
}
func TestEnvelope_Format(t *testing.T) {
for i, test := range envelopeTests {
fields := test.envelope.Format()
got, err := formatFields(fields)
if err != nil {
t.Error(err)
continue
}
expected, _ := formatFields(test.fields)
if got != expected {
t.Errorf("Invalid envelope fields for #%v: got %v but expected %v", i, got, expected)
}
}
}
var addrTests = []struct {
fields []interface{}
addr *Address
}{
{
fields: []interface{}{"The NSA", nil, "root", "nsa.gov"},
addr: &Address{
PersonalName: "The NSA",
MailboxName: "root",
HostName: "nsa.gov",
},
},
}
func TestAddress_Parse(t *testing.T) {
for i, test := range addrTests {
addr := &Address{}
if err := addr.Parse(test.fields); err != nil {
t.Error("Error parsing address:", err)
} else if !reflect.DeepEqual(addr, test.addr) {
t.Errorf("Invalid address for #%v: got %v but expected %v", i, addr, test.addr)
}
}
}
func TestAddress_Format(t *testing.T) {
for i, test := range addrTests {
fields := test.addr.Format()
if !reflect.DeepEqual(fields, test.fields) {
t.Errorf("Invalid address fields for #%v: got %v but expected %v", i, fields, test.fields)
}
}
}
func TestAddressList(t *testing.T) {
fields := make([]interface{}, len(addrTests))
addrs := make([]*Address, len(addrTests))
for i, test := range addrTests {
fields[i] = test.fields
addrs[i] = test.addr
}
gotAddrs := ParseAddressList(fields)
if !reflect.DeepEqual(gotAddrs, addrs) {
t.Error("Invalid address list: got", gotAddrs, "but expected", addrs)
}
gotFields := FormatAddressList(addrs)
if !reflect.DeepEqual(gotFields, fields) {
t.Error("Invalid address list fields: got", gotFields, "but expected", fields)
}
}
func TestEmptyAddressList(t *testing.T) {
addrs := make([]*Address, 0)
gotFields := FormatAddressList(addrs)
if !reflect.DeepEqual(gotFields, nil) {
t.Error("Invalid address list fields: got", gotFields, "but expected nil")
}
}
var paramsListTest = []struct {
fields []interface{}
params map[string]string
}{
{
fields: nil,
params: map[string]string{},
},
{
fields: []interface{}{"a", "b"},
params: map[string]string{"a": "b"},
},
}
func TestParseParamList(t *testing.T) {
for i, test := range paramsListTest {
if params, err := ParseParamList(test.fields); err != nil {
t.Errorf("Cannot parse params fields for #%v: %v", i, err)
} else if !reflect.DeepEqual(params, test.params) {
t.Errorf("Invalid params for #%v: got %v but expected %v", i, params, test.params)
}
}
// Malformed params lists
fields := []interface{}{"cc", []interface{}{"dille"}}
if params, err := ParseParamList(fields); err == nil {
t.Error("Parsed invalid params list:", params)
}
fields = []interface{}{"cc"}
if params, err := ParseParamList(fields); err == nil {
t.Error("Parsed invalid params list:", params)
}
}
func TestFormatParamList(t *testing.T) {
for i, test := range paramsListTest {
fields := FormatParamList(test.params)
if !reflect.DeepEqual(fields, test.fields) {
t.Errorf("Invalid params fields for #%v: got %v but expected %v", i, fields, test.fields)
}
}
}
var bodyStructureTests = []struct {
fields []interface{}
bodyStructure *BodyStructure
}{
{
fields: []interface{}{"image", "jpeg", []interface{}{}, "<foo4%[email protected]>", "A picture of cat", "base64", RawString("4242")},
bodyStructure: &BodyStructure{
MIMEType: "image",
MIMESubType: "jpeg",
Params: map[string]string{},
Id: "<foo4%[email protected]>",
Description: "A picture of cat",
Encoding: "base64",
Size: 4242,
},
},
{
fields: []interface{}{"text", "plain", []interface{}{"charset", "utf-8"}, nil, nil, "us-ascii", RawString("42"), RawString("2")},
bodyStructure: &BodyStructure{
MIMEType: "text",
MIMESubType: "plain",
Params: map[string]string{"charset": "utf-8"},
Encoding: "us-ascii",
Size: 42,
Lines: 2,
},
},
{
fields: []interface{}{
"message", "rfc822", []interface{}{}, nil, nil, "us-ascii", RawString("42"),
(&Envelope{}).Format(),
(&BodyStructure{}).Format(),
RawString("67"),
},
bodyStructure: &BodyStructure{
MIMEType: "message",
MIMESubType: "rfc822",
Params: map[string]string{},
Encoding: "us-ascii",
Size: 42,
Lines: 67,
Envelope: &Envelope{
From: nil,
Sender: nil,
ReplyTo: nil,
To: nil,
Cc: nil,
Bcc: nil,
},
BodyStructure: &BodyStructure{
Params: map[string]string{},
},
},
},
{
fields: []interface{}{
"application", "pdf", []interface{}{}, nil, nil, "base64", RawString("4242"),
"e0323a9039add2978bf5b49550572c7c",
[]interface{}{"attachment", []interface{}{"filename", "document.pdf"}},
[]interface{}{"en-US"}, []interface{}{},
},
bodyStructure: &BodyStructure{
MIMEType: "application",
MIMESubType: "pdf",
Params: map[string]string{},
Encoding: "base64",
Size: 4242,
Extended: true,
MD5: "e0323a9039add2978bf5b49550572c7c",
Disposition: "attachment",
DispositionParams: map[string]string{"filename": "document.pdf"},
Language: []string{"en-US"},
Location: []string{},
},
},
{
fields: []interface{}{
[]interface{}{"text", "plain", []interface{}{}, nil, nil, "us-ascii", RawString("87"), RawString("22")},
[]interface{}{"text", "html", []interface{}{}, nil, nil, "us-ascii", RawString("106"), RawString("36")},
"alternative",
},
bodyStructure: &BodyStructure{
MIMEType: "multipart",
MIMESubType: "alternative",
Params: map[string]string{},
Parts: []*BodyStructure{
{
MIMEType: "text",
MIMESubType: "plain",
Params: map[string]string{},
Encoding: "us-ascii",
Size: 87,
Lines: 22,
},
{
MIMEType: "text",
MIMESubType: "html",
Params: map[string]string{},
Encoding: "us-ascii",
Size: 106,
Lines: 36,
},
},
},
},
{
fields: []interface{}{
[]interface{}{"text", "plain", []interface{}{}, nil, nil, "us-ascii", RawString("87"), RawString("22")},
"alternative", []interface{}{"hello", "world"},
[]interface{}{"inline", []interface{}{}},
[]interface{}{"en-US"}, []interface{}{},
},
bodyStructure: &BodyStructure{
MIMEType: "multipart",
MIMESubType: "alternative",
Params: map[string]string{"hello": "world"},
Parts: []*BodyStructure{
{
MIMEType: "text",
MIMESubType: "plain",
Params: map[string]string{},
Encoding: "us-ascii",
Size: 87,
Lines: 22,
},
},
Extended: true,
Disposition: "inline",
DispositionParams: map[string]string{},
Language: []string{"en-US"},
Location: []string{},
},
},
}
func TestBodyStructure_Parse(t *testing.T) {
for i, test := range bodyStructureTests {
bs := &BodyStructure{}
if err := bs.Parse(test.fields); err != nil {
t.Errorf("Cannot parse #%v: %v", i, err)
} else if !reflect.DeepEqual(bs, test.bodyStructure) {
t.Errorf("Invalid body structure for #%v: got \n%+v\n but expected \n%+v", i, bs, test.bodyStructure)
}
}
}
func TestBodyStructure_Format(t *testing.T) {
for i, test := range bodyStructureTests {
fields := test.bodyStructure.Format()
got, err := formatFields(fields)
if err != nil {
t.Error(err)
continue
}
expected, _ := formatFields(test.fields)
if got != expected {
t.Errorf("Invalid body structure fields for #%v: has \n%v\n but expected \n%v", i, got, expected)
}
}
}
func TestBodyStructureFilename(t *testing.T) {
tests := []struct {
bs BodyStructure
filename string
}{
{
bs: BodyStructure{
DispositionParams: map[string]string{"filename": "cat.png"},
},
filename: "cat.png",
},
{
bs: BodyStructure{
Params: map[string]string{"name": "cat.png"},
},
filename: "cat.png",
},
{
bs: BodyStructure{},
filename: "",
},
{
bs: BodyStructure{
DispositionParams: map[string]string{"filename": "=?UTF-8?Q?Opis_przedmiotu_zam=c3=b3wienia_-_za=c5=82=c4=85cznik_nr_1?= =?UTF-8?Q?=2epdf?="},
},
filename: "Opis przedmiotu zamówienia - załącznik nr 1.pdf",
},
}
for i, test := range tests {
got, err := test.bs.Filename()
if err != nil {
t.Errorf("Invalid body structure filename for #%v: error: %v", i, err)
continue
}
if got != test.filename {
t.Errorf("Invalid body structure filename for #%v: got '%v', want '%v'", i, got, test.filename)
}
}
}
func TestBodyStructureWalk(t *testing.T) {
textPlain := &BodyStructure{
MIMEType: "text",
MIMESubType: "plain",
}
textHTML := &BodyStructure{
MIMEType: "text",
MIMESubType: "plain",
}
multipartAlternative := &BodyStructure{
MIMEType: "multipart",
MIMESubType: "alternative",
Parts: []*BodyStructure{textPlain, textHTML},
}
imagePNG := &BodyStructure{
MIMEType: "image",
MIMESubType: "png",
}
multipartMixed := &BodyStructure{
MIMEType: "multipart",
MIMESubType: "mixed",
Parts: []*BodyStructure{multipartAlternative, imagePNG},
}
type testNode struct {
path []int
part *BodyStructure
}
tests := []struct {
bs *BodyStructure
nodes []testNode
walkChildren bool
}{
{
bs: textPlain,
nodes: []testNode{
{path: []int{1}, part: textPlain},
},
},
{
bs: multipartAlternative,
nodes: []testNode{
{path: nil, part: multipartAlternative},
{path: []int{1}, part: textPlain},
{path: []int{2}, part: textHTML},
},
walkChildren: true,
},
{
bs: multipartMixed,
nodes: []testNode{
{path: nil, part: multipartMixed},
{path: []int{1}, part: multipartAlternative},
{path: []int{1, 1}, part: textPlain},
{path: []int{1, 2}, part: textHTML},
{path: []int{2}, part: imagePNG},
},
walkChildren: true,
},
{
bs: multipartMixed,
nodes: []testNode{
{path: nil, part: multipartMixed},
},
walkChildren: false,
},
}
for i, test := range tests {
j := 0
test.bs.Walk(func(path []int, part *BodyStructure) bool {
if j >= len(test.nodes) {
t.Errorf("Test #%v: invalid node count: got > %v, want %v", i, j, len(test.nodes))
return false
}
n := &test.nodes[j]
if !reflect.DeepEqual(path, n.path) {
t.Errorf("Test #%v: node #%v: invalid path: got %v, want %v", i, j, path, n.path)
}
if part != n.part {
t.Errorf("Test #%v: node #%v: invalid part: got %v, want %v", i, j, part, n.part)
}
j++
return test.walkChildren
})
if j != len(test.nodes) {
t.Errorf("Test #%v: invalid node count: got %v, want %v", i, j, len(test.nodes))
}
}
}