
    VhmJ                        d Z ddlZddlZddlmZ ddlmZmZmZ ddl	m
Z
 ddlmZ  G d dej                        Z G d	 d
      Zd Z G d d      Z G d d      Z G d d      Z G d d      Z G d d      Zd Z G d de      Z G d de      Z G d dej2                  ej4                        Z G d dej2                  ej4                        Z G d d e      Zy)!z/
Test cases for L{jelly} object serialization.
    N)StringTransport)bananajellypb)unittest)TestCasec                       e Zd ZdZdZddZy)TestNodezD
    An object to test jellyfying of new style class instances.
       Nc                     |r0|j                   dz   | _         |j                  j                  |        nd| _         || _        g | _        y )N   )idchildrenappendparent)selfr   s     N/home/dcms/DCMS/lib/python3.12/site-packages/twisted/spread/test/test_jelly.py__init__zTestNode.__init__   s;    ii!mDGOO""4(DG    N)__name__
__module____qualname____doc__	classAttrr    r   r   r
   r
      s     Ir   r
   c                       e Zd ZdZd Zy)A
    Dummy class.
    c                      yz;
        Method to be used in serialization tests.
        Nr   r   s    r   amethodz	A.amethod(       r   N)r   r   r   r   r#   r   r   r   r   r   #       r   r   c                      y)z:
    A dummy function to test function serialization.
    Nr   r"   s    r   afuncr'   .   r$   r   c                       e Zd ZdZd Zy)Br   c                      yr!   r   r"   s    r   bmethodz	B.bmethod9   r$   r   N)r   r   r   r   r+   r   r   r   r)   r)   4   r%   r   r)   c                       e Zd ZdZd Zy)Cr   c                      yr!   r   r"   s    r   cmethodz	C.cmethodD   r$   r   N)r   r   r   r   r/   r   r   r   r-   r-   ?   r%   r   r-   c                       e Zd ZdZy)Dz 
    Dummy new-style class.
    N)r   r   r   r   r   r   r   r1   r1   J   s    r   r1   c                   (    e Zd ZdZdZddZd Zd Zy)Ez+
    Dummy new-style class with slots.
    xyNc                      || _         || _        y r   r4   r   r5   r6   s      r   r   z
E.__init__W       r   c                 4    | j                   | j                  dS )Nr4   r4   r"   s    r   __getstate__zE.__getstate__[   s    VV$&&))r   c                 ,    |d   | _         |d   | _        y )Nr5   r6   r4   r   states     r   __setstate__zE.__setstate__^   s    ssr   )NN)r   r   r   r   	__slots__r   r;   r?   r   r   r   r3   r3   P   s     I*r   r3   c                       e Zd Zd Zd Zy)SimpleJellyTestc                      || _         || _        y r   r4   r8   s      r   r   zSimpleJellyTest.__init__d   r9   r   c                 4    | j                   |j                   k(  S r   )__dict__r   others     r   isTheSameAszSimpleJellyTest.isTheSameAsh   s    }}..r   N)r   r   r   r   rH   r   r   r   rB   rB   c   s    /r   rB   c                     t        j                   |      }t        j                  |      }t        j                  |      }t        j                  |      }| j                  ||       y)zt
    Verify that the given object round-trips through jelly & banana and comes
    out equivalent to the input.
    N)r   r   encodedecodeunjellyassertEqual)testCasetoSerializejelliedencodeddecoded	unjellieds         r   jellyRoundTriprT   l   sL    
 kk+&GmmG$GmmG$Gg&Ii0r   c                       e Zd ZdZdg dg dg dg dgZd Zd Zd	 Zd
 Zd Z	d Z
d Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd  Zd! Zd" 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}
    s   list)   decimali  )rW   r   r   )rW   @ r   )rW   ic                 8   t        j                   |      }t        j                         }|j                          t        j                  ||       |j                  j                  |       | j                  t         j                  t         j                  ||       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)r   SecurityOptionsallowBasicTypesrL   allowedTypespopassertRaisesInsecureJelly)r   	inputListatomctasters        r   _testSecurityzJellyTests._testSecurity   sl     KK	"&&( a %%--u}}aHr   c                 D   t               }t               }t               }|j                  |_        ||_        t        j                  }t        `	 | j                  t        t        j                  t        j                  |             |t        _        y# |t        _        w xY w)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)
r   r)   r-   r/   r+   ar`   	TypeErrorr   rL   )r   rh   brd   savecmethods        r   test_methodsNotSelfIdentityz&JellyTests.test_methodsNotSelfIdentity   sk    
 CCCII	iiI	$iAG#AIAIs   8B Bc                 Z   t               }d|_        t               }||_        ||_        t	        j                  |      }t	        j
                  |      }| j                  |t                | j                  |j                  |j                         | j                  |j                  d       y)z
        Test that a new style class can be jellied and unjellied with its
        objects and attribute values preserved.
        r   N)	r1   r5   n2n3r   rL   assertIsInstanceassertIsrM   )r   nrn   rd   ms        r   test_newStylezJellyTests.test_newStyle   s|    
 CSKKNMM!a#addADD!a r   c                     t               }d|_        t        j                  |      }t        j                  |      }| j	                  |t                | j                  |j                  d       y)z
        A class defined with I{slots} can be jellied and unjellied with the
        values for its attributes preserved.
        r   N)r3   r5   r   rL   rp   rM   )r   rr   rd   rs   s       r   test_newStyleWithSlotsz!JellyTests.test_newStyleWithSlots   sQ    
 CKKNMM!a#a r   c                     t         g}t        j                  t        j                  |            }| j                  ||       y)zm
        Test that a new style class type can be jellied and unjellied
        to the original type.
        N)r1   r   rL   rM   r   trs      r   test_typeNewStylezJellyTests.test_typeNewStyle   s1    
 CMM%++a.)Ar   c                     t         g}t        j                  t        j                  |            }| j                  ||       y)ze
        Test that a builtin type can be jellied and unjellied to the original
        type.
        N)strr   rL   rM   rx   s      r   test_typeBuiltinzJellyTests.test_typeBuiltin   s1    
 EMM%++a.)Ar   c                 &   t         j                   j                         }t         j                   j                         |z
  }||g}t        j                  |      }t        j                  |      }| 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)datetimenowr   rL   rM   assertIsNot)r   dtndtdrb   rd   outputs         r   test_dateTimezJellyTests.test_dateTime   sv     ##%##%+#J	KK	"q!F+F+r   c                 6   t        j                  ddd      }t        j                  dddd      }t         j                   j                  ||      }|t        j                   ddd      z
  }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         iX     N)r   datetimecombinerT   )r   
sampleDate
sampleTimesampleDateTimesampleTimeDeltas        r   test_bananaTimeTypeszJellyTests.test_bananaTimeTypes   s     ]]4B/
]]1b!S1
!**22:zJ(8+<+<T1a+HHtZ(tZ(t^,t_-r   c                 D   t        j                  d      t        j                  d      t        j                  d      t        j                  d      g}t        j                  |      }t        j                  |      }| j	                  ||       | j                  ||       y)z
        Jellying L{decimal.Decimal} instances and then unjellying the result
        should produce objects which represent the values of the original
        inputs.
        9.95r   rY   -78.901N)decimalDecimalr   rL   rM   r   )r   rb   rd   r   s       r   test_decimalzJellyTests.test_decimal   sy     OOF#OOAOOF#OOI&	
	 KK	"q!F+F+r   c                 
   t        j                  d      t        j                  d      t        j                  d      t        j                  d      g}t        j                  | j                        }| j                  ||       y)a  
        Unjellying the s-expressions produced by jelly for L{decimal.Decimal}
        instances should result in L{decimal.Decimal} instances with the values
        represented by the s-expressions.

        This test also verifies that L{decimalData} contains valid jellied
        data.  This is important since L{test_decimalMissing} re-uses
        L{decimalData} and is expected to be unable to produce
        L{decimal.Decimal} instances even though the s-expression correctly
        represents a list of them.
        r   r   rY   r   N)r   r   r   rL   decimalDatarM   )r   expectedr   s      r   test_decimalUnjellyzJellyTests.test_decimalUnjelly  sa     OOF#OOAOOF#OOI&	
 t//0*r   c                 T    t        j                  d      g}| j                  |d       y)z
        By default, C{decimal} objects should be allowed by
        L{jelly.SecurityOptions}. If not allowed, L{jelly.unjelly} should raise
        L{jelly.InsecureJelly} when trying to unjelly it.
        r   rW   N)r   r   rf   r   rb   s     r   test_decimalSecurityzJellyTests.test_decimalSecurity  s%     __V,-	9j1r   c                     h dg}t        j                  t        j                   |            }| j                  ||       | j                  ||       y)z
        Jellying C{set} instances and then unjellying the result
        should produce objects which represent the values of the original
        inputs.
           r      r   N)r   rL   rM   r   r   rb   r   s      r   test_setzJellyTests.test_set&  sB     K	u{{956F+F+r   c                     t        g d      g}t        j                  t        j                  |            }| j                  ||       | j	                  ||       y)z
        Jellying L{frozenset} instances and then unjellying the result
        should produce objects which represent the values of the original
        inputs.
        r   r   r   N)	frozensetr   rL   rM   r   r   s      r   test_frozensetzJellyTests.test_frozenset1  sH     y)*	u{{956F+F+r   c                 2    h dg}| j                  |d       y)z
        By default, C{set} objects should be allowed by
        L{jelly.SecurityOptions}. If not allowed, L{jelly.unjelly} should raise
        L{jelly.InsecureJelly} when trying to unjelly it.
        r   s   setN)rf   r   s     r   test_setSecurityzJellyTests.test_setSecurity<  s     K	9f-r   c                 D    t        g d      g}| j                  |d       y)z
        By default, L{frozenset} objects should be allowed by
        L{jelly.SecurityOptions}. If not allowed, L{jelly.unjelly} should raise
        L{jelly.InsecureJelly} when trying to unjelly it.
        r   s	   frozensetN)r   rf   r   s     r   test_frozensetSecurityz!JellyTests.test_frozensetSecurityE  s!     y)*	9l3r   c                    | j                  t        dd      j                  t        dd                   t        dd      }t        j                  |      }t        j                  |      }| j                  |j                  |             y)z%
        Simplest test case.
        rh   rj   r   r   N)
assertTruerB   rH   r   rL   )r   rh   cerealrj   s       r   test_simplezJellyTests.test_simpleN  sj     	C%11/#s2KL	
 Aq!QMM&!a()r   c                 d   g }|}|j                  |       |j                  |       | j                  |d   |d          | j                  |d   d   |       t        j                  |      }t        j                  |      }| j                  |d   |d          | j                  |d   d   |       y)zJ
        Test to make sure that objects retain identity properly.
        r   r   N)r   rq   r   rL   )r   r5   r6   szs        r   test_identityzJellyTests.test_identityZ  s     		adAaD!ad1gq!KKNMM!adAaD!ad1gq!r   c                     d}t        j                  t        j                   |            }| j                  ||       | j                  t        |      t        |             y )Nblah)r   rL   rM   typer8   s      r   test_strzJellyTests.test_stri  sE    MM%++a.)Aa$q'*r   c                     g }d|i|f}|j                  |       t        j                  |      }t        j                  |      }| j                  |d   d   |d          | j                  |d   d   d   |       y )Nlistr   r   r   r   rL   rq   )r   rereftoplevelTupler   r   s        r   test_stressReferencesz JellyTests.test_stressReferenceso  sq     %%0]#KK&MM!ad6lAaD)ad6l1oq)r   c                     g }|f}|j                  |f       t        j                  |      }t        j                  |      }| j                  |d   d   d   |       y Nr   r   )r   rh   ry   r   r   s        r   test_moreReferenceszJellyTests.test_moreReferencesx  sP    D	!KKNMM!ad1gaj!$r   c                     t        j                         }t        j                   i       }| j                  t         j                  t         j                  ||       y)z@
        Test for type-level security of serialization.
        N)r   r\   r`   ra   rL   )r   re   dcts      r   test_typeSecurityzJellyTests.test_typeSecurity  s=     &&(kk"o%--u}}c6Jr   c                 b    t        j                  t              }| j                  t        |       y r   )r   rL   r1   rq   )r   ujs     r   test_newStyleClasseszJellyTests.test_newStyleClasses  s    ]]1ar   c                    t               }t        j                  t        j                  |             t        j                  t        j                  |j                               t        g dt        d       t        d      ddddt         t        ddit         j                  g}|D ]:  }| j                  |t        j                  t        j                  |                   < y)zA
        Test for all types currently supported in jelly
        r   r   testgL4@Nrh   )r   r   rL   r#   r'   boolr   rM   )r   rh   itemsis       r   test_lotsaTypeszJellyTests.test_lotsaTypes  s     Cekk!n%ekk!)),-QKG!HII
  	?AQekk!n =>	?r   c                 \    G d d      a t               }t        |      }t        |      }t        ||f      }||||||d|i}t        j                  t        j                  |            d   }| j	                  |j
                  d   j
                  |j
                  d   j
                         y )Nc                   $    e Zd Zd Zd Zd Zd Zy)
TupleStatec                     || _         y r   rG   rF   s     r   r   zTupleState.__init__  s	    "
r   c                     | j                   fS r   r   r"   s    r   r;   zTupleState.__getstate__  s    

}$r   c                     |d   | _         y r   r   r=   s     r   r?   zTupleState.__setstate__  s    "1X
r   c                 ,    t        | j                        S r   )hashrG   r"   s    r   __hash__zTupleState.__hash__  s    DJJ''r   N)r   r   r   r   r;   r?   r   r   r   r   r   r     s    #%&(r   r   t3r   r   )r   r   r   rL   rq   rG   )r   rh   t1t2r   dt3primes          r   test_setStatezJellyTests.test_setState  s    	( 	( C]]R!RRT2.--A/5gmmA&,,gmmA.>.D.DEr   c                    t        j                         }|j                  t        t               t               }t	               }t               }||_        ||_        ||_        ||_        t        j                   ||      }t        j                  ||      }| j                  |j                  t         j                         t        j                   |      }| j                  t         j                  t         j                  ||       | j                  |j                  |j                  d       t        j                   t        |      }t        j                  ||      }| j                  |t        d|z         y)zA
        Test for class-level security of serialization.
        zIdentity mismatchzA came back: %sN)r   r\   allowInstancesOfr   r)   r-   rj   rd   r5   rL   rp   Unpersistabler`   ra   rq   )r   re   rh   rj   rd   friendlyr5   means           r   test_classSecurityzJellyTests.test_classSecurity  s    &&(1%CCC;;q&)MM(F+acc5#6#67{{1~%--u}}dFKacc133 34;;q&)MM(F+a-12r   c                 b    G d dt         j                        }t        j                  |t         j                          |       }d|_        t        j
                  t        j                   |            }| j                  |j                  d       | j                  |t         j                         y)zr
        Test that if Unjellyable is used to deserialize a jellied object,
        state comes out right.
        c                       e Zd Zy)7JellyTests.test_unjellyable.<locals>.JellyableTestClassNr   r   r   r   r   r   JellyableTestClassr         r   r   valueN)r   	JellyablesetUnjellyableForClassUnjellyable	attributerL   rM   rp   )r   r   inputr   s       r   test_unjellyablezJellyTests.test_unjellyable  s}    	 	 	$$%79J9JK"$!u{{512))73fe&7&78r   c                    i dg}|fd}|fd}t        dd      }t        dd      }t        dd      }||_        ||_        ||_        t        j                  ||	      }t        j                  ||
      }| j                  |j                  |j                  j                         | j                  |d   d       | j                  |j                  |j                  d       y )Nr   c                 J    |d   dz   |d<   | |d   |d   <   t        |d         S )Nr   r   )r}   )objjelpersts      r   persistentStorez:JellyTests.test_persistentStorage.<locals>.persistentStore  s3    Qx!|E!H!$E!HU1XuQx= r   c                 (    t        |       }|d   |   S r   )int)pidstrunjr   pids       r   persistentLoadz9JellyTests.test_persistentStorage.<locals>.persistentLoad  s    f+C8C= r   r   r   r   r      )r   )r   r   zpersistentStore was not called.z$Persistent storage identity failure.)rB   rj   rd   r   rL   rq   r   )	r   r   r   r   rh   rj   rd   r   r5   s	            r   test_persistentStoragez!JellyTests.test_persistentStorage  s    Q,1 	!
 /4 	! Aq!Aq!Aq!kk!_=MM#n=acc13355!a"CDacc133 FGr   c                     t               }t        |      }t        |       t        |       t        j                  |      }t        j                  |      }| j                  ||       y r   )r
   r   rL   _check_newstyle)r   rr   n1r   rs   s        r   test_newStyleClassesAttributesz)JellyTests.test_newStyleClassesAttributes  sK    Ja[kk!nMM#Q"r   c                    | j                  |j                  |j                         | j                  |j                  d       | j                  |j                  d       | j                  t        |j                        t        |j                               t        |j                  |j                        D ]  \  }}| j                  ||        y )Nr   )rM   r   r   lenr   zipr  )r   rh   rj   r5   r6   s        r   r  zJellyTests._check_newstyle  s    qtt$a(a(QZZ#ajj/:

AJJ/ 	'DAq  A&	'r   c                    t        j                         }t        j                         }|j                  t	                      t        j
                  ||      }t        j                         }|j                  t	                      t        j                  ||      }| j                  |j                  |j                         y)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   ReferenceableBrokermakeConnectionr   r   rL   assertInluidlocalObjects)r   refjellyBrokerjunjellyBrokerr   s         r   test_referenceablezJellyTests.test_referenceable  s      iik""?#45KK[1		$$_%67]]1m4bgg{778r   N)#r   r   r   r   r   rf   rl   rt   rv   r{   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r   r   r   rV   rV   x   s     	 KI&$"! 
!,.,"+*2	,	,.4
*"+*%K?0F0389 H6	#'9r   rV   c                       e Zd ZdZd Zd Zy)JellyDeprecationTestsz+
    Tests for deprecated Jelly things
    c                     t         j                   | j                  | j                  g      }| j	                  t        |      d       | j	                  |d   d   d       | j	                  |d   d   t               y)zD
        L{jelly.instance_atom} is deprecated since 15.0.0.
        r   r   messagezltwisted.spread.jelly.instance_atom was deprecated in Twisted 15.0.0: instance_atom is unused within Twisted.categoryN)r   instance_atomflushWarningstest_deprecatedInstanceAtomrM   r  DeprecationWarningr   warningss     r   r  z1JellyDeprecationTests.test_deprecatedInstanceAtom.  su     	%%t'G'G&HIX*QK	">	

 	!Z02DEr   c                     t        j                  dddgdgg       | j                         }| j                  t	        |      d       | j                  |d   d   d       | j                  |d   d	   t
               y
)zI
        Unjellying the instance atom is deprecated with 15.0.0.
        instanceclassz twisted.spread.test.test_jelly.A
dictionaryr   r   r  ztUnjelly support for the instance atom is deprecated since Twisted 15.0.0.  Upgrade peer for modern instance support.r  N)r   rL   r  rM   r  r  r  s     r   %test_deprecatedUnjellyingInstanceAtomz;JellyDeprecationTests.test_deprecatedUnjellyingInstanceAtom<  s     	'#EFW	
 %%'X*QK	"I	

 	!Z02DEr   N)r   r   r   r   r  r%  r   r   r   r  r  )  s    FFr   r  c                       e Zd Zd Zy)ClassAc                 $    t        |       | _        y r   )ClassBr  r"   s    r   r   zClassA.__init__N  s    $<r   Nr   r   r   r   r   r   r   r'  r'  M  s     r   r'  c                       e Zd Zd Zy)r)  c                     || _         y r   )r  )r   r  s     r   r   zClassB.__init__S  s	    r   Nr*  r   r   r   r)  r)  R  s    r   r)  c                   (    e Zd ZdZd Zd Zd Zd Zy)CircularReferenceTestszN
    Tests for circular references handling in the jelly/unjelly process.
    c                 *   t        j                  t        t               t        j                  t        t               t        j                  t        j                   t                           }| j                  |j                  j                  |d       y )N,Identity not preserved in circular reference)r   r   r'  r)  rL   rq   r  )r   rh   s     r   test_simpleCirclez(CircularReferenceTests.test_simpleCircle\  sT    $$VV4$$VV4MM%++fh/0aeeii$RSr   c                 f    G d d      } |       }d |_         t               }t        j                  t        t               t        j                  t        t               t        j                  ||      }t        j
                  |      }| j                  |j                  j                  |d       y )Nc                       e Zd Zy)HCircularReferenceTests.test_circleWithInvoker.<locals>.DummyInvokerClassNr   r   r   r   DummyInvokerClassr4  c  r   r   r5  r	  r0  )serializingPerspectiver'  r   r   r)  rL   failUnlessIdenticalr  )r   r5  dummyInvokera0r  a1s         r   test_circleWithInvokerz-CircularReferenceTests.test_circleWithInvokerb  s    	 	 )*.2+X$$VV4$$VV4KKL1]]1  FFJJJ	
r   c                 .   t               }t        |d      }|j                  |       t        j                  t        j                  |            }| j                  |j                  t                | j                  t        |j                        |g       y)z
        Check that a C{set} can contain a circular reference and be serialized
        and unserialized without losing the reference.
        N)	setrB   addr   rL   rp   r5   rM   r   )r   r   rh   ress       r   r   zCircularReferenceTests.test_setq  sf    
 EAt$	ammEKKN+ceeS)ceese,r   c                    t        dd      }t        |g      }||_        t        j                  t        j                  |            }| j                  |j                  t               | j                  t        |j                        |g       y)z
        Check that a L{frozenset} can contain a circular reference and be
        serialized and unserialized without losing the reference.
        N)rB   r   r5   r   rL   rp   rM   r   )r   rh   r   r?  s       r   r   z%CircularReferenceTests.test_frozenset}  sf    
 D$'qcNmmEKKN+ceeY/ceese,r   N)r   r   r   r   r1  r;  r   r   r   r   r   r.  r.  W  s    T

-
-r   r.  )r   r   r   twisted.internet.testingr   twisted.spreadr   r   r   twisted.trialr   twisted.trial.unittestr   r   r
   r   r'   r)   r-   r1   r3   rB   rT   rV   r  Copyable
RemoteCopyr'  r)  r.  r   r   r   <module>rG     s   
   4 , , " +u "     &/ /	1n9 n9b!FH !FH R[["--  
R[["-- 
0-X 0-r   