blob: e0457bb51c2734320f5b2859a2094d318d4d944f [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 Kasan4595dfe2017-04-13 00:38:24 +020016 subject string
17 date time.Time
18 from []mail.Address
19 sender mail.Address
20 to []mail.Address
21 replyTo []mail.Address
22 cc []mail.Address
23 bcc []mail.Address
24 messageID string
25 resentDate time.Time
26 resentFrom []mail.Address
27 resentSender mail.Address
28 resentTo []mail.Address
29 resentReplyTo []mail.Address
30 resentCc []mail.Address
31 resentBcc []mail.Address
Dusan Kasanb49ceb62017-04-13 00:00:36 +020032 resentMessageID string
Dusan Kasan4595dfe2017-04-13 00:38:24 +020033 inReplyTo []string
34 references []string
35 htmlBody string
36 textBody string
37 attachments []attachmentData
38 embeddedFiles []embeddedFileData
39 headerCheck func(mail.Header, *testing.T)
Dusan Kasan17e497e2017-04-10 22:44:22 +020040 }{
Dusan Kasanb49ceb62017-04-13 00:00:36 +020041 1: {
Dusan Kasan45ca2642017-04-18 10:39:35 +020042 mailData: rfc5322exampleA11,
Dusan Kasan4595dfe2017-04-13 00:38:24 +020043 subject: "Saying Hello",
Dusan Kasanb49ceb62017-04-13 00:00:36 +020044 from: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +020045 {
46 Name: "John Doe",
47 Address: "jdoe@machine.example",
48 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +020049 },
50 to: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +020051 {
52 Name: "Mary Smith",
53 Address: "mary@example.net",
54 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +020055 },
Dusan Kasan45ca2642017-04-18 10:39:35 +020056 sender: mail.Address{
57 Name: "Michael Jones",
58 Address: "mjones@machine.example",
59 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +020060 messageID: "1234@local.machine.example",
Dusan Kasan4595dfe2017-04-13 00:38:24 +020061 date: parseDate("Fri, 21 Nov 1997 09:55:06 -0600"),
Dusan Kasanb49ceb62017-04-13 00:00:36 +020062 textBody: `This is a message just to say hello.
63So, "Hello".`,
64 },
65 2: {
Dusan Kasan45ca2642017-04-18 10:39:35 +020066 mailData: rfc5322exampleA12,
Dusan Kasanb49ceb62017-04-13 00:00:36 +020067 from: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +020068 {
69 Name: "Joe Q. Public",
70 Address: "john.q.public@example.com",
71 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +020072 },
73 to: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +020074 {
75 Name: "Mary Smith",
76 Address: "mary@x.test",
77 },
78 {
79 Name: "",
80 Address: "jdoe@example.org",
81 },
82 {
83 Name: "Who?",
84 Address: "one@y.test",
85 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +020086 },
87 cc: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +020088 {
89 Name: "",
90 Address: "boss@nil.test",
91 },
92 {
93 Name: "Giant; \"Big\" Box",
94 Address: "sysservices@example.net",
95 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +020096 },
97 messageID: "5678.21-Nov-1997@example.com",
Dusan Kasan4595dfe2017-04-13 00:38:24 +020098 date: parseDate("Tue, 01 Jul 2003 10:52:37 +0200"),
99 textBody: `Hi everyone.`,
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200100 },
101 3: {
Dusan Kasan45ca2642017-04-18 10:39:35 +0200102 mailData: rfc5322exampleA2a,
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200103 subject: "Re: Saying Hello",
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200104 from: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200105 {
106 Name: "Mary Smith",
107 Address: "mary@example.net",
108 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200109 },
110 replyTo: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200111 {
112 Name: "Mary Smith: Personal Account",
113 Address: "smith@home.example",
114 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200115 },
116 to: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200117 {
118 Name: "John Doe",
119 Address: "jdoe@machine.example",
120 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200121 },
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200122 messageID: "3456@example.net",
123 inReplyTo: []string{"1234@local.machine.example"},
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200124 references: []string{"1234@local.machine.example"},
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200125 date: parseDate("Fri, 21 Nov 1997 10:01:10 -0600"),
126 textBody: `This is a reply to your hello.`,
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200127 },
128 4: {
Dusan Kasan45ca2642017-04-18 10:39:35 +0200129 mailData: rfc5322exampleA2b,
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200130 subject: "Re: Saying Hello",
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200131 from: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200132 {
133 Name: "John Doe",
134 Address: "jdoe@machine.example",
135 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200136 },
137 to: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200138 {
139 Name: "Mary Smith: Personal Account",
140 Address: "smith@home.example",
141 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200142 },
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200143 messageID: "abcd.1234@local.machine.test",
144 inReplyTo: []string{"3456@example.net"},
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200145 references: []string{"1234@local.machine.example", "3456@example.net"},
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200146 date: parseDate("Fri, 21 Nov 1997 11:00:00 -0600"),
147 textBody: `This is a reply to your reply.`,
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200148 },
149 5: {
Dusan Kasan45ca2642017-04-18 10:39:35 +0200150 mailData: rfc5322exampleA3,
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200151 subject: "Saying Hello",
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200152 from: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200153 {
154 Name: "John Doe",
155 Address: "jdoe@machine.example",
156 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200157 },
158 to: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200159 {
160 Name: "Mary Smith",
161 Address: "mary@example.net",
162 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200163 },
164 messageID: "1234@local.machine.example",
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200165 date: parseDate("Fri, 21 Nov 1997 09:55:06 -0600"),
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200166 resentFrom: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200167 {
168 Name: "Mary Smith",
169 Address: "mary@example.net",
170 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200171 },
172 resentTo: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200173 {
174 Name: "Jane Brown",
175 Address: "j-brown@other.example",
176 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200177 },
178 resentMessageID: "78910@example.net",
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200179 resentDate: parseDate("Mon, 24 Nov 1997 14:22:01 -0800"),
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200180 textBody: `This is a message just to say hello.
181So, "Hello".`,
182 },
183 6: {
Dusan Kasan45ca2642017-04-18 10:39:35 +0200184 mailData: data1,
Dusan Kasanf4376a62017-05-23 21:03:55 +0200185 subject: "Peter Paholík",
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200186 from: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200187 {
188 Name: "Peter Paholík",
189 Address: "peter.paholik@gmail.com",
190 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200191 },
192 to: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200193 {
194 Name: "",
195 Address: "dusan@kasan.sk",
196 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200197 },
Dusan Kasan17e497e2017-04-10 22:44:22 +0200198 messageID: "CACtgX4kNXE7T5XKSKeH_zEcfUUmf2vXVASxYjaaK9cCn-3zb_g@mail.gmail.com",
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200199 date: parseDate("Fri, 07 Apr 2017 09:17:26 +0200"),
200 htmlBody: "<div dir=\"ltr\"><br></div>",
Dusan Kasan17e497e2017-04-10 22:44:22 +0200201 attachments: []attachmentData{
202 {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200203 filename: "Peter Paholík 1 4 2017 2017-04-07.pdf",
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200204 contentType: "application/pdf",
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200205 base64data: "JVBERi0xLjQNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFuZyhlbi1VUykgL1N0cnVjdFRyZWVSb290IDY3IDAgUi9NYXJrSW5mbzw8L01hcmtlZCB0cnVlPj4vT3V0cHV0SW50ZW50c1s8PC9UeXBlL091dHB1dEludGVudC9TL0dUU19QREZBMS9PdXRwdXRDb25kZXYgMzk1MzYyDQo+Pg0Kc3RhcnR4cmVmDQo0MTk4ODUNCiUlRU9GDQo=",
Dusan Kasan17e497e2017-04-10 22:44:22 +0200206 },
207 },
208 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200209 7: {
Dusan Kasan45ca2642017-04-18 10:39:35 +0200210 mailData: data2,
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200211 subject: "Re: Test Subject 2",
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200212 from: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200213 {
214 Name: "Sender Man",
215 Address: "sender@domain.com",
216 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200217 },
218 to: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200219 {
220 Name: "",
221 Address: "info@receiver.com",
222 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200223 },
224 cc: []mail.Address{
Dusan Kasan45ca2642017-04-18 10:39:35 +0200225 {
226 Name: "Cc Man",
227 Address: "ccman@gmail.com",
228 },
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200229 },
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200230 messageID: "0e9a21b4-01dc-e5c1-dcd6-58ce5aa61f4f@receiver.com",
231 inReplyTo: []string{"9ff38d03-c4ab-89b7-9328-e99d5e24e3ba@receiver.eu"},
Dusan Kasan17e497e2017-04-10 22:44:22 +0200232 references: []string{"2f6b7595-c01e-46e5-42bc-f263e1c4282d@receiver.com", "9ff38d03-c4ab-89b7-9328-e99d5e24e3ba@domain.com"},
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200233 date: parseDate("Fri, 07 Apr 2017 12:59:55 +0200"),
234 htmlBody: `<html>data<img src="part2.9599C449.04E5EC81@develhell.com"/></html>`,
Dusan Kasan17e497e2017-04-10 22:44:22 +0200235 textBody: `First level
236> Second level
237>> Third level
238>
239`,
240 embeddedFiles: []embeddedFileData{
241 {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200242 cid: "part2.9599C449.04E5EC81@develhell.com",
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200243 contentType: "image/png",
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200244 base64data: "iVBORw0KGgoAAAANSUhEUgAAAQEAAAAYCAIAAAB1IN9NAAAACXBIWXMAAAsTAAALEwEAmpwYYKUKF+Os3baUndC0pDnwNAmLy1SUr2Gw0luxQuV/AwC6cEhVV5VRrwAAAABJRU5ErkJggg==",
Dusan Kasan17e497e2017-04-10 22:44:22 +0200245 },
246 },
247 },
248 }
249
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200250 for index, td := range testData {
Antonio331b1182019-01-15 09:58:52 +0100251 m, err := mail.ReadMessage(strings.NewReader(td.mailData))
252 if err != nil {
253 t.Error(err)
254 }
255 e, err := Parse(m)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200256 if err != nil {
257 t.Error(err)
258 }
259
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200260 if td.subject != e.Subject {
261 t.Errorf("[Test Case %v] Wrong subject. Expected: %s, Got: %s", index, td.subject, e.Subject)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200262 }
263
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200264 if td.messageID != e.MessageID {
265 t.Errorf("[Test Case %v] Wrong messageID. Expected: '%s', Got: '%s'", index, td.messageID, e.MessageID)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200266 }
267
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200268 if !td.date.Equal(e.Date) {
269 t.Errorf("[Test Case %v] Wrong date. Expected: %v, Got: %v", index, td.date, e.Date)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200270 }
271
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200272 d := dereferenceAddressList(e.From)
273 if !assertAddressListEq(td.from, d) {
274 t.Errorf("[Test Case %v] Wrong from. Expected: %s, Got: %s", index, td.from, d)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200275 }
276
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200277 var sender mail.Address
278 if e.Sender != nil {
279 sender = *e.Sender
280 }
281 if td.sender != sender {
282 t.Errorf("[Test Case %v] Wrong sender. Expected: %s, Got: %s", index, td.sender, sender)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200283 }
284
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200285 d = dereferenceAddressList(e.To)
286 if !assertAddressListEq(td.to, d) {
287 t.Errorf("[Test Case %v] Wrong to. Expected: %s, Got: %s", index, td.to, d)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200288 }
289
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200290 d = dereferenceAddressList(e.Cc)
291 if !assertAddressListEq(td.cc, d) {
292 t.Errorf("[Test Case %v] Wrong cc. Expected: %s, Got: %s", index, td.cc, d)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200293 }
294
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200295 d = dereferenceAddressList(e.Bcc)
296 if !assertAddressListEq(td.bcc, d) {
297 t.Errorf("[Test Case %v] Wrong bcc. Expected: %s, Got: %s", index, td.bcc, d)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200298 }
299
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200300 if td.resentMessageID != e.ResentMessageID {
301 t.Errorf("[Test Case %v] Wrong resent messageID. Expected: '%s', Got: '%s'", index, td.resentMessageID, e.ResentMessageID)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200302 }
303
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200304 if !td.resentDate.Equal(e.ResentDate) && !td.resentDate.IsZero() && !e.ResentDate.IsZero() {
305 t.Errorf("[Test Case %v] Wrong resent date. Expected: %v, Got: %v", index, td.resentDate, e.ResentDate)
306 }
307
308 d = dereferenceAddressList(e.ResentFrom)
309 if !assertAddressListEq(td.resentFrom, d) {
310 t.Errorf("[Test Case %v] Wrong resent from. Expected: %s, Got: %s", index, td.resentFrom, d)
311 }
312
313 var resentSender mail.Address
314 if e.ResentSender != nil {
315 resentSender = *e.ResentSender
316 }
317 if td.resentSender != resentSender {
318 t.Errorf("[Test Case %v] Wrong resent sender. Expected: %s, Got: %s", index, td.resentSender, resentSender)
319 }
320
321 d = dereferenceAddressList(e.ResentTo)
322 if !assertAddressListEq(td.resentTo, d) {
323 t.Errorf("[Test Case %v] Wrong resent to. Expected: %s, Got: %s", index, td.resentTo, d)
324 }
325
326 d = dereferenceAddressList(e.ResentCc)
327 if !assertAddressListEq(td.resentCc, d) {
328 t.Errorf("[Test Case %v] Wrong resent cc. Expected: %s, Got: %s", index, td.resentCc, d)
329 }
330
331 d = dereferenceAddressList(e.ResentBcc)
332 if !assertAddressListEq(td.resentBcc, d) {
333 t.Errorf("[Test Case %v] Wrong resent bcc. Expected: %s, Got: %s", index, td.resentBcc, d)
334 }
335
336 if !assertSliceEq(td.inReplyTo, e.InReplyTo) {
337 t.Errorf("[Test Case %v] Wrong in reply to. Expected: %s, Got: %s", index, td.inReplyTo, e.InReplyTo)
338 }
339
340 if !assertSliceEq(td.references, e.References) {
341 t.Errorf("[Test Case %v] Wrong references. Expected: %s, Got: %s", index, td.references, e.References)
342 }
343
344 d = dereferenceAddressList(e.ReplyTo)
345 if !assertAddressListEq(td.replyTo, d) {
346 t.Errorf("[Test Case %v] Wrong reply to. Expected: %s, Got: %s", index, td.replyTo, d)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200347 }
348
349 if td.htmlBody != e.HTMLBody {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200350 t.Errorf("[Test Case %v] Wrong html body. Expected: '%s', Got: '%s'", index, td.htmlBody, e.HTMLBody)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200351 }
352
353 if td.textBody != e.TextBody {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200354 t.Errorf("[Test Case %v] Wrong text body. Expected: '%s', Got: '%s'", index, td.textBody, e.TextBody)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200355 }
356
Dusan Kasan17e497e2017-04-10 22:44:22 +0200357 if len(td.attachments) != len(e.Attachments) {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200358 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 +0200359 } else {
360 attachs := e.Attachments[:]
361
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200362 for _, ad := range td.attachments {
Dusan Kasan17e497e2017-04-10 22:44:22 +0200363 found := false
364
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200365 for i, ra := range attachs {
Dusan Kasan17e497e2017-04-10 22:44:22 +0200366 b, err := ioutil.ReadAll(ra.Data)
367 if err != nil {
368 t.Error(err)
369 }
370
371 encoded := base64.StdEncoding.EncodeToString(b)
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200372 if ra.Filename == ad.filename && encoded == ad.base64data && ra.ContentType == ad.contentType {
Dusan Kasan17e497e2017-04-10 22:44:22 +0200373 found = true
374 attachs = append(attachs[:i], attachs[i+1:]...)
375 }
376 }
377
378 if !found {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200379 t.Errorf("[Test Case %v] Attachment not found: %s", index, ad.filename)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200380 }
381 }
382
383 if len(attachs) != 0 {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200384 t.Errorf("[Test Case %v] Email contains %v unexpected attachments: %v", index, len(attachs), attachs)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200385 }
386 }
387
388 if len(td.embeddedFiles) != len(e.EmbeddedFiles) {
Dusan Kasan1a966482017-04-18 10:45:25 +0200389 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 +0200390 } else {
391 embeds := e.EmbeddedFiles[:]
392
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200393 for _, ad := range td.embeddedFiles {
Dusan Kasan17e497e2017-04-10 22:44:22 +0200394 found := false
395
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200396 for i, ra := range embeds {
Dusan Kasan17e497e2017-04-10 22:44:22 +0200397 b, err := ioutil.ReadAll(ra.Data)
398 if err != nil {
399 t.Error(err)
400 }
401
402 encoded := base64.StdEncoding.EncodeToString(b)
403
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200404 if ra.CID == ad.cid && encoded == ad.base64data && ra.ContentType == ad.contentType {
Dusan Kasan17e497e2017-04-10 22:44:22 +0200405 found = true
406 embeds = append(embeds[:i], embeds[i+1:]...)
407 }
408 }
409
410 if !found {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200411 t.Errorf("[Test Case %v] Embedded file not found: %s", index, ad.cid)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200412 }
413 }
414
415 if len(embeds) != 0 {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200416 t.Errorf("[Test Case %v] Email contains %v unexpected embedded files: %v", index, len(embeds), embeds)
Dusan Kasan17e497e2017-04-10 22:44:22 +0200417 }
418 }
419 }
420}
421
422func parseDate(in string) time.Time {
423 out, err := time.Parse(time.RFC1123Z, in)
424 if err != nil {
425 panic(err)
426 }
427
428 return out
429}
430
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200431type attachmentData struct {
432 filename string
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200433 contentType string
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200434 base64data string
Dusan Kasan17e497e2017-04-10 22:44:22 +0200435}
436
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200437type embeddedFileData struct {
438 cid string
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200439 contentType string
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200440 base64data string
Dusan Kasan17e497e2017-04-10 22:44:22 +0200441}
442
443func assertSliceEq(a, b []string) bool {
444 if len(a) == len(b) && len(a) == 0 {
445 return true
446 }
447
448 if a == nil && b == nil {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200449 return true
Dusan Kasan17e497e2017-04-10 22:44:22 +0200450 }
451
452 if a == nil || b == nil {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200453 return false
Dusan Kasan17e497e2017-04-10 22:44:22 +0200454 }
455
456 if len(a) != len(b) {
457 return false
458 }
459
460 for i := range a {
461 if a[i] != b[i] {
462 return false
463 }
464 }
465
466 return true
467}
468
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200469func assertAddressListEq(a, b []mail.Address) bool {
470 if len(a) == len(b) && len(a) == 0 {
471 return true
472 }
473
474 if a == nil && b == nil {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200475 return true
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200476 }
477
478 if a == nil || b == nil {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200479 return false
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200480 }
481
482 if len(a) != len(b) {
483 return false
484 }
485
486 for i := range a {
487 if a[i] != b[i] {
488 return false
489 }
490 }
491
492 return true
493}
494
495func dereferenceAddressList(al []*mail.Address) (result []mail.Address) {
Dusan Kasan4595dfe2017-04-13 00:38:24 +0200496 for _, a := range al {
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200497 result = append(result, *a)
498 }
499
500 return
501}
502
Dusan Kasan45ca2642017-04-18 10:39:35 +0200503var data1 = `From: =?UTF-8?Q?Peter_Pahol=C3=ADk?= <peter.paholik@gmail.com>
Dusan Kasan17e497e2017-04-10 22:44:22 +0200504Date: Fri, 7 Apr 2017 09:17:26 +0200
505Message-ID: <CACtgX4kNXE7T5XKSKeH_zEcfUUmf2vXVASxYjaaK9cCn-3zb_g@mail.gmail.com>
Dusan Kasanf4376a62017-05-23 21:03:55 +0200506Subject: =?UTF-8?Q?Peter_Pahol=C3=ADk?=
Dusan Kasan17e497e2017-04-10 22:44:22 +0200507To: dusan@kasan.sk
508Content-Type: multipart/mixed; boundary=f403045f1dcc043a44054c8e6bbf
509
510--f403045f1dcc043a44054c8e6bbf
511Content-Type: multipart/alternative; boundary=f403045f1dcc043a3f054c8e6bbd
512
513--f403045f1dcc043a3f054c8e6bbd
514Content-Type: text/plain; charset=UTF-8
515
516
517
518--f403045f1dcc043a3f054c8e6bbd
519Content-Type: text/html; charset=UTF-8
520
521<div dir="ltr"><br></div>
522
523--f403045f1dcc043a3f054c8e6bbd--
524--f403045f1dcc043a44054c8e6bbf
525Content-Type: application/pdf;
526 name="=?UTF-8?Q?Peter_Paholi=CC=81k_1?=
527 =?UTF-8?Q?_4_2017_2017=2D04=2D07=2Epdf?="
528Content-Disposition: attachment;
529 filename="=?UTF-8?Q?Peter_Paholi=CC=81k_1?=
530 =?UTF-8?Q?_4_2017_2017=2D04=2D07=2Epdf?="
531Content-Transfer-Encoding: base64
532X-Attachment-Id: f_j17i0f0d0
533
534JVBERi0xLjQNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFu
535Zyhlbi1VUykgL1N0cnVjdFRyZWVSb290IDY3IDAgUi9NYXJrSW5mbzw8L01hcmtlZCB0cnVlPj4v
536T3V0cHV0SW50ZW50c1s8PC9UeXBlL091dHB1dEludGVudC9TL0dUU19QREZBMS9PdXRwdXRDb25k
537ZXYgMzk1MzYyDQo+Pg0Kc3RhcnR4cmVmDQo0MTk4ODUNCiUlRU9GDQo=
538--f403045f1dcc043a44054c8e6bbf--
539`
540
Dusan Kasan45ca2642017-04-18 10:39:35 +0200541var data2 = `Subject: Re: Test Subject 2
Dusan Kasan17e497e2017-04-10 22:44:22 +0200542To: info@receiver.com
543References: <2f6b7595-c01e-46e5-42bc-f263e1c4282d@receiver.com>
544 <9ff38d03-c4ab-89b7-9328-e99d5e24e3ba@domain.com>
545Cc: Cc Man <ccman@gmail.com>
546From: Sender Man <sender@domain.com>
547Message-ID: <0e9a21b4-01dc-e5c1-dcd6-58ce5aa61f4f@receiver.com>
548Date: Fri, 7 Apr 2017 12:59:55 +0200
549User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:45.0)
550 Gecko/20100101 Thunderbird/45.8.0
551MIME-Version: 1.0
552In-Reply-To: <9ff38d03-c4ab-89b7-9328-e99d5e24e3ba@receiver.eu>
553Content-Type: multipart/alternative;
554 boundary="------------C70C0458A558E585ACB75FB4"
555
556This is a multi-part message in MIME format.
557--------------C70C0458A558E585ACB75FB4
558Content-Type: text/plain; charset=utf-8; format=flowed
559Content-Transfer-Encoding: 8bit
560
561First level
562> Second level
563>> Third level
564>
565
566
567--------------C70C0458A558E585ACB75FB4
568Content-Type: multipart/related;
569 boundary="------------5DB4A1356834BB602A5F88B2"
570
571
572--------------5DB4A1356834BB602A5F88B2
573Content-Type: text/html; charset=utf-8
574Content-Transfer-Encoding: 8bit
575
576<html>data<img src="part2.9599C449.04E5EC81@develhell.com"/></html>
577
578--------------5DB4A1356834BB602A5F88B2
579Content-Type: image/png
580Content-Transfer-Encoding: base64
581Content-ID: <part2.9599C449.04E5EC81@develhell.com>
582
583iVBORw0KGgoAAAANSUhEUgAAAQEAAAAYCAIAAAB1IN9NAAAACXBIWXMAAAsTAAALEwEAmpwY
584YKUKF+Os3baUndC0pDnwNAmLy1SUr2Gw0luxQuV/AwC6cEhVV5VRrwAAAABJRU5ErkJggg==
585--------------5DB4A1356834BB602A5F88B2
586
587--------------C70C0458A558E585ACB75FB4--
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200588`
589
Dusan Kasan45ca2642017-04-18 10:39:35 +0200590var rfc5322exampleA11 = `From: John Doe <jdoe@machine.example>
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200591Sender: Michael Jones <mjones@machine.example>
592To: Mary Smith <mary@example.net>
593Subject: Saying Hello
594Date: Fri, 21 Nov 1997 09:55:06 -0600
595Message-ID: <1234@local.machine.example>
596
597This is a message just to say hello.
598So, "Hello".
599`
600
Dusan Kasan45ca2642017-04-18 10:39:35 +0200601var rfc5322exampleA12 = `From: "Joe Q. Public" <john.q.public@example.com>
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200602To: Mary Smith <mary@x.test>, jdoe@example.org, Who? <one@y.test>
603Cc: <boss@nil.test>, "Giant; \"Big\" Box" <sysservices@example.net>
604Date: Tue, 1 Jul 2003 10:52:37 +0200
605Message-ID: <5678.21-Nov-1997@example.com>
606
607Hi everyone.
608`
609
610//todo: not yet implemented in net/mail
611//once there is support for this, add it
Dusan Kasan45ca2642017-04-18 10:39:35 +0200612var rfc5322exampleA13 = `From: Pete <pete@silly.example>
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200613To: A Group:Ed Jones <c@a.test>,joe@where.test,John <jdoe@one.test>;
614Cc: Undisclosed recipients:;
615Date: Thu, 13 Feb 1969 23:32:54 -0330
616Message-ID: <testabcd.1234@silly.example>
617
618Testing.
619`
620
621//we skipped the first message bcause it's the same as A 1.1
Dusan Kasan45ca2642017-04-18 10:39:35 +0200622var rfc5322exampleA2a = `From: Mary Smith <mary@example.net>
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200623To: John Doe <jdoe@machine.example>
624Reply-To: "Mary Smith: Personal Account" <smith@home.example>
625Subject: Re: Saying Hello
626Date: Fri, 21 Nov 1997 10:01:10 -0600
627Message-ID: <3456@example.net>
628In-Reply-To: <1234@local.machine.example>
629References: <1234@local.machine.example>
630
631This is a reply to your hello.
632`
633
Dusan Kasan45ca2642017-04-18 10:39:35 +0200634var rfc5322exampleA2b = `To: "Mary Smith: Personal Account" <smith@home.example>
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200635From: John Doe <jdoe@machine.example>
636Subject: Re: Saying Hello
637Date: Fri, 21 Nov 1997 11:00:00 -0600
638Message-ID: <abcd.1234@local.machine.test>
639In-Reply-To: <3456@example.net>
640References: <1234@local.machine.example> <3456@example.net>
641
642This is a reply to your reply.
643`
644
Dusan Kasan45ca2642017-04-18 10:39:35 +0200645var rfc5322exampleA3 = `Resent-From: Mary Smith <mary@example.net>
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200646Resent-To: Jane Brown <j-brown@other.example>
647Resent-Date: Mon, 24 Nov 1997 14:22:01 -0800
648Resent-Message-ID: <78910@example.net>
649From: John Doe <jdoe@machine.example>
650To: Mary Smith <mary@example.net>
651Subject: Saying Hello
652Date: Fri, 21 Nov 1997 09:55:06 -0600
653Message-ID: <1234@local.machine.example>
654
655This is a message just to say hello.
656So, "Hello".`
657
Dusan Kasan45ca2642017-04-18 10:39:35 +0200658var rfc5322exampleA4 = `Received: from x.y.test
Dusan Kasanb49ceb62017-04-13 00:00:36 +0200659 by example.net
660 via TCP
661 with ESMTP
662 id ABC12345
663 for <mary@example.net>; 21 Nov 1997 10:05:43 -0600
664Received: from node.example by x.y.test; 21 Nov 1997 10:01:22 -0600
665From: John Doe <jdoe@node.example>
666To: Mary Smith <mary@example.net>
667Subject: Saying Hello
668Date: Fri, 21 Nov 1997 09:55:06 -0600
669Message-ID: <1234@local.node.example>
670
671This is a message just to say hello.
672So, "Hello".`