U
    
W[Ð3  ã                   @   sÀ   d 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 ddlmZ ddlmZmZmZmZ ddlmZ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dS )z*
Tests for L{twisted.test.proto_helpers}.
é    )ÚverifyObject)	Ú
ITransportÚIPushProducerÚ	IConsumerÚIReactorTCPÚIReactorSSLÚIReactorUNIXÚIAddressÚIListeningPortÚ
IConnector)ÚIPv4Address)ÚTestCase)ÚStringTransportÚMemoryReactorÚRaisingMemoryReactorÚNonStreamingProducer)ÚClientFactoryÚFactoryc                   @   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 ))ÚStringTransportTestszB
    Tests for L{twisted.test.proto_helpers.StringTransport}.
    c                 C   s   t ƒ | _d S ©N)r   Ú	transport©Úself© r   úC/usr/lib/python3/dist-packages/twisted/test/test_stringtransport.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_registerProducer&   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_disallowedRegisterProducer2   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   ZoldProducerZnewProducerr   r   r   Útest_unregisterProducer?   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_invalidUnregisterProducerO   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_initialProducerStateW   s    z.StringTransportTests.test_initialProducerStatec                 C   s   | j  ¡  |  | j jd¡ dS )zy
        L{StringTransport.pauseProducing} changes the C{producerState} of the
        transport to C{'paused'}.
        ZpausedN)r   ÚpauseProducingr-   r.   r   r   r   r   Útest_pauseProducing^   s    
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   r0   ÚresumeProducingr-   r.   r   r   r   r   Útest_resumeProducingg   s    

z)StringTransportTests.test_resumeProducingc                 C   s   | j  ¡  |  | j jd¡ dS )z{
        L{StringTransport.stopProducing} changes the C{'producerState'} of the
        transport to C{'stopped'}.
        ZstoppedN)r   ÚstopProducingr-   r.   r   r   r   r   Útest_stopProducingq   s    
z'StringTransportTests.test_stopProducingc                 C   s   | j  ¡  |  t| j j¡ dS )zu
        L{StringTransport.pauseProducing} raises L{RuntimeError} if the
        transport has been stopped.
        N)r   r4   r%   r&   r0   r   r   r   r   Ú test_stoppedTransportCannotPausez   s    
z5StringTransportTests.test_stoppedTransportCannotPausec                 C   s   | j  ¡  |  t| j j¡ dS )zv
        L{StringTransport.resumeProducing} raises L{RuntimeError} if the
        transport has been stopped.
        N)r   r4   r%   r&   r2   r   r   r   r   Ú!test_stoppedTransportCannotResumeƒ   s    
z6StringTransportTests.test_stoppedTransportCannotResumec                 C   s   | j  ¡  |  t| j j¡ dS )zz
        L{StringTransport.pauseProducing} raises L{RuntimeError} if the
        transport is being disconnected.
        N)r   ÚloseConnectionr%   r&   r0   r   r   r   r   Ú&test_disconnectingTransportCannotPauseŒ   s    
z;StringTransportTests.test_disconnectingTransportCannotPausec                 C   s   | j  ¡  |  t| j j¡ dS )z{
        L{StringTransport.resumeProducing} raises L{RuntimeError} if the
        transport is being disconnected.
        N)r   r8   r%   r&   r2   r   r   r   r   Ú'test_disconnectingTransportCannotResume•   s    
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   Zdisconnectingr8   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}.
        )ZpeerAddressN)r   r    r   ÚgetPeerr>   r   r   r   Útest_specifiedPeerAddress±   s
     ÿz.StringTransportTests.test_specifiedPeerAddressc                 C   s   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   r=   ÚassertIsInstancer   r>   r   r   r   Útest_defaultHostAddress»   s    
z,StringTransportTests.test_defaultHostAddressc                 C   s   t ƒ  ¡ }|  |t¡ dS )z•
        If no peer address is passed to L{StringTransport.__init__}, an
        L{IPv4Address} is returned from L{StringTransport.getPeer}.
        N)r   rA   rC   r   r>   r   r   r   Útest_defaultPeerAddressÄ   s    
z,StringTransportTests.test_defaultPeerAddressN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r$   r'   r*   r+   r/   r1   r3   r5   r6   r7   r9   r:   r<   r@   rB   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d	„ Zd
d„ Zdd„ Z	dS )ÚReactorTestszA
    Tests for L{MemoryReactor} and L{RaisingMemoryReactor}.
    c                 C   s(   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Ó   s    

z'ReactorTests.test_memoryReactorProvidesc                 C   s(   t ƒ }tt|ƒ tt|ƒ tt|ƒ dS )z{
        L{RaisingMemoryReactor} provides all of the attributes described by the
        interfaces it advertises.
        N)r   r   r   r   r   )r   ZraisingReactorr   r   r   Útest_raisingReactorProvidesÞ   s    

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   Z
connectTCPr   Z
connectSSLr   r   ZgetDestinationr	   r-   ÚhostÚportZconnectUNIXÚname)r   rK   Z	connectorr?   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   Z	listenTCPr   Z	listenSSLr   r
   r=   r	   r-   rN   rO   Z
listenUNIXrP   )r   rK   rO   r?   r   r   r   Útest_listenDefaultHost  s    	ÿ



z#ReactorTests.test_listenDefaultHostc                 C   sP   t ƒ }tƒ }| |¡ | |¡ |  | ¡ |g¡ | |¡ |  | ¡ g ¡ dS )z>
        Adding, removing, and listing readers works.
        N)r   r   Z	addReaderr-   Z
getReadersZremoveReader)r   ÚreaderÚreactorr   r   r   Útest_readers  s    


zReactorTests.test_readersc                 C   sP   t ƒ }tƒ }| |¡ | |¡ |  | ¡ |g¡ | |¡ |  | ¡ g ¡ dS )z>
        Adding, removing, and listing writers works.
        N)r   r   Z	addWriterr-   Z
getWritersZremoveWriter)r   ÚwriterrT   r   r   r   Útest_writers+  s    


zReactorTests.test_writersN)
rF   rG   rH   rI   rL   rM   rQ   rR   rU   rW   r   r   r   r   rJ   Î   s   rJ   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__A  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!   rZ   r#   r   r   r   r   G  s    zTestConsumer.registerProducerc                 C   s   d| _ d| _dS )zC
        Forget the producer we had previously registered.
        Nr\   r   r   r   r   r(   R  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)rY   Úappend)r   Údatar   r   r   ÚwriteZ  s    zTestConsumer.writeN)rF   rG   rH   rI   r[   r   r(   r_   r   r   r   r   rX   =  s
   rX   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 ]}| 	¡  qJddddddd	d
ddg
}|  
|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)rX   r   r   r    r!   Úconsumerr;   rZ   Úranger2   r)   r-   rY   r%   r&   )r   rl   r!   Ú_ZexpectedWritesr   r   r   Útest_producesOnly10Timesh  s0    
         ÿ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)rX   r   r   r2   r%   r&   r0   )r   rl   r!   r   r   r   Útest_cannotPauseProduction…  s
    z4NonStreamingProducerTests.test_cannotPauseProductionN)rF   rG   rH   rI   ro   rp   r   r   r   r   r`   d  s   r`   N)rI   Zzope.interface.verifyr   Ztwisted.internet.interfacesr   r   r   r   r   r   r	   r
   r   Ztwisted.internet.addressr   Ztwisted.trial.unittestr   Ztwisted.test.proto_helpersr   r   r   r   Ztwisted.internet.protocolr   r   r   rJ   r   rX   r`   r   r   r   r   Ú<module>   s   , ;o'