
    Yf)2              
           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 n# e1$ r dZ,Y nw xY w 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;            <                    e9=                                            e;            <                    e7=                                            e;            <                     e:            =                                            G d% d&ee e3          Z> e;            <                    e>=                                           dS )'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                   t    e Zd ZU egZeeee                           e	d<    e
j                    r	dZeedZdS dS )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     P/var/www/html/env/lib/python3.11/site-packages/twisted/internet/test/test_tls.pyr$   r$   2   sl         ?Jm$y/!:;KKKx 
> 	
 ;>8;
 

 
r.   r$   c                       e Zd ZddlZ e eej                                                d                              d          Z	[d Z
d ZdS )ContextGeneratingMixinr   Ns   tests
   server.pemc                     | j                                         }t          j        |t	          j        |t
                    t
                    }|                                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   sL     i""$$!&c<0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   @   s}        NNN 	w/001199'BBHHWW 	 	  & & & & &r.   r1   c                       e Zd ZdZd Zd ZdS )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                 "    || _         || _        d S r<   )wrappedcontextFactory)r7   rF   rG   s      r/   __init__zStartTLSClientEndpoint.__init__b   s    ,r.   c                 v      G  fddt                     } 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dS )6StartTLSClientEndpoint.connect.<locals>.WrapperFactoryc                 \                         |          j        ffd	}|_        S )Nc                 Z    j                             j                    |              d S r<   )	transportstartTLSrG   )origprotocolr7   s    r/   connectionMadez\StartTLSClientEndpoint.connect.<locals>.WrapperFactory.buildProtocol.<locals>.connectionMades   s,    &//0CDDDDFFFFFr.   )buildProtocolrR   )wrapperSelfaddrrR   rQ   factoryr7   s      @r/   rS   zDStartTLSClientEndpoint.connect.<locals>.WrapperFactory.buildProtocolp   sO    "0066(0(?        +9'r.   N)r&   r'   r(   rS   )rV   r7   s   r/   WrapperFactoryrK   o   s3                       r.   rW   )r   rF   connect)r7   rV   rW   s   `` r/   rX   zStartTLSClientEndpoint.connectf   s]    		  		  		  		  		  		  		  		 ] 		  		  		  |##NN$4$4555r.   N)r&   r'   r(   __doc__rH   rX   r-   r.   r/   rD   rD   V   s<         - - -6 6 6 6 6r.   rD   c                       e Zd ZdZd Zd ZdS )StartTLSClientCreatorz{
    Create L{ITLSTransport.startTLS} endpoint for the client, and normal SSL
    for server just because it's easier.
    c                 H    t          |d|                                           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.EFFFr.   c                 b    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   s1     &w]5GHH ""
 
 	
r.   Nr&   r'   r(   rY   r`   rf   r-   r.   r/   r[   r[   }   s?         
G G G
 
 
 
 
r.   r[   c                       e Zd ZdZd ZdS )BadContextTestsMixinz
    Mixin for L{ReactorBuilder} subclasses which defines a helper for testing
    the handling of broken context factories.
    c                     |                                  }|                     t          ||t                                }|                     t          j        t          |                     dS )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   s[     ##%%
E7<P<R<RSS-5s3xx@@@@@r.   N)r&   r'   r(   rY   rs   r-   r.   r/   ri   ri      s2         
A A A A Ar.   ri   c                   &    e Zd ZdZ e            ZdS )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(         
 &%''IIIr.   ru   c                       e Zd ZdZd Zd ZdS )
SSLCreatorz
    Create SSL endpoints.
    c                 H    t          |d|                                           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.EFFFr.   c                 H    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)     "[-"46J6L6L
 
 	
r.   Nrg   r-   r.   r/   rx   rx      s?         G G G	
 	
 	
 	
 	
r.   rx   c                   2    e Zd ZdZ e            Zd Zd ZdS )SSLClientTestsMixinz<
    Mixin defining tests relating to L{ITLSTransport}.
    c                 6    d }|                      |           dS )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                     }|                                 	t                      }t                      |_        ||_        |                                 |_        t                      }t                      |_        ||_        | 	                                |_        |j        j
        |j        _
        g t          |j        |j        gd          }fd}|                    |           	                    d|d          }|                     |j                   	                    |                                j        |                                j        |          }|                     |j                   |                    	fd	           |                     	           d                             t.                     d
                             t.                     dS )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dS )QSSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.ShortProtocolc                 ,   t          j        | j                  s<| j        j        }d | j        _        |                    t          d                     d S | j                            | j        j                   | j        	                    d           d S )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   s    $/?? #|4H,0DL)$$X.H%I%IJJJF ''(<=== $$T*****r.   c                 l    | j                             d           | j                                          d S )N   y)rN   r   loseConnection)r7   datas     r/   dataReceivedz^SSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.ShortProtocol.dataReceived   s6    
 $$T*** --/////r.   c                 h    | j         j        }|#d | j         _        |                    |           d S d S r<   )rV   r   callback)r7   reasonr   s      r/   connectionLostz`SSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.ShortProtocol.connectionLost  s@      <0',0DL)%%f----- ('r.   N)r&   r'   r(   rR   r   r   r-   r.   r/   ShortProtocolr      sA        + + +0 0 0. . . . .r.   r   T)consumeErrorsc                 f                         | d         d         | d         d         g           d S )Nr      )extend)resultslostConnectionResultss    r/   
cbFinishedzNSSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.cbFinished"  s2    !(('!*Q-A)GHHHHHr.   r   rb   )	interfacec                 ,                                     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   s   	. 	. 	. 	. 	.H 	. 	. 	.B ##%%%!)!. $ 5 5 7 7%!)!. $ 5 5 7 7'4'<'C$ "#]%;<D
 
 
	I 	I 	I 	I 	I 	Z(((  M[ II*+++&&LLNN!4m
 
	 		,---7777888   a %%&6777a %%&677777r.   N)r&   r'   r(   rY   rx   rv   r   r   r-   r.   r/   r|   r|      sN          
I$ $ $L8 L8 L8 L8 L8r.   r|   c                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )TLSPortTestsBuilderz,
    Tests for L{IReactorSSL.listenSSL}
    c                 T    |                     d||                                           S )z0
        Get a TLS port from a reactor.
        r   )	listenSSLr:   )r7   r_   rV   s      r/   getListeningPortz$TLSPortTestsBuilder.getListeningPortA  s(       GT-B-B-D-DEEEr.   c                 >    d||                                 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\\^^5H+IIIr.   c                 <    d|                                 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      :DLLNN/9999r.   c                 6    d }|                      |           dS )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                 H    |                      dt                      |          S )Nr   )r   r   r   s     r/   rq   z2TLSPortTestsBuilder.test_badContext.<locals>.useItZ  s    $$QHHHr.   Nr   r   s     r/   r   z#TLSPortTestsBuilder.test_badContextS  s0    	I 	I 	I 	U#####r.   c                 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_  s9    $ !!L!!##	
 
 	
r.   N)	r&   r'   r(   rY   r   r   r   r   r   r-   r.   r/   r   r   5  so         F F FJ J J: : :
$ 
$ 
$
 
 
 
 
r.   r   c                   2    e Zd ZdZefZ e            Zd ZdS )AbortSSLConnectionTestsz-
    C{abortConnection} tests using SSL.
    c                 2    t           t          d          d S )NzOpenSSL not available.)r   r   r=   s    r/   setUpzAbortSSLConnectionTests.setUp  s    3444  r.   N)	r&   r'   r(   rY   r   r%   rx   rv   r   r-   r.   r/   r   r   ~  sB          &
I5 5 5 5 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 1 1 1 1 1 1 1 9 9 9 9 9 9 9 9         
 4 3 3 3 3 3         
 M L L L L L L L L L         
 ? > > > > >         
 0 / / / / / , , , , , , + + + + + + + + + + + +W++++++ WVVVVVVVVVV    LLL
 
 
 
 
 
 
 
& & & & & & & &, "###6 #6 #6 #6 #6 #6 #6 $##6L
 
 
 
 
O-C 
 
 
0A A A A A A A A,( ( ( ( (x9M ( ( (
 
 
 
 
"8 
 
 
.g8 g8 g8 g8 g8g8 g8 g8TA
 A
 A
 A
 A
A
 A
 A
H 		  $88:: ; ; ; 		  )==?? @ @ @ 		  $$&&::<< = = =5 5 5 5 5(*@5 5 5 		  (<<>> ? ? ? ? ?s   4B BB