
    VhHr                     B   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
r	dd	lmZmZ n
 G d
 d      Z G d dej                        Z G d d      Z G d dej$                        Z G d de	j(                        Z G d de	j(                        Zy)z1
This module tests twisted.conch.ssh.connection.
    N)channel)test_userauth)requireModule)unittestcryptography)error)common
connectionc                        e Zd Z G d d      Zy)r
   c                       e Zd Zy)connection.SSHConnectionN)__name__
__module____qualname__     R/home/dcms/DCMS/lib/python3.12/site-packages/twisted/conch/test/test_connection.pySSHConnectionr      s    r   r   N)r   r   r   r   r   r   r   r
   r
      s    	 	r   r
   c                   R    e Zd ZdZdZdZdZd Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zy)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                      d| j                   z  S )NzTestChannel %i)idselfs    r   	logPrefixzTestChannel.logPrefix:   s    $''))r   c                 t    d| _         || _        g | _        g | _        d| _        d| _        d| _        d| _        y)zF
        The channel is open.  Set up the instance variables.
        Tr   FN)gotOpenspecificDatainBuffer	extBuffernumberRequestsgotEOFgotOneClose	gotClosed)r   r   s     r   channelOpenzTestChannel.channelOpen=   s?     ( r   c                     || _         y)zD
        Opening the channel failed.  Store the reason why.
        N)openFailureReason)r   reasons     r   
openFailedzTestChannel.openFailedJ   s     "(r   c                 6    | xj                   dz  c_         |dk(  S )z_
        A test request.  Return True if data is 'data'.

        @type data: L{bytes}
              data)r"   r   datas     r   request_testzTestChannel.request_testP   s     	q wr   c                 :    | j                   j                  |       y)z=
        Data was received.  Store it in the buffer.
        N)r    appendr.   s     r   dataReceivedzTestChannel.dataReceivedY   s     	T"r   c                 >    | j                   j                  ||f       y)zF
        Extended data was received.  Store it in the buffer.
        N)r!   r2   )r   coder/   s      r   extReceivedzTestChannel.extReceived_   s     	tTl+r   c                     d| _         y)z1
        EOF was received.  Remember it.
        TN)r#   r   s    r   eofReceivedzTestChannel.eofReceivede   s     r   c                     d| _         y)z3
        Close was received.  Remember it.
        TN)r$   r   s    r   closeReceivedzTestChannel.closeReceivedk   s      r   c                     d| _         y)z7
        The channel is closed.  Rembember it.
        TN)r%   r   s    r   closedzTestChannel.closedq   s     r   N)r   r   r   __doc__namer   r%   r   r&   r*   r0   r3   r6   r8   r:   r<   r   r   r   r   r      sE    2 DGI*(#, r   r   c                        e Zd ZdZdZd Zd Zy)
TestAvatarz?
    A mocked-up version of twisted.conch.avatar.ConchUser
    {   c                     |t         j                  k(  rt        ||||       S |dk(  r t        j                  | j                  d      y)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   channelType
windowSize	maxPacketr/   s        r   lookupChannelzTestAvatar.lookupChannel   sW    
 +***' )	  //
 ""4#8#8:UVV 0r   c                 "    |dk(  ry|dk(  rd|fS y)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/   s      r   gotGlobalRequestzTestAvatar.gotGlobalRequest   s$     -'K':r   N)r   r   r   r=   rI   rM   rR   r   r   r   r@   r@   x   s     W&r   r@   c                   6    e Zd ZdZesdZd Zd Zd Zd Z	d Z
y)	TestConnectionz}
    A subclass of SSHConnection for testing.

    @ivar channel: the current channel.
    @type channel. C{TestChannel}
    Cannot run without cryptographyc                      y)NrT   r   r   s    r   r   zTestConnection.logPrefix   s    r   c                      y)zT
        The other side made the 'TestGlobal' global request.  Return True.
        Tr   r.   s     r   global_TestGlobalz TestConnection.global_TestGlobal   s     r   c                 
    d|fS )zt
        The other side made the 'Test-Data' global request.  Return True and
        the data we received.
        Tr   r.   s     r   global_Test_DatazTestConnection.global_Test_Data   s    
 Tzr   c                 @    t        |||      | _        | j                  S )z
        The other side is requesting the TestChannel.  Create a C{TestChannel}
        instance, store it, and return it.
        )rC   rD   r/   )r   r   r   rK   rL   r/   s       r   channel_TestChannelz"TestConnection.channel_TestChannel   s#    
 ##YT
 ||r   c                     t        d      )zU
        The other side is requesting the ErrorChannel.  Raise an exception.
        zno such thing)AssertionErrorr\   s       r   channel_ErrorChannelz#TestConnection.channel_ErrorChannel   s     _--r   N)r   r   r   r=   r   skipr   rX   rZ   r]   r`   r   r   r   rT   rT      s*     0 .r   rT   c                       e Zd ZesdZd Zd Zd Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d  Z#d! Z$d" Z%y#)$ConnectionTestsrU   c                     t        j                  d       | _        t               | j                  _        t               | _        | j                  | j                  _        | j                  j                          y N)r   FakeTransport	transportr@   rE   rT   connserviceStartedr   s    r   setUpzConnectionTests.setUp   sL    &44T: *"$	"nn				  "r   c                    | j                   j                  |       | j                  j                  dd | j                  _        | j                   j	                  t        j                  d|j                  d      dz          y)z?
        Open the channel with the default connection.
        Nz>2L   s         )rh   openChannelrg   packetsssh_CHANNEL_OPEN_CONFIRMATIONstructpackr   r   r   s     r   _openChannelzConnectionTests._openChannel   s_     			g&!%!7!7!<		//KKwzz3/2UU	
r   c                 8    | j                   j                          y re   )rh   serviceStoppedr   s    r   tearDownzConnectionTests.tearDown   s    		  "r   c                 x    | j                  | j                  j                  j                  | j                         y)z_
        Test that the connection links itself to the avatar in the
        transport.
        N)assertIsrg   rE   rh   r   s    r   test_linkAvatarzConnectionTests.test_linkAvatar   s&    
 	dnn++00$))<r   c                    t               }t               }| j                  j                  |       | j                  j                  |       | j                  j                  d       | j	                  |j
                         | j                  |j                         | j                  |j
                         | j                  |j                         | j                  j                          | j	                  |j                         | j                  |j
                         | j                  |j                         ddl	m
} | j                  |j                  |       y)zF
        Test that serviceStopped() closes any open channels.
        s                   r   )ConnectionLostN)r   rh   rn   rp   
assertTruer   assertFalser%   rv   twisted.internet.errorr|   assertIsInstancer(   )r   channel1channel2r|   s       r   test_serviceStoppedz#ConnectionTests.test_serviceStopped   s     ==		h'		h'		//0GH(()++,))*++,		  "**+))*++,9h88.Ir   c                    | j                   j                  t        j                  d      dz          | j	                  | j
                  j                  t        j                  dfg       g | j
                  _        | j                   j                  t        j                  d      dz   dz          | j	                  | j
                  j                  t        j                  dfg       g | j
                  _        | j                   j                  t        j                  d      dz          | j	                  | j
                  j                  t        j                  dfg       g | j
                  _        | j                   j                  t        j                  d      dz          | j	                  | j
                  j                  g        y)	z
        Test that global request packets are dispatched to the global_*
        methods and the return values are translated into success or failure
        messages.
        rO      r   rP   s	   test datas   TestBad    N)
rh   ssh_GLOBAL_REQUESTr	   NSassertEqualrg   ro   r
   MSG_REQUEST_SUCCESSMSG_REQUEST_FAILUREr   s    r   test_GLOBAL_REQUESTz#ConnectionTests.test_GLOBAL_REQUEST  sN    			$$VYY}%=%GHNN""j&D&Dc%J$K	
 "$		$$VYY{%;g%E%TUNN""j&D&Dl%S$T	
 "$		$$VYYz%:W%DENN""j&D&Dc%J$K	
 "$		$$VYY}%=%GH//4r   c                       j                   j                  ddd      } j                   j                  d        fd}|j                  |       |j	                   j
                         |S )zh
        Test that global request success packets cause the Deferred to be
        called back.
           requestr-   Tc                 *    j                  | d       y Nr-   )r   )r/   r   s    r   checkz3ConnectionTests.test_REQUEST_SUCCESS.<locals>.check!  s    T7+r   )rh   sendGlobalRequestssh_REQUEST_SUCCESSaddCallback
addErrbackfailr   dr   s   `  r   test_REQUEST_SUCCESSz$ConnectionTests.test_REQUEST_SUCCESS  sW    
 II''
GTB		%%g.	, 	
e	TYYr   c                       j                   j                  ddd      } j                   j                  d        fd}|j                   j                         |j                  |       |S )zg
        Test that global request failure packets cause the Deferred to be
        erred back.
        r   r-   Tc                 R    j                  | j                  j                  d       y r   )r   valuer/   )fr   s    r   r   z3ConnectionTests.test_REQUEST_FAILURE.<locals>.check0  s    QWW\\73r   )rh   r   ssh_REQUEST_FAILUREr   r   r   r   s   `  r   test_REQUEST_FAILUREz$ConnectionTests.test_REQUEST_FAILURE(  sW    
 II''
GTB		%%g.	4 	
dii 	Ur   c                    | j                   `| j                  j                  t	        j
                  d      dz          | j                  | j                  j                  j                         | j                  | j                  j                  j                  | j                         | j                  | j                  j                  j                  d       | j                  | j                  j                  j                  d       | j                  | j                  j                  j                  d       | j                  | j                  j                  j                  d       | j                  | j                   j                  t        j                   dfg       g | j                   _        | j                  j                  t	        j
                  d      dz          | j#                          | j                  | j                   j                  t        j$                  dt	        j
                  d	      z   t	        j
                  d
      z   fg       g | j                   _        | j                  j                  t	        j
                  d      dz          | j#                          | j                  | j                   j                  t        j$                  dt	        j
                  d      z   t	        j
                  d
      z   fg       y)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 channelr   s   ErrorChannels         s   unknown failureN)rg   rE   rh   ssh_CHANNEL_OPENr	   r   r}   r   r   r   r/   r   remoteWindowLeftrD   ro   r
   MSG_CHANNEL_OPEN_CONFIRMATIONflushLoggedErrorsMSG_CHANNEL_OPEN_FAILUREr   s    r   test_CHANNEL_OPENz!ConnectionTests.test_CHANNEL_OPEN7  sF   
 NN!		""699^#<?V#VW		))112**//;**//1DE**779LM**;;Q?**::A>NN"" <<(		
 "$		""699]#;>U#UV NN"" 777ii 234iin%
	
 "$		""699_#=@W#WX NN"" 777ii 234iin%
	
r   c                 0   || j                   j                  _        | j                  j	                  t        j                  d      dz          | j                  t        j                        }| j                  t        |      dd|       | j                  |d   j                  j                  d       | j                  | j                   j                  t        j                   dt        j                  d      z   t        j                  d	      z   fg       y
)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.
        rF   r   r,   zExpected one error, got: r   )rA   rG   s         {s   error args in wrong orderr   N)rg   rE   rI   rh   r   r	   r   r   r   rH   r   lenr   argsro   r
   r   )r   r5   errorss      r   _lookupChannelErrorTestz'ConnectionTests._lookupChannelErrorTestk  s     26.		""II)*-DD	
 ''(8(89Va+DVJ)OP--/QRNN"" 77 8ii <=>iin%
	
r   c                 &    | j                  d       y)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.
        rA   N)r   r   s    r   test_lookupChannelErrorz'ConnectionTests.test_lookupChannelError  s     	$$S)r   c                    t               }| j                  j                  |       | j                  j                  d       | j	                  |j
                  d       | j	                  |j                  d       | j	                  |j                  d       | j	                  | j                  j                  |   d       | j	                  | j                  j                  d   d       y)zv
        Test that channel open confirmation packets cause the channel to be
        notified that it's open.
        s                       r          N)
r   rh   rn   rp   r   r   rD   r   channelsToRemoteChannellocalToRemoteChannelrs   s     r   test_CHANNEL_OPEN_CONFIRMATIONz.ConnectionTests.test_CHANNEL_OPEN_CONFIRMATION  s    
 -		g&		//0GH111500!4--/BC::7CQG77:A>r   c                 d   t               }| j                  j                  |       | j                  j                  dt	        j
                  d      z          | j                  |j                  j                  d       | j                  | j                  j                  j                  |             y)zz
        Test that channel open failure packets cause the channel to be
        notified that its opening failed.
                     failure!)r   r,   N)r   rh   rn   ssh_CHANNEL_OPEN_FAILUREr	   r   r   r(   r   assertIsNonechannelsgetrs   s     r   test_CHANNEL_OPEN_FAILUREz)ConnectionTests.test_CHANNEL_OPEN_FAILURE  s    
 -		g&		**3fii6LL	
 	22779IJ$)),,009:r   c                     t               }| j                  |       |j                  }| j                  j	                  d       | j                  |j                  |dz          y)zc
        Test that channel window adjust messages add bytes to the channel
        window.
        r   r,   N)r   rt   r   rh   ssh_CHANNEL_WINDOW_ADJUSTr   )r   r   oldWindowSizes      r   test_CHANNEL_WINDOW_ADJUSTz*ConnectionTests.test_CHANNEL_WINDOW_ADJUST  sR    
 -'"00		++,ST11=13DEr   c                    t        dd      }| j                  |       | j                  j                  dt	        j
                  d      z          | j                  |j                  dg       | j                  | j                  j                  t        j                  dfg       g | j                  _	        d|j                  dz   z  }| j                  j                  dt	        j
                  |      z          | j                  |j                  dg       | j                  | j                  j                  t        j                  d	fg       t               }| j                  |       d|j                  dz   z  }g | j                  _	        | j                  j                  d
t	        j
                  |      z          | j                  |j                  g        | j                  | j                  j                  t        j                  d	fg       y)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   rt   rh   ssh_CHANNEL_DATAr	   r   r   r    rg   ro   r
   MSG_CHANNEL_WINDOW_ADJUSTlocalWindowLeftMSG_CHANNEL_CLOSEr   r   r   longDatabigDatas       r   test_CHANNEL_DATAz!ConnectionTests.test_CHANNEL_DATA  s   
 !A>'"		""#679K#KL))G95NN"" 88;	
 "$722Q67		""#689L#LM))G95NN""**,?@A	
 -'"'00145!#		""#679K#KL))2.NN""**,?@A	
r   c                    t        dd      }| j                  |       | j                  j                  dt	        j
                  d      z          | j                  |j                  dg       | j                  | j                  j                  t        j                  dfg       g | j                  _	        d|j                  d	z   z  }| j                  j                  dt	        j
                  |      z          | j                  |j                  dg       | j                  | j                  j                  t        j                  d
fg       t               }| j                  |       d|j                  d	z   z  }g | j                  _	        | j                  j                  dt	        j
                  |      z          | j                  |j                  g        | j                  | j                  j                  t        j                  d
fg       y)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   rt   rh   ssh_CHANNEL_EXTENDED_DATAr	   r   r   r!   rg   ro   r
   r   r   r   r   r   s       r   test_CHANNEL_EXTENDED_DATAz*ConnectionTests.test_CHANNEL_EXTENDED_DATA  s   
 !A>'"		++3fii6HH	
 	**\N;NN"" 88;	
 "$722Q67		++3fii6II	
 	**\N;NN""**,?@A	
 -'"'00145!#		++3fii6HH	
 	**B/NN""**,?@A	
r   c                     t               }| j                  |       | j                  j                  d       | j	                  |j
                         y)zN
        Test that channel eof messages are passed up to the channel.
        r   N)r   rt   rh   ssh_CHANNEL_EOFr}   r#   rs   s     r   test_CHANNEL_EOFz ConnectionTests.test_CHANNEL_EOF
  s<     -'"		!!"56'r   c                    t               }| j                  |       | j                  |j                         | j	                  |j
                         | j	                  |j                         | j                  j                  |       | j                  j                  d       | j                  |j
                         | j                  |j                         y)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   rt   r}   r   r~   r$   r%   rh   	sendClosessh_CHANNEL_CLOSErs   s     r   test_CHANNEL_CLOSEz"ConnectionTests.test_CHANNEL_CLOSE  s     -'"(,,-**+		G$		##$78++,))*r   c                 x    t               } j                  |        j                  j                  dt	        j
                  d      z   dz           j                  |j                  d        j                  j                  dt	        j
                  d      z   dz   dz         } fd}|j                  |       |S )zS
        Test that channel requests that succeed send MSG_CHANNEL_SUCCESS.
        r      testr   r,   r   r-   c                 t    j                  j                  j                  t        j                  dfg       y Nr   )r   rg   ro   r
   MSG_CHANNEL_SUCCESSresultr   s    r   r   z;ConnectionTests.test_CHANNEL_REQUEST_success.<locals>.check1  0    &&002EFGr   )	r   rt   rh   ssh_CHANNEL_REQUESTr	   r   r   r"   r   r   r   r   r   s   `   r   test_CHANNEL_REQUEST_successz,ConnectionTests.test_CHANNEL_REQUEST_success#  s     -'"		%%&))G"44w>	
 	//3II))&))G"44w>H
	 	
er   c                     t               } j                  |        j                  j                  dt	        j
                  d      z   dz         } fd}|j                   j                         |j                  |       |S )zP
        Test that channel requests that fail send MSG_CHANNEL_FAILURE.
        r   r   r   c                 t    j                  j                  j                  t        j                  dfg       y r   )r   rg   ro   r
   MSG_CHANNEL_FAILUREr   s    r   r   z;ConnectionTests.test_CHANNEL_REQUEST_failure.<locals>.checkD  r   r   )	r   rt   rh   r   r	   r   r   r   r   r   s   `   r   test_CHANNEL_REQUEST_failurez,ConnectionTests.test_CHANNEL_REQUEST_failure:  sl     -'"II))&))G"44w>
	 	
dii 	Ur   c                      t               } j                  |        j                  j                  |ddd      } j                  j	                  d        fd}|S )zj
        Test that channel request success messages cause the Deferred to be
        called back.
        r   r-   Tr   c                 (    j                  |        y re   )r}   r   s    r   r   z;ConnectionTests.test_CHANNEL_REQUEST_SUCCESS.<locals>.checkX  s    OOF#r   )r   rt   rh   sendRequestssh_CHANNEL_SUCCESSr   s   `   r   test_CHANNEL_REQUEST_SUCCESSz,ConnectionTests.test_CHANNEL_REQUEST_SUCCESSN  sS    
 -'"II!!'7GTB		%%&9:	$ r   c                     t               } j                  |        j                  j                  |ddd      } j                  j	                  d        fd}|j                   j                         |j                  |       |S )zi
        Test that channel request failure messages cause the Deferred to be
        erred back.
        r   r   Tr   c                 R    j                  | j                  j                  d       y )Nzchannel request failed)r   r   r   s    r   r   z;ConnectionTests.test_CHANNEL_REQUEST_FAILURE.<locals>.checkg  s    V\\//1IJr   )r   rt   rh   r   ssh_CHANNEL_FAILUREr   r   r   r   s   `   r   test_CHANNEL_REQUEST_FAILUREz,ConnectionTests.test_CHANNEL_REQUEST_FAILURE]  sp    
 -'"II!!'7C>		%%&9:	K 	
dii 	Ur   c                    | j                   j                  ddd      }|j                  d        | j                   j                  ddd       | j                  | j                  j
                  t        j                  t        j                  d      dz   ft        j                  t        j                  d      d	z   fg       | j                  | j                   j                  d
|gi       y)zQ
        Test that global request messages are sent in the right format.
        s	   wantReplyr-   Tc                      y re   r   failures    r   <lambda>z8ConnectionTests.test_sendGlobalRequest.<locals>.<lambda>t      r   s   noReplyr   Fs   datar   globalN)rh   r   r   r   rg   ro   r
   MSG_GLOBAL_REQUESTr	   r   	deferredsr   r   s     r   test_sendGlobalRequestz&ConnectionTests.test_sendGlobalRequestn  s     II''gtD	)*		##JU;NN""..		,0G+0UV..		*0E0OP	
 	,,x!o>r   c                 n   t               }| j                  j                  |d       | j                  | j                  j
                  t        j                  t        j                  d      dz   fg       | j                  |j                  d       | j                  | j                  j                  d       y)zO
        Test that open channel messages are sent in the right format.
        s   aaaar   s             aaaar   r,   N)r   rh   rn   r   rg   ro   r
   MSG_CHANNEL_OPENr	   r   r   localChannelIDrs   s     r   test_openChannelz ConnectionTests.test_openChannel  s     -		gw/NN"" //IIn-MN		
 	Q'1115r   c           	      h   t               }| j                  |       | j                  j                  |ddd      }|j	                  d        | j                  j                  |ddd       d|_        | j                  j                  |ddd       | j                  | j                  j                  t        j                  dt        j                  d      z   d	z   ft        j                  dt        j                  d      z   d
z   fg       | j                  | j                  j                  d   |g       y)zR
        Test that channel request messages are sent in the right format.
        r   Tc                      y re   r   r   s    r   r   z2ConnectionTests.test_sendRequest.<locals>.<lambda>  r   r      test2r   Fs   test3r   s   testr   r   N)r   rt   rh   r   r   localClosedr   rg   ro   r
   MSG_CHANNEL_REQUESTr	   r   r   r   r   r   s      r   test_sendRequestz ConnectionTests.test_sendRequest  s    -'"II!!'7GTB	)*		gxe<"		gxd;NN"" 22'&))G*<<{J
 22'&))H*==G		
 	,,Q/!5r   c                    t        d      }| j                  |       d|_        | j                  j	                  |d       | j                  |j                  d       d|_        | j                  j	                  |d       | j                  |j                  d       | j                  | j                  j                  t        j                  dfg       y)	zi
        Test that channel window adjust messages cause bytes to be added
        to the window.
        r   )r   r   r,   T            N)r   rt   r   rh   adjustWindowr   r  rg   ro   r
   r   rs   s     r   test_adjustWindowz!ConnectionTests.test_adjustWindow  s    
 !,'""#		w*00!4"		w*00!4NN"" 88;	
r   c                 R   t               }| j                  |       | j                  j                  |d       d|_        | j                  j                  |d       | j                  | j                  j                  t        j                  dt        j                  d      z   fg       y)zO
        Test that channel data messages are sent in the right format.
        r   T   br   N)r   rt   rh   sendDatar  r   rg   ro   r
   MSG_CHANNEL_DATAr	   r   rs   s     r   test_sendDatazConnectionTests.test_sendData  s     -'"		7D)"		7D)NN""))+>4+PQR	
r   c                 V   t               }| j                  |       | j                  j                  |dd       d|_        | j                  j                  |dd       | j                  | j                  j                  t        j                  dt        j                  d      z   fg       y)zX
        Test that channel extended data messages are sent in the right format.
        r,   r   Tr  r  r  N)r   rt   rh   sendExtendedDatar  r   rg   ro   r
   MSG_CHANNEL_EXTENDED_DATAr	   r   rs   s     r   test_sendExtendedDataz%ConnectionTests.test_sendExtendedData  s     -'"		""7Aw7"		""7Ax8NN"" 88=		'@RR	
r   c                    t               }| j                  |       | j                  j                  |       | j	                  | j
                  j                  t        j                  dfg       d|_	        | j                  j                  |       | j	                  | j
                  j                  t        j                  dfg       y)zN
        Test that channel EOF messages are sent in the right format.
        r   TN)
r   rt   rh   sendEOFr   rg   ro   r
   MSG_CHANNEL_EOFr  rs   s     r   test_sendEOFzConnectionTests.test_sendEOF  s     -'"		'"NN""j&@&@BU%V$W	
 #		'"NN""j&@&@BU%V$W	
r   c                    t               }| j                  |       | j                  j                  |       | j	                  |j
                         | j                  | j                  j                  t        j                  dfg       | j                  j                  |       | j                  | j                  j                  t        j                  dfg       t               }| j                  |       | j	                  |j                         | j                  |j                         d|_        | j                  j                  |       | j	                  |j                         y)zP
        Test that channel close messages are sent in the right format.
        r   TN)r   rt   rh   r   r}   r  r   rg   ro   r
   r   r   r~   r%   remoteClosed)r   r   r   s      r   test_sendClosezConnectionTests.test_sendClose  s    -'"		G$++,NN""**,?@A	
 			G$NN""**,?@A	

 =(#(()++, $		H%**+r   c                 X   | j                   j                  dddd      }| j                  |j                  d       | j                  |j                  d       | j                  |j
                  d       | j                  t        j                  | j                   j                  dddd       y)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)	rh   
getChannelr   r/   r   rD   assertRaisesr   rH   rs   s     r   test_getChannelWithAvatarz)ConnectionTests.test_getChannelWithAvatar  s     ))&&~r2wGw/112600"5dii22M2r7	
r   c                     | j                   `| j                  | j                  j	                  dd             | j                  | j                  j	                  dd      d       | j                  | j                  j	                  dd             y)zW
        Test that gotGlobalRequests dispatches to global_* without an avatar.
        rO   r-   s	   Test-Data)Tr-   s	   BadGlobalN)rg   rE   r}   rh   rR   r   r~   r   s    r   "test_gotGlobalRequestWithoutAvatarz2ConnectionTests.test_gotGlobalRequestWithoutAvatar  sp     NN!		22='JKII&&|W=	
 	33L'JKr   c                     t               }| j                  |       | j                  j                  |ddd      }| j	                  |t
        j                        }| 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   rt   rh   r   assertFailurer   rH   channelClosedr	  s      r   9test_channelClosedCausesLeftoverChannelDeferredsToErrbackzIConnectionTests.test_channelClosedCausesLeftoverChannelDeferredsToErrback(  sf    
 -'"II!!'?LTU!Vq%"2"23		(r   N)&r   r   r   r   ra   rj   rt   rw   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'  r/  r   r   r   rc   rc      s    0#
#=J*502
h
8*?;	F#
J)
V(+ .("?"6&66
.

&
 ,4
	Lr   rc   c                   $    e Zd ZdZesdZd Zd Zy)CleanConnectionShutdownTestszL
    Check whether correct cleanup is performed on connection shutdown.
    rU   c                     t        j                  d       | _        t               | j                  _        t               | _        | j                  | j                  _        y re   )r   rf   rg   r@   rE   rT   rh   r   s    r   rj   z"CleanConnectionShutdownTests.setUp>  s<    &44T: *"$	"nn		r   c                     | j                   j                          | j                   j                  ddd      }| j                  |t        j
                        }| 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+  )rh   ri   r   r-  r   rH   rv   r   s     r   9test_serviceStoppedCausesLeftoverGlobalDeferredsToErrbackzVCleanConnectionShutdownTests.test_serviceStoppedCausesLeftoverGlobalDeferredsToErrbackD  s]    
 			  "II''QR'Sq%"2"23		  "r   N)r   r   r   r=   r   ra   rj   r4  r   r   r   r1  r1  6  s     0-
r   r1  )r=   rq   twisted.conch.sshr   twisted.conch.testr   twisted.python.reflectr   twisted.trialr   r   twisted.conchr   r	   r
   
SSHChannelr   r@   r   rT   TestCaserc   r1  r   r   r   <module>r<     s     % , 0 "^, 44 
Y'$$ Yx& &R).Z-- ).Xf	h'' f	R8#4#4 r   