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