
    Vh2                       d Z ddlmZ ddlmZ ddlmZ 	 ddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ dZddlmZ  G d de      Z	 	 d	 	 	 	 	 	 	 ddZ eee       G d de             Zy# e$ r dZeZY Bw xY w)z
Test ssh/channel.py.
    )annotations)skipIf)verifyObject)channel)SSHTransportAddress)
SSHService)SSHServerTransport)
interfaces)IPv4Address)StringTransport zConch SSH not supported.)TestCasec                  H    e Zd ZdZddZd	dZd
dZ	 	 	 	 	 	 	 	 ddZddZy)MockConnectiona  
    A mock for twisted.conch.ssh.connection.SSHConnection.  Record the data
    that channels send, and when they try to close the connection.

    @ivar data: a L{dict} mapping channel id #s to lists of data sent by that
        channel.
    @ivar extData: a L{dict} mapping channel id #s to lists of 2-tuples
        (extended data type, data) sent by that channel.
    @ivar closes: a L{dict} mapping channel id #s to True if that channel sent
        a close message.
    c                .    i | _         i | _        i | _        y )N)dataextDataclosesselfs    O/home/dcms/DCMS/lib/python3.12/site-packages/twisted/conch/test/test_channel.py__init__zMockConnection.__init__*   s    ;=	JL68    c                     y)z,
        Return our logging prefix.
        r    r   s    r   	logPrefixzMockConnection.logPrefix/   s      r   c                Z    | j                   j                  |g       j                  |       y)z'
        Record the sent data.
        N)r   
setdefaultappend)r   r   r   s      r   sendDatazMockConnection.sendData5   s#     			Wb)006r   c                ^    | j                   j                  |g       j                  ||f       y)z0
        Record the sent extended data.
        N)r   r   r   )r   r   typer   s       r   sendExtendedDatazMockConnection.sendExtendedData;   s'     	,33T4LAr   c                "    d| j                   |<   y)z?
        Record that the channel sent a close message.
        TN)r   )r   r   s     r   	sendClosezMockConnection.sendCloseC   s      $Gr   NreturnNone)r'   str)r   channel.SSHChannelr   bytesr'   r(   )r   r*   r"   intr   r+   r'   r(   )r   r*   r'   r(   )	__name__
__module____qualname____doc__r   r   r    r#   r%   r   r   r   r   r      sE    
9
 7B)B14B<AB	B$r   r   Nc                r    t               }|j                  t        ||             |j                  |        y)a  
    Connect a SSHTransport which is already connected to a remote peer to
    the channel under test.

    @param service: Service used over the connected transport.
    @type service: L{SSHService}

    @param hostAddress: Local address of the connected transport.
    @type hostAddress: L{interfaces.IAddress}

    @param peerAddress: Remote address of the connected transport.
    @type peerAddress: L{interfaces.IAddress}
    )hostAddresspeerAddressN)r	   makeConnectionr   
setService)servicer2   r3   	transports       r   connectSSHTransportr8   J   s4    $ #$IK[I !r   c                      e 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dZddZddZddZddZddZy)ChannelTestsz"
    Tests for L{SSHChannel}.
    c                    t               | _        t        j                  | j                  d      | _        d| j                  _        y)z
        Initialize the channel.  remoteMaxPacket is 10 so that data is able
        to be sent (the default of 0 means no data is sent because no packets
        are made).
        
   )connremoteMaxPackets   channelN)r   r=   r   
SSHChannelnamer   s    r   setUpzChannelTests.setUpi   s2     #$	))tyy"M&r   c                j    | j                  t        t        j                  | j                               y)zK
        L{SSHChannel} instances provide L{interfaces.ITransport}.
        N)
assertTruer   r
   
ITransportr   r   s    r   test_interfacezChannelTests.test_interfaces   s!     	Z%:%:DLLIJr   c           	     
   t        j                  | j                        }| j                  |j                  d       | j                  |j
                  d       | j                  |j                  d       | j                  |j                  d       | j                  |j                  d       | j                  |j                  | j                         | j                  |j                         | j                  |j                         t        j                  ddddd	d
d      }| j                  |j                  d       | j                  |j
                  d       | j                  |j                  d       | j                  |j                  d       | j                  |j                  d       | j                  |j                  d	       | j                  |j                  d
       | j                  |j                  d       y)a  
        Test that SSHChannel initializes correctly.  localWindowSize defaults
        to 131072 (2**17) and localMaxPacket to 32768 (2**15) as reasonable
        defaults (what OpenSSH uses for those variables).

        The values in the second set of assertions are meaningless; they serve
        only to verify that the instance variables are assigned in the correct
        order.
        )r=   i   i   r                        N)r   r?   r=   assertEquallocalWindowSizelocalWindowLeftlocalMaxPacketremoteWindowLeftr>   assertIsNoner   avatar)r   cc2s      r   	test_initzChannelTests.test_inity   s    DII.**F3**F3))51++Q/**A.+!&&!!((#1aAq!4++Q/++Q/**A.,,a0++Q/!$!$A&r   c                    | j                  t        | j                        d       | j                  t        t        j                  d            d       y)z{
        Test that str(SSHChannel) works gives the channel name and local and
        remote windows at a glance..
        z%<SSHChannel channel (lw 131072 rw 0)>rG   localWindowz<SSHChannel None (lw 1 rw 0)>N)rN   r)   r   r?   r   s    r   test_strzChannelTests.test_str   sB    
 	T\\*,ST""q124S	
r   c                    | j                  | j                  j                         d       | j                  t        j                  d      j                         d       y)z
        Test that bytes(SSHChannel) works, gives the channel name and
        local and remote windows at a glance..

        s%   <SSHChannel channel (lw 131072 rw 0)>rG   rY   s   <SSHChannel None (lw 1 rw 0)>N)rN   r   	__bytes__r?   r   s    r   
test_byteszChannelTests.test_bytes   sO     	LL""$&N	
 	1-779,	
r   c                X    | j                  | j                  j                         d       y)z
        Test that SSHChannel.logPrefix gives the name of the channel, the
        local channel ID and the underlying connection.
        z.SSHChannel channel (unknown) on MockConnectionN)rN   r   r   r   s    r   test_logPrefixzChannelTests.test_logPrefix   s%    
 	LL""$&V	
r   c                4   dgdfd}|| j                   _        | j                   j                  d       | j                   j                  dd       | j                   j	                  d       | j                  | j                   j                  d       | j                  | j                   j                         | j                  d          | j                  | j                   j                  d       | j                  | j                  j                  | j                      dg       | j                  | j                   j                  g        | j                  | j                  j                  | j                      d	g       dd<   | j                   j	                  d
       | j                  d          | j                   j                  d       | j                   j                          | j                   j	                  d
       | j                  d          y)zq
        Test that addWindowBytes adds bytes to the window and resumes writing
        if it was paused.
        Fc                     d d<   y NTr   r   cbs   r   stubStartWritingz:ChannelTests.test_addWindowBytes.<locals>.stubStartWriting       BqEr      testrG   2   *   r   r   )rG   rh      sP   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaNr&   )r   startWritingwritewriteExtendedaddWindowBytesrN   rR   rC   
areWritingbufr=   r   extBufr   assertFalseloseConnection)r   rf   re   s     @r   test_addWindowBytesz ChannelTests.test_addWindowBytes   s   
 W	 %5!7#""1g.##B'66
C//01))3/5yA,,b1**4<<8<.I1##B'A9%##%##B'Ar   c                *   d | j                   _        | j                  | j                   j                  dd             | j	                  | j                   j                  dd             | j	                  | j                   j                  dd             y)zn
        Test that requestReceived handles requests by dispatching them to
        request_* methods.
        c                    | dk(  S )Nr   r   )r   s    r   <lambda>z3ChannelTests.test_requestReceived.<locals>.<lambda>   s
     r   s   test-methodr      as
   bad-methodN)r   request_test_methodrC   requestReceivedrs   r   s    r   test_requestReceivedz!ChannelTests.test_requestReceived   sl    
 ,D(44^SIJ55ndKL55mSIJr   c                    | j                  | j                  j                         | j                  j                          | j	                  | j                  j                         y)zM
        Test that the default closeReceieved closes the connection.
        N)rs   r   closingcloseReceivedrC   r   s    r   test_closeReceievedz ChannelTests.test_closeReceieved   sB     	--.""$,,-r   c                Z   dgdfd}|| j                   _        | j                   j                  d       | j                   j                  d       | j                  | j                   j                         | j                  d          | j                   j                  d       | j                   j                  d       | j                  j                  | j                      }| j                  |ddg       | j                  | j                   j                  d	       | j                   j                  d
       | j                  |g d       | j                  | j                   j                  d       dd<   | j                   j                  d       | j                  | j                   j                         | j                  d          | j                  |g d       | j                  | j                   j                  d       | j                  | j                   j                  d       y)z
        Test that write handles data correctly.  Send data up to the size
        of the remote window, splitting the data into packets of length
        remoteMaxPacket.
        Fc                     d d<   y rc   r   rd   s   r   stubStopWritingz0ChannelTests.test_write.<locals>.stubStopWriting   rg   r      dry   r   rk      ta   da      12345678901)r   r   
   1234567890   1rK      123456)r   r   r   r      12345   6Nr&   )r   stopWritingrm   rs   rp   rC   ro   r=   r   rN   rR   rq   r   r   r   re   s      @r   
test_writezChannelTests.test_write   s    W	 $3 4 4 0011##B'5!yy~~dll+u~.66;>*BC66:19%0011LM))4066:r   c                   dgdfd}|| j                   _        | j                   j                  dd       | j                   j                  dd       | j                   j                  dd       | j                  | j                   j                         | j                  d          | j                   j                  d	       | j                   j                  dd       | j                  j                  | j                      }| j                  |g d
       | j                  | j                   j                  d       | j                   j                  dd       | j                  |g d       | j                  | j                   j                  d       dd<   | j                   j                  dd       | j                  | j                   j                         | j                  d          | j                  |g d       | j                  | j                   j                  ddgg       | j                  | j                   j                  d       y)z
        Test that writeExtended handles data correctly.  Send extended data
        up to the size of the window, splitting the extended data into packets
        of length remoteMaxPacket.
        Fc                     d d<   y rc   r   rd   s   r   r   z8ChannelTests.test_writeExtended.<locals>.stubStopWriting  rg   r   rG   r   ry   rH      tr   rk   )rG   r   rH   r   rH   ry   r   rI   r   )r   r   r   rI   r   rI   r   rK   rJ   r   )r   r   r   r   r   )rJ   r   r   Nr&   )r   r   rn   rs   rp   rC   ro   r=   r   rN   rR   rr   r   s      @r   test_writeExtendedzChannelTests.test_writeExtended  s    W	 $3 ""1d+""1d+""1d+0011##B'""1d+yy  .AB66;""1n5S	
 	66:1""1i00011
	
 	,,4yk:66:r   c                    | j                   j                  d       | j                   j                  d t        d      D               | j	                  | j
                  j                  | j                      dg       y)zS
        Test that writeSequence is equivalent to write(''.join(sequece)).
        rk   c              3  (   K   | ]
  }d |fz    yw)s   %dNr   ).0is     r   	<genexpr>z2ChannelTests.test_writeSequence.<locals>.<genexpr>C  s     "CA5A4<"Cs   r<   s
   0123456789N)r   ro   writeSequencerangerN   r=   r   r   s    r   test_writeSequencezChannelTests.test_writeSequence>  sU     	##B'"""Cr"CC5Gr   c                   | j                   j                  d       | j                   j                  dd       | j                   j                          | j	                  | j
                  j                  j                  | j                                | j                   j                  d       | j	                  | j
                  j                  j                  | j                                | j                   j                  d       | j                  | j
                  j                  j                  | j                                y)zk
        Tesyt that loseConnection() doesn't close the channel until all
        the data is sent.
        s   datarG   s   datadatarJ      N)
r   rm   rn   rt   rS   r=   r   getro   rC   r   s    r   test_loseConnectionz ChannelTests.test_loseConnectionF  s    
 	7#""1k2##%$))**..t||<=##A&$))**..t||<=##A&		((,,T\\:;r   c                    t        ddd      }t        | j                  j                  |       | j	                  t        |      | j                  j                                y)z
        L{SSHChannel.getPeer} returns the same object as the underlying
        transport's C{getPeer} method returns.
        TCPz192.168.0.1i1  )r6   r3   N)r   r8   r   r=   rN   r   getPeer)r   peers     r   test_getPeerzChannelTests.test_getPeerT  sI    
 5-7DLL$5$54H,T2DLL4H4H4JKr   c                    t        ddd      }t        | j                  j                  |       | j	                  t        |      | j                  j                                y)z
        L{SSHChannel.getHost} returns the same object as the underlying
        transport's C{getHost} method returns.
        r   z	127.0.0.1i90  )r6   r2   N)r   r8   r   r=   rN   r   getHost)r   hosts     r   test_getHostzChannelTests.test_getHost^  sI    
 5+u5DLL$5$54H,T2DLL4H4H4JKr   Nr&   )r-   r.   r/   r0   rA   rE   rW   r[   r^   r`   ru   r|   r   r   r   r   r   r   r   r   r   r   r:   r:   c   sY    'K'<


 >K.";H/;bH<LLr   r:   )NN)r6   r   r2   interfaces.IAddress | Noner3   r   r'   r(   )r0   
__future__r   unittestr   zope.interface.verifyr   twisted.conch.sshr   twisted.conch.ssh.addressr   twisted.conch.ssh.servicer   twisted.conch.ssh.transportr	   twisted.internetr
   twisted.internet.addressr   twisted.internet.testingr   skipTestImportErrorobjecttwisted.trial.unittestr   r   r8   r:   r   r   r   <module>r      s    #  .)=4>+48H ,*$Z *$^ /3.2""+" ," 
	"2 (BL8 BL BL[  )HJs   ,A9 9	BB