blob: 538525b5a3d979f583266ee95aae817a93d80ed5 [file] [log] [blame]
Richard Jones8cb36192010-07-23 16:20:40 +00001import asynchat
2from unittest import TestCase
3import socket
4from test import support
5import asyncore
6import io
7import smtpd
8
9# mock-ish socket to sit underneath asyncore
10class 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
44class DummyServer(smtpd.SMTPServer):
Georg Brandl6d23c442010-07-29 13:19:42 +000045 def __init__(self, *args):
46 smtpd.SMTPServer.__init__(self, *args)
47 self.messages = []
Richard Jones8cb36192010-07-23 16:20:40 +000048 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
56class DummyDispatcherBroken(Exception):
57 pass
58
59class BrokenDummyServer(DummyServer):
60 def listen(self, num):
61 raise DummyDispatcherBroken()
62
63class 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'')
82 self.assertEqual(self.channel.last,
83 b'500 Error: bad syntax\r\n')
84
85 def test_EHLO_not_implemented(self):
86 self.write_line(b'EHLO test.example')
87 self.assertEqual(self.channel.last,
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')
93 self.assertEqual(self.channel.last,
94 '250 {}\r\n'.format(name).encode('ascii'))
95
96 def test_HELO_bad_syntax(self):
97 self.write_line(b'HELO')
98 self.assertEqual(self.channel.last,
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')
104 self.assertEqual(self.channel.last,
105 b'503 Duplicate HELO/EHLO\r\n')
106
107 def test_NOOP(self):
108 self.write_line(b'NOOP')
109 self.assertEqual(self.channel.last, b'250 Ok\r\n')
110
111 def test_NOOP_bad_syntax(self):
112 self.write_line(b'NOOP hi')
113 self.assertEqual(self.channel.last,
114 b'501 Syntax: NOOP\r\n')
115
116 def test_QUIT(self):
117 self.write_line(b'QUIT')
118 self.assertEqual(self.channel.last, b'221 Bye\r\n')
119
120 def test_QUIT_arg_ignored(self):
121 self.write_line(b'QUIT bye bye')
122 self.assertEqual(self.channel.last, b'221 Bye\r\n')
123
124 def test_bad_state(self):
125 self.channel._SMTPChannel__state = 'BAD STATE'
126 self.write_line(b'HELO')
127 self.assertEqual(self.channel.last, b'451 Internal confusion\r\n')
128
129 def test_need_MAIL(self):
130 self.write_line(b'RCPT to:spam@example')
131 self.assertEqual(self.channel.last,
132 b'503 Error: need MAIL command\r\n')
133
134 def test_MAIL_syntax(self):
135 self.write_line(b'MAIL from eggs@example')
136 self.assertEqual(self.channel.last,
137 b'501 Syntax: MAIL FROM:<address>\r\n')
138
139 def test_MAIL_missing_from(self):
140 self.write_line(b'MAIL from:')
141 self.assertEqual(self.channel.last,
142 b'501 Syntax: MAIL FROM:<address>\r\n')
143
144 def test_MAIL_chevrons(self):
145 self.write_line(b'MAIL from:<eggs@example>')
146 self.assertEqual(self.channel.last, b'250 Ok\r\n')
147
148 def test_nested_MAIL(self):
149 self.write_line(b'MAIL from:eggs@example')
150 self.write_line(b'MAIL from:spam@example')
151 self.assertEqual(self.channel.last,
152 b'503 Error: nested MAIL command\r\n')
153
154 def test_need_RCPT(self):
155 self.write_line(b'MAIL From:eggs@example')
156 self.write_line(b'DATA')
157 self.assertEqual(self.channel.last,
158 b'503 Error: need RCPT command\r\n')
159
160 def test_RCPT_syntax(self):
161 self.write_line(b'MAIL From:eggs@example')
162 self.write_line(b'RCPT to eggs@example')
163 self.assertEqual(self.channel.last,
164 b'501 Syntax: RCPT TO: <address>\r\n')
165
166 def test_data_dialog(self):
167 self.write_line(b'MAIL From:eggs@example')
168 self.assertEqual(self.channel.last, b'250 Ok\r\n')
169 self.write_line(b'RCPT To:spam@example')
170 self.assertEqual(self.channel.last, b'250 Ok\r\n')
171
172 self.write_line(b'DATA')
173 self.assertEqual(self.channel.last,
174 b'354 End data with <CR><LF>.<CR><LF>\r\n')
175 self.write_line(b'data\r\nmore\r\n.')
176 self.assertEqual(self.channel.last, b'250 Ok\r\n')
177 self.assertEqual(self.server.messages[-1],
178 ('peer', 'eggs@example', ['spam@example'], 'data\nmore'))
179
180 def test_DATA_syntax(self):
181 self.write_line(b'MAIL From:eggs@example')
182 self.write_line(b'RCPT To:spam@example')
183 self.write_line(b'DATA spam')
184 self.assertEqual(self.channel.last, b'501 Syntax: DATA\r\n')
185
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.')
192 self.assertEqual(self.server.messages[-1],
193 ('peer', 'eggs@example', ['spam@example','ham@example'], 'data'))
194
195 def test_manual_status(self):
196 self.write_line(b'MAIL From:eggs@example')
197 self.write_line(b'RCPT To:spam@example')
198 self.write_line(b'DATA')
199 self.write_line(b'return status\r\n.')
200 self.assertEqual(self.channel.last, b'250 Okish\r\n')
201
202 def test_RSET(self):
203 self.write_line(b'MAIL From:eggs@example')
204 self.write_line(b'RCPT To:spam@example')
205 self.write_line(b'RSET')
206 self.assertEqual(self.channel.last, b'250 Ok\r\n')
207 self.write_line(b'MAIL From:foo@example')
208 self.write_line(b'RCPT To:eggs@example')
209 self.write_line(b'DATA')
210 self.write_line(b'data\r\n.')
211 self.assertEqual(self.server.messages[0],
212 ('peer', 'foo@example', ['eggs@example'], 'data'))
213
214 def test_RSET_syntax(self):
215 self.write_line(b'RSET hi')
216 self.assertEqual(self.channel.last, b'501 Syntax: RSET\r\n')
217
218
219def test_main():
220 support.run_unittest(SMTPDChannelTest)
221
222if __name__ == "__main__":
223 test_main()