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