github.com/hlts2/go@v0.0.0-20170904000733-812b34efaed8/src/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 TestAddressParsingError(t *testing.T) {
   128  	mustErrTestCases := [...]struct {
   129  		text        string
   130  		wantErrText string
   131  	}{
   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  	}
   143  
   144  	for i, tc := range mustErrTestCases {
   145  		_, err := ParseAddress(tc.text)
   146  		if err == nil || !strings.Contains(err.Error(), tc.wantErrText) {
   147  			t.Errorf(`mail.ParseAddress(%q) #%d want %q, got %v`, tc.text, i, tc.wantErrText, err)
   148  		}
   149  	}
   150  }
   151  
   152  func TestAddressParsing(t *testing.T) {
   153  	tests := []struct {
   154  		addrsStr string
   155  		exp      []*Address
   156  	}{
   157  		// Bare address
   158  		{
   159  			`jdoe@machine.example`,
   160  			[]*Address{{
   161  				Address: "jdoe@machine.example",
   162  			}},
   163  		},
   164  		// RFC 5322, Appendix A.1.1
   165  		{
   166  			`John Doe <jdoe@machine.example>`,
   167  			[]*Address{{
   168  				Name:    "John Doe",
   169  				Address: "jdoe@machine.example",
   170  			}},
   171  		},
   172  		// RFC 5322, Appendix A.1.2
   173  		{
   174  			`"Joe Q. Public" <john.q.public@example.com>`,
   175  			[]*Address{{
   176  				Name:    "Joe Q. Public",
   177  				Address: "john.q.public@example.com",
   178  			}},
   179  		},
   180  		{
   181  			`"John (middle) Doe" <jdoe@machine.example>`,
   182  			[]*Address{{
   183  				Name:    "John (middle) Doe",
   184  				Address: "jdoe@machine.example",
   185  			}},
   186  		},
   187  		{
   188  			`John (middle) Doe <jdoe@machine.example>`,
   189  			[]*Address{{
   190  				Name:    "John (middle) Doe",
   191  				Address: "jdoe@machine.example",
   192  			}},
   193  		},
   194  		{
   195  			`John !@M@! Doe <jdoe@machine.example>`,
   196  			[]*Address{{
   197  				Name:    "John !@M@! Doe",
   198  				Address: "jdoe@machine.example",
   199  			}},
   200  		},
   201  		{
   202  			`"John <middle> Doe" <jdoe@machine.example>`,
   203  			[]*Address{{
   204  				Name:    "John <middle> Doe",
   205  				Address: "jdoe@machine.example",
   206  			}},
   207  		},
   208  		{
   209  			`Mary Smith <mary@x.test>, jdoe@example.org, Who? <one@y.test>`,
   210  			[]*Address{
   211  				{
   212  					Name:    "Mary Smith",
   213  					Address: "mary@x.test",
   214  				},
   215  				{
   216  					Address: "jdoe@example.org",
   217  				},
   218  				{
   219  					Name:    "Who?",
   220  					Address: "one@y.test",
   221  				},
   222  			},
   223  		},
   224  		{
   225  			`<boss@nil.test>, "Giant; \"Big\" Box" <sysservices@example.net>`,
   226  			[]*Address{
   227  				{
   228  					Address: "boss@nil.test",
   229  				},
   230  				{
   231  					Name:    `Giant; "Big" Box`,
   232  					Address: "sysservices@example.net",
   233  				},
   234  			},
   235  		},
   236  		// RFC 5322, Appendix A.6.1
   237  		{
   238  			`Joe Q. Public <john.q.public@example.com>`,
   239  			[]*Address{{
   240  				Name:    "Joe Q. Public",
   241  				Address: "john.q.public@example.com",
   242  			}},
   243  		},
   244  		// RFC 5322, Appendix A.1.3
   245  		// TODO(dsymonds): Group addresses.
   246  
   247  		// RFC 2047 "Q"-encoded ISO-8859-1 address.
   248  		{
   249  			`=?iso-8859-1?q?J=F6rg_Doe?= <joerg@example.com>`,
   250  			[]*Address{
   251  				{
   252  					Name:    `Jörg Doe`,
   253  					Address: "joerg@example.com",
   254  				},
   255  			},
   256  		},
   257  		// RFC 2047 "Q"-encoded US-ASCII address. Dumb but legal.
   258  		{
   259  			`=?us-ascii?q?J=6Frg_Doe?= <joerg@example.com>`,
   260  			[]*Address{
   261  				{
   262  					Name:    `Jorg Doe`,
   263  					Address: "joerg@example.com",
   264  				},
   265  			},
   266  		},
   267  		// RFC 2047 "Q"-encoded UTF-8 address.
   268  		{
   269  			`=?utf-8?q?J=C3=B6rg_Doe?= <joerg@example.com>`,
   270  			[]*Address{
   271  				{
   272  					Name:    `Jörg Doe`,
   273  					Address: "joerg@example.com",
   274  				},
   275  			},
   276  		},
   277  		// RFC 2047 "Q"-encoded UTF-8 address with multiple encoded-words.
   278  		{
   279  			`=?utf-8?q?J=C3=B6rg?=  =?utf-8?q?Doe?= <joerg@example.com>`,
   280  			[]*Address{
   281  				{
   282  					Name:    `JörgDoe`,
   283  					Address: "joerg@example.com",
   284  				},
   285  			},
   286  		},
   287  		// RFC 2047, Section 8.
   288  		{
   289  			`=?ISO-8859-1?Q?Andr=E9?= Pirard <PIRARD@vm1.ulg.ac.be>`,
   290  			[]*Address{
   291  				{
   292  					Name:    `André Pirard`,
   293  					Address: "PIRARD@vm1.ulg.ac.be",
   294  				},
   295  			},
   296  		},
   297  		// Custom example of RFC 2047 "B"-encoded ISO-8859-1 address.
   298  		{
   299  			`=?ISO-8859-1?B?SvZyZw==?= <joerg@example.com>`,
   300  			[]*Address{
   301  				{
   302  					Name:    `Jörg`,
   303  					Address: "joerg@example.com",
   304  				},
   305  			},
   306  		},
   307  		// Custom example of RFC 2047 "B"-encoded UTF-8 address.
   308  		{
   309  			`=?UTF-8?B?SsO2cmc=?= <joerg@example.com>`,
   310  			[]*Address{
   311  				{
   312  					Name:    `Jörg`,
   313  					Address: "joerg@example.com",
   314  				},
   315  			},
   316  		},
   317  		// Custom example with "." in name. For issue 4938
   318  		{
   319  			`Asem H. <noreply@example.com>`,
   320  			[]*Address{
   321  				{
   322  					Name:    `Asem H.`,
   323  					Address: "noreply@example.com",
   324  				},
   325  			},
   326  		},
   327  		// RFC 6532 3.2.3, qtext /= UTF8-non-ascii
   328  		{
   329  			`"Gø Pher" <gopher@example.com>`,
   330  			[]*Address{
   331  				{
   332  					Name:    `Gø Pher`,
   333  					Address: "gopher@example.com",
   334  				},
   335  			},
   336  		},
   337  		// RFC 6532 3.2, atext /= UTF8-non-ascii
   338  		{
   339  			`µ <micro@example.com>`,
   340  			[]*Address{
   341  				{
   342  					Name:    `µ`,
   343  					Address: "micro@example.com",
   344  				},
   345  			},
   346  		},
   347  		// RFC 6532 3.2.2, local address parts allow UTF-8
   348  		{
   349  			`Micro <µ@example.com>`,
   350  			[]*Address{
   351  				{
   352  					Name:    `Micro`,
   353  					Address: "µ@example.com",
   354  				},
   355  			},
   356  		},
   357  		// RFC 6532 3.2.4, domains parts allow UTF-8
   358  		{
   359  			`Micro <micro@µ.example.com>`,
   360  			[]*Address{
   361  				{
   362  					Name:    `Micro`,
   363  					Address: "micro@µ.example.com",
   364  				},
   365  			},
   366  		},
   367  		// Issue 14866
   368  		{
   369  			`"" <emptystring@example.com>`,
   370  			[]*Address{
   371  				{
   372  					Name:    "",
   373  					Address: "emptystring@example.com",
   374  				},
   375  			},
   376  		},
   377  	}
   378  	for _, test := range tests {
   379  		if len(test.exp) == 1 {
   380  			addr, err := ParseAddress(test.addrsStr)
   381  			if err != nil {
   382  				t.Errorf("Failed parsing (single) %q: %v", test.addrsStr, err)
   383  				continue
   384  			}
   385  			if !reflect.DeepEqual([]*Address{addr}, test.exp) {
   386  				t.Errorf("Parse (single) of %q: got %+v, want %+v", test.addrsStr, addr, test.exp)
   387  			}
   388  		}
   389  
   390  		addrs, err := ParseAddressList(test.addrsStr)
   391  		if err != nil {
   392  			t.Errorf("Failed parsing (list) %q: %v", test.addrsStr, err)
   393  			continue
   394  		}
   395  		if !reflect.DeepEqual(addrs, test.exp) {
   396  			t.Errorf("Parse (list) of %q: got %+v, want %+v", test.addrsStr, addrs, test.exp)
   397  		}
   398  	}
   399  }
   400  
   401  func TestAddressParser(t *testing.T) {
   402  	tests := []struct {
   403  		addrsStr string
   404  		exp      []*Address
   405  	}{
   406  		// Bare address
   407  		{
   408  			`jdoe@machine.example`,
   409  			[]*Address{{
   410  				Address: "jdoe@machine.example",
   411  			}},
   412  		},
   413  		// RFC 5322, Appendix A.1.1
   414  		{
   415  			`John Doe <jdoe@machine.example>`,
   416  			[]*Address{{
   417  				Name:    "John Doe",
   418  				Address: "jdoe@machine.example",
   419  			}},
   420  		},
   421  		// RFC 5322, Appendix A.1.2
   422  		{
   423  			`"Joe Q. Public" <john.q.public@example.com>`,
   424  			[]*Address{{
   425  				Name:    "Joe Q. Public",
   426  				Address: "john.q.public@example.com",
   427  			}},
   428  		},
   429  		{
   430  			`Mary Smith <mary@x.test>, jdoe@example.org, Who? <one@y.test>`,
   431  			[]*Address{
   432  				{
   433  					Name:    "Mary Smith",
   434  					Address: "mary@x.test",
   435  				},
   436  				{
   437  					Address: "jdoe@example.org",
   438  				},
   439  				{
   440  					Name:    "Who?",
   441  					Address: "one@y.test",
   442  				},
   443  			},
   444  		},
   445  		{
   446  			`<boss@nil.test>, "Giant; \"Big\" Box" <sysservices@example.net>`,
   447  			[]*Address{
   448  				{
   449  					Address: "boss@nil.test",
   450  				},
   451  				{
   452  					Name:    `Giant; "Big" Box`,
   453  					Address: "sysservices@example.net",
   454  				},
   455  			},
   456  		},
   457  		// RFC 2047 "Q"-encoded ISO-8859-1 address.
   458  		{
   459  			`=?iso-8859-1?q?J=F6rg_Doe?= <joerg@example.com>`,
   460  			[]*Address{
   461  				{
   462  					Name:    `Jörg Doe`,
   463  					Address: "joerg@example.com",
   464  				},
   465  			},
   466  		},
   467  		// RFC 2047 "Q"-encoded US-ASCII address. Dumb but legal.
   468  		{
   469  			`=?us-ascii?q?J=6Frg_Doe?= <joerg@example.com>`,
   470  			[]*Address{
   471  				{
   472  					Name:    `Jorg Doe`,
   473  					Address: "joerg@example.com",
   474  				},
   475  			},
   476  		},
   477  		// RFC 2047 "Q"-encoded ISO-8859-15 address.
   478  		{
   479  			`=?ISO-8859-15?Q?J=F6rg_Doe?= <joerg@example.com>`,
   480  			[]*Address{
   481  				{
   482  					Name:    `Jörg Doe`,
   483  					Address: "joerg@example.com",
   484  				},
   485  			},
   486  		},
   487  		// RFC 2047 "B"-encoded windows-1252 address.
   488  		{
   489  			`=?windows-1252?q?Andr=E9?= Pirard <PIRARD@vm1.ulg.ac.be>`,
   490  			[]*Address{
   491  				{
   492  					Name:    `André Pirard`,
   493  					Address: "PIRARD@vm1.ulg.ac.be",
   494  				},
   495  			},
   496  		},
   497  		// Custom example of RFC 2047 "B"-encoded ISO-8859-15 address.
   498  		{
   499  			`=?ISO-8859-15?B?SvZyZw==?= <joerg@example.com>`,
   500  			[]*Address{
   501  				{
   502  					Name:    `Jörg`,
   503  					Address: "joerg@example.com",
   504  				},
   505  			},
   506  		},
   507  		// Custom example of RFC 2047 "B"-encoded UTF-8 address.
   508  		{
   509  			`=?UTF-8?B?SsO2cmc=?= <joerg@example.com>`,
   510  			[]*Address{
   511  				{
   512  					Name:    `Jörg`,
   513  					Address: "joerg@example.com",
   514  				},
   515  			},
   516  		},
   517  		// Custom example with "." in name. For issue 4938
   518  		{
   519  			`Asem H. <noreply@example.com>`,
   520  			[]*Address{
   521  				{
   522  					Name:    `Asem H.`,
   523  					Address: "noreply@example.com",
   524  				},
   525  			},
   526  		},
   527  	}
   528  
   529  	ap := AddressParser{WordDecoder: &mime.WordDecoder{
   530  		CharsetReader: func(charset string, input io.Reader) (io.Reader, error) {
   531  			in, err := ioutil.ReadAll(input)
   532  			if err != nil {
   533  				return nil, err
   534  			}
   535  
   536  			switch charset {
   537  			case "iso-8859-15":
   538  				in = bytes.Replace(in, []byte("\xf6"), []byte("ö"), -1)
   539  			case "windows-1252":
   540  				in = bytes.Replace(in, []byte("\xe9"), []byte("é"), -1)
   541  			}
   542  
   543  			return bytes.NewReader(in), nil
   544  		},
   545  	}}
   546  
   547  	for _, test := range tests {
   548  		if len(test.exp) == 1 {
   549  			addr, err := ap.Parse(test.addrsStr)
   550  			if err != nil {
   551  				t.Errorf("Failed parsing (single) %q: %v", test.addrsStr, err)
   552  				continue
   553  			}
   554  			if !reflect.DeepEqual([]*Address{addr}, test.exp) {
   555  				t.Errorf("Parse (single) of %q: got %+v, want %+v", test.addrsStr, addr, test.exp)
   556  			}
   557  		}
   558  
   559  		addrs, err := ap.ParseList(test.addrsStr)
   560  		if err != nil {
   561  			t.Errorf("Failed parsing (list) %q: %v", test.addrsStr, err)
   562  			continue
   563  		}
   564  		if !reflect.DeepEqual(addrs, test.exp) {
   565  			t.Errorf("Parse (list) of %q: got %+v, want %+v", test.addrsStr, addrs, test.exp)
   566  		}
   567  	}
   568  }
   569  
   570  func TestAddressString(t *testing.T) {
   571  	tests := []struct {
   572  		addr *Address
   573  		exp  string
   574  	}{
   575  		{
   576  			&Address{Address: "bob@example.com"},
   577  			"<bob@example.com>",
   578  		},
   579  		{ // quoted local parts: RFC 5322, 3.4.1. and 3.2.4.
   580  			&Address{Address: `my@idiot@address@example.com`},
   581  			`<"my@idiot@address"@example.com>`,
   582  		},
   583  		{ // quoted local parts
   584  			&Address{Address: ` @example.com`},
   585  			`<" "@example.com>`,
   586  		},
   587  		{
   588  			&Address{Name: "Bob", Address: "bob@example.com"},
   589  			`"Bob" <bob@example.com>`,
   590  		},
   591  		{
   592  			// note the ö (o with an umlaut)
   593  			&Address{Name: "Böb", Address: "bob@example.com"},
   594  			`=?utf-8?q?B=C3=B6b?= <bob@example.com>`,
   595  		},
   596  		{
   597  			&Address{Name: "Bob Jane", Address: "bob@example.com"},
   598  			`"Bob Jane" <bob@example.com>`,
   599  		},
   600  		{
   601  			&Address{Name: "Böb Jacöb", Address: "bob@example.com"},
   602  			`=?utf-8?q?B=C3=B6b_Jac=C3=B6b?= <bob@example.com>`,
   603  		},
   604  		{ // https://golang.org/issue/12098
   605  			&Address{Name: "Rob", Address: ""},
   606  			`"Rob" <@>`,
   607  		},
   608  		{ // https://golang.org/issue/12098
   609  			&Address{Name: "Rob", Address: "@"},
   610  			`"Rob" <@>`,
   611  		},
   612  		{
   613  			&Address{Name: "Böb, Jacöb", Address: "bob@example.com"},
   614  			`=?utf-8?b?QsO2YiwgSmFjw7Zi?= <bob@example.com>`,
   615  		},
   616  		{
   617  			&Address{Name: "=??Q?x?=", Address: "hello@world.com"},
   618  			`"=??Q?x?=" <hello@world.com>`,
   619  		},
   620  		{
   621  			&Address{Name: "=?hello", Address: "hello@world.com"},
   622  			`"=?hello" <hello@world.com>`,
   623  		},
   624  		{
   625  			&Address{Name: "world?=", Address: "hello@world.com"},
   626  			`"world?=" <hello@world.com>`,
   627  		},
   628  		{
   629  			// should q-encode even for invalid utf-8.
   630  			&Address{Name: string([]byte{0xed, 0xa0, 0x80}), Address: "invalid-utf8@example.net"},
   631  			"=?utf-8?q?=ED=A0=80?= <invalid-utf8@example.net>",
   632  		},
   633  	}
   634  	for _, test := range tests {
   635  		s := test.addr.String()
   636  		if s != test.exp {
   637  			t.Errorf("Address%+v.String() = %v, want %v", *test.addr, s, test.exp)
   638  			continue
   639  		}
   640  
   641  		// Check round-trip.
   642  		if test.addr.Address != "" && test.addr.Address != "@" {
   643  			a, err := ParseAddress(test.exp)
   644  			if err != nil {
   645  				t.Errorf("ParseAddress(%#q): %v", test.exp, err)
   646  				continue
   647  			}
   648  			if a.Name != test.addr.Name || a.Address != test.addr.Address {
   649  				t.Errorf("ParseAddress(%#q) = %#v, want %#v", test.exp, a, test.addr)
   650  			}
   651  		}
   652  	}
   653  }
   654  
   655  // Check if all valid addresses can be parsed, formatted and parsed again
   656  func TestAddressParsingAndFormatting(t *testing.T) {
   657  
   658  	// Should pass
   659  	tests := []string{
   660  		`<Bob@example.com>`,
   661  		`<bob.bob@example.com>`,
   662  		`<".bob"@example.com>`,
   663  		`<" "@example.com>`,
   664  		`<some.mail-with-dash@example.com>`,
   665  		`<"dot.and space"@example.com>`,
   666  		`<"very.unusual.@.unusual.com"@example.com>`,
   667  		`<admin@mailserver1>`,
   668  		`<postmaster@localhost>`,
   669  		"<#!$%&'*+-/=?^_`{}|~@example.org>",
   670  		`<"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com>`, // escaped quotes
   671  		`<"()<>[]:,;@\\\"!#$%&'*+-/=?^_{}| ~.a"@example.org>`,                      // escaped backslashes
   672  		`<"Abc\\@def"@example.com>`,
   673  		`<"Joe\\Blow"@example.com>`,
   674  		`<test1/test2=test3@example.com>`,
   675  		`<def!xyz%abc@example.com>`,
   676  		`<_somename@example.com>`,
   677  		`<joe@uk>`,
   678  		`<~@example.com>`,
   679  		`<"..."@test.com>`,
   680  		`<"john..doe"@example.com>`,
   681  		`<"john.doe."@example.com>`,
   682  		`<".john.doe"@example.com>`,
   683  		`<"."@example.com>`,
   684  		`<".."@example.com>`,
   685  		`<"0:"@0>`,
   686  	}
   687  
   688  	for _, test := range tests {
   689  		addr, err := ParseAddress(test)
   690  		if err != nil {
   691  			t.Errorf("Couldn't parse address %s: %s", test, err.Error())
   692  			continue
   693  		}
   694  		str := addr.String()
   695  		addr, err = ParseAddress(str)
   696  		if err != nil {
   697  			t.Errorf("ParseAddr(%q) error: %v", test, err)
   698  			continue
   699  		}
   700  
   701  		if addr.String() != test {
   702  			t.Errorf("String() round-trip = %q; want %q", addr, test)
   703  			continue
   704  		}
   705  
   706  	}
   707  
   708  	// Should fail
   709  	badTests := []string{
   710  		`<Abc.example.com>`,
   711  		`<A@b@c@example.com>`,
   712  		`<a"b(c)d,e:f;g<h>i[j\k]l@example.com>`,
   713  		`<just"not"right@example.com>`,
   714  		`<this is"not\allowed@example.com>`,
   715  		`<this\ still\"not\\allowed@example.com>`,
   716  		`<john..doe@example.com>`,
   717  		`<john.doe@example..com>`,
   718  		`<john.doe@example..com>`,
   719  		`<john.doe.@example.com>`,
   720  		`<john.doe.@.example.com>`,
   721  		`<.john.doe@example.com>`,
   722  		`<@example.com>`,
   723  		`<.@example.com>`,
   724  		`<test@.>`,
   725  		`< @example.com>`,
   726  		`<""test""blah""@example.com>`,
   727  		`<""@0>`,
   728  	}
   729  
   730  	for _, test := range badTests {
   731  		_, err := ParseAddress(test)
   732  		if err == nil {
   733  			t.Errorf("Should have failed to parse address: %s", test)
   734  			continue
   735  		}
   736  
   737  	}
   738  
   739  }
   740  
   741  func TestAddressFormattingAndParsing(t *testing.T) {
   742  	tests := []*Address{
   743  		{Name: "@lïce", Address: "alice@example.com"},
   744  		{Name: "Böb O'Connor", Address: "bob@example.com"},
   745  		{Name: "???", Address: "bob@example.com"},
   746  		{Name: "Böb ???", Address: "bob@example.com"},
   747  		{Name: "Böb (Jacöb)", Address: "bob@example.com"},
   748  		{Name: "à#$%&'(),.:;<>@[]^`{|}~'", Address: "bob@example.com"},
   749  		// https://golang.org/issue/11292
   750  		{Name: "\"\\\x1f,\"", Address: "0@0"},
   751  		// https://golang.org/issue/12782
   752  		{Name: "naé, mée", Address: "test.mail@gmail.com"},
   753  	}
   754  
   755  	for i, test := range tests {
   756  		parsed, err := ParseAddress(test.String())
   757  		if err != nil {
   758  			t.Errorf("test #%d: ParseAddr(%q) error: %v", i, test.String(), err)
   759  			continue
   760  		}
   761  		if parsed.Name != test.Name {
   762  			t.Errorf("test #%d: Parsed name = %q; want %q", i, parsed.Name, test.Name)
   763  		}
   764  		if parsed.Address != test.Address {
   765  			t.Errorf("test #%d: Parsed address = %q; want %q", i, parsed.Address, test.Address)
   766  		}
   767  	}
   768  }