Ϫf.k dZddlZddlmZddlmZmZmZmZm Z m Z ddl m Z m Z mZddlmZddlmZGdd ZGd d eej(ZGd d eej,ZGddeZGddeZGddej(Zeej8e d dGddeZeej8e d dGddeZeej>e d dGddeZ y)zG Tests for implementations of L{IReactorUDP} and L{IReactorMulticast}. N)skipIf)defererror interfacesprotocolreactorudp)Deferred gatherResults maybeDeferred)runtime)TestCasec*eZdZdZdZdZdZdZdZy)MixinrNcg|_yN)packetsselfs 7/usr/lib/python3/dist-packages/twisted/test/test_udp.py__init__zMixin.__init__s  cvd|_|j&|jdc}|_|jdyyN)startedstartedDeferredcallbackrds r startProtocolzMixin.startProtocols;    +&*&:&:D #At# JJt  ,rcd|_yr)stoppedrs r stopProtocolzMixin.stopProtocol"s  r) __name__ __module__ __qualname__rr#rrr!r$rrrrs GGO rrceZdZdZdZdZy)ServerNrc|jj||f|j&|jdc}|_|jdyyrrappendpacketReceivedr)rdataaddrr s rdatagramReceivedzServer.datagramReceived*sJ T4L)    *%)%8%8$ "At" JJt  +r)r%r&r'r.refusedr1r(rrr*r*&sNGrr*c&eZdZdZdZdZdZdZy)ClientNrc|jj||j&|jdc}|_|jdyyrr,)rr/r s rr1zClient.datagramReceived5sF D!    *%)%8%8$ "At" JJt  +rct|j%|jdc}|_|j|||_yr)rerrbackfailure)rr8r s rconnectionFailedzClient.connectionFailed;s6    +&*&:&:D #At# IIg  rc|j8|jdc}|_|jtjdd|_yNyuprrr7rConnectionRefusedErrorr2rs rconnectionRefusedzClient.connectionRefusedAA    +&*&:&:D #At# IIe2259 : r)r%r&r'r.r2r1r9r?r(rrr4r41sNG  rr4ceZdZdZy) GoodClientc|j8|jdc}|_|jtjdd|_yr;r=rs rr?zGoodClient.connectionRefusedIr@rN)r%r&r'r?r(rrrBrBHsrrBceZdZdZy)BadClientErrorzf Raised by BadClient at the end of every datagramReceived call to try and screw stuff up. N)r%r&r'__doc__r(rrrErEPsrrEc eZdZdZdZdZdZy) BadClientz A DatagramProtocol which always raises an exception from datagramReceived. Used to test error handling behavior in the reactor for that method. Nc||_y)ze Set the Deferred which will be called back when datagramReceived is called. N)r rs r setDeferredzBadClient.setDeferred_s rcz|j%|jdc}|_|j|td)NzApplication code is very buggy!)r rrE)rbytesr0r s rr1zBadClient.datagramReceivedfs5 66 IAtv JJu >??r)r%r&r'rFr rJr1r(rrrHrHWs A@rrH!This reactor does not support UDPceZdZdZdZdZdZdZee jjddk(dd Z d Z d Zd Zd Zy)UDPTestsct}tjx}|_t j d|dfd}|j |S)zu The C{type} of the host address of a listening L{DatagramProtocol}'s transport is C{"UDP"}. r 127.0.0.1 interfacec|j}j|jdjS)NUDP)getHost assertEqualtype stopListening)ignoredr0prs r cbStartedz+UDPTests.test_oldAddress..cbStartedxs/99;D   TYY .??$ $rr*rr rr listenUDP addCallback)rserverr r\r[s` @rtest_oldAddresszUDPTests.test_oldAddressosJ %*^^%55F "   a; ? % }}Y''rcttjx}_t j ddfd}fd}|j |j |S)z The L{DatagramProtocol}'s C{startProtocol} and C{stopProtocol} methods are called when its transports starts and stops listening, respectively. rrQrRcjjdjjdjS)Nrr)rWrr#rY)rZport1rr`s rr\z*UDPTests.test_startStop..cbStarteds:   V^^Q /   V^^Q /&&( (rc>jjdyr)rWr#)rZrr`s r cbStoppedz*UDPTests.test_startStop..cbStoppeds   V^^Q /rr])rr r\rfrdr`s` @@rtest_startStopzUDPTests.test_startStopsZ %*^^%55F "!!!V{C )  0}}Y'33I>>rcttjx}_t j dd}dfd}|j |S)zr Re-listening with the same L{DatagramProtocol} re-invokes the C{startProtocol} callback. rrQrRc"|jSrrYrZports rr\z'UDPTests.test_rebind..cbStarteds%%' 'rctjx}_tjdd}|j |SNrrQrR)rr rrr^r_)rZr r[r\r`s rrfz'UDPTests.test_rebind..cbStoppeds<).)9 9A&!!!V{CA==A. .rr])rr r[rfr\r`s @@r test_rebindzUDPTests.test_rebindsQ %*^^%55F "   a; ? ( / }}Y**rcttjx}_t j ddfd}|j |fd}|j ||S)z A L{CannotListenError} exception is raised when attempting to bind a second protocol instance to an already bound port rrQrRcjjjjt}j t j tjjj|dy)NrQrR) rWrV transportr* assertRaisesrCannotListenErrorrr^rl)rZserver2rlrr`s rr\z*UDPTests.test_bindError..cbStartedsi   T\\^V-=-=-E-E-G HhG   ''!! ##%  rc$jSrrjrks r cbFinishedz+UDPTests.test_bindError..cbFinisheds%%' 'rr])rr r\rwrlr`s` @@rtest_bindErrorzUDPTests.test_bindErrors_ %*^^%55F "  FkB  i  ( j!rc t tjx} _t j d d t tjx_fd}|j|} fd}|j| fd}|j| fd}|j||S)z Datagrams can be sent with the transport's C{write} method and received via the C{datagramReceived} callback method. rrQrRcBtjdd_Srn)rr^port2)rZclient clientStartedrs rcbServerStartedz2UDPTests.test_sendPackets..cbServerStarteds **1f LDJ rcjjdjjjjj}jj}t j x}_jjd|j|jfddd|j|jffgfdt jd|gdS) NrQhello)a)bNccrVtjx}_|jjj j d|SyNr)rr r.r_rrwritepop)rZnextClientWrite cbClientSendr| clientWritesr`s rrzHUDPTests.test_sendPackets..cbClientStarted..cbClientSendsT>Cnn>NNOf&;#// =*F$$**L,<,.cbClientStarteds    $ $[&2B2B2J2J2L2Q2Q R$$,,.E$$,,.E161A AJ.    " "8ejj%**-E F#\D5::uzz:R3STL +%%d#Z04 rcRjj}jj}jjd|j|j ffg|j|j f}jjd|fd|fd|fgy)Nrrrr)rrrVrWrrrl)rZrr clientAddrr|rr`s rcbSendsFinishedz2UDPTests.test_sendPackets..cbSendsFinisheds$$,,.E$$,,.E   V^^x%**ejj9Q.R-S T**ejj1J    #dJ%7$ 9KL rctjtjjtjjjgdSNTr)rrr rYr{)rZrdrs rrwz-UDPTests.test_sendPackets..cbFinishedsL%%''(;(;<'' (@(@A"&  r)r*rr rrr^rBr_) r serverStartedr~r rrrwr|r}rdr`s ` @@@@rtest_sendPacketszUDPTests.test_sendPacketss 161AA .!!!V{C161AA . !  % %o 6 0 o&  o&  j!rINFRASTRUCTUREAZUREPIPELINESz"Hangs on Pipelines due to firewallc ttjx}_t j ddt tjx} _t j d dtj||gd} fd}|j|fd}|j||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. rrQrRTrcPtjx}_jj ddt dD]A}jj d|fzjj d|fzdCj|tjS)NrQP %drQr) rr rrrrranger assertFailurerr>)rZr?ir|rr`s rr\z2UDPTests.test_connectionRefused..cbStarteds9>9I I  6    $ $[" 52Y H  &&ut|4  &&ut|5FG H%%&79U9UV VrctjtjjtjjgdSrrrr rY)rZrlr{s rrwz3UDPTests.test_connectionRefused..cbFinished'sH%%''(:(:;''(;(;<"&  r) rBrr rrr^r*rr_) rr}rr r\rwr|rlr{r`s ` @@@@rtest_connectionRefusedzUDPTests.test_connectionRefuseds161AA .  FkB161AA .!!!V{C    }=PT U W i   j!rc4 t tjx} _t j d d d t tjx} _tjx} _t j d d tj||gd} fd}|j|tj||gd} fd}|j| fd }|j||S) as When a server fails to successfully read a packet the server should still be able to process future packets. The IOCP reactor had a historical problem where a failure to read caused the reactor to ignore any future reads. This test should prevent a regression. Note: This test assumes no one is listening on port 80 UDP. rrQrRsSending test packet to serverTrcjjddjjdjfy)Ns$write to port no one is listening torrQ)rrr_realPortNumber)rZr|r` serverPorttest_data_to_sends rcbClientAndServerStartedzAUDPTests.test_serverReadFailure..cbClientAndServerStartedJsE    " "79J     " "!K1K1K#L rcJjjddyrrWr)rZrr`rs rverify_server_got_dataz?UDPTests.test_serverReadFailure..verify_server_got_data]s"   V^^A.q13D ErctjtjjtjjgdSrr)rZ clientPortrs rcleanupz0UDPTests.test_serverReadFailure..cleanupbsH%%'' (@(@A'' (@(@A"&  r) rBrr rrr^r*r.rr_)rr}r serverGotDataserver_client_started_dr all_data_sentrrr|rr`rrs` @@@@@rtest_serverReadFailurezUDPTests.test_serverReadFailure3s161AA .&&q&KH <161AA .050@@ -&&q&KH "'"4"4 M *T#   ++,DE** $m 4t  F !!"89  !!'*rcbt}tjd|d}|jtj |j jdd|j jdd|jt|j jdd|jS)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. rrQrR localhostr) rBrr^rsrInvalidAddressErrorrrr RuntimeErrorrY)rr|rls rtest_badConnectzUDPTests.test_badConnectos  FkB   % %v'7'7'?'?b    b1 ,(8(8(@(@+rR!!##rcJ tjfd}j|tt j ddfd}j |jttdg  fdS)zr When datagramReceived raises an exception it is logged but the port is not disconnected. cjt}jt|ddt|fzy)z Flush the exceptions which the reactor should have logged and make sure they're actually there. z'Incorrectly found %d errors, expected 2N)flushLoggedErrorsrErWlen)ignerrsrs r cbCompletedz8UDPTests.test_datagramReceivedError..cbCompleteds: )).9D   D 1G3t9,V rrrQrRchtjjjfdS)z Disconnect the port we started and pass on whatever was given to us in case it was a Failure. cSrr()rresults rzHUDPTests.test_datagramReceivedError..cbCleanup..svr)rr rYaddBoth)rrls`r cbCleanupz6UDPTests.test_datagramReceivedError..cbCleanups( &&t'9'9:BBCUV Vr<c^s jd jjdddj dfz}t j}j|jj|jjf fd} fd}|j|||jjtjd|jt!j"d y#t$rjYwxYw) 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 transportrrcjj|tdk(r"tjdj dyy)z A packet arrived. Cancel the timeout for it, record it, and maybe finish the test. rrN)cancelr-rr callLaterr)packet finalDeferred makeAttemptsucceededAttempts timeoutCalls rcbPacketReceivedzRUDPTests.test_datagramReceivedError..makeAttempt..cbPacketReceivedsL ""$!((0()Q. %%a)?)?FMrcy)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()errrs rebPacketTimeoutzQUDPTests.test_datagramReceivedError..makeAttempt..ebPacketTimeouts  rg?z&Timed out in testDatagramReceivedError)fail Exceptionr7failIfIdenticalrrrrr rJrrrl addCallbacks addErrbackrrr TimeoutError) rpacketDeferredrrrr0attemptsr|rrrrs @rrz8UDPTests.test_datagramReceivedError..makeAttempts,II;<    $(I hll1o//F"^^-N   ~ .    " "6DIItyy+A B "   ' '(8/ J  % %m&;&; <!++&&""#KLKU!,!))+,sDD,+D,) rr r_rHrr^rrVlistr) rrrr0rr|rrrlrs ` @@@@@@@rtest_datagramReceivedErrorz#UDPTests.test_datagramReceivedErrors (   !!+.  FkB W i(||~b ?8 8 t  rcGdd}tjdt}||_|j dd|j |j g}|jt|dy)ai C{''} is an alternative way to say C{'255.255.255.255'} ({socket.gethostbyname("")} 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. ceZdZdZy)6UDPTests.test_NoWarningOnBroadcast..fakeSocketcyrr()rfoobars rsendtoz=UDPTests.test_NoWarningOnBroadcast..fakeSocket.sendtosrN)r%r&r'rr(rr fakeSocketrs rrrstest)z iN) r Portr*socketr flushWarningstest_NoWarningOnBroadcastrWr)rrr[warningss rrz"UDPTests.test_NoWarningOnBroadcastse   HHQ !< ./%%t'E'E&FG X*rN)r%r&r'rargrorxrrosenvirongetrrrrrr(rrrOrOmsh( ?(+&8BH  '(,<<,' 'R:x$$gR+rrOcLeZdZdZej edsdZdZdZ dZ y)ReactorShutdownInteractionTestsz!Test reactor shutdown interactionNrMcpt|_tjd|jd|_y)zStart a UDP portrrQrRN)r*r`rr^rlrs rsetUpz%ReactorShutdownInteractionTests.setUps%h %%a L rc6|jjS)zStop the UDP port)rlrYrs rtearDownz(ReactorShutdownInteractionTests.tearDown syy&&((rc~tjtjx}j_fd}|j |fd}j |jj j ddjj jjfS)z0Test reactor shutdown while in a recvfrom() loopcjjjtjdj dyr)r`rrconnectionLostrrr)rZfinishedrs rpktRecezQReactorShutdownInteractionTests.testShutdownFromDatagramReceived..pktReces3 KK ! ! 0 0 2   a!2!2D 9rc&jyr)rrZrs r flushErrorszUReactorShutdownInteractionTests.testShutdownFromDatagramReceived..flushErrors(s  " " $rs@rQ) rr r`r.r_rrrrVrl)rprrrrs` @r testShutdownFromDatagramReceivedz@ReactorShutdownInteractionTests.testShutdownFromDatagramReceiveds>>#*/..*::T[[ ' : w % [) ## dkk&;&;&C&C&E&J&JK r) r%r&r'rFr IReactorUDPrskiprrrr(rrrrs,+ !: ! !'4 02M )$rr'This reactor does not support multicastceZdZejj ddk(rejjrdZ e je dsdZ dZ dZdZd Zd Zd Zeejj)xrejj+ d d ZdZeejj)ddZy)MulticastTestsrrz Does not work on Azure PipelinesNrcxt|_t|_t j d|j|_t j d|j|_|jjjd|jjjjy)NrrQ) r*r`r4r|rlistenMulticastrdr{rrrrVrlrs rrzMulticastTests.setUpDswh h ,,Q < ,,Q <  %%k4;;3H3H3P3P3R3W3WXrctt|jjt|jjgSr)r r rdrYr{rs rrzMulticastTests.tearDownLs8djj667djj667   rc|j|jfD]q}|j|jj d|jj d|j|jj dsy)Nrr)r|r`rWrrgetTTLsetTTL)ros rtestTTLzMulticastTests.testTTLTsjdkk) 6A   Q[[//11 5 KK  q !   Q[[//11 5 6rcjjjjdjjj jjj d}fd}|j |fd}|j |fd}|j ||S)zv Test that after loopback mode has been set, multicast packets are delivered to their sender. r 225.0.0.250ctx}j_jjj ddj f|S)Nrrr r`r.rrrrlrZr r0rs rcbJoinedz.MulticastTests.test_loopback..cbJoinedcs>-5Z 7A * KK ! ! ' '=$))2L MHrcjtjjdjjj djjjj djjjddjft}tjd|jd|S)Nrrrr) rWrr`rrrsetLoopbackModegetLoopbackModerrlr rrrrs rcbPacketz.MulticastTests.test_loopback..cbPacketjs   S!4!45q 9 KK ! ! 1 1! 4   T[[22BBDa H KK ! ! ' '=$))2L M A   aT 2Hrcdjtjjdyr)rWrr`rrs r cbNoPacketz0MulticastTests.test_loopback..cbNoPacketws"   S!4!45q 9r)rWr`rrrrV joinGroupr_)rjoinedrrrr0s` @r test_loopbackzMulticastTests.test_loopbackZs ..>>@!D{{$$,,.&&00?  8$  8$ : :& rcjjjjdjjjjdjjj d}jjj d}t ||g}fd}|j||S)zK Test C{getOutgoingInterface} and C{setOutgoingInterface}. z0.0.0.0rQcjjjjdjjjjdy)NrQ)rWr|rrgetOutgoingInterfacer`rs r cbInterfacesz3MulticastTests.test_interface..cbInterfacessI   T[[22GGI; W   T[[22GGI; Wr)rWr|rrr"r`setOutgoingInterfacer r_)rd1d2rr#s` rtest_interfacezMulticastTests.test_interface~s ..CCEyQ ..CCEyQ [[ " " 7 7 D [[ " " 7 7 DBx( X <( rcjjjd}fd}|j|fd}|j|fd}|j||S)zE Test that multicast a group can be joined and left. rcNjjjdSNr)r|rr leaveGrouprs r clientJoinedz3MulticastTests.test_joinLeave..clientJoined;;((33MB BrcNjjjdSr*)r`rrrrs r clientLeftz1MulticastTests.test_joinLeave..clientLefts;;((22=A ArcNjjjdSr*)r`rrr+rs r serverJoinedz3MulticastTests.test_joinLeave..serverJoinedr-r)r|rrrr_)rr r,r/r1s` rtest_joinLeavezMulticastTests.test_joinLeaves` KK ! ! + +M : C l# B j! C l#rz2Windows' UDP multicast is not yet fully supported.c|j|jjjdtj S)z Test that an attempt to join an address which is not a multicast address fails with L{error.MulticastJoinError}. rQ)rr|rrrrMulticastJoinErrorrs rtest_joinFailurezMulticastTests.test_joinFailures7!! KK ! ! + +K 8%:R:R  rchttjdjjj jjj d}fd}|j|fd}|j|fd}|j||S)zn Test that a multicast group can be joined and messages sent to and received from it. rrctx}j_jj ddj f|S)N hello worldrr)rZr r0crs rrz/MulticastTests.test_multicast..cbJoineds8-5Z 7A * KK  n}dii.H IHrc^jjjdddyNrr8)rWr`rrs rrz/MulticastTests.test_multicast..cbPackets'   T[[003A6 Grc\tj}|jfd|S)NcSrr(r passthroughs rrz@MulticastTests.test_multicast..cleanup..;r)r rYr_)r?rr[s` rrz.MulticastTests.test_multicast..cleanups%"1??3F   6 7Mr)r*rr r`rrrVrr_)rrrrrr0r9r[s` @@@rtest_multicastzMulticastTests.test_multicasts H  # #Aq ){{$$,,.&&00?  8$ H 8$  7# rzjon non-linux platforms it appears multiple processes can listen, but not multiple sockets in same process?c ttjddjjttjd d t j jj j  j g} fd}|j|fd}|j| fd}|j||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)listenMultiplerctx}_tx}_jjdft ||gS)Nr8)r r.rrrr )rZr%r& firstClientportno secondClienttheGroups rr1z5MulticastTests.test_multiListen..serverJoinedsK.6j 8B+/7z 9B,  ! ! ' '68J K "b* *rcjjdddjjdddyr;r)rZrErGrs r gotPacketsz3MulticastTests.test_multiListen..gotPacketssC   [003A6 G   \11!4Q7 Hrcttjtjg}|jfd|S)NcSrr(r>s rrzBMulticastTests.test_multiListen..cleanup..r@r)r r rYr_)r?r firstPort secondPorts` rrz0MulticastTests.test_multiListen..cleanupsD"!)"9"9:!*":":;F   6 7Mr) r*rr rVrlr r`rrrr_r) rrr1rJrrErMrFrGrNrHs ` @@@@@@rtest_multiListenzMulticastTests.test_multiListensh ++A{4P ""$))x ,,V\RVW   %%//9##H-$$X.   + <( I :&  w r)r%r&r'rrrr platformisMacOSXrrIReactorMulticastrrrrrr'r2r isWindowsisVistar5rArOr(rrrr6s '(,<<    % % '1 ': ' ' 68Y 6 "H$0 ""$GW-=-=-E-E-G)G<  @ ""$  /  /rr)!rFrunittestrtwisted.internetrrrrrr twisted.internet.deferr r r twisted.pythonr twisted.trial.unittestrrDatagramProtocolr*ConnectedDatagramProtocolr4rBrrErHrrOrrRrr(rrr\s0   MMII"+&UH--UH66.Y@))@, "J " "7D 113VWN+xN+XN+b  "J " "7D 113VW3h3X3l$ $$Wd33-QXQ Qr