o
    ¯bñ7  ã                   @   sî   d Z ddlmZ ddlmZmZmZ ddlmZ ddl	m
Z
mZmZ ddlmZ ddlmZmZ ddlmZ G d	d
„ d
ejƒZG dd„ deƒZG dd„ dƒZG dd„ deejƒZG dd„ deejƒZG dd„ deejƒZG dd„ dejƒZdS )z.
Test case for L{twisted.protocols.loopback}.
é    )Úimplementer)ÚdeferÚ
interfacesÚreactor)ÚDeferred)ÚIAddressÚIPullProducerÚIPushProducer)ÚProtocol)ÚbasicÚloopback)Úunittestc                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚSimpleProtocolc                 C   s   t  ¡ | _g | _g | _d S ©N)r   r   ÚconnÚlinesÚconnLost©Úself© r   ú</usr/lib/python3/dist-packages/twisted/test/test_loopback.pyÚ__init__   s   

zSimpleProtocol.__init__c                 C   s   | j  d ¡ d S r   )r   Úcallbackr   r   r   r   ÚconnectionMade   ó   zSimpleProtocol.connectionMadec                 C   ó   | j  |¡ d S r   )r   Úappend©r   Úliner   r   r   ÚlineReceived   r   zSimpleProtocol.lineReceivedc                 C   r   r   )r   r   )r   Úreasonr   r   r   ÚconnectionLost   r   zSimpleProtocol.connectionLostN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r!   r   r   r   r   r      s
    r   c                   @   ó   e Zd ZdZdd„ ZdS )ÚDoomProtocolr   c                 C   sV   |  j d7  _ | j dk r|  d| j f ¡ t | |¡ | jd dkr)| j ¡  d S d S )Né   é   s   Hello %déÿÿÿÿó   Hello 3)ÚiÚsendLiner   r   r   Ú	transportÚloseConnectionr   r   r   r   r   &   s   
ÿzDoomProtocol.lineReceivedN)r"   r#   r$   r+   r   r   r   r   r   r&   #   ó    r&   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚLoopbackTestCaseMixinc                    óR   t ƒ ‰t ƒ ‰ ‡fdd„}ˆj |¡ ‡ ‡‡fdd„}t ˆjˆˆ ¡}| |¡ |S )Nc                    s   ˆ   d¡ ˆ j ¡  d S )Nó   THIS IS LINE ONE!)r,   r-   r.   ©Úresult©Úsr   r   Ú	sendALine6   s   
z<LoopbackTestCaseMixin.testRegularFunction.<locals>.sendALinec                    s8   ˆ  ˆ jdg¡ ˆ  tˆjƒd¡ ˆ  tˆ jƒd¡ d S )Nr2   r'   ©ÚassertEqualr   Úlenr   ©Úignored©Úcr6   r   r   r   Úcheck<   s   z8LoopbackTestCaseMixin.testRegularFunction.<locals>.check)r   r   ÚaddCallbackr   ÚmaybeDeferredÚloopbackFunc©r   r7   r?   Údr   r=   r   ÚtestRegularFunction2   s   
z)LoopbackTestCaseMixin.testRegularFunctionc                    r1   )Nc                    s   ˆ   d¡ d S )Nó	   DOOM LINE)r,   r3   r5   r   r   r7   I   s   z=LoopbackTestCaseMixin.testSneakyHiddenDoom.<locals>.sendALinec                    sL   ˆ  ˆjg d¢¡ ˆ  ˆ jg d¢¡ ˆ  tˆjƒd¡ ˆ  tˆ jƒd¡ d S )N)ó   Hello 1ó   Hello 2r*   )rF   rG   rH   r*   r'   r8   r;   r=   r   r   r?   N   s   
ÿz9LoopbackTestCaseMixin.testSneakyHiddenDoom.<locals>.check)r&   r   r@   r   rA   rB   rC   r   r=   r   ÚtestSneakyHiddenDoomE   s   
z*LoopbackTestCaseMixin.testSneakyHiddenDoomN)r"   r#   r$   rE   rI   r   r   r   r   r0   1   s    r0   c                   @   sª   e Zd ZeejƒZdd„ Zdd„ Zdd„ Z	dd„ Z
d(d
d„Zdd„ Zdd„ Zdd„ Zdd„ Zd)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 )*ÚLoopbackAsyncTestsc                 C   sD   G dd„ dt ƒ}|ƒ }|ƒ }t ||¡ |  |j¡ |  |j¡ dS )z}
        Test that the client and server protocol both have makeConnection
        invoked on them by loopbackAsync.
        c                   @   s   e Zd ZdZdd„ ZdS )z<LoopbackAsyncTests.test_makeConnection.<locals>.TestProtocolNc                 S   ó
   || _ d S r   )r-   ©r   r-   r   r   r   ÚmakeConnectiong   ó   
zKLoopbackAsyncTests.test_makeConnection.<locals>.TestProtocol.makeConnection)r"   r#   r$   r-   rM   r   r   r   r   ÚTestProtocold   r/   rO   N)r
   r   ÚloopbackAsyncÚassertIsNotNoner-   )r   rO   ÚserverÚclientr   r   r   Útest_makeConnection^   s   z&LoopbackAsyncTests.test_makeConnectionc                    sj   G dd„ dt ƒ}|r|ƒ }tƒ  }|_t ƒ }nt ƒ }|ƒ }tƒ  }|_t ||¡ ‡ ‡fdd„}| |¡S )zJ
        Test one of the permutations of client/server host/peer.
        c                   @   ó   e Zd Zdd„ ZdS )z6LoopbackAsyncTests._hostpeertest.<locals>.TestProtocolc                 S   s   t  | |¡ | j |¡ d S r   )r
   rM   ÚonConnectionr   rL   r   r   r   rM   v   s   zELoopbackAsyncTests._hostpeertest.<locals>.TestProtocol.makeConnectionN)r"   r#   r$   rM   r   r   r   r   rO   u   ó    rO   c                    s    t | ˆ ƒƒ }ˆ t |¡¡ d S r   )ÚgetattrÚ
assertTruer   Ú
providedBy)r-   Úhost©Úgetr   r   r   Ú	connected…   s   z3LoopbackAsyncTests._hostpeertest.<locals>.connected)r
   r   rV   r   rP   r@   )r   r]   Ú
testServerrO   rR   rD   rS   r^   r   r\   r   Ú_hostpeertestp   s   
z LoopbackAsyncTests._hostpeertestc                 C   ó   |   dd¡S )z„
        Test that the server gets a transport with a properly functioning
        implementation of L{ITransport.getHost}.
        ÚgetHostT©r`   r   r   r   r   Útest_serverHost‹   ó   z"LoopbackAsyncTests.test_serverHostc                 C   ra   )zG
        Like C{test_serverHost} but for L{ITransport.getPeer}
        ÚgetPeerTrc   r   r   r   r   Útest_serverPeer’   ó   z"LoopbackAsyncTests.test_serverPeerrb   c                 C   ra   )z„
        Test that the client gets a transport with a properly functioning
        implementation of L{ITransport.getHost}.
        rb   Frc   )r   r]   r   r   r   Útest_clientHost˜   re   z"LoopbackAsyncTests.test_clientHostc                 C   ra   )zH
        Like C{test_clientHost} but for L{ITransport.getPeer}.
        rf   Frc   r   r   r   r   Útest_clientPeerŸ   rh   z"LoopbackAsyncTests.test_clientPeerc                    sj   G dd„ dt ƒ}G ‡ fdd„dt ƒ}|r!|ƒ }|ƒ }tƒ  }|_n|ƒ }tƒ  }|_|ƒ }t ||¡ |S )z¹
        Test one of the permutations of write/writeSequence client/server.

        @param write: The name of the method to test, C{"write"} or
            C{"writeSequence"}.
        c                   @   r%   )z9LoopbackAsyncTests._greetingtest.<locals>.GreeteeProtocoló    c                 S   s,   |  j |7  _ | j dkr| j d ¡ d S d S ©Nó   bytes)ÚbytesÚreceivedr   ©r   rn   r   r   r   ÚdataReceived°   s   
ÿzFLoopbackAsyncTests._greetingtest.<locals>.GreeteeProtocol.dataReceivedN©r"   r#   r$   rn   rq   r   r   r   r   ÚGreeteeProtocol­   r/   rs   c                       s   e Zd Z‡ fdd„ZdS )z9LoopbackAsyncTests._greetingtest.<locals>.GreeterProtocolc                    s,   ˆ dkr| j  d¡ d S | j  ddg¡ d S )NÚwriterm   s   byts   es)r-   rt   ÚwriteSequencer   ©rt   r   r   r   ¶   s   zHLoopbackAsyncTests._greetingtest.<locals>.GreeterProtocol.connectionMadeN©r"   r#   r$   r   r   rv   r   r   ÚGreeterProtocolµ   s    rx   )r
   r   ro   r   rP   )r   rt   r_   rs   rx   rR   rS   rD   r   rv   r   Ú_greetingtest¥   s   z LoopbackAsyncTests._greetingtestc                 C   ra   )z„
        Test that on a connection where the client speaks first, the server
        receives the bytes sent by the client.
        rt   F©ry   r   r   r   r   Útest_clientGreetingÈ   re   z&LoopbackAsyncTests.test_clientGreetingc                 C   ra   )zz
        Like C{test_clientGreeting}, but use C{writeSequence} instead of
        C{write} to issue the greeting.
        ru   Frz   r   r   r   r   Útest_clientGreetingSequenceÏ   re   z.LoopbackAsyncTests.test_clientGreetingSequencert   c                 C   ra   )z„
        Test that on a connection where the server speaks first, the client
        receives the bytes sent by the server.
        rt   Trz   )r   rt   r   r   r   Útest_serverGreetingÖ   re   z&LoopbackAsyncTests.test_serverGreetingc                 C   ra   )zz
        Like C{test_serverGreeting}, but use C{writeSequence} instead of
        C{write} to issue the greeting.
        ru   Trz   r   r   r   r   Útest_serverGreetingSequenceÝ   re   z.LoopbackAsyncTests.test_serverGreetingSequencec                    sh   dd„ t ddƒD ƒ‰G ‡‡fdd„dtƒ}G ‡ ‡‡fdd„dtƒ}|ƒ ‰|ƒ ‰ tƒ ˆ _t ˆˆ ¡ ˆ jS )	Nc                 S   s   g | ]}d |f ‘qS )s   %dr   )Ú.0r+   r   r   r   Ú
<listcomp>å   s    z4LoopbackAsyncTests._producertest.<locals>.<listcomp>r   é
   c                       s   e Zd Z‡ ‡fdd„ZdS )z;LoopbackAsyncTests._producertest.<locals>.ProducingProtocolc                    s    ˆ t ˆƒƒ| _| j | j¡ d S r   )ÚlistÚproducerÚstartr-   r   ©ÚproducerClassÚ	toProducer   r   r   è   s   zJLoopbackAsyncTests._producertest.<locals>.ProducingProtocol.connectionMadeNrw   r   r…   r   r   ÚProducingProtocolç   s    rˆ   c                       s    e Zd ZdZ‡ ‡‡fdd„ZdS )z;LoopbackAsyncTests._producertest.<locals>.ReceivingProtocolrk   c                    s6   |  j |7  _ | j d ˆ¡kr| j ˆ ˆf¡ d S d S )Nrk   )rn   Újoinro   r   ©r   Údata©rS   rR   r‡   r   r   rq   ï   s   ÿzHLoopbackAsyncTests._producertest.<locals>.ReceivingProtocol.dataReceivedNrr   r   rŒ   r   r   ÚReceivingProtocolì   s    r�   )Úranger
   r   ro   r   rP   )r   r†   rˆ   r�   r   )rS   r†   rR   r‡   r   Ú_producertestä   s   z LoopbackAsyncTests._producertestc                    s:   t tƒG dd„ dƒƒ}ˆ  |¡}‡ fdd„}| |¡ |S )zO
        Test a push producer registered against a loopback transport.
        c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )z:LoopbackAsyncTests.test_pushProducer.<locals>.PushProducerFc                 S   rK   r   ©r‡   ©r   r‡   r   r   r   r     rN   zCLoopbackAsyncTests.test_pushProducer.<locals>.PushProducer.__init__c                 S   s
   d| _ d S ©NT)Úresumedr   r   r   r   ÚresumeProducing  rN   zJLoopbackAsyncTests.test_pushProducer.<locals>.PushProducer.resumeProducingc                 S   s   || _ | | d¡ |  ¡  d S r’   )ÚconsumerÚregisterProducerÚ_produceAndSchedule©r   r•   r   r   r   r„   
  s   z@LoopbackAsyncTests.test_pushProducer.<locals>.PushProducer.startc                 S   s:   | j r| j | j  d¡¡ t d| j¡ d S | j ¡  d S ©Nr   )r‡   r•   rt   Úpopr   Ú	callLaterr—   ÚunregisterProducerr   r   r   r   r—     s   zNLoopbackAsyncTests.test_pushProducer.<locals>.PushProducer._produceAndScheduleN)r"   r#   r$   r“   r   r”   r„   r—   r   r   r   r   ÚPushProducer   s    r�   c                    s   | \}}ˆ   |jjd¡ d S )Nz0Streaming producer should not have been resumed.)ÚassertFalserƒ   r“   )ÚresultsrS   rR   r   r   r   Úfinished  s
   þz6LoopbackAsyncTests.test_pushProducer.<locals>.finished)r   r	   r�   r@   )r   r�   rD   r    r   r   r   Útest_pushProducerû   s   

z$LoopbackAsyncTests.test_pushProducerc                 C   s    t tƒG dd„ dƒƒ}|  |¡S )zO
        Test a pull producer registered against a loopback transport.
        c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )z:LoopbackAsyncTests.test_pullProducer.<locals>.PullProducerc                 S   rK   r   r�   r‘   r   r   r   r   )  rN   zCLoopbackAsyncTests.test_pullProducer.<locals>.PullProducer.__init__c                 S   s   || _ | j  | d¡ d S )NF)r•   r–   r˜   r   r   r   r„   ,  s   z@LoopbackAsyncTests.test_pullProducer.<locals>.PullProducer.startc                 S   s,   | j  | j d¡¡ | js| j  ¡  d S d S r™   )r•   rt   r‡   rš   rœ   r   r   r   r   r”   0  s   ÿzJLoopbackAsyncTests.test_pullProducer.<locals>.PullProducer.resumeProducingN)r"   r#   r$   r   r„   r”   r   r   r   r   ÚPullProducer'  s    r¢   )r   r   r�   )r   r¢   r   r   r   Útest_pullProducer"  s   
z$LoopbackAsyncTests.test_pullProducerc                    sT   G dd„ dt ƒ}G dd„ dt ƒ}|ƒ }|ƒ ‰ t ˆ |¡}‡ ‡fdd„}| |¡ |S )zº
        L{loopback.loopbackAsync} does not call a protocol's C{dataReceived}
        method while that protocol's transport's C{write} method is higher up
        on the stack.
        c                   @   rU   )z9LoopbackAsyncTests.test_writeNotReentrant.<locals>.Serverc                 S   s   | j  d¡ d S rl   )r-   rt   rp   r   r   r   rq   ?  r   zFLoopbackAsyncTests.test_writeNotReentrant.<locals>.Server.dataReceivedN)r"   r#   r$   rq   r   r   r   r   ÚServer>  rW   r¤   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	z9LoopbackAsyncTests.test_writeNotReentrant.<locals>.ClientFc                 S   s   t  d| j¡ d S r™   )r   r›   Úgor   r   r   r   r   E  s   zHLoopbackAsyncTests.test_writeNotReentrant.<locals>.Client.connectionMadec                 S   s   | j  d¡ d| _d S )Nó   fooT)r-   rt   Úreadyr   r   r   r   r¥   H  s   
z<LoopbackAsyncTests.test_writeNotReentrant.<locals>.Client.goc                 S   s   | j | _| j ¡  d S r   )r§   ÚwasReadyr-   r.   rp   r   r   r   rq   L  s   zFLoopbackAsyncTests.test_writeNotReentrant.<locals>.Client.dataReceivedN)r"   r#   r$   r§   r   r¥   rq   r   r   r   r   ÚClientB  s
    r©   c                    s   ˆ  ˆ j¡ d S r   )rY   r¨   r;   ©rS   r   r   r   Ú
cbFinishedT  r   z=LoopbackAsyncTests.test_writeNotReentrant.<locals>.cbFinished)r
   r   rP   r@   )r   r¤   r©   rR   rD   r«   r   rª   r   Útest_writeNotReentrant7  s   
z)LoopbackAsyncTests.test_writeNotReentrantc                    s�   g ‰‡fdd„}t ƒ ‰ t ƒ ‰t ˆˆ |¡}ˆ ˆg ¡ ˆ j d¡ ˆ j d¡ ˆj d¡ ˆj d¡ ˆj ¡  ‡ ‡‡‡fdd„}| |¡ |S )	zÜ
        The callable passed as the value for the C{pumpPolicy} parameter to
        L{loopbackAsync} is called with a L{_LoopbackQueue} of pending bytes
        and a protocol to which they should be delivered.
        c                    s,   g }| r|  |  ¡ ¡ | sˆ   ||f¡ d S r   )r   r]   )ÚqueueÚtargetrn   )Ú	pumpCallsr   r   ÚdummyPolicyb  s
   ÿz7LoopbackAsyncTests.test_pumpPolicy.<locals>.dummyPolicyr¦   ó   baró   bazó   quuxc                    s$   ˆ  ˆˆ g d¢fˆddgfg¡ d S )N)r²   r³   Nr¦   r±   )r9   r;   ©rS   r¯   r   rR   r   r   Ú
cbCompletet  s   üz6LoopbackAsyncTests.test_pumpPolicy.<locals>.cbComplete)r
   r   rP   r9   r-   rt   r.   r@   )r   r°   r    rµ   r   r´   r   Útest_pumpPolicyZ  s   

z"LoopbackAsyncTests.test_pumpPolicyc                 C   sX   g }t ƒ }|j|_t ¡ }| d¡ | d¡ | d¡ t ||¡ |  |ddg¡ dS )zŸ
        L{identityPumpPolicy} is a pump policy which calls the target's
        C{dataReceived} method one for each string in the queue passed to it.
        r¦   r±   N)r
   r   rq   r   Ú_LoopbackQueueÚputÚidentityPumpPolicyr9   ©r   rn   rS   r­   r   r   r   Útest_identityPumpPolicy  s   


z*LoopbackAsyncTests.test_identityPumpPolicyc                 C   sV   g }t ƒ }|j|_t ¡ }| d¡ | d¡ | d¡ t ||¡ |  |dg¡ dS )zÀ
        L{collapsingPumpPolicy} is a pump policy which calls the target's
        C{dataReceived} only once with all of the strings in the queue passed
        to it joined together.
        r¦   r±   Ns   foobar)r
   r   rq   r   r·   r¸   ÚcollapsingPumpPolicyr9   rº   r   r   r   Útest_collapsingPumpPolicy�  s   


z,LoopbackAsyncTests.test_collapsingPumpPolicyN)rb   rv   )r"   r#   r$   Ústaticmethodr   rP   rB   rT   r`   rd   rg   ri   rj   ry   r{   r|   r}   r~   r�   r¡   r£   r¬   r¶   r»   r½   r   r   r   r   rJ   [   s(    

#
'#%rJ   c                   @   s   e Zd ZeejƒZdS )ÚLoopbackTCPTestsN)r"   r#   r$   r¾   r   ÚloopbackTCPrB   r   r   r   r   r¿   £  s    r¿   c                   @   s.   e Zd ZeejƒZe e	d¡du rdZ
dS dS )ÚLoopbackUNIXTestsNz-Current reactor does not support UNIX sockets)r"   r#   r$   r¾   r   ÚloopbackUNIXrB   r   ÚIReactorUNIXr   Úskipr   r   r   r   rÁ   §  s
    
ÿrÁ   c                   @   s0   e Zd ZdZG dd„ deƒZdd„ Zdd„ ZdS )	ÚLoopbackRelayTestz>
    Test for L{twisted.protocols.loopback.LoopbackRelay}
    c                   @   s   e Zd ZdZdZdd„ ZdS )zLoopbackRelayTest.ReceiverzF
        Simple Receiver class used for testing LoopbackRelay
        rk   c                 C   s   |  j |7  _ dS )z)Accumulate received data for verificationN)r‹   rŠ   r   r   r   rq   º  s   z'LoopbackRelayTest.Receiver.dataReceivedN)r"   r#   r$   Ú__doc__r‹   rq   r   r   r   r   ÚReceiver³  s    rÇ   c                 C   sN   |   ¡ }t |¡}| d¡ | d¡ |  |jd¡ | ¡  |  |jd¡ dS )z8Test to verify that the write function works as expecteds   abcs   defrk   s   abcdefN)rÇ   r   ÚLoopbackRelayrt   r9   r‹   ÚclearBuffer©r   ÚreceiverÚrelayr   r   r   Ú
test_write¾  s   


zLoopbackRelayTest.test_writec                 C   sV   |   ¡ }t |¡}| g d¢¡ | g d¢¡ |  |jd¡ | ¡  |  |jd¡ dS )z@Test to verify that the writeSequence function works as expected)s   The s   quick s   brown s   fox )s   jumps s   over s   the lazy dogrk   s+   The quick brown fox jumps over the lazy dogN)rÇ   r   rÈ   ru   r9   r‹   rÉ   rÊ   r   r   r   Útest_writeSequenceÈ  s   
z$LoopbackRelayTest.test_writeSequenceN)r"   r#   r$   rÆ   r
   rÇ   rÍ   rÎ   r   r   r   r   rÅ   ®  s
    
rÅ   N)rÆ   Úzope.interfacer   Útwisted.internetr   r   r   Útwisted.internet.deferr   Útwisted.internet.interfacesr   r   r	   Útwisted.internet.protocolr
   Útwisted.protocolsr   r   Útwisted.trialr   ÚLineReceiverr   r&   r0   ÚTestCaserJ   r¿   rÁ   rÅ   r   r   r   r   Ú<module>   s"   *  J