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