o
    ¯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   ú@/usr/lib/python3/dist-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                   @   ó   e Zd ZdZdd„ ZdS )ÚAú
    Dummy class.
    c                 C   ó   dS ©z;
        Method to be used in serialization tests.
        Nr   ©r   r   r   r   Úamethod(   ó    z	A.amethodN)r   r   r   r   r   r   r   r   r   r   #   ó    r   c                 C   r   )z:
    A dummy function to test function serialization.
    Nr   r   r   r   r   Úafunc.   r    r"   c                   @   r   )ÚBr   c                 C   r   r   r   r   r   r   r   Úbmethod9   r    z	B.bmethodN)r   r   r   r   r$   r   r   r   r   r#   4   r!   r#   c                   @   r   )ÚCr   c                 C   r   r   r   r   r   r   r   ÚcmethodD   r    z	C.cmethodN)r   r   r   r   r&   r   r   r   r   r%   ?   r!   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   ó   || _ || _d S r   r)   ©r   r*   r+   r   r   r   r   W   ó   
z
E.__init__c                 C   s   | j | jdœS )Nr)   r)   r   r   r   r   Ú__getstate__[   ó   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/   r3   r   r   r   r   r(   P   s    
r(   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚSimpleJellyTestc                 C   r,   r   r)   r-   r   r   r   r   d   r.   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   r9   r   r   r   r   r5   c   s    r5   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)ÚtestCaseÚtoSerializeÚjelliedÚencodedÚdecodedÚ	unjelliedr   r   r   ÚjellyRoundTripl   s
   



rD   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ã  éþÿÿÿ)rF   r   r   )rF   é@â r   )rF   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   ÚSecurityOptionsÚallowBasicTypesr<   ÚallowedTypesÚpopÚassertRaisesÚInsecureJelly)r   Ú	inputListÚatomÚcÚtasterr   r   r   Ú_testSecurityˆ   s   

zJellyTests._testSecurityc                 C   sV   t ƒ }tƒ }tƒ }|j|_||_tj}t`z|  ttj	t |¡¡ W |t_dS |t_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$   ÚarN   Ú	TypeErrorr   r<   )r   rU   ÚbrR   Ú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*   Ún2Ún3r   r<   ÚassertIsInstanceÚassertIsr=   )r   ÚnrZ   rR   Ú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   r<   r\   r=   )r   r^   rR   r_   r   r   r   Útest_newStyleWithSlots¼   s   

z!JellyTests.test_newStyleWithSlotsc                 C   ó&   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   r<   r=   ©r   ÚtÚrr   r   r   Útest_typeNewStyleÈ   ó   zJellyTests.test_typeNewStylec                 C   rb   )ze
        Test that a builtin type can be jellied and unjellied to the original
        type.
        N)Ústrr   r<   r=   rc   r   r   r   Útest_typeBuiltinÑ   rg   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)ÚdatetimeÚnowr   r<   r=   ÚassertIsNot)r   ÚdtnÚdtdrP   rR   Ú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)rj   ÚdateÚtimeÚcombinerD   )r   Ú
sampleDateÚ
sampleTimeÚsampleDateTimeÚ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   rH   ú-78.901N)ÚdecimalÚDecimalr   r<   r=   rl   )r   rP   rR   ro   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.
        r~   r   rH   r   N)r€   r�   r   r<   ÚdecimalDatar=   )r   Úexpectedro   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.
        r~   rF   N)r€   r�   rT   ©r   rP   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
   é   ru   N)r   r<   r=   rl   ©r   rP   ro   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
   r‰   ru   N)Ú	frozensetr   r<   r=   rl   rŠ   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.
        rˆ   s   setN)rT   r†   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.
        rŒ   s	   frozensetN)r�   rT   r†   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.
        rU   rW   r
   r‰   N)Ú
assertTruer5   r9   r   r<   )r   rU   ÚcerealrW   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   r]   r   r<   )r   r*   r+   ÚsÚzr   r   r   Útest_identityZ  s   



zJellyTests.test_identityc                 C   s8   d}t  t   |¡¡}|  ||¡ |  t|ƒt|ƒ¡ d S )NÚblah)r   r<   r=   Ú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   r<   r]   )r   ÚrerefÚtoplevelTupler”   r•   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   rU   rd   r”   r•   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   rJ   rN   rO   r<   )r   rS   Údctr   r   r   Útest_typeSecurity€  s   
zJellyTests.test_typeSecurityc                 C   s   t  t¡}|  t|¡ d S r   )r   r<   r'   r]   )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
        rŒ   r
   ÚtestgÍÌÌÌÌL4@NrU   )r   r   r<   r   r"   Úboolr   r=   )r   rU   Ú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   ó
   || _ d S r   ©r8   r7   r   r   r   r   ¨  ó   
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¬   r1   r   r   r   r3   ®  r0   zTupleState.__setstate__c                 S   s
   t | jƒS r   )Úhashr8   r   r   r   r   Ú__hash__±  r­   zTupleState.__hash__N)r   r   r   r   r/   r3   r¯   r   r   r   r   rª   §  s
    rª   Út3r   r
   )rª   r   r   r<   r]   r8   )r   rU   Út1Út2r°   ÚdÚ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   rJ   ÚallowInstancesOfr   r#   r%   rW   rR   r*   r<   r\   ÚUnpersistablerN   rO   r]   )r   rS   rU   rW   rR   Úfriendlyr*   Ú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                   @   ó   e Zd ZdS )z7JellyTests.test_unjellyable.<locals>.JellyableTestClassN©r   r   r   r   r   r   r   ÚJellyableTestClassÞ  ó    r½   ÚvalueN)r   Ú	JellyableÚsetUnjellyableForClassÚUnjellyableÚ	attributer<   r=   r\   )r   r½   Úinputro   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   )rh   )ÚobjÚjelÚperstr   r   r   ÚpersistentStoreë  s   z:JellyTests.test_persistentStorage.<locals>.persistentStorec                 S   s   t | ƒ}|d | S rŸ   )Úint)ÚpidstrÚunjrÈ   Úpidr   r   r   ÚpersistentLoadð  s   z9JellyTests.test_persistentStorage.<locals>.persistentLoadr‰   ru   r	   rt   é   )rÉ   )rÎ   r   zpersistentStore was not called.z$Persistent storage identity failure.)r5   rW   rR   r   r<   r]   r‘   )	r   rÈ   rÉ   rÎ   rU   rW   rR   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   r<   Ú_check_newstyle)r   r^   Ún1rÇ   r_   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 ]
\}}|  ||¡ q)d S )Nr	   )r=   r   r   Úlenr   ÚziprÑ   )r   rU   rW   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}.
        ©ÚinvokerN)
r   ÚReferenceableÚBrokerÚmakeConnectionr   r   r<   ÚassertInÚluidÚlocalObjects)r   ÚrefÚjellyBrokerÚjÚunjellyBrokerr£   r   r   r   Útest_referenceable  s   zJellyTests.test_referenceableN)#r   r   r   r   rƒ   rT   rY   r`   ra   rf   ri   rp   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   rE   x   sL    û						rE   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   Úinstance_atomÚflushWarningsÚtest_deprecatedInstanceAtomr=   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.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   r<   rç   r=   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                   @   ó   e Zd Zdd„ ZdS )ÚClassAc                 C   s   t | ƒ| _d S r   )ÚClassBrÞ   r   r   r   r   r   N  r0   zClassA.__init__N©r   r   r   r   r   r   r   r   rñ   M  ó    rñ   c                   @   rð   )rò   c                 C   r«   r   )rÞ   )r   rÞ   r   r   r   r   S  r­   zClassB.__init__Nró   r   r   r   r   rò   R  rô   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ò   r<   r]   rÞ   )r   rU   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                   @   r»   )zHCircularReferenceTests.test_circleWithInvoker.<locals>.DummyInvokerClassNr¼   r   r   r   r   ÚDummyInvokerClassc  r¾   rø   rÖ   rö   )ÚserializingPerspectiverñ   r   rÁ   rò   r<   ÚfailUnlessIdenticalrÞ   )r   rø   ÚdummyInvokerÚa0rà   Ú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)	Úsetr5   Úaddr   r<   r\   r*   r=   rš   )r   r”   rU   Úresr   r   r   r‹   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)r5   r�   r*   r   r<   r\   r=   rš   )r   rU   r”   r  r   r   r   rŽ   }  s   

z%CircularReferenceTests.test_frozensetN)r   r   r   r   r÷   rþ   r‹   rŽ   r   r   r   r   rõ   W  s    rõ   )r   rj   r€   Útwisted.spreadr   r   r   Útwisted.test.proto_helpersr   Útwisted.trialr   Útwisted.trial.unittestr   rÀ   r   r   r"   r#   r%   r'   r(   r5   rD   rE   rã   ÚCopyableÚ
RemoteCopyrñ   rò   rõ   r   r   r   r   Ú<module>   s0   	   4$