U
    2ÜŸjƒ9  ã                   @   sr  d Z ddlmZ ddlmZ ddlm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ƒZG dd„ deƒZG dd„ dejƒZG dd„ dejƒZG dd„ de
jejejƒZG dd„ dejejƒZe ee¡ e ee¡ ej e¡ G dd„ dejƒZG dd„ dej ƒZ!G dd„ dej"ƒ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'd#S )$z!
Tests for error handling in PB.
é    )ÚStringIO)Údefer)Úlog)Úqual)ÚflavorsÚjellyÚpb)ÚconnectedServerAndClient)Úunittestc                   @   s   e Zd ZdZdS )ÚAsynchronousExceptionzˆ
    Helper used to test remote methods which return Deferreds which fail with
    exceptions which are not L{pb.Error} subclasses.
    N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úF/tmp/pip-unpacked-wheel-4b529toj/twisted/spread/test/test_pbfailure.pyr      s   r   c                   @   s   e Zd ZdZdS )ÚSynchronousExceptionzm
    Helper used to test remote methods which raise exceptions which are not
    L{pb.Error} subclasses.
    Nr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdS )ÚAsynchronousErrorz„
    Helper used to test remote methods which return Deferreds which fail with
    exceptions which are L{pb.Error} subclasses.
    Nr   r   r   r   r   r   "   s   r   c                   @   s   e Zd ZdZdS )ÚSynchronousErrorzi
    Helper used to test remote methods which raise exceptions which are
    L{pb.Error} subclasses.
    Nr   r   r   r   r   r   )   s   r   c                   @   s   e Zd ZdS )Ú
JellyErrorN©r   r   r   r   r   r   r   r   0   s   r   c                   @   s   e Zd ZdS )ÚSecurityErrorNr   r   r   r   r   r   4   s   r   c                   @   sh   e Z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d„Zddd„ZdS )Ú
SimpleRootc                 C   s   t  tdƒ¡S )zD
        Fail asynchronously with a non-pb.Error exception.
        zremote asynchronous exception)r   Úfailr   ©Úselfr   r   r   Úremote_asynchronousException?   s    z'SimpleRoot.remote_asynchronousExceptionc                 C   s   t dƒ‚dS )zC
        Fail synchronously with a non-pb.Error exception.
        zremote synchronous exceptionN)r   r   r   r   r   Úremote_synchronousExceptionE   s    z&SimpleRoot.remote_synchronousExceptionc                 C   s   t  tdƒ¡S )z@
        Fail asynchronously with a pb.Error exception.
        zremote asynchronous error)r   r   r   r   r   r   r   Úremote_asynchronousErrorK   s    z#SimpleRoot.remote_asynchronousErrorc                 C   s   t dƒ‚dS )z?
        Fail synchronously with a pb.Error exception.
        zremote synchronous errorN)r   r   r   r   r   Úremote_synchronousErrorQ   s    z"SimpleRoot.remote_synchronousErrorc                 C   s   G dd„ dt jƒ}|dƒ‚dS )z>
        Fail with error that is not known to client.
        c                   @   s   e Zd ZdS )z4SimpleRoot.remote_unknownError.<locals>.UnknownErrorNr   r   r   r   r   ÚUnknownError\   s   r!   zI'm not known to client!N)r   ÚError)r   r!   r   r   r   Úremote_unknownErrorW   s    zSimpleRoot.remote_unknownErrorc                 C   s   |   ¡  d S ©N)Ú
raiseJellyr   r   r   r   Úremote_jellya   s    zSimpleRoot.remote_jellyc                 C   s   |   ¡  d S r$   )ÚraiseSecurityr   r   r   r   Úremote_securityd   s    zSimpleRoot.remote_securityc                 C   s"   t  ¡ }| | j¡ | d ¡ |S r$   )r   ÚDeferredÚaddCallbackr%   Úcallback©r   Údr   r   r   Úremote_deferredJellyg   s    
zSimpleRoot.remote_deferredJellyc                 C   s"   t  ¡ }| | j¡ | d ¡ |S r$   )r   r)   r*   r'   r+   r,   r   r   r   Úremote_deferredSecuritym   s    
z"SimpleRoot.remote_deferredSecurityNc                 C   s   t dƒ‚d S )NzI'm jellyable!)r   ©r   Úresultsr   r   r   r%   s   s    zSimpleRoot.raiseJellyc                 C   s   t dƒ‚d S )NzI'm secure!)r   r0   r   r   r   r'   v   s    zSimpleRoot.raiseSecurity)N)N)r   r   r   r   r   r   r    r#   r&   r(   r.   r/   r%   r'   r   r   r   r   r   >   s   

r   c                   @   s   e Zd ZdZdZdd„ ZdS )ÚSaveProtocolServerFactoryzd
    A L{pb.PBServerFactory} that saves the latest connected client in
    C{protocolInstance}.
    Nc                 C   s
   || _ dS )z3
        Keep track of the given protocol.
        N)ÚprotocolInstance)r   Úprotocolr   r   r   ÚclientConnectionMade‚   s    z.SaveProtocolServerFactory.clientConnectionMade)r   r   r   r   r3   r5   r   r   r   r   r2   z   s   r2   c                   @   s   e Zd ZdZdd„ ZdS )ÚPBConnTestCaser   c                    sJ   t tƒ ƒˆ _ˆ jˆ j_t ¡ ˆ _t‡ fdd„‡ fdd„ƒ\ˆ _ˆ _	ˆ _
d S )Nc                      s   ˆ j  d ¡S r$   )ÚserverFactoryÚbuildProtocolr   r   r   r   Ú<lambda>•   ó    z&PBConnTestCase.setUp.<locals>.<lambda>c                      s   ˆ j  d ¡S r$   )ÚclientFactoryr8   r   r   r   r   r9   –   r:   )r2   r   r7   ÚunsafeTracebacksr   ZPBClientFactoryr;   r	   ZconnectedServerZconnectedClientÚpumpr   r   r   r   ÚsetUpŒ   s    



þüýzPBConnTestCase.setUpN)r   r   r   r<   r>   r   r   r   r   r6   ‰   s   r6   c                   @   sž   e Zd ZejjZdd„ Zdd„ Zdd„ Z	dd„ Z
d	d
„ Zdd„ Zdd„ Zd$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S )%ÚPBFailureTestsc                    s@   ‡‡‡fdd„‰ ˆj  ¡ }‡ ‡fdd„}| |¡ ˆj ¡  d S )Nc                    sF   |   ˆ ¡ ˆ | jd¡ ˆr6ˆ ˆ ¡}ˆ t|ƒd¡ | j| j| jfS )NúTraceback unavailable
é   )ÚtrapÚcompareÚ	tracebackÚflushLoggedErrorsÚassertEqualÚlenÚtypeÚvalue)ÚerrÚerrs)ÚexceptionTypeÚflushr   r   r   Úebž   s    

z)PBFailureTests._exceptionTest.<locals>.ebc                    s   |   ˆ¡}| ˆ ¡ |S r$   )Ú
callRemoteÚ
addErrback)Úrootr-   )rN   Úmethodr   r   ÚgotRootObject¨   s    

z4PBFailureTests._exceptionTest.<locals>.gotRootObject©r;   ÚgetRootObjectr*   r=   rM   )r   rR   rL   rM   r-   rS   r   )rN   rL   rM   rR   r   r   Ú_exceptionTest�   s
    

zPBFailureTests._exceptionTestc                 C   s   |   dtd¡S )z 
        Test that a Deferred returned by a remote method which already has a
        Failure correctly has that error passed back to the calling side.
        ZasynchronousExceptionT)rV   r   r   r   r   r   Útest_asynchronousException°   s    z)PBFailureTests.test_asynchronousExceptionc                 C   s   |   dtd¡S )zw
        Like L{test_asynchronousException}, but for a method which raises an
        exception synchronously.
        ÚsynchronousExceptionT)rV   r   r   r   r   r   Útest_synchronousException·   s    z(PBFailureTests.test_synchronousExceptionc                 C   s   |   dtd¡S )z�
        Like L{test_asynchronousException}, but for a method which returns a
        Deferred failing with an L{pb.Error} subclass.
        ZasynchronousErrorF)rV   r   r   r   r   r   Útest_asynchronousError¾   s    z%PBFailureTests.test_asynchronousErrorc                 C   s   |   dtd¡S )z}
        Like L{test_asynchronousError}, but for a method which synchronously
        raises a L{pb.Error} subclass.
        ÚsynchronousErrorF)rV   r   r   r   r   r   Útest_synchronousErrorÅ   s    z$PBFailureTests.test_synchronousErrorc                 C   s   |   ||¡ |S r$   )rF   )r   ÚresultÚexpectedResultr   r   r   Ú_successÌ   s    zPBFailureTests._successc                 C   s   |j | j||fd� |S )N)ZcallbackArgs)ZaddCallbacksr_   )r   Z
remoteCallr^   rN   r   r   r   Ú_addFailingCallbacksÐ   s    z#PBFailureTests._addFailingCallbacksNc                    s6   ˆj  ¡ }‡ ‡‡‡‡fdd„}| |¡ ˆj ¡  dS )zÆ
        Call the given remote method and attach the given errback to the
        resulting Deferred.  If C{exc} is not None, also assert that one
        exception of that type was logged.
        c                    s8   ˆ  |  ˆ¡ˆˆ ¡}ˆd k	r4‡‡fdd„}| |¡ |S )Nc                    s   ˆ  tˆ ˆ ¡ƒd¡ | S )NrA   )rF   rG   rE   )rJ   )Úexcr   r   r   Ú
gotFailureâ   s    z@PBFailureTests._testImpl.<locals>.gotRootObj.<locals>.gotFailure)r`   rO   ZaddBoth)ÚobjZfailureDeferredrb   ©rN   ra   ÚexpectedrR   r   r   r   Ú
gotRootObjÜ   s      ÿ
z,PBFailureTests._testImpl.<locals>.gotRootObjNrT   )r   rR   re   rN   ra   ZrootDeferredrf   r   rd   r   Ú	_testImplÔ   s    

zPBFailureTests._testImplc                    s   ‡ fdd„}ˆ   dd|¡S )z—
        Test that an exception which is a subclass of L{pb.Error} has more
        information passed across the network to the calling side.
        c                    s,   |   t¡ ˆ  | jt¡ ˆ  | j| j¡ dS )Né+   ©rB   r   ÚassertNotIsInstancerH   ÚstrÚassertIsInstancerI   ©r   r   r   r   ÚfailureJellyò   s    
z6PBFailureTests.test_jellyFailure.<locals>.failureJellyr   rh   ©rg   )r   rn   r   r   r   Útest_jellyFailureì   s    z PBFailureTests.test_jellyFailurec                    s   ‡ fdd„}ˆ   dd|¡S )z�
        Test that a Deferred which fails with a L{pb.Error} is treated in
        the same way as a synchronously raised L{pb.Error}.
        c                    s,   |   t¡ ˆ  | jt¡ ˆ  | j| j¡ dS )Né®  ri   rm   r   r   r   ÚfailureDeferredJelly   s    
zFPBFailureTests.test_deferredJellyFailure.<locals>.failureDeferredJellyZdeferredJellyrq   ro   )r   rr   r   r   r   Útest_deferredJellyFailureú   s    z(PBFailureTests.test_deferredJellyFailurec                    s   ‡ fdd„}ˆ   dd|¡S )z¬
        A non-jellyable L{pb.Error} subclass raised by a remote method is
        turned into a Failure with a type set to the FQPN of the exception
        type.
        c                    s   ˆ   | jd¡ dS )Ns3   twisted.spread.test.test_pbfailure.SynchronousErroré¯  ©rF   rH   rm   r   r   r   ÚfailureUnjellyable  s
     ÿzBPBFailureTests.test_unjellyableFailure.<locals>.failureUnjellyabler[   rt   ro   )r   rv   r   r   r   Útest_unjellyableFailure  s    z&PBFailureTests.test_unjellyableFailurec                    s   ‡ fdd„}ˆ   dd|¡S )z�
        Test that an exception which is a subclass of L{pb.Error} but not
        known on the client side has its type set properly.
        c                    s   ˆ   | jd¡ dS )Ns/   twisted.spread.test.test_pbfailure.UnknownErroréÖ  ru   rm   r   r   r   ÚfailureUnknown  s
     ÿz:PBFailureTests.test_unknownFailure.<locals>.failureUnknownZunknownErrorrx   ro   )r   ry   r   r   r   Útest_unknownFailure  s    z"PBFailureTests.test_unknownFailurec                    s   ‡ fdd„}ˆ   dd|¡S )zÔ
        Test that even if an exception is not explicitly jellyable (by being
        a L{pb.Jellyable} subclass), as long as it is an L{pb.Error}
        subclass it receives the same special treatment.
        c                    s,   |   t¡ ˆ  | jt¡ ˆ  | j| j¡ dS )NéÌ  ©rB   r   rj   rH   rk   rl   rI   rm   r   r   r   ÚfailureSecurity,  s    
z<PBFailureTests.test_securityFailure.<locals>.failureSecurityÚsecurityr{   ro   )r   r}   r   r   r   Útest_securityFailure%  s    z#PBFailureTests.test_securityFailurec                    s   ‡ fdd„}ˆ   dd|¡S )zÊ
        Test that a Deferred which fails with a L{pb.Error} which is not
        also a L{pb.Jellyable} is treated in the same way as a synchronously
        raised exception of the same type.
        c                    s,   |   t¡ ˆ  | jt¡ ˆ  | j| j¡ dS )Néø§  r|   rm   r   r   r   ÚfailureDeferredSecurity;  s    
zEPBFailureTests.test_deferredSecurity.<locals>.failureDeferredSecurityZdeferredSecurityr€   ro   )r   r�   r   r   r   Útest_deferredSecurity4  s    z$PBFailureTests.test_deferredSecurityc                    s   ‡ fdd„}ˆ   dd|t¡S )z�
        Test that attempting to call a method which is not defined correctly
        results in an AttributeError on the calling side.
        c                    s   |   tj¡ ˆ  | jd¡ dS )Nr@   é¤  )rB   r   ZNoSuchMethodrC   rD   rm   r   r   r   ÚfailureNoSuchI  s    z>PBFailureTests.test_noSuchMethodFailure.<locals>.failureNoSuchZnosuchrƒ   )rg   ÚAttributeError)r   r„   r   r   r   Útest_noSuchMethodFailureC  s    z'PBFailureTests.test_noSuchMethodFailurec                    s@   ˆ j  ¡ }dd„ }| |¡ ‡ fdd„}| |¡ ˆ j ¡  dS )zì
        Test that a copied failure received from a PB call can be logged
        locally.

        Note: this test needs some serious help: all it really tests is that
        log.err(copiedFailure) doesn't raise an exception.
        c                 S   s
   |   d¡S )NrX   )rO   )ZrootObjr   r   r   Ú	connectedZ  s    z;PBFailureTests.test_copiedFailureLogging.<locals>.connectedc                    s(   t  | ¡ ˆ  t¡}ˆ  t|ƒd¡ d S )Né   )r   rJ   rE   r   rF   rG   )ZfailurerK   r   r   r   Ú	exception_  s    

z;PBFailureTests.test_copiedFailureLogging.<locals>.exceptionN)r;   rU   r*   rP   r=   rM   )r   r-   r‡   r‰   r   r   r   Útest_copiedFailureLoggingP  s    


z(PBFailureTests.test_copiedFailureLoggingc                    s¦   t  tdƒ¡}t tj|tƒ d�¡}g ‰ ‡ ‡fdd„}|ƒ }| d¡ ˆ t|j	|¡ ˆ 
tˆ ƒd¡ ˆ d }ˆ 
|jttƒ d¡¡ ˆ 
|jd	¡ ˆ 
|jd
¡ dS )z±
        L{pb.CopiedFailure.throwExceptionIntoGenerator} will throw a
        L{RemoteError} into the given paused generator at the point where it
        last yielded.
        Úfoo©Zinvokerc               
   3   sH   z
d V  W n. t jk
r8 }  zˆ  | ¡ W 5 d } ~ X Y nX ˆ d¡ d S )NzRemoteError not raised)r   ZRemoteErrorÚappendr   )ra   ©r‰   r   r   r   ÚgeneratorFuncq  s
    
zFPBFailureTests.test_throwExceptionIntoGenerator.<locals>.generatorFuncNrA   r   Úascii)r‹   r@   )r   ÚCopyableFailurer…   r   ÚunjellyÚDummyInvokerÚsendÚassertRaisesÚStopIterationZthrowExceptionIntoGeneratorrF   rG   Z
remoteTyper   ÚencodeÚargsZremoteTraceback)r   ÚoriginalÚcopyr�   Úgenra   r   rŽ   r   Ú test_throwExceptionIntoGeneratorg  s    
z/PBFailureTests.test_throwExceptionIntoGenerator)N)r   r   r   r
   ÚTestCaserF   rC   rV   rW   rY   rZ   r\   r_   r`   rg   rp   rs   rw   rz   r   r‚   r†   rŠ   rœ   r   r   r   r   r?   š   s$   
r?   c                   @   s   e Zd ZejjZdZdS )ÚPBFailureUnsafeTestsrA   N)r   r   r   r
   r�   ZfailIfEqualsrC   r<   r   r   r   r   rž   ƒ  s   rž   c                   @   s   e Zd ZdZdZdS )r“   zZ
    A behaviorless object to be used as the invoker parameter to
    L{jelly.jelly}.
    N)r   r   r   r   ZserializingPerspectiver   r   r   r   r“   ˆ  s   r“   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚFailureJellyingTestsz:
    Tests for the interaction of jelly and failures.
    c                 C   sn   t  tƒ ¡}|  | t¡t¡ |  | t¡t¡ t tj|tƒ d�¡}|  | t¡t¡ |  | t¡t¡ dS )zš
        An unjellied L{CopyableFailure} has a check method which behaves the
        same way as the original L{CopyableFailure}'s check method.
        rŒ   N©	r   r‘   ÚZeroDivisionErrorZassertIsÚcheckÚArithmeticErrorr   r’   r“   )r   r™   Úcopiedr   r   r   Útest_unjelliedFailureCheck–  s    z/FailureJellyingTests.test_unjelliedFailureCheckc                 C   sŽ   t  tƒ ¡}|  | t¡t¡ |  | t¡t¡ t tj|tƒ d�¡}t  |¡}t tj|tƒ d�¡}|  | t¡t¡ |  | t¡t¡ dS )aX  
        The object which results from jellying a L{CopyableFailure}, unjellying
        the result, creating a new L{CopyableFailure} from the result of that,
        jellying it, and finally unjellying the result of that has a check
        method which behaves the same way as the original L{CopyableFailure}'s
        check method.
        rŒ   Nr    )r   r™   Z
copiedOnceZ
derivativeZcopiedTwicer   r   r   Útest_twiceUnjelliedFailureCheck¢  s    
z4FailureJellyingTests.test_twiceUnjelliedFailureCheckc                 C   sZ   t  tdƒ¡}t tj|tƒ d�¡}tƒ }| |¡ ttƒ}d 	|¡}|  
|| ¡ ¡ dS )a  
        When L{CopiedFailure.printTraceback} is used to print a copied failure
        which was unjellied from a L{CopyableFailure} with C{unsafeTracebacks}
        set to C{False}, the string representation of the exception value is
        included in the output.
        zsome reasonrŒ   z.Traceback from remote host -- {}: some reason
N)r   r‘   Ú	Exceptionr   r’   r“   r   ZprintTracebackr   ÚformatrF   Úgetvalue)r   r™   r¤   Úoutputr‰   ZexpectedOutputr   r   r   Ú test_printTracebackIncludesValue³  s    
ÿz5FailureJellyingTests.test_printTracebackIncludesValueN)r   r   r   r   r¥   r¦   r«   r   r   r   r   rŸ   ‘  s   rŸ   N)(r   Úior   Ztwisted.internetr   Ztwisted.pythonr   Ztwisted.python.reflectr   Ztwisted.spreadr   r   r   Ztwisted.test.iosimr	   Ztwisted.trialr
   r§   r   r   r"   r   r   Z	JellyableZ
RemoteCopyr   r   ZsetUnjellyableForClassZglobalSecurityZallowInstancesOfZRootr   ZPBServerFactoryr2   r�   r6   r?   rž   r“   rŸ   r   r   r   r   Ú<module>   s0   < j	