U
    Â“«jHr  ã                   @   sÎ   d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 edƒZ
ddlmZ e
rfdd	lmZmZ nG d
d„ dƒZG dd„ dejƒZG dd„ dƒZG dd„ dejƒZG dd„ de	jƒZG dd„ de	jƒZdS )z1
This module tests twisted.conch.ssh.connection.
é    N)Úchannel)Útest_userauth)ÚrequireModule)ÚunittestÚcryptography)Úerror)ÚcommonÚ
connectionc                   @   s   e Zd ZG dd„ dƒZdS )r	   c                   @   s   e Zd ZdS )zconnection.SSHConnectionN)Ú__name__Ú
__module__Ú__qualname__© r   r   úF/tmp/pip-unpacked-wheel-59kc99xd/twisted/conch/test/test_connection.pyÚSSHConnection   s   r   N)r
   r   r   r   r   r   r   r   r	      s   r	   c                   @   sd   e Zd 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d„ Zdd„ Zdd„ Zdd„ ZdS )ÚTestChannelaÒ  
    A mocked-up version of twisted.conch.ssh.channel.SSHChannel.

    @ivar gotOpen: True if channelOpen has been called.
    @type gotOpen: L{bool}
    @ivar specificData: the specific channel open data passed to channelOpen.
    @type specificData: L{bytes}
    @ivar openFailureReason: the reason passed to openFailed.
    @type openFailed: C{error.ConchError}
    @ivar inBuffer: a C{list} of strings received by the channel.
    @type inBuffer: C{list}
    @ivar extBuffer: a C{list} of 2-tuples (type, extended data) of received by
        the channel.
    @type extBuffer: C{list}
    @ivar numberRequests: the number of requests that have been made to this
        channel.
    @type numberRequests: L{int}
    @ivar gotEOF: True if the other side sent EOF.
    @type gotEOF: L{bool}
    @ivar gotOneClose: True if the other side closed the connection.
    @type gotOneClose: L{bool}
    @ivar gotClosed: True if the channel is closed.
    @type gotClosed: L{bool}
    ó   TestChannelFc                 C   s
   d| j  S )NzTestChannel %i)Úid©Úselfr   r   r   Ú	logPrefix:   s    zTestChannel.logPrefixc                 C   s4   d| _ || _g | _g | _d| _d| _d| _d| _dS )zF
        The channel is open.  Set up the instance variables.
        Tr   FN)ÚgotOpenÚspecificDataÚinBufferÚ	extBufferÚnumberRequestsÚgotEOFÚgotOneCloseÚ	gotClosed)r   r   r   r   r   ÚchannelOpen=   s    zTestChannel.channelOpenc                 C   s
   || _ dS )zD
        Opening the channel failed.  Store the reason why.
        N)ÚopenFailureReason)r   Úreasonr   r   r   Ú
openFailedJ   s    zTestChannel.openFailedc                 C   s   |  j d7  _ |dkS )z_
        A test request.  Return True if data is 'data'.

        @type data: L{bytes}
        é   ó   data)r   ©r   Údatar   r   r   Úrequest_testP   s    zTestChannel.request_testc                 C   s   | j  |¡ dS )z=
        Data was received.  Store it in the buffer.
        N)r   Úappendr$   r   r   r   ÚdataReceivedY   s    zTestChannel.dataReceivedc                 C   s   | j  ||f¡ dS )zF
        Extended data was received.  Store it in the buffer.
        N)r   r'   )r   Úcoder%   r   r   r   ÚextReceived_   s    zTestChannel.extReceivedc                 C   s
   d| _ dS )z1
        EOF was received.  Remember it.
        TN)r   r   r   r   r   ÚeofReceivede   s    zTestChannel.eofReceivedc                 C   s
   d| _ dS )z3
        Close was received.  Remember it.
        TN)r   r   r   r   r   ÚcloseReceivedk   s    zTestChannel.closeReceivedc                 C   s
   d| _ dS )z7
        The channel is closed.  Rembember it.
        TN)r   r   r   r   r   Úclosedq   s    zTestChannel.closedN)r
   r   r   Ú__doc__Únamer   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dZdd„ Zdd„ ZdS )Ú
TestAvatarz?
    A mocked-up version of twisted.conch.avatar.ConchUser
    é{   c                 C   s4   |t jkrt |||| d�S |dkr0t | jd¡‚dS )z’
        The server wants us to return a channel.  If the requested channel is
        our TestChannel, return it, otherwise return None.
        )ÚremoteWindowÚremoteMaxPacketr%   Úavataró   conch-error-argsúerror args in wrong orderN)r   r/   r   Ú
ConchErrorÚ_ARGS_ERROR_CODE)r   ZchannelTypeÚ
windowSizeÚ	maxPacketr%   r   r   r   ÚlookupChannel   s    
üzTestAvatar.lookupChannelc                 C   s$   |dkrdS |dkrd|fS dS dS )zû
        The client has made a global request.  If the global request is
        'TestGlobal', return True.  If the global request is 'TestData',
        return True and the request-specific data we received.  Otherwise,
        return False.
        ó
   TestGlobalTó   TestDataFNr   )r   ZrequestTyper%   r   r   r   ÚgotGlobalRequest’   s
    zTestAvatar.gotGlobalRequestN)r
   r   r   r.   r8   r;   r>   r   r   r   r   r0   x   s   r0   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 )ÚTestConnectionz}
    A subclass of SSHConnection for testing.

    @ivar channel: the current channel.
    @type channel. C{TestChannel}
    úCannot run without cryptographyc                 C   s   dS )Nr?   r   r   r   r   r   r   ¬   s    zTestConnection.logPrefixc                 C   s   dS )zT
        The other side made the 'TestGlobal' global request.  Return True.
        Tr   r$   r   r   r   Úglobal_TestGlobal¯   s    z TestConnection.global_TestGlobalc                 C   s   d|fS )zt
        The other side made the 'Test-Data' global request.  Return True and
        the data we received.
        Tr   r$   r   r   r   Úglobal_Test_Dataµ   s    zTestConnection.global_Test_Datac                 C   s   t |||d�| _| jS )zƒ
        The other side is requesting the TestChannel.  Create a C{TestChannel}
        instance, store it, and return it.
        )r2   r3   r%   )r   r   ©r   r9   r:   r%   r   r   r   Úchannel_TestChannel¼   s      ÿz"TestConnection.channel_TestChannelc                 C   s   t dƒ‚dS )zU
        The other side is requesting the ErrorChannel.  Raise an exception.
        zno such thingN)ÚAssertionErrorrC   r   r   r   Úchannel_ErrorChannelÆ   s    z#TestConnection.channel_ErrorChannelN)r
   r   r   r.   r   Úskipr   rA   rB   rD   rF   r   r   r   r   r?   ¡   s   
r?   c                   @   s  e 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.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ Z d:d;„ Z!d<d=„ Z"d>d?„ Z#d@dA„ Z$dBdC„ Z%dDS )EÚConnectionTestsr@   c                 C   s6   t  d ¡| _tƒ | j_tƒ | _| j| j_| j ¡  d S ©N)r   ÚFakeTransportÚ	transportr0   r4   r?   ÚconnÚserviceStartedr   r   r   r   ÚsetUpÑ   s
    

zConnectionTests.setUpc                 C   s@   | j  |¡ | jjdd… | j_| j  t d|jd¡d ¡ dS )z?
        Open the channel with the default connection.
        Néÿÿÿÿz>2Léÿ   s        € )rL   ÚopenChannelrK   ÚpacketsÚssh_CHANNEL_OPEN_CONFIRMATIONÚstructÚpackr   ©r   r   r   r   r   Ú_openChannelØ   s
    ÿzConnectionTests._openChannelc                 C   s   | j  ¡  d S rI   )rL   ÚserviceStoppedr   r   r   r   ÚtearDownâ   s    zConnectionTests.tearDownc                 C   s   |   | jjj| j¡ dS )z_
        Test that the connection links itself to the avatar in the
        transport.
        N)ZassertIsrK   r4   rL   r   r   r   r   Útest_linkAvatarå   s    zConnectionTests.test_linkAvatarc                 C   s¬   t ƒ }t ƒ }| j |¡ | j |¡ | j d¡ |  |j¡ |  |j¡ |  |j¡ |  |j¡ | j ¡  |  |j¡ |  |j¡ |  |j¡ ddl	m
} |  |j|¡ dS )zF
        Test that serviceStopped() closes any open channels.
        s                   r   )ÚConnectionLostN)r   rL   rQ   rS   Ú
assertTruer   ÚassertFalser   rX   Ztwisted.internet.errorr[   ZassertIsInstancer   )r   Zchannel1Úchannel2r[   r   r   r   Útest_serviceStoppedì   s    
z#ConnectionTests.test_serviceStoppedc                 C   sÐ   | j  t d¡d ¡ |  | jjtjdfg¡ g | j_| j  t d¡d d ¡ |  | jjtjdfg¡ g | j_| j  t d¡d ¡ |  | jjtj	dfg¡ g | j_| j  t d¡d ¡ |  | jjg ¡ dS )	z°
        Test that global request packets are dispatched to the global_*
        methods and the return values are translated into success or failure
        messages.
        r<   ó   ÿó    r=   s	   test datas   TestBadó    N)
rL   Zssh_GLOBAL_REQUESTr   ÚNSÚassertEqualrK   rR   r	   ZMSG_REQUEST_SUCCESSZMSG_REQUEST_FAILUREr   r   r   r   Útest_GLOBAL_REQUEST  s(     
ÿ 
ÿ 
ÿz#ConnectionTests.test_GLOBAL_REQUESTc                    sB   ˆ j  ddd¡}ˆ j  d¡ ‡ fdd„}| |¡ | ˆ j¡ |S )zh
        Test that global request success packets cause the Deferred to be
        called back.
        ó   requestr#   Tc                    s   ˆ   | d¡ d S ©Nr#   )rd   )r%   r   r   r   Úcheck!  s    z3ConnectionTests.test_REQUEST_SUCCESS.<locals>.check)rL   ÚsendGlobalRequestZssh_REQUEST_SUCCESSÚaddCallbackÚ
addErrbackÚfail©r   Údrh   r   r   r   Útest_REQUEST_SUCCESS  s    
z$ConnectionTests.test_REQUEST_SUCCESSc                    sB   ˆ j  ddd¡}ˆ j  d¡ ‡ fdd„}| ˆ j¡ | |¡ |S )zg
        Test that global request failure packets cause the Deferred to be
        erred back.
        rf   r#   Tc                    s   ˆ   | jjd¡ d S rg   )rd   Úvaluer%   )Úfr   r   r   rh   0  s    z3ConnectionTests.test_REQUEST_FAILURE.<locals>.check)rL   ri   Zssh_REQUEST_FAILURErj   rl   rk   rm   r   r   r   Útest_REQUEST_FAILURE(  s    
z$ConnectionTests.test_REQUEST_FAILUREc                 C   sH  | j `| j t d¡d ¡ |  | jjj¡ |  	| jjj| j¡ |  	| jjj
d¡ |  	| jjjd¡ |  	| jjjd¡ |  	| jjjd¡ |  	| j jtjdfg¡ g | j _| j t d¡d ¡ |  ¡  |  	| j jtjdt d	¡ t d
¡ fg¡ g | j _| j t d¡d ¡ |  ¡  |  	| j jtjdt d¡ t d
¡ fg¡ dS )z†
        Test that open channel packets cause a channel to be created and
        opened or a failure message to be returned.
        r   ó               ó      r"   s               € ó
   BadChannels               s         s   unknown channelra   s   ErrorChannels         s   unknown failureN)rK   r4   rL   Ússh_CHANNEL_OPENr   rc   r\   r   r   rd   r%   r   ÚremoteWindowLeftr3   rR   r	   ZMSG_CHANNEL_OPEN_CONFIRMATIONÚflushLoggedErrorsÚMSG_CHANNEL_OPEN_FAILUREr   r   r   r   Útest_CHANNEL_OPEN7  sV    þÿþ
ÿþþÿþÿþþÿþz!ConnectionTests.test_CHANNEL_OPENc                 C   sˆ   || j j_| j t d¡d ¡ |  tj	¡}|  
t|ƒdd|›�¡ |  
|d jjd¡ |  
| j jtjdt d¡ t d	¡ fg¡ d
S )zÅ
        Deliver a request for a channel open which will result in an exception
        being raised during channel lookup.  Assert that an error response is
        delivered as a result.
        r5   rs   r"   zExpected one error, got: r   )r1   r6   s         {s   error args in wrong orderra   N)rK   r4   r8   rL   rv   r   rc   rx   r   r7   rd   Úlenrp   ÚargsrR   r	   ry   )r   r)   Úerrorsr   r   r   Ú_lookupChannelErrorTestk  s$    
ÿÿþûÿþz'ConnectionTests._lookupChannelErrorTestc                 C   s   |   d¡ dS )aÔ  
        If a C{lookupChannel} implementation raises L{error.ConchError} with the
        arguments in the wrong order, a C{MSG_CHANNEL_OPEN} failure is still
        sent in response to the message.

        This is a temporary work-around until L{error.ConchError} is given
        better attributes and all of the Conch code starts constructing
        instances of it properly.  Eventually this functionality should be
        deprecated and then removed.
        r1   N)r~   r   r   r   r   Útest_lookupChannelError‡  s    z'ConnectionTests.test_lookupChannelErrorc                 C   st   t ƒ }| j |¡ | j d¡ |  |jd¡ |  |jd¡ |  |jd¡ |  | jj| d¡ |  | jj	d d¡ dS )zv
        Test that channel open confirmation packets cause the channel to be
        notified that it's open.
        s                       r   ó       N)
r   rL   rQ   rS   rd   rw   r3   r   ZchannelsToRemoteChannelZlocalToRemoteChannelrV   r   r   r   Útest_CHANNEL_OPEN_CONFIRMATION”  s    z.ConnectionTests.test_CHANNEL_OPEN_CONFIRMATIONc                 C   sP   t ƒ }| j |¡ | j dt d¡ ¡ |  |jjd¡ |  	| jj
 |¡¡ dS )zz
        Test that channel open failure packets cause the channel to be
        notified that its opening failed.
        ó          ó   failure!)rƒ   r"   N)r   rL   rQ   Zssh_CHANNEL_OPEN_FAILUREr   rc   rd   r   r|   ZassertIsNoneZchannelsÚgetrV   r   r   r   Útest_CHANNEL_OPEN_FAILURE¢  s    ÿz)ConnectionTests.test_CHANNEL_OPEN_FAILUREc                 C   s8   t ƒ }|  |¡ |j}| j d¡ |  |j|d ¡ dS )zc
        Test that channel window adjust messages add bytes to the channel
        window.
        r‚   r"   N)r   rW   rw   rL   Zssh_CHANNEL_WINDOW_ADJUSTrd   )r   r   ZoldWindowSizer   r   r   Útest_CHANNEL_WINDOW_ADJUST¯  s
    
z*ConnectionTests.test_CHANNEL_WINDOW_ADJUSTc                 C   s  t ddd�}|  |¡ | j dt d¡ ¡ |  |jdg¡ |  | jj	t
jdfg¡ g | j_	d|jd  }| j dt |¡ ¡ |  |jdg¡ |  | jj	t
jd	fg¡ t ƒ }|  |¡ d|jd  }g | j_	| j d
t |¡ ¡ |  |jg ¡ |  | jj	t
jd	fg¡ dS )z“
        Test that channel data messages are passed up to the channel, or
        cause the channel to be closed if the data is too large.
        é   é   ©ÚlocalWindowÚlocalMaxPacketr€   r#   ó      ÿ   ó   ar"   ó      ÿrt   N)r   rW   rL   Zssh_CHANNEL_DATAr   rc   rd   r   rK   rR   r	   ÚMSG_CHANNEL_WINDOW_ADJUSTÚlocalWindowLeftÚMSG_CHANNEL_CLOSEr‹   ©r   r   ZlongDataZbigDatar   r   r   Útest_CHANNEL_DATAº  s:    
þÿþ	
þ

þz!ConnectionTests.test_CHANNEL_DATAc                 C   s  t ddd�}|  |¡ | j dt d¡ ¡ |  |jdg¡ |  | jj	t
jdfg¡ g | j_	d|jd	  }| j dt |¡ ¡ |  |jdg¡ |  | jj	t
jd
fg¡ t ƒ }|  |¡ d|jd	  }g | j_	| j dt |¡ ¡ |  |jg ¡ |  | jj	t
jd
fg¡ dS )z–
        Test that channel extended data messages are passed up to the channel,
        or cause the channel to be closed if they're too big.
        r‡   rˆ   r‰   s           r#   )r   r#   rŒ   r�   r"   rŽ   s          N)r   rW   rL   Zssh_CHANNEL_EXTENDED_DATAr   rc   rd   r   rK   rR   r	   r�   r�   r‘   r‹   r’   r   r   r   Útest_CHANNEL_EXTENDED_DATAß  sF    
ÿþÿþ	ÿ
þ
ÿ
þz*ConnectionTests.test_CHANNEL_EXTENDED_DATAc                 C   s,   t ƒ }|  |¡ | j d¡ |  |j¡ dS )zN
        Test that channel eof messages are passed up to the channel.
        r€   N)r   rW   rL   Zssh_CHANNEL_EOFr\   r   rV   r   r   r   Útest_CHANNEL_EOF
  s    
z ConnectionTests.test_CHANNEL_EOFc                 C   sh   t ƒ }|  |¡ |  |j¡ |  |j¡ |  |j¡ | j |¡ | j 	d¡ |  |j¡ |  |j¡ dS )zÃ
        Test that channel close messages are passed up to the channel.  Also,
        test that channel.close() is called if both sides are closed when this
        message is received.
        r€   N)
r   rW   r\   r   r]   r   r   rL   Ú	sendCloseZssh_CHANNEL_CLOSErV   r   r   r   Útest_CHANNEL_CLOSE  s    
z"ConnectionTests.test_CHANNEL_CLOSEc                    sp   t ƒ }ˆ  |¡ ˆ j dt d¡ d ¡ ˆ  |jd¡ ˆ j dt d¡ d d ¡}‡ fdd„}| |¡ |S )	zS
        Test that channel requests that succeed send MSG_CHANNEL_SUCCESS.
        r€   ó   testrb   r"   r`   r#   c                    s   ˆ   ˆ jjtjdfg¡ d S ©NrŽ   )rd   rK   rR   r	   ZMSG_CHANNEL_SUCCESS©Úresultr   r   r   rh   1  s    
þz;ConnectionTests.test_CHANNEL_REQUEST_success.<locals>.check)	r   rW   rL   Ússh_CHANNEL_REQUESTr   rc   rd   r   rj   ©r   r   rn   rh   r   r   r   Útest_CHANNEL_REQUEST_success#  s    
ÿÿ
z,ConnectionTests.test_CHANNEL_REQUEST_successc                    sP   t ƒ }ˆ  |¡ ˆ j dt d¡ d ¡}‡ fdd„}| ˆ j¡ | |¡ |S )zP
        Test that channel requests that fail send MSG_CHANNEL_FAILURE.
        r€   r˜   r`   c                    s   ˆ   ˆ jjtjdfg¡ d S r™   )rd   rK   rR   r	   ZMSG_CHANNEL_FAILURErš   r   r   r   rh   D  s    
þz;ConnectionTests.test_CHANNEL_REQUEST_failure.<locals>.check)	r   rW   rL   rœ   r   rc   rj   rl   rk   r�   r   r   r   Útest_CHANNEL_REQUEST_failure:  s    
ÿ
z,ConnectionTests.test_CHANNEL_REQUEST_failurec                    s>   t ƒ }ˆ  |¡ ˆ j |ddd¡}ˆ j d¡ ‡ fdd„}|S )zj
        Test that channel request success messages cause the Deferred to be
        called back.
        r˜   r#   Tr€   c                    s   ˆ   | ¡ d S rI   )r\   rš   r   r   r   rh   X  s    z;ConnectionTests.test_CHANNEL_REQUEST_SUCCESS.<locals>.check)r   rW   rL   ÚsendRequestZssh_CHANNEL_SUCCESSr�   r   r   r   Útest_CHANNEL_REQUEST_SUCCESSN  s    
z,ConnectionTests.test_CHANNEL_REQUEST_SUCCESSc                    sT   t ƒ }ˆ  |¡ ˆ j |ddd¡}ˆ j d¡ ‡ fdd„}| ˆ j¡ | |¡ |S )zi
        Test that channel request failure messages cause the Deferred to be
        erred back.
        r˜   ra   Tr€   c                    s   ˆ   | jjd¡ d S )Nzchannel request failed)rd   rp   rš   r   r   r   rh   g  s    z;ConnectionTests.test_CHANNEL_REQUEST_FAILURE.<locals>.check)r   rW   rL   r    Zssh_CHANNEL_FAILURErj   rl   rk   r�   r   r   r   Útest_CHANNEL_REQUEST_FAILURE]  s    

z,ConnectionTests.test_CHANNEL_REQUEST_FAILUREc                 C   s|   | j  ddd¡}| dd„ ¡ | j  ddd¡ |  | jjtjt 	d¡d	 ftjt 	d¡d
 fg¡ |  | j j
d|gi¡ dS )zQ
        Test that global request messages are sent in the right format.
        s	   wantReplyr#   Tc                 S   s   d S rI   r   ©Zfailurer   r   r   Ú<lambda>t  ra   z8ConnectionTests.test_sendGlobalRequest.<locals>.<lambda>s   noReplyra   Fs   ÿdatarb   ÚglobalN)rL   ri   rk   rd   rK   rR   r	   ZMSG_GLOBAL_REQUESTr   rc   Ú	deferreds©r   rn   r   r   r   Útest_sendGlobalRequestn  s    þþz&ConnectionTests.test_sendGlobalRequestc                 C   sX   t ƒ }| j |d¡ |  | jjtjt 	d¡d fg¡ |  |j
d¡ |  | jjd¡ dS )zO
        Test that open channel messages are sent in the right format.
        s   aaaar   s            € aaaar   r"   N)r   rL   rQ   rd   rK   rR   r	   ZMSG_CHANNEL_OPENr   rc   r   ZlocalChannelIDrV   r   r   r   Útest_openChannel  s    ÿþÿþ
z ConnectionTests.test_openChannelc              	   C   s°   t ƒ }|  |¡ | j |ddd¡}| dd„ ¡ | j |ddd¡ d|_| j |ddd¡ |  | jjt	j
d	t d¡ d
 ft	j
d	t d¡ d fg¡ |  | jjd |g¡ dS )zR
        Test that channel request messages are sent in the right format.
        r˜   Tc                 S   s   d S rI   r   r£   r   r   r   r¤   š  ra   z2ConnectionTests.test_sendRequest.<locals>.<lambda>ó   test2ra   Fs   test3rŽ   s   testrb   r   N)r   rW   rL   r    rk   ÚlocalClosedrd   rK   rR   r	   ZMSG_CHANNEL_REQUESTr   rc   r¦   ©r   r   rn   r   r   r   Útest_sendRequest’  s$    
þþûþz ConnectionTests.test_sendRequestc                 C   st   t dd�}|  |¡ d|_| j |d¡ |  |jd¡ d|_| j |d¡ |  |jd¡ |  | jjt	j
dfg¡ dS )	zi
        Test that channel window adjust messages cause bytes to be added
        to the window.
        rˆ   )rŠ   r   r"   Té   ó      ÿ   N)r   rW   r�   rL   ZadjustWindowrd   r«   rK   rR   r	   r�   rV   r   r   r   Útest_adjustWindow­  s    

þÿþz!ConnectionTests.test_adjustWindowc                 C   sX   t ƒ }|  |¡ | j |d¡ d|_| j |d¡ |  | jjtj	dt
 d¡ fg¡ dS )zO
        Test that channel data messages are sent in the right format.
        r�   Tó   brŽ   N)r   rW   rL   ZsendDatar«   rd   rK   rR   r	   ZMSG_CHANNEL_DATAr   rc   rV   r   r   r   Útest_sendDataÄ  s    
þzConnectionTests.test_sendDatac                 C   s\   t ƒ }|  |¡ | j |dd¡ d|_| j |dd¡ |  | jjtj	dt
 d¡ fg¡ dS )zX
        Test that channel extended data messages are sent in the right format.
        r"   r˜   Tr®   rª   r¯   N)r   rW   rL   ZsendExtendedDatar«   rd   rK   rR   r	   ZMSG_CHANNEL_EXTENDED_DATAr   rc   rV   r   r   r   Útest_sendExtendedDataÒ  s    
þÿþz%ConnectionTests.test_sendExtendedDatac                 C   sb   t ƒ }|  |¡ | j |¡ |  | jjtjdfg¡ d|_	| j |¡ |  | jjtjdfg¡ dS )zN
        Test that channel EOF messages are sent in the right format.
        rŽ   TN)
r   rW   rL   ZsendEOFrd   rK   rR   r	   ZMSG_CHANNEL_EOFr«   rV   r   r   r   Útest_sendEOFå  s    
 
ÿ 
ÿzConnectionTests.test_sendEOFc                 C   s®   t ƒ }|  |¡ | j |¡ |  |j¡ |  | jjt	j
dfg¡ | j |¡ |  | jjt	j
dfg¡ t ƒ }|  |¡ |  |j¡ |  |j¡ d|_| j |¡ |  |j¡ dS )zP
        Test that channel close messages are sent in the right format.
        rŽ   TN)r   rW   rL   r–   r\   r«   rd   rK   rR   r	   r‘   r   r]   r   ZremoteClosed)r   r   r^   r   r   r   Útest_sendCloseõ  s(    

þ
þ
zConnectionTests.test_sendClosec                 C   sZ   | j  dddd¡}|  |jd¡ |  |jd¡ |  |jd¡ |  tj| j jdddd¡ dS )z³
        Test that getChannel dispatches to the avatar when an avatar is
        present. Correct functioning without the avatar is verified in
        test_CHANNEL_OPEN.
        r   é2   é   r#   ru   N)	rL   Z
getChannelrd   r%   rw   r3   ÚassertRaisesr   r7   rV   r   r   r   Útest_getChannelWithAvatar  s         ÿz)ConnectionTests.test_getChannelWithAvatarc                 C   sH   | j `|  | j dd¡¡ |  | j dd¡d¡ |  | j dd¡¡ dS )zW
        Test that gotGlobalRequests dispatches to global_* without an avatar.
        r<   r#   s	   Test-Data)Tr#   s	   BadGlobalN)rK   r4   r\   rL   r>   rd   r]   r   r   r   r   Ú"test_gotGlobalRequestWithoutAvatar  s     ÿz2ConnectionTests.test_gotGlobalRequestWithoutAvatarc                 C   sB   t ƒ }|  |¡ | jj|dddd�}|  |tj¡}| j |¡ |S )z›
        Whenever an SSH channel gets closed any Deferred that was returned by a
        sendRequest() on its parent connection must be errbacked.
        ó   dummyrequestó	   dummydatar"   ©Z	wantReply)r   rW   rL   r    ÚassertFailurer   r7   ZchannelClosedr¬   r   r   r   Ú9test_channelClosedCausesLeftoverChannelDeferredsToErrback(  s    
zIConnectionTests.test_channelClosedCausesLeftoverChannelDeferredsToErrbackN)&r
   r   r   r   rG   rN   rW   rY   rZ   r_   re   ro   rr   rz   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   rH   Í   sF   
4%+	rH   c                   @   s(   e Zd ZdZesdZdd„ Zdd„ ZdS )ÚCleanConnectionShutdownTestszL
    Check whether correct cleanup is performed on connection shutdown.
    r@   c                 C   s,   t  d ¡| _tƒ | j_tƒ | _| j| j_d S rI   )r   rJ   rK   r0   r4   r?   rL   r   r   r   r   rN   >  s    
z"CleanConnectionShutdownTests.setUpc                 C   s8   | j  ¡  | j jdddd�}|  |tj¡}| j  ¡  |S )zŒ
        Once the service is stopped any leftover global deferred returned by
        a sendGlobalRequest() call must be errbacked.
        r»   r¼   r"   r½   )rL   rM   ri   r¾   r   r7   rX   r§   r   r   r   Ú9test_serviceStoppedCausesLeftoverGlobalDeferredsToErrbackD  s
    

zVCleanConnectionShutdownTests.test_serviceStoppedCausesLeftoverGlobalDeferredsToErrbackN)r
   r   r   r.   r   rG   rN   rÁ   r   r   r   r   rÀ   6  s
   rÀ   )r.   rT   Ztwisted.conch.sshr   Ztwisted.conch.testr   Ztwisted.python.reflectr   Ztwisted.trialr   r   Ztwisted.conchr   r   r	   Z
SSHChannelr   r0   r   r?   ZTestCaserH   rÀ   r   r   r   r   Ú<module>   s&   \),    m