U
     T‚gèZ  ã                   @   s  d Z ddlZddlZddlmZ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 dd
lmZ z0ddlmamZ ddlma ddlmZmZ W n" ek
rÒ   dd„ Zeƒ  Y nX ddlm Z  G dd„ dej!ƒZ"G dd„ dej!ƒZ#G dd„ dej$ƒZ%G dd„ dej$ƒZ&e ej'ƒG dd„ dej$ƒƒZ(dd„ Z)dd„ Z*G dd „ d ƒZ+tdk	�r~G d!d"„ d"tj,ƒZ-G d#d$„ d$eeƒZ.G d%d&„ d&eƒZ/G d'd(„ d(e/ƒZ0G d)d*„ d*eƒZ1G d+d,„ d,ee+ƒZ2G d-d.„ d.ƒZ3G d/d0„ d0eƒZ4G d1d2„ d2eƒZ5dS )3z 
Tests for twisted SSL support.
é    N)ÚdeferÚ
interfacesÚprotocolÚreactor)ÚConnectionDone)ÚwaitUntilAllDisconnected)Úbasic)ÚFilePath)Úplatform)ÚProperlyCloseFilesMixin)ÚTestCase)ÚSSLÚcrypto)Ússl)ÚClientTLSContextÚcertPathc                   C   s   d  a ad S ©N)r   r   © r   r   ú9/tmp/pip-unpacked-wheel-1_7di8ic/twisted/test/test_ssl.pyÚ_noSSL   s    r   )Úimplementerc                   @   sB   e Zd ZdZdddgZddgZdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )ÚUnintelligentProtocola  
    @ivar deferred: a deferred that will fire at connection lost.
    @type deferred: L{defer.Deferred}

    @cvar pretext: text sent before TLS is set up.
    @type pretext: C{bytes}

    @cvar posttext: text sent after TLS is set up.
    @type posttext: C{bytes}
    s
   first lines   last thing before tls startsó   STARTTLSs   first thing after tls starteds   last thing everc                 C   s   t  ¡ | _d S r   ©r   ÚDeferredÚdeferred©Úselfr   r   r   Ú__init__7   s    zUnintelligentProtocol.__init__c                 C   s   | j D ]}|  |¡ qd S r   )ÚpretextÚsendLine)r   Úlr   r   r   ÚconnectionMade:   s    
z$UnintelligentProtocol.connectionMadec                 C   s@   |dkr<| j  tƒ | jj¡ | jD ]}|  |¡ q"| j  ¡  d S )Nó   READY)Ú	transportÚstartTLSr   ÚfactoryÚclientÚposttextr    ÚloseConnection)r   Úliner!   r   r   r   ÚlineReceived>   s
    
z"UnintelligentProtocol.lineReceivedc                 C   s   | j  d ¡ d S r   ©r   Úcallback©r   Úreasonr   r   r   ÚconnectionLostE   s    z$UnintelligentProtocol.connectionLostN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r(   r   r"   r+   r0   r   r   r   r   r   '   s   
r   c                   @   s:   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚLineCollectoraJ  
    @ivar deferred: a deferred that will fire at connection lost.
    @type deferred: L{defer.Deferred}

    @ivar doTLS: whether the protocol is initiate TLS or not.
    @type doTLS: C{bool}

    @ivar fillBuffer: if set to True, it will send lots of data once
        C{STARTTLS} is received.
    @type fillBuffer: C{bool}
    Fc                 C   s   || _ || _t ¡ | _d S r   )ÚdoTLSÚ
fillBufferr   r   r   )r   r6   r7   r   r   r   r   V   s    zLineCollector.__init__c                 C   s   d| j _g | j _d S )Nó    )r&   ÚrawdataÚlinesr   r   r   r   r"   [   s    zLineCollector.connectionMadec                 C   sp   | j j |¡ |dkrl| jr4tdƒD ]}|  d¡ q$|  d¡ | jrdtttd�}| j	 
|| j j¡ n|  ¡  d S )Nr   iô  sè  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXr#   )ZprivateKeyFileNameZcertificateFileName)r&   r:   Úappendr7   Úranger    r6   ÚServerTLSContextr   r$   r%   ÚserverZ
setRawMode)r   r*   ÚxÚctxr   r   r   r+   _   s    
þzLineCollector.lineReceivedc                 C   s   | j  j|7  _| j ¡  d S r   )r&   r9   r$   r)   ©r   Údatar   r   r   ÚrawDataReceivedo   s    zLineCollector.rawDataReceivedc                 C   s   | j  d ¡ d S r   r,   r.   r   r   r   r0   s   s    zLineCollector.connectionLostN)F)	r1   r2   r3   r4   r   r"   r+   rC   r0   r   r   r   r   r5   I   s   
r5   c                   @   s   e Zd ZdZdd„ ZdS )ÚSingleLineServerProtocolzK
    A protocol that sends a single line of data at C{connectionMade}.
    c                 C   s   | j  d¡ | j  ¡  d S )Nó   +OK <some crap>
)r$   ÚwriteÚgetPeerCertificater   r   r   r   r"   |   s    z'SingleLineServerProtocol.connectionMadeN)r1   r2   r3   r4   r"   r   r   r   r   rD   w   s   rD   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚRecordingClientProtocolzv
    @ivar deferred: a deferred that will fire with first received content.
    @type deferred: L{defer.Deferred}
    c                 C   s   t  ¡ | _d S r   r   r   r   r   r   r   ‡   s    z RecordingClientProtocol.__init__c                 C   s   | j  ¡  d S r   )r$   rG   r   r   r   r   r"   Š   s    z&RecordingClientProtocol.connectionMadec                 C   s   | j  |¡ d S r   r,   rA   r   r   r   ÚdataReceived�   s    z$RecordingClientProtocol.dataReceivedN)r1   r2   r3   r4   r   r"   rI   r   r   r   r   rH   �   s   rH   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú ImmediatelyDisconnectingProtocolz•
    A protocol that disconnect immediately on connection. It fires the
    C{connectionDisconnected} deferred of its factory on connetion lost.
    c                 C   s   | j  ¡  d S r   ©r$   r)   r   r   r   r   ÚhandshakeCompleted˜   s    z3ImmediatelyDisconnectingProtocol.handshakeCompletedc                 C   s   | j j d ¡ d S r   )r&   ÚconnectionDisconnectedr-   r.   r   r   r   r0   ›   s    z/ImmediatelyDisconnectingProtocol.connectionLostN)r1   r2   r3   r4   rL   r0   r   r   r   r   rJ   ‘   s   rJ   c                 C   s®   t  ¡ }| t jd¡ t  ¡ }| ¡ }| |_||_| |¡ | 	|d¡ t  
¡ }| d¡ | d¡ | d¡ | | ¡ ¡ | | ¡ ¡ | | ¡ ¡ | 	|d¡ |||fS )z‘
    Create a certificate for given C{organization} and C{organizationalUnit}.

    @return: a tuple of (key, request, certificate) objects.
    i   Úmd5é   r   é<   )r   ZPKeyZgenerate_keyZTYPE_RSAZX509ReqZget_subjectÚOZOUZ
set_pubkeyÚsignZX509Zset_serial_numberZgmtime_adj_notBeforeZgmtime_adj_notAfterZ
set_issuerZset_subjectZ
get_pubkey)ÚorganizationÚorganizationalUnitÚpkeyÚreqÚsubjectÚcertr   r   r   ÚgenerateCertificateObjectsŸ   s"    



rY   c           
      C   sn   t ||ƒ\}}}d|tjfd|tjfd|tjffD ]6\}}}tj | |f¡ d¡}	t	|	ƒ 
|tj|ƒ¡ q2dS )z…
    Create certificate files key, req and cert prefixed by C{basename} for
    given C{organization} and C{organizationalUnit}.
    ÚkeyrV   rX   zutf-8N)rY   r   Zdump_privatekeyZdump_certificate_requestZdump_certificateÚosÚextsepÚjoinÚencoder	   Z
setContentZFILETYPE_PEM)
ÚbasenamerS   rT   rU   rV   rX   ÚextÚobjZdumpFuncZfNamer   r   r   ÚgenerateCertificateFiles»   s    


ýrb   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚContextGeneratingMixinah  
    Offer methods to create L{ssl.DefaultOpenSSLContextFactory} for both client
    and server.

    @ivar clientBase: prefix of client certificate files.
    @type clientBase: C{str}

    @ivar serverBase: prefix of server certificate files.
    @type serverBase: C{str}

    @ivar clientCtxFactory: a generated context factory to be used in
        L{IReactorSSL.connectSSL}.
    @type clientCtxFactory: L{ssl.DefaultOpenSSLContextFactory}

    @ivar serverCtxFactory: a generated context factory to be used in
        L{IReactorSSL.listenSSL}.
    @type serverCtxFactory: L{ssl.DefaultOpenSSLContextFactory}
    c                 O   sH   |   ¡ }t|||ƒ tjtj |df¡tj |df¡f|ž|Ž}||fS )NrZ   rX   )Úmktemprb   r   ÚDefaultOpenSSLContextFactoryr[   r\   r]   )r   ÚorgZorgUnitÚargsZkwArgsÚbaseÚserverCtxFactoryr   r   r   ÚmakeContextFactoryß   s    þýüz)ContextGeneratingMixin.makeContextFactoryc                 C   s,   | j ||Ž\| _| _| j ||Ž\| _| _d S r   )rj   Z
clientBaseÚclientCtxFactoryZ
serverBaseri   )r   Z
clientArgsZclientKwArgsZ
serverArgsZserverKwArgsr   r   r   ÚsetupServerAndClientë   s     ÿ ÿz+ContextGeneratingMixin.setupServerAndClientN)r1   r2   r3   r4   rj   rl   r   r   r   r   rc   Ë   s   rc   c                   @   s   e Zd ZdZdZdd„ ZdS )r=   zf
        A context factory with a default method set to
        L{OpenSSL.SSL.SSLv23_METHOD}.
        Fc                 O   s"   t j|d< tjj| f|ž|Ž d S )NZ	sslmethod)r   ZSSLv23_METHODr   re   r   )r   rg   Úkwr   r   r   r   þ   s    
zServerTLSContext.__init__N)r1   r2   r3   r4   ZisClientr   r   r   r   r   r=   ö   s   r=   c                   @   sD   e Zd ZdZe ed¡dkr dZdd„ Zdd„ Z	dd	„ Z
d
d„ ZdS )ÚStolenTCPTestszc
    For SSL transports, test many of the same things which are tested for
    TCP transports.
    Nú2Reactor does not support SSL, cannot run SSL testsc                 C   s.   t j ttƒ ¡ ¡}| ¡ }tj||||d�S )zY
        Create an SSL server with a certificate using L{IReactorSSL.listenSSL}.
        ©Z	interface)	r   ZPrivateCertificateZloadPEMr	   r   Z
getContentÚoptionsr   Ú	listenSSL)r   ÚaddressÚ
portNumberr&   rX   ÚcontextFactoryr   r   r   ÚcreateServer  s    zStolenTCPTests.createServerc                 C   s   t  ¡ }| |||¡S )zG
        Create an SSL client using L{IReactorSSL.connectSSL}.
        )r   ZCertificateOptionsÚ
connectSSL)r   rs   rt   ZclientCreatorru   r   r   r   ÚconnectClient  s    zStolenTCPTests.connectClientc                 C   s   t jS )zº
        Return L{OpenSSL.SSL.Error} as the expected error type which will be
        raised by a write to the L{OpenSSL.SSL.Connection} object after it has
        been closed.
        )r   ÚErrorr   r   r   r   ÚgetHandleExceptionType  s    z%StolenTCPTests.getHandleExceptionTypec                 C   s<   t  t  t  d¡t  t  d¡t  d¡t  d¡¡t  d¡¡¡S )a4  
        Return a L{hamcrest.core.matcher.Matcher} for the argument
        L{OpenSSL.SSL.Error} will be constructed with for this case.
        This is basically just a random OpenSSL implementation detail.
        It would be better if this test worked in a way which did not
        require this.
        zSSL routinesZ	SSL_writeZssl_write_internalÚ zprotocol is shutdown)ÚhamcrestÚcontainsZequal_toZany_ofr   r   r   r   ÚgetHandleErrorCodeMatcher#  s    
ýùÿz(StolenTCPTests.getHandleErrorCodeMatcher)r1   r2   r3   r4   r   ÚIReactorSSLr   Úskiprv   rx   rz   r~   r   r   r   r   rn     s   rn   c                   @   sZ   e Zd ZdZe ed¡dkr dZdZdZ	dZ
dd„ Zddd„Zd	d
„ Zdd„ Zdd„ ZdS )ÚTLSTestszƒ
    Tests for startTLS support.

    @ivar fillBuffer: forwarded to L{LineCollector.fillBuffer}
    @type fillBuffer: C{bool}
    Nro   Fc                 C   s4   | j jd k	r| j j ¡  | jjd k	r0| jj ¡  d S r   )ÚclientProtor$   r)   ÚserverProtor   r   r   r   ÚtearDownJ  s    zTLSTests.tearDownc                    sª   ˆ | _ t ¡  }| _‡ fdd„|_|r.d|_nd|_ˆ| _t ¡  }| _‡fdd„|_|rbd|_nd|_t	j
d|dd�}|  |j¡ t	 d| ¡ j|¡ t ˆ jˆjg¡S )	a½  
        Helper method to run TLS tests.

        @param clientProto: protocol instance attached to the client
            connection.
        @param serverProto: protocol instance attached to the server
            connection.
        @param clientIsServer: flag indicated if client should initiate
            startTLS instead of server.

        @return: a L{defer.Deferred} that will fire when both connections are
            lost.
        c                      s   ˆ S r   r   r   ©r‚   r   r   Ú<lambda>`  r8   z#TLSTests._runTest.<locals>.<lambda>FTc                      s   ˆ S r   r   r   ©rƒ   r   r   r†   h  r8   r   ú	127.0.0.1rp   )r‚   r   ÚClientFactoryÚclientFactoryr>   r'   rƒ   ÚServerFactoryÚserverFactoryr   Z	listenTCPÚ
addCleanupÚstopListeningZ
connectTCPÚgetHostÚportr   ÚgatherResultsr   )r   r‚   rƒ   ZclientIsServerÚcfZsfr�   r   ©r‚   rƒ   r   Ú_runTestP  s     zTLSTests._runTestc                    s,   ‡ fdd„}ˆ   tƒ tdˆ jƒ¡}| |¡S )z~
        Test for server and client startTLS: client should received data both
        before and after the startTLS.
        c                    s   ˆ   ˆ jjtjtj ¡ d S r   )ÚassertEqualrŒ   r:   r   r   r(   )Úignorer   r   r   Úcheck{  s    
þz TLSTests.test_TLS.<locals>.checkT©r”   r   r5   r7   ÚaddCallback©r   r—   Údr   r   r   Útest_TLSu  s    zTLSTests.test_TLSc                    s,   ‡ fdd„}ˆ   tƒ tdˆ jƒ¡}| |¡S )z›
        Test for server startTLS not followed by a startTLS in client: the data
        received after server startTLS should be received as raw.
        c                    s&   ˆ   ˆ jjtj¡ ˆ  ˆ jjd¡ d S )NzNo encrypted bytes received)r•   rŒ   r:   r   r   Ú
assertTruer9   ©Zignoredr   r   r   r—   Š  s    z"TLSTests.test_unTLS.<locals>.checkFr˜   rš   r   r   r   Ú
test_unTLS„  s     
ÿzTLSTests.test_unTLSc                    s.   ‡ fdd„}ˆ   tdˆ jƒtƒ d¡}| |¡S )z:
        Test startTLS first initiated by client.
        c                    s   ˆ   ˆ jjtjtj ¡ d S r   )r•   rŠ   r:   r   r   r(   rž   r   r   r   r—   ˜  s    
þz)TLSTests.test_backwardsTLS.<locals>.checkT)r”   r5   r7   r   r™   rš   r   r   r   Útest_backwardsTLS“  s    
  ÿzTLSTests.test_backwardsTLS)F)r1   r2   r3   r4   r   r   r   r€   r7   r‚   rƒ   r„   r”   rœ   rŸ   r    r   r   r   r   r�   :  s   
%r�   c                   @   s(   e Zd ZdZe ed¡dkr dZdZdS )ÚSpammyTLSTestszA
    Test TLS features with bytes sitting in the out buffer.
    Nro   T)	r1   r2   r3   r4   r   r   r   r€   r7   r   r   r   r   r¡   ¤  s   r¡   c                   @   s8   e Zd Ze ed¡dkrdZdZdZdd„ Z	dd„ Z
dS )ÚBufferingTestsNro   c                 C   sB   | j jd k	r| j j ¡  | jjd k	r0| jj ¡  tt| j | jgƒS r   )rƒ   r$   r)   r‚   r   r   r   r   r   r   r„   ¶  s
    zBufferingTests.tearDownc                    s®   t ƒ  ‰| _tƒ  ‰ | _t ¡ }t ¡  }| _‡fdd„|_‡ fdd„|_t 	t
t
¡}t ¡ }tjd||dd�}|  |j¡ t d| ¡ j||¡}|  |j¡ ˆ j | jd¡S )Nc                      s   ˆ S r   r   r   r‡   r   r   r†   Å  r8   z6BufferingTests.test_openSSLBuffering.<locals>.<lambda>c                      s   ˆ S r   r   r   r…   r   r   r†   Æ  r8   r   rˆ   rp   rE   )rD   rƒ   rH   r‚   r   r‹   r‰   r'   r   re   r   ÚClientContextFactoryr   rr   r�   rŽ   rw   r�   r�   Z
disconnectr   r™   r•   )r   r>   r'   ZsCTXZcCTXr�   ZclientConnectorr   r“   r   Útest_openSSLBuffering¾  s*       ÿ ÿz$BufferingTests.test_openSSLBuffering)r1   r2   r3   r   r   r   r€   rƒ   r‚   r„   r¤   r   r   r   r   r¢   ¯  s   r¢   c                   @   sD   e Zd ZdZe ed¡dkr dZdd„ Zdd„ Z	dd	„ Z
d
d„ ZdS )ÚConnectionLostTestsz'
    SSL connection closing tests.
    Nro   c                    sŽ   d}ˆ   ||d fi ||d fi ¡ t ¡ }tj|_t d|ˆ j¡ ˆ _}t ¡ }t	|_t
 ¡ |_t d| ¡ j|ˆ j¡ |j ‡ fdd„¡S )Nútwisted.test.test_sslú, clientú, serverr   rˆ   c                    s
   ˆ j  ¡ S r   )Ú
serverPortrŽ   )ZignoredResultr   r   r   r†   ù  r8   z=ConnectionLostTests.testImmediateDisconnect.<locals>.<lambda>)rl   r   r‹   ÚProtocolr   rr   ri   r©   r‰   rJ   r   r   rM   rw   r�   r�   rk   r™   )r   rf   ÚserverProtocolFactoryr©   ÚclientProtocolFactoryr   r   r   ÚtestImmediateDisconnectà  s4    
  
 ÿ  ÿ

ü
ÿz+ConnectionLostTests.testImmediateDisconnectc                    sÒ   t tjƒG dd„ dtjƒƒ}d}|  ||d fi ||d fi ¡ |ƒ ‰t ¡ }‡fdd„|_t d|| j	¡}|  
|j¡ |ƒ ‰ t ¡ }‡ fd	d„|_t d
| ¡ j|| j¡ dd„ }t ˆ j |¡ˆj |¡g¡S )zµ
        Both sides of SSL connection close connection; the connections should
        close cleanly, and only after the underlying TCP connection has
        disconnected.
        c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	zMConnectionLostTests.test_bothSidesLoseConnection.<locals>.CloseAfterHandshakeFc                 S   s   t  ¡ | _d S r   )r   r   Údoner   r   r   r   r     s    zVConnectionLostTests.test_bothSidesLoseConnection.<locals>.CloseAfterHandshake.__init__c                 S   s   | j  ¡  d S r   rK   r   r   r   r   rL   
  s    z`ConnectionLostTests.test_bothSidesLoseConnection.<locals>.CloseAfterHandshake.handshakeCompletedc                 S   s   | j  |¡ | ` d S r   )r®   Zerrbackr.   r   r   r   r0     s    z\ConnectionLostTests.test_bothSidesLoseConnection.<locals>.CloseAfterHandshake.connectionLostN)r1   r2   r3   ZgotDatar   rL   r0   r   r   r   r   ÚCloseAfterHandshake  s   r¯   r¦   r§   r¨   c                      s   ˆ S r   r   r   ©ÚserverProtocolr   r   r†     r8   zBConnectionLostTests.test_bothSidesLoseConnection.<locals>.<lambda>r   c                      s   ˆ S r   r   r   ©ÚclientProtocolr   r   r†     r8   rˆ   c                 S   s   |   t¡ d S r   )Útrapr   )Zfailurer   r   r   ÚcheckResult&  s    zEConnectionLostTests.test_bothSidesLoseConnection.<locals>.checkResult)r   r   ÚIHandshakeListenerr   rª   rl   r‹   r   rr   ri   r�   rŽ   r‰   rw   r�   r�   rk   r   r‘   r®   Z
addErrback)r   r¯   rf   r«   r©   r¬   rµ   r   ©r³   r±   r   Útest_bothSidesLoseConnectionü  s:    
  
 ÿü

þÿz0ConnectionLostTests.test_bothSidesLoseConnectionc           	         sè   d}|   ||d fi ||d fi ¡ dd„ }| j ¡  tj|¡ t ¡ }t 	¡ ‰|j
ˆ_t ¡ }‡fdd„|_t d|| j¡ | _}t ¡ }t 	¡ ‰ |j
ˆ _t ¡ }‡ fd	d„|_t d
| ¡ j|| j¡ tj||gdd�}| | j¡S )Nr¦   r§   r¨   c                  W   s   dS )NFr   )Úar   r   r   Úverify6  s    z4ConnectionLostTests.testFailedVerify.<locals>.verifyc                      s   ˆ S r   r   r   r°   r   r   r†   ?  r8   z6ConnectionLostTests.testFailedVerify.<locals>.<lambda>r   c                      s   ˆ S r   r   r   r²   r   r   r†   H  r8   rˆ   T)ZconsumeErrors)rl   rk   Ú
getContextZ
set_verifyr   ZVERIFY_PEERr   r   r   rª   r-   r0   r‹   r   rr   ri   r©   r‰   rw   r�   r�   ZDeferredListr™   Ú_cbLostConns)	r   rf   rº   ZserverConnLostr«   r©   ZclientConnLostr¬   Údlr   r·   r   ÚtestFailedVerify0  s@    
  
 ÿ  ÿ
üz$ConnectionLostTests.testFailedVerifyc                 C   sh   |\\}}\}}|   |¡ |   |¡ tjg}t ¡ rJddlm} | |¡ |j|Ž  |j|Ž  | j	 
¡ S )Nr   )ÚConnectionLost)ÚassertFalser   ry   r
   Z	isWindowsÚtwisted.internet.errorr¿   r;   r´   r©   rŽ   )r   ÚresultsZsSuccessZsResultZcSuccessZcResultZacceptableErrorsr¿   r   r   r   r¼   S  s    





z ConnectionLostTests._cbLostConns)r1   r2   r3   r4   r   r   r   r€   r­   r¸   r¾   r¼   r   r   r   r   r¥   Ø  s   4#r¥   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚFakeContextzK
    L{OpenSSL.SSL.Context} double which can more easily be inspected.
    c                 C   s   || _ d| _d S )Nr   )Ú_methodÚ_options)r   Úmethodr   r   r   r   s  s    zFakeContext.__init__c                 C   s   |  j |O  _ d S r   )rÅ   )r   rq   r   r   r   Úset_optionsw  s    zFakeContext.set_optionsc                 C   s   d S r   r   ©r   ÚfileNamer   r   r   Úuse_certificate_filez  s    z FakeContext.use_certificate_filec                 C   s   d S r   r   rÈ   r   r   r   Úuse_privatekey_file}  s    zFakeContext.use_privatekey_fileN)r1   r2   r3   r4   r   rÇ   rÊ   rË   r   r   r   r   rÃ   n  s
   rÃ   c                   @   sD   e Zd ZdZe ed¡dkr dZdd„ Zdd„ Z	dd	„ Z
d
d„ ZdS )Ú!DefaultOpenSSLContextFactoryTestsz8
    Tests for L{ssl.DefaultOpenSSLContextFactory}.
    Nro   c                 C   s"   t jtttd�| _| j ¡ | _d S )N)Ú_contextFactory)r   re   r   rÃ   ru   r»   Úcontextr   r   r   r   ÚsetUp‰  s      ÿz'DefaultOpenSSLContextFactoryTests.setUpc                 C   sB   |   | jjtj¡ |   | jjtj@ tj¡ |  | jjtj@ ¡ dS )z‹
        L{ssl.DefaultOpenSSLContextFactory.getContext} returns an SSL context
        which can use SSLv3 or TLSv1 but not SSLv2.
        N)	r•   rÎ   rÄ   r   Ú
TLS_METHODrÅ   ÚOP_NO_SSLv2rÀ   ÚOP_NO_TLSv1_2r   r   r   r   Útest_method‘  s    z-DefaultOpenSSLContextFactoryTests.test_methodc                 C   s   |   tjtjt|  ¡ ¡ dS )zÑ
        Instantiating L{ssl.DefaultOpenSSLContextFactory} with a certificate
        filename which does not identify an existing file results in the
        initializer raising L{OpenSSL.SSL.Error}.
        N)ÚassertRaisesr   ry   r   re   r   rd   r   r   r   r   Útest_missingCertificateFileŸ  s       ÿz=DefaultOpenSSLContextFactoryTests.test_missingCertificateFilec                 C   s   |   tjtj|  ¡ t¡ dS )zÑ
        Instantiating L{ssl.DefaultOpenSSLContextFactory} with a private key
        filename which does not identify an existing file results in the
        initializer raising L{OpenSSL.SSL.Error}.
        N)rÔ   r   ry   r   re   rd   r   r   r   r   r   Útest_missingPrivateKeyFile©  s       ÿz<DefaultOpenSSLContextFactoryTests.test_missingPrivateKeyFile)r1   r2   r3   r4   r   r   r   r€   rÏ   rÓ   rÕ   rÖ   r   r   r   r   rÌ   �  s   
rÌ   c                   @   s4   e Zd ZdZe ed¡dkr dZdd„ Zdd„ Z	dS )ÚClientContextFactoryTestsz0
    Tests for L{ssl.ClientContextFactory}.
    Nro   c                 C   s"   t  ¡ | _t| j_| j ¡ | _d S r   )r   r£   ru   rÃ   rÍ   r»   rÎ   r   r   r   r   rÏ   ¼  s    
zClientContextFactoryTests.setUpc                 C   sV   |   | jjtj¡ |   | jjtj@ tj¡ |  | jjtj@ ¡ |  | jjtj	@ ¡ dS )z„
        L{ssl.ClientContextFactory.getContext} returns a context which can use
        TLSv1.2 or 1.3 but nothing earlier.
        N)
r•   rÎ   rÄ   r   rÐ   rÅ   rÑ   r�   ÚOP_NO_SSLv3ÚOP_NO_TLSv1r   r   r   r   rÓ   Á  s    z%ClientContextFactoryTests.test_method)
r1   r2   r3   r4   r   r   r   r€   rÏ   rÓ   r   r   r   r   r×   ´  s
   r×   )6r4   r[   r|   Ztwisted.internetr   r   r   r   rÁ   r   Ztwisted.internet.testingr   Ztwisted.protocolsr   Ztwisted.python.filepathr	   Ztwisted.python.runtimer
   Ztwisted.test.test_tcpr   Ztwisted.trial.unittestr   ZOpenSSLr   r   r   Ztwisted.test.ssl_helpersr   r   ÚImportErrorr   Zzope.interfacer   ZLineReceiverr   r5   rª   rD   rH   r¶   rJ   rY   rb   rc   re   r=   rn   r�   r¡   r¢   r¥   rÃ   rÌ   r×   r   r   r   r   Ú<module>   sL   ".
)
7j) 3