test_smtpd.py 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. import unittest
  2. import textwrap
  3. from test import support, mock_socket
  4. from test.support import socket_helper
  5. from test.support import warnings_helper
  6. import socket
  7. import io
  8. smtpd = warnings_helper.import_deprecated('smtpd')
  9. asyncore = warnings_helper.import_deprecated('asyncore')
  10. if not socket_helper.has_gethostname:
  11. raise unittest.SkipTest("test requires gethostname()")
  12. class DummyServer(smtpd.SMTPServer):
  13. def __init__(self, *args, **kwargs):
  14. smtpd.SMTPServer.__init__(self, *args, **kwargs)
  15. self.messages = []
  16. if self._decode_data:
  17. self.return_status = 'return status'
  18. else:
  19. self.return_status = b'return status'
  20. def process_message(self, peer, mailfrom, rcpttos, data, **kw):
  21. self.messages.append((peer, mailfrom, rcpttos, data))
  22. if data == self.return_status:
  23. return '250 Okish'
  24. if 'mail_options' in kw and 'SMTPUTF8' in kw['mail_options']:
  25. return '250 SMTPUTF8 message okish'
  26. class DummyDispatcherBroken(Exception):
  27. pass
  28. class BrokenDummyServer(DummyServer):
  29. def listen(self, num):
  30. raise DummyDispatcherBroken()
  31. class SMTPDServerTest(unittest.TestCase):
  32. def setUp(self):
  33. smtpd.socket = asyncore.socket = mock_socket
  34. def test_process_message_unimplemented(self):
  35. server = smtpd.SMTPServer((socket_helper.HOST, 0), ('b', 0),
  36. decode_data=True)
  37. conn, addr = server.accept()
  38. channel = smtpd.SMTPChannel(server, conn, addr, decode_data=True)
  39. def write_line(line):
  40. channel.socket.queue_recv(line)
  41. channel.handle_read()
  42. write_line(b'HELO example')
  43. write_line(b'MAIL From:eggs@example')
  44. write_line(b'RCPT To:spam@example')
  45. write_line(b'DATA')
  46. self.assertRaises(NotImplementedError, write_line, b'spam\r\n.\r\n')
  47. def test_decode_data_and_enable_SMTPUTF8_raises(self):
  48. self.assertRaises(
  49. ValueError,
  50. smtpd.SMTPServer,
  51. (socket_helper.HOST, 0),
  52. ('b', 0),
  53. enable_SMTPUTF8=True,
  54. decode_data=True)
  55. def tearDown(self):
  56. asyncore.close_all()
  57. asyncore.socket = smtpd.socket = socket
  58. class DebuggingServerTest(unittest.TestCase):
  59. def setUp(self):
  60. smtpd.socket = asyncore.socket = mock_socket
  61. def send_data(self, channel, data, enable_SMTPUTF8=False):
  62. def write_line(line):
  63. channel.socket.queue_recv(line)
  64. channel.handle_read()
  65. write_line(b'EHLO example')
  66. if enable_SMTPUTF8:
  67. write_line(b'MAIL From:eggs@example BODY=8BITMIME SMTPUTF8')
  68. else:
  69. write_line(b'MAIL From:eggs@example')
  70. write_line(b'RCPT To:spam@example')
  71. write_line(b'DATA')
  72. write_line(data)
  73. write_line(b'.')
  74. def test_process_message_with_decode_data_true(self):
  75. server = smtpd.DebuggingServer((socket_helper.HOST, 0), ('b', 0),
  76. decode_data=True)
  77. conn, addr = server.accept()
  78. channel = smtpd.SMTPChannel(server, conn, addr, decode_data=True)
  79. with support.captured_stdout() as s:
  80. self.send_data(channel, b'From: test\n\nhello\n')
  81. stdout = s.getvalue()
  82. self.assertEqual(stdout, textwrap.dedent("""\
  83. ---------- MESSAGE FOLLOWS ----------
  84. From: test
  85. X-Peer: peer-address
  86. hello
  87. ------------ END MESSAGE ------------
  88. """))
  89. def test_process_message_with_decode_data_false(self):
  90. server = smtpd.DebuggingServer((socket_helper.HOST, 0), ('b', 0))
  91. conn, addr = server.accept()
  92. channel = smtpd.SMTPChannel(server, conn, addr)
  93. with support.captured_stdout() as s:
  94. self.send_data(channel, b'From: test\n\nh\xc3\xa9llo\xff\n')
  95. stdout = s.getvalue()
  96. self.assertEqual(stdout, textwrap.dedent("""\
  97. ---------- MESSAGE FOLLOWS ----------
  98. b'From: test'
  99. b'X-Peer: peer-address'
  100. b''
  101. b'h\\xc3\\xa9llo\\xff'
  102. ------------ END MESSAGE ------------
  103. """))
  104. def test_process_message_with_enable_SMTPUTF8_true(self):
  105. server = smtpd.DebuggingServer((socket_helper.HOST, 0), ('b', 0),
  106. enable_SMTPUTF8=True)
  107. conn, addr = server.accept()
  108. channel = smtpd.SMTPChannel(server, conn, addr, enable_SMTPUTF8=True)
  109. with support.captured_stdout() as s:
  110. self.send_data(channel, b'From: test\n\nh\xc3\xa9llo\xff\n')
  111. stdout = s.getvalue()
  112. self.assertEqual(stdout, textwrap.dedent("""\
  113. ---------- MESSAGE FOLLOWS ----------
  114. b'From: test'
  115. b'X-Peer: peer-address'
  116. b''
  117. b'h\\xc3\\xa9llo\\xff'
  118. ------------ END MESSAGE ------------
  119. """))
  120. def test_process_SMTPUTF8_message_with_enable_SMTPUTF8_true(self):
  121. server = smtpd.DebuggingServer((socket_helper.HOST, 0), ('b', 0),
  122. enable_SMTPUTF8=True)
  123. conn, addr = server.accept()
  124. channel = smtpd.SMTPChannel(server, conn, addr, enable_SMTPUTF8=True)
  125. with support.captured_stdout() as s:
  126. self.send_data(channel, b'From: test\n\nh\xc3\xa9llo\xff\n',
  127. enable_SMTPUTF8=True)
  128. stdout = s.getvalue()
  129. self.assertEqual(stdout, textwrap.dedent("""\
  130. ---------- MESSAGE FOLLOWS ----------
  131. mail options: ['BODY=8BITMIME', 'SMTPUTF8']
  132. b'From: test'
  133. b'X-Peer: peer-address'
  134. b''
  135. b'h\\xc3\\xa9llo\\xff'
  136. ------------ END MESSAGE ------------
  137. """))
  138. def tearDown(self):
  139. asyncore.close_all()
  140. asyncore.socket = smtpd.socket = socket
  141. class TestFamilyDetection(unittest.TestCase):
  142. def setUp(self):
  143. smtpd.socket = asyncore.socket = mock_socket
  144. def tearDown(self):
  145. asyncore.close_all()
  146. asyncore.socket = smtpd.socket = socket
  147. @unittest.skipUnless(socket_helper.IPV6_ENABLED, "IPv6 not enabled")
  148. def test_socket_uses_IPv6(self):
  149. server = smtpd.SMTPServer((socket_helper.HOSTv6, 0), (socket_helper.HOSTv4, 0))
  150. self.assertEqual(server.socket.family, socket.AF_INET6)
  151. def test_socket_uses_IPv4(self):
  152. server = smtpd.SMTPServer((socket_helper.HOSTv4, 0), (socket_helper.HOSTv6, 0))
  153. self.assertEqual(server.socket.family, socket.AF_INET)
  154. class TestRcptOptionParsing(unittest.TestCase):
  155. error_response = (b'555 RCPT TO parameters not recognized or not '
  156. b'implemented\r\n')
  157. def setUp(self):
  158. smtpd.socket = asyncore.socket = mock_socket
  159. self.old_debugstream = smtpd.DEBUGSTREAM
  160. self.debug = smtpd.DEBUGSTREAM = io.StringIO()
  161. def tearDown(self):
  162. asyncore.close_all()
  163. asyncore.socket = smtpd.socket = socket
  164. smtpd.DEBUGSTREAM = self.old_debugstream
  165. def write_line(self, channel, line):
  166. channel.socket.queue_recv(line)
  167. channel.handle_read()
  168. def test_params_rejected(self):
  169. server = DummyServer((socket_helper.HOST, 0), ('b', 0))
  170. conn, addr = server.accept()
  171. channel = smtpd.SMTPChannel(server, conn, addr)
  172. self.write_line(channel, b'EHLO example')
  173. self.write_line(channel, b'MAIL from: <foo@example.com> size=20')
  174. self.write_line(channel, b'RCPT to: <foo@example.com> foo=bar')
  175. self.assertEqual(channel.socket.last, self.error_response)
  176. def test_nothing_accepted(self):
  177. server = DummyServer((socket_helper.HOST, 0), ('b', 0))
  178. conn, addr = server.accept()
  179. channel = smtpd.SMTPChannel(server, conn, addr)
  180. self.write_line(channel, b'EHLO example')
  181. self.write_line(channel, b'MAIL from: <foo@example.com> size=20')
  182. self.write_line(channel, b'RCPT to: <foo@example.com>')
  183. self.assertEqual(channel.socket.last, b'250 OK\r\n')
  184. class TestMailOptionParsing(unittest.TestCase):
  185. error_response = (b'555 MAIL FROM parameters not recognized or not '
  186. b'implemented\r\n')
  187. def setUp(self):
  188. smtpd.socket = asyncore.socket = mock_socket
  189. self.old_debugstream = smtpd.DEBUGSTREAM
  190. self.debug = smtpd.DEBUGSTREAM = io.StringIO()
  191. def tearDown(self):
  192. asyncore.close_all()
  193. asyncore.socket = smtpd.socket = socket
  194. smtpd.DEBUGSTREAM = self.old_debugstream
  195. def write_line(self, channel, line):
  196. channel.socket.queue_recv(line)
  197. channel.handle_read()
  198. def test_with_decode_data_true(self):
  199. server = DummyServer((socket_helper.HOST, 0), ('b', 0), decode_data=True)
  200. conn, addr = server.accept()
  201. channel = smtpd.SMTPChannel(server, conn, addr, decode_data=True)
  202. self.write_line(channel, b'EHLO example')
  203. for line in [
  204. b'MAIL from: <foo@example.com> size=20 SMTPUTF8',
  205. b'MAIL from: <foo@example.com> size=20 SMTPUTF8 BODY=8BITMIME',
  206. b'MAIL from: <foo@example.com> size=20 BODY=UNKNOWN',
  207. b'MAIL from: <foo@example.com> size=20 body=8bitmime',
  208. ]:
  209. self.write_line(channel, line)
  210. self.assertEqual(channel.socket.last, self.error_response)
  211. self.write_line(channel, b'MAIL from: <foo@example.com> size=20')
  212. self.assertEqual(channel.socket.last, b'250 OK\r\n')
  213. def test_with_decode_data_false(self):
  214. server = DummyServer((socket_helper.HOST, 0), ('b', 0))
  215. conn, addr = server.accept()
  216. channel = smtpd.SMTPChannel(server, conn, addr)
  217. self.write_line(channel, b'EHLO example')
  218. for line in [
  219. b'MAIL from: <foo@example.com> size=20 SMTPUTF8',
  220. b'MAIL from: <foo@example.com> size=20 SMTPUTF8 BODY=8BITMIME',
  221. ]:
  222. self.write_line(channel, line)
  223. self.assertEqual(channel.socket.last, self.error_response)
  224. self.write_line(
  225. channel,
  226. b'MAIL from: <foo@example.com> size=20 SMTPUTF8 BODY=UNKNOWN')
  227. self.assertEqual(
  228. channel.socket.last,
  229. b'501 Error: BODY can only be one of 7BIT, 8BITMIME\r\n')
  230. self.write_line(
  231. channel, b'MAIL from: <foo@example.com> size=20 body=8bitmime')
  232. self.assertEqual(channel.socket.last, b'250 OK\r\n')
  233. def test_with_enable_smtputf8_true(self):
  234. server = DummyServer((socket_helper.HOST, 0), ('b', 0), enable_SMTPUTF8=True)
  235. conn, addr = server.accept()
  236. channel = smtpd.SMTPChannel(server, conn, addr, enable_SMTPUTF8=True)
  237. self.write_line(channel, b'EHLO example')
  238. self.write_line(
  239. channel,
  240. b'MAIL from: <foo@example.com> size=20 body=8bitmime smtputf8')
  241. self.assertEqual(channel.socket.last, b'250 OK\r\n')
  242. class SMTPDChannelTest(unittest.TestCase):
  243. def setUp(self):
  244. smtpd.socket = asyncore.socket = mock_socket
  245. self.old_debugstream = smtpd.DEBUGSTREAM
  246. self.debug = smtpd.DEBUGSTREAM = io.StringIO()
  247. self.server = DummyServer((socket_helper.HOST, 0), ('b', 0),
  248. decode_data=True)
  249. conn, addr = self.server.accept()
  250. self.channel = smtpd.SMTPChannel(self.server, conn, addr,
  251. decode_data=True)
  252. def tearDown(self):
  253. asyncore.close_all()
  254. asyncore.socket = smtpd.socket = socket
  255. smtpd.DEBUGSTREAM = self.old_debugstream
  256. def write_line(self, line):
  257. self.channel.socket.queue_recv(line)
  258. self.channel.handle_read()
  259. def test_broken_connect(self):
  260. self.assertRaises(
  261. DummyDispatcherBroken, BrokenDummyServer,
  262. (socket_helper.HOST, 0), ('b', 0), decode_data=True)
  263. def test_decode_data_and_enable_SMTPUTF8_raises(self):
  264. self.assertRaises(
  265. ValueError, smtpd.SMTPChannel,
  266. self.server, self.channel.conn, self.channel.addr,
  267. enable_SMTPUTF8=True, decode_data=True)
  268. def test_server_accept(self):
  269. self.server.handle_accept()
  270. def test_missing_data(self):
  271. self.write_line(b'')
  272. self.assertEqual(self.channel.socket.last,
  273. b'500 Error: bad syntax\r\n')
  274. def test_EHLO(self):
  275. self.write_line(b'EHLO example')
  276. self.assertEqual(self.channel.socket.last, b'250 HELP\r\n')
  277. def test_EHLO_bad_syntax(self):
  278. self.write_line(b'EHLO')
  279. self.assertEqual(self.channel.socket.last,
  280. b'501 Syntax: EHLO hostname\r\n')
  281. def test_EHLO_duplicate(self):
  282. self.write_line(b'EHLO example')
  283. self.write_line(b'EHLO example')
  284. self.assertEqual(self.channel.socket.last,
  285. b'503 Duplicate HELO/EHLO\r\n')
  286. def test_EHLO_HELO_duplicate(self):
  287. self.write_line(b'EHLO example')
  288. self.write_line(b'HELO example')
  289. self.assertEqual(self.channel.socket.last,
  290. b'503 Duplicate HELO/EHLO\r\n')
  291. def test_HELO(self):
  292. name = smtpd.socket.getfqdn()
  293. self.write_line(b'HELO example')
  294. self.assertEqual(self.channel.socket.last,
  295. '250 {}\r\n'.format(name).encode('ascii'))
  296. def test_HELO_EHLO_duplicate(self):
  297. self.write_line(b'HELO example')
  298. self.write_line(b'EHLO example')
  299. self.assertEqual(self.channel.socket.last,
  300. b'503 Duplicate HELO/EHLO\r\n')
  301. def test_HELP(self):
  302. self.write_line(b'HELP')
  303. self.assertEqual(self.channel.socket.last,
  304. b'250 Supported commands: EHLO HELO MAIL RCPT ' + \
  305. b'DATA RSET NOOP QUIT VRFY\r\n')
  306. def test_HELP_command(self):
  307. self.write_line(b'HELP MAIL')
  308. self.assertEqual(self.channel.socket.last,
  309. b'250 Syntax: MAIL FROM: <address>\r\n')
  310. def test_HELP_command_unknown(self):
  311. self.write_line(b'HELP SPAM')
  312. self.assertEqual(self.channel.socket.last,
  313. b'501 Supported commands: EHLO HELO MAIL RCPT ' + \
  314. b'DATA RSET NOOP QUIT VRFY\r\n')
  315. def test_HELO_bad_syntax(self):
  316. self.write_line(b'HELO')
  317. self.assertEqual(self.channel.socket.last,
  318. b'501 Syntax: HELO hostname\r\n')
  319. def test_HELO_duplicate(self):
  320. self.write_line(b'HELO example')
  321. self.write_line(b'HELO example')
  322. self.assertEqual(self.channel.socket.last,
  323. b'503 Duplicate HELO/EHLO\r\n')
  324. def test_HELO_parameter_rejected_when_extensions_not_enabled(self):
  325. self.extended_smtp = False
  326. self.write_line(b'HELO example')
  327. self.write_line(b'MAIL from:<foo@example.com> SIZE=1234')
  328. self.assertEqual(self.channel.socket.last,
  329. b'501 Syntax: MAIL FROM: <address>\r\n')
  330. def test_MAIL_allows_space_after_colon(self):
  331. self.write_line(b'HELO example')
  332. self.write_line(b'MAIL from: <foo@example.com>')
  333. self.assertEqual(self.channel.socket.last,
  334. b'250 OK\r\n')
  335. def test_extended_MAIL_allows_space_after_colon(self):
  336. self.write_line(b'EHLO example')
  337. self.write_line(b'MAIL from: <foo@example.com> size=20')
  338. self.assertEqual(self.channel.socket.last,
  339. b'250 OK\r\n')
  340. def test_NOOP(self):
  341. self.write_line(b'NOOP')
  342. self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
  343. def test_HELO_NOOP(self):
  344. self.write_line(b'HELO example')
  345. self.write_line(b'NOOP')
  346. self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
  347. def test_NOOP_bad_syntax(self):
  348. self.write_line(b'NOOP hi')
  349. self.assertEqual(self.channel.socket.last,
  350. b'501 Syntax: NOOP\r\n')
  351. def test_QUIT(self):
  352. self.write_line(b'QUIT')
  353. self.assertEqual(self.channel.socket.last, b'221 Bye\r\n')
  354. def test_HELO_QUIT(self):
  355. self.write_line(b'HELO example')
  356. self.write_line(b'QUIT')
  357. self.assertEqual(self.channel.socket.last, b'221 Bye\r\n')
  358. def test_QUIT_arg_ignored(self):
  359. self.write_line(b'QUIT bye bye')
  360. self.assertEqual(self.channel.socket.last, b'221 Bye\r\n')
  361. def test_bad_state(self):
  362. self.channel.smtp_state = 'BAD STATE'
  363. self.write_line(b'HELO example')
  364. self.assertEqual(self.channel.socket.last,
  365. b'451 Internal confusion\r\n')
  366. def test_command_too_long(self):
  367. self.write_line(b'HELO example')
  368. self.write_line(b'MAIL from: ' +
  369. b'a' * self.channel.command_size_limit +
  370. b'@example')
  371. self.assertEqual(self.channel.socket.last,
  372. b'500 Error: line too long\r\n')
  373. def test_MAIL_command_limit_extended_with_SIZE(self):
  374. self.write_line(b'EHLO example')
  375. fill_len = self.channel.command_size_limit - len('MAIL from:<@example>')
  376. self.write_line(b'MAIL from:<' +
  377. b'a' * fill_len +
  378. b'@example> SIZE=1234')
  379. self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
  380. self.write_line(b'MAIL from:<' +
  381. b'a' * (fill_len + 26) +
  382. b'@example> SIZE=1234')
  383. self.assertEqual(self.channel.socket.last,
  384. b'500 Error: line too long\r\n')
  385. def test_MAIL_command_rejects_SMTPUTF8_by_default(self):
  386. self.write_line(b'EHLO example')
  387. self.write_line(
  388. b'MAIL from: <naive@example.com> BODY=8BITMIME SMTPUTF8')
  389. self.assertEqual(self.channel.socket.last[0:1], b'5')
  390. def test_data_longer_than_default_data_size_limit(self):
  391. # Hack the default so we don't have to generate so much data.
  392. self.channel.data_size_limit = 1048
  393. self.write_line(b'HELO example')
  394. self.write_line(b'MAIL From:eggs@example')
  395. self.write_line(b'RCPT To:spam@example')
  396. self.write_line(b'DATA')
  397. self.write_line(b'A' * self.channel.data_size_limit +
  398. b'A\r\n.')
  399. self.assertEqual(self.channel.socket.last,
  400. b'552 Error: Too much mail data\r\n')
  401. def test_MAIL_size_parameter(self):
  402. self.write_line(b'EHLO example')
  403. self.write_line(b'MAIL FROM:<eggs@example> SIZE=512')
  404. self.assertEqual(self.channel.socket.last,
  405. b'250 OK\r\n')
  406. def test_MAIL_invalid_size_parameter(self):
  407. self.write_line(b'EHLO example')
  408. self.write_line(b'MAIL FROM:<eggs@example> SIZE=invalid')
  409. self.assertEqual(self.channel.socket.last,
  410. b'501 Syntax: MAIL FROM: <address> [SP <mail-parameters>]\r\n')
  411. def test_MAIL_RCPT_unknown_parameters(self):
  412. self.write_line(b'EHLO example')
  413. self.write_line(b'MAIL FROM:<eggs@example> ham=green')
  414. self.assertEqual(self.channel.socket.last,
  415. b'555 MAIL FROM parameters not recognized or not implemented\r\n')
  416. self.write_line(b'MAIL FROM:<eggs@example>')
  417. self.write_line(b'RCPT TO:<eggs@example> ham=green')
  418. self.assertEqual(self.channel.socket.last,
  419. b'555 RCPT TO parameters not recognized or not implemented\r\n')
  420. def test_MAIL_size_parameter_larger_than_default_data_size_limit(self):
  421. self.channel.data_size_limit = 1048
  422. self.write_line(b'EHLO example')
  423. self.write_line(b'MAIL FROM:<eggs@example> SIZE=2096')
  424. self.assertEqual(self.channel.socket.last,
  425. b'552 Error: message size exceeds fixed maximum message size\r\n')
  426. def test_need_MAIL(self):
  427. self.write_line(b'HELO example')
  428. self.write_line(b'RCPT to:spam@example')
  429. self.assertEqual(self.channel.socket.last,
  430. b'503 Error: need MAIL command\r\n')
  431. def test_MAIL_syntax_HELO(self):
  432. self.write_line(b'HELO example')
  433. self.write_line(b'MAIL from eggs@example')
  434. self.assertEqual(self.channel.socket.last,
  435. b'501 Syntax: MAIL FROM: <address>\r\n')
  436. def test_MAIL_syntax_EHLO(self):
  437. self.write_line(b'EHLO example')
  438. self.write_line(b'MAIL from eggs@example')
  439. self.assertEqual(self.channel.socket.last,
  440. b'501 Syntax: MAIL FROM: <address> [SP <mail-parameters>]\r\n')
  441. def test_MAIL_missing_address(self):
  442. self.write_line(b'HELO example')
  443. self.write_line(b'MAIL from:')
  444. self.assertEqual(self.channel.socket.last,
  445. b'501 Syntax: MAIL FROM: <address>\r\n')
  446. def test_MAIL_chevrons(self):
  447. self.write_line(b'HELO example')
  448. self.write_line(b'MAIL from:<eggs@example>')
  449. self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
  450. def test_MAIL_empty_chevrons(self):
  451. self.write_line(b'EHLO example')
  452. self.write_line(b'MAIL from:<>')
  453. self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
  454. def test_MAIL_quoted_localpart(self):
  455. self.write_line(b'EHLO example')
  456. self.write_line(b'MAIL from: <"Fred Blogs"@example.com>')
  457. self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
  458. self.assertEqual(self.channel.mailfrom, '"Fred Blogs"@example.com')
  459. def test_MAIL_quoted_localpart_no_angles(self):
  460. self.write_line(b'EHLO example')
  461. self.write_line(b'MAIL from: "Fred Blogs"@example.com')
  462. self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
  463. self.assertEqual(self.channel.mailfrom, '"Fred Blogs"@example.com')
  464. def test_MAIL_quoted_localpart_with_size(self):
  465. self.write_line(b'EHLO example')
  466. self.write_line(b'MAIL from: <"Fred Blogs"@example.com> SIZE=1000')
  467. self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
  468. self.assertEqual(self.channel.mailfrom, '"Fred Blogs"@example.com')
  469. def test_MAIL_quoted_localpart_with_size_no_angles(self):
  470. self.write_line(b'EHLO example')
  471. self.write_line(b'MAIL from: "Fred Blogs"@example.com SIZE=1000')
  472. self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
  473. self.assertEqual(self.channel.mailfrom, '"Fred Blogs"@example.com')
  474. def test_nested_MAIL(self):
  475. self.write_line(b'HELO example')
  476. self.write_line(b'MAIL from:eggs@example')
  477. self.write_line(b'MAIL from:spam@example')
  478. self.assertEqual(self.channel.socket.last,
  479. b'503 Error: nested MAIL command\r\n')
  480. def test_VRFY(self):
  481. self.write_line(b'VRFY eggs@example')
  482. self.assertEqual(self.channel.socket.last,
  483. b'252 Cannot VRFY user, but will accept message and attempt ' + \
  484. b'delivery\r\n')
  485. def test_VRFY_syntax(self):
  486. self.write_line(b'VRFY')
  487. self.assertEqual(self.channel.socket.last,
  488. b'501 Syntax: VRFY <address>\r\n')
  489. def test_EXPN_not_implemented(self):
  490. self.write_line(b'EXPN')
  491. self.assertEqual(self.channel.socket.last,
  492. b'502 EXPN not implemented\r\n')
  493. def test_no_HELO_MAIL(self):
  494. self.write_line(b'MAIL from:<foo@example.com>')
  495. self.assertEqual(self.channel.socket.last,
  496. b'503 Error: send HELO first\r\n')
  497. def test_need_RCPT(self):
  498. self.write_line(b'HELO example')
  499. self.write_line(b'MAIL From:eggs@example')
  500. self.write_line(b'DATA')
  501. self.assertEqual(self.channel.socket.last,
  502. b'503 Error: need RCPT command\r\n')
  503. def test_RCPT_syntax_HELO(self):
  504. self.write_line(b'HELO example')
  505. self.write_line(b'MAIL From: eggs@example')
  506. self.write_line(b'RCPT to eggs@example')
  507. self.assertEqual(self.channel.socket.last,
  508. b'501 Syntax: RCPT TO: <address>\r\n')
  509. def test_RCPT_syntax_EHLO(self):
  510. self.write_line(b'EHLO example')
  511. self.write_line(b'MAIL From: eggs@example')
  512. self.write_line(b'RCPT to eggs@example')
  513. self.assertEqual(self.channel.socket.last,
  514. b'501 Syntax: RCPT TO: <address> [SP <mail-parameters>]\r\n')
  515. def test_RCPT_lowercase_to_OK(self):
  516. self.write_line(b'HELO example')
  517. self.write_line(b'MAIL From: eggs@example')
  518. self.write_line(b'RCPT to: <eggs@example>')
  519. self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
  520. def test_no_HELO_RCPT(self):
  521. self.write_line(b'RCPT to eggs@example')
  522. self.assertEqual(self.channel.socket.last,
  523. b'503 Error: send HELO first\r\n')
  524. def test_data_dialog(self):
  525. self.write_line(b'HELO example')
  526. self.write_line(b'MAIL From:eggs@example')
  527. self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
  528. self.write_line(b'RCPT To:spam@example')
  529. self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
  530. self.write_line(b'DATA')
  531. self.assertEqual(self.channel.socket.last,
  532. b'354 End data with <CR><LF>.<CR><LF>\r\n')
  533. self.write_line(b'data\r\nmore\r\n.')
  534. self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
  535. self.assertEqual(self.server.messages,
  536. [(('peer-address', 'peer-port'),
  537. 'eggs@example',
  538. ['spam@example'],
  539. 'data\nmore')])
  540. def test_DATA_syntax(self):
  541. self.write_line(b'HELO example')
  542. self.write_line(b'MAIL From:eggs@example')
  543. self.write_line(b'RCPT To:spam@example')
  544. self.write_line(b'DATA spam')
  545. self.assertEqual(self.channel.socket.last, b'501 Syntax: DATA\r\n')
  546. def test_no_HELO_DATA(self):
  547. self.write_line(b'DATA spam')
  548. self.assertEqual(self.channel.socket.last,
  549. b'503 Error: send HELO first\r\n')
  550. def test_data_transparency_section_4_5_2(self):
  551. self.write_line(b'HELO example')
  552. self.write_line(b'MAIL From:eggs@example')
  553. self.write_line(b'RCPT To:spam@example')
  554. self.write_line(b'DATA')
  555. self.write_line(b'..\r\n.\r\n')
  556. self.assertEqual(self.channel.received_data, '.')
  557. def test_multiple_RCPT(self):
  558. self.write_line(b'HELO example')
  559. self.write_line(b'MAIL From:eggs@example')
  560. self.write_line(b'RCPT To:spam@example')
  561. self.write_line(b'RCPT To:ham@example')
  562. self.write_line(b'DATA')
  563. self.write_line(b'data\r\n.')
  564. self.assertEqual(self.server.messages,
  565. [(('peer-address', 'peer-port'),
  566. 'eggs@example',
  567. ['spam@example','ham@example'],
  568. 'data')])
  569. def test_manual_status(self):
  570. # checks that the Channel is able to return a custom status message
  571. self.write_line(b'HELO example')
  572. self.write_line(b'MAIL From:eggs@example')
  573. self.write_line(b'RCPT To:spam@example')
  574. self.write_line(b'DATA')
  575. self.write_line(b'return status\r\n.')
  576. self.assertEqual(self.channel.socket.last, b'250 Okish\r\n')
  577. def test_RSET(self):
  578. self.write_line(b'HELO example')
  579. self.write_line(b'MAIL From:eggs@example')
  580. self.write_line(b'RCPT To:spam@example')
  581. self.write_line(b'RSET')
  582. self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
  583. self.write_line(b'MAIL From:foo@example')
  584. self.write_line(b'RCPT To:eggs@example')
  585. self.write_line(b'DATA')
  586. self.write_line(b'data\r\n.')
  587. self.assertEqual(self.server.messages,
  588. [(('peer-address', 'peer-port'),
  589. 'foo@example',
  590. ['eggs@example'],
  591. 'data')])
  592. def test_HELO_RSET(self):
  593. self.write_line(b'HELO example')
  594. self.write_line(b'RSET')
  595. self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
  596. def test_RSET_syntax(self):
  597. self.write_line(b'RSET hi')
  598. self.assertEqual(self.channel.socket.last, b'501 Syntax: RSET\r\n')
  599. def test_unknown_command(self):
  600. self.write_line(b'UNKNOWN_CMD')
  601. self.assertEqual(self.channel.socket.last,
  602. b'500 Error: command "UNKNOWN_CMD" not ' + \
  603. b'recognized\r\n')
  604. def test_attribute_deprecations(self):
  605. with warnings_helper.check_warnings(('', DeprecationWarning)):
  606. spam = self.channel._SMTPChannel__server
  607. with warnings_helper.check_warnings(('', DeprecationWarning)):
  608. self.channel._SMTPChannel__server = 'spam'
  609. with warnings_helper.check_warnings(('', DeprecationWarning)):
  610. spam = self.channel._SMTPChannel__line
  611. with warnings_helper.check_warnings(('', DeprecationWarning)):
  612. self.channel._SMTPChannel__line = 'spam'
  613. with warnings_helper.check_warnings(('', DeprecationWarning)):
  614. spam = self.channel._SMTPChannel__state
  615. with warnings_helper.check_warnings(('', DeprecationWarning)):
  616. self.channel._SMTPChannel__state = 'spam'
  617. with warnings_helper.check_warnings(('', DeprecationWarning)):
  618. spam = self.channel._SMTPChannel__greeting
  619. with warnings_helper.check_warnings(('', DeprecationWarning)):
  620. self.channel._SMTPChannel__greeting = 'spam'
  621. with warnings_helper.check_warnings(('', DeprecationWarning)):
  622. spam = self.channel._SMTPChannel__mailfrom
  623. with warnings_helper.check_warnings(('', DeprecationWarning)):
  624. self.channel._SMTPChannel__mailfrom = 'spam'
  625. with warnings_helper.check_warnings(('', DeprecationWarning)):
  626. spam = self.channel._SMTPChannel__rcpttos
  627. with warnings_helper.check_warnings(('', DeprecationWarning)):
  628. self.channel._SMTPChannel__rcpttos = 'spam'
  629. with warnings_helper.check_warnings(('', DeprecationWarning)):
  630. spam = self.channel._SMTPChannel__data
  631. with warnings_helper.check_warnings(('', DeprecationWarning)):
  632. self.channel._SMTPChannel__data = 'spam'
  633. with warnings_helper.check_warnings(('', DeprecationWarning)):
  634. spam = self.channel._SMTPChannel__fqdn
  635. with warnings_helper.check_warnings(('', DeprecationWarning)):
  636. self.channel._SMTPChannel__fqdn = 'spam'
  637. with warnings_helper.check_warnings(('', DeprecationWarning)):
  638. spam = self.channel._SMTPChannel__peer
  639. with warnings_helper.check_warnings(('', DeprecationWarning)):
  640. self.channel._SMTPChannel__peer = 'spam'
  641. with warnings_helper.check_warnings(('', DeprecationWarning)):
  642. spam = self.channel._SMTPChannel__conn
  643. with warnings_helper.check_warnings(('', DeprecationWarning)):
  644. self.channel._SMTPChannel__conn = 'spam'
  645. with warnings_helper.check_warnings(('', DeprecationWarning)):
  646. spam = self.channel._SMTPChannel__addr
  647. with warnings_helper.check_warnings(('', DeprecationWarning)):
  648. self.channel._SMTPChannel__addr = 'spam'
  649. @unittest.skipUnless(socket_helper.IPV6_ENABLED, "IPv6 not enabled")
  650. class SMTPDChannelIPv6Test(SMTPDChannelTest):
  651. def setUp(self):
  652. smtpd.socket = asyncore.socket = mock_socket
  653. self.old_debugstream = smtpd.DEBUGSTREAM
  654. self.debug = smtpd.DEBUGSTREAM = io.StringIO()
  655. self.server = DummyServer((socket_helper.HOSTv6, 0), ('b', 0),
  656. decode_data=True)
  657. conn, addr = self.server.accept()
  658. self.channel = smtpd.SMTPChannel(self.server, conn, addr,
  659. decode_data=True)
  660. class SMTPDChannelWithDataSizeLimitTest(unittest.TestCase):
  661. def setUp(self):
  662. smtpd.socket = asyncore.socket = mock_socket
  663. self.old_debugstream = smtpd.DEBUGSTREAM
  664. self.debug = smtpd.DEBUGSTREAM = io.StringIO()
  665. self.server = DummyServer((socket_helper.HOST, 0), ('b', 0),
  666. decode_data=True)
  667. conn, addr = self.server.accept()
  668. # Set DATA size limit to 32 bytes for easy testing
  669. self.channel = smtpd.SMTPChannel(self.server, conn, addr, 32,
  670. decode_data=True)
  671. def tearDown(self):
  672. asyncore.close_all()
  673. asyncore.socket = smtpd.socket = socket
  674. smtpd.DEBUGSTREAM = self.old_debugstream
  675. def write_line(self, line):
  676. self.channel.socket.queue_recv(line)
  677. self.channel.handle_read()
  678. def test_data_limit_dialog(self):
  679. self.write_line(b'HELO example')
  680. self.write_line(b'MAIL From:eggs@example')
  681. self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
  682. self.write_line(b'RCPT To:spam@example')
  683. self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
  684. self.write_line(b'DATA')
  685. self.assertEqual(self.channel.socket.last,
  686. b'354 End data with <CR><LF>.<CR><LF>\r\n')
  687. self.write_line(b'data\r\nmore\r\n.')
  688. self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
  689. self.assertEqual(self.server.messages,
  690. [(('peer-address', 'peer-port'),
  691. 'eggs@example',
  692. ['spam@example'],
  693. 'data\nmore')])
  694. def test_data_limit_dialog_too_much_data(self):
  695. self.write_line(b'HELO example')
  696. self.write_line(b'MAIL From:eggs@example')
  697. self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
  698. self.write_line(b'RCPT To:spam@example')
  699. self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
  700. self.write_line(b'DATA')
  701. self.assertEqual(self.channel.socket.last,
  702. b'354 End data with <CR><LF>.<CR><LF>\r\n')
  703. self.write_line(b'This message is longer than 32 bytes\r\n.')
  704. self.assertEqual(self.channel.socket.last,
  705. b'552 Error: Too much mail data\r\n')
  706. class SMTPDChannelWithDecodeDataFalse(unittest.TestCase):
  707. def setUp(self):
  708. smtpd.socket = asyncore.socket = mock_socket
  709. self.old_debugstream = smtpd.DEBUGSTREAM
  710. self.debug = smtpd.DEBUGSTREAM = io.StringIO()
  711. self.server = DummyServer((socket_helper.HOST, 0), ('b', 0))
  712. conn, addr = self.server.accept()
  713. self.channel = smtpd.SMTPChannel(self.server, conn, addr)
  714. def tearDown(self):
  715. asyncore.close_all()
  716. asyncore.socket = smtpd.socket = socket
  717. smtpd.DEBUGSTREAM = self.old_debugstream
  718. def write_line(self, line):
  719. self.channel.socket.queue_recv(line)
  720. self.channel.handle_read()
  721. def test_ascii_data(self):
  722. self.write_line(b'HELO example')
  723. self.write_line(b'MAIL From:eggs@example')
  724. self.write_line(b'RCPT To:spam@example')
  725. self.write_line(b'DATA')
  726. self.write_line(b'plain ascii text')
  727. self.write_line(b'.')
  728. self.assertEqual(self.channel.received_data, b'plain ascii text')
  729. def test_utf8_data(self):
  730. self.write_line(b'HELO example')
  731. self.write_line(b'MAIL From:eggs@example')
  732. self.write_line(b'RCPT To:spam@example')
  733. self.write_line(b'DATA')
  734. self.write_line(b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87')
  735. self.write_line(b'and some plain ascii')
  736. self.write_line(b'.')
  737. self.assertEqual(
  738. self.channel.received_data,
  739. b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87\n'
  740. b'and some plain ascii')
  741. class SMTPDChannelWithDecodeDataTrue(unittest.TestCase):
  742. def setUp(self):
  743. smtpd.socket = asyncore.socket = mock_socket
  744. self.old_debugstream = smtpd.DEBUGSTREAM
  745. self.debug = smtpd.DEBUGSTREAM = io.StringIO()
  746. self.server = DummyServer((socket_helper.HOST, 0), ('b', 0),
  747. decode_data=True)
  748. conn, addr = self.server.accept()
  749. # Set decode_data to True
  750. self.channel = smtpd.SMTPChannel(self.server, conn, addr,
  751. decode_data=True)
  752. def tearDown(self):
  753. asyncore.close_all()
  754. asyncore.socket = smtpd.socket = socket
  755. smtpd.DEBUGSTREAM = self.old_debugstream
  756. def write_line(self, line):
  757. self.channel.socket.queue_recv(line)
  758. self.channel.handle_read()
  759. def test_ascii_data(self):
  760. self.write_line(b'HELO example')
  761. self.write_line(b'MAIL From:eggs@example')
  762. self.write_line(b'RCPT To:spam@example')
  763. self.write_line(b'DATA')
  764. self.write_line(b'plain ascii text')
  765. self.write_line(b'.')
  766. self.assertEqual(self.channel.received_data, 'plain ascii text')
  767. def test_utf8_data(self):
  768. self.write_line(b'HELO example')
  769. self.write_line(b'MAIL From:eggs@example')
  770. self.write_line(b'RCPT To:spam@example')
  771. self.write_line(b'DATA')
  772. self.write_line(b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87')
  773. self.write_line(b'and some plain ascii')
  774. self.write_line(b'.')
  775. self.assertEqual(
  776. self.channel.received_data,
  777. 'utf8 enriched text: żźć\nand some plain ascii')
  778. class SMTPDChannelTestWithEnableSMTPUTF8True(unittest.TestCase):
  779. def setUp(self):
  780. smtpd.socket = asyncore.socket = mock_socket
  781. self.old_debugstream = smtpd.DEBUGSTREAM
  782. self.debug = smtpd.DEBUGSTREAM = io.StringIO()
  783. self.server = DummyServer((socket_helper.HOST, 0), ('b', 0),
  784. enable_SMTPUTF8=True)
  785. conn, addr = self.server.accept()
  786. self.channel = smtpd.SMTPChannel(self.server, conn, addr,
  787. enable_SMTPUTF8=True)
  788. def tearDown(self):
  789. asyncore.close_all()
  790. asyncore.socket = smtpd.socket = socket
  791. smtpd.DEBUGSTREAM = self.old_debugstream
  792. def write_line(self, line):
  793. self.channel.socket.queue_recv(line)
  794. self.channel.handle_read()
  795. def test_MAIL_command_accepts_SMTPUTF8_when_announced(self):
  796. self.write_line(b'EHLO example')
  797. self.write_line(
  798. 'MAIL from: <naïve@example.com> BODY=8BITMIME SMTPUTF8'.encode(
  799. 'utf-8')
  800. )
  801. self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
  802. def test_process_smtputf8_message(self):
  803. self.write_line(b'EHLO example')
  804. for mail_parameters in [b'', b'BODY=8BITMIME SMTPUTF8']:
  805. self.write_line(b'MAIL from: <a@example> ' + mail_parameters)
  806. self.assertEqual(self.channel.socket.last[0:3], b'250')
  807. self.write_line(b'rcpt to:<b@example.com>')
  808. self.assertEqual(self.channel.socket.last[0:3], b'250')
  809. self.write_line(b'data')
  810. self.assertEqual(self.channel.socket.last[0:3], b'354')
  811. self.write_line(b'c\r\n.')
  812. if mail_parameters == b'':
  813. self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
  814. else:
  815. self.assertEqual(self.channel.socket.last,
  816. b'250 SMTPUTF8 message okish\r\n')
  817. def test_utf8_data(self):
  818. self.write_line(b'EHLO example')
  819. self.write_line(
  820. 'MAIL From: naïve@examplé BODY=8BITMIME SMTPUTF8'.encode('utf-8'))
  821. self.assertEqual(self.channel.socket.last[0:3], b'250')
  822. self.write_line('RCPT To:späm@examplé'.encode('utf-8'))
  823. self.assertEqual(self.channel.socket.last[0:3], b'250')
  824. self.write_line(b'DATA')
  825. self.assertEqual(self.channel.socket.last[0:3], b'354')
  826. self.write_line(b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87')
  827. self.write_line(b'.')
  828. self.assertEqual(
  829. self.channel.received_data,
  830. b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87')
  831. def test_MAIL_command_limit_extended_with_SIZE_and_SMTPUTF8(self):
  832. self.write_line(b'ehlo example')
  833. fill_len = (512 + 26 + 10) - len('mail from:<@example>')
  834. self.write_line(b'MAIL from:<' +
  835. b'a' * (fill_len + 1) +
  836. b'@example>')
  837. self.assertEqual(self.channel.socket.last,
  838. b'500 Error: line too long\r\n')
  839. self.write_line(b'MAIL from:<' +
  840. b'a' * fill_len +
  841. b'@example>')
  842. self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
  843. def test_multiple_emails_with_extended_command_length(self):
  844. self.write_line(b'ehlo example')
  845. fill_len = (512 + 26 + 10) - len('mail from:<@example>')
  846. for char in [b'a', b'b', b'c']:
  847. self.write_line(b'MAIL from:<' + char * fill_len + b'a@example>')
  848. self.assertEqual(self.channel.socket.last[0:3], b'500')
  849. self.write_line(b'MAIL from:<' + char * fill_len + b'@example>')
  850. self.assertEqual(self.channel.socket.last[0:3], b'250')
  851. self.write_line(b'rcpt to:<hans@example.com>')
  852. self.assertEqual(self.channel.socket.last[0:3], b'250')
  853. self.write_line(b'data')
  854. self.assertEqual(self.channel.socket.last[0:3], b'354')
  855. self.write_line(b'test\r\n.')
  856. self.assertEqual(self.channel.socket.last[0:3], b'250')
  857. class MiscTestCase(unittest.TestCase):
  858. def test__all__(self):
  859. not_exported = {
  860. "program", "Devnull", "DEBUGSTREAM", "NEWLINE", "COMMASPACE",
  861. "DATA_SIZE_DEFAULT", "usage", "Options", "parseargs",
  862. }
  863. support.check__all__(self, smtpd, not_exported=not_exported)
  864. if __name__ == "__main__":
  865. unittest.main()