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