o
    ¯bo?  ã                   @   sÚ   d Z ddlmZ ddlmZ ddlmZmZmZ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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ƒZG dd„ deƒZG dd„ deƒZdS )z(
Tests for L{twisted.internet.testing}.
é    )ÚverifyObject)ÚIPv4Address)	ÚIAddressÚ
IConnectorÚ	IConsumerÚIListeningPortÚIPushProducerÚIReactorSSLÚIReactorTCPÚIReactorUNIXÚ
ITransport)ÚClientFactoryÚFactory)ÚMemoryReactorÚNonStreamingProducerÚRaisingMemoryReactorÚStringTransport)ÚnamedAny)Ú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"d#„ Zd$d%„ Zd&d'„ Zd(S ))ÚStringTransportTestsz@
    Tests for L{twisted.internet.testing.StringTransport}.
    c                 C   s   t ƒ | _d S ©N)r   Ú	transport©Úself© r   úD/usr/lib/python3/dist-packages/twisted/internet/test/test_testing.pyÚsetUp&   s   zStringTransportTests.setUpc                 C   s:   |   tt| jƒ¡ |   tt| jƒ¡ |   tt| jƒ¡ dS )zq
        L{StringTransport} instances provide L{ITransport}, L{IPushProducer},
        and L{IConsumer}.
        N)Ú
assertTruer   r   r   r   r   r   r   r   r   Útest_interfaces)   s   z$StringTransportTests.test_interfacesc                 C   s>   t ƒ }t ƒ }| j ||¡ |  | jj|¡ |  | jj|¡ dS )zz
        L{StringTransport.registerProducer} records the arguments supplied to
        it as instance attributes.
        N)Úobjectr   ÚregisterProducerÚassertIsÚproducerÚ	streaming©r   r"   r#   r   r   r   Útest_registerProducer2   s
   z*StringTransportTests.test_registerProducerc                 C   sL   t ƒ }| j |d¡ |  t| jjt ƒ d¡ |  | jj|¡ |  | jj¡ dS )zy
        L{StringTransport.registerProducer} raises L{RuntimeError} if a
        producer is already registered.
        TFN)	r   r   r    ÚassertRaisesÚRuntimeErrorr!   r"   r   r#   )r   r"   r   r   r   Útest_disallowedRegisterProducer=   s   ÿz4StringTransportTests.test_disallowedRegisterProducerc                 C   sb   t ƒ }t ƒ }| j |d¡ | j ¡  |  | jj¡ | j |d¡ |  | jj|¡ |  | jj¡ dS )z±
        L{StringTransport.unregisterProducer} causes the transport to forget
        about the registered producer and makes it possible to register a new
        one.
        FTN)	r   r   r    ÚunregisterProducerÚassertIsNoner"   r!   r   r#   )r   ÚoldProducerÚnewProducerr   r   r   Útest_unregisterProducerJ   s   
z,StringTransportTests.test_unregisterProducerc                 C   s   |   t| jj¡ dS )z€
        L{StringTransport.unregisterProducer} raises L{RuntimeError} if called
        when no producer is registered.
        N)r&   r'   r   r)   r   r   r   r   Útest_invalidUnregisterProducerY   s   z3StringTransportTests.test_invalidUnregisterProducerc                 C   s   |   | jjd¡ dS )zO
        L{StringTransport.producerState} is initially C{'producing'}.
        Ú	producingN)ÚassertEqualr   ÚproducerStater   r   r   r   Útest_initialProducerState`   s   z.StringTransportTests.test_initialProducerStatec                 C   ó   | j  ¡  |  | j jd¡ dS )zy
        L{StringTransport.pauseProducing} changes the C{producerState} of the
        transport to C{'paused'}.
        ÚpausedN)r   ÚpauseProducingr0   r1   r   r   r   r   Útest_pauseProducingf   ó   
z(StringTransportTests.test_pauseProducingc                 C   s(   | j  ¡  | j  ¡  |  | j jd¡ dS )z}
        L{StringTransport.resumeProducing} changes the C{producerState} of the
        transport to C{'producing'}.
        r/   N)r   r5   ÚresumeProducingr0   r1   r   r   r   r   Útest_resumeProducingn   s   

z)StringTransportTests.test_resumeProducingc                 C   r3   )z{
        L{StringTransport.stopProducing} changes the C{'producerState'} of the
        transport to C{'stopped'}.
        ÚstoppedN)r   ÚstopProducingr0   r1   r   r   r   r   Útest_stopProducingw   r7   z'StringTransportTests.test_stopProducingc                 C   ó   | j  ¡  |  t| j j¡ dS )zu
        L{StringTransport.pauseProducing} raises L{RuntimeError} if the
        transport has been stopped.
        N)r   r;   r&   r'   r5   r   r   r   r   Ú test_stoppedTransportCannotPause   r7   z5StringTransportTests.test_stoppedTransportCannotPausec                 C   r=   )zv
        L{StringTransport.resumeProducing} raises L{RuntimeError} if the
        transport has been stopped.
        N)r   r;   r&   r'   r8   r   r   r   r   Ú!test_stoppedTransportCannotResume‡   r7   z6StringTransportTests.test_stoppedTransportCannotResumec                 C   r=   )zz
        L{StringTransport.pauseProducing} raises L{RuntimeError} if the
        transport is being disconnected.
        N)r   ÚloseConnectionr&   r'   r5   r   r   r   r   Ú&test_disconnectingTransportCannotPause�   r7   z;StringTransportTests.test_disconnectingTransportCannotPausec                 C   r=   )z{
        L{StringTransport.resumeProducing} raises L{RuntimeError} if the
        transport is being disconnected.
        N)r   r@   r&   r'   r8   r   r   r   r   Ú'test_disconnectingTransportCannotResume—   r7   z<StringTransportTests.test_disconnectingTransportCannotResumec                 C   s*   |   | jj¡ | j ¡  |  | jj¡ dS )zv
        L{StringTransport.loseConnection} toggles the C{disconnecting} instance
        variable to C{True}.
        N)ÚassertFalser   Údisconnectingr@   r   r   r   r   r   Ú$test_loseConnectionSetsDisconnectingŸ   s   
z9StringTransportTests.test_loseConnectionSetsDisconnectingc                 C   s   t ƒ }|  t|ƒ ¡ |¡ dS )z�
        If a host address is passed to L{StringTransport.__init__}, that
        value is returned from L{StringTransport.getHost}.
        N)r   r!   r   ÚgetHost©r   Úaddressr   r   r   Útest_specifiedHostAddress¨   s   z.StringTransportTests.test_specifiedHostAddressc                 C   s    t ƒ }|  t|d� ¡ |¡ dS )z�
        If a peer address is passed to L{StringTransport.__init__}, that
        value is returned from L{StringTransport.getPeer}.
        )ÚpeerAddressN)r   r!   r   ÚgetPeerrG   r   r   r   Útest_specifiedPeerAddress°   s   z.StringTransportTests.test_specifiedPeerAddressc                 C   ó   t ƒ  ¡ }|  |t¡ dS )z•
        If no host address is passed to L{StringTransport.__init__}, an
        L{IPv4Address} is returned from L{StringTransport.getHost}.
        N)r   rF   ÚassertIsInstancer   rG   r   r   r   Útest_defaultHostAddress¸   ó   
z,StringTransportTests.test_defaultHostAddressc                 C   rM   )z•
        If no peer address is passed to L{StringTransport.__init__}, an
        L{IPv4Address} is returned from L{StringTransport.getPeer}.
        N)r   rK   rN   r   rG   r   r   r   Útest_defaultPeerAddressÀ   rP   z,StringTransportTests.test_defaultPeerAddressN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r%   r(   r-   r.   r2   r6   r9   r<   r>   r?   rA   rB   rE   rI   rL   rO   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d	„ Zd
d„ Zdd„ Z	dS )ÚReactorTestszA
    Tests for L{MemoryReactor} and L{RaisingMemoryReactor}.
    c                 C   ó(   t ƒ }tt|ƒ tt|ƒ tt|ƒ dS )zt
        L{MemoryReactor} provides all of the attributes described by the
        interfaces it advertises.
        N)r   r   r
   r	   r   )r   ÚmemoryReactorr   r   r   Útest_memoryReactorProvidesÎ   ó   

z'ReactorTests.test_memoryReactorProvidesc                 C   rW   )z{
        L{RaisingMemoryReactor} provides all of the attributes described by the
        interfaces it advertises.
        N)r   r   r
   r	   r   )r   ÚraisingReactorr   r   r   Útest_raisingReactorProvidesØ   rZ   z(ReactorTests.test_raisingReactorProvidesc                 C   s¢   t ƒ }| ddtƒ ¡| ddtƒ d¡fD ]}tt|ƒ | ¡ }tt|ƒ |  |j	d¡ |  |j
d¡ q| dtƒ ¡}tt|ƒ | ¡ }tt|ƒ |  |jd¡ dS )a  
        L{MemoryReactor.connectTCP}, L{MemoryReactor.connectSSL}, and
        L{MemoryReactor.connectUNIX} will return an L{IConnector} whose
        C{getDestination} method returns an L{IAddress} with attributes which
        reflect the values passed.
        ztest.example.comi�   Ns
   /fake/path)r   Ú
connectTCPr   Ú
connectSSLr   r   ÚgetDestinationr   r0   ÚhostÚportÚconnectUNIXÚname)r   rX   Ú	connectorrH   r   r   r   Útest_connectDestinationâ   s   þ



z$ReactorTests.test_connectDestinationc                 C   sž   t ƒ }| dtƒ ¡| dtƒ d¡fD ]}tt|ƒ | ¡ }tt|ƒ |  |j	d¡ |  |j
d¡ q| dtƒ ¡}tt|ƒ | ¡ }tt|ƒ |  |jd¡ dS )a–  
        L{MemoryReactor.listenTCP}, L{MemoryReactor.listenSSL} and
        L{MemoryReactor.listenUNIX} will return an L{IListeningPort} whose
        C{getHost} method returns an L{IAddress}; C{listenTCP} and C{listenSSL}
        will have a default host of C{'0.0.0.0'}, and a port that reflects the
        value passed, and C{listenUNIX} will have a name that reflects the path
        passed.
        i2   Nz0.0.0.0s   /path/to/socket)r   Ú	listenTCPr   Ú	listenSSLr   r   rF   r   r0   r`   ra   Ú
listenUNIXrc   )r   rX   ra   rH   r   r   r   Útest_listenDefaultHostù   s   	þ



z#ReactorTests.test_listenDefaultHostc                 C   óP   t ƒ }tƒ }| |¡ | |¡ |  | ¡ |g¡ | |¡ |  | ¡ g ¡ dS )z>
        Adding, removing, and listing readers works.
        N)r   r   Ú	addReaderr0   Ú
getReadersÚremoveReader)r   ÚreaderÚreactorr   r   r   Útest_readers  ó   


zReactorTests.test_readersc                 C   rj   )z>
        Adding, removing, and listing writers works.
        N)r   r   Ú	addWriterr0   Ú
getWritersÚremoveWriter)r   Úwriterro   r   r   r   Útest_writers"  rq   zReactorTests.test_writersN)
rR   rS   rT   rU   rY   r\   re   ri   rp   rv   r   r   r   r   rV   É   s    

rV   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚTestConsumerzP
    A very basic test consumer for use with the NonStreamingProducerTests.
    c                 C   s   g | _ d | _d | _d S r   )Úwritesr"   ÚproducerStreamingr   r   r   r   Ú__init__8  s   
zTestConsumer.__init__c                 C   s   || _ || _dS )z×
        Registers a single producer with this consumer. Just keeps track of it.

        @param producer: The producer to register.
        @param streaming: Whether the producer is a streaming one or not.
        N©r"   ry   r$   r   r   r   r    =  s   
zTestConsumer.registerProducerc                 C   s   d| _ d| _dS )zC
        Forget the producer we had previously registered.
        Nr{   r   r   r   r   r)   G  s   
zTestConsumer.unregisterProducerc                 C   s   | j  |¡ dS )zz
        Some data was written to the consumer: stores it for later use.

        @param data: The data to write.
        N)rx   Úappend)r   Údatar   r   r   ÚwriteN  s   zTestConsumer.writeN)rR   rS   rT   rU   rz   r    r)   r~   r   r   r   r   rw   3  s    
rw   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚNonStreamingProducerTestszF
    Tests for the L{NonStreamingProducer} to validate behaviour.
    c                 C   s¤   t ƒ }t|ƒ}| |d¡ |  |j|¡ |  |j|¡ |  |j¡ tdƒD ]}| 	¡  q%g d¢}|  
|j¡ |  
|j¡ |  
|j¡ |  |j|¡ |  t|j	¡ dS )z�
        When the L{NonStreamingProducer} has resumeProducing called 10 times,
        it writes the counter each time and then fails.
        Fé
   )
ó   0ó   1ó   2ó   3ó   4ó   5ó   6ó   7ó   8ó   9N)rw   r   r    r!   r"   ÚconsumerrC   ry   Úranger8   r*   r0   rx   r&   r'   )r   r‹   r"   Ú_ÚexpectedWritesr   r   r   Útest_producesOnly10Times\  s   
z2NonStreamingProducerTests.test_producesOnly10Timesc                 C   s4   t ƒ }t|ƒ}| |d¡ | ¡  |  t|j¡ dS )zb
        When the L{NonStreamingProducer} is paused, it raises a
        L{RuntimeError}.
        FN)rw   r   r    r8   r&   r'   r5   )r   r‹   r"   r   r   r   Útest_cannotPauseProductionv  s
   z4NonStreamingProducerTests.test_cannotPauseProductionN)rR   rS   rT   rU   r�   r�   r   r   r   r   r   W  s    r   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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 )!ÚDeprecationTestsz8
    Deprecations in L{twisted.test.proto_helpers}.
    c                 C   sd   d|j › �}|  |g¡}|  t|d d ¡ |  dt|ƒ¡ |  ||d d ¡ |  |t|ƒ¡ d S )Nztwisted.internet.testing.r   Úcategoryé   Úmessage)rR   ÚflushWarningsr0   ÚDeprecationWarningÚlenÚassertInr!   r   )r   ÚtestÚobjÚnew_pathÚwarningsr   r   r   ÚhelperŠ  s   zDeprecationTests.helperc                 C   ó   ddl m} |  | j|¡ d S )Nr   )ÚAccumulatingProtocol)Útwisted.test.proto_helpersrŸ   r�   Útest_accumulatingProtocol)r   rŸ   r   r   r   r¡   ’  ó   z*DeprecationTests.test_accumulatingProtocolc                 C   rž   )Nr   )ÚLineSendingProtocol)r    r£   r�   Útest_lineSendingProtocol)r   r£   r   r   r   r¤   —  r¢   z)DeprecationTests.test_lineSendingProtocolc                 C   rž   )Nr   )ÚFakeDatagramTransport)r    r¥   r�   Útest_fakeDatagramTransport)r   r¥   r   r   r   r¦   œ  r¢   z+DeprecationTests.test_fakeDatagramTransportc                 C   rž   )Nr   )r   )r    r   r�   Útest_stringTransport)r   r   r   r   r   r§   ¡  r¢   z%DeprecationTests.test_stringTransportc                 C   rž   )Nr   )Ú StringTransportWithDisconnection)r    r¨   r�   Ú%test_stringTransportWithDisconnection)r   r¨   r   r   r   r©   ¦  s   ÿz6DeprecationTests.test_stringTransportWithDisconnectionc                 C   rž   )Nr   )ÚStringIOWithoutClosing)r    rª   r�   Útest_stringIOWithoutClosing)r   rª   r   r   r   r«   ­  r¢   z,DeprecationTests.test_stringIOWithoutClosingc                 C   rž   )Nr   )Ú_FakeConnector)r    r¬   r�   Útest__fakeConnector)r   r¬   r   r   r   r­   ²  r¢   z$DeprecationTests.test__fakeConnectorc                 C   rž   )Nr   )Ú	_FakePort)r    r®   r�   Útest__fakePort)r   r®   r   r   r   r¯   ·  r¢   zDeprecationTests.test__fakePortc                 C   rž   )Nr   )r   )r    r   r�   Útest_memoryReactor)r   r   r   r   r   r°   ¼  r¢   z#DeprecationTests.test_memoryReactorc                 C   rž   )Nr   )ÚMemoryReactorClock)r    r±   r�   Útest_memoryReactorClock)r   r±   r   r   r   r²   Á  r¢   z(DeprecationTests.test_memoryReactorClockc                 C   rž   )Nr   )r   )r    r   r�   Útest_raisingMemoryReactor)r   r   r   r   r   r³   Æ  r¢   z*DeprecationTests.test_raisingMemoryReactorc                 C   rž   )Nr   )r   )r    r   r�   Útest_nonStreamingProducer)r   r   r   r   r   r´   Ë  r¢   z*DeprecationTests.test_nonStreamingProducerc                 C   rž   )Nr   )ÚwaitUntilAllDisconnected)r    rµ   r�   Útest_waitUntilAllDisconnected)r   rµ   r   r   r   r¶   Ð  r¢   z.DeprecationTests.test_waitUntilAllDisconnectedc                 C   rž   )Nr   )ÚEventLoggingObserver)r    r·   r�   Útest_eventLoggingObserver)r   r·   r   r   r   r¸   Õ  r¢   z*DeprecationTests.test_eventLoggingObserverN)rR   rS   rT   rU   r�   r¡   r¤   r¦   r§   r©   r«   r­   r¯   r°   r²   r³   r´   r¶   r¸   r   r   r   r   r‘   …  s"    r‘   N) rU   Úzope.interface.verifyr   Útwisted.internet.addressr   Útwisted.internet.interfacesr   r   r   r   r   r	   r
   r   r   Útwisted.internet.protocolr   r   Útwisted.internet.testingr   r   r   r   Útwisted.python.reflectr   Útwisted.trial.unittestr   r   rV   rw   r   r‘   r   r   r   r   Ú<module>   s   , )j$.