blob: dab4fe0b45bf60bacda0054d119193d8d6a36c1e [file] [log] [blame]
Facundo Batista16ed5b42007-07-24 21:20:42 +00001import asyncore
Facundo Batista366d6262007-03-28 18:25:54 +00002import socket
3import threading
Facundo Batista16ed5b42007-07-24 21:20:42 +00004import smtpd
Facundo Batista366d6262007-03-28 18:25:54 +00005import smtplib
Facundo Batista16ed5b42007-07-24 21:20:42 +00006import StringIO
7import sys
Facundo Batista366d6262007-03-28 18:25:54 +00008import time
Facundo Batista16ed5b42007-07-24 21:20:42 +00009import select
Facundo Batista366d6262007-03-28 18:25:54 +000010
11from unittest import TestCase
12from test import test_support
13
Facundo Batista412b8b62007-08-01 23:18:36 +000014# PORT is used to communicate the port number assigned to the server
15# to the test client
Facundo Batista16ed5b42007-07-24 21:20:42 +000016HOST = "localhost"
Facundo Batista412b8b62007-08-01 23:18:36 +000017PORT = None
Facundo Batista366d6262007-03-28 18:25:54 +000018
Facundo Batista16ed5b42007-07-24 21:20:42 +000019def server(evt, buf):
Facundo Batista366d6262007-03-28 18:25:54 +000020 try:
Facundo Batista412b8b62007-08-01 23:18:36 +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)
Facundo Batista366d6262007-03-28 18:25:54 +000028 conn, addr = serv.accept()
29 except socket.timeout:
30 pass
31 else:
Facundo Batista412b8b62007-08-01 23:18:36 +000032 n = 500
Facundo Batista16ed5b42007-07-24 21:20:42 +000033 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
Facundo Batista366d6262007-03-28 18:25:54 +000042 conn.close()
43 finally:
44 serv.close()
Facundo Batista412b8b62007-08-01 23:18:36 +000045 PORT = None
Facundo Batista366d6262007-03-28 18:25:54 +000046 evt.set()
47
48class GeneralTests(TestCase):
Neal Norwitz0d4c06e2007-04-25 06:30:05 +000049
Facundo Batista366d6262007-03-28 18:25:54 +000050 def setUp(self):
51 self.evt = threading.Event()
Facundo Batista16ed5b42007-07-24 21:20:42 +000052 servargs = (self.evt, "220 Hola mundo\n")
53 threading.Thread(target=server, args=servargs).start()
Facundo Batista412b8b62007-08-01 23:18:36 +000054
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)
Facundo Batista366d6262007-03-28 18:25:54 +000064
65 def tearDown(self):
66 self.evt.wait()
67
Facundo Batista16ed5b42007-07-24 21:20:42 +000068 def testBasic1(self):
Facundo Batista366d6262007-03-28 18:25:54 +000069 # connects
Facundo Batista16ed5b42007-07-24 21:20:42 +000070 smtp = smtplib.SMTP(HOST, PORT)
Facundo Batista366d6262007-03-28 18:25:54 +000071 smtp.sock.close()
Neal Norwitz0d4c06e2007-04-25 06:30:05 +000072
Facundo Batista16ed5b42007-07-24 21:20:42 +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
Facundo Batista412b8b62007-08-01 23:18:36 +000086 self.assertRaises(socket.error, smtplib.SMTP,
87 "localhost", "bogus")
Facundo Batista16ed5b42007-07-24 21:20:42 +000088
Facundo Batista366d6262007-03-28 18:25:54 +000089 def testTimeoutDefault(self):
90 # default
Facundo Batista16ed5b42007-07-24 21:20:42 +000091 smtp = smtplib.SMTP(HOST, PORT)
Facundo Batista366d6262007-03-28 18:25:54 +000092 self.assertTrue(smtp.sock.gettimeout() is None)
93 smtp.sock.close()
Neal Norwitz0d4c06e2007-04-25 06:30:05 +000094
Facundo Batista366d6262007-03-28 18:25:54 +000095 def testTimeoutValue(self):
96 # a value
Facundo Batista16ed5b42007-07-24 21:20:42 +000097 smtp = smtplib.SMTP(HOST, PORT, timeout=30)
Facundo Batista366d6262007-03-28 18:25:54 +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:
Facundo Batista16ed5b42007-07-24 21:20:42 +0000106 smtp = smtplib.SMTP(HOST, PORT, timeout=None)
Facundo Batista366d6262007-03-28 18:25:54 +0000107 finally:
108 socket.setdefaulttimeout(previous)
109 self.assertEqual(smtp.sock.gettimeout(), 30)
110 smtp.sock.close()
111
112
Facundo Batista16ed5b42007-07-24 21:20:42 +0000113# Test server using smtpd.DebuggingServer
Facundo Batista412b8b62007-08-01 23:18:36 +0000114def debugging_server(serv_evt, client_evt):
115 serv = smtpd.DebuggingServer(("", 0), ('nowhere', -1))
116 global PORT
117 PORT = serv.getsockname()[1]
Facundo Batista16ed5b42007-07-24 21:20:42 +0000118
119 try:
Facundo Batista412b8b62007-08-01 23:18:36 +0000120 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
Facundo Batista16ed5b42007-07-24 21:20:42 +0000137 except socket.timeout:
138 pass
139 finally:
140 # allow some time for the client to read the result
141 time.sleep(0.5)
Facundo Batista412b8b62007-08-01 23:18:36 +0000142 serv.close()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000143 asyncore.close_all()
Facundo Batista412b8b62007-08-01 23:18:36 +0000144 PORT = None
145 time.sleep(0.5)
146 serv_evt.set()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000147
148MSG_BEGIN = '---------- MESSAGE FOLLOWS ----------\n'
149MSG_END = '------------ END MESSAGE ------------\n'
150
151# Test behavior of smtpd.DebuggingServer
Facundo Batista412b8b62007-08-01 23:18:36 +0000152# 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.
Facundo Batista16ed5b42007-07-24 21:20:42 +0000156class DebuggingServerTests(TestCase):
157
158 def setUp(self):
Facundo Batista412b8b62007-08-01 23:18:36 +0000159 # temporarily replace sys.stdout to capture DebuggingServer output
Facundo Batista16ed5b42007-07-24 21:20:42 +0000160 self.old_stdout = sys.stdout
161 self.output = StringIO.StringIO()
162 sys.stdout = self.output
163
Facundo Batista412b8b62007-08-01 23:18:36 +0000164 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)
Facundo Batista16ed5b42007-07-24 21:20:42 +0000178
179 def tearDown(self):
Facundo Batista412b8b62007-08-01 23:18:36 +0000180 # 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
Facundo Batista16ed5b42007-07-24 21:20:42 +0000185 sys.stdout = self.old_stdout
186
187 def testBasic(self):
188 # connect
Facundo Batista412b8b62007-08-01 23:18:36 +0000189 smtp = smtplib.SMTP(HOST, PORT, local_hostname='localhost', timeout=3)
190 smtp.quit()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000191
192 def testEHLO(self):
Facundo Batista412b8b62007-08-01 23:18:36 +0000193 smtp = smtplib.SMTP(HOST, PORT, local_hostname='localhost', timeout=3)
194 expected = (502, 'Error: command "EHLO" not implemented')
195 self.assertEqual(smtp.ehlo(), expected)
196 smtp.quit()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000197
198 def testHELP(self):
Facundo Batista412b8b62007-08-01 23:18:36 +0000199 smtp = smtplib.SMTP(HOST, PORT, local_hostname='localhost', timeout=3)
Facundo Batista16ed5b42007-07-24 21:20:42 +0000200 self.assertEqual(smtp.help(), 'Error: command "HELP" not implemented')
Facundo Batista412b8b62007-08-01 23:18:36 +0000201 smtp.quit()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000202
203 def testSend(self):
204 # connect and send mail
205 m = 'A test message'
Facundo Batista412b8b62007-08-01 23:18:36 +0000206 smtp = smtplib.SMTP(HOST, PORT, local_hostname='localhost', timeout=3)
Facundo Batista16ed5b42007-07-24 21:20:42 +0000207 smtp.sendmail('John', 'Sally', m)
Facundo Batista412b8b62007-08-01 23:18:36 +0000208 smtp.quit()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000209
Facundo Batista412b8b62007-08-01 23:18:36 +0000210 self.client_evt.set()
211 self.serv_evt.wait()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000212 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 = StringIO.StringIO()
222 sys.stdout = self.output
223
224 self.evt = threading.Event()
225 servargs = (self.evt, "199 no hello for you!\n")
226 threading.Thread(target=server, args=servargs).start()
Facundo Batista412b8b62007-08-01 23:18:36 +0000227
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)
Facundo Batista16ed5b42007-07-24 21:20:42 +0000237
238 def tearDown(self):
239 self.evt.wait()
240 sys.stdout = self.old_stdout
241
242 def testFailingHELO(self):
Facundo Batista412b8b62007-08-01 23:18:36 +0000243 self.assertRaises(smtplib.SMTPConnectError, smtplib.SMTP,
244 HOST, PORT, 'localhost', 3)
Facundo Batista366d6262007-03-28 18:25:54 +0000245
246def test_main(verbose=None):
Facundo Batista412b8b62007-08-01 23:18:36 +0000247 test_support.run_unittest(GeneralTests, DebuggingServerTests,
248 BadHELOServerTests)
Facundo Batista366d6262007-03-28 18:25:54 +0000249
250if __name__ == '__main__':
251 test_main()