U
    Â“«jñ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   ú>/tmp/pip-unpacked-wheel-59kc99xd/twisted/test/test_loopback.pyÚ__init__   s    
zSimpleProtocol.__init__c                 C   s   | j  d ¡ d S r   )r   Úcallbackr   r   r   r   ÚconnectionMade   s    zSimpleProtocol.connectionMadec                 C   s   | j  |¡ d S r   )r   Úappend©r   Úliner   r   r   ÚlineReceived   s    zSimpleProtocol.lineReceivedc                 C   s   | j  |¡ d S r   )r   r   )r   Úreasonr   r   r   ÚconnectionLost   s    zSimpleProtocol.connectionLostN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdd„ ZdS )ÚDoomProtocolr   c                 C   sR   |  j d7  _ | j dk r*|  d| j f ¡ t | |¡ | jd dkrN| j ¡  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#   #   s   r#   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚLoopbackTestCaseMixinc                    sR   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 )Nr-   r$   ©ÚassertEqualr   Úlenr   ©Zignored©Úcr1   r   r   r   Úcheck<   s    z8LoopbackTestCaseMixin.testRegularFunction.<locals>.check)r   r   ÚaddCallbackr   ÚmaybeDeferredÚloopbackFunc©r   r2   r9   Údr   r7   r   ÚtestRegularFunction2   s    
z)LoopbackTestCaseMixin.testRegularFunctionc                    sR   t ƒ ‰t ƒ ‰ ‡fdd„}ˆj |¡ ‡ ‡‡fdd„}t ˆjˆˆ ¡}| |¡ |S )Nc                    s   ˆ   d¡ d S )Nó	   DOOM LINE)r)   r.   r0   r   r   r2   I   s    z=LoopbackTestCaseMixin.testSneakyHiddenDoom.<locals>.sendALinec                    sR   ˆ  ˆjdddg¡ ˆ  ˆ jddddg¡ ˆ  tˆjƒd¡ ˆ  tˆ jƒd¡ d S )Ns   Hello 1s   Hello 2r'   r@   r$   r3   r6   r7   r   r   r9   N   s     
ÿz9LoopbackTestCaseMixin.testSneakyHiddenDoom.<locals>.check)r#   r   r:   r   r;   r<   r=   r   r7   r   ÚtestSneakyHiddenDoomE   s    
z*LoopbackTestCaseMixin.testSneakyHiddenDoomN)r    r!   r"   r?   rA   r   r   r   r   r,   1   s   r,   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   s
   || _ d S r   )r*   ©r   r*   r   r   r   ÚmakeConnectiong   s    zKLoopbackAsyncTests.test_makeConnection.<locals>.TestProtocol.makeConnection)r    r!   r"   r*   rD   r   r   r   r   ÚTestProtocold   s   rE   N)r
   r   ÚloopbackAsyncZassertIsNotNoner*   )r   rE   Ú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                   @   s   e Zd Zdd„ ZdS )z6LoopbackAsyncTests._hostpeertest.<locals>.TestProtocolc                 S   s   t  | |¡ | j |¡ d S r   )r
   rD   ÚonConnectionr   rC   r   r   r   rD   v   s    zELoopbackAsyncTests._hostpeertest.<locals>.TestProtocol.makeConnectionN)r    r!   r"   rD   r   r   r   r   rE   u   s   rE   c                    s    t | ˆ ƒƒ }ˆ t |¡¡ d S r   )ÚgetattrÚ
assertTruer   Z
providedBy)r*   Úhost©Úgetr   r   r   Ú	connected…   s    z3LoopbackAsyncTests._hostpeertest.<locals>.connected)r
   r   rJ   r   rF   r:   )r   rO   Ú
testServerrE   rG   r>   rH   rP   r   rN   r   Ú_hostpeertestp   s    z LoopbackAsyncTests._hostpeertestc                 C   s   |   dd¡S )z„
        Test that the server gets a transport with a properly functioning
        implementation of L{ITransport.getHost}.
        ÚgetHostT©rR   r   r   r   r   Útest_serverHost‹   s    z"LoopbackAsyncTests.test_serverHostc                 C   s   |   dd¡S )zG
        Like C{test_serverHost} but for L{ITransport.getPeer}
        ÚgetPeerTrT   r   r   r   r   Útest_serverPeer’   s    z"LoopbackAsyncTests.test_serverPeerrS   c                 C   s   |   dd¡S )z„
        Test that the client gets a transport with a properly functioning
        implementation of L{ITransport.getHost}.
        rS   FrT   )r   rO   r   r   r   Útest_clientHost˜   s    z"LoopbackAsyncTests.test_clientHostc                 C   s   |   dd¡S )zH
        Like C{test_clientHost} but for L{ITransport.getPeer}.
        rV   FrT   r   r   r   r   Útest_clientPeerŸ   s    z"LoopbackAsyncTests.test_clientPeerc                    sj   G dd„ dt ƒ}G ‡ fdd„dt ƒ}|rB|ƒ }|ƒ }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                   @   s   e Zd ZdZdd„ ZdS )z9LoopbackAsyncTests._greetingtest.<locals>.GreeteeProtocoló    c                 S   s(   |  j |7  _ | j dkr$| j d ¡ d S ©Nó   bytes)ÚbytesÚreceivedr   ©r   r]   r   r   r   ÚdataReceived°   s    
zFLoopbackAsyncTests._greetingtest.<locals>.GreeteeProtocol.dataReceivedN©r    r!   r"   r]   r`   r   r   r   r   ÚGreeteeProtocol­   s   rb   c                       s   e Zd Z‡ fdd„ZdS )z9LoopbackAsyncTests._greetingtest.<locals>.GreeterProtocolc                    s*   ˆ dkr| j  d¡ n| j  ddg¡ d S )NÚwriter\   s   byts   es)r*   rc   ÚwriteSequencer   ©rc   r   r   r   ¶   s    zHLoopbackAsyncTests._greetingtest.<locals>.GreeterProtocol.connectionMadeN©r    r!   r"   r   r   re   r   r   ÚGreeterProtocolµ   s   rg   )r
   r   r^   r   rF   )r   rc   rQ   rb   rg   rG   rH   r>   r   re   r   Ú_greetingtest¥   s    z LoopbackAsyncTests._greetingtestc                 C   s   |   dd¡S )z„
        Test that on a connection where the client speaks first, the server
        receives the bytes sent by the client.
        rc   F©rh   r   r   r   r   Útest_clientGreetingÈ   s    z&LoopbackAsyncTests.test_clientGreetingc                 C   s   |   dd¡S )zz
        Like C{test_clientGreeting}, but use C{writeSequence} instead of
        C{write} to issue the greeting.
        rd   Fri   r   r   r   r   Útest_clientGreetingSequenceÏ   s    z.LoopbackAsyncTests.test_clientGreetingSequencerc   c                 C   s   |   dd¡S )z„
        Test that on a connection where the server speaks first, the client
        receives the bytes sent by the server.
        rc   Tri   )r   rc   r   r   r   Útest_serverGreetingÖ   s    z&LoopbackAsyncTests.test_serverGreetingc                 C   s   |   dd¡S )zz
        Like C{test_serverGreeting}, but use C{writeSequence} instead of
        C{write} to issue the greeting.
        rd   Tri   r   r   r   r   Útest_serverGreetingSequenceÝ   s    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.connectionMadeNrf   r   rt   r   r   ÚProducingProtocolç   s   rw   c                       s    e Zd ZdZ‡ ‡‡fdd„ZdS )z;LoopbackAsyncTests._producertest.<locals>.ReceivingProtocolrZ   c                    s2   |  j |7  _ | j d ˆ¡kr.| j ˆ ˆf¡ d S )NrZ   )r]   Újoinr^   r   ©r   Údata©rH   rG   rv   r   r   r`   ï   s    zHLoopbackAsyncTests._producertest.<locals>.ReceivingProtocol.dataReceivedNra   r   r{   r   r   ÚReceivingProtocolì   s   r|   )Úranger
   r   r^   r   rF   )r   ru   rw   r|   r   )rH   ru   rG   rv   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   s
   || _ d S r   ©rv   ©r   rv   r   r   r   r     s    zCLoopbackAsyncTests.test_pushProducer.<locals>.PushProducer.__init__c                 S   s
   d| _ d S ©NT)Úresumedr   r   r   r   ÚresumeProducing  s    zJLoopbackAsyncTests.test_pushProducer.<locals>.PushProducer.resumeProducingc                 S   s   || _ | | d¡ |  ¡  d S r�   )ÚconsumerÚregisterProducerÚ_produceAndSchedule©r   r„   r   r   r   rs   
  s    z@LoopbackAsyncTests.test_pushProducer.<locals>.PushProducer.startc                 S   s8   | j r*| j | j  d¡¡ t d| j¡ n
| j ¡  d S ©Nr   )rv   r„   rc   Úpopr   Ú	callLaterr†   ÚunregisterProducerr   r   r   r   r†     s    zNLoopbackAsyncTests.test_pushProducer.<locals>.PushProducer._produceAndScheduleN)r    r!   r"   r‚   r   rƒ   rs   r†   r   r   r   r   ÚPushProducer   s
   rŒ   c                    s   | \}}ˆ   |jjd¡ d S )Nz0Streaming producer should not have been resumed.)ZassertFalserr   r‚   )ÚresultsrH   rG   r   r   r   Úfinished  s
    þz6LoopbackAsyncTests.test_pushProducer.<locals>.finished)r   r	   r~   r:   )r   rŒ   r>   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   s
   || _ d S r   r   r€   r   r   r   r   )  s    zCLoopbackAsyncTests.test_pullProducer.<locals>.PullProducer.__init__c                 S   s   || _ | j  | d¡ d S )NF)r„   r…   r‡   r   r   r   rs   ,  s    z@LoopbackAsyncTests.test_pullProducer.<locals>.PullProducer.startc                 S   s(   | j  | j d¡¡ | js$| j  ¡  d S rˆ   )r„   rc   rv   r‰   r‹   r   r   r   r   rƒ   0  s    zJLoopbackAsyncTests.test_pullProducer.<locals>.PullProducer.resumeProducingN)r    r!   r"   r   rs   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                   @   s   e Zd Zdd„ ZdS )z9LoopbackAsyncTests.test_writeNotReentrant.<locals>.Serverc                 S   s   | j  d¡ d S r[   )r*   rc   r_   r   r   r   r`   ?  s    zFLoopbackAsyncTests.test_writeNotReentrant.<locals>.Server.dataReceivedN)r    r!   r"   r`   r   r   r   r   ÚServer>  s   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*   rc   Úreadyr   r   r   r   r“   H  s    z<LoopbackAsyncTests.test_writeNotReentrant.<locals>.Client.goc                 S   s   | j | _| j ¡  d S r   )r•   ÚwasReadyr*   r+   r_   r   r   r   r`   L  s    zFLoopbackAsyncTests.test_writeNotReentrant.<locals>.Client.dataReceivedN)r    r!   r"   r•   r   r“   r`   r   r   r   r   ÚClientB  s   r—   c                    s   ˆ  ˆ j¡ d S r   )rL   r–   r6   ©rH   r   r   r   Ú
cbFinishedT  s    z=LoopbackAsyncTests.test_writeNotReentrant.<locals>.cbFinished)r
   r   rF   r:   )r   r’   r—   rG   r>   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|  |  ¡ ¡ qˆ   ||f¡ d S r   )r   rO   )ÚqueueÚtargetr]   )Ú	pumpCallsr   r   ÚdummyPolicyb  s    z7LoopbackAsyncTests.test_pumpPolicy.<locals>.dummyPolicyr”   ó   baró   bazó   quuxc                    s&   ˆ  ˆˆ ddd gfˆddgfg¡ d S )Nr    r¡   r”   rŸ   )r4   r6   ©rH   r�   r   rG   r   r   Ú
cbCompletet  s    üz6LoopbackAsyncTests.test_pumpPolicy.<locals>.cbComplete)r
   r   rF   r4   r*   rc   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   r`   r   Ú_LoopbackQueueÚputZidentityPumpPolicyr4   ©r   r]   rH   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   r`   r   r¥   r¦   ZcollapsingPumpPolicyr4   r§   r   r   r   Útest_collapsingPumpPolicy�  s    


z,LoopbackAsyncTests.test_collapsingPumpPolicyN)rS   )rc   )r    r!   r"   Ústaticmethodr   rF   r<   rI   rR   rU   rW   rX   rY   rh   rj   rk   rl   rm   r~   r�   r‘   rš   r¤   r¨   r©   r   r   r   r   rB   [   s&   

#
'#%rB   c                   @   s   e Zd ZeejƒZdS )ÚLoopbackTCPTestsN)r    r!   r"   rª   r   ZloopbackTCPr<   r   r   r   r   r«   £  s   r«   c                   @   s*   e Zd ZeejƒZe e	d¡dkr&dZ
dS )ÚLoopbackUNIXTestsNz-Current reactor does not support UNIX sockets)r    r!   r"   rª   r   ZloopbackUNIXr<   r   Z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
        rZ   c                 C   s   |  j |7  _ dS )z)Accumulate received data for verificationN)rz   ry   r   r   r   r`   º  s    z'LoopbackRelayTest.Receiver.dataReceivedN)r    r!   r"   Ú__doc__rz   r`   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   defrZ   s   abcdefN)r°   r   ÚLoopbackRelayrc   r4   rz   ÚclearBuffer©r   ZreceiverZrelayr   r   r   Ú
test_write¾  s    


zLoopbackRelayTest.test_writec                 C   s\   |   ¡ }t |¡}| ddddg¡ | dddg¡ |  |jd¡ | ¡  |  |jd	¡ d
S )z@Test to verify that the writeSequence function works as expecteds   The s   quick s   brown s   fox s   jumps s   over s   the lazy dogrZ   s+   The quick brown fox jumps over the lazy dogN)r°   r   r±   rd   r4   rz   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¯   Zzope.interfacer   Ztwisted.internetr   r   r   Ztwisted.internet.deferr   Ztwisted.internet.interfacesr   r   r	   Ztwisted.internet.protocolr
   Ztwisted.protocolsr   r   Ztwisted.trialr   ZLineReceiverr   r#   r,   ZTestCaserB   r«   r¬   r®   r   r   r   r   Ú<module>   s    *  J