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