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_session.cpython-310.pyc
o

�b���@s�dZddlZddlZddlZddlZddlmZddlmZddl	m
Z
mZmZddl
mZddlmZmZddlmZmZdd	lmZdd
lmZddlmZddlmZed
�ZergddlmZm Z m!Z!nGdd�d�Z!Gdd�d�Z"Gdd�d�Z#ee!j$�Gdd�d��Z%ee!j&�Gdd�de%��Z'Gdd�d�Z(Gdd�dej)�Z*Gdd�d�Z+Gdd �d �Z,Gd!d"�d"e,�Z-Gd#d$�d$�Z.Gd%d&�d&ee�Z/Gd'd(�d(ee�Z0Gd)d*�d*e�Z1Gd+d,�d,e�Z2Gd-d.�d.e�Z3Gd/d0�d0e�Z4dS)1zb
Tests for the 'session' channel implementation in twisted.conch.ssh.session.

See also RFC 4254.
�N)�skipIf)�implementer)�defer�error�protocol��IPv4Address)�ProcessDone�ProcessTerminated)�
components�failure)�Failure)�
requireModule)�RegistryUsingMixin)�TestCase�cryptography)�common�
connection�sessionc@s eZdZddlmZmZmZdS)rr)�EnvironmentVariableNotPermitted�ISession�ISessionSetEnvN)�__name__�
__module__�__qualname__�twisted.conch.interfacesrrr�rr�A/usr/lib/python3/dist-packages/twisted/conch/test/test_session.pyr"src@�eZdZdZdd�ZdS)�SubsystemOnlyAvatarz^
    A stub class representing an avatar that is only useful for
    getting a subsystem.
    cCs|dksJ�t�S)z�
        If the other side requests the 'subsystem' subsystem, allow it by
        returning a MockProtocol to implement it. Otherwise raise an assertion.
        �	subsystem)�MockProtocol��self�name�datarrr�lookupSubsystem0sz#SubsystemOnlyAvatar.lookupSubsystemN�rrr�__doc__r&rrrrr*�rc@r)�
StubAvatarz�
    A stub class representing the avatar representing the authenticated user.
    It implements the I{ISession} interface.
    cCs"|dkrt�|_||j_|jSdS)z�
        If the user requests the TestSubsystem subsystem, connect them to a
        MockProtocol.  If they request neither, then None is returned which is
        interpreted by SSHSession as a failure.
        �
TestSubsystemN)r!�	subsystem�
packetDatar"rrrr&?s
�zStubAvatar.lookupSubsystemNr'rrrrr*9r)r*c@sHeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dS)�StubSessionForStubAvatara2
    A stub ISession implementation for our StubAvatar.  The instance
    variables generally keep track of method invocations so that we can test
    that the methods were called.

    @ivar avatar: the L{StubAvatar} we are adapting.
    @ivar ptyRequest: if present, the terminal, window size, and modes passed
        to the getPty method.
    @ivar windowChange: if present, the window size passed to the
        windowChangned method.
    @ivar shellProtocol: if present, the L{SSHSessionProcessProtocol} passed
        to the openShell method.
    @ivar shellTransport: if present, the L{EchoTransport} connected to
        shellProtocol.
    @ivar execProtocol: if present, the L{SSHSessionProcessProtocol} passed
        to the execCommand method.
    @ivar execTransport: if present, the L{EchoTransport} connected to
        execProtocol.
    @ivar execCommandLine: if present, the command line passed to the
        execCommand method.
    @ivar gotEOF: if present, an EOF message was received.
    @ivar gotClosed: if present, a closed message was received.
    cCs||_d|_dS)z2
        Store the avatar we're adapting.
        N)�avatar�
shellProtocol�r#r/rrr�__init__e�
z!StubSessionForStubAvatar.__init__cCs |dkr|||f|_dStd��)zw
        If the terminal is 'bad', fail.  Otherwise, store the information in
        the ptyRequest variable.
        �badznot getting a ptyN)�
ptyRequest�RuntimeError)r#�terminal�window�modesrrr�getPtylszStubSessionForStubAvatar.getPtycCs|dkrtd��||_dS)zw
        If all the window sizes are 0, fail.  Otherwise, store the size in the
        windowChange variable.
        �rrrrznot changing the window sizeN)r6�windowChange)r#r8rrr�
windowChangedvs
z&StubSessionForStubAvatar.windowChangedcCs&|jdur	td��||_t|�|_dS)z�
        If we have gotten a shell request before, fail.  Otherwise, store the
        process protocol in the shellProtocol variable, connect it to the
        EchoTransport and store that as shellTransport.
        Nznot getting a shell this time)r0r6�
EchoTransport�shellTransport)r#�pprrr�	openShell�s
z"StubSessionForStubAvatar.openShellcCsV||_|dkr||_dS|dd�dkr'||_t|�|_|�|dd��dStd��)z�
        If the command is 'true', store the command, the process protocol, and
        the transport we connect to the process protocol.  Otherwise, just
        store the command and raise an error.
        �successN�srepeat�znot getting a command)�execCommandLine�execProtocolr>�
execTransport�outReceivedr6)r#r@�commandrrr�execCommand�s

z$StubSessionForStubAvatar.execCommandcC�
d|_dS)z2
        Note that EOF has been received.
        TN)�gotEOF�r#rrr�eofReceived��
z$StubSessionForStubAvatar.eofReceivedcCrK)z4
        Note that close has been received.
        TN)�	gotClosedrMrrr�closed�rOzStubSessionForStubAvatar.closedN)rrrr(r2r:r=rArJrNrQrrrrr.Ks

r.cs0eZdZdZ�fdd�Zdd�Zdd�Z�ZS)�StubSessionForStubAvatarWithEnva�
    Same as StubSessionForStubAvatar, but supporting environment variables
    setting.

    End users would want to have the same class annotated with
    C{@implementer(session.ISession, session.ISessionSetEnv)}. The interfaces
    are split for backwards compatibility, so we split it here to test
    this compatibility too.

    @ivar environ: a L{dict} of environment variables passed to the setEnv
    method.
    cst��|�i|_i|_dS�N)�superr2�environ�environAtPtyr1��	__class__rrr2�s
z(StubSessionForStubAvatarWithEnv.__init__cCs0|dkrtd��|dkrt�d��||j|<dS)ak
        If the requested environment variable is 'FAIL', fail.  If it is
        'IGNORED', raise EnvironmentVariableNotPermitted, which should cause
        it to be silently ignored.  Otherwise, store the requested
        environment variable.

        (Real applications should normally implement an allowed list rather
        than a blocked list.)
        �FAILz$disallowed environment variable name�IGNOREDz!ignored environment variable nameN)r6rrrU)r#r$�valuerrr�setEnv�s
�z&StubSessionForStubAvatarWithEnv.setEnvcCs|j��|_dS)zs
        Just a simple implementation which records the current environment
        when PTY is requested.
        N)rU�copyrV)r#�term�
windowSizer9rrrr:�sz&StubSessionForStubAvatarWithEnv.getPty)rrrr(r2r\r:�
__classcell__rrrWrrR�s

rRc@s(eZdZdZdd�Zdd�Zdd�ZdS)	r>ay
    A transport for a ProcessProtocol which echos data that is sent to it with
    a Window newline (CR LF) appended to it.  If a null byte is in the data,
    disconnect.  When we are asked to disconnect, disconnect the
    C{ProcessProtocol} with a 0 exit code.

    @ivar proto: the C{ProcessProtocol} connected to us.
    @ivar data: a L{bytes} of data written to us.
    cCs ||_d|_d|_|�|�dS)z�
        Initialize our instance variables.

        @param processProtocol: a C{ProcessProtocol} to connect to ourself.
        F�N)�protorQr%�makeConnection)r#�processProtocolrrrr2�szEchoTransport.__init__cCs>|j|7_|j�|�|j�d�d|vr|��dSdS)z�
        We got some data.  Give it back to our C{ProcessProtocol} with
        a newline attached.  Disconnect if there's a null byte.
        �
�N)r%rbrH�loseConnection�r#r%rrr�write�s�zEchoTransport.writec	CsN|jrdSd|_|j��|j��|j��|j�t�t�	ddd���dS)z�
        If we're asked to disconnect (and we haven't already) shut down
        the C{ProcessProtocol} with a 0 exit code.
        N�r)
rQrb�inConnectionLost�outConnectionLost�errConnectionLost�processEndedrr
rr
rMrrrrg�s


 zEchoTransport.loseConnectionN)rrrr(r2rirgrrrrr>�s

r>c@s,eZdZdZdZdd�Zdd�Zdd�Zd	S)
r!a>
    A sample Protocol which stores the data passed to it.

    @ivar packetData: a L{bytes} of data to be sent when the connection is
        made.
    @ivar data: a L{bytes} of the data passed to us.
    @ivar open: True if the channel is open.
    @ivar reason: if not None, the reason the protocol was closed.
    racCs,d|_d|_d|_|jr|�|j�dSdS)zc
        Set up the instance variables.  If we have any packetData, send it
        along.
        raTN)r%�open�reasonr-�dataReceivedrMrrr�connectionMades�zMockProtocol.connectionMadecCs"|j|7_|j�|d�dS)z�
        Store the received data and write it back with a tilde appended.
        The tilde is appended so that the tests can verify that we processed
        the data.
        �~N)r%�	transportrirhrrrrq"szMockProtocol.dataReceivedcCsd|_||_dS)z:
        Close the protocol and store the reason.
        FN)rorp)r#rprrr�connectionLost+r3zMockProtocol.connectionLostN)rrrr(r-rrrqrurrrrr!	s
	r!c@sLeZdZdZddd�Zdd�Zdd�Zd	d
�Zddd
�Zdd�Z	dd�Z
dS)�StubConnectiona�
    A stub for twisted.conch.ssh.connection.SSHConnection.  Record the data
    that channels send, and when they try to close the connection.

    @ivar data: a L{dict} mapping C{SSHChannel}s to a C{list} of L{bytes} of
        data they sent.
    @ivar extData: a L{dict} mapping L{SSHChannel}s to a C{list} of L{tuple} of
        (L{int}, L{bytes}) of extended data they sent.
    @ivar requests: a L{dict} mapping L{SSHChannel}s to a C{list} of L{tuple}
        of (L{str}, L{bytes}) of channel requests they made.
    @ivar eofs: a L{dict} mapping L{SSHChannel}s to C{true} if they have sent
        an EOF.
    @ivar closes: a L{dict} mapping L{SSHChannel}s to C{true} if they have sent
        a close.
    NcCs(i|_i|_i|_i|_i|_||_dS)z4
        Initialize our instance variables.
        N)r%�extData�requests�eofs�closesrt)r#rtrrrr2Ds
zStubConnection.__init__cC�dS)z,
        Return our logging prefix.
        �MockConnectionrrMrrr�	logPrefixOszStubConnection.logPrefixcCs|j�|g��|�dS)z'
        Record the sent data.
        N)r%�
setdefault�append)r#�channelr%rrr�sendDataUszStubConnection.sendDatacCs|j�|g��||f�dS)z0
        Record the sent extended data.
        N)rwr~r)r#r��typer%rrr�sendExtendedData[�zStubConnection.sendExtendedDataFcCs,|j�|g��|||f�|rt�d�SdS)z2
        Record the sent channel request.
        N)rxr~rr�succeed)r#r��requestr%�	wantReplyrrr�sendRequestas
�zStubConnection.sendRequestcC�d|j|<dS)z&
        Record the sent EOF.
        TN)ry�r#r�rrr�sendEOFi�zStubConnection.sendEOFcCr�)z(
        Record the sent close.
        TN)rzr�rrr�	sendCloseor�zStubConnection.sendCloserS)F)rrrr(r2r}r�r�r�r�r�rrrrrv3s

rvc@s@eZdZdZdZdZdd�Zdd�Zdd	�Zd
d�Z	dd
�Z
dS)�
StubTransportz�
    A stub transport which records the data written.

    @ivar buf: the data sent to the transport.
    @type buf: L{bytes}

    @ivar close: flags indicating if the transport has been closed.
    @type close: L{bool}
    raFcC�tddd�S)�2
        Return an arbitrary L{IAddress}.
        �TCP�
remotehosti�"rrMrrr�getPeer��zStubTransport.getPeercCr�)r�r��	localhosti'rrMrrr�getHost�r�zStubTransport.getHostcC�|j|7_dS)z,
        Record data in the buffer.
        N)�bufrhrrrri�szStubTransport.writecCrK)z6
        Note that the connection was closed.
        TN)�closerMrrrrg�rOzStubTransport.loseConnectioncCr{)z0
        Pretend to set C{TCP_NODELAY}.
        Nr)r#�enabledrrr�
setTcpNoDelay�szStubTransport.setTcpNoDelayN)rrrr(r�r�r�r�rirgr�rrrrr�vs
r�c@seZdZdZdZdd�ZdS)�StubTransportWithWriteErrz�
    A version of StubTransport which records the error data sent to it.

    @ivar err: the extended data sent to the transport.
    @type err: L{bytes}
    racCr�)z�
        Record the extended data in the buffer.  This was an old interface
        that allowed the Transports from ISession.openShell() or
        ISession.execCommand() to receive extended data from the client.
        N)�errrhrrr�writeErr�sz"StubTransportWithWriteErr.writeErrN)rrrr(r�r�rrrrr��sr�c@r)�
StubClientz�
    A stub class representing the client to a SSHSession.

    @ivar transport: A L{StubTransport} object which keeps track of the data
        passed to it.
    cCst�|_dSrS)r�rtrMrrrr2�szStubClient.__init__N)rrrr(r2rrrrr��sr�c@s�eZdZdZes
dZd;dd�Zd;dd�Zdd	�Zd
d�Z	dd
�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Zd8d9�Z d:S)<�SessionInterfaceTestsz�
    Tests for the SSHSession class interface.  This interface is not ideal, but
    it is tested in order to maintain backwards compatibility.
    �cannot run without cryptographyTcCs6t�|�|��|_|rt�tttj�|��|_dS)z�
        Make an SSHSession object to test.  Give the channel some window
        so that it's allowed to send packets.  500 and 100 are arbitrary
        values.
        N)	r�setUp�
getSSHSessionrr�registerAdapterrRr*r�r#�register_adaptersrrrr��s

�zSessionInterfaceTests.setUpcCstjddt�t�d�S)z+
        Return a new SSH session.
        ���d)�remoteWindow�remoteMaxPacket�connr/)r�
SSHSessionrvr*r�rrrr��s�z#SessionInterfaceTests.getSSHSessioncCs|�|jjt�dS)zj
        Asserts that self.session.session is an instance of
        StubSessionForStubOldAvatar.
        N)�assertIsInstancerr.rMrrr�assertSessionIsStubSession�sz0SessionInterfaceTests.assertSessionIsStubSessioncCs6tjtd�}|�|jd�|�|j�|�|j�dS)z�
        SSHSession initializes its buffer (buf), client, and ISession adapter.
        The avatar should not need to be adaptable to an ISession immediately.
        )r/raN)rr��object�assertEqualr��assertIsNone�client)r#�srrr�	test_init�szSessionInterfaceTests.test_initcCs:|j�d�t�|j_|j�d�|�|jjjjd�dS)z�
        SSHSession.dataReceived() passes data along to a client.  If the data
        comes before there is a client, the data should be discarded.
        �1�2N)rrqr�r�r�rtr�rMrrr�test_client_dataReceived�s
z.SessionInterfaceTests.test_client_dataReceivedcCsP|j�tjd�|j�dd�t�|j_|j�tjd�|�|jjjjd�dS)z�
        SSHSession.extReceived() passed data of type EXTENDED_DATA_STDERR along
        to the client.  If the data comes before there is a client, or if the
        data is not of type EXTENDED_DATA_STDERR, it is discared.
        r��r��3N)	r�extReceivedr�EXTENDED_DATA_STDERRr�r�r�rtr�rMrrr�test_client_extReceiveds

z-SessionInterfaceTests.test_client_extReceivedcCs*t�}|j_t�|_|j�tjd�dS)z�
        SSHSession.extReceived() should handle the case where the transport
        on the client doesn't have a writeErr method.
        �ignoredN)r�rr�r�rtr�rr��r#r�rrr�&test_client_extReceivedWithoutWriteErrsz<SessionInterfaceTests.test_client_extReceivedWithoutWriteErrcCs6t�|j_|j��|�|jjjj�d|jjj_dS)z}
        SSHSession.closed() should tell the transport connected to the client
        that the connection was lost.
        FN)r�rr�rQ�
assertTruertr�rMrrr�test_client_closeds

z(SessionInterfaceTests.test_client_closedcCs0|j�dt�d��}|�|�|�|jj�dS)zV
        When a subsystem request fails, SSHSession.client should not be set.
        r sBadSubsystemN)r�requestReceivedr�NS�assertFalser�r��r#�retrrr�$test_badSubsystemDoesNotCreateClient!s
z:SessionInterfaceTests.test_badSubsystemDoesNotCreateClientcCsR|j�dt�d�d�}|�|�|�|jjtj�|�	|jjj
j|jjj
�dS)z�
        When a client requests a subsystem, the SSHSession object should get
        the subsystem by calling avatar.lookupSubsystem, and attach it as
        the client.
        r r+�dataN)rr�rr�r�r�r�r�ProcessProtocol�assertIsrtrbr/r,r�rrr�test_lookupSubsystem)s�
�z*SessionInterfaceTests.test_lookupSubsystemcCs�tjt�t�d�}|�t�d�d�}|�|�|�|j	�|�
|jj�
|��|��|�|jj�
|��|��|��dS)z�
        Previously, if one only wanted to implement a subsystem, an ISession
        adapter wasn't needed because subsystems were looked up using the
        lookupSubsystem method on the avatar.
        )r/r�r r�N)rr�rrv�request_subsystemrr�r��assertIsNotNoner�r�r�rz�getrNrgrQ)r#r�r�rrr�'test_lookupSubsystemDoesNotNeedISession8s
z=SessionInterfaceTests.test_lookupSubsystemDoesNotNeedISessioncCs^|j�dt�d�d�|�|jjj|jdg�|j�d�|�|jjj|jdd�dS)	aD
        After having looked up a subsystem, data should be passed along to the
        client.  Additionally, subsystems were passed the entire request packet
        as data, instead of just the additional data.

        We check for the additional tidle to verify that the data passed
        through the client.
        r r+r�s
TestSubsystemdata~s	more data���s
more data~N)rr�rr�r�r�r%rqrMrrr�test_lookupSubsystem_dataIs
�� z/SessionInterfaceTests.test_lookupSubsystem_datacCs<|j�dt�d�d�|j��|�|jjj|j�dS)zd
        SSHSession.closeReceived() should sent a close message to the remote
        side.
        r r+r�N)rr�rr��
closeReceivedr�r�rzrMrrr�"test_lookupSubsystem_closeReceivedas
�
z8SessionInterfaceTests.test_lookupSubsystem_closeReceivedc	CsB|�t�}|�t|�ddd�dd�|D���|d�t�dS)zo
        Assert that the request we just made raised a RuntimeError (and only a
        RuntimeError).
        rjz!Multiple RuntimeErrors raised: %s�
cSsg|]}t|��qSr)�repr)�.0rrrr�
<listcomp>wszISessionInterfaceTests.assertRequestRaisedRuntimeError.<locals>.<listcomp>rN)�flushLoggedErrorsr6r��len�join�trap)r#�errorsrrr�assertRequestRaisedRuntimeErrorms
��z5SessionInterfaceTests.assertRequestRaisedRuntimeErrorcCsh|j�dd�}|�|�|��|�|jjtj�|�|jjj|jj�|�	|j�dd��|�
�dS)z�
        When a client requests a shell, the SSHSession object should get
        the shell by getting an ISession adapter for the avatar, then
        calling openShell() with a ProcessProtocol to attach.
        �shellraN)rr�r�r�r�r��SSHSessionProcessProtocolr�r0r�r�r�rrr�test_requestShell{s
z'SessionInterfaceTests.test_requestShellcCs�|j�dd�}|�|�|��|j�d�|�|jjjjd�|�|jjj|jddg�|�|jjjj	�|�|jjj
|jdg�dS)z�
        When a client executes a shell, it should be able to give pass data
        back and forth between the local and the remote side.
        r�ras
some datare��exit-statussFN)rr�r�r�rqr�r?r%r�rQrxr�rrr�test_requestShellWithData�s
��z/SessionInterfaceTests.test_requestShellWithDatacCs�|j�dt�d��}|�|�|��|�|jj�|�|j�dt�d���|�	�|�
|jjtj�|�|jjj
|jj�|�|jjjd�dS)z�
        When a client requests a command, the SSHSession object should get
        the command by getting an ISession adapter for the avatar, then
        calling execCommand with a ProcessProtocol to attach and the
        command line.
        �execsfailurerBN)rr�rr�r�r�r�r�r�r�r�r�r�rFr�rEr�rrr�test_requestExec�s
z&SessionInterfaceTests.test_requestExeccCs�|j�dt�d��}|�|�|��|j�d�|�|jjjj	d�|�|jj
j	|jgd��|j��|j��|j�
�|�|jjjj
�|�|jj
j|jdg�dS)zo
        When a client executes a command, it should be able to give pass data
        back and forth.
        r�srepeat hello�	some data)shellor�rer�N)rr�rr�r�r�rqr�rGr%r�rNr�rQrxr�rrr�test_requestExecWithData�s 
�


�z.SessionInterfaceTests.test_requestExecWithDatac
Cs�|��|jdd�t�tttj�|��}|�	dt�
ddd��}|�|�|�|jt�|�
�|�|�	dt�
ddd���|�|jjddgf�dS)	a
        When a client requests a PTY, the SSHSession object should make
        the request by getting an ISession adapter for the avatar, then
        calling getPty with the terminal type, the window size, and any modes
        the client gave us.
        F�r��pty_reqr4�rj���rasgoodN)�
doCleanupsr�rr�r.r*rrr�r��packRequest_pty_reqr�r�r�r�r�r5)r#�test_sessionr�rrr�test_requestPty�s$��
��z%SessionInterfaceTests.test_requestPtyc	Cs�|�|j�dt�d�t�d���|�|jjt�|��|�|j�dt�d�t�d���|�|�	�g�|�
|j�dt�d�t�d���|�|jjjddi�dS)	a
        When a client requests passing an environment variable, the
        SSHSession object should make the request by getting an
        ISessionSetEnv adapter for the avatar, then calling setEnv with the
        environment variable name and value.
        �envrYr4rZr��NAME�valueN)r�rr�rr�r�rRr�r�r�r�rUrMrrr�test_setEnv�s"�����z!SessionInterfaceTests.test_setEnvc	Cst|��}|�|�dt�d�t�d���|�|�dt�d�t�d���|�|jt�|�ddd�|jj	�dS)zG
        Multiple setenv requests will share the same session.
        r��Key1�Value 1�Key2�Value2�r�r�N)
r�r�r�rr�r�rrRr�rU�r#r�rrr�test_setEnvSessionShares�����z-SessionInterfaceTests.test_setEnvSessionSharecCs~|��}|�dt�d�t�d��|�dt�d�t�d��|�dt�ddd	��|�|jt�|�ddd
�|jj	�dS)z�
        Calling another session service after setenv will provide the
        previous session with the environment variables.
        r�r�r�r�r�r��termr;rar�N)
r�r�rr�rr�r�rRr�rVr�rrr�test_setEnvMultiplexShares��z/SessionInterfaceTests.test_setEnvMultiplexSharec	CsL|��|jdd�t�tttj�|�|j�	dt
�d�t
�d���dS)z�
        If the avatar does not have an ISessionSetEnv adapter, then a
        request to pass an environment variable fails gracefully.
        Fr�r�r�r�N)r�r�rr�r.r*rrr�r�rr�rMrrr�%test_setEnvNotProvidingISessionSetEnv)s���z;SessionInterfaceTests.test_setEnvNotProvidingISessionSetEnvcCs^|j�dt�d��}|�|�|��|��|�|j�dt�d���|�|jjjd�dS)z�
        When the client requests to change the window size, the SSHSession
        object should make the request by getting an ISession adapter for the
        avatar, then calling windowChanged with the new window size.
        �
window_changer;r�N)	rr��packRequest_window_changer�r�r�r�r�r<r�rrr�test_requestWindowChange;s
�

��z.SessionInterfaceTests.test_requestWindowChangecC�0t�|jj�|j_|j��|�|jjj�dS)z
        When an EOF is received and an ISession adapter is present, it should
        be notified of the EOF message.
        N)rrr/rNr�rLrMrrr�test_eofReceivedN�
z&SessionInterfaceTests.test_eofReceivedcCs.|j��}|�|�|�|jjj|j�dS)zT
        When a close is received, the session should send a close message.
        N)rr�r�r�r�rzr�rrr�test_closeReceivedWs

z(SessionInterfaceTests.test_closeReceivedcCr)z�
        When a close is received and an ISession adapter is present, it should
        be notified of the close message.
        N)rrr/rQr�rPrMrrr�test_closed_rz!SessionInterfaceTests.test_closedN)T)!rrrr(r�skipr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrrrrrr��s>



	r�c@sHeZdZdZes
dZdd�Zdd�Zdd�Zd	d
�Z	dd�Z
d
d�ZdS)�SessionWithNoAvatarTestsa	
    Test for the SSHSession interface.  Several of the methods (request_shell,
    request_exec, request_pty_req, request_env, request_window_change) would
    create a 'session' instance variable from the avatar if one didn't exist
    when they were called.
    r�cCs@t�|�t�tttj�t��|_t�|j_	|�
|jj�dSrS)rr�rr�r.r*rrr�r/r�rMrrrr�ts
�

zSessionWithNoAvatarTests.setUpcCs$|�tj�|jj�d|jj�dS)zB
        self.session.session should provide I{ISession}.
        zISession not provided by %rN)r�rr�
providedByrMrrr�assertSessionProvidesISession}s
�z6SessionWithNoAvatarTests.assertSessionProvidesISessioncCs|j�dd�|��dS)ze
        If an ISession adapter isn't already present, request_shell should get
        one.
        r�raN)rr�rrMrrr�test_requestShellGetsSession�sz5SessionWithNoAvatarTests.test_requestShellGetsSessioncCs |j�dt�d��|��dS)zd
        If an ISession adapter isn't already present, request_exec should get
        one.
        r�rBN)rr�rr�rrMrrr�test_requestExecGetsSession�sz4SessionWithNoAvatarTests.test_requestExecGetsSessioncCs$|j�dt�ddd��|��dS)zg
        If an ISession adapter isn't already present, request_pty_req should
        get one.
        r�r�r;raN)rr�r�rrMrrr�test_requestPtyReqGetsSession�s�z6SessionWithNoAvatarTests.test_requestPtyReqGetsSessioncCs |j�dt�d��|��dS)zm
        If an ISession adapter isn't already present, request_window_change
        should get one.
        r)rjrjrjrjN)rr�rrrMrrr�#test_requestWindowChangeGetsSession�s
�z<SessionWithNoAvatarTests.test_requestWindowChangeGetsSessionN)rrrr(rrr�rrr
rrrrrrr	is		
r	c@s(eZdZdZes
dZdd�Zdd�ZdS)�
WrappersTestszH
    A test for the wrapProtocol and wrapProcessProtocol functions.
    r�cCszt�}t�|_|��t�|�}|�d�|�|jjd�|�	d�|�
ddg�|��|�|jd�|j
�tj�dS)z�
        L{wrapProtocol}, when passed a L{Protocol} should return something that
        has write(), writeSequence(), loseConnection() methods which call the
        Protocol's dataReceived() and connectionLost() methods, respectively.
        sdataReceivedr�r�r�sdata12N)r!r�rtrrr�wrapProtocolrqr�r�ri�
writeSequencergr%rpr�r�ConnectionDone)r#r�wrappedrrr�test_wrapProtocol�s


zWrappersTests.test_wrapProtocolc	Csft�}t�|_t�|�}|��|�d�|�|jjd�|�	t
�t�
ddd���|j�tj
�dS)a.
        L{wrapPRocessProtocol}, when passed a L{Protocol} should return
        something that follows the L{IProcessProtocol} interface, with
        connectionMade() mapping to connectionMade(), outReceived() mapping to
        dataReceived() and processEnded() mapping to connectionLost().
        r�sdata~rN)r!r�rtr�wrapProcessProtocolrrrHr�r�rnrr
rr
rpr�)r#r�process_protocolrrr�!test_wrapProcessProtocol_Protocol�s

�z/WrappersTests.test_wrapProcessProtocol_ProtocolN)rrrr(rrrrrrrrr�src@s@eZdZdZes
dZdd�Zdd�Zdd�Zd	d
�Z	dd�Z
d
S)�HelpersTestszM
    Tests for the 4 helper functions: parseRequest_* and packRequest_*.
    r�cCsJ|�t�t�d�t�ddddd�t�t�ddd	���dd
dgf�dS)
a1
        The payload of a pty-req message is::
            string  terminal
            uint32  columns
            uint32  rows
            uint32  x pixels
            uint32  y pixels
            string  modes

        Modes are::
            byte    mode number
            uint32  mode value
        �xterm�>4Lrjr�r�r��>BL�rC�r�rjr�r�)rrCN)r�r�parseRequest_pty_reqrr��struct�packrMrrr�test_parseRequest_pty_req�s���
�z&HelpersTests.test_parseRequest_pty_reqcC�Jt�ddd�}|�|t�d�t�ddddd�t�t�d	d
d���dS�
zG
        See test_parseRequest_pty_req for the payload format.
        rrsrrjr�r�r�rrrCN�rr�r�rr�r r!�r#�packedrrr�test_packRequest_pty_req_old�s����z)HelpersTests.test_packRequest_pty_req_oldcCr#r$r%r&rrr�test_packRequest_pty_reqs����z%HelpersTests.test_packRequest_pty_reqcCs$|�t�t�ddddd��d�dS)a
        The payload of a window_change request is::
            uint32  columns
            uint32  rows
            uint32  x pixels
            uint32  y pixels

        parseRequest_window_change() returns (rows, columns, x pixels,
        y pixels).
        rrjr�r�r�rN)r�r�parseRequest_window_changer r!rMrrr�test_parseRequest_window_changes�z,HelpersTests.test_parseRequest_window_changec
Cs$|�t�d�t�ddddd��dS)zM
        See test_parseRequest_window_change for the payload format.
        rrrjr�r�r�N)r�rrr r!rMrrr�test_packRequest_window_change$s�z+HelpersTests.test_packRequest_window_changeN)rrrr(rrr"r(r)r+r,rrrrr�src@seZdZdZes
dZdd�Zdd�Zdd�Zd	d
�Z	dd�Z
d
d�Zdd�Ze
eed�d�dd��Ze
eed�d�dd��Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Ze
eed)�d*�d+d,��Ze
eed)�d*�d-d.��Zd/S)0�SSHSessionProcessProtocolTestsz1
    Tests for L{SSHSessionProcessProtocol}.
    r�cCs@t�|_tjt|j�ddd�|_t�|j�|_|j�|j�dS)Nr�r�)r�r�r�)r�rtrr�rvr�r@rcrMrrrr�6s�z$SSHSessionProcessProtocolTests.setUpcCs|�|jjj|j�dS)z8
        Assert that C{self.session} is closed.
        N)r�rr�rzrMrrr�assertSessionClosed>sz2SSHSessionProcessProtocolTests.assertSessionClosedcCs|�|jjj|j|�dS)zO
        Assert that C{self.session} has sent the C{expectedRequests}.
        N)r�rr�rx)r#�expectedRequestsrrr�assertRequestsEqualDr�z2SSHSessionProcessProtocolTests.assertRequestsEqualcCs|�|jj|j�dS)zy
        SSHSessionProcessProtocol should set self.session to the session passed
        to the __init__ method.
        N)r�r@rrMrrrr�Jsz(SSHSessionProcessProtocolTests.test_initcC� |�|jjj��|j���dS)zu
        SSHSessionProcessProtocol.getHost() just delegates to its
        session.conn.transport.getHost().
        N)r�rr�rtr�r@rMrrr�test_getHostQ� z+SSHSessionProcessProtocolTests.test_getHostcCr1)zu
        SSHSessionProcessProtocol.getPeer() just delegates to its
        session.conn.transport.getPeer().
        N)r�rr�rtr�r@rMrrr�test_getPeerXr3z+SSHSessionProcessProtocolTests.test_getPeercCs&d|j_|j��|�|jjd�dS)z�
        SSHSessionProcessProtocol.connectionMade() should check if there's a
        'buf' attribute on its session and write it to the transport if so.
        sbufferN)rr�r@rrr�rtrMrrr�test_connectionMade_s
z2SSHSessionProcessProtocolTests.test_connectionMade�SIGALRMzNot all signals availablec	CsFtjD]}d|}tt|�}|j�|�}|�||d|||f�qdS)zi
        _getSignalName should return the name of a signal when given the
        signal number.
        �SIGz%i: %s != %sN)r�SUPPORTED_SIGNALS�getattr�signalr@�_getSignalNamer�)r#�
signalName�signalValue�sshNamerrr�test_getSignalNamehs

��z1SSHSessionProcessProtocolTests.test_getSignalNamecCs4tjdt_d|j_|�|j�tj�dtj�dS)z�
        If there are signals in the signal module which aren't in the SSH RFC,
        we map their name to [signal name]@[platform].
        rjNzSIGTwistedTest@)	r:�NSIG�SIGTwistedTestr@�_signalValuesToNamesr�r;�sys�platformrMrrr�!test_getSignalNameWithLocalSignalvs�z@SSHSessionProcessProtocolTests.test_getSignalNameWithLocalSignalcC�*|j�d�|�|jjj|jdg�dS)zy
        When data is passed to the outReceived method, it should be sent to
        the session's write method.
        �	test dataN)r@rHr�rr�r%rMrrr�test_outReceived��z/SSHSessionProcessProtocolTests.test_outReceivedcCrF)z{
        When data is passed to the write method, it should be sent to the
        session channel's write method.
        rGN)r@rir�rr�r%rMrrr�
test_write�rIz)SSHSessionProcessProtocolTests.test_writecCs.|j�ddg�|�|jjj|jdg�dS)z�
        When a sequence is passed to the writeSequence method, it should be
        joined together and sent to the session channel's write method.
        stest r�rGN)r@rr�rr�r%rMrrr�test_writeSequence�sz1SSHSessionProcessProtocolTests.test_writeSequencecCs*|j�d�|�|jjj|jdg�dS)z�
        When data is passed to the errReceived method, it should be sent to
        the session's writeExtended method.
        rG)rjrGN)r@�errReceivedr�rr�rwrMrrr�test_errReceived�rIz/SSHSessionProcessProtocolTests.test_errReceivedcC�D|j��|�|j|jjjv�|j��|�|jjj|j�dS)zv
        When outConnectionLost and errConnectionLost are both called, we should
        send an EOF message.
        N)r@rlr�rr�ryrmr�rMrrr�test_outConnectionLost��

z5SSHSessionProcessProtocolTests.test_outConnectionLostcCrN)zh
        Make sure reverse ordering of events in test_outConnectionLost also
        sends EOF.
        N)r@rmr�rr�ryrlr�rMrrr�test_errConnectionLost�rPz5SSHSessionProcessProtocolTests.test_errConnectionLostcCs$|j��|�|jjj|j�dS)zp
        When loseConnection() is called, it should call loseConnection
        on the session channel.
        N)r@rgr�rr�rzrMrrr�test_loseConnection�s
z2SSHSessionProcessProtocolTests.test_loseConnectioncCs|j�t�td���dS)zr
        When connectionLost() is called, it should call loseConnection()
        on the session channel.
        rN)r@rurr
r	rMrrr�test_connectionLost�sz2SSHSessionProcessProtocolTests.test_connectionLostcCs:|j�ttd���|�dt�dd�dfg�|��dS)z�
        When processEnded is called, if there is an exit code in the reason
        it should be sent in an exit-status method.  The connection should be
        closed.
        Nr�z>IrF)r@rnr
r	r0r r!r.rMrrr�test_processEndedWithExitCode�sz<SSHSessionProcessProtocolTests.test_processEndedWithExitCode�	WCOREDUMPzcan't run this w/o os.WCOREDUMPcC�V|j�ttdtjd���|�dt�d�dt�d�t�d�dfg�|�	�dS)	z�
        When processEnded is called, if there is an exit signal in the reason
        it should be sent in an exit-signal message.  The connection should be
        closed.
        rj��exit-signal�TERM�raFN�
r@rnr
r
r:�SIGTERMr0rr�r.rMrrr�'test_processEndedWithExitSignalCoreDump�s"�������zFSSHSessionProcessProtocolTests.test_processEndedWithExitSignalCoreDumpcCrV)	z�
        When processEnded is called, if there is an exit signal in the
        reason it should be sent in an exit-signal message.  If no
        core was dumped, don't set the core-dump bit.
        rjrrXrYrfraFNr[rMrrr�)test_processEndedWithExitSignalNoCoreDump�s ���	zHSSHSessionProcessProtocolTests.test_processEndedWithExitSignalNoCoreDumpN)rrrr(rrr�r.r0r�r2r4r5r�hasattrr:r?rErHrJrKrMrOrQrRrSrT�osr]r^rrrrr-.s8	






r-c@s eZdZdZes
dZdd�ZdS)�SSHSessionClientTestsza
    SSHSessionClient is an obsolete class used to connect standard IO to
    an SSHSession.
    r�cCs.t��}t�|_|�d�|�|jjd�dS)zL
        When data is received, it should be sent to the transport.
        rGN)r�SSHSessionClientr�rtrqr�r�r�rrr�test_dataReceiveds
z'SSHSessionClientTests.test_dataReceivedN)rrrr(rrrcrrrrra�s
ra)5r(r`r:r rC�unittestr�zope.interfacer�twisted.internetrrr�twisted.internet.addressr�twisted.internet.errorr	r
�twisted.pythonrr�twisted.python.failurer
�twisted.python.reflectr�#twisted.python.test.test_componentsr�twisted.trial.unittestrr�twisted.conch.sshrrrrr*rr.rrRr>�Protocolr!rvr�r�r�r�r	rrr-rarrrr�<module>sR]0/*C-*B-VR

Anon7 - 2022
AnonSec Team