Dusan Kasan | c2129de | 2017-04-13 10:42:21 +0200 | [diff] [blame] | 1 | package parsemail |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 2 | |
| 3 | import ( |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 4 | "encoding/base64" |
| 5 | "io/ioutil" |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 6 | "net/mail" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 10 | ) |
| 11 | |
| 12 | func TestParseEmail(t *testing.T) { |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 13 | var testData = map[int]struct { |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 14 | mailData string |
| 15 | |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 16 | 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 Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 32 | resentMessageID string |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 33 | 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 Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 40 | }{ |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 41 | 1: { |
| 42 | mailData: RFC5322_Example_A11, |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 43 | subject: "Saying Hello", |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 44 | from: []mail.Address{ |
| 45 | {"John Doe", "jdoe@machine.example"}, |
| 46 | }, |
| 47 | to: []mail.Address{ |
| 48 | {"Mary Smith", "mary@example.net"}, |
| 49 | }, |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 50 | sender: mail.Address{"Michael Jones", "mjones@machine.example"}, |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 51 | messageID: "1234@local.machine.example", |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 52 | date: parseDate("Fri, 21 Nov 1997 09:55:06 -0600"), |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 53 | textBody: `This is a message just to say hello. |
| 54 | So, "Hello".`, |
| 55 | }, |
| 56 | 2: { |
| 57 | mailData: RFC5322_Example_A12, |
| 58 | from: []mail.Address{ |
| 59 | {"Joe Q. Public", "john.q.public@example.com"}, |
| 60 | }, |
| 61 | to: []mail.Address{ |
| 62 | {"Mary Smith", "mary@x.test"}, |
| 63 | {"", "jdoe@example.org"}, |
| 64 | {"Who?", "one@y.test"}, |
| 65 | }, |
| 66 | cc: []mail.Address{ |
| 67 | {"", "boss@nil.test"}, |
| 68 | {"Giant; \"Big\" Box", "sysservices@example.net"}, |
| 69 | }, |
| 70 | messageID: "5678.21-Nov-1997@example.com", |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 71 | date: parseDate("Tue, 01 Jul 2003 10:52:37 +0200"), |
| 72 | textBody: `Hi everyone.`, |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 73 | }, |
| 74 | 3: { |
| 75 | mailData: RFC5322_Example_A2a, |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 76 | subject: "Re: Saying Hello", |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 77 | from: []mail.Address{ |
| 78 | {"Mary Smith", "mary@example.net"}, |
| 79 | }, |
| 80 | replyTo: []mail.Address{ |
| 81 | {"Mary Smith: Personal Account", "smith@home.example"}, |
| 82 | }, |
| 83 | to: []mail.Address{ |
| 84 | {"John Doe", "jdoe@machine.example"}, |
| 85 | }, |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 86 | messageID: "3456@example.net", |
| 87 | inReplyTo: []string{"1234@local.machine.example"}, |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 88 | references: []string{"1234@local.machine.example"}, |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 89 | date: parseDate("Fri, 21 Nov 1997 10:01:10 -0600"), |
| 90 | textBody: `This is a reply to your hello.`, |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 91 | }, |
| 92 | 4: { |
| 93 | mailData: RFC5322_Example_A2b, |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 94 | subject: "Re: Saying Hello", |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 95 | from: []mail.Address{ |
| 96 | {"John Doe", "jdoe@machine.example"}, |
| 97 | }, |
| 98 | to: []mail.Address{ |
| 99 | {"Mary Smith: Personal Account", "smith@home.example"}, |
| 100 | }, |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 101 | messageID: "abcd.1234@local.machine.test", |
| 102 | inReplyTo: []string{"3456@example.net"}, |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 103 | references: []string{"1234@local.machine.example", "3456@example.net"}, |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 104 | date: parseDate("Fri, 21 Nov 1997 11:00:00 -0600"), |
| 105 | textBody: `This is a reply to your reply.`, |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 106 | }, |
| 107 | 5: { |
| 108 | mailData: RFC5322_Example_A3, |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 109 | subject: "Saying Hello", |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 110 | from: []mail.Address{ |
| 111 | {"John Doe", "jdoe@machine.example"}, |
| 112 | }, |
| 113 | to: []mail.Address{ |
| 114 | {"Mary Smith", "mary@example.net"}, |
| 115 | }, |
| 116 | messageID: "1234@local.machine.example", |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 117 | date: parseDate("Fri, 21 Nov 1997 09:55:06 -0600"), |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 118 | resentFrom: []mail.Address{ |
| 119 | {"Mary Smith", "mary@example.net"}, |
| 120 | }, |
| 121 | resentTo: []mail.Address{ |
| 122 | {"Jane Brown", "j-brown@other.example"}, |
| 123 | }, |
| 124 | resentMessageID: "78910@example.net", |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 125 | resentDate: parseDate("Mon, 24 Nov 1997 14:22:01 -0800"), |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 126 | textBody: `This is a message just to say hello. |
| 127 | So, "Hello".`, |
| 128 | }, |
| 129 | 6: { |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 130 | mailData: Data1, |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 131 | subject: "Test Subject 1", |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 132 | from: []mail.Address{ |
| 133 | {"Peter Paholík", "peter.paholik@gmail.com"}, |
| 134 | }, |
| 135 | to: []mail.Address{ |
| 136 | {"", "dusan@kasan.sk"}, |
| 137 | }, |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 138 | messageID: "CACtgX4kNXE7T5XKSKeH_zEcfUUmf2vXVASxYjaaK9cCn-3zb_g@mail.gmail.com", |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 139 | date: parseDate("Fri, 07 Apr 2017 09:17:26 +0200"), |
| 140 | htmlBody: "<div dir=\"ltr\"><br></div>", |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 141 | attachments: []attachmentData{ |
| 142 | { |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 143 | filename: "Peter Paholík 1 4 2017 2017-04-07.pdf", |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 144 | contentType: "application/pdf", |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 145 | base64data: "JVBERi0xLjQNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFuZyhlbi1VUykgL1N0cnVjdFRyZWVSb290IDY3IDAgUi9NYXJrSW5mbzw8L01hcmtlZCB0cnVlPj4vT3V0cHV0SW50ZW50c1s8PC9UeXBlL091dHB1dEludGVudC9TL0dUU19QREZBMS9PdXRwdXRDb25kZXYgMzk1MzYyDQo+Pg0Kc3RhcnR4cmVmDQo0MTk4ODUNCiUlRU9GDQo=", |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 146 | }, |
| 147 | }, |
| 148 | }, |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 149 | 7: { |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 150 | mailData: Data2, |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 151 | subject: "Re: Test Subject 2", |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 152 | from: []mail.Address{ |
| 153 | {"Sender Man", "sender@domain.com"}, |
| 154 | }, |
| 155 | to: []mail.Address{ |
| 156 | {"", "info@receiver.com"}, |
| 157 | }, |
| 158 | cc: []mail.Address{ |
| 159 | {"Cc Man", "ccman@gmail.com"}, |
| 160 | }, |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 161 | messageID: "0e9a21b4-01dc-e5c1-dcd6-58ce5aa61f4f@receiver.com", |
| 162 | inReplyTo: []string{"9ff38d03-c4ab-89b7-9328-e99d5e24e3ba@receiver.eu"}, |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 163 | references: []string{"2f6b7595-c01e-46e5-42bc-f263e1c4282d@receiver.com", "9ff38d03-c4ab-89b7-9328-e99d5e24e3ba@domain.com"}, |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 164 | date: parseDate("Fri, 07 Apr 2017 12:59:55 +0200"), |
| 165 | htmlBody: `<html>data<img src="part2.9599C449.04E5EC81@develhell.com"/></html>`, |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 166 | textBody: `First level |
| 167 | > Second level |
| 168 | >> Third level |
| 169 | > |
| 170 | `, |
| 171 | embeddedFiles: []embeddedFileData{ |
| 172 | { |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 173 | cid: "part2.9599C449.04E5EC81@develhell.com", |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 174 | contentType: "image/png", |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 175 | base64data: "iVBORw0KGgoAAAANSUhEUgAAAQEAAAAYCAIAAAB1IN9NAAAACXBIWXMAAAsTAAALEwEAmpwYYKUKF+Os3baUndC0pDnwNAmLy1SUr2Gw0luxQuV/AwC6cEhVV5VRrwAAAABJRU5ErkJggg==", |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 176 | }, |
| 177 | }, |
| 178 | }, |
| 179 | } |
| 180 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 181 | for index, td := range testData { |
Dusan Kasan | c2129de | 2017-04-13 10:42:21 +0200 | [diff] [blame] | 182 | e, err := Parse(strings.NewReader(td.mailData)) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 183 | if err != nil { |
| 184 | t.Error(err) |
| 185 | } |
| 186 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 187 | if td.subject != e.Subject { |
| 188 | t.Errorf("[Test Case %v] Wrong subject. Expected: %s, Got: %s", index, td.subject, e.Subject) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 189 | } |
| 190 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 191 | if td.messageID != e.MessageID { |
| 192 | t.Errorf("[Test Case %v] Wrong messageID. Expected: '%s', Got: '%s'", index, td.messageID, e.MessageID) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 193 | } |
| 194 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 195 | if !td.date.Equal(e.Date) { |
| 196 | t.Errorf("[Test Case %v] Wrong date. Expected: %v, Got: %v", index, td.date, e.Date) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 197 | } |
| 198 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 199 | d := dereferenceAddressList(e.From) |
| 200 | if !assertAddressListEq(td.from, d) { |
| 201 | t.Errorf("[Test Case %v] Wrong from. Expected: %s, Got: %s", index, td.from, d) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 202 | } |
| 203 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 204 | var sender mail.Address |
| 205 | if e.Sender != nil { |
| 206 | sender = *e.Sender |
| 207 | } |
| 208 | if td.sender != sender { |
| 209 | t.Errorf("[Test Case %v] Wrong sender. Expected: %s, Got: %s", index, td.sender, sender) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 210 | } |
| 211 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 212 | d = dereferenceAddressList(e.To) |
| 213 | if !assertAddressListEq(td.to, d) { |
| 214 | t.Errorf("[Test Case %v] Wrong to. Expected: %s, Got: %s", index, td.to, d) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 217 | d = dereferenceAddressList(e.Cc) |
| 218 | if !assertAddressListEq(td.cc, d) { |
| 219 | t.Errorf("[Test Case %v] Wrong cc. Expected: %s, Got: %s", index, td.cc, d) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 220 | } |
| 221 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 222 | d = dereferenceAddressList(e.Bcc) |
| 223 | if !assertAddressListEq(td.bcc, d) { |
| 224 | t.Errorf("[Test Case %v] Wrong bcc. Expected: %s, Got: %s", index, td.bcc, d) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 225 | } |
| 226 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 227 | if td.resentMessageID != e.ResentMessageID { |
| 228 | t.Errorf("[Test Case %v] Wrong resent messageID. Expected: '%s', Got: '%s'", index, td.resentMessageID, e.ResentMessageID) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 231 | if !td.resentDate.Equal(e.ResentDate) && !td.resentDate.IsZero() && !e.ResentDate.IsZero() { |
| 232 | t.Errorf("[Test Case %v] Wrong resent date. Expected: %v, Got: %v", index, td.resentDate, e.ResentDate) |
| 233 | } |
| 234 | |
| 235 | d = dereferenceAddressList(e.ResentFrom) |
| 236 | if !assertAddressListEq(td.resentFrom, d) { |
| 237 | t.Errorf("[Test Case %v] Wrong resent from. Expected: %s, Got: %s", index, td.resentFrom, d) |
| 238 | } |
| 239 | |
| 240 | var resentSender mail.Address |
| 241 | if e.ResentSender != nil { |
| 242 | resentSender = *e.ResentSender |
| 243 | } |
| 244 | if td.resentSender != resentSender { |
| 245 | t.Errorf("[Test Case %v] Wrong resent sender. Expected: %s, Got: %s", index, td.resentSender, resentSender) |
| 246 | } |
| 247 | |
| 248 | d = dereferenceAddressList(e.ResentTo) |
| 249 | if !assertAddressListEq(td.resentTo, d) { |
| 250 | t.Errorf("[Test Case %v] Wrong resent to. Expected: %s, Got: %s", index, td.resentTo, d) |
| 251 | } |
| 252 | |
| 253 | d = dereferenceAddressList(e.ResentCc) |
| 254 | if !assertAddressListEq(td.resentCc, d) { |
| 255 | t.Errorf("[Test Case %v] Wrong resent cc. Expected: %s, Got: %s", index, td.resentCc, d) |
| 256 | } |
| 257 | |
| 258 | d = dereferenceAddressList(e.ResentBcc) |
| 259 | if !assertAddressListEq(td.resentBcc, d) { |
| 260 | t.Errorf("[Test Case %v] Wrong resent bcc. Expected: %s, Got: %s", index, td.resentBcc, d) |
| 261 | } |
| 262 | |
| 263 | if !assertSliceEq(td.inReplyTo, e.InReplyTo) { |
| 264 | t.Errorf("[Test Case %v] Wrong in reply to. Expected: %s, Got: %s", index, td.inReplyTo, e.InReplyTo) |
| 265 | } |
| 266 | |
| 267 | if !assertSliceEq(td.references, e.References) { |
| 268 | t.Errorf("[Test Case %v] Wrong references. Expected: %s, Got: %s", index, td.references, e.References) |
| 269 | } |
| 270 | |
| 271 | d = dereferenceAddressList(e.ReplyTo) |
| 272 | if !assertAddressListEq(td.replyTo, d) { |
| 273 | t.Errorf("[Test Case %v] Wrong reply to. Expected: %s, Got: %s", index, td.replyTo, d) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | if td.htmlBody != e.HTMLBody { |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 277 | t.Errorf("[Test Case %v] Wrong html body. Expected: '%s', Got: '%s'", index, td.htmlBody, e.HTMLBody) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | if td.textBody != e.TextBody { |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 281 | t.Errorf("[Test Case %v] Wrong text body. Expected: '%s', Got: '%s'", index, td.textBody, e.TextBody) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 282 | } |
| 283 | |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 284 | if len(td.attachments) != len(e.Attachments) { |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 285 | t.Errorf("[Test Case %v] Incorrect number of attachments! Expected: %v, Got: %v.", index, len(td.attachments), len(e.Attachments)) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 286 | } else { |
| 287 | attachs := e.Attachments[:] |
| 288 | |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 289 | for _, ad := range td.attachments { |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 290 | found := false |
| 291 | |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 292 | for i, ra := range attachs { |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 293 | b, err := ioutil.ReadAll(ra.Data) |
| 294 | if err != nil { |
| 295 | t.Error(err) |
| 296 | } |
| 297 | |
| 298 | encoded := base64.StdEncoding.EncodeToString(b) |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 299 | if ra.Filename == ad.filename && encoded == ad.base64data && ra.ContentType == ad.contentType { |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 300 | found = true |
| 301 | attachs = append(attachs[:i], attachs[i+1:]...) |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | if !found { |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 306 | t.Errorf("[Test Case %v] Attachment not found: %s", index, ad.filename) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | if len(attachs) != 0 { |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 311 | t.Errorf("[Test Case %v] Email contains %v unexpected attachments: %v", index, len(attachs), attachs) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | if len(td.embeddedFiles) != len(e.EmbeddedFiles) { |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 316 | t.Errorf("[Test Case %v] Incorrect number of embedded files! Expected: %s, Got: %s.", index, len(td.embeddedFiles), len(e.EmbeddedFiles)) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 317 | } else { |
| 318 | embeds := e.EmbeddedFiles[:] |
| 319 | |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 320 | for _, ad := range td.embeddedFiles { |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 321 | found := false |
| 322 | |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 323 | for i, ra := range embeds { |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 324 | b, err := ioutil.ReadAll(ra.Data) |
| 325 | if err != nil { |
| 326 | t.Error(err) |
| 327 | } |
| 328 | |
| 329 | encoded := base64.StdEncoding.EncodeToString(b) |
| 330 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 331 | if ra.CID == ad.cid && encoded == ad.base64data && ra.ContentType == ad.contentType { |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 332 | found = true |
| 333 | embeds = append(embeds[:i], embeds[i+1:]...) |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | if !found { |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 338 | t.Errorf("[Test Case %v] Embedded file not found: %s", index, ad.cid) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
| 342 | if len(embeds) != 0 { |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 343 | t.Errorf("[Test Case %v] Email contains %v unexpected embedded files: %v", index, len(embeds), embeds) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | func parseDate(in string) time.Time { |
| 350 | out, err := time.Parse(time.RFC1123Z, in) |
| 351 | if err != nil { |
| 352 | panic(err) |
| 353 | } |
| 354 | |
| 355 | return out |
| 356 | } |
| 357 | |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 358 | type attachmentData struct { |
| 359 | filename string |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 360 | contentType string |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 361 | base64data string |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 362 | } |
| 363 | |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 364 | type embeddedFileData struct { |
| 365 | cid string |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 366 | contentType string |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 367 | base64data string |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | func assertSliceEq(a, b []string) bool { |
| 371 | if len(a) == len(b) && len(a) == 0 { |
| 372 | return true |
| 373 | } |
| 374 | |
| 375 | if a == nil && b == nil { |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 376 | return true |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | if a == nil || b == nil { |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 380 | return false |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | if len(a) != len(b) { |
| 384 | return false |
| 385 | } |
| 386 | |
| 387 | for i := range a { |
| 388 | if a[i] != b[i] { |
| 389 | return false |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | return true |
| 394 | } |
| 395 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 396 | func assertAddressListEq(a, b []mail.Address) bool { |
| 397 | if len(a) == len(b) && len(a) == 0 { |
| 398 | return true |
| 399 | } |
| 400 | |
| 401 | if a == nil && b == nil { |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 402 | return true |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | if a == nil || b == nil { |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 406 | return false |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | if len(a) != len(b) { |
| 410 | return false |
| 411 | } |
| 412 | |
| 413 | for i := range a { |
| 414 | if a[i] != b[i] { |
| 415 | return false |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | return true |
| 420 | } |
| 421 | |
| 422 | func dereferenceAddressList(al []*mail.Address) (result []mail.Address) { |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 423 | for _, a := range al { |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 424 | result = append(result, *a) |
| 425 | } |
| 426 | |
| 427 | return |
| 428 | } |
| 429 | |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 430 | var Data1 = `From: =?UTF-8?Q?Peter_Pahol=C3=ADk?= <peter.paholik@gmail.com> |
| 431 | Date: Fri, 7 Apr 2017 09:17:26 +0200 |
| 432 | Message-ID: <CACtgX4kNXE7T5XKSKeH_zEcfUUmf2vXVASxYjaaK9cCn-3zb_g@mail.gmail.com> |
| 433 | Subject: Test Subject 1 |
| 434 | To: dusan@kasan.sk |
| 435 | Content-Type: multipart/mixed; boundary=f403045f1dcc043a44054c8e6bbf |
| 436 | |
| 437 | --f403045f1dcc043a44054c8e6bbf |
| 438 | Content-Type: multipart/alternative; boundary=f403045f1dcc043a3f054c8e6bbd |
| 439 | |
| 440 | --f403045f1dcc043a3f054c8e6bbd |
| 441 | Content-Type: text/plain; charset=UTF-8 |
| 442 | |
| 443 | |
| 444 | |
| 445 | --f403045f1dcc043a3f054c8e6bbd |
| 446 | Content-Type: text/html; charset=UTF-8 |
| 447 | |
| 448 | <div dir="ltr"><br></div> |
| 449 | |
| 450 | --f403045f1dcc043a3f054c8e6bbd-- |
| 451 | --f403045f1dcc043a44054c8e6bbf |
| 452 | Content-Type: application/pdf; |
| 453 | name="=?UTF-8?Q?Peter_Paholi=CC=81k_1?= |
| 454 | =?UTF-8?Q?_4_2017_2017=2D04=2D07=2Epdf?=" |
| 455 | Content-Disposition: attachment; |
| 456 | filename="=?UTF-8?Q?Peter_Paholi=CC=81k_1?= |
| 457 | =?UTF-8?Q?_4_2017_2017=2D04=2D07=2Epdf?=" |
| 458 | Content-Transfer-Encoding: base64 |
| 459 | X-Attachment-Id: f_j17i0f0d0 |
| 460 | |
| 461 | JVBERi0xLjQNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFu |
| 462 | Zyhlbi1VUykgL1N0cnVjdFRyZWVSb290IDY3IDAgUi9NYXJrSW5mbzw8L01hcmtlZCB0cnVlPj4v |
| 463 | T3V0cHV0SW50ZW50c1s8PC9UeXBlL091dHB1dEludGVudC9TL0dUU19QREZBMS9PdXRwdXRDb25k |
| 464 | ZXYgMzk1MzYyDQo+Pg0Kc3RhcnR4cmVmDQo0MTk4ODUNCiUlRU9GDQo= |
| 465 | --f403045f1dcc043a44054c8e6bbf-- |
| 466 | ` |
| 467 | |
| 468 | var Data2 = `Subject: Re: Test Subject 2 |
| 469 | To: info@receiver.com |
| 470 | References: <2f6b7595-c01e-46e5-42bc-f263e1c4282d@receiver.com> |
| 471 | <9ff38d03-c4ab-89b7-9328-e99d5e24e3ba@domain.com> |
| 472 | Cc: Cc Man <ccman@gmail.com> |
| 473 | From: Sender Man <sender@domain.com> |
| 474 | Message-ID: <0e9a21b4-01dc-e5c1-dcd6-58ce5aa61f4f@receiver.com> |
| 475 | Date: Fri, 7 Apr 2017 12:59:55 +0200 |
| 476 | User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:45.0) |
| 477 | Gecko/20100101 Thunderbird/45.8.0 |
| 478 | MIME-Version: 1.0 |
| 479 | In-Reply-To: <9ff38d03-c4ab-89b7-9328-e99d5e24e3ba@receiver.eu> |
| 480 | Content-Type: multipart/alternative; |
| 481 | boundary="------------C70C0458A558E585ACB75FB4" |
| 482 | |
| 483 | This is a multi-part message in MIME format. |
| 484 | --------------C70C0458A558E585ACB75FB4 |
| 485 | Content-Type: text/plain; charset=utf-8; format=flowed |
| 486 | Content-Transfer-Encoding: 8bit |
| 487 | |
| 488 | First level |
| 489 | > Second level |
| 490 | >> Third level |
| 491 | > |
| 492 | |
| 493 | |
| 494 | --------------C70C0458A558E585ACB75FB4 |
| 495 | Content-Type: multipart/related; |
| 496 | boundary="------------5DB4A1356834BB602A5F88B2" |
| 497 | |
| 498 | |
| 499 | --------------5DB4A1356834BB602A5F88B2 |
| 500 | Content-Type: text/html; charset=utf-8 |
| 501 | Content-Transfer-Encoding: 8bit |
| 502 | |
| 503 | <html>data<img src="part2.9599C449.04E5EC81@develhell.com"/></html> |
| 504 | |
| 505 | --------------5DB4A1356834BB602A5F88B2 |
| 506 | Content-Type: image/png |
| 507 | Content-Transfer-Encoding: base64 |
| 508 | Content-ID: <part2.9599C449.04E5EC81@develhell.com> |
| 509 | |
| 510 | iVBORw0KGgoAAAANSUhEUgAAAQEAAAAYCAIAAAB1IN9NAAAACXBIWXMAAAsTAAALEwEAmpwY |
| 511 | YKUKF+Os3baUndC0pDnwNAmLy1SUr2Gw0luxQuV/AwC6cEhVV5VRrwAAAABJRU5ErkJggg== |
| 512 | --------------5DB4A1356834BB602A5F88B2 |
| 513 | |
| 514 | --------------C70C0458A558E585ACB75FB4-- |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 515 | ` |
| 516 | |
| 517 | var RFC5322_Example_A11 = `From: John Doe <jdoe@machine.example> |
| 518 | Sender: Michael Jones <mjones@machine.example> |
| 519 | To: Mary Smith <mary@example.net> |
| 520 | Subject: Saying Hello |
| 521 | Date: Fri, 21 Nov 1997 09:55:06 -0600 |
| 522 | Message-ID: <1234@local.machine.example> |
| 523 | |
| 524 | This is a message just to say hello. |
| 525 | So, "Hello". |
| 526 | ` |
| 527 | |
| 528 | var RFC5322_Example_A12 = `From: "Joe Q. Public" <john.q.public@example.com> |
| 529 | To: Mary Smith <mary@x.test>, jdoe@example.org, Who? <one@y.test> |
| 530 | Cc: <boss@nil.test>, "Giant; \"Big\" Box" <sysservices@example.net> |
| 531 | Date: Tue, 1 Jul 2003 10:52:37 +0200 |
| 532 | Message-ID: <5678.21-Nov-1997@example.com> |
| 533 | |
| 534 | Hi everyone. |
| 535 | ` |
| 536 | |
| 537 | //todo: not yet implemented in net/mail |
| 538 | //once there is support for this, add it |
| 539 | var RFC5322_Example_A13 = `From: Pete <pete@silly.example> |
| 540 | To: A Group:Ed Jones <c@a.test>,joe@where.test,John <jdoe@one.test>; |
| 541 | Cc: Undisclosed recipients:; |
| 542 | Date: Thu, 13 Feb 1969 23:32:54 -0330 |
| 543 | Message-ID: <testabcd.1234@silly.example> |
| 544 | |
| 545 | Testing. |
| 546 | ` |
| 547 | |
| 548 | //we skipped the first message bcause it's the same as A 1.1 |
| 549 | var RFC5322_Example_A2a = `From: Mary Smith <mary@example.net> |
| 550 | To: John Doe <jdoe@machine.example> |
| 551 | Reply-To: "Mary Smith: Personal Account" <smith@home.example> |
| 552 | Subject: Re: Saying Hello |
| 553 | Date: Fri, 21 Nov 1997 10:01:10 -0600 |
| 554 | Message-ID: <3456@example.net> |
| 555 | In-Reply-To: <1234@local.machine.example> |
| 556 | References: <1234@local.machine.example> |
| 557 | |
| 558 | This is a reply to your hello. |
| 559 | ` |
| 560 | |
| 561 | var RFC5322_Example_A2b = `To: "Mary Smith: Personal Account" <smith@home.example> |
| 562 | From: John Doe <jdoe@machine.example> |
| 563 | Subject: Re: Saying Hello |
| 564 | Date: Fri, 21 Nov 1997 11:00:00 -0600 |
| 565 | Message-ID: <abcd.1234@local.machine.test> |
| 566 | In-Reply-To: <3456@example.net> |
| 567 | References: <1234@local.machine.example> <3456@example.net> |
| 568 | |
| 569 | This is a reply to your reply. |
| 570 | ` |
| 571 | |
| 572 | var RFC5322_Example_A3 = `Resent-From: Mary Smith <mary@example.net> |
| 573 | Resent-To: Jane Brown <j-brown@other.example> |
| 574 | Resent-Date: Mon, 24 Nov 1997 14:22:01 -0800 |
| 575 | Resent-Message-ID: <78910@example.net> |
| 576 | From: John Doe <jdoe@machine.example> |
| 577 | To: Mary Smith <mary@example.net> |
| 578 | Subject: Saying Hello |
| 579 | Date: Fri, 21 Nov 1997 09:55:06 -0600 |
| 580 | Message-ID: <1234@local.machine.example> |
| 581 | |
| 582 | This is a message just to say hello. |
| 583 | So, "Hello".` |
| 584 | |
| 585 | var RFC5322_Example_A4 = `Received: from x.y.test |
| 586 | by example.net |
| 587 | via TCP |
| 588 | with ESMTP |
| 589 | id ABC12345 |
| 590 | for <mary@example.net>; 21 Nov 1997 10:05:43 -0600 |
| 591 | Received: from node.example by x.y.test; 21 Nov 1997 10:01:22 -0600 |
| 592 | From: John Doe <jdoe@node.example> |
| 593 | To: Mary Smith <mary@example.net> |
| 594 | Subject: Saying Hello |
| 595 | Date: Fri, 21 Nov 1997 09:55:06 -0600 |
| 596 | Message-ID: <1234@local.node.example> |
| 597 | |
| 598 | This is a message just to say hello. |
| 599 | So, "Hello".` |