
    VhVH                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
mZmZmZ ddlmZmZmZmZmZmZ ddlmZmZ dd	lmZmZ dd
lmZ ddlmZ  G d de      Z G d de      Z  G d de      Z! G d de      Z" G d de      Z#y)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                   p    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zy)ClientCreatorTestsz?
    Tests for L{twisted.internet.protocol.ClientCreator}.
    c                      G d dt               }t               }t        ||      } |||      }|j                  d      }| j	                  ||       y)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                       e Zd Zy):ClientCreatorTests._basicConnectTest.<locals>.SomeProtocolN__name__
__module____qualname__     S/home/dcms/DCMS/lib/python3.12/site-packages/twisted/internet/test/test_protocol.pySomeProtocolr   5       r!   r#   N)r   r   r   buildProtocolassertIsInstance)selfcheckr#   reactorccfactoryprotocols          r"   _basicConnectTestz$ClientCreatorTests._basicConnectTest(   sP    	8 	 %&7L1$((.h5r!   c                 2      fd} j                  |       y)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                     |j                  dddd       | j                  j                         \  }}}}}j                  |d       j                  |d       j                  |d       j                  |d       |S )Nexample.com    )z1.2.3.4i&  )
connectTCP
tcpClientspopassertEqual)r)   r*   hostportr+   timeoutbindAddressr'   s          r"   r(   z1ClientCreatorTests.test_connectTCP.<locals>.checkE   s{    MM-t5FG8?8J8J8N8N8P5D$+T=1T4(Wd+[*;<Nr!   Nr-   r'   r(   s   ` r"   test_connectTCPz"ClientCreatorTests.test_connectTCP>   s    	 	u%r!   c                 2      fd} j                  |       y)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                     |j                  ddd       | j                  j                         \  }}}}j                  |d       j                  |d       j	                  |       |S )N/foo/bar{   T)connectUNIXunixClientsr5   r6   
assertTrue)r)   r*   addressr+   r9   checkPIDr'   s         r"   r(   z2ClientCreatorTests.test_connectUNIX.<locals>.checkW   sa    NN:sD1292E2E2I2I2K/GWgxWj1Wc*OOH%Nr!   Nr;   r<   s   ` r"   test_connectUNIXz#ClientCreatorTests.test_connectUNIXP   s    	 	u%r!   c                 2      fd} j                  |       y)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                 <   t               }|j                  dd|dd       | j                  j                         \  }}}}}}	j	                  |d       	j	                  |d       	j                  ||       	j	                  |d       	j	                  |d       |S )Nr0   r1   r2   )z4.3.2.1i.  )object
connectSSL
sslClientsr5   r6   assertIs)
r)   r*   expectedContextFactoryr7   r8   r+   contextFactoryr9   r:   r'   s
            r"   r(   z1ClientCreatorTests.test_connectSSL.<locals>.checkh   s    %+X"MMt%;TCT ""&&(T=1T4(MM.*@AWd+[*;<Nr!   Nr;   r<   s   ` r"   test_connectSSLz"ClientCreatorTests.test_connectSSLa   s    	( 	u%r!   c                 2   t               }t        |t              } ||      }|j                  j	                         }| j                  |j                         |j                          | j                  |j                         | j                  |t              S )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   
connectorsr5   assertFalse_disconnectedcancelrD   assertFailurer   r'   connectr)   r*   d	connectors         r"   _cancelConnectTestz%ClientCreatorTests._cancelConnectTest~   sw     %&7H-BK&&**,	001	
	//0!!!^44r!   c                 *    d }| j                  |      S )
        The L{Deferred} returned by L{ClientCreator.connectTCP} can be cancelled
        to abort the connection attempt before it completes.
        c                 &    | j                  dd      S Nr0   r1   )r3   r*   s    r"   rX   z9ClientCreatorTests.test_cancelConnectTCP.<locals>.connect   s    ==55r!   r[   r'   rX   s     r"   test_cancelConnectTCPz(ClientCreatorTests.test_cancelConnectTCP   s    	6 &&w//r!   c                 *    d }| j                  |      S )r]   c                 $    | j                  d      S Nr@   )rB   r`   s    r"   rX   z:ClientCreatorTests.test_cancelConnectUNIX.<locals>.connect   s    >>*--r!   ra   rb   s     r"   test_cancelConnectUNIXz)ClientCreatorTests.test_cancelConnectUNIX   s    	. &&w//r!   c                 *    d }| j                  |      S )r]   c                 8    | j                  ddt                     S r_   )rK   rJ   r`   s    r"   rX   z9ClientCreatorTests.test_cancelConnectSSL.<locals>.connect   s    ==fh??r!   ra   rb   s     r"   test_cancelConnectSSLz(ClientCreatorTests.test_cancelConnectSSL   s    	@ &&w//r!   c                    t               }t        |t              } |||      }|j                  j	                         }| j                  t        |j                               d       |j                          | j                  |j                         g        | j                  |j                         | j                  |t              S )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   rR   r5   r6   lengetDelayedCallsrU   rD   rT   rV   r   rW   s         r"   _cancelConnectTimeoutTestz,ClientCreatorTests._cancelConnectTimeoutTest   s     %&7H-GR &&**,	 	W4467; 	

002B7
 		//0!!!^44r!   c                 *    d }| j                  |      S )a  
        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                     |j                  dd      }| j                  j                         \  }}}}}|j                  d       }t	               }	|j                  |	       |S r_   )r3   r4   r5   r%   r   makeConnection)
r)   r*   rY   r7   r8   r+   r9   r:   r,   	transports
             r"   rX   z@ClientCreatorTests.test_cancelConnectTCPTimeout.<locals>.connect   s\    mT2A8?8J8J8N8N8P5D$+,,T2H')I##I.Hr!   ro   rb   s     r"   test_cancelConnectTCPTimeoutz/ClientCreatorTests.test_cancelConnectTCPTimeout       	 --g66r!   c                 *    d }| j                  |      S )a  
        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                     |j                  d      }| j                  j                         \  }}}}|j                  d       }t	               }|j                  |       |S rf   )rB   rC   r5   r%   r   rr   )	r)   r*   rY   rE   r+   r9   r:   r,   rs   s	            r"   rX   zAClientCreatorTests.test_cancelConnectUNIXTimeout.<locals>.connect   sX    z*A5<5H5H5L5L5N2GWg{,,T2H')I##I.Hr!   rt   rb   s     r"   test_cancelConnectUNIXTimeoutz0ClientCreatorTests.test_cancelConnectUNIXTimeout   rv   r!   c                 *    d }| j                  |      S )a  
        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                     |j                  ddt                     }| j                  j                         \  }}}}}}|j	                  d       }	t               }
|	j                  |
       |S r_   )rK   rJ   rL   r5   r%   r   rr   )r)   r*   rY   r7   r8   r+   rO   r9   bindADdressr,   rs   s              r"   rX   z@ClientCreatorTests.test_cancelConnectSSLTimeout.<locals>.connect  sl    mT68<A ""&&(,,T2H')I##I.Hr!   rt   rb   s     r"   test_cancelConnectSSLTimeoutz/ClientCreatorTests.test_cancelConnectSSLTimeout   s    	 --g66r!   c                    t               }t        |t              } |||      \  }}|j                  j	                         }|j                  |t        t        d                   | j                  t        |j                               d       |j                          | j                  |j                         g        | j                  |t              S )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 failurerl   )r   r   r   rR   r5   clientConnectionFailedr   	Exceptionr6   rm   rn   rU   rV   r   )r'   rX   r)   r*   rY   r+   rZ   s          r"   _cancelConnectFailedTimeoutTestz2ClientCreatorTests._cancelConnectFailedTimeoutTest  s     %&7H-Wb)
7&&**,	&&wy)<=>	
 	W4467; 	

002B7!!!^44r!   c                 *    d }| j                  |      S )zz
        Similar to L{test_cancelConnectTCPTimeout}, but for the case where the
        connection attempt fails.
        c                 n    |j                  dd      }| j                  j                         \  }}}}}||fS r_   )r3   r4   r5   )r)   r*   rY   r7   r8   r+   r9   r:   s           r"   rX   zFClientCreatorTests.test_cancelConnectTCPFailedTimeout.<locals>.connect2  s;    mT2A8?8J8J8N8N8P5D$+g:r!   r   rb   s     r"   "test_cancelConnectTCPFailedTimeoutz5ClientCreatorTests.test_cancelConnectTCPFailedTimeout,      	
 33G<<r!   c                 *    d }| j                  |      S )z{
        Similar to L{test_cancelConnectUNIXTimeout}, but for the case where the
        connection attempt fails.
        c                 j    |j                  d      }| j                  j                         \  }}}}||fS rf   )rB   rC   r5   )r)   r*   rY   rE   r+   r9   r:   s          r"   rX   zGClientCreatorTests.test_cancelConnectUNIXFailedTimeout.<locals>.connect?  s7    z*A5<5H5H5L5L5N2GWg{g:r!   r   rb   s     r"   #test_cancelConnectUNIXFailedTimeoutz6ClientCreatorTests.test_cancelConnectUNIXFailedTimeout9  r   r!   c                 *    d }| j                  |      S )zz
        Similar to L{test_cancelConnectSSLTimeout}, but for the case where the
        connection attempt fails.
        c                     |j                  ddt                     }| j                  j                         \  }}}}}}||fS r_   )rK   rJ   rL   r5   )	r)   r*   rY   r7   r8   r+   rO   r9   r|   s	            r"   rX   zFClientCreatorTests.test_cancelConnectSSLFailedTimeout.<locals>.connectL  sK    mT68<A ""&&(g:r!   r   rb   s     r"   "test_cancelConnectSSLFailedTimeoutz5ClientCreatorTests.test_cancelConnectSSLFailedTimeoutF  s    
	 33G<<r!   N)r   r   r   __doc__r-   r=   rG   rP   r[   rc   rg   rj   ro   ru   ry   r}   r   r   r   r   r    r!   r"   r   r   #   sW    6,&$&"&:5,	0	0	0567(7(7652===r!   r   c                   "    e Zd ZdZd Zd Zd Zy)ProtocolTestsz:
    Tests for L{twisted.internet.protocol.Protocol}.
    c                     t               }| j                  t        t        |             | j                  t        t        |             y)zT
        L{Protocol} instances provide L{IProtocol} and L{ILoggingContext}.
        N)r   rD   r   r	   r   )r'   protos     r"   test_interfaceszProtocolTests.test_interfaces`  s1     
Y67_e<=r!   c                 l     G d dt               }| j                  d |       j                                y)zJ
        L{Protocol.logPrefix} returns the protocol class's name.
        c                       e Zd Zy)/ProtocolTests.test_logPrefix.<locals>.SomeThingNr   r    r!   r"   	SomeThingr   m  r$   r!   r   N)r   r6   	logPrefix)r'   r   s     r"   test_logPrefixzProtocolTests.test_logPrefixh  s+    
	 	 	ik&;&;&=>r!   c                     g  G fddt               }t               } |       }|j                  |       | j                  |g       y)zz
        L{Protocol.makeConnection} sets the given transport on itself, and
        then calls C{connectionMade}.
        c                       e Zd Z fdZy)7ProtocolTests.test_makeConnection.<locals>.SomeProtocolc                 <    j                  | j                         y N)appendrs   )r'   results    r"   connectionMadezFProtocolTests.test_makeConnection.<locals>.SomeProtocol.connectionMadez  s    dnn-r!   N)r   r   r   r   r   s   r"   r#   r   y  s    .r!   r#   N)r   rJ   rr   r6   )r'   r#   rs   r,   r   s       @r"   test_makeConnectionz!ProtocolTests.test_makeConnectionr  sF    
 	.8 	. H	>	*)-r!   N)r   r   r   r   r   r   r   r    r!   r"   r   r   [  s    >?.r!   r   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	FactoryTestsz(
    Tests for L{protocol.Factory}.
    c                     t               }| j                  t        t        |             | j                  t        t        |             y)zg
        L{Factory} instances provide both L{IProtocolFactory} and
        L{ILoggingContext}.
        N)r   rD   r   r
   r   )r'   r+   s     r"   r   zFactoryTests.test_interfaces  s2    
 )%5w?@_g>?r!   c                 l     G d dt               }| j                  d |       j                                y)zM
        L{Factory.logPrefix} returns the name of the factory class.
        c                       e Zd Zy)6FactoryTests.test_logPrefix.<locals>.SomeKindOfFactoryNr   r    r!   r"   SomeKindOfFactoryr     r$   r!   r   N)r   r6   r   )r'   r   s     r"   r   zFactoryTests.test_logPrefix  s.    
	 	 	,.?.A.K.K.MNr!   c                      G d dt               }t               }||_        |j                  d      }| j	                  ||       | j                  |j                  |       y)z
        L{Factory.buildProtocol} by default constructs a protocol by calling
        its C{protocol} attribute, and attaches the factory to the result.
        c                       e Zd Zy)<FactoryTests.test_defaultBuildProtocol.<locals>.SomeProtocolNr   r    r!   r"   r#   r     r$   r!   r#   N)r   r   r,   r%   r&   rM   r+   )r'   r#   fr,   s       r"   test_defaultBuildProtocolz&FactoryTests.test_defaultBuildProtocol  sP    	8 	 I!
??4(h5h&&*r!   c                     G d dt               }|j                  t        ddd      }| j                  |j                  t               | j                  |j
                  d       | j                  |j                  ddi       y	)
z
        L{Factory.forProtocol} constructs a Factory, passing along any
        additional arguments, and sets its C{protocol} attribute to the given
        Protocol subclass.
        c                       e Zd Zd Zy)7FactoryTests.test_forProtocol.<locals>.ArgTakingFactoryc                 "    ||c| _         | _        y r   )argskwargs)r'   r   r   s      r"   __init__z@FactoryTests.test_forProtocol.<locals>.ArgTakingFactory.__init__  s    )-v&	4;r!   N)r   r   r   r   r    r!   r"   ArgTakingFactoryr     s    6r!   r   rl         )foo)rl   r   r   N)r   forProtocolr   r6   r,   r   r   )r'   r   r+   s      r"   test_forProtocolzFactoryTests.test_forProtocol  sk    	6w 	6 #..xA2.F))84v.%5r!   c                 P   g t        j                  j                         | j                  fd       t	               }|j                          | j                  d   d   |       | j                  d   d   t        j                         | j                  d   d   d       y)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                  B    t        j                   j                        S r   r   removeObserverr   eventss   r"   <lambda>z;FactoryTests.test_doStartLoggingStatement.<locals>.<lambda>       2 A A&-- P r!   r   r+   	log_level
log_formatzStarting factory {factory!r}N)
r   addObserverr   
addCleanupr   doStartrM   r6   r   info)r'   r   r   s     @r"   test_doStartLoggingStatementz)FactoryTests.test_doStartLoggingStatement  s    
 &&v}}5PQI			fQi	*A.;/?<02PQr!   c                 h   g t        j                  j                         | j                  fd        G d dt              } |       }|j                          | j                  d   d   |       | j                  d   d   t        j                         | j                  d   d   d       y	)
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                  B    t        j                   j                        S r   r   r   s   r"   r   z:FactoryTests.test_doStopLoggingStatement.<locals>.<lambda>  r   r!   c                       e Zd ZdZy);FactoryTests.test_doStopLoggingStatement.<locals>.MyFactoryrl   N)r   r   r   numPortsr    r!   r"   	MyFactoryr     s    Hr!   r   r   r+   r   r   zStopping factory {factory!r}N)
r   r   r   r   r   doStoprM   r6   r   r   )r'   r   r   r   s      @r"   test_doStopLoggingStatementz(FactoryTests.test_doStopLoggingStatement  s    
 &&v}}5PQ	 	 K	
fQi	*A.;/?<02PQr!   N)
r   r   r   r   r   r   r   r   r   r   r    r!   r"   r   r     s)    @O+6 R Rr!   r   c                       e Zd ZdZd Zd Zy)AdapterTestszR
    Tests for L{ProtocolToConsumerAdapter} and L{ConsumerToProtocolAdapter}.
    c                     g }t               }|j                  |_        t        |      }|j	                  d       | j                  |dg       | j                  |t               y)z}
        L{IProtocol} providers can be adapted to L{IConsumer} providers using
        L{ProtocolToConsumerAdapter}.
           helloN)r   r   dataReceivedr   writer6   r&   r   )r'   r   pconsumers       r"   test_protocolToConsumerz$AdapterTests.test_protocolToConsumer  sS    
 JQ<x (,h(ABr!   c                     g t        t               G fdd             } |       }t        |      }|j                  d       | j	                  dg       | j                  |t               y)z}
        L{IConsumer} providers can be adapted to L{IProtocol} providers using
        L{ProtocolToConsumerAdapter}.
        c                       e Zd Z fdZy)6AdapterTests.test_consumerToProtocol.<locals>.Consumerc                 (    j                  |       y r   )r   )r'   rY   r   s     r"   r   z<AdapterTests.test_consumerToProtocol.<locals>.Consumer.write  s    a r!   N)r   r   r   r   r   s   r"   Consumerr     s    !r!   r   r   N)r   r   r	   r   r6   r&   r   )r'   r   cr,   r   s       @r"   test_consumerToProtocolz$AdapterTests.test_consumerToProtocol  si    
 	Y		! 	! 
 	! JQ<h'(,h(ABr!   N)r   r   r   r   r   r   r    r!   r"   r   r     s    CCr!   r   c                       e Zd ZdZd Zd Zy)FileWrapperTestsz2
    L{twisted.internet.protocol.FileWrapper}
    c                 <   t        t                     }|j                  d       | j                  |j                  j                         d       t        t                     }|j                  d       | j                  |j                  j                         d       y)z@
        L{twisted.internet.protocol.FileWrapper.write}
           test1stuffN)r   r   r   r6   filegetvalueassertNotEqualr'   wrappers     r"   
test_writezFileWrapperTests.test_write  sp     gi(h..0(;gi( 	gGLL113W=r!   c                    t        t                     }|j                  ddg       | j                  |j                  j                         d       t        t                     }| j                  t        |j                  ddg       y)zH
        L{twisted.internet.protocol.FileWrapper.writeSequence}
        r   s   test2s
   test1test2test3test4N)r   r   writeSequencer6   r   r   assertRaises	TypeErrorr   s     r"   test_writeSequencez#FileWrapperTests.test_writeSequence  si     gi(x23..0-@gi()W%:%:Wg<NOr!   N)r   r   r   r   r   r   r    r!   r"   r   r     s    >
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>r      s   
  & . 1   I 7 * +u= u=p	%.H %.PXR8 XRv"C8 "CJPx Pr!   