U
     T‚gmJ  ã                   @   s"  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„ 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)ÚStringTransport)ÚbananaÚjellyÚpb)Ú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   úB/tmp/pip-unpacked-wheel-1_7di8ic/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jelliedÚencodedÚdecodedZ	unjelliedr   r   r   ÚjellyRoundTripl   s
    



r9   c                   @   s&  e Zd ZdZddddgdddgdddgddd	g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d0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ Zd:d;„ Zd<d=„ Zd>d?„ Zd@dA„ Z dBdC„ Z!dDdE„ Z"dFS )GÚ
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ã  éþÿÿÿr   é@â 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   sR   t ƒ }tƒ }tƒ }|j|_||_tj}t`z|  ttj	t |¡¡ W 5 |t_X 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   ÚarA   Ú	TypeErrorr   r5   )r   rG   ÚbrD   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   ÚnrK   rD   Ú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   rL   r6   )r   rN   rD   rO   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   rR   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)ÚdatetimeÚnowr   r5   r6   ÚassertIsNot)r   ZdtnZdtdrC   rD   Ú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)rX   ÚdateÚtimeÚcombiner9   )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   rZ   )r   rC   rD   r[   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.
        rf   r   r=   rg   N)rh   ri   r   r5   ÚdecimalDatar6   )r   Úexpectedr[   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.
        rf   r;   N)rh   ri   rF   ©r   rC   r   r   r   Útest_decimalSecurity  s    zJellyTests.test_decimalSecurityc                 C   s8   dddh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
   é   ra   N)r   r5   r6   rZ   ©r   rC   r[   r   r   r   Útest_set&  s    zJellyTests.test_setc                 C   s<   t dddgƒ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
   rp   ra   N)Ú	frozensetr   r5   r6   rZ   rq   r   r   r   Útest_frozenset1  s    zJellyTests.test_frozensetc                 C   s   dddh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
   rp   ra   s   setN)rF   rn   r   r   r   Útest_setSecurity<  s    zJellyTests.test_setSecurityc                 C   s    t dddgƒ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
   rp   ra   s	   frozensetN)rs   rF   rn   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.
        rG   rI   r
   rp   N)Ú
assertTruer.   r2   r   r5   )r   rG   ZcerealrI   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   rM   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   rM   )r   ZrerefZtoplevelTuplery   rz   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   rG   rS   ry   rz   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?   rA   rB   r5   )r   rE   Údctr   r   r   Útest_typeSecurity€  s    
zJellyTests.test_typeSecurityc                 C   s   t  t¡}|  t|¡ d S r'   )r   r5   r"   rM   )r   Úujr   r   r   Útest_newStyleClassesˆ  s    
zJellyTests.test_newStyleClassesc                 C   s€   t ƒ }t t |¡¡ t t |j¡¡ tdddg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
   rp   ra   ÚtestgÍÌÌÌÌL4@)r
   rp   ra   NrG   )r   r   r5   r   r   Úboolr   r6   )r   rG   Ú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   rM   r1   )r   rG   Ú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    rI   rD   r%   r5   rL   ZUnpersistablerA   rB   rM   )r   rE   rG   rI   rD   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UnjellyableÚ	attributer5   r6   rL   )r   r—   Úinputr[   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   )rV   )Ú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>.persistentLoadrp   ra   r	   r`   é   )r¡   )r¤   r   zpersistentStore was not called.z$Persistent storage identity failure.)r.   rI   rD   r   r5   rM   rw   )	r   r    r¡   r¤   rG   rI   rD   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   rN   Zn1rŸ   rO   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   rG   rI   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   rk   rF   rJ   rP   rQ   rU   rW   r\   re   rj   rm   ro   rr   rt   ru   rv   rx   r{   r}   r€   r‚   r„   r†   r‹   r”   r•   r�   r¦   r¨   r§   r®   r   r   r   r   r:   x   sJ   û						r:   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.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   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   rM   r¬   )r   rG   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   rL   r%   r6   r~   )r   ry   rG   Úresr   r   r   rr   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.   rs   r%   r   r5   rL   r6   r~   )r   rG   ry   rÅ   r   r   r   rt   }  s    

z%CircularReferenceTests.test_frozensetN)r   r   r   r   rÀ   rÂ   rr   rt   r   r   r   r   r¾   W  s
   r¾   )r   rX   rh   Ztwisted.internet.testingr   Ztwisted.spreadr   r   r   Ztwisted.trialr   Ztwisted.trial.unittestr   r™   r   r   r   r   r    r"   r#   r.   r9   r:   r¯   ZCopyableZ
RemoteCopyr»   r¼   r¾   r   r   r   r   Ú<module>   s.   	   4$