o
    Óó·iè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 zddlmamZ ddlma ddlmZmZ W n eyh   dd„ Zeƒ  Y nw 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u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   ú[C:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/test/test_ssl.pyÚ_noSSL   s   r   )Úimplementerc                   @   s@   e Zd ZdZg d¢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   ó   t  ¡ | _d S r   ©r   ÚDeferredÚdeferred©Úselfr   r   r   Ú__init__7   ó   zUnintelligentProtocol.__init__c                 C   s   | j D ]}|  |¡ qd S r   )ÚpretextÚsendLine)r   Úlr   r   r   ÚconnectionMade:   s   
ÿz$UnintelligentProtocol.connectionMadec                 C   sD   |dkr | j  tƒ | jj¡ | jD ]}|  |¡ q| j  ¡  d S d S )Nó   READY)Ú	transportÚstartTLSr   ÚfactoryÚclientÚposttextr"   ÚloseConnection)r   Úliner#   r   r   r   ÚlineReceived>   s   
üz"UnintelligentProtocol.lineReceivedc                 C   ó   | j  d ¡ d S r   ©r   Úcallback©r   Úreasonr   r   r   ÚconnectionLostE   ó   z$UnintelligentProtocol.connectionLostN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r!   r*   r   r$   r-   r3   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   r:   r;   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   sv   | j j |¡ |dkr9| jrtdƒD ]}|  d¡ q|  d¡ | jr3tttd�}| j	 
|| j j¡ d S |  ¡  d S d S )Nr   iô  sè  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXr%   )ZprivateKeyFileNameZcertificateFileName)r(   r>   Úappendr;   Úranger"   r:   Ú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(   r=   r&   r+   ©r   Údatar   r   r   ÚrawDataReceivedo   s   zLineCollector.rawDataReceivedc                 C   r.   r   r/   r1   r   r   r   r3   s   r4   zLineCollector.connectionLostN©F)	r5   r6   r7   r8   r   r$   r-   rG   r3   r   r   r   r   r9   I   s    
r9   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)r5   r6   r7   r8   r$   r   r   r   r   rI   w   s    rI   c                   @   ó(   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   r   r   r   r   r   r   r   r   ‡   r    z RecordingClientProtocol.__init__c                 C   ó   | j  ¡  d S r   )r&   rL   r   r   r   r   r$   Š   r    z&RecordingClientProtocol.connectionMadec                 C   s   | j  |¡ d S r   r/   rE   r   r   r   ÚdataReceived�   r4   z$RecordingClientProtocol.dataReceivedN)r5   r6   r7   r8   r   r$   rP   r   r   r   r   rN   �   s
    rN   c                   @   ó    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   rO   r   ©r&   r+   r   r   r   r   ÚhandshakeCompleted˜   r    z3ImmediatelyDisconnectingProtocol.handshakeCompletedc                 C   s   | j j d ¡ d S r   )r(   ÚconnectionDisconnectedr0   r1   r   r   r   r3   ›   ó   z/ImmediatelyDisconnectingProtocol.connectionLostN)r5   r6   r7   r8   rT   r3   r   r   r   r   rR   ‘   s    rR   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   é<   N)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"   




rb   c           
      C   sn   t ||ƒ\}}}d|tjfd|tjfd|tjffD ]\}}}tj | |f¡ d¡}	t	|	ƒ 
|tj|ƒ¡ qdS )z…
    Create certificate files key, req and cert prefixed by C{basename} for
    given C{organization} and C{organizationalUnit}.
    Úkeyr_   ra   zutf-8N)rb   r   Zdump_privatekeyZdump_certificate_requestZdump_certificateÚosÚextsepÚjoinÚencoder	   Z
setContentZFILETYPE_PEM)
Úbasenamer\   r]   r^   r_   ra   ÚextÚobjZdumpFuncZfNamer   r   r   ÚgenerateCertificateFiles»   s   


ýúrk   c                   @   rQ   )Ú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   sN   |   ¡ }t|||ƒ tjtj |df¡tj |df¡g|¢R i |¤Ž}||fS )Nrc   ra   )Úmktemprk   r   ÚDefaultOpenSSLContextFactoryrd   re   rf   )r   ÚorgZorgUnitÚargsZkwArgsÚbaseÚserverCtxFactoryr   r   r   ÚmakeContextFactoryß   s   þýüz)ContextGeneratingMixin.makeContextFactoryc                 C   s4   | j |i |¤Ž\| _| _| j |i |¤Ž\| _| _d S r   )rs   Z
clientBaseÚclientCtxFactoryZ
serverBaserr   )r   Z
clientArgsZclientKwArgsZ
serverArgsZserverKwArgsr   r   r   ÚsetupServerAndClientë   s   ÿÿÿÿz+ContextGeneratingMixin.setupServerAndClientN)r5   r6   r7   r8   rs   ru   r   r   r   r   rl   Ë   s    rl   c                   @   s   e Zd ZdZdZdd„ ZdS )rA   zf
        A context factory with a default method set to
        L{OpenSSL.SSL.SSLv23_METHOD}.
        Fc                 O   s(   t j|d< tjj| g|¢R i |¤Ž d S )NZ	sslmethod)r   ZSSLv23_METHODr   rn   r   )r   rp   Úkwr   r   r   r   þ   s   
zServerTLSContext.__init__N)r5   r6   r7   r8   ZisClientr   r   r   r   r   rA   ö   s    rA   c                   @   óD   e Zd ZdZe ed¡du 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	interfaceN)	r   ZPrivateCertificateZloadPEMr	   r   Z
getContentÚoptionsr   Ú	listenSSL)r   ÚaddressÚ
portNumberr(   ra   ÚcontextFactoryr   r   r   ÚcreateServer  s   zStolenTCPTests.createServerc                 C   s   t  ¡ }| |||¡S )zG
        Create an SSL client using L{IReactorSSL.connectSSL}.
        N)r   ZCertificateOptionsÚ
connectSSL)r   r}   r~   ZclientCreatorr   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.
        N)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 shutdownN)ÚhamcrestÚcontainsZequal_toZany_ofr   r   r   r   ÚgetHandleErrorCodeMatcher#  s   
ýùÿz(StolenTCPTests.getHandleErrorCodeMatcher)r5   r6   r7   r8   r   ÚIReactorSSLr   Úskipr€   r‚   r„   rˆ   r   r   r   r   rx     s    rx   c                   @   sZ   e Zd ZdZe ed¡du 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}
    Nry   Fc                 C   s8   | j jd ur| j j ¡  | jjd ur| jj ¡  d S 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„|_|r1d|_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 r   r   r   ©rŒ   r   r   Ú<lambda>`  ó    z#TLSTests._runTest.<locals>.<lambda>FTc                      r�   r   r   r   ©r�   r   r   r‘   h  r’   r   ú	127.0.0.1rz   N)rŒ   r   ÚClientFactoryÚclientFactoryrB   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                    ó,   ‡ 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                    ó   ˆ   ˆ jjtjtj ¡ d S r   )ÚassertEqualr˜   r>   r   r!   r*   )Úignorer   r   r   Úcheck{  ó   
þz TLSTests.test_TLS.<locals>.checkTN©r    r   r9   r;   ÚaddCallback©r   r¥   Údr   r   r   Útest_TLSu  s   
zTLSTests.test_TLSc                    r¡   )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!   Ú
assertTruer=   ©Zignoredr   r   r   r¥   Š  s   z"TLSTests.test_unTLS.<locals>.checkFNr§   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                    r¢   r   )r£   r–   r>   r   r!   r*   r­   r   r   r   r¥   ˜  r¦   z)TLSTests.test_backwardsTLS.<locals>.checkTN)r    r9   r;   r   r¨   r©   r   r   r   Útest_backwardsTLS“  s
   ÿ
zTLSTests.test_backwardsTLSrH   )r5   r6   r7   r8   r   r‰   r   rŠ   r;   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u rdZdZdS )ÚSpammyTLSTestszA
    Test TLS features with bytes sitting in the out buffer.
    Nry   T)	r5   r6   r7   r8   r   r‰   r   rŠ   r;   r   r   r   r   r°   ¤  s
    r°   c                   @   s8   e Zd Ze ed¡du rdZdZdZdd„ Z	dd„ Z
dS )ÚBufferingTestsNry   c                 C   sB   | j jd ur| j j ¡  | jjd ur| 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                      r�   r   r   r   r“   r   r   r‘   Å  r’   z6BufferingTests.test_openSSLBuffering.<locals>.<lambda>c                      r�   r   r   r   r�   r   r   r‘   Æ  r’   r   r”   rz   rJ   )rI   r�   rN   rŒ   r   r—   r•   r)   r   rn   r   ÚClientContextFactoryr   r|   r™   rš   r�   r›   rœ   Z
disconnectr   r¨   r£   )r   rB   r)   ZsCTXZcCTXrœ   ZclientConnectorr   rŸ   r   Útest_openSSLBuffering¾  s"   ÿÿz$BufferingTests.test_openSSLBuffering)r5   r6   r7   r   r‰   r   rŠ   r�   rŒ   rŽ   r³   r   r   r   r   r±   ¯  s    r±   c                   @   rw   )ÚConnectionLostTestsz'
    SSL connection closing tests.
    Nry   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‘   ù  s   
 z=ConnectionLostTests.testImmediateDisconnect.<locals>.<lambda>)ru   r   r—   ÚProtocolr   r|   rr   r¸   r•   rR   r   r   rU   r�   r›   rœ   rt   r¨   )r   ro   ÚserverProtocolFactoryr¸   ÚclientProtocolFactoryr   r   r   ÚtestImmediateDisconnectà  s*   ÿ
ÿ
ü
ÿ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                   @   rM   )	zMConnectionLostTests.test_bothSidesLoseConnection.<locals>.CloseAfterHandshakeFc                 S   r   r   )r   r   Údoner   r   r   r   r     r    zVConnectionLostTests.test_bothSidesLoseConnection.<locals>.CloseAfterHandshake.__init__c                 S   rO   r   rS   r   r   r   r   rT   
  r    z`ConnectionLostTests.test_bothSidesLoseConnection.<locals>.CloseAfterHandshake.handshakeCompletedc                 S   s   | j  |¡ | ` d S r   )r½   Zerrbackr1   r   r   r   r3     s   z\ConnectionLostTests.test_bothSidesLoseConnection.<locals>.CloseAfterHandshake.connectionLostN)r5   r6   r7   ZgotDatar   rT   r3   r   r   r   r   ÚCloseAfterHandshake  s
    r¾   rµ   r¶   r·   c                      r�   r   r   r   ©ÚserverProtocolr   r   r‘     r’   zBConnectionLostTests.test_bothSidesLoseConnection.<locals>.<lambda>r   c                      r�   r   r   r   ©ÚclientProtocolr   r   r‘     r’   r”   c                 S   s   |   t¡ d S r   )Útrapr   )Zfailurer   r   r   ÚcheckResult&  r    zEConnectionLostTests.test_bothSidesLoseConnection.<locals>.checkResultN)r   r   ÚIHandshakeListenerr   r¹   ru   r—   r   r|   rr   r™   rš   r•   r�   r›   rœ   rt   r   r�   r½   Z
addErrback)r   r¾   ro   rº   r¸   r»   rÄ   r   ©rÂ   rÀ   r   Útest_bothSidesLoseConnectionü  s4   ÿü

þÿ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  ó   z4ConnectionLostTests.testFailedVerify.<locals>.verifyc                      r�   r   r   r   r¿   r   r   r‘   ?  r’   z6ConnectionLostTests.testFailedVerify.<locals>.<lambda>r   c                      r�   r   r   r   rÁ   r   r   r‘   H  r’   r”   T)ZconsumeErrors)ru   rt   Ú
getContextZ
set_verifyr   ZVERIFY_PEERr   r   r   r¹   r0   r3   r—   r   r|   rr   r¸   r•   r�   r›   rœ   ZDeferredListr¨   Ú_cbLostConns)	r   ro   rÉ   ZserverConnLostrº   r¸   ZclientConnLostr»   Údlr   rÆ   r   ÚtestFailedVerify0  s6   ÿ
ÿüz$ConnectionLostTests.testFailedVerifyc                 C   sh   |\\}}\}}|   |¡ |   |¡ tjg}t ¡ r%ddlm} | |¡ |j|Ž  |j|Ž  | j	 
¡ S )Nr   )ÚConnectionLost)ÚassertFalser   rƒ   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)r5   r6   r7   r8   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   r{   r   r   r   Úset_optionsw  rV   zFakeContext.set_optionsc                 C   ó   d S r   r   ©r   ÚfileNamer   r   r   Úuse_certificate_filez  rÊ   z FakeContext.use_certificate_filec                 C   rØ   r   r   rÙ   r   r   r   Úuse_privatekey_file}  rÊ   zFakeContext.use_privatekey_fileN)r5   r6   r7   r8   r   r×   rÛ   rÜ   r   r   r   r   rÓ   n  s    rÓ   c                   @   rw   )Ú!DefaultOpenSSLContextFactoryTestsz8
    Tests for L{ssl.DefaultOpenSSLContextFactory}.
    Nry   c                 C   s"   t jtttd�| _| j ¡ | _d S )N)Ú_contextFactory)r   rn   r   rÓ   r   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   rƒ   r   rn   r   rm   r   r   r   r   Útest_missingCertificateFileŸ  ó   ÿ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   rƒ   r   rn   rm   r   r   r   r   r   Útest_missingPrivateKeyFile©  rç   z<DefaultOpenSSLContextFactoryTests.test_missingPrivateKeyFile)r5   r6   r7   r8   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u rdZdd„ Zdd„ Z	dS )ÚClientContextFactoryTestsz0
    Tests for L{ssl.ClientContextFactory}.
    Nry   c                 C   s"   t  ¡ | _t| j_| j ¡ | _d S r   )r   r²   r   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)
r5   r6   r7   r8   r   r‰   r   rŠ   rà   rä   r   r   r   r   ré   ´  s    ré   )6r8   rd   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   r9   r¹   rI   rN   rÅ   rR   rb   rk   rl   rn   rA   rx   r‹   r°   r±   r´   rÓ   rÝ   ré   r   r   r   r   Ú<module>   sP   
ø
".
)7j) 3