U
    
W[C/  ć                   @   sā   d Z ddlmZ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k
r   dZeZY nX ddlmZ ddlmZ G dd deZdddZG dd dejZdS )z
Test ssh/channel.py.
é    )ŚdivisionŚabsolute_import)ŚverifyObject)Śchannel)ŚSSHTransportAddress)ŚSSHServerTransport)Ś
SSHService)Ś
interfaces)ŚIPv4Address)ŚStringTransportNzConch SSH not supported.)Śunittest)Ś
intToBytesc                   @   s8   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.
    c                 C   s   i | _ i | _i | _d S )N)ŚdataŚextDataŚcloses©Śself© r   śA/usr/lib/python3/dist-packages/twisted/conch/test/test_channel.pyŚ__init__)   s    zMockConnection.__init__c                 C   s   dS )z,
        Return our logging prefix.
        r   r   r   r   r   r   Ś	logPrefix/   s    zMockConnection.logPrefixc                 C   s   | j  |g ” |” dS )z'
        Record the sent data.
        N)r   Ś
setdefaultŚappend)r   r   r   r   r   r   ŚsendData6   s    zMockConnection.sendDatac                 C   s   | j  |g ” ||f” dS )z0
        Record the sent extended data.
        N)r   r   r   )r   r   Śtyper   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   Ś	sendCloseD   s    zMockConnection.sendCloseN)	Ś__name__Ś
__module__Ś__qualname__Ś__doc__r   r   r   r   r   r   r   r   r   r      s   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}
    )ŚhostAddressŚpeerAddressN)r   ZmakeConnectionr   Z
setService)Śservicer"   r#   Z	transportr   r   r   ŚconnectSSHTransportL   s     ’r%   c                   @   s   e Zd ZdZe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}.
    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   r(   r   Ś
SSHChannelŚnamer   r   r   r   ŚsetUph   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_interfacet   s    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.
        )r(   i   i   r   é   é   é   é   é   é   é   N)r   r*   r(   ŚassertEqualZlocalWindowSizeZlocalWindowLeftZlocalMaxPacketŚremoteWindowLeftr)   ŚassertIsNoner   Zavatar)r   ŚcZc2r   r   r   Ś	test_init{   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/   ©ZlocalWindowz<SSHChannel None (lw 1 rw 0)>N)r6   Śstrr   r*   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/   r;   s   <SSHChannel None (lw 1 rw 0)>N)r6   r   Ś	__bytes__r*   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)r6   r   r   r   r   r   r   Śtest_logPrefix“   s    zChannelTests.test_logPrefixc                    s  dg  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.
        Fc                      s   d d< d S ©NTr   r   r   ©Ścbr   r   ŚstubStartWritingĆ   s    z:ChannelTests.test_addWindowBytes.<locals>.stubStartWritingó   testr/   é2   é*   r   ó    )r/   rE   é   sP   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaN)r   ZstartWritingŚwriteŚwriteExtendedŚaddWindowBytesr6   r7   r-   Ś
areWritingŚbufr(   r   ŚextBufr   ŚassertFalseŚloseConnection)r   rD   r   rB   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 )NrH   r   )r   r   r   r   Ś<lambda>ą   rH   z3ChannelTests.test_requestReceived.<locals>.<lambda>s   test-methodrH   ó   as
   bad-methodN)r   Zrequest_test_methodr-   ZrequestReceivedrP   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)rP   r   ŚclosingZcloseReceivedr-   r   r   r   r   Śtest_closeReceievedę   s    
z ChannelTests.test_closeReceievedc                    s.  dg  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” |  	|d	dddg” |  	| j j
d” d d< | j  d” |  | j j” |   d ” |  	|d	ddddg” |  	| 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.
        Fc                      s   d d< d S rA   r   r   rB   r   r   ŚstubStopWritingö   s    z0ChannelTests.test_write.<locals>.stubStopWritingó   drT   r   rI   s   taó   daé   ó   12345678901ó
   1234567890ó   1r3   ó   123456ó   12345ó   6N)r   ŚstopWritingrJ   rP   rM   r-   rL   r(   r   r6   r7   rN   ©r   rX   r   r   rB   r   Ś
test_writeļ   s,    zChannelTests.test_writec              	      sR  dg  fdd}|| j _| j  dd” | j  dd” | j  dd” |  | j j” |   d	 ” | j  d
” | j  dd” | jj| j  }|  	|dddg” |  	| j j
d” | j  dd” |  	|dddddg” |  	| j j
d” d d	< | j  dd” |  | j j” |   d	 ” |  	|ddddddg” |  	| 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.
        Fc                      s   d d< d S rA   r   r   rB   r   r   rX     s    z8ChannelTests.test_writeExtended.<locals>.stubStopWritingr/   rY   rT   r0   ó   tr   rI   )r/   rZ   )r0   re   )r0   rT   r[   r1   r\   )r1   r]   )r1   r^   r3   r2   r_   )r2   r`   ra   N)r   rb   rK   rP   rM   r-   rL   r(   r   r6   r7   rO   rc   r   rB   r   Śtest_writeExtended  s<     ’  ’zChannelTests.test_writeExtendedc                 C   s>   | j  d” | j  tttd” |  | jj| j  dg” dS )zS
        Test that writeSequence is equivalent to write(''.join(sequece)).
        rI   r'   s
   0123456789N)	r   rL   ZwriteSequenceŚmapr   Śranger6   r(   r   r   r   r   r   Śtest_writeSequence8  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   datadatar2   é   N)
r   rJ   rK   rQ   r8   r(   r   ŚgetrL   r-   r   r   r   r   Śtest_loseConnectionA  s    
z ChannelTests.test_loseConnectionc                 C   s6   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   r(   r6   r   ZgetPeer)r   Zpeerr   r   r   Śtest_getPeerP  s    zChannelTests.test_getPeerc                 C   s6   t ddd}t| jj|d |  t|| j ” ” dS )z
        L{SSHChannel.getHost} returns the same object as the underlying
        transport's C{getHost} method returns.
        rm   z	127.0.0.1i90  )r$   r"   N)r
   r%   r   r(   r6   r   ZgetHost)r   Zhostr   r   r   Śtest_getHost[  s    zChannelTests.test_getHostN)r   r   r    r!   ŚskipTestŚskipr,   r.   r:   r=   r?   r@   rR   rU   rW   rd   rf   ri   rl   rn   ro   r   r   r   r   r&   a   s"   		#&	r&   )NN)r!   Z
__future__r   r   Zzope.interface.verifyr   Ztwisted.conch.sshr   Ztwisted.conch.ssh.addressr   Ztwisted.conch.ssh.transportr   Ztwisted.conch.ssh.servicer   Ztwisted.internetr	   Ztwisted.internet.addressr
   Ztwisted.test.proto_helpersr   rp   ŚImportErrorŚobjectZtwisted.trialr   Ztwisted.python.compatr   r   r%   ZTestCaser&   r   r   r   r   Ś<module>   s&   
0
