Ϫf7rdZddlmZddlmZmZmZddlmZddl m Z m Z m Z ddl mZddlmZmZddlmZGd d ej(ZGd d eZGd dZGddeej0ZGddeej0ZGddeej0ZGddej0Zy)z. Test case for L{twisted.protocols.loopback}. ) implementer)defer interfacesreactor)Deferred)IAddress IPullProducer IPushProducer)Protocol)basicloopback)unittestc$eZdZdZdZdZdZy)SimpleProtocolcRtj|_g|_g|_yN)rrconnlinesconnLostselfs Z ' NN ) ) + (rN)r&r'r(r1r"r)rrr+r+#s  A,rr+ceZdZdZdZy)LoopbackTestCaseMixincttfd}jj|fd}tjj }|j||S)Nc\jdjjy)NTHIS IS LINE ONE!)r2r3r4resultss r sendALinez.sendALine6s JJ+ , KK & & (rcjjdgjtjdjtjdy)Nr9r- assertEqualrlenrignoredcr<rs rcheckz8LoopbackTestCaseMixin.testRegularFunction..check<sK   QWW';&< =   S_a 0   S_a 0r)rr addCallbackr maybeDeferred loopbackFuncrr=rEdrDr<s` @@rtestRegularFunctionz)LoopbackTestCaseMixin.testRegularFunction2s^     ) 9% 1    1 11a 8 ercttfd}jj|fd}tjj }|j||S)Nc(jdy)N DOOM LINE)r2r:s rr=z=LoopbackTestCaseMixin.testSneakyHiddenDoom..sendALineIs JJ| $rcjjgdjjgdjtjdjtjdy)N)Hello 1Hello 2r0)rNrPrQr0r-r?rBs rrEz9LoopbackTestCaseMixin.testSneakyHiddenDoom..checkNsb   QWW&J K   K    S_a 0   S_a 0r)r+rrFrrGrHrIs` @@rtestSneakyHiddenDoomz*LoopbackTestCaseMixin.testSneakyHiddenDoomEs\ N N % 9% 1    1 11a 8 erN)r&r'r(rKrRr)rrr6r61s &rr6ceZdZeej ZdZdZdZ dZ ddZ dZ dZ dZd Zdd Zd Zd Zd ZdZdZdZdZdZy)LoopbackAsyncTestscGddt}|}|}tj|||j|j|j|jy)z} Test that the client and server protocol both have makeConnection invoked on them by loopbackAsync. ceZdZdZdZy).TestProtocolNc||_yr)r3rr3s rmakeConnectionzKLoopbackAsyncTests.test_makeConnection..TestProtocol.makeConnectiong !*r)r&r'r(r3rZr)rr TestProtocolrWds I +rr\N)r r loopbackAsyncassertIsNotNoner3)rr\serverclients rtest_makeConnectionz&LoopbackAsyncTests.test_makeConnection^sV  +8 + vv. V--. V--.rc Gddt}|r#|}tx}|_t}n"t}|}tx}|_tj||fd}|j |S)zJ Test one of the permutations of client/server host/peer. ceZdZdZy)6LoopbackAsyncTests._hostpeertest..TestProtocolcftj|||jj|yr)r rZ onConnectionrrYs rrZzELoopbackAsyncTests._hostpeertest..TestProtocol.makeConnectionvs&''i8!!**95rN)r&r'r(rZr)rrr\rdus 6rr\cpt|}jtj|yr)getattr assertTruer providedBy)r3hostgetrs r connectedz3LoopbackAsyncTests._hostpeertest..connecteds+*79c*,D OOH//5 6r)r rrfr r]rF)rrl testServerr\r_rJr`rms`` r _hostpeertestz LoopbackAsyncTests._hostpeertestpst  68 6 !^F&.j 0A#ZFZF!^F&.j 0A#vv. 7}}Y''rc&|jddS)z Test that the server gets a transport with a properly functioning implementation of L{ITransport.getHost}. getHostTrors rtest_serverHostz"LoopbackAsyncTests.test_serverHosts !!)T22rc&|jddS)zG Like C{test_serverHost} but for L{ITransport.getPeer} getPeerTrrrs rtest_serverPeerz"LoopbackAsyncTests.test_serverPeers!!)T22rc&|jddS)z Test that the client gets a transport with a properly functioning implementation of L{ITransport.getHost}. rqFrr)rrls rtest_clientHostz"LoopbackAsyncTests.test_clientHosts !!)U33rc&|jddS)zH Like C{test_clientHost} but for L{ITransport.getPeer}. ruFrrrs rtest_clientPeerz"LoopbackAsyncTests.test_clientPeers!!)U33rcGddt}Gfddt}|r |}|}tx}|_n|}tx}|_|}tj|||S)z Test one of the permutations of write/writeSequence client/server. @param write: The name of the method to test, C{"write"} or C{"writeSequence"}. ceZdZdZdZy)9LoopbackAsyncTests._greetingtest..GreeteeProtocolrc|xj|z c_|jdk(r|jjdyyNbytes)bytesreceivedrrrs r dataReceivedzFLoopbackAsyncTests._greetingtest..GreeteeProtocol.dataReceiveds3 e# ::)MM**40*rNr&r'r(rrr)rrGreeteeProtocolr}s E 1rrceZdZfdZy)9LoopbackAsyncTests._greetingtest..GreeterProtocolcdk(r|jjdy|jjddgy)Nwritersbytses)r3r writeSequencerrs rrzHLoopbackAsyncTests._greetingtest..GreeterProtocol.connectionMades3G#NN((2NN00&%ArNr&r'r(rrsrGreeterProtocolrs  Brr)r rrr r])rrrnrrr_r`rJs ` r _greetingtestz LoopbackAsyncTests._greetingtestss 1h 1 Bh B $&F$&F"** ,A$&F"** ,A$&Fvv.rc&|jddS)z Test that on a connection where the client speaks first, the server receives the bytes sent by the client. rFrrs rtest_clientGreetingz&LoopbackAsyncTests.test_clientGreetings !!'511rc&|jddS)zz Like C{test_clientGreeting}, but use C{writeSequence} instead of C{write} to issue the greeting. rFrrs rtest_clientGreetingSequencez.LoopbackAsyncTests.test_clientGreetingSequences !!/599rc&|jddS)z Test that on a connection where the server speaks first, the client receives the bytes sent by the server. rTrrs rtest_serverGreetingz&LoopbackAsyncTests.test_serverGreetings !!'400rc&|jddS)zz Like C{test_serverGreeting}, but use C{writeSequence} instead of C{write} to issue the greeting. rTrrs rtest_serverGreetingSequencez.LoopbackAsyncTests.test_serverGreetingSequences !!/488rctddDcgc]}d|fz c}Gfddt}Gfddt}||t_t j jScc}w)Nr s%dceZdZfdZy);LoopbackAsyncTests._producertest..ProducingProtocolc|t|_|jj|jyr)listproducerstartr3)r producerClass toProduces rrzJLoopbackAsyncTests._producertest..ProducingProtocol.connectionMades) -d9o >  ##DNN3rNr)rrsrProducingProtocolrs 4rrc eZdZdZfdZy);LoopbackAsyncTests._producertest..ReceivingProtocolrc|xj|z c_|jdjk(r|jjfyy)Nr)rjoinrr)rdatar`r_rs rrzHLoopbackAsyncTests._producertest..ReceivingProtocol.dataReceivedsB d" ::)!44MM**FF+;<5rNr)r`r_rsrReceivingProtocolrs E =rr)ranger rrr r])rrr1rrr`r_rs ` @@@r _producertestz LoopbackAsyncTests._producertestsv+0B<8aUaT\8  4 4  = = =#$"$"*vv.)9s BcttGdd}j|}fd}|j||S)zO Test a push producer registered against a loopback transport. c(eZdZdZdZdZdZdZy):LoopbackAsyncTests.test_pushProducer..PushProducerFc||_yrrrrs rrzCLoopbackAsyncTests.test_pushProducer..PushProducer.__init__r[rcd|_yNT)resumedrs rresumeProducingzJLoopbackAsyncTests.test_pushProducer..PushProducer.resumeProducings # rcV||_|j|d|jyr)consumerregisterProducer_produceAndSchedulerrs rrz@LoopbackAsyncTests.test_pushProducer..PushProducer.start s% ( ))$5((*rc|jrU|jj|jjdt j d|j y|jjyNr)rrrpopr callLaterrunregisterProducerrs rrzNLoopbackAsyncTests.test_pushProducer..PushProducer._produceAndSchedulesP>>MM''(:(:1(=>%%a)A)ABMM446rN)r&r'r(rrrrrr)rr PushProducerrsG + $ +  7rrc\|\}}j|jjdy)Nz0Streaming producer should not have been resumed.) assertFalserr)resultsr`r_rs rfinishedz6LoopbackAsyncTests.test_pushProducer..finisheds+& VV   ''B r)rr rrF)rrrJrs` rtest_pushProducerz$LoopbackAsyncTests.test_pushProducersK ] # 7 7 $ 7*   | ,  hrc\ttGdd}|j|S)zO Test a pull producer registered against a loopback transport. ceZdZdZdZdZy):LoopbackAsyncTests.test_pullProducer..PullProducerc||_yrrrs rrzCLoopbackAsyncTests.test_pullProducer..PullProducer.__init__)r[rcJ||_|jj|dy)NF)rrrs rrz@LoopbackAsyncTests.test_pullProducer..PullProducer.start,s (  ..tU;rc|jj|jjd|js|jj yyr)rrrrrrs rrzJLoopbackAsyncTests.test_pullProducer..PullProducer.resumeProducing0s? ##DNN$6$6q$9:~~MM446&rN)r&r'r(rrrr)rr PullProducerr's + < 7rr)rr r)rrs rtest_pullProducerz$LoopbackAsyncTests.test_pullProducer"s3 ] # 7 7 $ 7!!,//rcGddt}Gddt}|}|tj|}fd}|j||S)z L{loopback.loopbackAsync} does not call a protocol's C{dataReceived} method while that protocol's transport's C{write} method is higher up on the stack. ceZdZdZy)9LoopbackAsyncTests.test_writeNotReentrant..Serverc:|jjdyr)r3rrs rrzFLoopbackAsyncTests.test_writeNotReentrant..Server.dataReceived?s$$X.rN)r&r'r(rr)rrServerr>s /rrc"eZdZdZdZdZdZy)9LoopbackAsyncTests.test_writeNotReentrant..ClientFcDtjd|jyr)rrgors rrzHLoopbackAsyncTests.test_writeNotReentrant..Client.connectionMadeEs!!!TWW-rcH|jjdd|_y)NfooT)r3rreadyrs rrz.Client.goHs$$V,! rcZ|j|_|jjyr)rwasReadyr3r4rs rrzFLoopbackAsyncTests.test_writeNotReentrant..Client.dataReceivedLs $  --/rN)r&r'r(rrrrr)rrClientrBsE . " 0rrc<jjyr)rir)rCr`rs r cbFinishedz=LoopbackAsyncTests.test_writeNotReentrant..cbFinishedTs OOFOO ,r)r r r]rF)rrrr_rJrr`s` @rtest_writeNotReentrantz)LoopbackAsyncTests.test_writeNotReentrant7sU /X / 0X 0  " "66 2 - j!rcgfd}tttj|}jgjj djj djj djj djj fd}|j||S)z The callable passed as the value for the C{pumpPolicy} parameter to L{loopbackAsync} is called with a L{_LoopbackQueue} of pending bytes and a protocol to which they should be delivered. cxg}|r"|j|j|r"j||fyr)rrl)queuetargetr pumpCallss r dummyPolicyz7LoopbackAsyncTests.test_pumpPolicy..dummyPolicybs4E UYY[)   fe_ -rrbarbazquuxc>jgdfddgfgy)N)rrNrr)r@)rCr`rrr_s r cbCompletez6LoopbackAsyncTests.test_pumpPolicy..cbCompletets.   12Vff=M4NO  r)r r r]r@r3rr4rF)rrrrr`rr_s` @@@rtest_pumpPolicyz"LoopbackAsyncTests.test_pumpPolicyZs   . ))&&+F B'v&v&v&w''')  Z(rc g}t}|j|_tj}|j d|j d|j dtj |||j|ddgy)z L{identityPumpPolicy} is a pump policy which calls the target's C{dataReceived} method one for each string in the queue passed to it. rrN)r rrr _LoopbackQueueputidentityPumpPolicyr@rrr`rs rtest_identityPumpPolicyz*LoopbackAsyncTests.test_identityPumpPolicysr #ll'') & & $##E62  01rcg}t}|j|_tj}|j d|j d|j dtj |||j|dgy)z L{collapsingPumpPolicy} is a pump policy which calls the target's C{dataReceived} only once with all of the strings in the queue passed to it joined together. rrNsfoobar)r rrr rrcollapsingPumpPolicyr@rs rtest_collapsingPumpPolicyz,LoopbackAsyncTests.test_collapsingPumpPolicyso #ll'') & & $%%eV4  ,rN)rqr)r&r'r( staticmethodr r]rHrarorsrvrxrzrrrrrrrrrrrrr)rrrTrT[sp 6 67L/$(633 44 !F2:19.%N0*!F#J2"-rrTc0eZdZeej Zy)LoopbackTCPTestsN)r&r'r(rr loopbackTCPrHr)rrrrs 4 45Lrrc\eZdZeej Zeje ddZ yy)LoopbackUNIXTestsNz-Current reactor does not support UNIX sockets) r&r'r(rr loopbackUNIXrHr IReactorUNIXrskipr)rrrrs1 5 56Lzw-5>6rrc2eZdZdZGddeZdZdZy)LoopbackRelayTestz> Test for L{twisted.protocols.loopback.LoopbackRelay} ceZdZdZdZdZy)LoopbackRelayTest.ReceiverzF Simple Receiver class used for testing LoopbackRelay rc.|xj|z c_y)z)Accumulate received data for verificationN)r)rrs rrz'LoopbackRelayTest.Receiver.dataReceiveds II IrN)r&r'r(__doc__rrr)rrReceiverrs  rrc"|j}tj|}|jd|jd|j |j d|j |j |j dy)z8Test to verify that the write function works as expectedsabcsdefrsabcdefN)rr LoopbackRelayrr@r clearBufferrreceiverrelays r test_writezLoopbackRelayTest.test_writesi==?&&x0 F F ,   2rc*|j}tj|}|jgd|jgd|j |j d|j |j |j dy)z@Test to verify that the writeSequence function works as expected)sThe squick sbrown sfox )sjumps sover s the lazy dogrs+The quick brown fox jumps over the lazy dogN)rr rrr@rrr s rtest_writeSequencez$LoopbackRelayTest.test_writeSequencesp==?&&x0 DE BC ,  (VWrN)r&r'r(rr rr rr)rrrrs 8 3XrrN)rzope.interfacertwisted.internetrrrtwisted.internet.deferrtwisted.internet.interfacesrr r twisted.internet.protocolr twisted.protocolsr r twisted.trialr LineReceiverrr+r6TestCaserTrrrr)rrrs '77+NN.-" %U'' % ,> ,''TE-.0A0AE-P 6,h.?.?6?-x/@/@?"X))"Xr