smtp_server_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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_EncodedSubject(t *testing.T) {
  81. email := `Date: Tue, 28 Dec 2021 00:30:10 +0100
  82. Subject: =?UTF-8?B?VGhyZWUgc2FudGFzIPCfjoXwn46F8J+OhQ==?=
  83. From: Phil <phil@example.com>
  84. To: ntfy-mytopic@ntfy.sh
  85. Content-Type: text/plain; charset="UTF-8"
  86. what's up
  87. `
  88. _, backend := newTestBackend(t, func(m *message) error {
  89. require.Equal(t, "Three santas ๐ŸŽ…๐ŸŽ…๐ŸŽ…", m.Title)
  90. return nil
  91. })
  92. session, _ := backend.AnonymousLogin(nil)
  93. require.Nil(t, session.Mail("phil@example.com", smtp.MailOptions{}))
  94. require.Nil(t, session.Rcpt("ntfy-mytopic@ntfy.sh"))
  95. require.Nil(t, session.Data(strings.NewReader(email)))
  96. }
  97. func TestSmtpBackend_Plaintext_TooLongTruncate(t *testing.T) {
  98. email := `Date: Tue, 28 Dec 2021 00:30:10 +0100
  99. Message-ID: <CAAvm79YP0C=Rt1N=KWmSUBB87KK2rRChmdzKqF1vCwMEUiVzLQ@mail.gmail.com>
  100. Subject: and one more
  101. From: Phil <phil@example.com>
  102. To: mytopic@ntfy.sh
  103. Content-Type: text/plain; charset="UTF-8"
  104. you know this is a string.
  105. it's a long string.
  106. it's supposed to be longer than the max message length
  107. which is 4096 bytes,
  108. it used to be 512 bytes, but I increased that for the UnifiedPush support
  109. the 512 bytes was a little short, some people said
  110. but it kinda makes sense when you look at what it looks like one a phone
  111. heck this wasn't even half of it so far.
  112. so i'm gonna fill the rest of this with AAAAAAAAAAAAAAAAAAAAAAAAAAA
  113. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  114. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAa
  115. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  116. ......................................................................
  117. ......................................................................
  118. ......................................................................
  119. ......................................................................
  120. ......................................................................
  121. ......................................................................
  122. ......................................................................
  123. ......................................................................
  124. ......................................................................
  125. ......................................................................
  126. ......................................................................
  127. ......................................................................
  128. ......................................................................
  129. ......................................................................
  130. ......................................................................
  131. ......................................................................
  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. and with BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
  165. BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
  166. BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
  167. that should do it
  168. `
  169. conf, backend := newTestBackend(t, func(m *message) error {
  170. expected := `you know this is a string.
  171. it's a long string.
  172. it's supposed to be longer than the max message length
  173. which is 4096 bytes,
  174. it used to be 512 bytes, but I increased that for the UnifiedPush support
  175. the 512 bytes was a little short, some people said
  176. but it kinda makes sense when you look at what it looks like one a phone
  177. heck this wasn't even half of it so far.
  178. so i'm gonna fill the rest of this with AAAAAAAAAAAAAAAAAAAAAAAAAAA
  179. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  180. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAa
  181. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  182. ......................................................................
  183. ......................................................................
  184. ......................................................................
  185. ......................................................................
  186. ......................................................................
  187. ......................................................................
  188. ......................................................................
  189. ......................................................................
  190. ......................................................................
  191. ......................................................................
  192. ......................................................................
  193. ......................................................................
  194. ......................................................................
  195. ......................................................................
  196. ......................................................................
  197. ......................................................................
  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. and with BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
  231. BBBBBBBBBBBBBBBBBBBBBBBB`
  232. require.Equal(t, 4096, len(expected)) // Sanity check
  233. require.Equal(t, expected, m.Message)
  234. return nil
  235. })
  236. conf.SMTPServerAddrPrefix = ""
  237. session, _ := backend.AnonymousLogin(nil)
  238. require.Nil(t, session.Mail("phil@example.com", smtp.MailOptions{}))
  239. require.Nil(t, session.Rcpt("mytopic@ntfy.sh"))
  240. require.Nil(t, session.Data(strings.NewReader(email)))
  241. }
  242. func TestSmtpBackend_Unsupported(t *testing.T) {
  243. email := `Date: Tue, 28 Dec 2021 00:30:10 +0100
  244. Message-ID: <CAAvm79YP0C=Rt1N=KWmSUBB87KK2rRChmdzKqF1vCwMEUiVzLQ@mail.gmail.com>
  245. Subject: and one more
  246. From: Phil <phil@example.com>
  247. To: mytopic@ntfy.sh
  248. Content-Type: text/SOMETHINGELSE
  249. what's up
  250. `
  251. conf, backend := newTestBackend(t, func(m *message) error {
  252. return nil
  253. })
  254. conf.SMTPServerAddrPrefix = ""
  255. session, _ := backend.Login(nil, "user", "pass")
  256. require.Nil(t, session.Mail("phil@example.com", smtp.MailOptions{}))
  257. require.Nil(t, session.Rcpt("mytopic@ntfy.sh"))
  258. require.Equal(t, errUnsupportedContentType, session.Data(strings.NewReader(email)))
  259. }
  260. func newTestBackend(t *testing.T, sub subscriber) (*Config, *smtpBackend) {
  261. conf := newTestConfig(t)
  262. conf.SMTPServerListen = ":25"
  263. conf.SMTPServerDomain = "ntfy.sh"
  264. conf.SMTPServerAddrPrefix = "ntfy-"
  265. backend := newMailBackend(conf, sub)
  266. return conf, backend
  267. }