Here are the examples of the python api twisted.protocols.loopback.loopbackAsync taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
68 Examples
3
Example 1
View licensedef _make_connection(self, server, protocolFactory): client = protocolFactory.buildProtocol(None) patch_protocol_for_agent(client) finished_d = loopbackAsync(server, client) server.finish_connecting(client, finished_d) return client
3
Example 2
View licensedef test_makeConnection(self): """ Test that the client and server protocol both have makeConnection invoked on them by loopbackAsync. """ class TestProtocol(Protocol): transport = None def makeConnection(self, transport): self.transport = transport server = TestProtocol() client = TestProtocol() loopback.loopbackAsync(server, client) self.failIfEqual(client.transport, None) self.failIfEqual(server.transport, None)
3
Example 3
View licensedef test_makeConnection(self): """ Test that the client and server protocol both have makeConnection invoked on them by loopbackAsync. """ class TestProtocol(Protocol): transport = None def makeConnection(self, transport): self.transport = transport server = TestProtocol() client = TestProtocol() loopback.loopbackAsync(server, client) self.failIfEqual(client.transport, None) self.failIfEqual(server.transport, None)
3
Example 4
View licensedef testLoopback(self): protocol = MyVirtualPOP3() protocol.service = self.factory clientProtocol = MyPOP3Downloader() def check(ignored): self.assertEqual(clientProtocol.message, self.message) protocol.connectionLost( failure.Failure(Exception("Test harness disconnect"))) d = loopback.loopbackAsync(protocol, clientProtocol) return d.addCallback(check)
3
Example 5
View licensedef testAuthListing(self): p = DummyPOP3() p.factory = internet.protocol.Factory() p.factory.challengers = {'Auth1': None, 'secondAuth': None, 'authLast': None} client = LineSendingProtocol([ "AUTH", "QUIT", ]) d = loopback.loopbackAsync(p, client) return d.addCallback(self._cbTestAuthListing, client)
3
Example 6
View licensedef testIllegalPASS(self): dummy = DummyPOP3() client = LineSendingProtocol([ "PASS fooz", "QUIT" ]) d = loopback.loopbackAsync(dummy, client) return d.addCallback(self._cbTestIllegalPASS, client, dummy)
3
Example 7
View licensedef testEmptyPASS(self): dummy = DummyPOP3() client = LineSendingProtocol([ "PASS ", "QUIT" ]) d = loopback.loopbackAsync(dummy, client) return d.addCallback(self._cbTestEmptyPASS, client, dummy)
3
Example 8
View licensedef testLoopback(self): protocol = MyVirtualPOP3() protocol.service = self.factory clientProtocol = MyPOP3Downloader() def check(ignored): self.assertEqual(clientProtocol.message, self.message) protocol.connectionLost( failure.Failure(Exception("Test harness disconnect"))) d = loopback.loopbackAsync(protocol, clientProtocol) return d.addCallback(check)
3
Example 9
View licensedef testAuthListing(self): p = DummyPOP3() p.factory = internet.protocol.Factory() p.factory.challengers = {'Auth1': None, 'secondAuth': None, 'authLast': None} client = LineSendingProtocol([ "AUTH", "QUIT", ]) d = loopback.loopbackAsync(p, client) return d.addCallback(self._cbTestAuthListing, client)
3
Example 10
View licensedef testIllegalPASS(self): dummy = DummyPOP3() client = LineSendingProtocol([ "PASS fooz", "QUIT" ]) d = loopback.loopbackAsync(dummy, client) return d.addCallback(self._cbTestIllegalPASS, client, dummy)
3
Example 11
View licensedef testEmptyPASS(self): dummy = DummyPOP3() client = LineSendingProtocol([ "PASS ", "QUIT" ]) d = loopback.loopbackAsync(dummy, client) return d.addCallback(self._cbTestEmptyPASS, client, dummy)
3
Example 12
View licensedef test_makeConnection(self): """ Test that the client and server protocol both have makeConnection invoked on them by loopbackAsync. """ class TestProtocol(Protocol): transport = None def makeConnection(self, transport): self.transport = transport server = TestProtocol() client = TestProtocol() loopback.loopbackAsync(server, client) self.failIfEqual(client.transport, None) self.failIfEqual(server.transport, None)
3
Example 13
View licensedef test_makeConnection(self): """ Test that the client and server protocol both have makeConnection invoked on them by loopbackAsync. """ class TestProtocol(Protocol): transport = None def makeConnection(self, transport): self.transport = transport server = TestProtocol() client = TestProtocol() loopback.loopbackAsync(server, client) self.failIfEqual(client.transport, None) self.failIfEqual(server.transport, None)
0
Example 14
View licensedef _make_connection(self, server, client): finished_d = loopbackAsync(server, client) self.fake_smsc.set_finished(finished_d) return client
0
Example 15
View licensedef test_loopback(self): """ Test that the userauth server and client play nicely with each other. """ server = userauth.SSHUserAuthServer() client = ClientUserAuth('foo', self.Factory.Service()) # set up transports server.transport = transport.SSHTransportBase() server.transport.service = server server.transport.isEncrypted = lambda x: True client.transport = transport.SSHTransportBase() client.transport.service = client server.transport.sessionID = client.transport.sessionID = '' # don't send key exchange packet server.transport.sendKexInit = client.transport.sendKexInit = \ lambda: None # set up server authentication server.transport.factory = self.Factory() server.passwordDelay = 0 # remove bad password delay realm = Realm() portal = Portal(realm) checker = SSHProtocolChecker() checker.registerChecker(PasswordChecker()) checker.registerChecker(PrivateKeyChecker()) checker.registerChecker(PAMChecker()) checker.areDone = lambda aId: ( len(checker.successfulCredentials[aId]) == 3) portal.registerChecker(checker) server.transport.factory.portal = portal d = loopback.loopbackAsync(server.transport, client.transport) server.transport.transport.logPrefix = lambda: '_ServerLoopback' client.transport.transport.logPrefix = lambda: '_ClientLoopback' server.serviceStarted() client.serviceStarted() def check(ignored): self.assertEquals(server.transport.service.name, 'TestService') return d.addCallback(check)
0
Example 16
View licensedef test_loopback(self): """ Test that the userauth server and client play nicely with each other. """ server = userauth.SSHUserAuthServer() client = ClientUserAuth('foo', self.Factory.Service()) # set up transports server.transport = transport.SSHTransportBase() server.transport.service = server server.transport.isEncrypted = lambda x: True client.transport = transport.SSHTransportBase() client.transport.service = client server.transport.sessionID = client.transport.sessionID = '' # don't send key exchange packet server.transport.sendKexInit = client.transport.sendKexInit = \ lambda: None # set up server authentication server.transport.factory = self.Factory() server.passwordDelay = 0 # remove bad password delay realm = Realm() portal = Portal(realm) checker = SSHProtocolChecker() checker.registerChecker(PasswordChecker()) checker.registerChecker(PrivateKeyChecker()) checker.registerChecker(PAMChecker()) checker.areDone = lambda aId: ( len(checker.successfulCredentials[aId]) == 3) portal.registerChecker(checker) server.transport.factory.portal = portal d = loopback.loopbackAsync(server.transport, client.transport) server.transport.transport.logPrefix = lambda: '_ServerLoopback' client.transport.transport.logPrefix = lambda: '_ClientLoopback' server.serviceStarted() client.serviceStarted() def check(ignored): self.assertEquals(server.transport.service.name, 'TestService') return d.addCallback(check)
0
Example 17
View licensedef test_handshake(self): """ The TLS handshake is performed when L{TLSMemoryBIOProtocol} is connected to a transport. """ clientFactory = ClientFactory() clientFactory.protocol = Protocol clientContextFactory, handshakeDeferred = ( HandshakeCallbackContextFactory.factoryAndDeferred()) wrapperFactory = TLSMemoryBIOFactory( clientContextFactory, True, clientFactory) sslClientProtocol = wrapperFactory.buildProtocol(None) serverFactory = ServerFactory() serverFactory.protocol = Protocol serverContextFactory = DefaultOpenSSLContextFactory(certPath, certPath) wrapperFactory = TLSMemoryBIOFactory( serverContextFactory, False, serverFactory) sslServerProtocol = wrapperFactory.buildProtocol(None) connectionDeferred = loopbackAsync(sslServerProtocol, sslClientProtocol) # Only wait for the handshake to complete. Anything after that isn't # important here. return handshakeDeferred
0
Example 18
View licensedef test_handshakeFailure(self): """ L{TLSMemoryBIOProtocol} reports errors in the handshake process to the application-level protocol object using its C{connectionLost} method and disconnects the underlying transport. """ clientConnectionLost = Deferred() clientFactory = ClientFactory() clientFactory.protocol = ( lambda: ConnectionLostNotifyingProtocol( clientConnectionLost)) clientContextFactory = HandshakeCallbackContextFactory() wrapperFactory = TLSMemoryBIOFactory( clientContextFactory, True, clientFactory) sslClientProtocol = wrapperFactory.buildProtocol(None) serverConnectionLost = Deferred() serverFactory = ServerFactory() serverFactory.protocol = ( lambda: ConnectionLostNotifyingProtocol( serverConnectionLost)) # This context factory rejects any clients which do not present a # certificate. certificateData = FilePath(certPath).getContent() certificate = PrivateCertificate.loadPEM(certificateData) serverContextFactory = certificate.options(certificate) wrapperFactory = TLSMemoryBIOFactory( serverContextFactory, False, serverFactory) sslServerProtocol = wrapperFactory.buildProtocol(None) connectionDeferred = loopbackAsync(sslServerProtocol, sslClientProtocol) def cbConnectionLost(protocol): # The connection should close on its own in response to the error # induced by the client not supplying the required certificate. # After that, check to make sure the protocol's connectionLost was # called with the right thing. protocol.lostConnectionReason.trap(Error) clientConnectionLost.addCallback(cbConnectionLost) serverConnectionLost.addCallback(cbConnectionLost) # Additionally, the underlying transport should have been told to # go away. return gatherResults([ clientConnectionLost, serverConnectionLost, connectionDeferred])
0
Example 19
View licensedef test_getPeerCertificate(self): """ L{TLSMemoryBIOFactory.getPeerCertificate} returns the L{OpenSSL.crypto.X509Type} instance representing the peer's certificate. """ # Set up a client and server so there's a certificate to grab. clientFactory = ClientFactory() clientFactory.protocol = Protocol clientContextFactory, handshakeDeferred = ( HandshakeCallbackContextFactory.factoryAndDeferred()) wrapperFactory = TLSMemoryBIOFactory( clientContextFactory, True, clientFactory) sslClientProtocol = wrapperFactory.buildProtocol(None) serverFactory = ServerFactory() serverFactory.protocol = Protocol serverContextFactory = DefaultOpenSSLContextFactory(certPath, certPath) wrapperFactory = TLSMemoryBIOFactory( serverContextFactory, False, serverFactory) sslServerProtocol = wrapperFactory.buildProtocol(None) connectionDeferred = loopbackAsync( sslServerProtocol, sslClientProtocol) # Wait for the handshake def cbHandshook(ignored): # Grab the server's certificate and check it out cert = sslClientProtocol.getPeerCertificate() self.assertIsInstance(cert, X509Type) self.assertEquals( cert.digest('md5'), '9B:A4:AB:43:10:BE:82:AE:94:3E:6B:91:F2:F3:40:E8') handshakeDeferred.addCallback(cbHandshook) return handshakeDeferred
0
Example 20
View licensedef test_writeAfterHandshake(self): """ Bytes written to L{TLSMemoryBIOProtocol} before the handshake is complete are received by the protocol on the other side of the connection once the handshake succeeds. """ bytes = "some bytes" clientProtocol = Protocol() clientFactory = ClientFactory() clientFactory.protocol = lambda: clientProtocol clientContextFactory, handshakeDeferred = ( HandshakeCallbackContextFactory.factoryAndDeferred()) wrapperFactory = TLSMemoryBIOFactory( clientContextFactory, True, clientFactory) sslClientProtocol = wrapperFactory.buildProtocol(None) serverProtocol = AccumulatingProtocol(len(bytes)) serverFactory = ServerFactory() serverFactory.protocol = lambda: serverProtocol serverContextFactory = DefaultOpenSSLContextFactory(certPath, certPath) wrapperFactory = TLSMemoryBIOFactory( serverContextFactory, False, serverFactory) sslServerProtocol = wrapperFactory.buildProtocol(None) connectionDeferred = loopbackAsync(sslServerProtocol, sslClientProtocol) # Wait for the handshake to finish before writing anything. def cbHandshook(ignored): clientProtocol.transport.write(bytes) # The server will drop the connection once it gets the bytes. return connectionDeferred handshakeDeferred.addCallback(cbHandshook) # Once the connection is lost, make sure the server received the # expected bytes. def cbDisconnected(ignored): self.assertEquals("".join(serverProtocol.received), bytes) handshakeDeferred.addCallback(cbDisconnected) return handshakeDeferred
0
Example 21
View licensedef test_writeBeforeHandshake(self): """ Bytes written to L{TLSMemoryBIOProtocol} before the handshake is complete are received by the protocol on the other side of the connection once the handshake succeeds. """ bytes = "some bytes" class SimpleSendingProtocol(Protocol): def connectionMade(self): self.transport.write(bytes) clientFactory = ClientFactory() clientFactory.protocol = SimpleSendingProtocol clientContextFactory, handshakeDeferred = ( HandshakeCallbackContextFactory.factoryAndDeferred()) wrapperFactory = TLSMemoryBIOFactory( clientContextFactory, True, clientFactory) sslClientProtocol = wrapperFactory.buildProtocol(None) serverProtocol = AccumulatingProtocol(len(bytes)) serverFactory = ServerFactory() serverFactory.protocol = lambda: serverProtocol serverContextFactory = DefaultOpenSSLContextFactory(certPath, certPath) wrapperFactory = TLSMemoryBIOFactory( serverContextFactory, False, serverFactory) sslServerProtocol = wrapperFactory.buildProtocol(None) connectionDeferred = loopbackAsync(sslServerProtocol, sslClientProtocol) # Wait for the connection to end, then make sure the server received # the bytes sent by the client. def cbConnectionDone(ignored): self.assertEquals("".join(serverProtocol.received), bytes) connectionDeferred.addCallback(cbConnectionDone) return connectionDeferred
0
Example 22
View licensedef test_writeSequence(self): """ Bytes written to L{TLSMemoryBIOProtocol} with C{writeSequence} are received by the protocol on the other side of the connection. """ bytes = "some bytes" class SimpleSendingProtocol(Protocol): def connectionMade(self): self.transport.writeSequence(list(bytes)) clientFactory = ClientFactory() clientFactory.protocol = SimpleSendingProtocol clientContextFactory = HandshakeCallbackContextFactory() wrapperFactory = TLSMemoryBIOFactory( clientContextFactory, True, clientFactory) sslClientProtocol = wrapperFactory.buildProtocol(None) serverProtocol = AccumulatingProtocol(len(bytes)) serverFactory = ServerFactory() serverFactory.protocol = lambda: serverProtocol serverContextFactory = DefaultOpenSSLContextFactory(certPath, certPath) wrapperFactory = TLSMemoryBIOFactory( serverContextFactory, False, serverFactory) sslServerProtocol = wrapperFactory.buildProtocol(None) connectionDeferred = loopbackAsync(sslServerProtocol, sslClientProtocol) # Wait for the connection to end, then make sure the server received # the bytes sent by the client. def cbConnectionDone(ignored): self.assertEquals("".join(serverProtocol.received), bytes) connectionDeferred.addCallback(cbConnectionDone) return connectionDeferred
0
Example 23
View licensedef test_multipleWrites(self): """ If multiple separate TLS messages are received in a single chunk from the underlying transport, all of the application bytes from each message are delivered to the application-level protocol. """ bytes = [str(i) for i in range(10)] class SimpleSendingProtocol(Protocol): def connectionMade(self): for b in bytes: self.transport.write(b) clientFactory = ClientFactory() clientFactory.protocol = SimpleSendingProtocol clientContextFactory = HandshakeCallbackContextFactory() wrapperFactory = TLSMemoryBIOFactory( clientContextFactory, True, clientFactory) sslClientProtocol = wrapperFactory.buildProtocol(None) serverProtocol = AccumulatingProtocol(sum(map(len, bytes))) serverFactory = ServerFactory() serverFactory.protocol = lambda: serverProtocol serverContextFactory = DefaultOpenSSLContextFactory(certPath, certPath) wrapperFactory = TLSMemoryBIOFactory( serverContextFactory, False, serverFactory) sslServerProtocol = wrapperFactory.buildProtocol(None) connectionDeferred = loopbackAsync(sslServerProtocol, sslClientProtocol, collapsingPumpPolicy) # Wait for the connection to end, then make sure the server received # the bytes sent by the client. def cbConnectionDone(ignored): self.assertEquals("".join(serverProtocol.received), ''.join(bytes)) connectionDeferred.addCallback(cbConnectionDone) return connectionDeferred
0
Example 24
View licensedef test_hugeWrite(self): """ If a very long string is passed to L{TLSMemoryBIOProtocol.write}, any trailing part of it which cannot be send immediately is buffered and sent later. """ bytes = "some bytes" factor = 8192 class SimpleSendingProtocol(Protocol): def connectionMade(self): self.transport.write(bytes * factor) clientFactory = ClientFactory() clientFactory.protocol = SimpleSendingProtocol clientContextFactory = HandshakeCallbackContextFactory() wrapperFactory = TLSMemoryBIOFactory( clientContextFactory, True, clientFactory) sslClientProtocol = wrapperFactory.buildProtocol(None) serverProtocol = AccumulatingProtocol(len(bytes) * factor) serverFactory = ServerFactory() serverFactory.protocol = lambda: serverProtocol serverContextFactory = DefaultOpenSSLContextFactory(certPath, certPath) wrapperFactory = TLSMemoryBIOFactory( serverContextFactory, False, serverFactory) sslServerProtocol = wrapperFactory.buildProtocol(None) connectionDeferred = loopbackAsync(sslServerProtocol, sslClientProtocol) # Wait for the connection to end, then make sure the server received # the bytes sent by the client. def cbConnectionDone(ignored): self.assertEquals("".join(serverProtocol.received), bytes * factor) connectionDeferred.addCallback(cbConnectionDone) return connectionDeferred
0
Example 25
View licensedef test_disorderlyShutdown(self): """ If a L{TLSMemoryBIOProtocol} loses its connection unexpectedly, this is reported to the application. """ clientConnectionLost = Deferred() clientFactory = ClientFactory() clientFactory.protocol = ( lambda: ConnectionLostNotifyingProtocol( clientConnectionLost)) clientContextFactory = HandshakeCallbackContextFactory() wrapperFactory = TLSMemoryBIOFactory( clientContextFactory, True, clientFactory) sslClientProtocol = wrapperFactory.buildProtocol(None) # Client speaks first, so the server can be dumb. serverProtocol = Protocol() connectionDeferred = loopbackAsync(serverProtocol, sslClientProtocol) # Now destroy the connection. serverProtocol.transport.loseConnection() # And when the connection completely dies, check the reason. def cbDisconnected(clientProtocol): clientProtocol.lostConnectionReason.trap(Error) clientConnectionLost.addCallback(cbDisconnected) return clientConnectionLost
0
Example 26
View licensedef test_loseConnectionAfterHandshake(self): """ L{TLSMemoryBIOProtocol.loseConnection} sends a TLS close alert and shuts down the underlying connection. """ clientConnectionLost = Deferred() clientFactory = ClientFactory() clientFactory.protocol = ( lambda: ConnectionLostNotifyingProtocol( clientConnectionLost)) clientContextFactory, handshakeDeferred = ( HandshakeCallbackContextFactory.factoryAndDeferred()) wrapperFactory = TLSMemoryBIOFactory( clientContextFactory, True, clientFactory) sslClientProtocol = wrapperFactory.buildProtocol(None) serverProtocol = Protocol() serverFactory = ServerFactory() serverFactory.protocol = lambda: serverProtocol serverContextFactory = DefaultOpenSSLContextFactory(certPath, certPath) wrapperFactory = TLSMemoryBIOFactory( serverContextFactory, False, serverFactory) sslServerProtocol = wrapperFactory.buildProtocol(None) connectionDeferred = loopbackAsync(sslServerProtocol, sslClientProtocol) # Wait for the handshake before dropping the connection. def cbHandshake(ignored): serverProtocol.transport.loseConnection() # Now wait for the client to notice. return clientConnectionLost handshakeDeferred.addCallback(cbHandshake) # Wait for the connection to end, then make sure the client was # notified of a handshake failure. def cbConnectionDone(clientProtocol): clientProtocol.lostConnectionReason.trap(ConnectionDone) # The server should have closed its underlying transport, in # addition to whatever it did to shut down the TLS layer. self.assertTrue(serverProtocol.transport.q.disconnect) # The client should also have closed its underlying transport once # it saw the server shut down the TLS layer, so as to avoid relying # on the server to close the underlying connection. self.assertTrue(clientProtocol.transport.q.disconnect) handshakeDeferred.addCallback(cbConnectionDone) return handshakeDeferred
0
Example 27
View licensedef test_handshake(self): """ The TLS handshake is performed when L{TLSMemoryBIOProtocol} is connected to a transport. """ clientFactory = ClientFactory() clientFactory.protocol = Protocol clientContextFactory, handshakeDeferred = ( HandshakeCallbackContextFactory.factoryAndDeferred()) wrapperFactory = TLSMemoryBIOFactory( clientContextFactory, True, clientFactory) sslClientProtocol = wrapperFactory.buildProtocol(None) serverFactory = ServerFactory() serverFactory.protocol = Protocol serverContextFactory = DefaultOpenSSLContextFactory(certPath, certPath) wrapperFactory = TLSMemoryBIOFactory( serverContextFactory, False, serverFactory) sslServerProtocol = wrapperFactory.buildProtocol(None) connectionDeferred = loopbackAsync(sslServerProtocol, sslClientProtocol) # Only wait for the handshake to complete. Anything after that isn't # important here. return handshakeDeferred
0
Example 28
View licensedef test_handshakeFailure(self): """ L{TLSMemoryBIOProtocol} reports errors in the handshake process to the application-level protocol object using its C{connectionLost} method and disconnects the underlying transport. """ clientConnectionLost = Deferred() clientFactory = ClientFactory() clientFactory.protocol = ( lambda: ConnectionLostNotifyingProtocol( clientConnectionLost)) clientContextFactory = HandshakeCallbackContextFactory() wrapperFactory = TLSMemoryBIOFactory( clientContextFactory, True, clientFactory) sslClientProtocol = wrapperFactory.buildProtocol(None) serverConnectionLost = Deferred() serverFactory = ServerFactory() serverFactory.protocol = ( lambda: ConnectionLostNotifyingProtocol( serverConnectionLost)) # This context factory rejects any clients which do not present a # certificate. certificateData = FilePath(certPath).getContent() certificate = PrivateCertificate.loadPEM(certificateData) serverContextFactory = certificate.options(certificate) wrapperFactory = TLSMemoryBIOFactory( serverContextFactory, False, serverFactory) sslServerProtocol = wrapperFactory.buildProtocol(None) connectionDeferred = loopbackAsync(sslServerProtocol, sslClientProtocol) def cbConnectionLost(protocol): # The connection should close on its own in response to the error # induced by the client not supplying the required certificate. # After that, check to make sure the protocol's connectionLost was # called with the right thing. protocol.lostConnectionReason.trap(Error) clientConnectionLost.addCallback(cbConnectionLost) serverConnectionLost.addCallback(cbConnectionLost) # Additionally, the underlying transport should have been told to # go away. return gatherResults([ clientConnectionLost, serverConnectionLost, connectionDeferred])
0
Example 29
View licensedef test_getPeerCertificate(self): """ L{TLSMemoryBIOFactory.getPeerCertificate} returns the L{OpenSSL.crypto.X509Type} instance representing the peer's certificate. """ # Set up a client and server so there's a certificate to grab. clientFactory = ClientFactory() clientFactory.protocol = Protocol clientContextFactory, handshakeDeferred = ( HandshakeCallbackContextFactory.factoryAndDeferred()) wrapperFactory = TLSMemoryBIOFactory( clientContextFactory, True, clientFactory) sslClientProtocol = wrapperFactory.buildProtocol(None) serverFactory = ServerFactory() serverFactory.protocol = Protocol serverContextFactory = DefaultOpenSSLContextFactory(certPath, certPath) wrapperFactory = TLSMemoryBIOFactory( serverContextFactory, False, serverFactory) sslServerProtocol = wrapperFactory.buildProtocol(None) connectionDeferred = loopbackAsync( sslServerProtocol, sslClientProtocol) # Wait for the handshake def cbHandshook(ignored): # Grab the server's certificate and check it out cert = sslClientProtocol.getPeerCertificate() self.assertIsInstance(cert, X509Type) self.assertEquals( cert.digest('md5'), '9B:A4:AB:43:10:BE:82:AE:94:3E:6B:91:F2:F3:40:E8') handshakeDeferred.addCallback(cbHandshook) return handshakeDeferred
0
Example 30
View licensedef test_writeAfterHandshake(self): """ Bytes written to L{TLSMemoryBIOProtocol} before the handshake is complete are received by the protocol on the other side of the connection once the handshake succeeds. """ bytes = "some bytes" clientProtocol = Protocol() clientFactory = ClientFactory() clientFactory.protocol = lambda: clientProtocol clientContextFactory, handshakeDeferred = ( HandshakeCallbackContextFactory.factoryAndDeferred()) wrapperFactory = TLSMemoryBIOFactory( clientContextFactory, True, clientFactory) sslClientProtocol = wrapperFactory.buildProtocol(None) serverProtocol = AccumulatingProtocol(len(bytes)) serverFactory = ServerFactory() serverFactory.protocol = lambda: serverProtocol serverContextFactory = DefaultOpenSSLContextFactory(certPath, certPath) wrapperFactory = TLSMemoryBIOFactory( serverContextFactory, False, serverFactory) sslServerProtocol = wrapperFactory.buildProtocol(None) connectionDeferred = loopbackAsync(sslServerProtocol, sslClientProtocol) # Wait for the handshake to finish before writing anything. def cbHandshook(ignored): clientProtocol.transport.write(bytes) # The server will drop the connection once it gets the bytes. return connectionDeferred handshakeDeferred.addCallback(cbHandshook) # Once the connection is lost, make sure the server received the # expected bytes. def cbDisconnected(ignored): self.assertEquals("".join(serverProtocol.received), bytes) handshakeDeferred.addCallback(cbDisconnected) return handshakeDeferred
0
Example 31
View licensedef test_writeBeforeHandshake(self): """ Bytes written to L{TLSMemoryBIOProtocol} before the handshake is complete are received by the protocol on the other side of the connection once the handshake succeeds. """ bytes = "some bytes" class SimpleSendingProtocol(Protocol): def connectionMade(self): self.transport.write(bytes) clientFactory = ClientFactory() clientFactory.protocol = SimpleSendingProtocol clientContextFactory, handshakeDeferred = ( HandshakeCallbackContextFactory.factoryAndDeferred()) wrapperFactory = TLSMemoryBIOFactory( clientContextFactory, True, clientFactory) sslClientProtocol = wrapperFactory.buildProtocol(None) serverProtocol = AccumulatingProtocol(len(bytes)) serverFactory = ServerFactory() serverFactory.protocol = lambda: serverProtocol serverContextFactory = DefaultOpenSSLContextFactory(certPath, certPath) wrapperFactory = TLSMemoryBIOFactory( serverContextFactory, False, serverFactory) sslServerProtocol = wrapperFactory.buildProtocol(None) connectionDeferred = loopbackAsync(sslServerProtocol, sslClientProtocol) # Wait for the connection to end, then make sure the server received # the bytes sent by the client. def cbConnectionDone(ignored): self.assertEquals("".join(serverProtocol.received), bytes) connectionDeferred.addCallback(cbConnectionDone) return connectionDeferred
0
Example 32
View licensedef test_writeSequence(self): """ Bytes written to L{TLSMemoryBIOProtocol} with C{writeSequence} are received by the protocol on the other side of the connection. """ bytes = "some bytes" class SimpleSendingProtocol(Protocol): def connectionMade(self): self.transport.writeSequence(list(bytes)) clientFactory = ClientFactory() clientFactory.protocol = SimpleSendingProtocol clientContextFactory = HandshakeCallbackContextFactory() wrapperFactory = TLSMemoryBIOFactory( clientContextFactory, True, clientFactory) sslClientProtocol = wrapperFactory.buildProtocol(None) serverProtocol = AccumulatingProtocol(len(bytes)) serverFactory = ServerFactory() serverFactory.protocol = lambda: serverProtocol serverContextFactory = DefaultOpenSSLContextFactory(certPath, certPath) wrapperFactory = TLSMemoryBIOFactory( serverContextFactory, False, serverFactory) sslServerProtocol = wrapperFactory.buildProtocol(None) connectionDeferred = loopbackAsync(sslServerProtocol, sslClientProtocol) # Wait for the connection to end, then make sure the server received # the bytes sent by the client. def cbConnectionDone(ignored): self.assertEquals("".join(serverProtocol.received), bytes) connectionDeferred.addCallback(cbConnectionDone) return connectionDeferred
0
Example 33
View licensedef test_multipleWrites(self): """ If multiple separate TLS messages are received in a single chunk from the underlying transport, all of the application bytes from each message are delivered to the application-level protocol. """ bytes = [str(i) for i in range(10)] class SimpleSendingProtocol(Protocol): def connectionMade(self): for b in bytes: self.transport.write(b) clientFactory = ClientFactory() clientFactory.protocol = SimpleSendingProtocol clientContextFactory = HandshakeCallbackContextFactory() wrapperFactory = TLSMemoryBIOFactory( clientContextFactory, True, clientFactory) sslClientProtocol = wrapperFactory.buildProtocol(None) serverProtocol = AccumulatingProtocol(sum(map(len, bytes))) serverFactory = ServerFactory() serverFactory.protocol = lambda: serverProtocol serverContextFactory = DefaultOpenSSLContextFactory(certPath, certPath) wrapperFactory = TLSMemoryBIOFactory( serverContextFactory, False, serverFactory) sslServerProtocol = wrapperFactory.buildProtocol(None) connectionDeferred = loopbackAsync(sslServerProtocol, sslClientProtocol, collapsingPumpPolicy) # Wait for the connection to end, then make sure the server received # the bytes sent by the client. def cbConnectionDone(ignored): self.assertEquals("".join(serverProtocol.received), ''.join(bytes)) connectionDeferred.addCallback(cbConnectionDone) return connectionDeferred
0
Example 34
View licensedef test_hugeWrite(self): """ If a very long string is passed to L{TLSMemoryBIOProtocol.write}, any trailing part of it which cannot be send immediately is buffered and sent later. """ bytes = "some bytes" factor = 8192 class SimpleSendingProtocol(Protocol): def connectionMade(self): self.transport.write(bytes * factor) clientFactory = ClientFactory() clientFactory.protocol = SimpleSendingProtocol clientContextFactory = HandshakeCallbackContextFactory() wrapperFactory = TLSMemoryBIOFactory( clientContextFactory, True, clientFactory) sslClientProtocol = wrapperFactory.buildProtocol(None) serverProtocol = AccumulatingProtocol(len(bytes) * factor) serverFactory = ServerFactory() serverFactory.protocol = lambda: serverProtocol serverContextFactory = DefaultOpenSSLContextFactory(certPath, certPath) wrapperFactory = TLSMemoryBIOFactory( serverContextFactory, False, serverFactory) sslServerProtocol = wrapperFactory.buildProtocol(None) connectionDeferred = loopbackAsync(sslServerProtocol, sslClientProtocol) # Wait for the connection to end, then make sure the server received # the bytes sent by the client. def cbConnectionDone(ignored): self.assertEquals("".join(serverProtocol.received), bytes * factor) connectionDeferred.addCallback(cbConnectionDone) return connectionDeferred
0
Example 35
View licensedef test_disorderlyShutdown(self): """ If a L{TLSMemoryBIOProtocol} loses its connection unexpectedly, this is reported to the application. """ clientConnectionLost = Deferred() clientFactory = ClientFactory() clientFactory.protocol = ( lambda: ConnectionLostNotifyingProtocol( clientConnectionLost)) clientContextFactory = HandshakeCallbackContextFactory() wrapperFactory = TLSMemoryBIOFactory( clientContextFactory, True, clientFactory) sslClientProtocol = wrapperFactory.buildProtocol(None) # Client speaks first, so the server can be dumb. serverProtocol = Protocol() connectionDeferred = loopbackAsync(serverProtocol, sslClientProtocol) # Now destroy the connection. serverProtocol.transport.loseConnection() # And when the connection completely dies, check the reason. def cbDisconnected(clientProtocol): clientProtocol.lostConnectionReason.trap(Error) clientConnectionLost.addCallback(cbDisconnected) return clientConnectionLost
0
Example 36
View licensedef test_loseConnectionAfterHandshake(self): """ L{TLSMemoryBIOProtocol.loseConnection} sends a TLS close alert and shuts down the underlying connection. """ clientConnectionLost = Deferred() clientFactory = ClientFactory() clientFactory.protocol = ( lambda: ConnectionLostNotifyingProtocol( clientConnectionLost)) clientContextFactory, handshakeDeferred = ( HandshakeCallbackContextFactory.factoryAndDeferred()) wrapperFactory = TLSMemoryBIOFactory( clientContextFactory, True, clientFactory) sslClientProtocol = wrapperFactory.buildProtocol(None) serverProtocol = Protocol() serverFactory = ServerFactory() serverFactory.protocol = lambda: serverProtocol serverContextFactory = DefaultOpenSSLContextFactory(certPath, certPath) wrapperFactory = TLSMemoryBIOFactory( serverContextFactory, False, serverFactory) sslServerProtocol = wrapperFactory.buildProtocol(None) connectionDeferred = loopbackAsync(sslServerProtocol, sslClientProtocol) # Wait for the handshake before dropping the connection. def cbHandshake(ignored): serverProtocol.transport.loseConnection() # Now wait for the client to notice. return clientConnectionLost handshakeDeferred.addCallback(cbHandshake) # Wait for the connection to end, then make sure the client was # notified of a handshake failure. def cbConnectionDone(clientProtocol): clientProtocol.lostConnectionReason.trap(ConnectionDone) # The server should have closed its underlying transport, in # addition to whatever it did to shut down the TLS layer. self.assertTrue(serverProtocol.transport.q.disconnect) # The client should also have closed its underlying transport once # it saw the server shut down the TLS layer, so as to avoid relying # on the server to close the underlying connection. self.assertTrue(clientProtocol.transport.q.disconnect) handshakeDeferred.addCallback(cbConnectionDone) return handshakeDeferred
0
Example 37
View licensedef _hostpeertest(self, get, testServer): """ Test one of the permutations of client/server host/peer. """ class TestProtocol(Protocol): def makeConnection(self, transport): Protocol.makeConnection(self, transport) self.onConnection.callback(transport) if testServer: server = TestProtocol() d = server.onConnection = Deferred() client = Protocol() else: server = Protocol() client = TestProtocol() d = client.onConnection = Deferred() loopback.loopbackAsync(server, client) def connected(transport): host = getattr(transport, get)() self.failUnless(IAddress.providedBy(host)) return d.addCallback(connected)
0
Example 38
View licensedef _greetingtest(self, write, testServer): """ Test one of the permutations of write/writeSequence client/server. """ class GreeteeProtocol(Protocol): bytes = "" def dataReceived(self, bytes): self.bytes += bytes if self.bytes == "bytes": self.received.callback(None) class GreeterProtocol(Protocol): def connectionMade(self): getattr(self.transport, write)("bytes") if testServer: server = GreeterProtocol() client = GreeteeProtocol() d = client.received = Deferred() else: server = GreeteeProtocol() d = server.received = Deferred() client = GreeterProtocol() loopback.loopbackAsync(server, client) return d
0
Example 39
View licensedef _producertest(self, producerClass): toProduce = map(str, range(0, 10)) class ProducingProtocol(Protocol): def connectionMade(self): self.producer = producerClass(list(toProduce)) self.producer.start(self.transport) class ReceivingProtocol(Protocol): bytes = "" def dataReceived(self, bytes): self.bytes += bytes if self.bytes == ''.join(toProduce): self.received.callback((client, server)) server = ProducingProtocol() client = ReceivingProtocol() client.received = Deferred() loopback.loopbackAsync(server, client) return client.received
0
Example 40
View licensedef test_writeNotReentrant(self): """ L{loopback.loopbackAsync} does not call a protocol's C{dataReceived} method while that protocol's transport's C{write} method is higher up on the stack. """ class Server(Protocol): def dataReceived(self, bytes): self.transport.write("bytes") class Client(Protocol): ready = False def connectionMade(self): reactor.callLater(0, self.go) def go(self): self.transport.write("foo") self.ready = True def dataReceived(self, bytes): self.wasReady = self.ready self.transport.loseConnection() server = Server() client = Client() d = loopback.loopbackAsync(client, server) def cbFinished(ignored): self.assertTrue(client.wasReady) d.addCallback(cbFinished) return d
0
Example 41
View licensedef test_pumpPolicy(self): """ The callable passed as the value for the C{pumpPolicy} parameter to L{loopbackAsync} is called with a L{_LoopbackQueue} of pending bytes and a protocol to which they should be delivered. """ pumpCalls = [] def dummyPolicy(queue, target): bytes = [] while queue: bytes.append(queue.get()) pumpCalls.append((target, bytes)) client = Protocol() server = Protocol() finished = loopback.loopbackAsync(server, client, dummyPolicy) self.assertEquals(pumpCalls, []) client.transport.write("foo") client.transport.write("bar") server.transport.write("baz") server.transport.write("quux") server.transport.loseConnection() def cbComplete(ignored): self.assertEquals( pumpCalls, # The order here is somewhat arbitrary. The implementation # happens to always deliver data to the client first. [(client, ["baz", "quux", None]), (server, ["foo", "bar"])]) finished.addCallback(cbComplete) return finished
0
Example 42
View licensedef _hostpeertest(self, get, testServer): """ Test one of the permutations of client/server host/peer. """ class TestProtocol(Protocol): def makeConnection(self, transport): Protocol.makeConnection(self, transport) self.onConnection.callback(transport) if testServer: server = TestProtocol() d = server.onConnection = Deferred() client = Protocol() else: server = Protocol() client = TestProtocol() d = client.onConnection = Deferred() loopback.loopbackAsync(server, client) def connected(transport): host = getattr(transport, get)() self.failUnless(IAddress.providedBy(host)) return d.addCallback(connected)
0
Example 43
View licensedef _greetingtest(self, write, testServer): """ Test one of the permutations of write/writeSequence client/server. """ class GreeteeProtocol(Protocol): bytes = "" def dataReceived(self, bytes): self.bytes += bytes if self.bytes == "bytes": self.received.callback(None) class GreeterProtocol(Protocol): def connectionMade(self): getattr(self.transport, write)("bytes") if testServer: server = GreeterProtocol() client = GreeteeProtocol() d = client.received = Deferred() else: server = GreeteeProtocol() d = server.received = Deferred() client = GreeterProtocol() loopback.loopbackAsync(server, client) return d
0
Example 44
View licensedef _producertest(self, producerClass): toProduce = map(str, range(0, 10)) class ProducingProtocol(Protocol): def connectionMade(self): self.producer = producerClass(list(toProduce)) self.producer.start(self.transport) class ReceivingProtocol(Protocol): bytes = "" def dataReceived(self, bytes): self.bytes += bytes if self.bytes == ''.join(toProduce): self.received.callback((client, server)) server = ProducingProtocol() client = ReceivingProtocol() client.received = Deferred() loopback.loopbackAsync(server, client) return client.received
0
Example 45
View licensedef test_writeNotReentrant(self): """ L{loopback.loopbackAsync} does not call a protocol's C{dataReceived} method while that protocol's transport's C{write} method is higher up on the stack. """ class Server(Protocol): def dataReceived(self, bytes): self.transport.write("bytes") class Client(Protocol): ready = False def connectionMade(self): reactor.callLater(0, self.go) def go(self): self.transport.write("foo") self.ready = True def dataReceived(self, bytes): self.wasReady = self.ready self.transport.loseConnection() server = Server() client = Client() d = loopback.loopbackAsync(client, server) def cbFinished(ignored): self.assertTrue(client.wasReady) d.addCallback(cbFinished) return d
0
Example 46
View licensedef test_pumpPolicy(self): """ The callable passed as the value for the C{pumpPolicy} parameter to L{loopbackAsync} is called with a L{_LoopbackQueue} of pending bytes and a protocol to which they should be delivered. """ pumpCalls = [] def dummyPolicy(queue, target): bytes = [] while queue: bytes.append(queue.get()) pumpCalls.append((target, bytes)) client = Protocol() server = Protocol() finished = loopback.loopbackAsync(server, client, dummyPolicy) self.assertEquals(pumpCalls, []) client.transport.write("foo") client.transport.write("bar") server.transport.write("baz") server.transport.write("quux") server.transport.loseConnection() def cbComplete(ignored): self.assertEquals( pumpCalls, # The order here is somewhat arbitrary. The implementation # happens to always deliver data to the client first. [(client, ["baz", "quux", None]), (server, ["foo", "bar"])]) finished.addCallback(cbComplete) return finished
0
0
0
Example 49
View licensedef setUp(self): self.server = DummyCacheProtocol() self.client = protocol.RRDCacheProtocol() self.client.factory = self loopback.loopbackAsync(self.server, self.client)
0
Example 50
View licensedef setUp(self): self.server = DummyCacheProtocol() self.client = protocol.RRDCacheProtocol() self.client.factory = self loopback.loopbackAsync(self.server, self.client)