PageRenderTime 43ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/Lib/test/test_smtpd.py

https://bitbucket.org/carljm/cpythonv/
Python | 289 lines | 269 code | 20 blank | 0 comment | 2 complexity | e1eca8345cb3e074d73b270037f56b20 MD5 | raw file
Possible License(s): 0BSD
  1. from unittest import TestCase
  2. from test import support, mock_socket
  3. import socket
  4. import io
  5. import smtpd
  6. import asyncore
  7. class DummyServer(smtpd.SMTPServer):
  8. def __init__(self, localaddr, remoteaddr):
  9. smtpd.SMTPServer.__init__(self, localaddr, remoteaddr)
  10. self.messages = []
  11. def process_message(self, peer, mailfrom, rcpttos, data):
  12. self.messages.append((peer, mailfrom, rcpttos, data))
  13. if data == 'return status':
  14. return '250 Okish'
  15. class DummyDispatcherBroken(Exception):
  16. pass
  17. class BrokenDummyServer(DummyServer):
  18. def listen(self, num):
  19. raise DummyDispatcherBroken()
  20. class SMTPDServerTest(TestCase):
  21. def setUp(self):
  22. smtpd.socket = asyncore.socket = mock_socket
  23. def test_process_message_unimplemented(self):
  24. server = smtpd.SMTPServer('a', 'b')
  25. conn, addr = server.accept()
  26. channel = smtpd.SMTPChannel(server, conn, addr)
  27. def write_line(line):
  28. channel.socket.queue_recv(line)
  29. channel.handle_read()
  30. write_line(b'MAIL From:eggs@example')
  31. write_line(b'RCPT To:spam@example')
  32. write_line(b'DATA')
  33. self.assertRaises(NotImplementedError, write_line, b'spam\r\n.\r\n')
  34. def tearDown(self):
  35. asyncore.close_all()
  36. asyncore.socket = smtpd.socket = socket
  37. class SMTPDChannelTest(TestCase):
  38. def setUp(self):
  39. smtpd.socket = asyncore.socket = mock_socket
  40. self.debug = smtpd.DEBUGSTREAM = io.StringIO()
  41. self.server = DummyServer('a', 'b')
  42. conn, addr = self.server.accept()
  43. self.channel = smtpd.SMTPChannel(self.server, conn, addr)
  44. def tearDown(self):
  45. asyncore.close_all()
  46. asyncore.socket = smtpd.socket = socket
  47. def write_line(self, line):
  48. self.channel.socket.queue_recv(line)
  49. self.channel.handle_read()
  50. def test_broken_connect(self):
  51. self.assertRaises(DummyDispatcherBroken, BrokenDummyServer, 'a', 'b')
  52. def test_server_accept(self):
  53. self.server.handle_accept()
  54. def test_missing_data(self):
  55. self.write_line(b'')
  56. self.assertEqual(self.channel.socket.last,
  57. b'500 Error: bad syntax\r\n')
  58. def test_EHLO_not_implemented(self):
  59. self.write_line(b'EHLO test.example')
  60. self.assertEqual(self.channel.socket.last,
  61. b'502 Error: command "EHLO" not implemented\r\n')
  62. def test_HELO(self):
  63. name = smtpd.socket.getfqdn()
  64. self.write_line(b'HELO test.example')
  65. self.assertEqual(self.channel.socket.last,
  66. '250 {}\r\n'.format(name).encode('ascii'))
  67. def test_HELO_bad_syntax(self):
  68. self.write_line(b'HELO')
  69. self.assertEqual(self.channel.socket.last,
  70. b'501 Syntax: HELO hostname\r\n')
  71. def test_HELO_duplicate(self):
  72. self.write_line(b'HELO test.example')
  73. self.write_line(b'HELO test.example')
  74. self.assertEqual(self.channel.socket.last,
  75. b'503 Duplicate HELO/EHLO\r\n')
  76. def test_NOOP(self):
  77. self.write_line(b'NOOP')
  78. self.assertEqual(self.channel.socket.last, b'250 Ok\r\n')
  79. def test_NOOP_bad_syntax(self):
  80. self.write_line(b'NOOP hi')
  81. self.assertEqual(self.channel.socket.last,
  82. b'501 Syntax: NOOP\r\n')
  83. def test_QUIT(self):
  84. self.write_line(b'QUIT')
  85. self.assertEqual(self.channel.socket.last, b'221 Bye\r\n')
  86. def test_QUIT_arg_ignored(self):
  87. self.write_line(b'QUIT bye bye')
  88. self.assertEqual(self.channel.socket.last, b'221 Bye\r\n')
  89. def test_bad_state(self):
  90. self.channel.smtp_state = 'BAD STATE'
  91. self.write_line(b'HELO')
  92. self.assertEqual(self.channel.socket.last,
  93. b'451 Internal confusion\r\n')
  94. def test_command_too_long(self):
  95. self.write_line(b'MAIL from ' +
  96. b'a' * self.channel.command_size_limit +
  97. b'@example')
  98. self.assertEqual(self.channel.socket.last,
  99. b'500 Error: line too long\r\n')
  100. def test_data_too_long(self):
  101. # Small hack. Setting limit to 2K octets here will save us some time.
  102. self.channel.data_size_limit = 2048
  103. self.write_line(b'MAIL From:eggs@example')
  104. self.write_line(b'RCPT To:spam@example')
  105. self.write_line(b'DATA')
  106. self.write_line(b'A' * self.channel.data_size_limit +
  107. b'A\r\n.')
  108. self.assertEqual(self.channel.socket.last,
  109. b'552 Error: Too much mail data\r\n')
  110. def test_need_MAIL(self):
  111. self.write_line(b'RCPT to:spam@example')
  112. self.assertEqual(self.channel.socket.last,
  113. b'503 Error: need MAIL command\r\n')
  114. def test_MAIL_syntax(self):
  115. self.write_line(b'MAIL from eggs@example')
  116. self.assertEqual(self.channel.socket.last,
  117. b'501 Syntax: MAIL FROM:<address>\r\n')
  118. def test_MAIL_missing_from(self):
  119. self.write_line(b'MAIL from:')
  120. self.assertEqual(self.channel.socket.last,
  121. b'501 Syntax: MAIL FROM:<address>\r\n')
  122. def test_MAIL_chevrons(self):
  123. self.write_line(b'MAIL from:<eggs@example>')
  124. self.assertEqual(self.channel.socket.last, b'250 Ok\r\n')
  125. def test_nested_MAIL(self):
  126. self.write_line(b'MAIL from:eggs@example')
  127. self.write_line(b'MAIL from:spam@example')
  128. self.assertEqual(self.channel.socket.last,
  129. b'503 Error: nested MAIL command\r\n')
  130. def test_need_RCPT(self):
  131. self.write_line(b'MAIL From:eggs@example')
  132. self.write_line(b'DATA')
  133. self.assertEqual(self.channel.socket.last,
  134. b'503 Error: need RCPT command\r\n')
  135. def test_RCPT_syntax(self):
  136. self.write_line(b'MAIL From:eggs@example')
  137. self.write_line(b'RCPT to eggs@example')
  138. self.assertEqual(self.channel.socket.last,
  139. b'501 Syntax: RCPT TO: <address>\r\n')
  140. def test_data_dialog(self):
  141. self.write_line(b'MAIL From:eggs@example')
  142. self.assertEqual(self.channel.socket.last, b'250 Ok\r\n')
  143. self.write_line(b'RCPT To:spam@example')
  144. self.assertEqual(self.channel.socket.last, b'250 Ok\r\n')
  145. self.write_line(b'DATA')
  146. self.assertEqual(self.channel.socket.last,
  147. b'354 End data with <CR><LF>.<CR><LF>\r\n')
  148. self.write_line(b'data\r\nmore\r\n.')
  149. self.assertEqual(self.channel.socket.last, b'250 Ok\r\n')
  150. self.assertEqual(self.server.messages,
  151. [('peer', 'eggs@example', ['spam@example'], 'data\nmore')])
  152. def test_DATA_syntax(self):
  153. self.write_line(b'MAIL From:eggs@example')
  154. self.write_line(b'RCPT To:spam@example')
  155. self.write_line(b'DATA spam')
  156. self.assertEqual(self.channel.socket.last, b'501 Syntax: DATA\r\n')
  157. def test_data_transparency_section_4_5_2(self):
  158. self.write_line(b'MAIL From:eggs@example')
  159. self.write_line(b'RCPT To:spam@example')
  160. self.write_line(b'DATA')
  161. self.write_line(b'..\r\n.\r\n')
  162. self.assertEqual(self.channel.received_data, '.')
  163. def test_multiple_RCPT(self):
  164. self.write_line(b'MAIL From:eggs@example')
  165. self.write_line(b'RCPT To:spam@example')
  166. self.write_line(b'RCPT To:ham@example')
  167. self.write_line(b'DATA')
  168. self.write_line(b'data\r\n.')
  169. self.assertEqual(self.server.messages,
  170. [('peer', 'eggs@example', ['spam@example','ham@example'], 'data')])
  171. def test_manual_status(self):
  172. # checks that the Channel is able to return a custom status message
  173. self.write_line(b'MAIL From:eggs@example')
  174. self.write_line(b'RCPT To:spam@example')
  175. self.write_line(b'DATA')
  176. self.write_line(b'return status\r\n.')
  177. self.assertEqual(self.channel.socket.last, b'250 Okish\r\n')
  178. def test_RSET(self):
  179. self.write_line(b'MAIL From:eggs@example')
  180. self.write_line(b'RCPT To:spam@example')
  181. self.write_line(b'RSET')
  182. self.assertEqual(self.channel.socket.last, b'250 Ok\r\n')
  183. self.write_line(b'MAIL From:foo@example')
  184. self.write_line(b'RCPT To:eggs@example')
  185. self.write_line(b'DATA')
  186. self.write_line(b'data\r\n.')
  187. self.assertEqual(self.server.messages,
  188. [('peer', 'foo@example', ['eggs@example'], 'data')])
  189. def test_RSET_syntax(self):
  190. self.write_line(b'RSET hi')
  191. self.assertEqual(self.channel.socket.last, b'501 Syntax: RSET\r\n')
  192. def test_attribute_deprecations(self):
  193. with support.check_warnings(('', PendingDeprecationWarning)):
  194. spam = self.channel._SMTPChannel__server
  195. with support.check_warnings(('', PendingDeprecationWarning)):
  196. self.channel._SMTPChannel__server = 'spam'
  197. with support.check_warnings(('', PendingDeprecationWarning)):
  198. spam = self.channel._SMTPChannel__line
  199. with support.check_warnings(('', PendingDeprecationWarning)):
  200. self.channel._SMTPChannel__line = 'spam'
  201. with support.check_warnings(('', PendingDeprecationWarning)):
  202. spam = self.channel._SMTPChannel__state
  203. with support.check_warnings(('', PendingDeprecationWarning)):
  204. self.channel._SMTPChannel__state = 'spam'
  205. with support.check_warnings(('', PendingDeprecationWarning)):
  206. spam = self.channel._SMTPChannel__greeting
  207. with support.check_warnings(('', PendingDeprecationWarning)):
  208. self.channel._SMTPChannel__greeting = 'spam'
  209. with support.check_warnings(('', PendingDeprecationWarning)):
  210. spam = self.channel._SMTPChannel__mailfrom
  211. with support.check_warnings(('', PendingDeprecationWarning)):
  212. self.channel._SMTPChannel__mailfrom = 'spam'
  213. with support.check_warnings(('', PendingDeprecationWarning)):
  214. spam = self.channel._SMTPChannel__rcpttos
  215. with support.check_warnings(('', PendingDeprecationWarning)):
  216. self.channel._SMTPChannel__rcpttos = 'spam'
  217. with support.check_warnings(('', PendingDeprecationWarning)):
  218. spam = self.channel._SMTPChannel__data
  219. with support.check_warnings(('', PendingDeprecationWarning)):
  220. self.channel._SMTPChannel__data = 'spam'
  221. with support.check_warnings(('', PendingDeprecationWarning)):
  222. spam = self.channel._SMTPChannel__fqdn
  223. with support.check_warnings(('', PendingDeprecationWarning)):
  224. self.channel._SMTPChannel__fqdn = 'spam'
  225. with support.check_warnings(('', PendingDeprecationWarning)):
  226. spam = self.channel._SMTPChannel__peer
  227. with support.check_warnings(('', PendingDeprecationWarning)):
  228. self.channel._SMTPChannel__peer = 'spam'
  229. with support.check_warnings(('', PendingDeprecationWarning)):
  230. spam = self.channel._SMTPChannel__conn
  231. with support.check_warnings(('', PendingDeprecationWarning)):
  232. self.channel._SMTPChannel__conn = 'spam'
  233. with support.check_warnings(('', PendingDeprecationWarning)):
  234. spam = self.channel._SMTPChannel__addr
  235. with support.check_warnings(('', PendingDeprecationWarning)):
  236. self.channel._SMTPChannel__addr = 'spam'
  237. def test_main():
  238. support.run_unittest(SMTPDServerTest, SMTPDChannelTest)
  239. if __name__ == "__main__":
  240. test_main()