ϪfmJdZddlZddlZddlmZddlmZmZmZddl m Z ddl m Z GddejZGd d Zd ZGd d ZGddZGddZGddZGddZdZGdde ZGdde ZGddej2ej4ZGddej2ej4ZGdd e Zy)!z/ Test cases for L{jelly} object serialization. N)StringTransport)bananajellypb)unittest)TestCaseceZdZdZdZddZy)TestNodezD An object to test jellyfying of new style class instances. Nc|r0|jdz|_|jj|nd|_||_g|_y)N)idchildrenappendparent)selfrs @/usr/lib/python3/dist-packages/twisted/spread/test/test_jelly.py__init__zTestNode.__init__s; ii!mDG OO " "4 (DG  N)__name__ __module__ __qualname____doc__ classAttrrrrr r sIrr ceZdZdZdZy)A Dummy class. cyz; Method to be used in serialization tests. Nrrs ramethodz A.amethod(rN)rrrrr#rrrrr#  rrcy)z: A dummy function to test function serialization. Nrr"s rafuncr'.r$rceZdZdZdZy)Brcyr!rr"s rbmethodz B.bmethod9r$rN)rrrrr+rrrr)r)4r%rr)ceZdZdZdZy)Crcyr!rr"s rcmethodz C.cmethodDr$rN)rrrrr/rrrr-r-?r%rr-ceZdZdZy)Dz Dummy new-style class. N)rrrrrrrr1r1Jsrr1c(eZdZdZdZddZdZdZy)Ez+ Dummy new-style class with slots. xyNc ||_||_yrr4rr5r6s rrz E.__init__Wrc4|j|jdS)Nr4r4r"s r __getstate__zE.__getstate__[sVV$&&))rc,|d|_|d|_y)Nr5r6r4rstates r __setstate__zE.__setstate__^sssr)NN)rrrr __slots__rr;r?rrrr3r3PsI*rr3ceZdZdZdZy)SimpleJellyTestc ||_||_yrr4r8s rrzSimpleJellyTest.__init__dr9rc4|j|jk(Sr)__dict__rothers r isTheSameAszSimpleJellyTest.isTheSameAshs}}..rN)rrrrrHrrrrBrBcs /rrBctj|}tj|}tj|}tj|}|j ||y)zt Verify that the given object round-trips through jelly & banana and comes out equivalent to the input. N)rrencodedecodeunjelly assertEqual)testCase toSerializejelliedencodeddecoded unjellieds rjellyRoundTriprTlsL kk+&GmmG$GmmG$G g&I i0rceZdZdZdgdgdgdgdgZdZdZd Zd Zd Z d Z d Z dZ dZ dZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZd Zd!Zd"Z d#Z!d$Z"y%)& JellyTestsz Testcases for L{jelly} module serialization. @cvar decimalData: serialized version of decimal data, to be used in tests. @type decimalData: L{list} slist)decimali)rWrr)rW@r)rWic8tj|}tj}|jtj|||jj ||j tjtj||y)z Helper test method to test security options for a type. @param inputList: a sample input for the type. @type inputList: L{list} @param atom: atom identifier for the type. @type atom: L{str} N)rSecurityOptionsallowBasicTypesrL allowedTypespop assertRaises InsecureJelly)r inputListatomctasters r _testSecurityzJellyTests._testSecuritysl KK "&&(  a % %--u}}aHrcDt}t}t}|j|_||_tj}t` |j ttjtj||t_y#|t_wxYw)z If a class change after an instance has been created, L{jelly.unjelly} shoud raise a C{TypeError} when trying to unjelly the instance. N) rr)r-r/r+ar` TypeErrorrrL)rrhbrd savecmethods rtest_methodsNotSelfIdentityz&JellyTests.test_methodsNotSelfIdentitysk C C CII ii I $   i A G#AI AIs 8B BcZt}d|_t}||_||_t j|}t j |}|j |t|j|j|j|j|jdy)z Test that a new style class can be jellied and unjellied with its objects and attribute values preserved. r N) r1r5n2n3rrLassertIsInstanceassertIsrM)rnrnrdms r test_newStylezJellyTests.test_newStyles| C S KKN MM!  a# addADD! a rct}d|_tj|}tj|}|j |t|j |jdy)z A class defined with I{slots} can be jellied and unjellied with the values for its attributes preserved. r N)r3r5rrLrprM)rrrrdrss rtest_newStyleWithSlotsz!JellyTests.test_newStyleWithSlotssQ C KKN MM!  a# a rctg}tjtj|}|j||y)zm Test that a new style class type can be jellied and unjellied to the original type. N)r1rrLrMrtrs rtest_typeNewStylezJellyTests.test_typeNewStyles1 C MM%++a. ) Arctg}tjtj|}|j||y)ze Test that a builtin type can be jellied and unjellied to the original type. N)strrrLrMrxs rtest_typeBuiltinzJellyTests.test_typeBuiltins1 E MM%++a. ) Arc&tjj}tjj|z }||g}tj|}tj|}|j |||j ||y)z Jellying L{datetime.timedelta} instances and then unjellying the result should produce objects which represent the values of the original inputs. N)datetimenowrrLrM assertIsNot)rdtndtdrbrdoutputs r test_dateTimezJellyTests.test_dateTimesv ##%##%+#J KK "q! F+ F+rc6tjddd}tjdddd}tjj||}|tjdddz }t ||t ||t ||t ||y ) z Jellying L{datetime.time}, L{datetime.timedelta}, L{datetime.datetime}, and L{datetime.date} objects should result in jellied objects which can be serialized and unserialized with banana. i r iXN)rdatetimecombinerT)r sampleDate sampleTimesampleDateTimesampleTimeDeltas rtest_bananaTimeTypeszJellyTests.test_bananaTimeTypess ]]4B/ ]]1b!S1 !**22:zJ(8+<+ ?rc\Gddat}t|}t|}t||f}||||||d|i}tjtj|d}|j |j dj |j dj y)Nc$eZdZdZdZdZdZy) TupleStatec||_yrrGrFs rrzTupleState.__init__s " rc|jfSrrr"s rr;zTupleState.__getstate__s }$rc|d|_yrrr=s rr?zTupleState.__setstate__s "1X rc,t|jSr)hashrGr"s r__hash__zTupleState.__hash__sDJJ''rN)rrrrr;r?rrrrrrs # % & (rrt3rr )rrrrLrqrG)rrht1t2rdt3primes r test_setStatezJellyTests.test_setStates ( ( C ] ] R ! RRT2 .-- A/5 gmmA&,,gmmA.>.D.DErctj}|jttt}t }t }||_||_||_||_tj||}tj||}|j|jtjtj|}|jtjtj|||j|j|j dtjt|}tj||}|j|td|zy)zA Test for class-level security of serialization. zIdentity mismatchzA came back: %sN)rr\allowInstancesOfrr)r-rjrdr5rLrp Unpersistabler`rarq)rrerhrjrdfriendlyr5means rtest_classSecurityzJellyTests.test_classSecuritys&&(1% C C C;;q&) MM(F + acc5#6#67{{1~ %--u}}dFK acc133 34;;q&) MM(F + a-12rcbGddtj}tj|tj|}d|_tj tj|}|j |jd|j|tjy)zr Test that if Unjellyable is used to deserialize a jellied object, state comes out right. c eZdZy)7JellyTests.test_unjellyable..JellyableTestClassNrrrrrrJellyableTestClassr rrvalueN)r JellyablesetUnjellyableForClass Unjellyable attributerLrMrp)rrinputrs rtest_unjellyablezJellyTests.test_unjellyables}    $$%79J9JK"$!u{{512 ))73 fe&7&78rcidg}|fd}|fd}tdd}tdd}tdd}||_||_||_tj|| }tj|| }|j |j|jj|j |d d |j |j|jd y)Nr cJ|ddz|d<||d|d<t|dS)Nr r)r})objjelpersts rpersistentStorez:JellyTests.test_persistentStorage..persistentStores3Qx!|E!H!$E!HU1X uQx= rc(t|}|d|Sr)int)pidstrunjrpids rpersistentLoadz9JellyTests.test_persistentStorage..persistentLoadsf+C8C= rrrr r)r)rrzpersistentStore was not called.z$Persistent storage identity failure.)rBrjrdrrLrqr) rrrrrhrjrdrr5s rtest_persistentStoragez!JellyTests.test_persistentStoragesQ,1 ! /4 ! Aq ! Aq ! Aq !kk!_= MM#n = acc13355! a"CD acc133 FGrct}t|}t|t|tj|}tj|}|j||yr)r rrL_check_newstyle)rrrn1rrss rtest_newStyleClassesAttributesz)JellyTests.test_newStyleClassesAttributessK J a[  kk!n MM#  Q"rc|j|j|j|j|jd|j|jd|jt|jt|jt |j|jD]\}}|j ||y)Nr )rMrrlenrzipr)rrhrjr5r6s rrzJellyTests._check_newstyles qtt$ a( a( QZZ#ajj/: AJJ/ 'DAq  A & 'rctj}tj}|jt t j ||}tj}|jt t j ||}|j|j|jy)a A L{pb.Referenceable} instance jellies to a structure which unjellies to a L{pb.RemoteReference}. The C{RemoteReference} has a I{luid} that matches up with the local object key in the L{pb.Broker} which sent the L{Referenceable}. invokerN) r ReferenceableBrokermakeConnectionrrrLassertInluid localObjects)rref jellyBrokerj unjellyBrokerrs rtest_referenceablezJellyTests.test_referenceables iik ""?#45 KK[ 1 $$_%67 ]]1m 4 bgg{778rN)#rrrrrrfrlrtrvr{r~rrrrrrrrrrrrrrrrrrrrrrrrrrrrVrVxs  KI&$"! ! , .,"+*2 , ,.4 * "+ *%K?0F0389 H6 #'9rrVceZdZdZdZdZy)JellyDeprecationTestsz+ Tests for deprecated Jelly things ctj|j|jg}|j t |d|j |ddd|j |ddt y)zD L{jelly.instance_atom} is deprecated since 15.0.0. r rmessagezltwisted.spread.jelly.instance_atom was deprecated in Twisted 15.0.0: instance_atom is unused within Twisted.categoryN)r instance_atom flushWarningstest_deprecatedInstanceAtomrMrDeprecationWarningrwarningss rrz1JellyDeprecationTests.test_deprecatedInstanceAtom.su %%t'G'G&HI X*  QK " > !Z02DErctjdddgdgg|j}|jt |d|j|ddd|j|dd t y ) zI Unjellying the instance atom is deprecated with 15.0.0. instanceclassz twisted.spread.test.test_jelly.A dictionaryr rrztUnjelly support for the instance atom is deprecated since Twisted 15.0.0. Upgrade peer for modern instance support.rN)rrLrrMrrrs r%test_deprecatedUnjellyingInstanceAtomz;JellyDeprecationTests.test_deprecatedUnjellyingInstanceAtom<s  '#EF W %%' X*  QK " I !Z02DErN)rrrrrr%rrrrr)s FFrrceZdZdZy)ClassAc$t||_yr)ClassBrr"s rrzClassA.__init__Ns $<rNrrrrrrrr'r'Ms rr'ceZdZdZy)r)c||_yr)r)rrs rrzClassB.__init__Ss rNr*rrrr)r)Rsrr)c(eZdZdZdZdZdZdZy)CircularReferenceTestszN Tests for circular references handling in the jelly/unjelly process. c*tjtttjtttjtjt}|j |j j |dy)N,Identity not preserved in circular reference)rrr'r)rLrqr)rrhs rtest_simpleCirclez(CircularReferenceTests.test_simpleCircle\sT $$VV4 $$VV4 MM%++fh/ 0 aeeii$RSrcfGdd}|}d|_t}tjtttjtttj||}tj |}|j |jj|dy)Nc eZdZy)HCircularReferenceTests.test_circleWithInvoker..DummyInvokerClassNrrrrDummyInvokerClassr4crrr5r r0)serializingPerspectiver'rrr)rLfailUnlessIdenticalr)rr5 dummyInvokera0ra1s rtest_circleWithInvokerz-CircularReferenceTests.test_circleWithInvokerbs  )* .2 + X $$VV4 $$VV4 KKL 1 ]]1    FFJJJ rc.t}t|d}|j|tjtj|}|j |j t|jt|j |gy)z Check that a C{set} can contain a circular reference and be serialized and unserialized without losing the reference. N) setrBaddrrLrpr5rMr)rrrhress rrzCircularReferenceTests.test_setqsf E At $ ammEKKN+ ceeS) ceese,rctdd}t|g}||_tjtj|}|j |jt|j t|j|gy)z Check that a L{frozenset} can contain a circular reference and be serialized and unserialized without losing the reference. N)rBrr5rrLrprMr)rrhrr?s rrz%CircularReferenceTests.test_frozenset}sf D$ ' qcNmmEKKN+ ceeY/ ceese,rN)rrrrr1r;rrrrrr.r.WsT   - -rr.)rrrtwisted.internet.testingrtwisted.spreadrrr twisted.trialrtwisted.trial.unittestrrr rr'r)r-r1r3rBrTrVrCopyable RemoteCopyr'r)r.rrrrGs 4,,"+u"        &// 1n9n9b !FH!FH R[["-- R[["-- 0-X0-r