示例#1
0
 def setUp(self):
     """
     Create a dummy terminal transport to test widgets against.
     """
     self.transport = None
     self.terminal = TerminalBuffer()
     self.terminal.width = self.width
     self.terminal.height = self.height
     self.terminal.makeConnection(self.transport)
示例#2
0
    def setUp(self):
        self.tcpConnections = []
        self.clock = Clock()

        self.transport = None
        self.terminal = TerminalBuffer()
        self.terminal.makeConnection(self.transport)
        self.protocol = UserInterface()
        self.protocol.reactor = self
        self.protocol.makeConnection(self.terminal)
示例#3
0
 def setUp(self):
     self.ssh = SSHProtocol()
     self.ssh.transport = TransportMock()
     self.ssh.terminal = TerminalBuffer()
     self.ssh.userName = "******"
     self.ssh.name = config.sshName
     self.ssh.port = config.sshPort
     FilesystemParser.honeytoken_directory = config.tokendir
     self.ssh._parser = FilesystemParser(config.resources +
                                         "/test_unix.xml")
     self.ssh.userIP = "TestIP"
     self.ssh.l = TestLogging
示例#4
0
class UserInterfaceTests(TestCase):
    """
    Test that the TerminalProtocol in charge of the user's terminal behaves in
    a bunch of desirable ways.
    """
    def setUp(self):
        self.transport = None
        self.terminal = TerminalBuffer()
        self.terminal.makeConnection(self.transport)
        self.protocol = UserInterface()


    def test_initialState(self):
        """
        Test that immediately after a connection is established the screen is
        cleared and cursor display is disabled.
        """
        # Scribble on the terminal so we can tell that it gets cleared.
        self.terminal.write('test bytes')
        self.protocol.makeConnection(self.terminal)
        self.failUnless(str(self.terminal).isspace())
        self.failIfIn(privateModes.CURSOR_MODE, self.terminal.privateModes)
示例#5
0
class InputParsingTests(TestCase):
    """
    Tests for dealing with user input which may contain commands or be a
    message destined for the network.
    """
    def setUp(self):
        self.tcpConnections = []
        self.clock = Clock()

        self.transport = None
        self.terminal = TerminalBuffer()
        self.terminal.makeConnection(self.transport)
        self.protocol = UserInterface()
        self.protocol.reactor = self
        self.protocol.makeConnection(self.terminal)


    def connectTCP(self, host, port, factory, timeout=30, bindAddress=''):
        self.tcpConnections.append((host, port, factory, timeout, bindAddress))


    def callLater(self, n, f, *a, **kw):
        return self.clock.callLater(n, f, *a, **kw)


    def test_commandDispatch(self):
        """
        Verify that a line starting with C{/} and a word is dispatched to a
        function determined by that word.
        """
        dispatched = []
        self.protocol.cmd_DISPATCHTEST = dispatched.append
        self.protocol.parseInputLine('/dispatchtest')
        self.assertEqual(dispatched, ['/dispatchtest'])


    def test_serverCommand(self):
        """
        Verify that C{/server} is interpreted as a command to establish a new
        server connection.  Also some more things (that a connection attempt is
        made, that when it succeeds an IRC login is attempted over it with the
        right nickname).

        This is poorly factored.  IRC testing should be done elsewhere.
        Connection setup testing should be done elsewhere.
        """
        # XXX See #2504 in Twisted tracker
        from twisted.words.im import ircsupport
        self.patch(ircsupport, 'reactor', self)

        self.protocol.cmd_SERVER('/server irc.example.org testuser')
        self.assertEqual(len(self.tcpConnections), 1)
        self.assertEqual(self.tcpConnections[0][:2], ('irc.example.org', 6667))
        factory = self.tcpConnections[0][2]
        protocol = factory.buildProtocol(('irc.example.org', 6667))
        transport = StringTransport()
        protocol.makeConnection(transport)

        while self.clock.calls:
            self.clock.advance(1)

        self.assertEqual(
            transport.value(),
            'NICK testuser\r\n'
            'USER testuser foo bar :Twisted-IM user\r\n')

        output = str(self.terminal).splitlines()
        input = output.pop()
        status = output.pop()
        report = output.pop()
        for L in output:
            self.assertEqual(L, ' ' * 80)
        message = '== Connection to irc.example.org established.'
        self.assertEqual(report, message + ' ' * (80 - len(message)))


    def test_serverCommandFailedConnection(self):
        """
        Like L{test_serverCommand} but for a connection which fails.
        """
        # XXX See #2504 in Twisted tracker
        from twisted.words.im import ircsupport
        self.patch(ircsupport, 'reactor', self)

        self.protocol.cmd_SERVER('/server irc.example.org testuser')
        self.assertEqual(len(self.tcpConnections), 1)
        self.assertEqual(self.tcpConnections[0][:2], ('irc.example.org', 6667))
        factory = self.tcpConnections[0][2]
        factory.clientConnectionFailed(None, TimeoutError("mock"))

        while self.clock.calls:
            self.clock.advance(1)

        output = str(self.terminal).splitlines()
        input = output.pop()
        status = output.pop()
        report = output.pop()
        for L in output:
            self.assertEqual(L, ' ' * 80)
        message = '== irc.example.org failed: User timeout caused connection failure: mock.'
        self.assertEqual(report, message + ' ' * (80 - len(message)))
示例#6
0
 def setUp(self):
     self.transport = None
     self.terminal = TerminalBuffer()
     self.terminal.makeConnection(self.transport)
     self.protocol = UserInterface()
示例#7
0
class StatusWidgetTests(TestCase):
    """
    Tests for the state tracking behavior of the status model object.
    """
    width = 80
    height = 1

    def setUp(self):
        """
        Create a dummy terminal transport to test widgets against.
        """
        self.transport = None
        self.terminal = TerminalBuffer()
        self.terminal.width = self.width
        self.terminal.height = self.height
        self.terminal.makeConnection(self.transport)


    def test_sizeHint(self):
        """
        Verify that the status widget asks for only one line of display area.
        """
        status = StatusWidget(DummyModel(None))
        self.assertEqual(status.sizeHint(), (None, 1))


    def test_rejectFocus(self):
        """
        Verify that the status widget will not take focus.
        """
        status = StatusWidget(DummyModel(None))
        self.assertRaises(YieldFocus, status.focusReceived)


    def test_noChannelRendering(self):
        """
        Verify that the status widget renders properly when there is no focused
        channel in its model.
        """
        status = StatusWidget(DummyModel(None))
        status.render(self.width, self.height, self.terminal)
        expected = '[%s] (No Channel)' % (version,)
        self.assertEqual(str(self.terminal), expected + ' ' * (self.width - len(expected)))


    def test_withChannelRendering(self):
        """
        Verify that the status widget renders properly when there is a focused
        channel in its model.
        """
        channel = '#example'
        status = StatusWidget(DummyModel(channel))
        status.render(self.width, self.height, self.terminal)
        expected = '[%s] %s' % (version, channel)
        self.assertEqual(str(self.terminal), expected + ' ' * (self.width - len(expected)))


    def test_shortenedStatus(self):
        """
        Verify that if a new status is shorter than the previous status, the
        previous status is completely erased.
        """
        longChannel = '#long-channel-name'
        shortChannel = '#short'
        status = StatusWidget(DummyModel(longChannel))
        status.render(self.width, self.height, self.terminal)
        status = StatusWidget(DummyModel(shortChannel))
        status.render(self.width, self.height, self.terminal)
        expected = '[%s] %s' % (version, shortChannel)
        self.assertEqual(str(self.terminal), expected + ' ' * (self.width - len(expected)))
示例#8
0
 def write(self, bytes):
     self.lastWrite = bytes
     TerminalBuffer.write(self, bytes)
示例#9
0
 def __str__(self):
     if hasattr(self, '__bytes__'):
         return self.__bytes__()
     else:
         return TerminalBuffer.__str__(self)
示例#10
0
 def setUp(self):
     self.transport = StringIO()
     self.terminal = TerminalBuffer()
     self.terminal.makeConnection(self.transport)
     self.widget = OutputWidget()
示例#11
0
class TextOutputTests(TestCase):
    """
    Verify that L{OutputWidget} renders properly in different situations.
    """
    width = 80
    height = 24

    def setUp(self):
        self.transport = StringIO()
        self.terminal = TerminalBuffer()
        self.terminal.makeConnection(self.transport)
        self.widget = OutputWidget()

    def test_noMessages(self):
        """
        Verify that when an L{OutputWidget} has no messages it renders as blank.
        """
        self.widget.render(self.width, self.height, self.terminal)
        self.failUnless(str(self.terminal).isspace())


    def test_oneMessage(self):
        """
        Verify that a single message is rendered at the bottom of the terminal.
        """
        message = 'Hello, world'
        self.widget.addMessage(message)
        self.widget.render(self.width, self.height, self.terminal)
        output = str(self.terminal).splitlines()
        message = output.pop()
        for L in output:
            self.assertEqual(L, ' ' * self.width)
        self.assertEqual(message, message + ' ' * (self.width - len(message)))


    def test_twoMessages(self):
        """
        Verify that a second message is placed below a first message, moving
        the first message up.
        """
        firstMessage = 'Hello, world'
        self.widget.addMessage(firstMessage)
        self.widget.render(self.width, self.height, self.terminal)
        secondMessage = 'Second'
        self.widget.addMessage(secondMessage)
        self.widget.render(self.width, self.height, self.terminal)
        output = str(self.terminal).splitlines()
        second = output.pop()
        first = output.pop()
        for L in output:
            self.assertEqual(L, ' ' * self.width)
        self.assertEqual(first, firstMessage + ' ' * (self.width - len(firstMessage)))
        self.assertEqual(second, secondMessage + ' ' * (self.width - len(secondMessage)))


    def test_messageWrapping(self):
        """
        Verify that a message longer than the terminal is wide is wrapped
        appropriately.
        """
        # 18 characters
        message = 'very long message '

        # 90 characters
        longMessage = message * 5

        self.widget.addMessage(longMessage)
        self.widget.render(self.width, self.height, self.terminal)

        output = str(self.terminal).splitlines()
        secondLine = output.pop()
        firstLine = output.pop()
        for L in output:
            self.assertEqual(L, ' ' * self.width)
        self.assertEqual(firstLine, 'very long message ' * 4 + 'very    ')
        self.assertEqual(secondLine, '  long message' + ' ' * (self.width - 14))
示例#12
0
 def setUp(self):
     self.transport = TerminalBuffer()
     self.protocol = self.protocolClass()
     self.protocol.makeConnection(self.transport)
示例#13
0
文件: Interface.py 项目: espes/esbot
 def write(self, bytes):
     self.lastWrite = bytes
     TerminalBuffer.write(self, bytes)