
    tf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n# e$ r dZeZY nw xY wddlmZ  G d de          Z	 	 dddZ eee           G d de                      ZdS )z
Test ssh/channel.py.
    )annotations)skipIf)verifyObject)channel)SSHTransportAddress)
SSHService)SSHServerTransport)
interfaces)IPv4Address)StringTransport zConch SSH not supported.)TestCasec                  :    e Zd ZdZddZddZddZddZddZdS )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.
    returnNonec                0    i | _         i | _        i | _        d S )N)dataextDataclosesselfs    ^/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/twisted/conch/test/test_channel.py__init__zMockConnection.__init__*   s    ;=	JL68    strc                    dS )z,
        Return our logging prefix.
        r    r   s    r   	logPrefixzMockConnection.logPrefix/   s
      r   r   channel.SSHChannelr   bytesc                b    | j                             |g                               |           dS )z'
        Record the sent data.
        N)r   
setdefaultappend)r   r   r   s      r   sendDatazMockConnection.sendData5   s0     		Wb))0066666r   typeintc                f    | j                             |g                               ||f           dS )z0
        Record the sent extended data.
        N)r   r#   r$   )r   r   r&   r   s       r   sendExtendedDatazMockConnection.sendExtendedData;   s4     	,,33T4LAAAAAr   c                    d| j         |<   dS )z?
        Record that the channel sent a close message.
        TN)r   )r   r   s     r   	sendClosezMockConnection.sendCloseC   s      $Gr   Nr   r   )r   r   )r   r    r   r!   r   r   )r   r    r&   r'   r   r!   r   r   )r   r    r   r   )	__name__
__module____qualname____doc__r   r   r%   r)   r+   r   r   r   r   r      s        
 
9 9 9 9
       7 7 7 7B B B B$ $ $ $ $ $r   r   Nservicer   hostAddressinterfaces.IAddress | NonepeerAddressr   r   c                    t                      }|                    t          ||                     |                    |            dS )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}
    )r2   r4   N)r	   makeConnectionr   
setService)r1   r2   r4   	transports       r   connectSSHTransportr9   J   sS    $ #$$IK[II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dS )ChannelTestsz"
    Tests for L{SSHChannel}.
    r   r   c                    t                      | _        t          j        | j        d          | _        d| j        _        dS )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   s:     #$$	)ty"MMM&r   c                j    |                      t          t          j        | j                             dS )zK
        L{SSHChannel} instances provide L{interfaces.ITransport}.
        N)
assertTruer   r
   
ITransportr   r   s    r   test_interfacezChannelTests.test_interfaces   s+     	Z%:DLIIJJJJJr   c           	        t          j        | j                  }|                     |j        d           |                     |j        d           |                     |j        d           |                     |j        d           |                     |j        d           |                     |j        | j                   | 	                    |j
                   | 	                    |j                   t          j        ddddd	d
d          }|                     |j        d           |                     |j        d           |                     |j        d           |                     |j        d           |                     |j        d           |                     |j        d	           |                     |j
        d
           |                     |j        d           dS )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    DI...*F333*F333)5111+Q///*A...+++!&!!!!(###1aAq!44+Q///+Q///*A...,a000+Q///!$$$!$$$A&&&&&r   c                    |                      t          | j                  d           |                      t          t          j        d                    d           dS )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)>rH   localWindowz<SSHChannel None (lw 1 rw 0)>N)rO   r   r   r@   r   s    r   test_strzChannelTests.test_str   sc    
 	T\**,STTT"q111224S	
 	
 	
 	
 	
r   c                    |                      | j                                        d           |                      t          j        d                                          d           dS )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)>rH   rZ   s   <SSHChannel None (lw 1 rw 0)>N)rO   r   	__bytes__r@   r   s    r   
test_byteszChannelTests.test_bytes   st     	L""$$&N	
 	
 	
 	1---7799,	
 	
 	
 	
 	
r   c                `    |                      | j                                        d           dS )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)rO   r   r   r   s    r   test_logPrefixzChannelTests.test_logPrefix   s:    
 	L""$$&V	
 	
 	
 	
 	
r   c                   dgdfd}|| j         _        | j                             d           | j                             dd           | j                             d           |                     | j         j        d           |                     | j         j                   |                     d	                    |                     | j         j	        d
           |                     | j
        j        | j                  dg           |                     | j         j        g            |                     | j
        j        | j                  dg           dd	<   | j                             d           |                     d	                    | j                             d           | j                                          | j                             d           |                     d	                    dS )zq
        Test that addWindowBytes adds bytes to the window and resumes writing
        if it was paused.
        Fr   r   c                     d d<   d S NTr   r   cbs   r   stubStartWritingz:ChannelTests.test_addWindowBytes.<locals>.stubStartWriting       BqEEEr      testrH   2   *   r   r   )rH   ri      sP   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaNr,   )r   startWritingwritewriteExtendedaddWindowBytesrO   rS   rD   
areWritingbufr>   r   extBufr   assertFalseloseConnection)r   rg   rf   s     @r   test_addWindowBytesz ChannelTests.test_addWindowBytes   s   
 W	 	 	 	 	 	 %5!7###""1g...##B'''6
CCC/0001)3///5yAAA,b111*4<8<.III1##B'''A9%%%##%%%##B'''Ar   c                4   d | j         _        |                     | j                             dd                     |                     | j                             dd                     |                     | j                             dd                     dS )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_methodrD   requestReceivedrt   r   s    r   test_requestReceivedz!ChannelTests.test_requestReceived   s    
 ,D+C(44^SIIJJJ55ndKKLLL55mSIIJJJJJr   c                    |                      | j        j                   | j                                         |                     | j        j                   dS )zM
        Test that the default closeReceieved closes the connection.
        N)rt   r   closingcloseReceivedrD   r   s    r   test_closeReceievedz ChannelTests.test_closeReceieved   sN     	-...""$$$,-----r   c                   dgdfd}|| j         _        | j                             d           | j                             d           |                     | j         j                   |                     d                    | j                             d           | j                             d	           | j        j        | j                  }| 	                    |d
d	g           | 	                    | j         j
        d           | j                             d           | 	                    |g d           | 	                    | j         j
        d           dd<   | j                             d           |                     | j         j                   |                     d                    | 	                    |g d           | 	                    | j         j        d           | 	                    | j         j
        d           dS )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.
        Fr   r   c                     d d<   d S rd   r   re   s   r   stubStopWritingz0ChannelTests.test_write.<locals>.stubStopWriting   rh   r      drz   r   rl      ta   da      12345678901)r   r   
   1234567890   1rL      123456)r   r   r   r      12345   6Nr,   )r   stopWritingrn   rt   rq   rD   rp   r>   r   rO   rS   rr   r   r   r   rf   s      @r   
test_writezChannelTests.test_write   s    W	 	 	 	 	 	 $3 4   4   01111##B'''5!!!y~dl+u~...6;;;>***BBBCCC6:::19%%%01111LLLMMM)40006:::::r   c                `   dgdfd}|| j         _        | j                             dd           | j                             dd           | j                             dd	           |                     | j         j                   |                     d
                    | j                             d           | j                             dd           | j        j        | j                  }| 	                    |g d           | 	                    | j         j
        d           | j                             dd           | 	                    |g d           | 	                    | j         j
        d           dd
<   | j                             dd           |                     | j         j                   |                     d
                    | 	                    |g d           | 	                    | j         j        ddgg           | 	                    | j         j
        d
           dS )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.
        Fr   r   c                     d d<   d S rd   r   re   s   r   r   z8ChannelTests.test_writeExtended.<locals>.stubStopWriting  rh   r   rH   r   rz   rI      tr   rl   )rH   r   rI   r   rI   rz   r   rJ   r   )r   r   r   rJ   r   rJ   r   rL   rK   r   )r   r   r   r   r   )rK   r   r   Nr,   )r   r   ro   rt   rq   rD   rp   r>   r   rO   rS   rs   r   s      @r   test_writeExtendedzChannelTests.test_writeExtended  s;    W	 	 	 	 	 	 $3 ""1d+++""1d+++""1d+++01111##B'''""1d+++y .AAABBB6;;;""1n555SSS	
 	
 	
 	6:::1""1i00001111  
	
 
	
 
	
 	,4yk:::6:::::r   c                    | j                             d           | j                             d t          d          D                        |                     | j        j        | j                  dg           dS )zS
        Test that writeSequence is equivalent to write(''.join(sequece)).
        rl   c              3  "   K   | ]
}d |fz  V  dS )s   %dNr   ).0is     r   	<genexpr>z2ChannelTests.test_writeSequence.<locals>.<genexpr>C  s(      "C"CA5A4<"C"C"C"C"C"Cr   r=   s
   0123456789N)r   rp   writeSequencerangerO   r>   r   r   s    r   test_writeSequencezChannelTests.test_writeSequence>  sn     	##B'''"""C"Cr"C"C"CCCC5GGGGGr   c                T   | j                             d           | j                             dd           | j                                          |                     | j        j                            | j                              | j                             d           |                     | j        j                            | j                              | j                             d           | 	                    | j        j                            | j                              dS )zk
        Tesyt that loseConnection() doesn't close the channel until all
        the data is sent.
        s   datarH   s   datadatarK      N)
r   rn   ro   ru   rT   r>   r   getrp   rD   r   s    r   test_loseConnectionz ChannelTests.test_loseConnectionF  s    
 	7###""1k222##%%%$)*..t|<<===##A&&&$)*..t|<<===##A&&&	(,,T\::;;;;;r   c                    t          ddd          }t          | j        j        |           |                     t          |          | j                                                   dS )z
        L{SSHChannel.getPeer} returns the same object as the underlying
        transport's C{getPeer} method returns.
        TCPz192.168.0.1i1  )r1   r4   N)r   r9   r   r>   rO   r   getPeer)r   peers     r   test_getPeerzChannelTests.test_getPeerT  s`    
 5-77DL$54HHHH,T22DL4H4H4J4JKKKKKr   c                    t          ddd          }t          | j        j        |           |                     t          |          | j                                                   dS )z
        L{SSHChannel.getHost} returns the same object as the underlying
        transport's C{getHost} method returns.
        r   z	127.0.0.1i90  )r1   r2   N)r   r9   r   r>   rO   r   getHost)r   hosts     r   test_getHostzChannelTests.test_getHost^  s`    
 5+u55DL$54HHHH,T22DL4H4H4J4JKKKKKr   Nr,   )r-   r.   r/   r0   rB   rF   rX   r\   r_   ra   rv   r}   r   r   r   r   r   r   r   r   r   r   r;   r;   c   sb        ' ' ' 'K K K K' ' ' '<
 
 
 

 
 
 

 
 
 
       >K K K K. . . ."; "; "; ";H/; /; /; /;bH H H H< < < <L L L LL L L L L Lr   r;   )NN)r1   r   r2   r3   r4   r3   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   r9   r;   r   r   r   <module>r      s    # " " " " "       . . . . . .))))))======444444>>>>>>++++++444444888888HH   )HJJJ , + + + + +*$ *$ *$ *$ *$Z *$ *$ *$^ /3.2" " " " "2 (BL BL BL BL BL8 BL BL BL BL BLs   ,A 	AA