U
    
W[4                  
   @   sL  d Z ddlmZmZ eZddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZmZmZ ddlmZmZmZ dd	lmZmZ dd
lmZmZmZ ddlmZ ddlm Z  ddl!m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z(m)Z)m*Z* ddl+m,Z,m-Z-m.Z. zddl/m0Z0 W n e1k
r"   dZ0Y nX ddl2m3Z3m4Z4 ddl2m5Z5 G dd dZ6G dd de7Z8eeG dd de7Z9G dd de,e8Z:G dd de7Z;G d d! d!e6ee-Z<G d"d# d#e,e8Z=G d$d% d%e6ee8e-e;Z>G d&d' d'e6e8e&e;e(e)e	Z?e@ Ae>B  e@ Ae<B  e@ Ae? B  G d(d) d)ee*e8ZCe@ AeCB  dS )*z0
Tests for implementations of L{ITLSTransport}.
    )divisionabsolute_import)implementer)networkString)FilePath)ReactorBuilder)ServerFactoryClientFactoryProtocol)IReactorSSLITLSTransportIStreamClientEndpoint)DeferredDeferredList)SSL4ServerEndpointSSL4ClientEndpointTCP4ClientEndpoint)ConnectionClosed)
Cooperator)SkipTest)platform)ObjectModelIntegrationMixin)ConnectToTCPListenerMixinStreamTransportTestsMixinAbortConnectionMixin)EndpointCreatorConnectionTestsMixinBrokenContextFactory)FILETYPE_PEMN)PrivateCertificateKeyPairClientContextFactoryc                   @   s(   e Zd ZegZe r$dZeedZdS )TLSMixinzcFor some reason, these reactors don't deal with SSL disconnection correctly on Windows.  See #3371.)z*twisted.internet.glib2reactor.Glib2Reactorz(twisted.internet.gtk2reactor.Gtk2ReactorN)	__name__
__module____qualname__r   requiredInterfacesr   Z	isWindowsmsgZskippedReactors r)   r)   @/usr/lib/python3/dist-packages/twisted/internet/test/test_tls.pyr#   ,   s   r#   c                   @   s@   e Zd ZddlZeeejddZ	[dd Z
dd ZdS )	ContextGeneratingMixinr   Ns   tests
   server.pemc                 C   s(   | j  }t|t|tt}| S )zM
        Return a new SSL context suitable for use in a test server.
        )_pemZ
getContentr   loadr    r   Zoptions)selfZpemZcertr)   r)   r*   getServerContext>   s    
 
 z'ContextGeneratingMixin.getServerContextc                 C   s   t  S Nr!   r.   r)   r)   r*   getClientContextH   s    z'ContextGeneratingMixin.getClientContext)r$   r%   r&   Ztwistedr   r   __file__ZsiblingZchildr,   r/   r2   r)   r)   r)   r*   r+   8   s   
r+   c                   @   s    e Zd ZdZdd Zdd ZdS )StartTLSClientEndpointa!  
    An endpoint which wraps another one and adds a TLS layer immediately when
    connections are set up.

    @ivar wrapped: A L{IStreamClientEndpoint} provider which will be used to
        really set up connections.

    @ivar contextFactory: A L{ContextFactory} to use to do TLS.
    c                 C   s   || _ || _d S r0   )wrappedcontextFactory)r.   r5   r6   r)   r)   r*   __init__Y   s    zStartTLSClientEndpoint.__init__c                    s$   G  fdddt }j| S )z
        Establish a connection using a protocol build by C{factory} and
        immediately start TLS on it.  Return a L{Deferred} which fires with the
        protocol instance.
        c                       s   e Zd Z fddZdS )z6StartTLSClientEndpoint.connect.<locals>.WrapperFactoryc                    s(    |  jf fdd	}| _ S )Nc                    s    j j |   d S r0   )	transportstartTLSr6   )Zorig)protocolr.   r)   r*   connectionMadei   s    z\StartTLSClientEndpoint.connect.<locals>.WrapperFactory.buildProtocol.<locals>.connectionMade)buildProtocolr;   )ZwrapperSelfZaddrr;   factoryr.   )r:   r*   r<   g   s    
zDStartTLSClientEndpoint.connect.<locals>.WrapperFactory.buildProtocolN)r$   r%   r&   r<   r)   r=   r)   r*   WrapperFactoryf   s   r?   )r   r5   connect)r.   r>   r?   r)   r=   r*   r@   ^   s    	zStartTLSClientEndpoint.connectN)r$   r%   r&   __doc__r7   r@   r)   r)   r)   r*   r4   M   s   
r4   c                   @   s    e Zd ZdZdd Zdd ZdS )StartTLSClientCreatorz{
    Create L{ITLSTransport.startTLS} endpoint for the client, and normal SSL
    for server just because it's easier.
    c                 C   s   t |d|  S )z
        Construct an SSL server endpoint.  This should be constructing a TCP
        server endpoint which immediately calls C{startTLS} instead, but that
        is hard.
        r   r   r/   r.   reactorr)   r)   r*   serverx   s    zStartTLSClientCreator.serverc                 C   s   t t|d|jt S )zS
        Construct a TCP client endpoint wrapped to immediately start TLS.
        	127.0.0.1)r4   r   portr"   r.   rE   ZserverAddressr)   r)   r*   client   s      zStartTLSClientCreator.clientNr$   r%   r&   rA   rF   rJ   r)   r)   r)   r*   rB   s   s   	rB   c                   @   s   e Zd ZdZdd ZdS )BadContextTestsMixinz
    Mixin for L{ReactorBuilder} subclasses which defines a helper for testing
    the handling of broken context factories.
    c                 C   s0   |   }| t||t }| tjt| dS )a  
        Assert that the exception raised by a broken context factory's
        C{getContext} method is raised by some reactor method.  If it is not, an
        exception will be raised to fail the test.

        @param useIt: A two-argument callable which will be called with a
            reactor and a broken context factory and which is expected to raise
            the same exception as the broken context factory's C{getContext}
            method.
        N)buildReactorZassertRaises
ValueErrorr   ZassertEqualmessagestr)r.   useItrE   excr)   r)   r*   _testBadContext   s       z$BadContextTestsMixin._testBadContextN)r$   r%   r&   rA   rS   r)   r)   r)   r*   rL      s   rL   c                   @   s   e Zd ZdZe ZdS )StartTLSClientTestsMixinz
    Tests for TLS connections established using L{ITLSTransport.startTLS} (as
    opposed to L{IReactorSSL.connectSSL} or L{IReactorSSL.listenSSL}).
    N)r$   r%   r&   rA   rB   	endpointsr)   r)   r)   r*   rT      s   rT   c                   @   s    e Zd ZdZdd Zdd ZdS )
SSLCreatorz
    Create SSL endpoints.
    c                 C   s   t |d|  S )zQ
        Create an SSL server endpoint on a TCP/IP-stack allocated port.
        r   rC   rD   r)   r)   r*   rF      s    zSSLCreator.serverc                 C   s   t |d|jt S )z
        Create an SSL client endpoint which will connect localhost on
        the port given by C{serverAddress}.

        @type serverAddress: L{IPv4Address}
        rG   )r   rH   r"   rI   r)   r)   r*   rJ      s      zSSLCreator.clientNrK   r)   r)   r)   r*   rV      s   rV   c                   @   s&   e Zd ZdZe Zdd Zdd ZdS )SSLClientTestsMixinz<
    Mixin defining tests relating to L{ITLSTransport}.
    c                 C   s   dd }|  | dS )z
        If the context factory passed to L{IReactorSSL.connectSSL} raises an
        exception from its C{getContext} method, that exception is raised by
        L{IReactorSSL.connectSSL}.
        c                 S   s   |  ddt |S )NrG   i  )
connectSSLr	   rE   r6   r)   r)   r*   rQ      s       z2SSLClientTestsMixin.test_badContext.<locals>.useItNrS   r.   rQ   r)   r)   r*   test_badContext   s    z#SSLClientTestsMixin.test_badContextc                    s  G dd dt }|  t }t |_||_|  |_t }t |_||_| 	 |_|jj
|j_
g  t|j|jgdd} fdd}|| jd|dd	}| |j | j| j|}| |j |fd
d |   d t  d t dS )aO  
        L{ITCPTransport.loseConnection} ends a connection which was set up with
        L{ITLSTransport.startTLS} and which has recently been written to.  This
        is intended to verify that a socket send error masked by the TLS
        implementation doesn't prevent the connection from being reported as
        closed.
        c                   @   s$   e Zd Zdd Zdd Zdd ZdS )zQSSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.ShortProtocolc                 S   sN   t | js.| jj}d | j_|td d S | j| jj | j	d d S )NzNo ITLSTransport support   x)
r   Z
providedByr8   r>   finishedZerrbackr   r9   contextwrite)r.   r^   r)   r)   r*   r;      s    z`SSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.ShortProtocol.connectionMadec                 S   s   | j d | j   d S )N   y)r8   r`   ZloseConnection)r.   datar)   r)   r*   dataReceived   s    z^SSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.ShortProtocol.dataReceivedc                 S   s&   | j j}|d k	r"d | j _|| d S r0   )r>   r^   callback)r.   reasonr^   r)   r)   r*   connectionLost   s    z`SSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.ShortProtocol.connectionLostN)r$   r%   r&   r;   rc   rf   r)   r)   r)   r*   ShortProtocol   s   
rg   T)ZconsumeErrorsc                    s"     | d d | d d g d S )Nr      )extend)Zresults)lostConnectionResultsr)   r*   
cbFinished  s    zNSSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.cbFinishedr   rG   )Z	interfacec                    s      S r0   )stop)ZignrE   r)   r*   <lambda>      zLSSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.<lambda>rh   N)r
   rM   r   r   r^   r:   r/   r_   r	   r2   methodr   ZaddCallbackZ	listenTCPZ
addCleanupZstopListeningZ
connectTCPgetHostZhostrH   Z
disconnectZ
runReactorZtrapr   )r.   rg   ZserverFactoryZclientFactoryr^   rk   rH   Z	connectorr)   )rj   rE   r*   &test_disconnectAfterWriteAfterStartTLS   s<    !



  
z:SSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLSN)r$   r%   r&   rA   rV   rU   r\   rr   r)   r)   r)   r*   rW      s   rW   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )TLSPortTestsBuilderz,
    Tests for L{IReactorSSL.listenSSL}
    c                 C   s   | d||  S )z0
        Get a TLS port from a reactor.
        r   )	listenSSLr/   )r.   rE   r>   r)   r)   r*   getListeningPort*  s    z$TLSPortTestsBuilder.getListeningPortc                 C   s   d||  jf S )zY
        Get the message expected to be logged when a TLS port starts listening.
        z%s (TLS) starting on %drq   rH   )r.   rH   r>   r)   r)   r*   #getExpectedStartListeningLogMessage1  s    z7TLSPortTestsBuilder.getExpectedStartListeningLogMessagec                 C   s   d|  jf S )zJ
        Get the expected connection lost message for a TLS port.
        z(TLS Port %s Closed)rv   )r.   rH   r)   r)   r*   getExpectedConnectionLostLogMsg8  s    z3TLSPortTestsBuilder.getExpectedConnectionLostLogMsgc                 C   s   dd }|  | dS )z
        If the context factory passed to L{IReactorSSL.listenSSL} raises an
        exception from its C{getContext} method, that exception is raised by
        L{IReactorSSL.listenSSL}.
        c                 S   s   |  dt |S )Nr   )rt   r   rY   r)   r)   r*   rQ   E  s    z2TLSPortTestsBuilder.test_badContext.<locals>.useItNrZ   r[   r)   r)   r*   r\   ?  s    z#TLSPortTestsBuilder.test_badContextc                 C   s   | | j|j||  S )a  
        Connect to the given listening TLS port, assuming the
        underlying transport is TCP.

        @param reactor: The reactor under test.
        @type reactor: L{IReactorSSL}

        @param address: The listening's address.  Only the C{port}
            component is used; see
            L{ConnectToTCPListenerMixin.LISTENER_HOST}.
        @type address: L{IPv4Address} or L{IPv6Address}

        @param factory: The client factory.
        @type factory: L{ClientFactory}

        @return: The connector
        )rX   ZLISTENER_HOSTrH   r2   )r.   rE   Zaddressr>   r)   r)   r*   connectToListenerJ  s    z%TLSPortTestsBuilder.connectToListenerN)	r$   r%   r&   rA   ru   rw   rx   r\   ry   r)   r)   r)   r*   rs   "  s   rs   c                   @   s,   e Zd ZdZefZe Zdd Zdd Z	dS )AbortSSLConnectionTestsz-
    C{abortConnection} tests using SSL.
    c                    s<   t |  ddlm} t fddd}| |d|j  S )Nr   )_producer_helpersc                    s     d| S )Ngh㈵>)Z	callLater)xrm   r)   r*   rn   y  ro   z6AbortSSLConnectionTests.buildReactor.<locals>.<lambda>)Z	scheduler	cooperate)r   rM   Ztwisted.internetr{   r   Zpatchr}   )r.   r{   Z
cooperatorr)   rm   r*   rM   r  s    

z$AbortSSLConnectionTests.buildReactorc                 C   s   t d krtdd S )NzOpenSSL not available.)r   r   r1   r)   r)   r*   setUp~  s    zAbortSSLConnectionTests.setUpN)
r$   r%   r&   rA   r   r'   rV   rU   rM   r~   r)   r)   r)   r*   rz   k  s
   rz   )DrA   Z
__future__r   r   typeZ__metaclass__Zzope.interfacer   Ztwisted.python.compatr   Ztwisted.python.filepathr   Z#twisted.internet.test.reactormixinsr   Ztwisted.internet.protocolr   r	   r
   Ztwisted.internet.interfacesr   r   r   Ztwisted.internet.deferr   r   Ztwisted.internet.endpointsr   r   r   Ztwisted.internet.errorr   Ztwisted.internet.taskr   Ztwisted.trial.unittestr   Ztwisted.python.runtimer   Ztwisted.internet.test.test_corer   Ztwisted.internet.test.test_tcpr   r   r   Z&twisted.internet.test.connectionmixinsr   r   r   ZOpenSSL.cryptor   ImportErrorZtwisted.internet.sslr   r    r"   r#   objectr+   r4   rB   rL   rT   rV   rW   rs   globalsupdateZmakeTestCaseClassesrz   r)   r)   r)   r*   <module>   s^   
%	 _  C