blob: 88dbfdf6f011021e723a68466655ec817c62de83 [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
Richard Jones8cb36192010-07-23 16:20:40 +00004import socket
Richard Jones8cb36192010-07-23 16:20:40 +00005import io
6import smtpd
Richard Jones64b02de2010-08-03 06:39:33 +00007import asyncore
Richard Jones8cb36192010-07-23 16:20:40 +00008
Richard Jones8cb36192010-07-23 16:20:40 +00009
10class DummyServer(smtpd.SMTPServer):
R David Murray2539e672014-08-09 16:40:49 -040011 def __init__(self, *args, **kwargs):
12 smtpd.SMTPServer.__init__(self, *args, **kwargs)
Georg Brandl6d23c442010-07-29 13:19:42 +000013 self.messages = []
R David Murray2539e672014-08-09 16:40:49 -040014 if self._decode_data:
R David Murray554bcbf2014-06-11 11:18:08 -040015 self.return_status = 'return status'
16 else:
17 self.return_status = b'return status'
Richard Jones64b02de2010-08-03 06:39:33 +000018
R David Murraya33df312015-05-11 12:11:40 -040019 def process_message(self, peer, mailfrom, rcpttos, data, **kw):
Richard Jones8cb36192010-07-23 16:20:40 +000020 self.messages.append((peer, mailfrom, rcpttos, data))
R David Murray554bcbf2014-06-11 11:18:08 -040021 if data == self.return_status:
Richard Jones8cb36192010-07-23 16:20:40 +000022 return '250 Okish'
R David Murraya33df312015-05-11 12:11:40 -040023 if 'mail_options' in kw and 'SMTPUTF8' in kw['mail_options']:
24 return '250 SMTPUTF8 message okish'
R David Murray2539e672014-08-09 16:40:49 -040025
Richard Jones4aa0d4d2010-08-04 01:20:14 +000026
Richard Jones8cb36192010-07-23 16:20:40 +000027class DummyDispatcherBroken(Exception):
28 pass
29
Richard Jones4aa0d4d2010-08-04 01:20:14 +000030
Richard Jones8cb36192010-07-23 16:20:40 +000031class BrokenDummyServer(DummyServer):
32 def listen(self, num):
33 raise DummyDispatcherBroken()
34
Richard Jones4aa0d4d2010-08-04 01:20:14 +000035
R David Murrayd1a30c92012-05-26 14:33:59 -040036class SMTPDServerTest(unittest.TestCase):
Richard Jones4aa0d4d2010-08-04 01:20:14 +000037 def setUp(self):
38 smtpd.socket = asyncore.socket = mock_socket
39
40 def test_process_message_unimplemented(self):
R David Murray6fe56a32014-06-11 13:48:58 -040041 server = smtpd.SMTPServer((support.HOST, 0), ('b', 0),
42 decode_data=True)
Richard Jones4aa0d4d2010-08-04 01:20:14 +000043 conn, addr = server.accept()
R David Murray554bcbf2014-06-11 11:18:08 -040044 channel = smtpd.SMTPChannel(server, conn, addr, decode_data=True)
Richard Jones4aa0d4d2010-08-04 01:20:14 +000045
46 def write_line(line):
47 channel.socket.queue_recv(line)
48 channel.handle_read()
49
R David Murrayd1a30c92012-05-26 14:33:59 -040050 write_line(b'HELO example')
Richard Jones4aa0d4d2010-08-04 01:20:14 +000051 write_line(b'MAIL From:eggs@example')
52 write_line(b'RCPT To:spam@example')
53 write_line(b'DATA')
54 self.assertRaises(NotImplementedError, write_line, b'spam\r\n.\r\n')
55
R David Murray554bcbf2014-06-11 11:18:08 -040056 def test_decode_data_default_warns(self):
57 with self.assertWarns(DeprecationWarning):
R David Murray6fe56a32014-06-11 13:48:58 -040058 smtpd.SMTPServer((support.HOST, 0), ('b', 0))
R David Murray554bcbf2014-06-11 11:18:08 -040059
R David Murray2539e672014-08-09 16:40:49 -040060 def test_decode_data_and_enable_SMTPUTF8_raises(self):
61 self.assertRaises(
62 ValueError,
63 smtpd.SMTPServer,
64 (support.HOST, 0),
65 ('b', 0),
66 enable_SMTPUTF8=True,
67 decode_data=True)
68
69 def tearDown(self):
70 asyncore.close_all()
71 asyncore.socket = smtpd.socket = socket
72
73
74class DebuggingServerTest(unittest.TestCase):
75
76 def setUp(self):
77 smtpd.socket = asyncore.socket = mock_socket
78
79 def send_data(self, channel, data, enable_SMTPUTF8=False):
80 def write_line(line):
81 channel.socket.queue_recv(line)
82 channel.handle_read()
83 write_line(b'EHLO example')
84 if enable_SMTPUTF8:
85 write_line(b'MAIL From:eggs@example BODY=8BITMIME SMTPUTF8')
86 else:
87 write_line(b'MAIL From:eggs@example')
88 write_line(b'RCPT To:spam@example')
89 write_line(b'DATA')
90 write_line(data)
91 write_line(b'.')
92
93 def test_process_message_with_decode_data_true(self):
94 server = smtpd.DebuggingServer((support.HOST, 0), ('b', 0),
95 decode_data=True)
96 conn, addr = server.accept()
97 channel = smtpd.SMTPChannel(server, conn, addr, decode_data=True)
98 with support.captured_stdout() as s:
99 self.send_data(channel, b'From: test\n\nhello\n')
100 stdout = s.getvalue()
101 self.assertEqual(stdout, textwrap.dedent("""\
102 ---------- MESSAGE FOLLOWS ----------
103 From: test
104 X-Peer: peer-address
105
106 hello
107 ------------ END MESSAGE ------------
108 """))
109
110 def test_process_message_with_decode_data_false(self):
111 server = smtpd.DebuggingServer((support.HOST, 0), ('b', 0),
112 decode_data=False)
113 conn, addr = server.accept()
114 channel = smtpd.SMTPChannel(server, conn, addr, decode_data=False)
115 with support.captured_stdout() as s:
116 self.send_data(channel, b'From: test\n\nh\xc3\xa9llo\xff\n')
117 stdout = s.getvalue()
118 self.assertEqual(stdout, textwrap.dedent("""\
119 ---------- MESSAGE FOLLOWS ----------
120 b'From: test'
121 b'X-Peer: peer-address'
122 b''
123 b'h\\xc3\\xa9llo\\xff'
124 ------------ END MESSAGE ------------
125 """))
126
127 def test_process_message_with_enable_SMTPUTF8_true(self):
128 server = smtpd.DebuggingServer((support.HOST, 0), ('b', 0),
129 enable_SMTPUTF8=True)
130 conn, addr = server.accept()
131 channel = smtpd.SMTPChannel(server, conn, addr, enable_SMTPUTF8=True)
132 with support.captured_stdout() as s:
133 self.send_data(channel, b'From: test\n\nh\xc3\xa9llo\xff\n')
134 stdout = s.getvalue()
135 self.assertEqual(stdout, textwrap.dedent("""\
136 ---------- MESSAGE FOLLOWS ----------
137 b'From: test'
138 b'X-Peer: peer-address'
139 b''
140 b'h\\xc3\\xa9llo\\xff'
141 ------------ END MESSAGE ------------
142 """))
143
144 def test_process_SMTPUTF8_message_with_enable_SMTPUTF8_true(self):
145 server = smtpd.DebuggingServer((support.HOST, 0), ('b', 0),
146 enable_SMTPUTF8=True)
147 conn, addr = server.accept()
148 channel = smtpd.SMTPChannel(server, conn, addr, enable_SMTPUTF8=True)
149 with support.captured_stdout() as s:
150 self.send_data(channel, b'From: test\n\nh\xc3\xa9llo\xff\n',
151 enable_SMTPUTF8=True)
152 stdout = s.getvalue()
153 self.assertEqual(stdout, textwrap.dedent("""\
R David Murraya33df312015-05-11 12:11:40 -0400154 ---------- MESSAGE FOLLOWS ----------
155 mail options: ['BODY=8BITMIME', 'SMTPUTF8']
R David Murray2539e672014-08-09 16:40:49 -0400156 b'From: test'
157 b'X-Peer: peer-address'
158 b''
159 b'h\\xc3\\xa9llo\\xff'
160 ------------ END MESSAGE ------------
161 """))
162
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000163 def tearDown(self):
Richard Jonesdaf23502010-08-16 01:48:14 +0000164 asyncore.close_all()
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000165 asyncore.socket = smtpd.socket = socket
166
167
R David Murray6fe56a32014-06-11 13:48:58 -0400168class TestFamilyDetection(unittest.TestCase):
169 def setUp(self):
170 smtpd.socket = asyncore.socket = mock_socket
171
172 def tearDown(self):
173 asyncore.close_all()
174 asyncore.socket = smtpd.socket = socket
175
176 @unittest.skipUnless(support.IPV6_ENABLED, "IPv6 not enabled")
177 def test_socket_uses_IPv6(self):
178 server = smtpd.SMTPServer((support.HOSTv6, 0), (support.HOST, 0),
179 decode_data=False)
180 self.assertEqual(server.socket.family, socket.AF_INET6)
181
182 def test_socket_uses_IPv4(self):
183 server = smtpd.SMTPServer((support.HOST, 0), (support.HOSTv6, 0),
184 decode_data=False)
185 self.assertEqual(server.socket.family, socket.AF_INET)
186
187
R David Murraya33df312015-05-11 12:11:40 -0400188class TestRcptOptionParsing(unittest.TestCase):
189 error_response = (b'555 RCPT TO parameters not recognized or not '
190 b'implemented\r\n')
191
192 def setUp(self):
193 smtpd.socket = asyncore.socket = mock_socket
194 self.old_debugstream = smtpd.DEBUGSTREAM
195 self.debug = smtpd.DEBUGSTREAM = io.StringIO()
196
197 def tearDown(self):
198 asyncore.close_all()
199 asyncore.socket = smtpd.socket = socket
200 smtpd.DEBUGSTREAM = self.old_debugstream
201
202 def write_line(self, channel, line):
203 channel.socket.queue_recv(line)
204 channel.handle_read()
205
206 def test_params_rejected(self):
207 server = DummyServer((support.HOST, 0), ('b', 0), decode_data=False)
208 conn, addr = server.accept()
209 channel = smtpd.SMTPChannel(server, conn, addr, decode_data=False)
210 self.write_line(channel, b'EHLO example')
211 self.write_line(channel, b'MAIL from: <foo@example.com> size=20')
212 self.write_line(channel, b'RCPT to: <foo@example.com> foo=bar')
213 self.assertEqual(channel.socket.last, self.error_response)
214
215 def test_nothing_accepted(self):
216 server = DummyServer((support.HOST, 0), ('b', 0), decode_data=False)
217 conn, addr = server.accept()
218 channel = smtpd.SMTPChannel(server, conn, addr, decode_data=False)
219 self.write_line(channel, b'EHLO example')
220 self.write_line(channel, b'MAIL from: <foo@example.com> size=20')
221 self.write_line(channel, b'RCPT to: <foo@example.com>')
222 self.assertEqual(channel.socket.last, b'250 OK\r\n')
223
224
225class TestMailOptionParsing(unittest.TestCase):
226 error_response = (b'555 MAIL FROM parameters not recognized or not '
227 b'implemented\r\n')
228
229 def setUp(self):
230 smtpd.socket = asyncore.socket = mock_socket
231 self.old_debugstream = smtpd.DEBUGSTREAM
232 self.debug = smtpd.DEBUGSTREAM = io.StringIO()
233
234 def tearDown(self):
235 asyncore.close_all()
236 asyncore.socket = smtpd.socket = socket
237 smtpd.DEBUGSTREAM = self.old_debugstream
238
239 def write_line(self, channel, line):
240 channel.socket.queue_recv(line)
241 channel.handle_read()
242
243 def test_with_decode_data_true(self):
244 server = DummyServer((support.HOST, 0), ('b', 0), decode_data=True)
245 conn, addr = server.accept()
246 channel = smtpd.SMTPChannel(server, conn, addr, decode_data=True)
247 self.write_line(channel, b'EHLO example')
248 for line in [
249 b'MAIL from: <foo@example.com> size=20 SMTPUTF8',
250 b'MAIL from: <foo@example.com> size=20 SMTPUTF8 BODY=8BITMIME',
251 b'MAIL from: <foo@example.com> size=20 BODY=UNKNOWN',
252 b'MAIL from: <foo@example.com> size=20 body=8bitmime',
253 ]:
254 self.write_line(channel, line)
255 self.assertEqual(channel.socket.last, self.error_response)
256 self.write_line(channel, b'MAIL from: <foo@example.com> size=20')
257 self.assertEqual(channel.socket.last, b'250 OK\r\n')
258
259 def test_with_decode_data_false(self):
260 server = DummyServer((support.HOST, 0), ('b', 0), decode_data=False)
261 conn, addr = server.accept()
262 channel = smtpd.SMTPChannel(server, conn, addr, decode_data=False)
263 self.write_line(channel, b'EHLO example')
264 for line in [
265 b'MAIL from: <foo@example.com> size=20 SMTPUTF8',
266 b'MAIL from: <foo@example.com> size=20 SMTPUTF8 BODY=8BITMIME',
267 ]:
268 self.write_line(channel, line)
269 self.assertEqual(channel.socket.last, self.error_response)
270 self.write_line(
271 channel,
272 b'MAIL from: <foo@example.com> size=20 SMTPUTF8 BODY=UNKNOWN')
273 self.assertEqual(
274 channel.socket.last,
275 b'501 Error: BODY can only be one of 7BIT, 8BITMIME\r\n')
276 self.write_line(
277 channel, b'MAIL from: <foo@example.com> size=20 body=8bitmime')
278 self.assertEqual(channel.socket.last, b'250 OK\r\n')
279
280 def test_with_enable_smtputf8_true(self):
281 server = DummyServer((support.HOST, 0), ('b', 0), enable_SMTPUTF8=True)
282 conn, addr = server.accept()
283 channel = smtpd.SMTPChannel(server, conn, addr, enable_SMTPUTF8=True)
284 self.write_line(channel, b'EHLO example')
285 self.write_line(
286 channel,
287 b'MAIL from: <foo@example.com> size=20 body=8bitmime smtputf8')
288 self.assertEqual(channel.socket.last, b'250 OK\r\n')
289
290
R David Murrayd1a30c92012-05-26 14:33:59 -0400291class SMTPDChannelTest(unittest.TestCase):
Richard Jones8cb36192010-07-23 16:20:40 +0000292 def setUp(self):
Richard Jones64b02de2010-08-03 06:39:33 +0000293 smtpd.socket = asyncore.socket = mock_socket
Antoine Pitroue0815e22011-11-12 20:36:29 +0100294 self.old_debugstream = smtpd.DEBUGSTREAM
Richard Jones8cb36192010-07-23 16:20:40 +0000295 self.debug = smtpd.DEBUGSTREAM = io.StringIO()
R David Murray2539e672014-08-09 16:40:49 -0400296 self.server = DummyServer((support.HOST, 0), ('b', 0),
297 decode_data=True)
Richard Jones8cb36192010-07-23 16:20:40 +0000298 conn, addr = self.server.accept()
R David Murray554bcbf2014-06-11 11:18:08 -0400299 self.channel = smtpd.SMTPChannel(self.server, conn, addr,
300 decode_data=True)
Richard Jones8cb36192010-07-23 16:20:40 +0000301
Richard Jones64b02de2010-08-03 06:39:33 +0000302 def tearDown(self):
Richard Jonesdaf23502010-08-16 01:48:14 +0000303 asyncore.close_all()
Richard Jones64b02de2010-08-03 06:39:33 +0000304 asyncore.socket = smtpd.socket = socket
Antoine Pitroue0815e22011-11-12 20:36:29 +0100305 smtpd.DEBUGSTREAM = self.old_debugstream
Richard Jones64b02de2010-08-03 06:39:33 +0000306
Richard Jones8cb36192010-07-23 16:20:40 +0000307 def write_line(self, line):
308 self.channel.socket.queue_recv(line)
309 self.channel.handle_read()
310
311 def test_broken_connect(self):
R David Murray6fe56a32014-06-11 13:48:58 -0400312 self.assertRaises(
313 DummyDispatcherBroken, BrokenDummyServer,
R David Murray2539e672014-08-09 16:40:49 -0400314 (support.HOST, 0), ('b', 0), decode_data=True)
Richard Jones8cb36192010-07-23 16:20:40 +0000315
R David Murray1a815382015-10-09 10:19:33 -0400316 def test_decode_data_and_enable_SMTPUTF8_raises(self):
317 self.assertRaises(
318 ValueError, smtpd.SMTPChannel,
319 self.server, self.channel.conn, self.channel.addr,
320 enable_SMTPUTF8=True, decode_data=True)
321
Richard Jones8cb36192010-07-23 16:20:40 +0000322 def test_server_accept(self):
323 self.server.handle_accept()
324
325 def test_missing_data(self):
326 self.write_line(b'')
Georg Brandl17e3d692010-07-31 10:08:09 +0000327 self.assertEqual(self.channel.socket.last,
Richard Jones8cb36192010-07-23 16:20:40 +0000328 b'500 Error: bad syntax\r\n')
329
R David Murrayd1a30c92012-05-26 14:33:59 -0400330 def test_EHLO(self):
331 self.write_line(b'EHLO example')
332 self.assertEqual(self.channel.socket.last, b'250 HELP\r\n')
333
334 def test_EHLO_bad_syntax(self):
335 self.write_line(b'EHLO')
Georg Brandl17e3d692010-07-31 10:08:09 +0000336 self.assertEqual(self.channel.socket.last,
R David Murrayd1a30c92012-05-26 14:33:59 -0400337 b'501 Syntax: EHLO hostname\r\n')
338
339 def test_EHLO_duplicate(self):
340 self.write_line(b'EHLO example')
341 self.write_line(b'EHLO example')
342 self.assertEqual(self.channel.socket.last,
343 b'503 Duplicate HELO/EHLO\r\n')
344
345 def test_EHLO_HELO_duplicate(self):
346 self.write_line(b'EHLO example')
347 self.write_line(b'HELO example')
348 self.assertEqual(self.channel.socket.last,
349 b'503 Duplicate HELO/EHLO\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000350
351 def test_HELO(self):
Richard Jones64b02de2010-08-03 06:39:33 +0000352 name = smtpd.socket.getfqdn()
R David Murrayd1a30c92012-05-26 14:33:59 -0400353 self.write_line(b'HELO example')
Georg Brandl17e3d692010-07-31 10:08:09 +0000354 self.assertEqual(self.channel.socket.last,
Richard Jones8cb36192010-07-23 16:20:40 +0000355 '250 {}\r\n'.format(name).encode('ascii'))
356
R David Murrayd1a30c92012-05-26 14:33:59 -0400357 def test_HELO_EHLO_duplicate(self):
358 self.write_line(b'HELO example')
359 self.write_line(b'EHLO example')
360 self.assertEqual(self.channel.socket.last,
361 b'503 Duplicate HELO/EHLO\r\n')
362
363 def test_HELP(self):
364 self.write_line(b'HELP')
365 self.assertEqual(self.channel.socket.last,
366 b'250 Supported commands: EHLO HELO MAIL RCPT ' + \
367 b'DATA RSET NOOP QUIT VRFY\r\n')
368
369 def test_HELP_command(self):
370 self.write_line(b'HELP MAIL')
371 self.assertEqual(self.channel.socket.last,
372 b'250 Syntax: MAIL FROM: <address>\r\n')
373
374 def test_HELP_command_unknown(self):
375 self.write_line(b'HELP SPAM')
376 self.assertEqual(self.channel.socket.last,
377 b'501 Supported commands: EHLO HELO MAIL RCPT ' + \
378 b'DATA RSET NOOP QUIT VRFY\r\n')
379
Richard Jones8cb36192010-07-23 16:20:40 +0000380 def test_HELO_bad_syntax(self):
381 self.write_line(b'HELO')
Georg Brandl17e3d692010-07-31 10:08:09 +0000382 self.assertEqual(self.channel.socket.last,
Richard Jones8cb36192010-07-23 16:20:40 +0000383 b'501 Syntax: HELO hostname\r\n')
384
385 def test_HELO_duplicate(self):
R David Murrayd1a30c92012-05-26 14:33:59 -0400386 self.write_line(b'HELO example')
387 self.write_line(b'HELO example')
Georg Brandl17e3d692010-07-31 10:08:09 +0000388 self.assertEqual(self.channel.socket.last,
Richard Jones8cb36192010-07-23 16:20:40 +0000389 b'503 Duplicate HELO/EHLO\r\n')
390
R David Murrayd1a30c92012-05-26 14:33:59 -0400391 def test_HELO_parameter_rejected_when_extensions_not_enabled(self):
392 self.extended_smtp = False
393 self.write_line(b'HELO example')
394 self.write_line(b'MAIL from:<foo@example.com> SIZE=1234')
395 self.assertEqual(self.channel.socket.last,
396 b'501 Syntax: MAIL FROM: <address>\r\n')
397
398 def test_MAIL_allows_space_after_colon(self):
399 self.write_line(b'HELO example')
400 self.write_line(b'MAIL from: <foo@example.com>')
401 self.assertEqual(self.channel.socket.last,
402 b'250 OK\r\n')
403
404 def test_extended_MAIL_allows_space_after_colon(self):
405 self.write_line(b'EHLO example')
406 self.write_line(b'MAIL from: <foo@example.com> size=20')
407 self.assertEqual(self.channel.socket.last,
408 b'250 OK\r\n')
409
Richard Jones8cb36192010-07-23 16:20:40 +0000410 def test_NOOP(self):
411 self.write_line(b'NOOP')
R David Murrayd1a30c92012-05-26 14:33:59 -0400412 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000413
R David Murray669b7552012-03-20 16:16:29 -0400414 def test_HELO_NOOP(self):
415 self.write_line(b'HELO example')
416 self.write_line(b'NOOP')
R David Murrayd1a30c92012-05-26 14:33:59 -0400417 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
R David Murray669b7552012-03-20 16:16:29 -0400418
Richard Jones8cb36192010-07-23 16:20:40 +0000419 def test_NOOP_bad_syntax(self):
420 self.write_line(b'NOOP hi')
Georg Brandl17e3d692010-07-31 10:08:09 +0000421 self.assertEqual(self.channel.socket.last,
Richard Jones8cb36192010-07-23 16:20:40 +0000422 b'501 Syntax: NOOP\r\n')
423
424 def test_QUIT(self):
425 self.write_line(b'QUIT')
Georg Brandl17e3d692010-07-31 10:08:09 +0000426 self.assertEqual(self.channel.socket.last, b'221 Bye\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000427
R David Murray669b7552012-03-20 16:16:29 -0400428 def test_HELO_QUIT(self):
429 self.write_line(b'HELO example')
430 self.write_line(b'QUIT')
431 self.assertEqual(self.channel.socket.last, b'221 Bye\r\n')
432
Richard Jones8cb36192010-07-23 16:20:40 +0000433 def test_QUIT_arg_ignored(self):
434 self.write_line(b'QUIT bye bye')
Georg Brandl17e3d692010-07-31 10:08:09 +0000435 self.assertEqual(self.channel.socket.last, b'221 Bye\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000436
437 def test_bad_state(self):
Georg Brandl17e3d692010-07-31 10:08:09 +0000438 self.channel.smtp_state = 'BAD STATE'
R David Murray669b7552012-03-20 16:16:29 -0400439 self.write_line(b'HELO example')
Georg Brandl17e3d692010-07-31 10:08:09 +0000440 self.assertEqual(self.channel.socket.last,
441 b'451 Internal confusion\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000442
Georg Brandl1e5c5f82010-12-03 07:38:22 +0000443 def test_command_too_long(self):
R David Murray669b7552012-03-20 16:16:29 -0400444 self.write_line(b'HELO example')
R David Murrayd1a30c92012-05-26 14:33:59 -0400445 self.write_line(b'MAIL from: ' +
Georg Brandl1e5c5f82010-12-03 07:38:22 +0000446 b'a' * self.channel.command_size_limit +
447 b'@example')
448 self.assertEqual(self.channel.socket.last,
449 b'500 Error: line too long\r\n')
450
R David Murrayd1a30c92012-05-26 14:33:59 -0400451 def test_MAIL_command_limit_extended_with_SIZE(self):
452 self.write_line(b'EHLO example')
453 fill_len = self.channel.command_size_limit - len('MAIL from:<@example>')
454 self.write_line(b'MAIL from:<' +
455 b'a' * fill_len +
456 b'@example> SIZE=1234')
457 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
458
459 self.write_line(b'MAIL from:<' +
460 b'a' * (fill_len + 26) +
461 b'@example> SIZE=1234')
462 self.assertEqual(self.channel.socket.last,
463 b'500 Error: line too long\r\n')
464
R David Murray2539e672014-08-09 16:40:49 -0400465 def test_MAIL_command_rejects_SMTPUTF8_by_default(self):
466 self.write_line(b'EHLO example')
467 self.write_line(
468 b'MAIL from: <naive@example.com> BODY=8BITMIME SMTPUTF8')
469 self.assertEqual(self.channel.socket.last[0:1], b'5')
470
R David Murrayd1a30c92012-05-26 14:33:59 -0400471 def test_data_longer_than_default_data_size_limit(self):
472 # Hack the default so we don't have to generate so much data.
473 self.channel.data_size_limit = 1048
R David Murray669b7552012-03-20 16:16:29 -0400474 self.write_line(b'HELO example')
Georg Brandl1e5c5f82010-12-03 07:38:22 +0000475 self.write_line(b'MAIL From:eggs@example')
476 self.write_line(b'RCPT To:spam@example')
477 self.write_line(b'DATA')
478 self.write_line(b'A' * self.channel.data_size_limit +
479 b'A\r\n.')
480 self.assertEqual(self.channel.socket.last,
481 b'552 Error: Too much mail data\r\n')
482
R David Murrayd1a30c92012-05-26 14:33:59 -0400483 def test_MAIL_size_parameter(self):
484 self.write_line(b'EHLO example')
485 self.write_line(b'MAIL FROM:<eggs@example> SIZE=512')
486 self.assertEqual(self.channel.socket.last,
487 b'250 OK\r\n')
488
489 def test_MAIL_invalid_size_parameter(self):
490 self.write_line(b'EHLO example')
491 self.write_line(b'MAIL FROM:<eggs@example> SIZE=invalid')
492 self.assertEqual(self.channel.socket.last,
493 b'501 Syntax: MAIL FROM: <address> [SP <mail-parameters>]\r\n')
494
495 def test_MAIL_RCPT_unknown_parameters(self):
496 self.write_line(b'EHLO example')
497 self.write_line(b'MAIL FROM:<eggs@example> ham=green')
498 self.assertEqual(self.channel.socket.last,
499 b'555 MAIL FROM parameters not recognized or not implemented\r\n')
500
501 self.write_line(b'MAIL FROM:<eggs@example>')
502 self.write_line(b'RCPT TO:<eggs@example> ham=green')
503 self.assertEqual(self.channel.socket.last,
504 b'555 RCPT TO parameters not recognized or not implemented\r\n')
505
506 def test_MAIL_size_parameter_larger_than_default_data_size_limit(self):
507 self.channel.data_size_limit = 1048
508 self.write_line(b'EHLO example')
509 self.write_line(b'MAIL FROM:<eggs@example> SIZE=2096')
510 self.assertEqual(self.channel.socket.last,
511 b'552 Error: message size exceeds fixed maximum message size\r\n')
512
Richard Jones8cb36192010-07-23 16:20:40 +0000513 def test_need_MAIL(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'RCPT to:spam@example')
Georg Brandl17e3d692010-07-31 10:08:09 +0000516 self.assertEqual(self.channel.socket.last,
Richard Jones8cb36192010-07-23 16:20:40 +0000517 b'503 Error: need MAIL command\r\n')
518
R David Murrayd1a30c92012-05-26 14:33:59 -0400519 def test_MAIL_syntax_HELO(self):
R David Murray669b7552012-03-20 16:16:29 -0400520 self.write_line(b'HELO example')
Richard Jones8cb36192010-07-23 16:20:40 +0000521 self.write_line(b'MAIL from eggs@example')
Georg Brandl17e3d692010-07-31 10:08:09 +0000522 self.assertEqual(self.channel.socket.last,
R David Murrayd1a30c92012-05-26 14:33:59 -0400523 b'501 Syntax: MAIL FROM: <address>\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000524
R David Murrayd1a30c92012-05-26 14:33:59 -0400525 def test_MAIL_syntax_EHLO(self):
526 self.write_line(b'EHLO example')
527 self.write_line(b'MAIL from eggs@example')
528 self.assertEqual(self.channel.socket.last,
529 b'501 Syntax: MAIL FROM: <address> [SP <mail-parameters>]\r\n')
530
531 def test_MAIL_missing_address(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:')
Georg Brandl17e3d692010-07-31 10:08:09 +0000534 self.assertEqual(self.channel.socket.last,
R David Murrayd1a30c92012-05-26 14:33:59 -0400535 b'501 Syntax: MAIL FROM: <address>\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000536
537 def test_MAIL_chevrons(self):
R David Murray669b7552012-03-20 16:16:29 -0400538 self.write_line(b'HELO example')
Richard Jones8cb36192010-07-23 16:20:40 +0000539 self.write_line(b'MAIL from:<eggs@example>')
R David Murrayd1a30c92012-05-26 14:33:59 -0400540 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
541
542 def test_MAIL_empty_chevrons(self):
543 self.write_line(b'EHLO example')
544 self.write_line(b'MAIL from:<>')
545 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
546
547 def test_MAIL_quoted_localpart(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_no_angles(self):
554 self.write_line(b'EHLO example')
555 self.write_line(b'MAIL from: "Fred Blogs"@example.com')
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(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')
564
565 def test_MAIL_quoted_localpart_with_size_no_angles(self):
566 self.write_line(b'EHLO example')
567 self.write_line(b'MAIL from: "Fred Blogs"@example.com SIZE=1000')
568 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
569 self.assertEqual(self.channel.mailfrom, '"Fred Blogs"@example.com')
Richard Jones8cb36192010-07-23 16:20:40 +0000570
571 def test_nested_MAIL(self):
R David Murray669b7552012-03-20 16:16:29 -0400572 self.write_line(b'HELO example')
Richard Jones8cb36192010-07-23 16:20:40 +0000573 self.write_line(b'MAIL from:eggs@example')
574 self.write_line(b'MAIL from:spam@example')
Georg Brandl17e3d692010-07-31 10:08:09 +0000575 self.assertEqual(self.channel.socket.last,
Richard Jones8cb36192010-07-23 16:20:40 +0000576 b'503 Error: nested MAIL command\r\n')
577
R David Murrayd1a30c92012-05-26 14:33:59 -0400578 def test_VRFY(self):
579 self.write_line(b'VRFY eggs@example')
580 self.assertEqual(self.channel.socket.last,
581 b'252 Cannot VRFY user, but will accept message and attempt ' + \
582 b'delivery\r\n')
583
584 def test_VRFY_syntax(self):
585 self.write_line(b'VRFY')
586 self.assertEqual(self.channel.socket.last,
587 b'501 Syntax: VRFY <address>\r\n')
588
589 def test_EXPN_not_implemented(self):
590 self.write_line(b'EXPN')
591 self.assertEqual(self.channel.socket.last,
592 b'502 EXPN not implemented\r\n')
593
R David Murray669b7552012-03-20 16:16:29 -0400594 def test_no_HELO_MAIL(self):
595 self.write_line(b'MAIL from:<foo@example.com>')
596 self.assertEqual(self.channel.socket.last,
597 b'503 Error: send HELO first\r\n')
598
Richard Jones8cb36192010-07-23 16:20:40 +0000599 def test_need_RCPT(self):
R David Murray669b7552012-03-20 16:16:29 -0400600 self.write_line(b'HELO example')
Richard Jones8cb36192010-07-23 16:20:40 +0000601 self.write_line(b'MAIL From:eggs@example')
602 self.write_line(b'DATA')
Georg Brandl17e3d692010-07-31 10:08:09 +0000603 self.assertEqual(self.channel.socket.last,
Richard Jones8cb36192010-07-23 16:20:40 +0000604 b'503 Error: need RCPT command\r\n')
605
R David Murrayd1a30c92012-05-26 14:33:59 -0400606 def test_RCPT_syntax_HELO(self):
R David Murray669b7552012-03-20 16:16:29 -0400607 self.write_line(b'HELO example')
R David Murrayd1a30c92012-05-26 14:33:59 -0400608 self.write_line(b'MAIL From: eggs@example')
Richard Jones8cb36192010-07-23 16:20:40 +0000609 self.write_line(b'RCPT to eggs@example')
Georg Brandl17e3d692010-07-31 10:08:09 +0000610 self.assertEqual(self.channel.socket.last,
Richard Jones8cb36192010-07-23 16:20:40 +0000611 b'501 Syntax: RCPT TO: <address>\r\n')
612
R David Murrayd1a30c92012-05-26 14:33:59 -0400613 def test_RCPT_syntax_EHLO(self):
614 self.write_line(b'EHLO example')
615 self.write_line(b'MAIL From: eggs@example')
616 self.write_line(b'RCPT to eggs@example')
617 self.assertEqual(self.channel.socket.last,
618 b'501 Syntax: RCPT TO: <address> [SP <mail-parameters>]\r\n')
619
620 def test_RCPT_lowercase_to_OK(self):
621 self.write_line(b'HELO example')
622 self.write_line(b'MAIL From: eggs@example')
623 self.write_line(b'RCPT to: <eggs@example>')
624 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
625
R David Murray669b7552012-03-20 16:16:29 -0400626 def test_no_HELO_RCPT(self):
627 self.write_line(b'RCPT to eggs@example')
628 self.assertEqual(self.channel.socket.last,
629 b'503 Error: send HELO first\r\n')
630
Richard Jones8cb36192010-07-23 16:20:40 +0000631 def test_data_dialog(self):
R David Murray669b7552012-03-20 16:16:29 -0400632 self.write_line(b'HELO example')
Richard Jones8cb36192010-07-23 16:20:40 +0000633 self.write_line(b'MAIL From:eggs@example')
R David Murrayd1a30c92012-05-26 14:33:59 -0400634 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000635 self.write_line(b'RCPT To:spam@example')
R David Murrayd1a30c92012-05-26 14:33:59 -0400636 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000637
638 self.write_line(b'DATA')
Georg Brandl17e3d692010-07-31 10:08:09 +0000639 self.assertEqual(self.channel.socket.last,
Richard Jones8cb36192010-07-23 16:20:40 +0000640 b'354 End data with <CR><LF>.<CR><LF>\r\n')
641 self.write_line(b'data\r\nmore\r\n.')
R David Murrayd1a30c92012-05-26 14:33:59 -0400642 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000643 self.assertEqual(self.server.messages,
R David Murray2539e672014-08-09 16:40:49 -0400644 [(('peer-address', 'peer-port'),
645 'eggs@example',
646 ['spam@example'],
647 'data\nmore')])
Richard Jones8cb36192010-07-23 16:20:40 +0000648
649 def test_DATA_syntax(self):
R David Murray669b7552012-03-20 16:16:29 -0400650 self.write_line(b'HELO example')
Richard Jones8cb36192010-07-23 16:20:40 +0000651 self.write_line(b'MAIL From:eggs@example')
652 self.write_line(b'RCPT To:spam@example')
653 self.write_line(b'DATA spam')
Georg Brandl17e3d692010-07-31 10:08:09 +0000654 self.assertEqual(self.channel.socket.last, b'501 Syntax: DATA\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000655
R David Murray669b7552012-03-20 16:16:29 -0400656 def test_no_HELO_DATA(self):
657 self.write_line(b'DATA spam')
658 self.assertEqual(self.channel.socket.last,
659 b'503 Error: send HELO first\r\n')
660
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000661 def test_data_transparency_section_4_5_2(self):
R David Murray669b7552012-03-20 16:16:29 -0400662 self.write_line(b'HELO example')
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000663 self.write_line(b'MAIL From:eggs@example')
664 self.write_line(b'RCPT To:spam@example')
665 self.write_line(b'DATA')
666 self.write_line(b'..\r\n.\r\n')
667 self.assertEqual(self.channel.received_data, '.')
668
Richard Jones8cb36192010-07-23 16:20:40 +0000669 def test_multiple_RCPT(self):
R David Murray669b7552012-03-20 16:16:29 -0400670 self.write_line(b'HELO example')
Richard Jones8cb36192010-07-23 16:20:40 +0000671 self.write_line(b'MAIL From:eggs@example')
672 self.write_line(b'RCPT To:spam@example')
673 self.write_line(b'RCPT To:ham@example')
674 self.write_line(b'DATA')
675 self.write_line(b'data\r\n.')
Richard Jones6a9e6bb2010-08-04 12:27:36 +0000676 self.assertEqual(self.server.messages,
R David Murray2539e672014-08-09 16:40:49 -0400677 [(('peer-address', 'peer-port'),
678 'eggs@example',
679 ['spam@example','ham@example'],
680 'data')])
Richard Jones8cb36192010-07-23 16:20:40 +0000681
682 def test_manual_status(self):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000683 # checks that the Channel is able to return a custom status message
R David Murray669b7552012-03-20 16:16:29 -0400684 self.write_line(b'HELO example')
Richard Jones8cb36192010-07-23 16:20:40 +0000685 self.write_line(b'MAIL From:eggs@example')
686 self.write_line(b'RCPT To:spam@example')
687 self.write_line(b'DATA')
688 self.write_line(b'return status\r\n.')
Georg Brandl17e3d692010-07-31 10:08:09 +0000689 self.assertEqual(self.channel.socket.last, b'250 Okish\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000690
691 def test_RSET(self):
R David Murray669b7552012-03-20 16:16:29 -0400692 self.write_line(b'HELO example')
Richard Jones8cb36192010-07-23 16:20:40 +0000693 self.write_line(b'MAIL From:eggs@example')
694 self.write_line(b'RCPT To:spam@example')
695 self.write_line(b'RSET')
R David Murrayd1a30c92012-05-26 14:33:59 -0400696 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000697 self.write_line(b'MAIL From:foo@example')
698 self.write_line(b'RCPT To:eggs@example')
699 self.write_line(b'DATA')
700 self.write_line(b'data\r\n.')
Richard Jones6a9e6bb2010-08-04 12:27:36 +0000701 self.assertEqual(self.server.messages,
R David Murray2539e672014-08-09 16:40:49 -0400702 [(('peer-address', 'peer-port'),
703 'foo@example',
704 ['eggs@example'],
705 'data')])
Richard Jones8cb36192010-07-23 16:20:40 +0000706
R David Murray669b7552012-03-20 16:16:29 -0400707 def test_HELO_RSET(self):
708 self.write_line(b'HELO example')
709 self.write_line(b'RSET')
R David Murrayd1a30c92012-05-26 14:33:59 -0400710 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
R David Murray669b7552012-03-20 16:16:29 -0400711
Richard Jones8cb36192010-07-23 16:20:40 +0000712 def test_RSET_syntax(self):
713 self.write_line(b'RSET hi')
Georg Brandl17e3d692010-07-31 10:08:09 +0000714 self.assertEqual(self.channel.socket.last, b'501 Syntax: RSET\r\n')
Richard Jones8cb36192010-07-23 16:20:40 +0000715
R David Murrayd1a30c92012-05-26 14:33:59 -0400716 def test_unknown_command(self):
717 self.write_line(b'UNKNOWN_CMD')
718 self.assertEqual(self.channel.socket.last,
719 b'500 Error: command "UNKNOWN_CMD" not ' + \
720 b'recognized\r\n')
721
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000722 def test_attribute_deprecations(self):
Florent Xicluna67317752011-12-10 11:07:42 +0100723 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000724 spam = self.channel._SMTPChannel__server
Florent Xicluna67317752011-12-10 11:07:42 +0100725 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000726 self.channel._SMTPChannel__server = 'spam'
Florent Xicluna67317752011-12-10 11:07:42 +0100727 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000728 spam = self.channel._SMTPChannel__line
Florent Xicluna67317752011-12-10 11:07:42 +0100729 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000730 self.channel._SMTPChannel__line = 'spam'
Florent Xicluna67317752011-12-10 11:07:42 +0100731 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000732 spam = self.channel._SMTPChannel__state
Florent Xicluna67317752011-12-10 11:07:42 +0100733 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000734 self.channel._SMTPChannel__state = 'spam'
Florent Xicluna67317752011-12-10 11:07:42 +0100735 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000736 spam = self.channel._SMTPChannel__greeting
Florent Xicluna67317752011-12-10 11:07:42 +0100737 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000738 self.channel._SMTPChannel__greeting = 'spam'
Florent Xicluna67317752011-12-10 11:07:42 +0100739 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000740 spam = self.channel._SMTPChannel__mailfrom
Florent Xicluna67317752011-12-10 11:07:42 +0100741 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000742 self.channel._SMTPChannel__mailfrom = 'spam'
Florent Xicluna67317752011-12-10 11:07:42 +0100743 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000744 spam = self.channel._SMTPChannel__rcpttos
Florent Xicluna67317752011-12-10 11:07:42 +0100745 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000746 self.channel._SMTPChannel__rcpttos = 'spam'
Florent Xicluna67317752011-12-10 11:07:42 +0100747 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000748 spam = self.channel._SMTPChannel__data
Florent Xicluna67317752011-12-10 11:07:42 +0100749 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000750 self.channel._SMTPChannel__data = 'spam'
Florent Xicluna67317752011-12-10 11:07:42 +0100751 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000752 spam = self.channel._SMTPChannel__fqdn
Florent Xicluna67317752011-12-10 11:07:42 +0100753 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000754 self.channel._SMTPChannel__fqdn = 'spam'
Florent Xicluna67317752011-12-10 11:07:42 +0100755 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000756 spam = self.channel._SMTPChannel__peer
Florent Xicluna67317752011-12-10 11:07:42 +0100757 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000758 self.channel._SMTPChannel__peer = 'spam'
Florent Xicluna67317752011-12-10 11:07:42 +0100759 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000760 spam = self.channel._SMTPChannel__conn
Florent Xicluna67317752011-12-10 11:07:42 +0100761 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000762 self.channel._SMTPChannel__conn = 'spam'
Florent Xicluna67317752011-12-10 11:07:42 +0100763 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000764 spam = self.channel._SMTPChannel__addr
Florent Xicluna67317752011-12-10 11:07:42 +0100765 with support.check_warnings(('', DeprecationWarning)):
Richard Jones4aa0d4d2010-08-04 01:20:14 +0000766 self.channel._SMTPChannel__addr = 'spam'
Richard Jones8cb36192010-07-23 16:20:40 +0000767
R David Murray554bcbf2014-06-11 11:18:08 -0400768 def test_decode_data_default_warning(self):
R David Murray2539e672014-08-09 16:40:49 -0400769 with self.assertWarns(DeprecationWarning):
770 server = DummyServer((support.HOST, 0), ('b', 0))
R David Murray554bcbf2014-06-11 11:18:08 -0400771 conn, addr = self.server.accept()
772 with self.assertWarns(DeprecationWarning):
773 smtpd.SMTPChannel(server, conn, addr)
774
R David Murray6fe56a32014-06-11 13:48:58 -0400775@unittest.skipUnless(support.IPV6_ENABLED, "IPv6 not enabled")
776class SMTPDChannelIPv6Test(SMTPDChannelTest):
777 def setUp(self):
778 smtpd.socket = asyncore.socket = mock_socket
779 self.old_debugstream = smtpd.DEBUGSTREAM
780 self.debug = smtpd.DEBUGSTREAM = io.StringIO()
R David Murray2539e672014-08-09 16:40:49 -0400781 self.server = DummyServer((support.HOSTv6, 0), ('b', 0),
782 decode_data=True)
R David Murray6fe56a32014-06-11 13:48:58 -0400783 conn, addr = self.server.accept()
784 self.channel = smtpd.SMTPChannel(self.server, conn, addr,
785 decode_data=True)
R David Murrayd1a30c92012-05-26 14:33:59 -0400786
787class SMTPDChannelWithDataSizeLimitTest(unittest.TestCase):
788
789 def setUp(self):
790 smtpd.socket = asyncore.socket = mock_socket
R David Murray05cab752012-06-04 15:55:51 -0400791 self.old_debugstream = smtpd.DEBUGSTREAM
R David Murrayd1a30c92012-05-26 14:33:59 -0400792 self.debug = smtpd.DEBUGSTREAM = io.StringIO()
R David Murray2539e672014-08-09 16:40:49 -0400793 self.server = DummyServer((support.HOST, 0), ('b', 0),
794 decode_data=True)
R David Murrayd1a30c92012-05-26 14:33:59 -0400795 conn, addr = self.server.accept()
796 # Set DATA size limit to 32 bytes for easy testing
R David Murray554bcbf2014-06-11 11:18:08 -0400797 self.channel = smtpd.SMTPChannel(self.server, conn, addr, 32,
798 decode_data=True)
R David Murrayd1a30c92012-05-26 14:33:59 -0400799
800 def tearDown(self):
801 asyncore.close_all()
802 asyncore.socket = smtpd.socket = socket
R David Murray05cab752012-06-04 15:55:51 -0400803 smtpd.DEBUGSTREAM = self.old_debugstream
R David Murrayd1a30c92012-05-26 14:33:59 -0400804
805 def write_line(self, line):
806 self.channel.socket.queue_recv(line)
807 self.channel.handle_read()
808
809 def test_data_limit_dialog(self):
810 self.write_line(b'HELO example')
811 self.write_line(b'MAIL From:eggs@example')
812 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
813 self.write_line(b'RCPT To:spam@example')
814 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
815
816 self.write_line(b'DATA')
817 self.assertEqual(self.channel.socket.last,
818 b'354 End data with <CR><LF>.<CR><LF>\r\n')
819 self.write_line(b'data\r\nmore\r\n.')
820 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
821 self.assertEqual(self.server.messages,
R David Murray2539e672014-08-09 16:40:49 -0400822 [(('peer-address', 'peer-port'),
823 'eggs@example',
824 ['spam@example'],
825 'data\nmore')])
R David Murrayd1a30c92012-05-26 14:33:59 -0400826
827 def test_data_limit_dialog_too_much_data(self):
828 self.write_line(b'HELO example')
829 self.write_line(b'MAIL From:eggs@example')
830 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
831 self.write_line(b'RCPT To:spam@example')
832 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
833
834 self.write_line(b'DATA')
835 self.assertEqual(self.channel.socket.last,
836 b'354 End data with <CR><LF>.<CR><LF>\r\n')
837 self.write_line(b'This message is longer than 32 bytes\r\n.')
838 self.assertEqual(self.channel.socket.last,
839 b'552 Error: Too much mail data\r\n')
840
Richard Jones8cb36192010-07-23 16:20:40 +0000841
R David Murray554bcbf2014-06-11 11:18:08 -0400842class SMTPDChannelWithDecodeDataFalse(unittest.TestCase):
843
844 def setUp(self):
845 smtpd.socket = asyncore.socket = mock_socket
846 self.old_debugstream = smtpd.DEBUGSTREAM
847 self.debug = smtpd.DEBUGSTREAM = io.StringIO()
R David Murray6fe56a32014-06-11 13:48:58 -0400848 self.server = DummyServer((support.HOST, 0), ('b', 0),
849 decode_data=False)
R David Murray554bcbf2014-06-11 11:18:08 -0400850 conn, addr = self.server.accept()
851 # Set decode_data to False
852 self.channel = smtpd.SMTPChannel(self.server, conn, addr,
853 decode_data=False)
854
855 def tearDown(self):
856 asyncore.close_all()
857 asyncore.socket = smtpd.socket = socket
858 smtpd.DEBUGSTREAM = self.old_debugstream
859
860 def write_line(self, line):
861 self.channel.socket.queue_recv(line)
862 self.channel.handle_read()
863
864 def test_ascii_data(self):
865 self.write_line(b'HELO example')
866 self.write_line(b'MAIL From:eggs@example')
867 self.write_line(b'RCPT To:spam@example')
868 self.write_line(b'DATA')
869 self.write_line(b'plain ascii text')
870 self.write_line(b'.')
871 self.assertEqual(self.channel.received_data, b'plain ascii text')
872
873 def test_utf8_data(self):
874 self.write_line(b'HELO example')
875 self.write_line(b'MAIL From:eggs@example')
876 self.write_line(b'RCPT To:spam@example')
877 self.write_line(b'DATA')
878 self.write_line(b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87')
879 self.write_line(b'and some plain ascii')
880 self.write_line(b'.')
881 self.assertEqual(
882 self.channel.received_data,
883 b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87\n'
884 b'and some plain ascii')
885
886
887class SMTPDChannelWithDecodeDataTrue(unittest.TestCase):
888
889 def setUp(self):
890 smtpd.socket = asyncore.socket = mock_socket
891 self.old_debugstream = smtpd.DEBUGSTREAM
892 self.debug = smtpd.DEBUGSTREAM = io.StringIO()
R David Murray6fe56a32014-06-11 13:48:58 -0400893 self.server = DummyServer((support.HOST, 0), ('b', 0),
894 decode_data=True)
R David Murray554bcbf2014-06-11 11:18:08 -0400895 conn, addr = self.server.accept()
896 # Set decode_data to True
897 self.channel = smtpd.SMTPChannel(self.server, conn, addr,
898 decode_data=True)
899
900 def tearDown(self):
901 asyncore.close_all()
902 asyncore.socket = smtpd.socket = socket
903 smtpd.DEBUGSTREAM = self.old_debugstream
904
905 def write_line(self, line):
906 self.channel.socket.queue_recv(line)
907 self.channel.handle_read()
908
909 def test_ascii_data(self):
910 self.write_line(b'HELO example')
911 self.write_line(b'MAIL From:eggs@example')
912 self.write_line(b'RCPT To:spam@example')
913 self.write_line(b'DATA')
914 self.write_line(b'plain ascii text')
915 self.write_line(b'.')
916 self.assertEqual(self.channel.received_data, 'plain ascii text')
917
918 def test_utf8_data(self):
919 self.write_line(b'HELO example')
920 self.write_line(b'MAIL From:eggs@example')
921 self.write_line(b'RCPT To:spam@example')
922 self.write_line(b'DATA')
923 self.write_line(b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87')
924 self.write_line(b'and some plain ascii')
925 self.write_line(b'.')
926 self.assertEqual(
927 self.channel.received_data,
928 'utf8 enriched text: żźć\nand some plain ascii')
929
930
R David Murray2539e672014-08-09 16:40:49 -0400931class SMTPDChannelTestWithEnableSMTPUTF8True(unittest.TestCase):
932 def setUp(self):
933 smtpd.socket = asyncore.socket = mock_socket
934 self.old_debugstream = smtpd.DEBUGSTREAM
935 self.debug = smtpd.DEBUGSTREAM = io.StringIO()
936 self.server = DummyServer((support.HOST, 0), ('b', 0),
937 enable_SMTPUTF8=True)
938 conn, addr = self.server.accept()
939 self.channel = smtpd.SMTPChannel(self.server, conn, addr,
940 enable_SMTPUTF8=True)
941
942 def tearDown(self):
943 asyncore.close_all()
944 asyncore.socket = smtpd.socket = socket
945 smtpd.DEBUGSTREAM = self.old_debugstream
946
947 def write_line(self, line):
948 self.channel.socket.queue_recv(line)
949 self.channel.handle_read()
950
951 def test_MAIL_command_accepts_SMTPUTF8_when_announced(self):
952 self.write_line(b'EHLO example')
953 self.write_line(
954 'MAIL from: <naïve@example.com> BODY=8BITMIME SMTPUTF8'.encode(
955 'utf-8')
956 )
957 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
958
959 def test_process_smtputf8_message(self):
960 self.write_line(b'EHLO example')
961 for mail_parameters in [b'', b'BODY=8BITMIME SMTPUTF8']:
962 self.write_line(b'MAIL from: <a@example> ' + mail_parameters)
963 self.assertEqual(self.channel.socket.last[0:3], b'250')
964 self.write_line(b'rcpt to:<b@example.com>')
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'c\r\n.')
969 if mail_parameters == b'':
970 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
971 else:
972 self.assertEqual(self.channel.socket.last,
973 b'250 SMTPUTF8 message okish\r\n')
974
975 def test_utf8_data(self):
976 self.write_line(b'EHLO example')
977 self.write_line(
978 'MAIL From: naïve@examplé BODY=8BITMIME SMTPUTF8'.encode('utf-8'))
979 self.assertEqual(self.channel.socket.last[0:3], b'250')
980 self.write_line('RCPT To:späm@examplé'.encode('utf-8'))
981 self.assertEqual(self.channel.socket.last[0:3], b'250')
982 self.write_line(b'DATA')
983 self.assertEqual(self.channel.socket.last[0:3], b'354')
984 self.write_line(b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87')
985 self.write_line(b'.')
986 self.assertEqual(
987 self.channel.received_data,
988 b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87')
989
990 def test_MAIL_command_limit_extended_with_SIZE_and_SMTPUTF8(self):
991 self.write_line(b'ehlo example')
992 fill_len = (512 + 26 + 10) - len('mail from:<@example>')
993 self.write_line(b'MAIL from:<' +
994 b'a' * (fill_len + 1) +
995 b'@example>')
996 self.assertEqual(self.channel.socket.last,
997 b'500 Error: line too long\r\n')
998 self.write_line(b'MAIL from:<' +
999 b'a' * fill_len +
1000 b'@example>')
1001 self.assertEqual(self.channel.socket.last, b'250 OK\r\n')
1002
1003 def test_multiple_emails_with_extended_command_length(self):
1004 self.write_line(b'ehlo example')
1005 fill_len = (512 + 26 + 10) - len('mail from:<@example>')
1006 for char in [b'a', b'b', b'c']:
1007 self.write_line(b'MAIL from:<' + char * fill_len + b'a@example>')
1008 self.assertEqual(self.channel.socket.last[0:3], b'500')
1009 self.write_line(b'MAIL from:<' + char * fill_len + b'@example>')
1010 self.assertEqual(self.channel.socket.last[0:3], b'250')
1011 self.write_line(b'rcpt to:<hans@example.com>')
1012 self.assertEqual(self.channel.socket.last[0:3], b'250')
1013 self.write_line(b'data')
1014 self.assertEqual(self.channel.socket.last[0:3], b'354')
1015 self.write_line(b'test\r\n.')
1016 self.assertEqual(self.channel.socket.last[0:3], b'250')
1017
Richard Jones8cb36192010-07-23 16:20:40 +00001018if __name__ == "__main__":
R David Murrayd1a30c92012-05-26 14:33:59 -04001019 unittest.main()