Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib64/python3.7/site-packages/twisted/test/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib64/python3.7/site-packages/twisted/test/__pycache__/test_udp.cpython-37.pyc

B

;��]�a�@sDdZddlmZmZddlZddlmZddlmZddl	m
Z
mZmZddl
mZmZmZmZmZmZddlmZGd	d
�d
�ZGdd�deej�ZGd
d�deej�ZGdd�de�ZGdd�de�ZGdd�dej�ZGdd�dej�Z Gdd�dej�Z!Gdd�dej�Z"e�#ed��s,de _$de!_$e�%ed��s@de"_$dS)zG
Tests for implementations of L{IReactorUDP} and L{IReactorMulticast}.
�)�division�absolute_importN)�unittest)�
intToBytes)�Deferred�
gatherResults�
maybeDeferred)�protocol�reactor�error�defer�
interfaces�udp)�runtimec@s0eZdZdZdZdZdd�Zdd�Zdd�ZdS)	�MixinrNcCs
g|_dS)N)�packets)�self�r�H/opt/alt/python37/lib64/python3.7/site-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|_dS)Nr)�stopped)rrrr�stopProtocol%szMixin.stopProtocol)	�__name__�
__module__�__qualname__rrrrrrrrrrrsrc@seZdZdZdZdd�ZdS)�ServerNrcCs6|j�||f�|jdk	r2|jd}|_|�d�dS)N)r�append�packetReceivedr)r�data�addrrrrr�datagramReceived.s
zServer.datagramReceived)rrr r#�refusedr&rrrrr!)sr!c@s,eZdZdZdZdd�Zdd�Zdd�ZdS)	�ClientNrcCs2|j�|�|jdk	r.|jd}|_|�d�dS)N)rr"r#r)rr$rrrrr&;s
zClient.datagramReceivedcCs,|jdk	r"|jd}|_|�|�||_dS)N)r�errback�failure)rr*rrrr�connectionFailedAs

zClient.connectionFailedcCs2|jdk	r(|jd}|_|�t�d��d|_dS)N�yupr)rr)r�ConnectionRefusedErrorr')rrrrr�connectionRefusedGs
zClient.connectionRefused)rrr r#r'r&r+r.rrrrr(6s
r(c@seZdZdd�ZdS)�
GoodClientcCs2|jdk	r(|jd}|_|�t�d��d|_dS)Nr,r)rr)rr-r')rrrrrr.Ps
zGoodClient.connectionRefusedN)rrr r.rrrrr/Nsr/c@seZdZdZdS)�BadClientErrorzf
    Raised by BadClient at the end of every datagramReceived call to try and
    screw stuff up.
    N)rrr �__doc__rrrrr0Xsr0c@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)r)rrrrr�setDeferredgszBadClient.setDeferredcCs.|jdk	r"|jd}|_|�|�td��dS)NzApplication code is very buggy!)rrr0)r�bytesr%rrrrr&os

zBadClient.datagramReceived)rrr r1rr3r&rrrrr2`sr2c@sjeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Ze	j
�d
�dkrNde_dd�Z
dd�Zdd�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"}.
        rz	127.0.0.1)�	interfacecs���}��|jd����S)NZUDP)�getHost�assertEqual�type�
stopListening)�ignoredr%)�prrr�	cbStarted�sz+UDPTests.test_oldAddress.<locals>.cbStarted)r!rrrr
�	listenUDP�addCallback)r�serverrr=r)r<rr�test_oldAddressys
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.
        rz	127.0.0.1)r6cs$���jd����jd����S)Nrr)r8rrr:)r;)�port1rr@rrr=�sz*UDPTests.test_startStop.<locals>.cbStartedcs���jd�dS)Nr)r8r)r;)rr@rr�	cbStopped�sz*UDPTests.test_startStop.<locals>.cbStopped)r!rrrr
r>r?)rrr=rCr)rBrr@r�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.
        rz	127.0.0.1)r6cSs|��S)N)r:)r;�portrrrr=�sz'UDPTests.test_rebind.<locals>.cbStartedcs*t��}�_tjd�dd�}|��|�S)Nrz	127.0.0.1)r6)rrrr
r>r?)r;rr<)r=r@rrrC�sz'UDPTests.test_rebind.<locals>.cbStopped)r!rrrr
r>r?)rrr<rCr)r=r@r�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
        rz	127.0.0.1)r6cs>������j���t�}�jtjtj���j	|dd�dS)Nz	127.0.0.1)r6)
r8r7�	transportr!�assertRaisesrZCannotListenErrorr
r>rE)r;Zserver2)rErr@rrr=�sz*UDPTests.test_bindError.<locals>.cbStartedcs���S)N)r:)r;)rErr�
cbFinished�sz+UDPTests.test_bindError.<locals>.cbFinished)r!rrrr
r>r?)rrr=rIr)rErr@r�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.
        rz	127.0.0.1)r6cstjd�dd��_�S)Nrz	127.0.0.1)r6)r
r>�port2)r;)�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)
Nz	127.0.0.1shello)�a)�bN�ccs6�r2t��}�_|����jj��d��|SdS)Nr)rrr#r?rG�write�pop)r;ZnextClientWrite)�cbClientSendrL�clientWritesr@rrrT�s

zHUDPTests.test_sendPackets.<locals>.cbClientStarted.<locals>.cbClientSendT)�fireOnOneErrback)
rG�connectr7rErrr#rR�host�DeferredList)r;�cAddr�sAddrZ
serverSend)rLr@)rTrUr�cbClientStarted�s


z2UDPTests.test_sendPackets.<locals>.cbClientStartedcs`�j��}�j��}���jd|j|jffg�|j|jf}���jd|fd|fd|fg�dS)NshellorOrPrQ)rGr7r8rrXrE)r;rZr[Z
clientAddr)rLrr@rr�cbSendsFinished�s

z2UDPTests.test_sendPackets.<locals>.cbSendsFinishedcs$tjt��j�t��jj�gdd�S)NT)rV)rrYrr:rK)r;)rBrrrrIs
z-UDPTests.test_sendPackets.<locals>.cbFinished)r!rrrr
r>r/r?)r�
serverStartedrNrr\r]rIr)rLrMrBrr@r�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.
        rz	127.0.0.1)r6T)rVcs^t��}�_�j�dd�x2td�D]&}�j�t|���j�t|�d�q&W��|t	j
�S)Nz	127.0.0.1�P�
)z	127.0.0.1r`)rrrrGrW�rangerRr�
assertFailurerr-)r;r.�i)rLrr@rrr=$sz2UDPTests.test_connectionRefused.<locals>.cbStartedcs"tjt��j�t��j�gdd�S)NT)rV)rrYrr:)r;)rErKrrrI2s
z3UDPTests.test_connectionRefused.<locals>.cbFinished)	r/rrrr
r>r!rYr?)rrMr^rr=rIr)rLrErKrr@r�test_connectionRefuseds

zUDPTests.test_connectionRefused�INFRASTRUCTURE�AZUREPIPELINESz"Hangs on Pipelines due to firewallcCsVt�}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.
        rz	127.0.0.1)r6Z	localhostr`)
r/r
r>rHrZInvalidAddressErrorrGrW�RuntimeErrorr:)rrLrErrr�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)�flushLoggedErrorsr0r8�len)�ignZerrs)rrr�cbCompletedZs
z8UDPTests.test_datagramReceivedError.<locals>.cbCompletedrz	127.0.0.1)r6cst��j���fdd��S)z�
            Disconnect the port we started and pass on whatever was given to us
            in case it was a Failure.
            cs�S)Nr)rm)�resultrr�<lambda>k�zHUDPTests.test_datagramReceivedError.<locals>.cbCleanup.<locals>.<lambda>)rrr:�addBoth)ro)rE)ror�	cbCleanupfsz6UDPTests.test_datagramReceivedError.<locals>.cbCleanup�<cs��s(y��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.
                rjrN)�cancelr"rlr
�	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)ryrr�ebPacketTimeout�s	zQUDPTests.test_datagramReceivedError.<locals>.makeAttempt.<locals>.ebPacketTimeoutg�������?z&Timed out in testDatagramReceivedError)Zfailr)ZfailIfIdenticalrGrrSrrr3rRrXrEZaddCallbacksZ
addErrbackr
rvr�TimeoutError)rwZpacketDeferredr|r~)r%�attemptsrLrxryrrz)r{rry{s$
z8UDPTests.test_datagramReceivedError.<locals>.makeAttempt)
rrr?r2r
r>rrr7�listrb)rrnrsr)r%r�rLrxryrErrzr�test_datagramReceivedErrorSs


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>.fakeSocketcSsdS)Nr)rZfooZbarrrr�sendto�sz=UDPTests.test_NoWarningOnBroadcast.<locals>.fakeSocket.sendtoN)rrr r�rrrr�
fakeSocket�sr�rstest)z<broadcast>i�N)	rZPortr!�socketrRZ
flushWarnings�test_NoWarningOnBroadcastr8rl)rr�r<�warningsrrrr��sz"UDPTests.test_NoWarningOnBroadcastN)rrr rArDrFrJr_re�os�environ�get�skiprir�r�rrrrr5wsK*cr5c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�ReactorShutdownInteractionTestsz!Test reactor shutdown interactioncCs t�|_tjd|jdd�|_dS)zStart a UDP portrz	127.0.0.1)r6N)r!r@r
r>rE)rrrr�setUp�sz%ReactorShutdownInteractionTests.setUpcCs
|j��S)zStop the UDP port)rEr:)rrrr�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�dS)Nr)r@rGZconnectionLostr
rvr)r;)�finishedrrr�pktRece�szQReactorShutdownInteractionTests.testShutdownFromDatagramReceived.<locals>.pktRececs���dS)N)rk)r;)rrr�flushErrors�szUReactorShutdownInteractionTests.testShutdownFromDatagramReceived.<locals>.flushErrorss@z	127.0.0.1)	rrr@r#r?rGrRr7rE)rZprr�r�r)r�rr� testShutdownFromDatagramReceived�s

z@ReactorShutdownInteractionTests.testShutdownFromDatagramReceivedN)rrr r1r�r�r�rrrrr��sr�c@s�eZdZej�d�dkr&ej��r&dZ	dd�Z
dd�Zdd	�Zd
d�Z
dd
�Zdd�Zdd�Zej��rxej��sxde_	dd�Zdd�Zej��r�de_	dS)�MulticastTestsrfrgz Does not work on Azure PipelinescCsNt�|_t�|_t�d|j�|_t�d|j�|_|jj�	d|jj�
�j�dS)Nrz	127.0.0.1)r!r@r(rLr
�listenMulticastrBrKrGrWr7rE)rrrrr�szMulticastTests.setUpcCstt|jj�t|jj�g�S)N)rrrBr:rK)rrrrr�s
zMulticastTests.tearDowncCsJxD|j|jfD]4}|�|j��d�|j�d�|�|j��d�qWdS)Nrrj)rLr@r8rGZgetTTLZsetTTL)r�orrr�testTTLs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.
        rz225.0.0.250cs(t�}�j_�jj�dd�jf�|S)Nshelloz225.0.0.250)rr@r#rGrRrE)r;r)r%rrr�cbJoined$sz.MulticastTests.test_loopback.<locals>.cbJoinedcsf��t�jj�d��jj�d����jj��d��jj�dd�jf�t	�}t
�d|jd�|S)Nrrshelloz225.0.0.250)
r8rlr@rrGZsetLoopbackMode�getLoopbackModerRrErr
rvr)r;r)r%rrr�cbPacket*sz.MulticastTests.test_loopback.<locals>.cbPacketcs��t�jj�d�dS)Nr)r8rlr@r)r;)rrr�
cbNoPacket6sz0MulticastTests.test_loopback.<locals>.cbNoPacket)r8r@rGr�r7�	joinGroupr?)r�joinedr�r�r�r)r%rr�
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.0z	127.0.0.1cs,���jj��d����jj��d�dS)Nz	127.0.0.1)r8rLrG�getOutgoingInterfacer@)r;)rrr�cbInterfacesJsz3MulticastTests.test_interface.<locals>.cbInterfaces)r8rLrGr�r@ZsetOutgoingInterfacerr?)r�d1�d2ror�r)rr�test_interface=s
zMulticastTests.test_interfacecsT�jj�d�}�fdd�}|�|��fdd�}|�|��fdd�}|�|�|S)zE
        Test that multicast a group can be joined and left.
        z225.0.0.250cs�jj�d�S)Nz225.0.0.250)rLrG�
leaveGroup)r;)rrr�clientJoinedYsz3MulticastTests.test_joinLeave.<locals>.clientJoinedcs�jj�d�S)Nz225.0.0.250)r@rGr�)r;)rrr�
clientLeft]sz1MulticastTests.test_joinLeave.<locals>.clientLeftcs�jj�d�S)Nz225.0.0.250)r@rGr�)r;)rrr�serverJoinedasz3MulticastTests.test_joinLeave.<locals>.serverJoined)rLrGr�r?)rrr�r�r�r)rr�test_joinLeaveSs


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}.
        z	127.0.0.1)rcrLrGr�rZMulticastJoinError)rrrr�test_joinFailurehs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.
        rz225.0.0.250cs&t�}�j_�j�dd�jf�|S)Nshello worldz225.0.0.250)rr@r#rGrRrE)r;r)r%�crrrr��sz/MulticastTests.test_multicast.<locals>.cbJoinedcs���jjddd�dS)Nrshello world)r8r@r)r;)rrrr��sz/MulticastTests.test_multicast.<locals>.cbPacketcs t�j�}|��fdd��|S)Ncs�S)Nr)rm)�passthroughrrrp�rqz@MulticastTests.test_multicast.<locals>.cleanup.<locals>.<lambda>)rr:r?)r�ro)r<)r�r�cleanup�s
z.MulticastTests.test_multicast.<locals>.cleanup)r!r
r�r@rGr7r�r?)rr�r�r�r�r)r%r�r<rr�test_multicastws


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)ZlistenMultiplez225.0.0.250cs6t�}�_t�}�_�j�d��f�t||g�S)Nshello world)rr#rGrRr)r;r�r�)�firstClient�portno�secondClient�theGrouprrr��sz5MulticastTests.test_multiListen.<locals>.serverJoinedcs0���jddd����jddd�dS)Nrshello world)r8r)r;)r�r�rrr�
gotPackets�sz3MulticastTests.test_multiListen.<locals>.gotPacketscs.tt�j�t�j�g�}|��fdd��|S)Ncs�S)Nr)rm)r�rrrp�rqzBMulticastTests.test_multiListen.<locals>.cleanup.<locals>.<lambda>)rrr:r?)r�ro)�	firstPort�
secondPort)r�rr��s
z0MulticastTests.test_multiListen.<locals>.cleanup)r!r
r�r7rErr@rGr�r?rr)rr�r�r�r�r)r�r�r�r�r�rr�r�test_multiListen�s$



zMulticastTests.test_multiListenzjon non-linux platforms it appears multiple processes can listen, but not multiple sockets in same process?N)rrr r�r�r�r�platformZisMacOSXr�r�r�r�r�r�r�r�Z	isWindowsZisVistar�r�rrrrr��s 

"	)
r�z!This reactor does not support UDPz'This reactor does not support multicast)&r1�
__future__rrr�Z
twisted.trialrZtwisted.python.compatrZtwisted.internet.deferrrrZtwisted.internetr	r
rrr
rZtwisted.pythonrrZDatagramProtocolr!ZConnectedDatagramProtocolr(r/�	Exceptionr0r2ZTestCaser5r�r�ZIReactorUDPr�ZIReactorMulticastrrrr�<module>s0 

V1I