U
    AcjmJ  ć                   @   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-nrvc_22y/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$