def test_finish(self): self.mox.StubOutWithMock(socket, "gethostbyaddr") socket.gethostbyaddr("127.0.0.1").AndReturn(("example.com", None, None)) self.mox.ReplayAll() ptr = PtrLookup("127.0.0.1") ptr.start() self.assertEqual("example.com", ptr.finish(runtime=1.0))
def test_finish(self): self.mox.StubOutWithMock(dns_resolver, 'query') dns_resolver.query(IgnoreArg(), 'PTR').AndReturn(['example.com']) self.mox.ReplayAll() ptr = PtrLookup('127.0.0.1') ptr.start() assert_equal('example.com', ptr.finish(runtime=1.0))
def test_finish(self): self.mox.StubOutWithMock(dns_resolver, 'query') dns_resolver.query(IgnoreArg(), 'PTR').AndReturn(['example.com']) self.mox.ReplayAll() ptr = PtrLookup('127.0.0.1') ptr.start() self.assertEqual('example.com', ptr.finish(runtime=1.0))
def test_finish(self): self.mox.StubOutWithMock(socket, 'gethostbyaddr') socket.gethostbyaddr('127.0.0.1').AndReturn( ('example.com', None, None)) self.mox.ReplayAll() ptr = PtrLookup('127.0.0.1') ptr.start() self.assertEqual('example.com', ptr.finish(runtime=1.0))
def test_finish_timeout(self): self.mox.StubOutWithMock(dns_resolver, 'query') def long_sleep(*args): gevent.sleep(1.0) dns_resolver.query(IgnoreArg(), 'PTR').WithSideEffects(long_sleep) self.mox.ReplayAll() ptr = PtrLookup('127.0.0.1') ptr.start() self.assertIsNone(ptr.finish(runtime=0.0))
def test_finish_timeout(self): def long_sleep(*args): gevent.sleep(1.0) self.mox.StubOutWithMock(socket, 'gethostbyaddr') socket.gethostbyaddr('127.0.0.1').WithSideEffects(long_sleep) self.mox.ReplayAll() ptr = PtrLookup('127.0.0.1') ptr.start() self.assertIsNone(ptr.finish(runtime=0.001))
def test_finish_timeout(self): def long_sleep(*args): gevent.sleep(1.0) self.mox.StubOutWithMock(socket, "gethostbyaddr") socket.gethostbyaddr("127.0.0.1").WithSideEffects(long_sleep) self.mox.ReplayAll() ptr = PtrLookup("127.0.0.1") ptr.start() self.assertIsNone(ptr.finish(runtime=0.001))
def test_finish_timeout(self): self.mox.StubOutWithMock(dns_resolver, 'query') def long_sleep(*args): gevent.sleep(1.0) dns_resolver.query(IgnoreArg(), 'PTR').WithSideEffects(long_sleep) self.mox.ReplayAll() ptr = PtrLookup('127.0.0.1') ptr.start() assert_is_none(ptr.finish(runtime=0.0))
class SmtpSession(object): def __init__(self, address, validator_class, handoff): self.extended_smtp = False self.security = None self.address = address self.reverse_address = None self.handoff = handoff self.validators = validator_class(self) if validator_class else None self.envelope = None self.ehlo_as = None self.auth = None self._ptr_lookup = None def _call_validator(self, command, *args): method = 'handle_' + command if hasattr(self.validators, method): getattr(self.validators, method)(*args) @property def protocol(self): proto = 'SMTP' if self.extended_smtp: proto = 'ESMTP' if self.security == 'TLS': proto += 'S' if self.auth: proto += 'A' return proto def BANNER_(self, reply): self._ptr_lookup = PtrLookup(self.address[0]) self._ptr_lookup.start() self._call_validator('banner', reply, self.address) def EHLO(self, reply, ehlo_as): self._call_validator('ehlo', reply, ehlo_as) self.extended_smtp = True if reply.code == '250': self.ehlo_as = ehlo_as self.envelope = None def HELO(self, reply, helo_as): self._call_validator('helo', reply, helo_as) if reply.code == '250': self.ehlo_as = helo_as self.envelope = None def TLSHANDSHAKE(self): self._call_validator('tls') self.security = 'TLS' def AUTH(self, reply, creds): self._call_validator('auth', reply, creds) if reply.code == '235': self.auth = (creds.authcid, creds.authzid) def RSET(self, reply): self.envelope = None def MAIL(self, reply, address, params): self._call_validator('mail', reply, address, params) if reply.code == '250': self.envelope = Envelope(sender=address) def RCPT(self, reply, address, params): self._call_validator('rcpt', reply, address, params) if reply.code == '250': self.envelope.recipients.append(address) def DATA(self, reply): self._call_validator('data', reply) def HAVE_DATA(self, reply, data, err): if isinstance(err, MessageTooBig): reply.code = '552' reply.message = '5.3.4 Message exceeded size limit' return elif err: raise err self._call_validator('have_data', reply, data) if reply.code != '250': return if self._ptr_lookup is not None: self.reverse_address = self._ptr_lookup.finish() self.envelope.client['ip'] = self.address[0] self.envelope.client['host'] = self.reverse_address self.envelope.client['name'] = self.ehlo_as self.envelope.client['protocol'] = self.protocol self.envelope.client['auth'] = self.auth self.envelope.parse(data) results = self.handoff(self.envelope) if isinstance(results[0][1], QueueError): default_reply = Reply('451', '4.3.0 Error queuing message') queue_reply = getattr(results[0][1], 'reply', default_reply) reply.copy(queue_reply) elif isinstance(results[0][1], RelayError): relay_reply = results[0][1].reply reply.copy(relay_reply) else: reply.message = '2.6.0 Message accepted for delivery' self._call_validator('queued', reply, results) self.envelope = None
class SmtpSession(object): def __init__(self, address, validator_class, handoff): self.extended_smtp = False self.security = None self.address = address self.reverse_address = None self.handoff = handoff self.validators = validator_class(self) if validator_class else None self.envelope = None self.ehlo_as = None self.auth = None self._ptr_lookup = None def _call_validator(self, command, *args): method = 'handle_'+command if hasattr(self.validators, method): getattr(self.validators, method)(*args) @property def protocol(self): proto = 'SMTP' if self.extended_smtp: proto = 'ESMTP' if self.security == 'TLS': proto += 'S' if self.auth: proto += 'A' return proto def BANNER_(self, reply): self._ptr_lookup = PtrLookup(self.address[0]) self._ptr_lookup.start() self._call_validator('banner', reply, self.address) def EHLO(self, reply, ehlo_as): self._call_validator('ehlo', reply, ehlo_as) self.extended_smtp = True if reply.code == '250': self.ehlo_as = ehlo_as self.envelope = None def HELO(self, reply, helo_as): self._call_validator('helo', reply, helo_as) if reply.code == '250': self.ehlo_as = helo_as self.envelope = None def TLSHANDSHAKE(self): self._call_validator('tls') self.security = 'TLS' def AUTH(self, reply, creds): self._call_validator('auth', reply, creds) if reply.code == '235': self.auth = (creds.authcid, creds.authzid) def RSET(self, reply): self.envelope = None def MAIL(self, reply, address, params): self._call_validator('mail', reply, address, params) if reply.code == '250': self.envelope = Envelope(sender=address) def RCPT(self, reply, address, params): self._call_validator('rcpt', reply, address, params) if reply.code == '250': self.envelope.recipients.append(address) def DATA(self, reply): self._call_validator('data', reply) def HAVE_DATA(self, reply, data, err): if isinstance(err, MessageTooBig): reply.code = '552' reply.message = '5.3.4 Message exceeded size limit' return elif err: raise err self._call_validator('have_data', reply, data) if reply.code != '250': return if self._ptr_lookup is not None: self.reverse_address = self._ptr_lookup.finish() self.envelope.client['ip'] = self.address[0] self.envelope.client['host'] = self.reverse_address self.envelope.client['name'] = self.ehlo_as self.envelope.client['protocol'] = self.protocol self.envelope.client['auth'] = self.auth self.envelope.parse(data) results = self.handoff(self.envelope) if isinstance(results[0][1], QueueError): default_reply = Reply('451', '4.3.0 Error queuing message') queue_reply = getattr(results[0][1], 'reply', default_reply) reply.copy(queue_reply) elif isinstance(results[0][1], RelayError): relay_reply = results[0][1].reply reply.copy(relay_reply) else: reply.message = '2.6.0 Message accepted for delivery' self._call_validator('queued', reply, results) self.envelope = None