
    Vh7                     r   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
ej(                        Z G d de      Z G d d      Z G d deej0                        Z G d deej0                        Z G d deej0                        Z G d dej0                        Zy)z.
Test case for L{twisted.protocols.loopback}.
    )implementer)defer
interfacesreactor)Deferred)IAddressIPullProducerIPushProducer)Protocol)basicloopback)unittestc                   $    e Zd Zd Zd Zd Zd Zy)SimpleProtocolc                 R    t        j                         | _        g | _        g | _        y N)r   r   connlinesconnLostselfs    J/home/dcms/DCMS/lib/python3.12/site-packages/twisted/test/test_loopback.py__init__zSimpleProtocol.__init__   s    NN$	
    c                 :    | j                   j                  d        y r   )r   callbackr   s    r   connectionMadezSimpleProtocol.connectionMade   s    		4 r   c                 :    | j                   j                  |       y r   )r   appendr   lines     r   lineReceivedzSimpleProtocol.lineReceived   s    

$r   c                 :    | j                   j                  |       y r   )r   r   )r   reasons     r   connectionLostzSimpleProtocol.connectionLost   s    V$r   N)__name__
__module____qualname__r   r   r"   r%    r   r   r   r      s    
! %r   r   c                       e Zd ZdZd Zy)DoomProtocolr   c                    | xj                   dz  c_         | j                   dk  r| j                  d| j                   fz         t        j                  | |       | j                  d   dk(  r| j
                  j                          y y )N      s   Hello %d   Hello 3)isendLiner   r"   r   	transportloseConnectionr    s     r   r"   zDoomProtocol.lineReceived&   sh    !66A: MM+	12##D$/::b>Z'NN))+ (r   N)r&   r'   r(   r1   r"   r)   r   r   r+   r+   #   s    	A,r   r+   c                       e Zd Zd Zd Zy)LoopbackTestCaseMixinc                      t               t               fd}j                  j                  |        fd}t        j                   j
                        }|j                  |       |S )Nc                 \    j                  d       j                  j                          y )N   THIS IS LINE ONE!)r2   r3   r4   resultss    r   	sendALinez<LoopbackTestCaseMixin.testRegularFunction.<locals>.sendALine6   s     JJ+,KK&&(r   c                     j                  j                  dg       j                  t        j                        d       j                  t        j                        d       y )Nr9   r-   assertEqualr   lenr   ignoredcr<   r   s    r   checkz8LoopbackTestCaseMixin.testRegularFunction.<locals>.check<   sK    QWW';&<=S_a0S_a0r   )r   r   addCallbackr   maybeDeferredloopbackFuncr   r=   rE   drD   r<   s   `   @@r   testRegularFunctionz)LoopbackTestCaseMixin.testRegularFunction2   s^    	) 	
9%	1
  1 11a8	er   c                      t               t               fd}j                  j                  |        fd}t        j                   j
                        }|j                  |       |S )Nc                 (    j                  d       y )N	   DOOM LINE)r2   r:   s    r   r=   z=LoopbackTestCaseMixin.testSneakyHiddenDoom.<locals>.sendALineI   s    JJ|$r   c                    j                  j                  g d       j                  j                  g d       j                  t        j                        d       j                  t        j                        d       y )N)   Hello 1   Hello 2r0   )rN   rP   rQ   r0   r-   r?   rB   s    r   rE   z9LoopbackTestCaseMixin.testSneakyHiddenDoom.<locals>.checkN   sb    QWW&JKK S_a0S_a0r   )r+   r   rF   r   rG   rH   rI   s   `   @@r   testSneakyHiddenDoomz*LoopbackTestCaseMixin.testSneakyHiddenDoomE   s\    NN	% 	
9%	1  1 11a8	er   N)r&   r'   r(   rK   rR   r)   r   r   r6   r6   1   s    &r   r6   c                       e Zd Z eej
                        Zd Zd Zd Z	d Z
ddZd Zd Zd Zd	 Zdd
Zd Zd Zd Zd Zd Zd Zd Zd Zy)LoopbackAsyncTestsc                      G d dt               } |       } |       }t        j                  ||       | j                  |j                         | j                  |j                         y)z}
        Test that the client and server protocol both have makeConnection
        invoked on them by loopbackAsync.
        c                       e Zd ZdZd Zy)<LoopbackAsyncTests.test_makeConnection.<locals>.TestProtocolNc                     || _         y r   )r3   r   r3   s     r   makeConnectionzKLoopbackAsyncTests.test_makeConnection.<locals>.TestProtocol.makeConnectiong   	    !*r   )r&   r'   r(   r3   rZ   r)   r   r   TestProtocolrW   d   s    I+r   r\   N)r   r   loopbackAsyncassertIsNotNoner3   )r   r\   serverclients       r   test_makeConnectionz&LoopbackAsyncTests.test_makeConnection^   sV    	+8 	+ vv.V--.V--.r   c                      G d dt               }|r# |       }t               x}|_        t               }n"t               } |       }t               x}|_        t        j                  ||        fd}|j                  |      S )zJ
        Test one of the permutations of client/server host/peer.
        c                       e Zd Zd Zy)6LoopbackAsyncTests._hostpeertest.<locals>.TestProtocolc                 f    t        j                  | |       | j                  j                  |       y r   )r   rZ   onConnectionr   rY   s     r   rZ   zELoopbackAsyncTests._hostpeertest.<locals>.TestProtocol.makeConnectionv   s&    ''i8!!**95r   N)r&   r'   r(   rZ   r)   r   r   r\   rd   u   s    6r   r\   c                 p     t        |              }j                  t        j                  |             y r   )getattr
assertTruer   
providedBy)r3   hostgetr   s     r   	connectedz3LoopbackAsyncTests._hostpeertest.<locals>.connected   s+    *79c*,DOOH//56r   )r   r   rf   r   r]   rF   )r   rl   
testServerr\   r_   rJ   r`   rm   s   ``      r   _hostpeertestz LoopbackAsyncTests._hostpeertestp   st    
	68 	6
 !^F&.j0A#ZFZF!^F&.j0A#vv.	7 }}Y''r   c                 &    | j                  dd      S )z
        Test that the server gets a transport with a properly functioning
        implementation of L{ITransport.getHost}.
        getHostTro   r   s    r   test_serverHostz"LoopbackAsyncTests.test_serverHost   s    
 !!)T22r   c                 &    | j                  dd      S )zG
        Like C{test_serverHost} but for L{ITransport.getPeer}
        getPeerTrr   r   s    r   test_serverPeerz"LoopbackAsyncTests.test_serverPeer   s     !!)T22r   c                 &    | j                  dd      S )z
        Test that the client gets a transport with a properly functioning
        implementation of L{ITransport.getHost}.
        rq   Frr   )r   rl   s     r   test_clientHostz"LoopbackAsyncTests.test_clientHost   s    
 !!)U33r   c                 &    | j                  dd      S )zH
        Like C{test_clientHost} but for L{ITransport.getPeer}.
        ru   Frr   r   s    r   test_clientPeerz"LoopbackAsyncTests.test_clientPeer   s     !!)U33r   c                      G d dt               } G fddt               }|r  |       } |       }t               x}|_        n |       }t               x}|_         |       }t        j                  ||       |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                       e Zd ZdZd Zy)9LoopbackAsyncTests._greetingtest.<locals>.GreeteeProtocolr   c                     | xj                   |z  c_         | j                   dk(  r| j                  j                  d        y y N   bytes)bytesreceivedr   r   r   s     r   dataReceivedzFLoopbackAsyncTests._greetingtest.<locals>.GreeteeProtocol.dataReceived   s3    

e#
::)MM**40 *r   Nr&   r'   r(   r   r   r)   r   r   GreeteeProtocolr}      s    E1r   r   c                       e Zd Z fdZy)9LoopbackAsyncTests._greetingtest.<locals>.GreeterProtocolc                     dk(  r| j                   j                  d       y | j                   j                  ddg       y )Nwriter   s   byts   es)r3   r   writeSequencer   r   s    r   r   zHLoopbackAsyncTests._greetingtest.<locals>.GreeterProtocol.connectionMade   s3    G#NN((2NN00&%Ar   Nr&   r'   r(   r   r   s   r   GreeterProtocolr      s	    Br   r   )r   r   r   r   r]   )r   r   rn   r   r   r_   r`   rJ   s    `      r   _greetingtestz LoopbackAsyncTests._greetingtest   ss    	1h 	1	Bh 	B $&F$&F"**,A$&F"**,A$&Fvv.r   c                 &    | j                  dd      S )z
        Test that on a connection where the client speaks first, the server
        receives the bytes sent by the client.
        r   Fr   r   s    r   test_clientGreetingz&LoopbackAsyncTests.test_clientGreeting   s    
 !!'511r   c                 &    | j                  dd      S )zz
        Like C{test_clientGreeting}, but use C{writeSequence} instead of
        C{write} to issue the greeting.
        r   Fr   r   s    r   test_clientGreetingSequencez.LoopbackAsyncTests.test_clientGreetingSequence   s    
 !!/599r   c                 &    | j                  dd      S )z
        Test that on a connection where the server speaks first, the client
        receives the bytes sent by the server.
        r   Tr   r   s     r   test_serverGreetingz&LoopbackAsyncTests.test_serverGreeting   s    
 !!'400r   c                 &    | j                  dd      S )zz
        Like C{test_serverGreeting}, but use C{writeSequence} instead of
        C{write} to issue the greeting.
        r   Tr   r   s    r   test_serverGreetingSequencez.LoopbackAsyncTests.test_serverGreetingSequence   s    
 !!/488r   c                    t        dd      D cg c]  }d|fz  
 c} G fddt              } G fddt              } |        |       t               _        t	        j
                         j                  S c c}w )Nr   
   s   %dc                       e Zd Z fdZy);LoopbackAsyncTests._producertest.<locals>.ProducingProtocolc                 |     t                    | _        | j                  j                  | j                         y r   )listproducerstartr3   )r   producerClass	toProduces    r   r   zJLoopbackAsyncTests._producertest.<locals>.ProducingProtocol.connectionMade   s)     -d9o >##DNN3r   Nr   )r   r   s   r   ProducingProtocolr      s    4r   r   c                        e Zd ZdZ fdZy);LoopbackAsyncTests._producertest.<locals>.ReceivingProtocolr   c                     | xj                   |z  c_         | j                   dj                        k(  r| j                  j                  f       y y )Nr   )r   joinr   r   )r   datar`   r_   r   s     r   r   zHLoopbackAsyncTests._producertest.<locals>.ReceivingProtocol.dataReceived   sB    

d"
::)!44MM**FF+;< 5r   Nr   )r`   r_   r   s   r   ReceivingProtocolr      s    E=r   r   )ranger   r   r   r   r]   )r   r   r1   r   r   r`   r_   r   s    `   @@@r   _producertestz LoopbackAsyncTests._producertest   sv    +0B<8aUaT\8		4 	4
	= 	= 	= #$"$"*vv.) 9s   Bc                      t        t               G d d             } j                  |      } fd}|j                  |       |S )zO
        Test a push producer registered against a loopback transport.
        c                   (    e Zd ZdZd Zd Zd Zd Zy):LoopbackAsyncTests.test_pushProducer.<locals>.PushProducerFc                     || _         y r   r   r   r   s     r   r   zCLoopbackAsyncTests.test_pushProducer.<locals>.PushProducer.__init__  r[   r   c                     d| _         y NT)resumedr   s    r   resumeProducingzJLoopbackAsyncTests.test_pushProducer.<locals>.PushProducer.resumeProducing  s	    #r   c                 V    || _         |j                  | d       | j                          y r   )consumerregisterProducer_produceAndScheduler   r   s     r   r   z@LoopbackAsyncTests.test_pushProducer.<locals>.PushProducer.start
  s%     ())$5((*r   c                     | j                   rU| j                  j                  | j                   j                  d             t	        j
                  d| j                         y | j                  j                          y Nr   )r   r   r   popr   	callLaterr   unregisterProducerr   s    r   r   zNLoopbackAsyncTests.test_pushProducer.<locals>.PushProducer._produceAndSchedule  sP    >>MM''(:(:1(=>%%a)A)ABMM446r   N)r&   r'   r(   r   r   r   r   r   r)   r   r   PushProducerr      s    G+$+
7r   r   c                 \    | \  }}j                  |j                  j                  d       y )Nz0Streaming producer should not have been resumed.)assertFalser   r   )resultsr`   r_   r   s      r   finishedz6LoopbackAsyncTests.test_pushProducer.<locals>.finished  s+    &VV''Br   )r   r
   r   rF   )r   r   rJ   r   s   `   r   test_pushProducerz$LoopbackAsyncTests.test_pushProducer   sK    
 
]	#	7 	7 
$	7* |,	 	
hr   c                 \    t        t               G d d             }| j                  |      S )zO
        Test a pull producer registered against a loopback transport.
        c                       e Zd Zd Zd Zd Zy):LoopbackAsyncTests.test_pullProducer.<locals>.PullProducerc                     || _         y r   r   r   s     r   r   zCLoopbackAsyncTests.test_pullProducer.<locals>.PullProducer.__init__)  r[   r   c                 J    || _         | j                   j                  | d       y )NF)r   r   r   s     r   r   z@LoopbackAsyncTests.test_pullProducer.<locals>.PullProducer.start,  s     (..tU;r   c                     | j                   j                  | j                  j                  d             | j                  s| j                   j	                          y y r   )r   r   r   r   r   r   s    r   r   zJLoopbackAsyncTests.test_pullProducer.<locals>.PullProducer.resumeProducing0  s?    ##DNN$6$6q$9:~~MM446 &r   N)r&   r'   r(   r   r   r   r)   r   r   PullProducerr   '  s    +<7r   r   )r   r	   r   )r   r   s     r   test_pullProducerz$LoopbackAsyncTests.test_pullProducer"  s3    
 
]	#	7 	7 
$	7 !!,//r   c                       G d dt               } G d dt               } |       } |       t        j                  |      } fd}|j                  |       |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                       e Zd Zd Zy)9LoopbackAsyncTests.test_writeNotReentrant.<locals>.Serverc                 :    | j                   j                  d       y r   )r3   r   r   s     r   r   zFLoopbackAsyncTests.test_writeNotReentrant.<locals>.Server.dataReceived?  s    $$X.r   N)r&   r'   r(   r   r)   r   r   Serverr   >  s    /r   r   c                   "    e Zd ZdZd Zd Zd Zy)9LoopbackAsyncTests.test_writeNotReentrant.<locals>.ClientFc                 D    t        j                  d| j                         y r   )r   r   gor   s    r   r   zHLoopbackAsyncTests.test_writeNotReentrant.<locals>.Client.connectionMadeE  s    !!!TWW-r   c                 H    | j                   j                  d       d| _        y )N   fooT)r3   r   readyr   s    r   r   z<LoopbackAsyncTests.test_writeNotReentrant.<locals>.Client.goH  s    $$V,!
r   c                 Z    | j                   | _        | j                  j                          y r   )r   wasReadyr3   r4   r   s     r   r   zFLoopbackAsyncTests.test_writeNotReentrant.<locals>.Client.dataReceivedL  s     $

--/r   N)r&   r'   r(   r   r   r   r   r)   r   r   Clientr   B  s    E."0r   r   c                 <    j                  j                         y r   )ri   r   )rC   r`   r   s    r   
cbFinishedz=LoopbackAsyncTests.test_writeNotReentrant.<locals>.cbFinishedT  s    OOFOO,r   )r   r   r]   rF   )r   r   r   r_   rJ   r   r`   s   `     @r   test_writeNotReentrantz)LoopbackAsyncTests.test_writeNotReentrant7  sU    	/X 	/	0X 	0 ""662	- 	
j!r   c                     g fd}t               t               t        j                  |      } j                  g        j                  j                  d       j                  j                  d       j                  j                  d       j                  j                  d       j                  j                           fd}|j                  |       |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                 x    g }| r"|j                  | j                                | r"j                  ||f       y r   )r   rl   )queuetargetr   	pumpCallss      r   dummyPolicyz7LoopbackAsyncTests.test_pumpPolicy.<locals>.dummyPolicyb  s4    EUYY[) fe_-r   r      bar   baz   quuxc                 >    j                  g dfddgfg       y )N)r   r   Nr   r   )r@   )rC   r`   r   r   r_   s    r   
cbCompletez6LoopbackAsyncTests.test_pumpPolicy.<locals>.cbCompletet  s.     12Vff=M4NO	r   )r   r   r]   r@   r3   r   r4   rF   )r   r   r   r   r`   r   r_   s   `   @@@r   test_pumpPolicyz"LoopbackAsyncTests.test_pumpPolicyZ  s     		. ))&&+FB'v&v&v&w''')	 	Z(r   c                     g }t               }|j                  |_        t        j                         }|j                  d       |j                  d       |j                  d       t        j                  ||       | j                  |ddg       y)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putidentityPumpPolicyr@   r   r   r`   r   s       r   test_identityPumpPolicyz*LoopbackAsyncTests.test_identityPumpPolicy  sr    
 #ll'')		&		&		$##E62 01r   c                    g }t               }|j                  |_        t        j                         }|j                  d       |j                  d       |j                  d       t        j                  ||       | j                  |dg       y)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   collapsingPumpPolicyr@   r   s       r   test_collapsingPumpPolicyz,LoopbackAsyncTests.test_collapsingPumpPolicy  so     #ll'')		&		&		$%%eV4,r   N)rq   r   )r&   r'   r(   staticmethodr   r]   rH   ra   ro   rs   rv   rx   rz   r   r   r   r   r   r   r   r   r   r   r   r   r)   r   r   rT   rT   [   sp     6 67L/$(63344!F2:19.%N0*!F#J2"-r   rT   c                   0    e Zd Z eej
                        Zy)LoopbackTCPTestsN)r&   r'   r(   r   r   loopbackTCPrH   r)   r   r   r   r     s     4 45Lr   r   c                   \    e Zd Z eej
                        Z ej                  e	d      dZ
yy)LoopbackUNIXTestsNz-Current reactor does not support UNIX sockets)r&   r'   r(   r   r   loopbackUNIXrH   r   IReactorUNIXr   skipr)   r   r   r   r     s1     5 56Lzw-5> 6r   r   c                   2    e Zd ZdZ G d de      Zd Zd Zy)LoopbackRelayTestz>
    Test for L{twisted.protocols.loopback.LoopbackRelay}
    c                       e Zd ZdZdZd Zy)LoopbackRelayTest.ReceiverzF
        Simple Receiver class used for testing LoopbackRelay
        r   c                 .    | xj                   |z  c_         y)z)Accumulate received data for verificationN)r   )r   r   s     r   r   z'LoopbackRelayTest.Receiver.dataReceived  s    IIIr   N)r&   r'   r(   __doc__r   r   r)   r   r   Receiverr    s    	 	r   r  c                 "   | j                         }t        j                  |      }|j                  d       |j                  d       | j	                  |j
                  d       |j                          | j	                  |j
                  d       y)z8Test to verify that the write function works as expecteds   abcs   defr   s   abcdefN)r  r   LoopbackRelayr   r@   r   clearBufferr   receiverrelays      r   
test_writezLoopbackRelayTest.test_write  si    ==?&&x0FF,	2r   c                 *   | j                         }t        j                  |      }|j                  g d       |j                  g d       | j	                  |j
                  d       |j                          | j	                  |j
                  d       y)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 dogr   s+   The quick brown fox jumps over the lazy dogN)r  r   r  r   r@   r   r  r	  s      r   test_writeSequencez$LoopbackRelayTest.test_writeSequence  sp    ==?&&x0DEBC,(VWr   N)r&   r'   r(   r  r   r  r  r  r)   r   r   r   r     s    	8 	3Xr   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+   r6   TestCaserT   r   r   r   r)   r   r   <module>r     s   
 ' 7 7 + N N . - "%U'' % ,> ,' 'TE-.0A0A E-P
6,h.?.? 6?-x/@/@ ?"X)) "Xr   