• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import datetime
2import textwrap
3import unittest
4from email import errors
5from email import policy
6from email.message import Message
7from test.test_email import TestEmailBase, parameterize
8from email import headerregistry
9from email.headerregistry import Address, Group
10from test.support import ALWAYS_EQ
11
12
13DITTO = object()
14
15
16class TestHeaderRegistry(TestEmailBase):
17
18    def test_arbitrary_name_unstructured(self):
19        factory = headerregistry.HeaderRegistry()
20        h = factory('foobar', 'test')
21        self.assertIsInstance(h, headerregistry.BaseHeader)
22        self.assertIsInstance(h, headerregistry.UnstructuredHeader)
23
24    def test_name_case_ignored(self):
25        factory = headerregistry.HeaderRegistry()
26        # Whitebox check that test is valid
27        self.assertNotIn('Subject', factory.registry)
28        h = factory('Subject', 'test')
29        self.assertIsInstance(h, headerregistry.BaseHeader)
30        self.assertIsInstance(h, headerregistry.UniqueUnstructuredHeader)
31
32    class FooBase:
33        def __init__(self, *args, **kw):
34            pass
35
36    def test_override_default_base_class(self):
37        factory = headerregistry.HeaderRegistry(base_class=self.FooBase)
38        h = factory('foobar', 'test')
39        self.assertIsInstance(h, self.FooBase)
40        self.assertIsInstance(h, headerregistry.UnstructuredHeader)
41
42    class FooDefault:
43        parse = headerregistry.UnstructuredHeader.parse
44
45    def test_override_default_class(self):
46        factory = headerregistry.HeaderRegistry(default_class=self.FooDefault)
47        h = factory('foobar', 'test')
48        self.assertIsInstance(h, headerregistry.BaseHeader)
49        self.assertIsInstance(h, self.FooDefault)
50
51    def test_override_default_class_only_overrides_default(self):
52        factory = headerregistry.HeaderRegistry(default_class=self.FooDefault)
53        h = factory('subject', 'test')
54        self.assertIsInstance(h, headerregistry.BaseHeader)
55        self.assertIsInstance(h, headerregistry.UniqueUnstructuredHeader)
56
57    def test_dont_use_default_map(self):
58        factory = headerregistry.HeaderRegistry(use_default_map=False)
59        h = factory('subject', 'test')
60        self.assertIsInstance(h, headerregistry.BaseHeader)
61        self.assertIsInstance(h, headerregistry.UnstructuredHeader)
62
63    def test_map_to_type(self):
64        factory = headerregistry.HeaderRegistry()
65        h1 = factory('foobar', 'test')
66        factory.map_to_type('foobar', headerregistry.UniqueUnstructuredHeader)
67        h2 = factory('foobar', 'test')
68        self.assertIsInstance(h1, headerregistry.BaseHeader)
69        self.assertIsInstance(h1, headerregistry.UnstructuredHeader)
70        self.assertIsInstance(h2, headerregistry.BaseHeader)
71        self.assertIsInstance(h2, headerregistry.UniqueUnstructuredHeader)
72
73
74class TestHeaderBase(TestEmailBase):
75
76    factory = headerregistry.HeaderRegistry()
77
78    def make_header(self, name, value):
79        return self.factory(name, value)
80
81
82class TestBaseHeaderFeatures(TestHeaderBase):
83
84    def test_str(self):
85        h = self.make_header('subject', 'this is a test')
86        self.assertIsInstance(h, str)
87        self.assertEqual(h, 'this is a test')
88        self.assertEqual(str(h), 'this is a test')
89
90    def test_substr(self):
91        h = self.make_header('subject', 'this is a test')
92        self.assertEqual(h[5:7], 'is')
93
94    def test_has_name(self):
95        h = self.make_header('subject', 'this is a test')
96        self.assertEqual(h.name, 'subject')
97
98    def _test_attr_ro(self, attr):
99        h = self.make_header('subject', 'this is a test')
100        with self.assertRaises(AttributeError):
101            setattr(h, attr, 'foo')
102
103    def test_name_read_only(self):
104        self._test_attr_ro('name')
105
106    def test_defects_read_only(self):
107        self._test_attr_ro('defects')
108
109    def test_defects_is_tuple(self):
110        h = self.make_header('subject', 'this is a test')
111        self.assertEqual(len(h.defects), 0)
112        self.assertIsInstance(h.defects, tuple)
113        # Make sure it is still true when there are defects.
114        h = self.make_header('date', '')
115        self.assertEqual(len(h.defects), 1)
116        self.assertIsInstance(h.defects, tuple)
117
118    # XXX: FIXME
119    #def test_CR_in_value(self):
120    #    # XXX: this also re-raises the issue of embedded headers,
121    #    # need test and solution for that.
122    #    value = '\r'.join(['this is', ' a test'])
123    #    h = self.make_header('subject', value)
124    #    self.assertEqual(h, value)
125    #    self.assertDefectsEqual(h.defects, [errors.ObsoleteHeaderDefect])
126
127
128@parameterize
129class TestUnstructuredHeader(TestHeaderBase):
130
131    def string_as_value(self,
132                        source,
133                        decoded,
134                        *args):
135        l = len(args)
136        defects = args[0] if l>0 else []
137        header = 'Subject:' + (' ' if source else '')
138        folded = header + (args[1] if l>1 else source) + '\n'
139        h = self.make_header('Subject', source)
140        self.assertEqual(h, decoded)
141        self.assertDefectsEqual(h.defects, defects)
142        self.assertEqual(h.fold(policy=policy.default), folded)
143
144    string_params = {
145
146        'rfc2047_simple_quopri': (
147            '=?utf-8?q?this_is_a_test?=',
148            'this is a test',
149            [],
150            'this is a test'),
151
152        'rfc2047_gb2312_base64': (
153            '=?gb2312?b?1eLKx9bQzsSy4srUo6E=?=',
154            '\u8fd9\u662f\u4e2d\u6587\u6d4b\u8bd5\uff01',
155            [],
156            '=?utf-8?b?6L+Z5piv5Lit5paH5rWL6K+V77yB?='),
157
158        'rfc2047_simple_nonascii_quopri': (
159            '=?utf-8?q?=C3=89ric?=',
160            'Éric'),
161
162        'rfc2047_quopri_with_regular_text': (
163            'The =?utf-8?q?=C3=89ric=2C?= Himself',
164            'The Éric, Himself'),
165
166    }
167
168
169@parameterize
170class TestDateHeader(TestHeaderBase):
171
172    datestring = 'Sun, 23 Sep 2001 20:10:55 -0700'
173    utcoffset = datetime.timedelta(hours=-7)
174    tz = datetime.timezone(utcoffset)
175    dt = datetime.datetime(2001, 9, 23, 20, 10, 55, tzinfo=tz)
176
177    def test_parse_date(self):
178        h = self.make_header('date', self.datestring)
179        self.assertEqual(h, self.datestring)
180        self.assertEqual(h.datetime, self.dt)
181        self.assertEqual(h.datetime.utcoffset(), self.utcoffset)
182        self.assertEqual(h.defects, ())
183
184    def test_set_from_datetime(self):
185        h = self.make_header('date', self.dt)
186        self.assertEqual(h, self.datestring)
187        self.assertEqual(h.datetime, self.dt)
188        self.assertEqual(h.defects, ())
189
190    def test_date_header_properties(self):
191        h = self.make_header('date', self.datestring)
192        self.assertIsInstance(h, headerregistry.UniqueDateHeader)
193        self.assertEqual(h.max_count, 1)
194        self.assertEqual(h.defects, ())
195
196    def test_resent_date_header_properties(self):
197        h = self.make_header('resent-date', self.datestring)
198        self.assertIsInstance(h, headerregistry.DateHeader)
199        self.assertEqual(h.max_count, None)
200        self.assertEqual(h.defects, ())
201
202    def test_no_value_is_defect(self):
203        h = self.make_header('date', '')
204        self.assertEqual(len(h.defects), 1)
205        self.assertIsInstance(h.defects[0], errors.HeaderMissingRequiredValue)
206
207    def test_invalid_date_format(self):
208        s = 'Not a date header'
209        h = self.make_header('date', s)
210        self.assertEqual(h, s)
211        self.assertIsNone(h.datetime)
212        self.assertEqual(len(h.defects), 1)
213        self.assertIsInstance(h.defects[0], errors.InvalidDateDefect)
214
215    def test_invalid_date_value(self):
216        s = 'Tue, 06 Jun 2017 27:39:33 +0600'
217        h = self.make_header('date', s)
218        self.assertEqual(h, s)
219        self.assertIsNone(h.datetime)
220        self.assertEqual(len(h.defects), 1)
221        self.assertIsInstance(h.defects[0], errors.InvalidDateDefect)
222
223    def test_datetime_read_only(self):
224        h = self.make_header('date', self.datestring)
225        with self.assertRaises(AttributeError):
226            h.datetime = 'foo'
227
228    def test_set_date_header_from_datetime(self):
229        m = Message(policy=policy.default)
230        m['Date'] = self.dt
231        self.assertEqual(m['Date'], self.datestring)
232        self.assertEqual(m['Date'].datetime, self.dt)
233
234
235@parameterize
236class TestContentTypeHeader(TestHeaderBase):
237
238    def content_type_as_value(self,
239                              source,
240                              content_type,
241                              maintype,
242                              subtype,
243                              *args):
244        l = len(args)
245        parmdict = args[0] if l>0 else {}
246        defects =  args[1] if l>1 else []
247        decoded =  args[2] if l>2 and args[2] is not DITTO else source
248        header = 'Content-Type:' + ' ' if source else ''
249        folded = args[3] if l>3 else header + decoded + '\n'
250        h = self.make_header('Content-Type', source)
251        self.assertEqual(h.content_type, content_type)
252        self.assertEqual(h.maintype, maintype)
253        self.assertEqual(h.subtype, subtype)
254        self.assertEqual(h.params, parmdict)
255        with self.assertRaises(TypeError):
256            h.params['abc'] = 'xyz'   # make sure params is read-only.
257        self.assertDefectsEqual(h.defects, defects)
258        self.assertEqual(h, decoded)
259        self.assertEqual(h.fold(policy=policy.default), folded)
260
261    content_type_params = {
262
263        # Examples from RFC 2045.
264
265        'RFC_2045_1': (
266            'text/plain; charset=us-ascii (Plain text)',
267            'text/plain',
268            'text',
269            'plain',
270            {'charset': 'us-ascii'},
271            [],
272            'text/plain; charset="us-ascii"'),
273
274        'RFC_2045_2': (
275            'text/plain; charset=us-ascii',
276            'text/plain',
277            'text',
278            'plain',
279            {'charset': 'us-ascii'},
280            [],
281            'text/plain; charset="us-ascii"'),
282
283        'RFC_2045_3': (
284            'text/plain; charset="us-ascii"',
285            'text/plain',
286            'text',
287            'plain',
288            {'charset': 'us-ascii'}),
289
290        # RFC 2045 5.2 says syntactically invalid values are to be treated as
291        # text/plain.
292
293        'no_subtype_in_content_type': (
294            'text/',
295            'text/plain',
296            'text',
297            'plain',
298            {},
299            [errors.InvalidHeaderDefect]),
300
301        'no_slash_in_content_type': (
302            'foo',
303            'text/plain',
304            'text',
305            'plain',
306            {},
307            [errors.InvalidHeaderDefect]),
308
309        'junk_text_in_content_type': (
310            '<crazy "stuff">',
311            'text/plain',
312            'text',
313            'plain',
314            {},
315            [errors.InvalidHeaderDefect]),
316
317        'too_many_slashes_in_content_type': (
318            'image/jpeg/foo',
319            'text/plain',
320            'text',
321            'plain',
322            {},
323            [errors.InvalidHeaderDefect]),
324
325        # But unknown names are OK.  We could make non-IANA names a defect, but
326        # by not doing so we make ourselves future proof.  The fact that they
327        # are unknown will be detectable by the fact that they don't appear in
328        # the mime_registry...and the application is free to extend that list
329        # to handle them even if the core library doesn't.
330
331        'unknown_content_type': (
332            'bad/names',
333            'bad/names',
334            'bad',
335            'names'),
336
337        # The content type is case insensitive, and CFWS is ignored.
338
339        'mixed_case_content_type': (
340            'ImAge/JPeg',
341            'image/jpeg',
342            'image',
343            'jpeg'),
344
345        'spaces_in_content_type': (
346            '  text  /  plain  ',
347            'text/plain',
348            'text',
349            'plain'),
350
351        'cfws_in_content_type': (
352            '(foo) text (bar)/(baz)plain(stuff)',
353            'text/plain',
354            'text',
355            'plain'),
356
357        # test some parameters (more tests could be added for parameters
358        # associated with other content types, but since parameter parsing is
359        # generic they would be redundant for the current implementation).
360
361        'charset_param': (
362            'text/plain; charset="utf-8"',
363            'text/plain',
364            'text',
365            'plain',
366            {'charset': 'utf-8'}),
367
368        'capitalized_charset': (
369            'text/plain; charset="US-ASCII"',
370            'text/plain',
371            'text',
372            'plain',
373            {'charset': 'US-ASCII'}),
374
375        'unknown_charset': (
376            'text/plain; charset="fOo"',
377            'text/plain',
378            'text',
379            'plain',
380            {'charset': 'fOo'}),
381
382        'capitalized_charset_param_name_and_comment': (
383            'text/plain; (interjection) Charset="utf-8"',
384            'text/plain',
385            'text',
386            'plain',
387            {'charset': 'utf-8'},
388            [],
389            # Should the parameter name be lowercased here?
390            'text/plain; Charset="utf-8"'),
391
392        # Since this is pretty much the ur-mimeheader, we'll put all the tests
393        # that exercise the parameter parsing and formatting here.  Note that
394        # when we refold we may canonicalize, so things like whitespace,
395        # quoting, and rfc2231 encoding may change from what was in the input
396        # header.
397
398        'unquoted_param_value': (
399            'text/plain; title=foo',
400            'text/plain',
401            'text',
402            'plain',
403            {'title': 'foo'},
404            [],
405            'text/plain; title="foo"',
406            ),
407
408        'param_value_with_tspecials': (
409            'text/plain; title="(bar)foo blue"',
410            'text/plain',
411            'text',
412            'plain',
413            {'title': '(bar)foo blue'}),
414
415        'param_with_extra_quoted_whitespace': (
416            'text/plain; title="  a     loong  way \t home   "',
417            'text/plain',
418            'text',
419            'plain',
420            {'title': '  a     loong  way \t home   '}),
421
422        'bad_params': (
423            'blarg; baz; boo',
424            'text/plain',
425            'text',
426            'plain',
427            {'baz': '', 'boo': ''},
428            [errors.InvalidHeaderDefect]*3),
429
430        'spaces_around_param_equals': (
431            'Multipart/mixed; boundary = "CPIMSSMTPC06p5f3tG"',
432            'multipart/mixed',
433            'multipart',
434            'mixed',
435            {'boundary': 'CPIMSSMTPC06p5f3tG'},
436            [],
437            'Multipart/mixed; boundary="CPIMSSMTPC06p5f3tG"',
438            ),
439
440        'spaces_around_semis': (
441            ('image/jpeg; name="wibble.JPG" ; x-mac-type="4A504547" ; '
442                'x-mac-creator="474B4F4E"'),
443            'image/jpeg',
444            'image',
445            'jpeg',
446            {'name': 'wibble.JPG',
447             'x-mac-type': '4A504547',
448             'x-mac-creator': '474B4F4E'},
449            [],
450            ('image/jpeg; name="wibble.JPG"; x-mac-type="4A504547"; '
451                'x-mac-creator="474B4F4E"'),
452            ('Content-Type: image/jpeg; name="wibble.JPG";'
453                ' x-mac-type="4A504547";\n'
454             ' x-mac-creator="474B4F4E"\n'),
455            ),
456
457        'lots_of_mime_params': (
458            ('image/jpeg; name="wibble.JPG"; x-mac-type="4A504547"; '
459                'x-mac-creator="474B4F4E"; x-extrastuff="make it longer"'),
460            'image/jpeg',
461            'image',
462            'jpeg',
463            {'name': 'wibble.JPG',
464             'x-mac-type': '4A504547',
465             'x-mac-creator': '474B4F4E',
466             'x-extrastuff': 'make it longer'},
467            [],
468            ('image/jpeg; name="wibble.JPG"; x-mac-type="4A504547"; '
469                'x-mac-creator="474B4F4E"; x-extrastuff="make it longer"'),
470            # In this case the whole of the MimeParameters does *not* fit
471            # one one line, so we break at a lower syntactic level.
472            ('Content-Type: image/jpeg; name="wibble.JPG";'
473                ' x-mac-type="4A504547";\n'
474             ' x-mac-creator="474B4F4E"; x-extrastuff="make it longer"\n'),
475            ),
476
477        'semis_inside_quotes': (
478            'image/jpeg; name="Jim&amp;&amp;Jill"',
479            'image/jpeg',
480            'image',
481            'jpeg',
482            {'name': 'Jim&amp;&amp;Jill'}),
483
484        'single_quotes_inside_quotes': (
485            'image/jpeg; name="Jim \'Bob\' Jill"',
486            'image/jpeg',
487            'image',
488            'jpeg',
489            {'name': "Jim 'Bob' Jill"}),
490
491        'double_quotes_inside_quotes': (
492            r'image/jpeg; name="Jim \"Bob\" Jill"',
493            'image/jpeg',
494            'image',
495            'jpeg',
496            {'name': 'Jim "Bob" Jill'},
497            [],
498            r'image/jpeg; name="Jim \"Bob\" Jill"'),
499
500        'non_ascii_in_params': (
501            ('foo\xa7/bar; b\xa7r=two; '
502                'baz=thr\xa7e'.encode('latin-1').decode('us-ascii',
503                                                        'surrogateescape')),
504            'foo\uFFFD/bar',
505            'foo\uFFFD',
506            'bar',
507            {'b\uFFFDr': 'two', 'baz': 'thr\uFFFDe'},
508            [errors.UndecodableBytesDefect]*3,
509            'foo�/bar; b�r="two"; baz="thr�e"',
510            # XXX Two bugs here: the mime type is not allowed to be an encoded
511            # word, and we shouldn't be emitting surrogates in the parameter
512            # names.  But I don't know what the behavior should be here, so I'm
513            # punting for now.  In practice this is unlikely to be encountered
514            # since headers with binary in them only come from a binary source
515            # and are almost certain to be re-emitted without refolding.
516            'Content-Type: =?unknown-8bit?q?foo=A7?=/bar; b\udca7r="two";\n'
517            " baz*=unknown-8bit''thr%A7e\n",
518            ),
519
520        # RFC 2231 parameter tests.
521
522        'rfc2231_segmented_normal_values': (
523            'image/jpeg; name*0="abc"; name*1=".html"',
524            'image/jpeg',
525            'image',
526            'jpeg',
527            {'name': "abc.html"},
528            [],
529            'image/jpeg; name="abc.html"'),
530
531        'quotes_inside_rfc2231_value': (
532            r'image/jpeg; bar*0="baz\"foobar"; bar*1="\"baz"',
533            'image/jpeg',
534            'image',
535            'jpeg',
536            {'bar': 'baz"foobar"baz'},
537            [],
538            r'image/jpeg; bar="baz\"foobar\"baz"'),
539
540        'non_ascii_rfc2231_value': (
541            ('text/plain; charset=us-ascii; '
542             "title*=us-ascii'en'This%20is%20"
543             'not%20f\xa7n').encode('latin-1').decode('us-ascii',
544                                                     'surrogateescape'),
545            'text/plain',
546            'text',
547            'plain',
548            {'charset': 'us-ascii', 'title': 'This is not f\uFFFDn'},
549             [errors.UndecodableBytesDefect],
550             'text/plain; charset="us-ascii"; title="This is not f�n"',
551            'Content-Type: text/plain; charset="us-ascii";\n'
552            " title*=unknown-8bit''This%20is%20not%20f%A7n\n",
553            ),
554
555        'rfc2231_encoded_charset': (
556            'text/plain; charset*=ansi-x3.4-1968\'\'us-ascii',
557            'text/plain',
558            'text',
559            'plain',
560            {'charset': 'us-ascii'},
561            [],
562            'text/plain; charset="us-ascii"'),
563
564        # This follows the RFC: no double quotes around encoded values.
565        'rfc2231_encoded_no_double_quotes': (
566            ("text/plain;"
567                "\tname*0*=''This%20is%20;"
568                "\tname*1*=%2A%2A%2Afun%2A%2A%2A%20;"
569                '\tname*2="is it not.pdf"'),
570            'text/plain',
571            'text',
572            'plain',
573            {'name': 'This is ***fun*** is it not.pdf'},
574            [],
575            'text/plain; name="This is ***fun*** is it not.pdf"',
576            ),
577
578        # Make sure we also handle it if there are spurious double quotes.
579        'rfc2231_encoded_with_double_quotes': (
580            ("text/plain;"
581                '\tname*0*="us-ascii\'\'This%20is%20even%20more%20";'
582                '\tname*1*="%2A%2A%2Afun%2A%2A%2A%20";'
583                '\tname*2="is it not.pdf"'),
584            'text/plain',
585            'text',
586            'plain',
587            {'name': 'This is even more ***fun*** is it not.pdf'},
588            [errors.InvalidHeaderDefect]*2,
589            'text/plain; name="This is even more ***fun*** is it not.pdf"',
590            ),
591
592        'rfc2231_single_quote_inside_double_quotes': (
593            ('text/plain; charset=us-ascii;'
594               '\ttitle*0*="us-ascii\'en\'This%20is%20really%20";'
595               '\ttitle*1*="%2A%2A%2Afun%2A%2A%2A%20";'
596               '\ttitle*2="isn\'t it!"'),
597            'text/plain',
598            'text',
599            'plain',
600            {'charset': 'us-ascii', 'title': "This is really ***fun*** isn't it!"},
601            [errors.InvalidHeaderDefect]*2,
602            ('text/plain; charset="us-ascii"; '
603               'title="This is really ***fun*** isn\'t it!"'),
604            ('Content-Type: text/plain; charset="us-ascii";\n'
605                ' title="This is really ***fun*** isn\'t it!"\n'),
606            ),
607
608        'rfc2231_single_quote_in_value_with_charset_and_lang': (
609            ('application/x-foo;'
610                "\tname*0*=\"us-ascii'en-us'Frank's\"; name*1*=\" Document\""),
611            'application/x-foo',
612            'application',
613            'x-foo',
614            {'name': "Frank's Document"},
615            [errors.InvalidHeaderDefect]*2,
616            'application/x-foo; name="Frank\'s Document"',
617            ),
618
619        'rfc2231_single_quote_in_non_encoded_value': (
620            ('application/x-foo;'
621                "\tname*0=\"us-ascii'en-us'Frank's\"; name*1=\" Document\""),
622            'application/x-foo',
623            'application',
624            'x-foo',
625            {'name': "us-ascii'en-us'Frank's Document"},
626            [],
627            'application/x-foo; name="us-ascii\'en-us\'Frank\'s Document"',
628             ),
629
630        'rfc2231_no_language_or_charset': (
631            'text/plain; NAME*0*=english_is_the_default.html',
632            'text/plain',
633            'text',
634            'plain',
635            {'name': 'english_is_the_default.html'},
636            [errors.InvalidHeaderDefect],
637            'text/plain; NAME="english_is_the_default.html"'),
638
639        'rfc2231_encoded_no_charset': (
640            ("text/plain;"
641                '\tname*0*="\'\'This%20is%20even%20more%20";'
642                '\tname*1*="%2A%2A%2Afun%2A%2A%2A%20";'
643                '\tname*2="is it.pdf"'),
644            'text/plain',
645            'text',
646            'plain',
647            {'name': 'This is even more ***fun*** is it.pdf'},
648            [errors.InvalidHeaderDefect]*2,
649            'text/plain; name="This is even more ***fun*** is it.pdf"',
650            ),
651
652        'rfc2231_partly_encoded': (
653            ("text/plain;"
654                '\tname*0*="\'\'This%20is%20even%20more%20";'
655                '\tname*1*="%2A%2A%2Afun%2A%2A%2A%20";'
656                '\tname*2="is it.pdf"'),
657            'text/plain',
658            'text',
659            'plain',
660            {'name': 'This is even more ***fun*** is it.pdf'},
661            [errors.InvalidHeaderDefect]*2,
662            'text/plain; name="This is even more ***fun*** is it.pdf"',
663            ),
664
665        'rfc2231_partly_encoded_2': (
666            ("text/plain;"
667                '\tname*0*="\'\'This%20is%20even%20more%20";'
668                '\tname*1="%2A%2A%2Afun%2A%2A%2A%20";'
669                '\tname*2="is it.pdf"'),
670            'text/plain',
671            'text',
672            'plain',
673            {'name': 'This is even more %2A%2A%2Afun%2A%2A%2A%20is it.pdf'},
674            [errors.InvalidHeaderDefect],
675            ('text/plain;'
676             ' name="This is even more %2A%2A%2Afun%2A%2A%2A%20is it.pdf"'),
677            ('Content-Type: text/plain;\n'
678             ' name="This is even more %2A%2A%2Afun%2A%2A%2A%20is'
679                ' it.pdf"\n'),
680            ),
681
682        'rfc2231_unknown_charset_treated_as_ascii': (
683            "text/plain; name*0*=bogus'xx'ascii_is_the_default",
684            'text/plain',
685            'text',
686            'plain',
687            {'name': 'ascii_is_the_default'},
688            [],
689            'text/plain; name="ascii_is_the_default"'),
690
691        'rfc2231_bad_character_in_charset_parameter_value': (
692            "text/plain; charset*=ascii''utf-8%F1%F2%F3",
693            'text/plain',
694            'text',
695            'plain',
696            {'charset': 'utf-8\uFFFD\uFFFD\uFFFD'},
697            [errors.UndecodableBytesDefect],
698            'text/plain; charset="utf-8\uFFFD\uFFFD\uFFFD"',
699            "Content-Type: text/plain;"
700            " charset*=unknown-8bit''utf-8%F1%F2%F3\n",
701            ),
702
703        'rfc2231_utf8_in_supposedly_ascii_charset_parameter_value': (
704            "text/plain; charset*=ascii''utf-8%E2%80%9D",
705            'text/plain',
706            'text',
707            'plain',
708            {'charset': 'utf-8”'},
709            [errors.UndecodableBytesDefect],
710            'text/plain; charset="utf-8”"',
711            # XXX Should folding change the charset to utf8?  Currently it just
712            # reproduces the original, which is arguably fine.
713            "Content-Type: text/plain;"
714            " charset*=unknown-8bit''utf-8%E2%80%9D\n",
715            ),
716
717        'rfc2231_encoded_then_unencoded_segments': (
718            ('application/x-foo;'
719                '\tname*0*="us-ascii\'en-us\'My";'
720                '\tname*1=" Document";'
721                '\tname*2=" For You"'),
722            'application/x-foo',
723            'application',
724            'x-foo',
725            {'name': 'My Document For You'},
726            [errors.InvalidHeaderDefect],
727            'application/x-foo; name="My Document For You"',
728            ),
729
730        # My reading of the RFC is that this is an invalid header.  The RFC
731        # says that if charset and language information is given, the first
732        # segment *must* be encoded.
733        'rfc2231_unencoded_then_encoded_segments': (
734            ('application/x-foo;'
735                '\tname*0=us-ascii\'en-us\'My;'
736                '\tname*1*=" Document";'
737                '\tname*2*=" For You"'),
738            'application/x-foo',
739            'application',
740            'x-foo',
741            {'name': 'My Document For You'},
742            [errors.InvalidHeaderDefect]*3,
743            'application/x-foo; name="My Document For You"',
744            ),
745
746        # XXX: I would say this one should default to ascii/en for the
747        # "encoded" segment, since the first segment is not encoded and is
748        # in double quotes, making the value a valid non-encoded string.  The
749        # old parser decodes this just like the previous case, which may be the
750        # better Postel rule, but could equally result in borking headers that
751        # intentionally have quoted quotes in them.  We could get this 98%
752        # right if we treat it as a quoted string *unless* it matches the
753        # charset'lang'value pattern exactly *and* there is at least one
754        # encoded segment.  Implementing that algorithm will require some
755        # refactoring, so I haven't done it (yet).
756        'rfc2231_quoted_unencoded_then_encoded_segments': (
757            ('application/x-foo;'
758                '\tname*0="us-ascii\'en-us\'My";'
759                '\tname*1*=" Document";'
760                '\tname*2*=" For You"'),
761            'application/x-foo',
762            'application',
763            'x-foo',
764            {'name': "us-ascii'en-us'My Document For You"},
765            [errors.InvalidHeaderDefect]*2,
766            'application/x-foo; name="us-ascii\'en-us\'My Document For You"',
767            ),
768
769        # Make sure our folding algorithm produces multiple sections correctly.
770        # We could mix encoded and non-encoded segments, but we don't, we just
771        # make them all encoded.  It might be worth fixing that, since the
772        # sections can get used for wrapping ascii text.
773        'rfc2231_folded_segments_correctly_formatted': (
774            ('application/x-foo;'
775                '\tname="' + "with spaces"*8 + '"'),
776            'application/x-foo',
777            'application',
778            'x-foo',
779            {'name': "with spaces"*8},
780            [],
781            'application/x-foo; name="' + "with spaces"*8 + '"',
782            "Content-Type: application/x-foo;\n"
783            " name*0*=us-ascii''with%20spaceswith%20spaceswith%20spaceswith"
784                "%20spaceswith;\n"
785            " name*1*=%20spaceswith%20spaceswith%20spaceswith%20spaces\n"
786            ),
787
788    }
789
790
791@parameterize
792class TestContentTransferEncoding(TestHeaderBase):
793
794    def cte_as_value(self,
795                     source,
796                     cte,
797                     *args):
798        l = len(args)
799        defects =  args[0] if l>0 else []
800        decoded =  args[1] if l>1 and args[1] is not DITTO else source
801        header = 'Content-Transfer-Encoding:' + ' ' if source else ''
802        folded = args[2] if l>2 else header + source + '\n'
803        h = self.make_header('Content-Transfer-Encoding', source)
804        self.assertEqual(h.cte, cte)
805        self.assertDefectsEqual(h.defects, defects)
806        self.assertEqual(h, decoded)
807        self.assertEqual(h.fold(policy=policy.default), folded)
808
809    cte_params = {
810
811        'RFC_2183_1': (
812            'base64',
813            'base64',),
814
815        'no_value': (
816            '',
817            '7bit',
818            [errors.HeaderMissingRequiredValue],
819            '',
820            'Content-Transfer-Encoding:\n',
821            ),
822
823        'junk_after_cte': (
824            '7bit and a bunch more',
825            '7bit',
826            [errors.InvalidHeaderDefect]),
827
828    }
829
830
831@parameterize
832class TestContentDisposition(TestHeaderBase):
833
834    def content_disp_as_value(self,
835                              source,
836                              content_disposition,
837                              *args):
838        l = len(args)
839        parmdict = args[0] if l>0 else {}
840        defects =  args[1] if l>1 else []
841        decoded =  args[2] if l>2 and args[2] is not DITTO else source
842        header = 'Content-Disposition:' + ' ' if source else ''
843        folded = args[3] if l>3 else header + source + '\n'
844        h = self.make_header('Content-Disposition', source)
845        self.assertEqual(h.content_disposition, content_disposition)
846        self.assertEqual(h.params, parmdict)
847        self.assertDefectsEqual(h.defects, defects)
848        self.assertEqual(h, decoded)
849        self.assertEqual(h.fold(policy=policy.default), folded)
850
851    content_disp_params = {
852
853        # Examples from RFC 2183.
854
855        'RFC_2183_1': (
856            'inline',
857            'inline',),
858
859        'RFC_2183_2': (
860            ('attachment; filename=genome.jpeg;'
861             '  modification-date="Wed, 12 Feb 1997 16:29:51 -0500";'),
862            'attachment',
863            {'filename': 'genome.jpeg',
864             'modification-date': 'Wed, 12 Feb 1997 16:29:51 -0500'},
865            [],
866            ('attachment; filename="genome.jpeg"; '
867                 'modification-date="Wed, 12 Feb 1997 16:29:51 -0500"'),
868            ('Content-Disposition: attachment; filename="genome.jpeg";\n'
869             ' modification-date="Wed, 12 Feb 1997 16:29:51 -0500"\n'),
870            ),
871
872        'no_value': (
873            '',
874            None,
875            {},
876            [errors.HeaderMissingRequiredValue],
877            '',
878            'Content-Disposition:\n'),
879
880        'invalid_value': (
881            'ab./k',
882            'ab.',
883            {},
884            [errors.InvalidHeaderDefect]),
885
886        'invalid_value_with_params': (
887            'ab./k; filename="foo"',
888            'ab.',
889            {'filename': 'foo'},
890            [errors.InvalidHeaderDefect]),
891
892        'invalid_parameter_value_with_fws_between_ew': (
893            'attachment; filename="=?UTF-8?Q?Schulbesuchsbest=C3=A4ttigung=2E?='
894            '               =?UTF-8?Q?pdf?="',
895            'attachment',
896            {'filename': 'Schulbesuchsbestättigung.pdf'},
897            [errors.InvalidHeaderDefect]*3,
898            ('attachment; filename="Schulbesuchsbestättigung.pdf"'),
899            ('Content-Disposition: attachment;\n'
900             ' filename*=utf-8\'\'Schulbesuchsbest%C3%A4ttigung.pdf\n'),
901            ),
902
903        'parameter_value_with_fws_between_tokens': (
904            'attachment; filename="File =?utf-8?q?Name?= With Spaces.pdf"',
905            'attachment',
906            {'filename': 'File Name With Spaces.pdf'},
907            [errors.InvalidHeaderDefect],
908            'attachment; filename="File Name With Spaces.pdf"',
909            ('Content-Disposition: attachment; filename="File Name With Spaces.pdf"\n'),
910            )
911    }
912
913
914@parameterize
915class TestMIMEVersionHeader(TestHeaderBase):
916
917    def version_string_as_MIME_Version(self,
918                                       source,
919                                       decoded,
920                                       version,
921                                       major,
922                                       minor,
923                                       defects):
924        h = self.make_header('MIME-Version', source)
925        self.assertEqual(h, decoded)
926        self.assertEqual(h.version, version)
927        self.assertEqual(h.major, major)
928        self.assertEqual(h.minor, minor)
929        self.assertDefectsEqual(h.defects, defects)
930        if source:
931            source = ' ' + source
932        self.assertEqual(h.fold(policy=policy.default),
933                         'MIME-Version:' + source + '\n')
934
935    version_string_params = {
936
937        # Examples from the RFC.
938
939        'RFC_2045_1': (
940            '1.0',
941            '1.0',
942            '1.0',
943            1,
944            0,
945            []),
946
947        'RFC_2045_2': (
948            '1.0 (produced by MetaSend Vx.x)',
949            '1.0 (produced by MetaSend Vx.x)',
950            '1.0',
951            1,
952            0,
953            []),
954
955        'RFC_2045_3': (
956            '(produced by MetaSend Vx.x) 1.0',
957            '(produced by MetaSend Vx.x) 1.0',
958            '1.0',
959            1,
960            0,
961            []),
962
963        'RFC_2045_4': (
964            '1.(produced by MetaSend Vx.x)0',
965            '1.(produced by MetaSend Vx.x)0',
966            '1.0',
967            1,
968            0,
969            []),
970
971        # Other valid values.
972
973        '1_1': (
974            '1.1',
975            '1.1',
976            '1.1',
977            1,
978            1,
979            []),
980
981        '2_1': (
982            '2.1',
983            '2.1',
984            '2.1',
985            2,
986            1,
987            []),
988
989        'whitespace': (
990            '1 .0',
991            '1 .0',
992            '1.0',
993            1,
994            0,
995            []),
996
997        'leading_trailing_whitespace_ignored': (
998            '  1.0  ',
999            '  1.0  ',
1000            '1.0',
1001            1,
1002            0,
1003            []),
1004
1005        # Recoverable invalid values.  We can recover here only because we
1006        # already have a valid value by the time we encounter the garbage.
1007        # Anywhere else, and we don't know where the garbage ends.
1008
1009        'non_comment_garbage_after': (
1010            '1.0 <abc>',
1011            '1.0 <abc>',
1012            '1.0',
1013            1,
1014            0,
1015            [errors.InvalidHeaderDefect]),
1016
1017        # Unrecoverable invalid values.  We *could* apply more heuristics to
1018        # get something out of the first two, but doing so is not worth the
1019        # effort.
1020
1021        'non_comment_garbage_before': (
1022            '<abc> 1.0',
1023            '<abc> 1.0',
1024            None,
1025            None,
1026            None,
1027            [errors.InvalidHeaderDefect]),
1028
1029        'non_comment_garbage_inside': (
1030            '1.<abc>0',
1031            '1.<abc>0',
1032            None,
1033            None,
1034            None,
1035            [errors.InvalidHeaderDefect]),
1036
1037        'two_periods': (
1038            '1..0',
1039            '1..0',
1040            None,
1041            None,
1042            None,
1043            [errors.InvalidHeaderDefect]),
1044
1045        '2_x': (
1046            '2.x',
1047            '2.x',
1048            None,  # This could be 2, but it seems safer to make it None.
1049            None,
1050            None,
1051            [errors.InvalidHeaderDefect]),
1052
1053        'foo': (
1054            'foo',
1055            'foo',
1056            None,
1057            None,
1058            None,
1059            [errors.InvalidHeaderDefect]),
1060
1061        'missing': (
1062            '',
1063            '',
1064            None,
1065            None,
1066            None,
1067            [errors.HeaderMissingRequiredValue]),
1068
1069        }
1070
1071
1072@parameterize
1073class TestAddressHeader(TestHeaderBase):
1074
1075    example_params = {
1076
1077        'empty':
1078            ('<>',
1079             [errors.InvalidHeaderDefect],
1080             '<>',
1081             '',
1082             '<>',
1083             '',
1084             '',
1085             None),
1086
1087        'address_only':
1088            ('zippy@pinhead.com',
1089             [],
1090             'zippy@pinhead.com',
1091             '',
1092             'zippy@pinhead.com',
1093             'zippy',
1094             'pinhead.com',
1095             None),
1096
1097        'name_and_address':
1098            ('Zaphrod Beblebrux <zippy@pinhead.com>',
1099             [],
1100             'Zaphrod Beblebrux <zippy@pinhead.com>',
1101             'Zaphrod Beblebrux',
1102             'zippy@pinhead.com',
1103             'zippy',
1104             'pinhead.com',
1105             None),
1106
1107        'quoted_local_part':
1108            ('Zaphrod Beblebrux <"foo bar"@pinhead.com>',
1109             [],
1110             'Zaphrod Beblebrux <"foo bar"@pinhead.com>',
1111             'Zaphrod Beblebrux',
1112             '"foo bar"@pinhead.com',
1113             'foo bar',
1114             'pinhead.com',
1115             None),
1116
1117        'quoted_parens_in_name':
1118            (r'"A \(Special\) Person" <person@dom.ain>',
1119             [],
1120             '"A (Special) Person" <person@dom.ain>',
1121             'A (Special) Person',
1122             'person@dom.ain',
1123             'person',
1124             'dom.ain',
1125             None),
1126
1127        'quoted_backslashes_in_name':
1128            (r'"Arthur \\Backslash\\ Foobar" <person@dom.ain>',
1129             [],
1130             r'"Arthur \\Backslash\\ Foobar" <person@dom.ain>',
1131             r'Arthur \Backslash\ Foobar',
1132             'person@dom.ain',
1133             'person',
1134             'dom.ain',
1135             None),
1136
1137        'name_with_dot':
1138            ('John X. Doe <jxd@example.com>',
1139             [errors.ObsoleteHeaderDefect],
1140             '"John X. Doe" <jxd@example.com>',
1141             'John X. Doe',
1142             'jxd@example.com',
1143             'jxd',
1144             'example.com',
1145             None),
1146
1147        'quoted_strings_in_local_part':
1148            ('""example" example"@example.com',
1149             [errors.InvalidHeaderDefect]*3,
1150             '"example example"@example.com',
1151             '',
1152             '"example example"@example.com',
1153             'example example',
1154             'example.com',
1155             None),
1156
1157        'escaped_quoted_strings_in_local_part':
1158            (r'"\"example\" example"@example.com',
1159             [],
1160             r'"\"example\" example"@example.com',
1161             '',
1162             r'"\"example\" example"@example.com',
1163             r'"example" example',
1164             'example.com',
1165            None),
1166
1167        'escaped_escapes_in_local_part':
1168            (r'"\\"example\\" example"@example.com',
1169             [errors.InvalidHeaderDefect]*5,
1170             r'"\\example\\\\ example"@example.com',
1171             '',
1172             r'"\\example\\\\ example"@example.com',
1173             r'\example\\ example',
1174             'example.com',
1175            None),
1176
1177        'spaces_in_unquoted_local_part_collapsed':
1178            ('merwok  wok  @example.com',
1179             [errors.InvalidHeaderDefect]*2,
1180             '"merwok wok"@example.com',
1181             '',
1182             '"merwok wok"@example.com',
1183             'merwok wok',
1184             'example.com',
1185             None),
1186
1187        'spaces_around_dots_in_local_part_removed':
1188            ('merwok. wok .  wok@example.com',
1189             [errors.ObsoleteHeaderDefect],
1190             'merwok.wok.wok@example.com',
1191             '',
1192             'merwok.wok.wok@example.com',
1193             'merwok.wok.wok',
1194             'example.com',
1195             None),
1196
1197        'rfc2047_atom_is_decoded':
1198            ('=?utf-8?q?=C3=89ric?= <foo@example.com>',
1199            [],
1200            'Éric <foo@example.com>',
1201            'Éric',
1202            'foo@example.com',
1203            'foo',
1204            'example.com',
1205            None),
1206
1207        'rfc2047_atom_in_phrase_is_decoded':
1208            ('The =?utf-8?q?=C3=89ric=2C?= Himself <foo@example.com>',
1209            [],
1210            '"The Éric, Himself" <foo@example.com>',
1211            'The Éric, Himself',
1212            'foo@example.com',
1213            'foo',
1214            'example.com',
1215            None),
1216
1217        'rfc2047_atom_in_quoted_string_is_decoded':
1218            ('"=?utf-8?q?=C3=89ric?=" <foo@example.com>',
1219            [errors.InvalidHeaderDefect,
1220            errors.InvalidHeaderDefect],
1221            'Éric <foo@example.com>',
1222            'Éric',
1223            'foo@example.com',
1224            'foo',
1225            'example.com',
1226            None),
1227
1228        }
1229
1230        # XXX: Need many more examples, and in particular some with names in
1231        # trailing comments, which aren't currently handled.  comments in
1232        # general are not handled yet.
1233
1234    def example_as_address(self, source, defects, decoded, display_name,
1235                           addr_spec, username, domain, comment):
1236        h = self.make_header('sender', source)
1237        self.assertEqual(h, decoded)
1238        self.assertDefectsEqual(h.defects, defects)
1239        a = h.address
1240        self.assertEqual(str(a), decoded)
1241        self.assertEqual(len(h.groups), 1)
1242        self.assertEqual([a], list(h.groups[0].addresses))
1243        self.assertEqual([a], list(h.addresses))
1244        self.assertEqual(a.display_name, display_name)
1245        self.assertEqual(a.addr_spec, addr_spec)
1246        self.assertEqual(a.username, username)
1247        self.assertEqual(a.domain, domain)
1248        # XXX: we have no comment support yet.
1249        #self.assertEqual(a.comment, comment)
1250
1251    def example_as_group(self, source, defects, decoded, display_name,
1252                         addr_spec, username, domain, comment):
1253        source = 'foo: {};'.format(source)
1254        gdecoded = 'foo: {};'.format(decoded) if decoded else 'foo:;'
1255        h = self.make_header('to', source)
1256        self.assertEqual(h, gdecoded)
1257        self.assertDefectsEqual(h.defects, defects)
1258        self.assertEqual(h.groups[0].addresses, h.addresses)
1259        self.assertEqual(len(h.groups), 1)
1260        self.assertEqual(len(h.addresses), 1)
1261        a = h.addresses[0]
1262        self.assertEqual(str(a), decoded)
1263        self.assertEqual(a.display_name, display_name)
1264        self.assertEqual(a.addr_spec, addr_spec)
1265        self.assertEqual(a.username, username)
1266        self.assertEqual(a.domain, domain)
1267
1268    def test_simple_address_list(self):
1269        value = ('Fred <dinsdale@python.org>, foo@example.com, '
1270                    '"Harry W. Hastings" <hasty@example.com>')
1271        h = self.make_header('to', value)
1272        self.assertEqual(h, value)
1273        self.assertEqual(len(h.groups), 3)
1274        self.assertEqual(len(h.addresses), 3)
1275        for i in range(3):
1276            self.assertEqual(h.groups[i].addresses[0], h.addresses[i])
1277        self.assertEqual(str(h.addresses[0]), 'Fred <dinsdale@python.org>')
1278        self.assertEqual(str(h.addresses[1]), 'foo@example.com')
1279        self.assertEqual(str(h.addresses[2]),
1280            '"Harry W. Hastings" <hasty@example.com>')
1281        self.assertEqual(h.addresses[2].display_name,
1282            'Harry W. Hastings')
1283
1284    def test_complex_address_list(self):
1285        examples = list(self.example_params.values())
1286        source = ('dummy list:;, another: (empty);,' +
1287                 ', '.join([x[0] for x in examples[:4]]) + ', ' +
1288                 r'"A \"list\"": ' +
1289                    ', '.join([x[0] for x in examples[4:6]]) + ';,' +
1290                 ', '.join([x[0] for x in examples[6:]])
1291            )
1292        # XXX: the fact that (empty) disappears here is a potential API design
1293        # bug.  We don't currently have a way to preserve comments.
1294        expected = ('dummy list:;, another:;, ' +
1295                 ', '.join([x[2] for x in examples[:4]]) + ', ' +
1296                 r'"A \"list\"": ' +
1297                    ', '.join([x[2] for x in examples[4:6]]) + ';, ' +
1298                 ', '.join([x[2] for x in examples[6:]])
1299            )
1300
1301        h = self.make_header('to', source)
1302        self.assertEqual(h.split(','), expected.split(','))
1303        self.assertEqual(h, expected)
1304        self.assertEqual(len(h.groups), 7 + len(examples) - 6)
1305        self.assertEqual(h.groups[0].display_name, 'dummy list')
1306        self.assertEqual(h.groups[1].display_name, 'another')
1307        self.assertEqual(h.groups[6].display_name, 'A "list"')
1308        self.assertEqual(len(h.addresses), len(examples))
1309        for i in range(4):
1310            self.assertIsNone(h.groups[i+2].display_name)
1311            self.assertEqual(str(h.groups[i+2].addresses[0]), examples[i][2])
1312        for i in range(7, 7 + len(examples) - 6):
1313            self.assertIsNone(h.groups[i].display_name)
1314            self.assertEqual(str(h.groups[i].addresses[0]), examples[i-1][2])
1315        for i in range(len(examples)):
1316            self.assertEqual(str(h.addresses[i]), examples[i][2])
1317            self.assertEqual(h.addresses[i].addr_spec, examples[i][4])
1318
1319    def test_address_read_only(self):
1320        h = self.make_header('sender', 'abc@xyz.com')
1321        with self.assertRaises(AttributeError):
1322            h.address = 'foo'
1323
1324    def test_addresses_read_only(self):
1325        h = self.make_header('sender', 'abc@xyz.com')
1326        with self.assertRaises(AttributeError):
1327            h.addresses = 'foo'
1328
1329    def test_groups_read_only(self):
1330        h = self.make_header('sender', 'abc@xyz.com')
1331        with self.assertRaises(AttributeError):
1332            h.groups = 'foo'
1333
1334    def test_addresses_types(self):
1335        source = 'me <who@example.com>'
1336        h = self.make_header('to', source)
1337        self.assertIsInstance(h.addresses, tuple)
1338        self.assertIsInstance(h.addresses[0], Address)
1339
1340    def test_groups_types(self):
1341        source = 'me <who@example.com>'
1342        h = self.make_header('to', source)
1343        self.assertIsInstance(h.groups, tuple)
1344        self.assertIsInstance(h.groups[0], Group)
1345
1346    def test_set_from_Address(self):
1347        h = self.make_header('to', Address('me', 'foo', 'example.com'))
1348        self.assertEqual(h, 'me <foo@example.com>')
1349
1350    def test_set_from_Address_list(self):
1351        h = self.make_header('to', [Address('me', 'foo', 'example.com'),
1352                                    Address('you', 'bar', 'example.com')])
1353        self.assertEqual(h, 'me <foo@example.com>, you <bar@example.com>')
1354
1355    def test_set_from_Address_and_Group_list(self):
1356        h = self.make_header('to', [Address('me', 'foo', 'example.com'),
1357                                    Group('bing', [Address('fiz', 'z', 'b.com'),
1358                                                   Address('zif', 'f', 'c.com')]),
1359                                    Address('you', 'bar', 'example.com')])
1360        self.assertEqual(h, 'me <foo@example.com>, bing: fiz <z@b.com>, '
1361                            'zif <f@c.com>;, you <bar@example.com>')
1362        self.assertEqual(h.fold(policy=policy.default.clone(max_line_length=40)),
1363                        'to: me <foo@example.com>,\n'
1364                        ' bing: fiz <z@b.com>, zif <f@c.com>;,\n'
1365                        ' you <bar@example.com>\n')
1366
1367    def test_set_from_Group_list(self):
1368        h = self.make_header('to', [Group('bing', [Address('fiz', 'z', 'b.com'),
1369                                                   Address('zif', 'f', 'c.com')])])
1370        self.assertEqual(h, 'bing: fiz <z@b.com>, zif <f@c.com>;')
1371
1372
1373class TestAddressAndGroup(TestEmailBase):
1374
1375    def _test_attr_ro(self, obj, attr):
1376        with self.assertRaises(AttributeError):
1377            setattr(obj, attr, 'foo')
1378
1379    def test_address_display_name_ro(self):
1380        self._test_attr_ro(Address('foo', 'bar', 'baz'), 'display_name')
1381
1382    def test_address_username_ro(self):
1383        self._test_attr_ro(Address('foo', 'bar', 'baz'), 'username')
1384
1385    def test_address_domain_ro(self):
1386        self._test_attr_ro(Address('foo', 'bar', 'baz'), 'domain')
1387
1388    def test_group_display_name_ro(self):
1389        self._test_attr_ro(Group('foo'), 'display_name')
1390
1391    def test_group_addresses_ro(self):
1392        self._test_attr_ro(Group('foo'), 'addresses')
1393
1394    def test_address_from_username_domain(self):
1395        a = Address('foo', 'bar', 'baz')
1396        self.assertEqual(a.display_name, 'foo')
1397        self.assertEqual(a.username, 'bar')
1398        self.assertEqual(a.domain, 'baz')
1399        self.assertEqual(a.addr_spec, 'bar@baz')
1400        self.assertEqual(str(a), 'foo <bar@baz>')
1401
1402    def test_address_from_addr_spec(self):
1403        a = Address('foo', addr_spec='bar@baz')
1404        self.assertEqual(a.display_name, 'foo')
1405        self.assertEqual(a.username, 'bar')
1406        self.assertEqual(a.domain, 'baz')
1407        self.assertEqual(a.addr_spec, 'bar@baz')
1408        self.assertEqual(str(a), 'foo <bar@baz>')
1409
1410    def test_address_with_no_display_name(self):
1411        a = Address(addr_spec='bar@baz')
1412        self.assertEqual(a.display_name, '')
1413        self.assertEqual(a.username, 'bar')
1414        self.assertEqual(a.domain, 'baz')
1415        self.assertEqual(a.addr_spec, 'bar@baz')
1416        self.assertEqual(str(a), 'bar@baz')
1417
1418    def test_null_address(self):
1419        a = Address()
1420        self.assertEqual(a.display_name, '')
1421        self.assertEqual(a.username, '')
1422        self.assertEqual(a.domain, '')
1423        self.assertEqual(a.addr_spec, '<>')
1424        self.assertEqual(str(a), '<>')
1425
1426    def test_domain_only(self):
1427        # This isn't really a valid address.
1428        a = Address(domain='buzz')
1429        self.assertEqual(a.display_name, '')
1430        self.assertEqual(a.username, '')
1431        self.assertEqual(a.domain, 'buzz')
1432        self.assertEqual(a.addr_spec, '@buzz')
1433        self.assertEqual(str(a), '@buzz')
1434
1435    def test_username_only(self):
1436        # This isn't really a valid address.
1437        a = Address(username='buzz')
1438        self.assertEqual(a.display_name, '')
1439        self.assertEqual(a.username, 'buzz')
1440        self.assertEqual(a.domain, '')
1441        self.assertEqual(a.addr_spec, 'buzz')
1442        self.assertEqual(str(a), 'buzz')
1443
1444    def test_display_name_only(self):
1445        a = Address('buzz')
1446        self.assertEqual(a.display_name, 'buzz')
1447        self.assertEqual(a.username, '')
1448        self.assertEqual(a.domain, '')
1449        self.assertEqual(a.addr_spec, '<>')
1450        self.assertEqual(str(a), 'buzz <>')
1451
1452    def test_quoting(self):
1453        # Ideally we'd check every special individually, but I'm not up for
1454        # writing that many tests.
1455        a = Address('Sara J.', 'bad name', 'example.com')
1456        self.assertEqual(a.display_name, 'Sara J.')
1457        self.assertEqual(a.username, 'bad name')
1458        self.assertEqual(a.domain, 'example.com')
1459        self.assertEqual(a.addr_spec, '"bad name"@example.com')
1460        self.assertEqual(str(a), '"Sara J." <"bad name"@example.com>')
1461
1462    def test_il8n(self):
1463        a = Address('Éric', 'wok', 'exàmple.com')
1464        self.assertEqual(a.display_name, 'Éric')
1465        self.assertEqual(a.username, 'wok')
1466        self.assertEqual(a.domain, 'exàmple.com')
1467        self.assertEqual(a.addr_spec, 'wok@exàmple.com')
1468        self.assertEqual(str(a), 'Éric <wok@exàmple.com>')
1469
1470    # XXX: there is an API design issue that needs to be solved here.
1471    #def test_non_ascii_username_raises(self):
1472    #    with self.assertRaises(ValueError):
1473    #        Address('foo', 'wők', 'example.com')
1474
1475    def test_crlf_in_constructor_args_raises(self):
1476        cases = (
1477            dict(display_name='foo\r'),
1478            dict(display_name='foo\n'),
1479            dict(display_name='foo\r\n'),
1480            dict(domain='example.com\r'),
1481            dict(domain='example.com\n'),
1482            dict(domain='example.com\r\n'),
1483            dict(username='wok\r'),
1484            dict(username='wok\n'),
1485            dict(username='wok\r\n'),
1486            dict(addr_spec='wok@example.com\r'),
1487            dict(addr_spec='wok@example.com\n'),
1488            dict(addr_spec='wok@example.com\r\n')
1489        )
1490        for kwargs in cases:
1491            with self.subTest(kwargs=kwargs), self.assertRaisesRegex(ValueError, "invalid arguments"):
1492                Address(**kwargs)
1493
1494    def test_non_ascii_username_in_addr_spec_raises(self):
1495        with self.assertRaises(ValueError):
1496            Address('foo', addr_spec='wők@example.com')
1497
1498    def test_address_addr_spec_and_username_raises(self):
1499        with self.assertRaises(TypeError):
1500            Address('foo', username='bing', addr_spec='bar@baz')
1501
1502    def test_address_addr_spec_and_domain_raises(self):
1503        with self.assertRaises(TypeError):
1504            Address('foo', domain='bing', addr_spec='bar@baz')
1505
1506    def test_address_addr_spec_and_username_and_domain_raises(self):
1507        with self.assertRaises(TypeError):
1508            Address('foo', username='bong', domain='bing', addr_spec='bar@baz')
1509
1510    def test_space_in_addr_spec_username_raises(self):
1511        with self.assertRaises(ValueError):
1512            Address('foo', addr_spec="bad name@example.com")
1513
1514    def test_bad_addr_sepc_raises(self):
1515        with self.assertRaises(ValueError):
1516            Address('foo', addr_spec="name@ex[]ample.com")
1517
1518    def test_empty_group(self):
1519        g = Group('foo')
1520        self.assertEqual(g.display_name, 'foo')
1521        self.assertEqual(g.addresses, tuple())
1522        self.assertEqual(str(g), 'foo:;')
1523
1524    def test_empty_group_list(self):
1525        g = Group('foo', addresses=[])
1526        self.assertEqual(g.display_name, 'foo')
1527        self.assertEqual(g.addresses, tuple())
1528        self.assertEqual(str(g), 'foo:;')
1529
1530    def test_null_group(self):
1531        g = Group()
1532        self.assertIsNone(g.display_name)
1533        self.assertEqual(g.addresses, tuple())
1534        self.assertEqual(str(g), 'None:;')
1535
1536    def test_group_with_addresses(self):
1537        addrs = [Address('b', 'b', 'c'), Address('a', 'b','c')]
1538        g = Group('foo', addrs)
1539        self.assertEqual(g.display_name, 'foo')
1540        self.assertEqual(g.addresses, tuple(addrs))
1541        self.assertEqual(str(g), 'foo: b <b@c>, a <b@c>;')
1542
1543    def test_group_with_addresses_no_display_name(self):
1544        addrs = [Address('b', 'b', 'c'), Address('a', 'b','c')]
1545        g = Group(addresses=addrs)
1546        self.assertIsNone(g.display_name)
1547        self.assertEqual(g.addresses, tuple(addrs))
1548        self.assertEqual(str(g), 'None: b <b@c>, a <b@c>;')
1549
1550    def test_group_with_one_address_no_display_name(self):
1551        addrs = [Address('b', 'b', 'c')]
1552        g = Group(addresses=addrs)
1553        self.assertIsNone(g.display_name)
1554        self.assertEqual(g.addresses, tuple(addrs))
1555        self.assertEqual(str(g), 'b <b@c>')
1556
1557    def test_display_name_quoting(self):
1558        g = Group('foo.bar')
1559        self.assertEqual(g.display_name, 'foo.bar')
1560        self.assertEqual(g.addresses, tuple())
1561        self.assertEqual(str(g), '"foo.bar":;')
1562
1563    def test_display_name_blanks_not_quoted(self):
1564        g = Group('foo bar')
1565        self.assertEqual(g.display_name, 'foo bar')
1566        self.assertEqual(g.addresses, tuple())
1567        self.assertEqual(str(g), 'foo bar:;')
1568
1569    def test_set_message_header_from_address(self):
1570        a = Address('foo', 'bar', 'example.com')
1571        m = Message(policy=policy.default)
1572        m['To'] = a
1573        self.assertEqual(m['to'], 'foo <bar@example.com>')
1574        self.assertEqual(m['to'].addresses, (a,))
1575
1576    def test_set_message_header_from_group(self):
1577        g = Group('foo bar')
1578        m = Message(policy=policy.default)
1579        m['To'] = g
1580        self.assertEqual(m['to'], 'foo bar:;')
1581        self.assertEqual(m['to'].addresses, g.addresses)
1582
1583    def test_address_comparison(self):
1584        a = Address('foo', 'bar', 'example.com')
1585        self.assertEqual(Address('foo', 'bar', 'example.com'), a)
1586        self.assertNotEqual(Address('baz', 'bar', 'example.com'), a)
1587        self.assertNotEqual(Address('foo', 'baz', 'example.com'), a)
1588        self.assertNotEqual(Address('foo', 'bar', 'baz'), a)
1589        self.assertFalse(a == object())
1590        self.assertTrue(a == ALWAYS_EQ)
1591
1592    def test_group_comparison(self):
1593        a = Address('foo', 'bar', 'example.com')
1594        g = Group('foo bar', [a])
1595        self.assertEqual(Group('foo bar', (a,)), g)
1596        self.assertNotEqual(Group('baz', [a]), g)
1597        self.assertNotEqual(Group('foo bar', []), g)
1598        self.assertFalse(g == object())
1599        self.assertTrue(g == ALWAYS_EQ)
1600
1601
1602class TestFolding(TestHeaderBase):
1603
1604    def test_address_display_names(self):
1605        """Test the folding and encoding of address headers."""
1606        for name, result in (
1607                ('Foo Bar, France', '"Foo Bar, France"'),
1608                ('Foo Bar (France)', '"Foo Bar (France)"'),
1609                ('Foo Bar, España', 'Foo =?utf-8?q?Bar=2C_Espa=C3=B1a?='),
1610                ('Foo Bar (España)', 'Foo Bar =?utf-8?b?KEVzcGHDsWEp?='),
1611                ('Foo, Bar España', '=?utf-8?q?Foo=2C_Bar_Espa=C3=B1a?='),
1612                ('Foo, Bar [España]', '=?utf-8?q?Foo=2C_Bar_=5BEspa=C3=B1a=5D?='),
1613                ('Foo Bär, France', 'Foo =?utf-8?q?B=C3=A4r=2C?= France'),
1614                ('Foo Bär <France>', 'Foo =?utf-8?q?B=C3=A4r_=3CFrance=3E?='),
1615                (
1616                    'Lôrem ipsum dôlôr sit amet, cônsectetuer adipiscing. '
1617                    'Suspendisse pôtenti. Aliquam nibh. Suspendisse pôtenti.',
1618                    '=?utf-8?q?L=C3=B4rem_ipsum_d=C3=B4l=C3=B4r_sit_amet=2C_c'
1619                    '=C3=B4nsectetuer?=\n =?utf-8?q?adipiscing=2E_Suspendisse'
1620                    '_p=C3=B4tenti=2E_Aliquam_nibh=2E?=\n Suspendisse =?utf-8'
1621                    '?q?p=C3=B4tenti=2E?=',
1622                    ),
1623                ):
1624            h = self.make_header('To', Address(name, addr_spec='a@b.com'))
1625            self.assertEqual(h.fold(policy=policy.default),
1626                                    'To: %s <a@b.com>\n' % result)
1627
1628    def test_short_unstructured(self):
1629        h = self.make_header('subject', 'this is a test')
1630        self.assertEqual(h.fold(policy=policy.default),
1631                         'subject: this is a test\n')
1632
1633    def test_long_unstructured(self):
1634        h = self.make_header('Subject', 'This is a long header '
1635            'line that will need to be folded into two lines '
1636            'and will demonstrate basic folding')
1637        self.assertEqual(h.fold(policy=policy.default),
1638                        'Subject: This is a long header line that will '
1639                            'need to be folded into two lines\n'
1640                        ' and will demonstrate basic folding\n')
1641
1642    def test_unstructured_short_max_line_length(self):
1643        h = self.make_header('Subject', 'this is a short header '
1644            'that will be folded anyway')
1645        self.assertEqual(
1646            h.fold(policy=policy.default.clone(max_line_length=20)),
1647            textwrap.dedent("""\
1648                Subject: this is a
1649                 short header that
1650                 will be folded
1651                 anyway
1652                """))
1653
1654    def test_fold_unstructured_single_word(self):
1655        h = self.make_header('Subject', 'test')
1656        self.assertEqual(h.fold(policy=policy.default), 'Subject: test\n')
1657
1658    def test_fold_unstructured_short(self):
1659        h = self.make_header('Subject', 'test test test')
1660        self.assertEqual(h.fold(policy=policy.default),
1661                        'Subject: test test test\n')
1662
1663    def test_fold_unstructured_with_overlong_word(self):
1664        h = self.make_header('Subject', 'thisisaverylonglineconsistingofa'
1665            'singlewordthatwontfit')
1666        self.assertEqual(
1667            h.fold(policy=policy.default.clone(max_line_length=20)),
1668            'Subject: \n'
1669            ' =?utf-8?q?thisisa?=\n'
1670            ' =?utf-8?q?verylon?=\n'
1671            ' =?utf-8?q?glineco?=\n'
1672            ' =?utf-8?q?nsistin?=\n'
1673            ' =?utf-8?q?gofasin?=\n'
1674            ' =?utf-8?q?gleword?=\n'
1675            ' =?utf-8?q?thatwon?=\n'
1676            ' =?utf-8?q?tfit?=\n'
1677            )
1678
1679    def test_fold_unstructured_with_two_overlong_words(self):
1680        h = self.make_header('Subject', 'thisisaverylonglineconsistingofa'
1681            'singlewordthatwontfit plusanotherverylongwordthatwontfit')
1682        self.assertEqual(
1683            h.fold(policy=policy.default.clone(max_line_length=20)),
1684            'Subject: \n'
1685            ' =?utf-8?q?thisisa?=\n'
1686            ' =?utf-8?q?verylon?=\n'
1687            ' =?utf-8?q?glineco?=\n'
1688            ' =?utf-8?q?nsistin?=\n'
1689            ' =?utf-8?q?gofasin?=\n'
1690            ' =?utf-8?q?gleword?=\n'
1691            ' =?utf-8?q?thatwon?=\n'
1692            ' =?utf-8?q?tfit_pl?=\n'
1693            ' =?utf-8?q?usanoth?=\n'
1694            ' =?utf-8?q?erveryl?=\n'
1695            ' =?utf-8?q?ongword?=\n'
1696            ' =?utf-8?q?thatwon?=\n'
1697            ' =?utf-8?q?tfit?=\n'
1698            )
1699
1700    # XXX Need test for when max_line_length is less than the chrome size.
1701
1702    def test_fold_unstructured_with_slightly_long_word(self):
1703        h = self.make_header('Subject', 'thislongwordislessthanmaxlinelen')
1704        self.assertEqual(
1705            h.fold(policy=policy.default.clone(max_line_length=35)),
1706            'Subject:\n thislongwordislessthanmaxlinelen\n')
1707
1708    def test_fold_unstructured_with_commas(self):
1709        # The old wrapper would fold this at the commas.
1710        h = self.make_header('Subject', "This header is intended to "
1711            "demonstrate, in a fairly succinct way, that we now do "
1712            "not give a , special treatment in unstructured headers.")
1713        self.assertEqual(
1714            h.fold(policy=policy.default.clone(max_line_length=60)),
1715            textwrap.dedent("""\
1716                Subject: This header is intended to demonstrate, in a fairly
1717                 succinct way, that we now do not give a , special treatment
1718                 in unstructured headers.
1719                 """))
1720
1721    def test_fold_address_list(self):
1722        h = self.make_header('To', '"Theodore H. Perfect" <yes@man.com>, '
1723            '"My address is very long because my name is long" <foo@bar.com>, '
1724            '"Only A. Friend" <no@yes.com>')
1725        self.assertEqual(h.fold(policy=policy.default), textwrap.dedent("""\
1726            To: "Theodore H. Perfect" <yes@man.com>,
1727             "My address is very long because my name is long" <foo@bar.com>,
1728             "Only A. Friend" <no@yes.com>
1729             """))
1730
1731    def test_fold_date_header(self):
1732        h = self.make_header('Date', 'Sat, 2 Feb 2002 17:00:06 -0800')
1733        self.assertEqual(h.fold(policy=policy.default),
1734                        'Date: Sat, 02 Feb 2002 17:00:06 -0800\n')
1735
1736    def test_fold_overlong_words_using_RFC2047(self):
1737        h = self.make_header(
1738            'X-Report-Abuse',
1739            '<https://www.mailitapp.com/report_abuse.php?'
1740              'mid=xxx-xxx-xxxxxxxxxxxxxxxxxxxxxxxx==-xxx-xx-xx>')
1741        self.assertEqual(
1742            h.fold(policy=policy.default),
1743            'X-Report-Abuse: =?utf-8?q?=3Chttps=3A//www=2Emailitapp=2E'
1744                'com/report=5Fabuse?=\n'
1745            ' =?utf-8?q?=2Ephp=3Fmid=3Dxxx-xxx-xxxx'
1746                'xxxxxxxxxxxxxxxxxxxx=3D=3D-xxx-xx-xx?=\n'
1747            ' =?utf-8?q?=3E?=\n')
1748
1749    def test_message_id_header_is_not_folded(self):
1750        h = self.make_header(
1751            'Message-ID',
1752            '<somemessageidlongerthan@maxlinelength.com>')
1753        self.assertEqual(
1754            h.fold(policy=policy.default.clone(max_line_length=20)),
1755            'Message-ID: <somemessageidlongerthan@maxlinelength.com>\n')
1756
1757        # Test message-id isn't folded when id-right is no-fold-literal.
1758        h = self.make_header(
1759            'Message-ID',
1760            '<somemessageidlongerthan@[127.0.0.0.0.0.0.0.0.1]>')
1761        self.assertEqual(
1762            h.fold(policy=policy.default.clone(max_line_length=20)),
1763            'Message-ID: <somemessageidlongerthan@[127.0.0.0.0.0.0.0.0.1]>\n')
1764
1765        # Test message-id isn't folded when id-right is non-ascii characters.
1766        h = self.make_header('Message-ID', '<ईमेल@wők.com>')
1767        self.assertEqual(
1768            h.fold(policy=policy.default.clone(max_line_length=30)),
1769            'Message-ID: <ईमेल@wők.com>\n')
1770
1771        # Test message-id is folded without breaking the msg-id token into
1772        # encoded words, *even* if they don't fit into max_line_length.
1773        h = self.make_header('Message-ID', '<ईमेलfromMessage@wők.com>')
1774        self.assertEqual(
1775            h.fold(policy=policy.default.clone(max_line_length=20)),
1776            'Message-ID:\n <ईमेलfromMessage@wők.com>\n')
1777
1778if __name__ == '__main__':
1779    unittest.main()
1780