404

[ Avaa Bypassed ]




Upload:

Command:

botdev@18.188.212.154: ~ $
# -*- test-case-name: twisted.conch.test.test_manhole -*-
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

# pylint: disable=I0011,W9401,W9402

"""
Tests for L{twisted.conch.manhole}.
"""

import traceback

from twisted.trial import unittest
from twisted.internet import error, defer
from twisted.test.proto_helpers import StringTransport
from twisted.conch.test.test_recvline import (
    _TelnetMixin, _SSHMixin, _StdioMixin, stdio, ssh)
from twisted.conch import manhole
from twisted.conch.insults import insults


def determineDefaultFunctionName():
    """
    Return the string used by Python as the name for code objects which are
    compiled from interactive input or at the top-level of modules.
    """
    try:
        1 // 0
    except:
        # The last frame is this function.  The second to last frame is this
        # function's caller, which is module-scope, which is what we want,
        # so -2.
        return traceback.extract_stack()[-2][2]
defaultFunctionName = determineDefaultFunctionName()



class ManholeInterpreterTests(unittest.TestCase):
    """
    Tests for L{manhole.ManholeInterpreter}.
    """
    def test_resetBuffer(self):
        """
        L{ManholeInterpreter.resetBuffer} should empty the input buffer.
        """
        interpreter = manhole.ManholeInterpreter(None)
        interpreter.buffer.extend(["1", "2"])
        interpreter.resetBuffer()
        self.assertFalse(interpreter.buffer)



class ManholeProtocolTests(unittest.TestCase):
    """
    Tests for L{manhole.Manhole}.
    """
    def test_interruptResetsInterpreterBuffer(self):
        """
        L{manhole.Manhole.handle_INT} should cause the interpreter input buffer
        to be reset.
        """
        transport = StringTransport()
        terminal = insults.ServerProtocol(manhole.Manhole)
        terminal.makeConnection(transport)
        protocol = terminal.terminalProtocol
        interpreter = protocol.interpreter
        interpreter.buffer.extend(["1", "2"])
        protocol.handle_INT()
        self.assertFalse(interpreter.buffer)



class WriterTests(unittest.TestCase):
    def test_Integer(self):
        """
        Colorize an integer.
        """
        manhole.lastColorizedLine("1")


    def test_DoubleQuoteString(self):
        """
        Colorize an integer in double quotes.
        """
        manhole.lastColorizedLine('"1"')


    def test_SingleQuoteString(self):
        """
        Colorize an integer in single quotes.
        """
        manhole.lastColorizedLine("'1'")


    def test_TripleSingleQuotedString(self):
        """
        Colorize an integer in triple quotes.
        """
        manhole.lastColorizedLine("'''1'''")


    def test_TripleDoubleQuotedString(self):
        """
        Colorize an integer in triple and double quotes.
        """
        manhole.lastColorizedLine('"""1"""')


    def test_FunctionDefinition(self):
        """
        Colorize a function definition.
        """
        manhole.lastColorizedLine("def foo():")


    def test_ClassDefinition(self):
        """
        Colorize a class definition.
        """
        manhole.lastColorizedLine("class foo:")



class ManholeLoopbackMixin:
    serverProtocol = manhole.ColoredManhole


    def wfd(self, d):
        return defer.waitForDeferred(d)


    def test_SimpleExpression(self):
        """
        Evaluate simple expression.
        """
        done = self.recvlineClient.expect(b"done")

        self._testwrite(
            b"1 + 1\n"
            b"done")

        def finished(ign):
            self._assertBuffer(
                [b">>> 1 + 1",
                 b"2",
                 b">>> done"])

        return done.addCallback(finished)


    def test_TripleQuoteLineContinuation(self):
        """
        Evaluate line continuation in triple quotes.
        """
        done = self.recvlineClient.expect(b"done")

        self._testwrite(
            b"'''\n'''\n"
            b"done")

        def finished(ign):
            self._assertBuffer(
                [b">>> '''",
                 b"... '''",
                 b"'\\n'",
                 b">>> done"])

        return done.addCallback(finished)


    def test_FunctionDefinition(self):
        """
        Evaluate function definition.
        """
        done = self.recvlineClient.expect(b"done")

        self._testwrite(
            b"def foo(bar):\n"
            b"\tprint(bar)\n\n"
            b"foo(42)\n"
            b"done")

        def finished(ign):
            self._assertBuffer(
                [b">>> def foo(bar):",
                 b"...     print(bar)",
                 b"... ",
                 b">>> foo(42)",
                 b"42",
                 b">>> done"])

        return done.addCallback(finished)


    def test_ClassDefinition(self):
        """
        Evaluate class definition.
        """
        done = self.recvlineClient.expect(b"done")
        self._testwrite(
            b"class Foo:\n"
            b"\tdef bar(self):\n"
            b"\t\tprint('Hello, world!')\n\n"
            b"Foo().bar()\n"
            b"done")

        def finished(ign):
            self._assertBuffer(
                [b">>> class Foo:",
                 b"...     def bar(self):",
                 b"...         print('Hello, world!')",
                 b"... ",
                 b">>> Foo().bar()",
                 b"Hello, world!",
                 b">>> done"])

        return done.addCallback(finished)


    def test_Exception(self):
        """
        Evaluate raising an exception.
        """
        done = self.recvlineClient.expect(b"done")

        self._testwrite(
            b"raise Exception('foo bar baz')\n"
            b"done")

        def finished(ign):
            self._assertBuffer(
                [b">>> raise Exception('foo bar baz')",
                 b"Traceback (most recent call last):",
                 b'  File "<console>", line 1, in ' +
                 defaultFunctionName.encode("utf-8"),
                 b"Exception: foo bar baz",
                 b">>> done"])

        return done.addCallback(finished)


    def test_ControlC(self):
        """
        Evaluate interrupting with CTRL-C.
        """
        done = self.recvlineClient.expect(b"done")

        self._testwrite(
            b"cancelled line" + manhole.CTRL_C +
            b"done")

        def finished(ign):
            self._assertBuffer(
                [b">>> cancelled line",
                 b"KeyboardInterrupt",
                 b">>> done"])

        return done.addCallback(finished)


    def test_interruptDuringContinuation(self):
        """
        Sending ^C to Manhole while in a state where more input is required to
        complete a statement should discard the entire ongoing statement and
        reset the input prompt to the non-continuation prompt.
        """
        continuing = self.recvlineClient.expect(b"things")

        self._testwrite(b"(\nthings")

        def gotContinuation(ignored):
            self._assertBuffer(
                [b">>> (",
                 b"... things"])
            interrupted = self.recvlineClient.expect(b">>> ")
            self._testwrite(manhole.CTRL_C)
            return interrupted
        continuing.addCallback(gotContinuation)

        def gotInterruption(ignored):
            self._assertBuffer(
                [b">>> (",
                 b"... things",
                 b"KeyboardInterrupt",
                 b">>> "])
        continuing.addCallback(gotInterruption)
        return continuing


    def test_ControlBackslash(self):
        """
        Evaluate cancelling with CTRL-\.
        """
        self._testwrite(b"cancelled line")
        partialLine = self.recvlineClient.expect(b"cancelled line")

        def gotPartialLine(ign):
            self._assertBuffer(
                [b">>> cancelled line"])
            self._testwrite(manhole.CTRL_BACKSLASH)

            d = self.recvlineClient.onDisconnection
            return self.assertFailure(d, error.ConnectionDone)

        def gotClearedLine(ign):
            self._assertBuffer(
                [b""])

        return partialLine.addCallback(gotPartialLine).addCallback(
            gotClearedLine)


    @defer.inlineCallbacks
    def test_controlD(self):
        """
        A CTRL+D in the middle of a line doesn't close a connection,
        but at the beginning of a line it does.
        """
        self._testwrite(b"1 + 1")
        yield self.recvlineClient.expect(br"\+ 1")
        self._assertBuffer([b">>> 1 + 1"])

        self._testwrite(manhole.CTRL_D + b" + 1")
        yield self.recvlineClient.expect(br"\+ 1")
        self._assertBuffer([b">>> 1 + 1 + 1"])

        self._testwrite(b"\n")
        yield self.recvlineClient.expect(b"3\n>>> ")

        self._testwrite(manhole.CTRL_D)
        d = self.recvlineClient.onDisconnection
        yield self.assertFailure(d, error.ConnectionDone)


    @defer.inlineCallbacks
    def test_ControlL(self):
        """
        CTRL+L is generally used as a redraw-screen command in terminal
        applications.  Manhole doesn't currently respect this usage of it,
        but it should at least do something reasonable in response to this
        event (rather than, say, eating your face).
        """
        # Start off with a newline so that when we clear the display we can
        # tell by looking for the missing first empty prompt line.
        self._testwrite(b"\n1 + 1")
        yield self.recvlineClient.expect(br"\+ 1")
        self._assertBuffer([b">>> ", b">>> 1 + 1"])

        self._testwrite(manhole.CTRL_L + b" + 1")
        yield self.recvlineClient.expect(br"1 \+ 1 \+ 1")
        self._assertBuffer([b">>> 1 + 1 + 1"])


    def test_controlA(self):
        """
        CTRL-A can be used as HOME - returning cursor to beginning of
        current line buffer.
        """
        self._testwrite(b'rint "hello"' + b'\x01' + b'p')
        d = self.recvlineClient.expect(b'print "hello"')
        def cb(ignore):
            self._assertBuffer([b'>>> print "hello"'])
        return d.addCallback(cb)


    def test_controlE(self):
        """
        CTRL-E can be used as END - setting cursor to end of current
        line buffer.
        """
        self._testwrite(b'rint "hello' + b'\x01' + b'p' + b'\x05' + b'"')
        d = self.recvlineClient.expect(b'print "hello"')
        def cb(ignore):
            self._assertBuffer([b'>>> print "hello"'])
        return d.addCallback(cb)


    @defer.inlineCallbacks
    def test_deferred(self):
        """
        When a deferred is returned to the manhole REPL, it is displayed with
        a sequence number, and when the deferred fires, the result is printed.
        """
        self._testwrite(
            b"from twisted.internet import defer, reactor\n"
            b"d = defer.Deferred()\n"
            b"d\n")

        yield self.recvlineClient.expect(b"<Deferred #0>")

        self._testwrite(
            b"c = reactor.callLater(0.1, d.callback, 'Hi!')\n")
        yield self.recvlineClient.expect(b">>> ")

        yield self.recvlineClient.expect(
            b"Deferred #0 called back: 'Hi!'\n>>> ")
        self._assertBuffer(
            [b">>> from twisted.internet import defer, reactor",
             b">>> d = defer.Deferred()",
             b">>> d",
             b"<Deferred #0>",
             b">>> c = reactor.callLater(0.1, d.callback, 'Hi!')",
             b"Deferred #0 called back: 'Hi!'",
             b">>> "])



class ManholeLoopbackTelnetTests(_TelnetMixin, unittest.TestCase,
                                 ManholeLoopbackMixin):
    """
    Test manhole loopback over Telnet.
    """
    pass



class ManholeLoopbackSSHTests(_SSHMixin, unittest.TestCase,
                              ManholeLoopbackMixin):
    """
    Test manhole loopback over SSH.
    """
    if ssh is None:
        skip = "cryptography requirements missing"



class ManholeLoopbackStdioTests(_StdioMixin, unittest.TestCase,
                                ManholeLoopbackMixin):
    """
    Test manhole loopback over standard IO.
    """
    if stdio is None:
        skip = "Terminal requirements missing"
    else:
        serverProtocol = stdio.ConsoleManhole



class ManholeMainTests(unittest.TestCase):
    """
    Test the I{main} method from the I{manhole} module.
    """
    if stdio is None:
        skip = "Terminal requirements missing"


    def test_mainClassNotFound(self):
        """
        Will raise an exception when called with an argument which is a
        dotted patch which can not be imported..
        """
        exception = self.assertRaises(
            ValueError,
            stdio.main, argv=['no-such-class'],
            )

        self.assertEqual('Empty module name', exception.args[0])

Filemanager

Name Type Size Permission Actions
__pycache__ Folder 0755
__init__.py File 14 B 0644
keydata.py File 17.06 KB 0644
loopback.py File 757 B 0644
test_address.py File 1.59 KB 0644
test_agent.py File 12.78 KB 0644
test_cftp.py File 49.7 KB 0644
test_channel.py File 11.82 KB 0644
test_checkers.py File 30.76 KB 0644
test_ckeygen.py File 19.8 KB 0644
test_conch.py File 24.55 KB 0644
test_connection.py File 27.49 KB 0644
test_default.py File 11.31 KB 0644
test_endpoints.py File 51.96 KB 0644
test_filetransfer.py File 26.65 KB 0644
test_forwarding.py File 2.16 KB 0644
test_helper.py File 20.01 KB 0644
test_insults.py File 32.79 KB 0644
test_keys.py File 53.57 KB 0644
test_knownhosts.py File 48.26 KB 0644
test_manhole.py File 12.52 KB 0644
test_manhole_tap.py File 4.14 KB 0644
test_mixin.py File 1.03 KB 0644
test_openssh_compat.py File 4.52 KB 0644
test_recvline.py File 24.81 KB 0644
test_scripts.py File 1.84 KB 0644
test_session.py File 38.54 KB 0644
test_ssh.py File 31.62 KB 0644
test_tap.py File 4.83 KB 0644
test_telnet.py File 25.9 KB 0644
test_text.py File 3.85 KB 0644
test_transport.py File 89.71 KB 0644
test_unix.py File 2.47 KB 0644
test_userauth.py File 31.84 KB 0644
test_window.py File 2.07 KB 0644