
    hZ                        S r SSKrSSKrSSKJrJrJrJr  SSKJ	r	  SSK
Jr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr   SSKJqJr  SSKJq  SSKJrJr  SSKJ r    " S S\RB                  5      r" " S S\RB                  5      r# " S S\RH                  5      r% " S S\RH                  5      r&\ " \RN                  5       " S S\RH                  5      5       r(S r)S r* " S S5      r+[.        b   " S S[2        RX                  5      r- " S  S!\\5      r. " S" S#\5      r/ " S$ S%\/5      r0 " S& S'\5      r1 " S( S)\\+5      r2 " S* S+5      r3 " S, S-\5      r4 " S. S/\5      r5g! \ a    S r\" 5          GN!f = f)0z 
Tests for twisted SSL support.
    N)defer
interfacesprotocolreactor)ConnectionDone)waitUntilAllDisconnected)basic)FilePath)platform)ProperlyCloseFilesMixin)TestCase)SSLcrypto)ssl)ClientTLSContextcertPathc                      S =q qg N)r   r        \/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/test/test_ssl.py_noSSLr      s     cr   )implementerc                   @    \ rS rSrSr/ SQrSS/rS rS rS r	S	 r
S
rg)UnintelligentProtocol'   z
@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                 8    [         R                  " 5       U l        g r   r   Deferreddeferredselfs    r   __init__UnintelligentProtocol.__init__7       (r   c                 L    U R                    H  nU R                  U5        M     g r   )pretextsendLine)r#   ls     r   connectionMade$UnintelligentProtocol.connectionMade:   s    AMM! r   c                     US:X  aw  U R                   R                  [        5       U R                  R                  5        U R
                   H  nU R                  U5        M     U R                   R                  5         g g )N   READY)	transportstartTLSr   factoryclientposttextr)   loseConnection)r#   liner*   s      r   lineReceived"UnintelligentProtocol.lineReceived>   s[    8NN##$4$68K8KL]]a  #NN))+	 r   c                 :    U R                   R                  S 5        g r   r!   callbackr#   reasons     r   connectionLost$UnintelligentProtocol.connectionLostE       t$r   r!   N)__name__
__module____qualname____firstlineno____doc__r(   r3   r$   r+   r6   r=   __static_attributes__r   r   r   r   r   '   s.    	 LG02DEH),%r   r   c                   :    \ rS rSrSrS
S jrS rS rS rS r	Sr
g	)LineCollectorI   a*  
@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}
c                 P    Xl         X l        [        R                  " 5       U l        g r   )doTLS
fillBufferr   r    r!   )r#   rK   rL   s      r   r$   LineCollector.__init__V   s    
$(r   c                 H    SU R                   l        / U R                   l        g )Nr   )r1   rawdatalinesr"   s    r   r+   LineCollector.connectionMade[   s    "r   c                    U R                   R                  R                  U5        US:X  a  U R                  (       a#  [	        S5       H  nU R                  S5        M     U R                  S5        U R                  (       aB  [        [        [        S9nU R                  R                  X0R                   R                  5        g U R                  5         g g )Nr   i  s  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXr.   )privateKeyFileNamecertificateFileName)r1   rP   appendrL   ranger)   rK   ServerTLSContextr   r/   r0   server
setRawMode)r#   r5   xctxs       r   r6   LineCollector.lineReceived_   s    !!$';sAMM+. $MM(#zz&'/(0 ''\\-@-@A! r   c                 v    U R                   =R                  U-  sl        U R                  R                  5         g r   )r1   rO   r/   r4   r#   datas     r   rawDataReceivedLineCollector.rawDataReceivedo   s'    $%%'r   c                 :    U R                   R                  S 5        g r   r9   r;   s     r   r=   LineCollector.connectionLosts   r?   r   )r!   rK   rL   NF)rA   rB   rC   rD   rE   r$   r+   r6   r`   r=   rF   r   r   r   rH   rH   I   s     
)
 " (%r   rH   c                       \ rS rSrSrS rSrg)SingleLineServerProtocolw   zC
A protocol that sends a single line of data at C{connectionMade}.
c                 n    U R                   R                  S5        U R                   R                  5         g )N   +OK <some crap>
)r/   writegetPeerCertificater"   s    r   r+   'SingleLineServerProtocol.connectionMade|   s%    34))+r   r   N)rA   rB   rC   rD   rE   r+   rF   r   r   r   rf   rf   w   s    ,r   rf   c                   *    \ rS rSrSrS rS rS rSrg)RecordingClientProtocol   zj
@ivar deferred: a deferred that will fire with first received content.
@type deferred: L{defer.Deferred}
c                 8    [         R                  " 5       U l        g r   r   r"   s    r   r$    RecordingClientProtocol.__init__   r&   r   c                 8    U R                   R                  5         g r   )r/   rk   r"   s    r   r+   &RecordingClientProtocol.connectionMade   s    ))+r   c                 :    U R                   R                  U5        g r   r9   r^   s     r   dataReceived$RecordingClientProtocol.dataReceived   r?   r   r@   N)	rA   rB   rC   rD   rE   r$   r+   ru   rF   r   r   r   rn   rn      s    
),%r   rn   c                   $    \ rS rSrSrS rS rSrg) ImmediatelyDisconnectingProtocol   z
A protocol that disconnect immediately on connection. It fires the
C{connectionDisconnected} deferred of its factory on connetion lost.
c                 8    U R                   R                  5         g r   r/   r4   r"   s    r   handshakeCompleted3ImmediatelyDisconnectingProtocol.handshakeCompleted   s    %%'r   c                 N    U R                   R                  R                  S 5        g r   )r1   connectionDisconnectedr:   r;   s     r   r=   /ImmediatelyDisconnectingProtocol.connectionLost   s    ++44T:r   r   N)rA   rB   rC   rD   rE   r|   r=   rF   r   r   r   rx   rx      s    
(;r   rx   c                    [         R                  " 5       nUR                  [         R                  S5        [         R                  " 5       nUR                  5       nXl        Xl        UR                  U5        UR                  US5        [         R                  " 5       nUR                  S5        UR                  S5        UR                  S5        UR                  UR                  5       5        UR                  UR                  5       5        UR                  UR!                  5       5        UR                  US5        X#U4$ )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      s     ;;=Dfoot,
..
CooGI#JNN4HHT5 ;;=D1a R OOCOO%&S__&'OOCNN$%IIdEd?r   c                 `   [        X5      u  p4nSU[        R                  4SU[        R                  4SU[        R                  44 Hd  u  pgn[
        R                  R                  X45      R                  S5      n	[        U	5      R                  U" [        R                  U5      5        Mf     g)zy
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RODt 
f,,-	V445	v../(
 		/66w?""8F,?,?#EFr   c                   $    \ rS rSrSrS rS rSrg)ContextGeneratingMixin   a4  
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                     U R                  5       n[        XQU5        [        R                  " [        R
                  R                  US45      [        R
                  R                  US45      /UQ70 UD6nXV4$ )Nr   r   )mktempr   r   DefaultOpenSSLContextFactoryr   r   r   )r#   orgorgUnitargskwArgsbaseserverCtxFactorys          r   makeContextFactory)ContextGeneratingMixin.makeContextFactory   so    {{} G4;;IINND%=)IINND&>*
 
 	
 %%r   c                     U R                   " U0 UD6u  U l        U l        U R                   " U0 UD6u  U l        U l        g r   )r   
clientBaseclientCtxFactory
serverBaser   )r#   
clientArgsclientKwArgs
serverArgsserverKwArgss        r   setupServerAndClient+ContextGeneratingMixin.setupServerAndClient   sN    151H1H2
'2
.. 261H1H2
'2
..r   )r   r   r   r   N)rA   rB   rC   rD   rE   r   r   rF   r   r   r   r   r      s    &
&
r   r   c                   "    \ rS rSrSrSrS rSrg)rW      zN
A context factory with a default method set to
L{OpenSSL.SSL.SSLv23_METHOD}.
Fc                 r    [         R                  US'   [        R                  R                  " U /UQ70 UD6  g )N	sslmethod)r   SSLv23_METHODr   r   r$   )r#   r   kws      r   r$   ServerTLSContext.__init__   s/    !//B{O,,55dHTHRHr   r   N)rA   rB   rC   rD   rE   isClientr$   rF   r   r   r   rW   rW      s    	
 	Ir   rW   c                   \    \ rS rSrSr\R                  " \S5      c  SrS r	S r
S rS rS	rg)
StolenTCPTestsi  zW
For SSL transports, test many of the same things which are tested for
TCP transports.
N2Reactor does not support SSL, cannot run SSL testsc                     [         R                  R                  [        [        5      R                  5       5      nUR                  5       n[        R                  " X#XQS9$ )zI
Create an SSL server with a certificate using L{IReactorSSL.listenSSL}.
	interface)	r   PrivateCertificateloadPEMr
   r   
getContentoptionsr   	listenSSL)r#   address
portNumberr1   r   contextFactorys         r   createServerStolenTCPTests.createServer  sF     %%--hx.@.K.K.MN  nXXr   c                 P    [         R                  " 5       nUR                  XU5      $ )z7
Create an SSL client using L{IReactorSSL.connectSSL}.
)r   CertificateOptions
connectSSL)r#   r   r   clientCreatorr   s        r   connectClientStolenTCPTests.connectClient  s%     //1''^LLr   c                 "    [         R                  $ )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   getHandleExceptionType%StolenTCPTests.getHandleExceptionType  s     yyr   c                 N   [         R                  " [         R                  " [         R                  " S5      [         R                  " [         R                  " S5      [         R                  " S5      [         R                  " S5      5      [         R                  " S5      5      5      $ )a  
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   getHandleErrorCodeMatcher(StolenTCPTests.getHandleErrorCodeMatcher#  s{       !!.1%%k2%%&:;%%b)
 !!"89

 
	
r   r   )rA   rB   rC   rD   rE   r   IReactorSSLr   skipr   r   r   r   rF   r   r   r   r   r     s7    
 gt,4CYM
r   r   c                   r    \ rS rSrSr\R                  " \S5      c  SrSr	Sr
SrS rSS jrS rS	 rS
 rSrg)TLSTestsi:  zs
Tests for startTLS support.

@ivar fillBuffer: forwarded to L{LineCollector.fillBuffer}
@type fillBuffer: C{bool}
Nr   Fc                     U R                   R                  b$  U R                   R                  R                  5         U R                  R                  b%  U R                  R                  R                  5         g g r   )clientProtor/   r4   serverProtor"   s    r   tearDownTLSTests.tearDownJ  s[    %%1&&557%%1&&557 2r   c                 ,  ^^ TU l         [        R                  " 5       =o@l        U4S jUl        U(       a  SUl        OSUl        TU l        [        R                  " 5       =oPl        U4S jUl        U(       a  SUl        OSUl        [        R                  " SUSS9nU R                  UR                  5        [        R                  " SUR                  5       R                  U5        [         R"                  " TR$                  TR$                  /5      $ )am  
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                     > T $ r   r   r   s   r   <lambda>#TLSTests._runTest.<locals>.<lambda>`      kr   FTc                     > T $ r   r   r   s   r   r   r   h  r   r   r   	127.0.0.1r   )r   r   ClientFactoryclientFactoryrX   r2   r   ServerFactoryserverFactoryr   	listenTCP
addCleanupstopListening
connectTCPgetHostportr   gatherResultsr!   )r#   r   r   clientIsServercfsfr  s    ``    r   _runTestTLSTests._runTestP  s     '"*"8"8"::)BIBI&"*"8"8"::)BIBI  B+>**+;(;(;R@""K$8$8+:N:N#OPPr   c                    ^  U 4S jnT R                  [        5       [        ST R                  5      5      nUR	                  U5      $ )zf
Test for server and client startTLS: client should received data both
before and after the startTLS.
c                    > TR                  TR                  R                  [        R                  [        R
                  -   5        g r   )assertEqualr  rP   r   r(   r3   )ignorer#   s    r   check TLSTests.test_TLS.<locals>.check{  5    ""((%--0E0N0NNr   Tr  r   rH   rL   addCallbackr#   r  ds   `  r   test_TLSTLSTests.test_TLSu  s8    	 MM/1=t3WX}}U##r   c                    ^  U 4S jnT R                  [        5       [        ST R                  5      5      nUR	                  U5      $ )z
Test for server startTLS not followed by a startTLS in client: the data
received after server startTLS should be received as raw.
c                    > TR                  TR                  R                  [        R                  5        TR                  TR                  R                  S5        g )NzNo encrypted bytes received)r  r  rP   r   r(   
assertTruerO   ignoredr#   s    r   r  "TLSTests.test_unTLS.<locals>.check  sA    T//557L7T7TUOOD..668UVr   Fr  r  s   `  r   
test_unTLSTLSTests.test_unTLS  s=    	W MM!#]5$//%J
 }}U##r   c                    ^  U 4S jnT R                  [        ST R                  5      [        5       S5      nUR	                  U5      $ )z*
Test startTLS first initiated by client.
c                    > TR                  TR                  R                  [        R                  [        R
                  -   5        g r   )r  r  rP   r   r(   r3   r#  s    r   r  )TLSTests.test_backwardsTLS.<locals>.check  r  r   T)r  rH   rL   r   r  r  s   `  r   test_backwardsTLSTLSTests.test_backwardsTLS  s?    
	 MM$02G2I4
 }}U##r   )r  r   r  r   rd   )rA   rB   rC   rD   rE   r   r   r   r   rL   r   r   r   r  r  r&  r+  rF   r   r   r   r   r   :  sK     gt,4CJKK8#QJ$$$r   r   c                   H    \ rS rSrSr\R                  " \S5      c  SrSr	Sr
g)SpammyTLSTestsi  z9
Test TLS features with bytes sitting in the out buffer.
Nr   Tr   )rA   rB   rC   rD   rE   r   r   r   r   rL   rF   r   r   r   r.  r.    s&     gt,4CJr   r.  c                   T    \ rS rSr\R
                  " \S5      c  SrSrSr	S r
S rSrg)BufferingTestsi  Nr   c                 :   U R                   R                  b$  U R                   R                  R                  5         U R                  R                  b$  U R                  R                  R                  5         [	        [
        U R                   U R                  /5      $ r   )r   r/   r4   r   r   r   r"   s    r   r   BufferingTests.tearDown  st    %%1&&557%%1&&557'$2B2BDDTDT1UVVr   c                   ^^ [        5       =mU l        [        5       =mU l        [        R
                  " 5       n[        R                  " 5       =o l        U4S jUl        U4S jUl        [        R                  " [        [        5      n[        R                  " 5       n[        R                  " SXSS9nU R                  UR                  5        [        R                   " SUR#                  5       R$                  X$5      nU R                  UR&                  5        TR(                  R+                  U R,                  S5      $ )Nc                     > T $ r   r   r  s   r   r   6BufferingTests.test_openSSLBuffering.<locals>.<lambda>      +r   c                     > T $ r   r   r   s   r   r   r5    r6  r   r   r  r   ri   )rf   r   rn   r   r   r  r  r2   r   r   r   ClientContextFactoryr   r   r  r	  r   r  r  
disconnectr!   r  r  )	r#   rX   r2   sCTXcCTXr  clientConnectorr   r   s	          @@r   test_openSSLBuffering$BufferingTests.test_openSSLBuffering  s    )A)CCd&)@)BBd&'')'5577--//(C'')  FKH**+!,,,,f
 	223##//4
 	
r   )r2   r   r   )rA   rB   rC   rD   r   r   r   r   r   r   r   r=  rF   r   r   r   r0  r0    s/    gt,4CKKW
r   r0  c                   \    \ rS rSrSr\R                  " \S5      c  SrS r	S r
S rS rS	rg)
ConnectionLostTestsi  z
SSL connection closing tests.
Nr   c                   ^  SnT R                  XS-   40 XS-   40 5        [        R                  " 5       n[        R                  Ul        [        R
                  " SUT R                  5      =T l        n[        R                  " 5       n[        Ul        [        R                  " 5       Ul        [        R                  " SUR                  5       R                  UT R                   5        UR                  R#                  U 4S j5      $ )Ntwisted.test.test_ssl, client, serverr   r  c                 8   > TR                   R                  5       $ r   )
serverPortr	  )ignoredResultr#   s    r   r   =ConnectionLostTests.testImmediateDisconnect.<locals>.<lambda>  s    $//"?"?"Ar   )r   r   r  Protocolr   r   r   rF  r  rx   r   r    r   r   r  r  r   r  )r#   r   serverProtocolFactoryrF  clientProtocolFactorys   `    r   testImmediateDisconnect+ConnectionLostTests.testImmediateDisconnect  s    %!!
"#R#Z/?)@"	
 !) 6 6 8)1):):&'.'8'8$d&;&;(
 	
* !) 6 6 8)I&7<~~7G4 %%!!!		
 %;;GGA
 	
r   c                   ^^ [        [        R                  5       " S S[        R                  5      5       nSnU R                  X"S-   40 X"S-   40 5        U" 5       m[        R                  " 5       nU4S jUl        [        R                  " SX0R                  5      nU R                  UR                  5        U" 5       m[        R                  " 5       nU4S jUl        [        R                  " S	UR                  5       R                  UU R                   5        S
 n["        R$                  " TR&                  R)                  U5      TR&                  R)                  U5      /5      $ )z
Both sides of SSL connection close connection; the connections should
close cleanly, and only after the underlying TCP connection has
disconnected.
c                   *    \ rS rSrSrS rS rS rSrg)MConnectionLostTests.test_bothSidesLoseConnection.<locals>.CloseAfterHandshakei  Fc                 8    [         R                  " 5       U l        g r   )r   r    doner"   s    r   r$   VConnectionLostTests.test_bothSidesLoseConnection.<locals>.CloseAfterHandshake.__init__  s    !NN,	r   c                 8    U R                   R                  5         g r   r{   r"   s    r   r|   `ConnectionLostTests.test_bothSidesLoseConnection.<locals>.CloseAfterHandshake.handshakeCompleted
  s    --/r   c                 >    U R                   R                  U5        U ? g r   )rR  errbackr;   s     r   r=   \ConnectionLostTests.test_bothSidesLoseConnection.<locals>.CloseAfterHandshake.connectionLost  s    		!!&)Ir   )rR  N)	rA   rB   rC   rD   gotDatar$   r|   r=   rF   r   r   r   CloseAfterHandshakerP    s    G-0r   rZ  rB  rC  rD  c                     > T $ r   r   serverProtocols   r   r   BConnectionLostTests.test_bothSidesLoseConnection.<locals>.<lambda>      r   r   c                     > T $ r   r   clientProtocols   r   r   r^    r_  r   r  c                 .    U R                  [        5        g r   )trapr   )failures    r   checkResultEConnectionLostTests.test_bothSidesLoseConnection.<locals>.checkResult&  s    LL(r   )r   r   IHandshakeListenerr   rI  r   r  r   r   r   r  r	  r  r   r  r  r   r   r  rR  
addErrback)	r#   rZ  r   rJ  rF  rK  rf  rb  r]  s	          @@r   test_bothSidesLoseConnection0ConnectionLostTests.test_bothSidesLoseConnection  s@    
Z22	3	("3"3 	 
4	 &!!
"#R#Z/?)@"	
 -. ( 6 6 8)?&&&q*?AVAVW

001,. ( 6 6 8)?& %%!!!		
	) ""##..{;##..{;
 	
r   c                 L  ^	^
 SnU R                  XS-   40 XS-   40 5        S nU R                  R                  5       R                  [        R
                  U5        [        R                  " 5       n[        R                  " 5       m
UR                  T
l        [        R                  " 5       nU
4S jUl        [        R                  " SX@R                  5      =U l        n[        R                  " 5       n[        R                  " 5       m	UR                  T	l        [        R"                  " 5       nU	4S jUl        [        R$                  " SUR'                  5       R(                  UU R                  5        [        R*                  " X6/S	S
9nUR-                  U R.                  5      $ )NrB  rC  rD  c                      g)NFr   )as    r   verify4ConnectionLostTests.testFailedVerify.<locals>.verify6  s    r   c                     > T $ r   r   r\  s   r   r   6ConnectionLostTests.testFailedVerify.<locals>.<lambda>?  r_  r   r   c                     > T $ r   r   ra  s   r   r   rr  H  r_  r   r  T)consumeErrors)r   r   
getContext
set_verifyr   VERIFY_PEERr   r    r   rI  r:   r=   r  r   r   r   rF  r  r   r  r  DeferredListr  _cbLostConns)r#   r   ro  serverConnLostrJ  rF  clientConnLostrK  dlrb  r]  s            @@r   testFailedVerify$ConnectionLostTests.testFailedVerify0  s\   %!!
"#R#Z/?)@"	
	 	((*55coovN)!**,(6(?(?% ( 6 6 8)?&'.'8'8$&;&;(
 	
* )!**,(6(?(?% ( 6 6 8)?& %%!!!		
  @PTU~~d//00r   c                 L   Uu  u  p#u  pEU R                  U5        U R                  U5        [        R                  /n[        R                  " 5       (       a  SSKJn  UR                  U5        UR                  " U6   UR                  " U6   U R                  R                  5       $ )Nr   )ConnectionLost)assertFalser   r   r   	isWindowstwisted.internet.errorr  rU   rd  rF  r	  )r#   resultssSuccesssResultcSuccesscResultacceptableErrorsr  s           r   ry   ConnectionLostTests._cbLostConnsS  s    3:00h""II; =##N3&'&',,..r   )rF  )rA   rB   rC   rD   rE   r   r   r   r   rL  rj  r}  ry  rF   r   r   r   r@  r@    s7     gt,4C
82
h!1F/r   r@  c                   0    \ rS rSrSrS rS rS rS rSr	g)	FakeContextin  zC
L{OpenSSL.SSL.Context} double which can more easily be inspected.
c                     Xl         SU l        g )Nr   _method_options)r#   methods     r   r$   FakeContext.__init__s  s    r   c                 .    U =R                   U-  sl         g r   )r  )r#   r   s     r   set_optionsFakeContext.set_optionsw  s     r   c                     g r   r   r#   fileNames     r   use_certificate_file FakeContext.use_certificate_filez      r   c                     g r   r   r  s     r   use_privatekey_fileFakeContext.use_privatekey_file}  r  r   r  N)
rA   rB   rC   rD   rE   r$   r  r  r  rF   r   r   r   r  r  n  s    !r   r  c                   \    \ rS rSrSr\R                  " \S5      c  SrS r	S r
S rS rS	rg)
!DefaultOpenSSLContextFactoryTestsi  z0
Tests for L{ssl.DefaultOpenSSLContextFactory}.
Nr   c                     [         R                  " [        [        [        S9U l        U R                  R                  5       U l        g )N)_contextFactory)r   r   r   r  r   ru  contextr"   s    r   setUp'DefaultOpenSSLContextFactoryTests.setUp  s6     ">>h
 **557r   c                 b   U R                  U R                  R                  [        R                  5        U R                  U R                  R
                  [        R                  -  [        R                  5        U R                  U R                  R
                  [        R                  -  5        g)zs
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"   s    r   test_method-DefaultOpenSSLContextFactoryTests.test_method  sp     	--s~~> 	..@#//R 	..1B1BBCr   c                     U R                  [        R                  [        R                  [
        U R                  5       5        g)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_missingCertificateFile=DefaultOpenSSLContextFactoryTests.test_missingCertificateFile  s+     	IIs774;;=	
r   c                     U R                  [        R                  [        R                  U R                  5       [        5        g)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   r   r   r   r"   s    r   test_missingPrivateKeyFile<DefaultOpenSSLContextFactoryTests.test_missingPrivateKeyFile  s+     	IIs77	
r   r  r   )rA   rB   rC   rD   rE   r   r   r   r   r  r  r  r  rF   r   r   r   r  r    s6     gt,4C8D

r   r  c                   P    \ rS rSrSr\R                  " \S5      c  SrS r	S r
Srg)ClientContextFactoryTestsi  z(
Tests for L{ssl.ClientContextFactory}.
Nr   c                     [         R                  " 5       U l        [        U R                  l        U R                  R                  5       U l        g r   )r   r8  r   r  r  ru  r  r"   s    r   r  ClientContextFactoryTests.setUp  s7    !668.9+**557r   c                    U R                  U R                  R                  [        R                  5        U R                  U R                  R
                  [        R                  -  [        R                  5        U R                  U R                  R
                  [        R                  -  5        U R                  U R                  R
                  [        R                  -  5        g)zl
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"   s    r   r  %ClientContextFactoryTests.test_method  s    
 	--s~~>..@#//R--?@--?@r   r  )rA   rB   rC   rD   rE   r   r   r   r   r  r  rF   r   r   r   r  r    s,     gt,4C8
Ar   r  )6rE   r   r   twisted.internetr   r   r   r   r  r   twisted.internet.testingr   twisted.protocolsr	   twisted.python.filepathr
   twisted.python.runtimer   twisted.test.test_tcpr   twisted.trial.unittestr   OpenSSLr   r   r   twisted.test.ssl_helpersr   r   ImportErrorr   zope.interfacer   LineReceiverr   rH   rI  rf   rn   rh  rx   r   r   r   r   rW   r   r   r.  r0  r@  r  r  r  r   r   r   <module>r     st   
  A A 1 = # , + 9 +#$C '%E.. %D+%E&& +%\,x00 ,%h// %  Z**+
;x'8'8 
; ,
;8G &
 &
R ?
I3;; 
I4
,h 4
ng$x g$TX &
X &
RS/($: S/l &0
 0
fA Au   Hs   E& &E:9E: