
    Vh9                        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j0                        Z G d dej(                  ej0                        Z ej6                  ee        ej6                  ee       ej8                  j;                  e        G d dej<                        Z G d dej@                        Z! G d dejD                        Z# G d de#      Z$ G d de$      Z% G d d       Z& G d! d"ejD                        Z'y#)$z!
Tests for error handling in PB.
    )StringIO)defer)log)qual)flavorsjellypb)connectedServerAndClient)unittestc                       e Zd ZdZy)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/home/dcms/DCMS/lib/python3.12/site-packages/twisted/spread/test/test_pbfailure.pyr   r          r   r   c                       e Zd ZdZy)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y)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y)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y)
JellyErrorNr   r   r   r   r   r   r   r   0       r   r   c                       e Zd Zy)SecurityErrorNr   r   r   r   r"   r"   4   r    r   r"   c                   R    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y
)
SimpleRootc                 >    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     zz/0OP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Br   c                 >    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     zz+,GH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                 D     G d dt         j                        } |d      )z>
        Fail with error that is not known to client.
        c                       e Zd Zy)4SimpleRoot.remote_unknownError.<locals>.UnknownErrorNr   r   r   r   UnknownErrorr2   \   s    r   r3   zI'm not known to client!)r	   Error)r(   r3   s     r   remote_unknownErrorzSimpleRoot.remote_unknownErrorW   s     
	288 	 566r   c                 $    | j                          y N)
raiseJellyr'   s    r   remote_jellyzSimpleRoot.remote_jellya   s    r   c                 $    | j                          y r7   )raiseSecurityr'   s    r   remote_securityzSimpleRoot.remote_securityd   s    r   c                     t        j                         }|j                  | j                         |j	                  d        |S r7   )r   DeferredaddCallbackr8   callbackr(   ds     r   remote_deferredJellyzSimpleRoot.remote_deferredJellyg   s/    NN	doo&	

4r   c                     t        j                         }|j                  | j                         |j	                  d        |S r7   )r   r>   r?   r;   r@   rA   s     r   remote_deferredSecurityz"SimpleRoot.remote_deferredSecuritym   s1    NN	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CJ;7++r   r$   c                       e Zd ZdZdZd Zy)SaveProtocolServerFactoryzd
    A L{pb.PBServerFactory} that saves the latest connected client in
    C{protocolInstance}.
    Nc                     || _         y)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   s    
 )r   rK   c                       e Zd ZdZd Zy)PBConnTestCaser   c                      t        t                      _         j                   j                  _        t	        j
                          _        t         fd fd      \   _         _	         _
        y )Nc                  :     j                   j                  d       S r7   )serverFactorybuildProtocolr'   s   r   <lambda>z&PBConnTestCase.setUp.<locals>.<lambda>       D&&44T: r   c                  :     j                   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   s]    6z|D.2.C.C+//1
 %::
		
  Ir   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y)PBFailureTestsc                       fd j                   j                         }fd}|j                  |        j                  j	                          y )Nc                     | j                         j                  | j                  d       r,j                        }j	                  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   sb    HH]#LL(AB--m<  TA.HHcii77r   c                 L    | j                        }|j                         |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 ,,.	
 	
m$		r   c                 0    | j                  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Xr   c                 0    | j                  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Vr   c                 0    | j                  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Qr   c                 0    | j                  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Or   c                 *    | j                  ||       |S r7   )rj   )r(   resultexpectedResults      r   _successzPBFailureTests._success   s    0r   c                 D    |j                  | j                  ||f       |S )N)callbackArgs)addCallbacksr   )r(   
remoteCallr   rr   s       r   _addFailingCallbacksz#PBFailureTests._addFailingCallbacks   s$    r@QRr   Nc                       j                   j                         } fd}|j                  |        j                  j	                          y)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                 ~    j                  | j                              }fd}|j                  |       |S )Nc                 \    j                  t        j                              d       | S )Nre   )rj   rk   ri   )rn   excr(   s    r   
gotFailurez@PBFailureTests._testImpl.<locals>.gotRootObj.<locals>.gotFailure   s(    $$S)?)?)D%EqIJr   )r   rt   addBoth)objfailureDeferredr   rr   r   expectedrw   r(   s      r   
gotRootObjz,PBFailureTests._testImpl.<locals>.gotRootObj   sE    "77v&"O   ''
3""r   Nry   )r(   rw   r   rr   r   rootDeferredr   s   `````  r   	_testImplzPBFailureTests._testImpl   sA     ))779	# 	# 	  ,		r   c                 4      fd} j                  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                     | j                  t               j                  | j                  t               j                  | j                  | j                         y)N+   rf   r   assertNotIsInstancerl   strassertIsInstancerm   r&   r(   s    r   failureJellyz6PBFailureTests.test_jellyFailure.<locals>.failureJelly   s>    IIj!$$TYY4!!$**dii8r   r   r   r   )r(   r   s   ` r   test_jellyFailurez PBFailureTests.test_jellyFailure   s    	 ~~gr<88r   c                 4      fd} j                  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                     | j                  t               j                  | j                  t               j                  | j                  | j                         y)N  r   r   s    r   failureDeferredJellyzFPBFailureTests.test_deferredJellyFailure.<locals>.failureDeferredJelly   s>    IIj!$$TYY4!!$**dii8r   deferredJellyr   r   )r(   r   s   ` r   test_deferredJellyFailurez(PBFailureTests.test_deferredJellyFailure   s    	 ~~os4HIIr   c                 4      fd} j                  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                  | j                  d       y)Ns3   twisted.spread.test.test_pbfailure.SynchronousError  rj   rl   r   s    r   failureUnjellyablezBPBFailureTests.test_unjellyableFailure.<locals>.failureUnjellyable  s     		Q r   r   r   r   )r(   r   s   ` r   test_unjellyableFailurez&PBFailureTests.test_unjellyableFailure  s    	 ~~0#7IJJr   c                 4      fd} j                  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                  | j                  d       y)Ns/   twisted.spread.test.test_pbfailure.UnknownError  r   r   s    r   failureUnknownz:PBFailureTests.test_unknownFailure.<locals>.failureUnknown  s     		M r   unknownErrorr   r   )r(   r   s   ` r   test_unknownFailurez"PBFailureTests.test_unknownFailure  s    	 ~~ndNCCr   c                 4      fd} j                  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                     | j                  t               j                  | j                  t               j                  | j                  | j                         y)N  rf   r"   r   rl   r   r   rm   r   s    r   failureSecurityz<PBFailureTests.test_securityFailure.<locals>.failureSecurity,  s>    IIm$$$TYY4!!$**dii8r   securityr   r   )r(   r   s   ` r   test_securityFailurez#PBFailureTests.test_securityFailure%  s    	 ~~j$@@r   c                 4      fd} j                  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                     | j                  t               j                  | j                  t               j                  | j                  | j                         y)N  r   r   s    r   failureDeferredSecurityzEPBFailureTests.test_deferredSecurity.<locals>.failureDeferredSecurity;  s>    IIm$$$TYY4!!$**dii8r   deferredSecurityr   r   )r(   r   s   ` r   test_deferredSecurityz$PBFailureTests.test_deferredSecurity4  s    	 ~~0%9PQQr   c                 >      fd} j                  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                 |    | j                  t        j                         j                  | j                  d       y)Nrd     )rf   r	   NoSuchMethodrg   rh   r   s    r   failureNoSuchz>PBFailureTests.test_noSuchMethodFailure.<locals>.failureNoSuchI  s)    IIboo&LL)BCr   nosuchr   )r   AttributeError)r(   r   s   ` r   test_noSuchMethodFailurez'PBFailureTests.test_noSuchMethodFailureC  s    	
 ~~h}nMMr   c                       j                   j                         }d }|j                  |        fd}|j                  |        j                  j                          y)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                 $    | j                  d      S )Nr   )rt   )rootObjs    r   	connectedz;PBFailureTests.test_copiedFailureLogging.<locals>.connectedZ  s    %%&<==r   c                     t        j                  |        j                  t              }j	                  t        |      d       y )N   )r   rn   ri   r   rj   rk   )failurero   r(   s     r   	exceptionz;PBFailureTests.test_copiedFailureLogging.<locals>.exception_  s4    GGG))*>?DSY*r   N)rY   rz   r?   ru   r^   rq   )r(   rB   r   r   s   `   r   test_copiedFailureLoggingz(PBFailureTests.test_copiedFailureLoggingP  sM     ,,.	> 	
i 	+
 	
Y		r   c                 J    t        j                  t        d            }t        j                  t        j                  |t                           }g  fd} |       }|j                  d        j                  t        |j                  |        j                  t              d       d   } j                  |j                  t        t              j                  d              j                  |j                  d        j                  |j                   d	       y)
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  j                  d       y # t        j                  $ r} j                  |        Y d } ~ y d } ~ ww xY ww)NzRemoteError not raised)r&   r	   RemoteErrorappend)r   r   r(   s    r   generatorFunczFPBFailureTests.test_throwExceptionIntoGenerator.<locals>.generatorFuncq  sF     4
 		23 >> &  %%&s)   A AA	A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K		4 o-)I)I3OY+ln)=)D)DW)MN8,,,.G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      ss    ++G&YWRP09JKDARN.Ir   ra   c                   <    e Zd Zej                  j
                  ZdZy)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y)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y)FailureJellyingTestsz:
    Tests for the interaction of jelly and failures.
    c                    t        j                  t                     }| j                  |j	                  t              t               | j                  |j	                  t
              t
               t        j                  t        j                  |t                           }| j                  |j	                  t              t               | j                  |j	                  t
              t
               y)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:hnn%679JKhnn_5Gu{{8\^LMfll#457HIfll?3_Er   c                 x   t        j                  t                     }| j                  |j	                  t              t               | j                  |j	                  t
              t
               t        j                  t        j                  |t                           }t        j                  |      }t        j                  t        j                  |t                           }| j                  |j	                  t              t               | j                  |j	                  t
              t
               y)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:hnn%679JKhnn_5G]]5;;x#PQ
''
3
mmEKK
LN$STk''(9:<MNk''8/Jr   c                 Z   t        j                  t        d            }t        j                  t        j                  |t                           }t               }|j                  |       t        t              }dj                  |      }| j                  ||j                                y)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Mf%O	MTT
 	):;r   N)r   r   r   r   r   r  r  r   r   r   r   r     s    
F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     s8    "  ' - - 7 "
I 9  rxx 	""BHHbmm 		BHHbmm 	   *j 1   - 7    " "= 19+ 9+x) 2 2 )
X&& 
"fI^ fIR> 
" "1<8,, 1<r   