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_protocol.cpython-38.pyc
U


W['I�@sdZddlmZmZddlmZddlmZddlm	Z	ddl
mZddlm
Z
mZmZmZddlmZmZmZmZmZmZdd	lmZmZdd
lmZddlmZddlm Z m!Z!dd
l"m#Z#Gdd�de#�Z$Gdd�de#�Z%Gdd�de#�Z&Gdd�de#�Z'Gdd�de#�Z(dS)z)
Tests for L{twisted.internet.protocol}.
�)�division�absolute_import)�BytesIO)�implementer)�verifyObject)�CancelledError)�	IProtocol�ILoggingContext�IProtocolFactory�	IConsumer)�Protocol�
ClientCreator�Factory�ProtocolToConsumerAdapter�ConsumerToProtocolAdapter�FileWrapper)�LogLevel�globalLogPublisher)�_PY3)�Failure)�MemoryReactorClock�StringTransport)�TestCasec@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"S)#�ClientCreatorTestsz?
    Tests for L{twisted.internet.protocol.ClientCreator}.
    cCsDGdd�dt�}t�}t||�}|||�}|�d�}|�||�dS)a
        Helper for implementing a test to verify that one of the I{connect}
        methods of L{ClientCreator} passes the right arguments to the right
        reactor method.

        @param check: A function which will be invoked with a reactor and a
            L{ClientCreator} instance and which should call one of the
            L{ClientCreator}'s I{connect} methods and assert that all of its
            arguments except for the factory are passed on as expected to the
            reactor.  The factory should be returned.
        c@seZdZdS)z:ClientCreatorTests._basicConnectTest.<locals>.SomeProtocolN��__name__�
__module__�__qualname__�rr�E/usr/lib/python3/dist-packages/twisted/internet/test/test_protocol.py�SomeProtocol-sr N)rrr
�
buildProtocol�assertIsInstance)�self�checkr �reactor�cc�factory�protocolrrr�_basicConnectTest!s


z$ClientCreatorTests._basicConnectTestcs�fdd�}��|�dS)z�
        L{ClientCreator.connectTCP} calls C{reactor.connectTCP} with the host
        and port information passed to it, and with a factory which will
        construct the protocol passed to L{ClientCreator.__init__}.
        csX|�dddd�|j��\}}}}}��|d���|d���|d���|d�|S)N�example.com����)z1.2.3.4i�&)�
connectTCP�
tcpClients�pop�assertEqual)r%r&�host�portr'�timeout�bindAddress�r#rrr$=sz1ClientCreatorTests.test_connectTCP.<locals>.checkN�r)�r#r$rr5r�test_connectTCP7sz"ClientCreatorTests.test_connectTCPcs�fdd�}��|�dS)z�
        L{ClientCreator.connectUNIX} calls C{reactor.connectUNIX} with the
        filename passed to it, and with a factory which will construct the
        protocol passed to L{ClientCreator.__init__}.
        csF|�ddd�|j��\}}}}��|d���|d���|�|S)N�/foo/bar�{T)�connectUNIX�unixClientsr/r0�
assertTrue)r%r&�addressr'r3ZcheckPIDr5rrr$Ns
z2ClientCreatorTests.test_connectUNIX.<locals>.checkNr6r7rr5r�test_connectUNIXHsz#ClientCreatorTests.test_connectUNIXcs�fdd�}��|�dS)z�
        L{ClientCreator.connectSSL} calls C{reactor.connectSSL} with the host,
        port, and context factory passed to it, and with a factory which will
        construct the protocol passed to L{ClientCreator.__init__}.
        c	snt�}|�dd|dd�|j��\}}}}}}��|d���|d���||���|d���|d�|S)Nr*r+r,)z4.3.2.1i.)�object�
connectSSL�
sslClientsr/r0�assertIs)	r%r&ZexpectedContextFactoryr1r2r'�contextFactoryr3r4r5rrr$^sz1ClientCreatorTests.test_connectSSL.<locals>.checkNr6r7rr5r�test_connectSSLXs
z"ClientCreatorTests.test_connectSSLcCsNt�}t|t�}||�}|j��}|�|j�|��|�|j�|�	|t
�S)a
        Helper for implementing a test to verify that cancellation of the
        L{Deferred} returned by one of L{ClientCreator}'s I{connect} methods is
        implemented to cancel the underlying connector.

        @param connect: A function which will be invoked with a L{ClientCreator}
            instance as an argument and which should call one its I{connect}
            methods and return the result.

        @return: A L{Deferred} which fires when the test is complete or fails if
            there is a problem.
        )rr
r�
connectorsr/ZassertFalse�
_disconnected�cancelr=�
assertFailurer�r#�connectr%r&�d�	connectorrrr�_cancelConnectTestks


z%ClientCreatorTests._cancelConnectTestcCsdd�}|�|�S)��
        The L{Deferred} returned by L{ClientCreator.connectTCP} can be cancelled
        to abort the connection attempt before it completes.
        cSs|�dd�S�Nr*r+)r-�r&rrrrK�sz9ClientCreatorTests.test_cancelConnectTCP.<locals>.connect�rN�r#rKrrr�test_cancelConnectTCP�sz(ClientCreatorTests.test_cancelConnectTCPcCsdd�}|�|�S)rOcSs
|�d�S�Nr9)r;rQrrrrK�sz:ClientCreatorTests.test_cancelConnectUNIX.<locals>.connectrRrSrrr�test_cancelConnectUNIX�sz)ClientCreatorTests.test_cancelConnectUNIXcCsdd�}|�|�S)rOcSs|�ddt��SrP)rAr@rQrrrrK�sz9ClientCreatorTests.test_cancelConnectSSL.<locals>.connectrRrSrrr�test_cancelConnectSSL�sz(ClientCreatorTests.test_cancelConnectSSLcCsht�}t|t�}|||�}|j��}|�t|���d�|��|�|��g�|�	|j
�|�|t�S)z�
        Like L{_cancelConnectTest}, but for the case where the L{Deferred} is
        cancelled after the connection is set up but before it is fired with the
        resulting protocol instance.
        �)
rr
rrFr/r0�len�getDelayedCallsrHr=rGrIrrJrrr�_cancelConnectTimeoutTest�s


z,ClientCreatorTests._cancelConnectTimeoutTestcCsdd�}|�|�S)a�
        L{ClientCreator.connectTCP} inserts a very short delayed call between
        the time the connection is established and the time the L{Deferred}
        returned from one of its connect methods actually fires.  If the
        L{Deferred} is cancelled in this interval, the established connection is
        closed, the timeout is cancelled, and the L{Deferred} fails with
        L{CancelledError}.
        c
Ss>|�dd�}|j��\}}}}}|�d�}t�}	|�|	�|SrP)r-r.r/r!r�makeConnection)
r%r&rLr1r2r'r3r4r(�	transportrrrrK�s

z@ClientCreatorTests.test_cancelConnectTCPTimeout.<locals>.connect�r[rSrrr�test_cancelConnectTCPTimeout�s	z/ClientCreatorTests.test_cancelConnectTCPTimeoutcCsdd�}|�|�S)a�
        L{ClientCreator.connectUNIX} inserts a very short delayed call between
        the time the connection is established and the time the L{Deferred}
        returned from one of its connect methods actually fires.  If the
        L{Deferred} is cancelled in this interval, the established connection is
        closed, the timeout is cancelled, and the L{Deferred} fails with
        L{CancelledError}.
        c	Ss:|�d�}|j��\}}}}|�d�}t�}|�|�|SrU)r;r<r/r!rr\)	r%r&rLr>r'r3r4r(r]rrrrK�s


zAClientCreatorTests.test_cancelConnectUNIXTimeout.<locals>.connectr^rSrrr�test_cancelConnectUNIXTimeout�s	z0ClientCreatorTests.test_cancelConnectUNIXTimeoutcCsdd�}|�|�S)a�
        L{ClientCreator.connectSSL} inserts a very short delayed call between
        the time the connection is established and the time the L{Deferred}
        returned from one of its connect methods actually fires.  If the
        L{Deferred} is cancelled in this interval, the established connection is
        closed, the timeout is cancelled, and the L{Deferred} fails with
        L{CancelledError}.
        cSsD|�ddt��}|j��\}}}}}}|�d�}	t�}
|	�|
�|SrP)rAr@rBr/r!rr\)r%r&rLr1r2r'rDr3�bindADdressr(r]rrrrK�s

z@ClientCreatorTests.test_cancelConnectSSLTimeout.<locals>.connectr^rSrrr�test_cancelConnectSSLTimeout�s	z/ClientCreatorTests.test_cancelConnectSSLTimeoutcCstt�}t|t�}|||�\}}|j��}|�|ttd���|�t	|�
��d�|��|�|�
�g�|�|t
�S)z�
        Like L{_cancelConnectTest}, but for the case where the L{Deferred} is
        cancelled after the connection attempt has failed but before it is fired
        with the resulting failure.
        zSimulated failurerX)rr
rrFr/ZclientConnectionFailedr�	Exceptionr0rYrZrHrIr)r#rKr%r&rLr'rMrrr�_cancelConnectFailedTimeoutTest�s


�z2ClientCreatorTests._cancelConnectFailedTimeoutTestcCsdd�}|�|�S)zz
        Similar to L{test_cancelConnectTCPTimeout}, but for the case where the
        connection attempt fails.
        cSs(|�dd�}|j��\}}}}}||fSrP)r-r.r/)r%r&rLr1r2r'r3r4rrrrKszFClientCreatorTests.test_cancelConnectTCPFailedTimeout.<locals>.connect�rdrSrrr�"test_cancelConnectTCPFailedTimeoutsz5ClientCreatorTests.test_cancelConnectTCPFailedTimeoutcCsdd�}|�|�S)z{
        Similar to L{test_cancelConnectUNIXTimeout}, but for the case where the
        connection attempt fails.
        cSs$|�d�}|j��\}}}}||fSrU)r;r<r/)r%r&rLr>r'r3r4rrrrKs
zGClientCreatorTests.test_cancelConnectUNIXFailedTimeout.<locals>.connectrerSrrr�#test_cancelConnectUNIXFailedTimeoutsz6ClientCreatorTests.test_cancelConnectUNIXFailedTimeoutcCsdd�}|�|�S)zz
        Similar to L{test_cancelConnectSSLTimeout}, but for the case where the
        connection attempt fails.
        c	Ss.|�ddt��}|j��\}}}}}}||fSrP)rAr@rBr/)	r%r&rLr1r2r'rDr3rarrrrK+szFClientCreatorTests.test_cancelConnectSSLFailedTimeout.<locals>.connectrerSrrr�"test_cancelConnectSSLFailedTimeout&sz5ClientCreatorTests.test_cancelConnectSSLFailedTimeoutN)rrr�__doc__r)r8r?rErNrTrVrWr[r_r`rbrdrfrgrhrrrrrs"


rc@s(eZdZdZdd�Zdd�Zdd�ZdS)	�
ProtocolTestsz:
    Tests for L{twisted.internet.protocol.Protocol}.
    cCs*t�}|�tt|��|�tt|��dS)zT
        L{Protocol} instances provide L{IProtocol} and L{ILoggingContext}.
        N)rr=rrr	)r#�protorrr�test_interfaces7szProtocolTests.test_interfacescCs&Gdd�dt�}|�d|����dS)zJ
        L{Protocol.logPrefix} returns the protocol class's name.
        c@seZdZdS)z/ProtocolTests.test_logPrefix.<locals>.SomeThingNrrrrr�	SomeThingDsrmN)rr0�	logPrefix)r#rmrrr�test_logPrefix@szProtocolTests.test_logPrefixcs@g�G�fdd�dt�}t�}|�}|�|�|��|g�dS)zz
        L{Protocol.makeConnection} sets the given transport on itself, and
        then calls C{connectionMade}.
        cseZdZ�fdd�ZdS)z7ProtocolTests.test_makeConnection.<locals>.SomeProtocolcs��|j�dS�N)�appendr]r5��resultrr�connectionMadePszFProtocolTests.test_makeConnection.<locals>.SomeProtocol.connectionMadeN)rrrrtrrrrrr Osr N)rr@r\r0)r#r r]r(rrrr�test_makeConnectionIs
z!ProtocolTests.test_makeConnectionN)rrrrirlrorurrrrrj3s		rjc@s@eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�FactoryTestsz(
    Tests for L{protocol.Factory}.
    cCs*t�}|�tt|��|�tt|��dS)zg
        L{Factory} instances provide both L{IProtocolFactory} and
        L{ILoggingContext}.
        N)rr=rr
r	)r#r'rrrrl_szFactoryTests.test_interfacescCs&Gdd�dt�}|�d|����dS)zM
        L{Factory.logPrefix} returns the name of the factory class.
        c@seZdZdS)z6FactoryTests.test_logPrefix.<locals>.SomeKindOfFactoryNrrrrr�SomeKindOfFactorymsrwN)rr0rn)r#rwrrrroiszFactoryTests.test_logPrefixcCsDGdd�dt�}t�}||_|�d�}|�||�|�|j|�dS)z�
        L{Factory.buildProtocol} by default constructs a protocol by calling
        its C{protocol} attribute, and attaches the factory to the result.
        c@seZdZdS)z<FactoryTests.test_defaultBuildProtocol.<locals>.SomeProtocolNrrrrrr xsr N)rrr(r!r"rCr')r#r �fr(rrr�test_defaultBuildProtocolss
z&FactoryTests.test_defaultBuildProtocolcCsTGdd�dt�}|jtdddd�}|�|jt�|�|jd�|�|jddi�d	S)
z�
        L{Factory.forProtocol} constructs a Factory, passing along any
        additional arguments, and sets its C{protocol} attribute to the given
        Protocol subclass.
        c@seZdZdd�ZdS)z7FactoryTests.test_forProtocol.<locals>.ArgTakingFactoryc_s|||_|_dSrp)�args�kwargs)r#rzr{rrr�__init__�sz@FactoryTests.test_forProtocol.<locals>.ArgTakingFactory.__init__N)rrrr|rrrr�ArgTakingFactory�sr}rX��)�foo)rXr~r�N)rZforProtocolrr0r(rzr{)r#r}r'rrr�test_forProtocol�s
zFactoryTests.test_forProtocolcsrg�t��j�|��fdd��t�}|��|��dd|�|��ddtj	�|��ddd�dS)	z�
        L{Factory.doStart} logs that it is starting a factory, followed by
        the L{repr} of the L{Factory} instance that is being started.
        cst��j�Srp�rZremoveObserverrqr�Zeventsrr�<lambda>��z;FactoryTests.test_doStartLoggingStatement.<locals>.<lambda>rr'�	log_level�
log_formatzStarting factory {factory!r}N)
r�addObserverrq�
addCleanuprZdoStartrCr0r�info)r#rxrr�r�test_doStartLoggingStatement�s
��z)FactoryTests.test_doStartLoggingStatementcs�g�t��j�|��fdd��Gdd�dt�}|�}|��|��dd|�|��ddtj	�|��ddd	�d
S)z�
        L{Factory.doStop} logs that it is stopping a factory, followed by
        the L{repr} of the L{Factory} instance that is being stopped.
        cst��j�Srpr�rr�rrr��r�z:FactoryTests.test_doStopLoggingStatement.<locals>.<lambda>c@seZdZdZdS)z;FactoryTests.test_doStopLoggingStatement.<locals>.MyFactoryrXN)rrrZnumPortsrrrr�	MyFactory�sr�rr'r�r�zStopping factory {factory!r}N)
rr�rqr�rZdoStoprCr0rr�)r#r�rxrr�r�test_doStopLoggingStatement�s
��z(FactoryTests.test_doStopLoggingStatementN)
rrrrirlroryr�r�r�rrrrrvZs

rvc@s eZdZdZdd�Zdd�ZdS)�AdapterTestszR
    Tests for L{ProtocolToConsumerAdapter} and L{ConsumerToProtocolAdapter}.
    cCsBg}t�}|j|_t|�}|�d�|�|dg�|�|t�dS)z}
        L{IProtocol} providers can be adapted to L{IConsumer} providers using
        L{ProtocolToConsumerAdapter}.
        �helloN)rrq�dataReceivedr�writer0r"r)r#rs�pZconsumerrrr�test_protocolToConsumer�s
z$AdapterTests.test_protocolToConsumercsVg�tt�G�fdd�dt��}|�}t|�}|�d�|��dg�|�|t�dS)z}
        L{IConsumer} providers can be adapted to L{IProtocol} providers using
        L{ProtocolToConsumerAdapter}.
        cseZdZ�fdd�ZdS)z6AdapterTests.test_consumerToProtocol.<locals>.Consumercs��|�dSrp)rq)r#rLrrrrr��sz<AdapterTests.test_consumerToProtocol.<locals>.Consumer.writeN)rrrr�rrrrr�Consumer�sr�r�N)rrr@rr�r0r"r)r#r��cr(rrrr�test_consumerToProtocol�s
z$AdapterTests.test_consumerToProtocolN)rrrrir�r�rrrrr��sr�c@s eZdZdZdd�Zdd�ZdS)�FileWrapperTestsz2
    L{twisted.internet.protocol.FileWrapper}
    cCsPtt��}|�d�|�|j��d�tt��}|�d�|�|j��d�dS)z@
        L{twisted.internet.protocol.FileWrapper.write}
        �test1ZstuffN)rrr�r0�file�getvalueZassertNotEqual�r#�wrapperrrr�
test_write�s



zFileWrapperTests.test_writecCsxtt��}|�ddg�|�|j��d�tt��}trN|�t|jddg�n&|�ddg�|�	t
|j���dk�dS)zH
        L{twisted.internet.protocol.FileWrapper.writeSequence}
        r�stest2s
test1test2Ztest3Ztest4rN)rrZ
writeSequencer0r�r�rZassertRaises�	TypeErrorr=rYr�rrr�test_writeSequence�s

�z#FileWrapperTests.test_writeSequenceN)rrrrir�r�rrrrr��sr�N))riZ
__future__rr�iorZzope.interfacerZzope.interface.verifyrZtwisted.internet.deferrZtwisted.internet.interfacesrr	r
rZtwisted.internet.protocolrr
rrrrZtwisted.loggerrrZtwisted.python.compatrZtwisted.python.failurerZtwisted.test.proto_helpersrrZtwisted.trial.unittestrrrjrvr�r�rrrr�<module>s& '`%