blob: cf92662a4453b4c29e2a10ee5b2ba8e95d817108 [file] [log] [blame]
Guido van Rossum806c2462007-08-06 23:33:07 +00001import asyncore
Guido van Rossumd8faa362007-04-27 19:54:29 +00002import socket
3import threading
Guido van Rossum806c2462007-08-06 23:33:07 +00004import smtpd
Guido van Rossumd8faa362007-04-27 19:54:29 +00005import smtplib
Guido van Rossum806c2462007-08-06 23:33:07 +00006import io
7import sys
Guido van Rossumd8faa362007-04-27 19:54:29 +00008import time
Guido van Rossum806c2462007-08-06 23:33:07 +00009import select
Guido van Rossumd8faa362007-04-27 19:54:29 +000010
11from unittest import TestCase
12from test import test_support
13
Guido van Rossum806c2462007-08-06 23:33:07 +000014# PORT is used to communicate the port number assigned to the server
15# to the test client
16HOST = "localhost"
17PORT = None
Guido van Rossumd8faa362007-04-27 19:54:29 +000018
Guido van Rossum806c2462007-08-06 23:33:07 +000019def server(evt, buf):
Guido van Rossumd8faa362007-04-27 19:54:29 +000020 try:
Guido van Rossum806c2462007-08-06 23:33:07 +000021 serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
22 serv.settimeout(3)
23 serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
24 serv.bind(("", 0))
25 global PORT
26 PORT = serv.getsockname()[1]
27 serv.listen(5)
Guido van Rossumd8faa362007-04-27 19:54:29 +000028 conn, addr = serv.accept()
29 except socket.timeout:
30 pass
31 else:
Guido van Rossum806c2462007-08-06 23:33:07 +000032 n = 500
33 while buf and n > 0:
34 r, w, e = select.select([], [conn], [])
35 if w:
36 sent = conn.send(buf)
37 buf = buf[sent:]
38
39 n -= 1
40 time.sleep(0.01)
41
Guido van Rossumd8faa362007-04-27 19:54:29 +000042 conn.close()
43 finally:
44 serv.close()
Guido van Rossum806c2462007-08-06 23:33:07 +000045 PORT = None
Guido van Rossumd8faa362007-04-27 19:54:29 +000046 evt.set()
47
48class GeneralTests(TestCase):
49
50 def setUp(self):
51 self.evt = threading.Event()
Guido van Rossum806c2462007-08-06 23:33:07 +000052 servargs = (self.evt, "220 Hola mundo\n")
53 threading.Thread(target=server, args=servargs).start()
54
55 # wait until server thread has assigned a port number
56 n = 500
57 while PORT is None and n > 0:
58 time.sleep(0.01)
59 n -= 1
60
61 # wait a little longer (sometimes connections are refused
62 # on slow machines without this additional wait)
63 time.sleep(0.5)
Guido van Rossumd8faa362007-04-27 19:54:29 +000064
65 def tearDown(self):
66 self.evt.wait()
67
Guido van Rossum806c2462007-08-06 23:33:07 +000068 def testBasic1(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +000069 # connects
Guido van Rossum806c2462007-08-06 23:33:07 +000070 smtp = smtplib.SMTP(HOST, PORT)
Guido van Rossumd8faa362007-04-27 19:54:29 +000071 smtp.sock.close()
72
Guido van Rossum806c2462007-08-06 23:33:07 +000073 def testBasic2(self):
74 # connects, include port in host name
75 smtp = smtplib.SMTP("%s:%s" % (HOST, PORT))
76 smtp.sock.close()
77
78 def testLocalHostName(self):
79 # check that supplied local_hostname is used
80 smtp = smtplib.SMTP(HOST, PORT, local_hostname="testhost")
81 self.assertEqual(smtp.local_hostname, "testhost")
82 smtp.sock.close()
83
84 def testNonnumericPort(self):
85 # check that non-numeric port raises ValueError
86 self.assertRaises(socket.error, smtplib.SMTP,
87 "localhost", "bogus")
88
Guido van Rossumd8faa362007-04-27 19:54:29 +000089 def testTimeoutDefault(self):
90 # default
Guido van Rossum806c2462007-08-06 23:33:07 +000091 smtp = smtplib.SMTP(HOST, PORT)
Guido van Rossumd8faa362007-04-27 19:54:29 +000092 self.assertTrue(smtp.sock.gettimeout() is None)
93 smtp.sock.close()
94
95 def testTimeoutValue(self):
96 # a value
Guido van Rossum806c2462007-08-06 23:33:07 +000097 smtp = smtplib.SMTP(HOST, PORT, timeout=30)
Guido van Rossumd8faa362007-04-27 19:54:29 +000098 self.assertEqual(smtp.sock.gettimeout(), 30)
99 smtp.sock.close()
100
101 def testTimeoutNone(self):
102 # None, having other default
103 previous = socket.getdefaulttimeout()
104 socket.setdefaulttimeout(30)
105 try:
Guido van Rossum806c2462007-08-06 23:33:07 +0000106 smtp = smtplib.SMTP(HOST, PORT, timeout=None)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000107 finally:
108 socket.setdefaulttimeout(previous)
109 self.assertEqual(smtp.sock.gettimeout(), 30)
110 smtp.sock.close()
111
112
Guido van Rossum806c2462007-08-06 23:33:07 +0000113# Test server using smtpd.DebuggingServer
114def debugging_server(serv_evt, client_evt):
115 serv = smtpd.DebuggingServer(("", 0), ('nowhere', -1))
116 global PORT
117 PORT = serv.getsockname()[1]
118
119 try:
120 if hasattr(select, 'poll'):
121 poll_fun = asyncore.poll2
122 else:
123 poll_fun = asyncore.poll
124
125 n = 1000
126 while asyncore.socket_map and n > 0:
127 poll_fun(0.01, asyncore.socket_map)
128
129 # when the client conversation is finished, it will
130 # set client_evt, and it's then ok to kill the server
131 if client_evt.isSet():
132 serv.close()
133 break
134
135 n -= 1
136
137 except socket.timeout:
138 pass
139 finally:
140 # allow some time for the client to read the result
141 time.sleep(0.5)
142 serv.close()
143 asyncore.close_all()
144 PORT = None
145 time.sleep(0.5)
146 serv_evt.set()
147
148MSG_BEGIN = '---------- MESSAGE FOLLOWS ----------\n'
149MSG_END = '------------ END MESSAGE ------------\n'
150
151# Test behavior of smtpd.DebuggingServer
152# NOTE: the SMTP objects are created with a non-default local_hostname
153# argument to the constructor, since (on some systems) the FQDN lookup
154# caused by the default local_hostname sometimes takes so long that the
155# test server times out, causing the test to fail.
156class DebuggingServerTests(TestCase):
157
158 def setUp(self):
159 # temporarily replace sys.stdout to capture DebuggingServer output
160 self.old_stdout = sys.stdout
161 self.output = io.StringIO()
162 sys.stdout = self.output
163
164 self.serv_evt = threading.Event()
165 self.client_evt = threading.Event()
166 serv_args = (self.serv_evt, self.client_evt)
167 threading.Thread(target=debugging_server, args=serv_args).start()
168
169 # wait until server thread has assigned a port number
170 n = 500
171 while PORT is None and n > 0:
172 time.sleep(0.01)
173 n -= 1
174
175 # wait a little longer (sometimes connections are refused
176 # on slow machines without this additional wait)
177 time.sleep(0.5)
178
179 def tearDown(self):
180 # indicate that the client is finished
181 self.client_evt.set()
182 # wait for the server thread to terminate
183 self.serv_evt.wait()
184 # restore sys.stdout
185 sys.stdout = self.old_stdout
186
187 def testBasic(self):
188 # connect
189 smtp = smtplib.SMTP(HOST, PORT, local_hostname='localhost', timeout=3)
190 smtp.quit()
191
192 def testEHLO(self):
193 smtp = smtplib.SMTP(HOST, PORT, local_hostname='localhost', timeout=3)
194 expected = (502, b'Error: command "EHLO" not implemented')
195 self.assertEqual(smtp.ehlo(), expected)
196 smtp.quit()
197
198 def testHELP(self):
199 smtp = smtplib.SMTP(HOST, PORT, local_hostname='localhost', timeout=3)
200 self.assertEqual(smtp.help(), b'Error: command "HELP" not implemented')
201 smtp.quit()
202
203 def testSend(self):
204 # connect and send mail
205 m = 'A test message'
206 smtp = smtplib.SMTP(HOST, PORT, local_hostname='localhost', timeout=3)
207 smtp.sendmail('John', 'Sally', m)
208 smtp.quit()
209
210 self.client_evt.set()
211 self.serv_evt.wait()
212 self.output.flush()
213 mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END)
214 self.assertEqual(self.output.getvalue(), mexpect)
215
216
217class BadHELOServerTests(TestCase):
218
219 def setUp(self):
220 self.old_stdout = sys.stdout
221 self.output = io.StringIO()
222 sys.stdout = self.output
223
224 self.evt = threading.Event()
225 servargs = (self.evt, b"199 no hello for you!\n")
226 threading.Thread(target=server, args=servargs).start()
227
228 # wait until server thread has assigned a port number
229 n = 500
230 while PORT is None and n > 0:
231 time.sleep(0.01)
232 n -= 1
233
234 # wait a little longer (sometimes connections are refused
235 # on slow machines without this additional wait)
236 time.sleep(0.5)
237
238 def tearDown(self):
239 self.evt.wait()
240 sys.stdout = self.old_stdout
241
242 def testFailingHELO(self):
243 self.assertRaises(smtplib.SMTPConnectError, smtplib.SMTP,
244 HOST, PORT, 'localhost', 3)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000245
246def test_main(verbose=None):
Guido van Rossum806c2462007-08-06 23:33:07 +0000247 test_support.run_unittest(GeneralTests, DebuggingServerTests,
248 BadHELOServerTests)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000249
250if __name__ == '__main__':
251 test_main()