o
    ¯bIr  ã                   @   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   úD/usr/lib/python3/dist-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}
        é   ó   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   ó
   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   Ú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ó   TestDataFr   )r   ÚrequestTyper&   r   r   r   ÚgotGlobalRequest’   s
   zTestAvatar.gotGlobalRequestN)r
   r   r   r0   r:   r>   rB   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 )NrC   r   r   r   r   r   r   ¬   s   zTestConnection.logPrefixc                 C   rE   )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.
        )r4   r5   r&   )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 thing)ÚAssertionErrorrH   r   r   r   Úchannel_ErrorChannelÆ   s   z#TestConnection.channel_ErrorChannelN)r
   r   r   r0   r   Úskipr   rF   rG   rI   rK   r   r   r   r   rC   ¡   s    
rC   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ÚConnectionTestsrD   c                 C   s6   t  d ¡| _tƒ | j_tƒ | _| j| j_| j ¡  d S ©N)r   ÚFakeTransportÚ	transportr2   r6   rC   Ú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        € )rQ   ÚopenChannelrP   ÚpacketsÚssh_CHANNEL_OPEN_CONFIRMATIONÚstructÚpackr   ©r   r   r   r   r   Ú_openChannelÙ   s
   ÿzConnectionTests._openChannelc                 C   s   | j  ¡  d S rN   )rQ   Ú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)ÚassertIsrP   r6   rQ   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   rQ   rV   rX   Ú
assertTruer   ÚassertFalser   r]   Útwisted.internet.errorrb   ÚassertIsInstancer   )r   Úchannel1Úchannel2rb   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)
rQ   Ússh_GLOBAL_REQUESTr   ÚNSÚassertEqualrP   rW   r	   ÚMSG_REQUEST_SUCCESSÚ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$   )ro   )r&   r   r   r   Úcheck"  s   z3ConnectionTests.test_REQUEST_SUCCESS.<locals>.check)rQ   ÚsendGlobalRequestÚssh_REQUEST_SUCCESSÚaddCallbackÚ
addErrbackÚfail©r   Údru   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.
        rs   r$   Tc                    s   ˆ   | jjd¡ d S rt   )ro   Úvaluer&   )Úfr   r   r   ru   1  ó   z3ConnectionTests.test_REQUEST_FAILURE.<locals>.check)rQ   rv   Ússh_REQUEST_FAILURErx   rz   ry   r{   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 channelrk   s   ErrorChannels         s   unknown failureN)rP   r6   rQ   Ússh_CHANNEL_OPENr   rn   rc   r   r   ro   r&   r   ÚremoteWindowLeftr5   rW   r	   ÚMSG_CHANNEL_OPEN_CONFIRMATIONÚflushLoggedErrorsÚMSG_CHANNEL_OPEN_FAILUREr   r   r   r   Útest_CHANNEL_OPEN8  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   rƒ   r#   zExpected one error, got: r   )r3   r8   s         {s   error args in wrong orderrk   N)rP   r6   r:   rQ   r†   r   rn   r‰   r   r9   ro   Úlenr~   ÚargsrW   r	   rŠ   )r   r*   Úerrorsr   r   r   Ú_lookupChannelErrorTestl  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   rQ   rV   rX   ro   r‡   r5   r   ÚchannelsToRemoteChannelÚlocalToRemoteChannelr[   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   rQ   rV   Ússh_CHANNEL_OPEN_FAILUREr   rn   ro   r   r�   ÚassertIsNoneÚchannelsÚgetr[   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   r\   r‡   rQ   Ússh_CHANNEL_WINDOW_ADJUSTro   )r   r   Ú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#   ó      ÿr„   N)r   r\   rQ   Ússh_CHANNEL_DATAr   rn   ro   r   rP   rW   r	   ÚMSG_CHANNEL_WINDOW_ADJUSTÚlocalWindowLeftÚMSG_CHANNEL_CLOSEr£   ©r   r   ÚlongDataÚ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   r\   rQ   Ússh_CHANNEL_EXTENDED_DATAr   rn   ro   r   rP   rW   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   r\   rQ   Ússh_CHANNEL_EOFrc   r   r[   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   r\   rc   r   rd   r   r   rQ   Ú	sendCloseÚssh_CHANNEL_CLOSEr[   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‘   ó   testrl   r#   rj   r$   c                    ó   ˆ   ˆ jjtjdfg¡ d S ©Nr¦   )ro   rP   rW   r	   ÚMSG_CHANNEL_SUCCESS©Úresultr   r   r   ru   2  ó   
þz;ConnectionTests.test_CHANNEL_REQUEST_success.<locals>.check)	r   r\   rQ   Ússh_CHANNEL_REQUESTr   rn   ro   r   rx   ©r   r   r|   ru   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¶   rj   c                    r·   r¸   )ro   rP   rW   r	   ÚMSG_CHANNEL_FAILURErº   r   r   r   ru   E  r¼   z;ConnectionTests.test_CHANNEL_REQUEST_failure.<locals>.check)	r   r\   rQ   r½   r   rn   rx   rz   ry   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 rN   )rc   rº   r   r   r   ru   Y  r_   z;ConnectionTests.test_CHANNEL_REQUEST_SUCCESS.<locals>.check)r   r\   rQ   ÚsendRequestÚssh_CHANNEL_SUCCESSr¾   r   r   r   Útest_CHANNEL_REQUEST_SUCCESSO  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¶   rk   Tr‘   c                    s   ˆ   | jjd¡ d S )Nzchannel request failed)ro   r~   rº   r   r   r   ru   h  r€   z;ConnectionTests.test_CHANNEL_REQUEST_FAILURE.<locals>.check)r   r\   rQ   rÂ   Ússh_CHANNEL_FAILURErx   rz   ry   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 rN   r   ©Úfailurer   r   r   Ú<lambda>u  ó    z8ConnectionTests.test_sendGlobalRequest.<locals>.<lambda>s   noReplyrk   Fs   ÿdatarl   ÚglobalN)rQ   rv   ry   ro   rP   rW   r	   ÚMSG_GLOBAL_REQUESTr   rn   Ú	deferreds©r   r|   r   r   r   Útest_sendGlobalRequesto  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   rQ   rV   ro   rP   rW   r	   ÚMSG_CHANNEL_OPENr   rn   r   ÚlocalChannelIDr[   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Ç   rN   r   rÈ   r   r   r   rÊ   ›  rË   z2ConnectionTests.test_sendRequest.<locals>.<lambda>ó   test2rk   Fs   test3r¦   s   testrl   r   N)r   r\   rQ   rÂ   ry   ÚlocalClosedro   rP   rW   r	   ÚMSG_CHANNEL_REQUESTr   rn   rÎ   ©r   r   r|   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   r\   r©   rQ   ÚadjustWindowro   rÕ   rP   rW   r	   r¨   r[   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   r\   rQ   ÚsendDatarÕ   ro   rP   rW   r	   ÚMSG_CHANNEL_DATAr   rn   r[   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   r\   rQ   ÚsendExtendedDatarÕ   ro   rP   rW   r	   ÚMSG_CHANNEL_EXTENDED_DATAr   rn   r[   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   r\   rQ   ÚsendEOFro   rP   rW   r	   ÚMSG_CHANNEL_EOFrÕ   r[   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   r\   rQ   r³   rc   rÕ   ro   rP   rW   r	   rª   r   rd   r   ÚremoteClosed)r   r   rh   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$   r…   N)	rQ   Ú
getChannelro   r&   r‡   r5   ÚassertRaisesr   r9   r[   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)rP   r6   rc   rQ   rB   ro   rd   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#   ©Ú	wantReply)r   r\   rQ   rÂ   ÚassertFailurer   r9   ÚchannelClosedr×   r   r   r   Ú9test_channelClosedCausesLeftoverChannelDeferredsToErrback)  s   
zIConnectionTests.test_channelClosedCausesLeftoverChannelDeferredsToErrbackN)&r
   r   r   r   rL   rS   r\   r^   ra   ri   rr   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   r   r   rM   Í   sH    
4%+	rM   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.
    rD   c                 C   s,   t  d ¡| _tƒ | j_tƒ | _| j| j_d S rN   )r   rO   rP   r2   r6   rC   rQ   r   r   r   r   rS   ?  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ñ   )rQ   rR   rv   ró   r   r9   r]   rÏ   r   r   r   Ú9test_serviceStoppedCausesLeftoverGlobalDeferredsToErrbackE  s
   

zVCleanConnectionShutdownTests.test_serviceStoppedCausesLeftoverGlobalDeferredsToErrbackN)r
   r   r   r0   r   rL   rS   r÷   r   r   r   r   rö   7  s    rö   )r0   rY   Útwisted.conch.sshr   Útwisted.conch.testr   Útwisted.python.reflectr   Útwisted.trialr   r   Útwisted.conchr   r   r	   Ú
SSHChannelr   r2   r   rC   ÚTestCaserM   rö   r   r   r   r   Ú<module>   s(   \),    n