o
    bh                     @   s   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 eejG d	d
 d
ZG dd dejZG dd dejZG dd dejZG dd dejZdS )z$
Tests for L{twisted.conch.telnet}.
    )implementer)verifyObject)telnet)defer)	iterbytes)proto_helpers)unittestc                   @   st   e Zd 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S )TestProtocol c                 C   s.   d| _ g | _g | _g | _g | _g | _g | _d S )N    )datasubcmdcallsenabledLocalenabledRemotedisabledLocaldisabledRemoteselfr
   r
   @/usr/lib/python3/dist-packages/twisted/conch/test/test_telnet.py__init__   s   
zTestProtocol.__init__c                    sL   i  }|_  j|d< |j  }|_dD ]}|f fdd	|tt|< qd S )N   )
EORNOPDMBRKIPAOAYTECELGAc                    s    j |S N)r   append)argcmdr   r
   r   <lambda>)   s    z-TestProtocol.makeConnection.<locals>.<lambda>)negotiationMapneg_TEST_COMMAND
commandMapcopygetattrr   )r   	transportdr%   r
   r   r   makeConnection#   s   

zTestProtocol.makeConnectionc                 C   s   |  j |7  _ d S r"   )r   r   r   r
   r
   r   dataReceived+   s   zTestProtocol.dataReceivedc                 C      d S r"   r
   )r   reasonr
   r
   r   connectionLost.   s   zTestProtocol.connectionLostc                 C   s
   || _ d S r"   )r   )r   payloadr
   r
   r   r(   1   s   
zTestProtocol.neg_TEST_COMMANDc                 C      || j v r| j| dS dS NTF)localEnableabler   r#   r   optionr
   r
   r   enableLocal4      
zTestProtocol.enableLocalc                 C      | j | d S r"   )r   r#   r8   r
   r
   r   disableLocal:      zTestProtocol.disableLocalc                 C   r5   r6   )remoteEnableabler   r#   r8   r
   r
   r   enableRemote=   r;   zTestProtocol.enableRemotec                 C   r<   r"   )r   r#   r8   r
   r
   r   disableRemoteC   r>   zTestProtocol.disableRemotec                 C   r1   r"   r
   r   r
   r
   r   connectionMadeF      zTestProtocol.connectionMadec                 C   r1   r"   r
   )r   commandargumentr
   r
   r   unhandledCommandJ   rC   zTestProtocol.unhandledCommandc                 C   r1   r"   r
   r   rD   r   r
   r
   r   unhandledSubnegotiationN   rC   z$TestProtocol.unhandledSubnegotiationN)__name__
__module____qualname__r7   r?   r   r.   r0   r3   r(   r:   r=   r@   rA   rB   rF   rH   r
   r
   r
   r   r	      s    
r	   c                   @   s   e Zd Zdd ZdS )InterfacesTestsc                 C   s   t  }tt j| dS )zO
        L{telnet.TelnetProtocol} implements L{telnet.ITelnetProtocol}
        N)r   TelnetProtocolr   ITelnetProtocol)r   pr
   r
   r   test_interfaceT   s   zInterfacesTests.test_interfaceN)rI   rJ   rK   rP   r
   r
   r
   r   rL   S   s    rL   c                   @   sB  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 d! Zd"d# Zd$d% Zg g g g fd&d'Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Z d<d= Z!d>d? Z"d@dA Z#dBdC Z$dDdE Z%dFdG Z&dHdI Z'dJdK Z(dLS )MTelnetTransportTestsz.
    Tests for L{telnet.TelnetTransport}.
    c                 C   s(   t t| _t | _| j| j d S r"   )r   TelnetTransportr	   rO   r   StringTransporttr.   r   r
   r
   r   setUpa   s   
zTelnetTransportTests.setUpc                 C   s>   | j j}g d}|D ]}| j | q
| |jd| d S )N)s   here are some bytes la la las   some more arrive heres   lots of bytes to play withs   la la las   ta de das   dumr   )rO   protocolr0   assertEqualr   joinr   hLbr
   r
   r   testRegularBytesf   s
   z%TelnetTransportTests.testRegularBytesc                 C   s   | j j}g d}|D ]}| j | q
| |j|d d d d |d d d  d |d d d  d |d d d  d  d S )	N)s   here is the first line
s   here is the second line s   here is the third line
s   here is the last line r      
            )rO   rV   r0   rW   r   rY   r
   r
   r   testNewlineHandlingy   s,   z(TelnetTransportTests.testNewlineHandlingc                 C   sF   | j j}g d}|D ]}| j | q
| |jd|dd d S )N)s*   here are some bytes with an embedded IACs&   and here is a test of a border escapes    did you get that IAC?r   s      )rO   rV   r0   rW   r   rX   replacerY   r
   r
   r   testIACEscape   s
    z"TelnetTransportTests.testIACEscapec                 C   sn   | j j}tjtt| }dd| d g}|D ]}| j | q| |j|g | |jd	|
|d d S )Ns   Here's some bytes, tra la las   But ono!s    an interruptr   )rO   rV   r   IACr+   r0   rW   r   r   rX   rf   )r   cmdNamerZ   r%   r[   r\   r
   r
   r   _simpleCommandTest   s    z'TelnetTransportTests._simpleCommandTestc                 C      |  d d S )Nr   rj   r   r
   r
   r   testInterrupt      z"TelnetTransportTests.testInterruptc                 C   rk   )Nr   rl   r   r
   r
   r   testEndOfRecord   rn   z$TelnetTransportTests.testEndOfRecordc                 C   rk   )Nr   rl   r   r
   r
   r   testNoOperation   rn   z$TelnetTransportTests.testNoOperationc                 C   rk   )Nr   rl   r   r
   r
   r   testDataMark   rn   z!TelnetTransportTests.testDataMarkc                 C   rk   )Nr   rl   r   r
   r
   r   	testBreak   rn   zTelnetTransportTests.testBreakc                 C   rk   )Nr   rl   r   r
   r
   r   testAbortOutput   rn   z$TelnetTransportTests.testAbortOutputc                 C   rk   )Nr   rl   r   r
   r
   r   testAreYouThere   rn   z$TelnetTransportTests.testAreYouTherec                 C   rk   )Nr   rl   r   r
   r
   r   testEraseCharacter   rn   z'TelnetTransportTests.testEraseCharacterc                 C   rk   )Nr    rl   r   r
   r
   r   testEraseLine   rn   z"TelnetTransportTests.testEraseLinec                 C   rk   )Nr!   rl   r   r
   r
   r   testGoAhead   rn   z TelnetTransportTests.testGoAheadc                 C   s|   | j j}tjtj d tj tj }d| dg}|D ]}| j | q| |jd	|
|d | |jttd d S )Ns   hello worlds   These are some bytes but soons   there will be some morer   s   hello world)rO   rV   r   rh   SBSEr0   rW   r   rX   rf   r   listr   r   rZ   r%   r[   r\   r
   r
   r   testSubnegotiation   s   z'TelnetTransportTests.testSubnegotiationc                 C   s   | j j}tjtj d tj tj tj }d| d dg}|D ]}| j | q| |jd	|
|d | |jtjg d S )Nr   s   Some bytes are heres   and herer   )rO   rV   r   rh   rx   ry   r0   rW   r   rX   rf   r   r{   r
   r
   r    testSubnegotiationWithEmbeddedSE   s   "z5TelnetTransportTests.testSubnegotiationWithEmbeddedSEc                 C   s   t jt j d t j d t j t j }tt|D ]L}t  }| j_|	| j |d | ||d  }}d| |d g}|D ]}| j
| q?| |jd||d | |jt jgttd  qd S )Nr   s   hellos
   first parts	   last partr   )r   rh   rx   ry   rangelenr	   rO   rV   r.   r0   rW   r   rX   rf   r   rz   r   )r   r%   irZ   ar\   r[   r   r
   r
   r   testBoundarySubnegotiation   s0   
 z/TelnetTransportTests.testBoundarySubnegotiationc                 C   s<   |  |j| |  |j| |  |j| |  |j| d S r"   )rW   r   r   r   r   )r   oeLeRdLdRr
   r
   r   _enabledHelper
  s   z#TelnetTransportTests._enabledHelperc                 C   r   t jt j d }d| d }| j| | | jjj||d | | j	
 t jt j d  | | jj d S Nr   s   surrounding bytess   to spice things upr   )r   rh   WILLrO   r0   rW   rV   r   rf   rT   valueDONTr   r   r%   r   r
   r
   r   testRefuseWill     z#TelnetTransportTests.testRefuseWillc                 C   r   r   )r   rh   DOrO   r0   rW   rV   r   rf   rT   r   WONTr   r   r
   r
   r   testRefuseDo  r   z!TelnetTransportTests.testRefuseDoc                 C   h   t jt j d }d| d }| jj}d|_| j| | | j	 t jt j
 d  | j|dgd d S )N      paddings   trailer)r   )r   )r   rh   r   rO   rV   r7   r0   rW   rT   r   r   r   r   r%   r   rZ   r
   r
   r   testAcceptDo&  s   z!TelnetTransportTests.testAcceptDoc                 C   r   )N   s   headerr   )r   r   )r   rh   r   rO   rV   r?   r0   rW   rT   r   r   r   r   r
   r
   r   testAcceptWill3  s   z#TelnetTransportTests.testAcceptWillc                 C      t jt j d }| jd}d|j_d| }| j| | | jj	j
||d | | j t jt j d  | |jjd | j| jj	dgd d S )N   )yess
   fiddle deer   nor   )r   rh   r   rO   getOptionStatehimstater0   rW   rV   r   rf   rT   r   r   r   r   r%   sr   r
   r
   r   testAcceptWont?     z#TelnetTransportTests.testAcceptWontc                 C   r   )Nr   r   s   fiddle dum r   r   )r   )r   rh   r   rO   r   usr   r0   rW   rV   r   rf   rT   r   r   r   r   r
   r
   r   testAcceptDontS  r   z#TelnetTransportTests.testAcceptDontc                 C   f   t jt j d }d| d }| j| | | jjj||d | | j	
 d | | jj d S N   G
   dum de dum	   tra la lar   )r   rh   r   rO   r0   rW   rV   r   rf   rT   r   r   r   r
   r
   r   testIgnoreWontg  s   z#TelnetTransportTests.testIgnoreWontc                 C   r   r   )r   rh   r   rO   r0   rW   rV   r   rf   rT   r   r   r   r
   r
   r   testIgnoreDonts  s   z#TelnetTransportTests.testIgnoreDontc                 C   z   t jt j d }| jd}d|j_d| d }| j| | | jj	j
||d | | j d | | jj	 d S N   Vr   r   r   r   )r   rh   r   rO   r   r   r   r0   rW   rV   r   rf   rT   r   r   r   r
   r
   r   testIgnoreWill     z#TelnetTransportTests.testIgnoreWillc                 C   r   r   )r   rh   r   rO   r   r   r   r0   rW   rV   r   rf   rT   r   r   r   r
   r
   r   testIgnoreDo  r   z!TelnetTransportTests.testIgnoreDoc                    sv   j d}j j d _j tjtj	 d  j 
tjtj d  |jd | fdd |S )N   Br   Tc                    s   j  dgdS )Nr   r   )r   _rZ   r   r
   r   r&     s    z@TelnetTransportTests.testAcceptedEnableRequest.<locals>.<lambda>)rO   dorV   r?   rW   rT   r   r   rh   r   r0   r   addCallbackr   r-   r
   r   r   testAcceptedEnableRequest  s   z.TelnetTransportTests.testAcceptedEnableRequestc                       dj j_j d}j tjtj	 d  j 
d  jjd  jjd  jj  jj j tjtj d  |tj}|fdd | fdd |S )z
        If the peer refuses to enable an option we request it to enable, the
        L{Deferred} returned by L{TelnetProtocol.do} fires with an
        L{OptionRefused} L{Failure}.
        r   r   r   c                          jjS r"   r   rO   rV   ignoredr   r
   r   r&         z@TelnetTransportTests.test_refusedEnableRequest.<locals>.<lambda>c                         jjS r"   )assertFalser   negotiatingr   r   r   r
   r   r&     r   )rO   rV   r?   r   rW   rT   r   r   rh   r   r   r   r   r   
assertTruer   r   r0   r   assertFailureOptionRefusedr   r   r
   r   r   test_refusedEnableRequest     

z.TelnetTransportTests.test_refusedEnableRequestc                    r   )z
        If the peer refuses to allow us to enable an option, the L{Deferred}
        returned by L{TelnetProtocol.will} fires with an L{OptionRefused}
        L{Failure}.
        r   r   r   c                    r   r"   r   r   r   r
   r   r&     r   z>TelnetTransportTests.test_refusedEnableOffer.<locals>.<lambda>c                    r   r"   )r   r   r   r   r   r
   r   r&     r   )rO   rV   r7   willrW   rT   r   r   rh   r   r   r   r   r   r   r   r   r0   r   r   r   r   r   r
   r   r   test_refusedEnableOffer  r   z,TelnetTransportTests.test_refusedEnableOfferc                    sz    j d}d|j_ j d}  j tj	tj
 d   j tj	tj d  | jd | fdd |S )Nr   r   Tc                    s    j  jjdgdS )Nr   r   r   r   r   r
   r   r&     s    zATelnetTransportTests.testAcceptedDisableRequest.<locals>.<lambda>)rO   r   r   r   dontrW   rT   r   r   rh   r   r0   r   r   r   r   r-   r
   r   r   testAcceptedDisableRequest  s   z/TelnetTransportTests.testAcceptedDisableRequestc                    sd    j d}d|j_ j d  fdd} fdd} fdd}|d }|| || |S )	N   $r   c                        j d} |tjS Nr   )rO   r   r   r   AlreadyNegotiatingxr-   r   r
   r   _do
     zITelnetTransportTests.testNegotiationBlocksFurtherNegotiation.<locals>._doc                    r   r   )rO   r   r   r   r   r   r   r
   r   _dont  r   zKTelnetTransportTests.testNegotiationBlocksFurtherNegotiation.<locals>._dontc                    s~    j tjtj d   j j jdgd d j j_ j d} j tjtj	 d  |
 jd |
 fdd |S )Nr   r   )r   Tc                    s    j  jjdgdgdS )Nr   )r   r   r   r   r   r
   r   r&     s    z^TelnetTransportTests.testNegotiationBlocksFurtherNegotiation.<locals>._final.<locals>.<lambda>)rO   r0   r   rh   r   r   rV   r?   r   r   r   rW   r   r   r
   r   _final  s   

zLTelnetTransportTests.testNegotiationBlocksFurtherNegotiation.<locals>._final)rO   r   r   r   r   r   )r   r   r   r   r   r-   r
   r   r   'testNegotiationBlocksFurtherNegotiation  s   

z<TelnetTransportTests.testNegotiationBlocksFurtherNegotiationc                 C   s   | j d}| |tjS )N   )rO   r   r   r   AlreadyDisabledr   r
   r
   r   #testSuperfluousDisableRequestRaises'  s   z8TelnetTransportTests.testSuperfluousDisableRequestRaisesc                 C   s.   | j d}d|j_| j d}| |tjS )Nr   r   )rO   r   r   r   r   r   r   AlreadyEnabledr   r
   r
   r   "testSuperfluousEnableRequestRaises,  s   z7TelnetTransportTests.testSuperfluousEnableRequestRaisesc                 C   sx   | j d}| j d}| j d}G dd dt}| j |d | ||}| ||}| ||}t|||gS )Nr      #   4c                   @   s   e Zd ZdS )zLTelnetTransportTests.testLostConnectionFailsDeferreds.<locals>.TestExceptionN)rI   rJ   rK   r
   r
   r
   r   TestException8  s    r   zTotal failure!)rO   r   	Exceptionr3   r   r   gatherResults)r   d1d2d3r   r
   r
   r    testLostConnectionFailsDeferreds3  s   z5TelnetTransportTests.testLostConnectionFailsDeferredsN))rI   rJ   rK   __doc__rU   r]   rd   rg   rj   rm   ro   rp   rq   rr   rs   rt   ru   rv   rw   r|   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
   r
   r
   r   rQ   \   sN    %rQ   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )
TestTelnetzP
    A trivial extension of the telnet protocol class useful to unit tests.
    c                 C   s   t j|  g | _d S r"   )r   Telnetr   eventsr   r
   r
   r   r   H  s   
zTestTelnet.__init__c                 C   s   | j d|f dS )z:
        Record the given data in C{self.events}.
        bytesNr   r#   r/   r
   r
   r   applicationDataReceivedL  s   z"TestTelnet.applicationDataReceivedc                 C      | j d||f dS )z=
        Record the given command in C{self.events}.
        rD   Nr   rG   r
   r
   r   rF   R     zTestTelnet.unhandledCommandc                 C   r   )zL
        Record the given subnegotiation command in C{self.events}.
        	negotiateNr   rG   r
   r
   r   rH   X  r   z"TestTelnet.unhandledSubnegotiationN)rI   rJ   rK   r   r   r   rF   rH   r
   r
   r
   r   r   C  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 d! Zd"S )#TelnetTestsz
    Tests for L{telnet.Telnet}.

    L{telnet.Telnet} implements the TELNET protocol (RFC 854), including option
    and suboption negotiation, and option state tracking.
    c                 C   s   t  | _dS )zM
        Create an unconnected L{telnet.Telnet} to be used by tests.
        N)r   rV   r   r
   r
   r   rU   g  s   zTelnetTests.setUpc                 C      |  | jd dS )z
        L{telnet.Telnet.enableLocal} should reject all options, since
        L{telnet.Telnet} does not know how to implement any options.
            N)r   rV   r:   r   r
   r
   r   test_enableLocalm     zTelnetTests.test_enableLocalc                 C   r   )z
        L{telnet.Telnet.enableRemote} should reject all options, since
        L{telnet.Telnet} does not know how to implement any options.
        r   N)r   rV   r@   r   r
   r
   r   test_enableRemotet  r   zTelnetTests.test_enableRemotec                 C      |  t| jjd dS )a	  
        It is an error for L{telnet.Telnet.disableLocal} to be called, since
        L{telnet.Telnet.enableLocal} will never allow any options to be enabled
        locally.  If a subclass overrides enableLocal, it must also override
        disableLocal.
        r   N)assertRaisesNotImplementedErrorrV   r=   r   r
   r
   r   test_disableLocal{     zTelnetTests.test_disableLocalc                 C   r   )a  
        It is an error for L{telnet.Telnet.disableRemote} to be called, since
        L{telnet.Telnet.enableRemote} will never allow any options to be
        enabled remotely.  If a subclass overrides enableRemote, it must also
        override disableRemote.
        r   N)r   r   rV   rA   r   r
   r
   r   test_disableRemote  r  zTelnetTests.test_disableRemotec                 C   6   t  }| j| | jdd | | d dS )z
        L{telnet.Telnet.requestNegotiation} formats the feature byte and the
        payload bytes into the subnegotiation format and sends them.

        See RFC 855.
           s   s   Nr   rS   rV   r.   requestNegotiationrW   r   r   r,   r
   r
   r   test_requestNegotiation  s   z#TelnetTests.test_requestNegotiationc                 C   r  )z
        If the payload for a subnegotiation includes I{IAC}, it is escaped by
        L{telnet.Telnet.requestNegotiation} with another I{IAC}.

        See RFC 855.
        r  re   s   Nr  r  r
   r
   r   !test_requestNegotiationEscapesIAC  s   z-TelnetTests.test_requestNegotiationEscapesIACc                 G   s,   g  }| j _| j | | |t| dS )z
        Pass the given bytes to the protocol's C{dataReceived} method and
        assert that the given events occur.
        N)rV   r   r0   rW   rz   )r   r   expectedreceivedr
   r
   r   _deliver  s   zTelnetTests._deliverc                 C      |  dd dS )zc
        One application-data byte in the default state gets delivered right
        away.
           a)r   r  Nr  r   r
   r
   r   test_oneApplicationDataByte     z'TelnetTests.test_oneApplicationDataBytec                 C   r  )za
        Two application-data bytes in the default state get delivered
        together.
           bc)r   r  Nr  r   r
   r
   r   test_twoApplicationDataBytes  r  z(TelnetTests.test_twoApplicationDataBytesc                 C   s   |  dtj d dS )z~
        Three application-data bytes followed by a control byte get
        delivered, but the control byte doesn't.
           def)r   r  Nr  r   rh   r   r
   r
   r   test_threeApplicationDataBytes  r   z*TelnetTests.test_threeApplicationDataBytesc                 C   s,   |  tj |  tjd dtjd f dS )zy
        IAC in the escaped state gets delivered and so does another
        application-data byte following it.
           gr   Nr  r   r
   r
   r   test_escapedControl  s    zTelnetTests.test_escapedControlc                 C   s   |  d |  dd |  dd |  d |  dd |  dd |  d |  dd	 |  d
d	 |  d |  tjtj d ddtj d f dS )a  
        A carriage return only puts the protocol into the newline state.  A
        linefeed in the newline state causes just the newline to be
        delivered.  A nul in the newline state causes a carriage return to
        be delivered.  An IAC in the newline state causes a carriage return
        to be delivered and puts the protocol into the escaped state.
        Anything else causes a carriage return and that thing to be
        delivered.
        ra   r_   )r   r_   s   
r   )r   ra   s    r  )r      ar     xr   Nr  r   r
   r
   r   test_carriageReturn  s   




zTelnetTests.test_carriageReturnc                 C   s&   |  dtj tj ddtjdf dS )zt
        Application bytes received before a command are delivered before the
        command is processed.
        r  )r   r  rD   N)r  r   rh   r   r   r
   r
   r   'test_applicationDataBeforeSimpleCommand  s
   
z3TelnetTests.test_applicationDataBeforeSimpleCommandc                 C   s2   i | j _| dtj tj d ddtjdf dS )z~
        Application bytes received before a WILL/WONT/DO/DONT are delivered
        before the command is processed.
           yr   )r   r  rD   N)rV   r)   r  r   rh   r   r   r
   r
   r   !test_applicationDataBeforeCommand  s   
z-TelnetTests.test_applicationDataBeforeCommandc                 C   s6   |  dtj tj d tj tj ddddgf dS )z
        Application bytes received before a subnegotiation command are
        delivered before the negotiation is processed.
           zs   Qx)r   r  r      Qr  N)r  r   rh   rx   ry   r   r
   r
   r   (test_applicationDataBeforeSubnegotiation  s
   
z4TelnetTests.test_applicationDataBeforeSubnegotiationN)rI   rJ   rK   r   rU   r   r   r   r  r  r	  r  r  r  r  r  r  r  r  r!  r
   r
   r
   r   r   _  s$    			r   N)r   zope.interfacer   zope.interface.verifyr   twisted.conchr   twisted.internetr   twisted.python.compatr   twisted.testr   twisted.trialr   rN   r	   TestCaserL   rQ   r   r   r   r
   r
   r
   r   <module>   s"   >	   j