a
    )¶(boJ  ã                   @   s"  d Z ddlZddlZddlmZmZmZ ddlmZ ddl	m
Z
 ddlmZ G dd„ dejƒZG d	d
„ d
ƒZdd„ ZG dd„ dƒZG dd„ dƒZG dd„ dƒZG dd„ dƒZG dd„ dƒZdd„ ZG dd„ deƒZG dd„ deƒZG dd„ dejejƒZG dd „ d ejejƒZG d!d"„ d"eƒZdS )#z/
Test cases for L{jelly} object serialization.
é    N)ÚbananaÚjellyÚpb)ÚStringTransport)Úunittest)ÚTestCasec                   @   s   e Zd ZdZdZddd„ZdS )ÚTestNodezD
    An object to test jellyfying of new style class instances.
    é   Nc                 C   s4   |r|j d | _ |j | ¡ nd| _ || _g | _d S )Né   )ÚidÚchildrenÚappendÚparent)Úselfr   © r   ú=lib/python3.9/site-packages/twisted/spread/test/test_jelly.pyÚ__init__   s    zTestNode.__init__)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	classAttrr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdd„ ZdS )ÚAú
    Dummy class.
    c                 C   s   dS ©z;
        Method to be used in serialization tests.
        Nr   ©r   r   r   r   Úamethod(   s    z	A.amethodN)r   r   r   r   r   r   r   r   r   r   #   s   r   c                 C   s   dS )z:
    A dummy function to test function serialization.
    Nr   r   r   r   r   Úafunc.   s    r   c                   @   s   e Zd ZdZdd„ ZdS )ÚBr   c                 C   s   dS r   r   r   r   r   r   Úbmethod9   s    z	B.bmethodN)r   r   r   r   r   r   r   r   r   r   4   s   r   c                   @   s   e Zd ZdZdd„ ZdS )ÚCr   c                 C   s   dS r   r   r   r   r   r   ÚcmethodD   s    z	C.cmethodN)r   r   r   r   r!   r   r   r   r   r    ?   s   r    c                   @   s   e Zd ZdZdS )ÚDz 
    Dummy new-style class.
    N)r   r   r   r   r   r   r   r   r"   J   s   r"   c                   @   s.   e Zd ZdZdZd
dd„Zdd„ Zdd	„ ZdS )ÚEz+
    Dummy new-style class with slots.
    ©ÚxÚyNc                 C   s   || _ || _d S ©Nr$   ©r   r%   r&   r   r   r   r   W   s    z
E.__init__c                 C   s   | j | jdœS )Nr$   r$   r   r   r   r   Ú__getstate__[   s    zE.__getstate__c                 C   s   |d | _ |d | _d S )Nr%   r&   r$   ©r   Ústater   r   r   Ú__setstate__^   s    
zE.__setstate__)NN)r   r   r   r   Ú	__slots__r   r)   r,   r   r   r   r   r#   P   s
   
r#   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚSimpleJellyTestc                 C   s   || _ || _d S r'   r$   r(   r   r   r   r   d   s    zSimpleJellyTest.__init__c                 C   s   | j |j kS r'   )Ú__dict__©r   Úotherr   r   r   ÚisTheSameAsh   s    zSimpleJellyTest.isTheSameAsN)r   r   r   r   r2   r   r   r   r   r.   c   s   r.   c                 C   s8   t   |¡}t |¡}t |¡}t  |¡}|  ||¡ 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)ZtestCaseZtoSerializeZjelliedZencodedZdecodedZ	unjelliedr   r   r   ÚjellyRoundTripl   s
    



r7   c                   @   s  e Zd ZdZdg d¢g d¢g d¢g d¢gZdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Zd9d:„ Zd;d<„ Zd=d>„ Z d?d@„ Z!dAdB„ Z"dCS )DÚ
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ã  éþÿÿÿ)r9   r   r   )r9   é@â r   )r9   iËËþÿéýÿÿÿc                 C   sJ   t   |¡}t  ¡ }| ¡  t  ||¡ |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   ÚSecurityOptionsZallowBasicTypesr5   ZallowedTypesÚpopÚassertRaisesÚInsecureJelly)r   Ú	inputListZatomÚcÚtasterr   r   r   Ú_testSecurityˆ   s    

zJellyTests._testSecurityc                 C   sX   t ƒ }tƒ }tƒ }|j|_||_tj}t`z |  ttj	t |¡¡ W |t_n|t_0 dS )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   r5   )r   rE   ÚbrB   Zsavecmethodr   r   r   Útest_methodsNotSelfIdentity›   s    z&JellyTests.test_methodsNotSelfIdentityc                 C   s`   t ƒ }d|_t ƒ }||_||_t |¡}t |¡}|  |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)	r"   r%   Ún2Zn3r   r5   ÚassertIsInstanceÚassertIsr6   )r   ÚnrI   rB   Úmr   r   r   Útest_newStyle¬   s    

zJellyTests.test_newStylec                 C   s>   t ƒ }d|_t |¡}t |¡}|  |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)r#   r%   r   r5   rJ   r6   )r   rL   rB   rM   r   r   r   Útest_newStyleWithSlots¼   s    

z!JellyTests.test_newStyleWithSlotsc                 C   s&   t g}t t |¡¡}|  ||¡ dS )zm
        Test that a new style class type can be jellied and unjellied
        to the original type.
        N)r"   r   r5   r6   ©r   ÚtÚrr   r   r   Útest_typeNewStyleÈ   s    zJellyTests.test_typeNewStylec                 C   s&   t g}t t |¡¡}|  ||¡ dS )ze
        Test that a builtin type can be jellied and unjellied to the original
        type.
        N)Ústrr   r5   r6   rP   r   r   r   Útest_typeBuiltinÑ   s    zJellyTests.test_typeBuiltinc                 C   sP   t j  ¡ }t j  ¡ | }||g}t |¡}t |¡}|  ||¡ |  ||¡ 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)ÚdatetimeZnowr   r5   r6   ÚassertIsNot)r   ZdtnZdtdrA   rB   Úoutputr   r   r   Útest_dateTimeÚ   s    


zJellyTests.test_dateTimec                 C   sj   t  ddd¡}t  dddd¡}t j  ||¡}|t   ddd¡ }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)rV   ÚdateÚtimeZcombiner7   )r   Z
sampleDateZ
sampleTimeZsampleDateTimeZsampleTimeDeltar   r   r   Útest_bananaTimeTypesè   s    


zJellyTests.test_bananaTimeTypesc                 C   sT   t  d¡t  d¡t  d¡t  d¡g}t |¡}t |¡}|  ||¡ |  ||¡ 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   r;   ú-78.901N)ÚdecimalÚDecimalr   r5   r6   rW   )r   rA   rB   rX   r   r   r   Útest_decimal÷   s    ü

zJellyTests.test_decimalc                 C   s@   t  d¡t  d¡t  d¡t  d¡g}t | 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.
        rb   r   r;   rc   N)rd   re   r   r5   ÚdecimalDatar6   )r   ZexpectedrX   r   r   r   Útest_decimalUnjelly  s    üzJellyTests.test_decimalUnjellyc                 C   s   t  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.
        rb   r9   N)rd   re   rD   ©r   rA   r   r   r   Útest_decimalSecurity  s    zJellyTests.test_decimalSecurityc                 C   s6   h d£g}t  t   |¡¡}|  ||¡ |  ||¡ 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   r5   r6   rW   ©r   rA   rX   r   r   r   Útest_set&  s    
zJellyTests.test_setc                 C   s:   t g d¢ƒg}t t |¡¡}|  ||¡ |  ||¡ dS )zª
        Jellying L{frozenset} instances and then unjellying the result
        should produce objects which represent the values of the original
        inputs.
        ©r
   rl   r^   N)Ú	frozensetr   r5   r6   rW   rm   r   r   r   Útest_frozenset1  s    zJellyTests.test_frozensetc                 C   s   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.
        rk   s   setN)rD   ri   r   r   r   Útest_setSecurity<  s    
zJellyTests.test_setSecurityc                 C   s   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.
        ro   s	   frozensetN)rp   rD   ri   r   r   r   Útest_frozensetSecurityE  s    z!JellyTests.test_frozensetSecurityc                 C   sN   |   tddƒ tddƒ¡¡ tddƒ}t |¡}t |¡}|   | |¡¡ dS )z%
        Simplest test case.
        rE   rG   r
   rl   N)Ú
assertTruer.   r2   r   r5   )r   rE   ZcerealrG   r   r   r   Útest_simpleN  s    ÿ


zJellyTests.test_simplec                 C   s„   g }|}|  |¡ |  |¡ |  |d |d ¡ |  |d d |¡ t |¡}t |¡}|  |d |d ¡ |  |d d |¡ dS )zJ
        Test to make sure that objects retain identity properly.
        r   r
   N)r   rK   r   r5   )r   r%   r&   ÚsÚzr   r   r   Útest_identityZ  s    



zJellyTests.test_identityc                 C   s8   d}t  t   |¡¡}|  ||¡ |  t|ƒt|ƒ¡ d S )NZblah)r   r5   r6   Útyper(   r   r   r   Útest_stri  s    zJellyTests.test_strc                 C   sb   g }d|i|f}|  |¡ t |¡}t |¡}|  |d d |d ¡ |  |d d d |¡ d S )NÚlistr   r
   ©r   r   r5   rK   )r   ZrerefZtoplevelTuplerv   rw   r   r   r   Útest_stressReferenceso  s    


z JellyTests.test_stressReferencesc                 C   sF   g }|f}|  |f¡ t |¡}t |¡}|  |d d d |¡ d S ©Nr   r|   )r   rE   rQ   rv   rw   r   r   r   Útest_moreReferencesx  s    

zJellyTests.test_moreReferencesc                 C   s*   t  ¡ }t   i ¡}|  t jt j||¡ dS )z@
        Test for type-level security of serialization.
        N)r   r=   r?   r@   r5   )r   rC   Zdctr   r   r   Útest_typeSecurity€  s    
zJellyTests.test_typeSecurityc                 C   s   t  t¡}|  t|¡ d S r'   )r   r5   r"   rK   )r   Úujr   r   r   Útest_newStyleClassesˆ  s    
zJellyTests.test_newStyleClassesc                 C   s~   t ƒ }t t |¡¡ t t |j¡¡ tg d¢tdƒ tdƒddddt tddit jg}|D ]}|  |t t |¡¡¡ q\dS )zA
        Test for all types currently supported in jelly
        ro   r
   ÚtestgÍÌÌÌÌL4@NrE   )r   r   r5   r   r   Úboolr   r6   )r   rE   ÚitemsÚir   r   r   Útest_lotsaTypesŒ  s$    ôzJellyTests.test_lotsaTypesc                 C   sx   G dd„ dƒa tƒ }t |ƒ}t |ƒ}t ||fƒ}||||||d|i}t t |¡¡d }|  |jd j|jd j¡ d S )Nc                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
Ú
TupleStatec                 S   s
   || _ d S r'   ©r1   r0   r   r   r   r   ¨  s    zTupleState.__init__c                 S   s   | j fS r'   r‰   r   r   r   r   r)   «  s    zTupleState.__getstate__c                 S   s   |d | _ d S r~   r‰   r*   r   r   r   r,   ®  s    zTupleState.__setstate__c                 S   s
   t | jƒS r'   )Úhashr1   r   r   r   r   Ú__hash__±  s    zTupleState.__hash__N)r   r   r   r   r)   r,   r‹   r   r   r   r   rˆ   §  s   rˆ   Út3r   r
   )rˆ   r   r   r5   rK   r1   )r   rE   Út1Út2rŒ   ÚdZt3primer   r   r   Útest_setState¤  s    zJellyTests.test_setStatec                 C   sÄ   t  ¡ }| tt¡ tƒ }tƒ }tƒ }||_||_||_||_t   ||¡}t  	||¡}|  
|jt j¡ t   |¡}|  t jt j	||¡ |  |j|jd¡ t   t|¡}t  	||¡}|  |td| ¡ dS )zA
        Test for class-level security of serialization.
        zIdentity mismatchzA came back: %sN)r   r=   ZallowInstancesOfr   r   r    rG   rB   r%   r5   rJ   ZUnpersistabler?   r@   rK   )r   rC   rE   rG   rB   Zfriendlyr%   Zmeanr   r   r   Útest_classSecurity¼  s$    
zJellyTests.test_classSecurityc                 C   s\   G dd„ dt jƒ}t  |t j¡ |ƒ }d|_t  t   |¡¡}|  |jd¡ |  |t j¡ dS )zr
        Test that if Unjellyable is used to deserialize a jellied object,
        state comes out right.
        c                   @   s   e Zd ZdS )z7JellyTests.test_unjellyable.<locals>.JellyableTestClassN©r   r   r   r   r   r   r   ÚJellyableTestClassÞ  s   r“   ÚvalueN)r   Ú	JellyableÚsetUnjellyableForClassZUnjellyableZ	attributer5   r6   rJ   )r   r“   ÚinputrX   r   r   r   Útest_unjellyableØ  s    zJellyTests.test_unjellyablec           	      C   s¤   i dg}|fdd„}|fdd„}t ddƒ}t ddƒ}t d	d
ƒ}||_||_||_tj||d}tj||d}|  |j|jj¡ |  |d d¡ |  |j|jd¡ d S )Nr
   c                 S   s,   |d d |d< | |d |d < t |d ƒS )Nr
   r   )rT   )ÚobjÚjelÚperstr   r   r   ÚpersistentStoreë  s    z:JellyTests.test_persistentStorage.<locals>.persistentStorec                 S   s   t | ƒ}|d | S r~   )Úint)ZpidstrZunjr›   Úpidr   r   r   ÚpersistentLoadð  s    z9JellyTests.test_persistentStorage.<locals>.persistentLoadrl   r^   r	   r]   é   )rœ   )rŸ   r   zpersistentStore was not called.z$Persistent storage identity failure.)r.   rG   rB   r   r5   rK   rt   )	r   r›   rœ   rŸ   rE   rG   rB   rš   r%   r   r   r   Útest_persistentStorageè  s    


z!JellyTests.test_persistentStoragec                 C   sB   t ƒ }t |ƒ}t |ƒ t |ƒ t |¡}t |¡}|  ||¡ d S r'   )r   r   r5   Ú_check_newstyle)r   rL   Zn1rš   rM   r   r   r   Útest_newStyleClassesAttributes  s    

z)JellyTests.test_newStyleClassesAttributesc                 C   sl   |   |j|j¡ |   |jd¡ |   |jd¡ |   t|jƒt|jƒ¡ t|j|jƒD ]\}}|  ||¡ qRd S )Nr	   )r6   r   r   Úlenr   Úzipr¢   )r   rE   rG   r%   r&   r   r   r   r¢     s    zJellyTests._check_newstylec                 C   s`   t  ¡ }t  ¡ }| tƒ ¡ tj||d}t  ¡ }| tƒ ¡ tj||d}|  |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}.
        ©ZinvokerN)
r   ZReferenceableZBrokerZmakeConnectionr   r   r5   ZassertInZluidZlocalObjects)r   ÚrefZjellyBrokerÚjZunjellyBrokerr   r   r   r   Útest_referenceable  s    zJellyTests.test_referenceableN)#r   r   r   r   rg   rD   rH   rN   rO   rS   rU   rY   ra   rf   rh   rj   rn   rq   rr   rs   ru   rx   rz   r}   r   r€   r‚   r‡   r   r‘   r˜   r¡   r£   r¢   r©   r   r   r   r   r8   x   sJ   û						r8   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚJellyDeprecationTestsz+
    Tests for deprecated Jelly things
    c                 C   sP   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   Zinstance_atomÚflushWarningsÚtest_deprecatedInstanceAtomr6   r¤   ÚDeprecationWarning©r   Úwarningsr   r   r   r®   .  s    
þz1JellyDeprecationTests.test_deprecatedInstanceAtomc                 C   sZ   t  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.AZ
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   r5   r­   r6   r¤   r¯   r°   r   r   r   Ú%test_deprecatedUnjellyingInstanceAtom<  s    ÿ
þz;JellyDeprecationTests.test_deprecatedUnjellyingInstanceAtomN)r   r   r   r   r®   r´   r   r   r   r   rª   )  s   rª   c                   @   s   e Zd Zdd„ ZdS )ÚClassAc                 C   s   t | ƒ| _d S r'   )ÚClassBr§   r   r   r   r   r   N  s    zClassA.__init__N©r   r   r   r   r   r   r   r   rµ   M  s   rµ   c                   @   s   e Zd Zdd„ ZdS )r¶   c                 C   s
   || _ d S r'   )r§   )r   r§   r   r   r   r   S  s    zClassB.__init__Nr·   r   r   r   r   r¶   R  s   r¶   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚCircularReferenceTestszN
    Tests for circular references handling in the jelly/unjelly process.
    c                 C   s@   t  tt¡ t  tt¡ t  t   tƒ ¡¡}|  |jj|d¡ d S )Nú,Identity not preserved in circular reference)r   r–   rµ   r¶   r5   rK   r§   )r   rE   r   r   r   Útest_simpleCircle\  s    z(CircularReferenceTests.test_simpleCirclec                 C   sf   G dd„ dƒ}|ƒ }d |_ tƒ }t tt¡ t tt¡ tj||d}t |¡}|  |jj|d¡ d S )Nc                   @   s   e Zd ZdS )zHCircularReferenceTests.test_circleWithInvoker.<locals>.DummyInvokerClassNr’   r   r   r   r   ÚDummyInvokerClassc  s   r»   r¦   r¹   )ZserializingPerspectiverµ   r   r–   r¶   r5   ZfailUnlessIdenticalr§   )r   r»   ZdummyInvokerZa0r¨   Za1r   r   r   Útest_circleWithInvokerb  s    

ÿz-CircularReferenceTests.test_circleWithInvokerc                 C   sP   t ƒ }t|dƒ}| |¡ t t |¡¡}|  |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)	Úsetr.   Úaddr   r5   rJ   r%   r6   r{   )r   rv   rE   Úresr   r   r   rn   q  s    

zCircularReferenceTests.test_setc                 C   sP   t ddƒ}t|gƒ}||_t t |¡¡}|  |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)r.   rp   r%   r   r5   rJ   r6   r{   )r   rE   rv   r¿   r   r   r   rq   }  s    

z%CircularReferenceTests.test_frozensetN)r   r   r   r   rº   r¼   rn   rq   r   r   r   r   r¸   W  s
   r¸   )r   rV   rd   Ztwisted.spreadr   r   r   Ztwisted.test.proto_helpersr   Ztwisted.trialr   Ztwisted.trial.unittestr   r•   r   r   r   r   r    r"   r#   r.   r7   r8   rª   ZCopyableZ
RemoteCopyrµ   r¶   r¸   r   r   r   r   Ú<module>   s.   	   4$