1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
18 var parseTests = []struct {
24 // RFC 5322, Appendix A.1.1
25 in: `From: John Doe <jdoe@machine.example>
26 To: Mary Smith <mary@example.net>
28 Date: Fri, 21 Nov 1997 09:55:06 -0600
29 Message-ID: <1234@local.machine.example>
31 This is a message just to say hello.
35 "From": []string{"John Doe <jdoe@machine.example>"},
36 "To": []string{"Mary Smith <mary@example.net>"},
37 "Subject": []string{"Saying Hello"},
38 "Date": []string{"Fri, 21 Nov 1997 09:55:06 -0600"},
39 "Message-Id": []string{"<1234@local.machine.example>"},
41 body: "This is a message just to say hello.\nSo, \"Hello\".\n",
45 func TestParsing(t *testing.T) {
46 for i, test := range parseTests {
47 msg, err := ReadMessage(bytes.NewBuffer([]byte(test.in)))
49 t.Errorf("test #%d: Failed parsing message: %v", i, err)
52 if !headerEq(msg.Header, test.header) {
53 t.Errorf("test #%d: Incorrectly parsed message header.\nGot:\n%+v\nWant:\n%+v",
54 i, msg.Header, test.header)
56 body, err := ioutil.ReadAll(msg.Body)
58 t.Errorf("test #%d: Failed reading body: %v", i, err)
61 bodyStr := string(body)
62 if bodyStr != test.body {
63 t.Errorf("test #%d: Incorrectly parsed message body.\nGot:\n%+v\nWant:\n%+v",
64 i, bodyStr, test.body)
69 func headerEq(a, b Header) bool {
73 for k, as := range a {
78 if !reflect.DeepEqual(as, bs) {
85 func TestDateParsing(t *testing.T) {
90 // RFC 5322, Appendix A.1.1
92 "Fri, 21 Nov 1997 09:55:06 -0600",
93 time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)),
95 // RFC 5322, Appendix A.6.2
98 "21 Nov 97 09:55:06 GMT",
99 time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("GMT", 0)),
101 // Commonly found format not specified by RFC 5322.
103 "Fri, 21 Nov 1997 09:55:06 -0600 (MDT)",
104 time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)),
107 for _, test := range tests {
109 "Date": []string{test.dateStr},
111 date, err := hdr.Date()
113 t.Errorf("Header(Date: %s).Date(): %v", test.dateStr, err)
114 } else if !date.Equal(test.exp) {
115 t.Errorf("Header(Date: %s).Date() = %+v, want %+v", test.dateStr, date, test.exp)
118 date, err = ParseDate(test.dateStr)
120 t.Errorf("ParseDate(%s): %v", test.dateStr, err)
121 } else if !date.Equal(test.exp) {
122 t.Errorf("ParseDate(%s) = %+v, want %+v", test.dateStr, date, test.exp)
127 func TestAddressParsingError(t *testing.T) {
128 mustErrTestCases := [...]struct {
132 0: {"=?iso-8859-2?Q?Bogl=E1rka_Tak=E1cs?= <unknown@gmail.com>", "charset not supported"},
133 1: {"a@gmail.com b@gmail.com", "expected single address"},
134 2: {string([]byte{0xed, 0xa0, 0x80}) + " <micro@example.net>", "invalid utf-8 in address"},
135 3: {"\"" + string([]byte{0xed, 0xa0, 0x80}) + "\" <half-surrogate@example.com>", "invalid utf-8 in quoted-string"},
136 4: {"\"\\" + string([]byte{0x80}) + "\" <escaped-invalid-unicode@example.net>", "invalid utf-8 in quoted-string"},
137 5: {"\"\x00\" <null@example.net>", "bad character in quoted-string"},
138 6: {"\"\\\x00\" <escaped-null@example.net>", "bad character in quoted-string"},
139 7: {"John Doe", "no angle-addr"},
140 8: {`<jdoe#machine.example>`, "missing @ in addr-spec"},
141 9: {`John <middle> Doe <jdoe@machine.example>`, "missing @ in addr-spec"},
142 10: {"cfws@example.com (", "misformatted parenthetical comment"},
143 11: {"empty group: ;", "empty group"},
144 12: {"root group: embed group: null@example.com;", "no angle-addr"},
145 13: {"group not closed: null@example.com", "expected comma"},
146 14: {"group: first@example.com, second@example.com;", "group with multiple addresses"},
149 for i, tc := range mustErrTestCases {
150 _, err := ParseAddress(tc.text)
151 if err == nil || !strings.Contains(err.Error(), tc.wantErrText) {
152 t.Errorf(`mail.ParseAddress(%q) #%d want %q, got %v`, tc.text, i, tc.wantErrText, err)
157 func TestAddressParsing(t *testing.T) {
164 `jdoe@machine.example`,
166 Address: "jdoe@machine.example",
169 // RFC 5322, Appendix A.1.1
171 `John Doe <jdoe@machine.example>`,
174 Address: "jdoe@machine.example",
177 // RFC 5322, Appendix A.1.2
179 `"Joe Q. Public" <john.q.public@example.com>`,
181 Name: "Joe Q. Public",
182 Address: "john.q.public@example.com",
186 `"John (middle) Doe" <jdoe@machine.example>`,
188 Name: "John (middle) Doe",
189 Address: "jdoe@machine.example",
193 `John (middle) Doe <jdoe@machine.example>`,
195 Name: "John (middle) Doe",
196 Address: "jdoe@machine.example",
200 `John !@M@! Doe <jdoe@machine.example>`,
202 Name: "John !@M@! Doe",
203 Address: "jdoe@machine.example",
207 `"John <middle> Doe" <jdoe@machine.example>`,
209 Name: "John <middle> Doe",
210 Address: "jdoe@machine.example",
214 `Mary Smith <mary@x.test>, jdoe@example.org, Who? <one@y.test>`,
218 Address: "mary@x.test",
221 Address: "jdoe@example.org",
225 Address: "one@y.test",
230 `<boss@nil.test>, "Giant; \"Big\" Box" <sysservices@example.net>`,
233 Address: "boss@nil.test",
236 Name: `Giant; "Big" Box`,
237 Address: "sysservices@example.net",
241 // RFC 5322, Appendix A.6.1
243 `Joe Q. Public <john.q.public@example.com>`,
245 Name: "Joe Q. Public",
246 Address: "john.q.public@example.com",
249 // RFC 5322, Appendix A.1.3
251 `group1: groupaddr1@example.com;`,
255 Address: "groupaddr1@example.com",
264 `A Group:Ed Jones <c@a.test>,joe@where.test,John <jdoe@one.test>;`,
272 Address: "joe@where.test",
276 Address: "jdoe@one.test",
281 `Group1: <addr1@example.com>;, Group 2: addr2@example.com;, John <addr3@example.com>`,
285 Address: "addr1@example.com",
289 Address: "addr2@example.com",
293 Address: "addr3@example.com",
297 // RFC 2047 "Q"-encoded ISO-8859-1 address.
299 `=?iso-8859-1?q?J=F6rg_Doe?= <joerg@example.com>`,
303 Address: "joerg@example.com",
307 // RFC 2047 "Q"-encoded US-ASCII address. Dumb but legal.
309 `=?us-ascii?q?J=6Frg_Doe?= <joerg@example.com>`,
313 Address: "joerg@example.com",
317 // RFC 2047 "Q"-encoded UTF-8 address.
319 `=?utf-8?q?J=C3=B6rg_Doe?= <joerg@example.com>`,
323 Address: "joerg@example.com",
327 // RFC 2047 "Q"-encoded UTF-8 address with multiple encoded-words.
329 `=?utf-8?q?J=C3=B6rg?= =?utf-8?q?Doe?= <joerg@example.com>`,
333 Address: "joerg@example.com",
337 // RFC 2047, Section 8.
339 `=?ISO-8859-1?Q?Andr=E9?= Pirard <PIRARD@vm1.ulg.ac.be>`,
342 Name: `André Pirard`,
343 Address: "PIRARD@vm1.ulg.ac.be",
347 // Custom example of RFC 2047 "B"-encoded ISO-8859-1 address.
349 `=?ISO-8859-1?B?SvZyZw==?= <joerg@example.com>`,
353 Address: "joerg@example.com",
357 // Custom example of RFC 2047 "B"-encoded UTF-8 address.
359 `=?UTF-8?B?SsO2cmc=?= <joerg@example.com>`,
363 Address: "joerg@example.com",
367 // Custom example with "." in name. For issue 4938
369 `Asem H. <noreply@example.com>`,
373 Address: "noreply@example.com",
377 // RFC 6532 3.2.3, qtext /= UTF8-non-ascii
379 `"Gø Pher" <gopher@example.com>`,
383 Address: "gopher@example.com",
387 // RFC 6532 3.2, atext /= UTF8-non-ascii
389 `µ <micro@example.com>`,
393 Address: "micro@example.com",
397 // RFC 6532 3.2.2, local address parts allow UTF-8
399 `Micro <µ@example.com>`,
403 Address: "µ@example.com",
407 // RFC 6532 3.2.4, domains parts allow UTF-8
409 `Micro <micro@µ.example.com>`,
413 Address: "micro@µ.example.com",
419 `"" <emptystring@example.com>`,
423 Address: "emptystring@example.com",
429 `<cfws@example.com> (CFWS (cfws)) (another comment)`,
433 Address: "cfws@example.com",
438 `<cfws@example.com> () (another comment), <cfws2@example.com> (another)`,
442 Address: "cfws@example.com",
446 Address: "cfws2@example.com",
450 // Comment as display name
452 `john@example.com (John Doe)`,
456 Address: "john@example.com",
460 // Comment and display name
462 `John Doe <john@example.com> (Joey)`,
466 Address: "john@example.com",
470 // Comment as display name, no space
472 `john@example.com(John Doe)`,
476 Address: "john@example.com",
480 // Comment as display name, Q-encoded
482 `asjo@example.com (Adam =?utf-8?Q?Sj=C3=B8gren?=)`,
485 Name: "Adam Sjøgren",
486 Address: "asjo@example.com",
490 // Comment as display name, Q-encoded and tab-separated
492 `asjo@example.com (Adam =?utf-8?Q?Sj=C3=B8gren?=)`,
495 Name: "Adam Sjøgren",
496 Address: "asjo@example.com",
500 // Nested comment as display name, Q-encoded
502 `asjo@example.com (Adam =?utf-8?Q?Sj=C3=B8gren?= (Debian))`,
505 Name: "Adam Sjøgren (Debian)",
506 Address: "asjo@example.com",
511 for _, test := range tests {
512 if len(test.exp) == 1 {
513 addr, err := ParseAddress(test.addrsStr)
515 t.Errorf("Failed parsing (single) %q: %v", test.addrsStr, err)
518 if !reflect.DeepEqual([]*Address{addr}, test.exp) {
519 t.Errorf("Parse (single) of %q: got %+v, want %+v", test.addrsStr, addr, test.exp)
523 addrs, err := ParseAddressList(test.addrsStr)
525 t.Errorf("Failed parsing (list) %q: %v", test.addrsStr, err)
528 if !reflect.DeepEqual(addrs, test.exp) {
529 t.Errorf("Parse (list) of %q: got %+v, want %+v", test.addrsStr, addrs, test.exp)
534 func TestAddressParser(t *testing.T) {
541 `jdoe@machine.example`,
543 Address: "jdoe@machine.example",
546 // RFC 5322, Appendix A.1.1
548 `John Doe <jdoe@machine.example>`,
551 Address: "jdoe@machine.example",
554 // RFC 5322, Appendix A.1.2
556 `"Joe Q. Public" <john.q.public@example.com>`,
558 Name: "Joe Q. Public",
559 Address: "john.q.public@example.com",
563 `Mary Smith <mary@x.test>, jdoe@example.org, Who? <one@y.test>`,
567 Address: "mary@x.test",
570 Address: "jdoe@example.org",
574 Address: "one@y.test",
579 `<boss@nil.test>, "Giant; \"Big\" Box" <sysservices@example.net>`,
582 Address: "boss@nil.test",
585 Name: `Giant; "Big" Box`,
586 Address: "sysservices@example.net",
590 // RFC 2047 "Q"-encoded ISO-8859-1 address.
592 `=?iso-8859-1?q?J=F6rg_Doe?= <joerg@example.com>`,
596 Address: "joerg@example.com",
600 // RFC 2047 "Q"-encoded US-ASCII address. Dumb but legal.
602 `=?us-ascii?q?J=6Frg_Doe?= <joerg@example.com>`,
606 Address: "joerg@example.com",
610 // RFC 2047 "Q"-encoded ISO-8859-15 address.
612 `=?ISO-8859-15?Q?J=F6rg_Doe?= <joerg@example.com>`,
616 Address: "joerg@example.com",
620 // RFC 2047 "B"-encoded windows-1252 address.
622 `=?windows-1252?q?Andr=E9?= Pirard <PIRARD@vm1.ulg.ac.be>`,
625 Name: `André Pirard`,
626 Address: "PIRARD@vm1.ulg.ac.be",
630 // Custom example of RFC 2047 "B"-encoded ISO-8859-15 address.
632 `=?ISO-8859-15?B?SvZyZw==?= <joerg@example.com>`,
636 Address: "joerg@example.com",
640 // Custom example of RFC 2047 "B"-encoded UTF-8 address.
642 `=?UTF-8?B?SsO2cmc=?= <joerg@example.com>`,
646 Address: "joerg@example.com",
650 // Custom example with "." in name. For issue 4938
652 `Asem H. <noreply@example.com>`,
656 Address: "noreply@example.com",
662 ap := AddressParser{WordDecoder: &mime.WordDecoder{
663 CharsetReader: func(charset string, input io.Reader) (io.Reader, error) {
664 in, err := ioutil.ReadAll(input)
671 in = bytes.ReplaceAll(in, []byte("\xf6"), []byte("ö"))
673 in = bytes.ReplaceAll(in, []byte("\xe9"), []byte("é"))
676 return bytes.NewReader(in), nil
680 for _, test := range tests {
681 if len(test.exp) == 1 {
682 addr, err := ap.Parse(test.addrsStr)
684 t.Errorf("Failed parsing (single) %q: %v", test.addrsStr, err)
687 if !reflect.DeepEqual([]*Address{addr}, test.exp) {
688 t.Errorf("Parse (single) of %q: got %+v, want %+v", test.addrsStr, addr, test.exp)
692 addrs, err := ap.ParseList(test.addrsStr)
694 t.Errorf("Failed parsing (list) %q: %v", test.addrsStr, err)
697 if !reflect.DeepEqual(addrs, test.exp) {
698 t.Errorf("Parse (list) of %q: got %+v, want %+v", test.addrsStr, addrs, test.exp)
703 func TestAddressString(t *testing.T) {
709 &Address{Address: "bob@example.com"},
712 { // quoted local parts: RFC 5322, 3.4.1. and 3.2.4.
713 &Address{Address: `my@idiot@address@example.com`},
714 `<"my@idiot@address"@example.com>`,
716 { // quoted local parts
717 &Address{Address: ` @example.com`},
721 &Address{Name: "Bob", Address: "bob@example.com"},
722 `"Bob" <bob@example.com>`,
725 // note the ö (o with an umlaut)
726 &Address{Name: "Böb", Address: "bob@example.com"},
727 `=?utf-8?q?B=C3=B6b?= <bob@example.com>`,
730 &Address{Name: "Bob Jane", Address: "bob@example.com"},
731 `"Bob Jane" <bob@example.com>`,
734 &Address{Name: "Böb Jacöb", Address: "bob@example.com"},
735 `=?utf-8?q?B=C3=B6b_Jac=C3=B6b?= <bob@example.com>`,
737 { // https://golang.org/issue/12098
738 &Address{Name: "Rob", Address: ""},
741 { // https://golang.org/issue/12098
742 &Address{Name: "Rob", Address: "@"},
746 &Address{Name: "Böb, Jacöb", Address: "bob@example.com"},
747 `=?utf-8?b?QsO2YiwgSmFjw7Zi?= <bob@example.com>`,
750 &Address{Name: "=??Q?x?=", Address: "hello@world.com"},
751 `"=??Q?x?=" <hello@world.com>`,
754 &Address{Name: "=?hello", Address: "hello@world.com"},
755 `"=?hello" <hello@world.com>`,
758 &Address{Name: "world?=", Address: "hello@world.com"},
759 `"world?=" <hello@world.com>`,
762 // should q-encode even for invalid utf-8.
763 &Address{Name: string([]byte{0xed, 0xa0, 0x80}), Address: "invalid-utf8@example.net"},
764 "=?utf-8?q?=ED=A0=80?= <invalid-utf8@example.net>",
767 for _, test := range tests {
768 s := test.addr.String()
770 t.Errorf("Address%+v.String() = %v, want %v", *test.addr, s, test.exp)
775 if test.addr.Address != "" && test.addr.Address != "@" {
776 a, err := ParseAddress(test.exp)
778 t.Errorf("ParseAddress(%#q): %v", test.exp, err)
781 if a.Name != test.addr.Name || a.Address != test.addr.Address {
782 t.Errorf("ParseAddress(%#q) = %#v, want %#v", test.exp, a, test.addr)
788 // Check if all valid addresses can be parsed, formatted and parsed again
789 func TestAddressParsingAndFormatting(t *testing.T) {
794 `<bob.bob@example.com>`,
795 `<".bob"@example.com>`,
797 `<some.mail-with-dash@example.com>`,
798 `<"dot.and space"@example.com>`,
799 `<"very.unusual.@.unusual.com"@example.com>`,
800 `<admin@mailserver1>`,
801 `<postmaster@localhost>`,
802 "<#!$%&'*+-/=?^_`{}|~@example.org>",
803 `<"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com>`, // escaped quotes
804 `<"()<>[]:,;@\\\"!#$%&'*+-/=?^_{}| ~.a"@example.org>`, // escaped backslashes
805 `<"Abc\\@def"@example.com>`,
806 `<"Joe\\Blow"@example.com>`,
807 `<test1/test2=test3@example.com>`,
808 `<def!xyz%abc@example.com>`,
809 `<_somename@example.com>`,
813 `<"john..doe"@example.com>`,
814 `<"john.doe."@example.com>`,
815 `<".john.doe"@example.com>`,
817 `<".."@example.com>`,
821 for _, test := range tests {
822 addr, err := ParseAddress(test)
824 t.Errorf("Couldn't parse address %s: %s", test, err.Error())
828 addr, err = ParseAddress(str)
830 t.Errorf("ParseAddr(%q) error: %v", test, err)
834 if addr.String() != test {
835 t.Errorf("String() round-trip = %q; want %q", addr, test)
842 badTests := []string{
844 `<A@b@c@example.com>`,
845 `<a"b(c)d,e:f;g<h>i[j\k]l@example.com>`,
846 `<just"not"right@example.com>`,
847 `<this is"not\allowed@example.com>`,
848 `<this\ still\"not\\allowed@example.com>`,
849 `<john..doe@example.com>`,
850 `<john.doe@example..com>`,
851 `<john.doe@example..com>`,
852 `<john.doe.@example.com>`,
853 `<john.doe.@.example.com>`,
854 `<.john.doe@example.com>`,
859 `<""test""blah""@example.com>`,
863 for _, test := range badTests {
864 _, err := ParseAddress(test)
866 t.Errorf("Should have failed to parse address: %s", test)
874 func TestAddressFormattingAndParsing(t *testing.T) {
876 {Name: "@lïce", Address: "alice@example.com"},
877 {Name: "Böb O'Connor", Address: "bob@example.com"},
878 {Name: "???", Address: "bob@example.com"},
879 {Name: "Böb ???", Address: "bob@example.com"},
880 {Name: "Böb (Jacöb)", Address: "bob@example.com"},
881 {Name: "à#$%&'(),.:;<>@[]^`{|}~'", Address: "bob@example.com"},
882 // https://golang.org/issue/11292
883 {Name: "\"\\\x1f,\"", Address: "0@0"},
884 // https://golang.org/issue/12782
885 {Name: "naé, mée", Address: "test.mail@gmail.com"},
888 for i, test := range tests {
889 parsed, err := ParseAddress(test.String())
891 t.Errorf("test #%d: ParseAddr(%q) error: %v", i, test.String(), err)
894 if parsed.Name != test.Name {
895 t.Errorf("test #%d: Parsed name = %q; want %q", i, parsed.Name, test.Name)
897 if parsed.Address != test.Address {
898 t.Errorf("test #%d: Parsed address = %q; want %q", i, parsed.Address, test.Address)