U
    �AcjVH  ã                   @   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mZ dd
l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)ÚMemoryReactorClockÚStringTransport)ÚLogLevelÚglobalLogPublisher)ÚFailure)Ú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                   @   s   e Zd ZdS )z:ClientCreatorTests._basicConnectTest.<locals>.SomeProtocolN©Ú__name__Ú
__module__Ú__qualname__© r   r   úG/tmp/pip-unpacked-wheel-nrvc_22y/twisted/internet/test/test_protocol.pyÚSomeProtocol5   s   r   N)r   r   r
   ÚbuildProtocolÚassertIsInstance)ÚselfÚcheckr   ÚreactorÚccÚfactoryÚprotocolr   r   r   Ú_basicConnectTest(   s    


z$ClientCreatorTests._basicConnectTestc                    s   ‡ 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   r2   r   Útest_connectTCP>   s    	z"ClientCreatorTests.test_connectTCPc                    s   ‡ fdd„}ˆ   |¡ dS )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,   r-   Ú
assertTrue)r"   r#   Úaddressr$   r0   ZcheckPIDr2   r   r   r!   W   s    
z2ClientCreatorTests.test_connectUNIX.<locals>.checkNr3   r4   r   r2   r   Útest_connectUNIXP   s    z#ClientCreatorTests.test_connectUNIXc                    s   ‡ fdd„}ˆ   |¡ dS )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,   r-   ÚassertIs)	r"   r#   ZexpectedContextFactoryr.   r/   r$   ÚcontextFactoryr0   r1   r2   r   r   r!   h   s,        ÿ
ùz1ClientCreatorTests.test_connectSSL.<locals>.checkNr3   r4   r   r2   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,   ZassertFalseÚ_disconnectedÚcancelr:   ÚassertFailurer   ©r    Úconnectr"   r#   ÚdÚ	connectorr   r   r   Ú_cancelConnectTest~   s    

z%ClientCreatorTests._cancelConnectTestc                 C   s   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   rH   š   s    z9ClientCreatorTests.test_cancelConnectTCP.<locals>.connect©rK   ©r    rH   r   r   r   Útest_cancelConnectTCP”   s    z(ClientCreatorTests.test_cancelConnectTCPc                 C   s   dd„ }|   |¡S )rL   c                 S   s
   |   d¡S ©Nr6   )r8   rN   r   r   r   rH   ¥   s    z:ClientCreatorTests.test_cancelConnectUNIX.<locals>.connectrO   rP   r   r   r   Útest_cancelConnectUNIXŸ   s    z)ClientCreatorTests.test_cancelConnectUNIXc                 C   s   dd„ }|   |¡S )rL   c                 S   s   |   ddtƒ ¡S rM   )r>   r=   rN   r   r   r   rH   °   s    z9ClientCreatorTests.test_cancelConnectSSL.<locals>.connectrO   rP   r   r   r   Útest_cancelConnectSSLª   s    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   rC   r,   r-   ÚlenÚgetDelayedCallsrE   r:   rD   rF   r   rG   r   r   r   Ú_cancelConnectTimeoutTestµ   s    


z,ClientCreatorTests._cancelConnectTimeoutTestc                 C   s   dd„ }|   |¡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           
      S   s>   |  dd¡}| j ¡ \}}}}}| d ¡}tƒ }	| |	¡ |S rM   )r*   r+   r,   r   r   ÚmakeConnection)
r"   r#   rI   r.   r/   r$   r0   r1   r%   Ú	transportr   r   r   rH   Ú   s    

z@ClientCreatorTests.test_cancelConnectTCPTimeout.<locals>.connect©rX   rP   r   r   r   Útest_cancelConnectTCPTimeoutÐ   s    
z/ClientCreatorTests.test_cancelConnectTCPTimeoutc                 C   s   dd„ }|   |¡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           	      S   s:   |  d¡}| j ¡ \}}}}| d ¡}tƒ }| |¡ |S rR   )r8   r9   r,   r   r   rY   )	r"   r#   rI   r;   r$   r0   r1   r%   rZ   r   r   r   rH   î   s    


zAClientCreatorTests.test_cancelConnectUNIXTimeout.<locals>.connectr[   rP   r   r   r   Útest_cancelConnectUNIXTimeoutä   s    
z0ClientCreatorTests.test_cancelConnectUNIXTimeoutc                 C   s   dd„ }|   |¡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                 S   sD   |  ddtƒ ¡}| j ¡ \}}}}}}| d ¡}	tƒ }
|	 |
¡ |S rM   )r>   r=   r?   r,   r   r   rY   )r"   r#   rI   r.   r/   r$   rA   r0   ÚbindADdressr%   rZ   r   r   r   rH     s    ù

z@ClientCreatorTests.test_cancelConnectSSLTimeout.<locals>.connectr[   rP   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 failurerU   )r   r
   r   rC   r,   ZclientConnectionFailedr   Ú	Exceptionr-   rV   rW   rE   rF   r   )r    rH   r"   r#   rI   r$   rJ   r   r   r   Ú_cancelConnectFailedTimeoutTest  s    

 
ÿz2ClientCreatorTests._cancelConnectFailedTimeoutTestc                 C   s   dd„ }|   |¡S )zz
        Similar to L{test_cancelConnectTCPTimeout}, but for the case where the
        connection attempt fails.
        c                 S   s(   |  dd¡}| j ¡ \}}}}}||fS rM   )r*   r+   r,   )r"   r#   rI   r.   r/   r$   r0   r1   r   r   r   rH   2  s    zFClientCreatorTests.test_cancelConnectTCPFailedTimeout.<locals>.connect©ra   rP   r   r   r   Ú"test_cancelConnectTCPFailedTimeout,  s    z5ClientCreatorTests.test_cancelConnectTCPFailedTimeoutc                 C   s   dd„ }|   |¡S )z{
        Similar to L{test_cancelConnectUNIXTimeout}, but for the case where the
        connection attempt fails.
        c                 S   s$   |  d¡}| j ¡ \}}}}||fS rR   )r8   r9   r,   )r"   r#   rI   r;   r$   r0   r1   r   r   r   rH   ?  s    
zGClientCreatorTests.test_cancelConnectUNIXFailedTimeout.<locals>.connectrb   rP   r   r   r   Ú#test_cancelConnectUNIXFailedTimeout9  s    z6ClientCreatorTests.test_cancelConnectUNIXFailedTimeoutc                 C   s   dd„ }|   |¡S )zz
        Similar to L{test_cancelConnectSSLTimeout}, but for the case where the
        connection attempt fails.
        c           	      S   s.   |  ddtƒ ¡}| j ¡ \}}}}}}||fS rM   )r>   r=   r?   r,   )	r"   r#   rI   r.   r/   r$   rA   r0   r^   r   r   r   rH   L  s    ùzFClientCreatorTests.test_cancelConnectSSLFailedTimeout.<locals>.connectrb   rP   r   r   r   Ú"test_cancelConnectSSLFailedTimeoutF  s    z5ClientCreatorTests.test_cancelConnectSSLFailedTimeoutN)r   r   r   Ú__doc__r&   r5   r<   rB   rK   rQ   rS   rT   rX   r\   r]   r_   ra   rc   rd   re   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   s*   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   s&   G dd„ dt ƒ}|  d|ƒ  ¡ ¡ dS )zJ
        L{Protocol.logPrefix} returns the protocol class's name.
        c                   @   s   e Zd ZdS )z/ProtocolTests.test_logPrefix.<locals>.SomeThingNr   r   r   r   r   Ú	SomeThingm  s   rj   N)r   r-   Ú	logPrefix)r    rj   r   r   r   Útest_logPrefixh  s    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                       s   e Zd Z‡ fdd„ZdS )z7ProtocolTests.test_makeConnection.<locals>.SomeProtocolc                    s   ˆ   | j¡ d S ©N)ÚappendrZ   r2   ©Úresultr   r   ÚconnectionMadez  s    zFProtocolTests.test_makeConnection.<locals>.SomeProtocol.connectionMadeN)r   r   r   rq   r   ro   r   r   r   y  s   r   N)r   r=   rY   r-   )r    r   rZ   r%   r   ro   r   Útest_makeConnectionr  s    
z!ProtocolTests.test_makeConnectionN)r   r   r   rf   ri   rl   rr   r   r   r   r   rg   [  s   
rg   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   s*   t ƒ }|  tt|ƒ¡ |  tt|ƒ¡ dS )zg
        L{Factory} instances provide both L{IProtocolFactory} and
        L{ILoggingContext}.
        N)r   r:   r   r	   r   )r    r$   r   r   r   ri   ˆ  s    zFactoryTests.test_interfacesc                 C   s&   G dd„ dt ƒ}|  d|ƒ  ¡ ¡ dS )zM
        L{Factory.logPrefix} returns the name of the factory class.
        c                   @   s   e Zd ZdS )z6FactoryTests.test_logPrefix.<locals>.SomeKindOfFactoryNr   r   r   r   r   ÚSomeKindOfFactory–  s   rt   N)r   r-   rk   )r    rt   r   r   r   rl   ‘  s    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                   @   s   e Zd ZdS )z<FactoryTests.test_defaultBuildProtocol.<locals>.SomeProtocolNr   r   r   r   r   r   ¡  s   r   N)r   r   r%   r   r   r@   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 rm   )ÚargsÚkwargs)r    rw   rx   r   r   r   Ú__init__²  s    z@FactoryTests.test_forProtocol.<locals>.ArgTakingFactory.__init__N)r   r   r   ry   r   r   r   r   ÚArgTakingFactory±  s   rz   rU   é   é   )Úfoo)rU   r{   r}   N)r   ZforProtocolr   r-   r%   rw   rx   )r    rz   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                      s   t  ˆ j¡S rm   ©r   ZremoveObserverrn   r   ©Úeventsr   r   Ú<lambda>Á  ó    z;FactoryTests.test_doStartLoggingStatement.<locals>.<lambda>r   r$   Ú	log_levelÚ
log_formatzStarting factory {factory!r}N)
r   ÚaddObserverrn   Ú
addCleanupr   ZdoStartr@   r-   r   Úinfo)r    ru   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                      s   t  ˆ j¡S rm   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>.MyFactoryrU   N)r   r   r   ZnumPortsr   r   r   r   Ú	MyFactoryÓ  s   rŠ   r   r$   r„   r…   zStopping factory {factory!r}N)
r   r†   rn   r‡   r   ZdoStopr@   r-   r   rˆ   )r    rŠ   ru   r   r€   r   Útest_doStopLoggingStatementÊ  s    z(FactoryTests.test_doStopLoggingStatementN)
r   r   r   rf   ri   rl   rv   r~   r‰   r‹   r   r   r   r   rs   ƒ  s   	
rs   c                   @   s    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   rn   ÚdataReceivedr   Úwriter-   r   r   )r    rp   ÚpZ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                       s   e Zd Z‡ fdd„ZdS )z6AdapterTests.test_consumerToProtocol.<locals>.Consumerc                    s   ˆ   |¡ d S rm   )rn   )r    rI   ro   r   r   r�   ù  s    z<AdapterTests.test_consumerToProtocol.<locals>.Consumer.writeN)r   r   r   r�   r   ro   r   r   ÚConsumer÷  s   r’   r�   N)r   r   r   rŽ   r-   r   r   )r    r’   Úcr%   r   ro   r   Útest_consumerToProtocolð  s    
z$AdapterTests.test_consumerToProtocolN)r   r   r   rf   r‘   r”   r   r   r   r   rŒ   Þ  s   rŒ   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú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�   r-   ÚfileÚgetvalueZ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
   test1test2Ztest3Ztest4N)r   r   ZwriteSequencer-   r˜   r™   ÚassertRaisesÚ	TypeErrorrš   r   r   r   Útest_writeSequence  s
    

z#FileWrapperTests.test_writeSequenceN)r   r   r   rf   rœ   rŸ   r   r   r   r   r•     s   r•   N)$rf   Úior   Zzope.interfacer   Zzope.interface.verifyr   Ztwisted.internet.deferr   Ztwisted.internet.interfacesr   r   r   r	   Ztwisted.internet.protocolr
   r   r   r   r   r   Ztwisted.internet.testingr   r   Ztwisted.loggerr   r   Ztwisted.python.failurer   Ztwisted.trial.unittestr   r   rg   rs   rŒ   r•   r   r   r   r   Ú<module>   s"      :([%