Ë
    oj2  ã                  ó  — d 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d	lmZ dd
lmZ ddlmZ dZddlmZ  G d„ de«      Z	 	 d	 	 	 	 	 	 	 dd„Z eee«       G d„ de«      «       Zy# e$ r dZeZY ŒBw xY w)z
Test ssh/channel.py.
é    )Úannotations)ÚskipIf)ÚverifyObject)Úchannel)ÚSSHTransportAddress)Ú
SSHService)ÚSSHServerTransport)Ú
interfaces)ÚIPv4Address)ÚStringTransportÚ zConch SSH not supported.)ÚTestCasec                  óH   — e Zd ZdZdd„Zd	d„Zd
d„Z	 	 	 	 	 	 	 	 dd„Zdd„Zy)Ú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                ó.   — i | _         i | _        i | _        y )N)ÚdataÚextDataÚcloses©Úselfs    úA/usr/lib/python3/dist-packages/twisted/conch/test/test_channel.pyÚ__init__zMockConnection.__init__*   s   € Ø;=ˆŒ	ØJLˆŒØ68ˆ�ó    c                 ó   — y)z,
        Return our logging prefix.
        r   © r   s    r   Ú	logPrefixzMockConnection.logPrefix/   s   € ð  r   c                óZ   — | j                   j                  |g «      j                  |«       y)z'
        Record the sent data.
        N)r   Ú
setdefaultÚappend)r   r   r   s      r   ÚsendDatazMockConnection.sendData5   s#   € ð 	�	‰	×Ñ˜W bÓ)×0Ñ0°Õ6r   c                ó^   — | j                   j                  |g «      j                  ||f«       y)z0
        Record the sent extended data.
        N)r   r   r   )r   r   Útyper   s       r   ÚsendExtendedDatazMockConnection.sendExtendedData;   s'   € ð 	�‰×Ñ ¨Ó,×3Ñ3°T¸4°LÕAr   c                ó"   — d| j                   |<   y)z?
        Record that the channel sent a close message.
        TN)r   )r   r   s     r   Ú	sendClosezMockConnection.sendCloseC   s   € ð  $ˆ�‰�GÒr   N©ÚreturnÚNone)r'   Ústr)r   úchannel.SSHChannelr   Úbytesr'   r(   )r   r*   r"   Úintr   r+   r'   r(   )r   r*   r'   r(   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r    r#   r%   r   r   r   r   r      sE   „ ñ
ó9ó
 ó7ðBØ)ðBØ14ðBØ<AðBà	óBô$r   r   Nc                ór   — t        «       }|j                  t        ||¬«      «       |j                  | «       y)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)Úservicer2   r3   Ú	transports       r   ÚconnectSSHTransportr8   J   s4   € ô$ #Ó$€IØ×ÑÜ K¸[ÔIôð ×Ñ˜Õ!r   c                  óˆ   — 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y)ÚChannelTestsz"
    Tests for L{SSHChannel}.
    c                ó�   — t        «       | _        t        j                  | j                  d¬«      | _        d| j                  _        y)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   s    r   ÚsetUpzChannelTests.setUpi   s2   € ô #Ó$ˆŒ	Ü×)Ñ)¨t¯y©yÈ"ÔMˆŒØ&ˆ�‰Õr   c                ój   — | j                  t        t        j                  | j                  «      «       y)zK
        L{SSHChannel} instances provide L{interfaces.ITransport}.
        N)Ú
assertTruer   r
   Ú
ITransportr   r   s    r   Útest_interfacezChannelTests.test_interfaces   s!   € ð 	�‰œ¤Z×%:Ñ%:¸D¿L¹LÓIÕJr   c           	     ó
  — t        j                  | j                  ¬«      }| j                  |j                  d«       | j                  |j
                  d«       | j                  |j                  d«       | j                  |j                  d«       | j                  |j                  d«       | j                  |j                  | j                  «       | j                  |j                  «       | j                  |j                  «       t        j                  ddddd	d
d«      }| j                  |j                  d«       | j                  |j
                  d«       | j                  |j                  d«       | j                  |j                  d«       | j                  |j                  d«       | j                  |j                  d	«       | j                  |j                  d
«       | j                  |j                  d«       y)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Úc2s      r   Ú	test_initzChannelTests.test_inity   sˆ  € ô ×Ñ D§I¡IÔ.ˆØ×Ñ˜×*Ñ*¨FÔ3Ø×Ñ˜×*Ñ*¨FÔ3Ø×Ñ˜×)Ñ)¨5Ô1Ø×Ñ˜×+Ñ+¨QÔ/Ø×Ñ˜×*Ñ*¨AÔ.Ø×Ñ˜Ÿ™ §¡Ô+Ø×Ñ˜!Ÿ&™&Ô!Ø×Ñ˜!Ÿ(™(Ô#ä×Ñ  1 a¨¨A¨q°!Ó4ˆØ×Ñ˜×+Ñ+¨QÔ/Ø×Ñ˜×+Ñ+¨QÔ/Ø×Ñ˜×*Ñ*¨AÔ.Ø×Ñ˜×,Ñ,¨aÔ0Ø×Ñ˜×+Ñ+¨QÔ/Ø×Ñ˜Ÿ™ !Ô$Ø×Ñ˜Ÿ™ !Ô$Ø×Ñ˜Ÿ™ AÕ&r   c                ó¬   — | j                  t        | j                  «      d«       | j                  t        t        j                  d¬«      «      d«       y)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)>rG   ©ÚlocalWindowz<SSHChannel None (lw 1 rw 0)>N)rN   r)   r   r?   r   s    r   Útest_strzChannelTests.test_str—   sB   € ð
 	×Ñœ˜TŸ\™\Ó*Ð,SÔTØ×ÑÜ”×"Ñ"¨qÔ1Ó2Ð4Sõ	
r   c                óÀ   — | j                  | j                  j                  «       d«       | j                  t        j                  d¬«      j                  «       d«       y)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)>rG   rY   s   <SSHChannel None (lw 1 rw 0)>N)rN   r   Ú	__bytes__r?   r   s    r   Ú
test_byteszChannelTests.test_bytes¡   sO   € ð 	×ÑØ�L‰L×"Ñ"Ó$Ð&Nô	
ð 	×ÑÜ×Ñ¨1Ô-×7Ñ7Ó9Ø,õ	
r   c                óX   — | j                  | j                  j                  «       d«       y)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)rN   r   r   r   s    r   Útest_logPrefixzChannelTests.test_logPrefix¯   s%   € ð
 	×ÑØ�L‰L×"Ñ"Ó$Ð&Võ	
r   c                ó4  ‡— dgŠdˆfd„}|| j                   _        | j                   j                  d«       | j                   j                  dd«       | j                   j	                  d«       | j                  | j                   j                  d«       | j                  | j                   j                  «       | j                  ‰d   «       | j                  | j                   j                  d«       | j                  | j                  j                  | j                      dg«       | j                  | j                   j                  g «       | j                  | j                  j                  | j                      d	g«       d‰d<   | j                   j	                  d
«       | j                  ‰d   «       | j                   j                  d«       | j                   j                  «        | j                   j	                  d
«       | j                  ‰d   «       y)zq
        Test that addWindowBytes adds bytes to the window and resumes writing
        if it was paused.
        Fc                 ó   •— d‰ d<   y ©NTr   r   ©Úcbs   €r   ÚstubStartWritingz:ChannelTests.test_addWindowBytes.<locals>.stubStartWriting¿   ó   ø€ ØˆBˆqŠEr   ó   testrG   é2   é*   r   r   )rG   rh   é   sP   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaNr&   )r   ÚstartWritingÚwriteÚwriteExtendedÚaddWindowBytesrN   rR   rC   Ú
areWritingÚbufr=   r   ÚextBufr   ÚassertFalseÚloseConnection)r   rf   re   s     @r   Útest_addWindowBytesz ChannelTests.test_addWindowBytes¸   s†  ø€ ð
 ˆWˆõ	ð %5ˆ�‰Ô!Ø�‰×Ñ˜7Ô#Ø�‰×"Ñ" 1 gÔ.Ø�‰×#Ñ# BÔ'Ø×Ñ˜Ÿ™×6Ñ6¸
ÔCØ�‰˜Ÿ™×/Ñ/Ô0Ø�‰˜˜1™ÔØ×Ñ˜Ÿ™×)Ñ)¨3Ô/Ø×Ñ˜Ÿ™Ÿ™¨¯©Ñ5¸°yÔAØ×Ñ˜Ÿ™×,Ñ,¨bÔ1Ø×Ñ˜Ÿ™×*Ñ*¨4¯<©<Ñ8¸<¸.ÔIàˆˆ1‰Ø�‰×#Ñ# BÔ'Ø×Ñ˜˜A™Ôà�‰×Ñ˜9Ô%Ø�‰×#Ñ#Ô%Ø�‰×#Ñ# BÔ'Ø×Ñ˜˜A™Õr   c                ó*  — d„ | j                   _        | j                  | j                   j                  dd«      «       | j	                  | j                   j                  dd«      «       | j	                  | j                   j                  dd«      «       y)zn
        Test that requestReceived handles requests by dispatching them to
        request_* methods.
        c                ó   — | dk(  S )Nr   r   )r   s    r   ú<lambda>z3ChannelTests.test_requestReceived.<locals>.<lambda>Ü   s
   € ¸À¹€ r   s   test-methodr   ó   as
   bad-methodN)r   Úrequest_test_methodrC   ÚrequestReceivedrs   r   s    r   Útest_requestReceivedz!ChannelTests.test_requestReceived×   sl   € ñ
 ,Dˆ�‰Ô(Ø�‰˜Ÿ™×4Ñ4°^ÀSÓIÔJØ×Ñ˜Ÿ™×5Ñ5°nÀdÓKÔLØ×Ñ˜Ÿ™×5Ñ5°mÀSÓIÕJr   c                óÌ   — | j                  | j                  j                  «       | j                  j                  «        | j	                  | j                  j                  «       y)zM
        Test that the default closeReceieved closes the connection.
        N)rs   r   ÚclosingÚcloseReceivedrC   r   s    r   Útest_closeReceievedz ChannelTests.test_closeReceievedá   sB   € ð 	×Ñ˜Ÿ™×-Ñ-Ô.Ø�‰×"Ñ"Ô$Ø�‰˜Ÿ™×,Ñ,Õ-r   c                óZ  ‡— dgŠdˆfd„}|| j                   _        | j                   j                  d«       | j                   j                  d«       | j                  | j                   j                  «       | j                  ‰d   «       | j                   j                  d«       | j                   j                  d«       | j                  j                  | j                      }| j                  |ddg«       | j                  | j                   j                  d	«       | j                   j                  d
«       | j                  |g d¢«       | j                  | j                   j                  d«       d‰d<   | j                   j                  d«       | j                  | j                   j                  «       | j                  ‰d   «       | j                  |g d¢«       | j                  | j                   j                  d«       | j                  | j                   j                  d«       y)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                 ó   •— d‰ d<   y rc   r   rd   s   €r   ÚstubStopWritingz0ChannelTests.test_write.<locals>.stubStopWritingñ   rg   r   ó   dry   r   rk   ó   taó   daé   ó   12345678901)r†   r…   ó
   1234567890ó   1rK   ó   123456)r†   r…   r‰   rŠ   ó   12345ó   6Nr&   )r   ÚstopWritingrm   rs   rp   rC   ro   r=   r   rN   rR   rq   ©r   rƒ   r   re   s      @r   Ú
test_writezChannelTests.test_writeé   s˜  ø€ ð ˆWˆõ	ð $3ˆ�‰Ô Ø�‰×Ñ˜4Ô Ø�‰×Ñ˜4Ô Ø×Ñ˜Ÿ™×0Ñ0Ô1Ø�‰˜˜1™Ôà�‰×#Ñ# BÔ'Ø�‰×Ñ˜5Ô!Ø�y‰y�~‰~˜dŸl™lÑ+ˆØ×Ñ˜  u˜~Ô.Ø×Ñ˜Ÿ™×6Ñ6¸Ô;à�‰×Ñ˜>Ô*Ø×Ñ˜ÒBÔCØ×Ñ˜Ÿ™×6Ñ6¸Ô:àˆˆ1‰Ø�‰×Ñ˜9Ô%Ø×Ñ˜Ÿ™×0Ñ0Ô1Ø�‰˜˜1™ÔØ×Ñ˜ÒLÔMØ×Ñ˜Ÿ™×)Ñ)¨4Ô0Ø×Ñ˜Ÿ™×6Ñ6¸Õ:r   c                ó¢  ‡— dgŠdˆfd„}|| j                   _        | j                   j                  dd«       | j                   j                  dd«       | j                   j                  dd«       | j                  | j                   j                  «       | j                  ‰d   «       | j                   j                  d	«       | j                   j                  dd«       | j                  j                  | j                      }| j                  |g d
¢«       | j                  | j                   j                  d«       | j                   j                  dd«       | j                  |g d¢«       | j                  | j                   j                  d«       d‰d<   | j                   j                  dd«       | j                  | j                   j                  «       | j                  ‰d   «       | j                  |g d¢«       | j                  | j                   j                  ddgg«       | j                  | j                   j                  d«       y)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                 ó   •— d‰ d<   y rc   r   rd   s   €r   rƒ   z8ChannelTests.test_writeExtended.<locals>.stubStopWriting  rg   r   rG   r„   ry   rH   ó   tr   rk   )©rG   r†   ©rH   r“   ©rH   ry   r‡   rI   rˆ   )r”   r•   r–   ©rI   r‰   ©rI   rŠ   rK   rJ   r‹   )r”   r•   r–   r—   r˜   )rJ   rŒ   r�   Nr&   )r   rŽ   rn   rs   rp   rC   ro   r=   r   rN   rR   rr   r�   s      @r   Útest_writeExtendedzChannelTests.test_writeExtended  sÇ  ø€ ð ˆWˆõ	ð $3ˆ�‰Ô Ø�‰×"Ñ" 1 dÔ+Ø�‰×"Ñ" 1 dÔ+Ø�‰×"Ñ" 1 dÔ+Ø×Ñ˜Ÿ™×0Ñ0Ô1Ø�‰˜˜1™Ôà�‰×#Ñ# BÔ'Ø�‰×"Ñ" 1 dÔ+Ø�y‰y× Ñ  §¡Ñ.ˆØ×Ñ˜ÒAÔBØ×Ñ˜Ÿ™×6Ñ6¸Ô;à�‰×"Ñ" 1 nÔ5Ø×ÑØÒSô	
ð 	×Ñ˜Ÿ™×6Ñ6¸Ô:àˆˆ1‰Ø�‰×"Ñ" 1 iÔ0Ø×Ñ˜Ÿ™×0Ñ0Ô1Ø�‰˜˜1™ÔØ×ÑØòô
	
ð 	×Ñ˜Ÿ™×,Ñ,°°4¨y¨kÔ:Ø×Ñ˜Ÿ™×6Ñ6¸Õ:r   c                óø   — | j                   j                  d«       | j                   j                  d„ t        d«      D «       «       | j	                  | j
                  j                  | j                      dg«       y)zS
        Test that writeSequence is equivalent to write(''.join(sequece)).
        rk   c              3  ó(   K  — | ]
  }d |fz  –— Œ y­w)s   %dNr   )Ú.0Úis     r   ú	<genexpr>z2ChannelTests.test_writeSequence.<locals>.<genexpr>C  s   è ø€ Ò"C°A 5¨A¨4¥<Ñ"Cùs   ‚r<   s
   0123456789N)r   ro   ÚwriteSequenceÚrangerN   r=   r   r   s    r   Útest_writeSequencezChannelTests.test_writeSequence>  sU   € ð 	�‰×#Ñ# BÔ'Ø�‰×"Ñ"Ñ"C¼¸r»Ô"CÔCØ×Ñ˜Ÿ™Ÿ™¨¯©Ñ5¸°ÕGr   c                ó†  — | j                   j                  d«       | j                   j                  dd«       | j                   j                  «        | j	                  | j
                  j                  j                  | j                   «      «       | j                   j                  d«       | j	                  | j
                  j                  j                  | j                   «      «       | j                   j                  d«       | j                  | j
                  j                  j                  | j                   «      «       y)zk
        Tesyt that loseConnection() doesn't close the channel until all
        the data is sent.
        s   datarG   s   datadatarJ   é   N)
r   rm   rn   rt   rS   r=   r   Úgetro   rC   r   s    r   Útest_loseConnectionz ChannelTests.test_loseConnectionF  sÑ   € ð
 	�‰×Ñ˜7Ô#Ø�‰×"Ñ" 1 kÔ2Ø�‰×#Ñ#Ô%Ø×Ñ˜$Ÿ)™)×*Ñ*×.Ñ.¨t¯|©|Ó<Ô=Ø�‰×#Ñ# AÔ&Ø×Ñ˜$Ÿ)™)×*Ñ*×.Ñ.¨t¯|©|Ó<Ô=Ø�‰×#Ñ# AÔ&Ø�‰˜Ÿ	™	×(Ñ(×,Ñ,¨T¯\©\Ó:Õ;r   c                óÆ   — t        ddd«      }t        | j                  j                  |¬«       | j	                  t        |«      | j                  j                  «       «       y)z€
        L{SSHChannel.getPeer} returns the same object as the underlying
        transport's C{getPeer} method returns.
        ÚTCPz192.168.0.1i1Ô  )r6   r3   N)r   r8   r   r=   rN   r   ÚgetPeer)r   Úpeers     r   Útest_getPeerzChannelTests.test_getPeerT  sI   € ô
 ˜5 -°Ó7ˆÜ D§L¡L×$5Ñ$5À4ÕHà×ÑÔ,¨TÓ2°D·L±L×4HÑ4HÓ4JÕKr   c                óÆ   — t        ddd«      }t        | j                  j                  |¬«       | j	                  t        |«      | j                  j                  «       «       y)z€
        L{SSHChannel.getHost} returns the same object as the underlying
        transport's C{getHost} method returns.
        r§   z	127.0.0.1i90  )r6   r2   N)r   r8   r   r=   rN   r   ÚgetHost)r   Úhosts     r   Útest_getHostzChannelTests.test_getHost^  sI   € ô
 ˜5 +¨uÓ5ˆÜ D§L¡L×$5Ñ$5À4ÕHà×ÑÔ,¨TÓ2°D·L±L×4HÑ4HÓ4JÕKr   Nr&   )r-   r.   r/   r0   rA   rE   rW   r[   r^   r`   ru   r|   r€   r�   r™   r¡   r¥   rª   r®   r   r   r   r:   r:   c   sY   „ ñó'óKó'ó<
ó
ó
ó ó>Kó.ó";óH/;óbHó<óLôLr   r:   )NN)r6   r   r2   úinterfaces.IAddress | Noner3   r¯   r'   r(   )r0   Ú
__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   r8   r:   r   r   r   ú<module>r¾      s»   ðñõ #å å .ðÝ)Ý=Ý4Ý>Ý+Ý4Ý8à€Hõ ,ô*$�Zô *$ð^ /3Ø.2ð"Øð"à+ð"ð ,ð"ð 
ó	"ñ2 ˆ�(ÓôBL�8ó BLó ñBLøð[ ò Ø)€HØ‚Jðús   –,A9 Á9	BÂB