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