File: parser_mailbox_test.py

package info (click to toggle)
python-flanker 0.9.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,976 kB
  • sloc: python: 9,308; makefile: 4
file content (543 lines) | stat: -rw-r--r-- 28,174 bytes parent folder | download
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
# coding:utf-8
import six
from nose.tools import nottest, assert_equal
from ply.lex import LexError
from ply.yacc import YaccError

from flanker.addresslib import address
from flanker.addresslib.address import EmailAddress

VALID_QTEXT = [chr(x) for x in [0x21] + list(range(0x23, 0x5b)) + list(range(0x5d, 0x7e))]
VALID_QUOTED_PAIR = [chr(x) for x in range(0x20, 0x7e)]

FULL_QTEXT = ''.join(VALID_QTEXT)
FULL_QUOTED_PAIR = '\\' + '\\'.join(VALID_QUOTED_PAIR)

CONTROL_CHARS = ''.join(map(six.unichr, list(range(0, 9)) + list(range(14, 32)) + [127]))

@nottest
def chunks(l, n):
    for i in range(0, len(l), n):
        yield l[i:i+n]

@nottest
def run_full_mailbox_test(string, expected, full_spec=None):
    mbox = address.parse(string, strict=True)
    if mbox:
        assert_equal(expected.display_name, mbox.display_name)
        assert_equal(expected.address, mbox.address)
        if full_spec:
            assert_equal(full_spec, mbox.full_spec())
        assert_equal(mbox, address.parse(mbox.to_unicode(), strict=True)) # check symmetry
        return
    assert_equal(expected, mbox)

@nottest
def run_mailbox_test(string, expected_string):
    mbox = address.parse(string, strict=True)
    if mbox:
        assert_equal(expected_string, mbox.address)
        return
    assert_equal(expected_string, mbox)


def test_mailbox():
    "Grammar: mailbox -> name-addr | addr-spec"

    # sanity
    run_full_mailbox_test('Steve Jobs <steve@apple.com>', EmailAddress('Steve Jobs', 'steve@apple.com'))
    run_full_mailbox_test('"Steve Jobs" <steve@apple.com>', EmailAddress('Steve Jobs', 'steve@apple.com'))

    run_mailbox_test('steve@apple.com', 'steve@apple.com')


def test_name_addr():
    "Grammar: name-addr -> [ display-name ] angle-addr"

    # sanity
    run_full_mailbox_test('Linus Torvalds <linus@kernel.org>', EmailAddress('Linus Torvalds','linus@kernel.org'))
    run_mailbox_test('Linus Torvalds', None)
    run_mailbox_test('Linus Torvalds <>', None)
    run_mailbox_test('linus@kernel.org', 'linus@kernel.org')
    try:
        run_mailbox_test(' ', None)
    except LexError:
        pass
    except YaccError:
        pass


def test_display_name():
    "Grammar: display-name -> word { [ whitespace ] word }"

    # pass atom display-name rfc
    run_full_mailbox_test('ABCDEFGHIJKLMNOPQRSTUVWXYZ <a@b>', EmailAddress('ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'a@b'))
    run_full_mailbox_test('abcdefghijklmnopqrstuvwzyz <a@b>', EmailAddress('abcdefghijklmnopqrstuvwzyz', 'a@b'))
    run_full_mailbox_test('0123456789 <a@b>', EmailAddress('0123456789', 'a@b'))
    run_full_mailbox_test('!#$%&\'*+-/=?^_`{|}~ <a@b>', EmailAddress('!#$%&\'*+-/=?^_`{|}~', 'a@b'))
    run_full_mailbox_test('Bill <bill@microsoft.com>', EmailAddress('Bill', 'bill@microsoft.com'))
    run_full_mailbox_test('Bill Gates <bill@microsoft.com>', EmailAddress('Bill Gates', 'bill@microsoft.com'))
    run_full_mailbox_test(' Bill  Gates <bill@microsoft.com>', EmailAddress('Bill Gates', 'bill@microsoft.com'))
    run_full_mailbox_test(' Bill Gates <bill@microsoft.com>', EmailAddress('Bill Gates', 'bill@microsoft.com'))
    run_full_mailbox_test('Bill Gates<bill@microsoft.com>', EmailAddress('Bill Gates', 'bill@microsoft.com'))
    run_full_mailbox_test(' Bill Gates<bill@microsoft.com>', EmailAddress('Bill Gates', 'bill@microsoft.com'))
    run_full_mailbox_test(' Bill<bill@microsoft.com>', EmailAddress('Bill', 'bill@microsoft.com'))

    # fail atom display-name rfc
    run_full_mailbox_test('< <bill@microsoft.com>', None)
    run_full_mailbox_test('< bill <bill@microsoft.com>', None)
    run_full_mailbox_test(' < bill <bill@microsoft.com>', None)

    # pass display-name quoted-string rfc
    run_full_mailbox_test('"{0}" <a@b>'.format(FULL_QTEXT),
                          EmailAddress(FULL_QTEXT, 'a@b'))
    run_full_mailbox_test('"{0}" <a@b>'.format(FULL_QUOTED_PAIR),
                          EmailAddress(''.join(VALID_QUOTED_PAIR), 'a@b'))
    run_full_mailbox_test('"<a@b>" <a@b>', EmailAddress('<a@b>', 'a@b'))
    run_full_mailbox_test('"Bill" <bill@microsoft.com>',
                          EmailAddress('Bill', 'bill@microsoft.com'))
    run_full_mailbox_test('"Bill Gates" <bill@microsoft.com>',
                          EmailAddress('Bill Gates', 'bill@microsoft.com'))
    run_full_mailbox_test('" Bill Gates" <bill@microsoft.com>',
                          EmailAddress(' Bill Gates', 'bill@microsoft.com'))
    run_full_mailbox_test('"Bill Gates " <bill@microsoft.com>',
                          EmailAddress('Bill Gates ', 'bill@microsoft.com'))
    run_full_mailbox_test('" Bill Gates " <bill@microsoft.com>',
                          EmailAddress(' Bill Gates ', 'bill@microsoft.com'))
    run_full_mailbox_test(' " Bill Gates "<bill@microsoft.com>',
                          EmailAddress(' Bill Gates ', 'bill@microsoft.com'))

    # fail display-name quoted-string rfc
    run_mailbox_test('"{0} <a@b>"'.format(FULL_QUOTED_PAIR), None)
    run_mailbox_test('"{0} <a@b>'.format(FULL_QTEXT), None)
    run_mailbox_test('{0}" <a@b>'.format(FULL_QUOTED_PAIR), None)
    run_mailbox_test('{0} <a@b>'.format(FULL_QUOTED_PAIR), None)
    run_mailbox_test(u'{0} <a@b>'.format(''.join(CONTROL_CHARS)), None)
    run_mailbox_test(u'"{0}" <a@b>'.format(''.join(CONTROL_CHARS)), None)
    for cc in CONTROL_CHARS:

        # FIXME: In Python 3 subgroup of separator symbols is treated as
        # FIXME: allowed. We need to figure out why.
        if six.PY3 and ord(cc) in [0x1c, 0x1d, 0x1e, 0x1f]:
            continue

        run_mailbox_test(u'"{0}" <a@b>'.format(cc), None)
        run_mailbox_test(u'{0} <a@b>'.format(cc), None)

    # pass unicode display-name sanity
    run_full_mailbox_test(u'Bill <bill@microsoft.com>', EmailAddress(u'Bill', 'bill@microsoft.com'))
    run_full_mailbox_test(u'ϐill <bill@microsoft.com>', EmailAddress(u'ϐill', 'bill@microsoft.com'))
    run_full_mailbox_test(u'ϐΙλλ <bill@microsoft.com>', EmailAddress(u'ϐΙλλ', 'bill@microsoft.com'))
    run_full_mailbox_test(u'ϐΙλλ Γαθεσ <bill@microsoft.com>', EmailAddress(u'ϐΙλλ Γαθεσ', 'bill@microsoft.com'))
    run_full_mailbox_test(u'BΙλλ Γαθεσ <bill@microsoft.com>', EmailAddress(u'BΙλλ Γαθεσ', 'bill@microsoft.com'))
    run_full_mailbox_test(u'Bill Γαθεσ <bill@microsoft.com>', EmailAddress(u'Bill Γαθεσ', 'bill@microsoft.com'))

    # period in display name
    run_full_mailbox_test(u'Bill. Gates. <bill@microsoft.com>', EmailAddress(u'Bill. Gates.', 'bill@microsoft.com'))

    # name addr without display name
    run_full_mailbox_test(u'<bill@microsoft.com>', EmailAddress(None, 'bill@microsoft.com'))


def test_unicode_display_name():
    # unicode, no quotes, display-name rfc
    run_full_mailbox_test(u'ö <{0}>'.format(u'foo@example.com'),
        EmailAddress(u'ö', 'foo@example.com'), '=?utf-8?b?w7Y=?= <foo@example.com>')
    run_full_mailbox_test(u'Föö <{0}>'.format(u'foo@example.com'),
        EmailAddress(u'Föö', 'foo@example.com'), '=?utf-8?b?RsO2w7Y=?= <foo@example.com>')
    run_full_mailbox_test(u'Foo ö <{0}>'.format(u'foo@example.com'),
        EmailAddress(u'Foo ö', 'foo@example.com'), '=?utf-8?b?Rm9vIMO2?= <foo@example.com>')
    run_full_mailbox_test(u'Foo Föö <{0}>'.format(u'foo@example.com'),
        EmailAddress(u'Foo Föö', 'foo@example.com'), '=?utf-8?b?Rm9vIEbDtsO2?= <foo@example.com>')
    run_full_mailbox_test(u'Foo Föö Foo <{0}>'.format(u'foo@example.com'),
        EmailAddress(u'Foo Föö Foo', 'foo@example.com'), '=?utf-8?b?Rm9vIEbDtsO2IEZvbw==?= <foo@example.com>')
    run_full_mailbox_test(u'Foo Föö Foo Föö <{0}>'.format(u'foo@example.com'),
        EmailAddress(u'Foo Föö Foo Föö', 'foo@example.com'), '=?utf-8?b?Rm9vIEbDtsO2IEZvbyBGw7bDtg==?= <foo@example.com>')

    # unicode, quotes, display-name rfc
    # Note that redundant quotes are removed from the parsed address
    run_full_mailbox_test(
        u'"ö" <foo@example.com>',
        EmailAddress(u'ö', 'foo@example.com'),
        '=?utf-8?b?w7Y=?= <foo@example.com>')
    run_full_mailbox_test(
        u'"Föö" <foo@example.com>',
        EmailAddress(u'Föö', 'foo@example.com'),
        '=?utf-8?b?RsO2w7Y=?= <foo@example.com>')
    run_full_mailbox_test(
        u'"Foo ö" <foo@example.com>',
        EmailAddress(u'Foo ö', 'foo@example.com'),
        '=?utf-8?b?Rm9vIMO2?= <foo@example.com>')
    run_full_mailbox_test(
        u'"Foo Föö" <foo@example.com>',
        EmailAddress(u'Foo Föö', 'foo@example.com'),
        '=?utf-8?b?Rm9vIEbDtsO2?= <foo@example.com>')
    run_full_mailbox_test(
        u'"Foo Föö Foo" <foo@example.com>',
        EmailAddress(u'Foo Föö Foo', 'foo@example.com'),
        '=?utf-8?b?Rm9vIEbDtsO2IEZvbw==?= <foo@example.com>')
    run_full_mailbox_test(
        u'"Foo Föö Foo Föö" <foo@example.com>',
        EmailAddress(u'Foo Föö Foo Föö', 'foo@example.com'),
        '=?utf-8?b?Rm9vIEbDtsO2IEZvbyBGw7bDtg==?= <foo@example.com>')

    # unicode, random language sampling, see: http://www.columbia.edu/~fdc/utf8/index.html
    run_full_mailbox_test(u'나는 유리를 먹을 수 있어요 <foo@example.com>',
        EmailAddress(u'나는 유리를 먹을 수 있어요', 'foo@example.com'),
        '=?utf-8?b?64KY64qUIOycoOumrOulvCDrqLnsnYQg7IiYIOyeiOyWtOyalA==?= <foo@example.com>')
    run_full_mailbox_test(u'私はガラスを食べられます <foo@example.com>',
        EmailAddress(u'私はガラスを食べられます', 'foo@example.com'),
        '=?utf-8?b?56eB44Gv44Ks44Op44K544KS6aOf44G544KJ44KM44G+44GZ?= <foo@example.com>')
    run_full_mailbox_test(u'ᛖᚴ ᚷᛖᛏ ᛖᛏᛁ <foo@example.com>',
        EmailAddress(u'ᛖᚴ ᚷᛖᛏ ᛖᛏᛁ', 'foo@example.com'),
        '=?utf-8?b?4ZuW4Zq0IOGat+GbluGbjyDhm5bhm4/hm4E=?= <foo@example.com>')
    run_full_mailbox_test(u'Falsches Üben von Xylophonmusik <foo@example.com>',
        EmailAddress(u'Falsches Üben von Xylophonmusik', 'foo@example.com'),
        '=?utf-8?q?Falsches_=C3=9Cben_von_Xylophonmusik?= <foo@example.com>')
    run_full_mailbox_test(u'Съешь же ещё этих <foo@example.com>',
        EmailAddress(u'Съешь же ещё этих', 'foo@example.com'),
        '=?utf-8?b?0KHRitC10YjRjCDQttC1INC10YnRkSDRjdGC0LjRhQ==?= <foo@example.com>')
    run_full_mailbox_test(u'ξεσκεπάζω την <foo@example.com>',
        EmailAddress(u'ξεσκεπάζω την', 'foo@example.com'),
        '=?utf-8?b?zr7Otc+DzrrOtc+AzqzOts+JIM+EzrfOvQ==?= <foo@example.com>')

    # unicode + punctuation
    for i in u'''.!#$%&*+-/=?^_`{|}~''':
        run_full_mailbox_test(u'"ö {0}" <foo@example.com>'.format(i),
            EmailAddress(u'ö {0}'.format(i), 'foo@example.com'))


def test_unicode_special_chars():
    # unicode, special chars, no quotes
    run_full_mailbox_test(u'foo © bar <foo@example.com>',
        EmailAddress(u'foo © bar', 'foo@example.com'),
        '=?utf-8?q?foo_=C2=A9_bar?= <foo@example.com>')
    run_full_mailbox_test(u'foo œ bar <foo@example.com>',
        EmailAddress(u'foo œ bar', 'foo@example.com'),
        '=?utf-8?q?foo_=C5=93_bar?= <foo@example.com>')
    run_full_mailbox_test(u'foo – bar <foo@example.com>',
        EmailAddress(u'foo – bar', 'foo@example.com'),
        '=?utf-8?b?Zm9vIOKAkyBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'foo Ǽ bar <foo@example.com>',
        EmailAddress(u'foo Ǽ bar', 'foo@example.com'),
        '=?utf-8?q?foo_=C7=BC_bar?= <foo@example.com>')
    run_full_mailbox_test(u'foo ₤ bar <foo@example.com>',
        EmailAddress(u'foo ₤ bar', 'foo@example.com'),
        '=?utf-8?b?Zm9vIOKCpCBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'foo Ω bar <foo@example.com>',
        EmailAddress(u'foo Ω bar', 'foo@example.com'),
        '=?utf-8?b?Zm9vIOKEpiBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'foo ↵ bar <foo@example.com>',
        EmailAddress(u'foo ↵ bar', 'foo@example.com'),
        '=?utf-8?b?Zm9vIOKGtSBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'foo ∑ bar <foo@example.com>',
        EmailAddress(u'foo ∑ bar', 'foo@example.com'),
        '=?utf-8?b?Zm9vIOKIkSBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'foo ⏲ bar <foo@example.com>',
        EmailAddress(u'foo ⏲ bar', 'foo@example.com'),
        '=?utf-8?b?Zm9vIOKPsiBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'foo Ⓐ bar <foo@example.com>',
        EmailAddress(u'foo Ⓐ bar', 'foo@example.com'),
        '=?utf-8?b?Zm9vIOKStiBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'foo ▒ bar <foo@example.com>',
        EmailAddress(u'foo ▒ bar', 'foo@example.com'),
        '=?utf-8?b?Zm9vIOKWkiBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'foo ▲ bar <foo@example.com>',
        EmailAddress(u'foo ▲ bar', 'foo@example.com'),
        '=?utf-8?b?Zm9vIOKWsiBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'foo ⚔ bar <foo@example.com>',
        EmailAddress(u'foo ⚔ bar', 'foo@example.com'),
        '=?utf-8?b?Zm9vIOKalCBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'foo ✎ bar <foo@example.com>',
        EmailAddress(u'foo ✎ bar', 'foo@example.com'),
        '=?utf-8?b?Zm9vIOKcjiBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'foo ⠂ bar <foo@example.com>',
        EmailAddress(u'foo ⠂ bar', 'foo@example.com'),
        '=?utf-8?b?Zm9vIOKggiBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'foo ⬀ bar <foo@example.com>',
        EmailAddress(u'foo ⬀ bar', 'foo@example.com'),
        '=?utf-8?b?Zm9vIOKsgCBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'foo 💩 bar <foo@example.com>',
        EmailAddress(u'foo 💩 bar', 'foo@example.com'),
        '=?utf-8?b?Zm9vIPCfkqkgYmFy?= <foo@example.com>')

    # unicode, special chars, quotes
    # Note that quotes are removed from the parsed display name
    run_full_mailbox_test(u'"foo © bar" <foo@example.com>',
        EmailAddress(u'foo © bar', u'foo@example.com'),
        '=?utf-8?q?foo_=C2=A9_bar?= <foo@example.com>')
    run_full_mailbox_test(u'"foo œ bar" <foo@example.com>',
        EmailAddress(u'foo œ bar', u'foo@example.com'),
        '=?utf-8?q?foo_=C5=93_bar?= <foo@example.com>')
    run_full_mailbox_test(u'"foo – bar" <foo@example.com>',
        EmailAddress(u'foo – bar', 'foo@example.com'),
        '=?utf-8?b?Zm9vIOKAkyBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'"foo Ǽ bar" <foo@example.com>',
        EmailAddress(u'foo Ǽ bar', u'foo@example.com'),
        '=?utf-8?q?foo_=C7=BC_bar?= <foo@example.com>')
    run_full_mailbox_test(u'"foo Ω bar" <foo@example.com>',
        EmailAddress(u'foo Ω bar', u'foo@example.com'),
        '=?utf-8?b?Zm9vIOKEpiBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'"foo ↵ bar" <foo@example.com>',
        EmailAddress(u'foo ↵ bar', u'foo@example.com'),
        '=?utf-8?b?Zm9vIOKGtSBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'"foo ∑ bar" <foo@example.com>',
        EmailAddress(u'foo ∑ bar', u'foo@example.com'),
        '=?utf-8?b?Zm9vIOKIkSBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'"foo ⏲ bar" <foo@example.com>',
        EmailAddress(u'foo ⏲ bar', u'foo@example.com'),
        '=?utf-8?b?Zm9vIOKPsiBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'"foo Ⓐ bar" <foo@example.com>',
        EmailAddress(u'foo Ⓐ bar', u'foo@example.com'),
        '=?utf-8?b?Zm9vIOKStiBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'"foo ▒ bar" <foo@example.com>',
        EmailAddress(u'foo ▒ bar', u'foo@example.com'),
        '=?utf-8?b?Zm9vIOKWkiBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'"foo ▲ bar" <foo@example.com>',
        EmailAddress(u'foo ▲ bar', u'foo@example.com'),
        '=?utf-8?b?Zm9vIOKWsiBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'"foo ⚔ bar" <foo@example.com>',
        EmailAddress(u'foo ⚔ bar', u'foo@example.com'),
        '=?utf-8?b?Zm9vIOKalCBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'"foo ✎ bar" <foo@example.com>',
        EmailAddress(u'foo ✎ bar', u'foo@example.com'),
        '=?utf-8?b?Zm9vIOKcjiBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'"foo ⠂ bar" <foo@example.com>',
        EmailAddress(u'foo ⠂ bar', u'foo@example.com'),
        '=?utf-8?b?Zm9vIOKggiBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'"foo ⬀ bar" <foo@example.com>',
        EmailAddress(u'foo ⬀ bar', u'foo@example.com'),
        '=?utf-8?b?Zm9vIOKsgCBiYXI=?= <foo@example.com>')
    run_full_mailbox_test(u'"foo 💩 bar" <foo@example.com>',
        EmailAddress(u'foo 💩 bar', u'foo@example.com'),
        '=?utf-8?b?Zm9vIPCfkqkgYmFy?= <foo@example.com>')

    # unicode, language specific punctuation, just test with !
    run_full_mailbox_test(u'fooǃ <foo@example.com>',
        EmailAddress(u'fooǃ', u'foo@example.com'),
        '=?utf-8?b?Zm9vx4M=?= <foo@example.com>')
    run_full_mailbox_test(u'foo‼ <foo@example.com>',
        EmailAddress(u'foo‼', u'foo@example.com'),
        '=?utf-8?b?Zm9v4oC8?= <foo@example.com>')
    run_full_mailbox_test(u'foo⁈ <foo@example.com>',
        EmailAddress(u'foo⁈', u'foo@example.com'),
        '=?utf-8?b?Zm9v4oGI?= <foo@example.com>')
    run_full_mailbox_test(u'foo⁉ <foo@example.com>',
        EmailAddress(u'foo⁉', u'foo@example.com'),
        '=?utf-8?b?Zm9v4oGJ?= <foo@example.com>')
    run_full_mailbox_test(u'foo❕ <foo@example.com>',
        EmailAddress(u'foo❕', u'foo@example.com'),
        '=?utf-8?b?Zm9v4p2V?= <foo@example.com>')
    run_full_mailbox_test(u'foo❗ <foo@example.com>',
        EmailAddress(u'foo❗', u'foo@example.com'),
        '=?utf-8?b?Zm9v4p2X?= <foo@example.com>')
    run_full_mailbox_test(u'foo❢ <foo@example.com>',
        EmailAddress(u'foo❢', u'foo@example.com'),
        '=?utf-8?b?Zm9v4p2i?= <foo@example.com>')
    run_full_mailbox_test(u'foo❣ <foo@example.com>',
        EmailAddress(u'foo❣', u'foo@example.com'),
        '=?utf-8?b?Zm9v4p2j?= <foo@example.com>')
    run_full_mailbox_test(u'fooꜝ <foo@example.com>',
        EmailAddress(u'fooꜝ', u'foo@example.com'),
        '=?utf-8?b?Zm9v6pyd?= <foo@example.com>')
    run_full_mailbox_test(u'fooꜞ <foo@example.com>',
        EmailAddress(u'fooꜞ', u'foo@example.com'),
        '=?utf-8?b?Zm9v6pye?= <foo@example.com>')
    run_full_mailbox_test(u'fooꜟ <foo@example.com>',
        EmailAddress(u'fooꜟ', u'foo@example.com'),
        '=?utf-8?b?Zm9v6pyf?= <foo@example.com>')
    run_full_mailbox_test(u'foo﹗ <foo@example.com>',
        EmailAddress(u'foo﹗', u'foo@example.com'),
        '=?utf-8?b?Zm9v77mX?= <foo@example.com>')
    run_full_mailbox_test(u'foo! <foo@example.com>',
        EmailAddress(u'foo!', u'foo@example.com'),
        '=?utf-8?b?Zm9v77yB?= <foo@example.com>')
    run_full_mailbox_test(u'foo՜ <foo@example.com>',
        EmailAddress(u'foo՜', u'foo@example.com'),
        '=?utf-8?b?Zm9v1Zw=?= <foo@example.com>')
    run_full_mailbox_test(u'foo߹ <foo@example.com>',
        EmailAddress(u'foo߹', u'foo@example.com'),
        '=?utf-8?b?Zm9v37k=?= <foo@example.com>')
    run_full_mailbox_test(u'foo႟ <foo@example.com>',
        EmailAddress(u'foo႟', u'foo@example.com'),
        '=?utf-8?b?Zm9v4YKf?= <foo@example.com>')
    run_full_mailbox_test(u'foo᥄ <foo@example.com>',
        EmailAddress(u'foo᥄', u'foo@example.com'),
        '=?utf-8?b?Zm9v4aWE?= <foo@example.com>')


def test_angle_addr():
    "Grammar: angle-addr -> [ whitespace ] < addr-spec > [ whitespace ]"

    # pass angle-addr
    run_full_mailbox_test('Steve Jobs <steve@apple.com>', EmailAddress('Steve Jobs', 'steve@apple.com'))
    run_full_mailbox_test('Steve Jobs < steve@apple.com>', EmailAddress('Steve Jobs', 'steve@apple.com'))
    run_full_mailbox_test('Steve Jobs <steve@apple.com >', EmailAddress('Steve Jobs', 'steve@apple.com'))
    run_full_mailbox_test('Steve Jobs < steve@apple.com >', EmailAddress('Steve Jobs', 'steve@apple.com'))

    # fail angle-addr
    run_full_mailbox_test('<steve@apple.com', None)
    run_full_mailbox_test('Steve Jobs steve@apple.com>', None)
    run_full_mailbox_test('steve@apple.com>', None)
    run_full_mailbox_test('Steve Jobs <steve@apple.com', None)
    run_full_mailbox_test('Steve Jobs <@steve@apple.com>', None)
    run_full_mailbox_test('<Steve Jobs <steve@apple.com>>', None)
    run_full_mailbox_test('<Steve Jobs <steve@apple.com>', None)
    run_full_mailbox_test('Steve Jobs <steve@apple.com>>', None)
    run_full_mailbox_test('<Steve Jobs> <steve@apple.com>', None)
    run_full_mailbox_test('<Steve Jobs <steve@apple.com>', None)
    run_full_mailbox_test('Steve Jobs> <steve@apple.com>', None)
    run_full_mailbox_test('Steve Jobs <<steve@apple.com>>', None)
    run_full_mailbox_test('Steve Jobs <<steve@apple.com>', None)


def test_addr_spec():
    "Grammar: addr-spec -> [ whitespace ] local-part @ domain [ whitespace ]"

    # pass addr-spec
    run_mailbox_test('linus@kernel.org', 'linus@kernel.org')
    run_mailbox_test(' linus@kernel.org', 'linus@kernel.org')
    run_mailbox_test('linus@kernel.org ', 'linus@kernel.org')
    run_mailbox_test(' linus@kernel.org ', 'linus@kernel.org')
    run_mailbox_test('linus@localhost', 'linus@localhost')

    # fail addr-spec
    run_mailbox_test('linus@', None)
    run_mailbox_test('linus@ ', None)
    run_mailbox_test('linus@;', None)
    run_mailbox_test('linus@@kernel.org', None)
    run_mailbox_test('linus@ @kernel.org', None)
    run_mailbox_test('linus@ @localhost', None)
    run_mailbox_test('linus-at-kernel.org', None)
    run_mailbox_test('linus at kernel.org', None)
    run_mailbox_test('linus kernel.org', None)


def test_local_part():
    "Grammar: local-part -> dot-atom | quoted-string"

    # test length limits
    run_mailbox_test(''.join(['a'*128, '@b']), ''.join(['a'*128, '@b']))
    run_mailbox_test(''.join(['a'*1025, '@b']), None)

    # because qtext and quoted-pair are longer than 64 bytes (limit on local-part)
    # we use a sample in testing, every other for qtext and every fifth for quoted-pair
    sample_qtext = FULL_QTEXT[::2]
    sample_qpair = FULL_QUOTED_PAIR[::5]
    sample_qpair_without_slashes = sample_qpair[1::2]

    # pass dot-atom
    run_mailbox_test('ABCDEFGHIJKLMNOPQRSTUVWXYZ@apple.com', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ@apple.com')
    run_mailbox_test('abcdefghijklmnopqrstuvwzyz@apple.com', 'abcdefghijklmnopqrstuvwzyz@apple.com')
    run_mailbox_test('0123456789@apple.com', '0123456789@apple.com')
    run_mailbox_test('!#$%&\'*+-/=?^_`{|}~@apple.com', '!#$%&\'*+-/=?^_`{|}~@apple.com')
    run_mailbox_test('AZaz09!#$%&\'*+-/=?^_`{|}~@apple.com', 'AZaz09!#$%&\'*+-/=?^_`{|}~@apple.com')
    run_mailbox_test('steve@apple.com', 'steve@apple.com')
    run_mailbox_test(' steve@apple.com', 'steve@apple.com')
    run_mailbox_test('  steve@apple.com', 'steve@apple.com')

    # fail dot-atom
    run_mailbox_test(', steve@apple.com', None)
    run_mailbox_test(';;steve@apple.com', None)
    run_mailbox_test('"steve@apple.com', None)
    run_mailbox_test('steve"@apple.com', None)
    run_mailbox_test('steve..jobs@apple.com', None)

    # pass qtext
    for cnk in chunks(FULL_QTEXT, len(FULL_QTEXT) // 2):
        run_mailbox_test('"{0}"@b'.format(cnk), '"{0}"@b'.format(cnk))
    run_mailbox_test('" {0}"@b'.format(sample_qtext), '" {0}"@b'.format(sample_qtext))
    run_mailbox_test('"{0} "@b'.format(sample_qtext), '"{0} "@b'.format(sample_qtext))
    run_mailbox_test('" {0} "@b'.format(sample_qtext), '" {0} "@b'.format(sample_qtext))
    run_full_mailbox_test('"{0}" <"{0}"@b>'.format(sample_qtext),
        EmailAddress(sample_qtext, '"{0}"@b'.format(sample_qtext)))

    # fail qtext
    run_mailbox_test('"{0}@b'.format(sample_qtext), None)
    run_mailbox_test('{0}"@b'.format(sample_qtext), None)
    run_mailbox_test('{0}@b'.format(sample_qtext), None)
    run_mailbox_test('"{0}@b"'.format(sample_qtext), None)

    # pass quoted-pair
    for cnk in chunks(FULL_QUOTED_PAIR, len(FULL_QUOTED_PAIR) // 3):
        run_mailbox_test('"{0}"@b'.format(cnk), '"{0}"@b'.format(cnk))
    run_mailbox_test('" {0}"@b'.format(sample_qpair), '" {0}"@b'.format(sample_qpair))
    run_mailbox_test('"{0} "@b'.format(sample_qpair), '"{0} "@b'.format(sample_qpair))
    run_mailbox_test('" {0} "@b'.format(sample_qpair), '" {0} "@b'.format(sample_qpair))
    run_full_mailbox_test('"{0}" <"{0}"@b>'.format(sample_qpair),
        EmailAddress(sample_qpair_without_slashes, '"{0}"@b'.format(sample_qpair)))

    # fail quoted-pair
    run_mailbox_test('"{0}@b'.format(sample_qpair), None)
    run_mailbox_test('{0}"@b'.format(sample_qpair), None)
    run_mailbox_test('{0}@b'.format(sample_qpair), None)
    run_mailbox_test('"{0}@b"'.format(sample_qpair), None)


def test_domain():
    "Grammar: domain -> dot-atom"

    # pass dot-atom
    run_mailbox_test('bill@ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'bill@abcdefghijklmnopqrstuvwxyz')
    run_mailbox_test('bill@abcdefghijklmnopqrstuvwxyz', 'bill@abcdefghijklmnopqrstuvwxyz')
    run_mailbox_test('bill@0123456789', 'bill@0123456789')
    run_mailbox_test('bill@!#$%&\'*+-/=?^_`{|}~', 'bill@!#$%&\'*+-/=?^_`{|}~')
    run_mailbox_test('bill@microsoft.com', 'bill@microsoft.com')
    run_mailbox_test('bill@retired.microsoft.com', 'bill@retired.microsoft.com')
    run_mailbox_test('bill@microsoft.com ', 'bill@microsoft.com')
    run_mailbox_test('bill@microsoft.com  ', 'bill@microsoft.com')

    # fail dot-atom
    run_mailbox_test('bill@micro soft.com', None)
    run_mailbox_test('bill@micro. soft.com', None)
    run_mailbox_test('bill@micro .soft.com', None)
    run_mailbox_test('bill@micro. .soft.com', None)
    run_mailbox_test('bill@microsoft.com,', None)
    run_mailbox_test('bill@microsoft.com, ', None)
    run_mailbox_test('bill@microsoft.com, ', None)
    run_mailbox_test('bill@microsoft.com , ', None)
    run_mailbox_test('bill@microsoft.com,,', None)
    run_mailbox_test('bill@microsoft.com.', None)
    run_mailbox_test('bill@microsoft.com..', None)
    run_mailbox_test('bill@microsoft..com', None)
    run_mailbox_test('bill@retired.microsoft..com', None)
    run_mailbox_test('bill@.com', None)
    run_mailbox_test('bill@.com.', None)
    run_mailbox_test('bill@.microsoft.com', None)
    run_mailbox_test('bill@.microsoft.com.', None)
    run_mailbox_test('bill@"microsoft.com"', None)
    run_mailbox_test('bill@"microsoft.com', None)
    run_mailbox_test('bill@microsoft.com"', None)


def test_comments():
    run_full_mailbox_test('(comment) Steve Jobs <steve@apple.com>', EmailAddress('Steve Jobs', 'steve@apple.com'))
    run_full_mailbox_test('Steve Jobs (comment) <steve@apple.com>', EmailAddress('Steve Jobs', 'steve@apple.com'))
    run_full_mailbox_test('Steve Jobs <(comment) steve@apple.com>', EmailAddress('Steve Jobs', 'steve@apple.com'))
    run_full_mailbox_test('Steve Jobs <steve@apple.com (comment)>', EmailAddress('Steve Jobs', 'steve@apple.com'))
    run_full_mailbox_test('Steve Jobs <steve@apple.com> (comment)', EmailAddress('Steve Jobs', 'steve@apple.com'))

    run_full_mailbox_test('(comment)"Steve Jobs"<steve@apple.com>', EmailAddress('Steve Jobs', 'steve@apple.com'))
    run_full_mailbox_test('"Steve Jobs"(comment)<steve@apple.com>', EmailAddress('Steve Jobs', 'steve@apple.com'))
    run_full_mailbox_test('"Steve Jobs"<(comment)steve@apple.com>', EmailAddress('Steve Jobs', 'steve@apple.com'))
    run_full_mailbox_test('"Steve Jobs"<steve@apple.com(comment)>', EmailAddress('Steve Jobs', 'steve@apple.com'))
    run_full_mailbox_test('"Steve Jobs"<steve@apple.com>(comment)', EmailAddress('Steve Jobs', 'steve@apple.com'))


def test_full_spec_symmetry_bug():
    """
    There was a bug that if an address has a display name that is equal or
    longer then 78 characters and consists of at least two words, then
    `full_spec` returned a string where a '\n' character was inserted between
    the display name words.
    """
    # Given
    original = 'very l' + ('o' * 70) + 'ng <foo@example.com>'
    addr = address.parse(original)

    # When
    restored = addr.full_spec()

    # Then
    assert_equal(original, restored)