U
    
W[N  ã                   @   s2  d Z ddlmZmZ ddlZddlZddlmZ ddlm	Z	m
Z
 ddlmZ ddlmZ G dd	„ d	e	jeƒZG d
d„ dƒZdd„ ZG dd„ dƒZG dd„ dƒZG dd„ deƒZG dd„ deƒZG dd„ dƒZG dd„ dejƒZG dd„ dejƒZG dd„ de
je
jƒZG dd„ de
je
jƒZG d d!„ d!ejƒZ dS )"z/
Test cases for L{jelly} object serialization.
é    )Úabsolute_importÚdivisionN)Úunicode)ÚjellyÚpb)Úunittest)ÚStringTransportc                   @   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                   @   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   Úafunc0   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   Úbmethod<   s    z	B.bmethodN)r   r   r   r   r    r   r   r   r   r   7   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   ÚcmethodH   s    z	C.cmethodN)r   r   r   r   r"   r   r   r   r   r!   C   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#   O   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   ]   s    z
E.__init__c                 C   s   | j | jdœS )Nr%   r%   r   r   r   r   Ú__getstate__b   s    zE.__getstate__c                 C   s   |d | _ |d | _d S )Nr&   r'   r%   ©r   Ústater   r   r   Ú__setstate__f   s    
zE.__setstate__)NN)r   r   r   r   Ú	__slots__r   r*   r-   r   r   r   r   r$   V   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   m   s    zSimpleJellyTest.__init__c                 C   s   | j |j kS r(   )Ú__dict__©r   Úotherr   r   r   ÚisTheSameAsq   s    zSimpleJellyTest.isTheSameAsN)r   r   r   r   r3   r   r   r   r   r/   l   s   r/   c                   @   sN  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ejs¾d*e_d+d,„ ZejsÒd-e_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$dFdG„ Z%dHdI„ Z&dJdK„ Z'dLS )MÚ
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allowBasicTypesÚunjellyZ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    Úar<   Ú	TypeErrorr   r:   )r   rB   Úbr?   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   r:   ÚassertIsInstanceÚassertIsÚassertEqual)r   ÚnrF   r?   Ú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:   rG   rI   )r   rJ   r?   rK   r   r   r   Útest_newStyleWithSlots¸   s    

z!JellyTests.test_newStyleWithSlotsc                 C   s&   t g}t t |¡¡}|  ||¡ dS )zn
        Test that an old style class type can be jellied and unjellied
        to the original type.
        N)r!   r   r:   rI   ©r   ÚtÚrr   r   r   Útest_typeOldStyleÅ   s    zJellyTests.test_typeOldStylec                 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   r:   rI   rN   r   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   r:   rI   rN   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   r:   rI   ÚassertIsNot)r   ZdtnZdtdr>   r?   Úoutputr   r   r   Útest_dateTimeã   s    


zJellyTests.test_dateTimec                 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   r7   ú-78.901N)ÚdecimalÚDecimalr   r:   rI   rV   )r   r>   r?   rW   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.
        rY   r   r7   rZ   N)r[   r\   r   r:   ÚdecimalDatarI   )r   ZexpectedrW   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.
        rY   r5   N)r[   r\   rA   ©r   r>   r   r   r   Útest_decimalSecurity  s    zJellyTests.test_decimalSecurityc                 C   s<   t dddgƒ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   é   é   N)Úsetr   r:   rI   rV   ©r   r>   rW   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   rb   rc   N)Ú	frozensetr   r:   rI   rV   re   r   r   r   Útest_frozenset,  s    zJellyTests.test_frozensetc                 C   s    t dddgƒ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   rb   rc   s   setN)rd   rA   r`   r   r   r   Útest_setSecurity8  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   rb   rc   s	   frozensetN)rg   rA   r`   r   r   r   Útest_frozensetSecurityB  s    z!JellyTests.test_frozensetSecurityc              	   C   s˜   t j dddg¡g}t   |¡}|  |t   tdddgƒg¡¡ t  |¡}|  t|d ƒt|d ƒ¡ tt jjkr„|  |d t jj¡ n|  |d t¡ dS )z”
        Test jellying C{sets.Set}: it should serialize to the same thing as
        C{set} jelly, and be unjellied as C{set} if available.
        r   rb   rc   r   N)r   Ú_setsÚSetrI   rd   r:   ÚlistrG   ©r   r>   Z
inputJellyrW   r   r   r   Útest_oldSetsL  s    

zJellyTests.test_oldSetsz'sets.Set is gone in Python 3 and higherc              	   C   s˜   t j dddg¡g}t   |¡}|  |t   tdddgƒg¡¡ t  |¡}|  t|d ƒt|d ƒ¡ tt jjkr„|  |d t jj¡ n|  |d t¡ dS )z±
        Test jellying C{sets.ImmutableSet}: it should serialize to the same
        thing as L{frozenset} jelly, and be unjellied as L{frozenset} if
        available.
        r   rb   rc   r   N)r   rk   ZImmutableSetrI   rg   r:   rm   rG   rn   r   r   r   Útest_oldImmutableSets`  s    

z JellyTests.test_oldImmutableSetsz1sets.ImmutableSets is gone in Python 3 and higherc                 C   sN   |   tddƒ tddƒ¡¡ tddƒ}t |¡}t |¡}|   | |¡¡ dS )z%
        Simplest test case.
        rB   rD   r   rb   N)Ú
assertTruer/   r3   r   r:   )r   rB   ZcerealrD   r   r   r   Útest_simplev  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   rH   r   r:   )r   r&   r'   ÚsÚzr   r   r   Útest_identity‚  s    



zJellyTests.test_identityc                 C   s<   t dƒ}t t |¡¡}|  ||¡ |  t|ƒt|ƒ¡ d S )NZblah)r   r   r:   rI   Útyper)   r   r   r   Útest_unicode’  s    zJellyTests.test_unicodec                 C   sb   g }d|i|f}|  |¡ t |¡}t |¡}|  |d d |d ¡ |  |d d d |¡ d S )Nrm   r   r   ©r   r   r:   rH   )r   ZrerefZtoplevelTuplers   rt   r   r   r   Útest_stressReferences™  s    


z JellyTests.test_stressReferencesc                 C   sF   g }|f}|  |f¡ t |¡}t |¡}|  |d d d |¡ d S ©Nr   rx   )r   rB   rO   rs   rt   r   r   r   Útest_moreReferences£  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   r9   r<   r=   r:   )r   r@   Zdctr   r   r   Útest_typeSecurity¬  s    
zJellyTests.test_typeSecurityc                 C   s   t  t¡}|  t|¡ d S r(   )r   r:   r#   rH   )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   rb   rc   ZtestgÍÌÌÌÌL4@)r   rb   rc   NrB   )r   r   r:   r   r   Úboolr   rI   )r   rB   Ú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(   ©r2   r1   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 rz   r„   r+   r   r   r   r-   Î  s    zTupleState.__setstate__c                 S   s
   t | jƒS r(   )Úhashr2   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   r:   rH   r2   )r   rB   Ú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   r9   ZallowInstancesOfr   r   r!   rD   r?   r&   r:   rG   ZUnpersistabler<   r=   rH   )r   r@   rB   rD   r?   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	attributer:   rI   rG   )r   rŽ   ÚinputrW   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   )rS   )ÚobjÚjelÚperstr   r   r   ÚpersistentStore	  s    z:JellyTests.test_persistentStorage.<locals>.persistentStorec                 S   s   t | ƒ}|d | S rz   )Úint)ZpidstrZunjr–   Úpidr   r   r   ÚpersistentLoad  s    z9JellyTests.test_persistentStorage.<locals>.persistentLoadrb   rc   r
   é   é   )r—   )rš   r   zpersistentStore was not called.z$Persistent storage identity failure.)r/   rD   r?   r   r:   rH   rq   )	r   r–   r—   rš   rB   rD   r?   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   rJ   Zn1r•   rK   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
   )rI   r   r   Úlenr   Úziprž   )r   rB   rD   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   r:   ZassertInZluidZlocalObjects)r   ÚrefZjellyBrokerÚjZunjellyBrokerr}   r   r   r   Útest_referenceable7  s    zJellyTests.test_referenceableN)(r   r   r   r   r^   rA   rE   rL   rM   rQ   rR   rT   rX   r]   r_   ra   rf   rh   ri   rj   ro   r   rk   Úskiprp   rr   ru   rw   ry   r{   r|   r~   r‚   r‹   rŒ   r“   r   rŸ   rž   r¥   r   r   r   r   r4   v   sT    ÿ





ÿ
			r4   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_deprecatedInstanceAtomrI   r    ÚDeprecationWarning©r   Úwarningsr   r   r   r«   P  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   r:   rª   rI   r    r¬   r­   r   r   r   Ú%test_deprecatedUnjellyingInstanceAtom`  s     þÿ
þ
þz;JellyDeprecationTests.test_deprecatedUnjellyingInstanceAtomN)r   r   r   r   r«   r±   r   r   r   r   r§   K  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   u  s    zClassA.__init__N©r   r   r   r   r   r   r   r   r²   t  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    zClassB.__init__Nr´   r   r   r   r   r³   z  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³   r:   rH   r£   )r   rB   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   ÚDummyInvokerClassŽ  s   r¸   r¢   r¶   )ZserializingPerspectiver²   r   r‘   r³   r:   ZfailUnlessIdenticalr£   )r   r¸   ZdummyInvokerZa0r¤   Za1r   r   r   Útest_circleWithInvoker  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)	rd   r/   Úaddr   r:   rG   r&   rI   rm   )r   rs   rB   Úresr   r   r   rf   ›  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/   rg   r&   r   r:   rG   rI   rm   )r   rB   rs   r»   r   r   r   rh   ¨  s    

z%CircularReferenceTests.test_frozensetN)r   r   r   r   r·   r¹   rf   rh   r   r   r   r   rµ   €  s
   rµ   )!r   Z
__future__r   r   rU   r[   Ztwisted.python.compatr   Ztwisted.spreadr   r   Ztwisted.trialr   Ztwisted.test.proto_helpersr   r   Úobjectr	   r   r   r   r!   r#   r$   r/   ZTestCaser4   r§   ZCopyableZ
RemoteCopyr²   r³   rµ   r   r   r   r   Ú<module>   s.   
   X)