U
    
W[õm  ć                   @   sź   d Z ddlmZmZ ddlZddlmZ edZddlm	Z	 erRddl
mZmZ nG dd	 d	Zdd
l
mZ ddlmZ ddlmZ ddlm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.
é    )ŚdivisionŚabsolute_importN)ŚrequireModuleŚ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   )Śchannel)Ślong)Śunittest)Śtest_userauthc                   @   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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   Ś	logPrefix8   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   Ś
openFailedH   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_testN   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   ŚdataReceivedW   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   ŚeofReceivedc   s    zTestChannel.eofReceivedc                 C   s
   d| _ dS )z3
        Close was received.  Remember it.
        TN)r   r   r   r   r   ŚcloseReceivedi   s    zTestChannel.closeReceivedc                 C   s
   d| _ dS )z7
        The channel is closed.  Rembember it.
        TN)r    r   r   r   r   Śclosedo   s    zTestChannel.closedN)r	   r
   r   Ś__doc__Śnamer   r   r!   r$   r)   r+   r-   r.   r/   r0   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   r2   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   r1   r;   r>   rA   r   r   r   r   r3   u   s   r3   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 )NrB   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.
        )r5   r6   r(   )r   r   ©r   r<   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)ŚAssertionErrorrF   r   r   r   Śchannel_ErrorChannelĮ   s    z#TestConnection.channel_ErrorChannelN)r	   r
   r   r1   r   Śskipr   rD   rE   rG   rI   r   r   r   r   rB      s   	rB   c                   @   s2  e Zd ZesdZejdkr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&dDdE Z'dFdG Z(dS )HŚConnectionTestsrC   Nś/Cannot run without both cryptography and pyasn1c                 C   s6   t  d ”| _t | j_t | _| j| j_| j ”  d S ©N)r   ŚFakeTransportŚ	transportr3   r7   rB   Ś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         )rP   ŚopenChannelrO   ŚpacketsŚssh_CHANNEL_OPEN_CONFIRMATIONŚstructZpackr   ©r   r   r   r   r   Ś_openChannel×   s     ’’zConnectionTests._openChannelc                 C   s   | j  ”  d S rM   )rP   Ś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assertIsrO   r7   rP   r   r   r   r   Śtest_linkAvatarć   s    zConnectionTests.test_linkAvatarc                 C   sb   t  }t  }| j |” | j |” | j d” |  |j” |  |j” | j ”  |  |j” dS )zF
        Test that serviceStopped() closes any open channels.
        s                   N)	r   rP   rU   rW   Ś
assertTruer   ŚassertFalser[   r    )r   Zchannel1Śchannel2r   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)
rP   Zssh_GLOBAL_REQUESTr   ŚNSŚassertEqualrO   rV   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&   )rf   )r(   r   r   r   Ścheck  s    z3ConnectionTests.test_REQUEST_SUCCESS.<locals>.check)rP   ŚsendGlobalRequestZssh_REQUEST_SUCCESSŚaddCallbackŚ
addErrbackŚfail©r   Śdrj   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.
        rh   r&   Tc                    s      | jjd” d S ri   )rf   Śvaluer(   )Śfr   r   r   rj   "  s    z3ConnectionTests.test_REQUEST_FAILURE.<locals>.check)rP   rk   Zssh_REQUEST_FAILURErl   rn   rm   ro   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 channelrc   s   ErrorChannels         s   unknown failureN)rO   r7   rP   Śssh_CHANNEL_OPENr   re   r^   r   r   rf   r(   r   ŚremoteWindowLeftr6   rV   r   ZMSG_CHANNEL_OPEN_CONFIRMATIONŚflushLoggedErrorsŚMSG_CHANNEL_OPEN_FAILUREr   r   r   r   Śtest_CHANNEL_OPEN(  sV    ’
’’’
’’’’’
’’’’z!ConnectionTests.test_CHANNEL_OPENc                 C   s   || j j_| j t d”d ” |  tj	”}|  
t|dd|f ” |  
|d jjtddf” |  
| 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.
        r8   ru   r%   zExpected one error, got: %rr   r4   r9   s         {s   error args in wrong orderrc   N)rO   r7   r;   rP   rx   r   re   rz   r   r:   rf   Ślenrr   Śargsr   rV   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.
        r4   N)r   r   r   r   r   Śtest_lookupChannelErrorc  s    z'ConnectionTests.test_lookupChannelErrorc                 C   s   |   td” dS )z
        Like L{test_lookupChannelError}, but for the case where the failure code
        is represented as a L{long} instead of a L{int}.
        r4   N)r   r   r   r   r   r   Śtest_lookupChannelErrorLongCodeq  s    z/ConnectionTests.test_lookupChannelErrorLongCodec                 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   rP   rU   rW   rf   ry   r6   r   ZchannelsToRemoteChannelZlocalToRemoteChannelrY   r   r   r   Śtest_CHANNEL_OPEN_CONFIRMATIONy  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   rP   rU   Zssh_CHANNEL_OPEN_FAILUREr   re   rf   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   ry   rP   Zssh_CHANNEL_WINDOW_ADJUSTrf   )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%   ó      ’rv   N)r   rZ   rP   Zssh_CHANNEL_DATAr   re   rf   r   rO   rV   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   rP   Zssh_CHANNEL_EXTENDED_DATAr   re   rf   r   rO   rV   r   r   r   r   r   r   r   r   r   Śtest_CHANNEL_EXTENDED_DATA¼  s:    
’

’’

’
’

’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   rP   Zssh_CHANNEL_EOFr^   r   rY   r   r   r   Śtest_CHANNEL_EOFŚ  s    
z ConnectionTests.test_CHANNEL_EOFc                 C   sD   t  }|  |” | 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   rP   Ś	sendCloseZssh_CHANNEL_CLOSEr^   r   r    rY   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   ó   testrd   r%   rb   r&   c                    s       jjtjdfg” d S ©Nr   )rf   rO   rV   r   ZMSG_CHANNEL_SUCCESS©Śresultr   r   r   rj   ū  s    

’z;ConnectionTests.test_CHANNEL_REQUEST_success.<locals>.check)	r   rZ   rP   Śssh_CHANNEL_REQUESTr   re   rf   r   rl   ©r   r   rp   rj   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   rb   c                    s       jjtjdfg” d S r   )rf   rO   rV   r   ZMSG_CHANNEL_FAILUREr   r   r   r   rj   	  s    

’z;ConnectionTests.test_CHANNEL_REQUEST_failure.<locals>.check)	r   rZ   rP   r   r   re   rl   rn   rm   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 rM   )r^   r   r   r   r   rj     s    z;ConnectionTests.test_CHANNEL_REQUEST_SUCCESS.<locals>.check)r   rZ   rP   ŚsendRequestZssh_CHANNEL_SUCCESSr    r   r   r   Śtest_CHANNEL_REQUEST_SUCCESS  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   rc   Tr   c                    s      | jjd” d S )Nzchannel request failed)rf   rr   r   r   r   r   rj   '  s    z;ConnectionTests.test_CHANNEL_REQUEST_FAILURE.<locals>.check)r   rZ   rP   r£   Zssh_CHANNEL_FAILURErl   rn   rm   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 rM   r   ©Zfailurer   r   r   Ś<lambda>3  rc   z8ConnectionTests.test_sendGlobalRequest.<locals>.<lambda>s   noReplyrc   Fs   ’datard   ŚglobalN)rP   rk   rm   rf   rO   rV   r   ZMSG_GLOBAL_REQUESTr   re   Ś	deferreds©r   rp   r   r   r   Śtest_sendGlobalRequest-  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   rP   rU   rf   rO   rV   r   ZMSG_CHANNEL_OPENr   re   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   s   d S rM   r   r¦   r   r   r   r§   P  rc   z2ConnectionTests.test_sendRequest.<locals>.<lambda>ó   test2rc   Fs   test3r   s   testrd   r   N)r   rZ   rP   r£   rm   ŚlocalClosedrf   rO   rV   r   ZMSG_CHANNEL_REQUESTr   re   r©   ©r   r   rp   r   r   r   Śtest_sendRequestH  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   rP   ZadjustWindowrf   r®   rO   rV   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   rP   ZsendDatar®   rf   rO   rV   r   ZMSG_CHANNEL_DATAr   re   rY   r   r   r   Śtest_sendDatal  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   rP   ZsendExtendedDatar®   rf   rO   rV   r   ZMSG_CHANNEL_EXTENDED_DATAr   re   rY   r   r   r   Śtest_sendExtendedDatay  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   rP   ZsendEOFrf   rO   rV   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  }|  |” d|_| j |” |  |j” dS )zP
        Test that channel close messages are sent in the right format.
        r   TN)r   rZ   rP   r   r^   r®   rf   rO   rV   r   r   ZremoteClosedr    )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&   rw   N)	rP   Z
getChannelrf   r(   ry   r6   ZassertRaisesr   r:   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)rO   r7   r^   rP   rA   rf   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   rZ   rP   r£   ŚassertFailurer   r:   ZchannelClosedrÆ   r   r   r   Ś9test_channelClosedCausesLeftoverChannelDeferredsToErrbackĄ  s    
   ’zIConnectionTests.test_channelClosedCausesLeftoverChannelDeferredsToErrback))r	   r
   r   r   rJ   r   rO   rR   rZ   r\   r]   ra   rg   rq   rt   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   rK   É   sL   
	$	rK   c                   @   s6   e Zd ZdZesdZejdkr"dZdd Zdd Z	dS )	ŚCleanConnectionShutdownTestszL
    Check whether correct cleanup is performed on connection shutdown.
    rC   NrL   c                 C   s,   t  d ”| _t | j_t | _| j| j_d S rM   )r   rN   rO   r3   r7   rB   rP   r   r   r   r   rR   Ś  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æ   )rP   rQ   rk   rĄ   r   r:   r[   rŖ   r   r   r   Ś9test_serviceStoppedCausesLeftoverGlobalDeferredsToErrbackį  s    
  ’
zVCleanConnectionShutdownTests.test_serviceStoppedCausesLeftoverGlobalDeferredsToErrback)
r	   r
   r   r1   r   rJ   r   rO   rR   rĆ   r   r   r   r   rĀ   Š  s   
rĀ   )r1   Z
__future__r   r   rX   Ztwisted.python.reflectr   r   Ztwisted.conchr   Ztwisted.conch.sshr   r   r   Ztwisted.python.compatr   Ztwisted.trialr   Ztwisted.conch.testr   Z
SSHChannelr   r3   r   rB   ZTestCaserK   rĀ   r   r   r   r   Ś<module>   s*   Y(,    