
    Vh6                     "   d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z
 ddlmZ ddlmZmZ ddlmZmZmZ ddlmZmZ dd	lmZmZmZmZ dd
lmZmZmZ ddl m!Z! ddl"m#Z# ddl$m%Z% ddl&m'Z'm(Z( ddl)m*Z* ddl+m,Z,m-Z-m.Z.m/Z/ ddl0m1Z1  G d de-jd                        Z3 G d de-jh                        Z5 eejl                         G d d             Z7 G d d      Z8d Z9 G d d      Z: G d de#      Z;ddZ< G d  d!e-jz                        Z> G d" d#e-jz                        Z? G d$ d%e-j                        ZA G d& d'e-j                        ZC e-j                  eAeC        G d( d)e-j                        ZEd* ZF e-j                  eEeF        G d+ d,e-jz                        ZH G d- d.e-j                        ZJ G d/ d0e-jz                        ZK G d1 d2e-j                        ZL G d3 d4e-j                        ZN e-j                  eLeN        G d5 d6e-j                        ZO e-j                  eJeO        G d7 d8e-jz                        ZP G d9 d:e-jz                        ZQ G d; d<e-jz                        ZR G d= d>e-jz                        ZS G d? d@e-j                  e-j                        ZT e-j                  eTeT        G dA dBe-j                  e-j                        ZU e-j                  eUeU        G dC dDe-j                  e-j                        ZV e-j                  eVeV        G dE dFe-j                        ZX G dG dHe-j                        ZY G dI dJe1j                        Z[ G dK dLe-j                        Z] G dM dNe1j                        Z_ G dO dPe1j                        Z`dQZadabdacdR Zd G dS dTe-jz                        Ze G dU dVe-jz                        Zf G dW dXe1j                        Zg G dY dZe.j                        Zi G d[ d\e.j                        Zk G d] d^e-jz                        Zl e-j                  eiek        G d_ d`e1j                        Zm G da dben      Zo G dc dde-j                        Zp G de dfe-jz                        Zq G dg dhe/j                        Zs ee-j                         G di dje-jh                               Zu G dk dl      Zv G dm dne-jd                        Zw G do dpev      Zx G dq dre1j                        Zy G ds dte1j                        Zz ee-j                         G du dv             Z{ G dw dxe1j                        Z| G dy dze      Z} ee}       G d{ d|             Z~ G d} d~e1j                        Z G d de1j                        Zy)z
Tests for Perspective Broker module.

TODO: update protocol level tests to use new connection API, leaving
only specific tests for old API.
    N)deque)BytesIO)Dict)	Interfaceimplementer)checkerscredentialsportal)UnauthorizedLoginUnhandledCredentials)addressmainprotocolreactor)DeferredgatherResultssucceed)ConnectionRefusedError)_FakeConnector)WrappingFactory)failurelog)	iterbytes)jellypbpublishutil)unittestc                       e Zd Zd Zy)Dummyc                 &    t        |t              ryy)Nzhello world!zgoodbye, cruel world!)
isinstanceDummyPerspectiveselfusers     K/home/dcms/DCMS/lib/python3.12/site-packages/twisted/spread/test/test_pb.pyview_doNothingzDummy.view_doNothing(   s    d,-!*    N)__name__
__module____qualname__r(    r)   r'   r    r    '   s    +r)   r    c                       e Zd ZdZd Zy)r#   zE
    An L{IPerspective} avatar which will be used in some tests.
    c                     t               S N)r    r%   s    r'   perspective_getDummyViewPointz.DummyPerspective.perspective_getDummyViewPoint4   s	    wr)   N)r*   r+   r,   __doc__r2   r-   r)   r'   r#   r#   /   s    r)   r#   c                       e Zd Zd Zy)
DummyRealmc                 X    |D ]%  }|t         j                  u s|t        |      d fc S  y )Nc                       y r0   r-   r-   r)   r'   <lambda>z*DummyRealm.requestAvatar.<locals>.<lambda>=       r)   )r   IPerspectiver#   )r%   avatarIdmind
interfacesifaces        r'   requestAvatarzDummyRealm.requestAvatar:   s2     	GE'.x8,FF	Gr)   N)r*   r+   r,   r?   r-   r)   r'   r5   r5   8   s    Gr)   r5   c                   (    e Zd ZdZd Zd Zd Zd Zy)IOPumpz
    Utility to pump data between clients and servers for protocol testing.

    Perhaps this is a utility worthy of being in protocol.py?
    c                 <    || _         || _        || _        || _        y r0   )clientserverclientIOserverIO)r%   rC   rD   rE   rF   s        r'   __init__zIOPump.__init__G   s      r)   c                     d| _         t        j                         dz   }| j                   sH| j                         r7t        j                         |kD  ry| j                   s| j                         r5yyyy)z
        Pump until there is no more input or output or until L{stop} is called.
        This does not run any timers, so don't use it with any code that calls
        reactor.callLater.
        F   N)_stoptimepump)r%   timeouts     r'   flushzIOPump.flushM   sQ     
))+/**yy{W$ ****r)   c                     d| _         y)zd
        Stop a running L{flush} operation, even if data remains to be
        transferred.
        TN)rJ   r1   s    r'   stopzIOPump.stopZ   s    
 
r)   c                    | j                   j                  d       | j                  j                  d       | j                   j                         }| j                  j                         }| j                   j                  d       | j                  j                  d       | j                   j	                          | j                  j	                          | j
                  j                  j                          | j                  j                  j                          t        |      D ]  }| j                  j                  |        t        |      D ]  }| j
                  j                  |        |s|ryy)zX
        Move data back and forth.

        Returns whether any data was moved.
        r      )rE   seekrF   readtruncaterC   	transport_checkProducerrD   r   dataReceived)r%   cDatasDatabytes       r'   rL   zIOPump.pumpa   s    	11""$""$11  ,,.,,.e$ 	+DKK$$T*	+e$ 	+DKK$$T*	+Er)   N)r*   r+   r,   r3   rG   rN   rP   rL   r-   r)   r'   rA   rA   @   s    !r)   rA   c                    d}j                  |      }|j                  |      }t               }t               }|j                  t        j                  |             |j                  t        j                  |             t        ||||      }d }	fd}
| j                  |	|       | j                  |	|       | j                  |
       |j                          |||fS )al  
    Create a server and a client and connect the two with an
    L{IOPump}.

    @param test: the test case where the client and server will be
        used.
    @type test: L{twisted.trial.unittest.TestCase}

    @param clientFactory: The factory that creates the client object.
    @type clientFactory: L{twisted.spread.pb.PBClientFactory}

    @param serverFactory: The factory that creates the server object.
    @type serverFactory: L{twisted.spread.pb.PBServerFactory}

    @return: a 3-tuple of (client, server, pump)
    @rtype: (L{twisted.spread.pb.Broker}, L{twisted.spread.pb.Broker},
        L{IOPump})
    )	127.0.0.1c                     | j                   s3| j                  t        j                  t        j
                               y y r0   )disconnectedconnectionLostr   Failurer   CONNECTION_DONE)brokers    r'   maybeDisconnectz/connectServerAndClient.<locals>.maybeDisconnect   s-    ""!!'//$2F2F"GH #r)   c                  n     j                  d t        j                  t        j                               y )N)	connectorreason)clientConnectionLostr   ra   r   rb   )clientFactorys   r'   disconnectClientFactoryz7connectServerAndClient.<locals>.disconnectClientFactory   s,    
 	**7??43G3G#H 	+ 	
r)   )buildProtocolStringIOmakeConnectionr   FileWrapperrA   
addCleanuprL   )testri   serverFactoryaddrclientBrokerserverBrokerclientTransportserverTransportrL   rd   rj   s    `         r'   connectServerAndClientrw   {   s    & D ..t4L ..t4LjOjO 4 4_ EF 4 4_ EF,oODI
 	OOO\2OOO\2OO+,IIKt++r)   c                   "    e Zd ZdZd Zd Zd Zy)_ReconnectingFakeConnectorStatez
    Manages connection notifications for a
    L{_ReconnectingFakeConnector} instance.

    @ivar notifications: pending L{Deferreds} that will fire when the
        L{_ReconnectingFakeConnector}'s connect method is called
    c                 "    t               | _        y r0   )r   notificationsr1   s    r'   rG   z(_ReconnectingFakeConnectorState.__init__   s    "Wr)   c                 P    t               }| j                  j                  |       |S )z
        Connection notification.

        @return: A L{Deferred} that fires when this instance's
            L{twisted.internet.interfaces.IConnector.connect} method
            is called.
        @rtype: L{Deferred}
        )r   r{   
appendleft)r%   notifiers     r'   notifyOnConnectz/_ReconnectingFakeConnectorState.notifyOnConnect   s$     :%%h/r)   c                     | j                   r7| j                   j                         j                  |        | j                   r6yy)z1
        Fire all pending notifications.
        N)r{   popcallbackr1   s    r'   	notifyAllz)_ReconnectingFakeConnectorState.notifyAll   s5       ""$--d3   r)   N)r*   r+   r,   r3   rG   r   r   r-   r)   r'   ry   ry      s    %4r)   ry   c                   ,     e Zd ZdZ fdZ fdZ xZS )_ReconnectingFakeConnectorzc
    A fake L{IConnector} that can fire L{Deferred}s when its
    C{connect} method is called.
    c                 2    t         |   |       || _        y)a  
        @param address: An L{IAddress} provider that represents this
            connector's destination.
        @type address: An L{IAddress} provider.

        @param state: The state instance
        @type state: L{_ReconnectingFakeConnectorState}
        N)superrG   _state)r%   r   state	__class__s      r'   rG   z#_ReconnectingFakeConnector.__init__   s     	!r)   c                 V    t         |           | j                  j                          y)zM
        A C{connect} implementation that calls C{reconnectCallback}
        N)r   connectr   r   )r%   r   s    r'   r   z"_ReconnectingFakeConnector.connect   s     	r)   )r*   r+   r,   r3   rG   r   __classcell__)r   s   @r'   r   r      s    

   r)   r   c                     |xs
 t               }t        j                  d      }t        j                  t        j                  ||g            }t        j                         }t        | ||      S )z
    Connect a client and server L{Broker} together with an L{IOPump}

    @param realm: realm to use, defaulting to a L{DummyRealm}

    @returns: a 3-tuple (client, server, pump).
       guest)guest)	r5   r   'InMemoryUsernamePasswordDatabaseDontUser   PBServerFactoryr
   PortalPBClientFactoryrw   )rp   realmcheckerrq   ri   s        r'   connectedServerAndClientr      sZ     !Z\E>>XNG&&v}}UWI'FGM&&(M!$}EEr)   c                       e Zd Zd Zd Zy)SimpleRemotec                     || _         |dz   S NrR   )argr%   r   s     r'   remote_thunkzSimpleRemote.remote_thunk       Qwr)   c                     t               r0   )	Exceptionr   s     r'   remote_knuthzSimpleRemote.remote_knuth   s
    kr)   N)r*   r+   r,   r   r   r-   r)   r'   r   r      s    r)   r   c                       e Zd Zd Zy)NestedRemotec                     t               S r0   )r   r1   s    r'   remote_getSimplezNestedRemote.remote_getSimple  s
    ~r)   N)r*   r+   r,   r   r-   r)   r'   r   r     s    r)   r   c                       e Zd Zd Zy)
SimpleCopyc                 4    d| _         ddi| _        dg| _        y NrR   HelloWorldrp   xyzr1   s    r'   rG   zSimpleCopy.__init__      7#r)   Nr*   r+   r,   rG   r-   r)   r'   r   r         r)   r   c                       e Zd Zy)SimpleLocalCopyNr*   r+   r,   r-   r)   r'   r   r         r)   r   c                   0    e Zd ZU dZi Zeed f   ed<   d Zy)SimpleFactoryCopyz]
    @cvar allIDs: hold every created instances of this class.
    @type allIDs: C{dict}
    allIDsc                 8    || _         | t        j                  |<   y r0   )idr   r   )r%   r   s     r'   rG   zSimpleFactoryCopy.__init__  s    '+  $r)   N)	r*   r+   r,   r3   r   r   int__annotations__rG   r-   r)   r'   r   r     s#    
 .0FD))*/,r)   r   c                     | j                  dd      }|t        dt        |              |t        j                  vrt        dt        j                         t        j                  |   }|st        d      |S )zl
    Factory of L{SimpleFactoryCopy}, getting a created instance given the
    C{id} found in C{state}.
    r   Nz&factory copy state has no 'id' member zfactory class has no ID: z&factory method found no object with id)getRuntimeErrorreprr   r   )r   stateIdinsts      r'   createFactoryCopyr   !  s~    
 iid#GCDK=QRR'...67H7O7O6PQRR##G,DCDDKr)   c                       e Zd Zd Zd Zy)
NestedCopyc                     t               S r0   )r   r1   s    r'   remote_getCopyzNestedCopy.remote_getCopy5  s
    |r)   c                     t        |      S r0   )r   )r%   values     r'   remote_getFactoryzNestedCopy.remote_getFactory8  s     ''r)   N)r*   r+   r,   r   r   r-   r)   r'   r   r   4  s    (r)   r   c                       e Zd Zd Zy)SimpleCachec                 4    d| _         ddi| _        dg| _        y r   r   r1   s    r'   	__init___zSimpleCache.__init___=  r   r)   N)r*   r+   r,   r   r-   r)   r'   r   r   <  r   r)   r   c                       e Zd Zd Zd Zy)NestedComplicatedCachec                 "    t               | _        y r0   )VeryVeryComplicatedCacheablecr1   s    r'   rG   zNestedComplicatedCache.__init__D  s    -/r)   c                     | j                   S r0   )r   r1   s    r'   remote_getCachez&NestedComplicatedCache.remote_getCacheG  s    vvr)   N)r*   r+   r,   rG   r   r-   r)   r'   r   r   C  s    0r)   r   c                   $    e Zd Zd Zd Zd Zd Zy)r   c                 .    d| _         d| _        d| _        y )NrR         r   r   foor1   s    r'   rG   z%VeryVeryComplicatedCacheable.__init__L  s    r)   c                 J    d| _         | j                  j                  dd       y )N   r   )r   observer
callRemoter1   s    r'   setFoo4z$VeryVeryComplicatedCacheable.setFoo4Q  s      *r)   c                 X    || _         | j                  | j                  | j                  dS )Nr   )r   r   r   r   r%   perspectiver   s      r'   getStateToCacheAndObserveForz9VeryVeryComplicatedCacheable.getStateToCacheAndObserveForU  s"     VV$&&::r)   c                 ~    t        j                  d       |j                  d       || j                  k(  rd | _        y y )Nzstopped observingend)r   msgr   r   r   s      r'   stoppedObservingz-VeryVeryComplicatedCacheable.stoppedObservingY  s5    #$E"t}}$ DM %r)   N)r*   r+   r,   rG   r   r   r   r-   r)   r'   r   r   K  s    
+;!r)   r   c                       e Zd Zd Zd Zy)RatherBaroqueCachec                     || _         y r0   r   )r%   newFoos     r'   observe_foozRatherBaroqueCache.observe_fooa  s	    r)   c                 .    t        j                  d       y )Nzthe end of things)r   r   r1   s    r'   observe_endzRatherBaroqueCache.observe_endd  s    #$r)   N)r*   r+   r,   r   r   r-   r)   r'   r   r   `  s    %r)   r   c                   $    e Zd Zd Zd Zd Zd Zy)SimpleLocalCachec                 :    | j                   j                  |       y r0   )__dict__update)r%   r   s     r'   setCopyableStatez!SimpleLocalCache.setCopyableStatel  s    U#r)   c                     | j                   S r0   )checkr1   s    r'   checkMethodzSimpleLocalCache.checkMethodo      zzr)   c                     | S r0   r-   r1   s    r'   	checkSelfzSimpleLocalCache.checkSelfr  s    r)   c                      yr   r-   r1   s    r'   r   zSimpleLocalCache.checku  s    r)   N)r*   r+   r,   r   r   r   r   r-   r)   r'   r   r   k  s    $r)   r   c                       e Zd Zd Zd Zd Zy)NestedCachec                 "    t               | _        y r0   )r   r   r1   s    r'   rG   zNestedCache.__init__}  s    r)   c                 2    | j                   | j                   gS r0   r   r1   s    r'   r   zNestedCache.remote_getCache  s    r)   c                     | j                   |u S r0   r  r%   caches     r'   remote_putCachezNestedCache.remote_putCache  s    vvr)   N)r*   r+   r,   rG   r   r  r-   r)   r'   r  r  |  s     r)   r  c                   $    e Zd Zd Zd Zd Zd Zy)
Observablec                     g | _         y r0   )	observersr1   s    r'   rG   zObservable.__init__  s	    r)   c                 :    | j                   j                  |       y r0   )r  appendr%   obss     r'   remote_observezObservable.remote_observe      c"r)   c                 :    | j                   j                  |       y r0   )r  remover  s     r'   remote_unobservezObservable.remote_unobserve  r  r)   c                 L    | j                   D ]  }|j                  d| |        y )Nnotify)r  r   )r%   objr   s      r'   r  zObservable.notify  s'     	5H$4	5r)   N)r*   r+   r,   rG   r  r  r  r-   r)   r'   r
  r
    s    ##5r)   r
  c                   $    e Zd Zd Zd Zd Zd Zy)DeferredRemotec                     d| _         y )Nr   runr1   s    r'   rG   zDeferredRemote.__init__  s	    r)   c                     || _         |dz   S r   r  r   s     r'   runMezDeferredRemote.runMe  r   r)   c                     J d       )Nzshouldn't have been run!r-   r   s     r'   	dontRunMezDeferredRemote.dontRunMe  s    ,,,qr)   c                 t    t               }|j                  | j                  | j                         || _        |S )zo
        Return a L{Deferred} to be fired on client side. When fired,
        C{self.runMe} is called.
        )r   addCallbacksr  r!  d)r%   r$  s     r'   remote_doItLaterzDeferredRemote.remote_doItLater  s-    
 J	tzz4>>2r)   N)r*   r+   r,   rG   r  r!  r%  r-   r)   r'   r  r    s    -r)   r  c                       e Zd ZdZdZd Zy)Observerr   Nc                 ^    || _         | j                  dz   | _        |j                  d|        y )NrR   	unobserve)r  notifiedr   )r%   otherr  s      r'   remote_notifyzObserver.remote_notify  s)    )d+r)   )r*   r+   r,   r*  r  r,  r-   r)   r'   r'  r'    s    H
C,r)   r'  c                       e Zd Zd Zy)NewStyleCopyc                     || _         y r0   )s)r%   r0  s     r'   rG   zNewStyleCopy.__init__  s	    r)   Nr   r-   r)   r'   r.  r.    s    r)   r.  c                   $    e Zd ZdZdZdZd Zd Zy)NewStyleCopy2r   rR   c                 p    t         xj                  dz  c_        t        j                  |       }d|_        |S )NrR   r   )r2  	allocatedobject__new__r   )r%   r   s     r'   r6  zNewStyleCopy2.__new__  s-    1$~~d#
r)   c                 6    t         xj                  dz  c_        y r   )r2  initializedr1   s    r'   rG   zNewStyleCopy2.__init__  s    !!Q&!r)   N)r*   r+   r,   r4  r8  r   r6  rG   r-   r)   r'   r2  r2    s    IKE'r)   r2  c                       e Zd Zd Zy)NewStyleCacheCopyc                     | j                   S r0   )r   r   s      r'   r   z.NewStyleCacheCopy.getStateToCacheAndObserveFor  s    }}r)   N)r*   r+   r,   r   r-   r)   r'   r:  r:    s    r)   r:  c                       e Zd Zd Zd Zy)Echoerc                     |S r0   r-   r%   sts     r'   remote_echozEchoer.remote_echo  s    	r)   c                 
    ||fS r0   r-   )r%   r@  kws      r'   remote_echoWithKeywordszEchoer.remote_echoWithKeywords  s    Bxr)   N)r*   r+   r,   rA  rD  r-   r)   r'   r=  r=    s    r)   r=  c                       e Zd Zd Zd Zy)CachedReturnerc                     || _         y r0   r  r  s     r'   rG   zCachedReturner.__init__  s	    
r)   c                     | j                   S r0   rH  r?  s     r'   remote_giveMeCachez!CachedReturner.remote_giveMeCache  r   r)   N)r*   r+   r,   rG   rJ  r-   r)   r'   rF  rF    s    r)   rF  c                   *    e Zd Zd Zd Zd Zd Zd Zy)NewStyleTestsc                    t        j                  t                     | _        t        j                         }t        | || j                        \  }| _        | _        | j                  |j                               | _
        y)zY
        Create a pb server using L{Echoer} protocol and connect a client to it.
        )rp   ri   rq   N)r   r   r=  rq   r   rw   rD   rL   successResultOfgetRootObjectref)r%   ri   rC   s      r'   setUpzNewStyleTests.setUp  sf      //9**,)?]$BTBT*
&TY ''(C(C(EFr)   c                 F    dt         _        dt         _        dt         _        y)zp
        Close client and server connections, reset values of L{NewStyleCopy2}
        class variables.
        r   rR   N)r2  r4  r8  r   r1   s    r'   tearDownzNewStyleTests.tearDown  s    
 #$$%!r)   c                      t        d       j                  j                  d      } j                  j	                           fd}|j                  |       |S )zY
        Create a new style object, send it over the wire, and check the result.
        r   echoc                     j                  | t               j                  | j                  d       j	                  | u        y )Nr   )assertIsInstancer.  assertEqualr0  assertFalseresorigr%   s    r'   cbz'NewStyleTests.test_newStyle.<locals>.cb  s9    !!#|4SUUG,SD[)r)   r.  rP  r   rL   rN   addCallbackr%   r$  r]  r\  s   `  @r'   test_newStylezNewStyleTests.test_newStyle  sK     G$HH-			*
 	
br)   c                 8    t                j                  t         j                  d        j                  t         j                  d        j                  j                  d      } j                  j                           fd}|j                  |       |S )zN
        Send a new style object and check the number of allocations.
        rR   rU  c                    j                  | t               j                  | j                  d       j                  t        j                  d       j                  t        j
                  d       j                  |        y )Nr   r   rR   )rW  r2  rX  r   r4  r8  assertIsNotrZ  s    r'   r]  z$NewStyleTests.test_alloc.<locals>.cb  sc    !!#}5SYY*]44a8]66:S$'r)   )	r2  rX  r4  r8  rP  r   rL   rN   r_  r`  s   `  @r'   
test_alloczNewStyleTests.test_alloc  st     00!422A6HH-			( 	
br)   c                      t        d       j                  j                  ddd      } j                  j	                           fd}|j                  |       |S )zo
        Create a new style object with keywords,
        send it over the wire, and check the result.
        value1echoWithKeywordsonetwokeyword1keyword2c                 .   j                  | t               j                  | d   t               j                  | d   t               j	                  | d   j
                  d       j                  | d          j	                  | d   ddd       y )Nr   rR   rg  ri  rj  rk  )rW  tupler.  dictrX  r0  rd  rZ  s    r'   r]  z3NewStyleTests.test_newStyleWithKeywords.<locals>.cb.  s    !!#u-!!#a&,7!!#a&$/SVXXx0SVT*SV%U%KLr)   r^  r`  s   `  @r'   test_newStyleWithKeywordsz'NewStyleTests.test_newStyleWithKeywords#  sZ    
 H%HHuu   
 				M 	
br)   N)r*   r+   r,   rQ  rS  ra  re  rq  r-   r)   r'   rL  rL    s    	G  ,r)   rL  c                        e Zd ZdZdZd Zd Zy)ConnectionNotifyServerFactoryaj  
    A server factory which stores the last connection and fires a
    L{Deferred} on connection made. This factory can handle only one
    client connection.

    @ivar protocolInstance: the last protocol instance.
    @type protocolInstance: C{pb.Broker}

    @ivar connectionMade: the deferred fired upon connection.
    @type connectionMade: C{Deferred}
    Nc                 b    t         j                  j                  | |       t               | _        y)z)
        Initialize the factory.
        N)r   r   rG   r   connectionMade)r%   roots     r'   rG   z&ConnectionNotifyServerFactory.__init__I  s$     	##D$/&jr)   c                 b    || _         | j                  dc}| _        ||j                  d       yy)zF
        Store the protocol and fire the connection deferred.
        N)protocolInstanceru  r   )r%   r   r$  s      r'   clientConnectionMadez2ConnectionNotifyServerFactory.clientConnectionMadeP  s7     !)!%!4!4d4=JJt r)   )r*   r+   r,   r3   rx  rG   ry  r-   r)   r'   rs  rs  :  s    
 )r)   rs  c                       e Zd Zd Zd Zd Zy)NewStyleCachedTestsc                     t                _        d j                  _        t        j                  dt        t         j                                     _        t        j                         }t        j                  d j                  j                         j                  |        fd}|j                         j                  |      } j                  j                  j                   }t#        ||g      S )zi
        Create a pb server using L{CachedReturner} protocol and connect a
        client to it.
        r   r   	localhostc                     | _         y r0   )rP  )rP  r%   s    r'   gotRootz*NewStyleCachedTests.setUp.<locals>.gotRooth  s
    DHr)   )r:  r\  r0  r   	listenTCPrs  rF  rD   r   r   
connectTCPgetHostportrO  r_  factoryru  r   )r%   ri   r  d1d2s   `    r'   rQ  zNewStyleCachedTests.setUp[  s    
 &'			'',^DII-FG
 **,;(;(;(=(B(BMR	 ((*66w?[[  //b"X&&r)   c                    | j                   j                  j                  j                  j	                          | j
                  j                  j                  j	                          | j                   j                         S )z6
        Close client and server connections.
        )rD   r  rx  rV   loseConnectionrP  rc   stopListeningr1   s    r'   rS  zNewStyleCachedTests.tearDowno  sS     	,,66EEG!!002{{((**r)   c                       j                   j                  d j                        } fd}|j                  |d       |j                  |d       |S )z
        A new-style cacheable object can be retrieved and re-retrieved over a
        single connection.  The value of an attribute of the cacheable can be
        accessed on the receiving side.
        giveMeCachec                     j                  | t               j                  d| j                         j	                  j
                  |        |r-| _        j                  j                  dj
                        S y )Nr   r  )	rW  r:  rX  r0  rd  r\  r[  rP  r   )r[  againr%   s     r'   r]  z2NewStyleCachedTests.test_newStyleCache.<locals>.cb  sh    !!#'89Wcee,TYY, xx**=$))DD r)   TF)rP  r   r\  r_  )r%   r$  r]  s   `  r'   test_newStyleCachez&NewStyleCachedTests.test_newStyleCachew  sH     HHtyy9	E 	
b$	b% r)   N)r*   r+   r,   rQ  rS  r  r-   r)   r'   r{  r{  Z  s    '(+r)   r{  c                   v    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zy)BrokerTestsNc                 N    	 t        j                  d       y # t        $ r Y y w xY w)NNone-None-TESTING.pub)osunlinkOSErrorr1   s    r'   rS  zBrokerTests.tearDown  s&    	II-. 		s    	$$c                 ,    | j                  d|        y )Nz&This should cause a return value, not fail)r%   errors     r'   thunkErrorBadzBrokerTests.thunkErrorBad  s    		:5'BCr)   c                     || _         y r0   )thunkResultr%   results     r'   thunkResultGoodzBrokerTests.thunkResultGood  s
    !r)   c                      y r0   r-   )r%   tbs     r'   thunkErrorGoodzBrokerTests.thunkErrorGood      r)   c                 ,    | j                  d|        y )Nz This should cause an error, not r  r  s     r'   thunkResultBadzBrokerTests.thunkResultBad  s    		4VH=>r)   c                    t        |       \  }}} G d dt        j                        } G d dt        j                        }|j                  d |              |j	                  d      } |       } |       }|j                  d||       |j                          |j                          |j                          | j                  |j                  |d       | j                  |j                  d      |j                  d             y )	Nrp   c                       e Zd Zd Zy)%BrokerTests.test_reference.<locals>.Xc                     || _         y r0   )caughtr   s     r'   remote_catchz2BrokerTests.test_reference.<locals>.X.remote_catch  s	    !r)   N)r*   r+   r,   r  r-   r)   r'   Xr    s    "r)   r  c                       e Zd Zd Zy)%BrokerTests.test_reference.<locals>.Yc                 (    |j                  d|       y )Ncatchr   r%   abs      r'   remote_throwz2BrokerTests.test_reference.<locals>.Y.remote_throw  s    Wa(r)   N)r*   r+   r,   r  r-   r)   r'   Yr    s    )r)   r  r   throwzX should have caught Z)r   r   ReferenceablesetNameForLocalremoteForNamer   rL   assertIsr  rX  remoteMethod)	r%   r   r0  rL   r  r  r   r   r   s	            r'   test_referencezBrokerTests.test_reference  s    -48
1d	"   	"	)   	) 	
#qs#OOC CC	Wa#						ahh#;< 	0!..2IJr)   c                    t        |       \  }}}||f||ffD ]  \  }}t               }|j                  d|       |j                  d      }d| _        |j                  d| j                  dz
        j                  | j                  | j                         |j                          |j                          | j                  | j                  | j                  d        y )Nr  r      thunkrR   zresult wasn't received.)r   r   r  r  expectedThunkResultr   r#  r  r  rL   rX  r  )r%   r   r0  rL   r   r   r   bars           r'   test_resultzBrokerTests.test_result  s    -48
1dFQFN 	DAq.CeS)//%(C'(D$NN7D$<$<q$@ANN$$d&8&8 IIKIIK  $":":<U	r)   c                     || _         y r0   )nestedRemoter  s     r'   refcountResultzBrokerTests.refcountResult  s
    "r)   c           
      v   g }g }t        |       \  }}}t               }|j                  d|       |j                  d      }t	        t
        j                  dz         D ]u  }|j                  j                  s|j                  j                  r nG|j                  d      j                  |j                  |j                         |j                          w t
        j                  dz
  }	| j                  |j                  j                  d       | j                  t        |      |	d|	 dt        |              y )	Nr  r   
   	getSimplerR   ztransport was not closedz	expected z got )r   r   r  r  ranger   MAX_BROKER_REFSrV   closedr   r#  r  rL   
assertTruerX  len)
r%   ler   r0  rL   r   r   ignoexpecteds
             r'   test_tooManyRefszBrokerTests.test_tooManyRefs  s    -48
1dn	%%OOE"",,r12 	D{{!!Q[[%7%7LL%22188QXXFIIK		
 %%)**,FGQYxjc!fX+NOr)   c                    t        |       \  }}}t               }|j                  d|       |j                  d      }|j	                  d      j                  | j                  | j                         |j                          |j                          | j                  | j                  j                  d       | j                  | j                  j                  d   d       | j                  | j                  j                  d   d       y )	Nr  r   getCopyrR   r   r   r   rp   )r   r   r  r  r   r#  r  r  rL   rX  r  r   r   r   )r%   r   r0  rL   r   r   s         r'   	test_copyzBrokerTests.test_copy  s    -48
1dl	%%OOE"	Y,,T-A-A4CUCUV				))++Q/))++G4g>))++A.7r)   c                    t        |       \  }}}t               }t               }|j                  d|       |j	                  d      }|j                  d|       |j                          |j                  d       |j                          |j                          |j                  d       |j                          |j                          | j                  |j                  d       | j                  |j                  dd       y )Nr  r  observerR   r  zdidn't notifyznotified too much)r   r
  r'  r  r  r   rL   r  assertIsNotNoner  rX  )r%   r   r0  rL   r  r  ras          r'   test_observezBrokerTests.test_observe  s    -48
1d LJ	#q!__S!
i#												QUUO4#67r)   c                 j   t        |       \  }}}t               }|j                  d|       |j                  d      }|j	                          |j	                          g }|j                  d      j                  |j                         |j	                          |j	                          | j                  |j                  d       |j                  j                  d       | j                  |j                  dd       |j	                          |j	                          | j                  |d   dd	       y )
Nr  r$  	doItLaterzDeferred method run too early.rI   zDeferred method run too late.r      zIncorrect result.)r   r  r  r  rL   r   r_  r  rY  r  r$  r   rX  )r%   r   r0  rL   r$  r  resultss          r'   
test_deferzBrokerTests.test_defer  s    -48
1d	#q!OOC 					[!--gnn=				 @A	Q#BC				Q(;<r)   c                 v   t        |       \  }}}t               }|j                  d|       |j                  d      }|j	                  d      j                  | j                  | j                         |j                          |j                          | j                  j                  }| j                  ||j                         | `	t        j                  dk\  rt        j                           |j                          |j                          |j                          | j#                  ||j                         y )Nr  r   r     )r   r   r  r  r   r#  r  r  rL   r  luidassertInlocalObjectssys
hexversiongccollectassertNotIn)r%   r   r0  rL   r   r  rluids          r'   test_refcountzBrokerTests.test_refcount  s    -48
1dn	%%ooe${#00!3!3	

 					 !!&&eQ^^,>>Y&JJL						/r)   c                 *   t        |       \  }}}t               }t               }|j                  }|j	                  d|       |j	                  d|       |j                  d      }|j                  d      }g }	|j                  d      j                  |	j                        j                  |	j                         |j                  d      j                  |	j                        j                  |	j                         g }
|j                  d      j                  |
j                         |j                  d      j                  |
j                         |j                          | j                  |
d   j                  d       | j                  |
d   j                  d       | j                  |
d   j                  d       |j                          |j                          | j                  |
d   j                  d	       | j                  t!        |	      d       |	d   d   }| j#                  |j%                         j&                  |d
       | j#                  |j)                         |d       g }|j                  d|      j                  |j                         |j                          | j+                  |d          | j                  |j-                  d      |j-                  d             |j.                  }|
d   j.                  }| j1                  ||j2                  d       ~	~|j                          ~
~|j                          t4        j6                  dk\  rt9        j:                          |j                          | j=                  ||j2                  d       | j=                  ||j>                  d       | j=                  ||j2                  d       | j=                  ||j>                  d       | jA                  |jB                  d       y )Nr  r  xxxgetCacher   rR   r   r   r   zpotential refcounting issuez!other potential refcounting issueputCachezremote cache doesn't have itr  zServer still had it after GCzClient still had it after GCz!Server still had complex after GCz!Client still had complex after GCzobserver was not removed)"r   r  r   r   r  r  r   r_  r  
addErrbackrN   rX  r   r   r   r   r  assertIdenticalr   __self__r   r  r  r  r  remotelyCachedObjectsr  r  r  r  r  locallyCachedObjectsassertIsNoner   )r%   r   r0  rL   r  obj2vcco2o3collcomplexcpcol2r  baroqueLuids                  r'   
test_cachezBrokerTests.test_cache4  s!   -48
1dm%'ff	%%	%&__U#__U#
j!--dkk:EEdkkR
j!--dkk:EEdkkR
j!--gnn=
j!--gnn=

q)q)+

+TA&!WQZNN%%r+H	
 	R\\^R1TU
j"%11$++>

Q 4booj6QR wwajoodA335ST



 >>Y&JJL

q668VWq557UV002U	
 	//1T	
 	#,,(BCr)   c                    	 t        j                  d       t        |       \  }}}t	               }|j                  d|       |j                  d      }g }|j                  d      j                  |j                  | j                         |j                          |j                         }| j                  |j                  d       | j                  |j                  d       | j                  |j                   d       | j                  |j"                  d       t        |       \  }}}|j                  d|       |j                  d      }|j                  d      j                  |j                  | j                         |j                          |j                         }| j                  |j"                  d       y # t        $ r Y w xY w)Nr  r  r   getPubrR   r   )r  r  r  r   GetPublisherr  r  r   r#  r  r  rN   r   rX  activateCalledisActivatedyayIGotPublished_wasCleanWhenLoaded)r%   r   r0  rL   r   r  accumr  s           r'   test_publishablezBrokerTests.test_publishablev  sl   	II-. .48
1dn	%%ooe$x --ellD<N<NO

iik++Q/!,--q100!4-48
1d	%%ooe$x --ellD<N<NO

iik00!4/  		s   G   	GGc                 &    |j                   | _        y r0   )r   r  )r%   vals     r'   gotCopyzBrokerTests.gotCopy  s    66r)   c                    t        |       \  }}}d}t               }|j                  d|       |j                  d      }|j	                  d|      j                  | j                  | j                         |j                          |j                          |j                          | j                  | j                  |d| j                          y )Nr  c   r   
getFactoryz!ID not correct on factory object )r   r   r  r  r   r#  r  r  rL   rX  r  )r%   r   r0  rL   IDr  r   s          r'   test_factoryCopyzBrokerTests.test_factoryCopy  s    -48
1dl	%%OOE"	\2&33DLL$BUBUV						/0@0@/AB	
r)   )r*   r+   r,   r  rS  r  r  r  r  r  r  r  r  r  r  r  r  r  r
  r  r  r-   r)   r'   r  r    sb    KD"?K0(#P 
88(=$08@DD58"
r)   r  s  helloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldc                      | a |ay r0   )callbackArgscallbackKeyword)argsrC  s     r'   finishedCallbackr    s    LOr)   c                       e Zd Zd Zd Zy)	Pagerizerc                 0    |||c| _         | _        | _        y r0   )r   r  rC  )r%   r   r  rC  s       r'   rG   zPagerizer.__init__  s    ,4dB)ty$'r)   c                     t        j                  |t        d| j                  g| j                  i | j
                   d x| _        | _        y )Nd   )r   StringPager	bigStringr   r  rC  r%   	collectors     r'   remote_getPageszPagerizer.remote_getPages  sE    y#t}}	
7;yy	
DHGG	
 #"	DGr)   N)r*   r+   r,   rG   r!  r-   r)   r'   r  r    s    ?#r)   r  c                       e Zd ZdZd Zd Zy)FilePagerizerNc                 >    || _         |||c| _        | _        | _        y r0   )filenamer   r  rC  )r%   r%  r   r  rC  s        r'   rG   zFilePagerizer.__init__  s     ,4dB)ty$'r)   c                     t        j                  |t        | j                  d      | j                  g| j
                  i | j                  | _        d x| _        | _        y )Nrb)r   	FilePageropenr%  r   r  rC  pagerr  s     r'   r!  zFilePagerizer.remote_getPages  sP    ^^tDMM40$--
BF))
OSww

 #"	DGr)   )r*   r+   r,   r*  rG   r!  r-   r)   r'   r#  r#    s    E?#r)   r#  c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	PagingTestsz0
    Test pb objects sending data by pages.
    c                     | j                         | _        t        | j                  d      5 }|j                  t               ddd       y# 1 sw Y   yxY w)z?
        Create a file used to test L{util.FilePager}.
        wbN)mktempr%  r)  writer  )r%   fs     r'   rQ  zPagingTests.setUp  sB     $--& 	!GGI	 	 	s   AAc                    t        |       \  }}}|j                  dt        t        dd             |j	                  d      }g }t        j                  |d      j                  |j                         |s|j                          |s| j                  dj                  |d         t        d	       | j                  t        d
d       | j                  t        ddid       y)zg
        Test L{util.StringPager}, passing a callback to fire when all pages
        are sent.
        r  r   hellor  r   getPagesr)   r   'Pages received not equal to pages sent!)r3  Completed callback not invokedr   N)r   r  r  r  r  r   getAllPagesr_  r  rL   rX  joinr  r  r  r%   r   r0  rL   r   r  s         r'   test_pagingWithCallbackz#PagingTests.test_pagingWithCallback  s    
 .48
1d	%+;WB!OPOOE"J'33AHH=IIK HHQqTNI'P	
 	z3STgr],L	
r)   c                 Z   t        |       \  }}}|j                  dt        d             |j                  d      }g }t	        j
                  |d      j                  |j                         |s|j                          |s| j                  dj                  |d         t        d       y)z>
        Test L{util.StringPager} without a callback.
        r  r   Nr5  r)   r   r6  )r   r  r  r  r   r8  r_  r  rL   rX  r9  r  r:  s         r'   test_pagingWithoutCallbackz&PagingTests.test_pagingWithoutCallback  s     .48
1d	%41OOE"J'33AHH=IIK HHQqTNI'P	
r)   c                    | j                         }t        |d      j                          t        |       \  }}}t	        |d      }|j                  d|       |j                  d      }g }t        j                  |d      j                  |j                         d}|s"|dkD  r|j                          |dz  }|s|dkD  r|s| j                  d	       | j                  d
j                  |d         d
d       y)z@
        Test L{util.FilePager}, sending an empty file.
        wr  Nr  r5  r  r   rR   zgetAllPages timed outr)   r6  )r/  r)  closer   r#  r  r  r   r8  r_  r  rL   r  rX  r9  )	r%   filenameEmptyr   r0  rL   	pagerizerr   r  ttls	            r'   test_emptyFilePagingz PagingTests.test_emptyFilePaging  s     ]C &&(-48
1d!-6		%+OOE"J'33AHH=aIIK1HC a II-.!A$.WXr)   c                 .   t        |       \  }}}t        | j                  t        dd      }|j	                  d|       |j                  d      }g }t        j                  |d      j                  |j                         |s|j                          |s| j                  dj                  |d         t        d	       | j                  t        d
d       | j                  t        ddid       | j                  |j                   j"                  g        y)z
        Test L{util.FilePager}, passing a callback to fire when all pages
        are sent, and verify that the pager doesn't keep chunks in memory.
        r  frodo	   r4  r  r5  r)   r   r6  )rF  r7  r   N)r   r#  r%  r  r  r  r   r8  r_  r  rL   rX  r9  r  r  r  r*  chunksr%   r   r0  rL   rB  r   r  s          r'   test_filePagingWithCallbackz'PagingTests.test_filePagingWithCallback  s    
 .48
1d!$--1A7RST		%+OOE"J'33AHH=IIK HHQqTNI'P	
 	z3STgq\+K	
 	//4r)   c                    t        |       \  }}}t        | j                  d      }|j                  d|       |j	                  d      }g }t        j                  |d      j                  |j                         |s|j                          |s| j                  dj                  |d         t        d       | j                  |j                  j                  g        y)z<
        Test L{util.FilePager} without a callback.
        r  Nr  r5  r)   r   r6  )r   r#  r%  r  r  r   r8  r_  r  rL   rX  r9  r  r*  rH  rI  s          r'   test_filePagingWithoutCallbackz*PagingTests.test_filePagingWithoutCallback%  s     .48
1d!$--6		%+OOE"J'33AHH=IIK HHQqTNI'P	
 	//4r)   N)
r*   r+   r,   r3   rQ  r;  r=  rD  rJ  rL  r-   r)   r'   r,  r,    s&    
(
Y(5,5r)   r,  c                       e Zd Zd Zy)DumbPublishablec                 
    ddiS )Nr  rR   r-   r1   s    r'   getStateToPublishz!DumbPublishable.getStateToPublish8  s    "A&&r)   N)r*   r+   r,   rP  r-   r)   r'   rN  rN  7  s    'r)   rN  c                       e Zd Zd Zy)DumbPubc                     d| _         y r   )r  r1   s    r'   	activatedzDumbPub.activated=  s
    r)   N)r*   r+   r,   rT  r-   r)   r'   rR  rR  <  s     r)   rR  c                       e Zd Zd Zd Zy)r  c                 $    t        d      | _        y )NTESTING)rN  pubr1   s    r'   rG   zGetPublisher.__init__B  s    "9-r)   c                     | j                   S r0   )rX  r1   s    r'   remote_getPubzGetPublisher.remote_getPubE  s    xxr)   N)r*   r+   r,   rG   rZ  r-   r)   r'   r  r  A  s    .r)   r  c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)DisconnectionTestsz'
    Test disconnection callbacks.
    c                     t        d|       )NzI shouldn't have been called: r   )r%   r  s     r'   r  zDisconnectionTests.errorQ  s    ;D6BCCr)   c                     d| _         y)z.
        Called on broker disconnect.
        rR   N)gotCallbackr1   s    r'   gotDisconnectedz"DisconnectionTests.gotDisconnectedT  s     r)   c                 J    | j                  || j                         d| _        y)z7
        Called on RemoteReference disconnect.
        rR   N)rX  remoteObjectobjectCallbackr%   os     r'   objectDisconnectedz%DisconnectionTests.objectDisconnectedZ  s"     	D--.r)   c                 Z   t        |       \  }}}|j                          |j                  dt                      |j	                  d      }g }|j                  dt                     j                  |j                         |j                          | j                  t        |      d       y )Nr  rf  
setBadCopyrR   )r   rL   r  
BadCopySetr  r   BadCopyabler  r  rN   rX  r  )r%   r   r0  rL   gr  s         r'   test_badSerializationz(DisconnectionTests.test_badSerializationa  s|    -48
1d			#z|,OOC 	\;=1<<QXXF

Q#r)   c                    t        |       \  }}}|j                          |j                  dt                      |j	                  d      }|j                          |j                          |j                          |j                  | j                         | j                  | j                  |j                         |j                  | j                         | j                  | j                  |j                         |j                  | j                         | j                  |j                  |j                         | j                  | j                  |j                         |j                  | j                         | j                  |j                  |j                         | j                  | j                  |j                         |j                  | j                         |j                  | j                         || _        |j!                  t#        j$                  t&        j(                               | j+                  | j,                         | j+                  | j.                         y )Nr  rf  )r   rL   r  r   r  notifyOnDisconnectr  r  disconnectsdontNotifyOnDisconnectr  _disconnecteddisconnectCallbacksra  rg  rc  r`   r   ra   r   rb   r  r`  rd  )r%   r   r0  rL   rs        r'   test_disconnectionz%DisconnectionTests.test_disconnectionk  s   -48
1d			#|~. OOC 						 	
TZZ(djj!--0	  ,Q]]3	TZZ(aooq}}5djj!"7"78	  ,!--8Q%:%:; 	
T112	T445 	
)=)=>?(()++,r)   N)	r*   r+   r,   r3   r  ra  rg  rm  ru  r-   r)   r'   r\  r\  L  s!    D $!-r)   r\  c                       e Zd Zy)FreakOutNr   r-   r)   r'   rw  rw    r   r)   rw  c                       e Zd Zd Zy)rk  c                     t               r0   )rw  )r%   ps     r'   getStateToCopyForzBadCopyable.getStateToCopyFor  s
    jr)   N)r*   r+   r,   r{  r-   r)   r'   rk  rk    s    r)   rk  c                       e Zd Zd Zy)rj  c                      y r0   r-   )r%   bcs     r'   remote_setBadCopyzBadCopySet.remote_setBadCopy  s    r)   N)r*   r+   r,   r  r-   r)   r'   rj  rj    s    r)   rj  c                   $    e Zd ZdZd ZddZd Zy)LocalRemoteTestr   c                     |dz   S r   r-   )r%   r   s     r'   	sync_add1zLocalRemoteTest.sync_add1      1ur)   c                     ||z   S r0   r-   )r%   r   r   s      r'   	async_addzLocalRemoteTest.async_add  r  r)   c                     t               r0   r^  r1   s    r'   
async_failzLocalRemoteTest.async_fail  s
    nr)   N)r   rR   )r*   r+   r,   reportAllTracebacksr  r  r  r-   r)   r'   r  r    s    r)   r  c                   6    e Zd ZdZdxZZd Zd Zd Zd Z	d Z
y)	MyPerspectivez
    @ivar loggedIn: set to C{True} when the avatar is logged in.
    @type loggedIn: C{bool}

    @ivar loggedOut: set to C{True} when the avatar is logged out.
    @type loggedOut: C{bool}
    Fc                     || _         y r0   r;   r%   r;   s     r'   rG   zMyPerspective.__init__  s	     r)   c                     | j                   S )zT
        Return the avatar identifier which was used to access this avatar.
        r  r1   s    r'   perspective_getAvatarIdz%MyPerspective.perspective_getAvatarId  s     }}r)   c                     t               S r0   )MyViewr1   s    r'   perspective_getViewPointz&MyPerspective.perspective_getViewPoint  s	    xr)   c                     ||z   S )z
        Add the given objects and return the result.  This is a method
        unavailable on L{Echoer}, so it can only be invoked by authenticated
        users who received their avatar from L{TestRealm}.
        r-   r  s      r'   perspective_addzMyPerspective.perspective_add  s     1ur)   c                     d| _         y NT	loggedOutr1   s    r'   logoutzMyPerspective.logout  	    r)   N)r*   r+   r,   r3   loggedInr  rG   r  r  r  r  r-   r)   r'   r  r    s,     ! Hy!r)   r  c                       e Zd ZdZeZdZd Zy)	TestRealma  
    A realm which repeatedly gives out a single instance of L{MyPerspective}
    for non-anonymous logins and which gives out a new instance of L{Echoer}
    for each anonymous login.

    @ivar lastPerspective: The L{MyPerspective} most recently created and
        returned from C{requestAvatar}.

    @ivar perspectiveFactory: A one-argument callable which will be used to
        create avatars to be returned from C{requestAvatar}.
    Nc                 F   |t         j                  k(  sJ |dk(  sJ |t        j                  u rt         j                  t	               d fS | j                  |      | _        d| j                  _        t         j                  | j                  | j                  j                  fS )z
        Verify that the mind and interface supplied have the expected values
        (this should really be done somewhere else, like inside a test method)
        and return an avatar appropriate for the given identifier.
        BRAINS!c                       y r0   r-   r-   r)   r'   r8   z)TestRealm.requestAvatar.<locals>.<lambda>  r9   r)   T)	r   r:   r   	ANONYMOUSr=  perspectiveFactorylastPerspectiver  r  )r%   r;   r<   	interfaces       r'   r?   zTestRealm.requestAvatar  s     BOO+++y   x)))??FHl::#'#:#:8#DD ,0D  )OOT%9%94;O;O;V;VWWr)   )r*   r+   r,   r3   r  r  r  r?   r-   r)   r'   r  r    s    
 'OXr)   r  c                       e Zd Zd Zy)r  c                 "    t        |t              S r0   )r"   r  r$   s     r'   
view_checkzMyView.view_check  s    $..r)   N)r*   r+   r,   r  r-   r)   r'   r  r    s    /r)   r  c                       e Zd ZdZd Zd Zy)
LeakyRealmz]
    A realm which hangs onto a reference to the mind object in its logout
    function.
    c                     || _         y)z
        Create a L{LeakyRealm}.

        @param mindEater: a callable that will be called with the C{mind}
        object when it is available
        N)
_mindEater)r%   	mindEaters     r'   rG   zLeakyRealm.__init__  s     $r)   c                 x    | j                         | j                  |      t        j                  fdfS )Nc                  (     j                         fS r0   )r  )r<   persps   r'   r8   z*LeakyRealm.requestAvatar.<locals>.<lambda>  s    u||~0F r)   )r  r  r   r:   )r%   r;   r<   r  r  s     ` @r'   r?   zLeakyRealm.requestAvatar  s1    ''1(FGGr)   N)r*   r+   r,   r3   rG   r?   r-   r)   r'   r  r    s    
$Hr)   r  c                       e Zd ZdZd Zy)NewCredLeakTestsz/
    Tests to try to trigger memory leaks.
    c                    	 d _          fd}t         t        |            \  }	g |j                  d      }|j	                  dd      }d }|j                  |       	fd}|j                  |       j                           j                  d	g       t        j                           j                   j                                y)
z
        The server does not leak a reference when the client disconnects
        suddenly, even if the cred logout function forms a reference cycle with
        the perspective.
        Nc                 :    t        j                  |       _        y r0   )weakrefrP  mindRef)r<   r%   s    r'   
setMindRefz4NewCredLeakTests.test_logoutLeak.<locals>.setMindRef  s    ";;t,DLr)   )rp   r   rv  loginr   c                 n    | \  }}t               }|j                  dt        j                  |d      |      S )Nrespondr   )r   r   r   r  )r   	challenge
challengerr<   s       r'   
cbResponsez4NewCredLeakTests.test_logoutLeak.<locals>.cbResponse$  s8    $%!Iz>D((2::i:D r)   c                     j                          j                  d       j                  t        j                  t        d                   y )NrR   boom)rP   r  r`   r   ra   r   )_connectionBrokenrL   rt   s    r'   r`   z8NewCredLeakTests.test_logoutLeak.<locals>.connectionLost-  s6    IIK##A&''V8L(MNr)   rR   )r  r   r  r  r   r_  rN   rX  r  r  r  )
r%   r  rs   rv  r$  r  r`   r  rL   rt   s
   `      @@@r'   test_logoutLeakz NewCredLeakTests.test_logoutLeak  s     	- ,DZ
3,
(lD
 ))&1OOGX.	 	
j!	O
 	
n% 	

)A3/
 	

$,,.)r)   N)r*   r+   r,   r3   r  r-   r)   r'   r  r  	  s    .*r)   r  c                       e Zd ZdZd ZddZ ej                  ej                        fdZ
d Zd Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)NewCredTestsz=
    Tests related to the L{twisted.cred} support in PB.
    c                     t               | _        t        j                  | j                        | _        t	        | j                        | _        t        j                         | _        y)z
        Create a portal with no checkers and wrap it around a simple test
        realm.  Set up a PB server on a TCP port which serves perspectives
        using that portal.
        N)	r  r   r
   r   rs  rq   r   r   ri   r1   s    r'   rQ  zNewCredTests.setUpD  sC     [
mmDJJ/:4;;G//1r)   Nc                 P   t        | | j                  | j                        \  | _        | _        | _        t               | _        t        t        j                  ddd      | j                        | _        | j                  j                         j                  | j                         y)a  
        Connect a client obtained from C{clientFactory} and a server
        obtained from the current server factory via an L{IOPump},
        then assign them to the appropriate instance variables

        @ivar clientFactory: the broker client factory
        @ivar clientFactory: L{pb.PBClientFactory} instance

        @ivar client: the client broker
        @type client: L{pb.Broker}

        @ivar server: the server broker
        @type server: L{pb.Broker}

        @ivar pump: the IOPump connecting the client and server
        @type pump: L{IOPump}

        @ivar connector: A connector whose connect method recreates
            the above instance variables
        @type connector: L{twisted.internet.base.IConnector}
        TCPr]   i  N)rw   ri   rq   rC   rD   rL   ry   connectorStater   r   IPv4Addressrf   r   r_  establishClientAndServer)r%   _ignoreds     r'   r  z%NewCredTests.establishClientAndServerO  s    , /E$$$d&8&8/
+T[$) >?3{D94;N;N
 	++-99$:W:WXr)   c                     | j                  | j                  j                  j                         | j                  j	                  |       | j
                  j                  | j                  |       y)a  
        Asserts that the client broker's transport was closed and then
        mimics the event loop by calling the broker's connectionLost
        callback with C{reason}, followed by C{self.clientFactory}'s
        C{clientConnectionLost}

        @param reason: (optional) the reason to pass to the client
            broker's connectionLost callback
        @type reason: L{Failure}
        N)r  rC   rV   r  r`   ri   rh   rf   )r%   rg   s     r'   completeClientLostConnectionz)NewCredTests.completeClientLostConnectiono  sN     	--445 	""6*//Gr)   c                       j                           j                  j                         } fd} fd}|j                  |       |j                  |       |S )z
        Assert that L{PBClientFactory.getRootObject}'s Deferred fires with
        a L{RemoteReference}, and that disconnecting it runs its
        disconnection callbacks.
        c                 H    j                  | t        j                         | S r0   )rW  r   RemoteReference)rootObjr%   s    r'   gotRootObjectz6NewCredTests.test_getRootObject.<locals>.gotRootObject  s    !!'2+=+=>Nr)   c                     t               }| j                  |j                         j                  j	                          j                          |S r0   )r   ro  r   ri   
disconnectr  )r  disconnectedDeferredr%   s     r'   r  z3NewCredTests.test_getRootObject.<locals>.disconnect  sD    #+: &&';'D'DE))+--/''r)   r  ri   rO  r_  )r%   rootObjDeferredr  r  s   `   r'   test_getRootObjectzNewCredTests.test_getRootObject  sS     	%%',,::<		( 	##M2##J/r)   c                       j                           j                  j                         } fd}|j                  |      S )z
        Test that when a connection is lost, calling a method on a
        RemoteReference obtained from it raises L{DeadReferenceError}.
        c                      t               } j                  |j                          fd}|j                  |       j                  j                          j                          |S )Nc                 \    j                  t        j                  j                  d       y )Nmethod)assertRaisesr   DeadReferenceErrorr   )ignr  r%   s    r'   lostConnectionzSNewCredTests.test_deadReferenceError.<locals>.gotRootObject.<locals>.lostConnection  s!    !!""7"79K9KXVr)   )r   ro  r   r_  ri   r  r  )r  r  r  r%   s   `  r'   r  z;NewCredTests.test_deadReferenceError.<locals>.gotRootObject  sZ    #+: &&';'D'DEW !,,^<))+--/''r)   r  )r%   r  r  s   `  r'   test_deadReferenceErrorz$NewCredTests.test_deadReferenceError  s=    
 	%%',,::<	( **=99r)   c                       G d dt         j                        } |        _         j                           j                  j	                         } fd}|j                  |      S )z
        Test that if the L{reconnecting} flag is passed with a True value then
        a remote call made from a disconnection notification callback gets a
        result successfully.
        c                       e Zd ZdZd Zy)=NewCredTests.test_clientConnectionLost.<locals>.ReconnectOnceFc                     | j                    }d| _         t        j                  j                  | |||      }|r|j	                          |S r  )reconnectedAlreadyr   r   rh   r   )r%   rf   rg   reconnectingr  s        r'   rh   zRNewCredTests.test_clientConnectionLost.<locals>.ReconnectOnce.clientConnectionLost  sM    #'#:#::*.'++@@)V\  %%'r)   N)r*   r+   r,   r  rh   r-   r)   r'   ReconnectOncer    s    !&r)   r  c                    j                  | t        j                         t               }| j	                  |j
                         j                  j                          j                          fd}|j                  |      S )Nc                 d    j                   j                         }fd}|j                  |      S )Nc                     j                  | t        j                         t               }| j	                  |j
                         j                  j                          j                          |S r0   )	rW  r   r  r   ro  r   ri   r  r  )anotherRootObjr$  r%   s     r'   gotAnotherRootObjectzqNewCredTests.test_clientConnectionLost.<locals>.gotRootObject.<locals>.disconnected.<locals>.gotAnotherRootObject  sU    )).":L:LM 
A"55ajjA&&113557Hr)   )ri   rO  r_  )r  r$  r  r%   s      r'   r_   zSNewCredTests.test_clientConnectionLost.<locals>.gotRootObject.<locals>.disconnected  s.    &&446 }}%9::r)   )
rW  r   r  r   ro  r   ri   r  r  r_  )r  r$  r_   r%   s      r'   r  z=NewCredTests.test_clientConnectionLost.<locals>.gotRootObject  sf    !!'2+=+=>
A&&qzz2))+--/; ==..r)   )r   r   ri   r  rO  r_  )r%   r  r  r  s   `   r'   test_clientConnectionLostz&NewCredTests.test_clientConnectionLost  sY    	B.. 	 +_%%',,::<	/0 **=99r)   c                    | j                          | j                  j                  d      }|j                  t	        j
                  t                            |j                  t        j                  t        j                               y)z
        Test that if a Broker loses its connection without receiving any bytes,
        it doesn't raise any exceptions or log any errors.
        )r]   i90  N)r  rq   rk   rm   r   rn   rl   r`   r   ra   r   rb   )r%   serverProtos     r'   test_immediateClosez NewCredTests.test_immediateClose  s_    
 	%%'((667KL""8#7#7
#CD""7??43G3G#HIr)   c                     t        j                         }|j                  t        j                  dd            }|j                  dt        j                  t        d                   | j                  |t              S )z
        L{PBClientFactory.login} returns a L{Deferred} which is errbacked
        with the L{ConnectionRefusedError} if the underlying connection is
        refused.
           foo   barNz!Test simulated refused connection)
r   r   r  r	   UsernamePasswordclientConnectionFailedr   ra   r   assertFailure)r%   ri   loginDeferreds      r'   test_loginConnectionRefusedz(NewCredTests.test_loginConnectionRefused  sp     **,%++((8
 	,,OO&'JK	
 !!-1GHHr)   c                 <     j                   j                  t        j                  d             t	        j
                  dd      }d}t                j                  j                  ||      } fd} fd}|j                  |       |j                  |        fd}|j                  |        j                           j                  j                          t        j                           j                  j                          j                  d       |S )	z
        Test that login can be performed with IUsernamePassword credentials and
        that when the connection is dropped the avatar is logged out.
           passr&      userr  c                     j                  j                  j                  j                         j	                  | t
        j                         S r0   )r  r   r  r  rW  r   r  )r   loginCompletedr%   s    r'   cbLoginz.NewCredTests.test_loginLogout.<locals>.cbLogin  s;    OODJJ66??@!!+r/A/AB!!r)   c                 Z    j                   j                          j                          y r0   )ri   r  r  ignoredr%   s    r'   cbDisconnectz3NewCredTests.test_loginLogout.<locals>.cbDisconnect  s!    ))+--/r)   c                 d    j                  j                  j                  j                         y r0   )r  r   r  r  r  s    r'   cbLogoutz/NewCredTests.test_loginLogout.<locals>.cbLogout&  s    OODJJ66@@Ar)   N)r
   registerCheckerr   r   r	   r  r   ri   r  r_  r  rL   rN   r  r  r   )r%   credsr<   r$  r   r  r  r  s   `      @r'   test_loginLogoutzNewCredTests.test_loginLogout  s    
 	##<<'J	
 ,,Wg> !$$UD1	"
	0 	
g	l#	B 	
h%%'		 	

		%r)   c                 <    t                G fddt        j                        }| j                  _         j
                  j                  t        j                  d              j                  j                  t        j                  dd      d      }fd}|j                  |        fd}|j                  |        j                           j                  j!                          t#        j$                           j                  j!                          |S )	z
        If a L{RemoteReference} to an L{IPerspective} avatar is decrefed and
        there remain no other references to the avatar on the server, the
        avatar is garbage collected and the logout method called.
        c                   "    e Zd ZdZd Z fdZy)=NewCredTests.test_logoutAfterDecref.<locals>.EventPerspectivezQ
            An avatar which fires a Deferred when it is logged out.
            c                      y r0   r-   r  s     r'   rG   zFNewCredTests.test_logoutAfterDecref.<locals>.EventPerspective.__init__C  s    r)   c                 (    j                  d        y r0   )r   )r%   r  s    r'   r  zDNewCredTests.test_logoutAfterDecref.<locals>.EventPerspective.logoutF  s    ""4(r)   N)r*   r+   r,   r3   rG   r  r  s   r'   EventPerspectiver  >  s    )r)   r  r  r   r  r  c                     S r0   r-   )avatarr  s    r'   
cbLoggedInz7NewCredTests.test_logoutAfterDecref.<locals>.cbLoggedInS  s     r)   c                 f    j                  j                  j                  j                  i        y r0   )rX  rq   rx  _localCleanupr  s    r'   cbLoggedOutz8NewCredTests.test_logoutAfterDecref.<locals>.cbLoggedOutZ  s(     T//@@NNPRSr)   )r   r   Avatarr   r  r
   r  r   r   ri   r  r	   r  r_  r  rL   rN   r  r  )r%   r  r$  r  r  r  s   `    @r'   test_logoutAfterDecrefz#NewCredTests.test_logoutAfterDecref6  s     J			)ryy 		) )9

%##<<H	
 $$((8)
	
 	
j!	T
 	
k"%%' 			 	

		r)   c                      j                   j                  t        j                  dd              j                  j                  t        j                  dd      d      } j                  j                  t        j                  dd      d      }t        ||g      }d }|j                  |        fd}|j                  |        j                           j                  j                          |S )	z
        Two different correct login attempts can be made on the same root
        object at the same time and produce two different resulting avatars.
        r  s   quux)r   bazr  r     bazc                 b    | \  }}t        |j                  d      |j                  d      g      S )NgetAvatarId)r   r   )r  firstseconds      r'   r  z5NewCredTests.test_concurrentLogin.<locals>.cbLoggedIn}  s6    $OUF !!-0&2C2CM2RS r)   c                 X    | \  }}j                  |d       j                  |d       y )Nr  r  )rX  )r   r  r  r%   s      r'   cbAvatarIdsz6NewCredTests.test_concurrentLogin.<locals>.cbAvatarIds  s,    ME6UF+VV,r)   )r
   r  r   r   ri   r  r	   r  r   r_  r  rL   rN   )r%   
firstLoginsecondLoginr$  r  r   s   `     r'   test_concurrentLoginz!NewCredTests.test_concurrentLoginl  s    
 	##<<WU	
 ''--((8)

 ((..((99
 :{34	 	
j!	-
 	
k"%%'		r)   c                      j                   j                  t        j                  d              j                  j                  t        j                  dd            } j                  j                  t        j                  dd            } j                  |t                j                  |t               t        ||g      } fd}|j                  |        j                           j                  j                          |S )zz
        Test that a login attempt with an invalid user or invalid password
        fails in the appropriate way.
        r  r  s
   nosuchuserr  s	   wrongpassc                 f    j                  t              }j                  t        |      d       y )Nr   )flushLoggedErrorsr   rX  r  ignoreerrorsr%   s     r'   cleanupz;NewCredTests.test_badUsernamePasswordLogin.<locals>.cleanup  s(    ++,=>FS[!,r)   )r
   r  r   r   ri   r  r	   r  r  r   r   r_  r  rL   rN   )r%   r!  r"  r$  r*  s   `    r'   test_badUsernamePasswordLoginz*NewCredTests.test_badUsernamePasswordLogin  s    
 	##<<'J	
 ''--((@

 ((..((,?
 	:'89;(9::{34	- 	
g%%'		r)   c                 p   | j                   j                  t        j                                | j                  j                  t        j                         d      }d }|j                  |       |j                  | j                  d       | j                          | j                  j                          |S )z
        Verify that a PB server using a portal configured with a checker which
        allows IAnonymous credentials can be logged into using IAnonymous
        credentials.
        r  c                 &    | j                  dd      S NrU  {   r  r   s    r'   r  z4NewCredTests.test_anonymousLogin.<locals>.cbLoggedIn      ))&#66r)   r/  )r
   r  r   AllowAnonymousAccessri   r  r	   	Anonymousr_  rX  r  rL   rN   )r%   r$  r  s      r'   test_anonymousLoginz NewCredTests.test_anonymousLogin  s     	##H$A$A$CD$$[%:%:%<iH	7 	
j!	d&&,%%'		r)   c                 n     j                   j                  t        j                  d              j                  j                  t        j                         d      } j                  |t                fd}|j                  |        j                           j                  j                          |S )zg
        Verify that without an anonymous checker set up, anonymous login is
        rejected.
        passr  r  c                 f    j                  t              }j                  t        |      d       y r   )r&  r   rX  r  r'  s     r'   r*  z=NewCredTests.test_anonymousLoginNotPermitted.<locals>.cleanup  s(    ++,@AFS[!,r)   )r
   r  r   r   ri   r  r	   r3  r  r   r_  r  rL   rN   )r%   r$  r*  s   `  r'   test_anonymousLoginNotPermittedz,NewCredTests.test_anonymousLoginNotPermitted  s    
 	##<<&I	
 $$[%:%:%<iH123	- 	
g%%'		r)   c                    | j                   j                  t        j                                | j                   j                  t        j                  d             | j
                  j                  t        j                         d      }d }|j                  |       |j                  | j                  d       | j                          | j                  j                          |S )z
        Like L{test_anonymousLogin} but against a portal with a checker for
        both IAnonymous and IUsernamePassword.
        r  r  r  c                 &    | j                  dd      S r.  r  r0  s    r'   r   zENewCredTests.test_anonymousLoginWithMultipleCheckers.<locals>.cbLogin  r1  r)   r/  )r
   r  r   r2  r   ri   r  r	   r3  r_  rX  r  rL   rN   r%   r$  r   s      r'   'test_anonymousLoginWithMultipleCheckersz4NewCredTests.test_anonymousLoginWithMultipleCheckers  s    
 	##H$A$A$CD##<<'J	
 $$[%:%:%<iH	7 	
g	d&&,%%'		r)   c                    | j                   j                  t        j                                | j                   j                  t        j                  d             | j
                  j                  t        j                  dd      d      }d }|j                  |       |j                  | j                  d       | j                          | j                  j                          |S )z
        Like L{test_anonymousLoginWithMultipleCheckers} but check that
        username/password authentication works.
        r  r  r  r  c                 (    | j                  ddd      S )Naddr     r  r0  s    r'   r   zINewCredTests.test_authenticatedLoginWithMultipleCheckers.<locals>.cbLogin  s    ))%b99r)   r/  )r
   r  r   r2  r   ri   r  r	   r  r_  rX  r  rL   rN   r;  s      r'   +test_authenticatedLoginWithMultipleCheckersz8NewCredTests.test_authenticatedLoginWithMultipleCheckers  s    
 	##H$A$A$CD##<<'J	
 $$((':I
	: 	
g	d&&,%%'		r)   c                    | j                   j                  t        j                  d             | j                  j                  t        j                  dd      d      }d }|j                  |       d }|j                  |       |j                  | j                         | j                          | j                  j                          |S )zb
        Verify that a viewpoint can be retrieved after authenticating with
        cred.
        r  r  r  r  c                 $    | j                  d      S )NgetViewPointr  r0  s    r'   r   z'NewCredTests.test_view.<locals>.cbLogin  s    )).99r)   c                 $    | j                  d      S )Nr   r  )	viewpoints    r'   cbViewz&NewCredTests.test_view.<locals>.cbView  s    ''00r)   )r
   r  r   r   ri   r  r	   r  r_  r  r  rL   rN   )r%   r$  r   rG  s       r'   	test_viewzNewCredTests.test_view
  s    
 	##<<'J	
 $$((':I
	: 	
g	1 	
f	doo&%%'		r)   r0   )r*   r+   r,   r3   rQ  r  r   ra   r   rb   r  r  r  r  r  r  r	  r  r#  r+  r4  r8  r<  rA  rH  r-   r)   r'   r  r  ?  s{    	2YB %W__T%9%9:H(6:01:fJI$/b4l#J>(,.2r)   r  c                       e Zd Zd Zd Zd Zy)NonSubclassingPerspectivec                      y r0   r-   r  s     r'   rG   z"NonSubclassingPerspective.__init__*  r  r)   c                 r    |j                  ||       }|j                  ||       }|j                  |||f      S r0   )unserialize	serialize)r%   rc   messager  kwargss        r'   perspectiveMessageReceivedz4NonSubclassingPerspective.perspectiveMessageReceived.  s>    !!$-##FD1$ 788r)   c                     d| _         y r  r  r1   s    r'   r  z NonSubclassingPerspective.logout4  r  r)   N)r*   r+   r,   rG   rQ  r  r-   r)   r'   rJ  rJ  (  s    9r)   rJ  c                       e Zd ZdZd Zd Zy)NSPTestsz
    Tests for authentication against a realm where the L{IPerspective}
    implementation is not a subclass of L{Avatar}.
    c                    t               | _        t        | j                  _        t	        j
                  | j                        | _        t        j                         | _        | j                  j                  dd       | j                  j                  | j                         t        t        j                  | j                              | _        t        j                   d| j                  d      | _        | j%                  | j"                  j&                         | j"                  j)                         j"                  | _        y )Nr  r  r   r]   )r  )r  r   rJ  r  r
   r   r   r   r   addUserr  r   r   r   r  r   r  r  ro   r  r  portnor1   s    r'   rQ  zNSPTests.setUp>  s    [
(A

%mmDJJ/GGIWg.##DLL1&r'9'9$++'FG%%aM			//0ii'')..r)   c                 T    t        j                         j                  t        j                  dd      d      }t        j                  d j                         |j                  d        |j                   j                  dddd	if        fd
}|j                  |       |S )z
        An L{IPerspective} implementation which does not subclass
        L{Avatar} can expose remote methods for the client to call.
        r  r  r  r]   c                 *    | j                  ddd      S )NANYTHINGherer  )r  r  )rz  s    r'   r8   z#NSPTests.test_NSP.<locals>.<lambda>R  s    ZU K r)   rZ  )r[  r  r  c                     j                          j                  j                  D ]  }|j                  j	                           y r0   )r  r  	protocolsrV   r  )r  rz  r  r%   s     r'   r*  z"NSPTests.test_NSP.<locals>.cleanupU  s8     \\++ -**,-r)   )
r   r   r  r	   r  r   r  rW  r_  rX  )r%   r$  r*  r  s   `  @r'   test_NSPzNSPTests.test_NSPJ  s    
 $$&MM+66wH)T;W=	KL	d&&Y(OP	-
 	
gr)   N)r*   r+   r,   r3   rQ  r^  r-   r)   r'   rT  rT  8  s    

/r)   rT  c                       e Zd ZdZd Zd Zy)
IForwardedzA
    Interface used for testing L{util.LocalAsyncForwarder}.
    c                       y)z,
        Simple synchronous method.
        Nr-   r-   r)   r'   	forwardMezIForwarded.forwardMec  r9   r)   c                       y)z-
        Simple asynchronous method.
        Nr-   r-   r)   r'   forwardDeferredzIForwarded.forwardDeferredh  r9   r)   N)r*   r+   r,   r3   rb  rd  r-   r)   r'   r`  r`  ^  s    
r)   r`  c                   *    e Zd ZdZdZdZd Zd Zd Zy)	Forwardedz
    Test implementation of L{IForwarded}.

    @ivar forwarded: set if C{forwardMe} is called.
    @type forwarded: C{bool}
    @ivar unforwarded: set if C{dontForwardMe} is called.
    @type unforwarded: C{bool}
    Fc                     d| _         y)z6
        Set a local flag to test afterwards.
        TN)	forwardedr1   s    r'   rb  zForwarded.forwardMe|  s     r)   c                     d| _         y)zv
        Set a local flag to test afterwards. This should not be called as it's
        not in the interface.
        TN)unforwardedr1   s    r'   dontForwardMezForwarded.dontForwardMe  s    
  r)   c                     t        d      S )z0
        Asynchronously return C{True}.
        T)r   r1   s    r'   rd  zForwarded.forwardDeferred  s     t}r)   N)	r*   r+   r,   r3   rh  rj  rb  rk  rd  r-   r)   r'   rf  rf  n  s"     IK r)   rf  c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)SpreadUtilTestsz+
    Tests for L{twisted.spread.util}.
    c                 \    t               }| j                  |j                  dd      d       y)zk
        Call a synchronous method of a L{util.LocalAsRemote} object and check
        the result.
        add1r   r   N)r  rX  r   re  s     r'   	test_synczSpreadUtilTests.test_sync  s'    
 fa0!4r)   c                     t               }t               }|j                  ddd      }| j                  |t               |j	                  | j
                  d       |S )zm
        Call an asynchronous method of a L{util.LocalAsRemote} object and check
        the result.
        r?  r   r   )r   r  )r  r   rW  r   r_  rX  )r%   rf  r$  s      r'   
test_asynczSpreadUtilTests.test_async  sQ    
 LLQL'a*	d&&*r)   c                 r     t               }|j                  d      } fd}|j                   fd|       |S )zG
        Test an asynchronous failure on a remote method call.
        r  c                 p    j                  | t        j                         | j                  t               y r0   )rW  r   ra   trapr   )r1  r%   s    r'   ebz*SpreadUtilTests.test_asyncFail.<locals>.eb  s#    !!!W__5FF< r)   c                 &    j                  d      S )Nzsupposed to failr  )r[  r%   s    r'   r8   z0SpreadUtilTests.test_asyncFail.<locals>.<lambda>  s    499-?#@ r)   )r  r   r#  )r%   rf  r$  rw  s   `   r'   test_asyncFailzSpreadUtilTests.test_asyncFail  s6     LL 	! 	
@"Er)   c                 j    t               }|j                  d      }| j                   |d      d       y)zM
        Test the C{remoteMethod} facility of L{util.LocalAsRemote}.
        rp  r   r   N)r  r  rX  )r%   rf  ms      r'   test_remoteMethodz!SpreadUtilTests.test_remoteMethod  s.     NN6"1q!r)   c                    t               }t        j                  |t              }|j	                  d       | j                  |j                         |j	                  d       | j                  |j                         |j	                  d      }g }|j                  |j                         | j                  |d   d       y)ze
        Test a call to L{util.LocalAsyncForwarder} using L{Forwarded} local
        object.
        rb  rk  rd  r   rR   N)rf  r   LocalAsyncForwarderr`  r   r  rh  rY  rj  r_  r  rX  )r%   r1  lfrrr  s        r'   test_localAsyncForwarderz(SpreadUtilTests.test_localAsyncForwarder  s    
 K%%a4
k"$
o&']],-
qxx 1q!r)   N)	r*   r+   r,   r3   rq  rs  ry  r|  r  r-   r)   r'   rn  rn    s     5
""r)   rn  c                   (    e Zd ZdZd Zd Zd Zd Zy)PBWithSecurityOptionsTestsz&
    Test security customization.
    c                     t        j                         }|j                  d      }| j                  |j                  t
        j                         y)zl
        By default, client broker should use C{jelly.globalSecurity} as
        security settings.
        N)r   r   rk   r  securityr   globalSecurityr%   r  rc   s      r'   !test_clientDefaultSecurityOptionsz<PBWithSecurityOptionsTests.test_clientDefaultSecurityOptions  s:    
 $$&&&t,foou';';<r)   c                     t        j                  t                     }|j                  d      }| j	                  |j
                  t        j                         y)zl
        By default, server broker should use C{jelly.globalSecurity} as
        security settings.
        N)r   r   r=  rk   r  r  r   r  r  s      r'   !test_serverDefaultSecurityOptionsz<PBWithSecurityOptionsTests.test_serverDefaultSecurityOptions  s>    
 $$VX.&&t,foou';';<r)   c                     t        j                         }t        j                  |      }|j	                  d      }| j                  |j                  |       y)zs
        Check that the security settings are passed from the client factory to
        the broker object.
        r  N)r   SecurityOptionsr   r   rk   r  r  r%   r  r  rc   s       r'    test_clientSecurityCustomizationz;PBWithSecurityOptionsTests.test_clientSecurityCustomization  sD    
 ((*$$h7&&t,foox0r)   c                     t        j                         }t        j                  t	               |      }|j                  d      }| j                  |j                  |       y)zs
        Check that the security settings are passed from the server factory to
        the broker object.
        r  N)r   r  r   r   r=  rk   r  r  r  s       r'    test_serverSecurityCustomizationz;PBWithSecurityOptionsTests.test_serverSecurityCustomization  sH    
 ((*$$VXA&&t,foox0r)   N)r*   r+   r,   r3   r  r  r  r  r-   r)   r'   r  r    s    ==11r)   r  r0   )r3   r  r  r  rK   r  collectionsr   ior   rl   typingr   zope.interfacer   r   twisted.credr   r	   r
   twisted.cred.errorr   r   twisted.internetr   r   r   r   twisted.internet.deferr   r   r   twisted.internet.errorr   twisted.internet.testingr   twisted.protocols.policiesr   twisted.pythonr   r   twisted.python.compatr   twisted.spreadr   r   r   r   twisted.trialr   Viewabler    r  r#   IRealmr5   rA   rw   ry   r   r   r  r   r   Copyabler   
RemoteCopyr   setUnjellyableForClassr   r   setUnjellyableFactoryForClassr   	Cacheabler   r   r   RemoteCacher   r   r  r
  r  r'  r.  r2  r:  Rootr=  rF  SynchronousTestCaserL  r   rs  TestCaser{  r  r  r  r  r  r  r#  r,  PublishablerN  RemotePublishedrR  r  r\  r   rw  rk  rj  LocalAsRemoter  r:   r  r  r  r  r  r  rJ  rT  r`  rf  rn  r  r-   r)   r'   <module>r     s    
 	 
    "  1 6 6 F = = C C 9 3 6 ' + 3 3 "+BKK +ryy  V]]G G G8 8v0,f4 4B   4F2## 2## 
 	bmm 	   *o 6
, 
,  !    !24E F(!! (",, R-- !2<< !*% %   68J Kr~~    +'7 8""" 5!! 5R%% ,,r ,2;; 
   , 5'BKK '   - 7bnn 
   +-> ?RWW RWW OH00 OdB$6$6 @4(++ 4nR
(## R
j 	#   ##B$$ #i5(## i5X'g)) '
 g%%  
2##    /7 3@-** @-F	y 	"++ 
!! 

d(( 
 R__ BII    FX XB/R[[ /
H H*3*x(( 3*lf8$$ fR R__  #x   #L   Z  B="h'' ="@)1!2!2 )1r)   