R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 1 | import unittest |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 2 | from test import support, mock_socket |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 3 | import socket |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 4 | import io |
| 5 | import smtpd |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 6 | import asyncore |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 7 | |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 8 | |
| 9 | class DummyServer(smtpd.SMTPServer): |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 10 | def __init__(self, localaddr, remoteaddr, decode_data=True): |
| 11 | smtpd.SMTPServer.__init__(self, localaddr, remoteaddr, |
| 12 | decode_data=decode_data) |
Georg Brandl | 6d23c44 | 2010-07-29 13:19:42 +0000 | [diff] [blame] | 13 | self.messages = [] |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 14 | if decode_data: |
| 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 | |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 19 | def process_message(self, peer, mailfrom, rcpttos, data): |
| 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' |
| 23 | |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 24 | |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 25 | class DummyDispatcherBroken(Exception): |
| 26 | pass |
| 27 | |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 28 | |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 29 | class BrokenDummyServer(DummyServer): |
| 30 | def listen(self, num): |
| 31 | raise DummyDispatcherBroken() |
| 32 | |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 33 | |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 34 | class SMTPDServerTest(unittest.TestCase): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 35 | def setUp(self): |
| 36 | smtpd.socket = asyncore.socket = mock_socket |
| 37 | |
| 38 | def test_process_message_unimplemented(self): |
R David Murray | 6fe56a3 | 2014-06-11 13:48:58 -0400 | [diff] [blame] | 39 | server = smtpd.SMTPServer((support.HOST, 0), ('b', 0), |
| 40 | decode_data=True) |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 41 | conn, addr = server.accept() |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 42 | channel = smtpd.SMTPChannel(server, conn, addr, decode_data=True) |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 43 | |
| 44 | def write_line(line): |
| 45 | channel.socket.queue_recv(line) |
| 46 | channel.handle_read() |
| 47 | |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 48 | write_line(b'HELO example') |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 49 | write_line(b'MAIL From:eggs@example') |
| 50 | write_line(b'RCPT To:spam@example') |
| 51 | write_line(b'DATA') |
| 52 | self.assertRaises(NotImplementedError, write_line, b'spam\r\n.\r\n') |
| 53 | |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 54 | def test_decode_data_default_warns(self): |
| 55 | with self.assertWarns(DeprecationWarning): |
R David Murray | 6fe56a3 | 2014-06-11 13:48:58 -0400 | [diff] [blame] | 56 | smtpd.SMTPServer((support.HOST, 0), ('b', 0)) |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 57 | |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 58 | def tearDown(self): |
Richard Jones | daf2350 | 2010-08-16 01:48:14 +0000 | [diff] [blame] | 59 | asyncore.close_all() |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 60 | asyncore.socket = smtpd.socket = socket |
| 61 | |
| 62 | |
R David Murray | 6fe56a3 | 2014-06-11 13:48:58 -0400 | [diff] [blame] | 63 | class TestFamilyDetection(unittest.TestCase): |
| 64 | def setUp(self): |
| 65 | smtpd.socket = asyncore.socket = mock_socket |
| 66 | |
| 67 | def tearDown(self): |
| 68 | asyncore.close_all() |
| 69 | asyncore.socket = smtpd.socket = socket |
| 70 | |
| 71 | @unittest.skipUnless(support.IPV6_ENABLED, "IPv6 not enabled") |
| 72 | def test_socket_uses_IPv6(self): |
| 73 | server = smtpd.SMTPServer((support.HOSTv6, 0), (support.HOST, 0), |
| 74 | decode_data=False) |
| 75 | self.assertEqual(server.socket.family, socket.AF_INET6) |
| 76 | |
| 77 | def test_socket_uses_IPv4(self): |
| 78 | server = smtpd.SMTPServer((support.HOST, 0), (support.HOSTv6, 0), |
| 79 | decode_data=False) |
| 80 | self.assertEqual(server.socket.family, socket.AF_INET) |
| 81 | |
| 82 | |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 83 | class SMTPDChannelTest(unittest.TestCase): |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 84 | def setUp(self): |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 85 | smtpd.socket = asyncore.socket = mock_socket |
Antoine Pitrou | e0815e2 | 2011-11-12 20:36:29 +0100 | [diff] [blame] | 86 | self.old_debugstream = smtpd.DEBUGSTREAM |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 87 | self.debug = smtpd.DEBUGSTREAM = io.StringIO() |
R David Murray | 6fe56a3 | 2014-06-11 13:48:58 -0400 | [diff] [blame] | 88 | self.server = DummyServer((support.HOST, 0), ('b', 0)) |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 89 | conn, addr = self.server.accept() |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 90 | self.channel = smtpd.SMTPChannel(self.server, conn, addr, |
| 91 | decode_data=True) |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 92 | |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 93 | def tearDown(self): |
Richard Jones | daf2350 | 2010-08-16 01:48:14 +0000 | [diff] [blame] | 94 | asyncore.close_all() |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 95 | asyncore.socket = smtpd.socket = socket |
Antoine Pitrou | e0815e2 | 2011-11-12 20:36:29 +0100 | [diff] [blame] | 96 | smtpd.DEBUGSTREAM = self.old_debugstream |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 97 | |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 98 | def write_line(self, line): |
| 99 | self.channel.socket.queue_recv(line) |
| 100 | self.channel.handle_read() |
| 101 | |
| 102 | def test_broken_connect(self): |
R David Murray | 6fe56a3 | 2014-06-11 13:48:58 -0400 | [diff] [blame] | 103 | self.assertRaises( |
| 104 | DummyDispatcherBroken, BrokenDummyServer, |
| 105 | (support.HOST, 0), ('b', 0)) |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 106 | |
| 107 | def test_server_accept(self): |
| 108 | self.server.handle_accept() |
| 109 | |
| 110 | def test_missing_data(self): |
| 111 | self.write_line(b'') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 112 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 113 | b'500 Error: bad syntax\r\n') |
| 114 | |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 115 | def test_EHLO(self): |
| 116 | self.write_line(b'EHLO example') |
| 117 | self.assertEqual(self.channel.socket.last, b'250 HELP\r\n') |
| 118 | |
| 119 | def test_EHLO_bad_syntax(self): |
| 120 | self.write_line(b'EHLO') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 121 | self.assertEqual(self.channel.socket.last, |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 122 | b'501 Syntax: EHLO hostname\r\n') |
| 123 | |
| 124 | def test_EHLO_duplicate(self): |
| 125 | self.write_line(b'EHLO example') |
| 126 | self.write_line(b'EHLO example') |
| 127 | self.assertEqual(self.channel.socket.last, |
| 128 | b'503 Duplicate HELO/EHLO\r\n') |
| 129 | |
| 130 | def test_EHLO_HELO_duplicate(self): |
| 131 | self.write_line(b'EHLO example') |
| 132 | self.write_line(b'HELO example') |
| 133 | self.assertEqual(self.channel.socket.last, |
| 134 | b'503 Duplicate HELO/EHLO\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 135 | |
| 136 | def test_HELO(self): |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 137 | name = smtpd.socket.getfqdn() |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 138 | self.write_line(b'HELO example') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 139 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 140 | '250 {}\r\n'.format(name).encode('ascii')) |
| 141 | |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 142 | def test_HELO_EHLO_duplicate(self): |
| 143 | self.write_line(b'HELO example') |
| 144 | self.write_line(b'EHLO example') |
| 145 | self.assertEqual(self.channel.socket.last, |
| 146 | b'503 Duplicate HELO/EHLO\r\n') |
| 147 | |
| 148 | def test_HELP(self): |
| 149 | self.write_line(b'HELP') |
| 150 | self.assertEqual(self.channel.socket.last, |
| 151 | b'250 Supported commands: EHLO HELO MAIL RCPT ' + \ |
| 152 | b'DATA RSET NOOP QUIT VRFY\r\n') |
| 153 | |
| 154 | def test_HELP_command(self): |
| 155 | self.write_line(b'HELP MAIL') |
| 156 | self.assertEqual(self.channel.socket.last, |
| 157 | b'250 Syntax: MAIL FROM: <address>\r\n') |
| 158 | |
| 159 | def test_HELP_command_unknown(self): |
| 160 | self.write_line(b'HELP SPAM') |
| 161 | self.assertEqual(self.channel.socket.last, |
| 162 | b'501 Supported commands: EHLO HELO MAIL RCPT ' + \ |
| 163 | b'DATA RSET NOOP QUIT VRFY\r\n') |
| 164 | |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 165 | def test_HELO_bad_syntax(self): |
| 166 | self.write_line(b'HELO') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 167 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 168 | b'501 Syntax: HELO hostname\r\n') |
| 169 | |
| 170 | def test_HELO_duplicate(self): |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 171 | self.write_line(b'HELO example') |
| 172 | self.write_line(b'HELO example') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 173 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 174 | b'503 Duplicate HELO/EHLO\r\n') |
| 175 | |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 176 | def test_HELO_parameter_rejected_when_extensions_not_enabled(self): |
| 177 | self.extended_smtp = False |
| 178 | self.write_line(b'HELO example') |
| 179 | self.write_line(b'MAIL from:<foo@example.com> SIZE=1234') |
| 180 | self.assertEqual(self.channel.socket.last, |
| 181 | b'501 Syntax: MAIL FROM: <address>\r\n') |
| 182 | |
| 183 | def test_MAIL_allows_space_after_colon(self): |
| 184 | self.write_line(b'HELO example') |
| 185 | self.write_line(b'MAIL from: <foo@example.com>') |
| 186 | self.assertEqual(self.channel.socket.last, |
| 187 | b'250 OK\r\n') |
| 188 | |
| 189 | def test_extended_MAIL_allows_space_after_colon(self): |
| 190 | self.write_line(b'EHLO example') |
| 191 | self.write_line(b'MAIL from: <foo@example.com> size=20') |
| 192 | self.assertEqual(self.channel.socket.last, |
| 193 | b'250 OK\r\n') |
| 194 | |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 195 | def test_NOOP(self): |
| 196 | self.write_line(b'NOOP') |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 197 | self.assertEqual(self.channel.socket.last, b'250 OK\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 198 | |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 199 | def test_HELO_NOOP(self): |
| 200 | self.write_line(b'HELO example') |
| 201 | self.write_line(b'NOOP') |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 202 | self.assertEqual(self.channel.socket.last, b'250 OK\r\n') |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 203 | |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 204 | def test_NOOP_bad_syntax(self): |
| 205 | self.write_line(b'NOOP hi') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 206 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 207 | b'501 Syntax: NOOP\r\n') |
| 208 | |
| 209 | def test_QUIT(self): |
| 210 | self.write_line(b'QUIT') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 211 | self.assertEqual(self.channel.socket.last, b'221 Bye\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 212 | |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 213 | def test_HELO_QUIT(self): |
| 214 | self.write_line(b'HELO example') |
| 215 | self.write_line(b'QUIT') |
| 216 | self.assertEqual(self.channel.socket.last, b'221 Bye\r\n') |
| 217 | |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 218 | def test_QUIT_arg_ignored(self): |
| 219 | self.write_line(b'QUIT bye bye') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 220 | self.assertEqual(self.channel.socket.last, b'221 Bye\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 221 | |
| 222 | def test_bad_state(self): |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 223 | self.channel.smtp_state = 'BAD STATE' |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 224 | self.write_line(b'HELO example') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 225 | self.assertEqual(self.channel.socket.last, |
| 226 | b'451 Internal confusion\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 227 | |
Georg Brandl | 1e5c5f8 | 2010-12-03 07:38:22 +0000 | [diff] [blame] | 228 | def test_command_too_long(self): |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 229 | self.write_line(b'HELO example') |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 230 | self.write_line(b'MAIL from: ' + |
Georg Brandl | 1e5c5f8 | 2010-12-03 07:38:22 +0000 | [diff] [blame] | 231 | b'a' * self.channel.command_size_limit + |
| 232 | b'@example') |
| 233 | self.assertEqual(self.channel.socket.last, |
| 234 | b'500 Error: line too long\r\n') |
| 235 | |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 236 | def test_MAIL_command_limit_extended_with_SIZE(self): |
| 237 | self.write_line(b'EHLO example') |
| 238 | fill_len = self.channel.command_size_limit - len('MAIL from:<@example>') |
| 239 | self.write_line(b'MAIL from:<' + |
| 240 | b'a' * fill_len + |
| 241 | b'@example> SIZE=1234') |
| 242 | self.assertEqual(self.channel.socket.last, b'250 OK\r\n') |
| 243 | |
| 244 | self.write_line(b'MAIL from:<' + |
| 245 | b'a' * (fill_len + 26) + |
| 246 | b'@example> SIZE=1234') |
| 247 | self.assertEqual(self.channel.socket.last, |
| 248 | b'500 Error: line too long\r\n') |
| 249 | |
| 250 | def test_data_longer_than_default_data_size_limit(self): |
| 251 | # Hack the default so we don't have to generate so much data. |
| 252 | self.channel.data_size_limit = 1048 |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 253 | self.write_line(b'HELO example') |
Georg Brandl | 1e5c5f8 | 2010-12-03 07:38:22 +0000 | [diff] [blame] | 254 | self.write_line(b'MAIL From:eggs@example') |
| 255 | self.write_line(b'RCPT To:spam@example') |
| 256 | self.write_line(b'DATA') |
| 257 | self.write_line(b'A' * self.channel.data_size_limit + |
| 258 | b'A\r\n.') |
| 259 | self.assertEqual(self.channel.socket.last, |
| 260 | b'552 Error: Too much mail data\r\n') |
| 261 | |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 262 | def test_MAIL_size_parameter(self): |
| 263 | self.write_line(b'EHLO example') |
| 264 | self.write_line(b'MAIL FROM:<eggs@example> SIZE=512') |
| 265 | self.assertEqual(self.channel.socket.last, |
| 266 | b'250 OK\r\n') |
| 267 | |
| 268 | def test_MAIL_invalid_size_parameter(self): |
| 269 | self.write_line(b'EHLO example') |
| 270 | self.write_line(b'MAIL FROM:<eggs@example> SIZE=invalid') |
| 271 | self.assertEqual(self.channel.socket.last, |
| 272 | b'501 Syntax: MAIL FROM: <address> [SP <mail-parameters>]\r\n') |
| 273 | |
| 274 | def test_MAIL_RCPT_unknown_parameters(self): |
| 275 | self.write_line(b'EHLO example') |
| 276 | self.write_line(b'MAIL FROM:<eggs@example> ham=green') |
| 277 | self.assertEqual(self.channel.socket.last, |
| 278 | b'555 MAIL FROM parameters not recognized or not implemented\r\n') |
| 279 | |
| 280 | self.write_line(b'MAIL FROM:<eggs@example>') |
| 281 | self.write_line(b'RCPT TO:<eggs@example> ham=green') |
| 282 | self.assertEqual(self.channel.socket.last, |
| 283 | b'555 RCPT TO parameters not recognized or not implemented\r\n') |
| 284 | |
| 285 | def test_MAIL_size_parameter_larger_than_default_data_size_limit(self): |
| 286 | self.channel.data_size_limit = 1048 |
| 287 | self.write_line(b'EHLO example') |
| 288 | self.write_line(b'MAIL FROM:<eggs@example> SIZE=2096') |
| 289 | self.assertEqual(self.channel.socket.last, |
| 290 | b'552 Error: message size exceeds fixed maximum message size\r\n') |
| 291 | |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 292 | def test_need_MAIL(self): |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 293 | self.write_line(b'HELO example') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 294 | self.write_line(b'RCPT to:spam@example') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 295 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 296 | b'503 Error: need MAIL command\r\n') |
| 297 | |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 298 | def test_MAIL_syntax_HELO(self): |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 299 | self.write_line(b'HELO example') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 300 | self.write_line(b'MAIL from eggs@example') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 301 | self.assertEqual(self.channel.socket.last, |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 302 | b'501 Syntax: MAIL FROM: <address>\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 303 | |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 304 | def test_MAIL_syntax_EHLO(self): |
| 305 | self.write_line(b'EHLO example') |
| 306 | self.write_line(b'MAIL from eggs@example') |
| 307 | self.assertEqual(self.channel.socket.last, |
| 308 | b'501 Syntax: MAIL FROM: <address> [SP <mail-parameters>]\r\n') |
| 309 | |
| 310 | def test_MAIL_missing_address(self): |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 311 | self.write_line(b'HELO example') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 312 | self.write_line(b'MAIL from:') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 313 | self.assertEqual(self.channel.socket.last, |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 314 | b'501 Syntax: MAIL FROM: <address>\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 315 | |
| 316 | def test_MAIL_chevrons(self): |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 317 | self.write_line(b'HELO example') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 318 | self.write_line(b'MAIL from:<eggs@example>') |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 319 | self.assertEqual(self.channel.socket.last, b'250 OK\r\n') |
| 320 | |
| 321 | def test_MAIL_empty_chevrons(self): |
| 322 | self.write_line(b'EHLO example') |
| 323 | self.write_line(b'MAIL from:<>') |
| 324 | self.assertEqual(self.channel.socket.last, b'250 OK\r\n') |
| 325 | |
| 326 | def test_MAIL_quoted_localpart(self): |
| 327 | self.write_line(b'EHLO example') |
| 328 | self.write_line(b'MAIL from: <"Fred Blogs"@example.com>') |
| 329 | self.assertEqual(self.channel.socket.last, b'250 OK\r\n') |
| 330 | self.assertEqual(self.channel.mailfrom, '"Fred Blogs"@example.com') |
| 331 | |
| 332 | def test_MAIL_quoted_localpart_no_angles(self): |
| 333 | self.write_line(b'EHLO example') |
| 334 | self.write_line(b'MAIL from: "Fred Blogs"@example.com') |
| 335 | self.assertEqual(self.channel.socket.last, b'250 OK\r\n') |
| 336 | self.assertEqual(self.channel.mailfrom, '"Fred Blogs"@example.com') |
| 337 | |
| 338 | def test_MAIL_quoted_localpart_with_size(self): |
| 339 | self.write_line(b'EHLO example') |
| 340 | self.write_line(b'MAIL from: <"Fred Blogs"@example.com> SIZE=1000') |
| 341 | self.assertEqual(self.channel.socket.last, b'250 OK\r\n') |
| 342 | self.assertEqual(self.channel.mailfrom, '"Fred Blogs"@example.com') |
| 343 | |
| 344 | def test_MAIL_quoted_localpart_with_size_no_angles(self): |
| 345 | self.write_line(b'EHLO example') |
| 346 | self.write_line(b'MAIL from: "Fred Blogs"@example.com SIZE=1000') |
| 347 | self.assertEqual(self.channel.socket.last, b'250 OK\r\n') |
| 348 | self.assertEqual(self.channel.mailfrom, '"Fred Blogs"@example.com') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 349 | |
| 350 | def test_nested_MAIL(self): |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 351 | self.write_line(b'HELO example') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 352 | self.write_line(b'MAIL from:eggs@example') |
| 353 | self.write_line(b'MAIL from:spam@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 | b'503 Error: nested MAIL command\r\n') |
| 356 | |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 357 | def test_VRFY(self): |
| 358 | self.write_line(b'VRFY eggs@example') |
| 359 | self.assertEqual(self.channel.socket.last, |
| 360 | b'252 Cannot VRFY user, but will accept message and attempt ' + \ |
| 361 | b'delivery\r\n') |
| 362 | |
| 363 | def test_VRFY_syntax(self): |
| 364 | self.write_line(b'VRFY') |
| 365 | self.assertEqual(self.channel.socket.last, |
| 366 | b'501 Syntax: VRFY <address>\r\n') |
| 367 | |
| 368 | def test_EXPN_not_implemented(self): |
| 369 | self.write_line(b'EXPN') |
| 370 | self.assertEqual(self.channel.socket.last, |
| 371 | b'502 EXPN not implemented\r\n') |
| 372 | |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 373 | def test_no_HELO_MAIL(self): |
| 374 | self.write_line(b'MAIL from:<foo@example.com>') |
| 375 | self.assertEqual(self.channel.socket.last, |
| 376 | b'503 Error: send HELO first\r\n') |
| 377 | |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 378 | def test_need_RCPT(self): |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 379 | self.write_line(b'HELO example') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 380 | self.write_line(b'MAIL From:eggs@example') |
| 381 | self.write_line(b'DATA') |
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 Error: need RCPT command\r\n') |
| 384 | |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 385 | def test_RCPT_syntax_HELO(self): |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 386 | self.write_line(b'HELO example') |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 387 | self.write_line(b'MAIL From: eggs@example') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 388 | self.write_line(b'RCPT to eggs@example') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 389 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 390 | b'501 Syntax: RCPT TO: <address>\r\n') |
| 391 | |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 392 | def test_RCPT_syntax_EHLO(self): |
| 393 | self.write_line(b'EHLO example') |
| 394 | self.write_line(b'MAIL From: eggs@example') |
| 395 | self.write_line(b'RCPT to eggs@example') |
| 396 | self.assertEqual(self.channel.socket.last, |
| 397 | b'501 Syntax: RCPT TO: <address> [SP <mail-parameters>]\r\n') |
| 398 | |
| 399 | def test_RCPT_lowercase_to_OK(self): |
| 400 | self.write_line(b'HELO example') |
| 401 | self.write_line(b'MAIL From: eggs@example') |
| 402 | self.write_line(b'RCPT to: <eggs@example>') |
| 403 | self.assertEqual(self.channel.socket.last, b'250 OK\r\n') |
| 404 | |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 405 | def test_no_HELO_RCPT(self): |
| 406 | self.write_line(b'RCPT to eggs@example') |
| 407 | self.assertEqual(self.channel.socket.last, |
| 408 | b'503 Error: send HELO first\r\n') |
| 409 | |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 410 | def test_data_dialog(self): |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 411 | self.write_line(b'HELO example') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 412 | self.write_line(b'MAIL From:eggs@example') |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 413 | self.assertEqual(self.channel.socket.last, b'250 OK\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 414 | self.write_line(b'RCPT To:spam@example') |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 415 | self.assertEqual(self.channel.socket.last, b'250 OK\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 416 | |
| 417 | self.write_line(b'DATA') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 418 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 419 | b'354 End data with <CR><LF>.<CR><LF>\r\n') |
| 420 | self.write_line(b'data\r\nmore\r\n.') |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 421 | self.assertEqual(self.channel.socket.last, b'250 OK\r\n') |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 422 | self.assertEqual(self.server.messages, |
| 423 | [('peer', 'eggs@example', ['spam@example'], 'data\nmore')]) |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 424 | |
| 425 | def test_DATA_syntax(self): |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 426 | self.write_line(b'HELO example') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 427 | self.write_line(b'MAIL From:eggs@example') |
| 428 | self.write_line(b'RCPT To:spam@example') |
| 429 | self.write_line(b'DATA spam') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 430 | self.assertEqual(self.channel.socket.last, b'501 Syntax: DATA\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 431 | |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 432 | def test_no_HELO_DATA(self): |
| 433 | self.write_line(b'DATA spam') |
| 434 | self.assertEqual(self.channel.socket.last, |
| 435 | b'503 Error: send HELO first\r\n') |
| 436 | |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 437 | def test_data_transparency_section_4_5_2(self): |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 438 | self.write_line(b'HELO example') |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 439 | self.write_line(b'MAIL From:eggs@example') |
| 440 | self.write_line(b'RCPT To:spam@example') |
| 441 | self.write_line(b'DATA') |
| 442 | self.write_line(b'..\r\n.\r\n') |
| 443 | self.assertEqual(self.channel.received_data, '.') |
| 444 | |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 445 | def test_multiple_RCPT(self): |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 446 | self.write_line(b'HELO example') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 447 | self.write_line(b'MAIL From:eggs@example') |
| 448 | self.write_line(b'RCPT To:spam@example') |
| 449 | self.write_line(b'RCPT To:ham@example') |
| 450 | self.write_line(b'DATA') |
| 451 | self.write_line(b'data\r\n.') |
Richard Jones | 6a9e6bb | 2010-08-04 12:27:36 +0000 | [diff] [blame] | 452 | self.assertEqual(self.server.messages, |
| 453 | [('peer', 'eggs@example', ['spam@example','ham@example'], 'data')]) |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 454 | |
| 455 | def test_manual_status(self): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 456 | # 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] | 457 | self.write_line(b'HELO example') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 458 | self.write_line(b'MAIL From:eggs@example') |
| 459 | self.write_line(b'RCPT To:spam@example') |
| 460 | self.write_line(b'DATA') |
| 461 | self.write_line(b'return status\r\n.') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 462 | self.assertEqual(self.channel.socket.last, b'250 Okish\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 463 | |
| 464 | def test_RSET(self): |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 465 | self.write_line(b'HELO example') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 466 | self.write_line(b'MAIL From:eggs@example') |
| 467 | self.write_line(b'RCPT To:spam@example') |
| 468 | self.write_line(b'RSET') |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 469 | self.assertEqual(self.channel.socket.last, b'250 OK\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 470 | self.write_line(b'MAIL From:foo@example') |
| 471 | self.write_line(b'RCPT To:eggs@example') |
| 472 | self.write_line(b'DATA') |
| 473 | self.write_line(b'data\r\n.') |
Richard Jones | 6a9e6bb | 2010-08-04 12:27:36 +0000 | [diff] [blame] | 474 | self.assertEqual(self.server.messages, |
| 475 | [('peer', 'foo@example', ['eggs@example'], 'data')]) |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 476 | |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 477 | def test_HELO_RSET(self): |
| 478 | self.write_line(b'HELO example') |
| 479 | self.write_line(b'RSET') |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 480 | self.assertEqual(self.channel.socket.last, b'250 OK\r\n') |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 481 | |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 482 | def test_RSET_syntax(self): |
| 483 | self.write_line(b'RSET hi') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 484 | self.assertEqual(self.channel.socket.last, b'501 Syntax: RSET\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 485 | |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 486 | def test_unknown_command(self): |
| 487 | self.write_line(b'UNKNOWN_CMD') |
| 488 | self.assertEqual(self.channel.socket.last, |
| 489 | b'500 Error: command "UNKNOWN_CMD" not ' + \ |
| 490 | b'recognized\r\n') |
| 491 | |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 492 | def test_attribute_deprecations(self): |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 493 | with support.check_warnings(('', DeprecationWarning)): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 494 | spam = self.channel._SMTPChannel__server |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 495 | with support.check_warnings(('', DeprecationWarning)): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 496 | self.channel._SMTPChannel__server = 'spam' |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 497 | with support.check_warnings(('', DeprecationWarning)): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 498 | spam = self.channel._SMTPChannel__line |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 499 | with support.check_warnings(('', DeprecationWarning)): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 500 | self.channel._SMTPChannel__line = 'spam' |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 501 | with support.check_warnings(('', DeprecationWarning)): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 502 | spam = self.channel._SMTPChannel__state |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 503 | with support.check_warnings(('', DeprecationWarning)): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 504 | self.channel._SMTPChannel__state = 'spam' |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 505 | with support.check_warnings(('', DeprecationWarning)): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 506 | spam = self.channel._SMTPChannel__greeting |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 507 | with support.check_warnings(('', DeprecationWarning)): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 508 | self.channel._SMTPChannel__greeting = 'spam' |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 509 | with support.check_warnings(('', DeprecationWarning)): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 510 | spam = self.channel._SMTPChannel__mailfrom |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 511 | with support.check_warnings(('', DeprecationWarning)): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 512 | self.channel._SMTPChannel__mailfrom = 'spam' |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 513 | with support.check_warnings(('', DeprecationWarning)): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 514 | spam = self.channel._SMTPChannel__rcpttos |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 515 | with support.check_warnings(('', DeprecationWarning)): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 516 | self.channel._SMTPChannel__rcpttos = 'spam' |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 517 | with support.check_warnings(('', DeprecationWarning)): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 518 | spam = self.channel._SMTPChannel__data |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 519 | with support.check_warnings(('', DeprecationWarning)): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 520 | self.channel._SMTPChannel__data = 'spam' |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 521 | with support.check_warnings(('', DeprecationWarning)): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 522 | spam = self.channel._SMTPChannel__fqdn |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 523 | with support.check_warnings(('', DeprecationWarning)): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 524 | self.channel._SMTPChannel__fqdn = 'spam' |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 525 | with support.check_warnings(('', DeprecationWarning)): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 526 | spam = self.channel._SMTPChannel__peer |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 527 | with support.check_warnings(('', DeprecationWarning)): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 528 | self.channel._SMTPChannel__peer = 'spam' |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 529 | with support.check_warnings(('', DeprecationWarning)): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 530 | spam = self.channel._SMTPChannel__conn |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 531 | with support.check_warnings(('', DeprecationWarning)): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 532 | self.channel._SMTPChannel__conn = 'spam' |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 533 | with support.check_warnings(('', DeprecationWarning)): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 534 | spam = self.channel._SMTPChannel__addr |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 535 | with support.check_warnings(('', DeprecationWarning)): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 536 | self.channel._SMTPChannel__addr = 'spam' |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 537 | |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 538 | def test_decode_data_default_warning(self): |
R David Murray | 6fe56a3 | 2014-06-11 13:48:58 -0400 | [diff] [blame] | 539 | server = DummyServer((support.HOST, 0), ('b', 0)) |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 540 | conn, addr = self.server.accept() |
| 541 | with self.assertWarns(DeprecationWarning): |
| 542 | smtpd.SMTPChannel(server, conn, addr) |
| 543 | |
R David Murray | 6fe56a3 | 2014-06-11 13:48:58 -0400 | [diff] [blame] | 544 | @unittest.skipUnless(support.IPV6_ENABLED, "IPv6 not enabled") |
| 545 | class SMTPDChannelIPv6Test(SMTPDChannelTest): |
| 546 | def setUp(self): |
| 547 | smtpd.socket = asyncore.socket = mock_socket |
| 548 | self.old_debugstream = smtpd.DEBUGSTREAM |
| 549 | self.debug = smtpd.DEBUGSTREAM = io.StringIO() |
| 550 | self.server = DummyServer((support.HOSTv6, 0), ('b', 0)) |
| 551 | conn, addr = self.server.accept() |
| 552 | self.channel = smtpd.SMTPChannel(self.server, conn, addr, |
| 553 | decode_data=True) |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 554 | |
| 555 | class SMTPDChannelWithDataSizeLimitTest(unittest.TestCase): |
| 556 | |
| 557 | def setUp(self): |
| 558 | smtpd.socket = asyncore.socket = mock_socket |
R David Murray | 05cab75 | 2012-06-04 15:55:51 -0400 | [diff] [blame] | 559 | self.old_debugstream = smtpd.DEBUGSTREAM |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 560 | self.debug = smtpd.DEBUGSTREAM = io.StringIO() |
R David Murray | 6fe56a3 | 2014-06-11 13:48:58 -0400 | [diff] [blame] | 561 | self.server = DummyServer((support.HOST, 0), ('b', 0)) |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 562 | conn, addr = self.server.accept() |
| 563 | # Set DATA size limit to 32 bytes for easy testing |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 564 | self.channel = smtpd.SMTPChannel(self.server, conn, addr, 32, |
| 565 | decode_data=True) |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 566 | |
| 567 | def tearDown(self): |
| 568 | asyncore.close_all() |
| 569 | asyncore.socket = smtpd.socket = socket |
R David Murray | 05cab75 | 2012-06-04 15:55:51 -0400 | [diff] [blame] | 570 | smtpd.DEBUGSTREAM = self.old_debugstream |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 571 | |
| 572 | def write_line(self, line): |
| 573 | self.channel.socket.queue_recv(line) |
| 574 | self.channel.handle_read() |
| 575 | |
| 576 | def test_data_limit_dialog(self): |
| 577 | self.write_line(b'HELO example') |
| 578 | self.write_line(b'MAIL From:eggs@example') |
| 579 | self.assertEqual(self.channel.socket.last, b'250 OK\r\n') |
| 580 | self.write_line(b'RCPT To:spam@example') |
| 581 | self.assertEqual(self.channel.socket.last, b'250 OK\r\n') |
| 582 | |
| 583 | self.write_line(b'DATA') |
| 584 | self.assertEqual(self.channel.socket.last, |
| 585 | b'354 End data with <CR><LF>.<CR><LF>\r\n') |
| 586 | self.write_line(b'data\r\nmore\r\n.') |
| 587 | self.assertEqual(self.channel.socket.last, b'250 OK\r\n') |
| 588 | self.assertEqual(self.server.messages, |
| 589 | [('peer', 'eggs@example', ['spam@example'], 'data\nmore')]) |
| 590 | |
| 591 | def test_data_limit_dialog_too_much_data(self): |
| 592 | self.write_line(b'HELO example') |
| 593 | self.write_line(b'MAIL From:eggs@example') |
| 594 | self.assertEqual(self.channel.socket.last, b'250 OK\r\n') |
| 595 | self.write_line(b'RCPT To:spam@example') |
| 596 | self.assertEqual(self.channel.socket.last, b'250 OK\r\n') |
| 597 | |
| 598 | self.write_line(b'DATA') |
| 599 | self.assertEqual(self.channel.socket.last, |
| 600 | b'354 End data with <CR><LF>.<CR><LF>\r\n') |
| 601 | self.write_line(b'This message is longer than 32 bytes\r\n.') |
| 602 | self.assertEqual(self.channel.socket.last, |
| 603 | b'552 Error: Too much mail data\r\n') |
| 604 | |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 605 | |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 606 | class SMTPDChannelWithDecodeDataFalse(unittest.TestCase): |
| 607 | |
| 608 | def setUp(self): |
| 609 | smtpd.socket = asyncore.socket = mock_socket |
| 610 | self.old_debugstream = smtpd.DEBUGSTREAM |
| 611 | self.debug = smtpd.DEBUGSTREAM = io.StringIO() |
R David Murray | 6fe56a3 | 2014-06-11 13:48:58 -0400 | [diff] [blame] | 612 | self.server = DummyServer((support.HOST, 0), ('b', 0), |
| 613 | decode_data=False) |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 614 | conn, addr = self.server.accept() |
| 615 | # Set decode_data to False |
| 616 | self.channel = smtpd.SMTPChannel(self.server, conn, addr, |
| 617 | decode_data=False) |
| 618 | |
| 619 | def tearDown(self): |
| 620 | asyncore.close_all() |
| 621 | asyncore.socket = smtpd.socket = socket |
| 622 | smtpd.DEBUGSTREAM = self.old_debugstream |
| 623 | |
| 624 | def write_line(self, line): |
| 625 | self.channel.socket.queue_recv(line) |
| 626 | self.channel.handle_read() |
| 627 | |
| 628 | def test_ascii_data(self): |
| 629 | self.write_line(b'HELO example') |
| 630 | self.write_line(b'MAIL From:eggs@example') |
| 631 | self.write_line(b'RCPT To:spam@example') |
| 632 | self.write_line(b'DATA') |
| 633 | self.write_line(b'plain ascii text') |
| 634 | self.write_line(b'.') |
| 635 | self.assertEqual(self.channel.received_data, b'plain ascii text') |
| 636 | |
| 637 | def test_utf8_data(self): |
| 638 | self.write_line(b'HELO example') |
| 639 | self.write_line(b'MAIL From:eggs@example') |
| 640 | self.write_line(b'RCPT To:spam@example') |
| 641 | self.write_line(b'DATA') |
| 642 | self.write_line(b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87') |
| 643 | self.write_line(b'and some plain ascii') |
| 644 | self.write_line(b'.') |
| 645 | self.assertEqual( |
| 646 | self.channel.received_data, |
| 647 | b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87\n' |
| 648 | b'and some plain ascii') |
| 649 | |
| 650 | |
| 651 | class SMTPDChannelWithDecodeDataTrue(unittest.TestCase): |
| 652 | |
| 653 | def setUp(self): |
| 654 | smtpd.socket = asyncore.socket = mock_socket |
| 655 | self.old_debugstream = smtpd.DEBUGSTREAM |
| 656 | self.debug = smtpd.DEBUGSTREAM = io.StringIO() |
R David Murray | 6fe56a3 | 2014-06-11 13:48:58 -0400 | [diff] [blame] | 657 | self.server = DummyServer((support.HOST, 0), ('b', 0), |
| 658 | decode_data=True) |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 659 | conn, addr = self.server.accept() |
| 660 | # Set decode_data to True |
| 661 | self.channel = smtpd.SMTPChannel(self.server, conn, addr, |
| 662 | decode_data=True) |
| 663 | |
| 664 | def tearDown(self): |
| 665 | asyncore.close_all() |
| 666 | asyncore.socket = smtpd.socket = socket |
| 667 | smtpd.DEBUGSTREAM = self.old_debugstream |
| 668 | |
| 669 | def write_line(self, line): |
| 670 | self.channel.socket.queue_recv(line) |
| 671 | self.channel.handle_read() |
| 672 | |
| 673 | def test_ascii_data(self): |
| 674 | self.write_line(b'HELO example') |
| 675 | self.write_line(b'MAIL From:eggs@example') |
| 676 | self.write_line(b'RCPT To:spam@example') |
| 677 | self.write_line(b'DATA') |
| 678 | self.write_line(b'plain ascii text') |
| 679 | self.write_line(b'.') |
| 680 | self.assertEqual(self.channel.received_data, 'plain ascii text') |
| 681 | |
| 682 | def test_utf8_data(self): |
| 683 | self.write_line(b'HELO example') |
| 684 | self.write_line(b'MAIL From:eggs@example') |
| 685 | self.write_line(b'RCPT To:spam@example') |
| 686 | self.write_line(b'DATA') |
| 687 | self.write_line(b'utf8 enriched text: \xc5\xbc\xc5\xba\xc4\x87') |
| 688 | self.write_line(b'and some plain ascii') |
| 689 | self.write_line(b'.') |
| 690 | self.assertEqual( |
| 691 | self.channel.received_data, |
| 692 | 'utf8 enriched text: żźć\nand some plain ascii') |
| 693 | |
| 694 | |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 695 | if __name__ == "__main__": |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 696 | unittest.main() |