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


W[�B�@sbdZddlmZmZeZddlZddlmZddl	m
Z
ddlmZddl
mZmZddlmZdd	lmZmZdd
lmZmZmZmZddlmZmZddlmZdd
lm Z m!Z!ddl"m#Z#m$Z$ddl%m&Z&m'Z'ddl(m)Z)dd�Z*e*�Z+dd�Z,Gdd�de �Z-Gdd�de.�Z/Gdd�dee/e-�Z0Gdd�dee/e-�Z1e2��3e0�4��e2��3e1�4��dS)zU
Tests for implementations of L{IReactorUDP} and the UDP parts of
L{IReactorSocket}.
�)�division�absolute_importN)�implementer)�verifyObject)�context)�ILogContext�err)�ReactorBuilder)�Deferred�
maybeDeferred)�ILoggingContext�IListeningPort�IReactorUDP�IReactorSocket)�IPv4Address�IPv6Address)�DatagramProtocol)�LogObserverMixin�findFreePort)�defer�error)�Server�
GoodClient)�SkipTestcCsNd}d}zt�tj�}|�d�d}Wntjk
r<YnX|rJ|��|S)z5 Returns True if the system can bind an IPv6 address.NF)�::1rT)�socket�AF_INET6�bindr�close)ZsockZhas_ipv6�r�@/usr/lib/python3/dist-packages/twisted/internet/test/test_udp.py�	_has_ipv6"s
r!cCsts
d|_|S)Nz.Does not work on systems without IPv6 support.)�HAS_IPV6�skip)�frrr �skipWithoutIPv66sr%c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�DatagramTransportTestsMixinzP
    Mixin defining tests which apply to any port/datagram based transport.
    cCsd|��}|��}tt�Gdd�dt��}|�}|�||�}d|��jf}|�|f|dd�dS)zu
        When a port starts, a message including a description of the associated
        protocol is logged.
        c@seZdZdd�ZdS)zQDatagramTransportTestsMixin.test_startedListeningLogMessage.<locals>.SomeProtocolcSsdS)NzCrazy Protocolr��selfrrr �	logPrefixKsz[DatagramTransportTestsMixin.test_startedListeningLogMessage.<locals>.SomeProtocol.logPrefixN)�__name__�
__module__�__qualname__r)rrrr �SomeProtocolIsr-zCrazy Protocol starting on %dr�messageN)	�observe�buildReactorrrr�getListeningPort�getHost�port�assertEqual)r(�loggedMessages�reactorr-�protocol�p�expectedMessagerrr �test_startedListeningLogMessageAsz;DatagramTransportTestsMixin.test_startedListeningLogMessagecsx|���|���|��t���d���jf}�fdd�����fdd�}��|�|���|�|f�dd�dS)	z�
        When a connection is lost a message is logged containing an
        address identifying the port and the fact that it was closed.
        z(UDP Port %s Closed)cs���dS�N��stop��ignored�r6rr �stopReactor^szNDatagramTransportTestsMixin.test_connectionLostLogMessage.<locals>.stopReactorcs�dd�=t�j����dSr;)r�
stopListening�addCallbackr)r5r8rArr �doStopListeningas
zRDatagramTransportTestsMixin.test_connectionLostLogMessage.<locals>.doStopListeningrr.N)	r/r0r1rr2r3ZcallWhenRunning�
runReactorr4)r(r9rDr)r5r8r6rAr �test_connectionLostLogMessageTs

z9DatagramTransportTestsMixin.test_connectionLostLogMessagecs`G�fdd�dt�}|���|�}|��|�|���|�|j�|�|j�|�|j�dS)z�
        L{DatagramProtocol.stopProtocol} is called asynchronously (ie, not
        re-entrantly) when C{stopListening} is used to stop the datagram
        transport.
        cs0eZdZdZdZdZdZdd�Z�fdd�ZdS)zVDatagramTransportTestsMixin.test_stopProtocolScheduling.<locals>.DisconnectingProtocolFcSs d|_d|_|j��d|_dS)NTF)�started�inStartProtocol�	transportrBr'rrr �
startProtocolxs
zdDatagramTransportTestsMixin.test_stopProtocolScheduling.<locals>.DisconnectingProtocol.startProtocolcsd|_|j|_���dS)NT)�stoppedrH�stoppedInStartr=r'r@rr �stopProtocol~szcDatagramTransportTestsMixin.test_stopProtocolScheduling.<locals>.DisconnectingProtocol.stopProtocolN)	r*r+r,rGrKrHrLrJrMrr@rr �DisconnectingProtocolqsrNN)	rr0r1rE�
assertTruerGrKZassertFalserL)r(rNr7rr@r �test_stopProtocolSchedulingks
z7DatagramTransportTestsMixin.test_stopProtocolSchedulingN)r*r+r,�__doc__r:rFrPrrrr r&=sr&c@s�eZdZdZdd�Zdd�Zedd��Zdd	�Zd
d�Z	dd
�Z
dd�Zdd�Zedd��Z
edd��Zdd�Zedd��Zedd��Zdd�Zdd�Zd S)!�UDPPortTestsMixinzY
    Tests for L{IReactorUDP.listenUDP} and
    L{IReactorSocket.adoptDatagramPort}.
    cCs*|��}|�|t��}|�tt|��dS)zY
        L{IReactorUDP.listenUDP} returns an object providing L{IListeningPort}.
        N)r0r1rrOrr
�r(r6r3rrr �test_interface�sz UDPPortTestsMixin.test_interfacecCsHttjd�\}}|��}|j|t�||d�}|�|��td||��dS)z�
        L{IListeningPort.getHost} returns an L{IPv4Address} giving a
        dotted-quad of the IPv4 address the port is listening on as well as
        the port number.
        )�type)r3�	interfaceZUDPN)	rr�
SOCK_DGRAMr0r1rr4r2r)r(�hostZ
portNumberr6r3rrr �test_getHost�s�
�zUDPPortTestsMixin.test_getHostcCs@|��}|j|t�dd�}|��}|�|jd�|�|t�dS)zr
        L{IListeningPort.getHost} returns an L{IPv6Address} when listening on
        an IPv6 interface.
        r�rVN)r0r1rr2r4rXZassertIsInstancer)r(r6r3�addrrrr �test_getHostIPv6�s�z"UDPPortTestsMixin.test_getHostIPv6cCs&|��}|jtj|jt�ddd�dS)z�
        An L{InvalidAddressError} is raised when trying to listen on an address
        that isn't a valid IPv4 or IPv6 address.
        rzexample.comrZN)r0�assertRaisesr�InvalidAddressError�	listenUDPr)r(r6rrr �test_invalidInterface�s�z'UDPPortTestsMixin.test_invalidInterfacecs�Gdd�dt�}����|d�}|j}���|�}|��}�fdd�}|�|�|�t�|��fdd��|�dd	|j	f���
��d
S)z�
        Datagram transports implement L{ILoggingContext.logPrefix} to return a
        message reflecting the protocol they are running.
        c@s$eZdZdd�Zdd�Zdd�ZdS)zIUDPPortTestsMixin.test_logPrefix.<locals>.CustomLogPrefixDatagramProtocolcSs||_t�|_dSr;)�_prefixr
�system)r(�prefixrrr �__init__�szRUDPPortTestsMixin.test_logPrefix.<locals>.CustomLogPrefixDatagramProtocol.__init__cSs|jSr;)rar'rrr r)�szSUDPPortTestsMixin.test_logPrefix.<locals>.CustomLogPrefixDatagramProtocol.logPrefixcSs.|jdk	r*|j}d|_|�t�t�d�dS)Nrb)rb�callbackr�getr)r(�bytesr[rbrrr �datagramReceived�s
zZUDPPortTestsMixin.test_logPrefix.<locals>.CustomLogPrefixDatagramProtocol.datagramReceivedN)r*r+r,rdr)rhrrrr �CustomLogPrefixDatagramProtocol�srizCustom Datagramscs��d|�dS)NzCustom Datagrams (UDP))r4)rbr'rr �	gotSystem�sz3UDPPortTestsMixin.test_logPrefix.<locals>.gotSystemcs���Sr;r<r>r@rr �<lambda>��z2UDPPortTestsMixin.test_logPrefix.<locals>.<lambda>s
some bytes�	127.0.0.1N)rr0rbr1r2rC�
addErrbackr�writer3rE)r(rir7�dr3�addressrjr)r6r(r �test_logPrefix�s

z UDPPortTestsMixin.test_logPrefixcs�Gdd�dt�}����|�}|j}���|�}|��}d���fdd�}|�|�|�t�|��fdd��|��d|j	f���
��d	S)
zH
        Write a sequence of L{bytes} to a L{DatagramProtocol}.
        c@seZdZdd�Zdd�ZdS)zDUDPPortTestsMixin.test_writeSequence.<locals>.SimpleDatagramProtocolcSst�|_dSr;)r
rr'rrr rd�szMUDPPortTestsMixin.test_writeSequence.<locals>.SimpleDatagramProtocol.__init__cSs|j�|�dSr;)rre)r(�datar[rrr rh�szUUDPPortTestsMixin.test_writeSequence.<locals>.SimpleDatagramProtocol.datagramReceivedN)r*r+r,rdrhrrrr �SimpleDatagramProtocol�srt)ssomesbytesstoswritecs��d���|�dS)Nrl)r4�join)rs)�dataToWriter(rr �gotData�sz5UDPPortTestsMixin.test_writeSequence.<locals>.gotDatacs���Sr;r<r>r@rr rk�rlz6UDPPortTestsMixin.test_writeSequence.<locals>.<lambda>rmN)rr0rr1r2rCrnrZ
writeSequencer3rE)r(rtr7rr3rqrwr)rvr6r(r �test_writeSequence�s

z$UDPPortTestsMixin.test_writeSequencecCs4|��}|�|t��}|�t|��j�t|��dS)zQ
        C{str()} on the listening port object includes the port number.
        N)r0r1r�assertIn�strr2r3rSrrr �test_strszUDPPortTestsMixin.test_strcCs4|��}|�|t��}|�t|��j�t|��dS)zR
        C{repr()} on the listening port object includes the port number.
        N)r0r1rry�reprr2r3rzrSrrr �	test_reprszUDPPortTestsMixin.test_reprcs�|���t��t��}�_|j��dd�t��t��}�_|j��dd��j��}��fdd�}�fdd�}t�	||g�}|�
|�|�
|�|�t�|�
���jd}|�|d|j|jff�d	S)
zS
        Writing to an IPv6 UDP socket on the loopback interface succeeds.
        rrZcs,�j�dd�j��jf�t��}�_|S)��
            Send a datagram from the client once it's started.

            @param ignored: a list of C{[None, None]}, which is ignored
            @returns: a deferred which fires when the server has received a
                datagram.
            �spamr)rIror2r3rr
�packetReceived�r?ZserverReceived��client�serverrr �cbClientStarted#s�zDUDPPortTestsMixin.test_writeToIPv6Interface.<locals>.cbClientStartedcs���dS�z�
            Stop the reactor after a datagram is received.

            @param ignored: L{None}, which is ignored
            @returns: L{None}
            Nr<r>r@rr �cbServerReceived0szEUDPPortTestsMixin.test_writeToIPv6Interface.<locals>.cbServerReceivedrrN�r0rrr
ZstartedDeferredr1rrIr2Z
gatherResultsrCrnrrEZpacketsr4rXr3�r(Z
serverStartedZ
clientStartedZcAddrr�r�rpZpacketr�r�r6r�r �test_writeToIPv6Interfaces"

	




z+UDPPortTestsMixin.test_writeToIPv6Interfacecs�|���t��t��}�_|j��dd�t��t��}�_|j��dd��j��}��fdd�}�fdd�}t�	||g�}|�
|�|�
|�|�t�|�
���jd}|�|d|j|jff�d	S)
z�
        An IPv6 address can be passed as the C{interface} argument to
        L{listenUDP}. The resulting Port accepts IPv6 datagrams.
        rrZcs4�j�d�j��j��j�d�t��}�_|S)r~rr)rI�connectr2r3rorr
r�r�r�rr r�Ss	zMUDPPortTestsMixin.test_connectedWriteToIPv6Interface.<locals>.cbClientStartedcs���dSr�r<r>r@rr r�aszNUDPPortTestsMixin.test_connectedWriteToIPv6Interface.<locals>.cbServerReceivedrrNr�r�rr�r �"test_connectedWriteToIPv6InterfaceCs"






z4UDPPortTestsMixin.test_connectedWriteToIPv6InterfacecCs.|��}|�|t��}|�tj|jdd�dS)zn
        Writing to a hostname instead of an IP address will raise an
        L{InvalidAddressError}.
        �spam)�example.invalid�N�r0r1rr]rr^rorSrrr �/test_writingToHostnameRaisesInvalidAddressErrorus�zAUDPPortTestsMixin.test_writingToHostnameRaisesInvalidAddressErrorcCs2|��}|j|t�dd�}|�tj|jdd�dS)�l
        Writing to an IPv6 address on an IPv4 socket will raise an
        L{InvalidAddressError}.
        rmrZr�)rr�Nr�rSrrr �1test_writingToIPv6OnIPv4RaisesInvalidAddressError�s��zCUDPPortTestsMixin.test_writingToIPv6OnIPv4RaisesInvalidAddressErrorcCs2|��}|j|t�dd�}|�tj|jdd�dS)r�rrZr�)rmr�Nr�rSrrr �1test_writingToIPv4OnIPv6RaisesInvalidAddressError�s��zCUDPPortTestsMixin.test_writingToIPv4OnIPv6RaisesInvalidAddressErrorcCs.|��}|�|t��}|�tj|jdd�dS)zq
        Connecting to a hostname instead of an IP address will raise an
        L{InvalidAddressError}.
        r�r�N)r0r1rr]rr^r�rSrrr �2test_connectingToHostnameRaisesInvalidAddressError�s�zDUDPPortTestsMixin.test_connectingToHostnameRaisesInvalidAddressErrorcCs2|��}|�|t��}|�d�|�|���dS)zk
        L{IListeningPort.setBroadcastAllowed} sets broadcast to be allowed
        on the socket.
        TN)r0r1rZsetBroadcastAllowedrOZgetBroadcastAllowedrSrrr �test_allowBroadcast�s
z%UDPPortTestsMixin.test_allowBroadcastN)r*r+r,rQrTrYr%r\r`rrrxr{r}r�r�r�r�r�r�r�rrrr rR�s*	

#		
.
1

rRc@s eZdZdZefZddd�ZdS)	�UDPServerTestsBuilderzM
    Run L{UDPPortTestsMixin} tests using newly created UDP
    sockets.
    r�� cCs|j||||d�S)aB
        Get a UDP port from a reactor.

        @param reactor: A reactor used to build the returned
            L{IListeningPort} provider.
        @type reactor: L{twisted.internet.interfaces.IReactorUDP}

        @see: L{twisted.internet.IReactorUDP.listenUDP} for other
            argument and return types.
        )rV�
maxPacketSize)r_)r(r6r7r3rVr�rrr r1�s
�z&UDPServerTestsBuilder.getListeningPortN)rr�r�)r*r+r,rQr�requiredInterfacesr1rrrr r��s
�r�c@s eZdZdZefZddd�ZdS)	�UDPFDServerTestsBuilderzC
    Run L{UDPPortTestsMixin} tests using adopted UDP sockets.
    rr�r�c	Cs�t�|�r�d|kr.tj}t�||�dd}ntj}||f}t�|tj�}|�|�|�d�z|�|�	�|j||�W�S|�	�|�
�Xnt
d��dS)a�
        Get a UDP port from a reactor, wrapping an already-initialized file
        descriptor.

        @param reactor: A reactor used to build the returned
            L{IListeningPort} provider.
        @type reactor: L{twisted.internet.interfaces.IReactorSocket}

        @param port: A port number to which the adopted socket will be
            bound.
        @type port: C{int}

        @param interface: The local IPv4 or IPv6 address to which the
            adopted socket will be bound.  defaults to '', ie all IPv4
            addresses.
        @type interface: C{str}

        @see: L{twisted.internet.IReactorSocket.adoptDatagramPort} for other
            argument and return types.
        �:r�Fz'Reactor does not provide IReactorSocketN)rZ
providedByrrZgetaddrinfoZAF_INETrWrZsetblocking�filenorZadoptDatagramPortZfamilyr)	r(r6r7r3rVr�ZdomainrqZportSockrrr r1�s&


�z(UDPFDServerTestsBuilder.getListeningPortN)rr�r�)r*r+r,rQrr�r1rrrr r��s
�r�)5rQZ
__future__rrrUZ
__metaclass__rZzope.interfacerZzope.interface.verifyrZtwisted.pythonrZtwisted.python.logrrZ#twisted.internet.test.reactormixinsr	Ztwisted.internet.deferr
rZtwisted.internet.interfacesrr
rrZtwisted.internet.addressrrZtwisted.internet.protocolrZ&twisted.internet.test.connectionmixinsrrZtwisted.internetrrZtwisted.test.test_udprrZtwisted.trial.unittestrr!r"r%r&�objectrRr�r��globals�updateZmakeTestCaseClassesrrrr �<module>sBQ&��7