o
    Ņó·i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   śeC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/conch/test/test_channel.pyŚ__init__*   s   
zMockConnection.__init__Śstrc                 C  s   dS )z,
        Return our logging prefix.
        r   Nr   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   ZmakeConnectionr   Z
setService)r*   r+   r-   Ś	transportr   r   r   ŚconnectSSHTransportJ   s
   
’r/   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   r2   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	   Z
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.
        )r2   i   i   r   é   é   é   é   é   é   é   N)r   r4   r2   ŚassertEqualZlocalWindowSizeZlocalWindowLeftZlocalMaxPacketŚremoteWindowLeftr3   ŚassertIsNoner   Z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)>r9   ©ZlocalWindowz<SSHChannel None (lw 1 rw 0)>N)r@   r   r   r4   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)>r9   rF   s   <SSHChannel None (lw 1 rw 0)>N)r@   r   Ś	__bytes__r4   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)r@   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ó   testr9   é2   é*   r   ó    )r9   rQ   é   sP   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaNr%   )r   ZstartWritingŚwriteŚwriteExtendedŚaddWindowBytesr@   rA   r7   Ś
areWritingŚbufr2   r   ŚextBufr   ŚassertFalseŚloseConnection)r   rO   r   rM   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 )NrT   r   )r   r   r   r   Ś<lambda>Ü   s    z3ChannelTests.test_requestReceived.<locals>.<lambda>s   test-methodrT   ó   as
   bad-methodN)r   Zrequest_test_methodr7   ZrequestReceivedr\   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)r\   r   ŚclosingZcloseReceivedr7   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                     rK   rL   r   r   rM   r   r   ŚstubStopWritingń   rP   z0ChannelTests.test_write.<locals>.stubStopWritingó   dr`   r   rU   ó   taó   daé   ó   12345678901)rg   rf   ó
   1234567890ó   1r=   ó   123456)rg   rf   rj   rk   ó   12345ó   6Nr%   )r   ŚstopWritingrV   r\   rY   r7   rX   r2   r   r@   rA   rZ   ©r   rd   r   r   rM   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                     rK   rL   r   r   rM   r   r   rd     rP   z8ChannelTests.test_writeExtended.<locals>.stubStopWritingr9   re   r`   r:   ó   tr   rU   )©r9   rg   ©r:   rr   ©r:   r`   rh   r;   ri   )rs   rt   ru   ©r;   rj   ©r;   rk   r=   r<   rl   )rs   rt   ru   rv   rw   )r<   rm   rn   Nr%   )r   ro   rW   r\   rY   r7   rX   r2   r   r@   rA   r[   rp   r   rM   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)).
        rU   c                 s  s    | ]}d |f V  qdS )s   %dNr   )Ś.0Śir   r   r   Ś	<genexpr>C  s    z2ChannelTests.test_writeSequence.<locals>.<genexpr>r1   s
   0123456789N)r   rX   ZwriteSequenceŚranger@   r2   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   datar9   s   datadatar<   é   N)
r   rV   rW   r]   rB   r2   r   ŚgetrX   r7   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
   r/   r   r2   r@   r   ZgetPeer)r   Z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
   r/   r   r2   r@   r   ZgetHost)r   Śhostr   r   r   Śtest_getHost^  r   zChannelTests.test_getHostNr%   )r&   r'   r(   r)   r6   r8   rE   rG   rI   rJ   r^   ra   rc   rq   rx   r}   r   r   r   r   r   r   r   r0   c   s"    








	




$
1


r0   )NN)r*   r   r+   r,   r-   r,   r   r   )r)   Ś
__future__r   Zunittestr   Zzope.interface.verifyr   Ztwisted.conch.sshr   Ztwisted.conch.ssh.addressr   Ztwisted.conch.ssh.servicer   Ztwisted.conch.ssh.transportr   Ztwisted.internetr	   Ztwisted.internet.addressr
   Ztwisted.internet.testingr   ZskipTestŚImportErrorŚobjectZtwisted.trial.unittestr   r   r/   r0   r   r   r   r   Ś<module>   s0   ž/ż