Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4aa0d4d

Browse files
author
Richard Jones
committed
improve smtpd module test coverage
1 parent dd24cf6 commit 4aa0d4d

1 file changed

Lines changed: 83 additions & 5 deletions

File tree

Lib/test/test_smtpd.py

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,47 @@
77

88

99
class DummyServer(smtpd.SMTPServer):
10-
def __init__(self, *args):
11-
smtpd.SMTPServer.__init__(self, *args)
10+
def __init__(self, localaddr, remoteaddr):
11+
smtpd.SMTPServer.__init__(self, localaddr, remoteaddr)
1212
self.messages = []
1313

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

19+
1920
class DummyDispatcherBroken(Exception):
2021
pass
2122

23+
2224
class BrokenDummyServer(DummyServer):
2325
def listen(self, num):
2426
raise DummyDispatcherBroken()
2527

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):
48+
asyncore.socket = smtpd.socket = socket
49+
50+
2651
class SMTPDChannelTest(TestCase):
2752
def setUp(self):
2853
smtpd.socket = asyncore.socket = mock_socket
@@ -142,15 +167,22 @@ def test_data_dialog(self):
142167
b'354 End data with <CR><LF>.<CR><LF>\r\n')
143168
self.write_line(b'data\r\nmore\r\n.')
144169
self.assertEqual(self.channel.socket.last, b'250 Ok\r\n')
145-
self.assertEqual(self.server.messages[-1],
146-
('peer', 'eggs@example', ['spam@example'], 'data\nmore'))
170+
self.assertEqual(self.server.messages,
171+
[('peer', 'eggs@example', ['spam@example'], 'data\nmore')])
147172

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

179+
def test_data_transparency_section_4_5_2(self):
180+
self.write_line(b'MAIL From:eggs@example')
181+
self.write_line(b'RCPT To:spam@example')
182+
self.write_line(b'DATA')
183+
self.write_line(b'..\r\n.\r\n')
184+
self.assertEqual(self.channel.received_data, '.')
185+
154186
def test_multiple_RCPT(self):
155187
self.write_line(b'MAIL From:eggs@example')
156188
self.write_line(b'RCPT To:spam@example')
@@ -161,6 +193,7 @@ def test_multiple_RCPT(self):
161193
('peer', 'eggs@example', ['spam@example','ham@example'], 'data'))
162194

163195
def test_manual_status(self):
196+
# checks that the Channel is able to return a custom status message
164197
self.write_line(b'MAIL From:eggs@example')
165198
self.write_line(b'RCPT To:spam@example')
166199
self.write_line(b'DATA')
@@ -183,9 +216,54 @@ def test_RSET_syntax(self):
183216
self.write_line(b'RSET hi')
184217
self.assertEqual(self.channel.socket.last, b'501 Syntax: RSET\r\n')
185218

219+
def test_attribute_deprecations(self):
220+
with support.check_warnings(('', PendingDeprecationWarning)):
221+
spam = self.channel._SMTPChannel__server
222+
with support.check_warnings(('', PendingDeprecationWarning)):
223+
self.channel._SMTPChannel__server = 'spam'
224+
with support.check_warnings(('', PendingDeprecationWarning)):
225+
spam = self.channel._SMTPChannel__line
226+
with support.check_warnings(('', PendingDeprecationWarning)):
227+
self.channel._SMTPChannel__line = 'spam'
228+
with support.check_warnings(('', PendingDeprecationWarning)):
229+
spam = self.channel._SMTPChannel__state
230+
with support.check_warnings(('', PendingDeprecationWarning)):
231+
self.channel._SMTPChannel__state = 'spam'
232+
with support.check_warnings(('', PendingDeprecationWarning)):
233+
spam = self.channel._SMTPChannel__greeting
234+
with support.check_warnings(('', PendingDeprecationWarning)):
235+
self.channel._SMTPChannel__greeting = 'spam'
236+
with support.check_warnings(('', PendingDeprecationWarning)):
237+
spam = self.channel._SMTPChannel__mailfrom
238+
with support.check_warnings(('', PendingDeprecationWarning)):
239+
self.channel._SMTPChannel__mailfrom = 'spam'
240+
with support.check_warnings(('', PendingDeprecationWarning)):
241+
spam = self.channel._SMTPChannel__rcpttos
242+
with support.check_warnings(('', PendingDeprecationWarning)):
243+
self.channel._SMTPChannel__rcpttos = 'spam'
244+
with support.check_warnings(('', PendingDeprecationWarning)):
245+
spam = self.channel._SMTPChannel__data
246+
with support.check_warnings(('', PendingDeprecationWarning)):
247+
self.channel._SMTPChannel__data = 'spam'
248+
with support.check_warnings(('', PendingDeprecationWarning)):
249+
spam = self.channel._SMTPChannel__fqdn
250+
with support.check_warnings(('', PendingDeprecationWarning)):
251+
self.channel._SMTPChannel__fqdn = 'spam'
252+
with support.check_warnings(('', PendingDeprecationWarning)):
253+
spam = self.channel._SMTPChannel__peer
254+
with support.check_warnings(('', PendingDeprecationWarning)):
255+
self.channel._SMTPChannel__peer = 'spam'
256+
with support.check_warnings(('', PendingDeprecationWarning)):
257+
spam = self.channel._SMTPChannel__conn
258+
with support.check_warnings(('', PendingDeprecationWarning)):
259+
self.channel._SMTPChannel__conn = 'spam'
260+
with support.check_warnings(('', PendingDeprecationWarning)):
261+
spam = self.channel._SMTPChannel__addr
262+
with support.check_warnings(('', PendingDeprecationWarning)):
263+
self.channel._SMTPChannel__addr = 'spam'
186264

187265
def test_main():
188-
support.run_unittest(SMTPDChannelTest)
266+
support.run_unittest(SMTPDServerTest, SMTPDChannelTest)
189267

190268
if __name__ == "__main__":
191269
test_main()

0 commit comments

Comments
 (0)