
    Vh)2              
       X   d Z ddlmZmZmZ ddlmZmZ ddlm	Z	m
Z
 ddlmZmZmZ ddlmZ ddlmZmZmZ ddlmZmZmZ dd	lmZmZmZ dd
lmZ ddlm Z m!Z!m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z( ddl)m*Z* 	 ddl+m,Z, ddl-m.Z.m/Z/m0Z0  G d d      Z2 G d d      Z3 ee       G d d             Z4 G d dee3      Z5 G d d      Z6 G d de2ee      Z7 G d d ee3      Z8 G d! d"e2ee3ee6      Z9 G d# d$e2e3e6e!e"e      Z: e;       jy                  e9j{                                 e;       jy                  e7j{                                 e;       jy                   e:       j{                                 G d% d&ee e3      Z> e;       jy                  e>j{                                y# e1$ r dZ,Y $w xY w)'z0
Tests for implementations of L{ITLSTransport}.
    )OptionalSequenceType)	Interfaceimplementer)DeferredDeferredList)SSL4ClientEndpointSSL4ServerEndpointTCP4ClientEndpoint)ConnectionClosed)IReactorSSLIStreamClientEndpointITLSTransport)ClientFactoryProtocolServerFactory)BrokenContextFactoryConnectionTestsMixinEndpointCreator)ReactorBuilder)AbortConnectionMixinConnectToTCPListenerMixinStreamTransportTestsMixin)networkString)FilePath)platform)SkipTest)FILETYPE_PEM)ClientContextFactoryKeyPairPrivateCertificateNc                   b    e Zd ZU egZeeee         e	d<    e
j                         rdZeedZyy)TLSMixinrequiredInterfaceszcFor some reason, these reactors don't deal with SSL disconnection correctly on Windows.  See #3371.)z*twisted.internet.glib2reactor.Glib2Reactorz(twisted.internet.gtk2reactor.Gtk2ReactorN)__name__
__module____qualname__r   r%   r   r   r   r   __annotations__r   	isWindowsmsgskippedReactors     N/home/dcms/DCMS/lib/python3.12/site-packages/twisted/internet/test/test_tls.pyr$   r$   2   sI    ?Jm$y/!:;Kx> 	
 ;>8;
 r.   r$   c                       e Zd ZddlZ e eej                              j                  d      j                  d      Z	[d Z
d Zy)ContextGeneratingMixinr   Ns   tests
   server.pemc                     | j                   j                         }t        j                  |t	        j                  |t
              t
              }|j                         S )zM
        Return a new SSL context suitable for use in a test server.
        )_pem
getContentr"   loadr!   r   options)selfpemcerts      r/   getServerContextz'ContextGeneratingMixin.getServerContextH   sF     ii""$!&&c<0,
 ||~r.   c                     t               S N)r    r7   s    r/   getClientContextz'ContextGeneratingMixin.getClientContextR   s    #%%r.   )r&   r'   r(   twistedr   r   __file__siblingchildr3   r:   r>   r-   r.   r/   r1   r1   @   sD     	w//0199'BHHW 	 	&r.   r1   c                       e Zd ZdZd Zd Zy)StartTLSClientEndpointa!  
    An endpoint which wraps another one and adds a TLS layer immediately when
    connections are set up.

    @ivar wrapped: A L{IStreamClientEndpoint} provider which will be used to
        really set up connections.

    @ivar contextFactory: A L{ContextFactory} to use to do TLS.
    c                      || _         || _        y r<   )wrappedcontextFactory)r7   rF   rG   s      r/   __init__zStartTLSClientEndpoint.__init__b   s    ,r.   c                 j      G  fddt               } j                  j                   |             S )z
        Establish a connection using a protocol build by C{factory} and
        immediately start TLS on it.  Return a L{Deferred} which fires with the
        protocol instance.
        c                       e Zd Z fdZy)6StartTLSClientEndpoint.connect.<locals>.WrapperFactoryc                 ^    j                  |      j                  ffd	}|_        S )Nc                 ^    j                   j                  j                          |         y r<   )	transportstartTLSrG   )origprotocolr7   s    r/   connectionMadez\StartTLSClientEndpoint.connect.<locals>.WrapperFactory.buildProtocol.<locals>.connectionMades   s"    &&//0C0CDFr.   )buildProtocolrR   )wrapperSelfaddrrR   rQ   factoryr7   s      @r/   rS   zDStartTLSClientEndpoint.connect.<locals>.WrapperFactory.buildProtocolp   s0    "006(0(?(?  +9'r.   N)r&   r'   r(   rS   )rV   r7   s   r/   WrapperFactoryrK   o   s     r.   rW   )r   rF   connect)r7   rV   rW   s   `` r/   rX   zStartTLSClientEndpoint.connectf   s)    		 ] 		  ||##N$455r.   N)r&   r'   r(   __doc__rH   rX   r-   r.   r/   rD   rD   V   s    -6r.   rD   c                       e Zd ZdZd Zd Zy)StartTLSClientCreatorz{
    Create L{ITLSTransport.startTLS} endpoint for the client, and normal SSL
    for server just because it's easier.
    c                 8    t        |d| j                               S )z
        Construct an SSL server endpoint.  This should be constructing a TCP
        server endpoint which immediately calls C{startTLS} instead, but that
        is hard.
        r   r   r:   r7   reactors     r/   serverzStartTLSClientCreator.server   s     "'1d.C.C.EFFr.   c                 T    t        t        |d|j                        t                     S )zS
        Construct a TCP client endpoint wrapped to immediately start TLS.
        	127.0.0.1)rD   r   portr    r7   r_   serverAddresss      r/   clientzStartTLSClientCreator.client   s)     &w]5G5GH "
 	
r.   Nr&   r'   r(   rY   r`   rf   r-   r.   r/   r[   r[   }   s    
G
r.   r[   c                       e Zd ZdZd Zy)BadContextTestsMixinz
    Mixin for L{ReactorBuilder} subclasses which defines a helper for testing
    the handling of broken context factories.
    c                     | j                         }| j                  t        ||t                     }| j	                  t        j
                  t        |             y)a  
        Assert that the exception raised by a broken context factory's
        C{getContext} method is raised by some reactor method.  If it is not, an
        exception will be raised to fail the test.

        @param useIt: A two-argument callable which will be called with a
            reactor and a broken context factory and which is expected to raise
            the same exception as the broken context factory's C{getContext}
            method.
        N)buildReactorassertRaises
ValueErrorr   assertEqualmessagestr)r7   useItr_   excs       r/   _testBadContextz$BadContextTestsMixin._testBadContext   sG     ##%
E7<P<RS-55s3x@r.   N)r&   r'   r(   rY   rs   r-   r.   r/   ri   ri      s    
Ar.   ri   c                       e Zd ZdZ e       Zy)StartTLSClientTestsMixinz
    Tests for TLS connections established using L{ITLSTransport.startTLS} (as
    opposed to L{IReactorSSL.connectSSL} or L{IReactorSSL.listenSSL}).
    N)r&   r'   r(   rY   r[   	endpointsr-   r.   r/   ru   ru      s    
 &'Ir.   ru   c                       e Zd ZdZd Zd Zy)
SSLCreatorz
    Create SSL endpoints.
    c                 8    t        |d| j                               S )zQ
        Create an SSL server endpoint on a TCP/IP-stack allocated port.
        r   r]   r^   s     r/   r`   zSSLCreator.server   s     "'1d.C.C.EFFr.   c                 B    t        |d|j                  t                     S )z
        Create an SSL client endpoint which will connect localhost on
        the port given by C{serverAddress}.

        @type serverAddress: L{IPv4Address}
        rb   )r
   rc   r    rd   s      r/   rf   zSSLCreator.client   s$     "[-"4"46J6L
 	
r.   Nrg   r-   r.   r/   rx   rx      s    G	
r.   rx   c                   *    e Zd ZdZ e       Zd Zd Zy)SSLClientTestsMixinz<
    Mixin defining tests relating to L{ITLSTransport}.
    c                 ,    d }| j                  |       y)z
        If the context factory passed to L{IReactorSSL.connectSSL} raises an
        exception from its C{getContext} method, that exception is raised by
        L{IReactorSSL.connectSSL}.
        c                 :    | j                  ddt               |      S )Nrb   i  )
connectSSLr   r_   rG   s     r/   rq   z2SSLClientTestsMixin.test_badContext.<locals>.useIt   s     %%T=?N r.   Nrs   r7   rq   s     r/   test_badContextz#SSLClientTestsMixin.test_badContext   s    	
 	U#r.   c                   	  G d dt               }| j                         	t               }t               |_        ||_        | j                         |_        t               }t               |_        ||_        | j                         |_        |j                  j                  |j                  _
        g t        |j                  |j                  gd      }fd}|j                  |       	j                  d|d      }| j                  |j                         	j!                  |j#                         j$                  |j#                         j&                  |      }| j                  |j(                         |j                  	fd	       | j+                  	       d   j-                  t.               d
   j-                  t.               y)aO  
        L{ITCPTransport.loseConnection} ends a connection which was set up with
        L{ITLSTransport.startTLS} and which has recently been written to.  This
        is intended to verify that a socket send error masked by the TLS
        implementation doesn't prevent the connection from being reported as
        closed.
        c                       e Zd Zd Zd Zd Zy)QSSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.ShortProtocolc                 Z   t        j                  | j                        sB| j                  j                  }d | j                  _        |j                  t        d             y | j                  j                  | j                  j                         | j                  j                  d       y )NzNo ITLSTransport support   x)
r   
providedByrN   rV   finishederrbackr   rO   contextwrite)r7   r   s     r/   rR   z`SSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.ShortProtocol.connectionMade   sx    $//?#||44H,0DLL)$$X.H%IJ ''(<(<= $$T*r.   c                 n    | j                   j                  d       | j                   j                          y )N   y)rN   r   loseConnection)r7   datas     r/   dataReceivedz^SSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.ShortProtocol.dataReceived   s(    
 $$T* --/r.   c                 z    | j                   j                  }|#d | j                   _        |j                  |       y y r<   )rV   r   callback)r7   reasonr   s      r/   connectionLostz`SSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.ShortProtocol.connectionLost  s8      <<00',0DLL)%%f- (r.   N)r&   r'   r(   rR   r   r   r-   r.   r/   ShortProtocolr      s    +0.r.   r   T)consumeErrorsc                 D    j                  | d   d   | d   d   g       y )Nr      )extend)resultslostConnectionResultss    r/   
cbFinishedzNSSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.cbFinished"  s&    !(('!*Q-A)GHr.   r   rb   )	interfacec                 $    j                         S r<   )stop)ignr_   s    r/   <lambda>zLSSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.<lambda>/  s     r.   r   N)r   rk   r   r   r   rQ   r:   r   r   r>   methodr	   addCallback	listenTCP
addCleanupstopListening
connectTCPgetHosthostrc   
disconnect
runReactortrapr   )
r7   r   serverFactoryclientFactoryr   r   rc   	connectorr   r_   s
           @@r/   &test_disconnectAfterWriteAfterStartTLSz:SSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS   sy   	.H 	.B ##%%!)!. $ 5 5 7%!)!. $ 5 5 7'4'<'<'C'C$ "##]%;%;<D
	I 	Z(  M[ I**+&&LLN!4!4m
	 		,,-78 a %%&67a %%&67r.   N)r&   r'   r(   rY   rx   rv   r   r   r-   r.   r/   r|   r|      s     I$L8r.   r|   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)TLSPortTestsBuilderz,
    Tests for L{IReactorSSL.listenSSL}
    c                 D    |j                  d|| j                               S )z0
        Get a TLS port from a reactor.
        r   )	listenSSLr:   )r7   r_   rV   s      r/   getListeningPortz$TLSPortTestsBuilder.getListeningPortA  s"       GT-B-B-DEEr.   c                 @    d||j                         j                  fz  S )zY
        Get the message expected to be logged when a TLS port starts listening.
        z%s (TLS) starting on %dr   rc   )r7   rc   rV   s      r/   #getExpectedStartListeningLogMessagez7TLSPortTestsBuilder.getExpectedStartListeningLogMessageG  s      )GT\\^5H5H+IIIr.   c                 >    d|j                         j                   dS )zJ
        Get the expected connection lost message for a TLS port.
        z
(TLS Port z Closed)r   )r7   rc   s     r/   getExpectedConnectionLostLogMsgz3TLSPortTestsBuilder.getExpectedConnectionLostLogMsgM  s      DLLN//099r.   c                 ,    d }| j                  |       y)z
        If the context factory passed to L{IReactorSSL.listenSSL} raises an
        exception from its C{getContext} method, that exception is raised by
        L{IReactorSSL.listenSSL}.
        c                 8    | j                  dt               |      S )Nr   )r   r   r   s     r/   rq   z2TLSPortTestsBuilder.test_badContext.<locals>.useItZ  s    $$QHHr.   Nr   r   s     r/   r   z#TLSPortTestsBuilder.test_badContextS  s    	I 	U#r.   c                 n    |j                  | j                  |j                  || j                               S )a  
        Connect to the given listening TLS port, assuming the
        underlying transport is TCP.

        @param reactor: The reactor under test.
        @type reactor: L{IReactorSSL}

        @param address: The listening's address.  Only the C{port}
            component is used; see
            L{ConnectToTCPListenerMixin.LISTENER_HOST}.
        @type address: L{IPv4Address} or L{IPv6Address}

        @param factory: The client factory.
        @type factory: L{ClientFactory}

        @return: The connector
        )r   LISTENER_HOSTrc   r>   )r7   r_   addressrV   s       r/   connectToListenerz%TLSPortTestsBuilder.connectToListener_  s6    $ !!LL!!#	
 	
r.   N)	r&   r'   r(   rY   r   r   r   r   r   r-   r.   r/   r   r   5  s"    FJ:
$
r.   r   c                   *    e Zd ZdZefZ e       Zd Zy)AbortSSLConnectionTestsz-
    C{abortConnection} tests using SSL.
    c                 &    t         t        d      y )NzOpenSSL not available.)r   r   r=   s    r/   setUpzAbortSSLConnectionTests.setUp  s    344  r.   N)	r&   r'   r(   rY   r   r%   rx   rv   r   r-   r.   r/   r   r   ~  s     &I5r.   r   )?rY   typingr   r   r   zope.interfacer   r   twisted.internet.deferr   r	   twisted.internet.endpointsr
   r   r   twisted.internet.errorr   twisted.internet.interfacesr   r   r   twisted.internet.protocolr   r   r   &twisted.internet.test.connectionmixinsr   r   r   #twisted.internet.test.reactormixinsr   twisted.internet.test.test_tcpr   r   r   twisted.python.compatr   twisted.python.filepathr   twisted.python.runtimer   twisted.trial.unittestr   OpenSSL.cryptor   twisted.internet.sslr    r!   r"   ImportErrorr$   r1   rD   r[   ri   ru   rx   r|   r   globalsupdatemakeTestCaseClassesr   r-   r.   r/   <module>r      s  
 , + 1 9 
 4 
 M L 
 ? 
 0 , + +W+ WV
 
& &, "##6 #6 $#6L
O-C 
0A A,(x9M (
"8 
.g8g8TA
A
H 	  $88: ; 	  )==? @ 	  $&::< =5(*@5 	  (<<> ?C  Ls   4F F)(F)