o
    Òó·iHr  ã                   @   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
r3d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   úhC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\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   ó   
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}
        é   ó   dataN)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   ó
   d| _ dS )z1
        EOF was received.  Remember it.
        TN)r   r   r   r   r   ÚeofReceivede   r"   zTestChannel.eofReceivedc                 C   r,   )z3
        Close was received.  Remember it.
        TN)r   r   r   r   r   ÚcloseReceivedk   r"   zTestChannel.closeReceivedc                 C   r,   )z7
        The channel is closed.  Rembember it.
        TN)r   r   r   r   r   Úclosedq   r"   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rt | 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   r1   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 )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   r0   r:   r=   r@   r   r   r   r   r2   x   s
    r2   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   ó   dS )NrA   r   r   r   r   r   r   ¬   s   zTestConnection.logPrefixc                 C   rC   )zT
        The other side made the 'TestGlobal' global request.  Return True.
        TNr   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.
        TNr   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.
        )r4   r5   r&   N)r   r   ©r   r;   r<   r&   r   r   r   Úchannel_TestChannel¼   s   ÿz"TestConnection.channel_TestChannelc                 C   s   t dƒ‚)zU
        The other side is requesting the ErrorChannel.  Raise an exception.
        zno such thingN)ÚAssertionErrorrF   r   r   r   Úchannel_ErrorChannelÆ   s   z#TestConnection.channel_ErrorChannelN)r
   r   r   r0   r   Úskipr   rD   rE   rG   rI   r   r   r   r   rA   ¡   s    
rA   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ÚConnectionTestsrB   c                 C   s6   t  d ¡| _tƒ | j_tƒ | _| j| j_| j ¡  d S ©N)r   ÚFakeTransportÚ	transportr2   r6   rA   Ú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        € )rO   ÚopenChannelrN   ÚpacketsÚssh_CHANNEL_OPEN_CONFIRMATIONÚstructÚpackr   ©r   r   r   r   r   Ú_openChannelØ   s
   ÿzConnectionTests._openChannelc                 C   s   | j  ¡  d S rL   )rO   ÚserviceStoppedr   r   r   r   ÚtearDownâ   ó   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assertIsrN   r6   rO   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   rO   rT   rV   Ú
assertTruer   ÚassertFalser   r[   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)
rO   Zssh_GLOBAL_REQUESTr   ÚNSÚassertEqualrN   rU   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$   )rh   )r&   r   r   r   Úcheck!  s   z3ConnectionTests.test_REQUEST_SUCCESS.<locals>.checkN)rO   ÚsendGlobalRequestZssh_REQUEST_SUCCESSÚaddCallbackÚ
addErrbackÚfail©r   Údrl   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.
        rj   r$   Tc                    s   ˆ   | jjd¡ d S rk   )rh   Úvaluer&   )Úfr   r   r   rl   0  ó   z3ConnectionTests.test_REQUEST_FAILURE.<locals>.checkN)rO   rm   Zssh_REQUEST_FAILURErn   rp   ro   rq   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 channelre   s   ErrorChannels         s   unknown failureN)rN   r6   rO   Ússh_CHANNEL_OPENr   rg   r`   r   r   rh   r&   r   ÚremoteWindowLeftr5   rU   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.
        r7   rx   r#   zExpected one error, got: r   )r3   r8   s         {s   error args in wrong orderre   N)rN   r6   r:   rO   r{   r   rg   r}   r   r9   rh   Úlenrt   ÚargsrU   r	   r~   )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.
        r3   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   rO   rT   rV   rh   r|   r5   r   ZchannelsToRemoteChannelZlocalToRemoteChannelrY   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   rO   rT   Zssh_CHANNEL_OPEN_FAILUREr   rg   rh   r   r�   ZassertIsNoneZchannelsÚgetrY   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   rZ   r|   rO   Zssh_CHANNEL_WINDOW_ADJUSTrh   )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#   ó      ÿry   N)r   rZ   rO   Zssh_CHANNEL_DATAr   rg   rh   r   rN   rU   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   rZ   rO   Zssh_CHANNEL_EXTENDED_DATAr   rg   rh   r   rN   rU   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   rZ   rO   Zssh_CHANNEL_EOFr`   r   rY   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   rZ   r`   r   ra   r   r   rO   Ú	sendCloseZssh_CHANNEL_CLOSErY   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…   ó   testrf   r#   rd   r$   c                    ó   ˆ   ˆ jjtjdfg¡ d S ©Nr“   )rh   rN   rU   r	   ZMSG_CHANNEL_SUCCESS©Úresultr   r   r   rl   1  ó   
þz;ConnectionTests.test_CHANNEL_REQUEST_success.<locals>.checkN)	r   rZ   rO   Ússh_CHANNEL_REQUESTr   rg   rh   r   rn   ©r   r   rr   rl   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�   rd   c                    rž   rŸ   )rh   rN   rU   r	   ZMSG_CHANNEL_FAILUREr    r   r   r   rl   D  r¢   z;ConnectionTests.test_CHANNEL_REQUEST_failure.<locals>.checkN)	r   rZ   rO   r£   r   rg   rn   rp   ro   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 rL   )r`   r    r   r   r   rl   X  r]   z;ConnectionTests.test_CHANNEL_REQUEST_SUCCESS.<locals>.checkN)r   rZ   rO   Ú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�   re   Tr…   c                    s   ˆ   | jjd¡ d S )Nzchannel request failed)rh   rt   r    r   r   r   rl   g  rv   z;ConnectionTests.test_CHANNEL_REQUEST_FAILURE.<locals>.checkN)r   rZ   rO   r§   Zssh_CHANNEL_FAILURErn   rp   ro   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   ó   d S rL   r   ©Zfailurer   r   r   Ú<lambda>t  ó    z8ConnectionTests.test_sendGlobalRequest.<locals>.<lambda>s   noReplyre   Fs   ÿdatarf   ÚglobalN)rO   rm   ro   rh   rN   rU   r	   ZMSG_GLOBAL_REQUESTr   rg   Ú	deferreds©r   rr   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   rO   rT   rh   rN   rU   r	   ZMSG_CHANNEL_OPENr   rg   r   ZlocalChannelIDrY   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   rª   rL   r   r«   r   r   r   r¬   š  r­   z2ConnectionTests.test_sendRequest.<locals>.<lambda>ó   test2re   Fs   test3r“   s   testrf   r   N)r   rZ   rO   r§   ro   ÚlocalClosedrh   rN   rU   r	   ZMSG_CHANNEL_REQUESTr   rg   r¯   ©r   r   rr   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   rZ   r•   rO   ZadjustWindowrh   r´   rN   rU   r	   r”   rY   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   rZ   rO   ZsendDatar´   rh   rN   rU   r	   ZMSG_CHANNEL_DATAr   rg   rY   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   rZ   rO   ZsendExtendedDatar´   rh   rN   rU   r	   ZMSG_CHANNEL_EXTENDED_DATAr   rg   rY   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   rZ   rO   ZsendEOFrh   rN   rU   r	   ZMSG_CHANNEL_EOFr´   rY   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   rZ   rO   r›   r`   r´   rh   rN   rU   r	   r–   r   ra   r   ZremoteClosed)r   r   rb   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$   rz   N)	rO   Z
getChannelrh   r&   r|   r5   ÚassertRaisesr   r9   rY   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)rN   r6   r`   rO   r@   rh   ra   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	wantReplyN)r   rZ   rO   r§   ÚassertFailurer   r9   ZchannelClosedrµ   r   r   r   Ú9test_channelClosedCausesLeftoverChannelDeferredsToErrback(  s   
zIConnectionTests.test_channelClosedCausesLeftoverChannelDeferredsToErrbackN)&r
   r   r   r   rJ   rQ   rZ   r\   r^   rc   ri   rs   rw   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   r   rK   Í   sH    
4%+	rK   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.
    rB   c                 C   s,   t  d ¡| _tƒ | j_tƒ | _| j| j_d S rL   )r   rM   rN   r2   r6   rA   rO   r   r   r   r   rQ   >  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Æ   N)rO   rP   rm   rÇ   r   r9   r[   r°   r   r   r   Ú9test_serviceStoppedCausesLeftoverGlobalDeferredsToErrbackD  s
   

zVCleanConnectionShutdownTests.test_serviceStoppedCausesLeftoverGlobalDeferredsToErrbackN)r
   r   r   r0   r   rJ   rQ   rÊ   r   r   r   r   rÉ   6  s    rÉ   )r0   rW   Ztwisted.conch.sshr   Ztwisted.conch.testr   Ztwisted.python.reflectr   Ztwisted.trialr   r   Ztwisted.conchr   r   r	   Z
SSHChannelr   r2   r   rA   ZTestCaserK   rÉ   r   r   r   r   Ú<module>   s(   \),    m