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: //proc/self/root/lib/python3/dist-packages/twisted/test/__pycache__/iosim.cpython-38.pyc
U


W[�E�@sVdZddlmZmZmZddlZzddlmZWne	k
rDYnXddl
mZmZddl
mZmZddlmZmZddlmZdd	lmZdd
lmZddlmZdd
lmZGdd�d�Zeej�Gdd�de��Zeej ej!�Gdd�d��Z"dd�Z#dd�Z$Gdd�d�Z%d&dd�Z&e#e$ddfdd�Z'd d!�Z(Gd"d#�d#e�Z)d'd$d%�Z*dS)(z0
Utilities and helpers for simulating a network
�)�absolute_import�division�print_functionN)�Error)�implementer�directlyProvides)�TCP4ClientEndpoint�TCP4ServerEndpoint)�Factory�Protocol)�ConnectionRefusedError)�Failure)�error)�
interfaces�)�MemoryReactorClockc@s$eZdZdd�Zdd�Zdd�ZdS)�TLSNegotiationcCs||_||_d|_||_dS)NF)�obj�connectState�sent�readyToSend)�selfrr�r�4/usr/lib/python3/dist-packages/twisted/test/iosim.py�__init__szTLSNegotiation.__init__cCsd|jfS)NzTLSNegotiation(%r))r�rrrr�__repr__&szTLSNegotiation.__repr__cCs"|j�|j�st�|_|��dS�N)rZiosimVerify�NativeOpenSSLError�disconnectReason�loseConnection)r�otherZtptrrr�pretendToVerify*szTLSNegotiation.pretendToVerifyN)�__name__�
__module__�__qualname__rrr"rrrrrsrc@seZdZdZdS)�FakeAddressz]
    The default address type for the host and peer of L{FakeTransport}
    connections.
    N)r#r$r%�__doc__rrrrr&4sr&c@s�eZdZdZee��fdd��ZdZdZ	dZ
e�d�Z
dZdZdZd0dd�Zd	d
�Zdd�Zd
d�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Z d'd(�Z!d1d*d+�Z"d,d-�Z#d.d/�Z$dS)2�
FakeTransportz�
    A wrapper around a file-like object to make it behave as a Transport.

    This doesn't actually stream the file to the attached protocol,
    and is thus useful mainly as a utility for debugging protocols.
    cCst|�Sr)�next)Zcounterrrr�<lambda>G�zFakeTransport.<lambda>rzConnection doneNcCsH||_||_g|_|��|_|dkr*t�}||_|dkr>t�}||_dS)a�
        @param protocol: This transport will deliver bytes to this protocol.
        @type protocol: L{IProtocol} provider

        @param isServer: C{True} if this is the accepting side of the
            connection, C{False} if it is the connecting side.
        @type isServer: L{bool}

        @param hostAddress: The value to return from C{getHost}.  L{None}
            results in a new L{FakeAddress} being created to use as the value.
        @type hostAddress: L{IAddress} provider or L{None}

        @param peerAddress: The value to return from C{getPeer}.  L{None}
            results in a new L{FakeAddress} being created to use as the value.
        @type peerAddress: L{IAddress} provider or L{None}
        N)�protocol�isServer�stream�_nextserial�serialr&�hostAddress�peerAddress)rr,r-r1r2rrrrPs
zFakeTransport.__init__cCs d|jrdpd|j|jjjfS)NzFakeTransport<%s,%s,%s>�S�C)r-r0r,�	__class__r#rrrrrms
�zFakeTransport.__repr__cCs2|jr
dS|jdk	r"|j�|�n|j�|�dSr)�
disconnecting�tls�tlsbuf�appendr.)r�datarrr�writess

zFakeTransport.writecCs|jr|js|j��dSr��producer�streamingProducer�resumeProducingrrrr�_checkProducer~szFakeTransport._checkProducercCs||_||_|s|��dS)z.
        From abstract.FileDescriptor
        Nr<)rr=Z	streamingrrr�registerProducer�szFakeTransport.registerProducercCs
d|_dSr)r=rrrr�unregisterProducer�sz FakeTransport.unregisterProducercCs|��|��dSr)rBr rrrr�
stopConsuming�szFakeTransport.stopConsumingcCs|�d�|��dS)Nr+)r;�join)rZiovecrrr�
writeSequence�szFakeTransport.writeSequencecCs
d|_dS�NT�r6rrrrr �szFakeTransport.loseConnectioncCs
d|_dS)zp
        For the time being, this is the same as loseConnection; no buffered
        data will be lost.
        TNrGrrrr�abortConnection�szFakeTransport.abortConnectioncCs,|jdk	rt�}n|j}|j�t|��dSr)r7rrr,ZconnectionLostr
)r�errrrr�reportDisconnect�s
zFakeTransport.reportDisconnectcCsdS)zM
        Identify this transport/event source to the logging system.
        Ziosimrrrrr�	logPrefix�szFakeTransport.logPrefixcCs|jSr)r2rrrr�getPeer�szFakeTransport.getPeercCs|jSr)r1rrrr�getHost�szFakeTransport.getHostcCsdSrrrrrrr?�szFakeTransport.resumeProducingcCsdSrrrrrr�pauseProducing�szFakeTransport.pauseProducingcCs|��dSr)r rrrr�
stopProducing�szFakeTransport.stopProducingTcCs |j|A}t||�|_g|_dSr)r-rr7r8)rZcontextFactoryZbeNormalrrrr�startTLS�s
zFakeTransport.startTLScCsH|j}|rg|_d�|�S|jdk	r@|jjr:d|j_|jSdSndSdS)z�
        Get the pending writes from this transport, clearing them from the
        pending buffer.

        @return: the bytes written with C{transport.write}
        @rtype: L{bytes}
        r+NT)r.rDr7rr)rr3rrr�getOutBuffer�s

zFakeTransport.getOutBuffercCstt|t�rd|jdk	st�|jjrZ|j�||�d|_|jd}|_|�|�t|t	j
�qpd|j_n|j�
|�dSrF)�
isinstancerr7�AssertionErrorrr"r8rErrZ
ISSLTransportrr,ZdataReceived)rZbuf�brrr�bufferReceived�s


zFakeTransport.bufferReceived)NN)T)%r#r$r%r'�staticmethod�	itertools�countr/�closedr6�disconnectedrZConnectionDonerr=r>r7rrr;r@rArBrCrEr rHrJrKrLrMr?rNrOrPrQrUrrrrr(=s:




	r(cCst|dd�S)z�
    Create and return a new in-memory transport hooked up to the given protocol.

    @param clientProtocol: The client protocol to use.
    @type clientProtocol: L{IProtocol} provider

    @return: The transport.
    @rtype: L{FakeTransport}
    F�r-�r()�clientProtocolrrr�makeFakeClients
r^cCst|dd�S)z�
    Create and return a new in-memory transport hooked up to the given protocol.

    @param serverProtocol: The server protocol to use.
    @type serverProtocol: L{IProtocol} provider

    @return: The transport.
    @rtype: L{FakeTransport}
    Tr[r\)�serverProtocolrrr�makeFakeServers
r`c@s,eZdZdZdd�Zd
dd�Zddd�Zd	S)�IOPumpz�
    Utility to pump data between clients and servers for protocol testing.

    Perhaps this is a utility worthy of being in protocol.py?
    cCs"||_||_||_||_||_dSr)�client�server�clientIO�serverIO�debug)rrbrcrdrerfrrrr&s
zIOPump.__init__FcCs6d}td�D]}|�|�r d}qq2qds2td��|S)zk
        Pump until there is no more input or output.

        Returns whether any data was moved.
        Fi�TrzToo long)�range�pumprS)rrf�result�xrrr�flush.s
zIOPump.flushcCs,|js
|rtd�|j��}|j��}|j��|j��|jsD|rttd�|r`tdt|��|rttdt|��|r�|j�|�|r�|j�|�|s�|r�dS|jjr�|jj	s�|js�|r�td�d|j_	d|j_|j�
�dS|jj�r(|jj	�s(|j�s|�r
td�d|j_	d|j_|j�
�dSdS)	zX
        Move data back and forth.

        Returns whether any data was moved.
        z
-- GLUG --�.zC: zS: Tz* Cz* SF)rf�printrerQrdr@�reprrUr6rZrJ)rrfZsDataZcDatarrrrh?sF





�


zIOPump.pumpN)F)F)r#r$r%r'rrkrhrrrrra s
raFTcCs4|�|�|�|�t|||||�}|r0|��|S)aN
    Create a new L{IOPump} connecting two protocols.

    @param serverProtocol: The protocol to use on the accepting side of the
        connection.
    @type serverProtocol: L{IProtocol} provider

    @param serverTransport: The transport to associate with C{serverProtocol}.
    @type serverTransport: L{FakeTransport}

    @param clientProtocol: The protocol to use on the initiating side of the
        connection.
    @type clientProtocol: L{IProtocol} provider

    @param clientTransport: The transport to associate with C{clientProtocol}.
    @type clientTransport: L{FakeTransport}

    @param debug: A flag indicating whether to log information about what the
        L{IOPump} is doing.
    @type debug: L{bool}

    @param greet: Should the L{IOPump} be L{flushed <IOPump.flush>} once before
        returning to put the protocols into their post-handshake or
        post-server-greeting state?
    @type greet: L{bool}

    @return: An L{IOPump} which connects C{serverProtocol} and
        C{clientProtocol} and delivers bytes between them when it is pumped.
    @rtype: L{IOPump}
    )ZmakeConnectionrark)r_�serverTransportr]�clientTransportrf�greetrhrrr�connectks 

�rrc
	Cs4|�}|�}||�}||�}	||t||	||||�fS)a
    Connect a given server and client class to each other.

    @param ServerClass: a callable that produces the server-side protocol.
    @type ServerClass: 0-argument callable returning L{IProtocol} provider.

    @param ClientClass: like C{ServerClass} but for the other side of the
        connection.
    @type ClientClass: 0-argument callable returning L{IProtocol} provider.

    @param clientTransportFactory: a callable that produces the transport which
        will be attached to the protocol returned from C{ClientClass}.
    @type clientTransportFactory: callable taking (L{IProtocol}) and returning
        L{FakeTransport}

    @param serverTransportFactory: a callable that produces the transport which
        will be attached to the protocol returned from C{ServerClass}.
    @type serverTransportFactory: callable taking (L{IProtocol}) and returning
        L{FakeTransport}

    @param debug: Should this dump an escaped version of all traffic on this
        connection to stdout for inspection?
    @type debug: L{bool}

    @param greet: Should the L{IOPump} be L{flushed <IOPump.flush>} once before
        returning to put the protocols into their post-handshake or
        post-server-greeting state?
    @type greet: L{bool}

    @return: the client protocol, the server protocol, and an L{IOPump} which,
        when its C{pump} and C{flush} methods are called, will move data
        between the created client and server protocol instances.
    @rtype: 3-L{tuple} of L{IProtocol}, L{IProtocol}, L{IOPump}
    )rr)
ZServerClassZClientClassZclientTransportFactoryZserverTransportFactoryrfrq�c�sZcioZsiorrr�connectedServerAndClient�s
&rucCs2|\}}}}}|\}}}	}
||kr*||fSdSdS)a'
    Should the client and server described by the arguments be connected to
    each other, i.e. do their port numbers match?

    @param clientInfo: the args for connectTCP
    @type clientInfo: L{tuple}

    @param serverInfo: the args for listenTCP
    @type serverInfo: L{tuple}

    @return: If they do match, return factories for the client and server that
        should connect; otherwise return L{None}, indicating they shouldn't be
        connected.
    @rtype: L{None} or 2-L{tuple} of (L{ClientFactory},
        L{IProtocolFactory})
    Nr)�
clientInfo�
serverInfoZ
clientHostZ
clientPort�
clientFactoryZ
clientTimeoutZclientBindAddressZ
serverPort�
serverFactoryZ
serverBacklogZserverInterfacerrr�_factoriesShouldConnect�s�
�rzc@s4eZdZdZdd�Zd
dd�Zee��fdd�Zd	S)�ConnectionCompleterz�
    A L{ConnectionCompleter} can cause synthetic TCP connections established by
    L{MemoryReactor.connectTCP} and L{MemoryReactor.listenTCP} to succeed or
    fail.
    cCs
||_dS)z�
        Create a L{ConnectionCompleter} from a L{MemoryReactor}.

        @param memoryReactor: The reactor to attach to.
        @type memoryReactor: L{MemoryReactor}
        N)�_reactor)r�
memoryReactorrrrr�szConnectionCompleter.__init__Fc
Cs�|j}t|j�D]~\}}|jD]n}t||�}|r|j�|�|j�|�|\}}|�d�}	|�d�}
t	|
�}t
|	�}t|
||	||�SqqdS)a�
        Complete a single TCP connection established on this
        L{ConnectionCompleter}'s L{MemoryReactor}.

        @param debug: A flag; whether to dump output from the established
            connection to stdout.
        @type debug: L{bool}

        @return: a pump for the connection, or L{None} if no connection could
            be established.
        @rtype: L{IOPump} or L{None}
        N)r|�	enumerate�
tcpClientsZ
tcpServersrz�remove�
connectors�popZ
buildProtocolr`r^rr)
rrfr}Z	clientIdxrvrwZ	factoriesrxryr]r_rorprrr�succeedOnce�s"




�zConnectionCompleter.succeedOncecCs(|jj�d�d�|jj�d�|�dS)z�
        Fail a single TCP connection established on this
        L{ConnectionCompleter}'s L{MemoryReactor}.

        @param reason: the reason to provide that the connection failed.
        @type reason: L{Failure}
        r�N)r|rr�ZclientConnectionFailedr�)r�reasonrrr�failOnces�zConnectionCompleter.failOnceN)F)	r#r$r%r'rr�r
rr�rrrrr{�s

r{cCs8t�}t|dd�}t|d�}|�t�t��|t|�fS)a�
    Create an endpoint that can be fired on demand.

    @param debug: A flag; whether to dump output from the established
        connection to stdout.
    @type debug: L{bool}

    @return: A client endpoint, and an object that will cause one of the
        L{Deferred}s returned by that client endpoint.
    @rtype: 2-L{tuple} of (L{IStreamClientEndpoint}, L{ConnectionCompleter})
    z0.0.0.0i�)rrr	Zlistenr
ZforProtocolrr{)rfZreactorZclientEndpointZserverEndpointrrr�connectableEndpoints

r�)FT)F)+r'Z
__future__rrrrWZOpenSSL.SSLrr�ImportErrorZzope.interfacerrZtwisted.internet.endpointsrr	Ztwisted.internet.protocolr
rZtwisted.internet.errorrZtwisted.python.failurer
Ztwisted.internetrrZ
proto_helpersrrZIAddress�objectr&Z
ITransportZ
ITLSTransportr(r^r`rarrrurzr{r�rrrr�<module>sH�FL�
-�
.<