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   ic                 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
   testgL4@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$