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