...

Source file src/net/mail/message_test.go

Documentation: net/mail

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

View as plain text