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