Ϫf1DdZddlZddlZddlZddlmZddlmZmZm Z m Z m Z ddl m Z mZddlmZddlmZddlmZdd lmZmZd Zeej4Zej8j;ej<ed <Gd d e j>Z GddeZ!y)z Tests for L{twisted.internet.stdio}. @var properEnv: A copy of L{os.environ} which has L{bytes} keys/values on POSIX platforms and native L{str} keys/values on Windows. N)skipIf)defererrorprotocolreactorstdio)filepathlog) requireModule)platform)ConnectionLostNotifyingProtocol)SkipTestTestCasesxyz123abc Twisted is great! PYTHONPATHc,eZdZdZdZdZdZdZdZy)StandardIOTestProcessProtocola Test helper for collecting output from a child process and notifying something when it exits. @ivar onConnection: A L{defer.Deferred} which will be called back with L{None} when the connection to the child process is established. @ivar onCompletion: A L{defer.Deferred} which will be errbacked with the failure associated with the child process exiting when it exits. @ivar onDataReceived: A L{defer.Deferred} which will be called back with this instance whenever C{childDataReceived} is called, or L{None} to suppress these callbacks. @ivar data: A C{dict} mapping file descriptors to strings containing all bytes received from the child process on each file descriptor. Ncvtj|_tj|_i|_yN)rDeferred onConnection onCompletiondataselfs 9/usr/lib/python3/dist-packages/twisted/test/test_stdio.py__init__z&StandardIOTestProcessProtocol.__init__8s'!NN,!NN, c:|jjdyr)rcallbackrs rconnectionMadez,StandardIOTestProcessProtocol.connectionMade=s ""4(rc|jj|d|z|j|<|j&|jdc}|_|j|yy)z Record all bytes received from the child process in the C{data} dictionary. Fire C{onDataReceived} if it is not L{None}. rN)rgetonDataReceivedr)rnamebytesds rchildDataReceivedz/StandardIOTestProcessProtocol.childDataReceived@sW ))--c2U: $    *%)%8%8$ "At" JJt  +rc:|jj|yr)rr)rreasons r processEndedz*StandardIOTestProcessProtocol.processEndedJs ""6*r) __name__ __module__ __qualname____doc__r#rr r'r*rrrr#s"$N )+rrceZdZejr eddZdZdZdZ dZ dZ d Z d Z d Zd Zd ZdZeejddZy)StandardInputOutputTests win32processNzIOn windows, spawnProcess is not available in the absence of win32process.ctjdd|ztjjgt |z}tj |tj|fdti|S)a_ Launch a child Python process and communicate with it using the given ProcessProtocol. @param proto: A L{ProcessProtocol} instance which will be connected to the child process. @param sibling: The basename of a file containing the Python program to run in the child process. @param *args: strings which will be passed to the child process on the command line as C{argv[2:]}. @param **kw: additional arguments to pass to L{reactor.spawnProcess}. @return: The L{IProcessTransport} provider for the spawned process. s-ms twisted.test.env)sys executabler __class__r,list spawnProcess properEnv)rprotosiblingargskws r _spawnProcessz&StandardInputOutputTests._spawnProcessUs`& NN  w &    ( (  J  ##E3>>4UYURTUUrc>fd}fd}|j||S)Nc.jd|y)Nz%Process terminated with non-Failure: )fail)resultrs rcbz4StandardInputOutputTests._requireFailure..cbps II=fZH Irc|Srr/)errrs rebz4StandardInputOutputTests._requireFailure..ebss C= r) addCallbacks)rr&rrDrGs` ` r_requireFailurez(StandardInputOutputTests._requireFailureos! J !~~b"%%rcjtjdztj}j dfd}j ||S)z Verify that a protocol connected to L{StandardIO} can disconnect itself using C{transport.loseConnection}. Child process logging to sstdio_test_loseconnct5}|D](}tjd|jz* dddj dj |j tjy#1swYExYw)NzChild logged: ) openr msgrstripfailIfInrtrapr ProcessDone)r)fline errorLogFileprs rr*zBStandardInputOutputTests.test_loseConnection..processEndedsol# >q>DGG,t{{}<=> > MM!QVV $ KK)) *  > >s .BB )mktempr rOrrr?rI)rr&r*rVrWs` @@rtest_loseConnectionz,StandardInputOutputTests.test_loseConnectionxs` {{}  +l:; ) + NN 14lC +##A|44rcB|j}tjd|ztt j _fd}j j|d}|jj |}|jd||S)z When stdin is closed and the protocol connected to it implements L{IHalfCloseableProtocol}, the protocol's C{readConnectionLost} method is called. rKcTj}jj|Sr)r transport closeStdin)ignoredr&rWs rcbByteszAStandardInputOutputTests.test_readConnectionLost..cbBytess!A KK " " $HrcB|jtjyr)rRrrS)r)s rr*zFStandardInputOutputTests.test_readConnectionLost..processEndeds KK)) *rsstdio_test_halfclose) rXr rOrrrr# addCallbackrIr?)rrVr_r*r&rWs @rtest_readConnectionLostz0StandardInputOutputTests.test_readConnectionLosts {{}  +l:; ) + >>+  $$W- +  !1!1< @ 15|Drct jdtdfd}j j|S#t$r}t t |d}~wwxYw)z Verify that a write made directly to stdout using L{os.write} after StandardIO has finished is reliably received by the process reading that stdout. sstdio_test_lastwriteT)usePTYNcjjdjtdjd|j t j y)z Asserts that the parent received the bytes written by the child immediately after the child starts. rMz Received z) from child, did not find expected bytes.N) assertTruerendswithUNIQUE_LAST_WRITE_STRINGrRrrSr)rWrs rr*zEStandardInputOutputTests.test_lastWriteReceived..processEndedsM OOq ""#;<AFF:%NO  KK)) *r)rr?rh ValueErrorrstrrIr)rer*rWs` @rtest_lastWriteReceivedz/StandardInputOutputTests.test_lastWriteReceivedso * + #   *,DT   +##ANNLAA #3q6" " #sA A+A&&A+ctj}jdfd}j||S)z Verify that the transport of a protocol connected to L{StandardIO} has C{getHost} and C{getPeer} methods. sstdio_test_hostpeercjdj\}}j|j||jtj y)NrM)r splitlinesrfrRrrS)r)hostpeerrWrs rr*z?StandardInputOutputTests.test_hostAndPeer..processEndedsG--/JD$ OOD ! OOD ! KK)) *rrrr?rIrr&r*rWs` @rtest_hostAndPeerz)StandardInputOutputTests.test_hostAndPeersA * + NN 145 + ##A|44rctj}jdfd}j||S)z Verify that the C{write} method of the transport of a protocol connected to L{StandardIO} sends bytes to standard out. sstdio_test_writecjjdd|jtjyNrMsok! assertEqualrrRrrSris rr*z9StandardInputOutputTests.test_write..processEnded-   QVVAY / KK)) *rrsrts` @r test_writez#StandardInputOutputTests.test_writesA * + NN 112 +##A|44rctj}jdfd}j||S)z Verify that the C{writeSequence} method of the transport of a protocol connected to L{StandardIO} sends bytes to standard out. sstdio_test_writeseqcjjdd|jtjyrxryris rr*zAStandardInputOutputTests.test_writeSequence..processEndedr{rrsrts` @rtest_writeSequencez+StandardInputOutputTests.test_writeSequencesA * + NN 145 +##A|44rc|j}t|d5}tdD]}|jd|fz ddd|S#1swY|SxYw)Nwbi%d )rXrNrangewrite)rjunkPathjunkFileis r _junkPathz"StandardInputOutputTests._junkPaths\;;= (D ! /X4[ /w!~. / / /s &AActj}gttdfdj dj j fd}j||S)z Verify that the transport of a protocol connected to L{StandardIO} is a working L{IProducer} provider. dcrOjdjfzjdtjddyy)Nrg{Gz?)appendpoprr callLater)ignr proctoWritewrittens rr z>StandardInputOutputTests.test_producer..connectionMadesGw'++-)99: 72;'!!$=rsstdio_test_producercjjddjjdt fz|j t jy)NrMrz*Connection lost with %d writes left to go.)rzrjoin assertFalselenrRrrS)r)rWrrrs rr*z.processEndedsW   QVVAY(9 :   EWW  KK)) *r)rrr8rr?rrarI)rr&r*r rWrrrs` @@@@@r test_producerz&StandardInputOutputTests.test_producerso * + NNuSz" > !!!%;< "">2 +##A|44rctj}jjdfd}j ||S)z Verify that the transport of a protocol connected to L{StandardIO} is a working L{IConsumer} provider. sstdio_test_consumerctd5}jjd|jddd|j t j y#1swY)xYw)NrbrM)rNrzrreadrRrrS)r)rTrrWrs rr*z.processEnded1sTh% 6  AFFH5 6 KK)) * 6 6s .A$$A-)rrrr?rI)rr&r*rrWs` @@r test_consumerz&StandardInputOutputTests.test_consumer%sO * + NN>># 14h? + ##A|44rzpStandardIO does not accept stdout as an argument to Windows. Testing redirection to a file is therefore harder.c tj}t|}tjj  j dx_}j|jt|j}tjs\tj\}}jtj|jtj|||d<t!j"|fi|d t%j&  fd t)j*d  fd}|j-||S)aE If L{StandardIO} is created with a file descriptor which refers to a normal file (ie, a file from the filesystem), L{StandardIO.write} writes bytes to that file. In particular, it does not immediately consider the file closed or call its protocol's C{connectionLost} method. r)stdoutstdincD]/}|k(rjyjd|fzntjdy)N%dr)loseConnectionrrr)value connectioncounthowManyspins rrzAStandardInputOutputTests.test_normalFileStandardOut..spin\sS G#--/  %!12     a &rrc jtdzjjdjdt Dy)NrMrc3(K|] }d|fz yw)rNr/).0rs r zVStandardInputOutputTests.test_normalFileStandardOut..cbLost..ls+QQEQDL+Qs)rznext getContentrr)r)rrpathrs rcbLostzCStandardInputOutputTests.test_normalFileStandardOut..cbLostisH   T%['A+ 6   !388+Q%.+Q#Q r)rrr r FilePathrXrNnormal addCleanupclosedictfilenor isWindowsospiper StandardIO itertoolsrrrra) r onConnLostr;rkwargsrwrrrrrrs ` @@@@@rtest_normalFileStandardOutz3StandardInputOutputTests.test_normalFileStandardOut8s ^^% / ;  /#yy. f  %V]]_-!!#779DAq OOBHHa ( OOBHHa (F7O%%e6v6 ! ' !T"  v&r)r+r,r-r rr skipr?rIrYrbrmrur|rrrrrrr/rrr1r1Nsx n = E ' V4&5,4%BN5"5 5 5<5&  = 3  3rr1)"r.rrr5unittestrtwisted.internetrrrrrtwisted.pythonr r twisted.python.reflectr twisted.python.runtimer twisted.test.test_tcpr twisted.trial.unittestrrrhrenvironr:pathseprrProcessProtocolrr1r/rrrs CC(0+A5:   **//#((3 ,(+H$<$<(+Vbxbr