U
    ª€lj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k
r    dZeZY nX ddlmZ G dd„ deƒZdddd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d„Zddœdd„Zd	d
ddœdd„Zd	dd
ddœdd„Zd	d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.
    ÚNone©Úreturnc                 C  s   i | _ i | _i | _d S )N)ÚdataÚextDataÚcloses©Úself© r   úC/tmp/pip-unpacked-wheel-aghyhbke/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.logPrefixzchannel.SSHChannelÚbytes)r   r   r   c                 C  s   | j  |g ¡ |¡ dS )z'
        Record the sent data.
        N)r   Ú
setdefaultÚappend)r   r   r   r   r   r   ÚsendData5   s    zMockConnection.sendDataÚint)r   Útyper   r   c                 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.sendExtendedData)r   r   c                 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)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r"   r#   r   r   r   r   r      s   r   Nr   zinterfaces.IAddress | Noner   )ÚserviceÚhostAddressÚpeerAddressr   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d„Zddœdd„Zddœdd	„Zddœd
d„Zddœdd„Zddœdd„Z	ddœdd„Z
ddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zd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   r/   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   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   r1   r/   ÚassertEqualZlocalWindowSizeZlocalWindowLeftZlocalMaxPacketÚremoteWindowLeftr0   Ú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)>r6   ©ZlocalWindowz<SSHChannel None (lw 1 rw 0)>N)r=   r   r   r1   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)>r6   rC   s   <SSHChannel None (lw 1 rw 0)>N)r=   r   Ú	__bytes__r1   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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                     s   dˆ d< d S ©NTr   r   r   ©Úcbr   r   ÚstubStartWriting¿   s    z:ChannelTests.test_addWindowBytes.<locals>.stubStartWritingó   testr6   é2   é*   r   ó    )r6   rL   é   sP   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaN)r   ZstartWritingÚwriteÚwriteExtendedÚaddWindowBytesr=   r>   r4   Ú
areWritingÚbufr/   r   ÚextBufr   ÚassertFalseÚloseConnection)r   rK   r   rI   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 )NrO   r   )r   r   r   r   Ú<lambda>Ü   rO   z3ChannelTests.test_requestReceived.<locals>.<lambda>s   test-methodrO   ó   as
   bad-methodN)r   Zrequest_test_methodr4   ZrequestReceivedrW   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)rW   r   ÚclosingZcloseReceivedr4   r   r   r   r   Útest_closeReceievedá   s    
z ChannelTests.test_closeReceievedc                   s4  dg‰ d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¡ |  	|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.
        Fr   r   c                     s   dˆ d< d S rH   r   r   rI   r   r   ÚstubStopWritingñ   s    z0ChannelTests.test_write.<locals>.stubStopWritingó   dr[   r   rP   s   taó   daé   ó   12345678901ó
   1234567890ó   1r:   ó   123456ó   12345ó   6N)r   ÚstopWritingrQ   rW   rT   r4   rS   r/   r   r=   r>   rU   ©r   r_   r   r   rI   r   Ú
test_writeé   s,    zChannelTests.test_writec              	     sX  dg‰ d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  }|  	|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.
        Fr   r   c                     s   dˆ d< d S rH   r   r   rI   r   r   r_     s    z8ChannelTests.test_writeExtended.<locals>.stubStopWritingr6   r`   r[   r7   ó   tr   rP   )r6   ra   )r7   rl   )r7   r[   rb   r8   rc   )r8   rd   )r8   re   r:   r9   rf   )r9   rg   rh   N)r   ri   rR   rW   rT   r4   rS   r/   r   r=   r>   rV   rj   r   rI   r   Útest_writeExtended  sF     ÿúþ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)).
        rP   c                 s  s   | ]}d |f V  qdS )s   %dNr   )Ú.0Úir   r   r   Ú	<genexpr>C  s     z2ChannelTests.test_writeSequence.<locals>.<genexpr>r.   s
   0123456789N)r   rS   ZwriteSequenceÚranger=   r/   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   datar6   s   datadatar9   é   N)
r   rQ   rR   rX   r?   r/   r   ÚgetrS   r4   r   r   r   r   Útest_loseConnectionF  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/   r=   r   ZgetPeer)r   Zpeerr   r   r   Útest_getPeerT  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.
        rv   z	127.0.0.1i90  )r(   r)   N)r
   r,   r   r/   r=   r   ZgetHost)r   Úhostr   r   r   Útest_getHost^  s    zChannelTests.test_getHostN)r$   r%   r&   r'   r3   r5   rB   rD   rF   rG   rY   r\   r^   rk   rm   rr   ru   rw   ry   r   r   r   r   r-   c   s    

	
$1
r-   )NN)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,   r-   r   r   r   r   Ú<module>   s,   
/  ý