U
    
W[8                     @   s  d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZmZ ddlmZ ddlmZ dd	lmZ dd
lmZmZmZ ddlmZ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}.
    )divisionabsolute_import)implementer)
intToBytes)unittest)basicloopback)defer)Protocol)Deferred)IAddressIPushProducerIPullProducer)reactor
interfacesc                   @   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   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   sT   |  j d7  _ | j dk r,| dt| j   t| | | jd dkrP| j  d S )N      s   Hello    Hello 3)isendLiner   r   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   	sendALine=   s    
z<LoopbackTestCaseMixin.testRegularFunction.<locals>.sendALinec                    s8     jdg  tjd  t jd d S )Nr0   r'   assertEqualr   lenr   Zignoredcr4   r   r   r   checkB   s    z8LoopbackTestCaseMixin.testRegularFunction.<locals>.check)r   r   addCallbackr	   maybeDeferredloopbackFuncr   r5   r<   dr   r:   r   testRegularFunction9   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,   r1   r3   r   r   r5   O   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*   rC   r'   r6   r9   r:   r   r   r<   S   s     
z9LoopbackTestCaseMixin.testSneakyHiddenDoom.<locals>.check)r&   r   r=   r	   r>   r?   r@   r   r:   r   testSneakyHiddenDoomK   s    
z*LoopbackTestCaseMixin.testSneakyHiddenDoomN)r#   r$   r%   rB   rD   r   r   r   r   r/   8   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   makeConnectionj   s    zKLoopbackAsyncTests.test_makeConnection.<locals>.TestProtocol.makeConnection)r#   r$   r%   r-   rG   r   r   r   r   TestProtocolh   s   rH   N)r
   r   loopbackAsyncZassertIsNotNoner-   )r   rH   serverclientr   r   r   test_makeConnectionc   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
   rG   onConnectionr   rF   r   r   r   rG   y   s    zELoopbackAsyncTests._hostpeertest.<locals>.TestProtocol.makeConnectionN)r#   r$   r%   rG   r   r   r   r   rH   x   s   rH   c                    s    t |   }t| d S r   )getattr
assertTruer   Z
providedBy)r-   Zhostgetr   r   r   	connected   s    z3LoopbackAsyncTests._hostpeertest.<locals>.connected)r
   r   rM   r   rI   r=   )r   rQ   
testServerrH   rJ   rA   rK   rR   r   rP   r   _hostpeertestt   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rT   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}
        getPeerTrV   r   r   r   r   test_serverPeer   s    z"LoopbackAsyncTests.test_serverPeerrU   c                 C   s   |  ddS )z
        Test that the client gets a transport with a properly functioning
        implementation of L{ITransport.getHost}.
        rU   FrV   )r   rQ   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}.
        rX   FrV   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_   rb   r   r   r   r   GreeteeProtocol   s   rd   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-   re   writeSequencer   re   r   r   r      s    zHLoopbackAsyncTests._greetingtest.<locals>.GreeterProtocol.connectionMadeNr#   r$   r%   r   r   rg   r   r   GreeterProtocol   s   ri   )r
   r   r`   r   rI   )r   re   rS   rd   ri   rJ   rK   rA   r   rg   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.
        re   Frj   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.
        rf   Frk   r   r   r   r   test_clientGreetingSequence   s    z.LoopbackAsyncTests.test_clientGreetingSequencere   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.
        re   Trk   )r   re   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.
        rf   Trk   r   r   r   r   test_serverGreetingSequence   s    z.LoopbackAsyncTests.test_serverGreetingSequencec                    sh   t tttddG fdddt}G  fdddt}| |  t  _t   jS )Nr   
   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.connectionMadeNrh   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>.ReceivingProtocolr\   c                    s2   |  j |7  _ | j dkr.| j f d S )Nr\   )r_   joinr`   r   r   datarK   rJ   rv   r   r   rb      s    zHLoopbackAsyncTests._producertest.<locals>.ReceivingProtocol.dataReceivedNrc   r   r{   r   r   ReceivingProtocol   s   r|   )	rq   mapr   ranger
   r   r`   r   rI   )r   ru   rw   r|   r   )rK   ru   rJ   rv   r   _producertest   s    z LoopbackAsyncTests._producertestc                    s<   t tG dd dt} |} 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   re   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   )ZresultsrK   rJ   r   r   r   finished!  s
    z6LoopbackAsyncTests.test_pushProducer.<locals>.finished)r   r   objectr   r=   )r   r   rA   r   r   r   r   test_pushProducer  s    

z$LoopbackAsyncTests.test_pushProducerc                 C   s"   t tG dd dt}| |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   0  s    zCLoopbackAsyncTests.test_pullProducer.<locals>.PullProducer.__init__c                 S   s   || _ | j | d d S )NF)r   r   r   r   r   r   rs   3  s    z@LoopbackAsyncTests.test_pullProducer.<locals>.PullProducer.startc                 S   s(   | j | jd | js$| j   d S r   )r   re   rv   r   r   r   r   r   r   r   7  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   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-   re   ra   r   r   r   rb   E  s    zFLoopbackAsyncTests.test_writeNotReentrant.<locals>.Server.dataReceivedN)r#   r$   r%   rb   r   r   r   r   ServerD  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   K  s    zHLoopbackAsyncTests.test_writeNotReentrant.<locals>.Client.connectionMadec                 S   s   | j d d| _d S )N   fooT)r-   re   readyr   r   r   r   r   N  s    z<LoopbackAsyncTests.test_writeNotReentrant.<locals>.Client.goc                 S   s   | j | _| j  d S r   )r   wasReadyr-   r.   ra   r   r   r   rb   R  s    zFLoopbackAsyncTests.test_writeNotReentrant.<locals>.Client.dataReceivedN)r#   r$   r%   r   r   r   rb   r   r   r   r   ClientH  s   r   c                    s     j d S r   )rO   r   r9   rK   r   r   r   
cbFinishedY  s    z=LoopbackAsyncTests.test_writeNotReentrant.<locals>.cbFinished)r
   r   rI   r=   )r   r   r   rJ   rA   r   r   r   r   test_writeNotReentrant>  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   rQ   )queuetargetr_   )	pumpCallsr   r   dummyPolicyf  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   )r7   r9   rK   r   r   rJ   r   r   
cbCompletex  s    
z6LoopbackAsyncTests.test_pumpPolicy.<locals>.cbComplete)r
   r   rI   r7   r-   re   r.   r=   )r   r   r   r   r   r   r   test_pumpPolicy_  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   rb   r   _LoopbackQueueputZidentityPumpPolicyr7   r   r_   rK   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   rb   r   r   r   ZcollapsingPumpPolicyr7   r   r   r   r   test_collapsingPumpPolicy  s    


z,LoopbackAsyncTests.test_collapsingPumpPolicyN)rU   )re   )r#   r$   r%   staticmethodr   rI   r?   rL   rT   rW   rY   rZ   r[   rj   rl   rm   rn   ro   r   r   r   r   r   r   r   r   r   r   r   rE   _   s&   

"
$!$rE   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
        r\   c                 C   s   |  j |7  _ dS )z)Accumulate received data for verificationN)rz   ry   r   r   r   rb     s    z'LoopbackRelayTest.Receiver.dataReceivedN)r#   r$   r%   __doc__rz   rb   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   defr\   s   abcdefN)r   r   LoopbackRelayre   r7   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 dogr\   s+   The quick brown fox jumps over the lazy dogN)r   r   r   rf   r7   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
__future__r   r   Zzope.interfacer   Ztwisted.python.compatr   Ztwisted.trialr   Ztwisted.protocolsr   r   Ztwisted.internetr	   Ztwisted.internet.protocolr
   Ztwisted.internet.deferr   Ztwisted.internet.interfacesr   r   r   r   r   ZLineReceiverr   r&   r/   ZTestCaserE   r   r   r   r   r   r   r   <module>   s&   '  L