blob: ac663b9c430637334691eba9d388c853a15ff83c [file] [log] [blame]
Dusan Kasanc2129de2017-04-13 10:42:21 +02001package parsemail
Dusan Kasan17e497e2017-04-10 22:44:22 +02002
3import (
Dusan Kasan17e497e2017-04-10 22:44:22 +02004 "encoding/base64"
Obi Symons333687f2020-04-05 14:31:35 +10005 "fmt"
Dusan Kasan17e497e2017-04-10 22:44:22 +02006 "io/ioutil"
Dusan Kasan4595dfe2017-04-13 00:38:24 +02007 "net/mail"
8 "strings"
9 "testing"
10 "time"
Dusan Kasan17e497e2017-04-10 22:44:22 +020011)
12
13func TestParseEmail(t *testing.T) {
Dusan Kasan4595dfe2017-04-13 00:38:24 +020014 var testData = map[int]struct {
Dusan Kasan17e497e2017-04-10 22:44:22 +020015 mailData string
16
Dusan Kasan428369f2020-02-24 00:47:31 +010017 contentType string
18 content string
Dusan Kasan4595dfe2017-04-13 00:38:24 +020019 subject string
20 date time.Time
21 from []mail.Address
22 sender mail.Address
23 to []mail.Address
24 replyTo []mail.Address
25 cc []mail.Address
26 bcc []mail.Address
27 messageID string
28 resentDate time.Time
29 resentFrom []mail.Address
30 resentSender mail.Address
31 resentTo []mail.Address
32 resentReplyTo []mail.Address
33 resentCc []mail.Address
34 resentBcc []mail.Address
Dusan Kasanb49ceb62017-04-13 00:00:36 +020035 resentMessageID string
Dusan Kasan4595dfe2017-04-13 00:38:24 +020036 inReplyTo []string
37 references []string
38 htmlBody string
39 textBody string
40 attachments []attachmentData
41 embeddedFiles []embeddedFileData
42 headerCheck func(mail.Header, *testing.T)
Dusan Kasan17e497e2017-04-10 22:44:22 +020043 }{
Dusan Kasanb49ceb62017-04-13 00:00:36 +020044 1: {
Dusan Kasan45ca2642017-04-18 10:39:35 +020045 mailData: rfc5322exampleA11,
Dusan Kasan4595dfe2017-04-13 00:38:24 +020046 subject: "Saying Hello",
Dusan Kasanb49ceb62017-04-13 00:00:36 +020047 from: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +020048 {
49 Name: "John Doe",
50 Address: "jdoe@machine.example",
51 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +020052 },
53 to: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +020054 {
55 Name: "Mary Smith",
56 Address: "mary@example.net",
57 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +020058 },
Dusan Kasan45ca2642017-04-18 10:39:35 +020059 sender: mail.Address{
60 Name: "Michael Jones",
61 Address: "mjones@machine.example",
62 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +020063 messageID: "1234@local.machine.example",
Dusan Kasan4595dfe2017-04-13 00:38:24 +020064 date: parseDate("Fri, 21 Nov 1997 09:55:06 -0600"),
Dusan Kasanb49ceb62017-04-13 00:00:36 +020065 textBody: `This is a message just to say hello.
66So, "Hello".`,
67 },
68 2: {
Dusan Kasan45ca2642017-04-18 10:39:35 +020069 mailData: rfc5322exampleA12,
Dusan Kasanb49ceb62017-04-13 00:00:36 +020070 from: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +020071 {
72 Name: "Joe Q. Public",
73 Address: "john.q.public@example.com",
74 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +020075 },
76 to: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +020077 {
78 Name: "Mary Smith",
79 Address: "mary@x.test",
80 },
81 {
82 Name: "",
83 Address: "jdoe@example.org",
84 },
85 {
86 Name: "Who?",
87 Address: "one@y.test",
88 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +020089 },
90 cc: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +020091 {
92 Name: "",
93 Address: "boss@nil.test",
94 },
95 {
96 Name: "Giant; \"Big\" Box",
97 Address: "sysservices@example.net",
98 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +020099 },
100 messageID: "5678.21-Nov-1997@example.com",
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200101 date: parseDate("Tue, 01 Jul 2003 10:52:37 +0200"),
102 textBody: `Hi everyone.`,
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200103 },
104 3: {
Dusan Kasan45ca2642017-04-18 10:39:35 +0200105 mailData: rfc5322exampleA2a,
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200106 subject: "Re: Saying Hello",
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200107 from: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200108 {
109 Name: "Mary Smith",
110 Address: "mary@example.net",
111 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200112 },
113 replyTo: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200114 {
115 Name: "Mary Smith: Personal Account",
116 Address: "smith@home.example",
117 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200118 },
119 to: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200120 {
121 Name: "John Doe",
122 Address: "jdoe@machine.example",
123 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200124 },
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200125 messageID: "3456@example.net",
126 inReplyTo: []string{"1234@local.machine.example"},
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200127 references: []string{"1234@local.machine.example"},
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200128 date: parseDate("Fri, 21 Nov 1997 10:01:10 -0600"),
129 textBody: `This is a reply to your hello.`,
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200130 },
131 4: {
Dusan Kasan45ca2642017-04-18 10:39:35 +0200132 mailData: rfc5322exampleA2b,
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200133 subject: "Re: Saying Hello",
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200134 from: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200135 {
136 Name: "John Doe",
137 Address: "jdoe@machine.example",
138 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200139 },
140 to: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200141 {
142 Name: "Mary Smith: Personal Account",
143 Address: "smith@home.example",
144 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200145 },
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200146 messageID: "abcd.1234@local.machine.test",
147 inReplyTo: []string{"3456@example.net"},
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200148 references: []string{"1234@local.machine.example", "3456@example.net"},
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200149 date: parseDate("Fri, 21 Nov 1997 11:00:00 -0600"),
150 textBody: `This is a reply to your reply.`,
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200151 },
152 5: {
Dusan Kasan45ca2642017-04-18 10:39:35 +0200153 mailData: rfc5322exampleA3,
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200154 subject: "Saying Hello",
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200155 from: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200156 {
157 Name: "John Doe",
158 Address: "jdoe@machine.example",
159 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200160 },
161 to: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200162 {
163 Name: "Mary Smith",
164 Address: "mary@example.net",
165 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200166 },
167 messageID: "1234@local.machine.example",
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200168 date: parseDate("Fri, 21 Nov 1997 09:55:06 -0600"),
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200169 resentFrom: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200170 {
171 Name: "Mary Smith",
172 Address: "mary@example.net",
173 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200174 },
175 resentTo: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200176 {
177 Name: "Jane Brown",
178 Address: "j-brown@other.example",
179 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200180 },
181 resentMessageID: "78910@example.net",
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200182 resentDate: parseDate("Mon, 24 Nov 1997 14:22:01 -0800"),
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200183 textBody: `This is a message just to say hello.
184So, "Hello".`,
185 },
186 6: {
Dusan Kasan45ca2642017-04-18 10:39:35 +0200187 mailData: data1,
Dusan Kasan428369f2020-02-24 00:47:31 +0100188 contentType: `multipart/mixed; boundary=f403045f1dcc043a44054c8e6bbf`,
189 content: "",
Dusan Kasanf4376a62017-05-23 21:03:55 +0200190 subject: "Peter Paholík",
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200191 from: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200192 {
193 Name: "Peter Paholík",
194 Address: "peter.paholik@gmail.com",
195 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200196 },
197 to: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200198 {
199 Name: "",
200 Address: "dusan@kasan.sk",
201 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200202 },
Dusan Kasan17e497e2017-04-10 22:44:22 +0200203 messageID: "CACtgX4kNXE7T5XKSKeH_zEcfUUmf2vXVASxYjaaK9cCn-3zb_g@mail.gmail.com",
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200204 date: parseDate("Fri, 07 Apr 2017 09:17:26 +0200"),
205 htmlBody: "<div dir=\"ltr\"><br></div>",
Dusan Kasan17e497e2017-04-10 22:44:22 +0200206 attachments: []attachmentData{
207 {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200208 filename: "Peter Paholík 1 4 2017 2017-04-07.pdf",
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200209 contentType: "application/pdf",
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200210 base64data: "JVBERi0xLjQNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFuZyhlbi1VUykgL1N0cnVjdFRyZWVSb290IDY3IDAgUi9NYXJrSW5mbzw8L01hcmtlZCB0cnVlPj4vT3V0cHV0SW50ZW50c1s8PC9UeXBlL091dHB1dEludGVudC9TL0dUU19QREZBMS9PdXRwdXRDb25kZXYgMzk1MzYyDQo+Pg0Kc3RhcnR4cmVmDQo0MTk4ODUNCiUlRU9GDQo=",
Dusan Kasan17e497e2017-04-10 22:44:22 +0200211 },
212 },
213 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200214 7: {
Dusan Kasan45ca2642017-04-18 10:39:35 +0200215 mailData: data2,
Dusan Kasan428369f2020-02-24 00:47:31 +0100216 contentType: `multipart/alternative; boundary="------------C70C0458A558E585ACB75FB4"`,
217 content: "",
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200218 subject: "Re: Test Subject 2",
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200219 from: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200220 {
221 Name: "Sender Man",
222 Address: "sender@domain.com",
223 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200224 },
225 to: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200226 {
227 Name: "",
228 Address: "info@receiver.com",
229 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200230 },
231 cc: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200232 {
233 Name: "Cc Man",
234 Address: "ccman@gmail.com",
235 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200236 },
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200237 messageID: "0e9a21b4-01dc-e5c1-dcd6-58ce5aa61f4f@receiver.com",
238 inReplyTo: []string{"9ff38d03-c4ab-89b7-9328-e99d5e24e3ba@receiver.eu"},
Dusan Kasan17e497e2017-04-10 22:44:22 +0200239 references: []string{"2f6b7595-c01e-46e5-42bc-f263e1c4282d@receiver.com", "9ff38d03-c4ab-89b7-9328-e99d5e24e3ba@domain.com"},
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200240 date: parseDate("Fri, 07 Apr 2017 12:59:55 +0200"),
241 htmlBody: `<html>data<img src="part2.9599C449.04E5EC81@develhell.com"/></html>`,
Dusan Kasan17e497e2017-04-10 22:44:22 +0200242 textBody: `First level
243> Second level
244>> Third level
245>
246`,
247 embeddedFiles: []embeddedFileData{
248 {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200249 cid: "part2.9599C449.04E5EC81@develhell.com",
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200250 contentType: "image/png",
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200251 base64data: "iVBORw0KGgoAAAANSUhEUgAAAQEAAAAYCAIAAAB1IN9NAAAACXBIWXMAAAsTAAALEwEAmpwYYKUKF+Os3baUndC0pDnwNAmLy1SUr2Gw0luxQuV/AwC6cEhVV5VRrwAAAABJRU5ErkJggg==",
Dusan Kasan17e497e2017-04-10 22:44:22 +0200252 },
253 },
254 },
Dusan Kasan428369f2020-02-24 00:47:31 +0100255 8: {
256 mailData: imageContentExample,
257 subject: "Saying Hello",
258 from: []mail.Address{
259 {
260 Name: "John Doe",
261 Address: "jdoe@machine.example",
262 },
263 },
264 to: []mail.Address{
265 {
266 Name: "Mary Smith",
267 Address: "mary@example.net",
268 },
269 },
270 sender: mail.Address{
271 Name: "Michael Jones",
272 Address: "mjones@machine.example",
273 },
274 messageID: "1234@local.machine.example",
275 date: parseDate("Fri, 21 Nov 1997 09:55:06 -0600"),
276 contentType: `image/jpeg; x-unix-mode=0644; name="image.gif"`,
277 content: `GIF89a;`,
278 },
Dusan Kasan17e497e2017-04-10 22:44:22 +0200279 }
280
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200281 for index, td := range testData {
Dusan Kasan428369f2020-02-24 00:47:31 +0100282 e, err := Parse(strings.NewReader(td.mailData))
Antonio331b1182019-01-15 09:58:52 +0100283 if err != nil {
284 t.Error(err)
285 }
Dusan Kasan428369f2020-02-24 00:47:31 +0100286
287 if td.contentType != e.ContentType {
288 t.Errorf("[Test Case %v] Wrong content type. Expected: %s, Got: %s", index, td.contentType, e.ContentType)
289 }
290
291 if td.content != "" {
292 b, err := ioutil.ReadAll(e.Content)
293 if err != nil {
294 t.Error(err)
295 } else if td.content != string(b) {
296 t.Errorf("[Test Case %v] Wrong content. Expected: %s, Got: %s", index, td.content, string(b))
297 }
Dusan Kasan17e497e2017-04-10 22:44:22 +0200298 }
299
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200300 if td.subject != e.Subject {
301 t.Errorf("[Test Case %v] Wrong subject. Expected: %s, Got: %s", index, td.subject, e.Subject)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200302 }
303
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200304 if td.messageID != e.MessageID {
305 t.Errorf("[Test Case %v] Wrong messageID. Expected: '%s', Got: '%s'", index, td.messageID, e.MessageID)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200306 }
307
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200308 if !td.date.Equal(e.Date) {
309 t.Errorf("[Test Case %v] Wrong date. Expected: %v, Got: %v", index, td.date, e.Date)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200310 }
311
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200312 d := dereferenceAddressList(e.From)
313 if !assertAddressListEq(td.from, d) {
314 t.Errorf("[Test Case %v] Wrong from. Expected: %s, Got: %s", index, td.from, d)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200315 }
316
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200317 var sender mail.Address
318 if e.Sender != nil {
319 sender = *e.Sender
320 }
321 if td.sender != sender {
322 t.Errorf("[Test Case %v] Wrong sender. Expected: %s, Got: %s", index, td.sender, sender)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200323 }
324
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200325 d = dereferenceAddressList(e.To)
326 if !assertAddressListEq(td.to, d) {
327 t.Errorf("[Test Case %v] Wrong to. Expected: %s, Got: %s", index, td.to, d)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200328 }
329
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200330 d = dereferenceAddressList(e.Cc)
331 if !assertAddressListEq(td.cc, d) {
332 t.Errorf("[Test Case %v] Wrong cc. Expected: %s, Got: %s", index, td.cc, d)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200333 }
334
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200335 d = dereferenceAddressList(e.Bcc)
336 if !assertAddressListEq(td.bcc, d) {
337 t.Errorf("[Test Case %v] Wrong bcc. Expected: %s, Got: %s", index, td.bcc, d)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200338 }
339
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200340 if td.resentMessageID != e.ResentMessageID {
341 t.Errorf("[Test Case %v] Wrong resent messageID. Expected: '%s', Got: '%s'", index, td.resentMessageID, e.ResentMessageID)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200342 }
343
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200344 if !td.resentDate.Equal(e.ResentDate) && !td.resentDate.IsZero() && !e.ResentDate.IsZero() {
345 t.Errorf("[Test Case %v] Wrong resent date. Expected: %v, Got: %v", index, td.resentDate, e.ResentDate)
346 }
347
348 d = dereferenceAddressList(e.ResentFrom)
349 if !assertAddressListEq(td.resentFrom, d) {
350 t.Errorf("[Test Case %v] Wrong resent from. Expected: %s, Got: %s", index, td.resentFrom, d)
351 }
352
353 var resentSender mail.Address
354 if e.ResentSender != nil {
355 resentSender = *e.ResentSender
356 }
357 if td.resentSender != resentSender {
358 t.Errorf("[Test Case %v] Wrong resent sender. Expected: %s, Got: %s", index, td.resentSender, resentSender)
359 }
360
361 d = dereferenceAddressList(e.ResentTo)
362 if !assertAddressListEq(td.resentTo, d) {
363 t.Errorf("[Test Case %v] Wrong resent to. Expected: %s, Got: %s", index, td.resentTo, d)
364 }
365
366 d = dereferenceAddressList(e.ResentCc)
367 if !assertAddressListEq(td.resentCc, d) {
368 t.Errorf("[Test Case %v] Wrong resent cc. Expected: %s, Got: %s", index, td.resentCc, d)
369 }
370
371 d = dereferenceAddressList(e.ResentBcc)
372 if !assertAddressListEq(td.resentBcc, d) {
373 t.Errorf("[Test Case %v] Wrong resent bcc. Expected: %s, Got: %s", index, td.resentBcc, d)
374 }
375
376 if !assertSliceEq(td.inReplyTo, e.InReplyTo) {
377 t.Errorf("[Test Case %v] Wrong in reply to. Expected: %s, Got: %s", index, td.inReplyTo, e.InReplyTo)
378 }
379
380 if !assertSliceEq(td.references, e.References) {
381 t.Errorf("[Test Case %v] Wrong references. Expected: %s, Got: %s", index, td.references, e.References)
382 }
383
384 d = dereferenceAddressList(e.ReplyTo)
385 if !assertAddressListEq(td.replyTo, d) {
386 t.Errorf("[Test Case %v] Wrong reply to. Expected: %s, Got: %s", index, td.replyTo, d)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200387 }
388
389 if td.htmlBody != e.HTMLBody {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200390 t.Errorf("[Test Case %v] Wrong html body. Expected: '%s', Got: '%s'", index, td.htmlBody, e.HTMLBody)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200391 }
392
393 if td.textBody != e.TextBody {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200394 t.Errorf("[Test Case %v] Wrong text body. Expected: '%s', Got: '%s'", index, td.textBody, e.TextBody)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200395 }
396
Dusan Kasan17e497e2017-04-10 22:44:22 +0200397 if len(td.attachments) != len(e.Attachments) {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200398 t.Errorf("[Test Case %v] Incorrect number of attachments! Expected: %v, Got: %v.", index, len(td.attachments), len(e.Attachments))
Dusan Kasan17e497e2017-04-10 22:44:22 +0200399 } else {
400 attachs := e.Attachments[:]
401
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200402 for _, ad := range td.attachments {
Dusan Kasan17e497e2017-04-10 22:44:22 +0200403 found := false
404
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200405 for i, ra := range attachs {
Dusan Kasan17e497e2017-04-10 22:44:22 +0200406 b, err := ioutil.ReadAll(ra.Data)
407 if err != nil {
408 t.Error(err)
409 }
410
411 encoded := base64.StdEncoding.EncodeToString(b)
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200412 if ra.Filename == ad.filename && encoded == ad.base64data && ra.ContentType == ad.contentType {
Dusan Kasan17e497e2017-04-10 22:44:22 +0200413 found = true
414 attachs = append(attachs[:i], attachs[i+1:]...)
415 }
416 }
417
418 if !found {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200419 t.Errorf("[Test Case %v] Attachment not found: %s", index, ad.filename)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200420 }
421 }
422
423 if len(attachs) != 0 {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200424 t.Errorf("[Test Case %v] Email contains %v unexpected attachments: %v", index, len(attachs), attachs)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200425 }
426 }
427
428 if len(td.embeddedFiles) != len(e.EmbeddedFiles) {
Dusan Kasan1a966482017-04-18 10:45:25 +0200429 t.Errorf("[Test Case %v] Incorrect number of embedded files! Expected: %v, Got: %v.", index, len(td.embeddedFiles), len(e.EmbeddedFiles))
Dusan Kasan17e497e2017-04-10 22:44:22 +0200430 } else {
431 embeds := e.EmbeddedFiles[:]
432
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200433 for _, ad := range td.embeddedFiles {
Dusan Kasan17e497e2017-04-10 22:44:22 +0200434 found := false
435
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200436 for i, ra := range embeds {
Dusan Kasan17e497e2017-04-10 22:44:22 +0200437 b, err := ioutil.ReadAll(ra.Data)
438 if err != nil {
439 t.Error(err)
440 }
441
442 encoded := base64.StdEncoding.EncodeToString(b)
443
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200444 if ra.CID == ad.cid && encoded == ad.base64data && ra.ContentType == ad.contentType {
Dusan Kasan17e497e2017-04-10 22:44:22 +0200445 found = true
446 embeds = append(embeds[:i], embeds[i+1:]...)
447 }
448 }
449
450 if !found {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200451 t.Errorf("[Test Case %v] Embedded file not found: %s", index, ad.cid)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200452 }
453 }
454
455 if len(embeds) != 0 {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200456 t.Errorf("[Test Case %v] Email contains %v unexpected embedded files: %v", index, len(embeds), embeds)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200457 }
458 }
459 }
460}
461
Obi Symons333687f2020-04-05 14:31:35 +1000462func TestMultiPartRelatedEmail(t *testing.T) {
463 email, err := Parse(strings.NewReader(multipartRelatedExample))
464 if err != nil {
465 t.Errorf("[Test Multipart Related] Emailed failed to parse: %v", err)
466 }
467
468 if len(email.TextBody) == 0 {
469 t.Errorf("[Test Multipart Related] Failed to parse multipart related at the top level.")
470 }
471 body := "Time for the egg."
472 fmt.Println(email.TextBody)
473 if email.TextBody != body {
474 t.Errorf("Test Multipart Related] Body didn't match. \nExpected: %v,\nbut got: %v", body, email.TextBody)
475 }
476
477 html := "<div dir=\"ltr\"><div>Time for the egg.</div><div><br></div><div><br><br></div></div>"
478 if email.HTMLBody != html {
479 t.Errorf("Test Multipart Related] HTML didn't match. \nExpected: %v,\nbut got: %v", html, email.TextBody)
480 }
481}
482
Dusan Kasan17e497e2017-04-10 22:44:22 +0200483func parseDate(in string) time.Time {
484 out, err := time.Parse(time.RFC1123Z, in)
485 if err != nil {
486 panic(err)
487 }
488
489 return out
490}
491
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200492type attachmentData struct {
493 filename string
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200494 contentType string
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200495 base64data string
Dusan Kasan17e497e2017-04-10 22:44:22 +0200496}
497
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200498type embeddedFileData struct {
499 cid string
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200500 contentType string
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200501 base64data string
Dusan Kasan17e497e2017-04-10 22:44:22 +0200502}
503
504func assertSliceEq(a, b []string) bool {
505 if len(a) == len(b) && len(a) == 0 {
506 return true
507 }
508
509 if a == nil && b == nil {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200510 return true
Dusan Kasan17e497e2017-04-10 22:44:22 +0200511 }
512
513 if a == nil || b == nil {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200514 return false
Dusan Kasan17e497e2017-04-10 22:44:22 +0200515 }
516
517 if len(a) != len(b) {
518 return false
519 }
520
521 for i := range a {
522 if a[i] != b[i] {
523 return false
524 }
525 }
526
527 return true
528}
529
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200530func assertAddressListEq(a, b []mail.Address) bool {
531 if len(a) == len(b) && len(a) == 0 {
532 return true
533 }
534
535 if a == nil && b == nil {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200536 return true
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200537 }
538
539 if a == nil || b == nil {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200540 return false
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200541 }
542
543 if len(a) != len(b) {
544 return false
545 }
546
547 for i := range a {
548 if a[i] != b[i] {
549 return false
550 }
551 }
552
553 return true
554}
555
556func dereferenceAddressList(al []*mail.Address) (result []mail.Address) {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200557 for _, a := range al {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200558 result = append(result, *a)
559 }
560
561 return
562}
563
Dusan Kasan45ca2642017-04-18 10:39:35 +0200564var data1 = `From: =?UTF-8?Q?Peter_Pahol=C3=ADk?= <peter.paholik@gmail.com>
Dusan Kasan17e497e2017-04-10 22:44:22 +0200565Date: Fri, 7 Apr 2017 09:17:26 +0200
566Message-ID: <CACtgX4kNXE7T5XKSKeH_zEcfUUmf2vXVASxYjaaK9cCn-3zb_g@mail.gmail.com>
Dusan Kasanf4376a62017-05-23 21:03:55 +0200567Subject: =?UTF-8?Q?Peter_Pahol=C3=ADk?=
Dusan Kasan17e497e2017-04-10 22:44:22 +0200568To: dusan@kasan.sk
569Content-Type: multipart/mixed; boundary=f403045f1dcc043a44054c8e6bbf
570
571--f403045f1dcc043a44054c8e6bbf
572Content-Type: multipart/alternative; boundary=f403045f1dcc043a3f054c8e6bbd
573
574--f403045f1dcc043a3f054c8e6bbd
575Content-Type: text/plain; charset=UTF-8
576
577
578
579--f403045f1dcc043a3f054c8e6bbd
580Content-Type: text/html; charset=UTF-8
581
582<div dir="ltr"><br></div>
583
584--f403045f1dcc043a3f054c8e6bbd--
585--f403045f1dcc043a44054c8e6bbf
586Content-Type: application/pdf;
587 name="=?UTF-8?Q?Peter_Paholi=CC=81k_1?=
588 =?UTF-8?Q?_4_2017_2017=2D04=2D07=2Epdf?="
589Content-Disposition: attachment;
590 filename="=?UTF-8?Q?Peter_Paholi=CC=81k_1?=
591 =?UTF-8?Q?_4_2017_2017=2D04=2D07=2Epdf?="
592Content-Transfer-Encoding: base64
593X-Attachment-Id: f_j17i0f0d0
594
595JVBERi0xLjQNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFu
596Zyhlbi1VUykgL1N0cnVjdFRyZWVSb290IDY3IDAgUi9NYXJrSW5mbzw8L01hcmtlZCB0cnVlPj4v
597T3V0cHV0SW50ZW50c1s8PC9UeXBlL091dHB1dEludGVudC9TL0dUU19QREZBMS9PdXRwdXRDb25k
598ZXYgMzk1MzYyDQo+Pg0Kc3RhcnR4cmVmDQo0MTk4ODUNCiUlRU9GDQo=
599--f403045f1dcc043a44054c8e6bbf--
600`
601
Dusan Kasan45ca2642017-04-18 10:39:35 +0200602var data2 = `Subject: Re: Test Subject 2
Dusan Kasan17e497e2017-04-10 22:44:22 +0200603To: info@receiver.com
604References: <2f6b7595-c01e-46e5-42bc-f263e1c4282d@receiver.com>
605 <9ff38d03-c4ab-89b7-9328-e99d5e24e3ba@domain.com>
606Cc: Cc Man <ccman@gmail.com>
607From: Sender Man <sender@domain.com>
608Message-ID: <0e9a21b4-01dc-e5c1-dcd6-58ce5aa61f4f@receiver.com>
609Date: Fri, 7 Apr 2017 12:59:55 +0200
610User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:45.0)
611 Gecko/20100101 Thunderbird/45.8.0
612MIME-Version: 1.0
613In-Reply-To: <9ff38d03-c4ab-89b7-9328-e99d5e24e3ba@receiver.eu>
614Content-Type: multipart/alternative;
615 boundary="------------C70C0458A558E585ACB75FB4"
616
617This is a multi-part message in MIME format.
618--------------C70C0458A558E585ACB75FB4
619Content-Type: text/plain; charset=utf-8; format=flowed
620Content-Transfer-Encoding: 8bit
621
622First level
623> Second level
624>> Third level
625>
626
627
628--------------C70C0458A558E585ACB75FB4
629Content-Type: multipart/related;
630 boundary="------------5DB4A1356834BB602A5F88B2"
631
632
633--------------5DB4A1356834BB602A5F88B2
634Content-Type: text/html; charset=utf-8
635Content-Transfer-Encoding: 8bit
636
637<html>data<img src="part2.9599C449.04E5EC81@develhell.com"/></html>
638
639--------------5DB4A1356834BB602A5F88B2
640Content-Type: image/png
641Content-Transfer-Encoding: base64
642Content-ID: <part2.9599C449.04E5EC81@develhell.com>
643
644iVBORw0KGgoAAAANSUhEUgAAAQEAAAAYCAIAAAB1IN9NAAAACXBIWXMAAAsTAAALEwEAmpwY
645YKUKF+Os3baUndC0pDnwNAmLy1SUr2Gw0luxQuV/AwC6cEhVV5VRrwAAAABJRU5ErkJggg==
646--------------5DB4A1356834BB602A5F88B2
647
648--------------C70C0458A558E585ACB75FB4--
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200649`
650
Dusan Kasan45ca2642017-04-18 10:39:35 +0200651var rfc5322exampleA11 = `From: John Doe <jdoe@machine.example>
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200652Sender: Michael Jones <mjones@machine.example>
653To: Mary Smith <mary@example.net>
654Subject: Saying Hello
655Date: Fri, 21 Nov 1997 09:55:06 -0600
656Message-ID: <1234@local.machine.example>
657
658This is a message just to say hello.
659So, "Hello".
660`
661
Dusan Kasan45ca2642017-04-18 10:39:35 +0200662var rfc5322exampleA12 = `From: "Joe Q. Public" <john.q.public@example.com>
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200663To: Mary Smith <mary@x.test>, jdoe@example.org, Who? <one@y.test>
664Cc: <boss@nil.test>, "Giant; \"Big\" Box" <sysservices@example.net>
665Date: Tue, 1 Jul 2003 10:52:37 +0200
666Message-ID: <5678.21-Nov-1997@example.com>
667
668Hi everyone.
669`
670
671//todo: not yet implemented in net/mail
672//once there is support for this, add it
Dusan Kasan45ca2642017-04-18 10:39:35 +0200673var rfc5322exampleA13 = `From: Pete <pete@silly.example>
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200674To: A Group:Ed Jones <c@a.test>,joe@where.test,John <jdoe@one.test>;
675Cc: Undisclosed recipients:;
676Date: Thu, 13 Feb 1969 23:32:54 -0330
677Message-ID: <testabcd.1234@silly.example>
678
679Testing.
680`
681
682//we skipped the first message bcause it's the same as A 1.1
Dusan Kasan45ca2642017-04-18 10:39:35 +0200683var rfc5322exampleA2a = `From: Mary Smith <mary@example.net>
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200684To: John Doe <jdoe@machine.example>
685Reply-To: "Mary Smith: Personal Account" <smith@home.example>
686Subject: Re: Saying Hello
687Date: Fri, 21 Nov 1997 10:01:10 -0600
688Message-ID: <3456@example.net>
689In-Reply-To: <1234@local.machine.example>
690References: <1234@local.machine.example>
691
692This is a reply to your hello.
693`
694
Dusan Kasan45ca2642017-04-18 10:39:35 +0200695var rfc5322exampleA2b = `To: "Mary Smith: Personal Account" <smith@home.example>
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200696From: John Doe <jdoe@machine.example>
697Subject: Re: Saying Hello
698Date: Fri, 21 Nov 1997 11:00:00 -0600
699Message-ID: <abcd.1234@local.machine.test>
700In-Reply-To: <3456@example.net>
701References: <1234@local.machine.example> <3456@example.net>
702
703This is a reply to your reply.
704`
705
Dusan Kasan45ca2642017-04-18 10:39:35 +0200706var rfc5322exampleA3 = `Resent-From: Mary Smith <mary@example.net>
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200707Resent-To: Jane Brown <j-brown@other.example>
708Resent-Date: Mon, 24 Nov 1997 14:22:01 -0800
709Resent-Message-ID: <78910@example.net>
710From: John Doe <jdoe@machine.example>
711To: Mary Smith <mary@example.net>
712Subject: Saying Hello
713Date: Fri, 21 Nov 1997 09:55:06 -0600
714Message-ID: <1234@local.machine.example>
715
716This is a message just to say hello.
717So, "Hello".`
718
Dusan Kasan45ca2642017-04-18 10:39:35 +0200719var rfc5322exampleA4 = `Received: from x.y.test
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200720 by example.net
721 via TCP
722 with ESMTP
723 id ABC12345
724 for <mary@example.net>; 21 Nov 1997 10:05:43 -0600
725Received: from node.example by x.y.test; 21 Nov 1997 10:01:22 -0600
726From: John Doe <jdoe@node.example>
727To: Mary Smith <mary@example.net>
728Subject: Saying Hello
729Date: Fri, 21 Nov 1997 09:55:06 -0600
730Message-ID: <1234@local.node.example>
731
732This is a message just to say hello.
733So, "Hello".`
Dusan Kasan428369f2020-02-24 00:47:31 +0100734
735var imageContentExample = `From: John Doe <jdoe@machine.example>
736Sender: Michael Jones <mjones@machine.example>
737To: Mary Smith <mary@example.net>
738Subject: Saying Hello
739Date: Fri, 21 Nov 1997 09:55:06 -0600
740Message-ID: <1234@local.machine.example>
741Content-Type: image/jpeg;
742 x-unix-mode=0644;
743 name="image.gif"
744Content-Transfer-Encoding: base64
745
746R0lGODlhAQE7`
Obi Symons333687f2020-04-05 14:31:35 +1000747
748var multipartRelatedExample = `MIME-Version: 1.0
749From: John Doe <jdoe@machine.example>
750Sender: Michael Jones <mjones@machine.example>
751To: Mary Smith <mary@example.net>
752Subject: Saying Hello
753Date: Fri, 21 Nov 1997 09:55:06 -0600
754Message-ID: <1234@local.machine.example>
755Subject: ooops
756To: test@example.rocks
757Content-Type: multipart/related; boundary="000000000000ab2e2205a26de587"
758
759--000000000000ab2e2205a26de587
760Content-Type: multipart/alternative; boundary="000000000000ab2e1f05a26de586"
761
762--000000000000ab2e1f05a26de586
763Content-Type: text/plain; charset="UTF-8"
764
765Time for the egg.
766
767--000000000000ab2e1f05a26de586
768Content-Type: text/html; charset="UTF-8"
769
770<div dir="ltr"><div>Time for the egg.</div><div><br></div><div><br><br></div></div>
771
772--000000000000ab2e1f05a26de586--
773
774
775--000000000000ab2e2205a26de587--
776`