3
,sYC1                 @   sF  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) ddl*m+Z+m,Z,m-Z- yddl.m/Z/ W n e0k
r   dZ/Y nX ddl1m2Z2m3Z3 ddl1m4Z4 G dd dZ5G dd de6Z7eeG dd de6Z8G dd de+e7Z9G dd de6Z:G d d! d!e5ee,Z;G d"d# d#e+e7Z<G d$d% d%e5ee7e,e:Z=G d&d' d'e5e7e&e:e(eZ>e? j@e=jA  e? j@e;jA  e? j@e> jA  G d(d) d)ee)e7ZBe? j@eBjA  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)StreamTransportTestsMixinAbortConnectionMixin)EndpointCreatorConnectionTestsMixinBrokenContextFactory)FILETYPE_PEMN)PrivateCertificateKeyPair)ClientContextFactoryc               @   s(   e Zd ZegZej 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jdjdZ	[dd Z
dd ZdS )	ContextGeneratingMixinr   Ns   tests
   server.pemc             C   s(   | j j }tj|tj|tt}|j 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 )N)r    )r,   r'   r'   r(   getClientContextG   s    z'ContextGeneratingMixin.getClientContext)r"   r#   r$   Ztwistedr   r   __file__ZsiblingZchildr*   r-   r.   r'   r'   r'   r(   r)   7   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 )N)wrappedcontextFactory)r,   r1   r2   r'   r'   r(   __init__X   s    zStartTLSClientEndpoint.__init__c                s$   G  fdddt }j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 |  jf fdd	}| _ S )Nc                s    j jj |   d S )N)	transportstartTLSr2   )Zorig)protocolr,   r'   r(   connectionMadeh   s    z\StartTLSClientEndpoint.connect.<locals>.WrapperFactory.buildProtocol.<locals>.connectionMade)buildProtocolr7   )ZwrapperSelfZaddrr7   )factoryr,   )r6   r(   r8   f   s    
zDStartTLSClientEndpoint.connect.<locals>.WrapperFactory.buildProtocolN)r"   r#   r$   r8   r'   )r9   r,   r'   r(   WrapperFactorye   s   r:   )r   r1   connect)r,   r9   r:   r'   )r9   r,   r(   r;   ]   s    	zStartTLSClientEndpoint.connectN)r"   r#   r$   __doc__r3   r;   r'   r'   r'   r(   r0   L   s   
r0   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| j 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(   serverw   s    zStartTLSClientCreator.serverc             C   s   t t|d|jt S )zS
        Construct a TCP client endpoint wrapped to immediately start TLS.
        z	127.0.0.1)r0   r   portr    )r,   r>   serverAddressr'   r'   r(   client   s    
zStartTLSClientCreator.clientN)r"   r#   r$   r<   r?   rB   r'   r'   r'   r(   r=   r   s   	r=   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   | j  }| jt||t }| j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,   useItr>   excr'   r'   r(   _testBadContext   s    z$BadContextTestsMixin._testBadContextN)r"   r#   r$   r<   rJ   r'   r'   r'   r(   rC      s   rC   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$   r<   r=   	endpointsr'   r'   r'   r(   rK      s   rK   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| j S )zQ
        Create an SSL server endpoint on a TCP/IP-stack allocated port.
        r   )r   r-   )r,   r>   r'   r'   r(   r?      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}
        z	127.0.0.1)r   r@   r    )r,   r>   rA   r'   r'   r(   rB      s    zSSLCreator.clientN)r"   r#   r$   r<   r?   rB   r'   r'   r'   r(   rM      s   rM   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 }| j | 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   | j ddt |S )Nz	127.0.0.1i  )Z
connectSSLr	   )r>   r2   r'   r'   r(   rH      s    z2SSLClientTestsMixin.test_badContext.<locals>.useItN)rJ   )r,   rH   r'   r'   r(   test_badContext   s    z#SSLClientTestsMixin.test_badContextc                s  G dd dt }| j t }t |_||_| j |_t }t |_||_| j	 |_|jj
|j_
g  t|j|jgdd} fdd}|j| jd|dd	}| j|j j|j j|j j|}| j|j |jfd
d | j  d jt  d j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| js.| jj}d | j_|jtd d S | jj| jj | jj	d d S )NzNo ITLSTransport support   x)
r   Z
providedByr4   r9   finishedZerrbackr   r5   contextwrite)r,   rQ   r'   r'   r(   r7      s    z`SSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.ShortProtocol.connectionMadec             S   s   | j jd | j j  d S )N   y)r4   rS   ZloseConnection)r,   datar'   r'   r(   dataReceived   s    z^SSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.ShortProtocol.dataReceivedc             S   s&   | j j}|d k	r"d | j _|j| d S )N)r9   rQ   callback)r,   reasonrQ   r'   r'   r(   connectionLost   s    z`SSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.ShortProtocol.connectionLostN)r"   r#   r$   r7   rV   rY   r'   r'   r'   r(   ShortProtocol   s   
rZ   T)ZconsumeErrorsc                s"    j | d d | d d g d S )Nr      )extend)Zresults)lostConnectionResultsr'   r(   
cbFinished  s    zNSSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.cbFinishedr   z	127.0.0.1)Z	interfacec                s    j  S )N)stop)Zign)r>   r'   r(   <lambda>  s    zLSSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.<lambda>r[   N)r
   rD   r   r   rQ   r6   r-   rR   r	   r.   methodr   ZaddCallbackZ	listenTCPZ
addCleanupZstopListeningZ
connectTCPgetHosthostr@   Z
disconnectZ
runReactorZtrapr   )r,   rZ   ZserverFactoryZclientFactoryrQ   r^   r@   Z	connectorr'   )r]   r>   r(   &test_disconnectAfterWriteAfterStartTLS   s4    !




z:SSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLSN)r"   r#   r$   r<   rM   rL   rO   rd   r'   r'   r'   r(   rN      s   rN   c               @   s0   e Zd Z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   |j d|| j S )z0
        Get a TLS port from a reactor.
        r   )	listenSSLr-   )r,   r>   r9   r'   r'   r(   getListeningPort'  s    z$TLSPortTestsBuilder.getListeningPortc             C   s   d||j  jf S )zY
        Get the message expected to be logged when a TLS port starts listening.
        z%s (TLS) starting on %d)rb   r@   )r,   r@   r9   r'   r'   r(   #getExpectedStartListeningLogMessage.  s    z7TLSPortTestsBuilder.getExpectedStartListeningLogMessagec             C   s   d|j  jf S )zJ
        Get the expected connection lost message for a TLS port.
        z(TLS Port %s Closed))rb   r@   )r,   r@   r'   r'   r(   getExpectedConnectionLostLogMsg5  s    z3TLSPortTestsBuilder.getExpectedConnectionLostLogMsgc             C   s   dd }| j | 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   | j dt |S )Nr   )rf   r   )r>   r2   r'   r'   r(   rH   B  s    z2TLSPortTestsBuilder.test_badContext.<locals>.useItN)rJ   )r,   rH   r'   r'   r(   rO   <  s    z#TLSPortTestsBuilder.test_badContextN)r"   r#   r$   r<   rg   rh   ri   rO   r'   r'   r'   r(   re   !  s
   re   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 j|  ddlm} t fddd}| j|d|j  S )Nr   )_producer_helpersc                s    j d| S )Ngh㈵>)Z	callLater)x)r>   r'   r(   r`   \  s    z6AbortSSLConnectionTests.buildReactor.<locals>.<lambda>)Z	scheduler	cooperate)r   rD   Ztwisted.internetrk   r   Zpatchrm   )r,   rk   Z
cooperatorr'   )r>   r(   rD   U  s    
z$AbortSSLConnectionTests.buildReactorc             C   s   t d krtdd S )NzOpenSSL not available.)r   r   )r,   r'   r'   r(   setUpa  s    zAbortSSLConnectionTests.setUpN)
r"   r#   r$   r<   r   r%   rM   rL   rD   rn   r'   r'   r'   r(   rj   N  s
   rj   )Cr<   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   Z&twisted.internet.test.connectionmixinsr   r   r   ZOpenSSL.cryptor   ImportErrorZtwisted.internet.sslr   r   r    r!   objectr)   r0   r=   rC   rK   rM   rN   re   globalsupdateZmakeTestCaseClassesrj   r'   r'   r'   r(   <module>   sR   
%	^%