
    YfmJ                        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j        ej                  Z G d dej        ej                  Z G d d e          ZdS )!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dS )TestNodezD
    An object to test jellyfying of new style class instances.
       Nc                     |r*|j         dz   | _         |j                            |            nd| _         || _        g | _        d S )N   )idchildrenappendparent)selfr   s     P/var/www/html/env/lib/python3.11/site-packages/twisted/spread/test/test_jelly.py__init__zTestNode.__init__   sI     	i!mDGO""4((((DG    N)__name__
__module____qualname____doc__	classAttrr    r   r   r
   r
      s9          I     r   r
   c                       e Zd ZdZd ZdS )A
    Dummy class.
    c                     dS 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                     dS )z:
    A dummy function to test function serialization.
    Nr   r"   s    r   afuncr'   .   r$   r   c                       e Zd ZdZd ZdS )Br   c                     dS 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dS )Cr   c                     dS 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dS )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dS )Ez+
    Dummy new-style class with slots.
    xyNc                 "    || _         || _        d S r   r4   r   r5   r6   s      r   r   z
E.__init__W       r   c                      | j         | j        dS )Nr4   r4   r"   s    r   __getstate__zE.__getstate__[   s    V$&)))r   c                 :    |d         | _         |d         | _        d S )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   sW          I   * * *    r   r3   c                       e Zd Zd Zd ZdS )SimpleJellyTestc                 "    || _         || _        d S r   r4   r8   s      r   r   zSimpleJellyTest.__init__d   r9   r   c                 "    | 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   s2          / / / / /r   rB   c                     t          j         |          }t          j        |          }t          j        |          }t          j        |          }|                     ||           dS )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   s\    
 k+&&GmG$$GmG$$Gg&&Ii0000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"d%S )&
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                 2   t          j         |          }t          j                    }|                                 t          j        ||           |j                            |           |                     t           j        t           j        ||           dS )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   s     K	""&((   a   %%%%-u}aHHHHHr   c                 R   t                      }t                      }t                      }|j        |_        ||_        t          j        }t          `	 |                     t          t          j	        t          j        |                     |t          _        dS # |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   s    
 CCCCCCI	iI	$iAGGG#AIIIAI####s   8B B&c                 d   t                      }d|_        t                      }||_        ||_        t	          j        |          }t	          j        |          }|                     |t                      |                     |j        |j                   |                     |j        d           dS )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    
 CCSSKNNM!a###adAD!!!a     r   c                     t                      }d|_        t          j        |          }t          j        |          }|                     |t                      |                     |j        d           dS )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   sd    
 CCKNNM!a###a     r   c                     t           g}t          j        t          j        |                    }|                     ||           dS )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   s>    
 CM%+a..))Ar   c                     t           g}t          j        t          j        |                    }|                     ||           dS )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   s>    
 EM%+a..))Ar   c                 4   t           j                                         }t           j                                         |z
  }||g}t          j        |          }t          j        |          }|                     ||           |                     ||           dS )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   s     ##%%##%%+#J	K	""q!!F+++F+++++r   c                 R   t          j        ddd          }t          j        dddd          }t           j                             ||          }|t          j         ddd          z
  }t	          | |           t	          | |           t	          | |           t	          | |           d	S )
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1
!*22:zJJ(8+<T1a+H+HHtZ(((tZ(((t^,,,t_-----r   c                 J   t          j        d          t          j        d          t          j        d          t          j        d          g}t          j        |          }t          j        |          }|                     ||           |                     ||           dS )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   s     OF##OAOF##OI&&	
	 K	""q!!F+++F+++++r   c                     t          j        d          t          j        d          t          j        d          t          j        d          g}t          j        | j                  }|                     ||           dS )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  so     OF##OAOF##OI&&	
 t/00*****r   c                 \    t          j        d          g}|                     |d           dS )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  s2     _V,,-	9j11111r   c                     h dg}t          j        t          j         |                    }|                     ||           |                     ||           dS )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&  sZ     YYK	u{95566F+++F+++++r   c                     t          g d          g}t          j        t          j        |                    }|                     ||           |                     ||           dS )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  sc     yyy))*	u{95566F+++F+++++r   c                 <    h dg}|                      |d           dS )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+     YYK	9f-----r   c                 V    t          g d          g}|                     |d           dS )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  s4     yyy))*	9l33333r   c                 N   |                      t          dd                              t          dd                               t          dd          }t          j        |          }t          j        |          }|                      |                    |                     dS )z%
        Simplest test case.
        rh   rj   r   r   N)
assertTruerB   rH   r   rL   )r   rh   cerealrj   s       r   test_simplezJellyTests.test_simpleN  s     	C%%11/#s2K2KLL	
 	
 	
 Aq!!QM&!!a(()))))r   c                    g }|}|                     |           |                     |           |                     |d         |d                    |                     |d         d         |           t          j        |          }t          j        |          }|                     |d         |d                    |                     |d         d         |           dS )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!!!KNNM!adAaD!!!ad1gq!!!!!r   c                     d}t          j        t          j         |                    }|                     ||           |                     t          |          t          |                     d S )Nblah)r   rL   rM   typer8   s      r   test_strzJellyTests.test_stri  s\    M%+a..))Aa$q''*****r   c                 0   g }d|i|f}|                     |           t          j        |          }t          j        |          }|                     |d         d         |d                    |                     |d         d         d         |           d S )Nlistr   r   r   r   rL   rq   )r   rereftoplevelTupler   r   s        r   test_stressReferencesz JellyTests.test_stressReferenceso  s     %%0]###K&&M!ad6lAaD)))ad6l1oq)))))r   c                     g }|f}|                     |f           t          j        |          }t          j        |          }|                     |d         d         d         |           d S Nr   r   )r   rh   ry   r   r   s        r   test_moreReferenceszJellyTests.test_moreReferencesx  sa    D	!KNNM!ad1gaj!$$$$$r   c                     t          j                    }t          j         i           }|                     t           j        t           j        ||           dS )z@
        Test for type-level security of serialization.
        N)r   r\   r`   ra   rL   )r   re   dcts      r   test_typeSecurityzJellyTests.test_typeSecurity  sD     &((k"oo%-u}c6JJJJJr   c                 n    t          j        t                    }|                     t          |           d S 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 ]<}|                     |t          j        t          j        |                               =dS )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     CCek!nn%%%ek!),,---IIQKGG!HI
  	? 	?AQek!nn = =>>>>	? 	?r   c                 r    G d d          a t                      }t          |          }t          |          }t          ||f          }||||||d|i}t          j        t          j        |                    d         }|                     |j        d         j        |j        d         j                   d S )Nc                   &    e Zd Zd Zd Zd Zd ZdS )
TupleStatec                     || _         d S 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         | _         d S 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    DJ'''r   N)r   r   r   r   r;   r?   r   r   r   r   r   r     sP        # # #% % %& & &( ( ( ( (r   r   t3r   r   )r   r   r   rL   rq   rG   )r   rh   t1t2r   dt3primes          r   test_setStatezJellyTests.test_setState  s    	( 	( 	( 	( 	( 	( 	( 	( CC]]]]R!!RRT2.-A//5gmA&,gmA.>.DEEEEEr   c                    t          j                    }|                    t          t                     t                      }t	                      }t                      }||_        ||_        ||_        ||_        t          j         ||          }t          j	        ||          }| 
                    |j        t           j                   t          j         |          }|                     t           j        t           j	        ||           |                     |j        |j        d           t          j         t          |          }t          j	        ||          }|                     |t          d|z             dS )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%%%CCCCCC;q&))M(F++ac5#6777{1~~%-u}dFKKKac13 3444;q&))M(F++a-122222r   c                 X    G d dt           j                  }t          j        |t           j                    |            }d|_        t          j        t          j         |                    }|                     |j        d           |                     |t           j                   dS )zr
        Test that if Unjellyable is used to deserialize a jellied object,
        state comes out right.
        c                       e Zd ZdS )7JellyTests.test_unjellyable.<locals>.JellyableTestClassNr   r   r   r   r   r   JellyableTestClassr             Dr   r   valueN)r   	JellyablesetUnjellyableForClassUnjellyable	attributerL   rM   rp   )r   r   inputr   s       r   test_unjellyablezJellyTests.test_unjellyable  s    	 	 	 	 	 	 	 	 	$%79JKKK""$$!u{51122)7333fe&78888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                   |                     |d         d           |                     |j        |j        d           d S )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  s5    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!!k!_===M#n===ac135!!!a"CDDDac13 FGGGGGr   c                     t                      }t          |          }t          |           t          |           t          j        |          }t          j        |          }|                     ||           d S r   )r
   r   rL   _check_newstyle)r   rr   n1r   rs   s        r   test_newStyleClassesAttributesz)JellyTests.test_newStyleClassesAttributes  sd    JJa[[k!nnM#Q"""""r   c                    |                      |j        |j                   |                      |j        d           |                      |j        d           |                      t          |j                  t          |j                             t          |j        |j                  D ]\  }}|                     ||           d S )Nr   )rM   r   r   lenr   zipr  )r   rh   rj   r5   r6   s        r   r  zJellyTests._check_newstyle  s    qt$$$a(((a(((QZ#aj//:::
AJ// 	' 	'DAq  A&&&&	' 	'r   c                    t          j                    }t          j                    }|                    t	                                 t          j        ||          }t          j                    }|                    t	                                 t          j        ||          }|                     |j        |j	                   dS )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       ikk""?#4#4555K[111	$$_%6%6777]1m444bg{78888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 I I&$ $ $"! ! ! 
! 
! 
!    , , ,. . ., , ,"+ + +*2 2 2	, 	, 	,	, 	, 	,. . .4 4 4
* 
* 
*" " "+ + +* * *% % %K K K  ? ? ?0F F F03 3 389 9 9 H H H6	# 	# 	#' ' '9 9 9 9 9r   rV   c                       e Zd ZdZd Zd ZdS )JellyDeprecationTestsz+
    Tests for deprecated Jelly things
    c                 ,   t           j         |                     | j        g          }|                     t          |          d           |                     |d         d         d           |                     |d         d         t                     dS )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.  s     	%%t'G&HIIX***QK	">	
 	
 	

 	!Z02DEEEEEr   c                 :   t          j        dddgdgg           |                                 }|                     t	          |          d           |                     |d         d         d           |                     |d         d	         t
                     d
S )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EEEEEr   N)r   r   r   r   r  r%  r   r   r   r  r  )  sD         F F FF F F F Fr   r  c                       e Zd Zd ZdS )ClassAc                 .    t          |           | _        d S 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dS )r)  c                     || _         d S 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dS )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        |d           d S )N,Identity not preserved in circular reference)r   r   r'  r)  rL   rq   r  )r   rh   s     r   test_simpleCirclez(CircularReferenceTests.test_simpleCircle\  sf    $VV444$VV444M%+fhh//00aei$RSSSSSr   c                 p    G d d          } |            }d |_         t                      }t          j        t          t                     t          j        t          t                     t          j        ||          }t          j        |          }|                     |j        j        |d           d S )Nc                       e Zd ZdS )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+XX$VV444$VV444KL111]1  FJJ	
 	
 	
 	
 	
r   c                 J   t                      }t          |d          }|                    |           t          j        t          j        |                    }|                     |j        t                      |                     t          |j                  |g           dS )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  s    
 EEAt$$	amEKNN++ceS)))cese,,,,,r   c                 2   t          dd          }t          |g          }||_        t          j        t          j        |                    }|                     |j        t                     |                     t          |j                  |g           dS )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}  s|    
 D$''qcNNmEKNN++ceY///cese,,,,,r   N)r   r   r   r   r1  r;  r   r   r   r   r   r.  r.  W  s]         T T 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 4 4 4 4 4 , , , , , , , , , , " " " " " " + + + + + +    u   "                                     &/ / / / / / / /	1 	1 	1n9 n9 n9 n9 n9 n9 n9 n9b!F !F !F !F !FH !F !F !FH         R["-      
    R["-   
0- 0- 0- 0- 0-X 0- 0- 0- 0- 0-r   