o
    ¯b—/  ã                   @   sØ   d 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yI   dZeZ
Y nw ddlmZ G dd„ de
ƒZddd„ZeeeƒG dd„ deƒƒZdS )z
Test ssh/channel.py.
é    )ÚskipIf)ÚverifyObject)Úchannel)ÚSSHTransportAddress)Ú
SSHService)ÚSSHServerTransport)Ú
interfaces)ÚIPv4Address)ÚStringTransportÚ zConch SSH not supported.)ÚTestCasec                   @   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   ÚsendData5   ó   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   Ú	sendCloseA   s   zMockConnection.sendCloseN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r   r      s    r   Nc                 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   ÚmakeConnectionr
   Ú
setService)Úservicer"   r#   Ú	transportr   r   r   ÚconnectSSHTransportH   s
   
ÿr(   c                   @   sˆ   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}.
    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   ÚsetUpc   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_interfacem   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.
        )r+   i   i €  r   é   é   é   é   é   é   é   N)r   r-   r+   ÚassertEqualÚlocalWindowSizeÚlocalWindowLeftÚlocalMaxPacketÚremoteWindowLeftr,   ÚassertIsNoner   Úavatar)r   ÚcÚc2r   r   r   Ú	test_inits   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)>r3   ©ÚlocalWindowz<SSHChannel None (lw 1 rw 0)>N)r:   Ú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)>r3   rD   s   <SSHChannel None (lw 1 rw 0)>N)r:   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)r:   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                      ó   dˆ d< d S ©NTr   r   r   ©Úcbr   r   ÚstubStartWriting¹   ó   z:ChannelTests.test_addWindowBytes.<locals>.stubStartWritingó   testr3   é2   é*   r   ó    )r3   rQ   é   sP   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaN)r   ÚstartWritingÚwriteÚwriteExtendedÚaddWindowBytesr:   r>   r0   Ú
areWritingÚbufr+   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   Úrequest_test_methodr0   Ú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   ÚclosingÚcloseReceivedr0   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¡ |  	|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.
        Fc                      rK   rL   r   r   rM   r   r   ÚstubStopWritingë   rP   z0ChannelTests.test_write.<locals>.stubStopWritingó   dra   r   rU   ó   taó   daé   ó   12345678901)rk   rj   ó
   1234567890ó   1r7   ó   123456)rk   rj   rn   ro   ó   12345ó   6N)r   ÚstopWritingrW   r]   rZ   r0   rY   r+   r   r:   r>   r[   ©r   rh   r   r   rM   r   Ú
test_writeã   s,   zChannelTests.test_writec                    sB  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  }|  	|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.
        Fc                      rK   rL   r   r   rM   r   r   rh     rP   z8ChannelTests.test_writeExtended.<locals>.stubStopWritingr3   ri   ra   r4   ó   tr   rU   )©r3   rk   ©r4   rv   ©r4   ra   rl   r5   rm   )rw   rx   ry   ©r5   rn   ©r5   ro   r7   r6   rp   )rw   rx   ry   rz   r{   )r6   rq   rr   N)r   rs   rX   r]   rZ   r0   rY   r+   r   r:   r>   r\   rt   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>=  s   € z2ChannelTests.test_writeSequence.<locals>.<genexpr>r*   s
   0123456789N)r   rY   ÚwriteSequenceÚranger:   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   datar3   s   datadatar6   é   N)
r   rW   rX   r^   r?   r+   r   ÚgetrY   r0   r   r   r   r   Útest_loseConnection@  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   r+   r:   r   ÚgetPeer)r   Úpeerr   r   r   Útest_getPeerN  ó   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   r+   r:   r   ÚgetHost)r   Úhostr   r   r   Útest_getHostX  r‹   zChannelTests.test_getHostN)r   r   r    r!   r/   r2   rC   rG   rI   rJ   r_   rd   rg   ru   r|   r‚   r…   rŠ   rŽ   r   r   r   r   r)   ]   s"    

	
$1
r)   )NN)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.test.proto_helpersr
   ÚskipTestÚImportErrorÚobjectÚtwisted.trial.unittestr   r   r(   r)   r   r   r   r   Ú<module>   s*   þ
+