summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_smtpd.py
blob: a7dc5f626deda113f75b2cd6084880f2cb8c38da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
from unittest import TestCase
from test import support, mock_socket
import socket
import io
import smtpd
import asyncore


class DummyServer(smtpd.SMTPServer):
    def __init__(self, localaddr, remoteaddr):
        smtpd.SMTPServer.__init__(self, localaddr, remoteaddr)
        self.messages = []

    def process_message(self, peer, mailfrom, rcpttos, data):
        self.messages.append((peer, mailfrom, rcpttos, data))
        if data == 'return status':
            return '250 Okish'


class DummyDispatcherBroken(Exception):
    pass


class BrokenDummyServer(DummyServer):
    def listen(self, num):
        raise DummyDispatcherBroken()


class SMTPDServerTest(TestCase):
    def setUp(self):
        smtpd.socket = asyncore.socket = mock_socket

    def test_process_message_unimplemented(self):
        server = smtpd.SMTPServer('a', 'b')
        conn, addr = server.accept()
        channel = smtpd.SMTPChannel(server, conn, addr)

        def write_line(line):
            channel.socket.queue_recv(line)
            channel.handle_read()

        write_line(b'HELO test.example')
        write_line(b'MAIL From:eggs@example')
        write_line(b'RCPT To:spam@example')
        write_line(b'DATA')
        self.assertRaises(NotImplementedError, write_line, b'spam\r\n.\r\n')

    def tearDown(self):
        asyncore.close_all()
        asyncore.socket = smtpd.socket = socket


class SMTPDChannelTest(TestCase):
    def setUp(self):
        smtpd.socket = asyncore.socket = mock_socket
        self.old_debugstream = smtpd.DEBUGSTREAM
        self.debug = smtpd.DEBUGSTREAM = io.StringIO()
        self.server = DummyServer('a', 'b')
        conn, addr = self.server.accept()
        self.channel = smtpd.SMTPChannel(self.server, conn, addr)

    def tearDown(self):
        asyncore.close_all()
        asyncore.socket = smtpd.socket = socket
        smtpd.DEBUGSTREAM = self.old_debugstream

    def write_line(self, line):
        self.channel.socket.queue_recv(line)
        self.channel.handle_read()

    def test_broken_connect(self):
        self.assertRaises(DummyDispatcherBroken, BrokenDummyServer, 'a', 'b')

    def test_server_accept(self):
        self.server.handle_accept()

    def test_missing_data(self):
        self.write_line(b'')
        self.assertEqual(self.channel.socket.last,
                         b'500 Error: bad syntax\r\n')

    def test_EHLO_not_implemented(self):
        self.write_line(b'EHLO test.example')
        self.assertEqual(self.channel.socket.last,
                         b'502 Error: command "EHLO" not implemented\r\n')

    def test_HELO(self):
        name = smtpd.socket.getfqdn()
        self.write_line(b'HELO test.example')
        self.assertEqual(self.channel.socket.last,
                         '250 {}\r\n'.format(name).encode('ascii'))

    def test_HELO_bad_syntax(self):
        self.write_line(b'HELO')
        self.assertEqual(self.channel.socket.last,
                         b'501 Syntax: HELO hostname\r\n')

    def test_HELO_duplicate(self):
        self.write_line(b'HELO test.example')
        self.write_line(b'HELO test.example')
        self.assertEqual(self.channel.socket.last,
                         b'503 Duplicate HELO/EHLO\r\n')

    def test_NOOP(self):
        self.write_line(b'NOOP')
        self.assertEqual(self.channel.socket.last, b'250 Ok\r\n')

    def test_HELO_NOOP(self):
        self.write_line(b'HELO example')
        self.write_line(b'NOOP')
        self.assertEqual(self.channel.socket.last, b'250 Ok\r\n')

    def test_NOOP_bad_syntax(self):
        self.write_line(b'NOOP hi')
        self.assertEqual(self.channel.socket.last,
                         b'501 Syntax: NOOP\r\n')

    def test_QUIT(self):
        self.write_line(b'QUIT')
        self.assertEqual(self.channel.socket.last, b'221 Bye\r\n')

    def test_HELO_QUIT(self):
        self.write_line(b'HELO example')
        self.write_line(b'QUIT')
        self.assertEqual(self.channel.socket.last, b'221 Bye\r\n')

    def test_QUIT_arg_ignored(self):
        self.write_line(b'QUIT bye bye')
        self.assertEqual(self.channel.socket.last, b'221 Bye\r\n')

    def test_bad_state(self):
        self.channel.smtp_state = 'BAD STATE'
        self.write_line(b'HELO example')
        self.assertEqual(self.channel.socket.last,
                         b'451 Internal confusion\r\n')

    def test_command_too_long(self):
        self.write_line(b'HELO example')
        self.write_line(b'MAIL from ' +
                        b'a' * self.channel.command_size_limit +
                        b'@example')
        self.assertEqual(self.channel.socket.last,
                         b'500 Error: line too long\r\n')

    def test_data_too_long(self):
        # Small hack. Setting limit to 2K octets here will save us some time.
        self.channel.data_size_limit = 2048
        self.write_line(b'HELO example')
        self.write_line(b'MAIL From:eggs@example')
        self.write_line(b'RCPT To:spam@example')
        self.write_line(b'DATA')
        self.write_line(b'A' * self.channel.data_size_limit +
                        b'A\r\n.')
        self.assertEqual(self.channel.socket.last,
                         b'552 Error: Too much mail data\r\n')

    def test_need_MAIL(self):
        self.write_line(b'HELO example')
        self.write_line(b'RCPT to:spam@example')
        self.assertEqual(self.channel.socket.last,
            b'503 Error: need MAIL command\r\n')

    def test_MAIL_syntax(self):
        self.write_line(b'HELO example')
        self.write_line(b'MAIL from eggs@example')
        self.assertEqual(self.channel.socket.last,
            b'501 Syntax: MAIL FROM:<address>\r\n')

    def test_MAIL_missing_from(self):
        self.write_line(b'HELO example')
        self.write_line(b'MAIL from:')
        self.assertEqual(self.channel.socket.last,
            b'501 Syntax: MAIL FROM:<address>\r\n')

    def test_MAIL_chevrons(self):
        self.write_line(b'HELO example')
        self.write_line(b'MAIL from:<eggs@example>')
        self.assertEqual(self.channel.socket.last, b'250 Ok\r\n')

    def test_nested_MAIL(self):
        self.write_line(b'HELO example')
        self.write_line(b'MAIL from:eggs@example')
        self.write_line(b'MAIL from:spam@example')
        self.assertEqual(self.channel.socket.last,
            b'503 Error: nested MAIL command\r\n')

    def test_no_HELO_MAIL(self):
        self.write_line(b'MAIL from:<foo@example.com>')
        self.assertEqual(self.channel.socket.last,
                         b'503 Error: send HELO first\r\n')

    def test_need_RCPT(self):
        self.write_line(b'HELO example')
        self.write_line(b'MAIL From:eggs@example')
        self.write_line(b'DATA')
        self.assertEqual(self.channel.socket.last,
            b'503 Error: need RCPT command\r\n')

    def test_RCPT_syntax(self):
        self.write_line(b'HELO example')
        self.write_line(b'MAIL From:eggs@example')
        self.write_line(b'RCPT to eggs@example')
        self.assertEqual(self.channel.socket.last,
            b'501 Syntax: RCPT TO: <address>\r\n')

    def test_no_HELO_RCPT(self):
        self.write_line(b'RCPT to eggs@example')
        self.assertEqual(self.channel.socket.last,
                         b'503 Error: send HELO first\r\n')

    def test_data_dialog(self):
        self.write_line(b'HELO example')
        self.write_line(b'MAIL From:eggs@example')
        self.assertEqual(self.channel.socket.last, b'250 Ok\r\n')
        self.write_line(b'RCPT To:spam@example')
        self.assertEqual(self.channel.socket.last, b'250 Ok\r\n')

        self.write_line(b'DATA')
        self.assertEqual(self.channel.socket.last,
            b'354 End data with <CR><LF>.<CR><LF>\r\n')
        self.write_line(b'data\r\nmore\r\n.')
        self.assertEqual(self.channel.socket.last, b'250 Ok\r\n')
        self.assertEqual(self.server.messages,
            [('peer', 'eggs@example', ['spam@example'], 'data\nmore')])

    def test_DATA_syntax(self):
        self.write_line(b'HELO example')
        self.write_line(b'MAIL From:eggs@example')
        self.write_line(b'RCPT To:spam@example')
        self.write_line(b'DATA spam')
        self.assertEqual(self.channel.socket.last, b'501 Syntax: DATA\r\n')

    def test_no_HELO_DATA(self):
        self.write_line(b'DATA spam')
        self.assertEqual(self.channel.socket.last,
                         b'503 Error: send HELO first\r\n')

    def test_data_transparency_section_4_5_2(self):
        self.write_line(b'HELO example')
        self.write_line(b'MAIL From:eggs@example')
        self.write_line(b'RCPT To:spam@example')
        self.write_line(b'DATA')
        self.write_line(b'..\r\n.\r\n')
        self.assertEqual(self.channel.received_data, '.')

    def test_multiple_RCPT(self):
        self.write_line(b'HELO example')
        self.write_line(b'MAIL From:eggs@example')
        self.write_line(b'RCPT To:spam@example')
        self.write_line(b'RCPT To:ham@example')
        self.write_line(b'DATA')
        self.write_line(b'data\r\n.')
        self.assertEqual(self.server.messages,
            [('peer', 'eggs@example', ['spam@example','ham@example'], 'data')])

    def test_manual_status(self):
        # checks that the Channel is able to return a custom status message
        self.write_line(b'HELO example')
        self.write_line(b'MAIL From:eggs@example')
        self.write_line(b'RCPT To:spam@example')
        self.write_line(b'DATA')
        self.write_line(b'return status\r\n.')
        self.assertEqual(self.channel.socket.last, b'250 Okish\r\n')

    def test_RSET(self):
        self.write_line(b'HELO example')
        self.write_line(b'MAIL From:eggs@example')
        self.write_line(b'RCPT To:spam@example')
        self.write_line(b'RSET')
        self.assertEqual(self.channel.socket.last, b'250 Ok\r\n')
        self.write_line(b'MAIL From:foo@example')
        self.write_line(b'RCPT To:eggs@example')
        self.write_line(b'DATA')
        self.write_line(b'data\r\n.')
        self.assertEqual(self.server.messages,
            [('peer', 'foo@example', ['eggs@example'], 'data')])

    def test_HELO_RSET(self):
        self.write_line(b'HELO example')
        self.write_line(b'RSET')
        self.assertEqual(self.channel.socket.last, b'250 Ok\r\n')

    def test_RSET_syntax(self):
        self.write_line(b'RSET hi')
        self.assertEqual(self.channel.socket.last, b'501 Syntax: RSET\r\n')

    def test_attribute_deprecations(self):
        with support.check_warnings(('', DeprecationWarning)):
            spam = self.channel._SMTPChannel__server
        with support.check_warnings(('', DeprecationWarning)):
            self.channel._SMTPChannel__server = 'spam'
        with support.check_warnings(('', DeprecationWarning)):
            spam = self.channel._SMTPChannel__line
        with support.check_warnings(('', DeprecationWarning)):
            self.channel._SMTPChannel__line = 'spam'
        with support.check_warnings(('', DeprecationWarning)):
            spam = self.channel._SMTPChannel__state
        with support.check_warnings(('', DeprecationWarning)):
            self.channel._SMTPChannel__state = 'spam'
        with support.check_warnings(('', DeprecationWarning)):
            spam = self.channel._SMTPChannel__greeting
        with support.check_warnings(('', DeprecationWarning)):
            self.channel._SMTPChannel__greeting = 'spam'
        with support.check_warnings(('', DeprecationWarning)):
            spam = self.channel._SMTPChannel__mailfrom
        with support.check_warnings(('', DeprecationWarning)):
            self.channel._SMTPChannel__mailfrom = 'spam'
        with support.check_warnings(('', DeprecationWarning)):
            spam = self.channel._SMTPChannel__rcpttos
        with support.check_warnings(('', DeprecationWarning)):
            self.channel._SMTPChannel__rcpttos = 'spam'
        with support.check_warnings(('', DeprecationWarning)):
            spam = self.channel._SMTPChannel__data
        with support.check_warnings(('', DeprecationWarning)):
            self.channel._SMTPChannel__data = 'spam'
        with support.check_warnings(('', DeprecationWarning)):
            spam = self.channel._SMTPChannel__fqdn
        with support.check_warnings(('', DeprecationWarning)):
            self.channel._SMTPChannel__fqdn = 'spam'
        with support.check_warnings(('', DeprecationWarning)):
            spam = self.channel._SMTPChannel__peer
        with support.check_warnings(('', DeprecationWarning)):
            self.channel._SMTPChannel__peer = 'spam'
        with support.check_warnings(('', DeprecationWarning)):
            spam = self.channel._SMTPChannel__conn
        with support.check_warnings(('', DeprecationWarning)):
            self.channel._SMTPChannel__conn = 'spam'
        with support.check_warnings(('', DeprecationWarning)):
            spam = self.channel._SMTPChannel__addr
        with support.check_warnings(('', DeprecationWarning)):
            self.channel._SMTPChannel__addr = 'spam'

def test_main():
    support.run_unittest(SMTPDServerTest, SMTPDChannelTest)

if __name__ == "__main__":
    test_main()