def startTLS(self, contextFactory, normal=True): """ @see: L{ITLSTransport.startTLS} """ # Figure out which direction the SSL goes in. If normal is True, # we'll go in the direction indicated by the subclass. Otherwise, # we'll go the other way (client = not normal ^ _tlsClientDefault, # in other words). if normal: client = self._tlsClientDefault else: client = not self._tlsClientDefault tlsFactory = TLSMemoryBIOFactory(contextFactory, client, None) tlsProtocol = TLSMemoryBIOProtocol(tlsFactory, self.protocol, False) self.protocol = tlsProtocol self.getHandle = tlsProtocol.getHandle self.getPeerCertificate = tlsProtocol.getPeerCertificate # Mark the transport as secure. directlyProvides(self, interfaces.ISSLTransport) # Remember we did this so that write and writeSequence can send the # data to the right place. self._tls = True # Hook it up self.protocol.makeConnection(_BypassTLS(self))
def test_builtins(self): # Setup intspec = implementedBy(int) olddeclared = intspec.declared classImplements(int, I1) class myint(int): implements(I2) x = 42 self.assertEqual([i.getName() for i in providedBy(x)], ['I1']) x = myint(42) directlyProvides(x, I3) self.assertEqual([i.getName() for i in providedBy(x)], ['I3', 'I2', 'I1']) # cleanup intspec.declared = olddeclared classImplements(int) x = 42 self.assertEqual([i.getName() for i in providedBy(x)], [])
def test_module(self): from reqs.zope.interface.tests import m1, m2 #import reqs.zope.interface.tests.m2 directlyProvides(m2, m1.I1, m1.I2, ) self.assertEqual(list(providedBy(m1)), list(providedBy(m2)), )
def makeConnection(self, transport): """ When a connection is made, register this wrapper with its factory, save the real transport, and connect the wrapped protocol to this L{ProtocolWrapper} to intercept any transport calls it makes. """ directlyProvides(self, providedBy(transport)) Protocol.makeConnection(self, transport) self.factory.registerProtocol(self) self.wrappedProtocol.makeConnection(self)
def test_module(self): from reqs.zope.interface.tests import m1, m2 #import reqs.zope.interface.tests.m2 directlyProvides( m2, m1.I1, m1.I2, ) self.assertEqual( list(providedBy(m1)), list(providedBy(m2)), )
def __init__(self, connectedDeferred, wrappedProtocol): """ @param connectedDeferred: The L{Deferred} that will callback with the C{wrappedProtocol} when it is connected. @param wrappedProtocol: An L{IProtocol} provider that will be connected. """ self._connectedDeferred = connectedDeferred self._wrappedProtocol = wrappedProtocol if interfaces.IHalfCloseableProtocol.providedBy( self._wrappedProtocol): directlyProvides(self, interfaces.IHalfCloseableProtocol)
def upgradeWithIQResponseTracker(xs): """ Enhances an XmlStream for iq response tracking. This makes an L{XmlStream} object provide L{IIQResponseTracker}. When a response is an error iq stanza, the deferred has its errback invoked with a failure that holds a L{StanzaException<error.StanzaException>} that is easier to examine. """ def callback(iq): """ Handle iq response by firing associated deferred. """ if getattr(iq, 'handled', False): return try: d = xs.iqDeferreds[iq["id"]] except KeyError: pass else: del xs.iqDeferreds[iq["id"]] iq.handled = True if iq['type'] == 'error': d.errback(error.exceptionFromStanza(iq)) else: d.callback(iq) def disconnected(_): """ Make sure deferreds do not linger on after disconnect. This errbacks all deferreds of iq's for which no response has been received with a L{ConnectionLost} failure. Otherwise, the deferreds will never be fired. """ iqDeferreds = xs.iqDeferreds xs.iqDeferreds = {} for d in iqDeferreds.itervalues(): d.errback(ConnectionLost()) xs.iqDeferreds = {} xs.iqDefaultTimeout = getattr(xs, 'iqDefaultTimeout', None) xs.addObserver(xmlstream.STREAM_END_EVENT, disconnected) xs.addObserver('/iq[@type="result"]', callback) xs.addObserver('/iq[@type="error"]', callback) directlyProvides(xs, ijabber.IIQResponseTracker)
def test_ObjectSpecification(self): c = C() directlyProvides(c, I4) self.assertEqual([i.getName() for i in providedBy(c)], ['I4', 'I31', 'I1', 'I2']) self.assertEqual([i.getName() for i in providedBy(c).flattened()], ['I4', 'I31', 'I3', 'I1', 'I2', 'Interface']) self.assert_(I1 in providedBy(c)) self.failIf(I3 in providedBy(c)) self.assert_(providedBy(c).extends(I3)) self.assert_(providedBy(c).extends(I31)) self.failIf(providedBy(c).extends(I5)) class COnly(A, B): pass classImplementsOnly(COnly, I31) class D(COnly): pass classImplements(D, I5) classImplements(D, I5) c = D() directlyProvides(c, I4) self.assertEqual([i.getName() for i in providedBy(c)], ['I4', 'I5', 'I31']) self.assertEqual([i.getName() for i in providedBy(c).flattened()], ['I4', 'I5', 'I31', 'I3', 'Interface']) self.failIf(I1 in providedBy(c)) self.failIf(I3 in providedBy(c)) self.assert_(providedBy(c).extends(I3)) self.failIf(providedBy(c).extends(I1)) self.assert_(providedBy(c).extends(I31)) self.assert_(providedBy(c).extends(I5)) class COnly(A, B): __implemented__ = I31 class D(COnly): pass classImplements(D, I5) classImplements(D, I5) c = D() directlyProvides(c, I4) self.assertEqual([i.getName() for i in providedBy(c)], ['I4', 'I5', 'I31']) self.assertEqual([i.getName() for i in providedBy(c).flattened()], ['I4', 'I5', 'I31', 'I3', 'Interface']) self.failIf(I1 in providedBy(c)) self.failIf(I3 in providedBy(c)) self.assert_(providedBy(c).extends(I3)) self.failIf(providedBy(c).extends(I1)) self.assert_(providedBy(c).extends(I31)) self.assert_(providedBy(c).extends(I5))
def testInvariant(self): from reqs.zope.interface.exceptions import Invalid from reqs.zope.interface import directlyProvides from reqs.zope.interface.tests.unitfixtures import BarGreaterThanFoo from reqs.zope.interface.tests.unitfixtures import ifFooThenBar from reqs.zope.interface.tests.unitfixtures import IInvariant from reqs.zope.interface.tests.unitfixtures import InvariantC from reqs.zope.interface.tests.unitfixtures import ISubInvariant # set up o = InvariantC() directlyProvides(o, IInvariant) # a helper def errorsEqual(self, o, error_len, error_msgs, iface=None): if iface is None: iface = IInvariant self.assertRaises(Invalid, iface.validateInvariants, o) e = [] try: iface.validateInvariants(o, e) except Invalid, error: self.assertEquals(error.args[0], e) else:
def test_directlyProvides(self): class IA1(Interface): pass class IA2(Interface): pass class IB(Interface): pass class IC(Interface): pass class A(Odd): pass classImplements(A, IA1, IA2) class B(Odd): pass classImplements(B, IB) class C(A, B): pass classImplements(C, IC) ob = C() directlyProvides(ob, I1, I2) self.assert_(I1 in providedBy(ob)) self.assert_(I2 in providedBy(ob)) self.assert_(IA1 in providedBy(ob)) self.assert_(IA2 in providedBy(ob)) self.assert_(IB in providedBy(ob)) self.assert_(IC in providedBy(ob)) directlyProvides(ob, directlyProvidedBy(ob) - I2) self.assert_(I1 in providedBy(ob)) self.failIf(I2 in providedBy(ob)) self.failIf(I2 in providedBy(ob)) directlyProvides(ob, directlyProvidedBy(ob), I2) self.assert_(I2 in providedBy(ob))
# the tests self.assertEquals(IInvariant.getTaggedValue('invariants'), [ifFooThenBar]) self.assertEquals(IInvariant.validateInvariants(o), None) o.bar = 27 self.assertEquals(IInvariant.validateInvariants(o), None) o.foo = 42 self.assertEquals(IInvariant.validateInvariants(o), None) del o.bar errorsEqual(self, o, 1, ['If Foo, then Bar!']) # nested interfaces with invariants: self.assertEquals(ISubInvariant.getTaggedValue('invariants'), [BarGreaterThanFoo]) o = InvariantC() directlyProvides(o, ISubInvariant) o.foo = 42 # even though the interface has changed, we should still only have one # error. errorsEqual(self, o, 1, ['If Foo, then Bar!'], ISubInvariant) # however, if we set foo to 0 (Boolean False) and bar to a negative # number then we'll get the new error o.foo = 2 o.bar = 1 errorsEqual(self, o, 1, ['Please, Boo MUST be greater than Foo!'], ISubInvariant) # and if we set foo to a positive number and boo to 0, we'll # get both errors! o.foo = 1 o.bar = 0 errorsEqual(