smtp_server_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. type smtpHandlerFunc func(http.ResponseWriter, *http.Request)
  325. func newTestSMTPServer(t *testing.T, handler smtpHandlerFunc) (s *smtp.Server, c net.Conn, conf *Config, scanner *bufio.Scanner) {
  326. conf = newTestConfig(t)
  327. conf.SMTPServerListen = ":25"
  328. conf.SMTPServerDomain = "ntfy.sh"
  329. conf.SMTPServerAddrPrefix = "ntfy-"
  330. backend := newMailBackend(conf, handler)
  331. l, err := net.Listen("tcp", "127.0.0.1:0")
  332. if err != nil {
  333. t.Fatal(err)
  334. }
  335. s = smtp.NewServer(backend)
  336. s.Domain = conf.SMTPServerDomain
  337. s.AllowInsecureAuth = true
  338. go func() {
  339. require.Nil(t, s.Serve(l))
  340. }()
  341. c, err = net.Dial("tcp", l.Addr().String())
  342. if err != nil {
  343. t.Fatal(err)
  344. }
  345. scanner = bufio.NewScanner(c)
  346. return
  347. }
  348. func writeAndReadUntilLine(t *testing.T, email string, conn net.Conn, scanner *bufio.Scanner, expectedLine string) {
  349. _, err := io.WriteString(conn, email)
  350. require.Nil(t, err)
  351. readUntilLine(t, conn, scanner, expectedLine)
  352. }
  353. func readUntilLine(t *testing.T, conn net.Conn, scanner *bufio.Scanner, expectedLine string) {
  354. cancelChan := make(chan bool)
  355. go func() {
  356. select {
  357. case <-cancelChan:
  358. case <-time.After(3 * time.Second):
  359. conn.Close()
  360. t.Error("Failed waiting for expected output")
  361. }
  362. }()
  363. var output string
  364. for scanner.Scan() {
  365. text := scanner.Text()
  366. if strings.TrimSpace(text) == expectedLine {
  367. cancelChan <- true
  368. return
  369. }
  370. output += text + "\n"
  371. //fmt.Println(text)
  372. }
  373. t.Fatalf("Expected line '%s' not found in output:\n%s", expectedLine, output)
  374. }