
    Yf9                        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
e          Z G d de          Z G d dej                  Z G d dej                  Z G d de
j        ej        ej                  Z G d dej        ej                  Z ej        ee            ej        ee           ej                            e            G d dej                  Z G d dej                   Z! G d dej"                  Z# G d de#          Z$ G d de$          Z% G d d           Z& G d! d"ej"                  Z'd#S )$z!
Tests for error handling in PB.
    )StringIO)defer)log)qual)flavorsjellypb)connectedServerAndClient)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__     T/var/www/html/env/lib/python3.11/site-packages/twisted/spread/test/test_pbfailure.pyr   r                 r   r   c                       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      r   r   r   c                       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   "   r   r   r   c                       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   )   r   r   r   c                       e Zd ZdS )
JellyErrorNr   r   r   r   r   r   r   r   0           Dr   r   c                       e Zd ZdS )SecurityErrorNr   r   r   r   r"   r"   4   r    r   r"   c                   T    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 ZddZddZd
S )
SimpleRootc                 D    t          j        t          d                    S )zD
        Fail asynchronously with a non-pb.Error exception.
        zremote asynchronous exception)r   failr   selfs    r   remote_asynchronousExceptionz'SimpleRoot.remote_asynchronousException?   s     z/0OPPQQQr   c                      t          d          )zC
        Fail synchronously with a non-pb.Error exception.
        zremote synchronous exception)r   r'   s    r   remote_synchronousExceptionz&SimpleRoot.remote_synchronousExceptionE   s     ##ABBBr   c                 D    t          j        t          d                    S )z@
        Fail asynchronously with a pb.Error exception.
        zremote asynchronous error)r   r&   r   r'   s    r   remote_asynchronousErrorz#SimpleRoot.remote_asynchronousErrorK   s     z+,GHHIIIr   c                      t          d          )z?
        Fail synchronously with a pb.Error exception.
        zremote synchronous error)r   r'   s    r   remote_synchronousErrorz"SimpleRoot.remote_synchronousErrorQ   s     9:::r   c                 H     G d dt           j                  } |d          )z>
        Fail with error that is not known to client.
        c                       e Zd ZdS )4SimpleRoot.remote_unknownError.<locals>.UnknownErrorNr   r   r   r   UnknownErrorr2   \   s        Dr   r3   zI'm not known to client!)r	   Error)r(   r3   s     r   remote_unknownErrorzSimpleRoot.remote_unknownErrorW   sA    
	 	 	 	 	28 	 	 	 l5666r   c                 .    |                                   d S N)
raiseJellyr'   s    r   remote_jellyzSimpleRoot.remote_jellya   s    r   c                 .    |                                   d S r7   )raiseSecurityr'   s    r   remote_securityzSimpleRoot.remote_securityd   s    r   c                     t          j                    }|                    | j                   |                    d            |S r7   )r   DeferredaddCallbackr8   callbackr(   ds     r   remote_deferredJellyzSimpleRoot.remote_deferredJellyg   s:    N	do&&&	

4r   c                     t          j                    }|                    | j                   |                    d            |S r7   )r   r>   r?   r;   r@   rA   s     r   remote_deferredSecurityz"SimpleRoot.remote_deferredSecuritym   s;    N	d()))	

4r   Nc                      t          d          )NzI'm jellyable!)r   r(   resultss     r   r8   zSimpleRoot.raiseJellys   s    )***r   c                      t          d          )NzI'm secure!)r"   rG   s     r   r;   zSimpleRoot.raiseSecurityv   s    M***r   r7   )r   r   r   r)   r+   r-   r/   r5   r9   r<   rC   rE   r8   r;   r   r   r   r$   r$   >   s        R R RC C CJ J J; ; ;7 7 7        + + + ++ + + + + +r   r$   c                       e Zd ZdZdZd ZdS )SaveProtocolServerFactoryzd
    A L{pb.PBServerFactory} that saves the latest connected client in
    C{protocolInstance}.
    Nc                     || _         dS )z3
        Keep track of the given protocol.
        N)protocolInstance)r(   protocols     r   clientConnectionMadez.SaveProtocolServerFactory.clientConnectionMade   s     !)r   )r   r   r   r   rM   rO   r   r   r   rK   rK   z   s5         
 ) ) ) ) )r   rK   c                       e Zd ZdZd ZdS )PBConnTestCaser   c                      t          t                                 _         j         j        _        t	          j                     _        t           fd fd          \   _         _	         _
        d S )Nc                  8     j                             d           S r7   )serverFactorybuildProtocolr'   s   r   <lambda>z&PBConnTestCase.setUp.<locals>.<lambda>       D&44T:: r   c                  8     j                             d           S r7   )clientFactoryrU   r'   s   r   rV   z&PBConnTestCase.setUp.<locals>.<lambda>   rW   r   )rK   r$   rT   unsafeTracebacksr	   PBClientFactoryrY   r
   connectedServerconnectedClientpumpr'   s   `r   setUpzPBConnTestCase.setUp   sw    6z||DD.2.C+/11
 %::::::::
 
		
  IIIr   N)r   r   r   rZ   r_   r   r   r   rQ   rQ      s)        
 
 
 
 
r   rQ   c                       e Zd Zej        j        Zd Zd Zd Z	d Z
d Zd Zd Zdd	Zd
 Zd Zd Zd Zd Zd Zd Zd Zd ZdS )PBFailureTestsc                       fd j                                         }fd}|                    |            j                                         d S )Nc                     |                                                     | j        d           r8                              }                    t          |          d           | j        | j        | j        fS )NTraceback unavailable
   )trapcompare	tracebackflushLoggedErrorsassertEquallentypevalue)errerrsexceptionTypeflushr(   s     r   ebz)PBFailureTests._exceptionTest.<locals>.eb   sv    HH]###LL(ABBB /--m<<  TA...Hci77r   c                 \    |                                }|                               |S r7   )
callRemote
addErrback)rootrB   rr   methods     r   gotRootObjectz4PBFailureTests._exceptionTest.<locals>.gotRootObject   s+    ''ALLHr   rY   getRootObjectr?   r^   rq   )r(   rw   rp   rq   rB   rx   rr   s   ````  @r   _exceptionTestzPBFailureTests._exceptionTest   s    	8 	8 	8 	8 	8 	8 	8 ,,..	 	 	 	 	 	
 	
m$$$	r   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)r{   r   r'   s    r   test_asynchronousExceptionz)PBFailureTests.test_asynchronousException   s    
 ""#:<QSWXXXr   c                 :    |                      dt          d          S )zw
        Like L{test_asynchronousException}, but for a method which raises an
        exception synchronously.
        synchronousExceptionT)r{   r   r'   s    r   test_synchronousExceptionz(PBFailureTests.test_synchronousException   s    
 ""#9;OQUVVVr   c                 :    |                      dt          d          S )z
        Like L{test_asynchronousException}, but for a method which returns a
        Deferred failing with an L{pb.Error} subclass.
        asynchronousErrorF)r{   r   r'   s    r   test_asynchronousErrorz%PBFailureTests.test_asynchronousError   s    
 ""#68I5QQQr   c                 :    |                      dt          d          S )z}
        Like L{test_asynchronousError}, but for a method which synchronously
        raises a L{pb.Error} subclass.
        synchronousErrorF)r{   r   r'   s    r   test_synchronousErrorz$PBFailureTests.test_synchronousError   s    
 ""#57GOOOr   c                 2    |                      ||           |S r7   )rj   )r(   resultexpectedResults      r   _successzPBFailureTests._success   s    000r   c                 B    |                     | j        ||f           |S )N)callbackArgs)addCallbacksr   )r(   
remoteCallr   rr   s       r   _addFailingCallbacksz#PBFailureTests._addFailingCallbacks   s(    r@QRRRr   Nc                       j                                         } f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                                          |                                         }fd}|                    |           |S )Nc                 t                         t                                                  d           | S )Nre   )rj   rk   ri   )rn   excr(   s    r   
gotFailurez@PBFailureTests._testImpl.<locals>.gotRootObj.<locals>.gotFailure   s4    $$S)?)?)D)D%E%EqIIIJr   )r   rt   addBoth)objfailureDeferredr   rr   r   expectedrw   r(   s      r   
gotRootObjz,PBFailureTests._testImpl.<locals>.gotRootObj   sl    "77v&&" O        ''
333""r   Nry   )r(   rw   r   rr   r   rootDeferredr   s   `````  r   	_testImplzPBFailureTests._testImpl   sx     )7799	# 	# 	# 	# 	# 	# 	# 	# 	# 	  ,,,	r   c                 <      f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+   rf   r   assertNotIsInstancerl   strassertIsInstancerm   r&   r(   s    r   failureJellyz6PBFailureTests.test_jellyFailure.<locals>.failureJelly   sL    IIj!!!$$TY444!!$*di8882r   r   r   r   )r(   r   s   ` r   test_jellyFailurez PBFailureTests.test_jellyFailure   s4    	 	 	 	 	 ~~gr<888r   c                 <      f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                     |                      t                                         | j        t                                         | j        | j                   dS )N  r   r   s    r   failureDeferredJellyzFPBFailureTests.test_deferredJellyFailure.<locals>.failureDeferredJelly   sL    IIj!!!$$TY444!!$*di8883r   deferredJellyr   r   )r(   r   s   ` r   test_deferredJellyFailurez(PBFailureTests.test_deferredJellyFailure   s5    	 	 	 	 	 ~~os4HIIIr   c                 <      f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                 >                         | j        d           dS )Ns3   twisted.spread.test.test_pbfailure.SynchronousError  rj   rl   r   s    r   failureUnjellyablezBPBFailureTests.test_unjellyableFailure.<locals>.failureUnjellyable  s*    	Q   3r   r   r   r   )r(   r   s   ` r   test_unjellyableFailurez&PBFailureTests.test_unjellyableFailure  s6    	 	 	 	 	 ~~0#7IJJJr   c                 <      f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                 >                         | j        d           dS )Ns/   twisted.spread.test.test_pbfailure.UnknownError  r   r   s    r   failureUnknownz:PBFailureTests.test_unknownFailure.<locals>.failureUnknown  s*    	M   4r   unknownErrorr   r   )r(   r   s   ` r   test_unknownFailurez"PBFailureTests.test_unknownFailure  s4    	 	 	 	 	 ~~ndNCCCr   c                 <      f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                     |                      t                                         | j        t                                         | j        | j                   dS )N  rf   r"   r   rl   r   r   rm   r   s    r   failureSecurityz<PBFailureTests.test_securityFailure.<locals>.failureSecurity,  sL    IIm$$$$$TY444!!$*di8884r   securityr   r   )r(   r   s   ` r   test_securityFailurez#PBFailureTests.test_securityFailure%  s4    	 	 	 	 	 ~~j$@@@r   c                 <      f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                     |                      t                                         | j        t                                         | j        | j                   dS )N  r   r   s    r   failureDeferredSecurityzEPBFailureTests.test_deferredSecurity.<locals>.failureDeferredSecurity;  sL    IIm$$$$$TY444!!$*di8885r   deferredSecurityr   r   )r(   r   s   ` r   test_deferredSecurityz$PBFailureTests.test_deferredSecurity4  s6    	 	 	 	 	 ~~0%9PQQQr   c                 H      f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                 |    |                      t          j                                       | j        d           dS )Nrd     )rf   r	   NoSuchMethodrg   rh   r   s    r   failureNoSuchz>PBFailureTests.test_noSuchMethodFailure.<locals>.failureNoSuchI  s3    IIbo&&&LL)BCCC5r   nosuchr   )r   AttributeError)r(   r   s   ` r   test_noSuchMethodFailurez'PBFailureTests.test_noSuchMethodFailureC  s6    	 	 	 	 	
 ~~h}nMMMr   c                       j                                         }d }|                    |            f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                 ,    |                      d          S )Nr   )rt   )rootObjs    r   	connectedz;PBFailureTests.test_copiedFailureLogging.<locals>.connectedZ  s    %%&<===r   c                     t          j        |                                t                    }                    t          |          d           d S )N   )r   rn   ri   r   rj   rk   )failurero   r(   s     r   	exceptionz;PBFailureTests.test_copiedFailureLogging.<locals>.exception_  sI    GG))*>??DSYY*****r   N)rY   rz   r?   ru   r^   rq   )r(   rB   r   r   s   `   r   test_copiedFailureLoggingz(PBFailureTests.test_copiedFailureLoggingP  s     ,,..	> 	> 	> 	
i   	+ 	+ 	+ 	+ 	+
 	
Y	r   c                     t          j        t          d                    }t          j        t          j        |t                                          }g  f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      K   	 d V                       d           d S # t          j        $ r }                     |            Y d } ~ d S d } ~ ww xY w)NzRemoteError not raised)r&   r	   RemoteErrorappend)r   r   r(   s    r   generatorFunczFPBFailureTests.test_throwExceptionIntoGenerator.<locals>.generatorFuncq  sy      4


 		233333 > & & &  %%%%%%%%%&s     AA

ANre   r   ascii)r   rd   )r	   CopyableFailurer   r   unjellyDummyInvokersendassertRaisesStopIterationthrowExceptionIntoGeneratorrj   rk   
remoteTyper   encodeargsremoteTraceback)r(   originalcopyr   genr   r   s   `     @r    test_throwExceptionIntoGeneratorz/PBFailureTests.test_throwExceptionIntoGeneratorg  s%    %nU&;&;<<}U[<>>JJJKK		4 	4 	4 	4 	4 	4 moo-)I3OOOY+++ln)=)=)D)DW)M)MNNN8,,,,.GHHHHHr   r7   )r   r   r   r   TestCaserj   rg   r{   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ra   ra      sF       +G  &Y Y YW W WR R RP P P       09 9 9J J JK K KD D DA A AR R RN N N  .I I I I Ir   ra   c                   *    e Zd Zej        j        ZdZdS )PBFailureUnsafeTestsre   N)r   r   r   r   r   failIfEqualsrg   rZ   r   r   r   r   r     s!        ,Gr   r   c                       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   r   c                   $    e Zd ZdZd Zd Zd ZdS )FailureJellyingTestsz:
    Tests for the interaction of jelly and failures.
    c                 F   t          j        t                                }|                     |                    t                    t                     |                     |                    t
                    t
                     t          j        t          j        |t                                          }|                     |                    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   copieds      r   test_unjelliedFailureCheckz/FailureJellyingTests.test_unjelliedFailureCheck  s    
 %&7&9&9::hnn%6779JKKKhnn_55GGGu{8\^^LLLMMfll#4557HIIIfll?33_EEEEEr   c                    t          j        t                                }|                     |                    t                    t                     |                     |                    t
                    t
                     t          j        t          j        |t                                          }t          j        |          }t          j        t          j        |t                                          }|                     |                    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copiedTwices        r   test_twiceUnjelliedFailureCheckz4FailureJellyingTests.test_twiceUnjelliedFailureCheck  s     %&7&9&9::hnn%6779JKKKhnn_55GGG]5;x#P#P#PQQ
'
33
mEK
LNN$S$S$STTk''(9::<MNNNk''88/JJJJJr   c                    t          j        t          d                    }t          j        t          j        |t                                          }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   formatrj   getvalue)r(   r   r   outputr   expectedOutputs         r    test_printTracebackIncludesValuez5FailureJellyingTests.test_printTracebackIncludesValue  s     %i&>&>??u{8\^^LLLMMf%%%OO	MTT
 
 	):):;;;;;r   N)r   r   r   r   r   r  r  r   r   r   r   r     sQ         
F 
F 
FK K K"< < < < <r   r   N)(r   ior   twisted.internetr   twisted.pythonr   twisted.python.reflectr   twisted.spreadr   r   r	   twisted.test.iosimr
   twisted.trialr   r  r   r   r4   r   r   	Jellyable
RemoteCopyr   r"   setUnjellyableForClassglobalSecurityallowInstancesOfRootr$   PBServerFactoryrK   r   rQ   ra   r   r   r   r   r   r   <module>r     s:          " " " " " "       ' ' ' ' ' ' - - - - - - - - - - 7 7 7 7 7 7 " " " " " "
    I       9              rx   	 	 	 	 	"BHbm 	 	 		 	 	 	 	BHbm 	 	 	  *j 1 1 1  - 7 7 7   " "= 1 1 19+ 9+ 9+ 9+ 9+ 9+ 9+ 9+x) ) ) ) ) 2 ) ) )
 
 
 
 
X& 
 
 
"fI fI fI fI fI^ fI fI fIR    >   
" " " " " " " "1< 1< 1< 1< 1<8, 1< 1< 1< 1< 1<r   