envelope_test.go 5.4 KB

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