-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparse_address_list_test.go
685 lines (657 loc) · 14.7 KB
/
parse_address_list_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
package rfc5322
import (
"encoding/xml"
"io"
"net/mail"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParseSingleAddress(t *testing.T) {
tests := []struct {
input string
addrs []*mail.Address
}{
{
input: `[email protected]`,
addrs: []*mail.Address{{
Address: `[email protected]`,
}},
},
{
input: `John Doe <[email protected]>`,
addrs: []*mail.Address{{
Name: `John Doe`,
Address: `[email protected]`,
}},
},
{
input: `Mary Smith <[email protected]>`,
addrs: []*mail.Address{{
Name: `Mary Smith`,
Address: `[email protected]`,
}},
},
{
input: `"Joe Q. Public" <[email protected]>`,
addrs: []*mail.Address{{
Name: `Joe Q. Public`,
Address: `[email protected]`,
}},
},
{
input: `Mary Smith <[email protected]>`,
addrs: []*mail.Address{{
Name: `Mary Smith`,
Address: `[email protected]`,
}},
},
{
input: `[email protected]`,
addrs: []*mail.Address{{
Address: `[email protected]`,
}},
},
{
input: `Who? <[email protected]>`,
addrs: []*mail.Address{{
Name: `Who?`,
Address: `[email protected]`,
}},
},
{
input: `<[email protected]>`,
addrs: []*mail.Address{{
Address: `[email protected]`,
}},
},
{
input: `"Giant; \"Big\" Box" <[email protected]>`,
addrs: []*mail.Address{{
Name: `Giant; "Big" Box`,
Address: `[email protected]`,
}},
},
{
input: `Pete <[email protected]>`,
addrs: []*mail.Address{{
Name: `Pete`,
Address: `[email protected]`,
}},
},
{
input: `"Mary Smith: Personal Account" <[email protected]>`,
addrs: []*mail.Address{{
Name: `Mary Smith: Personal Account`,
Address: `[email protected]`,
}},
},
{
input: `Pete(A nice \) chap) <pete(his account)@silly.test(his host)>`,
addrs: []*mail.Address{{
Name: `Pete`,
Address: `[email protected]`,
}},
},
{
input: `Gogh Fir <[email protected]>`,
addrs: []*mail.Address{{
Name: `Gogh Fir`,
Address: `[email protected]`,
}},
},
{
input: `normal name <[email protected]>`,
addrs: []*mail.Address{{
Name: `normal name`,
Address: `[email protected]`,
}},
},
{
input: `"comma, name" <[email protected]>`,
addrs: []*mail.Address{{
Name: `comma, name`,
Address: `[email protected]`,
}},
},
{
input: `name <[email protected]> (ignore comment)`,
addrs: []*mail.Address{{
Name: `name`,
Address: `[email protected]`,
}},
},
{
input: `"Mail Robot" <>`,
addrs: []*mail.Address{{
Name: `Mail Robot`,
}},
},
{
input: `Michal Hořejšek <hořejš[email protected]>`,
addrs: []*mail.Address{{
Name: `Michal Hořejšek`,
Address: `hořejš[email protected]`, // Not his real address.
}},
},
{
input: `First Last <[email protected] >`,
addrs: []*mail.Address{{
Name: `First Last`,
Address: `[email protected]`,
}},
},
{
input: `First Last <[email protected]. >`,
addrs: []*mail.Address{{
Name: `First Last`,
Address: `[email protected].`,
}},
},
{
input: `First Last <[email protected].>`,
addrs: []*mail.Address{{
Name: `First Last`,
Address: `[email protected].`,
}},
},
{
input: `First Last <[email protected]:25>`,
addrs: []*mail.Address{{
Name: `First Last`,
Address: `[email protected]:25`,
}},
},
{
input: `First Last <user@[10.0.0.1]>`,
addrs: []*mail.Address{{
Name: `First Last`,
Address: `user@[10.0.0.1]`,
}},
},
{
input: `<postmaster@[10.10.10.10]>`,
addrs: []*mail.Address{{
Address: `postmaster@[10.10.10.10]`,
}},
},
{
input: `First Last < [email protected]>`,
addrs: []*mail.Address{{
Name: `First Last`,
Address: `[email protected]`,
}},
},
{
input: `[email protected],`,
addrs: []*mail.Address{{
Address: `[email protected]`,
}},
},
{
input: `First Middle "Last" <[email protected]>`,
addrs: []*mail.Address{{
Name: `First Middle Last`,
Address: `[email protected]`,
}},
},
{
input: `First Middle Last <[email protected]>`,
addrs: []*mail.Address{{
Name: `First Middle Last`,
Address: `[email protected]`,
}},
},
{
input: `First Middle"Last" <[email protected]>`,
addrs: []*mail.Address{{
Name: `First Middle Last`,
Address: `[email protected]`,
}},
},
{
input: `First Middle "Last"<[email protected]>`,
addrs: []*mail.Address{{
Name: `First Middle Last`,
Address: `[email protected]`,
}},
},
{
input: `First "Middle" "Last" <[email protected]>`,
addrs: []*mail.Address{{
Name: `First Middle Last`,
Address: `[email protected]`,
}},
},
{
input: `First "Middle""Last" <[email protected]>`,
addrs: []*mail.Address{{
Name: `First Middle Last`,
Address: `[email protected]`,
}},
},
{
input: `first.last <[email protected]>`,
addrs: []*mail.Address{{
Name: `first.last`,
Address: `[email protected]`,
}},
},
{
input: `first . last <[email protected]>`,
addrs: []*mail.Address{{
Name: `first.last`,
Address: `[email protected]`,
}},
},
{
input: `user@domain <[email protected]>`,
addrs: []*mail.Address{{
Name: `user@domain`,
Address: `[email protected]`,
}},
},
{
input: `user @ domain <[email protected]>`,
addrs: []*mail.Address{{
Name: `user@domain`,
Address: `[email protected]`,
}},
},
}
for _, test := range tests {
test := test
t.Run(test.input, func(t *testing.T) {
addrs, err := ParseAddressList(test.input)
assert.NoError(t, err)
assert.ElementsMatch(t, test.addrs, addrs)
})
}
}
func TestParseSingleAddressEncodedWord(t *testing.T) {
tests := []struct {
input string
addrs []*mail.Address
}{
{
input: `=?US-ASCII?Q?Keith_Moore?= <[email protected]>`,
addrs: []*mail.Address{{
Name: `Keith Moore`,
Address: `[email protected]`,
}},
},
{
input: `=?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <[email protected]>`,
addrs: []*mail.Address{{
Name: `Keld Jørn Simonsen`,
Address: `[email protected]`,
}},
},
{
input: `=?ISO-8859-1?Q?Andr=E9?= Pirard <[email protected]>`,
addrs: []*mail.Address{{
Name: `André Pirard`,
Address: `[email protected]`,
}},
},
{
input: `=?ISO-8859-1?Q?Olle_J=E4rnefors?= <[email protected]>`,
addrs: []*mail.Address{{
Name: `Olle Järnefors`,
Address: `[email protected]`,
}},
},
{
input: `=?ISO-8859-1?Q?Patrik_F=E4ltstr=F6m?= <[email protected]>`,
addrs: []*mail.Address{{
Name: `Patrik Fältström`,
Address: `[email protected]`,
}},
},
{
input: `Nathaniel Borenstein <[email protected]> (=?iso-8859-8?b?7eXs+SDv4SDp7Oj08A==?=)`,
addrs: []*mail.Address{{
Name: `Nathaniel Borenstein`,
Address: `[email protected]`,
}},
},
{
input: `=?UTF-8?B?PEJlemUgam3DqW5hPg==?= <[email protected]>`,
addrs: []*mail.Address{{
Name: `<Beze jména>`,
Address: `[email protected]`,
}},
},
{
input: `First Middle =?utf-8?Q?Last?= <[email protected]>`,
addrs: []*mail.Address{{
Name: `First Middle Last`,
Address: `[email protected]`,
}},
},
{
input: `First Middle=?utf-8?Q?Last?= <[email protected]>`,
addrs: []*mail.Address{{
Name: `First Middle=?utf-8?Q?Last?=`,
Address: `[email protected]`,
}},
},
{
input: `First Middle =?utf-8?Q?Last?=<[email protected]>`,
addrs: []*mail.Address{{
Name: `First Middle Last`,
Address: `[email protected]`,
}},
},
{
input: `First =?utf-8?Q?Middle?= =?utf-8?Q?Last?= <[email protected]>`,
addrs: []*mail.Address{{
Name: `First MiddleLast`,
Address: `[email protected]`,
}},
},
{
input: `First =?utf-8?Q?Middle?==?utf-8?Q?Last?= <[email protected]>`,
addrs: []*mail.Address{{
Name: `First MiddleLast`,
Address: `[email protected]`,
}},
},
{
input: `First "Middle"=?utf-8?Q?Last?= <[email protected]>`,
addrs: []*mail.Address{{
Name: `First Middle Last`,
Address: `[email protected]`,
}},
},
{
input: `First "Middle" =?utf-8?Q?Last?= <[email protected]>`,
addrs: []*mail.Address{{
Name: `First Middle Last`,
Address: `[email protected]`,
}},
},
{
input: `First "Middle" =?utf-8?Q?Last?=<[email protected]>`,
addrs: []*mail.Address{{
Name: `First Middle Last`,
Address: `[email protected]`,
}},
},
{
input: `=?UTF-8?B?PEJlemUgam3DqW5hPg==?= <[email protected]>`,
addrs: []*mail.Address{{
Name: `<Beze jména>`,
Address: `[email protected]`,
}},
},
}
for _, test := range tests {
test := test
t.Run(test.input, func(t *testing.T) {
addrs, err := ParseAddressList(test.input)
assert.NoError(t, err)
assert.ElementsMatch(t, test.addrs, addrs)
})
}
}
func TestParseAddressList(t *testing.T) {
tests := []struct {
input string
addrs []*mail.Address
}{
{
addrs: []*mail.Address{
{
Name: `Alice`,
Address: `[email protected]`,
},
{
Name: `Bob`,
Address: `[email protected]`,
},
{
Name: `Eve`,
Address: `[email protected]`,
},
},
},
{
addrs: []*mail.Address{
{
Name: `Alice`,
Address: `[email protected]`,
},
{
Name: `Bob`,
Address: `[email protected]`,
},
{
Name: `Eve`,
Address: `[email protected]`,
},
},
},
{
addrs: []*mail.Address{
{
Name: `Ed Jones`,
Address: `[email protected]`,
},
{
Address: `[email protected]`,
},
{
Name: `John`,
Address: `[email protected]`,
},
},
},
{
input: `name (ignore comment) <[email protected]>, (Comment as name) [email protected]`,
addrs: []*mail.Address{
{
Name: `name`,
Address: `[email protected]`,
},
{
Address: `[email protected]`,
},
},
},
{
input: `"normal name" <[email protected]>, "comma, name" <[email protected]>`,
addrs: []*mail.Address{
{
Name: `normal name`,
Address: `[email protected]`,
},
{
Name: `comma, name`,
Address: `[email protected]`,
},
},
},
{
input: `"comma, one" <[email protected]>, "comma, two" <[email protected]>`,
addrs: []*mail.Address{
{
Name: `comma, one`,
Address: `[email protected]`,
},
{
Name: `comma, two`,
Address: `[email protected]`,
},
},
},
{
input: `normal name <[email protected]>, (comment)All.(around)address@(the)server.com`,
addrs: []*mail.Address{
{
Name: `normal name`,
Address: `[email protected]`,
},
{
Address: `[email protected]`,
},
},
},
{
input: `normal name <[email protected]>, All.("comma, in comment")address@(the)server.com`,
addrs: []*mail.Address{
{
Name: `normal name`,
Address: `[email protected]`,
},
{
Address: `[email protected]`,
},
},
},
}
for _, test := range tests {
test := test
t.Run(test.input, func(t *testing.T) {
addrs, err := ParseAddressList(test.input)
assert.NoError(t, err)
assert.ElementsMatch(t, test.addrs, addrs)
})
}
}
func TestParseGroup(t *testing.T) {
tests := []struct {
input string
addrs []*mail.Address
}{
{
addrs: []*mail.Address{
{
Name: `Ed Jones`,
Address: `[email protected]`,
},
{
Address: `[email protected]`,
},
{
Name: `John`,
Address: `[email protected]`,
},
},
},
{
input: `undisclosed recipients:;`,
addrs: []*mail.Address{},
},
{
// We permit the group to not end in a semicolon, although as per RFC5322 it really should.
input: `undisclosed recipients:`,
addrs: []*mail.Address{},
},
{
// We permit the group to be surrounded with quotes, although as per RFC5322 it really shouldn't be.
input: `"undisclosed recipients:"`,
addrs: []*mail.Address{},
},
{
input: `(Empty list)(start)Hidden recipients :(nobody(that I know)) ;`,
addrs: []*mail.Address{},
},
}
for _, test := range tests {
test := test
t.Run(test.input, func(t *testing.T) {
addrs, err := ParseAddressList(test.input)
assert.NoError(t, err)
assert.ElementsMatch(t, test.addrs, addrs)
})
}
}
// TestParseRejectedAddresses tests that weird addresses that are rejected by
// serverside are also rejected by us. If for some reason we end up being able
// to parse these malformed addresses, great! For now let's collect them here.
func TestParseRejectedAddresses(t *testing.T) {
tests := []struct {
input string
addrs []*mail.Address
}{
{input: `"comma, name" <[email protected]>, another, name <[email protected]>`},
{input: `username`},
{input: `=?ISO-8859-2?Q?First_Last?= <[email protected]>, <[email protected],First/AAA/BBB/CCC,>`},
{input: `[email protected]`},
{input: `=?windows-1250?Q?Spr=E1vce_syst=E9mu?=`},
{input: `"'[email protected].'"`},
{input: `<this is not an email address>`},
}
for _, test := range tests {
test := test
t.Run(test.input, func(t *testing.T) {
_, err := ParseAddressList(test.input)
assert.Error(t, err)
})
}
}
// TestIsEmailValidCategory runs over the "IsEmail" standard tests,
// ensuring it can at least recognize all emails in the "valid" category.
// In future, we should expand these tests to run over more categories.
func TestIsEmailValidCategory(t *testing.T) {
f, err := os.Open("tests.xml")
require.NoError(t, err)
defer func() { require.NoError(t, err) }()
for test := range readTestCases(f) {
test := test
if test.category != "ISEMAIL_VALID_CATEGORY" {
continue
}
t.Run(test.id, func(t *testing.T) {
_, err := ParseAddressList(test.address)
assert.NoError(t, err)
})
}
}
type testCase struct {
id string
address string
category string
diagnosis string
}
func readTestCases(r io.Reader) chan testCase {
ch := make(chan testCase)
var (
test testCase
data string
)
go func() {
decoder := xml.NewDecoder(r)
for token, err := decoder.Token(); err == nil; token, err = decoder.Token() {
switch t := token.(type) {
case xml.StartElement:
if t.Name.Local == "test" {
test = testCase{
id: t.Attr[0].Value,
}
}
case xml.EndElement:
switch t.Name.Local {
case "test":
ch <- test
case "address":
test.address = data
case "category":
test.category = data
case "diagnosis":
test.diagnosis = data
}
case xml.CharData:
data = string(t)
}
}
close(ch)
}()
return ch
}