U
    
W[(š  ã                   @   sð  d Z ddlmZmZ ddlZddlZddlZddlZddlm	Z	 ddl
mZ edƒZernddlmZmZmZ nG dd	„ d	ƒZdd
lmZ ddlmZmZ ddlmZ ddlmZmZmZ ddlmZmZ ddlm Z  G dd„ de!ƒZ"G dd„ dƒZ#e	ej$ƒG dd„ de!ƒƒZ%er"e &e%e#ej$¡ G dd„ dƒZ'G dd„ dej(ƒZ)G dd„ de!ƒZ*G dd„ dƒZ+G dd„ de+ƒZ,G d d!„ d!e!ƒZ-G d"d#„ d#e j.ƒZ/G d$d%„ d%e j.ƒZ0G d&d'„ d'e j.ƒZ1G d(d)„ d)e j.ƒZ2G d*d+„ d+e j.ƒZ3G d,d-„ d-e j.ƒZ4dS ).zb
Tests for the 'session' channel implementation in twisted.conch.ssh.session.

See also RFC 4254.
é    )ÚdivisionÚabsolute_importN)Úimplementer)ÚrequireModuleÚcryptography)ÚcommonÚsessionÚ
connectionc                   @   s   e Zd ZddlmZ dS )r   r   )ÚISessionN)Ú__name__Ú
__module__Ú__qualname__Ztwisted.conch.interfacesr
   © r   r   úA/usr/lib/python3/dist-packages/twisted/conch/test/test_session.pyr      s   r   ©ÚIPv4Address)ÚProcessTerminatedÚProcessDone)ÚFailure)ÚdeferÚprotocolÚerror)Ú
componentsÚfailure)Úunittestc                   @   s   e Zd ZdZdd„ ZdS )ÚSubsystemOnlyAvatarz^
    A stub class representing an avatar that is only useful for
    getting a subsystem.
    c                 C   s   |dkst ‚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)ÚAssertionErrorÚMockProtocol©ÚselfÚnameÚdatar   r   r   ÚlookupSubsystem)   s    z#SubsystemOnlyAvatar.lookupSubsystemN©r   r   r   Ú__doc__r#   r   r   r   r   r   "   s   r   c                   @   s   e Zd ZdZdd„ ZdS )Ú
StubAvatarz€
    A stub class representing the avatar representing the authenticated user.
    It implements the I{ISession} interface.
    c                 C   s"   |dkrt ƒ | _|| j_| jS dS )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   r   r   r   r#   :   s    zStubAvatar.lookupSubsystemNr$   r   r   r   r   r&   3   s   r&   c                   @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )Ú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.
    c                 C   s   || _ d| _dS )z2
        Store the avatar we're adapting.
        N)ÚavatarÚshellProtocol)r    r+   r   r   r   Ú__init__`   s    z!StubSessionForStubAvatar.__init__c                 C   s"   |dkr|||f| _ ntdƒ‚dS )zw
        If the terminal is 'bad', fail.  Otherwise, store the information in
        the ptyRequest variable.
        ó   badznot getting a ptyN)Ú
ptyRequestÚRuntimeError)r    ZterminalÚwindowZmodesr   r   r   ÚgetPtyh   s    zStubSessionForStubAvatar.getPtyc                 C   s   |dkrt dƒ‚n|| _dS )zw
        If all the window sizes are 0, fail.  Otherwise, store the size in the
        windowChange variable.
        ©r   r   r   r   znot changing the window sizeN)r0   ÚwindowChange)r    r1   r   r   r   ÚwindowChangeds   s    
z&StubSessionForStubAvatar.windowChangedc                 C   s(   | j dk	rtdƒ‚n|| _ t|ƒ| _dS )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.
        Nznot getting a shell this time)r,   r0   ÚEchoTransportÚshellTransport)r    Úppr   r   r   Ú	openShell~   s    

z"StubSessionForStubAvatar.openShellc                 C   sV   || _ |dkr|| _n<|dd… dkrJ|| _t|ƒ| _| |dd… ¡ ntdƒ‚dS )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é   s   repeaté   znot getting a command)ÚexecCommandLineÚexecProtocolr6   ÚexecTransportÚoutReceivedr0   )r    r8   Zcommandr   r   r   ÚexecCommand‹   s    
z$StubSessionForStubAvatar.execCommandc                 C   s
   d| _ dS )z2
        Note that EOF has been received.
        TN)ÚgotEOF©r    r   r   r   ÚeofReceivedœ   s    z$StubSessionForStubAvatar.eofReceivedc                 C   s
   d| _ dS )z4
        Note that close has been received.
        TN)Ú	gotClosedrC   r   r   r   Úclosed£   s    zStubSessionForStubAvatar.closedN)r   r   r   r%   r-   r2   r5   r9   rA   rD   rF   r   r   r   r   r*   G   s   r*   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	r6   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.
    c                 C   s    || _ d| _d| _| | ¡ dS )z
        Initialize our instance variables.

        @param processProtocol: a C{ProcessProtocol} to connect to ourself.
        Fó    N)ÚprotorF   r"   ÚmakeConnection)r    ZprocessProtocolr   r   r   r-   ¼   s    zEchoTransport.__init__c                 C   s:   |  j |7  _ | j |¡ | j d¡ d|kr6|  ¡  dS )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"   rH   r@   ÚloseConnection©r    r"   r   r   r   ÚwriteÈ   s
    zEchoTransport.writec              	   C   sN   | j r
dS d| _ | j ¡  | j ¡  | j ¡  | j t t 	ddd¡¡¡ dS )z„
        If we're asked to disconnect (and we haven't already) shut down
        the C{ProcessProtocol} with a 0 exit code.
        Né   r   )
rF   rH   ZinConnectionLostÚoutConnectionLostÚerrConnectionLostÚprocessEndedr   r   r   r   rC   r   r   r   rL   Ô   s    



ÿzEchoTransport.loseConnectionN)r   r   r   r%   r-   rN   rL   r   r   r   r   r6   °   s   r6   c                   @   s,   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	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.
    rG   c                 C   s(   d| _ d| _d| _| jr$|  | j¡ dS )zc
        Set up the instance variables.  If we have any packetData, send it
        along.
        rG   TN)r"   ÚopenÚreasonr)   ÚdataReceivedrC   r   r   r   ÚconnectionMadeñ   s
    zMockProtocol.connectionMadec                 C   s"   |  j |7  _ | j |d ¡ dS )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"   Ú	transportrN   rM   r   r   r   rU   þ   s    zMockProtocol.dataReceivedc                 C   s   d| _ || _dS )z:
        Close the protocol and store the reason.
        FN)rS   rT   )r    rT   r   r   r   ÚconnectionLost  s    zMockProtocol.connectionLostN)r   r   r   r%   r)   rV   rU   rY   r   r   r   r   r   ä   s
   	
r   c                   @   sL   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zddd„Zdd„ Z	dd„ Z
dS )Ú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.
    Nc                 C   s(   i | _ i | _i | _i | _i | _|| _dS )z4
        Initialize our instance variables.
        N)r"   ÚextDataÚrequestsÚeofsÚclosesrX   )r    rX   r   r   r   r-   #  s    zStubConnection.__init__c                 C   s   dS )z,
        Return our logging prefix.
        ZMockConnectionr   rC   r   r   r   Ú	logPrefix/  s    zStubConnection.logPrefixc                 C   s   | j  |g ¡ |¡ dS )z'
        Record the sent data.
        N)r"   Ú
setdefaultÚappend)r    Úchannelr"   r   r   r   ÚsendData6  s    zStubConnection.sendDatac                 C   s   | j  |g ¡ ||f¡ dS )z0
        Record the sent extended data.
        N)r[   r`   ra   )r    rb   Útyper"   r   r   r   ÚsendExtendedData=  s    zStubConnection.sendExtendedDataFc                 C   s,   | j  |g ¡ |||f¡ |r(t d¡S dS )z2
        Record the sent channel request.
        N)r\   r`   ra   r   Zsucceed)r    rb   Zrequestr"   Z	wantReplyr   r   r   ÚsendRequestD  s
    ÿzStubConnection.sendRequestc                 C   s   d| j |< dS )z&
        Record the sent EOF.
        TN)r]   ©r    rb   r   r   r   ÚsendEOFN  s    zStubConnection.sendEOFc                 C   s   d| j |< dS )z(
        Record the sent close.
        TN)r^   rg   r   r   r   Ú	sendCloseU  s    zStubConnection.sendClose)N)F)r   r   r   r%   r-   r_   rc   re   rf   rh   ri   r   r   r   r   rZ     s   


rZ   c                   @   s@   e Zd ZdZdZdZdd„ Zdd„ Zdd	„ Zd
d„ Z	dd„ Z
dS )Ú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}
    rG   Fc                 C   s   t dddƒS )ú2
        Return an arbitrary L{IAddress}.
        ÚTCPZ
remotehosti¸"  r   rC   r   r   r   ÚgetPeerl  s    zStubTransport.getPeerc                 C   s   t dddƒS )rk   rl   Z	localhosti'  r   rC   r   r   r   ÚgetHosts  s    zStubTransport.getHostc                 C   s   |  j |7  _ dS )z,
        Record data in the buffer.
        N)ÚbufrM   r   r   r   rN   z  s    zStubTransport.writec                 C   s
   d| _ dS )z6
        Note that the connection was closed.
        TN)ÚcloserC   r   r   r   rL     s    zStubTransport.loseConnectionc                 C   s   dS )z0
        Pretend to set C{TCP_NODELAY}.
        Nr   )r    Úenabledr   r   r   ÚsetTcpNoDelayˆ  s    zStubTransport.setTcpNoDelayN)r   r   r   r%   ro   rp   rm   rn   rN   rL   rr   r   r   r   r   rj   ]  s   
rj   c                   @   s   e Zd ZdZdZdd„ ZdS )Ú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}
    rG   c                 C   s   |  j |7  _ dS )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)ÚerrrM   r   r   r   ÚwriteErrš  s    z"StubTransportWithWriteErr.writeErrN)r   r   r   r%   rt   ru   r   r   r   r   rs     s   rs   c                   @   s   e Zd ZdZdd„ ZdS )Ú
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.
    c                 C   s   t ƒ | _d S ©N)rs   rX   rC   r   r   r   r-   ­  s    zStubClient.__init__N)r   r   r   r%   r-   r   r   r   r   rv   ¤  s   rv   c                   @   sÈ   e Zd ZdZesdZdd„ Zdd„ Zdd„ Zd	d
„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/S )0Ú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 cryptographyc                 C   s   t jddtƒ tƒ d| _ dS )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.
        éô  éd   )ÚremoteWindowÚremoteMaxPacketÚconnr+   N)r   Ú
SSHSessionrZ   r&   rC   r   r   r   ÚsetUp»  s
     þzSessionInterfaceTests.setUpc                 C   s   |   | jjt¡ dS )zj
        Asserts that self.session.session is an instance of
        StubSessionForStubOldAvatar.
        N)ÚassertIsInstancer   r*   rC   r   r   r   ÚassertSessionIsStubSessionÆ  s    
ÿz0SessionInterfaceTests.assertSessionIsStubSessionc                 C   s6   t jtd}|  |jd¡ |  |j¡ |  |j ¡ dS )z§
        SSHSession initializes its buffer (buf), client, and ISession adapter.
        The avatar should not need to be adaptable to an ISession immediately.
        )r+   rG   N)r   r   ÚobjectÚassertEqualro   ÚassertIsNoneÚclient)r    Úsr   r   r   Ú	test_initÏ  s    zSessionInterfaceTests.test_initc                 C   s:   | j  d¡ tƒ | j _| j  d¡ |  | j jjjd¡ dS )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)r   rU   rv   r†   r„   rX   ro   rC   r   r   r   Útest_client_dataReceivedÛ  s    
z.SessionInterfaceTests.test_client_dataReceivedc                 C   sP   | j  tjd¡ | j  dd¡ tƒ | j _| j  tjd¡ |  | j jjjd¡ dS )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_STDERRrv   r†   r„   rX   rt   rC   r   r   r   Útest_client_extReceivedå  s
    
z-SessionInterfaceTests.test_client_extReceivedc                 C   s*   t ƒ  }| j_tƒ |_| j tjd¡ dS )z‹
        SSHSession.extReceived() should handle the case where the transport
        on the client doesn't have a writeErr method.
        s   ignoredN)rv   r   r†   rj   rX   rŽ   r	   r   ©r    r†   r   r   r   Ú&test_client_extReceivedWithoutWriteErrò  s    z<SessionInterfaceTests.test_client_extReceivedWithoutWriteErrc                 C   s6   t ƒ | j_| j ¡  |  | jjjj¡ d| jjj_dS )z}
        SSHSession.closed() should tell the transport connected to the client
        that the connection was lost.
        FN)rv   r   r†   rF   Ú
assertTruerX   rp   rC   r   r   r   Útest_client_closedÿ  s    

z(SessionInterfaceTests.test_client_closedc                 C   s0   | j  dt d¡¡}|  |¡ |  | j j¡ dS )zV
        When a subsystem request fails, SSHSession.client should not be set.
        r   s   BadSubsystemN)r   ÚrequestReceivedr   ÚNSÚassertFalser…   r†   ©r    Úretr   r   r   Ú$test_badSubsystemDoesNotCreateClient
  s     ÿ
z:SessionInterfaceTests.test_badSubsystemDoesNotCreateClientc                 C   sR   | j  dt d¡d ¡}|  |¡ |  | j jtj¡ |  	| j jj
j| j jj¡ dS )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)r   r•   r   r–   r“   r   r†   r   ZProcessProtocolÚassertIsrX   rH   r+   r(   r˜   r   r   r   Útest_lookupSubsystem  s     ÿ
ÿz*SessionInterfaceTests.test_lookupSubsystemc                 C   s€   t jtƒ tƒ d}| t d¡d ¡}|  |¡ |  |j	¡ |  
|jj |¡¡ | ¡  |  |jj |¡¡ | ¡  | ¡  dS )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)r   r   r   rZ   Zrequest_subsystemr   r–   r“   ZassertIsNotNoner†   r…   r~   r^   ÚgetrD   rL   rF   )r    r‡   r™   r   r   r   Ú'test_lookupSubsystemDoesNotNeedISession#  s    ÿÿ
z=SessionInterfaceTests.test_lookupSubsystemDoesNotNeedISessionc                 C   s^   | j  dt d¡d ¡ |  | j jj| j  dg¡ | j  d¡ |  | j jj| j  d d¡ dS )	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)r   r•   r   r–   r„   r~   r"   rU   rC   r   r   r   Útest_lookupSubsystem_data7  s    ÿÿÿz/SessionInterfaceTests.test_lookupSubsystem_datac                 C   s<   | j  dt d¡d ¡ | j  ¡  |  | j jj| j  ¡ dS )zd
        SSHSession.closeReceived() should sent a close message to the remote
        side.
        r   r'   r›   N)r   r•   r   r–   ÚcloseReceivedr“   r~   r^   rC   r   r   r   Ú"test_lookupSubsystem_closeReceivedN  s
    ÿ
z8SessionInterfaceTests.test_lookupSubsystem_closeReceivedc              	   C   sB   |   t¡}|  t|ƒddd dd„ |D ƒ¡ ¡ |d  t¡ dS )zo
        Assert that the request we just made raised a RuntimeError (and only a
        RuntimeError).
        rO   z!Multiple RuntimeErrors raised: %sÚ
c                 S   s   g | ]}t |ƒ‘qS r   )Úrepr)Ú.0r   r   r   r   Ú
<listcomp>a  s     zISessionInterfaceTests.assertRequestRaisedRuntimeError.<locals>.<listcomp>r   N)ZflushLoggedErrorsr0   r„   ÚlenÚjoinÚtrap)r    Úerrorsr   r   r   ÚassertRequestRaisedRuntimeErrorZ  s
    
ÿz5SessionInterfaceTests.assertRequestRaisedRuntimeErrorc                 C   sh   | j  dd¡}|  |¡ |  ¡  |  | j jt j¡ |  | j j j| j j¡ |  	| j  dd¡¡ |  
¡  dS )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.
        ó   shellrG   N)r   r•   r“   r‚   r   r†   ÚSSHSessionProcessProtocolrœ   r,   r—   r¬   r˜   r   r   r   Útest_requestShelle  s    

ÿz'SessionInterfaceTests.test_requestShellc                 C   sŒ   | j  dd¡}|  |¡ |  ¡  | j  d¡ |  | j j jjd¡ |  | j jj| j  ddg¡ |  | j j jj	¡ |  | j jj
| j  dg¡ dS )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­   rG   s
   some data rJ   ©ó   exit-statuss       FN)r   r•   r“   r‚   rU   r„   r7   r"   r~   rF   r\   r˜   r   r   r   Útest_requestShellWithDataw  s    
ÿÿÿz/SessionInterfaceTests.test_requestShellWithDatac                 C   s”   | j  dt d¡¡}|  |¡ |  ¡  |  | j j¡ |  | j  dt d¡¡¡ |  	¡  |  
| j jt j¡ |  | j j j| j j¡ |  | j j jd¡ dS )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.
        ó   execs   failurer:   N)r   r•   r   r–   r—   r¬   r…   r†   r“   r‚   r   r®   rœ   r>   r„   r=   r˜   r   r   r   Útest_requestExec‰  s"    ÿ
ÿ
ÿÿz&SessionInterfaceTests.test_requestExecc                 C   s²   | j  dt d¡¡}|  |¡ |  ¡  | j  d¡ |  | j j jj	d¡ |  | j j
j	| j  dddg¡ | j  ¡  | j  ¡  | j  ¡  |  | j j jj¡ |  | j j
j| j  dg¡ dS )zo
        When a client executes a command, it should be able to give pass data
        back and forth.
        r³   s   repeat hellos	   some datas   hellorJ   r°   N)r   r•   r   r–   r“   r‚   rU   r„   r?   r"   r~   rD   r¢   rF   r\   r˜   r   r   r   Útest_requestExecWithData   s"    ÿ
ÿ


ÿz.SessionInterfaceTests.test_requestExecWithDatac              
   C   sl   | j  dt  ddd¡¡}|  |¡ |  ¡  |  ¡  |  | j  dt  ddd¡¡¡ |  | j j jddg f¡ dS )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.
        ó   pty_reqr.   ©rO   é   é   é   rG   s   goodN)	r   r•   ÚpackRequest_pty_reqr—   r‚   r¬   r“   r„   r/   r˜   r   r   r   Útest_requestPtyµ  s"       ÿÿ
ÿÿz%SessionInterfaceTests.test_requestPtyc                 C   s^   | j  dt  d¡¡}|  |¡ |  ¡  |  ¡  |  | j  dt  d¡¡¡ |  | j j jd¡ dS )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_changer3   r·   N)	r   r•   ÚpackRequest_window_changer—   r¬   r‚   r“   r„   r4   r˜   r   r   r   Útest_requestWindowChangeÊ  s    þ
ÿÿz.SessionInterfaceTests.test_requestWindowChangec                 C   s0   t  | j j¡| j _ | j  ¡  |  | j j j¡ dS )z
        When an EOF is received and an ISession adapter is present, it should
        be notified of the EOF message.
        N)r   r
   r+   rD   r“   rB   rC   r   r   r   Útest_eofReceivedÜ  s    
z&SessionInterfaceTests.test_eofReceivedc                 C   s.   | j  ¡ }|  |¡ |  | j jj| j  ¡ dS )zT
        When a close is received, the session should send a close message.
        N)r   r¢   r…   r“   r~   r^   r˜   r   r   r   Útest_closeReceivedæ  s    

z(SessionInterfaceTests.test_closeReceivedc                 C   s0   t  | j j¡| j _ | j  ¡  |  | j j j¡ dS )z‚
        When a close is received and an ISession adapter is present, it should
        be notified of the close message.
        N)r   r
   r+   rF   r“   rE   rC   r   r   r   Útest_closedï  s    
z!SessionInterfaceTests.test_closedN)r   r   r   r%   r   Úskipr€   r‚   rˆ   r‹   r   r’   r”   rš   r   rŸ   r¡   r£   r¬   r¯   r²   r´   rµ   r¼   r¿   rÀ   rÁ   rÂ   r   r   r   r   rx   ²  s2   	


	rx   c                   @   sH   e Zd ZdZesdZdd„ Zdd„ Zdd„ Zd	d
„ Z	dd„ Z
dd„ ZdS )ÚSessionWithNoAvatarTestszü
    Test for the SSHSession interface.  Several of the methods (request_shell,
    request_exec, request_pty_req, request_window_change) would create a
    'session' instance variable from the avatar if one didn't exist when they
    were called.
    ry   c                 C   s&   t  ¡ | _ tƒ | j _|  | j j ¡ d S rw   )r   r   r&   r+   r…   rC   r   r   r   r€     s    

zSessionWithNoAvatarTests.setUpc                 C   s$   |   tj | jj¡d| jj ¡ dS )zB
        self.session.session should provide I{ISession}.
        zISession not provided by %rN)r“   r   r
   Z
providedByrC   r   r   r   ÚassertSessionProvidesISession  s    
ÿz6SessionWithNoAvatarTests.assertSessionProvidesISessionc                 C   s   | j  dd¡ |  ¡  dS )ze
        If an ISession adapter isn't already present, request_shell should get
        one.
        r­   rG   N)r   r•   rÅ   rC   r   r   r   Útest_requestShellGetsSession  s    z5SessionWithNoAvatarTests.test_requestShellGetsSessionc                 C   s    | j  dt d¡¡ |  ¡  dS )zd
        If an ISession adapter isn't already present, request_exec should get
        one.
        r³   r:   N)r   r•   r   r–   rÅ   rC   r   r   r   Útest_requestExecGetsSession  s    ÿz4SessionWithNoAvatarTests.test_requestExecGetsSessionc                 C   s$   | j  dt  ddd¡¡ |  ¡  dS )zg
        If an ISession adapter isn't already present, request_pty_req should
        get one.
        r¶   s   termr3   rG   N)r   r•   r»   rÅ   rC   r   r   r   Útest_requestPtyReqGetsSession&  s      ÿÿz6SessionWithNoAvatarTests.test_requestPtyReqGetsSessionc                 C   s    | j  dt  d¡¡ |  ¡  dS )zm
        If an ISession adapter isn't already present, request_window_change
        should get one.
        r½   )rO   rO   rO   rO   N)r   r•   r¾   rÅ   rC   r   r   r   Ú#test_requestWindowChangeGetsSession1  s    ÿþz<SessionWithNoAvatarTests.test_requestWindowChangeGetsSessionN)r   r   r   r%   r   rÃ   r€   rÅ   rÆ   rÇ   rÈ   rÉ   r   r   r   r   rÄ   ú  s   	
rÄ   c                   @   s(   e Zd ZdZesdZdd„ Zdd„ ZdS )ÚWrappersTestszH
    A test for the wrapProtocol and wrapProcessProtocol functions.
    ry   c                 C   sz   t ƒ }tƒ |_| ¡  t |¡}| d¡ |  |jjd¡ | 	d¡ | 
ddg¡ | ¡  |  |jd¡ |j tj¡ dS )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Š   s   data12N)r   rj   rX   rV   r   ZwrapProtocolrU   r„   ro   rN   ÚwriteSequencerL   r"   rT   rª   r   ZConnectionDone)r    r   Úwrappedr   r   r   Útest_wrapProtocolE  s    


zWrappersTests.test_wrapProtocolc              	   C   sf   t ƒ }tƒ |_t |¡}| ¡  | d¡ |  |jjd¡ | 	t
 t ddd¡¡¡ |j tj¡ dS )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›   s   data~r   N)r   rj   rX   r   ZwrapProcessProtocolrV   r@   r„   ro   rR   r   r   r   r   rT   rª   )r    r   Zprocess_protocolr   r   r   Ú!test_wrapProcessProtocol_ProtocolW  s    

ÿz/WrappersTests.test_wrapProcessProtocol_ProtocolN)r   r   r   r%   r   rÃ   rÍ   rÎ   r   r   r   r   rÊ   >  s
   rÊ   c                   @   s@   e Zd ZdZesdZdd„ Zdd„ Zdd„ Zd	d
„ Z	dd„ Z
dS )ÚHelpersTestszM
    Tests for the 4 helper functions: parseRequest_* and packRequest_*.
    ry   c                 C   sJ   |   t t d¡t ddddd¡ t t ddd	¡¡ ¡dd
dgf¡ 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ú>4LrO   r¸   r¹   rº   ú>BLé   r;   ©r¸   rO   r¹   rº   )rÓ   r;   N)r„   r   ZparseRequest_pty_reqr   r–   ÚstructÚpackrC   r   r   r   Útest_parseRequest_pty_reqr  s       ÿÿÿý
ûz&HelpersTests.test_parseRequest_pty_reqc                 C   sJ   t  ddd¡}|  |t d¡t ddddd¡ t t d	d
d¡¡ ¡ dS ©zG
        See test_parseRequest_pty_req for the payload format.
        rÐ   rÔ   s      rÑ   rO   r¸   r¹   rº   rÒ   rÓ   r;   N©r   r»   r„   r   r–   rÕ   rÖ   ©r    Zpackedr   r   r   Útest_packRequest_pty_req_oldˆ  s    ÿÿþÿz)HelpersTests.test_packRequest_pty_req_oldc                 C   sJ   t  ddd¡}|  |t d¡t ddddd¡ t t d	d
d¡¡ ¡ dS rØ   rÙ   rÚ   r   r   r   Útest_packRequest_pty_req•  s    ÿÿþÿz%HelpersTests.test_packRequest_pty_reqc                 C   s$   |   t t ddddd¡¡d¡ dS )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).
        rÑ   rO   r¸   r¹   rº   rÔ   N)r„   r   ZparseRequest_window_changerÕ   rÖ   rC   r   r   r   Útest_parseRequest_window_change¡  s
    ÿÿz,HelpersTests.test_parseRequest_window_changec              
   C   s$   |   t d¡t ddddd¡¡ dS )zM
        See test_parseRequest_window_change for the payload format.
        rÔ   rÑ   rO   r¸   r¹   rº   N)r„   r   r¾   rÕ   rÖ   rC   r   r   r   Útest_packRequest_window_change°  s    ÿz+HelpersTests.test_packRequest_window_changeN)r   r   r   r%   r   rÃ   r×   rÛ   rÜ   rÝ   rÞ   r   r   r   r   rÏ   j  s   rÏ   c                   @   sô   e Zd ZdZesdZdd„ Zdd„ Zdd„ Zd	d
„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zeeddƒdkrxd e_e_dd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zeed.dƒdkrðd/Zee_ee_dS )0ÚSSHSessionProcessProtocolTestsz1
    Tests for L{SSHSessionProcessProtocol}.
    ry   c                 C   s@   t ƒ | _tjt| jƒddd| _t | j¡| _| j | j¡ d S )Nrz   r{   )r~   r|   r}   )rj   rX   r   r   rZ   r®   r8   rI   rC   r   r   r   r€   À  s     þz$SSHSessionProcessProtocolTests.setUpc                 C   s   |   | jjj| j ¡ dS )z8
        Assert that C{self.session} is closed.
        N)r“   r   r~   r^   rC   r   r   r   ÚassertSessionClosedÉ  s    z2SSHSessionProcessProtocolTests.assertSessionClosedc                 C   s   |   | jjj| j |¡ dS )zO
        Assert that C{self.session} has sent the C{expectedRequests}.
        N)r„   r   r~   r\   )r    ZexpectedRequestsr   r   r   ÚassertRequestsEqualÐ  s    þz2SSHSessionProcessProtocolTests.assertRequestsEqualc                 C   s   |   | jj| j¡ dS )zy
        SSHSessionProcessProtocol should set self.session to the session passed
        to the __init__ method.
        N)r„   r8   r   rC   r   r   r   rˆ   Ù  s    z(SSHSessionProcessProtocolTests.test_initc                 C   s    |   | jjj ¡ | j ¡ ¡ dS )zu
        SSHSessionProcessProtocol.getHost() just delegates to its
        session.conn.transport.getHost().
        N)r„   r   r~   rX   rn   r8   rC   r   r   r   Útest_getHostá  s     ÿz+SSHSessionProcessProtocolTests.test_getHostc                 C   s    |   | jjj ¡ | j ¡ ¡ dS )zu
        SSHSessionProcessProtocol.getPeer() just delegates to its
        session.conn.transport.getPeer().
        N)r„   r   r~   rX   rm   r8   rC   r   r   r   Útest_getPeerê  s     ÿz+SSHSessionProcessProtocolTests.test_getPeerc                 C   s&   d| j _| j ¡  |  | jjd¡ dS )z¢
        SSHSessionProcessProtocol.connectionMade() should check if there's a
        'buf' attribute on its session and write it to the transport if so.
        s   bufferN)r   ro   r8   rV   r„   rX   rC   r   r   r   Útest_connectionMadeó  s    
z2SSHSessionProcessProtocolTests.test_connectionMadec              	   C   sF   t jD ]:}d| }tt|ƒ}| j |¡}|  ||d|||f ¡ qdS )zi
        _getSignalName should return the name of a signal when given the
        signal number.
        ZSIGz%i: %s != %sN)r   ZSUPPORTED_SIGNALSÚgetattrÚsignalr8   Ú_getSignalNamer„   )r    Z
signalNameZsignalValueZsshNamer   r   r   Útest_getSignalNameý  s    

ÿÿz1SSHSessionProcessProtocolTests.test_getSignalNamec                 C   s4   t jd t _d| j_|  | j t j¡dtj ¡ dS )z
        If there are signals in the signal module which aren't in the SSH RFC,
        we map their name to [signal name]@[platform].
        rO   NzSIGTwistedTest@)	ræ   ÚNSIGZSIGTwistedTestr8   Z_signalValuesToNamesr„   rç   ÚsysÚplatformrC   r   r   r   Ú!test_getSignalNameWithLocalSignal  s
    ÿz@SSHSessionProcessProtocolTests.test_getSignalNameWithLocalSignalÚSIGALRMNzNot all signals availablec                 C   s*   | j  d¡ |  | jjj| j dg¡ dS )zy
        When data is passed to the outReceived method, it should be sent to
        the session's write method.
        ó	   test dataN)r8   r@   r„   r   r~   r"   rC   r   r   r   Útest_outReceived  s    ÿz/SSHSessionProcessProtocolTests.test_outReceivedc                 C   s*   | j  d¡ |  | jjj| j dg¡ dS )z{
        When data is passed to the write method, it should be sent to the
        session channel's write method.
        rî   N)r8   rN   r„   r   r~   r"   rC   r   r   r   Ú
test_write&  s    ÿz)SSHSessionProcessProtocolTests.test_writec                 C   s.   | j  ddg¡ |  | jjj| j dg¡ dS )z
        When a sequence is passed to the writeSequence method, it should be
        joined together and sent to the session channel's write method.
        s   test r›   rî   N)r8   rË   r„   r   r~   r"   rC   r   r   r   Útest_writeSequence/  s    ÿz1SSHSessionProcessProtocolTests.test_writeSequencec                 C   s*   | j  d¡ |  | jjj| j dg¡ dS )z
        When data is passed to the errReceived method, it should be sent to
        the session's writeExtended method.
        rî   )rO   rî   N)r8   ZerrReceivedr„   r   r~   r[   rC   r   r   r   Útest_errReceived9  s    ÿz/SSHSessionProcessProtocolTests.test_errReceivedc                 C   sD   | j  ¡  |  | j| jjjk¡ | j  ¡  |  | jjj| j ¡ dS )zv
        When outConnectionLost and errConnectionLost are both called, we should
        send an EOF message.
        N)r8   rP   r—   r   r~   r]   rQ   r“   rC   r   r   r   Útest_outConnectionLostC  s    

z5SSHSessionProcessProtocolTests.test_outConnectionLostc                 C   sD   | j  ¡  |  | j| jjjk¡ | j  ¡  |  | jjj| j ¡ dS )zh
        Make sure reverse ordering of events in test_outConnectionLost also
        sends EOF.
        N)r8   rQ   r—   r   r~   r]   rP   r“   rC   r   r   r   Útest_errConnectionLostN  s    

z5SSHSessionProcessProtocolTests.test_errConnectionLostc                 C   s$   | j  ¡  |  | jjj| j ¡ dS )zp
        When loseConnection() is called, it should call loseConnection
        on the session channel.
        N)r8   rL   r“   r   r~   r^   rC   r   r   r   Útest_loseConnectionY  s    
z2SSHSessionProcessProtocolTests.test_loseConnectionc                 C   s   | j  t tdƒ¡¡ dS )zr
        When connectionLost() is called, it should call loseConnection()
        on the session channel.
        r   N)r8   rY   r   r   r   rC   r   r   r   Útest_connectionLostb  s    
ÿz2SSHSessionProcessProtocolTests.test_connectionLostc                 C   s:   | j  ttdƒƒ¡ |  dt dd¡dfg¡ |  ¡  dS )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>Ir   F)r8   rR   r   r   rá   rÕ   rÖ   rà   rC   r   r   r   Útest_processEndedWithExitCodek  s
    ÿz<SSHSessionProcessProtocolTests.test_processEndedWithExitCodec                 C   sV   | j  ttdtjdƒƒ¡ |  dt d¡d t d¡ t d¡ dfg¡ |  	¡  dS )	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.
        rO   é€   ó   exit-signaló   TERMó   rG   FN©
r8   rR   r   r   ræ   ÚSIGTERMrá   r   r–   rà   rC   r   r   r   Ú'test_processEndedWithExitSignalCoreDumpw  s&     ÿÿÿþýûÿzFSSHSessionProcessProtocolTests.test_processEndedWithExitSignalCoreDumpc                 C   sV   | j  ttdtjdƒƒ¡ |  dt d¡d t d¡ t d¡ dfg¡ |  	¡  dS )	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.
        rO   r   rù   rú   rK   rG   FNrü   rC   r   r   r   Ú)test_processEndedWithExitSignalNoCoreDumpŠ  s    ÿÿÿÿzHSSHSessionProcessProtocolTests.test_processEndedWithExitSignalNoCoreDumpÚ	WCOREDUMPzcan't run this w/o os.WCOREDUMP)r   r   r   r%   r   rÃ   r€   rà   rá   rˆ   râ   rã   rä   rè   rì   rå   ræ   rï   rð   rñ   rò   ró   rô   rõ   rö   r÷   rþ   rÿ   ÚosZskipMsgr   r   r   r   rß   ¹  s<   				
ÿ

	

		rß   c                   @   s    e Zd ZdZesdZdd„ ZdS )ÚSSHSessionClientTestsza
    SSHSessionClient is an obsolete class used to connect standard IO to
    an SSHSession.
    ry   c                 C   s.   t  ¡ }tƒ |_| d¡ |  |jjd¡ dS )zL
        When data is received, it should be sent to the transport.
        rî   N)r   ZSSHSessionClientrj   rX   rU   r„   ro   r‘   r   r   r   Útest_dataReceived©  s    
z'SSHSessionClientTests.test_dataReceivedN)r   r   r   r%   r   rÃ   r  r   r   r   r   r  ¡  s   r  )5r%   Z
__future__r   r   r  ræ   rê   rÕ   Zzope.interfacer   Ztwisted.python.reflectr   r   Ztwisted.conch.sshr   r   r	   Ztwisted.internet.addressr   Ztwisted.internet.errorr   r   Ztwisted.python.failurer   Ztwisted.internetr   r   r   Ztwisted.pythonr   r   Ztwisted.trialr   rƒ   r   r&   r
   r*   ZregisterAdapterr6   ZProtocolr   rZ   rj   rs   rv   ZTestCaserx   rÄ   rÊ   rÏ   rß   r  r   r   r   r   Ú<module>   sJ    bÿ4-L2  JD,O i