HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //lib/python3/dist-packages/twisted/internet/test/__pycache__/test_tls.cpython-38.pyc
U


W[�4�
@sLdZddlmZmZeZddlmZddlm	Z	ddl
mZddlm
Z
ddlmZmZmZddlmZmZmZdd	lmZmZdd
lmZmZmZddlmZddlm Z dd
l!m"Z"ddl#m$Z$ddl%m&Z&ddl'm(Z(m)Z)m*Z*ddl+m,Z,m-Z-m.Z.zddl/m0Z0Wne1k
�r"dZ0YnXddl2m3Z3m4Z4ddl2m5Z5Gdd�d�Z6Gdd�de7�Z8ee�Gdd�de7��Z9Gdd�de,e8�Z:Gdd�de7�Z;Gd d!�d!e6e
e-�Z<Gd"d#�d#e,e8�Z=Gd$d%�d%e6e
e8e-e;�Z>Gd&d'�d'e6e8e&e;e(e)e
�	Z?e@��Ae>�B��e@��Ae<�B��e@��Ae?��B��Gd(d)�d)e
e*e8�ZCe@��AeC�B��dS)*z0
Tests for implementations of L{ITLSTransport}.
�)�division�absolute_import)�implementer)�
networkString)�FilePath)�ReactorBuilder)�
ServerFactory�
ClientFactory�Protocol)�IReactorSSL�
ITLSTransport�IStreamClientEndpoint)�Deferred�DeferredList)�SSL4ServerEndpoint�SSL4ClientEndpoint�TCP4ClientEndpoint)�ConnectionClosed)�
Cooperator)�SkipTest)�platform)�ObjectModelIntegrationMixin)�ConnectToTCPListenerMixin�StreamTransportTestsMixin�AbortConnectionMixin)�EndpointCreator�ConnectionTestsMixin�BrokenContextFactory)�FILETYPE_PEMN)�PrivateCertificate�KeyPair��ClientContextFactoryc@s(eZdZegZe��r$dZeed�ZdS)�TLSMixinzcFor 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�requiredInterfacesrZ	isWindows�msgZskippedReactors�r)r)�@/usr/lib/python3/dist-packages/twisted/internet/test/test_tls.pyr#,s��r#c@s@eZdZddlZeeej���d��d�Z	[dd�Z
dd�ZdS)	�ContextGeneratingMixinrNstests
server.pemcCs(|j��}t�|t�|t�t�}|��S)zM
        Return a new SSL context suitable for use in a test server.
        )�_pemZ
getContentr�loadr rZoptions)�selfZpemZcertr)r)r*�getServerContext>s

�z'ContextGeneratingMixin.getServerContextcCst�S�Nr!�r.r)r)r*�getClientContextHsz'ContextGeneratingMixin.getClientContext)r$r%r&Ztwistedrr�__file__ZsiblingZchildr,r/r2r)r)r)r*r+8s���
r+c@s eZdZdZdd�Zdd�ZdS)�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.
    cCs||_||_dSr0)�wrapped�contextFactory)r.r5r6r)r)r*�__init__YszStartTLSClientEndpoint.__init__cs$G��fdd�dt�}�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.
        cseZdZ��fdd�ZdS)z6StartTLSClientEndpoint.connect.<locals>.WrapperFactorycs(��|���jf��fdd�	}|�_�S)Ncs�j��j�|�dSr0)�	transport�startTLSr6)Zorig)�protocolr.r)r*�connectionMadeisz\StartTLSClientEndpoint.connect.<locals>.WrapperFactory.buildProtocol.<locals>.connectionMade)�
buildProtocolr;)ZwrapperSelfZaddrr;��factoryr.)r:r*r<gs
zDStartTLSClientEndpoint.connect.<locals>.WrapperFactory.buildProtocolN)r$r%r&r<r)r=r)r*�WrapperFactoryfsr?)rr5�connect)r.r>r?r)r=r*r@^s	zStartTLSClientEndpoint.connectN)r$r%r&�__doc__r7r@r)r)r)r*r4Ms
r4c@s eZdZdZdd�Zdd�ZdS)�StartTLSClientCreatorz{
    Create L{ITLSTransport.startTLS} endpoint for the client, and normal SSL
    for server just because it's easier.
    cCst|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�rr/�r.�reactorr)r)r*�serverxszStartTLSClientCreator.servercCstt|d|j�t��S)zS
        Construct a TCP client endpoint wrapped to immediately start TLS.
        �	127.0.0.1)r4r�portr"�r.rEZ
serverAddressr)r)r*�client�s��zStartTLSClientCreator.clientN�r$r%r&rArFrJr)r)r)r*rBss	rBc@seZdZdZdd�ZdS)�BadContextTestsMixinz�
    Mixin for L{ReactorBuilder} subclasses which defines a helper for testing
    the handling of broken context factories.
    cCs0|��}|�t||t��}|�tjt|��dS)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)�buildReactorZassertRaises�
ValueErrorrZassertEqual�message�str)r.�useItrE�excr)r)r*�_testBadContext�s�z$BadContextTestsMixin._testBadContextN)r$r%r&rArSr)r)r)r*rL�srLc@seZdZdZe�ZdS)�StartTLSClientTestsMixinz�
    Tests for TLS connections established using L{ITLSTransport.startTLS} (as
    opposed to L{IReactorSSL.connectSSL} or L{IReactorSSL.listenSSL}).
    N)r$r%r&rArB�	endpointsr)r)r)r*rT�srTc@s eZdZdZdd�Zdd�ZdS)�
SSLCreatorz
    Create SSL endpoints.
    cCst|d|���S)zQ
        Create an SSL server endpoint on a TCP/IP-stack allocated port.
        rrCrDr)r)r*rF�szSSLCreator.servercCst|d|jt��S)z�
        Create an SSL client endpoint which will connect localhost on
        the port given by C{serverAddress}.

        @type serverAddress: L{IPv4Address}
        rG)rrHr"rIr)r)r*rJ�s�zSSLCreator.clientNrKr)r)r)r*rV�srVc@s&eZdZdZe�Zdd�Zdd�ZdS)�SSLClientTestsMixinz<
    Mixin defining tests relating to L{ITLSTransport}.
    cCsdd�}|�|�dS)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}.
        cSs|�ddt�|�S)NrGi�)�
connectSSLr	�rEr6r)r)r*rQ�s�z2SSLClientTestsMixin.test_badContext.<locals>.useItN�rS�r.rQr)r)r*�test_badContext�sz#SSLClientTestsMixin.test_badContextcsGdd�dt�}|���t�}t�|_||_|��|_t�}t�|_||_|�	�|_|jj
|j_
g�t|j|jgdd�}�fdd�}|�|��j
d|dd	�}|�|j���|��j|��j|�}|�|j�|��fd
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@s$eZdZdd�Zdd�Zdd�ZdS)zQSSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.ShortProtocolcSsNt�|j�s.|jj}d|j_|�td��dS|j�|jj�|j�	d�dS)NzNo ITLSTransport support�x)
rZ
providedByr8r>�finishedZerrbackrr9�context�write)r.r^r)r)r*r;�sz`SSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.ShortProtocol.connectionMadecSs|j�d�|j��dS)N�y)r8r`ZloseConnection)r.�datar)r)r*�dataReceived�sz^SSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.ShortProtocol.dataReceivedcSs&|jj}|dk	r"d|j_|�|�dSr0)r>r^�callback)r.�reasonr^r)r)r*�connectionLost�sz`SSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.ShortProtocol.connectionLostN)r$r%r&r;rcrfr)r)r)r*�
ShortProtocol�s
rgT)Z
consumeErrorscs"��|dd|ddg�dS)Nr�)�extend)Zresults)�lostConnectionResultsr)r*�
cbFinishedszNSSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.cbFinishedrrG)Z	interfacecs���Sr0)�stop)Zign�rEr)r*�<lambda>�zLSSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLS.<locals>.<lambda>rhN)r
rMrrr^r:r/r_r	r2�methodrZaddCallbackZ	listenTCPZ
addCleanupZ
stopListeningZ
connectTCP�getHostZhostrHZ
disconnectZ
runReactorZtrapr)r.rgZ
serverFactoryZ
clientFactoryr^rkrHZ	connectorr))rjrEr*�&test_disconnectAfterWriteAfterStartTLS�s<!


�
�
z:SSLClientTestsMixin.test_disconnectAfterWriteAfterStartTLSN)r$r%r&rArVrUr\rrr)r)r)r*rW�srWc@s8eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�TLSPortTestsBuilderz,
    Tests for L{IReactorSSL.listenSSL}
    cCs|�d||���S)z0
        Get a TLS port from a reactor.
        r)�	listenSSLr/)r.rEr>r)r)r*�getListeningPort*sz$TLSPortTestsBuilder.getListeningPortcCsd||��jfS)zY
        Get the message expected to be logged when a TLS port starts listening.
        z%s (TLS) starting on %d�rqrH)r.rHr>r)r)r*�#getExpectedStartListeningLogMessage1sz7TLSPortTestsBuilder.getExpectedStartListeningLogMessagecCsd|��jfS)zJ
        Get the expected connection lost message for a TLS port.
        z(TLS Port %s Closed)rv)r.rHr)r)r*�getExpectedConnectionLostLogMsg8sz3TLSPortTestsBuilder.getExpectedConnectionLostLogMsgcCsdd�}|�|�dS)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}.
        cSs|�dt�|�S)Nr)rtrrYr)r)r*rQEsz2TLSPortTestsBuilder.test_badContext.<locals>.useItNrZr[r)r)r*r\?sz#TLSPortTestsBuilder.test_badContextcCs|�|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
        )rXZ
LISTENER_HOSTrHr2)r.rEZaddressr>r)r)r*�connectToListenerJs�z%TLSPortTestsBuilder.connectToListenerN)	r$r%r&rArurwrxr\ryr)r)r)r*rs"srsc@s,eZdZdZefZe�Zdd�Zdd�Z	dS)�AbortSSLConnectionTestsz-
    C{abortConnection} tests using SSL.
    cs<t�|��ddlm}t�fdd�d�}|�|d|j��S)Nr)�_producer_helperscs��d|�S)Ng�h㈵��>)Z	callLater)�xrmr)r*rnyroz6AbortSSLConnectionTests.buildReactor.<locals>.<lambda>)Z	scheduler�	cooperate)rrMZtwisted.internetr{rZpatchr})r.r{Z
cooperatorr)rmr*rMrs

�z$AbortSSLConnectionTests.buildReactorcCstdkrtd��dS)NzOpenSSL not available.)rrr1r)r)r*�setUp~szAbortSSLConnectionTests.setUpN)
r$r%r&rArr'rVrUrMr~r)r)r)r*rzks
rz)DrAZ
__future__rr�typeZ
__metaclass__Zzope.interfacerZtwisted.python.compatrZtwisted.python.filepathrZ#twisted.internet.test.reactormixinsrZtwisted.internet.protocolrr	r
Ztwisted.internet.interfacesrrr
Ztwisted.internet.deferrrZtwisted.internet.endpointsrrrZtwisted.internet.errorrZtwisted.internet.taskrZtwisted.trial.unittestrZtwisted.python.runtimerZtwisted.internet.test.test_corerZtwisted.internet.test.test_tcprrrZ&twisted.internet.test.connectionmixinsrrrZOpenSSL.cryptor�ImportErrorZtwisted.internet.sslrr r"r#�objectr+r4rBrLrTrVrWrs�globals�updateZmakeTestCaseClassesrzr)r)r)r*�<module>s^
%	�_�C