Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 1 | from unittest import TestCase |
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): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 10 | def __init__(self, localaddr, remoteaddr): |
| 11 | smtpd.SMTPServer.__init__(self, localaddr, remoteaddr) |
Georg Brandl | 6d23c44 | 2010-07-29 13:19:42 +0000 | [diff] [blame] | 12 | self.messages = [] |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 13 | |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 14 | def process_message(self, peer, mailfrom, rcpttos, data): |
| 15 | self.messages.append((peer, mailfrom, rcpttos, data)) |
| 16 | if data == 'return status': |
| 17 | return '250 Okish' |
| 18 | |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 19 | |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 20 | class DummyDispatcherBroken(Exception): |
| 21 | pass |
| 22 | |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 23 | |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 24 | class BrokenDummyServer(DummyServer): |
| 25 | def listen(self, num): |
| 26 | raise DummyDispatcherBroken() |
| 27 | |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 28 | |
| 29 | class SMTPDServerTest(TestCase): |
| 30 | def setUp(self): |
| 31 | smtpd.socket = asyncore.socket = mock_socket |
| 32 | |
| 33 | def test_process_message_unimplemented(self): |
| 34 | server = smtpd.SMTPServer('a', 'b') |
| 35 | conn, addr = server.accept() |
| 36 | channel = smtpd.SMTPChannel(server, conn, addr) |
| 37 | |
| 38 | def write_line(line): |
| 39 | channel.socket.queue_recv(line) |
| 40 | channel.handle_read() |
| 41 | |
| 42 | write_line(b'MAIL From:eggs@example') |
| 43 | write_line(b'RCPT To:spam@example') |
| 44 | write_line(b'DATA') |
| 45 | self.assertRaises(NotImplementedError, write_line, b'spam\r\n.\r\n') |
| 46 | |
| 47 | def tearDown(self): |
| 48 | asyncore.socket = smtpd.socket = socket |
| 49 | |
| 50 | |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 51 | class SMTPDChannelTest(TestCase): |
| 52 | def setUp(self): |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 53 | smtpd.socket = asyncore.socket = mock_socket |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 54 | self.debug = smtpd.DEBUGSTREAM = io.StringIO() |
| 55 | self.server = DummyServer('a', 'b') |
| 56 | conn, addr = self.server.accept() |
| 57 | self.channel = smtpd.SMTPChannel(self.server, conn, addr) |
| 58 | |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 59 | def tearDown(self): |
| 60 | asyncore.socket = smtpd.socket = socket |
| 61 | |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 62 | def write_line(self, line): |
| 63 | self.channel.socket.queue_recv(line) |
| 64 | self.channel.handle_read() |
| 65 | |
| 66 | def test_broken_connect(self): |
| 67 | self.assertRaises(DummyDispatcherBroken, BrokenDummyServer, 'a', 'b') |
| 68 | |
| 69 | def test_server_accept(self): |
| 70 | self.server.handle_accept() |
| 71 | |
| 72 | def test_missing_data(self): |
| 73 | self.write_line(b'') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 74 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 75 | b'500 Error: bad syntax\r\n') |
| 76 | |
| 77 | def test_EHLO_not_implemented(self): |
| 78 | self.write_line(b'EHLO test.example') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 79 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 80 | b'502 Error: command "EHLO" not implemented\r\n') |
| 81 | |
| 82 | def test_HELO(self): |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 83 | name = smtpd.socket.getfqdn() |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 84 | self.write_line(b'HELO test.example') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 85 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 86 | '250 {}\r\n'.format(name).encode('ascii')) |
| 87 | |
| 88 | def test_HELO_bad_syntax(self): |
| 89 | self.write_line(b'HELO') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 90 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 91 | b'501 Syntax: HELO hostname\r\n') |
| 92 | |
| 93 | def test_HELO_duplicate(self): |
| 94 | self.write_line(b'HELO test.example') |
| 95 | self.write_line(b'HELO test.example') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 96 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 97 | b'503 Duplicate HELO/EHLO\r\n') |
| 98 | |
| 99 | def test_NOOP(self): |
| 100 | self.write_line(b'NOOP') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 101 | self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 102 | |
| 103 | def test_NOOP_bad_syntax(self): |
| 104 | self.write_line(b'NOOP hi') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 105 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 106 | b'501 Syntax: NOOP\r\n') |
| 107 | |
| 108 | def test_QUIT(self): |
| 109 | self.write_line(b'QUIT') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 110 | self.assertEqual(self.channel.socket.last, b'221 Bye\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 111 | |
| 112 | def test_QUIT_arg_ignored(self): |
| 113 | self.write_line(b'QUIT bye bye') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 114 | self.assertEqual(self.channel.socket.last, b'221 Bye\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 115 | |
| 116 | def test_bad_state(self): |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 117 | self.channel.smtp_state = 'BAD STATE' |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 118 | self.write_line(b'HELO') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 119 | self.assertEqual(self.channel.socket.last, |
| 120 | b'451 Internal confusion\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 121 | |
| 122 | def test_need_MAIL(self): |
| 123 | self.write_line(b'RCPT to:spam@example') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 124 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 125 | b'503 Error: need MAIL command\r\n') |
| 126 | |
| 127 | def test_MAIL_syntax(self): |
| 128 | self.write_line(b'MAIL from eggs@example') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 129 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 130 | b'501 Syntax: MAIL FROM:<address>\r\n') |
| 131 | |
| 132 | def test_MAIL_missing_from(self): |
| 133 | self.write_line(b'MAIL from:') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 134 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 135 | b'501 Syntax: MAIL FROM:<address>\r\n') |
| 136 | |
| 137 | def test_MAIL_chevrons(self): |
| 138 | self.write_line(b'MAIL from:<eggs@example>') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 139 | self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 140 | |
| 141 | def test_nested_MAIL(self): |
| 142 | self.write_line(b'MAIL from:eggs@example') |
| 143 | self.write_line(b'MAIL from:spam@example') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 144 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 145 | b'503 Error: nested MAIL command\r\n') |
| 146 | |
| 147 | def test_need_RCPT(self): |
| 148 | self.write_line(b'MAIL From:eggs@example') |
| 149 | self.write_line(b'DATA') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 150 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 151 | b'503 Error: need RCPT command\r\n') |
| 152 | |
| 153 | def test_RCPT_syntax(self): |
| 154 | self.write_line(b'MAIL From:eggs@example') |
| 155 | self.write_line(b'RCPT to eggs@example') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 156 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 157 | b'501 Syntax: RCPT TO: <address>\r\n') |
| 158 | |
| 159 | def test_data_dialog(self): |
| 160 | self.write_line(b'MAIL From:eggs@example') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 161 | self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 162 | self.write_line(b'RCPT To:spam@example') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 163 | self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 164 | |
| 165 | self.write_line(b'DATA') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 166 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 167 | b'354 End data with <CR><LF>.<CR><LF>\r\n') |
| 168 | self.write_line(b'data\r\nmore\r\n.') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 169 | self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 170 | self.assertEqual(self.server.messages, |
| 171 | [('peer', 'eggs@example', ['spam@example'], 'data\nmore')]) |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 172 | |
| 173 | def test_DATA_syntax(self): |
| 174 | self.write_line(b'MAIL From:eggs@example') |
| 175 | self.write_line(b'RCPT To:spam@example') |
| 176 | self.write_line(b'DATA spam') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 177 | self.assertEqual(self.channel.socket.last, b'501 Syntax: DATA\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 178 | |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 179 | def test_data_transparency_section_4_5_2(self): |
| 180 | self.write_line(b'MAIL From:eggs@example') |
| 181 | self.write_line(b'RCPT To:spam@example') |
| 182 | self.write_line(b'DATA') |
| 183 | self.write_line(b'..\r\n.\r\n') |
| 184 | self.assertEqual(self.channel.received_data, '.') |
| 185 | |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 186 | def test_multiple_RCPT(self): |
| 187 | self.write_line(b'MAIL From:eggs@example') |
| 188 | self.write_line(b'RCPT To:spam@example') |
| 189 | self.write_line(b'RCPT To:ham@example') |
| 190 | self.write_line(b'DATA') |
| 191 | self.write_line(b'data\r\n.') |
Richard Jones | 6a9e6bb | 2010-08-04 12:27:36 +0000 | [diff] [blame] | 192 | self.assertEqual(self.server.messages, |
| 193 | [('peer', 'eggs@example', ['spam@example','ham@example'], 'data')]) |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 194 | |
| 195 | def test_manual_status(self): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 196 | # checks that the Channel is able to return a custom status message |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 197 | self.write_line(b'MAIL From:eggs@example') |
| 198 | self.write_line(b'RCPT To:spam@example') |
| 199 | self.write_line(b'DATA') |
| 200 | self.write_line(b'return status\r\n.') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 201 | self.assertEqual(self.channel.socket.last, b'250 Okish\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 202 | |
| 203 | def test_RSET(self): |
| 204 | self.write_line(b'MAIL From:eggs@example') |
| 205 | self.write_line(b'RCPT To:spam@example') |
| 206 | self.write_line(b'RSET') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 207 | self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 208 | self.write_line(b'MAIL From:foo@example') |
| 209 | self.write_line(b'RCPT To:eggs@example') |
| 210 | self.write_line(b'DATA') |
| 211 | self.write_line(b'data\r\n.') |
Richard Jones | 6a9e6bb | 2010-08-04 12:27:36 +0000 | [diff] [blame] | 212 | self.assertEqual(self.server.messages, |
| 213 | [('peer', 'foo@example', ['eggs@example'], 'data')]) |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 214 | |
| 215 | def test_RSET_syntax(self): |
| 216 | self.write_line(b'RSET hi') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 217 | self.assertEqual(self.channel.socket.last, b'501 Syntax: RSET\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 218 | |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 219 | def test_attribute_deprecations(self): |
| 220 | with support.check_warnings(('', PendingDeprecationWarning)): |
| 221 | spam = self.channel._SMTPChannel__server |
| 222 | with support.check_warnings(('', PendingDeprecationWarning)): |
| 223 | self.channel._SMTPChannel__server = 'spam' |
| 224 | with support.check_warnings(('', PendingDeprecationWarning)): |
| 225 | spam = self.channel._SMTPChannel__line |
| 226 | with support.check_warnings(('', PendingDeprecationWarning)): |
| 227 | self.channel._SMTPChannel__line = 'spam' |
| 228 | with support.check_warnings(('', PendingDeprecationWarning)): |
| 229 | spam = self.channel._SMTPChannel__state |
| 230 | with support.check_warnings(('', PendingDeprecationWarning)): |
| 231 | self.channel._SMTPChannel__state = 'spam' |
| 232 | with support.check_warnings(('', PendingDeprecationWarning)): |
| 233 | spam = self.channel._SMTPChannel__greeting |
| 234 | with support.check_warnings(('', PendingDeprecationWarning)): |
| 235 | self.channel._SMTPChannel__greeting = 'spam' |
| 236 | with support.check_warnings(('', PendingDeprecationWarning)): |
| 237 | spam = self.channel._SMTPChannel__mailfrom |
| 238 | with support.check_warnings(('', PendingDeprecationWarning)): |
| 239 | self.channel._SMTPChannel__mailfrom = 'spam' |
| 240 | with support.check_warnings(('', PendingDeprecationWarning)): |
| 241 | spam = self.channel._SMTPChannel__rcpttos |
| 242 | with support.check_warnings(('', PendingDeprecationWarning)): |
| 243 | self.channel._SMTPChannel__rcpttos = 'spam' |
| 244 | with support.check_warnings(('', PendingDeprecationWarning)): |
| 245 | spam = self.channel._SMTPChannel__data |
| 246 | with support.check_warnings(('', PendingDeprecationWarning)): |
| 247 | self.channel._SMTPChannel__data = 'spam' |
| 248 | with support.check_warnings(('', PendingDeprecationWarning)): |
| 249 | spam = self.channel._SMTPChannel__fqdn |
| 250 | with support.check_warnings(('', PendingDeprecationWarning)): |
| 251 | self.channel._SMTPChannel__fqdn = 'spam' |
| 252 | with support.check_warnings(('', PendingDeprecationWarning)): |
| 253 | spam = self.channel._SMTPChannel__peer |
| 254 | with support.check_warnings(('', PendingDeprecationWarning)): |
| 255 | self.channel._SMTPChannel__peer = 'spam' |
| 256 | with support.check_warnings(('', PendingDeprecationWarning)): |
| 257 | spam = self.channel._SMTPChannel__conn |
| 258 | with support.check_warnings(('', PendingDeprecationWarning)): |
| 259 | self.channel._SMTPChannel__conn = 'spam' |
| 260 | with support.check_warnings(('', PendingDeprecationWarning)): |
| 261 | spam = self.channel._SMTPChannel__addr |
| 262 | with support.check_warnings(('', PendingDeprecationWarning)): |
| 263 | self.channel._SMTPChannel__addr = 'spam' |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 264 | |
| 265 | def test_main(): |
Richard Jones | 4aa0d4d | 2010-08-04 01:20:14 +0000 | [diff] [blame] | 266 | support.run_unittest(SMTPDServerTest, SMTPDChannelTest) |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 267 | |
| 268 | if __name__ == "__main__": |
| 269 | test_main() |