mimeparts_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package email
  2. import (
  3. "bytes"
  4. "strings"
  5. "testing"
  6. "github.com/emersion/go-imap/v2"
  7. gomessage "github.com/emersion/go-message"
  8. )
  9. func TestDecodeTransfer(t *testing.T) {
  10. tests := []struct {
  11. name string
  12. input string
  13. encoding string
  14. partial bool
  15. want string
  16. }{
  17. {"base64", "SGVsbG8gV29ybGQ=", "base64", false, "Hello World"},
  18. {"base64 with line breaks", "SGVs\r\nbG8g\r\nV29y\r\nbGQ=", "BASE64", false, "Hello World"},
  19. {"base64 truncated preview", "SGVsbG8gV29yb", "base64", true, "Hello Wor"},
  20. {"quoted printable", "caf=C3=A9 =\r\nsoft", "quoted-printable", false, "café soft"},
  21. {"plain passes through", "as is", "7bit", false, "as is"},
  22. }
  23. for _, test := range tests {
  24. t.Run(test.name, func(t *testing.T) {
  25. if got := string(decodeTransfer([]byte(test.input), test.encoding, test.partial)); got != test.want {
  26. t.Errorf("decodeTransfer = %q, want %q", got, test.want)
  27. }
  28. })
  29. }
  30. }
  31. func TestDecodeCharset(t *testing.T) {
  32. tests := []struct {
  33. name string
  34. input []byte
  35. charset string
  36. want string
  37. }{
  38. {"utf-8", []byte("中文"), "UTF-8", "中文"},
  39. {"gb2312", []byte{0xd6, 0xd0, 0xce, 0xc4}, "gb2312", "中文"},
  40. {"big5", []byte{0xa4, 0xa4, 0xa4, 0xe5}, "big5", "中文"},
  41. {"latin1", []byte{0x63, 0x61, 0x66, 0xe9}, "iso-8859-1", "café"},
  42. {"unknown charset is kept", []byte("plain"), "x-unknown", "plain"},
  43. }
  44. for _, test := range tests {
  45. t.Run(test.name, func(t *testing.T) {
  46. if got := decodeCharset(test.input, test.charset); got != test.want {
  47. t.Errorf("decodeCharset = %q, want %q", got, test.want)
  48. }
  49. })
  50. }
  51. }
  52. func TestRFC2231Filenames(t *testing.T) {
  53. tests := []struct {
  54. name string
  55. params map[string]string
  56. want string
  57. }{
  58. {"plain", map[string]string{"filename": "a.pdf"}, "a.pdf"},
  59. {"extended", map[string]string{"filename*": "utf-8''%E5%A0%B1%E5%91%8A.pdf"}, "報告.pdf"},
  60. {"continuations", map[string]string{"filename*0*": "utf-8''%E5%A0%B1", "filename*1*": "%E5%91%8A", "filename*2": ".pdf"}, "報告.pdf"},
  61. }
  62. for _, test := range tests {
  63. t.Run(test.name, func(t *testing.T) {
  64. if got := rfc2231Param(test.params, "filename"); got != test.want {
  65. t.Errorf("rfc2231Param = %q, want %q", got, test.want)
  66. }
  67. })
  68. }
  69. }
  70. func TestSanitizeFilename(t *testing.T) {
  71. tests := map[string]string{
  72. "../../etc/passwd": "_.._etc_passwd",
  73. "a:b*c?.txt": "a_b_c_.txt",
  74. " .hidden ": "hidden",
  75. "": "attachment",
  76. "報告 (final).docx": "報告 (final).docx",
  77. }
  78. for input, want := range tests {
  79. if got := sanitizeFilename(input); got != want {
  80. t.Errorf("sanitizeFilename(%q) = %q, want %q", input, got, want)
  81. }
  82. }
  83. }
  84. func TestSelectDisplayParts(t *testing.T) {
  85. raw := "Content-Type: multipart/signed; boundary=s; protocol=\"application/pkcs7-signature\"\r\n\r\n" +
  86. "--s\r\nContent-Type: multipart/alternative; boundary=a\r\n\r\n" +
  87. "--a\r\nContent-Type: text/plain\r\n\r\nplain\r\n" +
  88. "--a\r\nContent-Type: text/watch-html\r\n\r\nwatch\r\n" +
  89. "--a\r\nContent-Type: text/html\r\n\r\n<b>rich</b>\r\n" +
  90. "--a--\r\n" +
  91. "--s\r\nContent-Type: application/pkcs7-signature; name=smime.p7s\r\nContent-Disposition: attachment; filename=smime.p7s\r\n\r\nsig\r\n" +
  92. "--s--\r\n"
  93. entity, err := gomessage.Read(strings.NewReader(raw))
  94. if err != nil {
  95. t.Fatalf("parse: %v", err)
  96. }
  97. parts := selectDisplayParts(treeFromEntity(entity))
  98. if parts.html == nil || !bytes.Contains(parts.html.body, []byte("rich")) {
  99. t.Errorf("html part not selected")
  100. }
  101. if parts.text == nil || !bytes.Contains(parts.text.body, []byte("plain")) {
  102. t.Errorf("text alternative not kept")
  103. }
  104. if !parts.signed {
  105. t.Errorf("signed message not detected")
  106. }
  107. if len(parts.attachments) != 0 {
  108. t.Errorf("signature should not be listed as an attachment: %+v", parts.attachments)
  109. }
  110. }
  111. func TestTreeFromBodyStructurePaths(t *testing.T) {
  112. structure := &imap.BodyStructureMultiPart{
  113. Subtype: "mixed",
  114. Children: []imap.BodyStructure{
  115. &imap.BodyStructureMultiPart{
  116. Subtype: "alternative",
  117. Children: []imap.BodyStructure{
  118. &imap.BodyStructureSinglePart{Type: "text", Subtype: "plain", Params: map[string]string{"charset": "utf-8"}},
  119. &imap.BodyStructureSinglePart{Type: "text", Subtype: "html"},
  120. },
  121. },
  122. &imap.BodyStructureSinglePart{
  123. Type: "application", Subtype: "pdf", Encoding: "base64", Size: 400,
  124. Extended: &imap.BodyStructureSinglePartExt{Disposition: &imap.BodyStructureDisposition{Value: "attachment", Params: map[string]string{"filename": "a.pdf"}}},
  125. },
  126. },
  127. }
  128. tree := treeFromBodyStructure(structure)
  129. parts := selectDisplayParts(tree)
  130. if parts.text == nil || parts.text.Path != "1.1" || parts.html == nil || parts.html.Path != "1.2" {
  131. t.Fatalf("unexpected display parts %+v", parts)
  132. }
  133. attachments := attachmentInfo(parts.attachments)
  134. if len(attachments) != 1 || attachments[0].ID != "2" || attachments[0].Size != 300 {
  135. t.Errorf("attachments = %+v", attachments)
  136. }
  137. if !hasRealAttachments(tree) {
  138. t.Errorf("hasRealAttachments = false")
  139. }
  140. single := treeFromBodyStructure(&imap.BodyStructureSinglePart{Type: "text", Subtype: "plain"})
  141. if single.Path != "1" {
  142. t.Errorf("single part path = %q, want 1", single.Path)
  143. }
  144. }
  145. func TestDuplicateAttachmentNames(t *testing.T) {
  146. parts := []*mimePart{
  147. {Path: "2", Type: "image", Subtype: "png", DispParams: map[string]string{"filename": "photo.png"}},
  148. {Path: "3", Type: "image", Subtype: "png", DispParams: map[string]string{"filename": "photo.png"}},
  149. }
  150. info := attachmentInfo(parts)
  151. if info[0].Filename != "photo.png" || info[1].Filename != "photo (2).png" {
  152. t.Errorf("names = %s, %s", info[0].Filename, info[1].Filename)
  153. }
  154. }
  155. func TestPriorityParsing(t *testing.T) {
  156. tests := []struct {
  157. xPriority, importance, priority string
  158. want int
  159. }{
  160. {"1 (Highest)", "", "", 1},
  161. {"2", "", "", 1},
  162. {"3 (Normal)", "", "", 3},
  163. {"5", "", "", 5},
  164. {"", "High", "", 1},
  165. {"", "low", "", 5},
  166. {"", "", "urgent", 1},
  167. {"", "", "", 3},
  168. }
  169. for _, test := range tests {
  170. if got := priorityFromFields(test.xPriority, test.importance, test.priority); got != test.want {
  171. t.Errorf("priority(%q,%q,%q) = %d, want %d", test.xPriority, test.importance, test.priority, got, test.want)
  172. }
  173. }
  174. }
  175. func TestParseAuthResults(t *testing.T) {
  176. results := parseAuthResults([]string{"mx.google.com; dkim=fail header.d=a; dkim=pass header.d=b; spf=softfail smtp.mailfrom=c; dmarc=fail (p=NONE)"})
  177. if results == nil || results.DKIM != "pass" || results.SPF != "softfail" || results.DMARC != "fail" {
  178. t.Errorf("parseAuthResults = %+v", results)
  179. }
  180. if parseAuthResults(nil) != nil {
  181. t.Errorf("no header should give nil")
  182. }
  183. }
  184. func TestBaseSubject(t *testing.T) {
  185. tests := map[string]string{
  186. "Re: Re: Hello": "Hello",
  187. "FW: Fwd: Report": "Report",
  188. "回复:会议": "会议",
  189. "Re[2]: Status": "Status",
  190. "Regarding budgets": "Regarding budgets",
  191. }
  192. for input, want := range tests {
  193. if got := baseSubject(input); got != want {
  194. t.Errorf("baseSubject(%q) = %q, want %q", input, got, want)
  195. }
  196. }
  197. }