PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Python-3.2.3/Lib/test/test_smtpd.py

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