Ϫf 5dZddlZddlZddlZddlmZddlmZmZm Z m Z m Z m Z m Z ddlmZddlmZddlmZddlmZmZdd lmZGd d e j0Zee j4e d d Gd dej6ZGdde j:ZGdde j>Z ee jBe d dGddej6Z"y)zK Tests for implementations of L{IReactorUNIX} and L{IReactorUNIXDatagram}. N)skipIf)addressdefererror interfacesprotocolreactorutils)lockfile) networkString)FilePath)MyClientFactoryMyServerFactory)unittestceZdZdZdZy)FailedConnectionClientFactoryc||_yN)onFail)selfrs 8/usr/lib/python3/dist-packages/twisted/test/test_unix.py__init__z&FailedConnectionClientFactory.__init__s  c:|jj|yr)rerrback)r connectorreasons rclientConnectionFailedz4FailedConnectionClientFactory.clientConnectionFaileds F#rN)__name__ __module__ __qualname__rrrrrrs $rr1This reactor does not support UNIX domain socketscpeZdZdZej edsdZdZdZ dZ dZ dZ d Z d Zd Zd Zy) UnixSocketTestsz Test unix sockets. Nr#cj}jttjx}_t j |}j|jtjtjtj}j|j|j|j|fd}|j||S)z The address passed to the server factory's C{buildProtocol} method and the address returned by the connected protocol's transport's C{getPeer} method match the address the client socket is bound to. ctj}jj|gj|jj |yr)r UNIXAddress assertEqual peerAddresses transportgetPeer)protoexpectedpeernamer serverFactorys r cbConnMadez1UnixSocketTests.test_peerBind..cbConnMade;sH**84H   ]888* E   U__446 Ar)mktemprrDeferredprotocolConnectionMader listenUNIX addCleanup stopListeningsocketAF_UNIX SOCK_STREAMclosebindconnect addCallback)rfilenameconnMadeunixPort unixSocketr1r/r0s` @@r test_peerBindzUnixSocketTests.test_peerBind*s ;;=;;=') :?..:JJ=7%%h > ../]]6>>63E3EF   (()!8$ B Z(rcjt}tj}||_t j |}j|jttj}|_t jtj||g}fd}|j||S)z L{IReactorUNIX.connectUNIX} can be used to connect a client to a server started with L{IReactorUNIX.listenUNIX}. c|\}}jjtjg|jj |jj yr)r)r*rr(r+loseConnection)argsserverProtocolclientProtocol clientFactoryr?rs r allConnectedz1UnixSocketTests.test_dumber..allConnectedTs\-1 *NN   ++g.A.A(.K-L   $ $ 3 3 5  $ $ 3 3 5r) r2rrr3r4r r5r6r7r connectUNIX gatherResultsr>) rr0serverConnMaderAclientConnMadedrKrJr?s ` @@r test_dumberzUnixSocketTests.test_dumberCs ;;=') )/= ,%%h > ../') )/= ,Hm4    @ A 6 l#rc jt}tj}||_t j |d jtjdzttj}|_t jdtj||g} fd}|j|fd}|j||S)z A lockfile is created and locked when L{IReactorUNIX.listenUNIX} is called and released when the Deferred returned by the L{IListeningPort} provider's C{stopListening} method is called back. TwantPID.lockcheckPIDc|\}}jjtjg|jj |jj j Sr)r)r*rr(r+rFr7)rGrH clientProtorJr?rrAs r _portStuffz0UnixSocketTests.test_pidFile.._portStuffysk*. 'NK   ++g.A.A(.K-L   ! ! 0 0 2  $ $ 3 3 5))+ +rcVjtjdzdy)NrUlocked) assertFalser isLocked)ignoredr?rs r_checkz,UnixSocketTests.test_pidFile.._checks#   X..x'/ABH Mr)r2rrr3r4r r5 assertTruer r_rrLrMr>) rr0rNrOrPr[rarJr?rAs ` @@@r test_pidFilezUnixSocketTests.test_pidFilebs ;;=') )/= ,%%h tL ))(W*<=> () )/= ,Hma@    @ A , j! N frc|jttjd}|j t j tjdfd}|jj|S)z L{IReactorUNIX.listenUNIX} raises L{error.CannotListenError} if passed the name of a file on which a server is already listening. TrScTtjd}|jSNTrS)r r5r7)ignrAr?r0s rstoppedListeningz.stoppedListenings&))(M4PH))+ +r) r2rr r5 assertRaisesrCannotListenErrorr7r>)rrArhr?r0s @@rtest_socketLockingz"UnixSocketTests.test_socketLockings| ;;=') %%h tL   # #        ,%%'334DEErc|j|_td|jd}dttj j tjjji}ttjjj}tj|dd|f|}|j||S)NzBfrom twisted.internet import protocol, reactor reactor.listenUNIX(z), protocol.ServerFactory(),wantPID=True) s PYTHONPATHs-us-c)env)r2r?r r ospathsepjoinsyspath asBytesMode executabler getProcessValuer>)rcallbacksourcermpyExerPs r_uncleanSocketTestz"UnixSocketTests._uncleanSocketTests   }}   hrzzsxx'@AMMOTTU(446;;  ! !%%)?S I hrc0fd}j|S)a" If passed C{True} for the C{wantPID} parameter, a server can be started listening with L{IReactorUNIX.listenUNIX} when passed the name of a file on which a previous server which has not exited cleanly has been listening using the C{wantPID} option. cxtjjtd}|j Srf)r r5r?rr7)rgprs rranStupidChildzGUnixSocketTests.test_uncleanServerSocketLocking..ranStupidChilds+""4==/2CTRA??$ $rryrr}s` rtest_uncleanServerSocketLockingz/UnixSocketTests.test_uncleanServerSocketLockings % &&~66rc0fd}j|S)z If passed C{True} for the C{checkPID} parameter, a client connection attempt made with L{IReactorUNIX.connectUNIX} fails with L{error.BadFileError}. ctj}t|}tjj |dj |tjS)NTrW) rr3rr rLr? assertFailurer BadFileError)rgrPfrs rr}zCUnixSocketTests.test_connectToUncleanServer..ranStupidChildsH A-a0A    q4 @%%a););< c~dd}jt|jt|yNrz (not listening)>r)reprstr)rgunconnectedString factoryNamerrAs rrhz3UnixSocketTests._reprTest..stoppedListenings="#K=0A B    T(^-> ?   S],= >r) r2r r5r)rrr maybeDeferredr7r>)rr0rr?connectedStringrPrhrAs` ` @r _reprTestzUnixSocketTests._reprTests ;;=%%h >k]$xl!< h9 X8    6 6 7 ? &'rcpGdd}|j|t|j|dS)a! The two string representations of the L{IListeningPort} returned by L{IReactorUNIX.listenUNIX} contains the name of the new-style factory class being used and the filename on which the port is listening or indicates that the port is not listening. ceZdZdZdZy)EUnixSocketTests.test_reprWithNewStyleFactory..NewStyleFactorycyrr"rs rdoStartzMUnixSocketTests.test_reprWithNewStyleFactory..NewStyleFactory.doStartrcyrr"rs rdoStopzLUnixSocketTests.test_reprWithNewStyleFactory..NewStyleFactory.doStoprrN)rr r!rrr"rrNewStyleFactoryr   rrz&twisted.test.test_unix.NewStyleFactoryassertIsInstancetyper)rrs rtest_reprWithNewStyleFactoryz,UnixSocketTests.test_reprWithNewStyleFactorys8   ot4~~  G  r)rr r!__doc__r IReactorUNIXr skiprCrQrcrkryrrrrr"rrr%r%sO  #: " "7D 1B2>*XF." 7 7, rr%c0eZdZdxZZdZdZdZdZdZ y) ClientProtoFNchtj|_tj|_yr)rr3deferredStarteddeferredGotBackrs rrzClientProto.__init__ $~~/$~~/rcd|_yNTstoppedrs r stopProtocolzClientProto.stopProtocol   rcHd|_|jjdyrstartedrrvrs r startProtocolzClientProto.startProtocol   %%d+rcH||_|jjdyr)gotbackrrv)rdatas rdatagramReceivedzClientProto.datagramReceivedrr) rr r!rrrrrrrr"rrrrs%GgG0,,rrc4eZdZdxZZdxZZdZdZdZ dZ y) ServerProtoFNchtj|_tj|_yr)rr3rdeferredGotWhatrs rrzServerProto.__init__rrcd|_yrrrs rrzServerProto.stopProtocolrrcHd|_|jjdyrrrs rrzServerProto.startProtocol rrc||_|jjd|||_|jj dy)Nhi back)gotfromr+writegotwhatrrv)rraddrs rrzServerProto.datagramReceived$s8  Z.  %%d+r) rr r!rrrrrrrrr"rrrrs*GgGg0,,rrz3This reactor does not support UNIX datagram socketsc(eZdZdZdZdZdZdZy)DatagramUnixSocketTestsz% Test datagram UNIX sockets. c jj}t ttj| }j |j tj|}j |j tj jjg} fd} fd}|j||j||S)zf Test that a datagram can be sent to and received by a server and vice versa. ) bindAddresscjjdtjjj gS)Nhi)r+rrrMrr)r`cpsps rrz4DatagramUnixSocketTests.test_exchange..writeDs7 LL  u %&&(:(:BDatagramUnixSocketTests.test_exchange.._cbTestExchangeHs?   UBJJ /   Z 4   Z 4r) r2rrr listenUNIXDatagramr6r7connectUNIXDatagramrrMrr>) r serveraddrscrPrrrrrs ` @@@r test_exchangez%DatagramUnixSocketTests.test_exchange4s [[] [[] ] ]  & &z2 6 (  ' ' BJ O (   !3!3R5G5G H I Q 5 e o&rc|j}t}tj||}|j t j tj|||jtj|y)z L{IReactorUNIXDatagram.listenUNIXDatagram} raises L{error.CannotListenError} if the unix socket specified is already in use. N) r2rr rrirrjr7rnunlink)rrr|rs rtest_cannotListenz)DatagramUnixSocketTests.test_cannotListenQs^ {{} M  & &tQ / %1173M3MtUVW  $rcDj}tj||dd|d}jt |jt |t jj}fd}|j||S)z Test the C{__str__} and C{__repr__} implementations of a UNIX datagram port when used with the given protocol. rrrc~dd}jt|jt|yrr)rgr protocolNamerrAs rrhz;DatagramUnixSocketTests._reprTest..stoppedListeningns="#L>1B C    T(^-> ?   S],= >r) r2r rr)rrrrr7r>)r serverProtorr?r stopDeferredrhrAs` ` @rrz!DatagramUnixSocketTests._reprTest`s ;;=--h Dl^4|1= h9 X8**8+A+AB  ?   !12rcpGdd}|j|t|j|dS)a2 The two string representations of the L{IListeningPort} returned by L{IReactorUNIXDatagram.listenUNIXDatagram} contains the name of the new-style protocol class being used and the filename on which the port is listening or indicates that the port is not listening. ceZdZdZdZy)ODatagramUnixSocketTests.test_reprWithNewStyleProtocol..NewStyleProtocolcyrr")rr+s rmakeConnectionz^DatagramUnixSocketTests.test_reprWithNewStyleProtocol..NewStyleProtocol.makeConnectionrrcyrr"rs rrzVDatagramUnixSocketTests.test_reprWithNewStyleProtocol..NewStyleProtocol.doStoprrN)rr r!rrr"rrNewStyleProtocolr~rrrz'twisted.test.test_unix.NewStyleProtocolr)rrs rtest_reprWithNewStyleProtocolz5DatagramUnixSocketTests.test_reprWithNewStyleProtocolvs9   .5~~   I  rN)rr r!rrrrrr"rrrr+s : , rr)#rrnr8rqrrtwisted.internetrrrrrr r twisted.pythonr twisted.python.compatr twisted.python.filepathr twisted.test.test_tcprr twisted.trial ClientFactoryrrTestCaser%ConnectedDatagramProtocolrDatagramProtocolrIReactorUNIXDatagramrr"rrrs  XXX#/,B"$H$:$:$ ..7\ h''\ \ ~,(44,(,(++,,' ''669[ h//[ [ r