address_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package smtp
  2. import (
  3. "testing"
  4. )
  5. func TestParseRFC5322(t *testing.T) {
  6. var s RFC5322
  7. if _, err := s.Address([]byte("\"Mike Jones\" <[email protected]>")); err != nil {
  8. t.Error(err)
  9. }
  10. // parse a simple address
  11. if a, err := s.Address([]byte("[email protected]")); err != nil {
  12. t.Error(err)
  13. } else {
  14. if len(a.List) != 1 {
  15. t.Error("expecting 1 address")
  16. } else {
  17. // display name should be empty
  18. }
  19. }
  20. }
  21. func TestParseRFC5322Decoder(t *testing.T) {
  22. var s RFC5322
  23. if _, err := s.Address([]byte("=?ISO-8859-1?Q?Andr=E9?= =?ISO-8859-1?Q?Andr=E9?= <[email protected]>")); err != nil {
  24. t.Error(err)
  25. }
  26. }
  27. func TestParseRFC5322IP(t *testing.T) {
  28. var s RFC5322
  29. // this is an incorrect IPv6 address
  30. if _, err := s.Address([]byte("\"Mike Jones\" <\"testing 123\"@[IPv6:IPv6:2001:db8::1]>")); err == nil {
  31. t.Error("Expecting error, because Ip address was wrong")
  32. }
  33. // this one is correct, with quoted display name and quoted local-part
  34. if a, err := s.Address([]byte("\"Mike Jones\" <\"testing 123\"@[IPv6:2001:db8::1]>")); err != nil {
  35. t.Error(err)
  36. } else {
  37. if len(a.List) != 1 {
  38. t.Error("expecting 1 address, but got", len(a.List))
  39. } else {
  40. if a.List[0].DisplayNameQuoted == false {
  41. t.Error(".List[0].DisplayNameQuoted is false, expecting true")
  42. }
  43. if a.List[0].LocalPartQuoted == false {
  44. t.Error(".List[0].LocalPartQuotes is false, expecting true")
  45. }
  46. if a.List[0].IP == nil {
  47. t.Error("a.List[0].IP should not be nil")
  48. }
  49. if a.List[0].Domain != "2001:db8::1" {
  50. t.Error("a.List[0].Domain should be, but got", a.List[0].Domain)
  51. }
  52. }
  53. }
  54. }
  55. func TestParseRFC5322Group(t *testing.T) {
  56. // A Group:Ed Jones <[email protected]>,[email protected],John <[email protected]>;
  57. var s RFC5322
  58. if a, err := s.Address([]byte("A Group:Ed Jones <[email protected]>,[email protected],John <[email protected]> , \"te \\\" st\"<[email protected]> ;")); err != nil {
  59. t.Error(err)
  60. } else {
  61. if a.Group != "A Group" {
  62. t.Error("expecting a.Group to be \"A Group\" but got:", a.Group)
  63. }
  64. if len(a.List) != 4 {
  65. t.Error("expecting 4 addresses, but got", len(a.List))
  66. } else {
  67. if a.List[0].DisplayName != "Ed Jones" {
  68. t.Error("expecting a.List[0].DisplayName 'Ed Jones' but got:", a.List[0].DisplayName)
  69. }
  70. if a.List[0].LocalPart != "c" {
  71. t.Error("expecting a.List[0].LocalPart 'c' but got:", a.List[0].LocalPart)
  72. }
  73. if a.List[0].Domain != "a.test" {
  74. t.Error("expecting a.List[0].Domain 'a.test' but got:", a.List[0].Domain)
  75. }
  76. }
  77. }
  78. }