smtp_server_test.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. package server
  2. import (
  3. "bufio"
  4. "github.com/emersion/go-smtp"
  5. "github.com/stretchr/testify/require"
  6. "io"
  7. "net"
  8. "net/http"
  9. "strings"
  10. "testing"
  11. "time"
  12. )
  13. func TestSmtpBackend_Multipart(t *testing.T) {
  14. email := `EHLO example.com
  15. MAIL FROM: phil@example.com
  16. RCPT TO: ntfy-mytopic@ntfy.sh
  17. DATA
  18. MIME-Version: 1.0
  19. Date: Tue, 28 Dec 2021 00:30:10 +0100
  20. Message-ID: <CAAvm79YP0C=Rt1N=KWmSUBB87KK2rRChmdzKqF1vCwMEUiVzLQ@mail.gmail.com>
  21. Subject: and one more
  22. From: Phil <phil@example.com>
  23. To: ntfy-mytopic@ntfy.sh
  24. Content-Type: multipart/alternative; boundary="000000000000f3320b05d42915c9"
  25. --000000000000f3320b05d42915c9
  26. Content-Type: text/plain; charset="UTF-8"
  27. what's up
  28. --000000000000f3320b05d42915c9
  29. Content-Type: text/html; charset="UTF-8"
  30. <div dir="ltr">what&#39;s up<br clear="all"><div><br></div></div>
  31. --000000000000f3320b05d42915c9--
  32. .
  33. `
  34. s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) {
  35. require.Equal(t, "/mytopic", r.URL.Path)
  36. require.Equal(t, "and one more", r.Header.Get("Title"))
  37. require.Equal(t, "what's up", readAll(t, r.Body))
  38. })
  39. defer s.Close()
  40. defer c.Close()
  41. writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued")
  42. }
  43. func TestSmtpBackend_MultipartNoBody(t *testing.T) {
  44. email := `EHLO example.com
  45. MAIL FROM: phil@example.com
  46. RCPT TO: ntfy-emailtest@ntfy.sh
  47. DATA
  48. MIME-Version: 1.0
  49. Date: Tue, 28 Dec 2021 01:33:34 +0100
  50. Message-ID: <CAAvm7ABCDsi9vsuu0WTRXzZQBC8dXrDOLT8iCWdqrsmg@mail.gmail.com>
  51. Subject: This email has a subject but no body
  52. From: Phil <phil@example.com>
  53. To: ntfy-emailtest@ntfy.sh
  54. Content-Type: multipart/alternative; boundary="000000000000bcf4a405d429f8d4"
  55. --000000000000bcf4a405d429f8d4
  56. Content-Type: text/plain; charset="UTF-8"
  57. --000000000000bcf4a405d429f8d4
  58. Content-Type: text/html; charset="UTF-8"
  59. <div dir="ltr"><br></div>
  60. --000000000000bcf4a405d429f8d4--
  61. .
  62. `
  63. s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) {
  64. require.Equal(t, "/emailtest", r.URL.Path)
  65. require.Equal(t, "", r.Header.Get("Title")) // We flipped message and body
  66. require.Equal(t, "This email has a subject but no body", readAll(t, r.Body))
  67. })
  68. defer s.Close()
  69. defer c.Close()
  70. writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued")
  71. }
  72. func TestSmtpBackend_Plaintext(t *testing.T) {
  73. email := `EHLO example.com
  74. MAIL FROM: phil@example.com
  75. RCPT TO: mytopic@ntfy.sh
  76. DATA
  77. Date: Tue, 28 Dec 2021 00:30:10 +0100
  78. Message-ID: <CAAvm79YP0C=Rt1N=KWmSUBB87KK2rRChmdzKqF1vCwMEUiVzLQ@mail.gmail.com>
  79. Subject: and one more
  80. From: Phil <phil@example.com>
  81. To: mytopic@ntfy.sh
  82. Content-Type: text/plain; charset="UTF-8"
  83. what's up
  84. .
  85. `
  86. s, c, conf, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) {
  87. require.Equal(t, "/mytopic", r.URL.Path)
  88. require.Equal(t, "and one more", r.Header.Get("Title"))
  89. require.Equal(t, "what's up", readAll(t, r.Body))
  90. })
  91. conf.SMTPServerAddrPrefix = ""
  92. defer s.Close()
  93. defer c.Close()
  94. writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued")
  95. }
  96. func TestSmtpBackend_Plaintext_No_ContentType(t *testing.T) {
  97. email := `EHLO example.com
  98. MAIL FROM: phil@example.com
  99. RCPT TO: mytopic@ntfy.sh
  100. DATA
  101. Subject: Very short mail
  102. what's up
  103. .
  104. `
  105. s, c, conf, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) {
  106. require.Equal(t, "/mytopic", r.URL.Path)
  107. require.Equal(t, "Very short mail", r.Header.Get("Title"))
  108. require.Equal(t, "what's up", readAll(t, r.Body))
  109. })
  110. conf.SMTPServerAddrPrefix = ""
  111. defer s.Close()
  112. defer c.Close()
  113. writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued")
  114. }
  115. func TestSmtpBackend_Plaintext_EncodedSubject(t *testing.T) {
  116. email := `EHLO example.com
  117. MAIL FROM: phil@example.com
  118. RCPT TO: ntfy-mytopic@ntfy.sh
  119. DATA
  120. Date: Tue, 28 Dec 2021 00:30:10 +0100
  121. Subject: =?UTF-8?B?VGhyZWUgc2FudGFzIPCfjoXwn46F8J+OhQ==?=
  122. From: Phil <phil@example.com>
  123. To: ntfy-mytopic@ntfy.sh
  124. Content-Type: text/plain; charset="UTF-8"
  125. what's up
  126. .
  127. `
  128. s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) {
  129. require.Equal(t, "Three santas 🎅🎅🎅", r.Header.Get("Title"))
  130. })
  131. defer s.Close()
  132. defer c.Close()
  133. writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued")
  134. }
  135. func TestSmtpBackend_Plaintext_TooLongTruncate(t *testing.T) {
  136. email := `EHLO example.com
  137. MAIL FROM: phil@example.com
  138. RCPT TO: mytopic@ntfy.sh
  139. DATA
  140. Date: Tue, 28 Dec 2021 00:30:10 +0100
  141. Message-ID: <CAAvm79YP0C=Rt1N=KWmSUBB87KK2rRChmdzKqF1vCwMEUiVzLQ@mail.gmail.com>
  142. Subject: and one more
  143. From: Phil <phil@example.com>
  144. To: mytopic@ntfy.sh
  145. Content-Type: text/plain; charset="UTF-8"
  146. you know this is a string.
  147. it's a long string.
  148. it's supposed to be longer than the max message length
  149. which is 4096 bytes,
  150. it used to be 512 bytes, but I increased that for the UnifiedPush support
  151. the 512 bytes was a little short, some people said
  152. but it kinda makes sense when you look at what it looks like one a phone
  153. heck this wasn't even half of it so far.
  154. so i'm gonna fill the rest of this with AAAAAAAAAAAAAAAAAAAAAAAAAAA
  155. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  156. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAa
  157. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  158. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  159. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  160. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  161. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  162. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  163. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  164. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  165. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  166. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  167. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  168. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  169. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  170. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  171. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  172. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  173. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  174. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  175. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  176. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  177. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  178. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  179. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  180. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  181. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  182. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  183. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  184. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  185. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  186. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  187. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  188. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  189. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  190. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  191. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  192. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  193. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  194. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  195. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  196. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  197. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  198. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  199. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  200. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  201. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  202. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  203. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  204. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  205. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  206. and with BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
  207. BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
  208. BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
  209. that should do it
  210. .
  211. `
  212. s, c, conf, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) {
  213. expected := `you know this is a string.
  214. it's a long string.
  215. it's supposed to be longer than the max message length
  216. which is 4096 bytes,
  217. it used to be 512 bytes, but I increased that for the UnifiedPush support
  218. the 512 bytes was a little short, some people said
  219. but it kinda makes sense when you look at what it looks like one a phone
  220. heck this wasn't even half of it so far.
  221. so i'm gonna fill the rest of this with AAAAAAAAAAAAAAAAAAAAAAAAAAA
  222. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  223. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAa
  224. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  225. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  226. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  227. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  228. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  229. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  230. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  231. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  232. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  233. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  234. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  235. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  236. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  237. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  238. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  239. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  240. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  241. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  242. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  243. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  244. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  245. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  246. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  247. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  248. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  249. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  250. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  251. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  252. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  253. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  254. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  255. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  256. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  257. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  258. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  259. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  260. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  261. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  262. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  263. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  264. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  265. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  266. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  267. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  268. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  269. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  270. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  271. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  272. pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
  273. and with BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
  274. BBBBBBBBBBBBBBBBBBBBBBBBB`
  275. require.Equal(t, 4096, len(expected)) // Sanity check
  276. require.Equal(t, expected, readAll(t, r.Body))
  277. })
  278. defer s.Close()
  279. defer c.Close()
  280. conf.SMTPServerAddrPrefix = ""
  281. writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued")
  282. }
  283. func TestSmtpBackend_Plaintext_QuotedPrintable(t *testing.T) {
  284. email := `EHLO example.com
  285. MAIL FROM: phil@example.com
  286. RCPT TO: mytopic@ntfy.sh
  287. DATA
  288. Date: Tue, 28 Dec 2021 00:30:10 +0100
  289. Message-ID: <CAAvm79YP0C=Rt1N=KWmSUBB87KK2rRChmdzKqF1vCwMEUiVzLQ@mail.gmail.com>
  290. Subject: and one more
  291. From: Phil <phil@example.com>
  292. To: mytopic@ntfy.sh
  293. Content-Type: text/plain; charset="UTF-8"
  294. Content-Transfer-Encoding: quoted-printable
  295. what's
  296. =C3=A0&=C3=A9"'(-=C3=A8_=C3=A7=C3=A0)
  297. =3D=3D=3D=3D=3D
  298. up
  299. .
  300. `
  301. s, c, conf, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) {
  302. require.Equal(t, "/mytopic", r.URL.Path)
  303. require.Equal(t, "and one more", r.Header.Get("Title"))
  304. require.Equal(t, `what's
  305. à&é"'(-è_çà)
  306. =====
  307. up`, readAll(t, r.Body))
  308. })
  309. conf.SMTPServerAddrPrefix = ""
  310. defer s.Close()
  311. defer c.Close()
  312. writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued")
  313. }
  314. func TestSmtpBackend_Unsupported(t *testing.T) {
  315. email := `EHLO example.com
  316. MAIL FROM: phil@example.com
  317. RCPT TO: ntfy-mytopic@ntfy.sh
  318. DATA
  319. Date: Tue, 28 Dec 2021 00:30:10 +0100
  320. Message-ID: <CAAvm79YP0C=Rt1N=KWmSUBB87KK2rRChmdzKqF1vCwMEUiVzLQ@mail.gmail.com>
  321. Subject: and one more
  322. From: Phil <phil@example.com>
  323. To: mytopic@ntfy.sh
  324. Content-Type: text/SOMETHINGELSE
  325. what's up
  326. .
  327. `
  328. s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) {
  329. t.Fatal("This should not be called")
  330. })
  331. defer s.Close()
  332. defer c.Close()
  333. writeAndReadUntilLine(t, email, c, scanner, "554 5.0.0 Error: transaction failed, blame it on the weather: unsupported content type")
  334. }
  335. func TestSmtpBackend_InvalidAddress(t *testing.T) {
  336. email := `EHLO example.com
  337. MAIL FROM: phil@example.com
  338. RCPT TO: unsupported@ntfy.sh
  339. DATA
  340. Date: Tue, 28 Dec 2021 00:30:10 +0100
  341. Subject: and one more
  342. From: Phil <phil@example.com>
  343. To: mytopic@ntfy.sh
  344. Content-Type: text/plain
  345. what's up
  346. .
  347. `
  348. s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) {
  349. t.Fatal("This should not be called")
  350. })
  351. defer s.Close()
  352. defer c.Close()
  353. writeAndReadUntilLine(t, email, c, scanner, "451 4.0.0 invalid address")
  354. }
  355. func TestSmtpBackend_Base64Body(t *testing.T) {
  356. email := `EHLO example.com
  357. MAIL FROM: test@mydomain.me
  358. RCPT TO: ntfy-mytopic@ntfy.sh
  359. DATA
  360. Content-Type: multipart/mixed; boundary="===============2138658284696597373=="
  361. MIME-Version: 1.0
  362. Subject: TrueNAS truenas.local: TrueNAS Test Message hostname: truenas.local
  363. From: =?utf-8?q?Robbie?= <test@mydomain.me>
  364. To: test@mydomain.me
  365. Date: Thu, 16 Feb 2023 01:04:00 -0000
  366. Message-ID: <truenas-20230216.010400.344514.b'8jfL'@truenas.local>
  367. This is a multi-part message in MIME format.
  368. --===============2138658284696597373==
  369. Content-Type: text/plain; charset="utf-8"
  370. MIME-Version: 1.0
  371. Content-Transfer-Encoding: base64
  372. VGhpcyBpcyBhIHRlc3QgbWVzc2FnZSBmcm9tIFRydWVOQVMgQ09SRS4=
  373. --===============2138658284696597373==
  374. Content-Type: text/html; charset="utf-8"
  375. MIME-Version: 1.0
  376. Content-Transfer-Encoding: base64
  377. PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwv
  378. L0VOIj4KClRoaXMgaXMgYSB0ZXN0IG1lc3NhZ2UgZnJvbSBUcnVlTkFTIENPUkUuCg==
  379. --===============2138658284696597373==--
  380. .
  381. `
  382. s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) {
  383. require.Equal(t, "/mytopic", r.URL.Path)
  384. require.Equal(t, "TrueNAS truenas.local: TrueNAS Test Message hostname: truenas.local", r.Header.Get("Title"))
  385. require.Equal(t, "This is a test message from TrueNAS CORE.", readAll(t, r.Body))
  386. })
  387. defer s.Close()
  388. defer c.Close()
  389. writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued")
  390. }
  391. func TestSmtpBackend_MultipartQuotedPrintable(t *testing.T) {
  392. email := `EHLO example.com
  393. MAIL FROM: phil@example.com
  394. RCPT TO: ntfy-mytopic@ntfy.sh
  395. DATA
  396. MIME-Version: 1.0
  397. Date: Tue, 28 Dec 2021 00:30:10 +0100
  398. Message-ID: <CAAvm79YP0C=Rt1N=KWmSUBB87KK2rRChmdzKqF1vCwMEUiVzLQ@mail.gmail.com>
  399. Subject: and one more
  400. From: Phil <phil@example.com>
  401. To: ntfy-mytopic@ntfy.sh
  402. Content-Type: multipart/alternative; boundary="000000000000f3320b05d42915c9"
  403. --000000000000f3320b05d42915c9
  404. Content-Type: text/html; charset="UTF-8"
  405. html, ignore me
  406. --000000000000f3320b05d42915c9
  407. Content-Type: text/plain; charset="UTF-8"
  408. Content-Transfer-Encoding: quoted-printable
  409. what's
  410. =C3=A0&=C3=A9"'(-=C3=A8_=C3=A7=C3=A0)
  411. =3D=3D=3D=3D=3D
  412. up
  413. --000000000000f3320b05d42915c9--
  414. .
  415. `
  416. s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) {
  417. require.Equal(t, "/mytopic", r.URL.Path)
  418. require.Equal(t, "and one more", r.Header.Get("Title"))
  419. require.Equal(t, `what's
  420. à&é"'(-è_çà)
  421. =====
  422. up`, readAll(t, r.Body))
  423. })
  424. defer s.Close()
  425. defer c.Close()
  426. writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued")
  427. }
  428. func TestSmtpBackend_NestedMultipartBase64(t *testing.T) {
  429. email := `EHLO example.com
  430. MAIL FROM: test@mydomain.me
  431. RCPT TO: ntfy-mytopic@ntfy.sh
  432. DATA
  433. Content-Type: multipart/mixed; boundary="===============2138658284696597373=="
  434. MIME-Version: 1.0
  435. Subject: TrueNAS truenas.local: TrueNAS Test Message hostname: truenas.local
  436. From: =?utf-8?q?Robbie?= <test@mydomain.me>
  437. To: test@mydomain.me
  438. Date: Thu, 16 Feb 2023 01:04:00 -0000
  439. Message-ID: <truenas-20230216.010400.344514.b'8jfL'@truenas.local>
  440. This is a multi-part message in MIME format.
  441. --===============2138658284696597373==
  442. Content-Type: multipart/alternative; boundary="===============2233989480071754745=="
  443. MIME-Version: 1.0
  444. --===============2233989480071754745==
  445. Content-Type: text/plain; charset="utf-8"
  446. MIME-Version: 1.0
  447. Content-Transfer-Encoding: base64
  448. VGhpcyBpcyBhIHRlc3QgbWVzc2FnZSBmcm9tIFRydWVOQVMgQ09SRS4=
  449. --===============2233989480071754745==
  450. Content-Type: text/html; charset="utf-8"
  451. MIME-Version: 1.0
  452. Content-Transfer-Encoding: base64
  453. PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwv
  454. L0VOIj4KClRoaXMgaXMgYSB0ZXN0IG1lc3NhZ2UgZnJvbSBUcnVlTkFTIENPUkUuCg==
  455. --===============2233989480071754745==--
  456. --===============2138658284696597373==--
  457. .
  458. `
  459. s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) {
  460. require.Equal(t, "/mytopic", r.URL.Path)
  461. require.Equal(t, "TrueNAS truenas.local: TrueNAS Test Message hostname: truenas.local", r.Header.Get("Title"))
  462. require.Equal(t, "This is a test message from TrueNAS CORE.", readAll(t, r.Body))
  463. })
  464. defer s.Close()
  465. defer c.Close()
  466. writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued")
  467. }
  468. func TestSmtpBackend_NestedMultipartTooDeep(t *testing.T) {
  469. email := `EHLO example.com
  470. MAIL FROM: test@mydomain.me
  471. RCPT TO: ntfy-mytopic@ntfy.sh
  472. DATA
  473. Content-Type: multipart/mixed; boundary="===============1=="
  474. MIME-Version: 1.0
  475. Subject: TrueNAS truenas.local: TrueNAS Test Message hostname: truenas.local
  476. From: =?utf-8?q?Robbie?= <test@mydomain.me>
  477. To: test@mydomain.me
  478. Date: Thu, 16 Feb 2023 01:04:00 -0000
  479. Message-ID: <truenas-20230216.010400.344514.b'8jfL'@truenas.local>
  480. This is a multi-part message in MIME format.
  481. --===============1==
  482. Content-Type: multipart/alternative; boundary="===============2=="
  483. MIME-Version: 1.0
  484. --===============2==
  485. Content-Type: multipart/alternative; boundary="===============3=="
  486. MIME-Version: 1.0
  487. --===============3==
  488. Content-Type: text/plain; charset="utf-8"
  489. MIME-Version: 1.0
  490. Content-Transfer-Encoding: base64
  491. VGhpcyBpcyBhIHRlc3QgbWVzc2FnZSBmcm9tIFRydWVOQVMgQ09SRS4=
  492. --===============3==
  493. Content-Type: text/html; charset="utf-8"
  494. MIME-Version: 1.0
  495. Content-Transfer-Encoding: base64
  496. PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwv
  497. L0VOIj4KClRoaXMgaXMgYSB0ZXN0IG1lc3NhZ2UgZnJvbSBUcnVlTkFTIENPUkUuCg==
  498. --===============3==--
  499. --===============2==--
  500. --===============1==--
  501. .
  502. `
  503. s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) {
  504. t.Fatal("This should not be called")
  505. })
  506. defer s.Close()
  507. defer c.Close()
  508. writeAndReadUntilLine(t, email, c, scanner, "554 5.0.0 Error: transaction failed, blame it on the weather: multipart message nested too deep")
  509. }
  510. func TestSmtpBackend_PlaintextWithToken(t *testing.T) {
  511. email := `EHLO example.com
  512. MAIL FROM: phil@example.com
  513. RCPT TO: ntfy-mytopic+tk_KLORUqSqvNRLpY11DfkHVbHu9NGG2@ntfy.sh
  514. DATA
  515. Subject: Very short mail
  516. what's up
  517. .
  518. `
  519. s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) {
  520. require.Equal(t, "/mytopic", r.URL.Path)
  521. require.Equal(t, "Very short mail", r.Header.Get("Title"))
  522. require.Equal(t, "Bearer tk_KLORUqSqvNRLpY11DfkHVbHu9NGG2", r.Header.Get("Authorization"))
  523. require.Equal(t, "what's up", readAll(t, r.Body))
  524. })
  525. defer s.Close()
  526. defer c.Close()
  527. writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued")
  528. }
  529. type smtpHandlerFunc func(http.ResponseWriter, *http.Request)
  530. func newTestSMTPServer(t *testing.T, handler smtpHandlerFunc) (s *smtp.Server, c net.Conn, conf *Config, scanner *bufio.Scanner) {
  531. conf = newTestConfig(t)
  532. conf.SMTPServerListen = ":25"
  533. conf.SMTPServerDomain = "ntfy.sh"
  534. conf.SMTPServerAddrPrefix = "ntfy-"
  535. backend := newMailBackend(conf, handler)
  536. l, err := net.Listen("tcp", "127.0.0.1:0")
  537. if err != nil {
  538. t.Fatal(err)
  539. }
  540. s = smtp.NewServer(backend)
  541. s.Domain = conf.SMTPServerDomain
  542. s.AllowInsecureAuth = true
  543. go func() {
  544. require.Nil(t, s.Serve(l))
  545. }()
  546. c, err = net.Dial("tcp", l.Addr().String())
  547. if err != nil {
  548. t.Fatal(err)
  549. }
  550. scanner = bufio.NewScanner(c)
  551. return
  552. }
  553. func writeAndReadUntilLine(t *testing.T, email string, conn net.Conn, scanner *bufio.Scanner, expectedLine string) {
  554. _, err := io.WriteString(conn, email)
  555. require.Nil(t, err)
  556. readUntilLine(t, conn, scanner, expectedLine)
  557. }
  558. func readUntilLine(t *testing.T, conn net.Conn, scanner *bufio.Scanner, expectedLine string) {
  559. cancelChan := make(chan bool)
  560. go func() {
  561. select {
  562. case <-cancelChan:
  563. case <-time.After(3 * time.Second):
  564. conn.Close()
  565. t.Error("Failed waiting for expected output")
  566. }
  567. }()
  568. var output string
  569. for scanner.Scan() {
  570. text := scanner.Text()
  571. if strings.TrimSpace(text) == expectedLine {
  572. cancelChan <- true
  573. return
  574. }
  575. output += text + "\n"
  576. //fmt.Println(text)
  577. }
  578. t.Fatalf("Expected line '%s' not found in output:\n%s", expectedLine, output)
  579. }