o
    b7                     @   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