smtp_server_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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_Unsupported(t *testing.T) {
  284. email := `EHLO example.com
  285. MAIL FROM: phil@example.com
  286. RCPT TO: ntfy-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/SOMETHINGELSE
  294. what's up
  295. .
  296. `
  297. s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) {
  298. t.Fatal("This should not be called")
  299. })
  300. defer s.Close()
  301. defer c.Close()
  302. writeAndReadUntilLine(t, email, c, scanner, "554 5.0.0 Error: transaction failed, blame it on the weather: unsupported content type")
  303. }
  304. func TestSmtpBackend_InvalidAddress(t *testing.T) {
  305. email := `EHLO example.com
  306. MAIL FROM: phil@example.com
  307. RCPT TO: unsupported@ntfy.sh
  308. DATA
  309. Date: Tue, 28 Dec 2021 00:30:10 +0100
  310. Subject: and one more
  311. From: Phil <phil@example.com>
  312. To: mytopic@ntfy.sh
  313. Content-Type: text/plain
  314. what's up
  315. .
  316. `
  317. s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) {
  318. t.Fatal("This should not be called")
  319. })
  320. defer s.Close()
  321. defer c.Close()
  322. writeAndReadUntilLine(t, email, c, scanner, "451 4.0.0 invalid address")
  323. }
  324. func TestSmtpBackend_Base64Body(t *testing.T) {
  325. email := `EHLO example.com
  326. MAIL FROM: test@mydomain.me
  327. RCPT TO: ntfy-mytopic@ntfy.sh
  328. DATA
  329. Content-Type: multipart/mixed; boundary="===============2138658284696597373=="
  330. MIME-Version: 1.0
  331. Subject: TrueNAS truenas.local: TrueNAS Test Message hostname: truenas.local
  332. From: =?utf-8?q?Robbie?= <test@mydomain.me>
  333. To: test@mydomain.me
  334. Date: Thu, 16 Feb 2023 01:04:00 -0000
  335. Message-ID: <truenas-20230216.010400.344514.b'8jfL'@truenas.local>
  336. This is a multi-part message in MIME format.
  337. --===============2138658284696597373==
  338. Content-Type: text/plain; charset="utf-8"
  339. MIME-Version: 1.0
  340. Content-Transfer-Encoding: base64
  341. VGhpcyBpcyBhIHRlc3QgbWVzc2FnZSBmcm9tIFRydWVOQVMgQ09SRS4=
  342. --===============2138658284696597373==
  343. Content-Type: text/html; charset="utf-8"
  344. MIME-Version: 1.0
  345. Content-Transfer-Encoding: base64
  346. PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwv
  347. L0VOIj4KClRoaXMgaXMgYSB0ZXN0IG1lc3NhZ2UgZnJvbSBUcnVlTkFTIENPUkUuCg==
  348. --===============2138658284696597373==--
  349. .
  350. `
  351. s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) {
  352. require.Equal(t, "/mytopic", r.URL.Path)
  353. require.Equal(t, "TrueNAS truenas.local: TrueNAS Test Message hostname: truenas.local", r.Header.Get("Title"))
  354. require.Equal(t, "This is a test message from TrueNAS CORE.", readAll(t, r.Body))
  355. })
  356. defer s.Close()
  357. defer c.Close()
  358. writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued")
  359. }
  360. func TestSmtpBackend_NestedMultipartBase64(t *testing.T) {
  361. email := `EHLO example.com
  362. MAIL FROM: test@mydomain.me
  363. RCPT TO: ntfy-mytopic@ntfy.sh
  364. DATA
  365. Content-Type: multipart/mixed; boundary="===============2138658284696597373=="
  366. MIME-Version: 1.0
  367. Subject: TrueNAS truenas.local: TrueNAS Test Message hostname: truenas.local
  368. From: =?utf-8?q?Robbie?= <test@mydomain.me>
  369. To: test@mydomain.me
  370. Date: Thu, 16 Feb 2023 01:04:00 -0000
  371. Message-ID: <truenas-20230216.010400.344514.b'8jfL'@truenas.local>
  372. This is a multi-part message in MIME format.
  373. --===============2138658284696597373==
  374. Content-Type: multipart/alternative; boundary="===============2233989480071754745=="
  375. MIME-Version: 1.0
  376. --===============2233989480071754745==
  377. Content-Type: text/plain; charset="utf-8"
  378. MIME-Version: 1.0
  379. Content-Transfer-Encoding: base64
  380. VGhpcyBpcyBhIHRlc3QgbWVzc2FnZSBmcm9tIFRydWVOQVMgQ09SRS4=
  381. --===============2233989480071754745==
  382. Content-Type: text/html; charset="utf-8"
  383. MIME-Version: 1.0
  384. Content-Transfer-Encoding: base64
  385. PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwv
  386. L0VOIj4KClRoaXMgaXMgYSB0ZXN0IG1lc3NhZ2UgZnJvbSBUcnVlTkFTIENPUkUuCg==
  387. --===============2233989480071754745==--
  388. --===============2138658284696597373==--
  389. .
  390. `
  391. s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) {
  392. require.Equal(t, "/mytopic", r.URL.Path)
  393. require.Equal(t, "TrueNAS truenas.local: TrueNAS Test Message hostname: truenas.local", r.Header.Get("Title"))
  394. require.Equal(t, "This is a test message from TrueNAS CORE.", readAll(t, r.Body))
  395. })
  396. defer s.Close()
  397. defer c.Close()
  398. writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued")
  399. }
  400. func TestSmtpBackend_NestedMultipartTooDeep(t *testing.T) {
  401. email := `EHLO example.com
  402. MAIL FROM: test@mydomain.me
  403. RCPT TO: ntfy-mytopic@ntfy.sh
  404. DATA
  405. Content-Type: multipart/mixed; boundary="===============1=="
  406. MIME-Version: 1.0
  407. Subject: TrueNAS truenas.local: TrueNAS Test Message hostname: truenas.local
  408. From: =?utf-8?q?Robbie?= <test@mydomain.me>
  409. To: test@mydomain.me
  410. Date: Thu, 16 Feb 2023 01:04:00 -0000
  411. Message-ID: <truenas-20230216.010400.344514.b'8jfL'@truenas.local>
  412. This is a multi-part message in MIME format.
  413. --===============1==
  414. Content-Type: multipart/alternative; boundary="===============2=="
  415. MIME-Version: 1.0
  416. --===============2==
  417. Content-Type: multipart/alternative; boundary="===============3=="
  418. MIME-Version: 1.0
  419. --===============3==
  420. Content-Type: text/plain; charset="utf-8"
  421. MIME-Version: 1.0
  422. Content-Transfer-Encoding: base64
  423. VGhpcyBpcyBhIHRlc3QgbWVzc2FnZSBmcm9tIFRydWVOQVMgQ09SRS4=
  424. --===============3==
  425. Content-Type: text/html; charset="utf-8"
  426. MIME-Version: 1.0
  427. Content-Transfer-Encoding: base64
  428. PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwv
  429. L0VOIj4KClRoaXMgaXMgYSB0ZXN0IG1lc3NhZ2UgZnJvbSBUcnVlTkFTIENPUkUuCg==
  430. --===============3==--
  431. --===============2==--
  432. --===============1==--
  433. .
  434. `
  435. s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) {
  436. t.Fatal("This should not be called")
  437. })
  438. defer s.Close()
  439. defer c.Close()
  440. writeAndReadUntilLine(t, email, c, scanner, "554 5.0.0 Error: transaction failed, blame it on the weather: multipart message nested too deep")
  441. }
  442. func TestSmtpBackend_PlaintextWithToken(t *testing.T) {
  443. email := `EHLO example.com
  444. MAIL FROM: phil@example.com
  445. RCPT TO: ntfy-mytopic+tk_KLORUqSqvNRLpY11DfkHVbHu9NGG2@ntfy.sh
  446. DATA
  447. Subject: Very short mail
  448. what's up
  449. .
  450. `
  451. s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) {
  452. require.Equal(t, "/mytopic", r.URL.Path)
  453. require.Equal(t, "Very short mail", r.Header.Get("Title"))
  454. require.Equal(t, "Bearer tk_KLORUqSqvNRLpY11DfkHVbHu9NGG2", r.Header.Get("Authorization"))
  455. require.Equal(t, "what's up", readAll(t, r.Body))
  456. })
  457. defer s.Close()
  458. defer c.Close()
  459. writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued")
  460. }
  461. type smtpHandlerFunc func(http.ResponseWriter, *http.Request)
  462. func newTestSMTPServer(t *testing.T, handler smtpHandlerFunc) (s *smtp.Server, c net.Conn, conf *Config, scanner *bufio.Scanner) {
  463. conf = newTestConfig(t)
  464. conf.SMTPServerListen = ":25"
  465. conf.SMTPServerDomain = "ntfy.sh"
  466. conf.SMTPServerAddrPrefix = "ntfy-"
  467. backend := newMailBackend(conf, handler)
  468. l, err := net.Listen("tcp", "127.0.0.1:0")
  469. if err != nil {
  470. t.Fatal(err)
  471. }
  472. s = smtp.NewServer(backend)
  473. s.Domain = conf.SMTPServerDomain
  474. s.AllowInsecureAuth = true
  475. go func() {
  476. require.Nil(t, s.Serve(l))
  477. }()
  478. c, err = net.Dial("tcp", l.Addr().String())
  479. if err != nil {
  480. t.Fatal(err)
  481. }
  482. scanner = bufio.NewScanner(c)
  483. return
  484. }
  485. func writeAndReadUntilLine(t *testing.T, email string, conn net.Conn, scanner *bufio.Scanner, expectedLine string) {
  486. _, err := io.WriteString(conn, email)
  487. require.Nil(t, err)
  488. readUntilLine(t, conn, scanner, expectedLine)
  489. }
  490. func readUntilLine(t *testing.T, conn net.Conn, scanner *bufio.Scanner, expectedLine string) {
  491. cancelChan := make(chan bool)
  492. go func() {
  493. select {
  494. case <-cancelChan:
  495. case <-time.After(3 * time.Second):
  496. conn.Close()
  497. t.Error("Failed waiting for expected output")
  498. }
  499. }()
  500. var output string
  501. for scanner.Scan() {
  502. text := scanner.Text()
  503. if strings.TrimSpace(text) == expectedLine {
  504. cancelChan <- true
  505. return
  506. }
  507. output += text + "\n"
  508. //fmt.Println(text)
  509. }
  510. t.Fatalf("Expected line '%s' not found in output:\n%s", expectedLine, output)
  511. }