o
    ¯bXH  ã                   @   sø   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 dd
lmZmZ ddlmZ G dd„ deƒZG dd„ deƒZ G dd„ deƒZ!G dd„ deƒZ"G dd„ deƒZ#dS )z)
Tests for L{twisted.internet.protocol}.
é    )ÚBytesIO)Úimplementer)ÚverifyObject)ÚCancelledError)Ú	IConsumerÚILoggingContextÚ	IProtocolÚIProtocolFactory)ÚClientCreatorÚConsumerToProtocolAdapterÚFactoryÚFileWrapperÚProtocolÚProtocolToConsumerAdapter)ÚLogLevelÚglobalLogPublisher)ÚFailure)ÚMemoryReactorClockÚStringTransport)ÚTestCasec                   @   s   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"S )#ÚClientCreatorTestsz?
    Tests for L{twisted.internet.protocol.ClientCreator}.
    c                 C   sD   G dd„ dt ƒ}tƒ }t||ƒ}|||ƒ}| d¡}|  ||¡ dS )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dS )z:ClientCreatorTests._basicConnectTest.<locals>.SomeProtocolN©Ú__name__Ú
__module__Ú__qualname__© r   r   úE/usr/lib/python3/dist-packages/twisted/internet/test/test_protocol.pyÚSomeProtocol5   ó    r   N)r   r   r
   ÚbuildProtocolÚassertIsInstance)ÚselfÚcheckr   ÚreactorÚccÚfactoryÚprotocolr   r   r   Ú_basicConnectTest(   s   


z$ClientCreatorTests._basicConnectTestc                    ó   ‡ fdd„}ˆ   |¡ dS )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                    sX   |  dddd¡ | j ¡ \}}}}}ˆ  |d¡ ˆ  |d¡ ˆ  |d¡ ˆ  |d¡ |S )Núexample.coméÒ  éá  )z1.2.3.4i”&  )Ú
connectTCPÚ
tcpClientsÚpopÚassertEqual)r$   r%   ÚhostÚportr&   ÚtimeoutÚbindAddress©r"   r   r   r#   E   s   z1ClientCreatorTests.test_connectTCP.<locals>.checkN©r(   ©r"   r#   r   r5   r   Útest_connectTCP>   s   	z"ClientCreatorTests.test_connectTCPc                    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                    sF   |  ddd¡ | j ¡ \}}}}ˆ  |d¡ ˆ  |d¡ ˆ  |¡ |S )Nú/foo/baré{   T)ÚconnectUNIXÚunixClientsr/   r0   Ú
assertTrue)r$   r%   Úaddressr&   r3   ÚcheckPIDr5   r   r   r#   W   s   
z2ClientCreatorTests.test_connectUNIX.<locals>.checkNr6   r7   r   r5   r   Útest_connectUNIXP   s   z#ClientCreatorTests.test_connectUNIXc                    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           	         sn   t ƒ }| dd|dd¡ | j ¡ \}}}}}}ˆ  |d¡ ˆ  |d¡ ˆ  ||¡ ˆ  |d¡ ˆ  |d¡ |S )Nr*   r+   r,   )z4.3.2.1i.  )ÚobjectÚ
connectSSLÚ
sslClientsr/   r0   ÚassertIs)	r$   r%   ÚexpectedContextFactoryr1   r2   r&   ÚcontextFactoryr3   r4   r5   r   r   r#   h   s$   
ÿ
ùz1ClientCreatorTests.test_connectSSL.<locals>.checkNr6   r7   r   r5   r   Útest_connectSSLa   s   z"ClientCreatorTests.test_connectSSLc                 C   sN   t ƒ }t|tƒ}||ƒ}|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   Ú
connectorsr/   ÚassertFalseÚ_disconnectedÚcancelr=   ÚassertFailurer   ©r"   Úconnectr$   r%   ÚdÚ	connectorr   r   r   Ú_cancelConnectTest~   s   

z%ClientCreatorTests._cancelConnectTestc                 C   ó   dd„ }|   |¡S )ú—
        The L{Deferred} returned by L{ClientCreator.connectTCP} can be cancelled
        to abort the connection attempt before it completes.
        c                 S   s   |   dd¡S ©Nr*   r+   )r-   ©r%   r   r   r   rN   š   s   z9ClientCreatorTests.test_cancelConnectTCP.<locals>.connect©rQ   ©r"   rN   r   r   r   Útest_cancelConnectTCP”   ó   
z(ClientCreatorTests.test_cancelConnectTCPc                 C   rR   )rS   c                 S   s
   |   d¡S ©Nr9   )r;   rU   r   r   r   rN   ¥   s   
z:ClientCreatorTests.test_cancelConnectUNIX.<locals>.connectrV   rW   r   r   r   Útest_cancelConnectUNIXŸ   rY   z)ClientCreatorTests.test_cancelConnectUNIXc                 C   rR   )rS   c                 S   s   |   ddtƒ ¡S rT   )rB   rA   rU   r   r   r   rN   °   ó   z9ClientCreatorTests.test_cancelConnectSSL.<locals>.connectrV   rW   r   r   r   Útest_cancelConnectSSLª   rY   z(ClientCreatorTests.test_cancelConnectSSLc                 C   sh   t ƒ }t|tƒ}|||ƒ}|j ¡ }|  t| ¡ ƒd¡ | ¡  |  | ¡ g ¡ |  	|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   rH   r/   r0   ÚlenÚgetDelayedCallsrK   r=   rJ   rL   r   rM   r   r   r   Ú_cancelConnectTimeoutTestµ   s   


z,ClientCreatorTests._cancelConnectTimeoutTestc                 C   rR   )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           
      S   s>   |  dd¡}| j ¡ \}}}}}| d ¡}tƒ }	| |	¡ |S rT   )r-   r.   r/   r    r   ÚmakeConnection)
r$   r%   rO   r1   r2   r&   r3   r4   r'   Ú	transportr   r   r   rN   Ú   s   

z@ClientCreatorTests.test_cancelConnectTCPTimeout.<locals>.connect©ra   rW   r   r   r   Útest_cancelConnectTCPTimeoutÐ   ó   

z/ClientCreatorTests.test_cancelConnectTCPTimeoutc                 C   rR   )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           	      S   s:   |  d¡}| j ¡ \}}}}| d ¡}tƒ }| |¡ |S rZ   )r;   r<   r/   r    r   rb   )	r$   r%   rO   r>   r&   r3   r4   r'   rc   r   r   r   rN   î   s   


zAClientCreatorTests.test_cancelConnectUNIXTimeout.<locals>.connectrd   rW   r   r   r   Útest_cancelConnectUNIXTimeoutä   rf   z0ClientCreatorTests.test_cancelConnectUNIXTimeoutc                 C   rR   )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                 S   sD   |  ddtƒ ¡}| j ¡ \}}}}}}| d ¡}	tƒ }
|	 |
¡ |S rT   )rB   rA   rC   r/   r    r   rb   )r$   r%   rO   r1   r2   r&   rF   r3   ÚbindADdressr'   rc   r   r   r   rN     s   ù

z@ClientCreatorTests.test_cancelConnectSSLTimeout.<locals>.connectrd   rW   r   r   r   Útest_cancelConnectSSLTimeoutø   s   

z/ClientCreatorTests.test_cancelConnectSSLTimeoutc                 C   st   t ƒ }t|tƒ}|||ƒ\}}|j ¡ }| |ttdƒƒ¡ |  t	| 
¡ ƒd¡ | ¡  |  | 
¡ g ¡ |  |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 failurer^   )r   r
   r   rH   r/   ÚclientConnectionFailedr   Ú	Exceptionr0   r_   r`   rK   rL   r   )r"   rN   r$   r%   rO   r&   rP   r   r   r   Ú_cancelConnectFailedTimeoutTest  s   

ÿz2ClientCreatorTests._cancelConnectFailedTimeoutTestc                 C   rR   )zz
        Similar to L{test_cancelConnectTCPTimeout}, but for the case where the
        connection attempt fails.
        c                 S   s(   |  dd¡}| j ¡ \}}}}}||fS rT   )r-   r.   r/   )r$   r%   rO   r1   r2   r&   r3   r4   r   r   r   rN   2  s   zFClientCreatorTests.test_cancelConnectTCPFailedTimeout.<locals>.connect©rl   rW   r   r   r   Ú"test_cancelConnectTCPFailedTimeout,  ó   
z5ClientCreatorTests.test_cancelConnectTCPFailedTimeoutc                 C   rR   )z{
        Similar to L{test_cancelConnectUNIXTimeout}, but for the case where the
        connection attempt fails.
        c                 S   s$   |  d¡}| j ¡ \}}}}||fS rZ   )r;   r<   r/   )r$   r%   rO   r>   r&   r3   r4   r   r   r   rN   ?  s   
zGClientCreatorTests.test_cancelConnectUNIXFailedTimeout.<locals>.connectrm   rW   r   r   r   Ú#test_cancelConnectUNIXFailedTimeout9  ro   z6ClientCreatorTests.test_cancelConnectUNIXFailedTimeoutc                 C   rR   )zz
        Similar to L{test_cancelConnectSSLTimeout}, but for the case where the
        connection attempt fails.
        c           	      S   s.   |  ddtƒ ¡}| j ¡ \}}}}}}||fS rT   )rB   rA   rC   r/   )	r$   r%   rO   r1   r2   r&   rF   r3   rh   r   r   r   rN   L  s   ùzFClientCreatorTests.test_cancelConnectSSLFailedTimeout.<locals>.connectrm   rW   r   r   r   Ú"test_cancelConnectSSLFailedTimeoutF  s   
z5ClientCreatorTests.test_cancelConnectSSLFailedTimeoutN)r   r   r   Ú__doc__r(   r8   r@   rG   rQ   rX   r[   r]   ra   re   rg   ri   rl   rn   rp   rq   r   r   r   r   r   #   s$    r   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚProtocolTestsz:
    Tests for L{twisted.internet.protocol.Protocol}.
    c                 C   ó*   t ƒ }|  tt|ƒ¡ |  tt|ƒ¡ dS )zT
        L{Protocol} instances provide L{IProtocol} and L{ILoggingContext}.
        N)r   r=   r   r   r   )r"   Úprotor   r   r   Útest_interfaces`  s   zProtocolTests.test_interfacesc                 C   ó&   G dd„ dt ƒ}|  d|ƒ  ¡ ¡ dS )zJ
        L{Protocol.logPrefix} returns the protocol class's name.
        c                   @   r   )z/ProtocolTests.test_logPrefix.<locals>.SomeThingNr   r   r   r   r   Ú	SomeThingm  r   rx   N)r   r0   Ú	logPrefix)r"   rx   r   r   r   Útest_logPrefixh  ó   zProtocolTests.test_logPrefixc                    s@   g ‰ G ‡ fdd„dt ƒ}tƒ }|ƒ }| |¡ |  ˆ |g¡ dS )zz
        L{Protocol.makeConnection} sets the given transport on itself, and
        then calls C{connectionMade}.
        c                       ó   e Zd Z‡ fdd„ZdS )z7ProtocolTests.test_makeConnection.<locals>.SomeProtocolc                    s   ˆ   | j¡ d S ©N)Úappendrc   r5   ©Úresultr   r   ÚconnectionMadez  r\   zFProtocolTests.test_makeConnection.<locals>.SomeProtocol.connectionMadeN)r   r   r   r   r   r   r   r   r   y  s    r   N)r   rA   rb   r0   )r"   r   rc   r'   r   r   r   Útest_makeConnectionr  s   
z!ProtocolTests.test_makeConnectionN)r   r   r   rr   rv   rz   r‚   r   r   r   r   rs   [  s
    
rs   c                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )ÚFactoryTestsz(
    Tests for L{protocol.Factory}.
    c                 C   rt   )zg
        L{Factory} instances provide both L{IProtocolFactory} and
        L{ILoggingContext}.
        N)r   r=   r   r	   r   )r"   r&   r   r   r   rv   ˆ  s   zFactoryTests.test_interfacesc                 C   rw   )zM
        L{Factory.logPrefix} returns the name of the factory class.
        c                   @   r   )z6FactoryTests.test_logPrefix.<locals>.SomeKindOfFactoryNr   r   r   r   r   ÚSomeKindOfFactory–  r   r„   N)r   r0   ry   )r"   r„   r   r   r   rz   ‘  r{   zFactoryTests.test_logPrefixc                 C   sD   G dd„ dt ƒ}tƒ }||_| d¡}|  ||¡ |  |j|¡ dS )z¡
        L{Factory.buildProtocol} by default constructs a protocol by calling
        its C{protocol} attribute, and attaches the factory to the result.
        c                   @   r   )z<FactoryTests.test_defaultBuildProtocol.<locals>.SomeProtocolNr   r   r   r   r   r   ¡  r   r   N)r   r   r'   r    r!   rD   r&   )r"   r   Úfr'   r   r   r   Útest_defaultBuildProtocol›  s   
z&FactoryTests.test_defaultBuildProtocolc                 C   sT   G dd„ dt ƒ}|jtdddd}|  |jt¡ |  |jd¡ |  |jddi¡ d	S )
z¹
        L{Factory.forProtocol} constructs a Factory, passing along any
        additional arguments, and sets its C{protocol} attribute to the given
        Protocol subclass.
        c                   @   s   e Zd Zdd„ ZdS )z7FactoryTests.test_forProtocol.<locals>.ArgTakingFactoryc                 _   s   ||| _ | _d S r}   )ÚargsÚkwargs)r"   r‡   rˆ   r   r   r   Ú__init__²  s   z@FactoryTests.test_forProtocol.<locals>.ArgTakingFactory.__init__N)r   r   r   r‰   r   r   r   r   ÚArgTakingFactory±  s    rŠ   r^   é   é   )Úfoo)r^   r‹   r   N)r   ÚforProtocolr   r0   r'   r‡   rˆ   )r"   rŠ   r&   r   r   r   Útest_forProtocolª  s
   zFactoryTests.test_forProtocolc                    sr   g ‰ t  ˆ j¡ |  ‡ fdd„¡ tƒ }| ¡  |  ˆ d d |¡ |  ˆ d d tj	¡ |  ˆ d d d¡ dS )	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                      ó   t  ˆ j¡S r}   ©r   ÚremoveObserverr~   r   ©Úeventsr   r   Ú<lambda>Á  ó    z;FactoryTests.test_doStartLoggingStatement.<locals>.<lambda>r   r&   Ú	log_levelÚ
log_formatzStarting factory {factory!r}N)
r   ÚaddObserverr~   Ú
addCleanupr   ÚdoStartrD   r0   r   Úinfo)r"   r…   r   r“   r   Útest_doStartLoggingStatementº  s   z)FactoryTests.test_doStartLoggingStatementc                    s‚   g ‰ t  ˆ j¡ |  ‡ fdd„¡ G dd„ dtƒ}|ƒ }| ¡  |  ˆ d d |¡ |  ˆ d d tj	¡ |  ˆ d d d	¡ d
S )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                      r   r}   r‘   r   r“   r   r   r•   Ñ  r–   z:FactoryTests.test_doStopLoggingStatement.<locals>.<lambda>c                   @   s   e Zd ZdZdS )z;FactoryTests.test_doStopLoggingStatement.<locals>.MyFactoryr^   N)r   r   r   ÚnumPortsr   r   r   r   Ú	MyFactoryÓ  s    rŸ   r   r&   r—   r˜   zStopping factory {factory!r}N)
r   r™   r~   rš   r   ÚdoStoprD   r0   r   rœ   )r"   rŸ   r…   r   r“   r   Útest_doStopLoggingStatementÊ  s   z(FactoryTests.test_doStopLoggingStatementN)
r   r   r   rr   rv   rz   r†   r   r   r¡   r   r   r   r   rƒ   ƒ  s    	
rƒ   c                   @   ó    e Zd ZdZdd„ Zdd„ ZdS )ÚAdapterTestszR
    Tests for L{ProtocolToConsumerAdapter} and L{ConsumerToProtocolAdapter}.
    c                 C   sB   g }t ƒ }|j|_t|ƒ}| d¡ |  |dg¡ |  |t¡ dS )z}
        L{IProtocol} providers can be adapted to L{IConsumer} providers using
        L{ProtocolToConsumerAdapter}.
        ó   helloN)r   r~   ÚdataReceivedr   Úwriter0   r!   r   )r"   r€   ÚpÚconsumerr   r   r   Útest_protocolToConsumerã  s   
z$AdapterTests.test_protocolToConsumerc                    sT   g ‰ t tƒG ‡ fdd„dƒƒ}|ƒ }t|ƒ}| d¡ |  ˆ dg¡ |  |t¡ dS )z}
        L{IConsumer} providers can be adapted to L{IProtocol} providers using
        L{ProtocolToConsumerAdapter}.
        c                       r|   )z6AdapterTests.test_consumerToProtocol.<locals>.Consumerc                    s   ˆ   |¡ d S r}   )r~   )r"   rO   r   r   r   r¦   ù  s   z<AdapterTests.test_consumerToProtocol.<locals>.Consumer.writeN)r   r   r   r¦   r   r   r   r   ÚConsumer÷  s    rª   r¤   N)r   r   r   r¥   r0   r!   r   )r"   rª   Úcr'   r   r   r   Útest_consumerToProtocolð  s   
z$AdapterTests.test_consumerToProtocolN)r   r   r   rr   r©   r¬   r   r   r   r   r£   Þ  s    r£   c                   @   r¢   )ÚFileWrapperTestsz2
    L{twisted.internet.protocol.FileWrapper}
    c                 C   sP   t tƒ ƒ}| d¡ |  |j ¡ d¡ t tƒ ƒ}| d¡ |  |j ¡ d¡ dS )z@
        L{twisted.internet.protocol.FileWrapper.write}
        ó   test1ÚstuffN)r   r   r¦   r0   ÚfileÚgetvalueÚassertNotEqual©r"   Úwrapperr   r   r   Ú
test_write  s   



zFileWrapperTests.test_writec                 C   sL   t tƒ ƒ}| ddg¡ |  |j ¡ d¡ t tƒ ƒ}|  t|jddg¡ dS )zH
        L{twisted.internet.protocol.FileWrapper.writeSequence}
        r®   s   test2s
   test1test2Útest3Útest4N)r   r   ÚwriteSequencer0   r°   r±   ÚassertRaisesÚ	TypeErrorr³   r   r   r   Útest_writeSequence  s
   

z#FileWrapperTests.test_writeSequenceN)r   r   r   rr   rµ   r»   r   r   r   r   r­     s    r­   N)$rr   Úior   Úzope.interfacer   Úzope.interface.verifyr   Útwisted.internet.deferr   Útwisted.internet.interfacesr   r   r   r	   Útwisted.internet.protocolr
   r   r   r   r   r   Útwisted.loggerr   r   Útwisted.python.failurer   Útwisted.test.proto_helpersr   r   Útwisted.trial.unittestr   r   rs   rƒ   r£   r­   r   r   r   r   Ú<module>   s$      :([%