email_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. package email
  2. import (
  3. "testing"
  4. "bytes"
  5. "crypto/rand"
  6. "io"
  7. "io/ioutil"
  8. "mime"
  9. "mime/multipart"
  10. "mime/quotedprintable"
  11. "net/mail"
  12. "net/smtp"
  13. "net/textproto"
  14. )
  15. func prepareEmail() *Email {
  16. e := NewEmail()
  17. e.From = "Jordan Wright <test@example.com>"
  18. e.To = []string{"test@example.com"}
  19. e.Bcc = []string{"test_bcc@example.com"}
  20. e.Cc = []string{"test_cc@example.com"}
  21. e.Subject = "Awesome Subject"
  22. return e
  23. }
  24. func basicTests(t *testing.T, e *Email) *mail.Message {
  25. raw, err := e.Bytes()
  26. if err != nil {
  27. t.Fatal("Failed to render message: ", e)
  28. }
  29. msg, err := mail.ReadMessage(bytes.NewBuffer(raw))
  30. if err != nil {
  31. t.Fatal("Could not parse rendered message: ", err)
  32. }
  33. expectedHeaders := map[string]string{
  34. "To": "test@example.com",
  35. "From": "Jordan Wright <test@example.com>",
  36. "Cc": "test_cc@example.com",
  37. "Subject": "Awesome Subject",
  38. }
  39. for header, expected := range expectedHeaders {
  40. if val := msg.Header.Get(header); val != expected {
  41. t.Errorf("Wrong value for message header %s: %v != %v", header, expected, val)
  42. }
  43. }
  44. return msg
  45. }
  46. func TestEmailText(t *testing.T) {
  47. e := prepareEmail()
  48. e.Text = []byte("Text Body is, of course, supported!\n")
  49. msg := basicTests(t, e)
  50. // Were the right headers set?
  51. ct := msg.Header.Get("Content-type")
  52. mt, _, err := mime.ParseMediaType(ct)
  53. if err != nil {
  54. t.Fatal("Content-type header is invalid: ", ct)
  55. } else if mt != "text/plain" {
  56. t.Fatalf("Content-type expected \"text/plain\", not %v", mt)
  57. }
  58. }
  59. func TestEmailHTML(t *testing.T) {
  60. e := prepareEmail()
  61. e.HTML = []byte("<h1>Fancy Html is supported, too!</h1>\n")
  62. msg := basicTests(t, e)
  63. // Were the right headers set?
  64. ct := msg.Header.Get("Content-type")
  65. mt, _, err := mime.ParseMediaType(ct)
  66. if err != nil {
  67. t.Fatalf("Content-type header is invalid: %#v", ct)
  68. } else if mt != "text/html" {
  69. t.Fatalf("Content-type expected \"text/html\", not %v", mt)
  70. }
  71. }
  72. func TestEmailTextAttachment(t *testing.T) {
  73. e := prepareEmail()
  74. e.Text = []byte("Text Body is, of course, supported!\n")
  75. _, err := e.Attach(bytes.NewBufferString("Rad attachment"), "rad.txt", "text/plain; charset=utf-8")
  76. if err != nil {
  77. t.Fatal("Could not add an attachment to the message: ", err)
  78. }
  79. msg := basicTests(t, e)
  80. // Were the right headers set?
  81. ct := msg.Header.Get("Content-type")
  82. mt, params, err := mime.ParseMediaType(ct)
  83. if err != nil {
  84. t.Fatal("Content-type header is invalid: ", ct)
  85. } else if mt != "multipart/mixed" {
  86. t.Fatalf("Content-type expected \"multipart/mixed\", not %v", mt)
  87. }
  88. b := params["boundary"]
  89. if b == "" {
  90. t.Fatalf("Invalid or missing boundary parameter: %#v", b)
  91. }
  92. if len(params) != 1 {
  93. t.Fatal("Unexpected content-type parameters")
  94. }
  95. // Is the generated message parsable?
  96. mixed := multipart.NewReader(msg.Body, params["boundary"])
  97. text, err := mixed.NextPart()
  98. if err != nil {
  99. t.Fatalf("Could not find text component of email: %s", err)
  100. }
  101. // Does the text portion match what we expect?
  102. mt, _, err = mime.ParseMediaType(text.Header.Get("Content-type"))
  103. if err != nil {
  104. t.Fatal("Could not parse message's Content-Type")
  105. } else if mt != "text/plain" {
  106. t.Fatal("Message missing text/plain")
  107. }
  108. plainText, err := ioutil.ReadAll(text)
  109. if err != nil {
  110. t.Fatal("Could not read plain text component of message: ", err)
  111. }
  112. if !bytes.Equal(plainText, []byte("Text Body is, of course, supported!\r\n")) {
  113. t.Fatalf("Plain text is broken: %#q", plainText)
  114. }
  115. // Check attachments.
  116. _, err = mixed.NextPart()
  117. if err != nil {
  118. t.Fatalf("Could not find attachment component of email: %s", err)
  119. }
  120. if _, err = mixed.NextPart(); err != io.EOF {
  121. t.Error("Expected only text and one attachment!")
  122. }
  123. }
  124. func TestEmailTextHtmlAttachment(t *testing.T) {
  125. e := prepareEmail()
  126. e.Text = []byte("Text Body is, of course, supported!\n")
  127. e.HTML = []byte("<h1>Fancy Html is supported, too!</h1>\n")
  128. _, err := e.Attach(bytes.NewBufferString("Rad attachment"), "rad.txt", "text/plain; charset=utf-8")
  129. if err != nil {
  130. t.Fatal("Could not add an attachment to the message: ", err)
  131. }
  132. msg := basicTests(t, e)
  133. // Were the right headers set?
  134. ct := msg.Header.Get("Content-type")
  135. mt, params, err := mime.ParseMediaType(ct)
  136. if err != nil {
  137. t.Fatal("Content-type header is invalid: ", ct)
  138. } else if mt != "multipart/mixed" {
  139. t.Fatalf("Content-type expected \"multipart/mixed\", not %v", mt)
  140. }
  141. b := params["boundary"]
  142. if b == "" {
  143. t.Fatal("Unexpected empty boundary parameter")
  144. }
  145. if len(params) != 1 {
  146. t.Fatal("Unexpected content-type parameters")
  147. }
  148. // Is the generated message parsable?
  149. mixed := multipart.NewReader(msg.Body, params["boundary"])
  150. text, err := mixed.NextPart()
  151. if err != nil {
  152. t.Fatalf("Could not find text component of email: %s", err)
  153. }
  154. // Does the text portion match what we expect?
  155. mt, params, err = mime.ParseMediaType(text.Header.Get("Content-type"))
  156. if err != nil {
  157. t.Fatal("Could not parse message's Content-Type")
  158. } else if mt != "multipart/alternative" {
  159. t.Fatal("Message missing multipart/alternative")
  160. }
  161. mpReader := multipart.NewReader(text, params["boundary"])
  162. part, err := mpReader.NextPart()
  163. if err != nil {
  164. t.Fatal("Could not read plain text component of message: ", err)
  165. }
  166. plainText, err := ioutil.ReadAll(part)
  167. if err != nil {
  168. t.Fatal("Could not read plain text component of message: ", err)
  169. }
  170. if !bytes.Equal(plainText, []byte("Text Body is, of course, supported!\r\n")) {
  171. t.Fatalf("Plain text is broken: %#q", plainText)
  172. }
  173. // Check attachments.
  174. _, err = mixed.NextPart()
  175. if err != nil {
  176. t.Fatalf("Could not find attachment component of email: %s", err)
  177. }
  178. if _, err = mixed.NextPart(); err != io.EOF {
  179. t.Error("Expected only text and one attachment!")
  180. }
  181. }
  182. func TestEmailAttachment(t *testing.T) {
  183. e := prepareEmail()
  184. _, err := e.Attach(bytes.NewBufferString("Rad attachment"), "rad.txt", "text/plain; charset=utf-8")
  185. if err != nil {
  186. t.Fatal("Could not add an attachment to the message: ", err)
  187. }
  188. msg := basicTests(t, e)
  189. // Were the right headers set?
  190. ct := msg.Header.Get("Content-type")
  191. mt, params, err := mime.ParseMediaType(ct)
  192. if err != nil {
  193. t.Fatal("Content-type header is invalid: ", ct)
  194. } else if mt != "multipart/mixed" {
  195. t.Fatalf("Content-type expected \"multipart/mixed\", not %v", mt)
  196. }
  197. b := params["boundary"]
  198. if b == "" {
  199. t.Fatal("Unexpected empty boundary parameter")
  200. }
  201. if len(params) != 1 {
  202. t.Fatal("Unexpected content-type parameters")
  203. }
  204. // Is the generated message parsable?
  205. mixed := multipart.NewReader(msg.Body, params["boundary"])
  206. // Check attachments.
  207. _, err = mixed.NextPart()
  208. if err != nil {
  209. t.Fatalf("Could not find attachment component of email: %s", err)
  210. }
  211. if _, err = mixed.NextPart(); err != io.EOF {
  212. t.Error("Expected only one attachment!")
  213. }
  214. }
  215. func TestEmailFromReader(t *testing.T) {
  216. ex := &Email{
  217. Subject: "Test Subject",
  218. To: []string{"Jordan Wright <jmwright798@gmail.com>"},
  219. From: "Jordan Wright <jmwright798@gmail.com>",
  220. Text: []byte("This is a test email with HTML Formatting. It also has very long lines so\nthat the content must be wrapped if using quoted-printable decoding.\n"),
  221. HTML: []byte("<div dir=\"ltr\">This is a test email with <b>HTML Formatting.</b>\u00a0It also has very long lines so that the content must be wrapped if using quoted-printable decoding.</div>\n"),
  222. }
  223. raw := []byte(`
  224. MIME-Version: 1.0
  225. Subject: Test Subject
  226. From: Jordan Wright <jmwright798@gmail.com>
  227. To: Jordan Wright <jmwright798@gmail.com>
  228. Content-Type: multipart/alternative; boundary=001a114fb3fc42fd6b051f834280
  229. --001a114fb3fc42fd6b051f834280
  230. Content-Type: text/plain; charset=UTF-8
  231. This is a test email with HTML Formatting. It also has very long lines so
  232. that the content must be wrapped if using quoted-printable decoding.
  233. --001a114fb3fc42fd6b051f834280
  234. Content-Type: text/html; charset=UTF-8
  235. Content-Transfer-Encoding: quoted-printable
  236. <div dir=3D"ltr">This is a test email with <b>HTML Formatting.</b>=C2=A0It =
  237. also has very long lines so that the content must be wrapped if using quote=
  238. d-printable decoding.</div>
  239. --001a114fb3fc42fd6b051f834280--`)
  240. e, err := NewEmailFromReader(bytes.NewReader(raw))
  241. if err != nil {
  242. t.Fatalf("Error creating email %s", err.Error())
  243. }
  244. if e.Subject != ex.Subject {
  245. t.Fatalf("Incorrect subject. %#q != %#q", e.Subject, ex.Subject)
  246. }
  247. if !bytes.Equal(e.Text, ex.Text) {
  248. t.Fatalf("Incorrect text: %#q != %#q", e.Text, ex.Text)
  249. }
  250. if !bytes.Equal(e.HTML, ex.HTML) {
  251. t.Fatalf("Incorrect HTML: %#q != %#q", e.HTML, ex.HTML)
  252. }
  253. if e.From != ex.From {
  254. t.Fatalf("Incorrect \"From\": %#q != %#q", e.From, ex.From)
  255. }
  256. }
  257. func TestNonAsciiEmailFromReader(t *testing.T) {
  258. ex := &Email{
  259. Subject: "Test Subject",
  260. To: []string{"Anaïs <anais@example.org>"},
  261. Cc: []string{"Patrik Fältström <paf@example.com>"},
  262. From: "Mrs Valérie Dupont <valerie.dupont@example.com>",
  263. Text: []byte("This is a test message!"),
  264. }
  265. raw := []byte(`
  266. MIME-Version: 1.0
  267. Subject: =?UTF-8?Q?Test Subject?=
  268. From: Mrs =?ISO-8859-1?Q?Val=C3=A9rie=20Dupont?= <valerie.dupont@example.com>
  269. To: =?utf-8?q?Ana=C3=AFs?= <anais@example.org>
  270. Cc: =?ISO-8859-1?Q?Patrik_F=E4ltstr=F6m?= <paf@example.com>
  271. Content-type: text/plain; charset=ISO-8859-1
  272. This is a test message!`)
  273. e, err := NewEmailFromReader(bytes.NewReader(raw))
  274. if err != nil {
  275. t.Fatalf("Error creating email %s", err.Error())
  276. }
  277. if e.Subject != ex.Subject {
  278. t.Fatalf("Incorrect subject. %#q != %#q", e.Subject, ex.Subject)
  279. }
  280. if e.From != ex.From {
  281. t.Fatalf("Incorrect \"From\": %#q != %#q", e.From, ex.From)
  282. }
  283. if e.To[0] != ex.To[0] {
  284. t.Fatalf("Incorrect \"To\": %#q != %#q", e.To, ex.To)
  285. }
  286. if e.Cc[0] != ex.Cc[0] {
  287. t.Fatalf("Incorrect \"Cc\": %#q != %#q", e.Cc, ex.Cc)
  288. }
  289. }
  290. func TestNonMultipartEmailFromReader(t *testing.T) {
  291. ex := &Email{
  292. Text: []byte("This is a test message!"),
  293. Subject: "Example Subject (no MIME Type)",
  294. Headers: textproto.MIMEHeader{},
  295. }
  296. ex.Headers.Add("Content-Type", "text/plain; charset=us-ascii")
  297. ex.Headers.Add("Message-ID", "<foobar@example.com>")
  298. raw := []byte(`From: "Foo Bar" <foobar@example.com>
  299. Content-Type: text/plain
  300. To: foobar@example.com
  301. Subject: Example Subject (no MIME Type)
  302. Message-ID: <foobar@example.com>
  303. This is a test message!`)
  304. e, err := NewEmailFromReader(bytes.NewReader(raw))
  305. if err != nil {
  306. t.Fatalf("Error creating email %s", err.Error())
  307. }
  308. if ex.Subject != e.Subject {
  309. t.Errorf("Incorrect subject. %#q != %#q\n", ex.Subject, e.Subject)
  310. }
  311. if !bytes.Equal(ex.Text, e.Text) {
  312. t.Errorf("Incorrect body. %#q != %#q\n", ex.Text, e.Text)
  313. }
  314. if ex.Headers.Get("Message-ID") != e.Headers.Get("Message-ID") {
  315. t.Errorf("Incorrect message ID. %#q != %#q\n", ex.Headers.Get("Message-ID"), e.Headers.Get("Message-ID"))
  316. }
  317. }
  318. func TestBase64EmailFromReader(t *testing.T) {
  319. ex := &Email{
  320. Subject: "Test Subject",
  321. To: []string{"Jordan Wright <jmwright798@gmail.com>"},
  322. From: "Jordan Wright <jmwright798@gmail.com>",
  323. Text: []byte("This is a test email with HTML Formatting. It also has very long lines so that the content must be wrapped if using quoted-printable decoding."),
  324. HTML: []byte("<div dir=\"ltr\">This is a test email with <b>HTML Formatting.</b>\u00a0It also has very long lines so that the content must be wrapped if using quoted-printable decoding.</div>\n"),
  325. }
  326. raw := []byte(`
  327. MIME-Version: 1.0
  328. Subject: Test Subject
  329. From: Jordan Wright <jmwright798@gmail.com>
  330. To: Jordan Wright <jmwright798@gmail.com>
  331. Content-Type: multipart/alternative; boundary=001a114fb3fc42fd6b051f834280
  332. --001a114fb3fc42fd6b051f834280
  333. Content-Type: text/plain; charset=UTF-8
  334. Content-Transfer-Encoding: base64
  335. VGhpcyBpcyBhIHRlc3QgZW1haWwgd2l0aCBIVE1MIEZvcm1hdHRpbmcuIEl0IGFsc28gaGFzIHZl
  336. cnkgbG9uZyBsaW5lcyBzbyB0aGF0IHRoZSBjb250ZW50IG11c3QgYmUgd3JhcHBlZCBpZiB1c2lu
  337. ZyBxdW90ZWQtcHJpbnRhYmxlIGRlY29kaW5nLg==
  338. --001a114fb3fc42fd6b051f834280
  339. Content-Type: text/html; charset=UTF-8
  340. Content-Transfer-Encoding: quoted-printable
  341. <div dir=3D"ltr">This is a test email with <b>HTML Formatting.</b>=C2=A0It =
  342. also has very long lines so that the content must be wrapped if using quote=
  343. d-printable decoding.</div>
  344. --001a114fb3fc42fd6b051f834280--`)
  345. e, err := NewEmailFromReader(bytes.NewReader(raw))
  346. if err != nil {
  347. t.Fatalf("Error creating email %s", err.Error())
  348. }
  349. if e.Subject != ex.Subject {
  350. t.Fatalf("Incorrect subject. %#q != %#q", e.Subject, ex.Subject)
  351. }
  352. if !bytes.Equal(e.Text, ex.Text) {
  353. t.Fatalf("Incorrect text: %#q != %#q", e.Text, ex.Text)
  354. }
  355. if !bytes.Equal(e.HTML, ex.HTML) {
  356. t.Fatalf("Incorrect HTML: %#q != %#q", e.HTML, ex.HTML)
  357. }
  358. if e.From != ex.From {
  359. t.Fatalf("Incorrect \"From\": %#q != %#q", e.From, ex.From)
  360. }
  361. }
  362. func ExampleGmail() {
  363. e := NewEmail()
  364. e.From = "Jordan Wright <test@gmail.com>"
  365. e.To = []string{"test@example.com"}
  366. e.Bcc = []string{"test_bcc@example.com"}
  367. e.Cc = []string{"test_cc@example.com"}
  368. e.Subject = "Awesome Subject"
  369. e.Text = []byte("Text Body is, of course, supported!\n")
  370. e.HTML = []byte("<h1>Fancy Html is supported, too!</h1>\n")
  371. e.Send("smtp.gmail.com:587", smtp.PlainAuth("", e.From, "password123", "smtp.gmail.com"))
  372. }
  373. func ExampleAttach() {
  374. e := NewEmail()
  375. e.AttachFile("test.txt")
  376. }
  377. func Test_base64Wrap(t *testing.T) {
  378. file := "I'm a file long enough to force the function to wrap a\n" +
  379. "couple of lines, but I stop short of the end of one line and\n" +
  380. "have some padding dangling at the end."
  381. encoded := "SSdtIGEgZmlsZSBsb25nIGVub3VnaCB0byBmb3JjZSB0aGUgZnVuY3Rpb24gdG8gd3JhcCBhCmNv\r\n" +
  382. "dXBsZSBvZiBsaW5lcywgYnV0IEkgc3RvcCBzaG9ydCBvZiB0aGUgZW5kIG9mIG9uZSBsaW5lIGFu\r\n" +
  383. "ZApoYXZlIHNvbWUgcGFkZGluZyBkYW5nbGluZyBhdCB0aGUgZW5kLg==\r\n"
  384. var buf bytes.Buffer
  385. base64Wrap(&buf, []byte(file))
  386. if !bytes.Equal(buf.Bytes(), []byte(encoded)) {
  387. t.Fatalf("Encoded file does not match expected: %#q != %#q", string(buf.Bytes()), encoded)
  388. }
  389. }
  390. // *Since the mime library in use by ```email``` is now in the stdlib, this test is deprecated
  391. func Test_quotedPrintEncode(t *testing.T) {
  392. var buf bytes.Buffer
  393. text := []byte("Dear reader!\n\n" +
  394. "This is a test email to try and capture some of the corner cases that exist within\n" +
  395. "the quoted-printable encoding.\n" +
  396. "There are some wacky parts like =, and this input assumes UNIX line breaks so\r\n" +
  397. "it can come out a little weird. Also, we need to support unicode so here's a fish: 🐟\n")
  398. expected := []byte("Dear reader!\r\n\r\n" +
  399. "This is a test email to try and capture some of the corner cases that exist=\r\n" +
  400. " within\r\n" +
  401. "the quoted-printable encoding.\r\n" +
  402. "There are some wacky parts like =3D, and this input assumes UNIX line break=\r\n" +
  403. "s so\r\n" +
  404. "it can come out a little weird. Also, we need to support unicode so here's=\r\n" +
  405. " a fish: =F0=9F=90=9F\r\n")
  406. qp := quotedprintable.NewWriter(&buf)
  407. if _, err := qp.Write(text); err != nil {
  408. t.Fatal("quotePrintEncode: ", err)
  409. }
  410. if err := qp.Close(); err != nil {
  411. t.Fatal("Error closing writer", err)
  412. }
  413. if b := buf.Bytes(); !bytes.Equal(b, expected) {
  414. t.Errorf("quotedPrintEncode generated incorrect results: %#q != %#q", b, expected)
  415. }
  416. }
  417. func TestMultipartNoContentType(t *testing.T) {
  418. raw := []byte(`From: Mikhail Gusarov <dottedmag@dottedmag.net>
  419. To: notmuch@notmuchmail.org
  420. References: <20091117190054.GU3165@dottiness.seas.harvard.edu>
  421. Date: Wed, 18 Nov 2009 01:02:38 +0600
  422. Message-ID: <87iqd9rn3l.fsf@vertex.dottedmag>
  423. MIME-Version: 1.0
  424. Subject: Re: [notmuch] Working with Maildir storage?
  425. Content-Type: multipart/mixed; boundary="===============1958295626=="
  426. --===============1958295626==
  427. Content-Type: multipart/signed; boundary="=-=-=";
  428. micalg=pgp-sha1; protocol="application/pgp-signature"
  429. --=-=-=
  430. Content-Transfer-Encoding: quoted-printable
  431. Twas brillig at 14:00:54 17.11.2009 UTC-05 when lars@seas.harvard.edu did g=
  432. yre and gimble:
  433. --=-=-=
  434. Content-Type: application/pgp-signature
  435. -----BEGIN PGP SIGNATURE-----
  436. Version: GnuPG v1.4.9 (GNU/Linux)
  437. iQIcBAEBAgAGBQJLAvNOAAoJEJ0g9lA+M4iIjLYQAKp0PXEgl3JMOEBisH52AsIK
  438. =/ksP
  439. -----END PGP SIGNATURE-----
  440. --=-=-=--
  441. --===============1958295626==
  442. Content-Type: text/plain; charset="us-ascii"
  443. MIME-Version: 1.0
  444. Content-Transfer-Encoding: 7bit
  445. Content-Disposition: inline
  446. Testing!
  447. --===============1958295626==--
  448. `)
  449. e, err := NewEmailFromReader(bytes.NewReader(raw))
  450. if err != nil {
  451. t.Fatalf("Error when parsing email %s", err.Error())
  452. }
  453. if !bytes.Equal(e.Text, []byte("Testing!")) {
  454. t.Fatalf("Error incorrect text: %#q != %#q\n", e.Text, "Testing!")
  455. }
  456. }
  457. // *Since the mime library in use by ```email``` is now in the stdlib, this test is deprecated
  458. func Test_quotedPrintDecode(t *testing.T) {
  459. text := []byte("Dear reader!\r\n\r\n" +
  460. "This is a test email to try and capture some of the corner cases that exist=\r\n" +
  461. " within\r\n" +
  462. "the quoted-printable encoding.\r\n" +
  463. "There are some wacky parts like =3D, and this input assumes UNIX line break=\r\n" +
  464. "s so\r\n" +
  465. "it can come out a little weird. Also, we need to support unicode so here's=\r\n" +
  466. " a fish: =F0=9F=90=9F\r\n")
  467. expected := []byte("Dear reader!\r\n\r\n" +
  468. "This is a test email to try and capture some of the corner cases that exist within\r\n" +
  469. "the quoted-printable encoding.\r\n" +
  470. "There are some wacky parts like =, and this input assumes UNIX line breaks so\r\n" +
  471. "it can come out a little weird. Also, we need to support unicode so here's a fish: 🐟\r\n")
  472. qp := quotedprintable.NewReader(bytes.NewReader(text))
  473. got, err := ioutil.ReadAll(qp)
  474. if err != nil {
  475. t.Fatal("quotePrintDecode: ", err)
  476. }
  477. if !bytes.Equal(got, expected) {
  478. t.Errorf("quotedPrintDecode generated incorrect results: %#q != %#q", got, expected)
  479. }
  480. }
  481. func Benchmark_base64Wrap(b *testing.B) {
  482. // Reasonable base case; 128K random bytes
  483. file := make([]byte, 128*1024)
  484. if _, err := rand.Read(file); err != nil {
  485. panic(err)
  486. }
  487. for i := 0; i <= b.N; i++ {
  488. base64Wrap(ioutil.Discard, file)
  489. }
  490. }
  491. func TestParseSender(t *testing.T) {
  492. var cases = []struct {
  493. e Email
  494. want string
  495. haserr bool
  496. }{
  497. {
  498. Email{From: "from@test.com"},
  499. "from@test.com",
  500. false,
  501. },
  502. {
  503. Email{Sender: "sender@test.com", From: "from@test.com"},
  504. "sender@test.com",
  505. false,
  506. },
  507. {
  508. Email{Sender: "bad_address_sender"},
  509. "",
  510. true,
  511. },
  512. {
  513. Email{Sender: "good@sender.com", From: "bad_address_from"},
  514. "good@sender.com",
  515. false,
  516. },
  517. }
  518. for i, testcase := range cases {
  519. got, err := testcase.e.parseSender()
  520. if got != testcase.want || (err != nil) != testcase.haserr {
  521. t.Errorf(`%d: got %s != want %s or error "%t" != "%t"`, i+1, got, testcase.want, err != nil, testcase.haserr)
  522. }
  523. }
  524. }