blob: 6796a29d77e011b080e770a4d3bfb19fdcd68b67 [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 Kasan17e497e2017-04-10 22:44:22 +0200278 }
279
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200280 for index, td := range testData {
Dusan Kasan428369f2020-02-24 00:47:31 +0100281 e, err := Parse(strings.NewReader(td.mailData))
Antonio331b1182019-01-15 09:58:52 +0100282 if err != nil {
283 t.Error(err)
284 }
Dusan Kasan428369f2020-02-24 00:47:31 +0100285
286 if td.contentType != e.ContentType {
287 t.Errorf("[Test Case %v] Wrong content type. Expected: %s, Got: %s", index, td.contentType, e.ContentType)
288 }
289
290 if td.content != "" {
291 b, err := ioutil.ReadAll(e.Content)
292 if err != nil {
293 t.Error(err)
294 } else if td.content != string(b) {
295 t.Errorf("[Test Case %v] Wrong content. Expected: %s, Got: %s", index, td.content, string(b))
296 }
Dusan Kasan17e497e2017-04-10 22:44:22 +0200297 }
298
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200299 if td.subject != e.Subject {
300 t.Errorf("[Test Case %v] Wrong subject. Expected: %s, Got: %s", index, td.subject, e.Subject)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200301 }
302
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200303 if td.messageID != e.MessageID {
304 t.Errorf("[Test Case %v] Wrong messageID. Expected: '%s', Got: '%s'", index, td.messageID, e.MessageID)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200305 }
306
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200307 if !td.date.Equal(e.Date) {
308 t.Errorf("[Test Case %v] Wrong date. Expected: %v, Got: %v", index, td.date, e.Date)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200309 }
310
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200311 d := dereferenceAddressList(e.From)
312 if !assertAddressListEq(td.from, d) {
313 t.Errorf("[Test Case %v] Wrong from. Expected: %s, Got: %s", index, td.from, d)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200314 }
315
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200316 var sender mail.Address
317 if e.Sender != nil {
318 sender = *e.Sender
319 }
320 if td.sender != sender {
321 t.Errorf("[Test Case %v] Wrong sender. Expected: %s, Got: %s", index, td.sender, sender)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200322 }
323
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200324 d = dereferenceAddressList(e.To)
325 if !assertAddressListEq(td.to, d) {
326 t.Errorf("[Test Case %v] Wrong to. Expected: %s, Got: %s", index, td.to, d)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200327 }
328
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200329 d = dereferenceAddressList(e.Cc)
330 if !assertAddressListEq(td.cc, d) {
331 t.Errorf("[Test Case %v] Wrong cc. Expected: %s, Got: %s", index, td.cc, d)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200332 }
333
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200334 d = dereferenceAddressList(e.Bcc)
335 if !assertAddressListEq(td.bcc, d) {
336 t.Errorf("[Test Case %v] Wrong bcc. Expected: %s, Got: %s", index, td.bcc, d)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200337 }
338
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200339 if td.resentMessageID != e.ResentMessageID {
340 t.Errorf("[Test Case %v] Wrong resent messageID. Expected: '%s', Got: '%s'", index, td.resentMessageID, e.ResentMessageID)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200341 }
342
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200343 if !td.resentDate.Equal(e.ResentDate) && !td.resentDate.IsZero() && !e.ResentDate.IsZero() {
344 t.Errorf("[Test Case %v] Wrong resent date. Expected: %v, Got: %v", index, td.resentDate, e.ResentDate)
345 }
346
347 d = dereferenceAddressList(e.ResentFrom)
348 if !assertAddressListEq(td.resentFrom, d) {
349 t.Errorf("[Test Case %v] Wrong resent from. Expected: %s, Got: %s", index, td.resentFrom, d)
350 }
351
352 var resentSender mail.Address
353 if e.ResentSender != nil {
354 resentSender = *e.ResentSender
355 }
356 if td.resentSender != resentSender {
357 t.Errorf("[Test Case %v] Wrong resent sender. Expected: %s, Got: %s", index, td.resentSender, resentSender)
358 }
359
360 d = dereferenceAddressList(e.ResentTo)
361 if !assertAddressListEq(td.resentTo, d) {
362 t.Errorf("[Test Case %v] Wrong resent to. Expected: %s, Got: %s", index, td.resentTo, d)
363 }
364
365 d = dereferenceAddressList(e.ResentCc)
366 if !assertAddressListEq(td.resentCc, d) {
367 t.Errorf("[Test Case %v] Wrong resent cc. Expected: %s, Got: %s", index, td.resentCc, d)
368 }
369
370 d = dereferenceAddressList(e.ResentBcc)
371 if !assertAddressListEq(td.resentBcc, d) {
372 t.Errorf("[Test Case %v] Wrong resent bcc. Expected: %s, Got: %s", index, td.resentBcc, d)
373 }
374
375 if !assertSliceEq(td.inReplyTo, e.InReplyTo) {
376 t.Errorf("[Test Case %v] Wrong in reply to. Expected: %s, Got: %s", index, td.inReplyTo, e.InReplyTo)
377 }
378
379 if !assertSliceEq(td.references, e.References) {
380 t.Errorf("[Test Case %v] Wrong references. Expected: %s, Got: %s", index, td.references, e.References)
381 }
382
383 d = dereferenceAddressList(e.ReplyTo)
384 if !assertAddressListEq(td.replyTo, d) {
385 t.Errorf("[Test Case %v] Wrong reply to. Expected: %s, Got: %s", index, td.replyTo, d)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200386 }
387
388 if td.htmlBody != e.HTMLBody {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200389 t.Errorf("[Test Case %v] Wrong html body. Expected: '%s', Got: '%s'", index, td.htmlBody, e.HTMLBody)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200390 }
391
392 if td.textBody != e.TextBody {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200393 t.Errorf("[Test Case %v] Wrong text body. Expected: '%s', Got: '%s'", index, td.textBody, e.TextBody)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200394 }
395
Dusan Kasan17e497e2017-04-10 22:44:22 +0200396 if len(td.attachments) != len(e.Attachments) {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200397 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 +0200398 } else {
399 attachs := e.Attachments[:]
400
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200401 for _, ad := range td.attachments {
Dusan Kasan17e497e2017-04-10 22:44:22 +0200402 found := false
403
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200404 for i, ra := range attachs {
Dusan Kasan17e497e2017-04-10 22:44:22 +0200405 b, err := ioutil.ReadAll(ra.Data)
406 if err != nil {
407 t.Error(err)
408 }
409
410 encoded := base64.StdEncoding.EncodeToString(b)
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200411 if ra.Filename == ad.filename && encoded == ad.base64data && ra.ContentType == ad.contentType {
Dusan Kasan17e497e2017-04-10 22:44:22 +0200412 found = true
413 attachs = append(attachs[:i], attachs[i+1:]...)
414 }
415 }
416
417 if !found {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200418 t.Errorf("[Test Case %v] Attachment not found: %s", index, ad.filename)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200419 }
420 }
421
422 if len(attachs) != 0 {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200423 t.Errorf("[Test Case %v] Email contains %v unexpected attachments: %v", index, len(attachs), attachs)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200424 }
425 }
426
427 if len(td.embeddedFiles) != len(e.EmbeddedFiles) {
Dusan Kasan1a966482017-04-18 10:45:25 +0200428 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 +0200429 } else {
430 embeds := e.EmbeddedFiles[:]
431
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200432 for _, ad := range td.embeddedFiles {
Dusan Kasan17e497e2017-04-10 22:44:22 +0200433 found := false
434
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200435 for i, ra := range embeds {
Dusan Kasan17e497e2017-04-10 22:44:22 +0200436 b, err := ioutil.ReadAll(ra.Data)
437 if err != nil {
438 t.Error(err)
439 }
440
441 encoded := base64.StdEncoding.EncodeToString(b)
442
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200443 if ra.CID == ad.cid && encoded == ad.base64data && ra.ContentType == ad.contentType {
Dusan Kasan17e497e2017-04-10 22:44:22 +0200444 found = true
445 embeds = append(embeds[:i], embeds[i+1:]...)
446 }
447 }
448
449 if !found {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200450 t.Errorf("[Test Case %v] Embedded file not found: %s", index, ad.cid)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200451 }
452 }
453
454 if len(embeds) != 0 {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200455 t.Errorf("[Test Case %v] Email contains %v unexpected embedded files: %v", index, len(embeds), embeds)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200456 }
457 }
458 }
459}
460
461func parseDate(in string) time.Time {
462 out, err := time.Parse(time.RFC1123Z, in)
463 if err != nil {
464 panic(err)
465 }
466
467 return out
468}
469
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200470type attachmentData struct {
471 filename string
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200472 contentType string
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200473 base64data string
Dusan Kasan17e497e2017-04-10 22:44:22 +0200474}
475
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200476type embeddedFileData struct {
477 cid string
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200478 contentType string
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200479 base64data string
Dusan Kasan17e497e2017-04-10 22:44:22 +0200480}
481
482func assertSliceEq(a, b []string) bool {
483 if len(a) == len(b) && len(a) == 0 {
484 return true
485 }
486
487 if a == nil && b == nil {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200488 return true
Dusan Kasan17e497e2017-04-10 22:44:22 +0200489 }
490
491 if a == nil || b == nil {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200492 return false
Dusan Kasan17e497e2017-04-10 22:44:22 +0200493 }
494
495 if len(a) != len(b) {
496 return false
497 }
498
499 for i := range a {
500 if a[i] != b[i] {
501 return false
502 }
503 }
504
505 return true
506}
507
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200508func assertAddressListEq(a, b []mail.Address) bool {
509 if len(a) == len(b) && len(a) == 0 {
510 return true
511 }
512
513 if a == nil && b == nil {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200514 return true
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200515 }
516
517 if a == nil || b == nil {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200518 return false
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200519 }
520
521 if len(a) != len(b) {
522 return false
523 }
524
525 for i := range a {
526 if a[i] != b[i] {
527 return false
528 }
529 }
530
531 return true
532}
533
534func dereferenceAddressList(al []*mail.Address) (result []mail.Address) {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200535 for _, a := range al {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200536 result = append(result, *a)
537 }
538
539 return
540}
541
Dusan Kasan45ca2642017-04-18 10:39:35 +0200542var data1 = `From: =?UTF-8?Q?Peter_Pahol=C3=ADk?= <peter.paholik@gmail.com>
Dusan Kasan17e497e2017-04-10 22:44:22 +0200543Date: Fri, 7 Apr 2017 09:17:26 +0200
544Message-ID: <CACtgX4kNXE7T5XKSKeH_zEcfUUmf2vXVASxYjaaK9cCn-3zb_g@mail.gmail.com>
Dusan Kasanf4376a62017-05-23 21:03:55 +0200545Subject: =?UTF-8?Q?Peter_Pahol=C3=ADk?=
Dusan Kasan17e497e2017-04-10 22:44:22 +0200546To: dusan@kasan.sk
547Content-Type: multipart/mixed; boundary=f403045f1dcc043a44054c8e6bbf
548
549--f403045f1dcc043a44054c8e6bbf
550Content-Type: multipart/alternative; boundary=f403045f1dcc043a3f054c8e6bbd
551
552--f403045f1dcc043a3f054c8e6bbd
553Content-Type: text/plain; charset=UTF-8
554
555
556
557--f403045f1dcc043a3f054c8e6bbd
558Content-Type: text/html; charset=UTF-8
559
560<div dir="ltr"><br></div>
561
562--f403045f1dcc043a3f054c8e6bbd--
563--f403045f1dcc043a44054c8e6bbf
564Content-Type: application/pdf;
565 name="=?UTF-8?Q?Peter_Paholi=CC=81k_1?=
566 =?UTF-8?Q?_4_2017_2017=2D04=2D07=2Epdf?="
567Content-Disposition: attachment;
568 filename="=?UTF-8?Q?Peter_Paholi=CC=81k_1?=
569 =?UTF-8?Q?_4_2017_2017=2D04=2D07=2Epdf?="
570Content-Transfer-Encoding: base64
571X-Attachment-Id: f_j17i0f0d0
572
573JVBERi0xLjQNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFu
574Zyhlbi1VUykgL1N0cnVjdFRyZWVSb290IDY3IDAgUi9NYXJrSW5mbzw8L01hcmtlZCB0cnVlPj4v
575T3V0cHV0SW50ZW50c1s8PC9UeXBlL091dHB1dEludGVudC9TL0dUU19QREZBMS9PdXRwdXRDb25k
576ZXYgMzk1MzYyDQo+Pg0Kc3RhcnR4cmVmDQo0MTk4ODUNCiUlRU9GDQo=
577--f403045f1dcc043a44054c8e6bbf--
578`
579
Dusan Kasan45ca2642017-04-18 10:39:35 +0200580var data2 = `Subject: Re: Test Subject 2
Dusan Kasan17e497e2017-04-10 22:44:22 +0200581To: info@receiver.com
582References: <2f6b7595-c01e-46e5-42bc-f263e1c4282d@receiver.com>
583 <9ff38d03-c4ab-89b7-9328-e99d5e24e3ba@domain.com>
584Cc: Cc Man <ccman@gmail.com>
585From: Sender Man <sender@domain.com>
586Message-ID: <0e9a21b4-01dc-e5c1-dcd6-58ce5aa61f4f@receiver.com>
587Date: Fri, 7 Apr 2017 12:59:55 +0200
588User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:45.0)
589 Gecko/20100101 Thunderbird/45.8.0
590MIME-Version: 1.0
591In-Reply-To: <9ff38d03-c4ab-89b7-9328-e99d5e24e3ba@receiver.eu>
592Content-Type: multipart/alternative;
593 boundary="------------C70C0458A558E585ACB75FB4"
594
595This is a multi-part message in MIME format.
596--------------C70C0458A558E585ACB75FB4
597Content-Type: text/plain; charset=utf-8; format=flowed
598Content-Transfer-Encoding: 8bit
599
600First level
601> Second level
602>> Third level
603>
604
605
606--------------C70C0458A558E585ACB75FB4
607Content-Type: multipart/related;
608 boundary="------------5DB4A1356834BB602A5F88B2"
609
610
611--------------5DB4A1356834BB602A5F88B2
612Content-Type: text/html; charset=utf-8
613Content-Transfer-Encoding: 8bit
614
615<html>data<img src="part2.9599C449.04E5EC81@develhell.com"/></html>
616
617--------------5DB4A1356834BB602A5F88B2
618Content-Type: image/png
619Content-Transfer-Encoding: base64
620Content-ID: <part2.9599C449.04E5EC81@develhell.com>
621
622iVBORw0KGgoAAAANSUhEUgAAAQEAAAAYCAIAAAB1IN9NAAAACXBIWXMAAAsTAAALEwEAmpwY
623YKUKF+Os3baUndC0pDnwNAmLy1SUr2Gw0luxQuV/AwC6cEhVV5VRrwAAAABJRU5ErkJggg==
624--------------5DB4A1356834BB602A5F88B2
625
626--------------C70C0458A558E585ACB75FB4--
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200627`
628
Dusan Kasan45ca2642017-04-18 10:39:35 +0200629var rfc5322exampleA11 = `From: John Doe <jdoe@machine.example>
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200630Sender: Michael Jones <mjones@machine.example>
631To: Mary Smith <mary@example.net>
632Subject: Saying Hello
633Date: Fri, 21 Nov 1997 09:55:06 -0600
634Message-ID: <1234@local.machine.example>
635
636This is a message just to say hello.
637So, "Hello".
638`
639
Dusan Kasan45ca2642017-04-18 10:39:35 +0200640var rfc5322exampleA12 = `From: "Joe Q. Public" <john.q.public@example.com>
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200641To: Mary Smith <mary@x.test>, jdoe@example.org, Who? <one@y.test>
642Cc: <boss@nil.test>, "Giant; \"Big\" Box" <sysservices@example.net>
643Date: Tue, 1 Jul 2003 10:52:37 +0200
644Message-ID: <5678.21-Nov-1997@example.com>
645
646Hi everyone.
647`
648
649//todo: not yet implemented in net/mail
650//once there is support for this, add it
Dusan Kasan45ca2642017-04-18 10:39:35 +0200651var rfc5322exampleA13 = `From: Pete <pete@silly.example>
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200652To: A Group:Ed Jones <c@a.test>,joe@where.test,John <jdoe@one.test>;
653Cc: Undisclosed recipients:;
654Date: Thu, 13 Feb 1969 23:32:54 -0330
655Message-ID: <testabcd.1234@silly.example>
656
657Testing.
658`
659
660//we skipped the first message bcause it's the same as A 1.1
Dusan Kasan45ca2642017-04-18 10:39:35 +0200661var rfc5322exampleA2a = `From: Mary Smith <mary@example.net>
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200662To: John Doe <jdoe@machine.example>
663Reply-To: "Mary Smith: Personal Account" <smith@home.example>
664Subject: Re: Saying Hello
665Date: Fri, 21 Nov 1997 10:01:10 -0600
666Message-ID: <3456@example.net>
667In-Reply-To: <1234@local.machine.example>
668References: <1234@local.machine.example>
669
670This is a reply to your hello.
671`
672
Dusan Kasan45ca2642017-04-18 10:39:35 +0200673var rfc5322exampleA2b = `To: "Mary Smith: Personal Account" <smith@home.example>
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200674From: John Doe <jdoe@machine.example>
675Subject: Re: Saying Hello
676Date: Fri, 21 Nov 1997 11:00:00 -0600
677Message-ID: <abcd.1234@local.machine.test>
678In-Reply-To: <3456@example.net>
679References: <1234@local.machine.example> <3456@example.net>
680
681This is a reply to your reply.
682`
683
Dusan Kasan45ca2642017-04-18 10:39:35 +0200684var rfc5322exampleA3 = `Resent-From: Mary Smith <mary@example.net>
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200685Resent-To: Jane Brown <j-brown@other.example>
686Resent-Date: Mon, 24 Nov 1997 14:22:01 -0800
687Resent-Message-ID: <78910@example.net>
688From: John Doe <jdoe@machine.example>
689To: Mary Smith <mary@example.net>
690Subject: Saying Hello
691Date: Fri, 21 Nov 1997 09:55:06 -0600
692Message-ID: <1234@local.machine.example>
693
694This is a message just to say hello.
695So, "Hello".`
696
Dusan Kasan45ca2642017-04-18 10:39:35 +0200697var rfc5322exampleA4 = `Received: from x.y.test
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200698 by example.net
699 via TCP
700 with ESMTP
701 id ABC12345
702 for <mary@example.net>; 21 Nov 1997 10:05:43 -0600
703Received: from node.example by x.y.test; 21 Nov 1997 10:01:22 -0600
704From: John Doe <jdoe@node.example>
705To: Mary Smith <mary@example.net>
706Subject: Saying Hello
707Date: Fri, 21 Nov 1997 09:55:06 -0600
708Message-ID: <1234@local.node.example>
709
710This is a message just to say hello.
711So, "Hello".`
Dusan Kasan428369f2020-02-24 00:47:31 +0100712
713var imageContentExample = `From: John Doe <jdoe@machine.example>
714Sender: Michael Jones <mjones@machine.example>
715To: Mary Smith <mary@example.net>
716Subject: Saying Hello
717Date: Fri, 21 Nov 1997 09:55:06 -0600
718Message-ID: <1234@local.machine.example>
719Content-Type: image/jpeg;
720 x-unix-mode=0644;
721 name="image.gif"
722Content-Transfer-Encoding: base64
723
724R0lGODlhAQE7`