+
    DfjVH                        R t ^ RIHt ^ RIHt ^ RIHt ^ RIHt ^ RI	H
t
HtHtHt ^ RIHtHtHtHtHtHt ^ RIHtHt ^ RIHtHt ^ R	IHt ^ R
IHt  ! R R]4      t ! R R]4      t  ! R R]4      t! ! R R]4      t" ! R R]4      t#R# )z)
Tests for L{twisted.internet.protocol}.
)BytesIO)implementer)verifyObject)CancelledError)	IConsumerILoggingContext	IProtocolIProtocolFactory)ClientCreatorConsumerToProtocolAdapterFactoryFileWrapperProtocolProtocolToConsumerAdapter)MemoryReactorClockStringTransport)LogLevelglobalLogPublisher)Failure)TestCasec                      a  ] tR t^#t o RtR tR tR tR tR t	R t
R tR	 tR
 tR tR tR tR tR tR tR tRtV tR# )ClientCreatorTestsz7
Tests for L{twisted.internet.protocol.ClientCreator}.
c                     ! R R\         4      p\        4       p\        W24      pV! W44      pVP                  R4      pV P	                  Wb4       R# )a  
Helper for implementing a test to verify that one of the I{connect}
methods of L{ClientCreator} passes the right arguments to the right
reactor method.

@param check: A function which will be invoked with a reactor and a
    L{ClientCreator} instance and which should call one of the
    L{ClientCreator}'s I{connect} methods and assert that all of its
    arguments except for the factory are passed on as expected to the
    reactor.  The factory should be returned.
c                       ] tR t^5tRtR# ):ClientCreatorTests._basicConnectTest.<locals>.SomeProtocol N__name__
__module____qualname____firstlineno____static_attributes__r       E/usr/lib/python3/dist-packages/twisted/internet/test/test_protocol.pySomeProtocolr   5       r"   r$   N)r   r   r
   buildProtocolassertIsInstance)selfcheckr$   reactorccfactoryprotocols   &&     r#   _basicConnectTest$ClientCreatorTests._basicConnectTest(   sJ    	8 	 %&71$((.h5r"   c                6   a  V 3R lpS P                  V4       R# )z
L{ClientCreator.connectTCP} calls C{reactor.connectTCP} with the host
and port information passed to it, and with a factory which will
construct the protocol passed to L{ClientCreator.__init__}.
c                    < VP                  R RRR4       V P                  P                  4       w  r#rEpSP                  VR 4       SP                  VR4       SP                  VR4       SP                  VR4       V# )example.com    )z1.2.3.4i&  )
connectTCP
tcpClientspopassertEqual)r*   r+   hostportr,   timeoutbindAddressr(   s   &&     r#   r)   1ClientCreatorTests.test_connectTCP.<locals>.checkE   sw    MM-t5FG8?8J8J8N8N8P5D+T=1T4(Wd+[*;<Nr"   Nr.   r(   r)   s   f r#   test_connectTCP"ClientCreatorTests.test_connectTCP>   s    	 	u%r"   c                6   a  V 3R lpS P                  V4       R# )z
L{ClientCreator.connectUNIX} calls C{reactor.connectUNIX} with the
filename passed to it, and with a factory which will construct the
protocol passed to L{ClientCreator.__init__}.
c                    < VP                  R ^{R4       V P                  P                  4       w  r#rESP                  VR 4       SP                  V^{4       SP	                  V4       V# )/foo/barT)connectUNIXunixClientsr7   r8   
assertTrue)r*   r+   addressr,   r;   checkPIDr(   s   &&    r#   r)   2ClientCreatorTests.test_connectUNIX.<locals>.checkW   s]    NN:sD1292E2E2I2I2K/GgWj1Wc*OOH%Nr"   Nr>   r?   s   f r#   test_connectUNIX#ClientCreatorTests.test_connectUNIXP   s    	 	u%r"   c                6   a  V 3R lpS P                  V4       R# )z
L{ClientCreator.connectSSL} calls C{reactor.connectSSL} with the host,
port, and context factory passed to it, and with a factory which will
construct the protocol passed to L{ClientCreator.__init__}.
c                 :  < \        4       pVP                  R RVRR4       V P                  P                  4       w  ppppppS	P	                  VR 4       S	P	                  VR4       S	P                  Wb4       S	P	                  VR4       S	P	                  VR4       V# )r2   r3   r4   )z4.3.2.1i.  )object
connectSSL
sslClientsr7   r8   assertIs)
r*   r+   expectedContextFactoryr9   r:   r,   contextFactoryr;   r<   r(   s
   &&       r#   r)   1ClientCreatorTests.test_connectSSL.<locals>.checkh   s    %+X"MMt%;TCT ""&&(T=1T4(MM.AWd+[*;<Nr"   Nr>   r?   s   f r#   test_connectSSL"ClientCreatorTests.test_connectSSLa   s    	( 	u%r"   c                2   \        4       p\        V\        4      pV! V4      pVP                  P	                  4       pV P                  VP                  4       VP                  4        V P                  VP                  4       V P                  V\        4      # )a  
Helper for implementing a test to verify that cancellation of the
L{Deferred} returned by one of L{ClientCreator}'s I{connect} methods is
implemented to cancel the underlying connector.

@param connect: A function which will be invoked with a L{ClientCreator}
    instance as an argument and which should call one its I{connect}
    methods and return the result.

@return: A L{Deferred} which fires when the test is complete or fails if
    there is a problem.
)r   r
   r   
connectorsr7   assertFalse_disconnectedcancelrG   assertFailurer   r(   connectr*   r+   d	connectors   &&    r#   _cancelConnectTest%ClientCreatorTests._cancelConnectTest~   sw     %&7H-BK&&**,	001	
	//0!!!^44r"   c                *    R pV P                  V4      # )
The L{Deferred} returned by L{ClientCreator.connectTCP} can be cancelled
to abort the connection attempt before it completes.
c                 &    V P                  R R4      # r2   r3   )r5   r+   s   &r#   r_   9ClientCreatorTests.test_cancelConnectTCP.<locals>.connect   s    ==55r"   rb   r(   r_   s   & r#   test_cancelConnectTCP(ClientCreatorTests.test_cancelConnectTCP   s    	6 &&w//r"   c                *    R pV P                  V4      # )re   c                 $    V P                  R 4      # rD   )rE   rh   s   &r#   r_   :ClientCreatorTests.test_cancelConnectUNIX.<locals>.connect   s    >>*--r"   rj   rk   s   & r#   test_cancelConnectUNIX)ClientCreatorTests.test_cancelConnectUNIX   s    	. &&w//r"   c                *    R pV P                  V4      # )re   c                 8    V P                  R R\        4       4      # rg   )rP   rO   rh   s   &r#   r_   9ClientCreatorTests.test_cancelConnectSSL.<locals>.connect   s    ==fh??r"   rj   rk   s   & r#   test_cancelConnectSSL(ClientCreatorTests.test_cancelConnectSSL   s    	@ &&w//r"   c                   \        4       p\        V\        4      pV! W#4      pVP                  P	                  4       pV P                  \        VP                  4       4      ^4       VP                  4        V P                  VP                  4       . 4       V P                  VP                  4       V P                  V\        4      # )z
Like L{_cancelConnectTest}, but for the case where the L{Deferred} is
cancelled after the connection is set up but before it is fired with the
resulting protocol instance.
)r   r
   r   rY   r7   r8   lengetDelayedCallsr\   rG   r[   r]   r   r^   s   &&    r#   _cancelConnectTimeoutTest,ClientCreatorTests._cancelConnectTimeoutTest   s     %&7H-G &&**,	 	W4467; 	

002B7
 		//0!!!^44r"   c                *    R pV P                  V4      # )ai  
L{ClientCreator.connectTCP} inserts a very short delayed call between
the time the connection is established and the time the L{Deferred}
returned from one of its connect methods actually fires.  If the
L{Deferred} is cancelled in this interval, the established connection is
closed, the timeout is cancelled, and the L{Deferred} fails with
L{CancelledError}.
c                     VP                  R R4      pV P                  P                  4       w  r4rVpVP                  R4      p\	        4       p	VP                  V	4       V# r2   r3   N)r5   r6   r7   r&   r   makeConnection)
r*   r+   r`   r9   r:   r,   r;   r<   r-   	transports
   &&        r#   r_   @ClientCreatorTests.test_cancelConnectTCPTimeout.<locals>.connect   sX    mT2A8?8J8J8N8N8P5D+,,T2H')I##I.Hr"   r|   rk   s   & r#   test_cancelConnectTCPTimeout/ClientCreatorTests.test_cancelConnectTCPTimeout       	 --g66r"   c                *    R pV P                  V4      # )aj  
L{ClientCreator.connectUNIX} inserts a very short delayed call between
the time the connection is established and the time the L{Deferred}
returned from one of its connect methods actually fires.  If the
L{Deferred} is cancelled in this interval, the established connection is
closed, the timeout is cancelled, and the L{Deferred} fails with
L{CancelledError}.
c                     VP                  R 4      pV P                  P                  4       w  r4rVVP                  R4      p\	        4       pVP                  V4       V# )rD   N)rE   rF   r7   r&   r   r   )	r*   r+   r`   rH   r,   r;   r<   r-   r   s	   &&       r#   r_   AClientCreatorTests.test_cancelConnectUNIXTimeout.<locals>.connect   sT    z*A5<5H5H5L5L5N2Gg,,T2H')I##I.Hr"   r   rk   s   & r#   test_cancelConnectUNIXTimeout0ClientCreatorTests.test_cancelConnectUNIXTimeout   r   r"   c                *    R pV P                  V4      # )ai  
L{ClientCreator.connectSSL} inserts a very short delayed call between
the time the connection is established and the time the L{Deferred}
returned from one of its connect methods actually fires.  If the
L{Deferred} is cancelled in this interval, the established connection is
closed, the timeout is cancelled, and the L{Deferred} fails with
L{CancelledError}.
c                     VP                  R R\        4       4      pV P                  P                  4       w  ppppppVP	                  R4      p	\        4       p
V	P                  V
4       V# r   )rP   rO   rQ   r7   r&   r   r   )r*   r+   r`   r9   r:   r,   rT   r;   bindADdressr-   r   s   &&         r#   r_   @ClientCreatorTests.test_cancelConnectSSLTimeout.<locals>.connect  sl    mT68<A ""&&(,,T2H')I##I.Hr"   r   rk   s   & r#   test_cancelConnectSSLTimeout/ClientCreatorTests.test_cancelConnectSSLTimeout   s    	 --g66r"   c                   \        4       p\        V\        4      pV! W#4      w  rEVP                  P	                  4       pVP                  V\        \        R4      4      4       V P                  \        VP                  4       4      ^4       VP                  4        V P                  VP                  4       . 4       V P                  V\        4      # )z
Like L{_cancelConnectTest}, but for the case where the L{Deferred} is
cancelled after the connection attempt has failed but before it is fired
with the resulting failure.
zSimulated failure)r   r
   r   rY   r7   clientConnectionFailedr   	Exceptionr8   rz   r{   r\   r]   r   )r(   r_   r*   r+   r`   r,   ra   s   &&     r#   _cancelConnectFailedTimeoutTest2ClientCreatorTests._cancelConnectFailedTimeoutTest  s     %&7H-W)
&&**,	&&wy)<=>	
 	W4467; 	

002B7!!!^44r"   c                *    R pV P                  V4      # )zb
Similar to L{test_cancelConnectTCPTimeout}, but for the case where the
connection attempt fails.
c                 h    VP                  R R4      pV P                  P                  4       w  r4rVpW%3# rg   )r5   r6   r7   )r*   r+   r`   r9   r:   r,   r;   r<   s   &&      r#   r_   FClientCreatorTests.test_cancelConnectTCPFailedTimeout.<locals>.connect2  s5    mT2A8?8J8J8N8N8P5D+:r"   r   rk   s   & r#   "test_cancelConnectTCPFailedTimeout5ClientCreatorTests.test_cancelConnectTCPFailedTimeout,      	
 33G<<r"   c                *    R pV P                  V4      # )zc
Similar to L{test_cancelConnectUNIXTimeout}, but for the case where the
connection attempt fails.
c                 d    VP                  R 4      pV P                  P                  4       w  r4rVW$3# rp   )rE   rF   r7   )r*   r+   r`   rH   r,   r;   r<   s   &&     r#   r_   GClientCreatorTests.test_cancelConnectUNIXFailedTimeout.<locals>.connect?  s1    z*A5<5H5H5L5L5N2Gg:r"   r   rk   s   & r#   #test_cancelConnectUNIXFailedTimeout6ClientCreatorTests.test_cancelConnectUNIXFailedTimeout9  r   r"   c                *    R pV P                  V4      # )zb
Similar to L{test_cancelConnectSSLTimeout}, but for the case where the
connection attempt fails.
c                     VP                  R R\        4       4      pV P                  P                  4       w  ppppppW%3# rg   )rP   rO   rQ   r7   )	r*   r+   r`   r9   r:   r,   rT   r;   r   s	   &&       r#   r_   FClientCreatorTests.test_cancelConnectSSLFailedTimeout.<locals>.connectL  sI    mT68<A ""&&(:r"   r   rk   s   & r#   "test_cancelConnectSSLFailedTimeout5ClientCreatorTests.test_cancelConnectSSLFailedTimeoutF  s    
	 33G<<r"   r   N)r   r   r   r    __doc__r.   r@   rK   rV   rb   rl   rr   rw   r|   r   r   r   r   r   r   r   r!   __classdictcell____classdict__s   @r#   r   r   #   s_     6,&$&"&:5,	0	0	0567(7(7652=== =r"   r   c                   6   a  ] tR tRt o RtR tR tR tRtV t	R# )ProtocolTestsi[  z2
Tests for L{twisted.internet.protocol.Protocol}.
c                    \        4       pV P                  \        \        V4      4       V P                  \        \        V4      4       R# )zD
L{Protocol} instances provide L{IProtocol} and L{ILoggingContext}.
N)r   rG   r   r   r   )r(   protos   & r#   test_interfacesProtocolTests.test_interfaces`  s1     
Y67_e<=r"   c                n     ! R R\         4      pV P                  RV! 4       P                  4       4       R# )z:
L{Protocol.logPrefix} returns the protocol class's name.
c                       ] tR tRtRtR# )/ProtocolTests.test_logPrefix.<locals>.SomeThingim  r   Nr   r   r"   r#   	SomeThingr   m  r%   r"   r   N)r   r8   	logPrefix)r(   r   s   & r#   test_logPrefixProtocolTests.test_logPrefixh  s+    
	 	 	ik&;&;&=>r"   c                   a . o ! V3R lR\         4      p\        4       pV! 4       pVP                  V4       V P                  SV.4       R# )zb
L{Protocol.makeConnection} sets the given transport on itself, and
then calls C{connectionMade}.
c                   .   <a  ] tR tRt o V3R ltRtV tR# )7ProtocolTests.test_makeConnection.<locals>.SomeProtocoliy  c                >   < SP                  V P                  4       R # N)appendr   )r(   results   &r#   connectionMadeFProtocolTests.test_makeConnection.<locals>.SomeProtocol.connectionMadez  s    dnn-r"   r   N)r   r   r   r    r   r!   r   r   r   s   @r#   r$   r   y  s     . .r"   r$   N)r   rO   r   r8   )r(   r$   r   r-   r   s   &   @r#   test_makeConnection!ProtocolTests.test_makeConnectionr  sF    
 	.8 	. H	>	*)-r"   r   N)
r   r   r   r    r   r   r   r   r!   r   r   s   @r#   r   r   [  s     >?. .r"   r   c                   H   a  ] tR tRt o RtR tR tR tR tR t	R t
R	tV tR
# )FactoryTestsi  z 
Tests for L{protocol.Factory}.
c                    \        4       pV P                  \        \        V4      4       V P                  \        \        V4      4       R# )zO
L{Factory} instances provide both L{IProtocolFactory} and
L{ILoggingContext}.
N)r   rG   r   r	   r   )r(   r,   s   & r#   r   FactoryTests.test_interfaces  s2    
 )%5w?@_g>?r"   c                n     ! R R\         4      pV P                  RV! 4       P                  4       4       R# )z=
L{Factory.logPrefix} returns the name of the factory class.
c                       ] tR tRtRtR# )6FactoryTests.test_logPrefix.<locals>.SomeKindOfFactoryi  r   Nr   r   r"   r#   SomeKindOfFactoryr     r%   r"   r   N)r   r8   r   )r(   r   s   & r#   r   FactoryTests.test_logPrefix  s.    
	 	 	,.?.A.K.K.MNr"   c                     ! R R\         4      p\        4       pWn        VP                  R4      pV P	                  W14       V P                  VP                  V4       R# )z
L{Factory.buildProtocol} by default constructs a protocol by calling
its C{protocol} attribute, and attaches the factory to the result.
c                       ] tR tRtRtR# )<FactoryTests.test_defaultBuildProtocol.<locals>.SomeProtocoli  r   Nr   r   r"   r#   r$   r     r%   r"   r$   N)r   r   r-   r&   r'   rR   r,   )r(   r$   fr-   s   &   r#   test_defaultBuildProtocol&FactoryTests.test_defaultBuildProtocol  sL    	8 	 I!
??4(h5h&&*r"   c                
    ! R R\         4      pVP                  \        ^^^R7      pV P                  VP                  \        4       V P                  VP
                  R4       V P                  VP                  R^/4       R# )z
L{Factory.forProtocol} constructs a Factory, passing along any
additional arguments, and sets its C{protocol} attribute to the given
Protocol subclass.
c                   &   a  ] tR tRt o R tRtV tR# )7FactoryTests.test_forProtocol.<locals>.ArgTakingFactoryi  c                "    WuV n         V n        R # r   argskwargs)r(   r   r   s   &*,r#   __init__@FactoryTests.test_forProtocol.<locals>.ArgTakingFactory.__init__  s    )-&	4;r"   r   N)r   r   r   r    r   r!   r   r   s   @r#   ArgTakingFactoryr     s     6 6r"   r   )foor   N)      )r   forProtocolr   r8   r-   r   r   )r(   r   r,   s   &  r#   test_forProtocolFactoryTests.test_forProtocol  sk    	6w 	6 #..xA2.F))84v.%5r"   c                  a . o\         P                  ! SP                  4       V P                  V3R l4       \	        4       pVP                  4        V P                  S^ ,          R,          V4       V P                  S^ ,          R,          \        P                  4       V P                  S^ ,          R,          R4       R# )z
L{Factory.doStart} logs that it is starting a factory, followed by
the L{repr} of the L{Factory} instance that is being started.
c                  D   < \         P                  ! S P                  4      # r   r   removeObserverr   eventss   r#   <lambda>;FactoryTests.test_doStartLoggingStatement.<locals>.<lambda>       2 A A&-- Pr"   r,   	log_level
log_formatzStarting factory {factory!r}N)
r   addObserverr   
addCleanupr   doStartrR   r8   r   info)r(   r   r   s   & @r#   test_doStartLoggingStatement)FactoryTests.test_doStartLoggingStatement  s    
 &&v}}5PQI			fQi	*A.;/?<02PQr"   c                  a . o\         P                  ! SP                  4       V P                  V3R l4        ! R R\        4      pV! 4       pVP                  4        V P                  S^ ,          R,          V4       V P                  S^ ,          R,          \        P                  4       V P                  S^ ,          R,          R4       R# )	z
L{Factory.doStop} logs that it is stopping a factory, followed by
the L{repr} of the L{Factory} instance that is being stopped.
c                  D   < \         P                  ! S P                  4      # r   r   r   s   r#   r   :FactoryTests.test_doStopLoggingStatement.<locals>.<lambda>  r   r"   c                       ] tR tRt^tRtR# );FactoryTests.test_doStopLoggingStatement.<locals>.MyFactoryi  r   N)r   r   r   r    numPortsr!   r   r"   r#   	MyFactoryr     s    Hr"   r   r,   r   r   zStopping factory {factory!r}N)
r   r   r   r   r   doStoprR   r8   r   r   )r(   r   r   r   s   &  @r#   test_doStopLoggingStatement(FactoryTests.test_doStopLoggingStatement  s    
 &&v}}5PQ	 	 K	
fQi	*A.;/?<02PQr"   r   N)r   r   r   r    r   r   r   r   r   r   r   r!   r   r   s   @r#   r   r     s2     @O+6 R R Rr"   r   c                   0   a  ] tR tRt o RtR tR tRtV tR# )AdapterTestsi  zJ
Tests for L{ProtocolToConsumerAdapter} and L{ConsumerToProtocolAdapter}.
c                    . p\        4       pVP                  Vn        \        V4      pVP	                  R4       V P                  VR.4       V P                  V\        4       R# )ze
L{IProtocol} providers can be adapted to L{IConsumer} providers using
L{ProtocolToConsumerAdapter}.
   helloN)r   r   dataReceivedr   writer8   r'   r   )r(   r   pconsumers   &   r#   test_protocolToConsumer$AdapterTests.test_protocolToConsumer  sS    
 JQ<x (,h(ABr"   c                   a . o\        \        4       ! V3R lR4      4       pV! 4       p\        V4      pVP                  R4       V P	                  SR.4       V P                  V\        4       R# )ze
L{IConsumer} providers can be adapted to L{IProtocol} providers using
L{ProtocolToConsumerAdapter}.
c                   .   <a  ] tR tRt o V3R ltRtV tR# )6AdapterTests.test_consumerToProtocol.<locals>.Consumeri  c                *   < SP                  V4       R # r   )r   )r(   r`   r   s   &&r#   r  <AdapterTests.test_consumerToProtocol.<locals>.Consumer.write  s    a r"   r   N)r   r   r   r    r  r!   r   r   s   @r#   Consumerr    s     ! !r"   r  r  N)r   r   r   r  r8   r'   r   )r(   r  cr-   r   s   &   @r#   test_consumerToProtocol$AdapterTests.test_consumerToProtocol  si    
 	Y		! 	! 
 	! JQ<h'(,h(ABr"   r   N)	r   r   r   r    r   r  r  r!   r   r   s   @r#   r  r    s     CC Cr"   r  c                   0   a  ] tR tRt o RtR tR tRtV tR# )FileWrapperTestsi  z*
L{twisted.internet.protocol.FileWrapper}
c                >   \        \        4       4      pVP                  R4       V P                  VP                  P                  4       R4       \        \        4       4      pVP                  R4       V P                  VP                  P                  4       R4       R# )z0
L{twisted.internet.protocol.FileWrapper.write}
   test1stuffN)r   r   r  r8   filegetvalueassertNotEqualr(   wrappers   & r#   
test_writeFileWrapperTests.test_write  sp     gi(h..0(;gi( 	gGLL113W=r"   c                   \        \        4       4      pVP                  RR.4       V P                  VP                  P                  4       R4       \        \        4       4      pV P                  \        VP                  RR.4       R# )z8
L{twisted.internet.protocol.FileWrapper.writeSequence}
r  s   test2s
   test1test2test3test4N)r   r   writeSequencer8   r  r  assertRaises	TypeErrorr  s   & r#   test_writeSequence#FileWrapperTests.test_writeSequence  si     gi(x23..0-@gi()W%:%:Wg<NOr"   r   N)	r   r   r   r    r   r  r%  r!   r   r   s   @r#   r  r    s     >
P 
Pr"   r  N)$r   ior   zope.interfacer   zope.interface.verifyr   twisted.internet.deferr   twisted.internet.interfacesr   r   r   r	   twisted.internet.protocolr
   r   r   r   r   r   twisted.internet.testingr   r   twisted.loggerr   r   twisted.python.failurer   twisted.trial.unittestr   r   r   r   r  r  r   r"   r#   <module>r1     s   
  & . 1   I 7 * +u= u=p	%.H %.PXR8 XRv"C8 "CJPx Pr"   