blob: 3be7739743940ef95163bdf44c8b1b3005e491dc [file] [log] [blame]
R David Murrayd1a30c92012-05-26 14:33:59 -04001import unittest
R David Murray2539e672014-08-09 16:40:49 -04002import textwrap
Richard Jones64b02de2010-08-03 06:39:33 +00003from test import support, mock_socket
Serhiy Storchaka16994912020-04-25 10:06:29 +03004from test.support import socket_helper
Richard Jones8cb36192010-07-23 16:20:40 +00005import socket
Richard Jones8cb36192010-07-23 16:20:40 +00006import io
7import smtpd
Richard Jones64b02de2010-08-03 06:39:33 +00008import asyncore
Richard Jones8cb36192010-07-23 16:20:40 +00009
Richard Jones8cb36192010-07-23 16:20:40 +000010
11class DummyServer(smtpd.SMTPServer):
R David Murray2539e672014-08-09 16:40:49 -040012 def __init__(self, *args, **kwargs):
13 smtpd.SMTPServer.__init__(self, *args, **kwargs)
Georg Brandl6d23c442010-07-29 13:19:42 +000014 self.messages = []
R David Murray2539e672014-08-09 16:40:49 -040015 if self._decode_data:
R David Murray554bcbf2014-06-11 11:18:08 -040016 self.return_status = 'return status'
17 else:
18 self.return_status = b'return status'
Richard Jones64b02de2010-08-03 06:39:33 +000019
R David Murraya33df312015-05-11 12:11:40 -040020 def process_message(self, peer, mailfrom, rcpttos, data, **kw):
Richard Jones8cb36192010-07-23 16:20:40 +000021 self.messages.append((peer, mailfrom, rcpttos, data))
R David Murray554bcbf2014-06-11 11:18:08 -040022 if data == self.return_status:
Richard Jones8cb36192010-07-23 16:20:40 +000023 return '250 Okish'
R David Murraya33df312015-05-11 12:11:40 -040024 if 'mail_options' in kw and 'SMTPUTF8' in kw['mail_options']:
25 return '250 SMTPUTF8 message okish'
R David Murray2539e672014-08-09 16:40:49 -040026
Richard Jones4aa0d4d2010-08-04 01:20:14 +000027
Richard Jones8cb36192010-07-23 16:20:40 +000028class DummyDispatcherBroken(Exception):
29 pass
30
Richard Jones4aa0d4d2010-08-04 01:20:14 +000031
Richard Jones8cb36192010-07-23 16:20:40 +000032class BrokenDummyServer(DummyServer):
33 def listen(self, num):
34 raise DummyDispatcherBroken()
35
Richard Jones4aa0d4d2010-08-04 01:20:14 +000036
R David Murrayd1a30c92012-05-26 14:33:59 -040037class SMTPDServerTest(unittest.TestCase):
Richard Jones4aa0d4d2010-08-04 01:20:14 +000038 def setUp(self):
39 smtpd.socket = asyncore.socket = mock_socket
40
41 def test_process_message_unimplemented(self):
Serhiy Storchaka16994912020-04-25 10:06:29 +030042 server = smtpd.SMTPServer((socket_helper.HOST, 0), ('b', 0),
R David Murray6fe56a32014-06-11 13:48:58 -040043 decode_data=True)
Richard Jones4aa0d4d2010-08-04 01:20:14 +000044 conn, addr = server.accept()
R David Murray554bcbf2014-06-11 11:18:08 -040045 channel = smtpd.SMTPChannel(server, conn, addr, decode_data=True)
Richard Jones4aa0d4d2010-08-04 01:20:14 +000046
47 def write_line(line):
48 channel.socket.queue_recv(line)
49 channel.handle_read()
50
R David Murrayd1a30c92012-05-26 14:33:59 -040051 write_line(b'HELO example')
Richard Jones4aa0d4d2010-08-04 01:20:14 +000052 write_line(b'MAIL From:eggs@example')
53 write_line(b'RCPT To:spam@example')
54 write_line(b'DATA')
55 self.assertRaises(NotImplementedError, write_line, b'spam\r\n.\r\n')
56
R David Murray2539e672014-08-09 16:40:49 -040057 def test_decode_data_and_enable_SMTPUTF8_raises(self):
58 self.assertRaises(
59 ValueError,
60 smtpd.SMTPServer,
Serhiy Storchaka16994912020-04-25 10:06:29 +030061 (socket_helper.HOST, 0),
R David Murray2539e672014-08-09 16:40:49 -040062 ('b', 0),
63 enable_SMTPUTF8=True,
64 decode_data=True)
65
66 def tearDown(self):
67 asyncore.close_all()
68 asyncore.socket = smtpd.socket = socket
69
70
71class DebuggingServerTest(unittest.TestCase):
72
73 def setUp(self):
74 smtpd.socket = asyncore.socket = mock_socket
75
76 def send_data(self, channel, data, enable_SMTPUTF8=False):
77 def write_line(line):
78 channel.socket.queue_recv(line)
79 channel.handle_read()
80 write_line(b'EHLO example')
81 if enable_SMTPUTF8:
82 write_line(b'MAIL From:eggs@example BODY=8BITMIME SMTPUTF8')
83 else:
84 write_line(b'MAIL From:eggs@example')
85 write_line(b'RCPT To:spam@example')
86 write_line(b'DATA')
87 write_line(data)
88 write_line(b'.')
89
90 def test_process_message_with_decode_data_true(self):
Serhiy Storchaka16994912020-04-25 10:06:29 +030091 server = smtpd.DebuggingServer((socket_helper.HOST, 0), ('b', 0),
R David Murray2539e672014-08-09 16:40:49 -040092 decode_data=True)
93 conn, addr = server.accept()
94 channel = smtpd.SMTPChannel(server, conn, addr, decode_data=True)
95 with support.captured_stdout() as s:
96 self.send_data(channel, b'From: test\n\nhello\n')
97 stdout = s.getvalue()
98 self.assertEqual(stdout, textwrap.dedent("""\
99 ---------- MESSAGE FOLLOWS ----------
100 From: test
101 X-Peer: peer-address
102
103 hello
104 ------------ END MESSAGE ------------
105 """))
106
107 def test_process_message_with_decode_data_false(self):
Serhiy Storchaka16994912020-04-25 10:06:29 +0300108 server = smtpd.DebuggingServer((socket_helper.HOST, 0), ('b', 0))
R David Murray2539e672014-08-09 16:40:49 -0400109 conn, addr = server.accept()
Serhiy Storchakacbcc2fd2016-05-16 09:36:31 +0300110 channel = smtpd.SMTPChannel(server, conn, addr)
R David Murray2539e672014-08-09 16:40:49 -0400111 with support.captured_stdout() as s:
112 self.send_data(channel, b'From: test\n\nh\xc3\xa9llo\xff\n')
113 stdout = s.getvalue()
114 self.assertEqual(stdout, textwrap.dedent("""\
115 ---------- MESSAGE FOLLOWS ----------
116 b'From: test'
117 b'X-Peer: peer-address'
118 b''
119 b'h\\xc3\\xa9llo\\xff'
120 ------------ END MESSAGE ------------
121 """))
122
123 def test_process_message_with_enable_SMTPUTF8_true(self):
Serhiy Storchaka16994912020-04-25 10:06:29 +0300124 server = smtpd.DebuggingServer((socket_helper.HOST, 0), ('b', 0),
R David Murray2539e672014-08-09 16:40:49 -0400125 enable_SMTPUTF8=True)
126 conn, addr = server.accept()
127 channel = smtpd.SMTPChannel(server, conn, addr, enable_SMTPUTF8=True)
128 with support.captured_stdout() as s:
129 self.send_data(channel, b'From: test\n\nh\xc3\xa9llo\xff\n')
130 stdout = s.getvalue()
131 self.assertEqual(stdout, textwrap.dedent("""\
132 ---------- MESSAGE FOLLOWS ----------
133 b'From: test'
134 b'X-Peer: peer-address'
135 b''
136 b'h\\xc3\\xa9llo\\xff'
137 ------------ END MESSAGE ------------
138 """))
139
140 def test_process_SMTPUTF8_message_with_enable_SMTPUTF8_true(self):
Serhiy Storchaka16994912020-04-25 10:06:29 +0300141 server = smtpd.DebuggingServer((socket_helper.HOST, 0), ('b', 0),
R David Murray2539e672014-08-09 16:40:49 -0400142 enable_SMTPUTF8=True)
143 conn, addr = server.accept()
144 channel = smtpd.SMTPChannel(server, conn, addr, enable_SMTPUTF8=True)
145 with support.captured_stdout() as s:
146 self.send_data(channel, b'From: test\n\nh\xc3\xa9llo\xff\n',
147 enable_SMTPUTF8=True)
148 stdout = s.getvalue()
149 self.assertEqual(stdout, textwrap.dedent("""\
R David Murraya33df312015-05-11 12:11:40 -0400150 ---------- MESSAGE FOLLOWS ----------
151 mail options: ['BODY=8BITMIME', 'SMTPUTF8']
R David Murray2539e672014-08-09 16:40:49 -0400152 b'From: test'
153 b'X-Peer: peer-address'
154 b''
155 b'h\\xc3\\xa9llo\\xff'
156 ------------ END MESSAGE ------------
157 """))
158
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000159 def tearDown(self):
Richard Jonesdaf23502010-08-16 01:48:14 +0000160 asyncore.close_all()
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000161 asyncore.socket = smtpd.socket = socket
162
163
R David Murray6fe56a32014-06-11 13:48:58 -0400164class TestFamilyDetection(unittest.TestCase):
165 def setUp(self):
166 smtpd.socket = asyncore.socket = mock_socket
167
168 def tearDown(self):
169 asyncore.close_all()
170 asyncore.socket = smtpd.socket = socket
171
Serhiy Storchaka16994912020-04-25 10:06:29 +0300172 @unittest.skipUnless(socket_helper.IPV6_ENABLED, "IPv6 not enabled")
R David Murray6fe56a32014-06-11 13:48:58 -0400173 def test_socket_uses_IPv6(self):
Serhiy Storchaka16994912020-04-25 10:06:29 +0300174 server = smtpd.SMTPServer((socket_helper.HOSTv6, 0), (socket_helper.HOSTv4, 0))
R David Murray6fe56a32014-06-11 13:48:58 -0400175 self.assertEqual(server.socket.family, socket.AF_INET6)
176
177 def test_socket_uses_IPv4(self):
Serhiy Storchaka16994912020-04-25 10:06:29 +0300178 server = smtpd.SMTPServer((socket_helper.HOSTv4, 0), (socket_helper.HOSTv6, 0))
R David Murray6fe56a32014-06-11 13:48:58 -0400179 self.assertEqual(server.socket.family, socket.AF_INET)
180
181
R David Murraya33df312015-05-11 12:11:40 -0400182class TestRcptOptionParsing(unittest.TestCase):
183 error_response = (b'555 RCPT TO parameters not recognized or not '
184 b'implemented\r\n')
185
186 def setUp(self):
187 smtpd.socket = asyncore.socket = mock_socket
188 self.old_debugstream = smtpd.DEBUGSTREAM
189 self.debug = smtpd.DEBUGSTREAM = io.StringIO()
190
191 def tearDown(self):
192 asyncore.close_all()
193 asyncore.socket = smtpd.socket = socket
194 smtpd.DEBUGSTREAM = self.old_debugstream
195
196 def write_line(self, channel, line):
197 channel.socket.queue_recv(line)
198 channel.handle_read()
199
200 def test_params_rejected(self):
Serhiy Storchaka16994912020-04-25 10:06:29 +0300201 server = DummyServer((socket_helper.HOST, 0), ('b', 0))
R David Murraya33df312015-05-11 12:11:40 -0400202 conn, addr = server.accept()
Serhiy Storchakacbcc2fd2016-05-16 09:36:31 +0300203 channel = smtpd.SMTPChannel(server, conn, addr)
R David Murraya33df312015-05-11 12:11:40 -0400204 self.write_line(channel, b'EHLO example')
205 self.write_line(channel, b'MAIL from: <foo@example.com> size=20')
206 self.write_line(channel, b'RCPT to: <foo@example.com> foo=bar')
207 self.assertEqual(channel.socket.last, self.error_response)
208
209 def test_nothing_accepted(self):
Serhiy Storchaka16994912020-04-25 10:06:29 +0300210 server = DummyServer((socket_helper.HOST, 0), ('b', 0))
R David Murraya33df312015-05-11 12:11:40 -0400211 conn, addr = server.accept()
Serhiy Storchakacbcc2fd2016-05-16 09:36:31 +0300212 channel = smtpd.SMTPChannel(server, conn, addr)
R David Murraya33df312015-05-11 12:11:40 -0400213 self.write_line(channel, b'EHLO example')
214 self.write_line(channel, b'MAIL from: <foo@example.com> size=20')
215 self.write_line(channel, b'RCPT to: <foo@example.com>')
216 self.assertEqual(channel.socket.last, b'250 OK\r\n')
217
218
219class TestMailOptionParsing(unittest.TestCase):
220 error_response = (b'555 MAIL FROM parameters not recognized or not '
221 b'implemented\r\n')
222
223 def setUp(self):
224 smtpd.socket = asyncore.socket = mock_socket
225 self.old_debugstream = smtpd.DEBUGSTREAM
226 self.debug = smtpd.DEBUGSTREAM = io.StringIO()
227
228 def tearDown(self):
229 asyncore.close_all()
230 asyncore.socket = smtpd.socket = socket
231 smtpd.DEBUGSTREAM = self.old_debugstream
232
233 def write_line(self, channel, line):
234 channel.socket.queue_recv(line)
235 channel.handle_read()
236
237 def test_with_decode_data_true(self):
Serhiy Storchaka16994912020-04-25 10:06:29 +0300238 server = DummyServer((socket_helper.HOST, 0), ('b', 0), decode_data=True)
R David Murraya33df312015-05-11 12:11:40 -0400239 conn, addr = server.accept()
240 channel = smtpd.SMTPChannel(server, conn, addr, decode_data=True)
241 self.write_line(channel, b'EHLO example')
242 for line in [
243 b'MAIL from: <foo@example.com> size=20 SMTPUTF8',
244 b'MAIL from: <foo@example.com> size=20 SMTPUTF8 BODY=8BITMIME',
245 b'MAIL from: <foo@example.com> size=20 BODY=UNKNOWN',
246 b'MAIL from: <foo@example.com> size=20 body=8bitmime',
247 ]:
248 self.write_line(channel, line)
249 self.assertEqual(channel.socket.last, self.error_response)
250 self.write_line(channel, b'MAIL from: <foo@example.com> size=20')
251 self.assertEqual(channel.socket.last, b'250 OK\r\n')
252
253 def test_with_decode_data_false(self):
Serhiy Storchaka16994912020-04-25 10:06:29 +0300254 server = DummyServer((socket_helper.HOST, 0), ('b', 0))
R David Murraya33df312015-05-11 12:11:40 -0400255 conn, addr = server.accept()
Serhiy Storchakacbcc2fd2016-05-16 09:36:31 +0300256 channel = smtpd.SMTPChannel(server, conn, addr)
R David Murraya33df312015-05-11 12:11:40 -0400257 self.write_line(channel, b'EHLO example')
258 for line in [
259 b'MAIL from: <foo@example.com> size=20 SMTPUTF8',
260 b'MAIL from: <foo@example.com> size=20 SMTPUTF8 BODY=8BITMIME',
261 ]:
262 self.write_line(channel, line)
263 self.assertEqual(channel.socket.last, self.error_response)
264 self.write_line(
265 channel,
266 b'MAIL from: <foo@example.com> size=20 SMTPUTF8 BODY=UNKNOWN')
267 self.assertEqual(
268 channel.socket.last,
269 b'501 Error: BODY can only be one of 7BIT, 8BITMIME\r\n')
270 self.write_line(
271 channel, b'MAIL from: <foo@example.com> size=20 body=8bitmime')
272 self.assertEqual(channel.socket.last, b'250 OK\r\n')
273
274 def test_with_enable_smtputf8_true(self):
Serhiy Storchaka16994912020-04-25 10:06:29 +0300275 server = DummyServer((socket_helper.HOST, 0), ('b', 0), enable_SMTPUTF8=True)
R David Murraya33df312015-05-11 12:11:40 -0400276 conn, addr = server.accept()
277 channel = smtpd.SMTPChannel(server, conn, addr, enable_SMTPUTF8=True)
278 self.write_line(channel, b'EHLO example')
279 self.write_line(
280 channel,
281 b'MAIL from: <foo@example.com> size=20 body=8bitmime smtputf8')
282 self.assertEqual(channel.socket.last, b'250 OK\r\n')
283
284
R David Murrayd1a30c92012-05-26 14:33:59 -0400285class SMTPDChannelTest(unittest.TestCase):
Richard Jones8cb36192010-07-23 16:20:40 +0000286 def setUp(self):
Richard Jones64b02de2010-08-03 06:39:33 +0000287 smtpd.socket = asyncore.socket = mock_socket
Antoine Pitroue0815e22011-11-12 20:36:29 +0100288 self.old_debugstream = smtpd.DEBUGSTREAM
Richard Jones8cb36192010-07-23 16:20:40 +0000289 self.debug = smtpd.DEBUGSTREAM = io.StringIO()
Serhiy Storchaka16994912020-04-25 10:06:29 +0300290 self.server = DummyServer((socket_helper.HOST, 0), ('b', 0),
R David Murray2539e672014-08-09 16:40:49 -0400291 decode_data=True)
Richard Jones8cb36192010-07-23 16:20:40 +0000292 conn, addr = self.server.accept()
R David Murray554bcbf2014-06-11 11:18:08 -0400293 self.channel = smtpd.SMTPChannel(self.server, conn, addr,
294 decode_data=True)
Richard Jones8cb36192010-07-23 16:20:40 +0000295
Richard Jones64b02de2010-08-03 06:39:33 +0000296 def tearDown(self):
Richard Jonesdaf23502010-08-16 01:48:14 +0000297 asyncore.close_all()
Richard Jones64b02de2010-08-03 06:39:33 +0000298 asyncore.socket = smtpd.socket = socket
Antoine Pitroue0815e22011-11-12 20:36:29 +0100299 smtpd.DEBUGSTREAM = self.old_debugstream
Richard Jones64b02de2010-08-03 06:39:33 +0000300
Richard Jones8cb36192010-07-23 16:20:40 +0000301 def write_line(self, line):
302 self.channel.socket.queue_recv(line)
303 self.channel.handle_read()
304
305 def test_broken_connect(self):
R David Murray6fe56a32014-06-11 13:48:58 -0400306 self.assertRaises(
307 DummyDispatcherBroken, BrokenDummyServer,
Serhiy Storchaka16994912020-04-25 10:06:29 +0300308 (socket_helper.HOST, 0), ('b', 0), decode_data=True)
Richard Jones8cb36192010-07-23 16:20:40 +0000309
R David Murray1a815382015-10-09 10:19:33 -0400310 def test_decode_data_and_enable_SMTPUTF8_raises(self):
311 self.assertRaises(
312 ValueError, smtpd.SMTPChannel,
313 self.server, self.channel.conn, self.channel.addr,
314 enable_SMTPUTF8=True, decode_data=True)
315
Richard Jones8cb36192010-07-23 16:20:40 +0000316 def test_server_accept(self):
317 self.server.handle_accept()
318
319 def test_missing_data(self):
320 self.write_line(b'')
Georg Brandl17e3d692010-07-31 10:08:09 +0000321 self.assertEqual(self.channel.socket.last,
Richard Jones8cb36192010-07-23 16:20:40 +0000322 b'500 Error: bad syntax\r\n')
323
R David Murrayd1a30c92012-05-26 14:33:59 -0400324 def test_EHLO(self):
325 self.write_line(b'EHLO example')
326 self.assertEqual(self.channel.socket.last, b'250 HELP\r\n')
327
328 def test_EHLO_bad_syntax(self):
329 self.write_line(b'EHLO')
Georg Brandl17e3d692010-07-31 10:08:09 +0000330 self.assertEqual(self.channel.socket.last,
R David Murrayd1a30c92012-05-26 14:33:59 -0400331 b'501 Syntax: EHLO hostname\r\n')
332
333 def test_EHLO_duplicate(self):
334 self.write_line(b'EHLO example')
335 self.write_line(b'EHLO example')
336 self.assertEqual(self.channel.socket.last,
337 b'503 Duplicate HELO/EHLO\r\n')
338
339 def test_EHLO_HELO_duplicate(self):
340 self.write_line(b'EHLO example')
341 self.write_line(b'HELO example')
342 self.assertEqual(self.channel.socket.last,
343 b'503 Duplicate HELO/EHLO\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000344
345 def test_HELO(self):
Richard Jones64b02de2010-08-03 06:39:33 +0000346 name = smtpd.socket.getfqdn()
R David Murrayd1a30c92012-05-26 14:33:59 -0400347 self.write_line(b'HELO example')
Georg Brandl17e3d692010-07-31 10:08:09 +0000348 self.assertEqual(self.channel.socket.last,
Richard Jones8cb36192010-07-23 16:20:40 +0000349 '250 {}\r\n'.format(name).encode('ascii'))
350
R David Murrayd1a30c92012-05-26 14:33:59 -0400351 def test_HELO_EHLO_duplicate(self):
352 self.write_line(b'HELO example')
353 self.write_line(b'EHLO example')
354 self.assertEqual(self.channel.socket.last,
355 b'503 Duplicate HELO/EHLO\r\n')
356
357 def test_HELP(self):
358 self.write_line(b'HELP')
359 self.assertEqual(self.channel.socket.last,
360 b'250 Supported commands: EHLO HELO MAIL RCPT ' + \
361 b'DATA RSET NOOP QUIT VRFY\r\n')
362
363 def test_HELP_command(self):
364 self.write_line(b'HELP MAIL')
365 self.assertEqual(self.channel.socket.last,
366 b'250 Syntax: MAIL FROM: <address>\r\n')
367
368 def test_HELP_command_unknown(self):
369 self.write_line(b'HELP SPAM')
370 self.assertEqual(self.channel.socket.last,
371 b'501 Supported commands: EHLO HELO MAIL RCPT ' + \
372 b'DATA RSET NOOP QUIT VRFY\r\n')
373
Richard Jones8cb36192010-07-23 16:20:40 +0000374 def test_HELO_bad_syntax(self):
375 self.write_line(b'HELO')
Georg Brandl17e3d692010-07-31 10:08:09 +0000376 self.assertEqual(self.channel.socket.last,
Richard Jones8cb36192010-07-23 16:20:40 +0000377 b'501 Syntax: HELO hostname\r\n')
378
379 def test_HELO_duplicate(self):
R David Murrayd1a30c92012-05-26 14:33:59 -0400380 self.write_line(b'HELO example')
381 self.write_line(b'HELO example')
Georg Brandl17e3d692010-07-31 10:08:09 +0000382 self.assertEqual(self.channel.socket.last,
Richard Jones8cb36192010-07-23 16:20:40 +0000383 b'503 Duplicate HELO/EHLO\r\n')
384
R David Murrayd1a30c92012-05-26 14:33:59 -0400385 def test_HELO_parameter_rejected_when_extensions_not_enabled(self):
386 self.extended_smtp = False
387 self.write_line(b'HELO example')
388 self.write_line(b'MAIL from:<foo@example.com> SIZE=1234')
389 self.assertEqual(self.channel.socket.last,
390 b'501 Syntax: MAIL FROM: <address>\r\n')
391
392 def test_MAIL_allows_space_after_colon(self):
393 self.write_line(b'HELO example')
394 self.write_line(b'MAIL from: <foo@example.com>')
395 self.assertEqual(self.channel.socket.last,
396 b'250 OK\r\n')
397
398 def test_extended_MAIL_allows_space_after_colon(self):
399 self.write_line(b'EHLO example')
400 self.write_line(b'MAIL from: <foo@example.com> size=20')
401 self.assertEqual(self.channel.socket.last,
402 b'250 OK\r\n')
403
Richard Jones8cb36192010-07-23 16:20:40 +0000404 def test_NOOP(self):
405 self.write_line(b'NOOP')
R David Murrayd1a30c92012-05-26 14:33:59 -0400406 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000407
R David Murray669b7552012-03-20 16:16:29 -0400408 def test_HELO_NOOP(self):
409 self.write_line(b'HELO example')
410 self.write_line(b'NOOP')
R David Murrayd1a30c92012-05-26 14:33:59 -0400411 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
R David Murray669b7552012-03-20 16:16:29 -0400412
Richard Jones8cb36192010-07-23 16:20:40 +0000413 def test_NOOP_bad_syntax(self):
414 self.write_line(b'NOOP hi')
Georg Brandl17e3d692010-07-31 10:08:09 +0000415 self.assertEqual(self.channel.socket.last,
Richard Jones8cb36192010-07-23 16:20:40 +0000416 b'501 Syntax: NOOP\r\n')
417
418 def test_QUIT(self):
419 self.write_line(b'QUIT')
Georg Brandl17e3d692010-07-31 10:08:09 +0000420 self.assertEqual(self.channel.socket.last, b'221 Bye\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000421
R David Murray669b7552012-03-20 16:16:29 -0400422 def test_HELO_QUIT(self):
423 self.write_line(b'HELO example')
424 self.write_line(b'QUIT')
425 self.assertEqual(self.channel.socket.last, b'221 Bye\r\n')
426
Richard Jones8cb36192010-07-23 16:20:40 +0000427 def test_QUIT_arg_ignored(self):
428 self.write_line(b'QUIT bye bye')
Georg Brandl17e3d692010-07-31 10:08:09 +0000429 self.assertEqual(self.channel.socket.last, b'221 Bye\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000430
431 def test_bad_state(self):
Georg Brandl17e3d692010-07-31 10:08:09 +0000432 self.channel.smtp_state = 'BAD STATE'
R David Murray669b7552012-03-20 16:16:29 -0400433 self.write_line(b'HELO example')
Georg Brandl17e3d692010-07-31 10:08:09 +0000434 self.assertEqual(self.channel.socket.last,
435 b'451 Internal confusion\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000436
Georg Brandl1e5c5f82010-12-03 07:38:22 +0000437 def test_command_too_long(self):
R David Murray669b7552012-03-20 16:16:29 -0400438 self.write_line(b'HELO example')
R David Murrayd1a30c92012-05-26 14:33:59 -0400439 self.write_line(b'MAIL from: ' +
Georg Brandl1e5c5f82010-12-03 07:38:22 +0000440 b'a' * self.channel.command_size_limit +
441 b'@example')
442 self.assertEqual(self.channel.socket.last,
443 b'500 Error: line too long\r\n')
444
R David Murrayd1a30c92012-05-26 14:33:59 -0400445 def test_MAIL_command_limit_extended_with_SIZE(self):
446 self.write_line(b'EHLO example')
447 fill_len = self.channel.command_size_limit - len('MAIL from:<@example>')
448 self.write_line(b'MAIL from:<' +
449 b'a' * fill_len +
450 b'@example> SIZE=1234')
451 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
452
453 self.write_line(b'MAIL from:<' +
454 b'a' * (fill_len + 26) +
455 b'@example> SIZE=1234')
456 self.assertEqual(self.channel.socket.last,
457 b'500 Error: line too long\r\n')
458
R David Murray2539e672014-08-09 16:40:49 -0400459 def test_MAIL_command_rejects_SMTPUTF8_by_default(self):
460 self.write_line(b'EHLO example')
461 self.write_line(
462 b'MAIL from: <naive@example.com> BODY=8BITMIME SMTPUTF8')
463 self.assertEqual(self.channel.socket.last[0:1], b'5')
464
R David Murrayd1a30c92012-05-26 14:33:59 -0400465 def test_data_longer_than_default_data_size_limit(self):
466 # Hack the default so we don't have to generate so much data.
467 self.channel.data_size_limit = 1048
R David Murray669b7552012-03-20 16:16:29 -0400468 self.write_line(b'HELO example')
Georg Brandl1e5c5f82010-12-03 07:38:22 +0000469 self.write_line(b'MAIL From:eggs@example')
470 self.write_line(b'RCPT To:spam@example')
471 self.write_line(b'DATA')
472 self.write_line(b'A' * self.channel.data_size_limit +
473 b'A\r\n.')
474 self.assertEqual(self.channel.socket.last,
475 b'552 Error: Too much mail data\r\n')
476
R David Murrayd1a30c92012-05-26 14:33:59 -0400477 def test_MAIL_size_parameter(self):
478 self.write_line(b'EHLO example')
479 self.write_line(b'MAIL FROM:<eggs@example> SIZE=512')
480 self.assertEqual(self.channel.socket.last,
481 b'250 OK\r\n')
482
483 def test_MAIL_invalid_size_parameter(self):
484 self.write_line(b'EHLO example')
485 self.write_line(b'MAIL FROM:<eggs@example> SIZE=invalid')
486 self.assertEqual(self.channel.socket.last,
487 b'501 Syntax: MAIL FROM: <address> [SP <mail-parameters>]\r\n')
488
489 def test_MAIL_RCPT_unknown_parameters(self):
490 self.write_line(b'EHLO example')
491 self.write_line(b'MAIL FROM:<eggs@example> ham=green')
492 self.assertEqual(self.channel.socket.last,
493 b'555 MAIL FROM parameters not recognized or not implemented\r\n')
494
495 self.write_line(b'MAIL FROM:<eggs@example>')
496 self.write_line(b'RCPT TO:<eggs@example> ham=green')
497 self.assertEqual(self.channel.socket.last,
498 b'555 RCPT TO parameters not recognized or not implemented\r\n')
499
500 def test_MAIL_size_parameter_larger_than_default_data_size_limit(self):
501 self.channel.data_size_limit = 1048
502 self.write_line(b'EHLO example')
503 self.write_line(b'MAIL FROM:<eggs@example> SIZE=2096')
504 self.assertEqual(self.channel.socket.last,
505 b'552 Error: message size exceeds fixed maximum message size\r\n')
506
Richard Jones8cb36192010-07-23 16:20:40 +0000507 def test_need_MAIL(self):
R David Murray669b7552012-03-20 16:16:29 -0400508 self.write_line(b'HELO example')
Richard Jones8cb36192010-07-23 16:20:40 +0000509 self.write_line(b'RCPT to:spam@example')
Georg Brandl17e3d692010-07-31 10:08:09 +0000510 self.assertEqual(self.channel.socket.last,
Richard Jones8cb36192010-07-23 16:20:40 +0000511 b'503 Error: need MAIL command\r\n')
512
R David Murrayd1a30c92012-05-26 14:33:59 -0400513 def test_MAIL_syntax_HELO(self):
R David Murray669b7552012-03-20 16:16:29 -0400514 self.write_line(b'HELO example')
Richard Jones8cb36192010-07-23 16:20:40 +0000515 self.write_line(b'MAIL from eggs@example')
Georg Brandl17e3d692010-07-31 10:08:09 +0000516 self.assertEqual(self.channel.socket.last,
R David Murrayd1a30c92012-05-26 14:33:59 -0400517 b'501 Syntax: MAIL FROM: <address>\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000518
R David Murrayd1a30c92012-05-26 14:33:59 -0400519 def test_MAIL_syntax_EHLO(self):
520 self.write_line(b'EHLO example')
521 self.write_line(b'MAIL from eggs@example')
522 self.assertEqual(self.channel.socket.last,
523 b'501 Syntax: MAIL FROM: <address> [SP <mail-parameters>]\r\n')
524
525 def test_MAIL_missing_address(self):
R David Murray669b7552012-03-20 16:16:29 -0400526 self.write_line(b'HELO example')
Richard Jones8cb36192010-07-23 16:20:40 +0000527 self.write_line(b'MAIL from:')
Georg Brandl17e3d692010-07-31 10:08:09 +0000528 self.assertEqual(self.channel.socket.last,
R David Murrayd1a30c92012-05-26 14:33:59 -0400529 b'501 Syntax: MAIL FROM: <address>\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000530
531 def test_MAIL_chevrons(self):
R David Murray669b7552012-03-20 16:16:29 -0400532 self.write_line(b'HELO example')
Richard Jones8cb36192010-07-23 16:20:40 +0000533 self.write_line(b'MAIL from:<eggs@example>')
R David Murrayd1a30c92012-05-26 14:33:59 -0400534 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
535
536 def test_MAIL_empty_chevrons(self):
537 self.write_line(b'EHLO example')
538 self.write_line(b'MAIL from:<>')
539 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
540
541 def test_MAIL_quoted_localpart(self):
542 self.write_line(b'EHLO example')
543 self.write_line(b'MAIL from: <"Fred Blogs"@example.com>')
544 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
545 self.assertEqual(self.channel.mailfrom, '"Fred Blogs"@example.com')
546
547 def test_MAIL_quoted_localpart_no_angles(self):
548 self.write_line(b'EHLO example')
549 self.write_line(b'MAIL from: "Fred Blogs"@example.com')
550 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
551 self.assertEqual(self.channel.mailfrom, '"Fred Blogs"@example.com')
552
553 def test_MAIL_quoted_localpart_with_size(self):
554 self.write_line(b'EHLO example')
555 self.write_line(b'MAIL from: <"Fred Blogs"@example.com> SIZE=1000')
556 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
557 self.assertEqual(self.channel.mailfrom, '"Fred Blogs"@example.com')
558
559 def test_MAIL_quoted_localpart_with_size_no_angles(self):
560 self.write_line(b'EHLO example')
561 self.write_line(b'MAIL from: "Fred Blogs"@example.com SIZE=1000')
562 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
563 self.assertEqual(self.channel.mailfrom, '"Fred Blogs"@example.com')
Richard Jones8cb36192010-07-23 16:20:40 +0000564
565 def test_nested_MAIL(self):
R David Murray669b7552012-03-20 16:16:29 -0400566 self.write_line(b'HELO example')
Richard Jones8cb36192010-07-23 16:20:40 +0000567 self.write_line(b'MAIL from:eggs@example')
568 self.write_line(b'MAIL from:spam@example')
Georg Brandl17e3d692010-07-31 10:08:09 +0000569 self.assertEqual(self.channel.socket.last,
Richard Jones8cb36192010-07-23 16:20:40 +0000570 b'503 Error: nested MAIL command\r\n')
571
R David Murrayd1a30c92012-05-26 14:33:59 -0400572 def test_VRFY(self):
573 self.write_line(b'VRFY eggs@example')
574 self.assertEqual(self.channel.socket.last,
575 b'252 Cannot VRFY user, but will accept message and attempt ' + \
576 b'delivery\r\n')
577
578 def test_VRFY_syntax(self):
579 self.write_line(b'VRFY')
580 self.assertEqual(self.channel.socket.last,
581 b'501 Syntax: VRFY <address>\r\n')
582
583 def test_EXPN_not_implemented(self):
584 self.write_line(b'EXPN')
585 self.assertEqual(self.channel.socket.last,
586 b'502 EXPN not implemented\r\n')
587
R David Murray669b7552012-03-20 16:16:29 -0400588 def test_no_HELO_MAIL(self):
589 self.write_line(b'MAIL from:<foo@example.com>')
590 self.assertEqual(self.channel.socket.last,
591 b'503 Error: send HELO first\r\n')
592
Richard Jones8cb36192010-07-23 16:20:40 +0000593 def test_need_RCPT(self):
R David Murray669b7552012-03-20 16:16:29 -0400594 self.write_line(b'HELO example')
Richard Jones8cb36192010-07-23 16:20:40 +0000595 self.write_line(b'MAIL From:eggs@example')
596 self.write_line(b'DATA')
Georg Brandl17e3d692010-07-31 10:08:09 +0000597 self.assertEqual(self.channel.socket.last,
Richard Jones8cb36192010-07-23 16:20:40 +0000598 b'503 Error: need RCPT command\r\n')
599
R David Murrayd1a30c92012-05-26 14:33:59 -0400600 def test_RCPT_syntax_HELO(self):
R David Murray669b7552012-03-20 16:16:29 -0400601 self.write_line(b'HELO example')
R David Murrayd1a30c92012-05-26 14:33:59 -0400602 self.write_line(b'MAIL From: eggs@example')
Richard Jones8cb36192010-07-23 16:20:40 +0000603 self.write_line(b'RCPT to eggs@example')
Georg Brandl17e3d692010-07-31 10:08:09 +0000604 self.assertEqual(self.channel.socket.last,
Richard Jones8cb36192010-07-23 16:20:40 +0000605 b'501 Syntax: RCPT TO: <address>\r\n')
606
R David Murrayd1a30c92012-05-26 14:33:59 -0400607 def test_RCPT_syntax_EHLO(self):
608 self.write_line(b'EHLO example')
609 self.write_line(b'MAIL From: eggs@example')
610 self.write_line(b'RCPT to eggs@example')
611 self.assertEqual(self.channel.socket.last,
612 b'501 Syntax: RCPT TO: <address> [SP <mail-parameters>]\r\n')
613
614 def test_RCPT_lowercase_to_OK(self):
615 self.write_line(b'HELO example')
616 self.write_line(b'MAIL From: eggs@example')
617 self.write_line(b'RCPT to: <eggs@example>')
618 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
619
R David Murray669b7552012-03-20 16:16:29 -0400620 def test_no_HELO_RCPT(self):
621 self.write_line(b'RCPT to eggs@example')
622 self.assertEqual(self.channel.socket.last,
623 b'503 Error: send HELO first\r\n')
624
Richard Jones8cb36192010-07-23 16:20:40 +0000625 def test_data_dialog(self):
R David Murray669b7552012-03-20 16:16:29 -0400626 self.write_line(b'HELO example')
Richard Jones8cb36192010-07-23 16:20:40 +0000627 self.write_line(b'MAIL From:eggs@example')
R David Murrayd1a30c92012-05-26 14:33:59 -0400628 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000629 self.write_line(b'RCPT To:spam@example')
R David Murrayd1a30c92012-05-26 14:33:59 -0400630 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000631
632 self.write_line(b'DATA')
Georg Brandl17e3d692010-07-31 10:08:09 +0000633 self.assertEqual(self.channel.socket.last,
Richard Jones8cb36192010-07-23 16:20:40 +0000634 b'354 End data with <CR><LF>.<CR><LF>\r\n')
635 self.write_line(b'data\r\nmore\r\n.')
R David Murrayd1a30c92012-05-26 14:33:59 -0400636 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000637 self.assertEqual(self.server.messages,
R David Murray2539e672014-08-09 16:40:49 -0400638 [(('peer-address', 'peer-port'),
639 'eggs@example',
640 ['spam@example'],
641 'data\nmore')])
Richard Jones8cb36192010-07-23 16:20:40 +0000642
643 def test_DATA_syntax(self):
R David Murray669b7552012-03-20 16:16:29 -0400644 self.write_line(b'HELO example')
Richard Jones8cb36192010-07-23 16:20:40 +0000645 self.write_line(b'MAIL From:eggs@example')
646 self.write_line(b'RCPT To:spam@example')
647 self.write_line(b'DATA spam')
Georg Brandl17e3d692010-07-31 10:08:09 +0000648 self.assertEqual(self.channel.socket.last, b'501 Syntax: DATA\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000649
R David Murray669b7552012-03-20 16:16:29 -0400650 def test_no_HELO_DATA(self):
651 self.write_line(b'DATA spam')
652 self.assertEqual(self.channel.socket.last,
653 b'503 Error: send HELO first\r\n')
654
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000655 def test_data_transparency_section_4_5_2(self):
R David Murray669b7552012-03-20 16:16:29 -0400656 self.write_line(b'HELO example')
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000657 self.write_line(b'MAIL From:eggs@example')
658 self.write_line(b'RCPT To:spam@example')
659 self.write_line(b'DATA')
660 self.write_line(b'..\r\n.\r\n')
661 self.assertEqual(self.channel.received_data, '.')
662
Richard Jones8cb36192010-07-23 16:20:40 +0000663 def test_multiple_RCPT(self):
R David Murray669b7552012-03-20 16:16:29 -0400664 self.write_line(b'HELO example')
Richard Jones8cb36192010-07-23 16:20:40 +0000665 self.write_line(b'MAIL From:eggs@example')
666 self.write_line(b'RCPT To:spam@example')
667 self.write_line(b'RCPT To:ham@example')
668 self.write_line(b'DATA')
669 self.write_line(b'data\r\n.')
Richard Jones6a9e6bb2010-08-04 12:27:36 +0000670 self.assertEqual(self.server.messages,
R David Murray2539e672014-08-09 16:40:49 -0400671 [(('peer-address', 'peer-port'),
672 'eggs@example',
673 ['spam@example','ham@example'],
674 'data')])
Richard Jones8cb36192010-07-23 16:20:40 +0000675
676 def test_manual_status(self):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000677 # checks that the Channel is able to return a custom status message
R David Murray669b7552012-03-20 16:16:29 -0400678 self.write_line(b'HELO example')
Richard Jones8cb36192010-07-23 16:20:40 +0000679 self.write_line(b'MAIL From:eggs@example')
680 self.write_line(b'RCPT To:spam@example')
681 self.write_line(b'DATA')
682 self.write_line(b'return status\r\n.')
Georg Brandl17e3d692010-07-31 10:08:09 +0000683 self.assertEqual(self.channel.socket.last, b'250 Okish\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000684
685 def test_RSET(self):
R David Murray669b7552012-03-20 16:16:29 -0400686 self.write_line(b'HELO example')
Richard Jones8cb36192010-07-23 16:20:40 +0000687 self.write_line(b'MAIL From:eggs@example')
688 self.write_line(b'RCPT To:spam@example')
689 self.write_line(b'RSET')
R David Murrayd1a30c92012-05-26 14:33:59 -0400690 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000691 self.write_line(b'MAIL From:foo@example')
692 self.write_line(b'RCPT To:eggs@example')
693 self.write_line(b'DATA')
694 self.write_line(b'data\r\n.')
Richard Jones6a9e6bb2010-08-04 12:27:36 +0000695 self.assertEqual(self.server.messages,
R David Murray2539e672014-08-09 16:40:49 -0400696 [(('peer-address', 'peer-port'),
697 'foo@example',
698 ['eggs@example'],
699 'data')])
Richard Jones8cb36192010-07-23 16:20:40 +0000700
R David Murray669b7552012-03-20 16:16:29 -0400701 def test_HELO_RSET(self):
702 self.write_line(b'HELO example')
703 self.write_line(b'RSET')
R David Murrayd1a30c92012-05-26 14:33:59 -0400704 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
R David Murray669b7552012-03-20 16:16:29 -0400705
Richard Jones8cb36192010-07-23 16:20:40 +0000706 def test_RSET_syntax(self):
707 self.write_line(b'RSET hi')
Georg Brandl17e3d692010-07-31 10:08:09 +0000708 self.assertEqual(self.channel.socket.last, b'501 Syntax: RSET\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000709
R David Murrayd1a30c92012-05-26 14:33:59 -0400710 def test_unknown_command(self):
711 self.write_line(b'UNKNOWN_CMD')
712 self.assertEqual(self.channel.socket.last,
713 b'500 Error: command "UNKNOWN_CMD" not ' + \
714 b'recognized\r\n')
715
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000716 def test_attribute_deprecations(self):
Florent Xicluna67317752011-12-10 11:07:42 +0100717 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000718 spam = self.channel._SMTPChannel__server
Florent Xicluna67317752011-12-10 11:07:42 +0100719 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000720 self.channel._SMTPChannel__server = 'spam'
Florent Xicluna67317752011-12-10 11:07:42 +0100721 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000722 spam = self.channel._SMTPChannel__line
Florent Xicluna67317752011-12-10 11:07:42 +0100723 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000724 self.channel._SMTPChannel__line = 'spam'
Florent Xicluna67317752011-12-10 11:07:42 +0100725 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000726 spam = self.channel._SMTPChannel__state
Florent Xicluna67317752011-12-10 11:07:42 +0100727 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000728 self.channel._SMTPChannel__state = 'spam'
Florent Xicluna67317752011-12-10 11:07:42 +0100729 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000730 spam = self.channel._SMTPChannel__greeting
Florent Xicluna67317752011-12-10 11:07:42 +0100731 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000732 self.channel._SMTPChannel__greeting = 'spam'
Florent Xicluna67317752011-12-10 11:07:42 +0100733 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000734 spam = self.channel._SMTPChannel__mailfrom
Florent Xicluna67317752011-12-10 11:07:42 +0100735 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000736 self.channel._SMTPChannel__mailfrom = 'spam'
Florent Xicluna67317752011-12-10 11:07:42 +0100737 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000738 spam = self.channel._SMTPChannel__rcpttos
Florent Xicluna67317752011-12-10 11:07:42 +0100739 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000740 self.channel._SMTPChannel__rcpttos = 'spam'
Florent Xicluna67317752011-12-10 11:07:42 +0100741 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000742 spam = self.channel._SMTPChannel__data
Florent Xicluna67317752011-12-10 11:07:42 +0100743 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000744 self.channel._SMTPChannel__data = 'spam'
Florent Xicluna67317752011-12-10 11:07:42 +0100745 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000746 spam = self.channel._SMTPChannel__fqdn
Florent Xicluna67317752011-12-10 11:07:42 +0100747 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000748 self.channel._SMTPChannel__fqdn = 'spam'
Florent Xicluna67317752011-12-10 11:07:42 +0100749 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000750 spam = self.channel._SMTPChannel__peer
Florent Xicluna67317752011-12-10 11:07:42 +0100751 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000752 self.channel._SMTPChannel__peer = 'spam'
Florent Xicluna67317752011-12-10 11:07:42 +0100753 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000754 spam = self.channel._SMTPChannel__conn
Florent Xicluna67317752011-12-10 11:07:42 +0100755 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000756 self.channel._SMTPChannel__conn = 'spam'
Florent Xicluna67317752011-12-10 11:07:42 +0100757 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000758 spam = self.channel._SMTPChannel__addr
Florent Xicluna67317752011-12-10 11:07:42 +0100759 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000760 self.channel._SMTPChannel__addr = 'spam'
Richard Jones8cb36192010-07-23 16:20:40 +0000761
Serhiy Storchaka16994912020-04-25 10:06:29 +0300762@unittest.skipUnless(socket_helper.IPV6_ENABLED, "IPv6 not enabled")
R David Murray6fe56a32014-06-11 13:48:58 -0400763class SMTPDChannelIPv6Test(SMTPDChannelTest):
764 def setUp(self):
765 smtpd.socket = asyncore.socket = mock_socket
766 self.old_debugstream = smtpd.DEBUGSTREAM
767 self.debug = smtpd.DEBUGSTREAM = io.StringIO()
Serhiy Storchaka16994912020-04-25 10:06:29 +0300768 self.server = DummyServer((socket_helper.HOSTv6, 0), ('b', 0),
R David Murray2539e672014-08-09 16:40:49 -0400769 decode_data=True)
R David Murray6fe56a32014-06-11 13:48:58 -0400770 conn, addr = self.server.accept()
771 self.channel = smtpd.SMTPChannel(self.server, conn, addr,
772 decode_data=True)
R David Murrayd1a30c92012-05-26 14:33:59 -0400773
774class SMTPDChannelWithDataSizeLimitTest(unittest.TestCase):
775
776 def setUp(self):
777 smtpd.socket = asyncore.socket = mock_socket
R David Murray05cab752012-06-04 15:55:51 -0400778 self.old_debugstream = smtpd.DEBUGSTREAM
R David Murrayd1a30c92012-05-26 14:33:59 -0400779 self.debug = smtpd.DEBUGSTREAM = io.StringIO()
Serhiy Storchaka16994912020-04-25 10:06:29 +0300780 self.server = DummyServer((socket_helper.HOST, 0), ('b', 0),
R David Murray2539e672014-08-09 16:40:49 -0400781 decode_data=True)
R David Murrayd1a30c92012-05-26 14:33:59 -0400782 conn, addr = self.server.accept()
783 # Set DATA size limit to 32 bytes for easy testing
R David Murray554bcbf2014-06-11 11:18:08 -0400784 self.channel = smtpd.SMTPChannel(self.server, conn, addr, 32,
785 decode_data=True)
R David Murrayd1a30c92012-05-26 14:33:59 -0400786
787 def tearDown(self):
788 asyncore.close_all()
789 asyncore.socket = smtpd.socket = socket
R David Murray05cab752012-06-04 15:55:51 -0400790 smtpd.DEBUGSTREAM = self.old_debugstream
R David Murrayd1a30c92012-05-26 14:33:59 -0400791
792 def write_line(self, line):
793 self.channel.socket.queue_recv(line)
794 self.channel.handle_read()
795
796 def test_data_limit_dialog(self):
797 self.write_line(b'HELO example')
798 self.write_line(b'MAIL From:eggs@example')
799 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
800 self.write_line(b'RCPT To:spam@example')
801 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
802
803 self.write_line(b'DATA')
804 self.assertEqual(self.channel.socket.last,
805 b'354 End data with <CR><LF>.<CR><LF>\r\n')
806 self.write_line(b'data\r\nmore\r\n.')
807 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
808 self.assertEqual(self.server.messages,
R David Murray2539e672014-08-09 16:40:49 -0400809 [(('peer-address', 'peer-port'),
810 'eggs@example',
811 ['spam@example'],
812 'data\nmore')])
R David Murrayd1a30c92012-05-26 14:33:59 -0400813
814 def test_data_limit_dialog_too_much_data(self):
815 self.write_line(b'HELO example')
816 self.write_line(b'MAIL From:eggs@example')
817 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
818 self.write_line(b'RCPT To:spam@example')
819 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
820
821 self.write_line(b'DATA')
822 self.assertEqual(self.channel.socket.last,
823 b'354 End data with <CR><LF>.<CR><LF>\r\n')
824 self.write_line(b'This message is longer than 32 bytes\r\n.')
825 self.assertEqual(self.channel.socket.last,
826 b'552 Error: Too much mail data\r\n')
827
Richard Jones8cb36192010-07-23 16:20:40 +0000828
R David Murray554bcbf2014-06-11 11:18:08 -0400829class SMTPDChannelWithDecodeDataFalse(unittest.TestCase):
830
831 def setUp(self):
832 smtpd.socket = asyncore.socket = mock_socket
833 self.old_debugstream = smtpd.DEBUGSTREAM
834 self.debug = smtpd.DEBUGSTREAM = io.StringIO()
Serhiy Storchaka16994912020-04-25 10:06:29 +0300835 self.server = DummyServer((socket_helper.HOST, 0), ('b', 0))
R David Murray554bcbf2014-06-11 11:18:08 -0400836 conn, addr = self.server.accept()
Serhiy Storchakacbcc2fd2016-05-16 09:36:31 +0300837 self.channel = smtpd.SMTPChannel(self.server, conn, addr)
R David Murray554bcbf2014-06-11 11:18:08 -0400838
839 def tearDown(self):
840 asyncore.close_all()
841 asyncore.socket = smtpd.socket = socket
842 smtpd.DEBUGSTREAM = self.old_debugstream
843
844 def write_line(self, line):
845 self.channel.socket.queue_recv(line)
846 self.channel.handle_read()
847
848 def test_ascii_data(self):
849 self.write_line(b'HELO example')
850 self.write_line(b'MAIL From:eggs@example')
851 self.write_line(b'RCPT To:spam@example')
852 self.write_line(b'DATA')
853 self.write_line(b'plain ascii text')
854 self.write_line(b'.')
855 self.assertEqual(self.channel.received_data, b'plain ascii text')
856
857 def test_utf8_data(self):
858 self.write_line(b'HELO example')
859 self.write_line(b'MAIL From:eggs@example')
860 self.write_line(b'RCPT To:spam@example')
861 self.write_line(b'DATA')
862 self.write_line(b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87')
863 self.write_line(b'and some plain ascii')
864 self.write_line(b'.')
865 self.assertEqual(
866 self.channel.received_data,
867 b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87\n'
868 b'and some plain ascii')
869
870
871class SMTPDChannelWithDecodeDataTrue(unittest.TestCase):
872
873 def setUp(self):
874 smtpd.socket = asyncore.socket = mock_socket
875 self.old_debugstream = smtpd.DEBUGSTREAM
876 self.debug = smtpd.DEBUGSTREAM = io.StringIO()
Serhiy Storchaka16994912020-04-25 10:06:29 +0300877 self.server = DummyServer((socket_helper.HOST, 0), ('b', 0),
R David Murray6fe56a32014-06-11 13:48:58 -0400878 decode_data=True)
R David Murray554bcbf2014-06-11 11:18:08 -0400879 conn, addr = self.server.accept()
880 # Set decode_data to True
881 self.channel = smtpd.SMTPChannel(self.server, conn, addr,
882 decode_data=True)
883
884 def tearDown(self):
885 asyncore.close_all()
886 asyncore.socket = smtpd.socket = socket
887 smtpd.DEBUGSTREAM = self.old_debugstream
888
889 def write_line(self, line):
890 self.channel.socket.queue_recv(line)
891 self.channel.handle_read()
892
893 def test_ascii_data(self):
894 self.write_line(b'HELO example')
895 self.write_line(b'MAIL From:eggs@example')
896 self.write_line(b'RCPT To:spam@example')
897 self.write_line(b'DATA')
898 self.write_line(b'plain ascii text')
899 self.write_line(b'.')
900 self.assertEqual(self.channel.received_data, 'plain ascii text')
901
902 def test_utf8_data(self):
903 self.write_line(b'HELO example')
904 self.write_line(b'MAIL From:eggs@example')
905 self.write_line(b'RCPT To:spam@example')
906 self.write_line(b'DATA')
907 self.write_line(b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87')
908 self.write_line(b'and some plain ascii')
909 self.write_line(b'.')
910 self.assertEqual(
911 self.channel.received_data,
912 'utf8 enriched text: żźć\nand some plain ascii')
913
914
R David Murray2539e672014-08-09 16:40:49 -0400915class SMTPDChannelTestWithEnableSMTPUTF8True(unittest.TestCase):
916 def setUp(self):
917 smtpd.socket = asyncore.socket = mock_socket
918 self.old_debugstream = smtpd.DEBUGSTREAM
919 self.debug = smtpd.DEBUGSTREAM = io.StringIO()
Serhiy Storchaka16994912020-04-25 10:06:29 +0300920 self.server = DummyServer((socket_helper.HOST, 0), ('b', 0),
R David Murray2539e672014-08-09 16:40:49 -0400921 enable_SMTPUTF8=True)
922 conn, addr = self.server.accept()
923 self.channel = smtpd.SMTPChannel(self.server, conn, addr,
924 enable_SMTPUTF8=True)
925
926 def tearDown(self):
927 asyncore.close_all()
928 asyncore.socket = smtpd.socket = socket
929 smtpd.DEBUGSTREAM = self.old_debugstream
930
931 def write_line(self, line):
932 self.channel.socket.queue_recv(line)
933 self.channel.handle_read()
934
935 def test_MAIL_command_accepts_SMTPUTF8_when_announced(self):
936 self.write_line(b'EHLO example')
937 self.write_line(
938 'MAIL from: <naïve@example.com> BODY=8BITMIME SMTPUTF8'.encode(
939 'utf-8')
940 )
941 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
942
943 def test_process_smtputf8_message(self):
944 self.write_line(b'EHLO example')
945 for mail_parameters in [b'', b'BODY=8BITMIME SMTPUTF8']:
946 self.write_line(b'MAIL from: <a@example> ' + mail_parameters)
947 self.assertEqual(self.channel.socket.last[0:3], b'250')
948 self.write_line(b'rcpt to:<b@example.com>')
949 self.assertEqual(self.channel.socket.last[0:3], b'250')
950 self.write_line(b'data')
951 self.assertEqual(self.channel.socket.last[0:3], b'354')
952 self.write_line(b'c\r\n.')
953 if mail_parameters == b'':
954 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
955 else:
956 self.assertEqual(self.channel.socket.last,
957 b'250 SMTPUTF8 message okish\r\n')
958
959 def test_utf8_data(self):
960 self.write_line(b'EHLO example')
961 self.write_line(
962 'MAIL From: naïve@examplé BODY=8BITMIME SMTPUTF8'.encode('utf-8'))
963 self.assertEqual(self.channel.socket.last[0:3], b'250')
964 self.write_line('RCPT To:späm@examplé'.encode('utf-8'))
965 self.assertEqual(self.channel.socket.last[0:3], b'250')
966 self.write_line(b'DATA')
967 self.assertEqual(self.channel.socket.last[0:3], b'354')
968 self.write_line(b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87')
969 self.write_line(b'.')
970 self.assertEqual(
971 self.channel.received_data,
972 b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87')
973
974 def test_MAIL_command_limit_extended_with_SIZE_and_SMTPUTF8(self):
975 self.write_line(b'ehlo example')
976 fill_len = (512 + 26 + 10) - len('mail from:<@example>')
977 self.write_line(b'MAIL from:<' +
978 b'a' * (fill_len + 1) +
979 b'@example>')
980 self.assertEqual(self.channel.socket.last,
981 b'500 Error: line too long\r\n')
982 self.write_line(b'MAIL from:<' +
983 b'a' * fill_len +
984 b'@example>')
985 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
986
987 def test_multiple_emails_with_extended_command_length(self):
988 self.write_line(b'ehlo example')
989 fill_len = (512 + 26 + 10) - len('mail from:<@example>')
990 for char in [b'a', b'b', b'c']:
991 self.write_line(b'MAIL from:<' + char * fill_len + b'a@example>')
992 self.assertEqual(self.channel.socket.last[0:3], b'500')
993 self.write_line(b'MAIL from:<' + char * fill_len + b'@example>')
994 self.assertEqual(self.channel.socket.last[0:3], b'250')
995 self.write_line(b'rcpt to:<hans@example.com>')
996 self.assertEqual(self.channel.socket.last[0:3], b'250')
997 self.write_line(b'data')
998 self.assertEqual(self.channel.socket.last[0:3], b'354')
999 self.write_line(b'test\r\n.')
1000 self.assertEqual(self.channel.socket.last[0:3], b'250')
1001
Martin Panter380ef012016-06-06 02:03:11 +00001002
1003class MiscTestCase(unittest.TestCase):
1004 def test__all__(self):
1005 blacklist = {
1006 "program", "Devnull", "DEBUGSTREAM", "NEWLINE", "COMMASPACE",
1007 "DATA_SIZE_DEFAULT", "usage", "Options", "parseargs",
1008
1009 }
1010 support.check__all__(self, smtpd, blacklist=blacklist)
1011
1012
Richard Jones8cb36192010-07-23 16:20:40 +00001013if __name__ == "__main__":
R David Murrayd1a30c92012-05-26 14:33:59 -04001014 unittest.main()