2012-05-26 15:33:59 -03:00
|
|
|
import unittest
|
2014-08-09 17:40:49 -03:00
|
|
|
import textwrap
|
2010-08-03 03:39:33 -03:00
|
|
|
from test import support, mock_socket
|
2010-07-23 13:20:40 -03:00
|
|
|
import socket
|
|
|
|
import io
|
|
|
|
import smtpd
|
2010-08-03 03:39:33 -03:00
|
|
|
import asyncore
|
2010-07-23 13:20:40 -03:00
|
|
|
|
|
|
|
|
|
|
|
class DummyServer(smtpd.SMTPServer):
|
2014-08-09 17:40:49 -03:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
smtpd.SMTPServer.__init__(self, *args, **kwargs)
|
2010-07-29 10:19:42 -03:00
|
|
|
self.messages = []
|
2014-08-09 17:40:49 -03:00
|
|
|
if self._decode_data:
|
2014-06-11 12:18:08 -03:00
|
|
|
self.return_status = 'return status'
|
|
|
|
else:
|
|
|
|
self.return_status = b'return status'
|
2010-08-03 03:39:33 -03:00
|
|
|
|
2015-05-11 13:11:40 -03:00
|
|
|
def process_message(self, peer, mailfrom, rcpttos, data, **kw):
|
2010-07-23 13:20:40 -03:00
|
|
|
self.messages.append((peer, mailfrom, rcpttos, data))
|
2014-06-11 12:18:08 -03:00
|
|
|
if data == self.return_status:
|
2010-07-23 13:20:40 -03:00
|
|
|
return '250 Okish'
|
2015-05-11 13:11:40 -03:00
|
|
|
if 'mail_options' in kw and 'SMTPUTF8' in kw['mail_options']:
|
|
|
|
return '250 SMTPUTF8 message okish'
|
2014-08-09 17:40:49 -03:00
|
|
|
|
2010-08-03 22:20:14 -03:00
|
|
|
|
2010-07-23 13:20:40 -03:00
|
|
|
class DummyDispatcherBroken(Exception):
|
|
|
|
pass
|
|
|
|
|
2010-08-03 22:20:14 -03:00
|
|
|
|
2010-07-23 13:20:40 -03:00
|
|
|
class BrokenDummyServer(DummyServer):
|
|
|
|
def listen(self, num):
|
|
|
|
raise DummyDispatcherBroken()
|
|
|
|
|
2010-08-03 22:20:14 -03:00
|
|
|
|
2012-05-26 15:33:59 -03:00
|
|
|
class SMTPDServerTest(unittest.TestCase):
|
2010-08-03 22:20:14 -03:00
|
|
|
def setUp(self):
|
|
|
|
smtpd.socket = asyncore.socket = mock_socket
|
|
|
|
|
|
|
|
def test_process_message_unimplemented(self):
|
2014-06-11 14:48:58 -03:00
|
|
|
server = smtpd.SMTPServer((support.HOST, 0), ('b', 0),
|
|
|
|
decode_data=True)
|
2010-08-03 22:20:14 -03:00
|
|
|
conn, addr = server.accept()
|
2014-06-11 12:18:08 -03:00
|
|
|
channel = smtpd.SMTPChannel(server, conn, addr, decode_data=True)
|
2010-08-03 22:20:14 -03:00
|
|
|
|
|
|
|
def write_line(line):
|
|
|
|
channel.socket.queue_recv(line)
|
|
|
|
channel.handle_read()
|
|
|
|
|
2012-05-26 15:33:59 -03:00
|
|
|
write_line(b'HELO example')
|
2010-08-03 22:20:14 -03:00
|
|
|
write_line(b'MAIL From:eggs@example')
|
|
|
|
write_line(b'RCPT To:spam@example')
|
|
|
|
write_line(b'DATA')
|
|
|
|
self.assertRaises(NotImplementedError, write_line, b'spam\r\n.\r\n')
|
|
|
|
|
2014-08-09 17:40:49 -03:00
|
|
|
def test_decode_data_and_enable_SMTPUTF8_raises(self):
|
|
|
|
self.assertRaises(
|
|
|
|
ValueError,
|
|
|
|
smtpd.SMTPServer,
|
|
|
|
(support.HOST, 0),
|
|
|
|
('b', 0),
|
|
|
|
enable_SMTPUTF8=True,
|
|
|
|
decode_data=True)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
asyncore.close_all()
|
|
|
|
asyncore.socket = smtpd.socket = socket
|
|
|
|
|
|
|
|
|
|
|
|
class DebuggingServerTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
smtpd.socket = asyncore.socket = mock_socket
|
|
|
|
|
|
|
|
def send_data(self, channel, data, enable_SMTPUTF8=False):
|
|
|
|
def write_line(line):
|
|
|
|
channel.socket.queue_recv(line)
|
|
|
|
channel.handle_read()
|
|
|
|
write_line(b'EHLO example')
|
|
|
|
if enable_SMTPUTF8:
|
|
|
|
write_line(b'MAIL From:eggs@example BODY=8BITMIME SMTPUTF8')
|
|
|
|
else:
|
|
|
|
write_line(b'MAIL From:eggs@example')
|
|
|
|
write_line(b'RCPT To:spam@example')
|
|
|
|
write_line(b'DATA')
|
|
|
|
write_line(data)
|
|
|
|
write_line(b'.')
|
|
|
|
|
|
|
|
def test_process_message_with_decode_data_true(self):
|
|
|
|
server = smtpd.DebuggingServer((support.HOST, 0), ('b', 0),
|
|
|
|
decode_data=True)
|
|
|
|
conn, addr = server.accept()
|
|
|
|
channel = smtpd.SMTPChannel(server, conn, addr, decode_data=True)
|
|
|
|
with support.captured_stdout() as s:
|
|
|
|
self.send_data(channel, b'From: test\n\nhello\n')
|
|
|
|
stdout = s.getvalue()
|
|
|
|
self.assertEqual(stdout, textwrap.dedent("""\
|
|
|
|
---------- MESSAGE FOLLOWS ----------
|
|
|
|
From: test
|
|
|
|
X-Peer: peer-address
|
|
|
|
|
|
|
|
hello
|
|
|
|
------------ END MESSAGE ------------
|
|
|
|
"""))
|
|
|
|
|
|
|
|
def test_process_message_with_decode_data_false(self):
|
2016-05-16 03:36:31 -03:00
|
|
|
server = smtpd.DebuggingServer((support.HOST, 0), ('b', 0))
|
2014-08-09 17:40:49 -03:00
|
|
|
conn, addr = server.accept()
|
2016-05-16 03:36:31 -03:00
|
|
|
channel = smtpd.SMTPChannel(server, conn, addr)
|
2014-08-09 17:40:49 -03:00
|
|
|
with support.captured_stdout() as s:
|
|
|
|
self.send_data(channel, b'From: test\n\nh\xc3\xa9llo\xff\n')
|
|
|
|
stdout = s.getvalue()
|
|
|
|
self.assertEqual(stdout, textwrap.dedent("""\
|
|
|
|
---------- MESSAGE FOLLOWS ----------
|
|
|
|
b'From: test'
|
|
|
|
b'X-Peer: peer-address'
|
|
|
|
b''
|
|
|
|
b'h\\xc3\\xa9llo\\xff'
|
|
|
|
------------ END MESSAGE ------------
|
|
|
|
"""))
|
|
|
|
|
|
|
|
def test_process_message_with_enable_SMTPUTF8_true(self):
|
|
|
|
server = smtpd.DebuggingServer((support.HOST, 0), ('b', 0),
|
|
|
|
enable_SMTPUTF8=True)
|
|
|
|
conn, addr = server.accept()
|
|
|
|
channel = smtpd.SMTPChannel(server, conn, addr, enable_SMTPUTF8=True)
|
|
|
|
with support.captured_stdout() as s:
|
|
|
|
self.send_data(channel, b'From: test\n\nh\xc3\xa9llo\xff\n')
|
|
|
|
stdout = s.getvalue()
|
|
|
|
self.assertEqual(stdout, textwrap.dedent("""\
|
|
|
|
---------- MESSAGE FOLLOWS ----------
|
|
|
|
b'From: test'
|
|
|
|
b'X-Peer: peer-address'
|
|
|
|
b''
|
|
|
|
b'h\\xc3\\xa9llo\\xff'
|
|
|
|
------------ END MESSAGE ------------
|
|
|
|
"""))
|
|
|
|
|
|
|
|
def test_process_SMTPUTF8_message_with_enable_SMTPUTF8_true(self):
|
|
|
|
server = smtpd.DebuggingServer((support.HOST, 0), ('b', 0),
|
|
|
|
enable_SMTPUTF8=True)
|
|
|
|
conn, addr = server.accept()
|
|
|
|
channel = smtpd.SMTPChannel(server, conn, addr, enable_SMTPUTF8=True)
|
|
|
|
with support.captured_stdout() as s:
|
|
|
|
self.send_data(channel, b'From: test\n\nh\xc3\xa9llo\xff\n',
|
|
|
|
enable_SMTPUTF8=True)
|
|
|
|
stdout = s.getvalue()
|
|
|
|
self.assertEqual(stdout, textwrap.dedent("""\
|
2015-05-11 13:11:40 -03:00
|
|
|
---------- MESSAGE FOLLOWS ----------
|
|
|
|
mail options: ['BODY=8BITMIME', 'SMTPUTF8']
|
2014-08-09 17:40:49 -03:00
|
|
|
b'From: test'
|
|
|
|
b'X-Peer: peer-address'
|
|
|
|
b''
|
|
|
|
b'h\\xc3\\xa9llo\\xff'
|
|
|
|
------------ END MESSAGE ------------
|
|
|
|
"""))
|
|
|
|
|
2010-08-03 22:20:14 -03:00
|
|
|
def tearDown(self):
|
2010-08-15 22:48:14 -03:00
|
|
|
asyncore.close_all()
|
2010-08-03 22:20:14 -03:00
|
|
|
asyncore.socket = smtpd.socket = socket
|
|
|
|
|
|
|
|
|
2014-06-11 14:48:58 -03:00
|
|
|
class TestFamilyDetection(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
smtpd.socket = asyncore.socket = mock_socket
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
asyncore.close_all()
|
|
|
|
asyncore.socket = smtpd.socket = socket
|
|
|
|
|
|
|
|
@unittest.skipUnless(support.IPV6_ENABLED, "IPv6 not enabled")
|
|
|
|
def test_socket_uses_IPv6(self):
|
2016-05-16 03:36:31 -03:00
|
|
|
server = smtpd.SMTPServer((support.HOSTv6, 0), (support.HOST, 0))
|
2014-06-11 14:48:58 -03:00
|
|
|
self.assertEqual(server.socket.family, socket.AF_INET6)
|
|
|
|
|
|
|
|
def test_socket_uses_IPv4(self):
|
2016-05-16 03:36:31 -03:00
|
|
|
server = smtpd.SMTPServer((support.HOST, 0), (support.HOSTv6, 0))
|
2014-06-11 14:48:58 -03:00
|
|
|
self.assertEqual(server.socket.family, socket.AF_INET)
|
|
|
|
|
|
|
|
|
2015-05-11 13:11:40 -03:00
|
|
|
class TestRcptOptionParsing(unittest.TestCase):
|
|
|
|
error_response = (b'555 RCPT TO parameters not recognized or not '
|
|
|
|
b'implemented\r\n')
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
smtpd.socket = asyncore.socket = mock_socket
|
|
|
|
self.old_debugstream = smtpd.DEBUGSTREAM
|
|
|
|
self.debug = smtpd.DEBUGSTREAM = io.StringIO()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
asyncore.close_all()
|
|
|
|
asyncore.socket = smtpd.socket = socket
|
|
|
|
smtpd.DEBUGSTREAM = self.old_debugstream
|
|
|
|
|
|
|
|
def write_line(self, channel, line):
|
|
|
|
channel.socket.queue_recv(line)
|
|
|
|
channel.handle_read()
|
|
|
|
|
|
|
|
def test_params_rejected(self):
|
2016-05-16 03:36:31 -03:00
|
|
|
server = DummyServer((support.HOST, 0), ('b', 0))
|
2015-05-11 13:11:40 -03:00
|
|
|
conn, addr = server.accept()
|
2016-05-16 03:36:31 -03:00
|
|
|
channel = smtpd.SMTPChannel(server, conn, addr)
|
2015-05-11 13:11:40 -03:00
|
|
|
self.write_line(channel, b'EHLO example')
|
|
|
|
self.write_line(channel, b'MAIL from: <foo@example.com> size=20')
|
|
|
|
self.write_line(channel, b'RCPT to: <foo@example.com> foo=bar')
|
|
|
|
self.assertEqual(channel.socket.last, self.error_response)
|
|
|
|
|
|
|
|
def test_nothing_accepted(self):
|
2016-05-16 03:36:31 -03:00
|
|
|
server = DummyServer((support.HOST, 0), ('b', 0))
|
2015-05-11 13:11:40 -03:00
|
|
|
conn, addr = server.accept()
|
2016-05-16 03:36:31 -03:00
|
|
|
channel = smtpd.SMTPChannel(server, conn, addr)
|
2015-05-11 13:11:40 -03:00
|
|
|
self.write_line(channel, b'EHLO example')
|
|
|
|
self.write_line(channel, b'MAIL from: <foo@example.com> size=20')
|
|
|
|
self.write_line(channel, b'RCPT to: <foo@example.com>')
|
|
|
|
self.assertEqual(channel.socket.last, b'250 OK\r\n')
|
|
|
|
|
|
|
|
|
|
|
|
class TestMailOptionParsing(unittest.TestCase):
|
|
|
|
error_response = (b'555 MAIL FROM parameters not recognized or not '
|
|
|
|
b'implemented\r\n')
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
smtpd.socket = asyncore.socket = mock_socket
|
|
|
|
self.old_debugstream = smtpd.DEBUGSTREAM
|
|
|
|
self.debug = smtpd.DEBUGSTREAM = io.StringIO()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
asyncore.close_all()
|
|
|
|
asyncore.socket = smtpd.socket = socket
|
|
|
|
smtpd.DEBUGSTREAM = self.old_debugstream
|
|
|
|
|
|
|
|
def write_line(self, channel, line):
|
|
|
|
channel.socket.queue_recv(line)
|
|
|
|
channel.handle_read()
|
|
|
|
|
|
|
|
def test_with_decode_data_true(self):
|
|
|
|
server = DummyServer((support.HOST, 0), ('b', 0), decode_data=True)
|
|
|
|
conn, addr = server.accept()
|
|
|
|
channel = smtpd.SMTPChannel(server, conn, addr, decode_data=True)
|
|
|
|
self.write_line(channel, b'EHLO example')
|
|
|
|
for line in [
|
|
|
|
b'MAIL from: <foo@example.com> size=20 SMTPUTF8',
|
|
|
|
b'MAIL from: <foo@example.com> size=20 SMTPUTF8 BODY=8BITMIME',
|
|
|
|
b'MAIL from: <foo@example.com> size=20 BODY=UNKNOWN',
|
|
|
|
b'MAIL from: <foo@example.com> size=20 body=8bitmime',
|
|
|
|
]:
|
|
|
|
self.write_line(channel, line)
|
|
|
|
self.assertEqual(channel.socket.last, self.error_response)
|
|
|
|
self.write_line(channel, b'MAIL from: <foo@example.com> size=20')
|
|
|
|
self.assertEqual(channel.socket.last, b'250 OK\r\n')
|
|
|
|
|
|
|
|
def test_with_decode_data_false(self):
|
2016-05-16 03:36:31 -03:00
|
|
|
server = DummyServer((support.HOST, 0), ('b', 0))
|
2015-05-11 13:11:40 -03:00
|
|
|
conn, addr = server.accept()
|
2016-05-16 03:36:31 -03:00
|
|
|
channel = smtpd.SMTPChannel(server, conn, addr)
|
2015-05-11 13:11:40 -03:00
|
|
|
self.write_line(channel, b'EHLO example')
|
|
|
|
for line in [
|
|
|
|
b'MAIL from: <foo@example.com> size=20 SMTPUTF8',
|
|
|
|
b'MAIL from: <foo@example.com> size=20 SMTPUTF8 BODY=8BITMIME',
|
|
|
|
]:
|
|
|
|
self.write_line(channel, line)
|
|
|
|
self.assertEqual(channel.socket.last, self.error_response)
|
|
|
|
self.write_line(
|
|
|
|
channel,
|
|
|
|
b'MAIL from: <foo@example.com> size=20 SMTPUTF8 BODY=UNKNOWN')
|
|
|
|
self.assertEqual(
|
|
|
|
channel.socket.last,
|
|
|
|
b'501 Error: BODY can only be one of 7BIT, 8BITMIME\r\n')
|
|
|
|
self.write_line(
|
|
|
|
channel, b'MAIL from: <foo@example.com> size=20 body=8bitmime')
|
|
|
|
self.assertEqual(channel.socket.last, b'250 OK\r\n')
|
|
|
|
|
|
|
|
def test_with_enable_smtputf8_true(self):
|
|
|
|
server = DummyServer((support.HOST, 0), ('b', 0), enable_SMTPUTF8=True)
|
|
|
|
conn, addr = server.accept()
|
|
|
|
channel = smtpd.SMTPChannel(server, conn, addr, enable_SMTPUTF8=True)
|
|
|
|
self.write_line(channel, b'EHLO example')
|
|
|
|
self.write_line(
|
|
|
|
channel,
|
|
|
|
b'MAIL from: <foo@example.com> size=20 body=8bitmime smtputf8')
|
|
|
|
self.assertEqual(channel.socket.last, b'250 OK\r\n')
|
|
|
|
|
|
|
|
|
2012-05-26 15:33:59 -03:00
|
|
|
class SMTPDChannelTest(unittest.TestCase):
|
2010-07-23 13:20:40 -03:00
|
|
|
def setUp(self):
|
2010-08-03 03:39:33 -03:00
|
|
|
smtpd.socket = asyncore.socket = mock_socket
|
2011-11-12 15:36:29 -04:00
|
|
|
self.old_debugstream = smtpd.DEBUGSTREAM
|
2010-07-23 13:20:40 -03:00
|
|
|
self.debug = smtpd.DEBUGSTREAM = io.StringIO()
|
2014-08-09 17:40:49 -03:00
|
|
|
self.server = DummyServer((support.HOST, 0), ('b', 0),
|
|
|
|
decode_data=True)
|
2010-07-23 13:20:40 -03:00
|
|
|
conn, addr = self.server.accept()
|
2014-06-11 12:18:08 -03:00
|
|
|
self.channel = smtpd.SMTPChannel(self.server, conn, addr,
|
|
|
|
decode_data=True)
|
2010-07-23 13:20:40 -03:00
|
|
|
|
2010-08-03 03:39:33 -03:00
|
|
|
def tearDown(self):
|
2010-08-15 22:48:14 -03:00
|
|
|
asyncore.close_all()
|
2010-08-03 03:39:33 -03:00
|
|
|
asyncore.socket = smtpd.socket = socket
|
2011-11-12 15:36:29 -04:00
|
|
|
smtpd.DEBUGSTREAM = self.old_debugstream
|
2010-08-03 03:39:33 -03:00
|
|
|
|
2010-07-23 13:20:40 -03:00
|
|
|
def write_line(self, line):
|
|
|
|
self.channel.socket.queue_recv(line)
|
|
|
|
self.channel.handle_read()
|
|
|
|
|
|
|
|
def test_broken_connect(self):
|
2014-06-11 14:48:58 -03:00
|
|
|
self.assertRaises(
|
|
|
|
DummyDispatcherBroken, BrokenDummyServer,
|
2014-08-09 17:40:49 -03:00
|
|
|
(support.HOST, 0), ('b', 0), decode_data=True)
|
2010-07-23 13:20:40 -03:00
|
|
|
|
2015-10-09 11:19:33 -03:00
|
|
|
def test_decode_data_and_enable_SMTPUTF8_raises(self):
|
|
|
|
self.assertRaises(
|
|
|
|
ValueError, smtpd.SMTPChannel,
|
|
|
|
self.server, self.channel.conn, self.channel.addr,
|
|
|
|
enable_SMTPUTF8=True, decode_data=True)
|
|
|
|
|
2010-07-23 13:20:40 -03:00
|
|
|
def test_server_accept(self):
|
|
|
|
self.server.handle_accept()
|
|
|
|
|
|
|
|
def test_missing_data(self):
|
|
|
|
self.write_line(b'')
|
2010-07-31 07:08:09 -03:00
|
|
|
self.assertEqual(self.channel.socket.last,
|
2010-07-23 13:20:40 -03:00
|
|
|
b'500 Error: bad syntax\r\n')
|
|
|
|
|
2012-05-26 15:33:59 -03:00
|
|
|
def test_EHLO(self):
|
|
|
|
self.write_line(b'EHLO example')
|
|
|
|
self.assertEqual(self.channel.socket.last, b'250 HELP\r\n')
|
|
|
|
|
|
|
|
def test_EHLO_bad_syntax(self):
|
|
|
|
self.write_line(b'EHLO')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'501 Syntax: EHLO hostname\r\n')
|
|
|
|
|
|
|
|
def test_EHLO_duplicate(self):
|
|
|
|
self.write_line(b'EHLO example')
|
|
|
|
self.write_line(b'EHLO example')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'503 Duplicate HELO/EHLO\r\n')
|
|
|
|
|
|
|
|
def test_EHLO_HELO_duplicate(self):
|
|
|
|
self.write_line(b'EHLO example')
|
|
|
|
self.write_line(b'HELO example')
|
2010-07-31 07:08:09 -03:00
|
|
|
self.assertEqual(self.channel.socket.last,
|
2012-05-26 15:33:59 -03:00
|
|
|
b'503 Duplicate HELO/EHLO\r\n')
|
2010-07-23 13:20:40 -03:00
|
|
|
|
|
|
|
def test_HELO(self):
|
2010-08-03 03:39:33 -03:00
|
|
|
name = smtpd.socket.getfqdn()
|
2012-05-26 15:33:59 -03:00
|
|
|
self.write_line(b'HELO example')
|
2010-07-31 07:08:09 -03:00
|
|
|
self.assertEqual(self.channel.socket.last,
|
2010-07-23 13:20:40 -03:00
|
|
|
'250 {}\r\n'.format(name).encode('ascii'))
|
|
|
|
|
2012-05-26 15:33:59 -03:00
|
|
|
def test_HELO_EHLO_duplicate(self):
|
|
|
|
self.write_line(b'HELO example')
|
|
|
|
self.write_line(b'EHLO example')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'503 Duplicate HELO/EHLO\r\n')
|
|
|
|
|
|
|
|
def test_HELP(self):
|
|
|
|
self.write_line(b'HELP')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'250 Supported commands: EHLO HELO MAIL RCPT ' + \
|
|
|
|
b'DATA RSET NOOP QUIT VRFY\r\n')
|
|
|
|
|
|
|
|
def test_HELP_command(self):
|
|
|
|
self.write_line(b'HELP MAIL')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'250 Syntax: MAIL FROM: <address>\r\n')
|
|
|
|
|
|
|
|
def test_HELP_command_unknown(self):
|
|
|
|
self.write_line(b'HELP SPAM')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'501 Supported commands: EHLO HELO MAIL RCPT ' + \
|
|
|
|
b'DATA RSET NOOP QUIT VRFY\r\n')
|
|
|
|
|
2010-07-23 13:20:40 -03:00
|
|
|
def test_HELO_bad_syntax(self):
|
|
|
|
self.write_line(b'HELO')
|
2010-07-31 07:08:09 -03:00
|
|
|
self.assertEqual(self.channel.socket.last,
|
2010-07-23 13:20:40 -03:00
|
|
|
b'501 Syntax: HELO hostname\r\n')
|
|
|
|
|
|
|
|
def test_HELO_duplicate(self):
|
2012-05-26 15:33:59 -03:00
|
|
|
self.write_line(b'HELO example')
|
|
|
|
self.write_line(b'HELO example')
|
2010-07-31 07:08:09 -03:00
|
|
|
self.assertEqual(self.channel.socket.last,
|
2010-07-23 13:20:40 -03:00
|
|
|
b'503 Duplicate HELO/EHLO\r\n')
|
|
|
|
|
2012-05-26 15:33:59 -03:00
|
|
|
def test_HELO_parameter_rejected_when_extensions_not_enabled(self):
|
|
|
|
self.extended_smtp = False
|
|
|
|
self.write_line(b'HELO example')
|
|
|
|
self.write_line(b'MAIL from:<foo@example.com> SIZE=1234')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'501 Syntax: MAIL FROM: <address>\r\n')
|
|
|
|
|
|
|
|
def test_MAIL_allows_space_after_colon(self):
|
|
|
|
self.write_line(b'HELO example')
|
|
|
|
self.write_line(b'MAIL from: <foo@example.com>')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'250 OK\r\n')
|
|
|
|
|
|
|
|
def test_extended_MAIL_allows_space_after_colon(self):
|
|
|
|
self.write_line(b'EHLO example')
|
|
|
|
self.write_line(b'MAIL from: <foo@example.com> size=20')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'250 OK\r\n')
|
|
|
|
|
2010-07-23 13:20:40 -03:00
|
|
|
def test_NOOP(self):
|
|
|
|
self.write_line(b'NOOP')
|
2012-05-26 15:33:59 -03:00
|
|
|
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
|
2010-07-23 13:20:40 -03:00
|
|
|
|
2012-03-20 17:16:29 -03:00
|
|
|
def test_HELO_NOOP(self):
|
|
|
|
self.write_line(b'HELO example')
|
|
|
|
self.write_line(b'NOOP')
|
2012-05-26 15:33:59 -03:00
|
|
|
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
|
2012-03-20 17:16:29 -03:00
|
|
|
|
2010-07-23 13:20:40 -03:00
|
|
|
def test_NOOP_bad_syntax(self):
|
|
|
|
self.write_line(b'NOOP hi')
|
2010-07-31 07:08:09 -03:00
|
|
|
self.assertEqual(self.channel.socket.last,
|
2010-07-23 13:20:40 -03:00
|
|
|
b'501 Syntax: NOOP\r\n')
|
|
|
|
|
|
|
|
def test_QUIT(self):
|
|
|
|
self.write_line(b'QUIT')
|
2010-07-31 07:08:09 -03:00
|
|
|
self.assertEqual(self.channel.socket.last, b'221 Bye\r\n')
|
2010-07-23 13:20:40 -03:00
|
|
|
|
2012-03-20 17:16:29 -03:00
|
|
|
def test_HELO_QUIT(self):
|
|
|
|
self.write_line(b'HELO example')
|
|
|
|
self.write_line(b'QUIT')
|
|
|
|
self.assertEqual(self.channel.socket.last, b'221 Bye\r\n')
|
|
|
|
|
2010-07-23 13:20:40 -03:00
|
|
|
def test_QUIT_arg_ignored(self):
|
|
|
|
self.write_line(b'QUIT bye bye')
|
2010-07-31 07:08:09 -03:00
|
|
|
self.assertEqual(self.channel.socket.last, b'221 Bye\r\n')
|
2010-07-23 13:20:40 -03:00
|
|
|
|
|
|
|
def test_bad_state(self):
|
2010-07-31 07:08:09 -03:00
|
|
|
self.channel.smtp_state = 'BAD STATE'
|
2012-03-20 17:16:29 -03:00
|
|
|
self.write_line(b'HELO example')
|
2010-07-31 07:08:09 -03:00
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'451 Internal confusion\r\n')
|
2010-07-23 13:20:40 -03:00
|
|
|
|
2010-12-03 03:38:22 -04:00
|
|
|
def test_command_too_long(self):
|
2012-03-20 17:16:29 -03:00
|
|
|
self.write_line(b'HELO example')
|
2012-05-26 15:33:59 -03:00
|
|
|
self.write_line(b'MAIL from: ' +
|
2010-12-03 03:38:22 -04:00
|
|
|
b'a' * self.channel.command_size_limit +
|
|
|
|
b'@example')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'500 Error: line too long\r\n')
|
|
|
|
|
2012-05-26 15:33:59 -03:00
|
|
|
def test_MAIL_command_limit_extended_with_SIZE(self):
|
|
|
|
self.write_line(b'EHLO example')
|
|
|
|
fill_len = self.channel.command_size_limit - len('MAIL from:<@example>')
|
|
|
|
self.write_line(b'MAIL from:<' +
|
|
|
|
b'a' * fill_len +
|
|
|
|
b'@example> SIZE=1234')
|
|
|
|
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
|
|
|
|
|
|
|
|
self.write_line(b'MAIL from:<' +
|
|
|
|
b'a' * (fill_len + 26) +
|
|
|
|
b'@example> SIZE=1234')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'500 Error: line too long\r\n')
|
|
|
|
|
2014-08-09 17:40:49 -03:00
|
|
|
def test_MAIL_command_rejects_SMTPUTF8_by_default(self):
|
|
|
|
self.write_line(b'EHLO example')
|
|
|
|
self.write_line(
|
|
|
|
b'MAIL from: <naive@example.com> BODY=8BITMIME SMTPUTF8')
|
|
|
|
self.assertEqual(self.channel.socket.last[0:1], b'5')
|
|
|
|
|
2012-05-26 15:33:59 -03:00
|
|
|
def test_data_longer_than_default_data_size_limit(self):
|
|
|
|
# Hack the default so we don't have to generate so much data.
|
|
|
|
self.channel.data_size_limit = 1048
|
2012-03-20 17:16:29 -03:00
|
|
|
self.write_line(b'HELO example')
|
2010-12-03 03:38:22 -04:00
|
|
|
self.write_line(b'MAIL From:eggs@example')
|
|
|
|
self.write_line(b'RCPT To:spam@example')
|
|
|
|
self.write_line(b'DATA')
|
|
|
|
self.write_line(b'A' * self.channel.data_size_limit +
|
|
|
|
b'A\r\n.')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'552 Error: Too much mail data\r\n')
|
|
|
|
|
2012-05-26 15:33:59 -03:00
|
|
|
def test_MAIL_size_parameter(self):
|
|
|
|
self.write_line(b'EHLO example')
|
|
|
|
self.write_line(b'MAIL FROM:<eggs@example> SIZE=512')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'250 OK\r\n')
|
|
|
|
|
|
|
|
def test_MAIL_invalid_size_parameter(self):
|
|
|
|
self.write_line(b'EHLO example')
|
|
|
|
self.write_line(b'MAIL FROM:<eggs@example> SIZE=invalid')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'501 Syntax: MAIL FROM: <address> [SP <mail-parameters>]\r\n')
|
|
|
|
|
|
|
|
def test_MAIL_RCPT_unknown_parameters(self):
|
|
|
|
self.write_line(b'EHLO example')
|
|
|
|
self.write_line(b'MAIL FROM:<eggs@example> ham=green')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'555 MAIL FROM parameters not recognized or not implemented\r\n')
|
|
|
|
|
|
|
|
self.write_line(b'MAIL FROM:<eggs@example>')
|
|
|
|
self.write_line(b'RCPT TO:<eggs@example> ham=green')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'555 RCPT TO parameters not recognized or not implemented\r\n')
|
|
|
|
|
|
|
|
def test_MAIL_size_parameter_larger_than_default_data_size_limit(self):
|
|
|
|
self.channel.data_size_limit = 1048
|
|
|
|
self.write_line(b'EHLO example')
|
|
|
|
self.write_line(b'MAIL FROM:<eggs@example> SIZE=2096')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'552 Error: message size exceeds fixed maximum message size\r\n')
|
|
|
|
|
2010-07-23 13:20:40 -03:00
|
|
|
def test_need_MAIL(self):
|
2012-03-20 17:16:29 -03:00
|
|
|
self.write_line(b'HELO example')
|
2010-07-23 13:20:40 -03:00
|
|
|
self.write_line(b'RCPT to:spam@example')
|
2010-07-31 07:08:09 -03:00
|
|
|
self.assertEqual(self.channel.socket.last,
|
2010-07-23 13:20:40 -03:00
|
|
|
b'503 Error: need MAIL command\r\n')
|
|
|
|
|
2012-05-26 15:33:59 -03:00
|
|
|
def test_MAIL_syntax_HELO(self):
|
2012-03-20 17:16:29 -03:00
|
|
|
self.write_line(b'HELO example')
|
2010-07-23 13:20:40 -03:00
|
|
|
self.write_line(b'MAIL from eggs@example')
|
2010-07-31 07:08:09 -03:00
|
|
|
self.assertEqual(self.channel.socket.last,
|
2012-05-26 15:33:59 -03:00
|
|
|
b'501 Syntax: MAIL FROM: <address>\r\n')
|
2010-07-23 13:20:40 -03:00
|
|
|
|
2012-05-26 15:33:59 -03:00
|
|
|
def test_MAIL_syntax_EHLO(self):
|
|
|
|
self.write_line(b'EHLO example')
|
|
|
|
self.write_line(b'MAIL from eggs@example')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'501 Syntax: MAIL FROM: <address> [SP <mail-parameters>]\r\n')
|
|
|
|
|
|
|
|
def test_MAIL_missing_address(self):
|
2012-03-20 17:16:29 -03:00
|
|
|
self.write_line(b'HELO example')
|
2010-07-23 13:20:40 -03:00
|
|
|
self.write_line(b'MAIL from:')
|
2010-07-31 07:08:09 -03:00
|
|
|
self.assertEqual(self.channel.socket.last,
|
2012-05-26 15:33:59 -03:00
|
|
|
b'501 Syntax: MAIL FROM: <address>\r\n')
|
2010-07-23 13:20:40 -03:00
|
|
|
|
|
|
|
def test_MAIL_chevrons(self):
|
2012-03-20 17:16:29 -03:00
|
|
|
self.write_line(b'HELO example')
|
2010-07-23 13:20:40 -03:00
|
|
|
self.write_line(b'MAIL from:<eggs@example>')
|
2012-05-26 15:33:59 -03:00
|
|
|
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
|
|
|
|
|
|
|
|
def test_MAIL_empty_chevrons(self):
|
|
|
|
self.write_line(b'EHLO example')
|
|
|
|
self.write_line(b'MAIL from:<>')
|
|
|
|
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
|
|
|
|
|
|
|
|
def test_MAIL_quoted_localpart(self):
|
|
|
|
self.write_line(b'EHLO example')
|
|
|
|
self.write_line(b'MAIL from: <"Fred Blogs"@example.com>')
|
|
|
|
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
|
|
|
|
self.assertEqual(self.channel.mailfrom, '"Fred Blogs"@example.com')
|
|
|
|
|
|
|
|
def test_MAIL_quoted_localpart_no_angles(self):
|
|
|
|
self.write_line(b'EHLO example')
|
|
|
|
self.write_line(b'MAIL from: "Fred Blogs"@example.com')
|
|
|
|
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
|
|
|
|
self.assertEqual(self.channel.mailfrom, '"Fred Blogs"@example.com')
|
|
|
|
|
|
|
|
def test_MAIL_quoted_localpart_with_size(self):
|
|
|
|
self.write_line(b'EHLO example')
|
|
|
|
self.write_line(b'MAIL from: <"Fred Blogs"@example.com> SIZE=1000')
|
|
|
|
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
|
|
|
|
self.assertEqual(self.channel.mailfrom, '"Fred Blogs"@example.com')
|
|
|
|
|
|
|
|
def test_MAIL_quoted_localpart_with_size_no_angles(self):
|
|
|
|
self.write_line(b'EHLO example')
|
|
|
|
self.write_line(b'MAIL from: "Fred Blogs"@example.com SIZE=1000')
|
|
|
|
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
|
|
|
|
self.assertEqual(self.channel.mailfrom, '"Fred Blogs"@example.com')
|
2010-07-23 13:20:40 -03:00
|
|
|
|
|
|
|
def test_nested_MAIL(self):
|
2012-03-20 17:16:29 -03:00
|
|
|
self.write_line(b'HELO example')
|
2010-07-23 13:20:40 -03:00
|
|
|
self.write_line(b'MAIL from:eggs@example')
|
|
|
|
self.write_line(b'MAIL from:spam@example')
|
2010-07-31 07:08:09 -03:00
|
|
|
self.assertEqual(self.channel.socket.last,
|
2010-07-23 13:20:40 -03:00
|
|
|
b'503 Error: nested MAIL command\r\n')
|
|
|
|
|
2012-05-26 15:33:59 -03:00
|
|
|
def test_VRFY(self):
|
|
|
|
self.write_line(b'VRFY eggs@example')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'252 Cannot VRFY user, but will accept message and attempt ' + \
|
|
|
|
b'delivery\r\n')
|
|
|
|
|
|
|
|
def test_VRFY_syntax(self):
|
|
|
|
self.write_line(b'VRFY')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'501 Syntax: VRFY <address>\r\n')
|
|
|
|
|
|
|
|
def test_EXPN_not_implemented(self):
|
|
|
|
self.write_line(b'EXPN')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'502 EXPN not implemented\r\n')
|
|
|
|
|
2012-03-20 17:16:29 -03:00
|
|
|
def test_no_HELO_MAIL(self):
|
|
|
|
self.write_line(b'MAIL from:<foo@example.com>')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'503 Error: send HELO first\r\n')
|
|
|
|
|
2010-07-23 13:20:40 -03:00
|
|
|
def test_need_RCPT(self):
|
2012-03-20 17:16:29 -03:00
|
|
|
self.write_line(b'HELO example')
|
2010-07-23 13:20:40 -03:00
|
|
|
self.write_line(b'MAIL From:eggs@example')
|
|
|
|
self.write_line(b'DATA')
|
2010-07-31 07:08:09 -03:00
|
|
|
self.assertEqual(self.channel.socket.last,
|
2010-07-23 13:20:40 -03:00
|
|
|
b'503 Error: need RCPT command\r\n')
|
|
|
|
|
2012-05-26 15:33:59 -03:00
|
|
|
def test_RCPT_syntax_HELO(self):
|
2012-03-20 17:16:29 -03:00
|
|
|
self.write_line(b'HELO example')
|
2012-05-26 15:33:59 -03:00
|
|
|
self.write_line(b'MAIL From: eggs@example')
|
2010-07-23 13:20:40 -03:00
|
|
|
self.write_line(b'RCPT to eggs@example')
|
2010-07-31 07:08:09 -03:00
|
|
|
self.assertEqual(self.channel.socket.last,
|
2010-07-23 13:20:40 -03:00
|
|
|
b'501 Syntax: RCPT TO: <address>\r\n')
|
|
|
|
|
2012-05-26 15:33:59 -03:00
|
|
|
def test_RCPT_syntax_EHLO(self):
|
|
|
|
self.write_line(b'EHLO example')
|
|
|
|
self.write_line(b'MAIL From: eggs@example')
|
|
|
|
self.write_line(b'RCPT to eggs@example')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'501 Syntax: RCPT TO: <address> [SP <mail-parameters>]\r\n')
|
|
|
|
|
|
|
|
def test_RCPT_lowercase_to_OK(self):
|
|
|
|
self.write_line(b'HELO example')
|
|
|
|
self.write_line(b'MAIL From: eggs@example')
|
|
|
|
self.write_line(b'RCPT to: <eggs@example>')
|
|
|
|
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
|
|
|
|
|
2012-03-20 17:16:29 -03:00
|
|
|
def test_no_HELO_RCPT(self):
|
|
|
|
self.write_line(b'RCPT to eggs@example')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'503 Error: send HELO first\r\n')
|
|
|
|
|
2010-07-23 13:20:40 -03:00
|
|
|
def test_data_dialog(self):
|
2012-03-20 17:16:29 -03:00
|
|
|
self.write_line(b'HELO example')
|
2010-07-23 13:20:40 -03:00
|
|
|
self.write_line(b'MAIL From:eggs@example')
|
2012-05-26 15:33:59 -03:00
|
|
|
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
|
2010-07-23 13:20:40 -03:00
|
|
|
self.write_line(b'RCPT To:spam@example')
|
2012-05-26 15:33:59 -03:00
|
|
|
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
|
2010-07-23 13:20:40 -03:00
|
|
|
|
|
|
|
self.write_line(b'DATA')
|
2010-07-31 07:08:09 -03:00
|
|
|
self.assertEqual(self.channel.socket.last,
|
2010-07-23 13:20:40 -03:00
|
|
|
b'354 End data with <CR><LF>.<CR><LF>\r\n')
|
|
|
|
self.write_line(b'data\r\nmore\r\n.')
|
2012-05-26 15:33:59 -03:00
|
|
|
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
|
2010-08-03 22:20:14 -03:00
|
|
|
self.assertEqual(self.server.messages,
|
2014-08-09 17:40:49 -03:00
|
|
|
[(('peer-address', 'peer-port'),
|
|
|
|
'eggs@example',
|
|
|
|
['spam@example'],
|
|
|
|
'data\nmore')])
|
2010-07-23 13:20:40 -03:00
|
|
|
|
|
|
|
def test_DATA_syntax(self):
|
2012-03-20 17:16:29 -03:00
|
|
|
self.write_line(b'HELO example')
|
2010-07-23 13:20:40 -03:00
|
|
|
self.write_line(b'MAIL From:eggs@example')
|
|
|
|
self.write_line(b'RCPT To:spam@example')
|
|
|
|
self.write_line(b'DATA spam')
|
2010-07-31 07:08:09 -03:00
|
|
|
self.assertEqual(self.channel.socket.last, b'501 Syntax: DATA\r\n')
|
2010-07-23 13:20:40 -03:00
|
|
|
|
2012-03-20 17:16:29 -03:00
|
|
|
def test_no_HELO_DATA(self):
|
|
|
|
self.write_line(b'DATA spam')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'503 Error: send HELO first\r\n')
|
|
|
|
|
2010-08-03 22:20:14 -03:00
|
|
|
def test_data_transparency_section_4_5_2(self):
|
2012-03-20 17:16:29 -03:00
|
|
|
self.write_line(b'HELO example')
|
2010-08-03 22:20:14 -03:00
|
|
|
self.write_line(b'MAIL From:eggs@example')
|
|
|
|
self.write_line(b'RCPT To:spam@example')
|
|
|
|
self.write_line(b'DATA')
|
|
|
|
self.write_line(b'..\r\n.\r\n')
|
|
|
|
self.assertEqual(self.channel.received_data, '.')
|
|
|
|
|
2010-07-23 13:20:40 -03:00
|
|
|
def test_multiple_RCPT(self):
|
2012-03-20 17:16:29 -03:00
|
|
|
self.write_line(b'HELO example')
|
2010-07-23 13:20:40 -03:00
|
|
|
self.write_line(b'MAIL From:eggs@example')
|
|
|
|
self.write_line(b'RCPT To:spam@example')
|
|
|
|
self.write_line(b'RCPT To:ham@example')
|
|
|
|
self.write_line(b'DATA')
|
|
|
|
self.write_line(b'data\r\n.')
|
2010-08-04 09:27:36 -03:00
|
|
|
self.assertEqual(self.server.messages,
|
2014-08-09 17:40:49 -03:00
|
|
|
[(('peer-address', 'peer-port'),
|
|
|
|
'eggs@example',
|
|
|
|
['spam@example','ham@example'],
|
|
|
|
'data')])
|
2010-07-23 13:20:40 -03:00
|
|
|
|
|
|
|
def test_manual_status(self):
|
2010-08-03 22:20:14 -03:00
|
|
|
# checks that the Channel is able to return a custom status message
|
2012-03-20 17:16:29 -03:00
|
|
|
self.write_line(b'HELO example')
|
2010-07-23 13:20:40 -03:00
|
|
|
self.write_line(b'MAIL From:eggs@example')
|
|
|
|
self.write_line(b'RCPT To:spam@example')
|
|
|
|
self.write_line(b'DATA')
|
|
|
|
self.write_line(b'return status\r\n.')
|
2010-07-31 07:08:09 -03:00
|
|
|
self.assertEqual(self.channel.socket.last, b'250 Okish\r\n')
|
2010-07-23 13:20:40 -03:00
|
|
|
|
|
|
|
def test_RSET(self):
|
2012-03-20 17:16:29 -03:00
|
|
|
self.write_line(b'HELO example')
|
2010-07-23 13:20:40 -03:00
|
|
|
self.write_line(b'MAIL From:eggs@example')
|
|
|
|
self.write_line(b'RCPT To:spam@example')
|
|
|
|
self.write_line(b'RSET')
|
2012-05-26 15:33:59 -03:00
|
|
|
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
|
2010-07-23 13:20:40 -03:00
|
|
|
self.write_line(b'MAIL From:foo@example')
|
|
|
|
self.write_line(b'RCPT To:eggs@example')
|
|
|
|
self.write_line(b'DATA')
|
|
|
|
self.write_line(b'data\r\n.')
|
2010-08-04 09:27:36 -03:00
|
|
|
self.assertEqual(self.server.messages,
|
2014-08-09 17:40:49 -03:00
|
|
|
[(('peer-address', 'peer-port'),
|
|
|
|
'foo@example',
|
|
|
|
['eggs@example'],
|
|
|
|
'data')])
|
2010-07-23 13:20:40 -03:00
|
|
|
|
2012-03-20 17:16:29 -03:00
|
|
|
def test_HELO_RSET(self):
|
|
|
|
self.write_line(b'HELO example')
|
|
|
|
self.write_line(b'RSET')
|
2012-05-26 15:33:59 -03:00
|
|
|
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
|
2012-03-20 17:16:29 -03:00
|
|
|
|
2010-07-23 13:20:40 -03:00
|
|
|
def test_RSET_syntax(self):
|
|
|
|
self.write_line(b'RSET hi')
|
2010-07-31 07:08:09 -03:00
|
|
|
self.assertEqual(self.channel.socket.last, b'501 Syntax: RSET\r\n')
|
2010-07-23 13:20:40 -03:00
|
|
|
|
2012-05-26 15:33:59 -03:00
|
|
|
def test_unknown_command(self):
|
|
|
|
self.write_line(b'UNKNOWN_CMD')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'500 Error: command "UNKNOWN_CMD" not ' + \
|
|
|
|
b'recognized\r\n')
|
|
|
|
|
2010-08-03 22:20:14 -03:00
|
|
|
def test_attribute_deprecations(self):
|
2011-12-10 06:07:42 -04:00
|
|
|
with support.check_warnings(('', DeprecationWarning)):
|
2010-08-03 22:20:14 -03:00
|
|
|
spam = self.channel._SMTPChannel__server
|
2011-12-10 06:07:42 -04:00
|
|
|
with support.check_warnings(('', DeprecationWarning)):
|
2010-08-03 22:20:14 -03:00
|
|
|
self.channel._SMTPChannel__server = 'spam'
|
2011-12-10 06:07:42 -04:00
|
|
|
with support.check_warnings(('', DeprecationWarning)):
|
2010-08-03 22:20:14 -03:00
|
|
|
spam = self.channel._SMTPChannel__line
|
2011-12-10 06:07:42 -04:00
|
|
|
with support.check_warnings(('', DeprecationWarning)):
|
2010-08-03 22:20:14 -03:00
|
|
|
self.channel._SMTPChannel__line = 'spam'
|
2011-12-10 06:07:42 -04:00
|
|
|
with support.check_warnings(('', DeprecationWarning)):
|
2010-08-03 22:20:14 -03:00
|
|
|
spam = self.channel._SMTPChannel__state
|
2011-12-10 06:07:42 -04:00
|
|
|
with support.check_warnings(('', DeprecationWarning)):
|
2010-08-03 22:20:14 -03:00
|
|
|
self.channel._SMTPChannel__state = 'spam'
|
2011-12-10 06:07:42 -04:00
|
|
|
with support.check_warnings(('', DeprecationWarning)):
|
2010-08-03 22:20:14 -03:00
|
|
|
spam = self.channel._SMTPChannel__greeting
|
2011-12-10 06:07:42 -04:00
|
|
|
with support.check_warnings(('', DeprecationWarning)):
|
2010-08-03 22:20:14 -03:00
|
|
|
self.channel._SMTPChannel__greeting = 'spam'
|
2011-12-10 06:07:42 -04:00
|
|
|
with support.check_warnings(('', DeprecationWarning)):
|
2010-08-03 22:20:14 -03:00
|
|
|
spam = self.channel._SMTPChannel__mailfrom
|
2011-12-10 06:07:42 -04:00
|
|
|
with support.check_warnings(('', DeprecationWarning)):
|
2010-08-03 22:20:14 -03:00
|
|
|
self.channel._SMTPChannel__mailfrom = 'spam'
|
2011-12-10 06:07:42 -04:00
|
|
|
with support.check_warnings(('', DeprecationWarning)):
|
2010-08-03 22:20:14 -03:00
|
|
|
spam = self.channel._SMTPChannel__rcpttos
|
2011-12-10 06:07:42 -04:00
|
|
|
with support.check_warnings(('', DeprecationWarning)):
|
2010-08-03 22:20:14 -03:00
|
|
|
self.channel._SMTPChannel__rcpttos = 'spam'
|
2011-12-10 06:07:42 -04:00
|
|
|
with support.check_warnings(('', DeprecationWarning)):
|
2010-08-03 22:20:14 -03:00
|
|
|
spam = self.channel._SMTPChannel__data
|
2011-12-10 06:07:42 -04:00
|
|
|
with support.check_warnings(('', DeprecationWarning)):
|
2010-08-03 22:20:14 -03:00
|
|
|
self.channel._SMTPChannel__data = 'spam'
|
2011-12-10 06:07:42 -04:00
|
|
|
with support.check_warnings(('', DeprecationWarning)):
|
2010-08-03 22:20:14 -03:00
|
|
|
spam = self.channel._SMTPChannel__fqdn
|
2011-12-10 06:07:42 -04:00
|
|
|
with support.check_warnings(('', DeprecationWarning)):
|
2010-08-03 22:20:14 -03:00
|
|
|
self.channel._SMTPChannel__fqdn = 'spam'
|
2011-12-10 06:07:42 -04:00
|
|
|
with support.check_warnings(('', DeprecationWarning)):
|
2010-08-03 22:20:14 -03:00
|
|
|
spam = self.channel._SMTPChannel__peer
|
2011-12-10 06:07:42 -04:00
|
|
|
with support.check_warnings(('', DeprecationWarning)):
|
2010-08-03 22:20:14 -03:00
|
|
|
self.channel._SMTPChannel__peer = 'spam'
|
2011-12-10 06:07:42 -04:00
|
|
|
with support.check_warnings(('', DeprecationWarning)):
|
2010-08-03 22:20:14 -03:00
|
|
|
spam = self.channel._SMTPChannel__conn
|
2011-12-10 06:07:42 -04:00
|
|
|
with support.check_warnings(('', DeprecationWarning)):
|
2010-08-03 22:20:14 -03:00
|
|
|
self.channel._SMTPChannel__conn = 'spam'
|
2011-12-10 06:07:42 -04:00
|
|
|
with support.check_warnings(('', DeprecationWarning)):
|
2010-08-03 22:20:14 -03:00
|
|
|
spam = self.channel._SMTPChannel__addr
|
2011-12-10 06:07:42 -04:00
|
|
|
with support.check_warnings(('', DeprecationWarning)):
|
2010-08-03 22:20:14 -03:00
|
|
|
self.channel._SMTPChannel__addr = 'spam'
|
2010-07-23 13:20:40 -03:00
|
|
|
|
2014-06-11 14:48:58 -03:00
|
|
|
@unittest.skipUnless(support.IPV6_ENABLED, "IPv6 not enabled")
|
|
|
|
class SMTPDChannelIPv6Test(SMTPDChannelTest):
|
|
|
|
def setUp(self):
|
|
|
|
smtpd.socket = asyncore.socket = mock_socket
|
|
|
|
self.old_debugstream = smtpd.DEBUGSTREAM
|
|
|
|
self.debug = smtpd.DEBUGSTREAM = io.StringIO()
|
2014-08-09 17:40:49 -03:00
|
|
|
self.server = DummyServer((support.HOSTv6, 0), ('b', 0),
|
|
|
|
decode_data=True)
|
2014-06-11 14:48:58 -03:00
|
|
|
conn, addr = self.server.accept()
|
|
|
|
self.channel = smtpd.SMTPChannel(self.server, conn, addr,
|
|
|
|
decode_data=True)
|
2012-05-26 15:33:59 -03:00
|
|
|
|
|
|
|
class SMTPDChannelWithDataSizeLimitTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
smtpd.socket = asyncore.socket = mock_socket
|
2012-06-04 16:55:51 -03:00
|
|
|
self.old_debugstream = smtpd.DEBUGSTREAM
|
2012-05-26 15:33:59 -03:00
|
|
|
self.debug = smtpd.DEBUGSTREAM = io.StringIO()
|
2014-08-09 17:40:49 -03:00
|
|
|
self.server = DummyServer((support.HOST, 0), ('b', 0),
|
|
|
|
decode_data=True)
|
2012-05-26 15:33:59 -03:00
|
|
|
conn, addr = self.server.accept()
|
|
|
|
# Set DATA size limit to 32 bytes for easy testing
|
2014-06-11 12:18:08 -03:00
|
|
|
self.channel = smtpd.SMTPChannel(self.server, conn, addr, 32,
|
|
|
|
decode_data=True)
|
2012-05-26 15:33:59 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
asyncore.close_all()
|
|
|
|
asyncore.socket = smtpd.socket = socket
|
2012-06-04 16:55:51 -03:00
|
|
|
smtpd.DEBUGSTREAM = self.old_debugstream
|
2012-05-26 15:33:59 -03:00
|
|
|
|
|
|
|
def write_line(self, line):
|
|
|
|
self.channel.socket.queue_recv(line)
|
|
|
|
self.channel.handle_read()
|
|
|
|
|
|
|
|
def test_data_limit_dialog(self):
|
|
|
|
self.write_line(b'HELO example')
|
|
|
|
self.write_line(b'MAIL From:eggs@example')
|
|
|
|
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
|
|
|
|
self.write_line(b'RCPT To:spam@example')
|
|
|
|
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
|
|
|
|
|
|
|
|
self.write_line(b'DATA')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'354 End data with <CR><LF>.<CR><LF>\r\n')
|
|
|
|
self.write_line(b'data\r\nmore\r\n.')
|
|
|
|
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
|
|
|
|
self.assertEqual(self.server.messages,
|
2014-08-09 17:40:49 -03:00
|
|
|
[(('peer-address', 'peer-port'),
|
|
|
|
'eggs@example',
|
|
|
|
['spam@example'],
|
|
|
|
'data\nmore')])
|
2012-05-26 15:33:59 -03:00
|
|
|
|
|
|
|
def test_data_limit_dialog_too_much_data(self):
|
|
|
|
self.write_line(b'HELO example')
|
|
|
|
self.write_line(b'MAIL From:eggs@example')
|
|
|
|
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
|
|
|
|
self.write_line(b'RCPT To:spam@example')
|
|
|
|
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
|
|
|
|
|
|
|
|
self.write_line(b'DATA')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'354 End data with <CR><LF>.<CR><LF>\r\n')
|
|
|
|
self.write_line(b'This message is longer than 32 bytes\r\n.')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'552 Error: Too much mail data\r\n')
|
|
|
|
|
2010-07-23 13:20:40 -03:00
|
|
|
|
2014-06-11 12:18:08 -03:00
|
|
|
class SMTPDChannelWithDecodeDataFalse(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
smtpd.socket = asyncore.socket = mock_socket
|
|
|
|
self.old_debugstream = smtpd.DEBUGSTREAM
|
|
|
|
self.debug = smtpd.DEBUGSTREAM = io.StringIO()
|
2016-05-16 03:36:31 -03:00
|
|
|
self.server = DummyServer((support.HOST, 0), ('b', 0))
|
2014-06-11 12:18:08 -03:00
|
|
|
conn, addr = self.server.accept()
|
2016-05-16 03:36:31 -03:00
|
|
|
self.channel = smtpd.SMTPChannel(self.server, conn, addr)
|
2014-06-11 12:18:08 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
asyncore.close_all()
|
|
|
|
asyncore.socket = smtpd.socket = socket
|
|
|
|
smtpd.DEBUGSTREAM = self.old_debugstream
|
|
|
|
|
|
|
|
def write_line(self, line):
|
|
|
|
self.channel.socket.queue_recv(line)
|
|
|
|
self.channel.handle_read()
|
|
|
|
|
|
|
|
def test_ascii_data(self):
|
|
|
|
self.write_line(b'HELO example')
|
|
|
|
self.write_line(b'MAIL From:eggs@example')
|
|
|
|
self.write_line(b'RCPT To:spam@example')
|
|
|
|
self.write_line(b'DATA')
|
|
|
|
self.write_line(b'plain ascii text')
|
|
|
|
self.write_line(b'.')
|
|
|
|
self.assertEqual(self.channel.received_data, b'plain ascii text')
|
|
|
|
|
|
|
|
def test_utf8_data(self):
|
|
|
|
self.write_line(b'HELO example')
|
|
|
|
self.write_line(b'MAIL From:eggs@example')
|
|
|
|
self.write_line(b'RCPT To:spam@example')
|
|
|
|
self.write_line(b'DATA')
|
|
|
|
self.write_line(b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87')
|
|
|
|
self.write_line(b'and some plain ascii')
|
|
|
|
self.write_line(b'.')
|
|
|
|
self.assertEqual(
|
|
|
|
self.channel.received_data,
|
|
|
|
b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87\n'
|
|
|
|
b'and some plain ascii')
|
|
|
|
|
|
|
|
|
|
|
|
class SMTPDChannelWithDecodeDataTrue(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
smtpd.socket = asyncore.socket = mock_socket
|
|
|
|
self.old_debugstream = smtpd.DEBUGSTREAM
|
|
|
|
self.debug = smtpd.DEBUGSTREAM = io.StringIO()
|
2014-06-11 14:48:58 -03:00
|
|
|
self.server = DummyServer((support.HOST, 0), ('b', 0),
|
|
|
|
decode_data=True)
|
2014-06-11 12:18:08 -03:00
|
|
|
conn, addr = self.server.accept()
|
|
|
|
# Set decode_data to True
|
|
|
|
self.channel = smtpd.SMTPChannel(self.server, conn, addr,
|
|
|
|
decode_data=True)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
asyncore.close_all()
|
|
|
|
asyncore.socket = smtpd.socket = socket
|
|
|
|
smtpd.DEBUGSTREAM = self.old_debugstream
|
|
|
|
|
|
|
|
def write_line(self, line):
|
|
|
|
self.channel.socket.queue_recv(line)
|
|
|
|
self.channel.handle_read()
|
|
|
|
|
|
|
|
def test_ascii_data(self):
|
|
|
|
self.write_line(b'HELO example')
|
|
|
|
self.write_line(b'MAIL From:eggs@example')
|
|
|
|
self.write_line(b'RCPT To:spam@example')
|
|
|
|
self.write_line(b'DATA')
|
|
|
|
self.write_line(b'plain ascii text')
|
|
|
|
self.write_line(b'.')
|
|
|
|
self.assertEqual(self.channel.received_data, 'plain ascii text')
|
|
|
|
|
|
|
|
def test_utf8_data(self):
|
|
|
|
self.write_line(b'HELO example')
|
|
|
|
self.write_line(b'MAIL From:eggs@example')
|
|
|
|
self.write_line(b'RCPT To:spam@example')
|
|
|
|
self.write_line(b'DATA')
|
|
|
|
self.write_line(b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87')
|
|
|
|
self.write_line(b'and some plain ascii')
|
|
|
|
self.write_line(b'.')
|
|
|
|
self.assertEqual(
|
|
|
|
self.channel.received_data,
|
|
|
|
'utf8 enriched text: żźć\nand some plain ascii')
|
|
|
|
|
|
|
|
|
2014-08-09 17:40:49 -03:00
|
|
|
class SMTPDChannelTestWithEnableSMTPUTF8True(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
smtpd.socket = asyncore.socket = mock_socket
|
|
|
|
self.old_debugstream = smtpd.DEBUGSTREAM
|
|
|
|
self.debug = smtpd.DEBUGSTREAM = io.StringIO()
|
|
|
|
self.server = DummyServer((support.HOST, 0), ('b', 0),
|
|
|
|
enable_SMTPUTF8=True)
|
|
|
|
conn, addr = self.server.accept()
|
|
|
|
self.channel = smtpd.SMTPChannel(self.server, conn, addr,
|
|
|
|
enable_SMTPUTF8=True)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
asyncore.close_all()
|
|
|
|
asyncore.socket = smtpd.socket = socket
|
|
|
|
smtpd.DEBUGSTREAM = self.old_debugstream
|
|
|
|
|
|
|
|
def write_line(self, line):
|
|
|
|
self.channel.socket.queue_recv(line)
|
|
|
|
self.channel.handle_read()
|
|
|
|
|
|
|
|
def test_MAIL_command_accepts_SMTPUTF8_when_announced(self):
|
|
|
|
self.write_line(b'EHLO example')
|
|
|
|
self.write_line(
|
|
|
|
'MAIL from: <naïve@example.com> BODY=8BITMIME SMTPUTF8'.encode(
|
|
|
|
'utf-8')
|
|
|
|
)
|
|
|
|
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
|
|
|
|
|
|
|
|
def test_process_smtputf8_message(self):
|
|
|
|
self.write_line(b'EHLO example')
|
|
|
|
for mail_parameters in [b'', b'BODY=8BITMIME SMTPUTF8']:
|
|
|
|
self.write_line(b'MAIL from: <a@example> ' + mail_parameters)
|
|
|
|
self.assertEqual(self.channel.socket.last[0:3], b'250')
|
|
|
|
self.write_line(b'rcpt to:<b@example.com>')
|
|
|
|
self.assertEqual(self.channel.socket.last[0:3], b'250')
|
|
|
|
self.write_line(b'data')
|
|
|
|
self.assertEqual(self.channel.socket.last[0:3], b'354')
|
|
|
|
self.write_line(b'c\r\n.')
|
|
|
|
if mail_parameters == b'':
|
|
|
|
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
|
|
|
|
else:
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'250 SMTPUTF8 message okish\r\n')
|
|
|
|
|
|
|
|
def test_utf8_data(self):
|
|
|
|
self.write_line(b'EHLO example')
|
|
|
|
self.write_line(
|
|
|
|
'MAIL From: naïve@examplé BODY=8BITMIME SMTPUTF8'.encode('utf-8'))
|
|
|
|
self.assertEqual(self.channel.socket.last[0:3], b'250')
|
|
|
|
self.write_line('RCPT To:späm@examplé'.encode('utf-8'))
|
|
|
|
self.assertEqual(self.channel.socket.last[0:3], b'250')
|
|
|
|
self.write_line(b'DATA')
|
|
|
|
self.assertEqual(self.channel.socket.last[0:3], b'354')
|
|
|
|
self.write_line(b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87')
|
|
|
|
self.write_line(b'.')
|
|
|
|
self.assertEqual(
|
|
|
|
self.channel.received_data,
|
|
|
|
b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87')
|
|
|
|
|
|
|
|
def test_MAIL_command_limit_extended_with_SIZE_and_SMTPUTF8(self):
|
|
|
|
self.write_line(b'ehlo example')
|
|
|
|
fill_len = (512 + 26 + 10) - len('mail from:<@example>')
|
|
|
|
self.write_line(b'MAIL from:<' +
|
|
|
|
b'a' * (fill_len + 1) +
|
|
|
|
b'@example>')
|
|
|
|
self.assertEqual(self.channel.socket.last,
|
|
|
|
b'500 Error: line too long\r\n')
|
|
|
|
self.write_line(b'MAIL from:<' +
|
|
|
|
b'a' * fill_len +
|
|
|
|
b'@example>')
|
|
|
|
self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
|
|
|
|
|
|
|
|
def test_multiple_emails_with_extended_command_length(self):
|
|
|
|
self.write_line(b'ehlo example')
|
|
|
|
fill_len = (512 + 26 + 10) - len('mail from:<@example>')
|
|
|
|
for char in [b'a', b'b', b'c']:
|
|
|
|
self.write_line(b'MAIL from:<' + char * fill_len + b'a@example>')
|
|
|
|
self.assertEqual(self.channel.socket.last[0:3], b'500')
|
|
|
|
self.write_line(b'MAIL from:<' + char * fill_len + b'@example>')
|
|
|
|
self.assertEqual(self.channel.socket.last[0:3], b'250')
|
|
|
|
self.write_line(b'rcpt to:<hans@example.com>')
|
|
|
|
self.assertEqual(self.channel.socket.last[0:3], b'250')
|
|
|
|
self.write_line(b'data')
|
|
|
|
self.assertEqual(self.channel.socket.last[0:3], b'354')
|
|
|
|
self.write_line(b'test\r\n.')
|
|
|
|
self.assertEqual(self.channel.socket.last[0:3], b'250')
|
|
|
|
|
2016-06-05 23:03:11 -03:00
|
|
|
|
|
|
|
class MiscTestCase(unittest.TestCase):
|
|
|
|
def test__all__(self):
|
|
|
|
blacklist = {
|
|
|
|
"program", "Devnull", "DEBUGSTREAM", "NEWLINE", "COMMASPACE",
|
|
|
|
"DATA_SIZE_DEFAULT", "usage", "Options", "parseargs",
|
|
|
|
|
|
|
|
}
|
|
|
|
support.check__all__(self, smtpd, blacklist=blacklist)
|
|
|
|
|
|
|
|
|
2010-07-23 13:20:40 -03:00
|
|
|
if __name__ == "__main__":
|
2012-05-26 15:33:59 -03:00
|
|
|
unittest.main()
|