Ϫf9dZddlmZddlmZddlmZddlmZddl m Z m Z m Z ddl mZddlmZGd d eZGd d eZGd de j(ZGdde j(ZGdde j.e j(e j0ZGdde j(e j0Ze j6eee j6eee j8j;eGdde j<ZGdde j@Z!GddejDZ#Gdde#Z$Gdde$Z%Gdd Z&Gd!d"ejDZ'y#)$z! Tests for error handling in PB. )StringIO)defer)log)qual)flavorsjellypb)connectedServerAndClient)unittestceZdZdZy)AsynchronousExceptionz Helper used to test remote methods which return Deferreds which fail with exceptions which are not L{pb.Error} subclasses. N__name__ __module__ __qualname____doc__D/usr/lib/python3/dist-packages/twisted/spread/test/test_pbfailure.pyr r rr ceZdZdZy)SynchronousExceptionzm Helper used to test remote methods which raise exceptions which are not L{pb.Error} subclasses. NrrrrrrrrrceZdZdZy)AsynchronousErrorz Helper used to test remote methods which return Deferreds which fail with exceptions which are L{pb.Error} subclasses. Nrrrrrr"rrrceZdZdZy)SynchronousErrorzi Helper used to test remote methods which raise exceptions which are L{pb.Error} subclasses. Nrrrrrr)rrrc eZdZy) JellyErrorNrrrrrrrr0rrc eZdZy) SecurityErrorNrrrrr"r"4r rr"cReZdZdZdZdZdZdZdZdZ dZ d Z d d Z d d Z y ) SimpleRootc>tjtdS)zD Fail asynchronously with a non-pb.Error exception. zremote asynchronous exception)rfailr selfs rremote_asynchronousExceptionz'SimpleRoot.remote_asynchronousException?szz/0OPQQrctd)zC Fail synchronously with a non-pb.Error exception. zremote synchronous exception)rr's rremote_synchronousExceptionz&SimpleRoot.remote_synchronousExceptionEs##ABBrc>tjtdS)z@ Fail asynchronously with a pb.Error exception. zremote asynchronous error)rr&rr's rremote_asynchronousErrorz#SimpleRoot.remote_asynchronousErrorKszz+,GHIIrctd)z? Fail synchronously with a pb.Error exception. zremote synchronous error)rr's rremote_synchronousErrorz"SimpleRoot.remote_synchronousErrorQs9::rcDGddtj}|d)z> Fail with error that is not known to client. c eZdZy)4SimpleRoot.remote_unknownError..UnknownErrorNrrrr UnknownErrorr2\s rr3zI'm not known to client!)r Error)r(r3s rremote_unknownErrorzSimpleRoot.remote_unknownErrorWs  288 566rc$|jyN) raiseJellyr's r remote_jellyzSimpleRoot.remote_jellyas  rc$|jyr7) raiseSecurityr's rremote_securityzSimpleRoot.remote_securityds rctj}|j|j|j d|Sr7)rDeferred addCallbackr8callbackr(ds rremote_deferredJellyzSimpleRoot.remote_deferredJellygs/ NN  doo& 4rctj}|j|j|j d|Sr7)rr>r?r;r@rAs rremote_deferredSecurityz"SimpleRoot.remote_deferredSecurityms1 NN  d(() 4rNctd)NzI'm jellyable!)rr(resultss rr8zSimpleRoot.raiseJellyss)**rctd)Nz I'm secure!)r"rGs rr;zSimpleRoot.raiseSecurityvs M**rr7)rrrr)r+r-r/r5r9r<rCrEr8r;rrrr$r$>s<R C J ; 7  ++rr$ceZdZdZdZdZy)SaveProtocolServerFactoryzd A L{pb.PBServerFactory} that saves the latest connected client in C{protocolInstance}. Nc||_y)z3 Keep track of the given protocol. N)protocolInstance)r(protocols rclientConnectionMadez.SaveProtocolServerFactory.clientConnectionMades !)r)rrrrrMrOrrrrKrKzs )rrKceZdZdZdZy)PBConnTestCaserctt_jj_t j _tfdfd\__ _ y)Nc:jjdSr7) serverFactory buildProtocolr'srz&PBConnTestCase.setUp..D&&44T:rc:jjdSr7) clientFactoryrUr'srrVz&PBConnTestCase.setUp..rWr) rKr$rTunsafeTracebacksr PBClientFactoryrYr connectedServerconnectedClientpumpr's`rsetUpzPBConnTestCase.setUps]6z|D.2.C.C+//1 % : :    IrN)rrrrZr_rrrrQrQs   rrQceZdZejj ZdZdZdZ dZ dZ dZ dZ dd Zd Zd Zd Zd ZdZdZdZdZdZy)PBFailureTestscfdjj}fd}|j|jj y)Nc|jj|jdr,j}j t |d|j |j|jfS)NTraceback unavailable )trapcompare tracebackflushLoggedErrors assertEquallentypevalue)errerrs exceptionTypeflushr(s rebz)PBFailureTests._exceptionTest..ebsb HH] # LL(A B--m<  TA.HHcii7 7rcL|j}|j|Sr7) callRemote addErrback)rootrBrrmethods r gotRootObjectz4PBFailureTests._exceptionTest..gotRootObjects"'A LL HrrY getRootObjectr?r^rq)r(rwrprqrBrxrrs```` @r_exceptionTestzPBFailureTests._exceptionTests? 8    , , .  m$ rc0|jdtdS)z Test that a Deferred returned by a remote method which already has a Failure correctly has that error passed back to the calling side. asynchronousExceptionT)r{r r's rtest_asynchronousExceptionz)PBFailureTests.test_asynchronousExceptions ""#:.gotRootObj..gotFailures($$S)?)?)D%EqIJr)rrtaddBoth)objfailureDeferredrrrrexpectedrwr(s r gotRootObjz,PBFailureTests._testImpl..gotRootObjsE"77v&"O '' 3" "rNry)r(rwrrrr rootDeferredrs````` r _testImplzPBFailureTests._testImplsA ))779  # #   , rc4fd}jdd|S)z Test that an exception which is a subclass of L{pb.Error} has more information passed across the network to the calling side. c|jtj|jtj |j |jy)N+rfrassertNotIsInstancerlstrassertIsInstancermr&r(s r failureJellyz6PBFailureTests.test_jellyFailure..failureJellys> IIj !  $ $TYY 4  ! !$**dii 8rrrr)r(rs` rtest_jellyFailurez PBFailureTests.test_jellyFailures   ~~gr<88rc4fd}jdd|S)z Test that a Deferred which fails with a L{pb.Error} is treated in the same way as a synchronously raised L{pb.Error}. c|jtj|jtj |j |jy)Nrrs rfailureDeferredJellyzFPBFailureTests.test_deferredJellyFailure..failureDeferredJellys> IIj !  $ $TYY 4  ! !$**dii 8r deferredJellyrr)r(rs` rtest_deferredJellyFailurez(PBFailureTests.test_deferredJellyFailures   ~~os4HIIrc4fd}jdd|S)z A non-jellyable L{pb.Error} subclass raised by a remote method is turned into a Failure with a type set to the FQPN of the exception type. c>j|jdy)Ns3twisted.spread.test.test_pbfailure.SynchronousErrorrjrlrs rfailureUnjellyablezBPBFailureTests.test_unjellyableFailure..failureUnjellyables    Q rrrr)r(rs` rtest_unjellyableFailurez&PBFailureTests.test_unjellyableFailures  ~~0#7IJJrc4fd}jdd|S)z Test that an exception which is a subclass of L{pb.Error} but not known on the client side has its type set properly. c>j|jdy)Ns/twisted.spread.test.test_pbfailure.UnknownErrorrrs rfailureUnknownz:PBFailureTests.test_unknownFailure..failureUnknowns    M r unknownErrorrr)r(rs` rtest_unknownFailurez"PBFailureTests.test_unknownFailures   ~~ndNCCrc4fd}jdd|S)z Test that even if an exception is not explicitly jellyable (by being a L{pb.Jellyable} subclass), as long as it is an L{pb.Error} subclass it receives the same special treatment. c|jtj|jtj |j |jy)Nrfr"rrlrrrmrs rfailureSecurityz.failureSecurity,s> IIm $  $ $TYY 4  ! !$**dii 8rsecurityrr)r(rs` rtest_securityFailurez#PBFailureTests.test_securityFailure%s  ~~j$@@rc4fd}jdd|S)z Test that a Deferred which fails with a L{pb.Error} which is not also a L{pb.Jellyable} is treated in the same way as a synchronously raised exception of the same type. c|jtj|jtj |j |jy)Nrrs rfailureDeferredSecurityzEPBFailureTests.test_deferredSecurity..failureDeferredSecurity;s> IIm $  $ $TYY 4  ! !$**dii 8rdeferredSecurityrr)r(rs` rtest_deferredSecurityz$PBFailureTests.test_deferredSecurity4s  ~~0%9PQQrc>fd}jdd|tS)z Test that attempting to call a method which is not defined correctly results in an AttributeError on the calling side. c||jtjj|jdy)Nrd)rfr NoSuchMethodrgrhrs r failureNoSuchz>PBFailureTests.test_noSuchMethodFailure..failureNoSuchIs) IIboo & LL)B Crnosuchr)rAttributeError)r(rs` rtest_noSuchMethodFailurez'PBFailureTests.test_noSuchMethodFailureCs   ~~h}nMMrcjj}d}|j|fd}|j|jj y)z Test that a copied failure received from a PB call can be logged locally. Note: this test needs some serious help: all it really tests is that log.err(copiedFailure) doesn't raise an exception. c$|jdS)Nr)rt)rootObjs r connectedz;PBFailureTests.test_copiedFailureLogging..connectedZs%%&<= =rctj|jt}j t |dy)N)rrnrirrjrk)failureror(s r exceptionz;PBFailureTests.test_copiedFailureLogging..exception_s4 GGG ))*>?D   SY *rN)rYrzr?rur^rq)r(rBrrs` rtest_copiedFailureLoggingz(PBFailureTests.test_copiedFailureLoggingPsM    , , . > i  + Y rcJtjtd}tjtj|t }gfd}|}|j djt|j|jtdd}j|jttjdj|jdj|j d y) z L{pb.CopiedFailure.throwExceptionIntoGenerator} will throw a L{RemoteError} into the given paused generator at the point where it last yielded. fooinvokerc3K djdy#tj$r}j|Yd}~yd}~wwxYww)NzRemoteError not raised)r&r RemoteErrorappend)rrr(s r generatorFunczFPBFailureTests.test_throwExceptionIntoGenerator..generatorFuncqsF 4  23>> &  %% &s)A A A AA A  A Nrerascii)rrd)r CopyableFailurerrunjelly DummyInvokersend assertRaises StopIterationthrowExceptionIntoGeneratorrjrk remoteTyperencodeargsremoteTraceback)r(originalcopyrgenrrs` @r test_throwExceptionIntoGeneratorz/PBFailureTests.test_throwExceptionIntoGeneratorgs %%nU&;<}}U[[<>JK  4o  -)I)I3O Y+l n)=)D)DW)MN 8, ,,.GHrr7)rrrr TestCaserjrgr{r~rrrrrrrrrrrrrrrrrrrarass++G&YWRP0 9 J K D A R N.Irrac<eZdZejj ZdZy)PBFailureUnsafeTestsreN)rrrr r failIfEqualsrgrZrrrrrs,,GrrceZdZdZdZy)rzZ A behaviorless object to be used as the invoker parameter to L{jelly.jelly}. N)rrrrserializingPerspectiverrrrrs "rrc"eZdZdZdZdZdZy)FailureJellyingTestsz: Tests for the interaction of jelly and failures. ctjt}|j|j tt|j|j t t t jt j |t}|j|j tt|j|j t t y)z An unjellied L{CopyableFailure} has a check method which behaves the same way as the original L{CopyableFailure}'s check method. rN r rZeroDivisionErrorassertIscheckArithmeticErrorrrr)r(rcopieds rtest_unjelliedFailureCheckz/FailureJellyingTests.test_unjelliedFailureChecks %%&7&9: hnn%679JK hnn_5Gu{{8\^LM fll#457HI fll?3_Ercxtjt}|j|j tt|j|j t t t jt j |t}tj|}t jt j |t}|j|j tt|j|j t t y)aX The object which results from jellying a L{CopyableFailure}, unjellying the result, creating a new L{CopyableFailure} from the result of that, jellying it, and finally unjellying the result of that has a check method which behaves the same way as the original L{CopyableFailure}'s check method. rNr)r(r copiedOnce derivative copiedTwices rtest_twiceUnjelliedFailureCheckz4FailureJellyingTests.test_twiceUnjelliedFailureChecks%%&7&9: hnn%679JK hnn_5G]]5;;x#PQ '' 3 mmEKK LN$ST  k''(9:?u{{8\^LMf%O MTT   ):;rN)rrrrrrr rrrrrs FK"rs8"'--7" I9rxx ""BHHbmm  BHHbmm *j1-7""=19+9+x ) 2 2 ) X&& "fI^fIR> ""1<8,,1