o
    ¯b;  ã                   @   sj  d Z ddlmZ ddlmZ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 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Úreactor)Úlog)Úqual)ÚflavorsÚjellyÚpb)Úunittestc                   @   ó   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   úD/usr/lib/python3/dist-packages/twisted/spread/test/test_pbfailure.pyr      ó    r   c                   @   r   )ÚSynchronousExceptionzm
    Helper used to test remote methods which raise exceptions which are not
    L{pb.Error} subclasses.
    Nr   r   r   r   r   r      r   r   c                   @   r   )Ú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   !   r   r   c                   @   r   )ÚSynchronousErrorzi
    Helper used to test remote methods which raise exceptions which are
    L{pb.Error} subclasses.
    Nr   r   r   r   r   r   (   r   r   c                   @   ó   e Zd ZdS )Ú
JellyErrorN©r   r   r   r   r   r   r   r   /   ó    r   c                   @   r   )ÚSecurityErrorNr   r   r   r   r   r   3   r   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   ó   t  tdƒ¡S )zD
        Fail asynchronously with a non-pb.Error exception.
        zremote asynchronous exception)r   Úfailr   ©Úselfr   r   r   Úremote_asynchronousException>   ó   z'SimpleRoot.remote_asynchronousExceptionc                 C   ó   t dƒ‚)zC
        Fail synchronously with a non-pb.Error exception.
        zremote synchronous exception)r   r    r   r   r   Úremote_synchronousExceptionD   ó   z&SimpleRoot.remote_synchronousExceptionc                 C   r   )z@
        Fail asynchronously with a pb.Error exception.
        zremote asynchronous error)r   r   r   r    r   r   r   Úremote_asynchronousErrorJ   r#   z#SimpleRoot.remote_asynchronousErrorc                 C   r$   )z?
        Fail synchronously with a pb.Error exception.
        zremote synchronous error)r   r    r   r   r   Úremote_synchronousErrorP   r&   z"SimpleRoot.remote_synchronousErrorc                 C   s   G dd„ dt jƒ}|dƒ‚)z>
        Fail with error that is not known to client.
        c                   @   r   )z4SimpleRoot.remote_unknownError.<locals>.UnknownErrorNr   r   r   r   r   ÚUnknownError[   r   r)   zI'm not known to client!)r	   ÚError)r!   r)   r   r   r   Úremote_unknownErrorV   s   zSimpleRoot.remote_unknownErrorc                 C   ó   |   ¡  d S ©N)Ú
raiseJellyr    r   r   r   Úremote_jelly`   ó   zSimpleRoot.remote_jellyc                 C   r,   r-   )ÚraiseSecurityr    r   r   r   Úremote_securityc   r0   zSimpleRoot.remote_securityc                 C   ó"   t  ¡ }| | j¡ | d ¡ |S r-   )r   ÚDeferredÚaddCallbackr.   Úcallback©r!   Údr   r   r   Úremote_deferredJellyf   ó   
zSimpleRoot.remote_deferredJellyc                 C   r3   r-   )r   r4   r5   r1   r6   r7   r   r   r   Úremote_deferredSecurityl   r:   z"SimpleRoot.remote_deferredSecurityNc                 C   r$   )NzI'm jellyable!)r   ©r!   Úresultsr   r   r   r.   r   ó   zSimpleRoot.raiseJellyc                 C   r$   )NzI'm secure!)r   r<   r   r   r   r1   u   r>   zSimpleRoot.raiseSecurityr-   )r   r   r   r"   r%   r'   r(   r+   r/   r2   r9   r;   r.   r1   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   r@   rB   r   r   r   r   r?   y   s    r?   c                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )ÚPBConnTestCaser   c                 C   s   |   ¡  |  ¡  d S r-   )Ú_setUpServerÚ_setUpClientr    r   r   r   ÚsetUp‹   s   zPBConnTestCase.setUpc                 C   s.   t tƒ ƒ| _| j| j_tjd| jdd�| _d S )Nr   ú	127.0.0.1)Ú	interface)r?   r   ÚserverFactoryÚunsafeTracebacksr   Ú	listenTCPÚ
serverPortr    r   r   r   rD   �   s
   
ÿzPBConnTestCase._setUpServerc                 C   s,   | j  ¡ j}t ¡ | _t d|| j¡| _d S )NrG   )	rL   ÚgetHostÚportr	   ÚPBClientFactoryÚclientFactoryr   Ú
connectTCPÚclientConnector)r!   ÚportNor   r   r   rE   –   s
   

ÿzPBConnTestCase._setUpClientc                 C   s0   | j jd ur| j jj ¡  t |  ¡ |  ¡ g¡S r-   )rI   r@   Ú	transportÚloseConnectionr   ÚgatherResultsÚ_tearDownServerÚ_tearDownClientr    r   r   r   ÚtearDown�   s   zPBConnTestCase.tearDownc                 C   s   t  | jj¡S r-   )r   ÚmaybeDeferredrL   ÚstopListeningr    r   r   r   rW   ¢   s   zPBConnTestCase._tearDownServerc                 C   s   | j  ¡  t d ¡S r-   )rR   Ú
disconnectr   Úsucceedr    r   r   r   rX   ¥   s   

zPBConnTestCase._tearDownClientN)
r   r   r   rJ   rF   rD   rE   rY   rW   rX   r   r   r   r   rC   ˆ   s    rC   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                    s6   ‡‡‡fdd„‰ ˆj  ¡ }‡ ‡fdd„}| |¡ |S )Nc                    sF   |   ˆ ¡ ˆ | jd¡ ˆrˆ ˆ ¡}ˆ 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)Úrootr8   )rm   Úmethodr   r   ÚgotRootObject¸   s   

z4PBFailureTests._exceptionTest.<locals>.gotRootObject©rP   ÚgetRootObjectr5   )r!   rq   rk   rl   r8   rr   r   )rm   rk   rl   rq   r!   r   Ú_exceptionTest­   s
   

zPBFailureTests._exceptionTestc                 C   ó   |   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.
        ÚasynchronousExceptionT)ru   r   r    r   r   r   Útest_asynchronousExceptionÀ   ó   z)PBFailureTests.test_asynchronousExceptionc                 C   rv   )zw
        Like L{test_asynchronousException}, but for a method which raises an
        exception synchronously.
        ÚsynchronousExceptionT)ru   r   r    r   r   r   Útest_synchronousExceptionÇ   ry   z(PBFailureTests.test_synchronousExceptionc                 C   rv   )z�
        Like L{test_asynchronousException}, but for a method which returns a
        Deferred failing with an L{pb.Error} subclass.
        ÚasynchronousErrorF)ru   r   r    r   r   r   Útest_asynchronousErrorÎ   ry   z%PBFailureTests.test_asynchronousErrorc                 C   rv   )z}
        Like L{test_asynchronousError}, but for a method which synchronously
        raises a L{pb.Error} subclass.
        ÚsynchronousErrorF)ru   r   r    r   r   r   Útest_synchronousErrorÕ   ry   z$PBFailureTests.test_synchronousErrorc                 C   s   |   ||¡ |S r-   )re   )r!   ÚresultÚexpectedResultr   r   r   Ú_successÜ   s   zPBFailureTests._successc                 C   s   |j | j||fd� |S )N)ÚcallbackArgs)ÚaddCallbacksr‚   )r!   Ú
remoteCallr�   rm   r   r   r   Ú_addFailingCallbacksà   s   z#PBFailureTests._addFailingCallbacksNc                    s,   ˆj  ¡ }‡ ‡‡‡‡fd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 ur‡‡fdd„}| |¡ |S )Nc                    s   ˆ  tˆ ˆ ¡ƒd¡ | S )Nr`   )re   rf   rd   )ri   )Úexcr!   r   r   Ú
gotFailureò   s   z@PBFailureTests._testImpl.<locals>.gotRootObj.<locals>.gotFailure)r†   rn   ÚaddBoth)ÚobjÚfailureDeferredrˆ   ©rm   r‡   Úexpectedrq   r!   r   r   Ú
gotRootObjì   s   ÿ
z,PBFailureTests._testImpl.<locals>.gotRootObjrs   )r!   rq   r�   rm   r‡   ÚrootDeferredrŽ   r   rŒ   r   Ú	_testImplä   s   

zPBFailureTests._testImplc                    ó   ‡ 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                    ó,   |   t¡ ˆ  | jt¡ ˆ  | j| j¡ dS )Né+   ©ra   r   ÚassertNotIsInstancerg   ÚstrÚassertIsInstancerh   ©r   r    r   r   ÚfailureJelly  ó   
z6PBFailureTests.test_jellyFailure.<locals>.failureJellyr   r“   ©r�   )r!   r™   r   r    r   Útest_jellyFailureü   ó   z PBFailureTests.test_jellyFailurec                    r‘   )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                    r’   )Né®  r”   r˜   r    r   r   ÚfailureDeferredJelly  rš   zFPBFailureTests.test_deferredJellyFailure.<locals>.failureDeferredJellyÚdeferredJellyrž   r›   )r!   rŸ   r   r    r   Útest_deferredJellyFailure
  r�   z(PBFailureTests.test_deferredJellyFailurec                    r‘   )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                    ó   ˆ   | jd¡ dS )Ns3   twisted.spread.test.test_pbfailure.SynchronousErroré¯  ©re   rg   r˜   r    r   r   ÚfailureUnjellyable  ó   ÿzBPBFailureTests.test_unjellyableFailure.<locals>.failureUnjellyabler~   r£   r›   )r!   r¥   r   r    r   Útest_unjellyableFailure  ó   z&PBFailureTests.test_unjellyableFailurec                    r‘   )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                    r¢   )Ns/   twisted.spread.test.test_pbfailure.UnknownErroréÖ  r¤   r˜   r    r   r   ÚfailureUnknown-  r¦   z:PBFailureTests.test_unknownFailure.<locals>.failureUnknownÚunknownErrorr©   r›   )r!   rª   r   r    r   Útest_unknownFailure'  r�   z"PBFailureTests.test_unknownFailurec                    r‘   )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                    r’   )NéÌ  ©ra   r   r•   rg   r–   r—   rh   r˜   r    r   r   ÚfailureSecurity<  rš   z<PBFailureTests.test_securityFailure.<locals>.failureSecurityÚsecurityr­   r›   )r!   r¯   r   r    r   Útest_securityFailure5  r¨   z#PBFailureTests.test_securityFailurec                    r‘   )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                    r’   )Néø§  r®   r˜   r    r   r   ÚfailureDeferredSecurityK  rš   zEPBFailureTests.test_deferredSecurity.<locals>.failureDeferredSecurityÚdeferredSecurityr²   r›   )r!   r³   r   r    r   Útest_deferredSecurityD  r¨   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_   é¤  )ra   r	   ÚNoSuchMethodrb   rc   r˜   r    r   r   ÚfailureNoSuchY  s   z>PBFailureTests.test_noSuchMethodFailure.<locals>.failureNoSuchÚnosuchr¶   )r�   ÚAttributeError)r!   r¸   r   r    r   Útest_noSuchMethodFailureS  s   z'PBFailureTests.test_noSuchMethodFailurec                    s6   ˆ j  ¡ }dd„ }| |¡ ‡ fd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 )Nrz   )rn   )ÚrootObjr   r   r   Ú	connectedj  s   
z;PBFailureTests.test_copiedFailureLogging.<locals>.connectedc                    s(   t  | ¡ ˆ  t¡}ˆ  t|ƒd¡ d S )Né   )r   ri   rd   r   re   rf   )Úfailurerj   r    r   r   Ú	exceptiono  s   

z;PBFailureTests.test_copiedFailureLogging.<locals>.exception)rP   rt   r5   ro   )r!   r8   r½   rÀ   r   r    r   Útest_copiedFailureLogging`  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©Úinvokerc               
   3   sN   � zd V  W n t jy }  zˆ  | ¡ W Y d } ~ d S d } ~ ww ˆ d¡ d S )NzRemoteError not raised)r	   ÚRemoteErrorÚappendr   )r‡   ©rÀ   r!   r   r   ÚgeneratorFunc‚  s   €
€ÿzFPBFailureTests.test_throwExceptionIntoGenerator.<locals>.generatorFuncNr`   r   Úascii)rÂ   r_   )r	   ÚCopyableFailurerº   r   ÚunjellyÚDummyInvokerÚsendÚassertRaisesÚStopIterationÚthrowExceptionIntoGeneratorre   rf   Ú
remoteTyper   ÚencodeÚargsÚremoteTraceback)r!   ÚoriginalÚcopyrÈ   Úgenr‡   r   rÇ   r   Ú test_throwExceptionIntoGeneratorx  s   
z/PBFailureTests.test_throwExceptionIntoGeneratorr-   )r   r   r   r
   ÚTestCasere   rb   ru   rx   r{   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ejjZdZdS )ÚPBFailureUnsafeTestsr`   N)r   r   r   r
   rÙ   ÚfailIfEqualsrb   rJ   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   Ú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Ê   ÚZeroDivisionErrorÚ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Õ   Ú
copiedOnceÚ
derivativeÚ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   ÚprintTracebackr   Úformatre   Úgetvalue)r!   rÕ   rã   ÚoutputrÀ   Ú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   Útwisted.internetr   r   Útwisted.pythonr   Útwisted.python.reflectr   Útwisted.spreadr   r   r	   Útwisted.trialr
   ré   r   r   r*   r   r   Ú	JellyableÚ
RemoteCopyr   r   ÚsetUnjellyableForClassÚglobalSecurityÚallowInstancesOfÚRootr   ÚPBServerFactoryr?   rÙ   rC   r^   rÚ   rÌ   rÝ   r   r   r   r   Ú<module>   s0   <" k	