
    \dZ                     "   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 	 ddlmamZ ddlma ddlmZmZ n# e$ r d Z e             Y nw xY wddlm Z   G d dej!                  Z" G d dej!                  Z# G d dej$                  Z% G d dej$                  Z& e ej'                   G d dej$                              Z(d Z)d Z* G d d          Z+t.           G d dt2          j,                  Z- G d  d!ee          Z. G d" d#e          Z/ G d$ d%e/          Z0 G d& d'e          Z1 G d( d)ee+          Z2 G d* d+          Z3 G d, d-e          Z4 G d. d/e          Z5dS )0z 
Tests for twisted SSL support.
    N)defer
interfacesprotocolreactor)ConnectionDone)basic)FilePath)platform)waitUntilAllDisconnected)ProperlyCloseFilesMixin)TestCase)SSLcrypto)ssl)ClientTLSContextcertPathc                      d xa ad S N)r   r        5lib/python3.11/site-packages/twisted/test/test_ssl.py_noSSLr      s     cccr   )implementerc                   :    e Zd ZdZg dZddgZd Zd Zd Z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                 6    t          j                    | _        d S r   r   Deferreddeferredselfs    r   __init__zUnintelligentProtocol.__init__7       ((r   c                 D    | j         D ]}|                     |           d S r   )pretextsendLine)r"   ls     r   connectionMadez$UnintelligentProtocol.connectionMade:   s2     	 	AMM!	 	r   c                     |dk    rk| j                             t                      | j        j                   | j        D ]}|                     |           | j                                          d S d S )N   READY)	transportstartTLSr   factoryclientposttextr'   loseConnection)r"   liner(   s      r   lineReceivedz"UnintelligentProtocol.lineReceived>   sy    8N##$4$6$68KLLL] ! !a    N))+++++	 r   c                 :    | j                             d            d S r   r    callbackr"   reasons     r   connectionLostz$UnintelligentProtocol.connectionLostE       t$$$$$r   N)
__name__
__module____qualname____doc__r&   r0   r#   r)   r3   r9   r   r   r   r   r   '   ss        	 	 LKKG02DEH) ) )  , , ,% % % % %r   r   c                   2    e Zd ZdZd	dZd Zd Zd Z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                 R    || _         || _        t          j                    | _        d S r   )doTLS
fillBufferr   r   r    )r"   rB   rC   s      r   r#   zLineCollector.__init__V   s#    
$((r   c                 6    d| j         _        g | j         _        d S )Nr   )r.   rawdatalinesr!   s    r   r)   zLineCollector.connectionMade[   s    "r   c                    | j         j                            |           |dk    r| j        r't	          d          D ]}|                     d           |                     d           | j        rBt          t          t                    }| j	        
                    || j         j                   d S |                                  d S d S )Nr   i  s  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXr+   )privateKeyFileNamecertificateFileName)r.   rF   appendrC   ranger'   rB   ServerTLSContextr   r,   r-   server
setRawMode)r"   r2   xctxs       r   r3   zLineCollector.lineReceived_   s    !!$'''; /s / /AMM+....MM(###z "&'/(0   ''T\-@AAAAA!!!!! r   c                 b    | j         xj        |z  c_        | j                                         d S r   )r.   rE   r,   r1   r"   datas     r   rawDataReceivedzLineCollector.rawDataReceivedo   s2    $%%'''''r   c                 :    | j                             d            d S r   r5   r7   s     r   r9   zLineCollector.connectionLosts   r:   r   NF)	r;   r<   r=   r>   r#   r)   r3   rT   r9   r   r   r   r@   r@   I   sn        
 
) ) ) )
     " " " ( ( (% % % % %r   r@   c                       e Zd ZdZd ZdS )SingleLineServerProtocolzK
    A protocol that sends a single line of data at C{connectionMade}.
    c                 l    | j                             d           | j                                          d S )N   +OK <some crap>
)r,   writegetPeerCertificater!   s    r   r)   z'SingleLineServerProtocol.connectionMade|   s3    3444))+++++r   N)r;   r<   r=   r>   r)   r   r   r   rX   rX   w   s-         , , , , ,r   rX   c                   $    e Zd ZdZd Zd Zd ZdS )RecordingClientProtocolzv
    @ivar deferred: a deferred that will fire with first received content.
    @type deferred: L{defer.Deferred}
    c                 6    t          j                    | _        d S r   r   r!   s    r   r#   z RecordingClientProtocol.__init__   r$   r   c                 8    | j                                          d S r   )r,   r\   r!   s    r   r)   z&RecordingClientProtocol.connectionMade   s    ))+++++r   c                 :    | j                             |           d S r   r5   rR   s     r   dataReceivedz$RecordingClientProtocol.dataReceived   r:   r   N)r;   r<   r=   r>   r#   r)   rb   r   r   r   r^   r^      sK         
) ) ), , ,% % % % %r   r^   c                       e Zd ZdZd Z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                 8    | j                                          d S r   r,   r1   r!   s    r   handshakeCompletedz3ImmediatelyDisconnectingProtocol.handshakeCompleted   s    %%'''''r   c                 D    | j         j                            d            d S r   )r.   connectionDisconnectedr6   r7   s     r   r9   z/ImmediatelyDisconnectingProtocol.connectionLost   s!    +44T:::::r   N)r;   r<   r=   r>   rg   r9   r   r   r   rd   rd      s<         
( ( (; ; ; ; ;r   rd   c                    t          j                    }|                    t           j        d           t          j                    }|                                }| |_        ||_        |                    |           |	                    |d           t          j
                    }|                    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   PKeygenerate_keyTYPE_RSAX509Reqget_subjectOOU
set_pubkeysignX509set_serial_numbergmtime_adj_notBeforegmtime_adj_notAfter
set_issuerset_subject
get_pubkey)organizationorganizationalUnitpkeyreqsubjectcerts         r   generateCertificateObjectsr      s6    ;==Dfot,,,
.

CooGGI#GJNN4HHT5 ;==D1a   R   OOCOO%%&&&S__&&'''OOCNN$$%%%IIdEd?r   c                 h   t          ||          \  }}}d|t          j        fd|t          j        fd|t          j        ffD ]p\  }}}t
          j                            | |f                              d          }	t          |	          
                     |t          j        |                     qdS )z
    Create certificate files key, req and cert prefixed by C{basename} for
    given C{organization} and C{organizationalUnit}.
    keyr   r   zutf-8N)r   r   dump_privatekeydump_certificate_requestdump_certificateosextsepjoinencoder	   
setContentFILETYPE_PEM)
basenamer~   r   r   r   r   extobjdumpFuncfNames
             r   generateCertificateFilesr      s    
 1?QRROD#t 
f,-	V45	v./ G GS(
 	#//66w??""88F,?#E#EFFFFG Gr   c                       e Zd ZdZd Z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                     |                                  }t          |||           t          j        t          j                            |df          t          j                            |df          g|R i |}||fS )Nr   r   )mktempr   r   DefaultOpenSSLContextFactoryr   r   r   )r"   orgorgUnitargskwArgsbaseserverCtxFactorys          r   makeContextFactoryz)ContextGeneratingMixin.makeContextFactory   s    {{}} sG444;INND%=))INND&>**
 
 
 
 	
 
 %%%r   c                 n     | j         |i |\  | _        | _         | j         |i |\  | _        | _        d S r   )r   
clientBaseclientCtxFactory
serverBaser   )r"   
clientArgsclientKwArgs
serverArgsserverKwArgss        r   setupServerAndClientz+ContextGeneratingMixin.setupServerAndClient   s]    1H1H2
'2
 2
.. 2I1H2
'2
 2
....r   N)r;   r<   r=   r>   r   r   r   r   r   r   r      s<         &
& 
& 
&
 
 
 
 
r   r   c                       e Zd ZdZdZd ZdS )rL   zf
        A context factory with a default method set to
        L{OpenSSL.SSL.SSLv23_METHOD}.
        Fc                 X    t           j        |d<   t          j        j        | g|R i | d S )N	sslmethod)r   SSLv23_METHODr   r   r#   )r"   r   kws      r   r#   zServerTLSContext.__init__   s8    !/B{O,5dHTHHHRHHHHHr   N)r;   r<   r=   r>   isClientr#   r   r   r   rL   rL      s9        	 	
 	I 	I 	I 	I 	Ir   rL   c                   P    e Zd ZdZ ej        ed          dZd Zd Z	d Z
d ZdS )StolenTCPTestszc
    For SSL transports, test many of the same things which are tested for
    TCP transports.
    N2Reactor does not support SSL, cannot run SSL testsc                     t           j                            t          t                                                              }|                                }t          j        ||||          S )zY
        Create an SSL server with a certificate using L{IReactorSSL.listenSSL}.
        	interface)	r   PrivateCertificateloadPEMr	   r   
getContentoptionsr   	listenSSL)r"   address
portNumberr.   r   contextFactorys         r   createServerzStolenTCPTests.createServer  sV     %--hx.@.@.K.K.M.MNN WnPWXXXXr   c                 V    t          j                    }|                    |||          S )zG
        Create an SSL client using L{IReactorSSL.connectSSL}.
        )r   CertificateOptions
connectSSL)r"   r   r   clientCreatorr   s        r   connectClientzStolenTCPTests.connectClient  s*     /11''^LLLr   c                     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!   s    r   getHandleExceptionTypez%StolenTCPTests.getHandleExceptionType  s     yr   c                 .   t          j        t          j        t          j        d          t          j        t          j        d          t          j        d          t          j        d                    t          j        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 routines	SSL_writessl_write_internal zprotocol is shutdown)hamcrestcontainsequal_toany_ofr!   s    r   getHandleErrorCodeMatcherz(StolenTCPTests.getHandleErrorCodeMatcher#  s      !.11%k22%&:;;%b)) 
 !"899 

 

 
	
r   )r;   r<   r=   r>   r   IReactorSSLr   skipr   r   r   r   r   r   r   r   r     s|         
 zgt,,4CY Y YM M M  
 
 
 
 
r   r   c                   d    e Zd ZdZ ej        ed          dZdZdZ	dZ
d Zd
dZd Zd Zd	 ZdS )TLSTestsz
    Tests for startTLS support.

    @ivar fillBuffer: forwarded to L{LineCollector.fillBuffer}
    @type fillBuffer: C{bool}
    Nr   Fc                     | j         j        | j         j                                         | j        j         | j        j                                         d S d S r   )clientProtor,   r1   serverProtor!   s    r   tearDownzTLSTests.tearDownJ  sX    %1&55777%1&5577777 21r   c                    | _         t          j                    x}| _        fd|_        |rd|_        nd|_        | _        t          j                    x}| _        fd|_        |rd|_        nd|_        t          j
        d|d          }|                     |j                   t          j        d|                                j        |           t!          j        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   s   r   <lambda>z#TLSTests._runTest.<locals>.<lambda>`      k r   FTc                       S r   r   r   s   r   r   z#TLSTests._runTest.<locals>.<lambda>h  r   r   r   	127.0.0.1r   )r   r   ClientFactoryclientFactoryrM   r/   r   ServerFactoryserverFactoryr   	listenTCP
addCleanupstopListening
connectTCPgetHostportr   gatherResultsr    )r"   r   r   clientIsServercfsfr   s    ``    r   _runTestzTLSTests._runTestP  s     '"*"8":"::T)))) 	BIIBI&"*"8":"::T)))) 	BIIBI B+>>>*+++;(;R@@@"K$8+:N#OPPPr   c                       fd}                      t                      t          d j                            }|                    |          S )z~
        Test for server and client startTLS: client should received data both
        before and after the startTLS.
        c                 v                         j        j        t          j        t          j        z              d S r   )assertEqualr   rF   r   r&   r0   )ignorer"   s    r   checkz TLSTests.test_TLS.<locals>.check{  =    "(%-0E0NN    r   Tr   r   r@   rC   addCallbackr"   r   ds   `  r   test_TLSzTLSTests.test_TLSu  sV    	 	 	 	 	 MM/11=t3W3WXX}}U###r   c                       f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                                          j        j        t          j                                       j        j        d           d S )NzNo encrypted bytes received)r   r   rF   r   r&   
assertTruerE   ignoredr"   s    r   r   z"TLSTests.test_unTLS.<locals>.check  sD    T/57L7TUUUOOD.68UVVVVVr   Fr   r   s   `  r   
test_unTLSzTLSTests.test_unTLS  sa    	W 	W 	W 	W 	W MM!##]5$/%J%J
 
 }}U###r   c                       fd}                      t          d j                  t                      d          }|                    |          S )z:
        Test startTLS first initiated by client.
        c                 v                         j        j        t          j        t          j        z              d S r   )r   r   rF   r   r&   r0   r   s    r   r   z)TLSTests.test_backwardsTLS.<locals>.check  r   r   T)r   r@   rC   r   r   r   s   `  r   test_backwardsTLSzTLSTests.test_backwardsTLS  s_    
	 	 	 	 	 MM$002G2I2I4
 
 }}U###r   rV   )r;   r<   r=   r>   r   r   r   r   rC   r   r   r   r   r   r   r  r   r   r   r   r   :  s          zgt,,4CJKK8 8 8#Q #Q #Q #QJ$ $ $$ $ $$ $ $ $ $r   r   c                   <    e Zd ZdZ ej        ed          dZdZdS )SpammyTLSTestszA
    Test TLS features with bytes sitting in the out buffer.
    Nr   T)	r;   r<   r=   r>   r   r   r   r   rC   r   r   r   r  r    s9          zgt,,4CJJJr   r  c                   H    e Zd Z ej        ed          dZdZdZd Z	d Z
dS )BufferingTestsNr   c                     | j         j        | j         j                                         | j        j        | j        j                                         t	          t
          | j         | j        g          S r   )r   r,   r1   r   r   r   r!   s    r   r   zBufferingTests.tearDown  se    %1&55777%1&55777'$2BDDT1UVVVr   c                 t   t                      x| _        t                      x| _        t	          j                    }t	          j                    x}| _        fd|_        fd|_        t          j	        t          t                    }t          j                    }t          j        d||d          }|                     |j                   t          j        d|                                j        ||          }|                     |j                   j                            | j        d          S )Nc                       S r   r   r   s   r   r   z6BufferingTests.test_openSSLBuffering.<locals>.<lambda>      + r   c                       S r   r   r   s   r   r   z6BufferingTests.test_openSSLBuffering.<locals>.<lambda>  r  r   r   r   r   rZ   )rX   r   r^   r   r   r   r   r/   r   r   r   ClientContextFactoryr   r   r   r   r   r   r   
disconnectr    r   r   )	r"   rM   r/   sCTXcCTXr   clientConnectorr   r   s	          @@r   test_openSSLBufferingz$BufferingTests.test_openSSLBuffering  s   )A)C)CCd&)@)B)BBd&'))'5777--------/(CC')) FDKHHH*+++!,,fd
 
 	2333#//4
 
 	
r   )r;   r<   r=   r   r   r   r   r   r   r   r  r   r   r   r  r    sY        zgt,,4CKKW W W
 
 
 
 
r   r  c                   P    e Zd ZdZ ej        ed          dZd Zd Z	d Z
d ZdS )ConnectionLostTestsz'
    SSL connection closing tests.
    Nr   c                     d}                      ||dz   fi ||dz   fi            t          j                    }t          j        |_        t	          j        d| j                  x _        }t          j                    }t          |_        t          j                    |_        t	          j        d|                                j        | j                   |j                             fd          S )Ntwisted.test.test_ssl, client, serverr   r   c                 6    j                                         S r   )
serverPortr   )ignoredResultr"   s    r   r   z=ConnectionLostTests.testImmediateDisconnect.<locals>.<lambda>  s    $/"?"?"A"A r   )r   r   r   Protocolr   r   r   r  r   rd   r   r   ri   r   r   r   r   r   )r"   r   serverProtocolFactoryr  clientProtocolFactorys   `    r   testImmediateDisconnectz+ConnectionLostTests.testImmediateDisconnect  s   %!!#
"#R#sZ/?)@"	
 	
 	
 !) 6 8 8)1):&'.'8$d&;(
 (
 	
* !) 6 8 8)I&7<~7G7G4  %!!		
 	
 	
 %;GGAAAA
 
 	
r   c                    t          t          j                   G d dt          j                              }d}|                     ||dz   fi ||dz   fi             |            t          j                    }fd|_        t          j        d|| j	                  }| 
                    |j                    |            t          j                    }fd|_        t          j        d	|                                j        || j                   d
 }t#          j        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                   $    e Zd ZdZd Zd Zd ZdS )MConnectionLostTests.test_bothSidesLoseConnection.<locals>.CloseAfterHandshakeFc                 6    t          j                    | _        d S r   )r   r   doner!   s    r   r#   zVConnectionLostTests.test_bothSidesLoseConnection.<locals>.CloseAfterHandshake.__init__  s    !N,,			r   c                 8    | j                                          d S r   rf   r!   s    r   rg   z`ConnectionLostTests.test_bothSidesLoseConnection.<locals>.CloseAfterHandshake.handshakeCompleted  s    --/////r   c                 >    | j                             |           | ` d S r   )r$  errbackr7   s     r   r9   z\ConnectionLostTests.test_bothSidesLoseConnection.<locals>.CloseAfterHandshake.connectionLost  s!    	!!&)))IIIr   N)r;   r<   r=   gotDatar#   rg   r9   r   r   r   CloseAfterHandshaker"    sF        G- - -0 0 0    r   r)  r  r  r  c                       S r   r   serverProtocols   r   r   zBConnectionLostTests.test_bothSidesLoseConnection.<locals>.<lambda>       r   r   c                       S r   r   clientProtocols   r   r   zBConnectionLostTests.test_bothSidesLoseConnection.<locals>.<lambda>  r-  r   r   c                 :    |                      t                     d S r   )trapr   )failures    r   checkResultzEConnectionLostTests.test_bothSidesLoseConnection.<locals>.checkResult'  s    LL(((((r   )r   r   IHandshakeListenerr   r  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r$  
addErrback)	r"   r)  r   r  r  r  r4  r0  r,  s	          @@r   test_bothSidesLoseConnectionz0ConnectionLostTests.test_bothSidesLoseConnection  s    
Z2	3	3	 	 	 	 	("3 	 	 
4	3	 &!!#
"#R#sZ/?)@"	
 	
 	
 -,.. ( 6 8 8)?)?)?)?&&q*?AVWW

0111,,.. ( 6 8 8)?)?)?)?&  %!!		
 	
 	
	) 	) 	) "#..{;;#..{;;
 
 	
r   c                   	
 d}|                      ||dz   fi ||dz   fi            d }| j                                                            t          j        |           t          j                    }t          j	                    
|j
        
_        t          j                    }
fd|_        t          j        d|| j                  x| _        }t          j                    }t          j	                    	|j
        	_        t          j                    }	fd|_        t          j        d|                                j        || j                   t          j        ||gd	
          }|                    | j                  S )Nr  r  r  c                      dS )NFr   )as    r   verifyz4ConnectionLostTests.testFailedVerify.<locals>.verify7  s    5r   c                       S r   r   r+  s   r   r   z6ConnectionLostTests.testFailedVerify.<locals>.<lambda>@  r-  r   r   c                       S r   r   r/  s   r   r   z6ConnectionLostTests.testFailedVerify.<locals>.<lambda>I  r-  r   r   T)consumeErrors)r   r   
getContext
set_verifyr   VERIFY_PEERr   r   r   r  r6   r9   r   r   r   r   r  r   r   r   r   DeferredListr   _cbLostConns)r"   r   r;  serverConnLostr  r  clientConnLostr  dlr0  r,  s            @@r   testFailedVerifyz$ConnectionLostTests.testFailedVerify1  s   %!!#
"#R#sZ/?)@"	
 	
 	
	 	 	 	((**55covNNN))!*,,(6(?% ( 6 8 8)?)?)?)?&'.'8$d&;(
 (
 	
* ))!*,,(6(?% ( 6 8 8)?)?)?)?&  %!!		
 	
 	
  @PTUUU~~d/000r   c                 <   |\  \  }}\  }}|                      |           |                      |           t          j        g}t          j                    rddlm} |                    |            |j        |   |j        |  | j	        
                                S )Nr   )ConnectionLost)assertFalser   r   r
   	isWindowstwisted.internet.errorrI  rJ   r2  r  r   )r"   resultssSuccesssResultcSuccesscResultacceptableErrorsrI  s           r   rC  z ConnectionLostTests._cbLostConnsT  s    3:070h""""""I;  	4======##N333&''&'',,...r   )r;   r<   r=   r>   r   r   r   r   r  r7  rG  rC  r   r   r   r  r    sx          zgt,,4C
 
 
82
 2
 2
h!1 !1 !1F/ / / / /r   r  c                   *    e Zd ZdZd Zd Zd Zd ZdS )FakeContextzK
    L{OpenSSL.SSL.Context} double which can more easily be inspected.
    c                 "    || _         d| _        d S )Nr   )_method_options)r"   methods     r   r#   zFakeContext.__init__t  s    r   c                 &    | xj         |z  c_         d S r   )rW  )r"   r   s     r   set_optionszFakeContext.set_optionsx  s     r   c                     d S r   r   r"   fileNames     r   use_certificate_filez FakeContext.use_certificate_file{      r   c                     d S r   r   r\  s     r   use_privatekey_filezFakeContext.use_privatekey_file~  r_  r   N)r;   r<   r=   r>   r#   rZ  r^  ra  r   r   r   rT  rT  o  sZ           ! ! !      r   rT  c                   P    e Zd ZdZ ej        ed          dZd Zd Z	d Z
d ZdS )!DefaultOpenSSLContextFactoryTestsz8
    Tests for L{ssl.DefaultOpenSSLContextFactory}.
    Nr   c                     t          j        t          t          t                    | _        | j                                        | _        d S )N)_contextFactory)r   r   r   rT  r   r?  contextr!   s    r   setUpz'DefaultOpenSSLContextFactoryTests.setUp  sA     ">h
 
 
 *5577r   c                     |                      | j        j        t          j                   |                      | j        j        t          j        z  t          j                   |                     | j        j        t          j        z             dS )z
        L{ssl.DefaultOpenSSLContextFactory.getContext} returns an SSL context
        which can use SSLv3 or TLSv1 but not SSLv2.
        N)	r   rf  rV  r   
TLS_METHODrW  OP_NO_SSLv2rJ  OP_NO_TLSv1_2r!   s    r   test_methodz-DefaultOpenSSLContextFactoryTests.test_method  sp     	-s~>>> 	.@#/RRR 	.1BBCCCCCr   c                     |                      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   r   r   r   r!   s    r   test_missingCertificateFilez=DefaultOpenSSLContextFactoryTests.test_missingCertificateFile  s<     	Is74;;==	
 	
 	
 	
 	
r   c                     |                      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)rn  r   r   r   r   r   r   r!   s    r   test_missingPrivateKeyFilez<DefaultOpenSSLContextFactoryTests.test_missingPrivateKeyFile  s<     	Is7	
 	
 	
 	
 	
r   )r;   r<   r=   r>   r   r   r   r   rg  rl  ro  rq  r   r   r   rc  rc    sy          zgt,,4C8 8 8D D D
 
 

 
 
 
 
r   rc  c                   D    e Zd ZdZ ej        ed          dZd Zd Z	dS )ClientContextFactoryTestsz0
    Tests for L{ssl.ClientContextFactory}.
    Nr   c                     t          j                    | _        t          | j        _        | j                                        | _        d S r   )r   r  r   rT  re  r?  rf  r!   s    r   rg  zClientContextFactoryTests.setUp  s8    !688.9+*5577r   c                 x   |                      | j        j        t          j                   |                      | j        j        t          j        z  t          j                   |                     | j        j        t          j        z             |                     | j        j        t          j	        z             dS )z
        L{ssl.ClientContextFactory.getContext} returns a context which can use
        TLSv1.2 or 1.3 but nothing earlier.
        N)
r   rf  rV  r   ri  rW  rj  r   OP_NO_SSLv3OP_NO_TLSv1r!   s    r   rl  z%ClientContextFactoryTests.test_method  s    
 	-s~>>>.@#/RRR-?@@@-?@@@@@r   )
r;   r<   r=   r>   r   r   r   r   rg  rl  r   r   r   rs  rs    s]          zgt,,4C8 8 8
A A A A Ar   rs  )6r>   r   r   twisted.internetr   r   r   r   rL  r   twisted.protocolsr   twisted.python.filepathr	   twisted.python.runtimer
   twisted.test.proto_helpersr   twisted.test.test_tcpr   twisted.trial.unittestr   OpenSSLr   r   r   twisted.test.ssl_helpersr   r   ImportErrorr   zope.interfacer   LineReceiverr   r@   r  rX   r^   r5  rd   r   r   r   r   rL   r   r   r  r  r  rT  rc  rs  r   r   r   <module>r     s^    
			  A A A A A A A A A A A A 1 1 1 1 1 1 # # # # # # , , , , , , + + + + + + ? ? ? ? ? ? 9 9 9 9 9 9 + + + + + +########$$$$$$CCCCCCCCC      FHHHHH ' & & & & &% % % % %E. % % %D+% +% +% +% +%E& +% +% +%\, , , , ,x0 , , ,% % % % %h/ % % %  Z*++
; 
; 
; 
; 
;x'8 
; 
; ,+
;  8G G G &
 &
 &
 &
 &
 &
 &
 &
R ?
I 
I 
I 
I 
I3; 
I 
I 
I4
 4
 4
 4
 4
,h 4
 4
 4
ng$ g$ g$ g$ g$x g$ g$ g$T    X   '
 '
 '
 '
 '
X '
 '
 '
TS/ S/ S/ S/ S/($: S/ S/ S/l       &0
 0
 0
 0
 0
 0
 0
 0
fA A A A A A A A A As   A A.-A.