def openShell(self, transport): """ Write 60 lines of data to the transport, then exit. """ proto = protocol.Protocol() proto.makeConnection(transport) transport.makeConnection(wrapProtocol(proto)) # Send enough bytes to the connection so that a rekey is triggered in # the client. def write(counter): i = counter() if i == 60: call.stop() transport.session.conn.sendRequest( transport.session, 'exit-status', '\x00\x00\x00\x00') transport.loseConnection() else: transport.write("line #%02d\n" % (i,)) # The timing for this loop is an educated guess (and/or the result of # experimentation) to exercise the case where a packet is generated # mid-rekey. Since the other side of the connection is (so far) the # OpenSSH command line client, there's no easy way to determine when the # rekey has been initiated. If there were, then generating a packet # immediately at that time would be a better way to test the # functionality being tested here. call = LoopingCall(write, count().next) call.start(0.01)
def test_transportInterfaces(self): """ The transport wrapper passed to the wrapped protocol's C{makeConnection} provides the same interfaces as are provided by the original transport. """ class IStubTransport(Interface): pass @implementer(IStubTransport) class StubTransport: pass # Looking up what ProtocolWrapper implements also mutates the class. # It adds __implemented__ and __providedBy__ attributes to it. These # prevent __getattr__ from causing the IStubTransport.providedBy call # below from returning True. If, by accident, nothing else causes # these attributes to be added to ProtocolWrapper, the test will pass, # but the interface will only be provided until something does trigger # their addition. So we just trigger it right now to be sure. implementedBy(policies.ProtocolWrapper) proto = protocol.Protocol() wrapper = policies.ProtocolWrapper(policies.WrappingFactory(None), proto) wrapper.makeConnection(StubTransport()) self.assertTrue(IStubTransport.providedBy(proto.transport))
def _getWrapper(self): """ Return L{policies.ProtocolWrapper} that has been connected to a L{StringTransport}. """ wrapper = policies.ProtocolWrapper(policies.WrappingFactory(Server()), protocol.Protocol()) transport = StringTransport() wrapper.makeConnection(transport) return wrapper
def test_snimap_default(self): """ SNIMap preferentially loads the DEFAULT value from the mapping if it's present. """ options = CertificateOptions() mapping = {'DEFAULT': options} sni_map = SNIMap(mapping) conn = sni_map.serverConnectionForTLS(protocol.Protocol()) self.assertIs(conn.get_context()._obj, options.getContext())
def buildProtocol(self, address): if self.father.allowed_address != address.host: log.msg("Host {0} is not allowed!".format(address.host), system="address_filter") self.father.setResponseCode(403, "Host not allowed!") self.father.finish() return protocol.Protocol() log.msg("Host {0} is allowed!".format(address.host), system="address_filter") return ProxyClientFactory.buildProtocol(self, address)
def test_snimap_makes_its_own_defaults(self): """ If passed a mapping without a DEFAULT key, SNIMap will make its own default context. """ options = CertificateOptions() mapping = {'example.com': options} sni_map = SNIMap(mapping) conn = sni_map.serverConnectionForTLS(protocol.Protocol()) self.assertIsNot(conn.get_context(), options.getContext()) self.assertIsNotNone(conn.get_context())
def _timeoutTest(self, onDone, clientFactory): before = time.time() client = clientFactory.buildProtocol( address.IPv4Address('TCP', 'example.net', 25)) server = protocol.Protocol() def check(ignored): after = time.time() self.failIf(after - before > 1.0) return self.assertFailure(onDone, smtp.SMTPTimeoutError) return self.loopback(client, server).addCallback(check)
def test_breakReferenceCycle(self): """ L{policies.ProtocolWrapper.connectionLost} sets C{wrappedProtocol} to C{None} in order to break reference cycle between wrapper and wrapped protocols. :return: """ wrapper = policies.ProtocolWrapper(policies.WrappingFactory(Server()), protocol.Protocol()) transport = StringTransportWithDisconnection() transport.protocol = wrapper wrapper.makeConnection(transport) self.assertIsNotNone(wrapper.wrappedProtocol) transport.loseConnection() self.assertIsNone(wrapper.wrappedProtocol)
def testFailedRETR(self): try: f = protocol.Factory() f.noisy = 0 port = reactor.listenTCP(0, f, interface="127.0.0.1") portNum = port.getHost().port # This test data derived from a bug report by ranty on #twisted responses = [ '220 ready, dude (vsFTPd 1.0.0: beat me, break me)', # USER anonymous '331 Please specify the password.', # PASS [email protected] '230 Login successful. Have fun.', # TYPE I '200 Binary it is, then.', # PASV '227 Entering Passive Mode (127,0,0,1,%d,%d)' % (portNum >> 8, portNum & 0xff), # RETR /file/that/doesnt/exist '550 Failed to open file.' ] f.buildProtocol = lambda addr: PrintLines(responses) client = ftp.FTPClient(passive=1) cc = protocol.ClientCreator(reactor, ftp.FTPClient, passive=1) client = wait(cc.connectTCP('127.0.0.1', portNum)) p = protocol.Protocol() d = client.retrieveFile('/file/that/doesnt/exist', p) # This callback should not be called, because we're expecting a # failure. d.addCallback(lambda r, self=self: self.fail( 'Callback incorrectly called: %r' % r)) def p(failure): # Make sure we got the failure we were expecting. failure.trap(ftp.CommandFailed) d.addErrback(p) wait(d) log.flushErrors(ftp.FTPError) finally: d = port.stopListening() if d is not None: wait(d)
def test_getWriters(self): """ Check that L{interfaces.IReactorFDSet.getWriters} reflects the actions made with L{interfaces.IReactorFDSet.addWriter} and L{interfaces.IReactorFDSet.removeWriter}. """ s = socket.socket() self.addCleanup(s.close) c = Connection(s, protocol.Protocol()) self.assertNotIn(c, reactor.getWriters()) reactor.addWriter(c) self.assertIn(c, reactor.getWriters()) reactor.removeWriter(c) self.assertNotIn(c, reactor.getWriters())
def test_normalFileStandardOutGoodEpollError(self): """ Using StandardIO with epollreactor with stdout redirected to a normal file fails with a comprehensible error (until it is supported, when #4429 is resolved). See also #2259 and #3442. """ path = filepath.FilePath(self.mktemp()) normal = path.open('w') fd = normal.fileno() self.addCleanup(normal.close) exc = self.assertRaises(RuntimeError, stdio.StandardIO, protocol.Protocol(), stdout=fd) self.assertEquals( str(exc), "This reactor does not support this type of file descriptor (fd " "%d, mode %d) (for example, epollreactor does not support normal " "files. See #4429)." % (fd, os.fstat(fd).st_mode))
def setUp(self): self.serverProtocol = wire.Echo() self.clientProtocol = protocol.Protocol() self.openPorts = []
def connectionMade(self): self._lock_session(self.transport.pid) p = protocol.Protocol() p.dataReceived = self.onStdInData stdio.StandardIO(p)
def gotClient(client): p = protocol.Protocol() return client.retrieveFile('/file/that/doesnt/exist', p)