o
    Óó·iñ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   ú`C:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-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   ©Z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*   )rE   rF   rG   r*   r'   r8   r;   r<   r   r   r>   N   s   
ÿz9LoopbackTestCaseMixin.testSneakyHiddenDoom.<locals>.check)r&   r   r?   r   r@   rA   rB   r   r<   r   ÚtestSneakyHiddenDoomE   s   
z*LoopbackTestCaseMixin.testSneakyHiddenDoomN)r"   r#   r$   rD   rH   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-   rL   r   r   r   r   ÚTestProtocold   r/   rN   N)r
   r   ÚloopbackAsyncZassertIsNotNoner-   )r   rN   Ú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
   rL   ÚonConnectionr   rK   r   r   r   rL   v   s   zELoopbackAsyncTests._hostpeertest.<locals>.TestProtocol.makeConnectionN)r"   r#   r$   rL   r   r   r   r   rN   u   ó    rN   c                    s    t | ˆ ƒƒ }ˆ t |¡¡ d S r   )ÚgetattrÚ
assertTruer   Z
providedBy)r-   Úhost©Úgetr   r   r   Ú	connected…   s   z3LoopbackAsyncTests._hostpeertest.<locals>.connectedN)r
   r   rT   r   rO   r?   )r   rZ   Ú
testServerrN   rP   rC   rQ   r[   r   rY   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}.
        ÚgetHostTN©r]   r   r   r   r   Útest_serverHost‹   ó   z"LoopbackAsyncTests.test_serverHostc                 C   r^   )zG
        Like C{test_serverHost} but for L{ITransport.getPeer}
        ÚgetPeerTNr`   r   r   r   r   Útest_serverPeer’   ó   z"LoopbackAsyncTests.test_serverPeerr_   c                 C   r^   )z„
        Test that the client gets a transport with a properly functioning
        implementation of L{ITransport.getHost}.
        r_   FNr`   )r   rZ   r   r   r   Útest_clientHost˜   rb   z"LoopbackAsyncTests.test_clientHostc                 C   r^   )zH
        Like C{test_clientHost} but for L{ITransport.getPeer}.
        rc   FNr`   r   r   r   r   Útest_clientPeerŸ   re   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   rk   r   r   r   ÚdataReceived°   s   
ÿzFLoopbackAsyncTests._greetingtest.<locals>.GreeteeProtocol.dataReceivedN©r"   r#   r$   rk   rn   r   r   r   r   ÚGreeteeProtocol­   r/   rp   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Úwriterj   s   byts   es)r-   rq   ÚwriteSequencer   ©rq   r   r   r   ¶   s   zHLoopbackAsyncTests._greetingtest.<locals>.GreeterProtocol.connectionMadeN©r"   r#   r$   r   r   rs   r   r   ÚGreeterProtocolµ   s    ru   N)r
   r   rl   r   rO   )r   rq   r\   rp   ru   rP   rQ   rC   r   rs   r   Ú_greetingtest¥   s   z LoopbackAsyncTests._greetingtestc                 C   r^   )z„
        Test that on a connection where the client speaks first, the server
        receives the bytes sent by the client.
        rq   FN©rv   r   r   r   r   Útest_clientGreetingÈ   rb   z&LoopbackAsyncTests.test_clientGreetingc                 C   r^   )zz
        Like C{test_clientGreeting}, but use C{writeSequence} instead of
        C{write} to issue the greeting.
        rr   FNrw   r   r   r   r   Útest_clientGreetingSequenceÏ   rb   z.LoopbackAsyncTests.test_clientGreetingSequencerq   c                 C   r^   )z„
        Test that on a connection where the server speaks first, the client
        receives the bytes sent by the server.
        rq   TNrw   )r   rq   r   r   r   Útest_serverGreetingÖ   rb   z&LoopbackAsyncTests.test_serverGreetingc                 C   r^   )zz
        Like C{test_serverGreeting}, but use C{writeSequence} instead of
        C{write} to issue the greeting.
        rr   TNrw   r   r   r   r   Útest_serverGreetingSequenceÝ   rb   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.connectionMadeNrt   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>.ReceivingProtocolrh   c                    s6   |  j |7  _ | j d ˆ¡kr| j ˆ ˆf¡ d S d S )Nrh   )rk   Újoinrl   r   ©r   Údata©rQ   rP   r„   r   r   rn   ï   s   ÿzHLoopbackAsyncTests._producertest.<locals>.ReceivingProtocol.dataReceivedNro   r   r‰   r   r   ÚReceivingProtocolì   s    rŠ   )Úranger
   r   rl   r   rO   )r   rƒ   r…   rŠ   r   )rQ   rƒ   rP   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   rJ   r   ©r„   ©r   r„   r   r   r   r     rM   zCLoopbackAsyncTests.test_pushProducer.<locals>.PushProducer.__init__c                 S   s
   d| _ d S ©NT)Úresumedr   r   r   r   ÚresumeProducing  rM   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’   rq   Ú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.)ZassertFalser€   r�   )ÚresultsrQ   rP   r   r   r   Úfinished  s
   þz6LoopbackAsyncTests.test_pushProducer.<locals>.finishedN)r   r	   rŒ   r?   )r   rš   rC   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   rJ   r   r�   rŽ   r   r   r   r   )  rM   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’   rq   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ž   N)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                   @   rS   )z9LoopbackAsyncTests.test_writeNotReentrant.<locals>.Serverc                 S   s   | j  d¡ d S ri   )r-   rq   rm   r   r   r   rn   ?  r   zFLoopbackAsyncTests.test_writeNotReentrant.<locals>.Server.dataReceivedN)r"   r#   r$   rn   r   r   r   r   ÚServer>  rU   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-   rq   Úreadyr   r   r   r   r¡   H  s   
z<LoopbackAsyncTests.test_writeNotReentrant.<locals>.Client.goc                 S   s   | j | _| j ¡  d S r   )r£   ÚwasReadyr-   r.   rm   r   r   r   rn   L  s   zFLoopbackAsyncTests.test_writeNotReentrant.<locals>.Client.dataReceivedN)r"   r#   r$   r£   r   r¡   rn   r   r   r   r   ÚClientB  s
    r¥   c                    s   ˆ  ˆ j¡ d S r   )rW   r¤   r;   ©rQ   r   r   r   Ú
cbFinishedT  r   z=LoopbackAsyncTests.test_writeNotReentrant.<locals>.cbFinishedN)r
   r   rO   r?   )r   r    r¥   rP   rC   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   rZ   )ÚqueueÚtargetrk   )Ú	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;   ©rQ   r«   r   rP   r   r   Ú
cbCompletet  s   üz6LoopbackAsyncTests.test_pumpPolicy.<locals>.cbCompleteN)r
   r   rO   r9   r-   rq   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   rn   r   Ú_LoopbackQueueÚputZidentityPumpPolicyr9   ©r   rk   rQ   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   rn   r   r³   r´   ZcollapsingPumpPolicyr9   rµ   r   r   r   Útest_collapsingPumpPolicy�  s   


z,LoopbackAsyncTests.test_collapsingPumpPolicyN)r_   rs   )r"   r#   r$   Ústaticmethodr   rO   rA   rR   r]   ra   rd   rf   rg   rv   rx   ry   rz   r{   rŒ   r�   rŸ   r¨   r²   r¶   r·   r   r   r   r   rI   [   s(    

#
'#%rI   c                   @   s   e Zd ZeejƒZdS )ÚLoopbackTCPTestsN)r"   r#   r$   r¸   r   ZloopbackTCPrA   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   ZloopbackUNIXrA   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
        rh   c                 C   s   |  j |7  _ dS )z)Accumulate received data for verificationN)rˆ   r‡   r   r   r   rn   º  s   z'LoopbackRelayTest.Receiver.dataReceivedN)r"   r#   r$   Ú__doc__rˆ   rn   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   defrh   s   abcdefN)r¾   r   ÚLoopbackRelayrq   r9   rˆ   ÚclearBuffer©r   ZreceiverZ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 dogrh   s+   The quick brown fox jumps over the lazy dogN)r¾   r   r¿   rr   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½   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&   r0   ZTestCaserI   r¹   rº   r¼   r   r   r   r   Ú<module>   s"   *  J