U
    Â“«j…  ã                   @   s  d Z ddl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mZ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 eZddlmZ G dd„ dejƒZG dd„ dƒ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}.
é    )Ú
interfaces)ÚConnectableProtocolÚrunProtocolsWithReactor)ÚReactorBuilder)Ú
TCPCreator)ÚContextGeneratingMixinÚ
SSLCreatorÚStartTLSClientCreatorÚTLSMixin)Úunittest)Ú_newtls)ÚtlsN)Ú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                    sf   t ƒ ‰ g ‰G ‡ ‡fdd„dƒ}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© úE/tmp/pip-unpacked-wheel-59kc99xd/twisted/internet/test/test_newtls.pyÚloseConnection4   s    zSBypassTLSTests.test_loseConnectionPassThrough.<locals>.FakeTransport.loseConnectionN)Ú__name__Ú
__module__Ú__qualname__r   r   ©Údefaultr   r   r   ÚFakeTransport3   s   r   N)Úobjectr   Z
_BypassTLSr   ÚassertEqual)r   r   Ú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   ÚpauseProducingH   s    zFakeProducer.pauseProducingc                 C   s   d S r   r   r$   r   r   r   ÚresumeProducingK   s    zFakeProducer.resumeProducingc                 C   s   d S r   r   r$   r   r   r   ÚstopProducingN   s    zFakeProducer.stopProducingN)r   r   r   r!   r%   r&   r'   r   r   r   r   r#   C   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__Y   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BufferingTLSTransportÚRuntimeErrorÚregisterProducerr)   r   r   Ú	_producerÚunregisterProducerr   r$   r   r   r   ÚhandshakeCompleted]   s    
z#ProducerProtocol.handshakeCompletedN)r   r   r   r!   r*   r2   r   r   r   r   r(   R   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   r4   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,   r/   ÚstartTLSr   r-   r0   r)   r1   r   r$   ©r)   r   ÚserverContextÚ	streamingr   r   ÚconnectionMade¡   s    
z\ProducerTestsMixin.startTLSAfterRegisterProducer.<locals>.RegisterTLSProtocol.connectionMadeN©r   r   r   r;   r   r8   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,   r7   r$   ©ÚclientContextr   r   r;   ²   s    zYProducerTestsMixin.startTLSAfterRegisterProducer.<locals>.StartTLSProtocol.connectionMadeNr<   r   r>   r   r   ÚStartTLSProtocol±   s   r@   N)ZgetClientContextZgetServerContextr#   r   r   r   r   )r   r:   r=   r@   r   )r?   r)   r   r9   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©rA   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}.
        FNrB   r$   r   r   r   Ú.test_startTLSAfterRegisterProducerNonStreamingÁ   s    zAProducerTestsMixin.test_startTLSAfterRegisterProducerNonStreamingN)r   r   r   r!   r   r"   r5   r6   rA   rC   rD   r   r   r   r   r3   l   s   $r3   )!r!   Ztwisted.internetr   Z&twisted.internet.test.connectionmixinsr   r   Z#twisted.internet.test.reactormixinsr   Ztwisted.internet.test.test_tcpr   Ztwisted.internet.test.test_tlsr   r   r	   r
   Ztwisted.trialr   r   Z__newtlsZtwisted.protocolsr   ÚImportErrorZzope.interfacer   ZTestCaser   r#   ZIHandshakeListenerr(   r3   ÚglobalsÚupdateZmakeTestCaseClassesr   r   r   r   Ú<module>   s&   
!^