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