github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/libgo/go/net/mail/message_test.go (about)

     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.
     4  
     5  package mail
     6  
     7  import (
     8  	"bytes"
     9  	"io"
    10  	"io/ioutil"
    11  	"mime"
    12  	"reflect"
    13  	"strings"
    14  	"testing"
    15  	"time"
    16  )
    17  
    18  var parseTests = []struct {
    19  	in     string
    20  	header Header
    21  	body   string
    22  }{
    23  	{
    24  		// RFC 5322, Appendix A.1.1
    25  		in: `From: John Doe <jdoe@machine.example>
    26  To: Mary Smith <mary@example.net>
    27  Subject: Saying Hello
    28  Date: Fri, 21 Nov 1997 09:55:06 -0600
    29  Message-ID: <1234@local.machine.example>
    30  
    31  This is a message just to say hello.
    32  So, "Hello".
    33  `,
    34  		header: Header{
    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>"},
    40  		},
    41  		body: "This is a message just to say hello.\nSo, \"Hello\".\n",
    42  	},
    43  }
    44  
    45  func TestParsing(t *testing.T) {
    46  	for i, test := range parseTests {
    47  		msg, err := ReadMessage(bytes.NewBuffer([]byte(test.in)))
    48  		if err != nil {
    49  			t.Errorf("test #%d: Failed parsing message: %v", i, err)
    50  			continue
    51  		}
    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)
    55  		}
    56  		body, err := ioutil.ReadAll(msg.Body)
    57  		if err != nil {
    58  			t.Errorf("test #%d: Failed reading body: %v", i, err)
    59  			continue
    60  		}
    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)
    65  		}
    66  	}
    67  }
    68  
    69  func headerEq(a, b Header) bool {
    70  	if len(a) != len(b) {
    71  		return false
    72  	}
    73  	for k, as := range a {
    74  		bs, ok := b[k]
    75  		if !ok {
    76  			return false
    77  		}
    78  		if !reflect.DeepEqual(as, bs) {
    79  			return false
    80  		}
    81  	}
    82  	return true
    83  }
    84  
    85  func TestDateParsing(t *testing.T) {
    86  	tests := []struct {
    87  		dateStr string
    88  		exp     time.Time
    89  	}{
    90  		// RFC 5322, Appendix A.1.1
    91  		{
    92  			"Fri, 21 Nov 1997 09:55:06 -0600",
    93  			time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)),
    94  		},
    95  		// RFC 5322, Appendix A.6.2
    96  		// Obsolete date.
    97  		{
    98  			"21 Nov 97 09:55:06 GMT",
    99  			time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("GMT", 0)),
   100  		},
   101  		// Commonly found format not specified by RFC 5322.
   102  		{
   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)),
   105  		},
   106  	}
   107  	for _, test := range tests {
   108  		hdr := Header{
   109  			"Date": []string{test.dateStr},
   110  		}
   111  		date, err := hdr.Date()
   112  		if err != nil {
   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)
   116  		}
   117  
   118  		date, err = ParseDate(test.dateStr)
   119  		if err != nil {
   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)
   123  		}
   124  	}
   125  }
   126  
   127  func TestDateParsingCFWS(t *testing.T) {
   128  	tests := []struct {
   129  		dateStr string
   130  		exp     time.Time
   131  		valid   bool
   132  	}{
   133  		// FWS-only. No date.
   134  		{
   135  			"   ",
   136  			// nil is not allowed
   137  			time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)),
   138  			false,
   139  		},
   140  		// FWS is allowed before optional day of week.
   141  		{
   142  			"   Fri, 21 Nov 1997 09:55:06 -0600",
   143  			time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)),
   144  			true,
   145  		},
   146  		{
   147  			"21 Nov 1997 09:55:06 -0600",
   148  			time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)),
   149  			true,
   150  		},
   151  		{
   152  			"Fri 21 Nov 1997 09:55:06 -0600",
   153  			time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)),
   154  			false, // missing ,
   155  		},
   156  		// FWS is allowed before day of month but HTAB fails.
   157  		{
   158  			"Fri,        21 Nov 1997 09:55:06 -0600",
   159  			time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)),
   160  			true,
   161  		},
   162  		// FWS is allowed before and after year but HTAB fails.
   163  		{
   164  			"Fri, 21 Nov       1997     09:55:06 -0600",
   165  			time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)),
   166  			true,
   167  		},
   168  		// FWS is allowed before zone but HTAB is not handled. Obsolete timezone is handled.
   169  		{
   170  			"Fri, 21 Nov 1997 09:55:06           CST",
   171  			time.Time{},
   172  			true,
   173  		},
   174  		// FWS is allowed after date and a CRLF is already replaced.
   175  		{
   176  			"Fri, 21 Nov 1997 09:55:06           CST (no leading FWS and a trailing CRLF) \r\n",
   177  			time.Time{},
   178  			true,
   179  		},
   180  		// CFWS is a reduced set of US-ASCII where space and accentuated are obsolete. No error.
   181  		{
   182  			"Fri, 21    Nov 1997    09:55:06 -0600 (MDT and non-US-ASCII signs éèç )",
   183  			time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)),
   184  			true,
   185  		},
   186  		// CFWS is allowed after zone including a nested comment.
   187  		// Trailing FWS is allowed.
   188  		{
   189  			"Fri, 21 Nov 1997 09:55:06 -0600    \r\n (thisisa(valid)cfws)   \t ",
   190  			time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)),
   191  			true,
   192  		},
   193  		// CRLF is incomplete and misplaced.
   194  		{
   195  			"Fri, 21 Nov 1997 \r 09:55:06 -0600    \r\n (thisisa(valid)cfws)   \t ",
   196  			time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)),
   197  			false,
   198  		},
   199  		// CRLF is complete but misplaced. No error is returned.
   200  		{
   201  			"Fri, 21 Nov 199\r\n7  09:55:06 -0600    \r\n (thisisa(valid)cfws)   \t ",
   202  			time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)),
   203  			true, // should be false in the strict interpretation of RFC 5322.
   204  		},
   205  		// Invalid ASCII in date.
   206  		{
   207  			"Fri, 21 Nov 1997 ù 09:55:06 -0600    \r\n (thisisa(valid)cfws)   \t ",
   208  			time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)),
   209  			false,
   210  		},
   211  		// CFWS chars () in date.
   212  		{
   213  			"Fri, 21 Nov () 1997 09:55:06 -0600    \r\n (thisisa(valid)cfws)   \t ",
   214  			time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)),
   215  			false,
   216  		},
   217  		// Timezone is invalid but T is found in comment.
   218  		{
   219  			"Fri, 21 Nov 1997 09:55:06 -060    \r\n (Thisisa(valid)cfws)   \t ",
   220  			time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)),
   221  			false,
   222  		},
   223  		// Date has no month.
   224  		{
   225  			"Fri, 21  1997 09:55:06 -0600",
   226  			time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)),
   227  			false,
   228  		},
   229  		// Invalid month : OCT iso Oct
   230  		{
   231  			"Fri, 21 OCT 1997 09:55:06 CST",
   232  			time.Time{},
   233  			false,
   234  		},
   235  		// A too short time zone.
   236  		{
   237  			"Fri, 21 Nov 1997 09:55:06 -060",
   238  			time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)),
   239  			false,
   240  		},
   241  		// A too short obsolete time zone.
   242  		{
   243  			"Fri, 21  1997 09:55:06 GT",
   244  			time.Date(1997, 11, 21, 9, 55, 6, 0, time.FixedZone("", -6*60*60)),
   245  			false,
   246  		},
   247  	}
   248  	for _, test := range tests {
   249  		hdr := Header{
   250  			"Date": []string{test.dateStr},
   251  		}
   252  		date, err := hdr.Date()
   253  		if err != nil && test.valid {
   254  			t.Errorf("Header(Date: %s).Date(): %v", test.dateStr, err)
   255  		} else if err == nil && test.exp.IsZero() {
   256  			// OK.  Used when exact result depends on the
   257  			// system's local zoneinfo.
   258  		} else if err == nil && !date.Equal(test.exp) && test.valid {
   259  			t.Errorf("Header(Date: %s).Date() = %+v, want %+v", test.dateStr, date, test.exp)
   260  		} else if err == nil && !test.valid { // an invalid expression was tested
   261  			t.Errorf("Header(Date: %s).Date() did not return an error but %v", test.dateStr, date)
   262  		}
   263  
   264  		date, err = ParseDate(test.dateStr)
   265  		if err != nil && test.valid {
   266  			t.Errorf("ParseDate(%s): %v", test.dateStr, err)
   267  		} else if err == nil && test.exp.IsZero() {
   268  			// OK.  Used when exact result depends on the
   269  			// system's local zoneinfo.
   270  		} else if err == nil && !test.valid { // an invalid expression was tested
   271  			t.Errorf("ParseDate(%s) did not return an error but %v", test.dateStr, date)
   272  		} else if err == nil && test.valid && !date.Equal(test.exp) {
   273  			t.Errorf("ParseDate(%s) = %+v, want %+v", test.dateStr, date, test.exp)
   274  		}
   275  	}
   276  }
   277  
   278  func TestAddressParsingError(t *testing.T) {
   279  	mustErrTestCases := [...]struct {
   280  		text        string
   281  		wantErrText string
   282  	}{
   283  		0:  {"=?iso-8859-2?Q?Bogl=E1rka_Tak=E1cs?= <unknown@gmail.com>", "charset not supported"},
   284  		1:  {"a@gmail.com b@gmail.com", "expected single address"},
   285  		2:  {string([]byte{0xed, 0xa0, 0x80}) + " <micro@example.net>", "invalid utf-8 in address"},
   286  		3:  {"\"" + string([]byte{0xed, 0xa0, 0x80}) + "\" <half-surrogate@example.com>", "invalid utf-8 in quoted-string"},
   287  		4:  {"\"\\" + string([]byte{0x80}) + "\" <escaped-invalid-unicode@example.net>", "invalid utf-8 in quoted-string"},
   288  		5:  {"\"\x00\" <null@example.net>", "bad character in quoted-string"},
   289  		6:  {"\"\\\x00\" <escaped-null@example.net>", "bad character in quoted-string"},
   290  		7:  {"John Doe", "no angle-addr"},
   291  		8:  {`<jdoe#machine.example>`, "missing @ in addr-spec"},
   292  		9:  {`John <middle> Doe <jdoe@machine.example>`, "missing @ in addr-spec"},
   293  		10: {"cfws@example.com (", "misformatted parenthetical comment"},
   294  		11: {"empty group: ;", "empty group"},
   295  		12: {"root group: embed group: null@example.com;", "no angle-addr"},
   296  		13: {"group not closed: null@example.com", "expected comma"},
   297  		14: {"group: first@example.com, second@example.com;", "group with multiple addresses"},
   298  		15: {"john.doe", "missing '@' or angle-addr"},
   299  		16: {"john.doe@", "no angle-addr"},
   300  		17: {"John Doe@foo.bar", "no angle-addr"},
   301  	}
   302  
   303  	for i, tc := range mustErrTestCases {
   304  		_, err := ParseAddress(tc.text)
   305  		if err == nil || !strings.Contains(err.Error(), tc.wantErrText) {
   306  			t.Errorf(`mail.ParseAddress(%q) #%d want %q, got %v`, tc.text, i, tc.wantErrText, err)
   307  		}
   308  	}
   309  }
   310  
   311  func TestAddressParsing(t *testing.T) {
   312  	tests := []struct {
   313  		addrsStr string
   314  		exp      []*Address
   315  	}{
   316  		// Bare address
   317  		{
   318  			`jdoe@machine.example`,
   319  			[]*Address{{
   320  				Address: "jdoe@machine.example",
   321  			}},
   322  		},
   323  		// RFC 5322, Appendix A.1.1
   324  		{
   325  			`John Doe <jdoe@machine.example>`,
   326  			[]*Address{{
   327  				Name:    "John Doe",
   328  				Address: "jdoe@machine.example",
   329  			}},
   330  		},
   331  		// RFC 5322, Appendix A.1.2
   332  		{
   333  			`"Joe Q. Public" <john.q.public@example.com>`,
   334  			[]*Address{{
   335  				Name:    "Joe Q. Public",
   336  				Address: "john.q.public@example.com",
   337  			}},
   338  		},
   339  		{
   340  			`"John (middle) Doe" <jdoe@machine.example>`,
   341  			[]*Address{{
   342  				Name:    "John (middle) Doe",
   343  				Address: "jdoe@machine.example",
   344  			}},
   345  		},
   346  		{
   347  			`John (middle) Doe <jdoe@machine.example>`,
   348  			[]*Address{{
   349  				Name:    "John (middle) Doe",
   350  				Address: "jdoe@machine.example",
   351  			}},
   352  		},
   353  		{
   354  			`John !@M@! Doe <jdoe@machine.example>`,
   355  			[]*Address{{
   356  				Name:    "John !@M@! Doe",
   357  				Address: "jdoe@machine.example",
   358  			}},
   359  		},
   360  		{
   361  			`"John <middle> Doe" <jdoe@machine.example>`,
   362  			[]*Address{{
   363  				Name:    "John <middle> Doe",
   364  				Address: "jdoe@machine.example",
   365  			}},
   366  		},
   367  		{
   368  			`Mary Smith <mary@x.test>, jdoe@example.org, Who? <one@y.test>`,
   369  			[]*Address{
   370  				{
   371  					Name:    "Mary Smith",
   372  					Address: "mary@x.test",
   373  				},
   374  				{
   375  					Address: "jdoe@example.org",
   376  				},
   377  				{
   378  					Name:    "Who?",
   379  					Address: "one@y.test",
   380  				},
   381  			},
   382  		},
   383  		{
   384  			`<boss@nil.test>, "Giant; \"Big\" Box" <sysservices@example.net>`,
   385  			[]*Address{
   386  				{
   387  					Address: "boss@nil.test",
   388  				},
   389  				{
   390  					Name:    `Giant; "Big" Box`,
   391  					Address: "sysservices@example.net",
   392  				},
   393  			},
   394  		},
   395  		// RFC 5322, Appendix A.6.1
   396  		{
   397  			`Joe Q. Public <john.q.public@example.com>`,
   398  			[]*Address{{
   399  				Name:    "Joe Q. Public",
   400  				Address: "john.q.public@example.com",
   401  			}},
   402  		},
   403  		// RFC 5322, Appendix A.1.3
   404  		{
   405  			`group1: groupaddr1@example.com;`,
   406  			[]*Address{
   407  				{
   408  					Name:    "",
   409  					Address: "groupaddr1@example.com",
   410  				},
   411  			},
   412  		},
   413  		{
   414  			`empty group: ;`,
   415  			[]*Address(nil),
   416  		},
   417  		{
   418  			`A Group:Ed Jones <c@a.test>,joe@where.test,John <jdoe@one.test>;`,
   419  			[]*Address{
   420  				{
   421  					Name:    "Ed Jones",
   422  					Address: "c@a.test",
   423  				},
   424  				{
   425  					Name:    "",
   426  					Address: "joe@where.test",
   427  				},
   428  				{
   429  					Name:    "John",
   430  					Address: "jdoe@one.test",
   431  				},
   432  			},
   433  		},
   434  		{
   435  			`Group1: <addr1@example.com>;, Group 2: addr2@example.com;, John <addr3@example.com>`,
   436  			[]*Address{
   437  				{
   438  					Name:    "",
   439  					Address: "addr1@example.com",
   440  				},
   441  				{
   442  					Name:    "",
   443  					Address: "addr2@example.com",
   444  				},
   445  				{
   446  					Name:    "John",
   447  					Address: "addr3@example.com",
   448  				},
   449  			},
   450  		},
   451  		// RFC 2047 "Q"-encoded ISO-8859-1 address.
   452  		{
   453  			`=?iso-8859-1?q?J=F6rg_Doe?= <joerg@example.com>`,
   454  			[]*Address{
   455  				{
   456  					Name:    `Jörg Doe`,
   457  					Address: "joerg@example.com",
   458  				},
   459  			},
   460  		},
   461  		// RFC 2047 "Q"-encoded US-ASCII address. Dumb but legal.
   462  		{
   463  			`=?us-ascii?q?J=6Frg_Doe?= <joerg@example.com>`,
   464  			[]*Address{
   465  				{
   466  					Name:    `Jorg Doe`,
   467  					Address: "joerg@example.com",
   468  				},
   469  			},
   470  		},
   471  		// RFC 2047 "Q"-encoded UTF-8 address.
   472  		{
   473  			`=?utf-8?q?J=C3=B6rg_Doe?= <joerg@example.com>`,
   474  			[]*Address{
   475  				{
   476  					Name:    `Jörg Doe`,
   477  					Address: "joerg@example.com",
   478  				},
   479  			},
   480  		},
   481  		// RFC 2047 "Q"-encoded UTF-8 address with multiple encoded-words.
   482  		{
   483  			`=?utf-8?q?J=C3=B6rg?=  =?utf-8?q?Doe?= <joerg@example.com>`,
   484  			[]*Address{
   485  				{
   486  					Name:    `JörgDoe`,
   487  					Address: "joerg@example.com",
   488  				},
   489  			},
   490  		},
   491  		// RFC 2047, Section 8.
   492  		{
   493  			`=?ISO-8859-1?Q?Andr=E9?= Pirard <PIRARD@vm1.ulg.ac.be>`,
   494  			[]*Address{
   495  				{
   496  					Name:    `André Pirard`,
   497  					Address: "PIRARD@vm1.ulg.ac.be",
   498  				},
   499  			},
   500  		},
   501  		// Custom example of RFC 2047 "B"-encoded ISO-8859-1 address.
   502  		{
   503  			`=?ISO-8859-1?B?SvZyZw==?= <joerg@example.com>`,
   504  			[]*Address{
   505  				{
   506  					Name:    `Jörg`,
   507  					Address: "joerg@example.com",
   508  				},
   509  			},
   510  		},
   511  		// Custom example of RFC 2047 "B"-encoded UTF-8 address.
   512  		{
   513  			`=?UTF-8?B?SsO2cmc=?= <joerg@example.com>`,
   514  			[]*Address{
   515  				{
   516  					Name:    `Jörg`,
   517  					Address: "joerg@example.com",
   518  				},
   519  			},
   520  		},
   521  		// Custom example with "." in name. For issue 4938
   522  		{
   523  			`Asem H. <noreply@example.com>`,
   524  			[]*Address{
   525  				{
   526  					Name:    `Asem H.`,
   527  					Address: "noreply@example.com",
   528  				},
   529  			},
   530  		},
   531  		// RFC 6532 3.2.3, qtext /= UTF8-non-ascii
   532  		{
   533  			`"Gø Pher" <gopher@example.com>`,
   534  			[]*Address{
   535  				{
   536  					Name:    `Gø Pher`,
   537  					Address: "gopher@example.com",
   538  				},
   539  			},
   540  		},
   541  		// RFC 6532 3.2, atext /= UTF8-non-ascii
   542  		{
   543  			`µ <micro@example.com>`,
   544  			[]*Address{
   545  				{
   546  					Name:    `µ`,
   547  					Address: "micro@example.com",
   548  				},
   549  			},
   550  		},
   551  		// RFC 6532 3.2.2, local address parts allow UTF-8
   552  		{
   553  			`Micro <µ@example.com>`,
   554  			[]*Address{
   555  				{
   556  					Name:    `Micro`,
   557  					Address: "µ@example.com",
   558  				},
   559  			},
   560  		},
   561  		// RFC 6532 3.2.4, domains parts allow UTF-8
   562  		{
   563  			`Micro <micro@µ.example.com>`,
   564  			[]*Address{
   565  				{
   566  					Name:    `Micro`,
   567  					Address: "micro@µ.example.com",
   568  				},
   569  			},
   570  		},
   571  		// Issue 14866
   572  		{
   573  			`"" <emptystring@example.com>`,
   574  			[]*Address{
   575  				{
   576  					Name:    "",
   577  					Address: "emptystring@example.com",
   578  				},
   579  			},
   580  		},
   581  		// CFWS
   582  		{
   583  			`<cfws@example.com> (CFWS (cfws))  (another comment)`,
   584  			[]*Address{
   585  				{
   586  					Name:    "",
   587  					Address: "cfws@example.com",
   588  				},
   589  			},
   590  		},
   591  		{
   592  			`<cfws@example.com> ()  (another comment), <cfws2@example.com> (another)`,
   593  			[]*Address{
   594  				{
   595  					Name:    "",
   596  					Address: "cfws@example.com",
   597  				},
   598  				{
   599  					Name:    "",
   600  					Address: "cfws2@example.com",
   601  				},
   602  			},
   603  		},
   604  		// Comment as display name
   605  		{
   606  			`john@example.com (John Doe)`,
   607  			[]*Address{
   608  				{
   609  					Name:    "John Doe",
   610  					Address: "john@example.com",
   611  				},
   612  			},
   613  		},
   614  		// Comment and display name
   615  		{
   616  			`John Doe <john@example.com> (Joey)`,
   617  			[]*Address{
   618  				{
   619  					Name:    "John Doe",
   620  					Address: "john@example.com",
   621  				},
   622  			},
   623  		},
   624  		// Comment as display name, no space
   625  		{
   626  			`john@example.com(John Doe)`,
   627  			[]*Address{
   628  				{
   629  					Name:    "John Doe",
   630  					Address: "john@example.com",
   631  				},
   632  			},
   633  		},
   634  		// Comment as display name, Q-encoded
   635  		{
   636  			`asjo@example.com (Adam =?utf-8?Q?Sj=C3=B8gren?=)`,
   637  			[]*Address{
   638  				{
   639  					Name:    "Adam Sjøgren",
   640  					Address: "asjo@example.com",
   641  				},
   642  			},
   643  		},
   644  		// Comment as display name, Q-encoded and tab-separated
   645  		{
   646  			`asjo@example.com (Adam	=?utf-8?Q?Sj=C3=B8gren?=)`,
   647  			[]*Address{
   648  				{
   649  					Name:    "Adam Sjøgren",
   650  					Address: "asjo@example.com",
   651  				},
   652  			},
   653  		},
   654  		// Nested comment as display name, Q-encoded
   655  		{
   656  			`asjo@example.com (Adam =?utf-8?Q?Sj=C3=B8gren?= (Debian))`,
   657  			[]*Address{
   658  				{
   659  					Name:    "Adam Sjøgren (Debian)",
   660  					Address: "asjo@example.com",
   661  				},
   662  			},
   663  		},
   664  	}
   665  	for _, test := range tests {
   666  		if len(test.exp) == 1 {
   667  			addr, err := ParseAddress(test.addrsStr)
   668  			if err != nil {
   669  				t.Errorf("Failed parsing (single) %q: %v", test.addrsStr, err)
   670  				continue
   671  			}
   672  			if !reflect.DeepEqual([]*Address{addr}, test.exp) {
   673  				t.Errorf("Parse (single) of %q: got %+v, want %+v", test.addrsStr, addr, test.exp)
   674  			}
   675  		}
   676  
   677  		addrs, err := ParseAddressList(test.addrsStr)
   678  		if err != nil {
   679  			t.Errorf("Failed parsing (list) %q: %v", test.addrsStr, err)
   680  			continue
   681  		}
   682  		if !reflect.DeepEqual(addrs, test.exp) {
   683  			t.Errorf("Parse (list) of %q: got %+v, want %+v", test.addrsStr, addrs, test.exp)
   684  		}
   685  	}
   686  }
   687  
   688  func TestAddressParser(t *testing.T) {
   689  	tests := []struct {
   690  		addrsStr string
   691  		exp      []*Address
   692  	}{
   693  		// Bare address
   694  		{
   695  			`jdoe@machine.example`,
   696  			[]*Address{{
   697  				Address: "jdoe@machine.example",
   698  			}},
   699  		},
   700  		// RFC 5322, Appendix A.1.1
   701  		{
   702  			`John Doe <jdoe@machine.example>`,
   703  			[]*Address{{
   704  				Name:    "John Doe",
   705  				Address: "jdoe@machine.example",
   706  			}},
   707  		},
   708  		// RFC 5322, Appendix A.1.2
   709  		{
   710  			`"Joe Q. Public" <john.q.public@example.com>`,
   711  			[]*Address{{
   712  				Name:    "Joe Q. Public",
   713  				Address: "john.q.public@example.com",
   714  			}},
   715  		},
   716  		{
   717  			`Mary Smith <mary@x.test>, jdoe@example.org, Who? <one@y.test>`,
   718  			[]*Address{
   719  				{
   720  					Name:    "Mary Smith",
   721  					Address: "mary@x.test",
   722  				},
   723  				{
   724  					Address: "jdoe@example.org",
   725  				},
   726  				{
   727  					Name:    "Who?",
   728  					Address: "one@y.test",
   729  				},
   730  			},
   731  		},
   732  		{
   733  			`<boss@nil.test>, "Giant; \"Big\" Box" <sysservices@example.net>`,
   734  			[]*Address{
   735  				{
   736  					Address: "boss@nil.test",
   737  				},
   738  				{
   739  					Name:    `Giant; "Big" Box`,
   740  					Address: "sysservices@example.net",
   741  				},
   742  			},
   743  		},
   744  		// RFC 2047 "Q"-encoded ISO-8859-1 address.
   745  		{
   746  			`=?iso-8859-1?q?J=F6rg_Doe?= <joerg@example.com>`,
   747  			[]*Address{
   748  				{
   749  					Name:    `Jörg Doe`,
   750  					Address: "joerg@example.com",
   751  				},
   752  			},
   753  		},
   754  		// RFC 2047 "Q"-encoded US-ASCII address. Dumb but legal.
   755  		{
   756  			`=?us-ascii?q?J=6Frg_Doe?= <joerg@example.com>`,
   757  			[]*Address{
   758  				{
   759  					Name:    `Jorg Doe`,
   760  					Address: "joerg@example.com",
   761  				},
   762  			},
   763  		},
   764  		// RFC 2047 "Q"-encoded ISO-8859-15 address.
   765  		{
   766  			`=?ISO-8859-15?Q?J=F6rg_Doe?= <joerg@example.com>`,
   767  			[]*Address{
   768  				{
   769  					Name:    `Jörg Doe`,
   770  					Address: "joerg@example.com",
   771  				},
   772  			},
   773  		},
   774  		// RFC 2047 "B"-encoded windows-1252 address.
   775  		{
   776  			`=?windows-1252?q?Andr=E9?= Pirard <PIRARD@vm1.ulg.ac.be>`,
   777  			[]*Address{
   778  				{
   779  					Name:    `André Pirard`,
   780  					Address: "PIRARD@vm1.ulg.ac.be",
   781  				},
   782  			},
   783  		},
   784  		// Custom example of RFC 2047 "B"-encoded ISO-8859-15 address.
   785  		{
   786  			`=?ISO-8859-15?B?SvZyZw==?= <joerg@example.com>`,
   787  			[]*Address{
   788  				{
   789  					Name:    `Jörg`,
   790  					Address: "joerg@example.com",
   791  				},
   792  			},
   793  		},
   794  		// Custom example of RFC 2047 "B"-encoded UTF-8 address.
   795  		{
   796  			`=?UTF-8?B?SsO2cmc=?= <joerg@example.com>`,
   797  			[]*Address{
   798  				{
   799  					Name:    `Jörg`,
   800  					Address: "joerg@example.com",
   801  				},
   802  			},
   803  		},
   804  		// Custom example with "." in name. For issue 4938
   805  		{
   806  			`Asem H. <noreply@example.com>`,
   807  			[]*Address{
   808  				{
   809  					Name:    `Asem H.`,
   810  					Address: "noreply@example.com",
   811  				},
   812  			},
   813  		},
   814  	}
   815  
   816  	ap := AddressParser{WordDecoder: &mime.WordDecoder{
   817  		CharsetReader: func(charset string, input io.Reader) (io.Reader, error) {
   818  			in, err := ioutil.ReadAll(input)
   819  			if err != nil {
   820  				return nil, err
   821  			}
   822  
   823  			switch charset {
   824  			case "iso-8859-15":
   825  				in = bytes.ReplaceAll(in, []byte("\xf6"), []byte("ö"))
   826  			case "windows-1252":
   827  				in = bytes.ReplaceAll(in, []byte("\xe9"), []byte("é"))
   828  			}
   829  
   830  			return bytes.NewReader(in), nil
   831  		},
   832  	}}
   833  
   834  	for _, test := range tests {
   835  		if len(test.exp) == 1 {
   836  			addr, err := ap.Parse(test.addrsStr)
   837  			if err != nil {
   838  				t.Errorf("Failed parsing (single) %q: %v", test.addrsStr, err)
   839  				continue
   840  			}
   841  			if !reflect.DeepEqual([]*Address{addr}, test.exp) {
   842  				t.Errorf("Parse (single) of %q: got %+v, want %+v", test.addrsStr, addr, test.exp)
   843  			}
   844  		}
   845  
   846  		addrs, err := ap.ParseList(test.addrsStr)
   847  		if err != nil {
   848  			t.Errorf("Failed parsing (list) %q: %v", test.addrsStr, err)
   849  			continue
   850  		}
   851  		if !reflect.DeepEqual(addrs, test.exp) {
   852  			t.Errorf("Parse (list) of %q: got %+v, want %+v", test.addrsStr, addrs, test.exp)
   853  		}
   854  	}
   855  }
   856  
   857  func TestAddressString(t *testing.T) {
   858  	tests := []struct {
   859  		addr *Address
   860  		exp  string
   861  	}{
   862  		{
   863  			&Address{Address: "bob@example.com"},
   864  			"<bob@example.com>",
   865  		},
   866  		{ // quoted local parts: RFC 5322, 3.4.1. and 3.2.4.
   867  			&Address{Address: `my@idiot@address@example.com`},
   868  			`<"my@idiot@address"@example.com>`,
   869  		},
   870  		{ // quoted local parts
   871  			&Address{Address: ` @example.com`},
   872  			`<" "@example.com>`,
   873  		},
   874  		{
   875  			&Address{Name: "Bob", Address: "bob@example.com"},
   876  			`"Bob" <bob@example.com>`,
   877  		},
   878  		{
   879  			// note the ö (o with an umlaut)
   880  			&Address{Name: "Böb", Address: "bob@example.com"},
   881  			`=?utf-8?q?B=C3=B6b?= <bob@example.com>`,
   882  		},
   883  		{
   884  			&Address{Name: "Bob Jane", Address: "bob@example.com"},
   885  			`"Bob Jane" <bob@example.com>`,
   886  		},
   887  		{
   888  			&Address{Name: "Böb Jacöb", Address: "bob@example.com"},
   889  			`=?utf-8?q?B=C3=B6b_Jac=C3=B6b?= <bob@example.com>`,
   890  		},
   891  		{ // https://golang.org/issue/12098
   892  			&Address{Name: "Rob", Address: ""},
   893  			`"Rob" <@>`,
   894  		},
   895  		{ // https://golang.org/issue/12098
   896  			&Address{Name: "Rob", Address: "@"},
   897  			`"Rob" <@>`,
   898  		},
   899  		{
   900  			&Address{Name: "Böb, Jacöb", Address: "bob@example.com"},
   901  			`=?utf-8?b?QsO2YiwgSmFjw7Zi?= <bob@example.com>`,
   902  		},
   903  		{
   904  			&Address{Name: "=??Q?x?=", Address: "hello@world.com"},
   905  			`"=??Q?x?=" <hello@world.com>`,
   906  		},
   907  		{
   908  			&Address{Name: "=?hello", Address: "hello@world.com"},
   909  			`"=?hello" <hello@world.com>`,
   910  		},
   911  		{
   912  			&Address{Name: "world?=", Address: "hello@world.com"},
   913  			`"world?=" <hello@world.com>`,
   914  		},
   915  		{
   916  			// should q-encode even for invalid utf-8.
   917  			&Address{Name: string([]byte{0xed, 0xa0, 0x80}), Address: "invalid-utf8@example.net"},
   918  			"=?utf-8?q?=ED=A0=80?= <invalid-utf8@example.net>",
   919  		},
   920  	}
   921  	for _, test := range tests {
   922  		s := test.addr.String()
   923  		if s != test.exp {
   924  			t.Errorf("Address%+v.String() = %v, want %v", *test.addr, s, test.exp)
   925  			continue
   926  		}
   927  
   928  		// Check round-trip.
   929  		if test.addr.Address != "" && test.addr.Address != "@" {
   930  			a, err := ParseAddress(test.exp)
   931  			if err != nil {
   932  				t.Errorf("ParseAddress(%#q): %v", test.exp, err)
   933  				continue
   934  			}
   935  			if a.Name != test.addr.Name || a.Address != test.addr.Address {
   936  				t.Errorf("ParseAddress(%#q) = %#v, want %#v", test.exp, a, test.addr)
   937  			}
   938  		}
   939  	}
   940  }
   941  
   942  // Check if all valid addresses can be parsed, formatted and parsed again
   943  func TestAddressParsingAndFormatting(t *testing.T) {
   944  
   945  	// Should pass
   946  	tests := []string{
   947  		`<Bob@example.com>`,
   948  		`<bob.bob@example.com>`,
   949  		`<".bob"@example.com>`,
   950  		`<" "@example.com>`,
   951  		`<some.mail-with-dash@example.com>`,
   952  		`<"dot.and space"@example.com>`,
   953  		`<"very.unusual.@.unusual.com"@example.com>`,
   954  		`<admin@mailserver1>`,
   955  		`<postmaster@localhost>`,
   956  		"<#!$%&'*+-/=?^_`{}|~@example.org>",
   957  		`<"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com>`, // escaped quotes
   958  		`<"()<>[]:,;@\\\"!#$%&'*+-/=?^_{}| ~.a"@example.org>`,                      // escaped backslashes
   959  		`<"Abc\\@def"@example.com>`,
   960  		`<"Joe\\Blow"@example.com>`,
   961  		`<test1/test2=test3@example.com>`,
   962  		`<def!xyz%abc@example.com>`,
   963  		`<_somename@example.com>`,
   964  		`<joe@uk>`,
   965  		`<~@example.com>`,
   966  		`<"..."@test.com>`,
   967  		`<"john..doe"@example.com>`,
   968  		`<"john.doe."@example.com>`,
   969  		`<".john.doe"@example.com>`,
   970  		`<"."@example.com>`,
   971  		`<".."@example.com>`,
   972  		`<"0:"@0>`,
   973  	}
   974  
   975  	for _, test := range tests {
   976  		addr, err := ParseAddress(test)
   977  		if err != nil {
   978  			t.Errorf("Couldn't parse address %s: %s", test, err.Error())
   979  			continue
   980  		}
   981  		str := addr.String()
   982  		addr, err = ParseAddress(str)
   983  		if err != nil {
   984  			t.Errorf("ParseAddr(%q) error: %v", test, err)
   985  			continue
   986  		}
   987  
   988  		if addr.String() != test {
   989  			t.Errorf("String() round-trip = %q; want %q", addr, test)
   990  			continue
   991  		}
   992  
   993  	}
   994  
   995  	// Should fail
   996  	badTests := []string{
   997  		`<Abc.example.com>`,
   998  		`<A@b@c@example.com>`,
   999  		`<a"b(c)d,e:f;g<h>i[j\k]l@example.com>`,
  1000  		`<just"not"right@example.com>`,
  1001  		`<this is"not\allowed@example.com>`,
  1002  		`<this\ still\"not\\allowed@example.com>`,
  1003  		`<john..doe@example.com>`,
  1004  		`<john.doe@example..com>`,
  1005  		`<john.doe@example..com>`,
  1006  		`<john.doe.@example.com>`,
  1007  		`<john.doe.@.example.com>`,
  1008  		`<.john.doe@example.com>`,
  1009  		`<@example.com>`,
  1010  		`<.@example.com>`,
  1011  		`<test@.>`,
  1012  		`< @example.com>`,
  1013  		`<""test""blah""@example.com>`,
  1014  		`<""@0>`,
  1015  	}
  1016  
  1017  	for _, test := range badTests {
  1018  		_, err := ParseAddress(test)
  1019  		if err == nil {
  1020  			t.Errorf("Should have failed to parse address: %s", test)
  1021  			continue
  1022  		}
  1023  
  1024  	}
  1025  
  1026  }
  1027  
  1028  func TestAddressFormattingAndParsing(t *testing.T) {
  1029  	tests := []*Address{
  1030  		{Name: "@lïce", Address: "alice@example.com"},
  1031  		{Name: "Böb O'Connor", Address: "bob@example.com"},
  1032  		{Name: "???", Address: "bob@example.com"},
  1033  		{Name: "Böb ???", Address: "bob@example.com"},
  1034  		{Name: "Böb (Jacöb)", Address: "bob@example.com"},
  1035  		{Name: "à#$%&'(),.:;<>@[]^`{|}~'", Address: "bob@example.com"},
  1036  		// https://golang.org/issue/11292
  1037  		{Name: "\"\\\x1f,\"", Address: "0@0"},
  1038  		// https://golang.org/issue/12782
  1039  		{Name: "naé, mée", Address: "test.mail@gmail.com"},
  1040  	}
  1041  
  1042  	for i, test := range tests {
  1043  		parsed, err := ParseAddress(test.String())
  1044  		if err != nil {
  1045  			t.Errorf("test #%d: ParseAddr(%q) error: %v", i, test.String(), err)
  1046  			continue
  1047  		}
  1048  		if parsed.Name != test.Name {
  1049  			t.Errorf("test #%d: Parsed name = %q; want %q", i, parsed.Name, test.Name)
  1050  		}
  1051  		if parsed.Address != test.Address {
  1052  			t.Errorf("test #%d: Parsed address = %q; want %q", i, parsed.Address, test.Address)
  1053  		}
  1054  	}
  1055  }