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/thread-self/root/lib/python3/dist-packages/twisted/pair/__pycache__/testing.cpython-38.pyc
U


W[�C�@s dZddlZddlZddlmZmZmZmZmZm	Z	m
Z
mZddlm
Z
ddlmZddlmZddlmZddlmZdd	lmZdd
lmZddlmZmZmZmZddlm Z d
Z!dd�Z"dZ#dd�Z$dd�Z%dd�Z&Gdd�de'�Z(dd�Z)ee�Gdd�de'��Z*Gdd�de'�Z+dS)zD
Tools for automated testing of L{twisted.pair}-based applications.
�N)�EPERM�EAGAIN�EWOULDBLOCK�ENOSYS�EBADF�EINVAL�EINTR�ENOBUFS)�deque��wraps)�implementer)�DatagramProtocol)�EthernetProtocol)�RawUDPProtocol)�
IPProtocol)�	_IFNAMSIZ�
_TUNSETIFF�_IInputOutputSystem�TunnelFlags)�nativeString�cCst�d|�S)z�
    Pack an integer into a network-order two-byte string.

    @param n: The integer to pack.  Only values that fit into 16 bits are
        supported.

    @return: The packed representation of the integer.
    @rtype: L{bytes}
    z>H)�struct�pack)�n�r�6/usr/lib/python3/dist-packages/twisted/pair/testing.py�_H!s
r�cCs||t|�|S)a�
    Construct an ethernet frame.

    @param src: The source ethernet address, encoded.
    @type src: L{bytes}

    @param dst: The destination ethernet address, encoded.
    @type dst: L{bytes}

    @param protocol: The protocol number of the payload of this datagram.
    @type protocol: L{int}

    @param payload: The content of the ethernet frame (such as an IP datagram).
    @type payload: L{bytes}

    @return: The full ethernet frame.
    @rtype: L{bytes}
    )r��src�dstZprotocol�payloadrrr�	_ethernet1sr#cCs�dtdt|��dtd�t�tjt|��t�tjt|��}tt�d|��}|d?}|d@|}|dA}|dd	�t�	d
|�|dd�}||S)a�
    Construct an IP datagram with the given source, destination, and
    application payload.

    @param src: The source IPv4 address as a dotted-quad string.
    @type src: L{bytes}

    @param dst: The destination IPv4 address as a dotted-quad string.
    @type dst: L{bytes}

    @param payload: The content of the IP datagram (such as a UDP datagram).
    @type payload: L{bytes}

    @return: An IP datagram header and payload.
    @rtype: L{bytes}
    sE�s@rz!10H�i��N�
z!H�)
r�len�socketZ	inet_ptonZAF_INETr�sumr�unpackr)r r!r"ZipHeaderZ
checksumStep1ZcarryZ
checksumStep2Z
checksumStep3rrr�_ipHs.���	���

�
��r,cCs0t|�t|�tt|�d�td�}||S)a~
    Construct a UDP datagram with the given source, destination, and
    application payload.

    @param src: The source port number.
    @type src: L{int}

    @param dst: The destination port number.
    @type dst: L{int}

    @param payload: The content of the UDP datagram.
    @type payload: L{bytes}

    @return: A UDP datagram header and payload.
    @rtype: L{bytes}
    �r)rr()r r!r"Z	udpHeaderrrr�_udp}s����	r.c@sreZdZdZdZeed�Zee	d�Z
eed�ZeZ
dZdd�Zed	d
��Zedd��Zd
d�Zdd�Zdd�ZdS)�Tunnelz�
    An in-memory implementation of a tun or tap device.

    @cvar _DEVICE_NAME: A string representing the conventional filesystem entry
        for the tunnel factory character special device.
    @type _DEVICE_NAME: C{bytes}
    s/dev/net/tunz Resource temporarily unavailablezOperation would blockzInterrupted function callicCs:||_||_d|_d|_d|_t�|_t�|_t�|_dS)a
        @param system: An L{_IInputOutputSystem} provider to use to perform I/O.

        @param openFlags: Any flags to apply when opening the tunnel device.
            See C{os.O_*}.

        @type openFlags: L{int}

        @param fileMode: ignored
        N)	�system�	openFlags�
tunnelMode�
requestedName�namer
�
readBuffer�writeBuffer�pendingSignals)�selfr0r1ZfileModerrr�__init__�szTunnel.__init__cCs|j|jj@S)zx
        If the file descriptor for this tunnel is open in blocking mode,
        C{True}.  C{False} otherwise.
        )r1r0�
O_NONBLOCK�r8rrr�blocking�szTunnel.blockingcCst|j|jj@�S)zz
        If the file descriptor for this tunnel is marked as close-on-exec,
        C{True}.  C{False} otherwise.
        )�boolr1r0�	O_CLOEXECr;rrr�closeOnExec�szTunnel.closeOnExeccCs.|jtjj@rtddt|d�}|j�|�dS)aI
        Deliver a datagram to this tunnel's read buffer.  This makes it
        available to be read later using the C{read} method.

        @param datagram: The IPv4 datagram to deliver.  If the mode of this
            tunnel is TAP then ethernet framing will be added automatically.
        @type datagram: L{bytes}
        ss������rN)r2r�IFF_TAP�valuer#�_IPv4r5�append�r8�datagramrrr�addToReadBuffer�s
�zTunnel.addToReadBuffercCsX|jr@|jtjj@rd}ndt}|d8}||j��d|�S|jrNt��n|j	�dS)a
        Read a datagram out of this tunnel.

        @param limit: The maximum number of bytes from the datagram to return.
            If the next datagram is larger than this, extra bytes are dropped
            and lost forever.
        @type limit: L{int}

        @raise OSError: Any of the usual I/O problems can result in this
            exception being raised with some particular error number set.

        @raise IOError: Any of the usual I/O problems can result in this
            exception being raised with some particular error number set.

        @return: The datagram which was read from the tunnel.  If the tunnel
            mode does not include L{TunnelFlags.IFF_NO_PI} then the datagram is
            prefixed with a 4 byte PI header.
        @rtype: L{bytes}
        ��rN)
r5r2r�	IFF_NO_PIrA�_PI_SIZE�popleftr<�NotImplementedError�nonBlockingExceptionStyle)r8�limit�headerrrr�read�szTunnel.readcCsF|jr|j��ttd��t|�|jkr2ttd��|j�|�t|�S)a{
        Write a datagram into this tunnel.

        @param datagram: The datagram to write.
        @type datagram: L{bytes}

        @raise IOError: Any of the usual I/O problems can result in this
            exception being raised with some particular error number set.

        @return: The number of bytes of the datagram which were written.
        @rtype: L{int}
        zInterrupted system callzNo buffer space available)	r7rK�IOErrorrr(�SEND_BUFFER_SIZEr	r6rCrDrrr�writes



zTunnel.writeN)�__name__�
__module__�__qualname__�__doc__Z_DEVICE_NAMErQrZEAGAIN_STYLE�OSErrorrZEWOULDBLOCK_STYLErZEINTR_STYLErMrRr9�propertyr<r?rFrPrSrrrrr/�s




$r/cst���fdd��}|S)a|
    Wrap a L{MemoryIOSystem} method with permission-checking logic.  The
    returned function will check C{self.permissions} and raise L{IOError} with
    L{errno.EPERM} if the function name is not listed as an available
    permission.

    @param original: The L{MemoryIOSystem} instance to wrap.

    @return: A wrapper around C{original} that applies permission checks.
    cs&�j|jkrttd���|f|�|�S)NzOperation not permitted)rT�permissionsrQr)r8�args�kwargs��originalrr�permissionChecker6s
z&_privileged.<locals>.permissionCheckerr)r^r_rr]r�_privileged+sr`c@szeZdZdZdZdZdZdZdd�Zdd	�Z	d
d�Z
edd
d��Zdd�Z
dd�Zdd�Zedd��Zdd�Zdd�ZdS)�MemoryIOSystemz�
    An in-memory implementation of basic I/O primitives, useful in the context
    of unit testing as a drop-in replacement for parts of the C{os} module.

    @ivar _devices:
    @ivar _openFiles:
    @ivar permissions:

    @ivar _counter:
    i ��rcCsi|_i|_tddg�|_dS)N�open�ioctl)�_devices�
_openFiles�setrZr;rrrr9QszMemoryIOSystem.__init__cCs|j|��S)aX
        Get the L{Tunnel} object associated with the given L{TuntapPort}.

        @param port: A L{TuntapPort} previously initialized using this
            L{MemoryIOSystem}.

        @return: The tunnel object created by a prior use of C{open} on this
            object on the tunnel special device file.
        @rtype: L{Tunnel}
        )rg�fileno)r8�portrrr�	getTunnelWszMemoryIOSystem.getTunnelcCs||j|<dS)a1
        Specify a class which will be used to handle I/O to a device of a
        particular name.

        @param name: The filesystem path name of the device.
        @type name: L{bytes}

        @param cls: A class (like L{Tunnel}) to instantiated whenever this
            device is opened.
        N)rf)r8r4�clsrrr�registerSpecialDeviceesz$MemoryIOSystem.registerSpecialDeviceNcCsH||jkr:|j}|jd7_|j||||�|j|<|Sttd��dS)a�
        A replacement for C{os.open}.  This initializes state in this
        L{MemoryIOSystem} which will be reflected in the behavior of the other
        file descriptor-related methods (eg L{MemoryIOSystem.read},
        L{MemoryIOSystem.write}, etc).

        @param name: A string giving the name of the file to open.
        @type name: C{bytes}

        @param flags: The flags with which to open the file.
        @type flags: C{int}

        @param mode: The mode with which to open the file.
        @type mode: C{int}

        @raise OSError: With C{ENOSYS} if the file is not a recognized special
            device file.

        @return: A file descriptor associated with the newly opened file
            description.
        @rtype: L{int}
        rbzFunction not implementedN)rf�_counterrgrXr)r8r4�flags�mode�fdrrrrdss
zMemoryIOSystem.opencCs6z|j|�|�WStk
r0ttd��YnXdS)z�
        Try to read some bytes out of one of the in-memory buffers which may
        previously have been populated by C{write}.

        @see: L{os.read}
        �Bad file descriptorN)rgrP�KeyErrorrXr)r8rqrNrrrrP�szMemoryIOSystem.readcCs6z|j|�|�WStk
r0ttd��YnXdS)z�
        Try to add some bytes to one of the in-memory buffers to be accessed by
        a later C{read} call.

        @see: L{os.write}
        rrN)rgrSrsrXr)r8rq�datarrrrS�szMemoryIOSystem.writecCs0z|j|=Wntk
r*ttd��YnXdS)z�
        Discard the in-memory buffer and other in-memory state for the given
        file descriptor.

        @see: L{os.close}
        rrN)rgrsrXr)r8rqrrr�close�szMemoryIOSystem.closecCs�z|j|}Wntk
r,ttd��YnX|tkr@ttd��t�dtf|�\}}||_	||_
|dtd�d|_t�dtf|j|�S)z�
        Perform some configuration change to the in-memory state for the given
        file descriptor.

        @see: L{fcntl.ioctl}
        rrzRequest or args is not valid.z%dsHN�s123)
rgrsrQrrrrr+rr2r3r4r)r8rqZrequestr[Ztunnelr4rprrrre�s
zMemoryIOSystem.ioctlcCsLd}d}t||dt||d|d�d�}t|j���}|d�|�||fS)ah
        Write an ethernet frame containing an ip datagram containing a udp
        datagram containing the given payload, addressed to the given address,
        to a tunnel device previously opened on this I/O system.

        @param datagram: A UDP datagram payload to send.
        @type datagram: L{bytes}

        @param address: The destination to which to send the datagram.
        @type address: L{tuple} of (L{bytes}, L{int})

        @return: A two-tuple giving the address from which gives the address
            from which the datagram was sent.
        @rtype: L{tuple} of (L{bytes}, L{int})
        z10.1.2.3iaSrrb)r r!r")r,r.�listrg�valuesrF)r8rE�addressZsrcIPZsrcPortZ
serializedZ	openFilesrrr�sendUDP�s��zMemoryIOSystem.sendUDPcCs
t||�S)aa
        Get a socket-like object which can be used to receive a datagram sent
        from the given address.

        @param fileno: A file descriptor representing a tunnel device which the
            datagram will be received via.
        @type fileno: L{int}

        @param host: The IPv4 address to which the datagram was sent.
        @type host: L{bytes}

        @param port: The UDP port number to which the datagram was sent.
            received.
        @type port: L{int}

        @return: A L{socket.socket}-like object which can be used to receive
            the specified datagram.
        )�	_FakePort)r8riZhostrjrrr�
receiveUDP�szMemoryIOSystem.receiveUDP)N)rTrUrVrWrn�O_RDWRr:r>r9rkrmr`rdrPrSrurerzr|rrrrra?s"




rac@s eZdZdZdd�Zdd�ZdS)r{z�
    A socket-like object which can be used to read UDP datagrams from
    tunnel-like file descriptors managed by a L{MemoryIOSystem}.
    cCs||_||_dS�N)�_system�_fileno)r8r0rirrrr9sz_FakePort.__init__c
s�|jj|jj��}g�t�}�fdd�}||_t�}|�d|�t	����d|�|jj|jj
}|tjj
@r�t�}|�d��|j}n�fdd�}|tjj
@}	|	r�|td�}||��d	d|�S)
a_
        Receive a datagram sent to this port using the L{MemoryIOSystem} which
        created this object.

        This behaves like L{socket.socket.recv} but the data being I{sent} and
        I{received} only passes through various memory buffers managed by this
        object and L{MemoryIOSystem}.

        @see: L{socket.socket.recv}
        cs��|�dSr~)rC)rEry)�	datagramsrr�capture!sz_FakePort.recv.<locals>.capturei90�rcs��|dddd�Sr~)�datagramReceived)rt)�iprr�<lambda>2s�z _FakePort.recv.<locals>.<lambda>Nr)rrgr�r6rKrr�rZaddProtorr2rr@rArrIrJ)
r8�nbytesrtZreceiverr�ZudprpZetherr�Z	dataHasPIr)r�r�r�recvs(z_FakePort.recvN)rTrUrVrWr9r�rrrrr{sr{),rWrr)�errnorrrrrrrr	�collectionsr
�	functoolsrZzope.interfacer
Ztwisted.internet.protocolrZtwisted.pair.ethernetrZtwisted.pair.rawudprZtwisted.pair.iprZtwisted.pair.tuntaprrrrZtwisted.python.compatrrJrrBr#r,r.�objectr/r`rar{rrrr�<module>s2(
5H