
    \d7                     l   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j                  Z G d deej                  Z G d deej                  Z G 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                   &    e Zd Zd Zd Zd Zd ZdS )SimpleProtocolc                 R    t          j                    | _        g | _        g | _        d S N)r   r   connlinesconnLostselfs    :lib/python3.11/site-packages/twisted/test/test_loopback.py__init__zSimpleProtocol.__init__   s#    N$$	
    c                 :    | j                             d            d S r   )r   callbackr   s    r   connectionMadezSimpleProtocol.connectionMade   s    	4     r   c                 :    | j                             |           d S r   )r   appendr   lines     r   lineReceivedzSimpleProtocol.lineReceived   s    
$r   c                 :    | j                             |           d S r   )r   r   )r   reasons     r   connectionLostzSimpleProtocol.connectionLost   s    V$$$$$r   N)__name__
__module____qualname__r   r   r"   r%    r   r   r   r      sP          
! ! !     % % % % %r   r   c                       e Zd ZdZd ZdS )DoomProtocolr   c                    | xj         dz  c_         | j         dk     r|                     d| j         fz             t                              | |           | j        d         dk    r| j                                         d S d S )N      s   Hello %d   Hello 3)isendLiner   r"   r   	transportloseConnectionr    s     r   r"   zDoomProtocol.lineReceived&   s    !6A:: MM+	1222##D$///:b>Z''N))+++++ ('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dS )LoopbackTestCaseMixinc                      t                      t                      fd}j                            |            fd}t          j         j                  }|                    |           |S )Nc                 d                         d           j                                         d S )N   THIS IS LINE ONE!)r2   r3   r4   resultss    r   	sendALinez<LoopbackTestCaseMixin.testRegularFunction.<locals>.sendALine6   s0    JJ+,,,K&&(((((r   c                                          j        dg                                t          j                  d                                t          j                  d           d S )Nr9   r-   assertEqualr   lenr   ignoredcr<   r   s    r   checkz8LoopbackTestCaseMixin.testRegularFunction.<locals>.check<   sa    QW';&<===S__a000S__a00000r   )r   r   addCallbackr   maybeDeferredloopbackFuncr   r=   rE   drD   r<   s   `   @@r   testRegularFunctionz)LoopbackTestCaseMixin.testRegularFunction2   s    	) 	) 	) 	) 	) 	
9%%%	1 	1 	1 	1 	1 	1 	1
  11a88	er   c                      t                      t                      fd}j                            |            fd}t          j         j                  }|                    |           |S )Nc                 2                         d           d S )N	   DOOM LINE)r2   r:   s    r   r=   z=LoopbackTestCaseMixin.testSneakyHiddenDoom.<locals>.sendALineI   s    JJ|$$$$$r   c                                         j        g d                                j        g d                                t          j                  d                                t          j                  d           d S )N)   Hello 1   Hello 2r0   )rN   rP   rQ   r0   r-   r?   rB   s    r   rE   z9LoopbackTestCaseMixin.testSneakyHiddenDoom.<locals>.checkN   s    QW&J&J&JKKKKKK   S__a000S__a00000r   )r+   r   rF   r   rG   rH   rI   s   `   @@r   testSneakyHiddenDoomz*LoopbackTestCaseMixin.testSneakyHiddenDoomE   s    NNNN	% 	% 	% 	% 	% 	
9%%%	1 	1 	1 	1 	1 	1 	1  11a88	er   N)r&   r'   r(   rK   rR   r)   r   r   r6   r6   1   s2          &    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dS )LoopbackAsyncTestsc                      G d dt                     } |            } |            }t          j        ||           |                     |j                   |                     |j                   dS )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dS )<LoopbackAsyncTests.test_makeConnection.<locals>.TestProtocolNc                     || _         d S 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^   s    	+ 	+ 	+ 	+ 	+8 	+ 	+ 	+ vv...V-...V-.....r   c                 F     G d dt                     }|r. |            }t                      x}|_        t                      }n-t                      } |            }t                      x}|_        t          j        ||            fd}|                    |          S )zJ
        Test one of the permutations of client/server host/peer.
        c                       e Zd Zd ZdS )6LoopbackAsyncTests._hostpeertest.<locals>.TestProtocolc                 d    t          j        | |           | j                            |           d S r   )r   rZ   onConnectionr   rY   s     r   rZ   zELoopbackAsyncTests._hostpeertest.<locals>.TestProtocol.makeConnectionv   s2    'i888!**955555r   N)r&   r'   r(   rZ   r)   r   r   r\   rd   u   s#        6 6 6 6 6r   r\   c                      t          |                       }                    t          j        |                     d S r   )getattr
assertTruer   
providedBy)r3   hostgetr   s     r   	connectedz3LoopbackAsyncTests._hostpeertest.<locals>.connected   s=    *79c**,,DOOH/5566666r   )r   r   rf   r   r]   rF   )r   rl   
testServerr\   r_   rJ   r`   rm   s   ``      r   _hostpeertestz LoopbackAsyncTests._hostpeertestp   s    
	6 	6 	6 	6 	68 	6 	6 	6
  	1!\^^F&.jj0A#ZZFFZZF!\^^F&.jj0A#vv...	7 	7 	7 	7 	7 	7 }}Y'''r   c                 .    |                      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22r   c                 .    |                      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22r   rq   c                 .    |                      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33r   c                 .    |                      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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dS )9LoopbackAsyncTests._greetingtest.<locals>.GreeteeProtocolr   c                 t    | xj         |z  c_         | j         dk    r| j                            d            d S d S N   bytes)bytesreceivedr   r   r   s     r   dataReceivedzFLoopbackAsyncTests._greetingtest.<locals>.GreeteeProtocol.dataReceived   sC    

e#

:))M**400000 *)r   Nr&   r'   r(   r   r   r)   r   r   GreeteeProtocolr}      s(        E1 1 1 1 1r   r   c                       e Zd Z fdZdS )9LoopbackAsyncTests._greetingtest.<locals>.GreeterProtocolc                     dk    r| j                             d           d S | j                             ddg           d S )Nwriter   s   byts   es)r3   r   writeSequencer   r   s    r   r   zHLoopbackAsyncTests._greetingtest.<locals>.GreeterProtocol.connectionMade   sJ    G##N((22222N00&%AAAAAr   Nr&   r'   r(   r   r   s   r   GreeterProtocolr      s5        B B B B B B Br   r   )r   r   r   r   r]   )r   r   rn   r   r   r_   r`   rJ   s    `      r   _greetingtestz LoopbackAsyncTests._greetingtest   s    	1 	1 	1 	1 	1h 	1 	1 	1	B 	B 	B 	B 	B 	B 	Bh 	B 	B 	B  	'$_&&F$_&&F"***,A$_&&F"***,A$_&&Fvv...r   c                 .    |                      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11r   c                 .    |                      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99r   r   c                 .    |                      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00r   c                 .    |                      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88r   c                    d t          dd          D              G fddt                    } G fddt                    } |             |            t                      _        t	          j                   j        S )Nc                     g | ]}d |fz  	S )s   %dr)   ).0r1   s     r   
<listcomp>z4LoopbackAsyncTests._producertest.<locals>.<listcomp>   s    888aUaT\888r   r   
   c                       e Zd Z fdZdS );LoopbackAsyncTests._producertest.<locals>.ProducingProtocolc                      t                              | _        | j                            | j                   d S r   )listproducerstartr3   )r   producerClass	toProduces    r   r   zJLoopbackAsyncTests._producertest.<locals>.ProducingProtocol.connectionMade   s8     -d9oo > >##DN33333r   Nr   )r   r   s   r   ProducingProtocolr      s3        4 4 4 4 4 4 4 4r   r   c                   "    e Zd ZdZ fdZdS );LoopbackAsyncTests._producertest.<locals>.ReceivingProtocolr   c                     | xj         |z  c_         | j         d                              k    r| j                            f           d S d S )Nr   )r   joinr   r   )r   datar`   r_   r   s     r   r   zHLoopbackAsyncTests._producertest.<locals>.ReceivingProtocol.dataReceived   sU    

d"

:)!4!444M**FF+;<<<<< 54r   Nr   )r`   r_   r   s   r   ReceivingProtocolr      s=        E= = = = = = = = =r   r   )ranger   r   r   r   r]   )r   r   r   r   r`   r_   r   s    `  @@@r   _producertestz LoopbackAsyncTests._producertest   s    885B<<888		4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4
	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= #"$$""$$"**vv...r   c                      t          t                     G d d                      }                     |          } fd}|                    |           |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dS ):LoopbackAsyncTests.test_pushProducer.<locals>.PushProducerFc                     || _         d S r   r   r   r   s     r   r   zCLoopbackAsyncTests.test_pushProducer.<locals>.PushProducer.__init__  r[   r   c                     d| _         d S NT)resumedr   s    r   resumeProducingzJLoopbackAsyncTests.test_pushProducer.<locals>.PushProducer.resumeProducing  s    #r   c                 h    || _         |                    | d           |                                  d S r   )consumerregisterProducer_produceAndScheduler   r   s     r   r   z@LoopbackAsyncTests.test_pushProducer.<locals>.PushProducer.start
  s7     ())$555((*****r   c                     | j         rN| j                            | j                             d                     t	          j        d| j                   d S | j                                         d S Nr   )r   r   r   popr   	callLaterr   unregisterProducerr   s    r   r   zNLoopbackAsyncTests.test_pushProducer.<locals>.PushProducer._produceAndSchedule  sh    > 7M''(:(:1(=(=>>>%a)ABBBBBM4466666r   N)r&   r'   r(   r   r   r   r   r   r)   r   r   PushProducerr      sU        G+ + +$ $ $+ + +
7 7 7 7 7r   r   c                 R    | \  }}                     |j        j        d           d S )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   s    
 
]	#	#	7 	7 	7 	7 	7 	7 	7 
$	#	7* |,,	 	 	 	 	 	
hr   c                 z    t          t                     G d d                      }|                     |          S )zO
        Test a pull producer registered against a loopback transport.
        c                        e Zd Zd Zd Zd ZdS ):LoopbackAsyncTests.test_pullProducer.<locals>.PullProducerc                     || _         d S r   r   r   s     r   r   zCLoopbackAsyncTests.test_pullProducer.<locals>.PullProducer.__init__)  r[   r   c                 J    || _         | j                             | d           d S )NF)r   r   r   s     r   r   z@LoopbackAsyncTests.test_pullProducer.<locals>.PullProducer.start,  s'     (..tU;;;;;r   c                     | j                             | j                            d                     | j        s| j                                          d S d S r   )r   r   r   r   r   r   s    r   r   zJLoopbackAsyncTests.test_pullProducer.<locals>.PullProducer.resumeProducing0  sV    ##DN$6$6q$9$9:::~ 7M44666667 7r   N)r&   r'   r(   r   r   r   r)   r   r   PullProducerr   '  sA        + + +< < <7 7 7 7 7r   r   )r   r	   r   )r   r   s     r   test_pullProducerz$LoopbackAsyncTests.test_pullProducer"  sU    
 
]	#	#	7 	7 	7 	7 	7 	7 	7 
$	#	7 !!,///r   c                       G d dt                     } G d dt                     } |            } |            t          j        |          } f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                       e Zd Zd ZdS )9LoopbackAsyncTests.test_writeNotReentrant.<locals>.Serverc                 :    | j                             d           d S 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dS )9LoopbackAsyncTests.test_writeNotReentrant.<locals>.ClientFc                 :    t          j        d| j                   d S r   )r   r   gor   s    r   r   zHLoopbackAsyncTests.test_writeNotReentrant.<locals>.Client.connectionMadeE  s    !!TW-----r   c                 H    | j                             d           d| _        d S )N   fooT)r3   r   readyr   s    r   r   z<LoopbackAsyncTests.test_writeNotReentrant.<locals>.Client.goH  s#    $$V,,,!


r   c                 P    | j         | _        | j                                         d S 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  sF        E. . ." " "0 0 0 0 0r   r   c                 <                         j                   d S r   )ri   r   )rC   r`   r   s    r   
cbFinishedz=LoopbackAsyncTests.test_writeNotReentrant.<locals>.cbFinishedT  s    OOFO,,,,,r   )r   r   r]   rF   )r   r   r   r_   rJ   r   r`   s   `     @r   test_writeNotReentrantz)LoopbackAsyncTests.test_writeNotReentrant7  s    	/ 	/ 	/ 	/ 	/X 	/ 	/ 	/	0 	0 	0 	0 	0X 	0 	0 	0 "6622	- 	- 	- 	- 	- 	- 	
j!!!r   c                     g fd}t                      t                      t          j        |          }                     g            j                            d           j                            d           j                            d           j                            d           j                                          f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                     g }| r)|                     |                                            | )                     ||f           d S r   )r   rl   )queuetargetr   	pumpCallss      r   dummyPolicyz7LoopbackAsyncTests.test_pumpPolicy.<locals>.dummyPolicyb  sR    E *UYY[[)))  *fe_-----r   r      bar   baz   quuxc                 H                         g dfddgfg           d S )N)r   r   Nr   r   )r@   )rC   r`   r   r   r_   s    r   
cbCompletez6LoopbackAsyncTests.test_pumpPolicy.<locals>.cbCompletet  sH     11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FB'''v&&&v&&&v&&&w''''')))	 	 	 	 	 	 	 	 	Z(((r   c                 <   g }t                      }|j        |_        t          j                    }|                    d           |                    d           |                    d           t          j        ||           |                     |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putidentityPumpPolicyr@   r   r   r`   r   s       r   test_identityPumpPolicyz*LoopbackAsyncTests.test_identityPumpPolicy  s    
 #l'))		&		&		$#E6222 011111r   c                 :   g }t                      }|j        |_        t          j                    }|                    d           |                    d           |                    d           t          j        ||           |                     |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   collapsingPumpPolicyr@   r   s       r   test_collapsingPumpPolicyz,LoopbackAsyncTests.test_collapsingPumpPolicy  s     #l'))		&		&		$%eV44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   [   sB       < 677L/ / /$( ( (63 3 33 3 34 4 4 44 4 4! ! !F2 2 2: : :1 1 1 19 9 9  .% % %N0 0 0*! ! !F# # #J2 2 2"- - - - -r   rT   c                   .    e Zd Z eej                  ZdS )LoopbackTCPTestsN)r&   r'   r(   r   r   loopbackTCPrH   r)   r   r   r   r     s         < 455LLLr   r   c                   X    e Zd Z eej                  Z ej        e	d          dZ
dS dS )LoopbackUNIXTestsNz-Current reactor does not support UNIX sockets)r&   r'   r(   r   r   loopbackUNIXrH   r   IReactorUNIXr   skipr)   r   r   r   r     sB        < 566Lzw--5> 65r   r   c                   :    e Zd ZdZ G d de          Zd Zd ZdS )LoopbackRelayTestz>
    Test for L{twisted.protocols.loopback.LoopbackRelay}
    c                       e Zd ZdZdZd ZdS )LoopbackRelayTest.ReceiverzF
        Simple Receiver class used for testing LoopbackRelay
        r   c                 &    | xj         |z  c_         dS )z)Accumulate received data for verificationN)r   )r   r   s     r   r   z'LoopbackRelayTest.Receiver.dataReceived  s    IIIIIIr   N)r&   r'   r(   __doc__r   r   r)   r   r   Receiverr    s4        	 	 	 	 	 	 	r   r  c                 >   |                                  }t          j        |          }|                    d           |                    d           |                     |j        d           |                                 |                     |j        d           dS )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  s    ==??&x00FF,,,	22222r   c                 F   |                                  }t          j        |          }|                    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 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  s    ==??&x00DDDEEEBBBCCC,,,(VWWWWWr   N)r&   r'   r(   r  r   r  r  r  r)   r   r   r  r    sk         	 	 	 	 	8 	 	 	3 3 3X X X X 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 7 7 7 7 7 7 7 7 + + + + + + N N N N N N N N N N . . . . . . - - - - - - - - " " " " " "% % % % %U' % % % , , , , ,> , , ,' ' ' ' ' ' ' 'TE- E- E- E- E-.0A E- E- E-P
6 6 6 6 6,h.? 6 6 6? ? ? ? ?-x/@ ? ? ?"X "X "X "X "X) "X "X "X "X "Xr   