smtp_server_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package server
  2. import (
  3. "github.com/emersion/go-smtp"
  4. "github.com/stretchr/testify/require"
  5. "strings"
  6. "testing"
  7. )
  8. func TestSmtpBackend_Multipart(t *testing.T) {
  9. email := `MIME-Version: 1.0
  10. Date: Tue, 28 Dec 2021 00:30:10 +0100
  11. Message-ID: <CAAvm79YP0C=Rt1N=KWmSUBB87KK2rRChmdzKqF1vCwMEUiVzLQ@mail.gmail.com>
  12. Subject: and one more
  13. From: Phil <phil@example.com>
  14. To: ntfy-mytopic@ntfy.sh
  15. Content-Type: multipart/alternative; boundary="000000000000f3320b05d42915c9"
  16. --000000000000f3320b05d42915c9
  17. Content-Type: text/plain; charset="UTF-8"
  18. what's up
  19. --000000000000f3320b05d42915c9
  20. Content-Type: text/html; charset="UTF-8"
  21. <div dir="ltr">what&#39;s up<br clear="all"><div><br></div></div>
  22. --000000000000f3320b05d42915c9--`
  23. _, backend := newTestBackend(t, func(m *message) error {
  24. require.Equal(t, "mytopic", m.Topic)
  25. require.Equal(t, "and one more", m.Title)
  26. require.Equal(t, "what's up", m.Message)
  27. return nil
  28. })
  29. session, _ := backend.AnonymousLogin(nil)
  30. require.Nil(t, session.Mail("phil@example.com", smtp.MailOptions{}))
  31. require.Nil(t, session.Rcpt("ntfy-mytopic@ntfy.sh"))
  32. require.Nil(t, session.Data(strings.NewReader(email)))
  33. }
  34. func TestSmtpBackend_MultipartNoBody(t *testing.T) {
  35. email := `MIME-Version: 1.0
  36. Date: Tue, 28 Dec 2021 01:33:34 +0100
  37. Message-ID: <CAAvm7ABCDsi9vsuu0WTRXzZQBC8dXrDOLT8iCWdqrsmg@mail.gmail.com>
  38. Subject: This email has a subject but no body
  39. From: Phil <phil@example.com>
  40. To: ntfy-emailtest@ntfy.sh
  41. Content-Type: multipart/alternative; boundary="000000000000bcf4a405d429f8d4"
  42. --000000000000bcf4a405d429f8d4
  43. Content-Type: text/plain; charset="UTF-8"
  44. --000000000000bcf4a405d429f8d4
  45. Content-Type: text/html; charset="UTF-8"
  46. <div dir="ltr"><br></div>
  47. --000000000000bcf4a405d429f8d4--`
  48. _, backend := newTestBackend(t, func(m *message) error {
  49. require.Equal(t, "emailtest", m.Topic)
  50. require.Equal(t, "", m.Title) // We flipped message and body
  51. require.Equal(t, "This email has a subject but no body", m.Message)
  52. return nil
  53. })
  54. session, _ := backend.AnonymousLogin(nil)
  55. require.Nil(t, session.Mail("phil@example.com", smtp.MailOptions{}))
  56. require.Nil(t, session.Rcpt("ntfy-emailtest@ntfy.sh"))
  57. require.Nil(t, session.Data(strings.NewReader(email)))
  58. }
  59. func TestSmtpBackend_Plaintext(t *testing.T) {
  60. email := `Date: Tue, 28 Dec 2021 00:30:10 +0100
  61. Message-ID: <CAAvm79YP0C=Rt1N=KWmSUBB87KK2rRChmdzKqF1vCwMEUiVzLQ@mail.gmail.com>
  62. Subject: and one more
  63. From: Phil <phil@example.com>
  64. To: mytopic@ntfy.sh
  65. Content-Type: text/plain; charset="UTF-8"
  66. what's up
  67. `
  68. conf, backend := newTestBackend(t, func(m *message) error {
  69. require.Equal(t, "mytopic", m.Topic)
  70. require.Equal(t, "and one more", m.Title)
  71. require.Equal(t, "what's up", m.Message)
  72. return nil
  73. })
  74. conf.SMTPServerAddrPrefix = ""
  75. session, _ := backend.AnonymousLogin(nil)
  76. require.Nil(t, session.Mail("phil@example.com", smtp.MailOptions{}))
  77. require.Nil(t, session.Rcpt("mytopic@ntfy.sh"))
  78. require.Nil(t, session.Data(strings.NewReader(email)))
  79. }
  80. func TestSmtpBackend_Plaintext_No_ContentType(t *testing.T) {
  81. email := `Subject: Very short mail
  82. what's up
  83. `
  84. conf, backend := newTestBackend(t, func(m *message) error {
  85. require.Equal(t, "mytopic", m.Topic)
  86. require.Equal(t, "Very short mail", m.Title)
  87. require.Equal(t, "what's up", m.Message)
  88. return nil
  89. })
  90. conf.SMTPServerAddrPrefix = ""
  91. session, _ := backend.AnonymousLogin(nil)
  92. require.Nil(t, session.Mail("phil@example.com", smtp.MailOptions{}))
  93. require.Nil(t, session.Rcpt("mytopic@ntfy.sh"))
  94. require.Nil(t, session.Data(strings.NewReader(email)))
  95. }
  96. func TestSmtpBackend_Plaintext_EncodedSubject(t *testing.T) {
  97. email := `Date: Tue, 28 Dec 2021 00:30:10 +0100
  98. Subject: =?UTF-8?B?VGhyZWUgc2FudGFzIPCfjoXwn46F8J+OhQ==?=
  99. From: Phil <phil@example.com>
  100. To: ntfy-mytopic@ntfy.sh
  101. Content-Type: text/plain; charset="UTF-8"
  102. what's up
  103. `
  104. _, backend := newTestBackend(t, func(m *message) error {
  105. require.Equal(t, "Three santas 🎅🎅🎅", m.Title)
  106. return nil
  107. })
  108. session, _ := backend.AnonymousLogin(nil)
  109. require.Nil(t, session.Mail("phil@example.com", smtp.MailOptions{}))
  110. require.Nil(t, session.Rcpt("ntfy-mytopic@ntfy.sh"))
  111. require.Nil(t, session.Data(strings.NewReader(email)))
  112. }
  113. func TestSmtpBackend_Plaintext_TooLongTruncate(t *testing.T) {
  114. email := `Date: Tue, 28 Dec 2021 00:30:10 +0100
  115. Message-ID: <CAAvm79YP0C=Rt1N=KWmSUBB87KK2rRChmdzKqF1vCwMEUiVzLQ@mail.gmail.com>
  116. Subject: and one more
  117. From: Phil <phil@example.com>
  118. To: mytopic@ntfy.sh
  119. Content-Type: text/plain; charset="UTF-8"
  120. you know this is a string.
  121. it's a long string.
  122. it's supposed to be longer than the max message length
  123. which is 4096 bytes,
  124. it used to be 512 bytes, but I increased that for the UnifiedPush support
  125. the 512 bytes was a little short, some people said
  126. but it kinda makes sense when you look at what it looks like one a phone
  127. heck this wasn't even half of it so far.
  128. so i'm gonna fill the rest of this with AAAAAAAAAAAAAAAAAAAAAAAAAAA
  129. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  130. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAa
  131. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  132. ......................................................................
  133. ......................................................................
  134. ......................................................................
  135. ......................................................................
  136. ......................................................................
  137. ......................................................................
  138. ......................................................................
  139. ......................................................................
  140. ......................................................................
  141. ......................................................................
  142. ......................................................................
  143. ......................................................................
  144. ......................................................................
  145. ......................................................................
  146. ......................................................................
  147. ......................................................................
  148. ......................................................................
  149. ......................................................................
  150. ......................................................................
  151. ......................................................................
  152. ......................................................................
  153. ......................................................................
  154. ......................................................................
  155. ......................................................................
  156. ......................................................................
  157. ......................................................................
  158. ......................................................................
  159. ......................................................................
  160. ......................................................................
  161. ......................................................................
  162. ......................................................................
  163. ......................................................................
  164. ......................................................................
  165. ......................................................................
  166. ......................................................................
  167. ......................................................................
  168. ......................................................................
  169. ......................................................................
  170. ......................................................................
  171. ......................................................................
  172. ......................................................................
  173. ......................................................................
  174. ......................................................................
  175. ......................................................................
  176. ......................................................................
  177. ......................................................................
  178. ......................................................................
  179. ......................................................................
  180. and with BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
  181. BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
  182. BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
  183. that should do it
  184. `
  185. conf, backend := newTestBackend(t, func(m *message) error {
  186. expected := `you know this is a string.
  187. it's a long string.
  188. it's supposed to be longer than the max message length
  189. which is 4096 bytes,
  190. it used to be 512 bytes, but I increased that for the UnifiedPush support
  191. the 512 bytes was a little short, some people said
  192. but it kinda makes sense when you look at what it looks like one a phone
  193. heck this wasn't even half of it so far.
  194. so i'm gonna fill the rest of this with AAAAAAAAAAAAAAAAAAAAAAAAAAA
  195. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  196. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAa
  197. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  198. ......................................................................
  199. ......................................................................
  200. ......................................................................
  201. ......................................................................
  202. ......................................................................
  203. ......................................................................
  204. ......................................................................
  205. ......................................................................
  206. ......................................................................
  207. ......................................................................
  208. ......................................................................
  209. ......................................................................
  210. ......................................................................
  211. ......................................................................
  212. ......................................................................
  213. ......................................................................
  214. ......................................................................
  215. ......................................................................
  216. ......................................................................
  217. ......................................................................
  218. ......................................................................
  219. ......................................................................
  220. ......................................................................
  221. ......................................................................
  222. ......................................................................
  223. ......................................................................
  224. ......................................................................
  225. ......................................................................
  226. ......................................................................
  227. ......................................................................
  228. ......................................................................
  229. ......................................................................
  230. ......................................................................
  231. ......................................................................
  232. ......................................................................
  233. ......................................................................
  234. ......................................................................
  235. ......................................................................
  236. ......................................................................
  237. ......................................................................
  238. ......................................................................
  239. ......................................................................
  240. ......................................................................
  241. ......................................................................
  242. ......................................................................
  243. ......................................................................
  244. ......................................................................
  245. ......................................................................
  246. and with BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
  247. BBBBBBBBBBBBBBBBBBBBBBBB`
  248. require.Equal(t, 4096, len(expected)) // Sanity check
  249. require.Equal(t, expected, m.Message)
  250. return nil
  251. })
  252. conf.SMTPServerAddrPrefix = ""
  253. session, _ := backend.AnonymousLogin(nil)
  254. require.Nil(t, session.Mail("phil@example.com", smtp.MailOptions{}))
  255. require.Nil(t, session.Rcpt("mytopic@ntfy.sh"))
  256. require.Nil(t, session.Data(strings.NewReader(email)))
  257. }
  258. func TestSmtpBackend_Unsupported(t *testing.T) {
  259. email := `Date: Tue, 28 Dec 2021 00:30:10 +0100
  260. Message-ID: <CAAvm79YP0C=Rt1N=KWmSUBB87KK2rRChmdzKqF1vCwMEUiVzLQ@mail.gmail.com>
  261. Subject: and one more
  262. From: Phil <phil@example.com>
  263. To: mytopic@ntfy.sh
  264. Content-Type: text/SOMETHINGELSE
  265. what's up
  266. `
  267. conf, backend := newTestBackend(t, func(m *message) error {
  268. return nil
  269. })
  270. conf.SMTPServerAddrPrefix = ""
  271. session, _ := backend.Login(nil, "user", "pass")
  272. require.Nil(t, session.Mail("phil@example.com", smtp.MailOptions{}))
  273. require.Nil(t, session.Rcpt("mytopic@ntfy.sh"))
  274. require.Equal(t, errUnsupportedContentType, session.Data(strings.NewReader(email)))
  275. }
  276. func newTestBackend(t *testing.T, sub subscriber) (*Config, *smtpBackend) {
  277. conf := newTestConfig(t)
  278. conf.SMTPServerListen = ":25"
  279. conf.SMTPServerDomain = "ntfy.sh"
  280. conf.SMTPServerAddrPrefix = "ntfy-"
  281. backend := newMailBackend(conf, sub)
  282. return conf, backend
  283. }