envelope_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package mail
  2. import (
  3. "io"
  4. "io/ioutil"
  5. "strings"
  6. "testing"
  7. )
  8. // Test MimeHeader decoding, not using iconv
  9. func TestMimeHeaderDecode(t *testing.T) {
  10. /*
  11. Normally this would fail if not using iconv
  12. str := MimeHeaderDecode("=?ISO-2022-JP?B?GyRCIVo9dztSOWJAOCVBJWMbKEI=?=")
  13. if i := strings.Index(str, "【女子高生チャ"); i != 0 {
  14. t.Error("expecting 【女子高生チャ, got:", str)
  15. }
  16. */
  17. str := MimeHeaderDecode("=?utf-8?B?55So5oi34oCcRXBpZGVtaW9sb2d5IGluIG51cnNpbmcgYW5kIGg=?= =?utf-8?B?ZWFsdGggY2FyZSBlQm9vayByZWFkL2F1ZGlvIGlkOm8=?= =?utf-8?B?cTNqZWVr4oCd5Zyo572R56uZ4oCcU1BZ5Lit5paH5a6Y5pa5572R56uZ4oCd?= =?utf-8?B?55qE5biQ5Y+36K+m5oOF?=")
  18. if i := strings.Index(str, "用户“Epidemiology in nursing and health care eBook read/audio id:oq3jeek”在网站“SPY中文官方网站”的帐号详情"); i != 0 {
  19. t.Error("\nexpecting \n用户“Epidemiology in nursing and h ealth care eBook read/audio id:oq3jeek”在网站“SPY中文官方网站”的帐号详情\n got:\n", str)
  20. }
  21. str = MimeHeaderDecode("=?ISO-8859-1?Q?Andr=E9?= Pirard <[email protected]>")
  22. if strings.Index(str, "André Pirard") != 0 {
  23. t.Error("expecting André Pirard, got:", str)
  24. }
  25. }
  26. // TestMimeHeaderDecodeNone tests strings without any encoded words
  27. func TestMimeHeaderDecodeNone(t *testing.T) {
  28. // in the best case, there will be nothing to decode
  29. str := MimeHeaderDecode("Andre Pirard <[email protected]>")
  30. if strings.Index(str, "Andre Pirard") != 0 {
  31. t.Error("expecting Andre Pirard, got:", str)
  32. }
  33. }
  34. func TestAddressPostmaster(t *testing.T) {
  35. addr := &Address{User: "postmaster"}
  36. str := addr.String()
  37. if str != "postmaster" {
  38. t.Error("it was not postmaster,", str)
  39. }
  40. }
  41. func TestAddressNull(t *testing.T) {
  42. addr := &Address{NullPath: true}
  43. str := addr.String()
  44. if str != "" {
  45. t.Error("it was not empty", str)
  46. }
  47. }
  48. func TestNewAddress(t *testing.T) {
  49. addr, err := NewAddress("<hoop>")
  50. if err == nil {
  51. t.Error("there should be an error:", err)
  52. }
  53. addr, err = NewAddress(`Gogh Fir <[email protected]>`)
  54. if err != nil {
  55. t.Error("there should be no error:", addr.Host, err)
  56. }
  57. }
  58. func TestQuotedAddress(t *testing.T) {
  59. str := `<" yo-- man wazz'''up? surprise \surprise, this is [email protected] "@example.com>`
  60. //str = `<"post\master">`
  61. addr, err := NewAddress(str)
  62. if err != nil {
  63. t.Error("there should be no error:", err)
  64. }
  65. str = addr.String()
  66. // in this case, string should remove the unnecessary escape
  67. if strings.Contains(str, "\\surprise") {
  68. t.Error("there should be no \\surprise:", err)
  69. }
  70. }
  71. func TestAddressWithIP(t *testing.T) {
  72. str := `<" yo-- man wazz'''up? surprise \surprise, this is [email protected] "@[64.233.160.71]>`
  73. addr, err := NewAddress(str)
  74. if err != nil {
  75. t.Error("there should be no error:", err)
  76. } else if addr.IP == nil {
  77. t.Error("expecting the address host to be an IP")
  78. }
  79. }
  80. func TestEnvelope(t *testing.T) {
  81. e := NewEnvelope("127.0.0.1", 22)
  82. e.QueuedId = "abc123"
  83. e.Helo = "helo.example.com"
  84. e.MailFrom = Address{User: "test", Host: "example.com"}
  85. e.TLS = true
  86. e.RemoteIP = "222.111.233.121"
  87. to := Address{User: "test", Host: "example.com"}
  88. e.PushRcpt(to)
  89. if to.String() != "[email protected]" {
  90. t.Error("to does not equal [email protected], it was:", to.String())
  91. }
  92. e.Data.WriteString("Subject: Test\n\nThis is a test nbnb nbnb hgghgh nnnbnb nbnbnb nbnbn.")
  93. addHead := "Delivered-To: " + to.String() + "\n"
  94. addHead += "Received: from " + e.Helo + " (" + e.Helo + " [" + e.RemoteIP + "])\n"
  95. e.DeliveryHeader = addHead
  96. r := e.NewReader()
  97. data, _ := ioutil.ReadAll(r)
  98. if len(data) != e.Len() {
  99. t.Error("e.Len() is incorrect, it shown ", e.Len(), " but we wanted ", len(data))
  100. }
  101. if err := e.ParseHeaders(); err != nil && err != io.EOF {
  102. t.Error("cannot parse headers:", err)
  103. return
  104. }
  105. if e.Subject != "Test" {
  106. t.Error("Subject expecting: Test, got:", e.Subject)
  107. }
  108. }
  109. func TestEncodedWordAhead(t *testing.T) {
  110. str := "=?ISO-8859-1?Q?Andr=E9?= Pirard <[email protected]>"
  111. if hasEncodedWordAhead(str, 24) != -1 {
  112. t.Error("expecting no encoded word ahead")
  113. }
  114. str = "=?ISO-8859-1?Q?Andr=E9?= ="
  115. if hasEncodedWordAhead(str, 24) != -1 {
  116. t.Error("expecting no encoded word ahead")
  117. }
  118. str = "=?ISO-8859-1?Q?Andr=E9?= =?ISO-8859-1?Q?Andr=E9?="
  119. if hasEncodedWordAhead(str, 24) == -1 {
  120. t.Error("expecting an encoded word ahead")
  121. }
  122. }