o
    ¼>h2  ć                   @  sź   d Z ddlmZ ddlmZ ddlmZ 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W n eyO   dZeZY nw ddlmZ G dd deZ		ddddZeeeG d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                   @  sB   e Zd ZdZdddZdddZdddZdddZd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                 C  s   i | _ i | _i | _d S )N)ŚdataŚextDataŚcloses©Śself© r   ś/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/conch/test/test_channel.pyŚ__init__*   s   
zMockConnection.__init__Śstrc                 C  s   dS )z,
        Return our logging prefix.
        r   r   r   r   r   r   Ś	logPrefix/   s   zMockConnection.logPrefixr   śchannel.SSHChannelr   Śbytesc                 C  s   | j  |g ” |” dS )z'
        Record the sent data.
        N)r   Ś
setdefaultŚappend)r   r   r   r   r   r   ŚsendData5   ó   zMockConnection.sendDataŚtypeŚintc                 C  s   | j  |g ” ||f” dS )z0
        Record the sent extended data.
        N)r   r   r   )r   r   r!   r   r   r   r   ŚsendExtendedData;   s   zMockConnection.sendExtendedDatac                 C  s   d| j |< dS )z?
        Record that the channel sent a close message.
        TN)r   )r   r   r   r   r   Ś	sendCloseC   s   zMockConnection.sendCloseN©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    



r   NŚservicer   ŚhostAddressśinterfaces.IAddress | NoneŚpeerAddressr   r   c                 C  s&   t  }| t||d” | | ” 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}
    )r+   r-   N)r   ŚmakeConnectionr   Ś
setService)r*   r+   r-   Ś	transportr   r   r   ŚconnectSSHTransportJ   s
   
’r1   c                   @  s¦   e Zd ZdZd#ddZd#ddZd#dd	Zd#d
dZd#ddZd#ddZ	d#ddZ
d#ddZd#ddZd#ddZd#ddZd#ddZd#ddZd#ddZd#d d!Zd"S )$ŚChannelTestsz"
    Tests for L{SSHChannel}.
    r   r   c                 C  s&   t  | _tj| jd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   r4   r   Ś
SSHChannelŚnamer   r   r   r   ŚsetUpi   s   zChannelTests.setUpc                 C  s   |   ttj| j” dS )zK
        L{SSHChannel} instances provide L{interfaces.ITransport}.
        N)Ś
assertTruer   r	   Ś
ITransportr   r   r   r   r   Śtest_interfaces   r    zChannelTests.test_interfacec              	   C  s  t j| jd}|  |jd” |  |jd” |  |jd” |  |jd” |  |jd” |  |j| j” |  	|j
” |  	|j” t  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.
        )r4   i   i   r   é   é   é   é   é   é   é   N)r   r6   r4   ŚassertEqualŚlocalWindowSizeŚlocalWindowLeftŚlocalMaxPacketŚremoteWindowLeftr5   ŚassertIsNoner   Śavatar)r   ŚcŚc2r   r   r   Ś	test_inity   s$   
zChannelTests.test_initc                 C  s.   |   t| jd” |   ttjd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)>r<   ©ŚlocalWindowz<SSHChannel None (lw 1 rw 0)>N)rC   r   r   r6   r   r   r   r   Śtest_str   s   ’zChannelTests.test_strc                 C  s.   |   | j ” d” |   tjd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)>r<   rM   s   <SSHChannel None (lw 1 rw 0)>N)rC   r   Ś	__bytes__r6   r   r   r   r   Ś
test_bytes”   s   
’žzChannelTests.test_bytesc                 C  s   |   | 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)rC   r   r   r   r   r   r   Śtest_logPrefixÆ   s   
’zChannelTests.test_logPrefixc                   s  dg d fd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   r   ©Ścbr   r   ŚstubStartWritingæ   ó   z:ChannelTests.test_addWindowBytes.<locals>.stubStartWritingó   testr<   é2   é*   r   ó    )r<   rY   é   sP   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaNr%   )r   ŚstartWritingŚwriteŚwriteExtendedŚaddWindowBytesrC   rG   r9   Ś
areWritingŚbufr4   r   ŚextBufr   ŚassertFalseŚloseConnection)r   rW   r   rU   r   Śtest_addWindowBytesø   s(   
z ChannelTests.test_addWindowBytesc                 C  sL   d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                 S  s   | dkS )Nr\   r   )r   r   r   r   Ś<lambda>Ü   s    z3ChannelTests.test_requestReceived.<locals>.<lambda>s   test-methodr\   ó   as
   bad-methodN)r   Śrequest_test_methodr9   ŚrequestReceivedre   r   r   r   r   Śtest_requestReceived×   s   z!ChannelTests.test_requestReceivedc                 C  s*   |   | jj” | j ”  |  | jj” dS )zM
        Test that the default closeReceieved closes the connection.
        N)re   r   ŚclosingŚcloseReceivedr9   r   r   r   r   Śtest_closeReceievedį   s   
z ChannelTests.test_closeReceievedc                   s&  dg d fd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                     rS   rT   r   r   rU   r   r   ŚstubStopWritingń   rX   z0ChannelTests.test_write.<locals>.stubStopWritingó   dri   r   r]   ó   taó   daé   ó   12345678901)rs   rr   ó
   1234567890ó   1r@   ó   123456)rs   rr   rv   rw   ó   12345ó   6Nr%   )r   ŚstopWritingr_   re   rb   r9   ra   r4   r   rC   rG   rc   ©r   rp   r   r   rU   r   Ś
test_writeé   s,   zChannelTests.test_writec                   sD  dg d fd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                     rS   rT   r   r   rU   r   r   rp     rX   z8ChannelTests.test_writeExtended.<locals>.stubStopWritingr<   rq   ri   r=   ó   tr   r]   )©r<   rs   ©r=   r~   ©r=   ri   rt   r>   ru   )r   r   r   ©r>   rv   ©r>   rw   r@   r?   rx   )r   r   r   r   r   )r?   ry   rz   Nr%   )r   r{   r`   re   rb   r9   ra   r4   r   rC   rG   rd   r|   r   rU   r   Śtest_writeExtended  s8   ’žzChannelTests.test_writeExtendedc                 C  sB   | j  d” | j  dd tdD ” |  | jj| j  dg” dS )zS
        Test that writeSequence is equivalent to write(''.join(sequece)).
        r]   c                 s  s    | ]}d |f V  qdS )s   %dNr   )Ś.0Śir   r   r   Ś	<genexpr>C  s    z2ChannelTests.test_writeSequence.<locals>.<genexpr>r3   s
   0123456789N)r   ra   ŚwriteSequenceŚrangerC   r4   r   r   r   r   r   Śtest_writeSequence>  s   zChannelTests.test_writeSequencec                 C  s   | 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   datar<   s   datadatar?   é   N)
r   r_   r`   rf   rH   r4   r   Śgetra   r9   r   r   r   r   Śtest_loseConnectionF  s   
z ChannelTests.test_loseConnectionc                 C  ó6   t ddd}t| jj|d |  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Ō  )r*   r-   N)r
   r1   r   r4   rC   r   ŚgetPeer)r   Śpeerr   r   r   Śtest_getPeerT  ó   zChannelTests.test_getPeerc                 C  r   )z
        L{SSHChannel.getHost} returns the same object as the underlying
        transport's C{getHost} method returns.
        r   z	127.0.0.1i90  )r*   r+   N)r
   r1   r   r4   rC   r   ŚgetHost)r   Śhostr   r   r   Śtest_getHost^  r   zChannelTests.test_getHostNr%   )r&   r'   r(   r)   r8   r;   rL   rO   rQ   rR   rg   rl   ro   r}   r   r   r   r   r   r   r   r   r   r2   c   s"    








	




$
1


r2   )NN)r*   r   r+   r,   r-   r,   r   r   )r)   Ś
__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   r1   r2   r   r   r   r   Ś<module>   s0   ž/ż