2007-07-24 18:20:42 -03:00
|
|
|
import asyncore
|
2007-08-21 13:57:18 -03:00
|
|
|
import email.utils
|
2007-03-28 15:25:54 -03:00
|
|
|
import socket
|
2007-07-24 18:20:42 -03:00
|
|
|
import smtpd
|
2007-03-28 15:25:54 -03:00
|
|
|
import smtplib
|
2007-07-24 18:20:42 -03:00
|
|
|
import StringIO
|
|
|
|
import sys
|
2007-03-28 15:25:54 -03:00
|
|
|
import time
|
2007-07-24 18:20:42 -03:00
|
|
|
import select
|
2007-03-28 15:25:54 -03:00
|
|
|
|
2010-04-27 20:55:59 -03:00
|
|
|
import unittest
|
2007-03-28 15:25:54 -03:00
|
|
|
from test import test_support
|
|
|
|
|
2010-04-27 20:55:59 -03:00
|
|
|
try:
|
|
|
|
import threading
|
|
|
|
except ImportError:
|
|
|
|
threading = None
|
|
|
|
|
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
2008-04-08 20:47:30 -03:00
|
|
|
HOST = test_support.HOST
|
|
|
|
|
|
|
|
def server(evt, buf, serv):
|
2008-02-26 04:04:59 -04:00
|
|
|
serv.listen(5)
|
|
|
|
evt.set()
|
2007-03-28 15:25:54 -03:00
|
|
|
try:
|
|
|
|
conn, addr = serv.accept()
|
|
|
|
except socket.timeout:
|
|
|
|
pass
|
|
|
|
else:
|
2007-08-01 20:18:36 -03:00
|
|
|
n = 500
|
2007-07-24 18:20:42 -03:00
|
|
|
while buf and n > 0:
|
|
|
|
r, w, e = select.select([], [conn], [])
|
|
|
|
if w:
|
|
|
|
sent = conn.send(buf)
|
|
|
|
buf = buf[sent:]
|
|
|
|
|
|
|
|
n -= 1
|
|
|
|
|
2007-03-28 15:25:54 -03:00
|
|
|
conn.close()
|
|
|
|
finally:
|
|
|
|
serv.close()
|
|
|
|
evt.set()
|
|
|
|
|
2010-04-27 20:55:59 -03:00
|
|
|
@unittest.skipUnless(threading, 'Threading required for this test.')
|
|
|
|
class GeneralTests(unittest.TestCase):
|
2007-04-25 03:30:05 -03:00
|
|
|
|
2007-03-28 15:25:54 -03:00
|
|
|
def setUp(self):
|
2009-10-27 16:47:30 -03:00
|
|
|
self._threads = test_support.threading_setup()
|
2007-03-28 15:25:54 -03:00
|
|
|
self.evt = threading.Event()
|
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
2008-04-08 20:47:30 -03:00
|
|
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
self.sock.settimeout(15)
|
|
|
|
self.port = test_support.bind_port(self.sock)
|
|
|
|
servargs = (self.evt, "220 Hola mundo\n", self.sock)
|
2009-10-27 16:47:30 -03:00
|
|
|
self.thread = threading.Thread(target=server, args=servargs)
|
|
|
|
self.thread.start()
|
2008-02-26 04:04:59 -04:00
|
|
|
self.evt.wait()
|
|
|
|
self.evt.clear()
|
2007-03-28 15:25:54 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.evt.wait()
|
2009-10-27 16:47:30 -03:00
|
|
|
self.thread.join()
|
|
|
|
test_support.threading_cleanup(*self._threads)
|
2007-03-28 15:25:54 -03:00
|
|
|
|
2007-07-24 18:20:42 -03:00
|
|
|
def testBasic1(self):
|
2007-03-28 15:25:54 -03:00
|
|
|
# connects
|
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
2008-04-08 20:47:30 -03:00
|
|
|
smtp = smtplib.SMTP(HOST, self.port)
|
2008-05-29 13:39:26 -03:00
|
|
|
smtp.close()
|
2007-04-25 03:30:05 -03:00
|
|
|
|
2007-07-24 18:20:42 -03:00
|
|
|
def testBasic2(self):
|
|
|
|
# connects, include port in host name
|
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
2008-04-08 20:47:30 -03:00
|
|
|
smtp = smtplib.SMTP("%s:%s" % (HOST, self.port))
|
2008-05-29 13:39:26 -03:00
|
|
|
smtp.close()
|
2007-07-24 18:20:42 -03:00
|
|
|
|
|
|
|
def testLocalHostName(self):
|
|
|
|
# check that supplied local_hostname is used
|
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
2008-04-08 20:47:30 -03:00
|
|
|
smtp = smtplib.SMTP(HOST, self.port, local_hostname="testhost")
|
2007-07-24 18:20:42 -03:00
|
|
|
self.assertEqual(smtp.local_hostname, "testhost")
|
2008-05-29 13:39:26 -03:00
|
|
|
smtp.close()
|
2007-07-24 18:20:42 -03:00
|
|
|
|
2007-03-28 15:25:54 -03:00
|
|
|
def testTimeoutDefault(self):
|
2014-02-08 09:05:53 -04:00
|
|
|
self.assertIsNone(socket.getdefaulttimeout())
|
2008-05-29 13:39:26 -03:00
|
|
|
socket.setdefaulttimeout(30)
|
|
|
|
try:
|
|
|
|
smtp = smtplib.SMTP(HOST, self.port)
|
|
|
|
finally:
|
|
|
|
socket.setdefaulttimeout(None)
|
2007-03-28 15:25:54 -03:00
|
|
|
self.assertEqual(smtp.sock.gettimeout(), 30)
|
2008-05-29 13:39:26 -03:00
|
|
|
smtp.close()
|
2007-03-28 15:25:54 -03:00
|
|
|
|
|
|
|
def testTimeoutNone(self):
|
2014-02-08 09:05:53 -04:00
|
|
|
self.assertIsNone(socket.getdefaulttimeout())
|
2007-03-28 15:25:54 -03:00
|
|
|
socket.setdefaulttimeout(30)
|
|
|
|
try:
|
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
2008-04-08 20:47:30 -03:00
|
|
|
smtp = smtplib.SMTP(HOST, self.port, timeout=None)
|
2007-03-28 15:25:54 -03:00
|
|
|
finally:
|
2008-05-29 13:39:26 -03:00
|
|
|
socket.setdefaulttimeout(None)
|
2014-02-08 09:05:53 -04:00
|
|
|
self.assertIsNone(smtp.sock.gettimeout())
|
2008-05-29 13:39:26 -03:00
|
|
|
smtp.close()
|
|
|
|
|
|
|
|
def testTimeoutValue(self):
|
|
|
|
smtp = smtplib.SMTP(HOST, self.port, timeout=30)
|
2007-03-28 15:25:54 -03:00
|
|
|
self.assertEqual(smtp.sock.gettimeout(), 30)
|
2008-05-29 13:39:26 -03:00
|
|
|
smtp.close()
|
2007-03-28 15:25:54 -03:00
|
|
|
|
|
|
|
|
2007-08-21 13:57:18 -03:00
|
|
|
# Test server thread using the specified SMTP server class
|
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
2008-04-08 20:47:30 -03:00
|
|
|
def debugging_server(serv, serv_evt, client_evt):
|
2008-02-26 04:04:59 -04:00
|
|
|
serv_evt.set()
|
2007-07-24 18:20:42 -03:00
|
|
|
|
|
|
|
try:
|
2007-08-01 20:18:36 -03:00
|
|
|
if hasattr(select, 'poll'):
|
|
|
|
poll_fun = asyncore.poll2
|
|
|
|
else:
|
|
|
|
poll_fun = asyncore.poll
|
|
|
|
|
|
|
|
n = 1000
|
|
|
|
while asyncore.socket_map and n > 0:
|
|
|
|
poll_fun(0.01, asyncore.socket_map)
|
|
|
|
|
|
|
|
# when the client conversation is finished, it will
|
|
|
|
# set client_evt, and it's then ok to kill the server
|
2008-06-11 14:27:50 -03:00
|
|
|
if client_evt.is_set():
|
2007-08-01 20:18:36 -03:00
|
|
|
serv.close()
|
|
|
|
break
|
|
|
|
|
|
|
|
n -= 1
|
|
|
|
|
2007-07-24 18:20:42 -03:00
|
|
|
except socket.timeout:
|
|
|
|
pass
|
|
|
|
finally:
|
2008-06-11 14:27:50 -03:00
|
|
|
if not client_evt.is_set():
|
2008-02-26 04:04:59 -04:00
|
|
|
# allow some time for the client to read the result
|
|
|
|
time.sleep(0.5)
|
|
|
|
serv.close()
|
2007-07-24 18:20:42 -03:00
|
|
|
asyncore.close_all()
|
2007-08-01 20:18:36 -03:00
|
|
|
serv_evt.set()
|
2007-07-24 18:20:42 -03:00
|
|
|
|
|
|
|
MSG_BEGIN = '---------- MESSAGE FOLLOWS ----------\n'
|
|
|
|
MSG_END = '------------ END MESSAGE ------------\n'
|
|
|
|
|
2007-08-21 13:57:18 -03:00
|
|
|
# NOTE: Some SMTP objects in the tests below are created with a non-default
|
|
|
|
# local_hostname argument to the constructor, since (on some systems) the FQDN
|
|
|
|
# lookup caused by the default local_hostname sometimes takes so long that the
|
2007-08-01 20:18:36 -03:00
|
|
|
# test server times out, causing the test to fail.
|
2007-08-21 13:57:18 -03:00
|
|
|
|
|
|
|
# Test behavior of smtpd.DebuggingServer
|
2010-04-27 20:55:59 -03:00
|
|
|
@unittest.skipUnless(threading, 'Threading required for this test.')
|
|
|
|
class DebuggingServerTests(unittest.TestCase):
|
2007-07-24 18:20:42 -03:00
|
|
|
|
|
|
|
def setUp(self):
|
2007-08-01 20:18:36 -03:00
|
|
|
# temporarily replace sys.stdout to capture DebuggingServer output
|
2007-07-24 18:20:42 -03:00
|
|
|
self.old_stdout = sys.stdout
|
|
|
|
self.output = StringIO.StringIO()
|
|
|
|
sys.stdout = self.output
|
|
|
|
|
2009-10-27 16:47:30 -03:00
|
|
|
self._threads = test_support.threading_setup()
|
2007-08-01 20:18:36 -03:00
|
|
|
self.serv_evt = threading.Event()
|
|
|
|
self.client_evt = threading.Event()
|
2010-04-30 20:08:48 -03:00
|
|
|
# Pick a random unused port by passing 0 for the port number
|
|
|
|
self.serv = smtpd.DebuggingServer((HOST, 0), ('nowhere', -1))
|
|
|
|
# Keep a note of what port was assigned
|
|
|
|
self.port = self.serv.socket.getsockname()[1]
|
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
2008-04-08 20:47:30 -03:00
|
|
|
serv_args = (self.serv, self.serv_evt, self.client_evt)
|
2009-10-27 16:47:30 -03:00
|
|
|
self.thread = threading.Thread(target=debugging_server, args=serv_args)
|
|
|
|
self.thread.start()
|
2007-08-01 20:18:36 -03:00
|
|
|
|
|
|
|
# wait until server thread has assigned a port number
|
2008-02-26 04:04:59 -04:00
|
|
|
self.serv_evt.wait()
|
|
|
|
self.serv_evt.clear()
|
2007-07-24 18:20:42 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
2007-08-01 20:18:36 -03:00
|
|
|
# indicate that the client is finished
|
|
|
|
self.client_evt.set()
|
|
|
|
# wait for the server thread to terminate
|
|
|
|
self.serv_evt.wait()
|
2009-10-27 16:47:30 -03:00
|
|
|
self.thread.join()
|
|
|
|
test_support.threading_cleanup(*self._threads)
|
2007-08-01 20:18:36 -03:00
|
|
|
# restore sys.stdout
|
2007-07-24 18:20:42 -03:00
|
|
|
sys.stdout = self.old_stdout
|
|
|
|
|
|
|
|
def testBasic(self):
|
|
|
|
# connect
|
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
2008-04-08 20:47:30 -03:00
|
|
|
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
|
2007-08-01 20:18:36 -03:00
|
|
|
smtp.quit()
|
2007-07-24 18:20:42 -03:00
|
|
|
|
2007-08-21 13:57:18 -03:00
|
|
|
def testNOOP(self):
|
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
2008-04-08 20:47:30 -03:00
|
|
|
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
|
2007-08-21 13:57:18 -03:00
|
|
|
expected = (250, 'Ok')
|
|
|
|
self.assertEqual(smtp.noop(), expected)
|
|
|
|
smtp.quit()
|
|
|
|
|
|
|
|
def testRSET(self):
|
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
2008-04-08 20:47:30 -03:00
|
|
|
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
|
2007-08-21 13:57:18 -03:00
|
|
|
expected = (250, 'Ok')
|
|
|
|
self.assertEqual(smtp.rset(), expected)
|
|
|
|
smtp.quit()
|
|
|
|
|
|
|
|
def testNotImplemented(self):
|
|
|
|
# EHLO isn't implemented in DebuggingServer
|
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
2008-04-08 20:47:30 -03:00
|
|
|
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
|
2007-08-01 20:18:36 -03:00
|
|
|
expected = (502, 'Error: command "EHLO" not implemented')
|
|
|
|
self.assertEqual(smtp.ehlo(), expected)
|
|
|
|
smtp.quit()
|
2007-07-24 18:20:42 -03:00
|
|
|
|
2007-08-21 13:57:18 -03:00
|
|
|
def testVRFY(self):
|
|
|
|
# VRFY isn't implemented in DebuggingServer
|
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
2008-04-08 20:47:30 -03:00
|
|
|
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
|
2007-08-21 13:57:18 -03:00
|
|
|
expected = (502, 'Error: command "VRFY" not implemented')
|
|
|
|
self.assertEqual(smtp.vrfy('nobody@nowhere.com'), expected)
|
|
|
|
self.assertEqual(smtp.verify('nobody@nowhere.com'), expected)
|
|
|
|
smtp.quit()
|
|
|
|
|
|
|
|
def testSecondHELO(self):
|
|
|
|
# check that a second HELO returns a message that it's a duplicate
|
|
|
|
# (this behavior is specific to smtpd.SMTPChannel)
|
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
2008-04-08 20:47:30 -03:00
|
|
|
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
|
2007-08-21 13:57:18 -03:00
|
|
|
smtp.helo()
|
|
|
|
expected = (503, 'Duplicate HELO/EHLO')
|
|
|
|
self.assertEqual(smtp.helo(), expected)
|
|
|
|
smtp.quit()
|
|
|
|
|
2007-07-24 18:20:42 -03:00
|
|
|
def testHELP(self):
|
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
2008-04-08 20:47:30 -03:00
|
|
|
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
|
2007-07-24 18:20:42 -03:00
|
|
|
self.assertEqual(smtp.help(), 'Error: command "HELP" not implemented')
|
2007-08-01 20:18:36 -03:00
|
|
|
smtp.quit()
|
2007-07-24 18:20:42 -03:00
|
|
|
|
|
|
|
def testSend(self):
|
|
|
|
# connect and send mail
|
|
|
|
m = 'A test message'
|
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
2008-04-08 20:47:30 -03:00
|
|
|
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
|
2007-07-24 18:20:42 -03:00
|
|
|
smtp.sendmail('John', 'Sally', m)
|
2008-08-25 00:52:40 -03:00
|
|
|
# XXX(nnorwitz): this test is flaky and dies with a bad file descriptor
|
|
|
|
# in asyncore. This sleep might help, but should really be fixed
|
|
|
|
# properly by using an Event variable.
|
|
|
|
time.sleep(0.01)
|
2007-08-01 20:18:36 -03:00
|
|
|
smtp.quit()
|
2007-07-24 18:20:42 -03:00
|
|
|
|
2007-08-01 20:18:36 -03:00
|
|
|
self.client_evt.set()
|
|
|
|
self.serv_evt.wait()
|
2007-07-24 18:20:42 -03:00
|
|
|
self.output.flush()
|
|
|
|
mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END)
|
|
|
|
self.assertEqual(self.output.getvalue(), mexpect)
|
|
|
|
|
|
|
|
|
2010-04-27 20:55:59 -03:00
|
|
|
class NonConnectingTests(unittest.TestCase):
|
2008-02-26 04:04:59 -04:00
|
|
|
|
|
|
|
def testNotConnected(self):
|
|
|
|
# Test various operations on an unconnected SMTP object that
|
|
|
|
# should raise exceptions (at present the attempt in SMTP.send
|
|
|
|
# to reference the nonexistent 'sock' attribute of the SMTP object
|
|
|
|
# causes an AttributeError)
|
|
|
|
smtp = smtplib.SMTP()
|
|
|
|
self.assertRaises(smtplib.SMTPServerDisconnected, smtp.ehlo)
|
|
|
|
self.assertRaises(smtplib.SMTPServerDisconnected,
|
|
|
|
smtp.send, 'test msg')
|
|
|
|
|
|
|
|
def testNonnumericPort(self):
|
|
|
|
# check that non-numeric port raises socket.error
|
|
|
|
self.assertRaises(socket.error, smtplib.SMTP,
|
|
|
|
"localhost", "bogus")
|
|
|
|
self.assertRaises(socket.error, smtplib.SMTP,
|
|
|
|
"localhost:bogus")
|
|
|
|
|
|
|
|
|
2007-08-21 13:57:18 -03:00
|
|
|
# test response of client to a non-successful HELO message
|
2010-04-27 20:55:59 -03:00
|
|
|
@unittest.skipUnless(threading, 'Threading required for this test.')
|
|
|
|
class BadHELOServerTests(unittest.TestCase):
|
2007-07-24 18:20:42 -03:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.old_stdout = sys.stdout
|
|
|
|
self.output = StringIO.StringIO()
|
|
|
|
sys.stdout = self.output
|
|
|
|
|
2009-10-27 16:47:30 -03:00
|
|
|
self._threads = test_support.threading_setup()
|
2007-07-24 18:20:42 -03:00
|
|
|
self.evt = threading.Event()
|
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
2008-04-08 20:47:30 -03:00
|
|
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
self.sock.settimeout(15)
|
|
|
|
self.port = test_support.bind_port(self.sock)
|
|
|
|
servargs = (self.evt, "199 no hello for you!\n", self.sock)
|
2009-10-27 16:47:30 -03:00
|
|
|
self.thread = threading.Thread(target=server, args=servargs)
|
|
|
|
self.thread.start()
|
2008-02-26 04:04:59 -04:00
|
|
|
self.evt.wait()
|
|
|
|
self.evt.clear()
|
2007-07-24 18:20:42 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.evt.wait()
|
2009-10-27 16:47:30 -03:00
|
|
|
self.thread.join()
|
|
|
|
test_support.threading_cleanup(*self._threads)
|
2007-07-24 18:20:42 -03:00
|
|
|
sys.stdout = self.old_stdout
|
|
|
|
|
|
|
|
def testFailingHELO(self):
|
2007-08-01 20:18:36 -03:00
|
|
|
self.assertRaises(smtplib.SMTPConnectError, smtplib.SMTP,
|
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
2008-04-08 20:47:30 -03:00
|
|
|
HOST, self.port, 'localhost', 3)
|
2007-03-28 15:25:54 -03:00
|
|
|
|
2007-08-21 13:57:18 -03:00
|
|
|
|
|
|
|
sim_users = {'Mr.A@somewhere.com':'John A',
|
|
|
|
'Ms.B@somewhere.com':'Sally B',
|
|
|
|
'Mrs.C@somewhereesle.com':'Ruth C',
|
|
|
|
}
|
|
|
|
|
2009-05-23 18:48:06 -03:00
|
|
|
sim_auth = ('Mr.A@somewhere.com', 'somepassword')
|
2009-05-28 15:49:23 -03:00
|
|
|
sim_cram_md5_challenge = ('PENCeUxFREJoU0NnbmhNWitOMjNGNn'
|
|
|
|
'dAZWx3b29kLmlubm9zb2Z0LmNvbT4=')
|
|
|
|
sim_auth_credentials = {
|
|
|
|
'login': 'TXIuQUBzb21ld2hlcmUuY29t',
|
|
|
|
'plain': 'AE1yLkFAc29tZXdoZXJlLmNvbQBzb21lcGFzc3dvcmQ=',
|
|
|
|
'cram-md5': ('TXIUQUBZB21LD2HLCMUUY29TIDG4OWQ0MJ'
|
|
|
|
'KWZGQ4ODNMNDA4NTGXMDRLZWMYZJDMODG1'),
|
|
|
|
}
|
|
|
|
sim_auth_login_password = 'C29TZXBHC3N3B3JK'
|
2009-05-23 18:48:06 -03:00
|
|
|
|
2007-08-21 13:57:18 -03:00
|
|
|
sim_lists = {'list-1':['Mr.A@somewhere.com','Mrs.C@somewhereesle.com'],
|
|
|
|
'list-2':['Ms.B@somewhere.com',],
|
|
|
|
}
|
|
|
|
|
|
|
|
# Simulated SMTP channel & server
|
|
|
|
class SimSMTPChannel(smtpd.SMTPChannel):
|
2009-05-28 15:49:23 -03:00
|
|
|
|
2009-05-29 14:31:05 -03:00
|
|
|
def __init__(self, extra_features, *args, **kw):
|
|
|
|
self._extrafeatures = ''.join(
|
|
|
|
[ "250-{0}\r\n".format(x) for x in extra_features ])
|
2009-05-28 15:49:23 -03:00
|
|
|
smtpd.SMTPChannel.__init__(self, *args, **kw)
|
|
|
|
|
2007-08-21 13:57:18 -03:00
|
|
|
def smtp_EHLO(self, arg):
|
2009-05-28 15:49:23 -03:00
|
|
|
resp = ('250-testhost\r\n'
|
|
|
|
'250-EXPN\r\n'
|
|
|
|
'250-SIZE 20000000\r\n'
|
|
|
|
'250-STARTTLS\r\n'
|
|
|
|
'250-DELIVERBY\r\n')
|
|
|
|
resp = resp + self._extrafeatures + '250 HELP'
|
2007-08-21 13:57:18 -03:00
|
|
|
self.push(resp)
|
|
|
|
|
|
|
|
def smtp_VRFY(self, arg):
|
2011-07-18 22:34:04 -03:00
|
|
|
# For max compatibility smtplib should be sending the raw address.
|
|
|
|
if arg in sim_users:
|
|
|
|
self.push('250 %s %s' % (sim_users[arg], smtplib.quoteaddr(arg)))
|
2007-08-21 13:57:18 -03:00
|
|
|
else:
|
|
|
|
self.push('550 No such user: %s' % arg)
|
|
|
|
|
|
|
|
def smtp_EXPN(self, arg):
|
2011-07-18 22:34:04 -03:00
|
|
|
list_name = arg.lower()
|
2007-08-21 13:57:18 -03:00
|
|
|
if list_name in sim_lists:
|
|
|
|
user_list = sim_lists[list_name]
|
|
|
|
for n, user_email in enumerate(user_list):
|
|
|
|
quoted_addr = smtplib.quoteaddr(user_email)
|
|
|
|
if n < len(user_list) - 1:
|
|
|
|
self.push('250-%s %s' % (sim_users[user_email], quoted_addr))
|
|
|
|
else:
|
|
|
|
self.push('250 %s %s' % (sim_users[user_email], quoted_addr))
|
|
|
|
else:
|
|
|
|
self.push('550 No access for you!')
|
|
|
|
|
2009-05-23 18:48:06 -03:00
|
|
|
def smtp_AUTH(self, arg):
|
2009-05-28 15:49:23 -03:00
|
|
|
if arg.strip().lower()=='cram-md5':
|
|
|
|
self.push('334 {0}'.format(sim_cram_md5_challenge))
|
|
|
|
return
|
2009-05-23 18:48:06 -03:00
|
|
|
mech, auth = arg.split()
|
2009-05-28 15:49:23 -03:00
|
|
|
mech = mech.lower()
|
|
|
|
if mech not in sim_auth_credentials:
|
2009-05-23 18:48:06 -03:00
|
|
|
self.push('504 auth type unimplemented')
|
2009-05-28 15:49:23 -03:00
|
|
|
return
|
|
|
|
if mech == 'plain' and auth==sim_auth_credentials['plain']:
|
|
|
|
self.push('235 plain auth ok')
|
|
|
|
elif mech=='login' and auth==sim_auth_credentials['login']:
|
|
|
|
self.push('334 Password:')
|
|
|
|
else:
|
|
|
|
self.push('550 No access for you!')
|
2009-05-23 18:48:06 -03:00
|
|
|
|
2010-05-06 17:19:32 -03:00
|
|
|
def handle_error(self):
|
|
|
|
raise
|
|
|
|
|
2007-08-21 13:57:18 -03:00
|
|
|
|
|
|
|
class SimSMTPServer(smtpd.SMTPServer):
|
2009-05-28 15:49:23 -03:00
|
|
|
|
2009-05-29 14:31:05 -03:00
|
|
|
def __init__(self, *args, **kw):
|
|
|
|
self._extra_features = []
|
|
|
|
smtpd.SMTPServer.__init__(self, *args, **kw)
|
|
|
|
|
2007-08-21 13:57:18 -03:00
|
|
|
def handle_accept(self):
|
|
|
|
conn, addr = self.accept()
|
2009-05-29 14:31:05 -03:00
|
|
|
self._SMTPchannel = SimSMTPChannel(self._extra_features,
|
|
|
|
self, conn, addr)
|
2007-08-21 13:57:18 -03:00
|
|
|
|
|
|
|
def process_message(self, peer, mailfrom, rcpttos, data):
|
|
|
|
pass
|
|
|
|
|
2009-05-28 15:49:23 -03:00
|
|
|
def add_feature(self, feature):
|
2009-05-29 14:31:05 -03:00
|
|
|
self._extra_features.append(feature)
|
2009-05-28 15:49:23 -03:00
|
|
|
|
2010-05-06 17:19:32 -03:00
|
|
|
def handle_error(self):
|
|
|
|
raise
|
|
|
|
|
2007-08-21 13:57:18 -03:00
|
|
|
|
|
|
|
# Test various SMTP & ESMTP commands/behaviors that require a simulated server
|
|
|
|
# (i.e., something with more features than DebuggingServer)
|
2010-04-27 20:55:59 -03:00
|
|
|
@unittest.skipUnless(threading, 'Threading required for this test.')
|
|
|
|
class SMTPSimTests(unittest.TestCase):
|
2007-08-21 13:57:18 -03:00
|
|
|
|
|
|
|
def setUp(self):
|
2009-10-27 16:47:30 -03:00
|
|
|
self._threads = test_support.threading_setup()
|
2007-08-21 13:57:18 -03:00
|
|
|
self.serv_evt = threading.Event()
|
|
|
|
self.client_evt = threading.Event()
|
2010-04-30 20:08:48 -03:00
|
|
|
# Pick a random unused port by passing 0 for the port number
|
|
|
|
self.serv = SimSMTPServer((HOST, 0), ('nowhere', -1))
|
|
|
|
# Keep a note of what port was assigned
|
|
|
|
self.port = self.serv.socket.getsockname()[1]
|
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
2008-04-08 20:47:30 -03:00
|
|
|
serv_args = (self.serv, self.serv_evt, self.client_evt)
|
2009-10-27 16:47:30 -03:00
|
|
|
self.thread = threading.Thread(target=debugging_server, args=serv_args)
|
|
|
|
self.thread.start()
|
2007-08-21 13:57:18 -03:00
|
|
|
|
|
|
|
# wait until server thread has assigned a port number
|
2008-02-26 04:04:59 -04:00
|
|
|
self.serv_evt.wait()
|
|
|
|
self.serv_evt.clear()
|
2007-08-21 13:57:18 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
# indicate that the client is finished
|
|
|
|
self.client_evt.set()
|
|
|
|
# wait for the server thread to terminate
|
|
|
|
self.serv_evt.wait()
|
2009-10-27 16:47:30 -03:00
|
|
|
self.thread.join()
|
|
|
|
test_support.threading_cleanup(*self._threads)
|
2007-08-21 13:57:18 -03:00
|
|
|
|
|
|
|
def testBasic(self):
|
|
|
|
# smoke test
|
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
2008-04-08 20:47:30 -03:00
|
|
|
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
|
2007-08-21 13:57:18 -03:00
|
|
|
smtp.quit()
|
|
|
|
|
|
|
|
def testEHLO(self):
|
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
2008-04-08 20:47:30 -03:00
|
|
|
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
|
2007-08-21 13:57:18 -03:00
|
|
|
|
|
|
|
# no features should be present before the EHLO
|
|
|
|
self.assertEqual(smtp.esmtp_features, {})
|
|
|
|
|
|
|
|
# features expected from the test server
|
|
|
|
expected_features = {'expn':'',
|
|
|
|
'size': '20000000',
|
|
|
|
'starttls': '',
|
|
|
|
'deliverby': '',
|
|
|
|
'help': '',
|
|
|
|
}
|
|
|
|
|
|
|
|
smtp.ehlo()
|
|
|
|
self.assertEqual(smtp.esmtp_features, expected_features)
|
|
|
|
for k in expected_features:
|
|
|
|
self.assertTrue(smtp.has_extn(k))
|
|
|
|
self.assertFalse(smtp.has_extn('unsupported-feature'))
|
|
|
|
smtp.quit()
|
|
|
|
|
|
|
|
def testVRFY(self):
|
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
2008-04-08 20:47:30 -03:00
|
|
|
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
|
2007-08-21 13:57:18 -03:00
|
|
|
|
|
|
|
for email, name in sim_users.items():
|
|
|
|
expected_known = (250, '%s %s' % (name, smtplib.quoteaddr(email)))
|
|
|
|
self.assertEqual(smtp.vrfy(email), expected_known)
|
|
|
|
|
|
|
|
u = 'nobody@nowhere.com'
|
2011-07-18 22:34:04 -03:00
|
|
|
expected_unknown = (550, 'No such user: %s' % u)
|
2007-08-21 13:57:18 -03:00
|
|
|
self.assertEqual(smtp.vrfy(u), expected_unknown)
|
|
|
|
smtp.quit()
|
|
|
|
|
|
|
|
def testEXPN(self):
|
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
2008-04-08 20:47:30 -03:00
|
|
|
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
|
2007-08-21 13:57:18 -03:00
|
|
|
|
|
|
|
for listname, members in sim_lists.items():
|
|
|
|
users = []
|
|
|
|
for m in members:
|
|
|
|
users.append('%s %s' % (sim_users[m], smtplib.quoteaddr(m)))
|
|
|
|
expected_known = (250, '\n'.join(users))
|
|
|
|
self.assertEqual(smtp.expn(listname), expected_known)
|
|
|
|
|
|
|
|
u = 'PSU-Members-List'
|
|
|
|
expected_unknown = (550, 'No access for you!')
|
|
|
|
self.assertEqual(smtp.expn(u), expected_unknown)
|
|
|
|
smtp.quit()
|
|
|
|
|
2009-05-28 15:49:23 -03:00
|
|
|
def testAUTH_PLAIN(self):
|
|
|
|
self.serv.add_feature("AUTH PLAIN")
|
2009-05-29 14:31:05 -03:00
|
|
|
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
|
2009-05-23 18:48:06 -03:00
|
|
|
|
2009-05-28 15:49:23 -03:00
|
|
|
expected_auth_ok = (235, b'plain auth ok')
|
2009-05-23 18:48:06 -03:00
|
|
|
self.assertEqual(smtp.login(sim_auth[0], sim_auth[1]), expected_auth_ok)
|
2007-08-21 13:57:18 -03:00
|
|
|
|
2009-05-28 15:49:23 -03:00
|
|
|
# SimSMTPChannel doesn't fully support LOGIN or CRAM-MD5 auth because they
|
|
|
|
# require a synchronous read to obtain the credentials...so instead smtpd
|
|
|
|
# sees the credential sent by smtplib's login method as an unknown command,
|
|
|
|
# which results in smtplib raising an auth error. Fortunately the error
|
|
|
|
# message contains the encoded credential, so we can partially check that it
|
|
|
|
# was generated correctly (partially, because the 'word' is uppercased in
|
|
|
|
# the error message).
|
|
|
|
|
|
|
|
def testAUTH_LOGIN(self):
|
|
|
|
self.serv.add_feature("AUTH LOGIN")
|
2009-05-29 14:31:05 -03:00
|
|
|
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
|
2009-05-28 15:49:23 -03:00
|
|
|
try: smtp.login(sim_auth[0], sim_auth[1])
|
|
|
|
except smtplib.SMTPAuthenticationError as err:
|
|
|
|
if sim_auth_login_password not in str(err):
|
|
|
|
raise "expected encoded password not found in error message"
|
|
|
|
|
|
|
|
def testAUTH_CRAM_MD5(self):
|
|
|
|
self.serv.add_feature("AUTH CRAM-MD5")
|
2009-05-29 14:31:05 -03:00
|
|
|
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
|
2009-05-28 15:49:23 -03:00
|
|
|
|
|
|
|
try: smtp.login(sim_auth[0], sim_auth[1])
|
|
|
|
except smtplib.SMTPAuthenticationError as err:
|
|
|
|
if sim_auth_credentials['cram-md5'] not in str(err):
|
|
|
|
raise "expected encoded credentials not found in error message"
|
|
|
|
|
|
|
|
#TODO: add tests for correct AUTH method fallback now that the
|
|
|
|
#test infrastructure can support it.
|
|
|
|
|
2014-08-30 17:56:49 -03:00
|
|
|
def test_quit_resets_greeting(self):
|
|
|
|
smtp = smtplib.SMTP(HOST, self.port,
|
|
|
|
local_hostname='localhost',
|
|
|
|
timeout=15)
|
|
|
|
code, message = smtp.ehlo()
|
|
|
|
self.assertEqual(code, 250)
|
|
|
|
self.assertIn('size', smtp.esmtp_features)
|
|
|
|
smtp.quit()
|
|
|
|
self.assertNotIn('size', smtp.esmtp_features)
|
|
|
|
smtp.connect(HOST, self.port)
|
|
|
|
self.assertNotIn('size', smtp.esmtp_features)
|
|
|
|
smtp.ehlo_or_helo_if_needed()
|
|
|
|
self.assertIn('size', smtp.esmtp_features)
|
|
|
|
smtp.quit()
|
|
|
|
|
2007-08-21 13:57:18 -03:00
|
|
|
|
2007-03-28 15:25:54 -03:00
|
|
|
def test_main(verbose=None):
|
2007-08-01 20:18:36 -03:00
|
|
|
test_support.run_unittest(GeneralTests, DebuggingServerTests,
|
2008-02-26 04:04:59 -04:00
|
|
|
NonConnectingTests,
|
2007-08-21 13:57:18 -03:00
|
|
|
BadHELOServerTests, SMTPSimTests)
|
2007-03-28 15:25:54 -03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_main()
|