def test_mailfrom_size(self): self.sock.sendall(b'MAIL FROM:<test> SIZE=10\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 2.0.0 Ok\r\n') self.mox.ReplayAll() client = Client(self.sock) client.extensions.add('SIZE', 100) client.mailfrom('test', data_size=10)
def test_get_reply(self): self.sock.recv(IsA(int)).AndReturn(b"421 Test\r\n") self.mox.ReplayAll() client = Client(self.sock) reply = client.get_reply(b"[TEST]") self.assertEqual("421", reply.code) self.assertEqual("4.0.0 Test", reply.message) self.assertEqual(b"[TEST]", reply.command)
def test_get_banner(self): self.sock.recv(IsA(int)).AndReturn(b'220 Go\r\n') self.mox.ReplayAll() client = Client(self.sock) reply = client.get_banner() self.assertEqual('220', reply.code) self.assertEqual('Go', reply.message) self.assertEqual(b'[BANNER]', reply.command)
def test_get_reply(self): self.sock.recv(IsA(int)).AndReturn(b'421 Test\r\n') self.mox.ReplayAll() client = Client(self.sock) reply = client.get_reply(b'[TEST]') self.assertEqual('421', reply.code) self.assertEqual('4.0.0 Test', reply.message) self.assertEqual(b'[TEST]', reply.command)
def test_get_banner(self): self.sock.recv(IsA(int)).AndReturn(b"220 Go\r\n") self.mox.ReplayAll() client = Client(self.sock) reply = client.get_banner() self.assertEqual("220", reply.code) self.assertEqual("Go", reply.message) self.assertEqual(b"[BANNER]", reply.command)
def test_data(self): self.sock.sendall(b'DATA\r\n') self.sock.recv(IsA(int)).AndReturn(b'354 Go ahead\r\n') self.mox.ReplayAll() client = Client(self.sock) reply = client.data() self.assertEqual('354', reply.code) self.assertEqual('Go ahead', reply.message) self.assertEqual(b'DATA', reply.command)
def test_rcptto(self): self.sock.sendall(b'RCPT TO:<test>\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 2.0.0 Ok\r\n') self.mox.ReplayAll() client = Client(self.sock) reply = client.rcptto('test') self.assertEqual('250', reply.code) self.assertEqual('2.0.0 Ok', reply.message) self.assertEqual(b'RCPT', reply.command)
def test_starttls_noencrypt(self): self.sock.sendall(b"STARTTLS\r\n") self.sock.recv(IsA(int)).AndReturn(b"420 Nope\r\n") self.mox.ReplayAll() client = Client(self.sock) reply = client.starttls({}) self.assertEqual("420", reply.code) self.assertEqual("4.0.0 Nope", reply.message) self.assertEqual(b"STARTTLS", reply.command)
def test_send_empty_data(self): self.sock.sendall('.\r\n') self.sock.recv(IsA(int)).AndReturn('250 2.0.0 Done\r\n') self.mox.ReplayAll() client = Client(self.sock) reply = client.send_empty_data() assert_equal('250', reply.code) assert_equal('2.0.0 Done', reply.message) assert_equal('[SEND_DATA]', reply.command)
def test_data(self): self.sock.sendall(b"DATA\r\n") self.sock.recv(IsA(int)).AndReturn(b"354 Go ahead\r\n") self.mox.ReplayAll() client = Client(self.sock) reply = client.data() self.assertEqual("354", reply.code) self.assertEqual("Go ahead", reply.message) self.assertEqual(b"DATA", reply.command)
def test_rcptto(self): self.sock.sendall(b"RCPT TO:<test>\r\n") self.sock.recv(IsA(int)).AndReturn(b"250 2.0.0 Ok\r\n") self.mox.ReplayAll() client = Client(self.sock) reply = client.rcptto("test") self.assertEqual("250", reply.code) self.assertEqual("2.0.0 Ok", reply.message) self.assertEqual(b"RCPT", reply.command)
def test_starttls_noencrypt(self): self.sock.sendall('STARTTLS\r\n') self.sock.recv(IsA(int)).AndReturn('420 Nope\r\n') self.mox.ReplayAll() client = Client(self.sock) reply = client.starttls({}) assert_equal('420', reply.code) assert_equal('4.0.0 Nope', reply.message) assert_equal('STARTTLS', reply.command)
def test_quit(self): self.sock.sendall('QUIT\r\n') self.sock.recv(IsA(int)).AndReturn('221 Bye\r\n') self.mox.ReplayAll() client = Client(self.sock) reply = client.quit() assert_equal('221', reply.code) assert_equal('2.0.0 Bye', reply.message) assert_equal('QUIT', reply.command)
def test_rset(self): self.sock.sendall('RSET\r\n') self.sock.recv(IsA(int)).AndReturn('250 Ok\r\n') self.mox.ReplayAll() client = Client(self.sock) reply = client.rset() assert_equal('250', reply.code) assert_equal('2.0.0 Ok', reply.message) assert_equal('RSET', reply.command)
def test_quit(self): self.sock.sendall(b"QUIT\r\n") self.sock.recv(IsA(int)).AndReturn(b"221 Bye\r\n") self.mox.ReplayAll() client = Client(self.sock) reply = client.quit() self.assertEqual("221", reply.code) self.assertEqual("2.0.0 Bye", reply.message) self.assertEqual(b"QUIT", reply.command)
def test_send_data(self): self.sock.sendall(b'One\r\nTwo\r\n..Three\r\n.\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 2.0.0 Done\r\n') self.mox.ReplayAll() client = Client(self.sock) reply = client.send_data(b'One\r\nTwo\r\n.Three') self.assertEqual('250', reply.code) self.assertEqual('2.0.0 Done', reply.message) self.assertEqual(b'[SEND_DATA]', reply.command)
def test_rset(self): self.sock.sendall(b"RSET\r\n") self.sock.recv(IsA(int)).AndReturn(b"250 Ok\r\n") self.mox.ReplayAll() client = Client(self.sock) reply = client.rset() self.assertEqual("250", reply.code) self.assertEqual("2.0.0 Ok", reply.message) self.assertEqual(b"RSET", reply.command)
def test_custom_command(self): self.sock.sendall(b"cmd arg\r\n") self.sock.recv(IsA(int)).AndReturn(b"250 Ok\r\n") self.mox.ReplayAll() client = Client(self.sock) reply = client.custom_command(b"cmd", b"arg") self.assertEqual("250", reply.code) self.assertEqual("2.0.0 Ok", reply.message) self.assertEqual(b"CMD", reply.command)
def test_helo(self): self.sock.sendall(b'HELO there\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 Hello\r\n') self.mox.ReplayAll() client = Client(self.sock) reply = client.helo('there') self.assertEqual('250', reply.code) self.assertEqual('Hello', reply.message) self.assertEqual(b'HELO', reply.command)
def test_auth_force_mechanism(self): self.sock.sendall(b'AUTH PLAIN AHRlc3RAZXhhbXBsZS5jb20AYXNkZg==\r\n') self.sock.recv(IsA(int)).AndReturn(b'535 Nope!\r\n') self.mox.ReplayAll() client = Client(self.sock) reply = client.auth('*****@*****.**', 'asdf', mechanism=b'PLAIN') self.assertEqual('535', reply.code) self.assertEqual('5.0.0 Nope!', reply.message) self.assertEqual(b'AUTH', reply.command)
def test_send_data(self): self.sock.sendall(b"One\r\nTwo\r\n..Three\r\n.\r\n") self.sock.recv(IsA(int)).AndReturn(b"250 2.0.0 Done\r\n") self.mox.ReplayAll() client = Client(self.sock) reply = client.send_data(b"One\r\nTwo\r\n.Three") self.assertEqual("250", reply.code) self.assertEqual("2.0.0 Done", reply.message) self.assertEqual(b"[SEND_DATA]", reply.command)
def test_mailfrom(self): self.sock.sendall(b'MAIL FROM:<test>\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 2.0.0 Ok\r\n') self.mox.ReplayAll() client = Client(self.sock) reply = client.mailfrom('test') self.assertEqual('250', reply.code) self.assertEqual('2.0.0 Ok', reply.message) self.assertEqual(b'MAIL', reply.command)
def test_custom_command(self): self.sock.sendall(b'cmd arg\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 Ok\r\n') self.mox.ReplayAll() client = Client(self.sock) reply = client.custom_command(b'cmd', b'arg') self.assertEqual('250', reply.code) self.assertEqual('2.0.0 Ok', reply.message) self.assertEqual(b'CMD', reply.command)
def test_helo(self): self.sock.sendall(b"HELO there\r\n") self.sock.recv(IsA(int)).AndReturn(b"250 Hello\r\n") self.mox.ReplayAll() client = Client(self.sock) reply = client.helo("there") self.assertEqual("250", reply.code) self.assertEqual("Hello", reply.message) self.assertEqual(b"HELO", reply.command)
def test_auth_force_mechanism(self): self.sock.sendall(b"AUTH PLAIN AHRlc3RAZXhhbXBsZS5jb20AYXNkZg==\r\n") self.sock.recv(IsA(int)).AndReturn(b"535 Nope!\r\n") self.mox.ReplayAll() client = Client(self.sock) reply = client.auth("*****@*****.**", "asdf", mechanism=b"PLAIN") self.assertEqual("535", reply.code) self.assertEqual("5.0.0 Nope!", reply.message) self.assertEqual(b"AUTH", reply.command)
def test_mailfrom(self): self.sock.sendall(b"MAIL FROM:<test>\r\n") self.sock.recv(IsA(int)).AndReturn(b"250 2.0.0 Ok\r\n") self.mox.ReplayAll() client = Client(self.sock) reply = client.mailfrom("test") self.assertEqual("250", reply.code) self.assertEqual("2.0.0 Ok", reply.message) self.assertEqual(b"MAIL", reply.command)
def test_starttls_noencrypt(self): self.sock.sendall(b'STARTTLS\r\n') self.sock.recv(IsA(int)).AndReturn(b'420 Nope\r\n') self.mox.ReplayAll() client = Client(self.sock) reply = client.starttls({}) self.assertEqual('420', reply.code) self.assertEqual('4.0.0 Nope', reply.message) self.assertEqual(b'STARTTLS', reply.command)
def test_quit(self): self.sock.sendall(b'QUIT\r\n') self.sock.recv(IsA(int)).AndReturn(b'221 Bye\r\n') self.mox.ReplayAll() client = Client(self.sock) reply = client.quit() self.assertEqual('221', reply.code) self.assertEqual('2.0.0 Bye', reply.message) self.assertEqual(b'QUIT', reply.command)
def test_rset(self): self.sock.sendall(b'RSET\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 Ok\r\n') self.mox.ReplayAll() client = Client(self.sock) reply = client.rset() self.assertEqual('250', reply.code) self.assertEqual('2.0.0 Ok', reply.message) self.assertEqual(b'RSET', reply.command)
def test_mailfrom_size(self): self.sock.sendall(b"MAIL FROM:<test> SIZE=10\r\n") self.sock.recv(IsA(int)).AndReturn(b"250 2.0.0 Ok\r\n") self.mox.ReplayAll() client = Client(self.sock) client.extensions.add("SIZE", 100) reply = client.mailfrom("test", 10) self.assertEqual("250", reply.code) self.assertEqual("2.0.0 Ok", reply.message) self.assertEqual(b"MAIL", reply.command)
def test_auth(self): self.sock.sendall(b'AUTH PLAIN AHRlc3RAZXhhbXBsZS5jb20AYXNkZg==\r\n') self.sock.recv(IsA(int)).AndReturn(b'235 Ok\r\n') self.mox.ReplayAll() client = Client(self.sock) client.extensions.add('AUTH', b'PLAIN') reply = client.auth('*****@*****.**', 'asdf') self.assertEqual('235', reply.code) self.assertEqual('2.0.0 Ok', reply.message) self.assertEqual(b'AUTH', reply.command)
def test_mailfrom_auth(self): self.sock.sendall(b'MAIL FROM:<test> AUTH=<>\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 2.0.0 Ok\r\n') self.sock.sendall(b'MAIL FROM:<test> AUTH=1+2B1+3D2\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 2.0.0 Ok\r\n') self.mox.ReplayAll() client = Client(self.sock) client.extensions.add('AUTH', True) client.mailfrom('test', auth=False) client.mailfrom('test', auth='1+1=2')
def test_mailfrom_size(self): self.sock.sendall(b'MAIL FROM:<test> SIZE=10\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 2.0.0 Ok\r\n') self.mox.ReplayAll() client = Client(self.sock) client.extensions.add('SIZE', 100) reply = client.mailfrom('test', 10) self.assertEqual('250', reply.code) self.assertEqual('2.0.0 Ok', reply.message) self.assertEqual(b'MAIL', reply.command)
def test_auth(self): self.sock.sendall(b"AUTH PLAIN AHRlc3RAZXhhbXBsZS5jb20AYXNkZg==\r\n") self.sock.recv(IsA(int)).AndReturn(b"235 Ok\r\n") self.mox.ReplayAll() client = Client(self.sock) client.extensions.add("AUTH", b"PLAIN") reply = client.auth("*****@*****.**", "asdf") self.assertEqual("235", reply.code) self.assertEqual("2.0.0 Ok", reply.message) self.assertEqual(b"AUTH", reply.command)
def test_starttls(self): sock = self.mox.CreateMockAnything() sock.fileno = lambda: -1 sock.sendall(b'STARTTLS\r\n') sock.recv(IsA(int)).AndReturn(b'220 Go ahead\r\n') sock.tls_wrapper(sock, self.tls_args).AndReturn(sock) self.mox.ReplayAll() client = Client(sock, tls_wrapper=sock.tls_wrapper) reply = client.starttls(self.tls_args) self.assertEqual('220', reply.code) self.assertEqual('2.0.0 Go ahead', reply.message) self.assertEqual('STARTTLS', reply.command)
def test_mailfrom_pipelining(self): self.sock.sendall('MAIL FROM:<test>\r\n') self.sock.recv(IsA(int)).AndReturn('250 2.0.0 Ok\r\n') self.mox.ReplayAll() client = Client(self.sock) client.extensions.add('PIPELINING') reply = client.mailfrom('test') assert_equal(None, reply.code) assert_equal(None, reply.message) assert_equal('MAIL', reply.command) client._flush_pipeline() assert_equal('250', reply.code) assert_equal('2.0.0 Ok', reply.message)
def test_ehlo(self): self.sock.sendall('EHLO there\r\n') self.sock.recv(IsA(int)).AndReturn('250-Hello there\r\n250-TEST arg\r\n') self.sock.recv(IsA(int)).AndReturn('250 EXTEN\r\n') self.mox.ReplayAll() client = Client(self.sock) reply = client.ehlo('there') self.assertEqual('250', reply.code) self.assertEqual('Hello there', reply.message) self.assertEqual('EHLO', reply.command) self.assertTrue('TEST' in client.extensions) self.assertTrue('EXTEN' in client.extensions) self.assertEqual('arg', client.extensions.getparam('TEST'))
def test_auth(self): sock = self.mox.CreateMock(SSLSocket) sock.fileno = lambda: -1 sock.getpeername = lambda: ('test', 0) sock.sendall(b'AUTH PLAIN AHRlc3RAZXhhbXBsZS5jb20AYXNkZg==\r\n') sock.recv(IsA(int)).AndReturn(b'235 Ok\r\n') self.mox.ReplayAll() client = Client(sock) client.extensions.add('AUTH', 'PLAIN') reply = client.auth('*****@*****.**', 'asdf') self.assertEqual('235', reply.code) self.assertEqual('2.0.0 Ok', reply.message) self.assertEqual(b'AUTH', reply.command)
def test_starttls(self): sock = self.mox.CreateMockAnything() sock.fileno = lambda: -1 sock.getpeername = lambda: ('test', 0) sock.sendall(b'STARTTLS\r\n') sock.recv(IsA(int)).AndReturn(b'220 Go ahead\r\n') self.context.wrap_socket(sock, server_hostname='test').AndReturn(sock) self.mox.ReplayAll() client = Client(sock) reply = client.starttls(self.context) self.assertEqual('220', reply.code) self.assertEqual('2.0.0 Go ahead', reply.message) self.assertEqual(b'STARTTLS', reply.command)
def test_rcptto_pipelining(self): self.sock.sendall(b'RCPT TO:<test>\r\n') self.sock.recv(IsA(int)).AndReturn(b'250 2.0.0 Ok\r\n') self.mox.ReplayAll() client = Client(self.sock) client.extensions.add('PIPELINING') reply = client.rcptto('test') self.assertEqual(None, reply.code) self.assertEqual(None, reply.message) self.assertEqual(b'RCPT', reply.command) client._flush_pipeline() self.assertEqual('250', reply.code) self.assertEqual('2.0.0 Ok', reply.message)
class SmtpRelayClient(RelayPoolClient): def __init__(self, address, queue, socket_creator=None, ehlo_as=None, tls=None, tls_immediately=False, tls_required=False, tls_wrapper=None, connect_timeout=10.0, command_timeout=10.0, data_timeout=None, idle_timeout=None, credentials=None, binary_encoder=None): super(SmtpRelayClient, self).__init__(queue, idle_timeout) self.address = address if socket_creator: self._socket_creator = socket_creator self.socket = None self.client = None self.ehlo_as = ehlo_as or hostname self.tls = tls self.tls_immediately = tls_immediately self.tls_required = tls_required self.tls_wrapper = tls_wrapper self.connect_timeout = connect_timeout self.command_timeout = command_timeout self.data_timeout = data_timeout or command_timeout self.credentials = credentials self.binary_encoder = binary_encoder def _socket_creator(self, address): socket = create_connection(address) log.connect(socket, address) return socket def _connect(self): try: with Timeout(self.connect_timeout): self.socket = self._socket_creator(self.address) except socket_error as (err, msg): reply = Reply('451', '4.3.0 Connection failed') raise SmtpRelayError.factory(reply) self.client = Client(self.socket, self.tls_wrapper)
def test_auth_insecure(self): self.mox.ReplayAll() client = Client(self.sock) client.extensions.add('AUTH', 'PLAIN') self.assertRaises(InvalidMechanismError, client.auth, '*****@*****.**', 'asdf')
def test_smtp_edge(self): queue = self.mox.CreateMockAnything() queue.enqueue(IsA(Envelope)).AndReturn([(Envelope(), 'testid')]) self.mox.ReplayAll() server = SmtpEdge(('127.0.0.1', 0), queue) server.start() gevent.sleep(0) client_sock = create_connection(server.server.address) client = Client(client_sock) client.get_banner() client.ehlo('there') client.mailfrom('*****@*****.**') client.rcptto('*****@*****.**') client.data() client.send_empty_data() client.quit() client_sock.close()