U
    
W['I  ã                   @   s  d Z ddlmZm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 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}.
é    )ÚdivisionÚabsolute_import)ÚBytesIO)Úimplementer)ÚverifyObject)ÚCancelledError)Ú	IProtocolÚILoggingContextÚIProtocolFactoryÚ	IConsumer)ÚProtocolÚClientCreatorÚFactoryÚProtocolToConsumerAdapterÚConsumerToProtocolAdapterÚFileWrapper)ÚLogLevelÚglobalLogPublisher)Ú_PY3)Ú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                   @   s   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ÚSomeProtocol-   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$   =   s    z1ClientCreatorTests.test_connectTCP.<locals>.checkN©r)   ©r#   r$   r   r5   r   Útest_connectTCP7   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/   r0   Ú
assertTrue)r%   r&   Úaddressr'   r3   ZcheckPIDr5   r   r   r$   N   s    
z2ClientCreatorTests.test_connectUNIX.<locals>.checkNr6   r7   r   r5   r   Útest_connectUNIXH   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/   r0   ÚassertIs)	r%   r&   ZexpectedContextFactoryr1   r2   r'   ÚcontextFactoryr3   r4   r5   r   r   r$   ^   s    z1ClientCreatorTests.test_connectSSL.<locals>.checkNr6   r7   r   r5   r   Útest_connectSSLX   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   Ú_cancelConnectTestk   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   rK   ‡   s    z9ClientCreatorTests.test_cancelConnectTCP.<locals>.connect©rN   ©r#   rK   r   r   r   Útest_cancelConnectTCP‚   s    z(ClientCreatorTests.test_cancelConnectTCPc                 C   s   dd„ }|   |¡S )rO   c                 S   s
   |   d¡S ©Nr9   )r;   rQ   r   r   r   rK   ‘   s    z:ClientCreatorTests.test_cancelConnectUNIX.<locals>.connectrR   rS   r   r   r   Útest_cancelConnectUNIXŒ   s    z)ClientCreatorTests.test_cancelConnectUNIXc                 C   s   dd„ }|   |¡S )rO   c                 S   s   |   ddtƒ ¡S rP   )rA   r@   rQ   r   r   r   rK   ›   s    z9ClientCreatorTests.test_cancelConnectSSL.<locals>.connectrR   rS   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   rF   r/   r0   ÚlenÚgetDelayedCallsrH   r=   rG   rI   r   rJ   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 rP   )r-   r.   r/   r!   r   ÚmakeConnection)
r%   r&   rL   r1   r2   r'   r3   r4   r(   Ú	transportr   r   r   rK   Å   s    

z@ClientCreatorTests.test_cancelConnectTCPTimeout.<locals>.connect©r[   rS   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 rU   )r;   r<   r/   r!   r   r\   )	r%   r&   rL   r>   r'   r3   r4   r(   r]   r   r   r   rK   Ø   s    


zAClientCreatorTests.test_cancelConnectUNIXTimeout.<locals>.connectr^   rS   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 rP   )rA   r@   rB   r/   r!   r   r\   )r%   r&   rL   r1   r2   r'   rD   r3   ÚbindADdressr(   r]   r   r   r   rK   ë   s    

z@ClientCreatorTests.test_cancelConnectSSLTimeout.<locals>.connectr^   rS   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 failurerX   )r   r   r   rF   r/   ZclientConnectionFailedr   Ú	Exceptionr0   rY   rZ   rH   rI   r   )r#   rK   r%   r&   rL   r'   rM   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 rP   )r-   r.   r/   )r%   r&   rL   r1   r2   r'   r3   r4   r   r   r   rK     s    zFClientCreatorTests.test_cancelConnectTCPFailedTimeout.<locals>.connect©rd   rS   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 rU   )r;   r<   r/   )r%   r&   rL   r>   r'   r3   r4   r   r   r   rK     s    
zGClientCreatorTests.test_cancelConnectUNIXFailedTimeout.<locals>.connectre   rS   r   r   r   Ú#test_cancelConnectUNIXFailedTimeout  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 rP   )rA   r@   rB   r/   )	r%   r&   rL   r1   r2   r'   rD   r3   ra   r   r   r   rK   +  s    zFClientCreatorTests.test_cancelConnectSSLFailedTimeout.<locals>.connectre   rS   r   r   r   Ú"test_cancelConnectSSLFailedTimeout&  s    z5ClientCreatorTests.test_cancelConnectSSLFailedTimeoutN)r   r   r   Ú__doc__r)   r8   r?   rE   rN   rT   rV   rW   r[   r_   r`   rb   rd   rf   rg   rh   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_interfaces7  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   Ú	SomeThingD  s   rm   N)r   r0   Ú	logPrefix)r#   rm   r   r   r   Útest_logPrefix@  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)Úappendr]   r5   ©Úresultr   r   ÚconnectionMadeP  s    zFProtocolTests.test_makeConnection.<locals>.SomeProtocol.connectionMadeN)r   r   r   rt   r   rr   r   r   r    O  s   r    N)r   r@   r\   r0   )r#   r    r]   r(   r   rr   r   Útest_makeConnectionI  s    
z!ProtocolTests.test_makeConnectionN)r   r   r   ri   rl   ro   ru   r   r   r   r   rj   3  s   		rj   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   rl   _  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   ÚSomeKindOfFactorym  s   rw   N)r   r0   rn   )r#   rw   r   r   r   ro   i  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    x  s   r    N)r   r   r(   r!   r"   rC   r'   )r#   r    Úfr(   r   r   r   Útest_defaultBuildProtocols  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 rp   )ÚargsÚkwargs)r#   rz   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}   rX   é   é   )Úfoo)rX   r~   r€   N)r   ZforProtocolr   r0   r(   rz   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                      s   t  ˆ j¡S rp   ©r   ZremoveObserverrq   r   ©Zeventsr   r   Ú<lambda>˜  ó    z;FactoryTests.test_doStartLoggingStatement.<locals>.<lambda>r   r'   Ú	log_levelÚ
log_formatzStarting factory {factory!r}N)
r   ÚaddObserverrq   Ú
addCleanupr   ZdoStartrC   r0   r   Úinfo)r#   rx   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 rp   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>.MyFactoryrX   N)r   r   r   ZnumPortsr   r   r   r   Ú	MyFactory­  s   rŒ   r   r'   r†   r‡   zStopping factory {factory!r}N)
r   rˆ   rq   r‰   r   ZdoStoprC   r0   r   rŠ   )r#   rŒ   rx   r   rƒ   r   Útest_doStopLoggingStatement£  s    
ÿÿz(FactoryTests.test_doStopLoggingStatementN)
r   r   r   ri   rl   ro   ry   r   r‹   r   r   r   r   r   rv   Z  s   

rv   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   rq   ÚdataReceivedr   Úwriter0   r"   r   )r#   rs   ÚpZconsumerr   r   r   Útest_protocolToConsumer¾  s    
z$AdapterTests.test_protocolToConsumerc                    sV   g ‰ t tƒG ‡ fdd„dtƒƒ}|ƒ }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 rp   )rq   )r#   rL   rr   r   r   r‘   Ô  s    z<AdapterTests.test_consumerToProtocol.<locals>.Consumer.writeN)r   r   r   r‘   r   rr   r   r   ÚConsumerÒ  s   r”   r   N)r   r   r@   r   r   r0   r"   r   )r#   r”   Úcr(   r   rr   r   Útest_consumerToProtocolÌ  s    
z$AdapterTests.test_consumerToProtocolN)r   r   r   ri   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}
        ó   test1ZstuffN)r   r   r‘   r0   ÚfileÚgetvalueZassertNotEqual©r#   Úwrapperr   r   r   Ú
test_writeä  s    



zFileWrapperTests.test_writec                 C   sx   t tƒ ƒ}| ddg¡ |  |j ¡ d¡ t tƒ ƒ}trN|  t|jddg¡ n&| ddg¡ |  	t
|j ¡ ƒdk¡ dS )zH
        L{twisted.internet.protocol.FileWrapper.writeSequence}
        r˜   s   test2s
   test1test2Ztest3Ztest4r   N)r   r   ZwriteSequencer0   r™   rš   r   ZassertRaisesÚ	TypeErrorr=   rY   r›   r   r   r   Útest_writeSequenceô  s    

þz#FileWrapperTests.test_writeSequenceN)r   r   r   ri   r   rŸ   r   r   r   r   r—   ß  s   r—   N))ri   Z
__future__r   r   Ú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.loggerr   r   Ztwisted.python.compatr   Ztwisted.python.failurer   Ztwisted.test.proto_helpersr   r   Ztwisted.trial.unittestr   r   rj   rv   rŽ   r—   r   r   r   r   Ú<module>   s&      '`%