o
    ¯b„  ã                   @   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yG   d
ZY nw 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)ÚselfÚ	_connDone)Úresult© úC/usr/lib/python3/dist-packages/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   Ú
_BypassTLSr   ÚassertEqual)r   r   ÚbypassÚ
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   ó   d S r   r   ©r   r   r   r   ÚpauseProducingH   ó   zFakeProducer.pauseProducingc                 C   r'   r   r   r(   r   r   r   ÚresumeProducingK   r*   zFakeProducer.resumeProducingc                 C   r'   r   r   r(   r   r   r   ÚstopProducingN   r*   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   ÚTLSMemoryBIOProtocolÚRuntimeErrorÚregisterProducerr.   r   r   Ú	_producerÚunregisterProducerr   r(   r   r   r   ÚhandshakeCompleted]   s   
z#ProducerProtocol.handshakeCompletedN)r   r   r   r$   r/   r8   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   ó6   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   ó   üz,ProducerTestsMixin.test_producerSSLFromStartc                 C   r:   )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    r;   r   r   r   Útest_producerAfterStartTLS…   r=   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  ˆ¡ ˆr!ˆ | j jjj¡ ˆ | j jj¡ nˆ | j jjjj¡ ˆ | j jjj¡ | j  ¡  | j  ¡  d S r   )	r1   r5   ÚstartTLSr   r2   r6   r.   r7   r   r(   ©r.   r   ÚserverContextÚ	streamingr   r   ÚconnectionMade¡   s   
z\ProducerTestsMixin.startTLSAfterRegisterProducer.<locals>.RegisterTLSProtocol.connectionMadeN©r   r   r   rC   r   r@   r   r   ÚRegisterTLSProtocol    s    rE   c                       s   e Zd Z‡ fdd„ZdS )zJProducerTestsMixin.startTLSAfterRegisterProducer.<locals>.StartTLSProtocolc                    s   | j  ˆ ¡ d S r   )r1   r?   r(   ©ÚclientContextr   r   rC   ²   s   zYProducerTestsMixin.startTLSAfterRegisterProducer.<locals>.StartTLSProtocol.connectionMadeNrD   r   rF   r   r   ÚStartTLSProtocol±   s    rH   N)ÚgetClientContextÚgetServerContextr&   r   r   r   r    )r   rB   rE   rH   r   )rG   r.   r   rA   rB   r   ÚstartTLSAfterRegisterProducer–   s   ÿz0ProducerTestsMixin.startTLSAfterRegisterProducerc                 C   ó   |   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©rK   r(   r   r   r   Ú+test_startTLSAfterRegisterProducerStreamingº   s   z>ProducerTestsMixin.test_startTLSAfterRegisterProducerStreamingc                 C   rL   )z«
        When a non-streaming producer is registered, and then startTLS is
        called, the producer is re-registered with the
        C{TLSMemoryBIOProtocol}.
        FNrM   r(   r   r   r   Ú.test_startTLSAfterRegisterProducerNonStreamingÁ   s   zAProducerTestsMixin.test_startTLSAfterRegisterProducerNonStreamingN)r   r   r   r$   r   r%   r<   r>   rK   rN   rO   r   r   r   r   r9   l   s    $r9   )!r$   Útwisted.internetr   Ú&twisted.internet.test.connectionmixinsr   r   Ú#twisted.internet.test.reactormixinsr   Útwisted.internet.test.test_tcpr   Útwisted.internet.test.test_tlsr   r   r	   r
   Útwisted.trialr   r   Ú__newtlsÚtwisted.protocolsr   ÚImportErrorÚzope.interfacer   ÚTestCaser   r&   ÚIHandshakeListenerr-   r9   ÚglobalsÚupdateÚmakeTestCaseClassesr   r   r   r   Ú<module>   s*   ÿ!^