U
    
W[%                     @   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mZ ddlmZmZ ddlmZ dd	lmZ dd
lmZ zddlmZ ddlmZ W n ek
r   dZY nX ddlmZ G dd dejZG dd deZeejG dd deZG dd de	eeZ e! "e #  dS )z(
Tests for L{twisted.internet._newtls}.
    )divisionabsolute_import)unittest)
interfaces)ReactorBuilder)ConnectableProtocolrunProtocolsWithReactor)
SSLCreatorTLSMixin)StartTLSClientCreator)ContextGeneratingMixin)
TCPCreator)tls)_newtlsN)implementerc                   @   s    e Zd ZdZesdZdd ZdS )BypassTLSTestsz4
    Tests for the L{_newtls._BypassTLS} class.
    z<Couldn't import _newtls, perhaps pyOpenSSL is old or missingc                    sh   t   g G  fdddt }t|| }|  |  g t  }|| |  |g dS )z
        C{_BypassTLS.loseConnection} calls C{loseConnection} on the base
        class, while preserving any default argument in the base class'
        C{loseConnection} implementation.
        c                       s   e Zd Z ffdd	ZdS )zDBypassTLSTests.test_loseConnectionPassThrough.<locals>.FakeTransportc                    s     | d S N)append)selfZ	_connDone)result C/usr/lib/python3/dist-packages/twisted/internet/test/test_newtls.pyloseConnection-   s    zSBypassTLSTests.test_loseConnectionPassThrough.<locals>.FakeTransport.loseConnectionN)__name__
__module____qualname__r   r   defaultr   r   r   FakeTransport,   s   r   N)objectr   Z
_BypassTLSr   assertEqual)r   r   ZbypassZ
notDefaultr   r   r   test_loseConnectionPassThrough#   s    
z-BypassTLSTests.test_loseConnectionPassThroughN)r   r   r   __doc__r   skipr!   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 )	FakeProducerz'
    A producer that does nothing.
    c                 C   s   d S r   r   r   r   r   r   pauseProducingB   s    zFakeProducer.pauseProducingc                 C   s   d S r   r   r%   r   r   r   resumeProducingF   s    zFakeProducer.resumeProducingc                 C   s   d S r   r   r%   r   r   r   stopProducingJ   s    zFakeProducer.stopProducingN)r   r   r   r"   r&   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 )ProducerProtocolzy
    Register a producer, unregister it, and verify the producer hooks up to
    innards of C{TLSMemoryBIOProtocol}.
    c                 C   s   || _ || _d S r   )producerr   )r   r*   r   r   r   r   __init__V   s    zProducerProtocol.__init__c                 C   sf   t | jjtjstd| j| jd | j	| jjj
j
 | j  | j	| jjj
 | j  d S )Nz#TLSMemoryBIOProtocol not hooked up.T)
isinstance	transportprotocolr   ZTLSMemoryBIOProtocolRuntimeErrorregisterProducerr*   r   r   	_producerunregisterProducerr   r%   r   r   r   handshakeCompleted[   s    
z#ProducerProtocol.handshakeCompletedN)r   r   r   r"   r+   r3   r   r   r   r   r)   O   s   r)   c                   @   s@   e Zd ZdZesdZdd Zdd Zdd Zd	d
 Z	dd Z
dS )ProducerTestsMixinzM
    Test the new TLS code integrates C{TLSMemoryBIOProtocol} correctly.
    z)Could not import twisted.internet._newtlsc                 C   s6   g }t  }t| t t||t  | ||dg dS )z
        C{registerProducer} and C{unregisterProducer} on TLS transports
        created as SSL from the get go are passed to the
        C{TLSMemoryBIOProtocol}, not the underlying transport directly.
        N)r$   r   r   r)   r	   r    r   r   r*   r   r   r   test_producerSSLFromStartt   s    z,ProducerTestsMixin.test_producerSSLFromStartc                 C   s6   g }t  }t| t t||t  | ||dg dS )z
        C{registerProducer} and C{unregisterProducer} on TLS transports
        created by C{startTLS} are passed to the C{TLSMemoryBIOProtocol}, not
        the underlying transport directly.
        N)r$   r   r   r)   r   r    r5   r   r   r   test_producerAfterStartTLS   s    z-ProducerTestsMixin.test_producerAfterStartTLSc                    sp   |    |  g t G fdddt}G  fdddt}t| | | t  | g dS )z
        When a producer is registered, and then startTLS is called,
        the producer is re-registered with the C{TLSMemoryBIOProtocol}.
        c                       s   e Zd Z fddZdS )zMProducerTestsMixin.startTLSAfterRegisterProducer.<locals>.RegisterTLSProtocolc                    s   | j   | j  rB| j jjj | j jj n&| j jjjj | j jjj | j   | j   d S r   )	r-   r0   startTLSr   r.   r1   r*   r2   r   r%   r*   r   serverContext	streamingr   r   connectionMade   s    
z\ProducerTestsMixin.startTLSAfterRegisterProducer.<locals>.RegisterTLSProtocol.connectionMadeNr   r   r   r<   r   r9   r   r   RegisterTLSProtocol   s   r>   c                       s   e Zd Z fddZdS )zJProducerTestsMixin.startTLSAfterRegisterProducer.<locals>.StartTLSProtocolc                    s   | j   d S r   )r-   r8   r%   clientContextr   r   r<      s    zYProducerTestsMixin.startTLSAfterRegisterProducer.<locals>.StartTLSProtocol.connectionMadeNr=   r   r?   r   r   StartTLSProtocol   s   rA   N)ZgetClientContextZgetServerContextr$   r   r   r   r    )r   r;   r>   rA   r   )r@   r*   r   r:   r;   r   startTLSAfterRegisterProducer   s     z0ProducerTestsMixin.startTLSAfterRegisterProducerc                 C   s   |  d dS )z
        When a streaming producer is registered, and then startTLS is called,
        the producer is re-registered with the C{TLSMemoryBIOProtocol}.
        TNrB   r%   r   r   r   +test_startTLSAfterRegisterProducerStreaming   s    z>ProducerTestsMixin.test_startTLSAfterRegisterProducerStreamingc                 C   s   |  d dS )z
        When a non-streaming producer is registered, and then startTLS is
        called, the producer is re-registered with the
        C{TLSMemoryBIOProtocol}.
        FNrC   r%   r   r   r   .test_startTLSAfterRegisterProducerNonStreaming   s    zAProducerTestsMixin.test_startTLSAfterRegisterProducerNonStreamingN)r   r   r   r"   r   r#   r6   r7   rB   rD   rE   r   r   r   r   r4   l   s   %r4   )$r"   Z
__future__r   r   Ztwisted.trialr   Ztwisted.internetr   Z#twisted.internet.test.reactormixinsr   Z&twisted.internet.test.connectionmixinsr   r   Ztwisted.internet.test.test_tlsr	   r
   r   r   Ztwisted.internet.test.test_tcpr   Ztwisted.protocolsr   r   ImportErrorZzope.interfacer   ZTestCaser   r   r$   ZIHandshakeListenerr)   r4   globalsupdateZmakeTestCaseClassesr   r   r   r   <module>   s*   
"\