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__/test_udp.cpython-38.pyc
U

��W[d`�@s<dZddlmZmZddlmZddlmZddlm	Z	m
Z
mZddlm
Z
mZmZmZmZmZddlmZGdd	�d	�ZGd
d�dee
j�ZGdd
�d
ee
j�ZGdd�de�ZGdd�de�ZGdd�de
j�ZGdd�dej�ZGdd�dej�Z Gdd�dej�Z!e�"ed��s$de_#de _#e�$ed��s8de!_#dS)zG
Tests for implementations of L{IReactorUDP} and L{IReactorMulticast}.
�)�division�absolute_import)�unittest)�
intToBytes)�Deferred�
gatherResults�
maybeDeferred)�protocol�reactor�error�defer�
interfaces�udp)�runtimec@s0eZdZdZdZdZdd�Zdd�Zdd�ZdS)	�MixinrNcCs
g|_dS�N)�packets��self�r�7/usr/lib/python3/dist-packages/twisted/test/test_udp.py�__init__szMixin.__init__cCs,d|_|jdk	r(|jd}|_|�d�dS�N�)�started�startedDeferred�callback�r�drrr�
startProtocols
zMixin.startProtocolcCs
d|_dSr)�stoppedrrrr�stopProtocol#szMixin.stopProtocol)	�__name__�
__module__�__qualname__rr rrrr!rrrrrsrc@seZdZdZdZdd�ZdS)�ServerNrcCs6|j�||f�|jdk	r2|jd}|_|�d�dSr�r�append�packetReceivedr)r�data�addrrrrr�datagramReceived,s
zServer.datagramReceived)r"r#r$r(�refusedr+rrrrr%'sr%c@s,eZdZdZdZdd�Zdd�Zdd�ZdS)	�ClientNrcCs2|j�|�|jdk	r.|jd}|_|�d�dSrr&)rr)rrrrr+9s
zClient.datagramReceivedcCs,|jdk	r"|jd}|_|�|�||_dSr)r�errback�failure)rr/rrrr�connectionFailed?s

zClient.connectionFailedcCs2|jdk	r(|jd}|_|�t�d��d|_dS�NZyupr�rr.r�ConnectionRefusedErrorr,rrrr�connectionRefusedEs
zClient.connectionRefused)r"r#r$r(r,r+r0r4rrrrr-4s
r-c@seZdZdd�ZdS)�
GoodClientcCs2|jdk	r(|jd}|_|�t�d��d|_dSr1r2rrrrr4Ns
zGoodClient.connectionRefusedN)r"r#r$r4rrrrr5Lsr5c@seZdZdZdS)�BadClientErrorzf
    Raised by BadClient at the end of every datagramReceived call to try and
    screw stuff up.
    N)r"r#r$�__doc__rrrrr6Vsr6c@s$eZdZdZdZdd�Zdd�ZdS)�	BadClientz�
    A DatagramProtocol which always raises an exception from datagramReceived.
    Used to test error handling behavior in the reactor for that method.
    NcCs
||_dS)ze
        Set the Deferred which will be called back when datagramReceived is
        called.
        N)rrrrr�setDeferredeszBadClient.setDeferredcCs.|jdk	r"|jd}|_|�|�td��dS)NzApplication code is very buggy!)rrr6)r�bytesr*rrrrr+ms

zBadClient.datagramReceived)r"r#r$r7rr9r+rrrrr8^sr8c@sTeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�ZdS)�UDPTestscs<t�}t��}|_tjd|dd����fdd�}|�|�S)zu
        The C{type} of the host address of a listening L{DatagramProtocol}'s
        transport is C{"UDP"}.
        r�	127.0.0.1�Z	interfacecs���}��|jd����S)NZUDP)�getHost�assertEqual�type�
stopListening)�ignoredr*��prrr�	cbStartedsz+UDPTests.test_oldAddress.<locals>.cbStarted�r%rrrr
�	listenUDP�addCallback)r�serverrrErrCr�test_oldAddressws
zUDPTests.test_oldAddresscsRt��t��}�_tjd�dd�����fdd�}��fdd�}|�|��|�S)z�
        The L{DatagramProtocol}'s C{startProtocol} and C{stopProtocol}
        methods are called when its transports starts and stops listening,
        respectively.
        rr<r=cs$���jd����jd����S)Nrr)r?rr rA�rB��port1rrIrrrE�sz*UDPTests.test_startStop.<locals>.cbStartedcs���jd�dSr)r?r rK)rrIrr�	cbStopped�sz*UDPTests.test_startStop.<locals>.cbStoppedrF)rrrErNrrLr�test_startStop�szUDPTests.test_startStopcsFt��t��}�_tjd�dd�}dd����fdd�}|��|�S)zr
        Re-listening with the same L{DatagramProtocol} re-invokes the
        C{startProtocol} callback.
        rr<r=cSs|��Sr�rA)rB�portrrrrE�sz'UDPTests.test_rebind.<locals>.cbStartedcs*t��}�_tjd�dd�}|��|�S�Nrr<r=)rrrr
rGrH)rBrrD�rErIrrrN�sz'UDPTests.test_rebind.<locals>.cbStoppedrF)rrrDrNrrSr�test_rebind�szUDPTests.test_rebindcsXt��t��}�_tjd�dd�����fdd�}|�|��fdd�}|�|�|S)z�
        A L{CannotListenError} exception is raised when attempting to bind a
        second protocol instance to an already bound port
        rr<r=cs>������j���t�}�jtjtj���j	|dd�dS)Nr<r=)
r?r>�	transportr%�assertRaisesrZCannotListenErrorr
rGrQ)rBZserver2�rQrrIrrrE�s�z*UDPTests.test_bindError.<locals>.cbStartedcs���SrrPrK�rQrr�
cbFinished�sz+UDPTests.test_bindError.<locals>.cbFinishedrF)rrrErYrrWr�test_bindError�s

zUDPTests.test_bindErrorcs�t��t��}�_tjd�dd��t��t����_���fdd�}|�|�}��fdd�}|�|����fdd	�}|�|���fd
d�}|�|�|S)z�
        Datagrams can be sent with the transport's C{write} method and
        received via the C{datagramReceived} callback method.
        rr<r=cstjd�dd��_�SrR)r
rG�port2rK)�client�
clientStartedrrr�cbServerStarted�sz2UDPTests.test_sendPackets.<locals>.cbServerStartedcs��j�d�j��j��j��}�j��}t��}�_�j�d|j|jf�ddd|j|jffg�����fdd��tj	�d�|gdd	�S)
Nr<�hello)�a)�bN�ccs6�r2t��}�_|����jj��d��|SdS�Nr)rrr(rHrU�write�pop)rBZnextClientWrite)�cbClientSendr\�clientWritesrIrrrf�s

zHUDPTests.test_sendPackets.<locals>.cbClientStarted.<locals>.cbClientSendT�ZfireOnOneErrback)
rU�connectr>rQrrr(rd�host�DeferredList)rB�cAddr�sAddrZ
serverSend)r\rI)rfrgr�cbClientStarted�s$
�

�
��z2UDPTests.test_sendPackets.<locals>.cbClientStartedcs`�j��}�j��}���jd|j|jffg�|j|jf}���jd|fd|fd|fg�dS)Nr_r`rarb)rUr>r?rrjrQ)rBrlrmZ
clientAddr�r\rrIrr�cbSendsFinished�s

���z2UDPTests.test_sendPackets.<locals>.cbSendsFinishedcs$tjt��j�t��jj�gdd�S�NTrh)rrkrrAr[rK)rMrrrrYs
��z-UDPTests.test_sendPackets.<locals>.cbFinished)r%rrrr
rGr5rH)r�
serverStartedr^rrnrprYr)r\r]rMrrIr�test_sendPackets�s



zUDPTests.test_sendPacketscs�t��t��}�_tjd�dd��t��t��}�_tjd�dd��tj||gdd�}���fdd�}|�|���fdd	�}|�|�|S)
z�
        A L{ConnectionRefusedError} exception is raised when a connection
        attempt is actively refused by the other end.

        Note: This test assumes no one is listening on port 80 UDP.
        rr<r=TrhcsZt��}�_�j�dd�td�D]&}�j�t|���j�t|�d�q$��|t	j
�S)Nr<�P�
)r<rt)rrrrUri�rangerdr�
assertFailurerr3)rBr4�irorrrE"s�z2UDPTests.test_connectionRefused.<locals>.cbStartedcs"tjt��j�t��j�gdd�Srq)rrkrrArK)rQr[rrrY0s

��z3UDPTests.test_connectionRefused.<locals>.cbFinished)	r5rrrr
rGr%rkrH)rr]rrrrErYr)r\rQr[rrIr�test_connectionRefuseds�

zUDPTests.test_connectionRefusedcCsVt�}tjd|dd�}|�tj|jjdd�|j�dd�|�t|jjdd�|�	�S)a 
        A call to the transport's connect method fails with an
        L{InvalidAddressError} when a non-IP address is passed as the host
        value.

        A call to a transport's connect method fails with a L{RuntimeError}
        when the transport is already connected.
        rr<r=Z	localhostrt)
r5r
rGrVrZInvalidAddressErrorrUri�RuntimeErrorrA)rr\rQrrr�test_badConnect:s	��zUDPTests.test_badConnectcs�t����fdd�}��|�t��tjd�dd���fdd�}��|�����tt	d���g��������fd	d
�����S)zr
        When datagramReceived raises an exception it is logged but the port
        is not disconnected.
        cs*��t�}��t|�ddt|�f�dS)z�
            Flush the exceptions which the reactor should have logged and make
            sure they're actually there.
            �z'Incorrectly found %d errors, expected 2N)�flushLoggedErrorsr6r?�len)�ignZerrsrrr�cbCompletedUs
z8UDPTests.test_datagramReceivedError.<locals>.cbCompletedrr<r=cst��j���fdd��S)z�
            Disconnect the port we started and pass on whatever was given to us
            in case it was a Failure.
            cs�Srr�r��resultrr�<lambda>f�zHUDPTests.test_datagramReceivedError.<locals>.cbCleanup.<locals>.<lambda>)rrrA�addBothr�rXr�r�	cbCleanupasz6UDPTests.test_datagramReceivedError.<locals>.cbCleanup�<cs��s(z��d�Wn���YnX���jdd�t��d��}t��}��|��j�	|�j
�jf�����fdd�}�fdd�}|�||�|�
�j�t�d	|jt�d
���dS)a6
            Send one packet to the listening BadClient.  Set up a 0.1 second
            timeout to do re-transmits in case the packet is dropped.  When two
            packets have been received by the BadClient, stop sending and let
            the finalDeferred's callbacks do some assertions.
            zNot enough packets receivedNzUDP Protocol lost its transportrcs:�����|�t��dkr0t�d�jd�n��dS)z�
                A packet arrived.  Cancel the timeout for it, record it, and
                maybe finish the test.
                r|rN)Zcancelr'r~r
�	callLaterr)�packet)�
finalDeferred�makeAttempt�succeededAttempts�timeoutCallrr�cbPacketReceived�s

zRUDPTests.test_datagramReceivedError.<locals>.makeAttempt.<locals>.cbPacketReceivedcs
��dS)a�
                The packet wasn't received quickly enough.  Try sending another
                one.  It doesn't matter if the packet for which this was the
                timeout eventually arrives: makeAttempt throws away the
                Deferred on which this function is the errback, so when
                datagramReceived callbacks, so it won't be on this Deferred, so
                it won't raise an AlreadyCalledError.
                Nr)�err)r�rr�ebPacketTimeout�s	zQUDPTests.test_datagramReceivedError.<locals>.makeAttempt.<locals>.ebPacketTimeoutg�������?z&Timed out in testDatagramReceivedError)Zfailr.ZfailIfIdenticalrUrrerrr9rdrjrQZaddCallbacksZ
addErrbackr
r�r�TimeoutError)r�ZpacketDeferredr�r�)r*�attemptsr\r�r�rr�)r�rr�vs*
��z8UDPTests.test_datagramReceivedError.<locals>.makeAttempt)
rrrHr8r
rGr�r>�listrv)rr�r�r)r*r�r\r�r�rQrr�r�test_datagramReceivedErrorNs


7z#UDPTests.test_datagramReceivedErrorcCsRGdd�d�}t�dt��}|�|_|�dd�|�|jg�}|�t|�d�dS)ai
        C{'<broadcast>'} is an alternative way to say C{'255.255.255.255'}
        ({socket.gethostbyname("<broadcast>")} returns C{'255.255.255.255'}),
        so because it becomes a valid IP address, no deprecation warning about
        passing hostnames to L{twisted.internet.udp.Port.write} needs to be
        emitted by C{write()} in this case.
        c@seZdZdd�ZdS)z6UDPTests.test_NoWarningOnBroadcast.<locals>.fakeSocketcSsdSrr)rZfooZbarrrr�sendto�sz=UDPTests.test_NoWarningOnBroadcast.<locals>.fakeSocket.sendtoN)r"r#r$r�rrrr�
fakeSocket�sr�rstest)z<broadcast>i�N)	rZPortr%ZsocketrdZ
flushWarnings�test_NoWarningOnBroadcastr?r~)rr�rD�warningsrrrr��sz"UDPTests.test_NoWarningOnBroadcastN)r"r#r$rJrOrTrZrsryr{r�r�rrrrr;usK+cr;c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�ReactorShutdownInteractionTestsz!Test reactor shutdown interactioncCs t�|_tjd|jdd�|_dS)zStart a UDP portrr<r=N)r%rIr
rGrQrrrr�setUp�sz%ReactorShutdownInteractionTests.setUpcCs
|j��S)zStop the UDP port)rQrArrrr�tearDown�sz(ReactorShutdownInteractionTests.tearDowncsht���t��}�j_��fdd�}|�|��fdd�}��|��jj�dd�jj��jf��S)z0Test reactor shutdown while in a recvfrom() loopcs �jj��t�d�jd�dSrc)rIrUZconnectionLostr
r�rrK�Zfinishedrrr�pktRece�szQReactorShutdownInteractionTests.testShutdownFromDatagramReceived.<locals>.pktRececs���dSr)r}rKrrr�flushErrors�szUReactorShutdownInteractionTests.testShutdownFromDatagramReceived.<locals>.flushErrorss@r<)	rrrIr(rHrUrdr>rQ)r�prr�r�rr�r� testShutdownFromDatagramReceived�s

�z@ReactorShutdownInteractionTests.testShutdownFromDatagramReceivedN)r"r#r$r7r�r�r�rrrrr��sr�c@s~eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	e
j��rZe
j�
�sZde	_dd�Zdd�Ze
j��rzde_dS)�MulticastTestscCsNt�|_t�|_t�d|j�|_t�d|j�|_|jj�	d|jj�
�j�dS)Nrr<)r%rIr-r\r
�listenMulticastrMr[rUrir>rQrrrrr��s�zMulticastTests.setUpcCstt|jj�t|jj�g�Sr)rrrMrAr[rrrrr�s

�zMulticastTests.tearDowncCsF|j|jfD]4}|�|j��d�|j�d�|�|j��d�qdS)Nrr|)r\rIr?rUZgetTTLZsetTTL)r�orrr�testTTL	szMulticastTests.testTTLcsx���jj��d��jj����jj�d�}��fdd�}|�|���fdd�}|�|��fdd�}|�|�|S)	zv
        Test that after loopback mode has been set, multicast packets are
        delivered to their sender.
        r�225.0.0.250cs(t�}�j_�jj�dd�jf�|S)Nr_r��rrIr(rUrdrQ�rBr�r*rrr�cbJoinedsz.MulticastTests.test_loopback.<locals>.cbJoinedcsf��t�jj�d��jj�d����jj��d��jj�dd�jf�t	�}t
�d|jd�|S)Nrrr_r�)
r?r~rIrrUZsetLoopbackMode�getLoopbackModerdrQrr
r�rr�r�rr�cbPacketsz.MulticastTests.test_loopback.<locals>.cbPacketcs��t�jj�d�dSr)r?r~rIrrKrrr�
cbNoPacket+sz0MulticastTests.test_loopback.<locals>.cbNoPacket)r?rIrUr�r>�	joinGrouprH)r�joinedr�r�r�rr�r�
test_loopbacks



zMulticastTests.test_loopbackcsj���jj��d����jj��d��jj�d�}�jj�d�}t||g�}�fdd�}|�|�|S)zK
        Test C{getOutgoingInterface} and C{setOutgoingInterface}.
        z0.0.0.0r<cs,���jj��d����jj��d�dS)Nr<)r?r\rU�getOutgoingInterfacerIrKrrr�cbInterfaces?s
�
�z3MulticastTests.test_interface.<locals>.cbInterfaces)r?r\rUr�rIZsetOutgoingInterfacerrH)r�d1�d2r�r�rrr�test_interface2s
�
�
zMulticastTests.test_interfacecsT�jj�d�}�fdd�}|�|��fdd�}|�|��fdd�}|�|�|S)zE
        Test that multicast a group can be joined and left.
        r�cs�jj�d�S�Nr�)r\rU�
leaveGrouprKrrr�clientJoinedNsz3MulticastTests.test_joinLeave.<locals>.clientJoinedcs�jj�d�Sr�)rIrUr�rKrrr�
clientLeftRsz1MulticastTests.test_joinLeave.<locals>.clientLeftcs�jj�d�Sr�)rIrUr�rKrrr�serverJoinedVsz3MulticastTests.test_joinLeave.<locals>.serverJoined)r\rUr�rH)rrr�r�r�rrr�test_joinLeaveHs


zMulticastTests.test_joinLeavecCs|�|jj�d�tj�S)z�
        Test that an attempt to join an address which is not a multicast
        address fails with L{error.MulticastJoinError}.
        r<)rwr\rUr�rZMulticastJoinErrorrrrr�test_joinFailure]s�zMulticastTests.test_joinFailurez2Windows' UDP multicast is not yet fully supported.csvt��t�d����jj����jj�d�}���fdd�}|�|��fdd�}|�|��fdd�}|�|�|S)	zn
        Test that a multicast group can be joined and messages sent to and
        received from it.
        rr�cs&t�}�j_�j�dd�jf�|S)N�hello worldr�r�r�)r*�crrrr�wsz/MulticastTests.test_multicast.<locals>.cbJoinedcs���jjddd�dS�Nrr�)r?rIrrKrrrr�}sz/MulticastTests.test_multicast.<locals>.cbPacketcs t�j�}|��fdd��|S)Ncs�Srrr���passthroughrrr��r�z@MulticastTests.test_multicast.<locals>.cleanup.<locals>.<lambda>)rrArH�r�r�)rDr�r�cleanup�s
z.MulticastTests.test_multicast.<locals>.cleanup)r%r
r�rIrUr>r�rH)rr�r�r�r�r)r*r�rDrr�test_multicastls


zMulticastTests.test_multicastcs�t��tjd�dd�����j�t��tj��dd��d�t�jj�����������g�}����fdd�}|�	|����fdd�}|�	|���fd	d
�}|�
|�|S)z�
        Test that multiple sockets can listen on the same multicast port and
        that they both receive multicast messages directed to that address.
        rT)ZlistenMultipler�cs6t�}�_t�}�_�j�d��f�t||g�S)Nr�)rr(rUrdr)rBr�r�)�firstClient�portno�secondClient�theGrouprrr��sz5MulticastTests.test_multiListen.<locals>.serverJoinedcs0���jddd����jddd�dSr�)r?rrK)r�r�rrr�
gotPackets�sz3MulticastTests.test_multiListen.<locals>.gotPacketscs.tt�j�t�j�g�}|��fdd��|S)Ncs�Srrr�r�rrr��r�zBMulticastTests.test_multiListen.<locals>.cleanup.<locals>.<lambda>)rrrArHr�)�	firstPort�
secondPortr�rr��s�z0MulticastTests.test_multiListen.<locals>.cleanup)r%r
r�r>rQrrIrUr�rHr�)rr�r�r�r�r)r�r�r�r�r�rr�r�test_multiListen�s2�
��


zMulticastTests.test_multiListenzjon non-linux platforms it appears multiple processes can listen, but not multiple sockets in same process?N)r"r#r$r�r�r�r�r�r�r�r�platformZ	isWindowsZisVista�skipr�r�rrrrr��s
"	�)
r�Nz!This reactor does not support UDPz'This reactor does not support multicast)%r7Z
__future__rrZ
twisted.trialrZtwisted.python.compatrZtwisted.internet.deferrrrZtwisted.internetr	r
rrr
rZtwisted.pythonrrZDatagramProtocolr%ZConnectedDatagramProtocolr-r5�	Exceptionr6r8ZTestCaser;r�r�ZIReactorUDPr�ZIReactorMulticastrrrr�<module>s. 

S1C