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): |
Georg Brandl | 6d23c44 | 2010-07-29 13:19:42 +0000 | [diff] [blame] | 10 | def __init__(self, *args): |
| 11 | smtpd.SMTPServer.__init__(self, *args) |
| 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 | |
| 19 | class DummyDispatcherBroken(Exception): |
| 20 | pass |
| 21 | |
| 22 | class BrokenDummyServer(DummyServer): |
| 23 | def listen(self, num): |
| 24 | raise DummyDispatcherBroken() |
| 25 | |
| 26 | class SMTPDChannelTest(TestCase): |
| 27 | def setUp(self): |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame^] | 28 | smtpd.socket = asyncore.socket = mock_socket |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 29 | self.debug = smtpd.DEBUGSTREAM = io.StringIO() |
| 30 | self.server = DummyServer('a', 'b') |
| 31 | conn, addr = self.server.accept() |
| 32 | self.channel = smtpd.SMTPChannel(self.server, conn, addr) |
| 33 | |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame^] | 34 | def tearDown(self): |
| 35 | asyncore.socket = smtpd.socket = socket |
| 36 | |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 37 | def write_line(self, line): |
| 38 | self.channel.socket.queue_recv(line) |
| 39 | self.channel.handle_read() |
| 40 | |
| 41 | def test_broken_connect(self): |
| 42 | self.assertRaises(DummyDispatcherBroken, BrokenDummyServer, 'a', 'b') |
| 43 | |
| 44 | def test_server_accept(self): |
| 45 | self.server.handle_accept() |
| 46 | |
| 47 | def test_missing_data(self): |
| 48 | self.write_line(b'') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 49 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 50 | b'500 Error: bad syntax\r\n') |
| 51 | |
| 52 | def test_EHLO_not_implemented(self): |
| 53 | self.write_line(b'EHLO test.example') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 54 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 55 | b'502 Error: command "EHLO" not implemented\r\n') |
| 56 | |
| 57 | def test_HELO(self): |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame^] | 58 | name = smtpd.socket.getfqdn() |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 59 | self.write_line(b'HELO test.example') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 60 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 61 | '250 {}\r\n'.format(name).encode('ascii')) |
| 62 | |
| 63 | def test_HELO_bad_syntax(self): |
| 64 | self.write_line(b'HELO') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 65 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 66 | b'501 Syntax: HELO hostname\r\n') |
| 67 | |
| 68 | def test_HELO_duplicate(self): |
| 69 | self.write_line(b'HELO test.example') |
| 70 | self.write_line(b'HELO test.example') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 71 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 72 | b'503 Duplicate HELO/EHLO\r\n') |
| 73 | |
| 74 | def test_NOOP(self): |
| 75 | self.write_line(b'NOOP') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 76 | self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 77 | |
| 78 | def test_NOOP_bad_syntax(self): |
| 79 | self.write_line(b'NOOP hi') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 80 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 81 | b'501 Syntax: NOOP\r\n') |
| 82 | |
| 83 | def test_QUIT(self): |
| 84 | self.write_line(b'QUIT') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 85 | self.assertEqual(self.channel.socket.last, b'221 Bye\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 86 | |
| 87 | def test_QUIT_arg_ignored(self): |
| 88 | self.write_line(b'QUIT bye bye') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 89 | self.assertEqual(self.channel.socket.last, b'221 Bye\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 90 | |
| 91 | def test_bad_state(self): |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 92 | self.channel.smtp_state = 'BAD STATE' |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 93 | self.write_line(b'HELO') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 94 | self.assertEqual(self.channel.socket.last, |
| 95 | b'451 Internal confusion\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 96 | |
| 97 | def test_need_MAIL(self): |
| 98 | self.write_line(b'RCPT to:spam@example') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 99 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 100 | b'503 Error: need MAIL command\r\n') |
| 101 | |
| 102 | def test_MAIL_syntax(self): |
| 103 | self.write_line(b'MAIL from eggs@example') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 104 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 105 | b'501 Syntax: MAIL FROM:<address>\r\n') |
| 106 | |
| 107 | def test_MAIL_missing_from(self): |
| 108 | self.write_line(b'MAIL from:') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 109 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 110 | b'501 Syntax: MAIL FROM:<address>\r\n') |
| 111 | |
| 112 | def test_MAIL_chevrons(self): |
| 113 | self.write_line(b'MAIL from:<eggs@example>') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 114 | self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 115 | |
| 116 | def test_nested_MAIL(self): |
| 117 | self.write_line(b'MAIL from:eggs@example') |
| 118 | self.write_line(b'MAIL from:spam@example') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 119 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 120 | b'503 Error: nested MAIL command\r\n') |
| 121 | |
| 122 | def test_need_RCPT(self): |
| 123 | self.write_line(b'MAIL From:eggs@example') |
| 124 | self.write_line(b'DATA') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 125 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 126 | b'503 Error: need RCPT command\r\n') |
| 127 | |
| 128 | def test_RCPT_syntax(self): |
| 129 | self.write_line(b'MAIL From:eggs@example') |
| 130 | self.write_line(b'RCPT to 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: RCPT TO: <address>\r\n') |
| 133 | |
| 134 | def test_data_dialog(self): |
| 135 | self.write_line(b'MAIL From:eggs@example') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 136 | self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 137 | self.write_line(b'RCPT To:spam@example') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 138 | self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 139 | |
| 140 | self.write_line(b'DATA') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 141 | self.assertEqual(self.channel.socket.last, |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 142 | b'354 End data with <CR><LF>.<CR><LF>\r\n') |
| 143 | self.write_line(b'data\r\nmore\r\n.') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 144 | self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 145 | self.assertEqual(self.server.messages[-1], |
| 146 | ('peer', 'eggs@example', ['spam@example'], 'data\nmore')) |
| 147 | |
| 148 | def test_DATA_syntax(self): |
| 149 | self.write_line(b'MAIL From:eggs@example') |
| 150 | self.write_line(b'RCPT To:spam@example') |
| 151 | self.write_line(b'DATA spam') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 152 | self.assertEqual(self.channel.socket.last, b'501 Syntax: DATA\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 153 | |
| 154 | def test_multiple_RCPT(self): |
| 155 | self.write_line(b'MAIL From:eggs@example') |
| 156 | self.write_line(b'RCPT To:spam@example') |
| 157 | self.write_line(b'RCPT To:ham@example') |
| 158 | self.write_line(b'DATA') |
| 159 | self.write_line(b'data\r\n.') |
| 160 | self.assertEqual(self.server.messages[-1], |
| 161 | ('peer', 'eggs@example', ['spam@example','ham@example'], 'data')) |
| 162 | |
| 163 | def test_manual_status(self): |
| 164 | self.write_line(b'MAIL From:eggs@example') |
| 165 | self.write_line(b'RCPT To:spam@example') |
| 166 | self.write_line(b'DATA') |
| 167 | self.write_line(b'return status\r\n.') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 168 | self.assertEqual(self.channel.socket.last, b'250 Okish\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 169 | |
| 170 | def test_RSET(self): |
| 171 | self.write_line(b'MAIL From:eggs@example') |
| 172 | self.write_line(b'RCPT To:spam@example') |
| 173 | self.write_line(b'RSET') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 174 | self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 175 | self.write_line(b'MAIL From:foo@example') |
| 176 | self.write_line(b'RCPT To:eggs@example') |
| 177 | self.write_line(b'DATA') |
| 178 | self.write_line(b'data\r\n.') |
| 179 | self.assertEqual(self.server.messages[0], |
| 180 | ('peer', 'foo@example', ['eggs@example'], 'data')) |
| 181 | |
| 182 | def test_RSET_syntax(self): |
| 183 | self.write_line(b'RSET hi') |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 184 | self.assertEqual(self.channel.socket.last, b'501 Syntax: RSET\r\n') |
Richard Jones | 8cb3619 | 2010-07-23 16:20:40 +0000 | [diff] [blame] | 185 | |
| 186 | |
| 187 | def test_main(): |
| 188 | support.run_unittest(SMTPDChannelTest) |
| 189 | |
| 190 | if __name__ == "__main__": |
| 191 | test_main() |