blob: 944c0b70dec2c739371a1588c719c7af89cb99fb [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"
5 "io/ioutil"
Dusan Kasan4595dfe2017-04-13 00:38:24 +02006 "net/mail"
7 "strings"
8 "testing"
9 "time"
Dusan Kasan17e497e2017-04-10 22:44:22 +020010)
11
12func TestParseEmail(t *testing.T) {
Dusan Kasan4595dfe2017-04-13 00:38:24 +020013 var testData = map[int]struct {
Dusan Kasan17e497e2017-04-10 22:44:22 +020014 mailData string
15
Dusan Kasan428369f2020-02-24 00:47:31 +010016 contentType string
17 content string
Dusan Kasan4595dfe2017-04-13 00:38:24 +020018 subject string
19 date time.Time
20 from []mail.Address
21 sender mail.Address
22 to []mail.Address
23 replyTo []mail.Address
24 cc []mail.Address
25 bcc []mail.Address
26 messageID string
27 resentDate time.Time
28 resentFrom []mail.Address
29 resentSender mail.Address
30 resentTo []mail.Address
31 resentReplyTo []mail.Address
32 resentCc []mail.Address
33 resentBcc []mail.Address
Dusan Kasanb49ceb62017-04-13 00:00:36 +020034 resentMessageID string
Dusan Kasan4595dfe2017-04-13 00:38:24 +020035 inReplyTo []string
36 references []string
37 htmlBody string
38 textBody string
39 attachments []attachmentData
40 embeddedFiles []embeddedFileData
41 headerCheck func(mail.Header, *testing.T)
Dusan Kasan17e497e2017-04-10 22:44:22 +020042 }{
Dusan Kasanb49ceb62017-04-13 00:00:36 +020043 1: {
Dusan Kasan45ca2642017-04-18 10:39:35 +020044 mailData: rfc5322exampleA11,
Dusan Kasan4595dfe2017-04-13 00:38:24 +020045 subject: "Saying Hello",
Dusan Kasanb49ceb62017-04-13 00:00:36 +020046 from: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +020047 {
48 Name: "John Doe",
49 Address: "jdoe@machine.example",
50 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +020051 },
52 to: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +020053 {
54 Name: "Mary Smith",
55 Address: "mary@example.net",
56 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +020057 },
Dusan Kasan45ca2642017-04-18 10:39:35 +020058 sender: mail.Address{
59 Name: "Michael Jones",
60 Address: "mjones@machine.example",
61 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +020062 messageID: "1234@local.machine.example",
Dusan Kasan4595dfe2017-04-13 00:38:24 +020063 date: parseDate("Fri, 21 Nov 1997 09:55:06 -0600"),
Dusan Kasanb49ceb62017-04-13 00:00:36 +020064 textBody: `This is a message just to say hello.
65So, "Hello".`,
66 },
67 2: {
Dusan Kasan45ca2642017-04-18 10:39:35 +020068 mailData: rfc5322exampleA12,
Dusan Kasanb49ceb62017-04-13 00:00:36 +020069 from: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +020070 {
71 Name: "Joe Q. Public",
72 Address: "john.q.public@example.com",
73 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +020074 },
75 to: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +020076 {
77 Name: "Mary Smith",
78 Address: "mary@x.test",
79 },
80 {
81 Name: "",
82 Address: "jdoe@example.org",
83 },
84 {
85 Name: "Who?",
86 Address: "one@y.test",
87 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +020088 },
89 cc: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +020090 {
91 Name: "",
92 Address: "boss@nil.test",
93 },
94 {
95 Name: "Giant; \"Big\" Box",
96 Address: "sysservices@example.net",
97 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +020098 },
99 messageID: "5678.21-Nov-1997@example.com",
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200100 date: parseDate("Tue, 01 Jul 2003 10:52:37 +0200"),
101 textBody: `Hi everyone.`,
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200102 },
103 3: {
Dusan Kasan45ca2642017-04-18 10:39:35 +0200104 mailData: rfc5322exampleA2a,
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200105 subject: "Re: Saying Hello",
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200106 from: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200107 {
108 Name: "Mary Smith",
109 Address: "mary@example.net",
110 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200111 },
112 replyTo: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200113 {
114 Name: "Mary Smith: Personal Account",
115 Address: "smith@home.example",
116 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200117 },
118 to: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200119 {
120 Name: "John Doe",
121 Address: "jdoe@machine.example",
122 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200123 },
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200124 messageID: "3456@example.net",
125 inReplyTo: []string{"1234@local.machine.example"},
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200126 references: []string{"1234@local.machine.example"},
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200127 date: parseDate("Fri, 21 Nov 1997 10:01:10 -0600"),
128 textBody: `This is a reply to your hello.`,
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200129 },
130 4: {
Dusan Kasan45ca2642017-04-18 10:39:35 +0200131 mailData: rfc5322exampleA2b,
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200132 subject: "Re: Saying Hello",
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200133 from: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200134 {
135 Name: "John Doe",
136 Address: "jdoe@machine.example",
137 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200138 },
139 to: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200140 {
141 Name: "Mary Smith: Personal Account",
142 Address: "smith@home.example",
143 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200144 },
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200145 messageID: "abcd.1234@local.machine.test",
146 inReplyTo: []string{"3456@example.net"},
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200147 references: []string{"1234@local.machine.example", "3456@example.net"},
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200148 date: parseDate("Fri, 21 Nov 1997 11:00:00 -0600"),
149 textBody: `This is a reply to your reply.`,
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200150 },
151 5: {
Dusan Kasan45ca2642017-04-18 10:39:35 +0200152 mailData: rfc5322exampleA3,
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200153 subject: "Saying Hello",
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200154 from: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200155 {
156 Name: "John Doe",
157 Address: "jdoe@machine.example",
158 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200159 },
160 to: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200161 {
162 Name: "Mary Smith",
163 Address: "mary@example.net",
164 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200165 },
166 messageID: "1234@local.machine.example",
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200167 date: parseDate("Fri, 21 Nov 1997 09:55:06 -0600"),
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200168 resentFrom: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200169 {
170 Name: "Mary Smith",
171 Address: "mary@example.net",
172 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200173 },
174 resentTo: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200175 {
176 Name: "Jane Brown",
177 Address: "j-brown@other.example",
178 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200179 },
180 resentMessageID: "78910@example.net",
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200181 resentDate: parseDate("Mon, 24 Nov 1997 14:22:01 -0800"),
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200182 textBody: `This is a message just to say hello.
183So, "Hello".`,
184 },
185 6: {
Dusan Kasan45ca2642017-04-18 10:39:35 +0200186 mailData: data1,
Dusan Kasan428369f2020-02-24 00:47:31 +0100187 contentType: `multipart/mixed; boundary=f403045f1dcc043a44054c8e6bbf`,
188 content: "",
Dusan Kasanf4376a62017-05-23 21:03:55 +0200189 subject: "Peter Paholík",
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200190 from: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200191 {
192 Name: "Peter Paholík",
193 Address: "peter.paholik@gmail.com",
194 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200195 },
196 to: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200197 {
198 Name: "",
199 Address: "dusan@kasan.sk",
200 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200201 },
Dusan Kasan17e497e2017-04-10 22:44:22 +0200202 messageID: "CACtgX4kNXE7T5XKSKeH_zEcfUUmf2vXVASxYjaaK9cCn-3zb_g@mail.gmail.com",
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200203 date: parseDate("Fri, 07 Apr 2017 09:17:26 +0200"),
204 htmlBody: "<div dir=\"ltr\"><br></div>",
Dusan Kasan17e497e2017-04-10 22:44:22 +0200205 attachments: []attachmentData{
206 {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200207 filename: "Peter Paholík 1 4 2017 2017-04-07.pdf",
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200208 contentType: "application/pdf",
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200209 base64data: "JVBERi0xLjQNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFuZyhlbi1VUykgL1N0cnVjdFRyZWVSb290IDY3IDAgUi9NYXJrSW5mbzw8L01hcmtlZCB0cnVlPj4vT3V0cHV0SW50ZW50c1s8PC9UeXBlL091dHB1dEludGVudC9TL0dUU19QREZBMS9PdXRwdXRDb25kZXYgMzk1MzYyDQo+Pg0Kc3RhcnR4cmVmDQo0MTk4ODUNCiUlRU9GDQo=",
Dusan Kasan17e497e2017-04-10 22:44:22 +0200210 },
211 },
212 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200213 7: {
Dusan Kasan45ca2642017-04-18 10:39:35 +0200214 mailData: data2,
Dusan Kasan428369f2020-02-24 00:47:31 +0100215 contentType: `multipart/alternative; boundary="------------C70C0458A558E585ACB75FB4"`,
216 content: "",
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200217 subject: "Re: Test Subject 2",
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200218 from: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200219 {
220 Name: "Sender Man",
221 Address: "sender@domain.com",
222 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200223 },
224 to: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200225 {
226 Name: "",
227 Address: "info@receiver.com",
228 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200229 },
230 cc: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200231 {
232 Name: "Cc Man",
233 Address: "ccman@gmail.com",
234 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200235 },
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200236 messageID: "0e9a21b4-01dc-e5c1-dcd6-58ce5aa61f4f@receiver.com",
237 inReplyTo: []string{"9ff38d03-c4ab-89b7-9328-e99d5e24e3ba@receiver.eu"},
Dusan Kasan17e497e2017-04-10 22:44:22 +0200238 references: []string{"2f6b7595-c01e-46e5-42bc-f263e1c4282d@receiver.com", "9ff38d03-c4ab-89b7-9328-e99d5e24e3ba@domain.com"},
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200239 date: parseDate("Fri, 07 Apr 2017 12:59:55 +0200"),
240 htmlBody: `<html>data<img src="part2.9599C449.04E5EC81@develhell.com"/></html>`,
Dusan Kasan17e497e2017-04-10 22:44:22 +0200241 textBody: `First level
242> Second level
243>> Third level
244>
245`,
246 embeddedFiles: []embeddedFileData{
247 {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200248 cid: "part2.9599C449.04E5EC81@develhell.com",
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200249 contentType: "image/png",
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200250 base64data: "iVBORw0KGgoAAAANSUhEUgAAAQEAAAAYCAIAAAB1IN9NAAAACXBIWXMAAAsTAAALEwEAmpwYYKUKF+Os3baUndC0pDnwNAmLy1SUr2Gw0luxQuV/AwC6cEhVV5VRrwAAAABJRU5ErkJggg==",
Dusan Kasan17e497e2017-04-10 22:44:22 +0200251 },
252 },
253 },
Dusan Kasan428369f2020-02-24 00:47:31 +0100254 8: {
255 mailData: imageContentExample,
256 subject: "Saying Hello",
257 from: []mail.Address{
258 {
259 Name: "John Doe",
260 Address: "jdoe@machine.example",
261 },
262 },
263 to: []mail.Address{
264 {
265 Name: "Mary Smith",
266 Address: "mary@example.net",
267 },
268 },
269 sender: mail.Address{
270 Name: "Michael Jones",
271 Address: "mjones@machine.example",
272 },
273 messageID: "1234@local.machine.example",
274 date: parseDate("Fri, 21 Nov 1997 09:55:06 -0600"),
275 contentType: `image/jpeg; x-unix-mode=0644; name="image.gif"`,
276 content: `GIF89a;`,
277 },
Dusan Kasane97c2f22020-04-04 10:50:00 +0200278 9: {
279 contentType: `multipart/mixed; boundary="0000000000007e2bb40587e36196"`,
280 mailData: textPlainInMultipart,
Maya Rashishbae116e2019-06-10 12:15:25 +0300281 subject: "Re: kern/54143 (virtualbox)",
282 from: []mail.Address{
283 {
284 Name: "Rares",
285 Address: "rares@example.com",
286 },
287 },
288 to: []mail.Address{
289 {
290 Name: "",
291 Address: "bugs@example.com",
292 },
293 },
294 date: parseDate("Fri, 02 May 2019 11:25:35 +0300"),
295 textBody: `plain text part`,
296 },
Dusan Kasan17e497e2017-04-10 22:44:22 +0200297 }
298
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200299 for index, td := range testData {
Dusan Kasan428369f2020-02-24 00:47:31 +0100300 e, err := Parse(strings.NewReader(td.mailData))
Antonio331b1182019-01-15 09:58:52 +0100301 if err != nil {
302 t.Error(err)
303 }
Dusan Kasan428369f2020-02-24 00:47:31 +0100304
305 if td.contentType != e.ContentType {
306 t.Errorf("[Test Case %v] Wrong content type. Expected: %s, Got: %s", index, td.contentType, e.ContentType)
307 }
308
309 if td.content != "" {
310 b, err := ioutil.ReadAll(e.Content)
311 if err != nil {
312 t.Error(err)
313 } else if td.content != string(b) {
314 t.Errorf("[Test Case %v] Wrong content. Expected: %s, Got: %s", index, td.content, string(b))
315 }
Dusan Kasan17e497e2017-04-10 22:44:22 +0200316 }
317
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200318 if td.subject != e.Subject {
319 t.Errorf("[Test Case %v] Wrong subject. Expected: %s, Got: %s", index, td.subject, e.Subject)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200320 }
321
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200322 if td.messageID != e.MessageID {
323 t.Errorf("[Test Case %v] Wrong messageID. Expected: '%s', Got: '%s'", index, td.messageID, e.MessageID)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200324 }
325
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200326 if !td.date.Equal(e.Date) {
327 t.Errorf("[Test Case %v] Wrong date. Expected: %v, Got: %v", index, td.date, e.Date)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200328 }
329
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200330 d := dereferenceAddressList(e.From)
331 if !assertAddressListEq(td.from, d) {
332 t.Errorf("[Test Case %v] Wrong from. Expected: %s, Got: %s", index, td.from, d)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200333 }
334
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200335 var sender mail.Address
336 if e.Sender != nil {
337 sender = *e.Sender
338 }
339 if td.sender != sender {
340 t.Errorf("[Test Case %v] Wrong sender. Expected: %s, Got: %s", index, td.sender, sender)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200341 }
342
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200343 d = dereferenceAddressList(e.To)
344 if !assertAddressListEq(td.to, d) {
345 t.Errorf("[Test Case %v] Wrong to. Expected: %s, Got: %s", index, td.to, d)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200346 }
347
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200348 d = dereferenceAddressList(e.Cc)
349 if !assertAddressListEq(td.cc, d) {
350 t.Errorf("[Test Case %v] Wrong cc. Expected: %s, Got: %s", index, td.cc, d)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200351 }
352
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200353 d = dereferenceAddressList(e.Bcc)
354 if !assertAddressListEq(td.bcc, d) {
355 t.Errorf("[Test Case %v] Wrong bcc. Expected: %s, Got: %s", index, td.bcc, d)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200356 }
357
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200358 if td.resentMessageID != e.ResentMessageID {
359 t.Errorf("[Test Case %v] Wrong resent messageID. Expected: '%s', Got: '%s'", index, td.resentMessageID, e.ResentMessageID)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200360 }
361
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200362 if !td.resentDate.Equal(e.ResentDate) && !td.resentDate.IsZero() && !e.ResentDate.IsZero() {
363 t.Errorf("[Test Case %v] Wrong resent date. Expected: %v, Got: %v", index, td.resentDate, e.ResentDate)
364 }
365
366 d = dereferenceAddressList(e.ResentFrom)
367 if !assertAddressListEq(td.resentFrom, d) {
368 t.Errorf("[Test Case %v] Wrong resent from. Expected: %s, Got: %s", index, td.resentFrom, d)
369 }
370
371 var resentSender mail.Address
372 if e.ResentSender != nil {
373 resentSender = *e.ResentSender
374 }
375 if td.resentSender != resentSender {
376 t.Errorf("[Test Case %v] Wrong resent sender. Expected: %s, Got: %s", index, td.resentSender, resentSender)
377 }
378
379 d = dereferenceAddressList(e.ResentTo)
380 if !assertAddressListEq(td.resentTo, d) {
381 t.Errorf("[Test Case %v] Wrong resent to. Expected: %s, Got: %s", index, td.resentTo, d)
382 }
383
384 d = dereferenceAddressList(e.ResentCc)
385 if !assertAddressListEq(td.resentCc, d) {
386 t.Errorf("[Test Case %v] Wrong resent cc. Expected: %s, Got: %s", index, td.resentCc, d)
387 }
388
389 d = dereferenceAddressList(e.ResentBcc)
390 if !assertAddressListEq(td.resentBcc, d) {
391 t.Errorf("[Test Case %v] Wrong resent bcc. Expected: %s, Got: %s", index, td.resentBcc, d)
392 }
393
394 if !assertSliceEq(td.inReplyTo, e.InReplyTo) {
395 t.Errorf("[Test Case %v] Wrong in reply to. Expected: %s, Got: %s", index, td.inReplyTo, e.InReplyTo)
396 }
397
398 if !assertSliceEq(td.references, e.References) {
399 t.Errorf("[Test Case %v] Wrong references. Expected: %s, Got: %s", index, td.references, e.References)
400 }
401
402 d = dereferenceAddressList(e.ReplyTo)
403 if !assertAddressListEq(td.replyTo, d) {
404 t.Errorf("[Test Case %v] Wrong reply to. Expected: %s, Got: %s", index, td.replyTo, d)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200405 }
406
407 if td.htmlBody != e.HTMLBody {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200408 t.Errorf("[Test Case %v] Wrong html body. Expected: '%s', Got: '%s'", index, td.htmlBody, e.HTMLBody)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200409 }
410
411 if td.textBody != e.TextBody {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200412 t.Errorf("[Test Case %v] Wrong text body. Expected: '%s', Got: '%s'", index, td.textBody, e.TextBody)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200413 }
414
Dusan Kasan17e497e2017-04-10 22:44:22 +0200415 if len(td.attachments) != len(e.Attachments) {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200416 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 +0200417 } else {
418 attachs := e.Attachments[:]
419
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200420 for _, ad := range td.attachments {
Dusan Kasan17e497e2017-04-10 22:44:22 +0200421 found := false
422
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200423 for i, ra := range attachs {
Dusan Kasan17e497e2017-04-10 22:44:22 +0200424 b, err := ioutil.ReadAll(ra.Data)
425 if err != nil {
426 t.Error(err)
427 }
428
429 encoded := base64.StdEncoding.EncodeToString(b)
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200430 if ra.Filename == ad.filename && encoded == ad.base64data && ra.ContentType == ad.contentType {
Dusan Kasan17e497e2017-04-10 22:44:22 +0200431 found = true
432 attachs = append(attachs[:i], attachs[i+1:]...)
433 }
434 }
435
436 if !found {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200437 t.Errorf("[Test Case %v] Attachment not found: %s", index, ad.filename)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200438 }
439 }
440
441 if len(attachs) != 0 {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200442 t.Errorf("[Test Case %v] Email contains %v unexpected attachments: %v", index, len(attachs), attachs)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200443 }
444 }
445
446 if len(td.embeddedFiles) != len(e.EmbeddedFiles) {
Dusan Kasan1a966482017-04-18 10:45:25 +0200447 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 +0200448 } else {
449 embeds := e.EmbeddedFiles[:]
450
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200451 for _, ad := range td.embeddedFiles {
Dusan Kasan17e497e2017-04-10 22:44:22 +0200452 found := false
453
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200454 for i, ra := range embeds {
Dusan Kasan17e497e2017-04-10 22:44:22 +0200455 b, err := ioutil.ReadAll(ra.Data)
456 if err != nil {
457 t.Error(err)
458 }
459
460 encoded := base64.StdEncoding.EncodeToString(b)
461
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200462 if ra.CID == ad.cid && encoded == ad.base64data && ra.ContentType == ad.contentType {
Dusan Kasan17e497e2017-04-10 22:44:22 +0200463 found = true
464 embeds = append(embeds[:i], embeds[i+1:]...)
465 }
466 }
467
468 if !found {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200469 t.Errorf("[Test Case %v] Embedded file not found: %s", index, ad.cid)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200470 }
471 }
472
473 if len(embeds) != 0 {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200474 t.Errorf("[Test Case %v] Email contains %v unexpected embedded files: %v", index, len(embeds), embeds)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200475 }
476 }
477 }
478}
479
480func parseDate(in string) time.Time {
481 out, err := time.Parse(time.RFC1123Z, in)
482 if err != nil {
483 panic(err)
484 }
485
486 return out
487}
488
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200489type attachmentData struct {
490 filename string
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200491 contentType string
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200492 base64data string
Dusan Kasan17e497e2017-04-10 22:44:22 +0200493}
494
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200495type embeddedFileData struct {
496 cid string
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200497 contentType string
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200498 base64data string
Dusan Kasan17e497e2017-04-10 22:44:22 +0200499}
500
501func assertSliceEq(a, b []string) bool {
502 if len(a) == len(b) && len(a) == 0 {
503 return true
504 }
505
506 if a == nil && b == nil {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200507 return true
Dusan Kasan17e497e2017-04-10 22:44:22 +0200508 }
509
510 if a == nil || b == nil {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200511 return false
Dusan Kasan17e497e2017-04-10 22:44:22 +0200512 }
513
514 if len(a) != len(b) {
515 return false
516 }
517
518 for i := range a {
519 if a[i] != b[i] {
520 return false
521 }
522 }
523
524 return true
525}
526
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200527func assertAddressListEq(a, b []mail.Address) bool {
528 if len(a) == len(b) && len(a) == 0 {
529 return true
530 }
531
532 if a == nil && b == nil {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200533 return true
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200534 }
535
536 if a == nil || b == nil {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200537 return false
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200538 }
539
540 if len(a) != len(b) {
541 return false
542 }
543
544 for i := range a {
545 if a[i] != b[i] {
546 return false
547 }
548 }
549
550 return true
551}
552
553func dereferenceAddressList(al []*mail.Address) (result []mail.Address) {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200554 for _, a := range al {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200555 result = append(result, *a)
556 }
557
558 return
559}
560
Dusan Kasan45ca2642017-04-18 10:39:35 +0200561var data1 = `From: =?UTF-8?Q?Peter_Pahol=C3=ADk?= <peter.paholik@gmail.com>
Dusan Kasan17e497e2017-04-10 22:44:22 +0200562Date: Fri, 7 Apr 2017 09:17:26 +0200
563Message-ID: <CACtgX4kNXE7T5XKSKeH_zEcfUUmf2vXVASxYjaaK9cCn-3zb_g@mail.gmail.com>
Dusan Kasanf4376a62017-05-23 21:03:55 +0200564Subject: =?UTF-8?Q?Peter_Pahol=C3=ADk?=
Dusan Kasan17e497e2017-04-10 22:44:22 +0200565To: dusan@kasan.sk
566Content-Type: multipart/mixed; boundary=f403045f1dcc043a44054c8e6bbf
567
568--f403045f1dcc043a44054c8e6bbf
569Content-Type: multipart/alternative; boundary=f403045f1dcc043a3f054c8e6bbd
570
571--f403045f1dcc043a3f054c8e6bbd
572Content-Type: text/plain; charset=UTF-8
573
574
575
576--f403045f1dcc043a3f054c8e6bbd
577Content-Type: text/html; charset=UTF-8
578
579<div dir="ltr"><br></div>
580
581--f403045f1dcc043a3f054c8e6bbd--
582--f403045f1dcc043a44054c8e6bbf
583Content-Type: application/pdf;
584 name="=?UTF-8?Q?Peter_Paholi=CC=81k_1?=
585 =?UTF-8?Q?_4_2017_2017=2D04=2D07=2Epdf?="
586Content-Disposition: attachment;
587 filename="=?UTF-8?Q?Peter_Paholi=CC=81k_1?=
588 =?UTF-8?Q?_4_2017_2017=2D04=2D07=2Epdf?="
589Content-Transfer-Encoding: base64
590X-Attachment-Id: f_j17i0f0d0
591
592JVBERi0xLjQNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFu
593Zyhlbi1VUykgL1N0cnVjdFRyZWVSb290IDY3IDAgUi9NYXJrSW5mbzw8L01hcmtlZCB0cnVlPj4v
594T3V0cHV0SW50ZW50c1s8PC9UeXBlL091dHB1dEludGVudC9TL0dUU19QREZBMS9PdXRwdXRDb25k
595ZXYgMzk1MzYyDQo+Pg0Kc3RhcnR4cmVmDQo0MTk4ODUNCiUlRU9GDQo=
596--f403045f1dcc043a44054c8e6bbf--
597`
598
Dusan Kasan45ca2642017-04-18 10:39:35 +0200599var data2 = `Subject: Re: Test Subject 2
Dusan Kasan17e497e2017-04-10 22:44:22 +0200600To: info@receiver.com
601References: <2f6b7595-c01e-46e5-42bc-f263e1c4282d@receiver.com>
602 <9ff38d03-c4ab-89b7-9328-e99d5e24e3ba@domain.com>
603Cc: Cc Man <ccman@gmail.com>
604From: Sender Man <sender@domain.com>
605Message-ID: <0e9a21b4-01dc-e5c1-dcd6-58ce5aa61f4f@receiver.com>
606Date: Fri, 7 Apr 2017 12:59:55 +0200
607User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:45.0)
608 Gecko/20100101 Thunderbird/45.8.0
609MIME-Version: 1.0
610In-Reply-To: <9ff38d03-c4ab-89b7-9328-e99d5e24e3ba@receiver.eu>
611Content-Type: multipart/alternative;
612 boundary="------------C70C0458A558E585ACB75FB4"
613
614This is a multi-part message in MIME format.
615--------------C70C0458A558E585ACB75FB4
616Content-Type: text/plain; charset=utf-8; format=flowed
617Content-Transfer-Encoding: 8bit
618
619First level
620> Second level
621>> Third level
622>
623
624
625--------------C70C0458A558E585ACB75FB4
626Content-Type: multipart/related;
627 boundary="------------5DB4A1356834BB602A5F88B2"
628
629
630--------------5DB4A1356834BB602A5F88B2
631Content-Type: text/html; charset=utf-8
632Content-Transfer-Encoding: 8bit
633
634<html>data<img src="part2.9599C449.04E5EC81@develhell.com"/></html>
635
636--------------5DB4A1356834BB602A5F88B2
637Content-Type: image/png
638Content-Transfer-Encoding: base64
639Content-ID: <part2.9599C449.04E5EC81@develhell.com>
640
641iVBORw0KGgoAAAANSUhEUgAAAQEAAAAYCAIAAAB1IN9NAAAACXBIWXMAAAsTAAALEwEAmpwY
642YKUKF+Os3baUndC0pDnwNAmLy1SUr2Gw0luxQuV/AwC6cEhVV5VRrwAAAABJRU5ErkJggg==
643--------------5DB4A1356834BB602A5F88B2
644
645--------------C70C0458A558E585ACB75FB4--
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200646`
647
Dusan Kasane97c2f22020-04-04 10:50:00 +0200648var textPlainInMultipart = `From: Rares <rares@example.com>
Maya Rashishbae116e2019-06-10 12:15:25 +0300649Date: Thu, 2 May 2019 11:25:35 +0300
650Subject: Re: kern/54143 (virtualbox)
651To: bugs@example.com
652Content-Type: multipart/mixed; boundary="0000000000007e2bb40587e36196"
653
654--0000000000007e2bb40587e36196
655Content-Type: text/plain; charset="UTF-8"
656
657plain text part
658--0000000000007e2bb40587e36196--
659`
660
Dusan Kasan45ca2642017-04-18 10:39:35 +0200661var rfc5322exampleA11 = `From: John Doe <jdoe@machine.example>
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200662Sender: Michael Jones <mjones@machine.example>
663To: Mary Smith <mary@example.net>
664Subject: Saying Hello
665Date: Fri, 21 Nov 1997 09:55:06 -0600
666Message-ID: <1234@local.machine.example>
667
668This is a message just to say hello.
669So, "Hello".
670`
671
Dusan Kasan45ca2642017-04-18 10:39:35 +0200672var rfc5322exampleA12 = `From: "Joe Q. Public" <john.q.public@example.com>
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200673To: Mary Smith <mary@x.test>, jdoe@example.org, Who? <one@y.test>
674Cc: <boss@nil.test>, "Giant; \"Big\" Box" <sysservices@example.net>
675Date: Tue, 1 Jul 2003 10:52:37 +0200
676Message-ID: <5678.21-Nov-1997@example.com>
677
678Hi everyone.
679`
680
681//todo: not yet implemented in net/mail
682//once there is support for this, add it
Dusan Kasan45ca2642017-04-18 10:39:35 +0200683var rfc5322exampleA13 = `From: Pete <pete@silly.example>
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200684To: A Group:Ed Jones <c@a.test>,joe@where.test,John <jdoe@one.test>;
685Cc: Undisclosed recipients:;
686Date: Thu, 13 Feb 1969 23:32:54 -0330
687Message-ID: <testabcd.1234@silly.example>
688
689Testing.
690`
691
692//we skipped the first message bcause it's the same as A 1.1
Dusan Kasan45ca2642017-04-18 10:39:35 +0200693var rfc5322exampleA2a = `From: Mary Smith <mary@example.net>
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200694To: John Doe <jdoe@machine.example>
695Reply-To: "Mary Smith: Personal Account" <smith@home.example>
696Subject: Re: Saying Hello
697Date: Fri, 21 Nov 1997 10:01:10 -0600
698Message-ID: <3456@example.net>
699In-Reply-To: <1234@local.machine.example>
700References: <1234@local.machine.example>
701
702This is a reply to your hello.
703`
704
Dusan Kasan45ca2642017-04-18 10:39:35 +0200705var rfc5322exampleA2b = `To: "Mary Smith: Personal Account" <smith@home.example>
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200706From: John Doe <jdoe@machine.example>
707Subject: Re: Saying Hello
708Date: Fri, 21 Nov 1997 11:00:00 -0600
709Message-ID: <abcd.1234@local.machine.test>
710In-Reply-To: <3456@example.net>
711References: <1234@local.machine.example> <3456@example.net>
712
713This is a reply to your reply.
714`
715
Dusan Kasan45ca2642017-04-18 10:39:35 +0200716var rfc5322exampleA3 = `Resent-From: Mary Smith <mary@example.net>
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200717Resent-To: Jane Brown <j-brown@other.example>
718Resent-Date: Mon, 24 Nov 1997 14:22:01 -0800
719Resent-Message-ID: <78910@example.net>
720From: John Doe <jdoe@machine.example>
721To: Mary Smith <mary@example.net>
722Subject: Saying Hello
723Date: Fri, 21 Nov 1997 09:55:06 -0600
724Message-ID: <1234@local.machine.example>
725
726This is a message just to say hello.
727So, "Hello".`
728
Dusan Kasan45ca2642017-04-18 10:39:35 +0200729var rfc5322exampleA4 = `Received: from x.y.test
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200730 by example.net
731 via TCP
732 with ESMTP
733 id ABC12345
734 for <mary@example.net>; 21 Nov 1997 10:05:43 -0600
735Received: from node.example by x.y.test; 21 Nov 1997 10:01:22 -0600
736From: John Doe <jdoe@node.example>
737To: Mary Smith <mary@example.net>
738Subject: Saying Hello
739Date: Fri, 21 Nov 1997 09:55:06 -0600
740Message-ID: <1234@local.node.example>
741
742This is a message just to say hello.
743So, "Hello".`
Dusan Kasan428369f2020-02-24 00:47:31 +0100744
745var imageContentExample = `From: John Doe <jdoe@machine.example>
746Sender: Michael Jones <mjones@machine.example>
747To: Mary Smith <mary@example.net>
748Subject: Saying Hello
749Date: Fri, 21 Nov 1997 09:55:06 -0600
750Message-ID: <1234@local.machine.example>
751Content-Type: image/jpeg;
752 x-unix-mode=0644;
753 name="image.gif"
754Content-Transfer-Encoding: base64
755
756R0lGODlhAQE7`