U
    
W[‚ù  ã                   @   sŠ  d Z ddlmZm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mZ ddlmZ ddlmZmZmZmZ dd	lmZmZmZmZ dd
lmZ ddlm Z m!Z!m"Z" ddl#m$Z$ ddl%m&Z&m'Z' ddl(m)Z)m*Z*m+Z+ ddl,m-Z-m.Z. ddl/m0Z0m1Z1m2Z2 ddl3m4Z4 G dd„ dej5ƒZ6G dd„ dej7ƒZ8ee0j9ƒG dd„ de:ƒƒZ;G dd„ dƒZ<dd„ Z=G dd„ de:ƒZ>G dd„ de4ƒZ?d…d d!„Z@G d"d#„ d#ejAƒZBG d$d%„ d%ejAƒZCG d&d'„ d'ejDƒZEG d(d)„ d)ejFƒZGe HeEeG¡ G d*d+„ d+ejDƒZId,d-„ ZJe KeIeJ¡ G d.d/„ d/ejAƒZLG d0d1„ d1ejMƒZNG d2d3„ d3ejAƒZOG d4d5„ d5ejMƒZPG d6d7„ d7ejQƒZRe HePeR¡ G d8d9„ d9ejQƒZSe HeNeS¡ G d:d;„ d;ejAƒZTG d<d=„ d=ejAƒZUG d>d?„ d?ejAƒZVG d@dA„ dAejAƒZWG dBdC„ dCejDejFe:ƒZXe HeXeX¡ G dDdE„ dEejDejFe:ƒZYe HeYeY¡ G dFdG„ dGejMejQe:ƒZZe HeZeZ¡ G dHdI„ dIej[ƒZ\G dJdK„ dKej[ƒZ]G dLdM„ dMej^ƒZ_G dNdO„ dOej`ƒZaG dPdQ„ dQejbƒZcG dRdS„ dSejbƒZddTZedafdagdUdV„ ZhG dWdX„ dXejAƒZiG dYdZ„ dZejAƒZjG d[d\„ d\ejbƒZkG d]d^„ d^ejlƒZmG d_d`„ d`ejnƒZoG dadb„ dbejAƒZpe Hemeo¡ G dcdd„ ddejbƒZqG dedf„ dferƒZsG dgdh„ dhejDƒZtG didj„ djejAƒZuG dkdl„ dlejvƒZweejxƒG dmdn„ dnej7ƒƒZyG dodp„ dpe:ƒZzG dqdr„ drej5ƒZ{G dsdt„ dtezƒZ|G dudv„ dvejbƒZ}G dwdx„ dxejbƒZ~eejxƒG dydz„ dzƒƒZG d{d|„ d|ejbƒZ€G d}d~„ d~eƒZeeƒG dd€„ d€ƒƒZ‚G dd‚„ d‚ejbƒZƒG dƒd„„ d„ejbƒZ„dS )†z
Tests for Perspective Broker module.

TODO: update protocol level tests to use new connection API, leaving
only specific tests for old API.
é    )Úabsolute_importÚdivisionN)Údeque)ÚBytesIO)ÚimplementerÚ	Interface)Úunittest)ÚpbÚutilÚpublishÚjelly)ÚprotocolÚmainÚreactorÚaddress)ÚConnectionRefusedError)ÚDeferredÚgatherResultsÚsucceed)ÚWrappingFactory)ÚfailureÚlog)Ú	iterbytesÚrangeÚ_PY3)ÚUnauthorizedLoginÚUnhandledCredentials)ÚportalÚcheckersÚcredentials)Ú_FakeConnectorc                   @   s   e Zd Zdd„ ZdS )ÚDummyc                 C   s   t |tƒrdS dS d S )Nzhello world!zgoodbye, cruel world!)Ú
isinstanceÚDummyPerspective©ÚselfÚuser© r'   ú=/usr/lib/python3/dist-packages/twisted/spread/test/test_pb.pyÚview_doNothing&   s    
zDummy.view_doNothingN)Ú__name__Ú
__module__Ú__qualname__r)   r'   r'   r'   r(   r!   %   s   r!   c                   @   s   e Zd ZdZdd„ ZdS )r#   zE
    An L{IPerspective} avatar which will be used in some tests.
    c                 C   s   t ƒ S ©N)r!   ©r%   r'   r'   r(   Úperspective_getDummyViewPoint1   s    z.DummyPerspective.perspective_getDummyViewPointN)r*   r+   r,   Ú__doc__r/   r'   r'   r'   r(   r#   -   s   r#   c                   @   s   e Zd Zdd„ ZdS )Ú
DummyRealmc                 G   s.   |D ]$}|t jkr|t|ƒdd„ f  S qd S )Nc                   S   s   d S r-   r'   r'   r'   r'   r(   Ú<lambda>;   ó    z*DummyRealm.requestAvatar.<locals>.<lambda>)r	   ÚIPerspectiver#   )r%   ÚavatarIdÚmindZ
interfacesZifacer'   r'   r(   ÚrequestAvatar8   s    
zDummyRealm.requestAvatarN)r*   r+   r,   r7   r'   r'   r'   r(   r1   6   s   r1   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚIOPumpz
    Utility to pump data between clients and servers for protocol testing.

    Perhaps this is a utility worthy of being in protocol.py?
    c                 C   s   || _ || _|| _|| _d S r-   )ÚclientÚserverÚclientIOÚserverIO)r%   r9   r:   r;   r<   r'   r'   r(   Ú__init__D   s    zIOPump.__init__c                 C   s6   d| _ t ¡ d }| j s2|  ¡ r2t ¡ |krdS qdS )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%   Ztimeoutr'   r'   r(   ÚflushK   s
    zIOPump.flushc                 C   s
   d| _ dS )zd
        Stop a running L{flush} operation, even if data remains to be
        transferred.
        TN)r?   r.   r'   r'   r(   ÚstopY   s    zIOPump.stopc                 C   s¸   | j  d¡ | j d¡ | j  ¡ }| j ¡ }| j  d¡ | j d¡ | j  ¡  | j ¡  | jj ¡  | jj ¡  t	|ƒD ]}| j 
|¡ qxt	|ƒD ]}| j 
|¡ q’|s¬|r°dS dS dS )zX
        Move data back and forth.

        Returns whether any data was moved.
        r   é   N)r;   Úseekr<   ÚreadÚtruncater9   Ú	transportZ_checkProducerr:   r   ZdataReceived)r%   ZcDataZsDataZbyter'   r'   r(   rA   a   s"    



zIOPump.pumpN)r*   r+   r,   r0   r=   rB   rC   rA   r'   r'   r'   r(   r8   >   s
   r8   c                    sš   d}ˆ   |¡}|  |¡}tƒ }tƒ }| t |¡¡ | t |¡¡ t||||ƒ}dd„ }	‡ fdd„}
|  |	|¡ |  |	|¡ |  |
¡ | ¡  |||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                 S   s   | j s|  t tj¡¡ d S r-   )ÚdisconnectedÚconnectionLostr   ÚFailurer   ÚCONNECTION_DONE)Úbrokerr'   r'   r(   ÚmaybeDisconnectš   s    z/connectServerAndClient.<locals>.maybeDisconnectc                      s   ˆ j d t tj¡d d S )N)Ú	connectorÚreason)ÚclientConnectionLostr   rL   r   rM   r'   ©ÚclientFactoryr'   r(   ÚdisconnectClientFactoryž   s    
þz7connectServerAndClient.<locals>.disconnectClientFactory)ÚbuildProtocolÚStringIOÚmakeConnectionr   ÚFileWrapperr8   Ú
addCleanuprA   )ÚtestrT   ÚserverFactoryZaddrÚclientBrokerÚserverBrokerZclientTransportZserverTransportrA   rO   rU   r'   rS   r(   ÚconnectServerAndClient|   s    

	
r_   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú_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                 C   s   t ƒ | _d S r-   )r   Únotificationsr.   r'   r'   r(   r=   ¹   s    z(_ReconnectingFakeConnectorState.__init__c                 C   s   t ƒ }| 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   ra   Ú
appendleft)r%   Znotifierr'   r'   r(   ÚnotifyOnConnect½   s    	z/_ReconnectingFakeConnectorState.notifyOnConnectc                 C   s   | j r| j  ¡  | ¡ q dS )z1
        Fire all pending notifications.
        N)ra   ÚpopÚcallbackr.   r'   r'   r(   Ú	notifyAllË   s    z)_ReconnectingFakeConnectorState.notifyAllN)r*   r+   r,   r0   r=   rc   rf   r'   r'   r'   r(   r`   °   s   r`   c                       s,   e Zd ZdZ‡ fdd„Z‡ fdd„Z‡  ZS )Ú_ReconnectingFakeConnectorzc
    A fake L{IConnector} that can fire L{Deferred}s when its
    C{connect} method is called.
    c                    s   t t| ƒ |¡ || _dS )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   r=   Ú_state)r%   r   Ústate©Ú	__class__r'   r(   r=   Ú   s    	z#_ReconnectingFakeConnector.__init__c                    s   t t| ƒ ¡  | j ¡  dS )zM
        A C{connect} implementation that calls C{reconnectCallback}
        N)rh   rg   Úconnectri   rf   r.   rk   r'   r(   rm   ç   s    z"_ReconnectingFakeConnector.connect)r*   r+   r,   r0   r=   rm   Ú__classcell__r'   r'   rk   r(   rg   Ô   s   rg   c                 C   s>   |pt ƒ }tjdd}t t ||g¡¡}t ¡ }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)Zguest)	r1   r   Ú'InMemoryUsernamePasswordDatabaseDontUser	   ÚPBServerFactoryr   ÚPortalÚPBClientFactoryr_   )r[   ÚrealmÚcheckerr\   rT   r'   r'   r(   ÚconnectedServerAndClientð   s
    
rv   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚSimpleRemotec                 C   s   || _ |d S ©NrD   )Úarg©r%   ry   r'   r'   r(   Úremote_thunk  s    zSimpleRemote.remote_thunkc                 C   s
   t ƒ ‚d S r-   )Ú	Exceptionrz   r'   r'   r(   Úremote_knuth  s    zSimpleRemote.remote_knuthN)r*   r+   r,   r{   r}   r'   r'   r'   r(   rw      s   rw   c                   @   s   e Zd Zdd„ ZdS )ÚNestedRemotec                 C   s   t ƒ S r-   )rw   r.   r'   r'   r(   Úremote_getSimple
  s    zNestedRemote.remote_getSimpleN)r*   r+   r,   r   r'   r'   r'   r(   r~   	  s   r~   c                   @   s   e Zd Zdd„ ZdS )Ú
SimpleCopyc                 C   s   d| _ ddi| _dg| _d S ©NrD   ÚHelloÚWorldr[   ©ÚxÚyÚzr.   r'   r'   r(   r=     s    
zSimpleCopy.__init__N©r*   r+   r,   r=   r'   r'   r'   r(   r€     s   r€   c                   @   s   e Zd ZdS )ÚSimpleLocalCopyN©r*   r+   r,   r'   r'   r'   r(   r‰     s   r‰   c                   @   s   e Zd ZdZi Zdd„ ZdS )ÚSimpleFactoryCopyz]
    @cvar allIDs: hold every created instances of this class.
    @type allIDs: C{dict}
    c                 C   s   || _ | tj|< d S r-   )Úidr‹   ÚallIDs)r%   rŒ   r'   r'   r(   r=   !  s    zSimpleFactoryCopy.__init__N)r*   r+   r,   r0   r   r=   r'   r'   r'   r(   r‹     s   r‹   c                 C   sZ   |   dd¡}|dkr&tdt| ƒf ƒ‚|tjkr@tdtjf ƒ‚tj| }|sVt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 %szfactory class has no ID: %sz&factory method found no object with id)ÚgetÚRuntimeErrorÚreprr‹   r   )rj   ZstateIdÚinstr'   r'   r(   ÚcreateFactoryCopy&  s    ÿ
ÿ
r’   c                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú
NestedCopyc                 C   s   t ƒ S r-   )r€   r.   r'   r'   r(   Úremote_getCopy;  s    zNestedCopy.remote_getCopyc                 C   s   t |ƒS r-   )r‹   )r%   Úvaluer'   r'   r(   Úremote_getFactory>  s    zNestedCopy.remote_getFactoryN)r*   r+   r,   r”   r–   r'   r'   r'   r(   r“   :  s   r“   c                   @   s   e Zd Zdd„ ZdS )ÚSimpleCachec                 C   s   d| _ ddi| _dg| _d S r   r„   r.   r'   r'   r(   Ú	__init___D  s    
zSimpleCache.__init___N)r*   r+   r,   r˜   r'   r'   r'   r(   r—   C  s   r—   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚNestedComplicatedCachec                 C   s   t ƒ | _d S r-   )ÚVeryVeryComplicatedCacheableÚcr.   r'   r'   r(   r=   K  s    zNestedComplicatedCache.__init__c                 C   s   | j S r-   )r›   r.   r'   r'   r(   Úremote_getCacheN  s    z&NestedComplicatedCache.remote_getCacheN)r*   r+   r,   r=   rœ   r'   r'   r'   r(   r™   J  s   r™   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
rš   c                 C   s   d| _ d| _d| _d S )NrD   é   é   ©r…   r†   Úfoor.   r'   r'   r(   r=   S  s    z%VeryVeryComplicatedCacheable.__init__c                 C   s   d| _ | j dd¡ d S )Né   r    )r    ÚobserverÚ
callRemoter.   r'   r'   r(   ÚsetFoo4X  s    z$VeryVeryComplicatedCacheable.setFoo4c                 C   s   || _ | j| j| jdœS )NrŸ   )r¢   r…   r†   r    ©r%   Úperspectiver¢   r'   r'   r(   ÚgetStateToCacheAndObserveFor\  s
    þz9VeryVeryComplicatedCacheable.getStateToCacheAndObserveForc                 C   s(   t  d¡ | d¡ || jkr$d | _d S )Nzstopped observingÚend)r   Úmsgr£   r¢   r¥   r'   r'   r(   ÚstoppedObservingb  s    


z-VeryVeryComplicatedCacheable.stoppedObservingN)r*   r+   r,   r=   r¤   r§   rª   r'   r'   r'   r(   rš   R  s   rš   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚRatherBaroqueCachec                 C   s
   || _ d S r-   ©r    )r%   ZnewFoor'   r'   r(   Úobserve_fooj  s    zRatherBaroqueCache.observe_fooc                 C   s   t  d¡ d S )Nzthe end of things)r   r©   r.   r'   r'   r(   Úobserve_endm  s    zRatherBaroqueCache.observe_endN)r*   r+   r,   r­   r®   r'   r'   r'   r(   r«   i  s   r«   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚSimpleLocalCachec                 C   s   | j  |¡ d S r-   )Ú__dict__Úupdate)r%   rj   r'   r'   r(   ÚsetCopyableStatet  s    z!SimpleLocalCache.setCopyableStatec                 C   s   | j S r-   )Úcheckr.   r'   r'   r(   ÚcheckMethodw  s    zSimpleLocalCache.checkMethodc                 C   s   | S r-   r'   r.   r'   r'   r(   Ú	checkSelfz  s    zSimpleLocalCache.checkSelfc                 C   s   dS rx   r'   r.   r'   r'   r(   r³   }  s    zSimpleLocalCache.checkN)r*   r+   r,   r²   r´   rµ   r³   r'   r'   r'   r(   r¯   s  s   r¯   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚNestedCachec                 C   s   t ƒ | _d S r-   )r—   r…   r.   r'   r'   r(   r=   „  s    zNestedCache.__init__c                 C   s   | j | j gS r-   ©r…   r.   r'   r'   r(   rœ   ‡  s    zNestedCache.remote_getCachec                 C   s
   | j |kS r-   r·   ©r%   Úcacher'   r'   r(   Úremote_putCacheŠ  s    zNestedCache.remote_putCacheN)r*   r+   r,   r=   rœ   rº   r'   r'   r'   r(   r¶   ƒ  s   r¶   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
Ú
Observablec                 C   s
   g | _ d S r-   )Ú	observersr.   r'   r'   r(   r=     s    zObservable.__init__c                 C   s   | j  |¡ d S r-   )r¼   Úappend©r%   Zobsr'   r'   r(   Úremote_observe’  s    zObservable.remote_observec                 C   s   | j  |¡ d S r-   )r¼   Úremover¾   r'   r'   r(   Úremote_unobserve•  s    zObservable.remote_unobservec                 C   s   | j D ]}| d| |¡ qd S )NÚnotify)r¼   r£   )r%   Úobjr¢   r'   r'   r(   rÂ   ˜  s    
zObservable.notifyN)r*   r+   r,   r=   r¿   rÁ   rÂ   r'   r'   r'   r(   r»   Ž  s   r»   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚDeferredRemotec                 C   s
   d| _ d S )Nr   ©Úrunr.   r'   r'   r(   r=   ž  s    zDeferredRemote.__init__c                 C   s   || _ |d S rx   rÅ   rz   r'   r'   r(   ÚrunMe¡  s    zDeferredRemote.runMec                 C   s   dst dƒ‚d S )Nr   zshouldn't have been run!)ÚAssertionErrorrz   r'   r'   r(   Ú	dontRunMe¥  s    zDeferredRemote.dontRunMec                 C   s    t ƒ }| | 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Ë   r'   r'   r(   Úremote_doItLater¨  s    zDeferredRemote.remote_doItLaterN)r*   r+   r,   r=   rÇ   rÉ   rÌ   r'   r'   r'   r(   rÄ     s   rÄ   c                   @   s   e Zd ZdZdZdd„ ZdS )ÚObserverr   Nc                 C   s"   || _ | jd | _| d| ¡ d S )NrD   Z	unobserve)rÃ   Únotifiedr£   )r%   ÚotherrÃ   r'   r'   r(   Úremote_notify¶  s    zObserver.remote_notify)r*   r+   r,   rÎ   rÃ   rÐ   r'   r'   r'   r(   rÍ   ³  s   rÍ   c                   @   s   e Zd Zdd„ ZdS )ÚNewStyleCopyc                 C   s
   || _ d S r-   )Ús)r%   rÒ   r'   r'   r(   r=   ½  s    zNewStyleCopy.__init__Nrˆ   r'   r'   r'   r(   rÑ   ¼  s   rÑ   c                   @   s(   e Zd ZdZdZdZdd„ Zdd„ ZdS )ÚNewStyleCopy2r   rD   c                 C   s"   t  jd7  _t | ¡}d|_|S )NrD   r   )rÓ   Ú	allocatedÚobjectÚ__new__r•   )r%   r‘   r'   r'   r(   rÖ   Ç  s    
zNewStyleCopy2.__new__c                 C   s   t  jd7  _d S rx   )rÓ   Úinitializedr.   r'   r'   r(   r=   Í  s    zNewStyleCopy2.__init__N)r*   r+   r,   rÔ   r×   r•   rÖ   r=   r'   r'   r'   r(   rÓ   Â  s
   rÓ   c                   @   s   e Zd Zdd„ ZdS )ÚNewStyleCacheCopyc                 C   s   | j S r-   )r°   r¥   r'   r'   r(   r§   Ô  s    z.NewStyleCacheCopy.getStateToCacheAndObserveForN)r*   r+   r,   r§   r'   r'   r'   r(   rØ   Ó  s   rØ   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚEchoerc                 C   s   |S r-   r'   ©r%   Ústr'   r'   r(   Úremote_echoÛ  s    zEchoer.remote_echoc                 K   s   ||fS r-   r'   )r%   rÛ   Úkwr'   r'   r(   Úremote_echoWithKeywordsß  s    zEchoer.remote_echoWithKeywordsN)r*   r+   r,   rÜ   rÞ   r'   r'   r'   r(   rÙ   Ú  s   rÙ   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚCachedReturnerc                 C   s
   || _ d S r-   ©r¹   r¸   r'   r'   r(   r=   ä  s    zCachedReturner.__init__c                 C   s   | j S r-   rà   rÚ   r'   r'   r(   Úremote_giveMeCacheæ  s    z!CachedReturner.remote_giveMeCacheN)r*   r+   r,   r=   rá   r'   r'   r'   r(   rß   ã  s   rß   c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚNewStyleTestsc                 C   sD   t  tƒ ¡| _t  ¡ }t| || jd\}| _| _|  | 	¡ ¡| _
dS )zY
        Create a pb server using L{Echoer} protocol and connect a client to it.
        )r[   rT   r\   N)r	   rq   rÙ   r\   rs   r_   r:   rA   ZsuccessResultOfÚgetRootObjectÚref)r%   rT   r9   r'   r'   r(   ÚsetUpì  s    ýzNewStyleTests.setUpc                 C   s   dt _dt _dt _dS )zp
        Close client and server connections, reset values of L{NewStyleCopy2}
        class variables.
        r   rD   N)rÓ   rÔ   r×   r•   r.   r'   r'   r(   ÚtearDownù  s    zNewStyleTests.tearDownc                    s<   t dƒ‰ ˆj dˆ ¡}ˆj ¡  ‡ ‡fdd„}| |¡ |S )zY
        Create a new style object, send it over the wire, and check the result.
        r•   Úechoc                    s,   ˆ  | t¡ ˆ | jd¡ ˆ | ˆ k¡ d S )Nr•   )ÚassertIsInstancerÑ   ÚassertEqualrÒ   ÚassertFalse©Úres©Úorigr%   r'   r(   Úcb
  s    z'NewStyleTests.test_newStyle.<locals>.cb©rÑ   rä   r£   rA   rB   ÚaddCallback©r%   rË   rï   r'   rí   r(   Útest_newStyle  s    

zNewStyleTests.test_newStylec                    sV   t ƒ ‰ ˆ t jd¡ ˆ t jd¡ ˆj dˆ ¡}ˆj ¡  ‡ ‡fdd„}| |¡ |S )zN
        Send a new style object and check the number of allocations.
        rD   rç   c                    sF   ˆ  | t¡ ˆ | jd¡ ˆ tjd¡ ˆ tjd¡ ˆ | ˆ ¡ d S )Nr   rž   rD   )rè   rÓ   ré   r•   rÔ   r×   ÚassertIsNotrë   rí   r'   r(   rï     s
    z$NewStyleTests.test_alloc.<locals>.cb)	rÓ   ré   rÔ   r×   rä   r£   rA   rB   rñ   rò   r'   rí   r(   Ú
test_alloc  s    

zNewStyleTests.test_allocc                    sB   t dƒ‰ ˆjjdˆ ddd}ˆj ¡  ‡ ‡fdd„}| |¡ |S )zo
        Create a new style object with keywords,
        send it over the wire, and check the result.
        Úvalue1ZechoWithKeywordsÚoneÚtwo©Zkeyword1Zkeyword2c                    sh   ˆ  | t¡ ˆ  | d t¡ ˆ  | d t¡ ˆ | d jd¡ ˆ | d ˆ ¡ ˆ | d dddœ¡ d S )Nr   rD   rö   r÷   rø   rù   )rè   ÚtuplerÑ   Údictré   rÒ   rô   rë   rí   r'   r(   rï   /  s    z3NewStyleTests.test_newStyleWithKeywords.<locals>.cbrð   rò   r'   rí   r(   Útest_newStyleWithKeywords&  s    
 ÿ

z'NewStyleTests.test_newStyleWithKeywordsN)r*   r+   r,   rå   ræ   ró   rõ   rü   r'   r'   r'   r(   râ   ê  s
   
râ   c                   @   s$   e Zd ZdZdZdd„ Zdd„ ZdS )Ú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                 C   s   t j | |¡ tƒ | _dS )z)
        Initialize the factory.
        N)r	   rq   r=   r   ÚconnectionMade)r%   Úrootr'   r'   r(   r=   I  s    z&ConnectionNotifyServerFactory.__init__c                 C   s*   || _ | jd }| _|dk	r&| d¡ dS )zF
        Store the protocol and fire the connection deferred.
        N)ÚprotocolInstancerþ   re   )r%   r   rË   r'   r'   r(   ÚclientConnectionMadeQ  s    z2ConnectionNotifyServerFactory.clientConnectionMade)r*   r+   r,   r0   r   r=   r  r'   r'   r'   r(   rý   ;  s   rý   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚNewStyleCachedTestsc                    sv   t ƒ ˆ _dˆ j_t dttˆ jƒƒ¡ˆ _t 	¡ }t 
dˆ j ¡ j|¡ ‡ fdd„}| ¡  |¡}ˆ jjj}t||gƒS )zi
        Create a pb server using L{CachedReturner} protocol and connect a
        client to it.
        r•   r   Z	localhostc                    s
   | ˆ _ d S r-   ©rä   r  r.   r'   r(   ÚgotRooti  s    z*NewStyleCachedTests.setUp.<locals>.gotRoot)rØ   rî   rÒ   r   Ú	listenTCPrý   rß   r:   r	   rs   Ú
connectTCPÚgetHostÚportrã   rñ   Úfactoryrþ   r   )r%   rT   r  Zd1Zd2r'   r.   r(   rå   ]  s    ÿÿ
zNewStyleCachedTests.setUpc                 C   s(   | j jjj ¡  | jjj ¡  | j  ¡ S )z6
        Close client and server connections.
        )r:   r	  r   rH   ÚloseConnectionrä   rN   ÚstopListeningr.   r'   r'   r(   ræ   p  s    zNewStyleCachedTests.tearDownc                    s8   ˆ j  dˆ j¡}‡ fdd„}| |d¡ | |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                    sF   ˆ   | t¡ ˆ  d| j¡ ˆ  ˆ j| ¡ |rB| ˆ _ˆ j dˆ j¡S d S )Nr•   r  )	rè   rØ   ré   rÒ   rô   rî   rì   rä   r£   )rì   Zagainr.   r'   r(   rï   €  s    z2NewStyleCachedTests.test_newStyleCache.<locals>.cbTF)rä   r£   rî   rñ   rò   r'   r.   r(   Útest_newStyleCachey  s
    z&NewStyleCachedTests.test_newStyleCacheN)r*   r+   r,   rå   ræ   r  r'   r'   r'   r(   r  \  s   	r  c                   @   s˜   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ ZdS )$ÚBrokerTestsNc                 C   s(   zt  d¡ W n tk
r"   Y nX d S )NúNone-None-TESTING.pub)ÚosÚunlinkÚOSErrorr.   r'   r'   r(   ræ   •  s    zBrokerTests.tearDownc                 C   s   |   d|f ¡ d S )Nz(This should cause a return value, not %s©Úfail)r%   Úerrorr'   r'   r(   ÚthunkErrorBadœ  s    zBrokerTests.thunkErrorBadc                 C   s
   || _ d S r-   )ÚthunkResult©r%   Úresultr'   r'   r(   ÚthunkResultGoodŸ  s    zBrokerTests.thunkResultGoodc                 C   s   d S r-   r'   )r%   Útbr'   r'   r(   ÚthunkErrorGood¢  s    zBrokerTests.thunkErrorGoodc                 C   s   |   d|f ¡ d S )Nz"This should cause an error, not %sr  r  r'   r'   r(   ÚthunkResultBad¥  s    zBrokerTests.thunkResultBadc           	      C   sª   t | d\}}}G dd„ dtjƒ}G dd„ dtjƒ}| d|ƒ ¡ | d¡}|ƒ }|ƒ }| d||¡ | ¡  | ¡  | ¡  |  |j|d¡ |  	| 
d¡| 
d¡¡ d S )	N©r[   c                   @   s   e Zd Zdd„ ZdS )z%BrokerTests.test_reference.<locals>.Xc                 S   s
   || _ d S r-   )Úcaughtrz   r'   r'   r(   Úremote_catch¬  s    z2BrokerTests.test_reference.<locals>.X.remote_catchN)r*   r+   r,   r   r'   r'   r'   r(   ÚX«  s   r!  c                   @   s   e Zd Zdd„ ZdS )z%BrokerTests.test_reference.<locals>.Yc                 S   s   |  d|¡ d S )NZcatch©r£   ©r%   ÚaÚbr'   r'   r(   Úremote_throw°  s    z2BrokerTests.test_reference.<locals>.Y.remote_throwN)r*   r+   r,   r&  r'   r'   r'   r(   ÚY¯  s   r'  r†   ÚthrowzX should have caught Z)rv   r	   ÚReferenceableÚsetNameForLocalÚremoteForNamer£   rA   ÚassertIsr  ré   ÚremoteMethod)	r%   r›   rÒ   rA   r!  r'  r†   r…   r‡   r'   r'   r(   Útest_reference¨  s    
zBrokerTests.test_referencec                 C   s   t | d\}}}||f||ffD ]j\}}tƒ }| d|¡ | d¡}d| _| d| jd ¡ | j| j¡ | 	¡  | 	¡  |  
| j| jd¡ q d S )Nr  r    é   ZthunkrD   zresult wasn't received.)rv   rw   r*  r+  ZexpectedThunkResultr£   rÊ   r  r  rA   ré   r  )r%   r›   rÒ   rA   r…   r†   r    Úbarr'   r'   r(   Útest_resultÀ  s    
 ÿÿzBrokerTests.test_resultc                 C   s
   || _ d S r-   )ÚnestedRemoter  r'   r'   r(   ÚrefcountResultÒ  s    zBrokerTests.refcountResultc           
      C   s¶   g }g }t | d\}}}tƒ }| d|¡ | d¡}ttjd ƒD ]6}|jjsV|jjrZ qz| 	d¡ 
|j|j¡ | ¡  qBtjd }	|  |jjd¡ |  t|ƒ|	d|	t|ƒf ¡ d S )Nr  r    é
   Ú	getSimplerD   ztransport was not closedzexpected %s got %s)rv   r~   r*  r+  r   r	   ZMAX_BROKER_REFSrH   Úclosedr£   rÊ   r½   rA   Ú
assertTrueré   Úlen)
r%   ÚlÚer›   rÒ   rA   r    r…   ZignoZexpectedr'   r'   r(   Útest_tooManyRefsÕ  s     


ÿzBrokerTests.test_tooManyRefsc                 C   sŽ   t | d\}}}tƒ }| d|¡ | d¡}| d¡ | j| j¡ | ¡  | ¡  |  	| j
jd¡ |  	| j
jd d¡ |  	| j
jd d¡ d S )	Nr  r    ZgetCopyrD   r‚   rƒ   r   r[   )rv   r“   r*  r+  r£   rÊ   r  r  rA   ré   r  r…   r†   r‡   )r%   r›   rÒ   rA   r    r…   r'   r'   r(   Ú	test_copyæ  s    

 ÿzBrokerTests.test_copyc                 C   sœ   t | d\}}}tƒ }tƒ }| d|¡ | d¡}| d|¡ | ¡  | d¡ | ¡  | ¡  | d¡ | ¡  | ¡  |  |j	d¡ |  
|j	dd¡ d S )Nr  r$  ZobserverD   r4  zdidn't notifyznotified too much)rv   r»   rÍ   r*  r+  r£   rA   rÂ   ZassertIsNotNonerÃ   ré   )r%   r›   rÒ   rA   r$  r%  Zrar'   r'   r(   Útest_observeó  s    


zBrokerTests.test_observec                 C   s²   t | d\}}}tƒ }| d|¡ | d¡}| ¡  | ¡  g }| d¡ |j¡ | ¡  | ¡  |  |j	d¡ |j
 d¡ |  |j	dd¡ | ¡  | ¡  |  |d dd	¡ d S )
Nr  rË   Z	doItLaterzDeferred method run too early.r>   zDeferred method run too late.r   é   zIncorrect result.)rv   rÄ   r*  r+  rA   r£   rñ   r½   rê   rÆ   rË   re   ré   )r%   r›   rÒ   rA   rË   r:  Zresultsr'   r'   r(   Ú
test_defer  s     
   zBrokerTests.test_deferc                 C   s¨   t | d\}}}tƒ }| d|¡ | d¡}| d¡ | j| j¡ | ¡  | ¡  | j	j
}|  ||j¡ | `	tjdkr~t ¡  | ¡  | ¡  | ¡  |  ||j¡ d S )Nr  r    r5  é   )rv   r~   r*  r+  r£   rÊ   r3  r  rA   r2  ÚluidÚassertInZlocalObjectsÚsysÚ
hexversionÚgcÚcollectÚassertNotIn)r%   r›   rÒ   rA   r    r0  Zrluidr'   r'   r(   Útest_refcount  s&    

 ÿ
zBrokerTests.test_refcountc                 C   sL  t | d\}}}tƒ }tƒ }|j}| d|¡ | d|¡ | d¡}| d¡}g }	| d¡ |	j¡ 	|	j¡ | d¡ |	j¡ 	|	j¡ g }
| d¡ |
j¡ | d¡ |
j¡ | 
¡  |  |
d jd¡ |  |
d jd¡ |  |
d jd¡ | ¡  | 
¡  |  |
d jd	¡ |  t|	ƒd¡ |	d d }|  tr>| ¡ jn| ¡ j|d
¡ |  | ¡ |d¡ g }| d|¡ |j¡ | 
¡  |  |d ¡ |  | d¡| d¡¡ |j}|
d j}|  ||jd¡ ~	~| 
¡  ~
~| 
¡  tjdkròt ¡  | 
¡  |   ||jd¡ |   ||j!d¡ |   ||jd¡ |   ||j!d¡ |  "|j#d¡ d S )Nr  rÃ   ZxxxZgetCacher   rD   r   rž   r¡   zpotential refcounting issuez!other potential refcounting issueZ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)$rv   r¶   r™   r›   r*  r+  r£   rñ   r½   Ú
addErrbackrB   ré   r…   r†   r    r¤   r8  ZassertIdenticalr   r´   Ú__self__Zim_selfrµ   r7  r-  rA  rB  ZremotelyCachedObjectsrC  rD  rE  rF  rG  ZlocallyCachedObjectsÚassertIsNoner¢   )r%   r›   rÒ   rA   rÃ   Zobj2ZvccZo2Zo3ZcollÚcomplexÚcpZcol2rA  ZbaroqueLuidr'   r'   r(   Ú
test_cache2  s’    


ÿÿ
ÿÿ þÿÿ

ÿ
ÿ
ÿ
ÿ
ÿzBrokerTests.test_cachec                 C   s  zt  d¡ W n tk
r"   Y nX t| d\}}}tƒ }| d|¡ | d¡}g }| d¡ |j	| j
¡ | ¡  | ¡ }|  |jd¡ |  |jd¡ |  |jd¡ |  |jd¡ t| d\}}}| d|¡ | d¡}| d¡ |j	| j
¡ | ¡  | ¡ }|  |jd¡ d S )Nr  r  r    ZgetPubrD   r   )r  r  r  rv   ÚGetPublisherr*  r+  r£   rÊ   r½   r  rB   rd   ré   ÚactivateCalledZisActivatedÚyayIGotPublishedZ_wasCleanWhenLoaded)r%   r›   rÒ   rA   r    r0  ZaccumrÃ   r'   r'   r(   Útest_publishabley  s.    

zBrokerTests.test_publishablec                 C   s   |j | _d S r-   )rŒ   r  )r%   Úvalr'   r'   r(   ÚgotCopy•  s    zBrokerTests.gotCopyc                 C   s|   t | d\}}}d}tƒ }| d|¡ | d¡}| d|¡ | j| j¡ | ¡  | ¡  | ¡  |  	| j
|d| j
f ¡ d S )Nr  éc   r    Z
getFactoryz#ID not correct on factory object %s)rv   r“   r*  r+  r£   rÊ   rT  r  rA   ré   r  )r%   r›   rÒ   rA   ZIDrÃ   r…   r'   r'   r(   Útest_factoryCopy™  s    
 ÿ

ÿzBrokerTests.test_factoryCopy)r*   r+   r,   r  ræ   r  r  r  r  r.  r1  r3  r;  r<  r=  r?  rH  rN  rR  rT  rV  r'   r'   r'   r(   r  ’  s$   Gr  sô  helloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldc                  O   s   | a |ad S r-   )ÚcallbackArgsÚcallbackKeyword)ÚargsrÝ   r'   r'   r(   ÚfinishedCallback­  s    rZ  c                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú	Pagerizerc                 O   s   |||  | _ | _| _d S r-   )re   rY  rÝ   )r%   re   rY  rÝ   r'   r'   r(   r=   ´  s    zPagerizer.__init__c                 C   s.   t j|td| jf| jž| jŽ d  | _| _d S )Néd   )r
   ZStringPagerÚ	bigStringre   rY  rÝ   ©r%   Z	collectorr'   r'   r(   Úremote_getPages·  s    
ÿÿÿzPagerizer.remote_getPagesN)r*   r+   r,   r=   r_  r'   r'   r'   r(   r[  ³  s   r[  c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚFilePagerizerNc                 O   s    || _ |||  | _| _| _d S r-   )Úfilenamere   rY  rÝ   )r%   ra  re   rY  rÝ   r'   r'   r(   r=   À  s    zFilePagerizer.__init__c                 C   s6   t j|t| jdƒ| jf| jž| jŽ| _d  | _| _d S )NÚrb)r
   Z	FilePagerÚopenra  re   rY  rÝ   Úpagerr^  r'   r'   r(   r_  Ä  s    ÿÿÿzFilePagerizer.remote_getPages)r*   r+   r,   rd  r=   r_  r'   r'   r'   r(   r`  ½  s   r`  c                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )ÚPagingTestsz0
    Test pb objects sending data by pages.
    c              	   C   s0   |   ¡ | _t| jdƒ}| t¡ W 5 Q R X dS )z?
        Create a file used to test L{util.FilePager}.
        ÚwbN)Úmktempra  rc  Úwriter]  )r%   Úfr'   r'   r(   rå   Ð  s    
zPagingTests.setUpc                 C   s’   t | d\}}}| dttddd¡ | d¡}g }t |d¡ |j¡ |sV| 	¡  qH|  
d |d ¡td	¡ |  
td
d¡ |  
tddid¡ dS )zg
        Test L{util.StringPager}, passing a callback to fire when all pages
        are sent.
        r  r    Úhellor4  ©r•   ÚgetPagesr3   r   ú'Pages received not equal to pages sent!)rj  úCompleted callback not invokedr•   N)rv   r*  r[  rZ  r+  r
   ÚgetAllPagesrñ   r½   rA   ré   Újoinr]  rW  rX  ©r%   r›   rÒ   rA   r…   r9  r'   r'   r(   Útest_pagingWithCallbackÙ  s     

ÿÿÿz#PagingTests.test_pagingWithCallbackc                 C   sl   t | d\}}}| dtdƒ¡ | d¡}g }t |d¡ |j¡ |sP| ¡  qB|  	d 
|d ¡td¡ dS )z>
        Test L{util.StringPager} without a callback.
        r  r    Nrl  r3   r   rm  )rv   r*  r[  r+  r
   ro  rñ   r½   rA   ré   rp  r]  rq  r'   r'   r(   Útest_pagingWithoutCallbackí  s    

ÿz&PagingTests.test_pagingWithoutCallbackc           	      C   sª   |   ¡ }t|dƒ ¡  t| d\}}}t|dƒ}| d|¡ | d¡}g }t |d¡ 	|j
¡ d}|s€|dkr€| ¡  |d8 }qb|sŽ|  d	¡ |  d
 |d ¡d
d¡ dS )z@
        Test L{util.FilePager}, sending an empty file.
        Úwr  Nr0  rl  r4  r   rD   zgetAllPages timed outr3   rm  )rg  rc  Úcloserv   r`  r*  r+  r
   ro  rñ   r½   rA   r  ré   rp  )	r%   ZfilenameEmptyr›   rÒ   rA   Ú	pagerizerr…   r9  Zttlr'   r'   r(   Útest_emptyFilePagingü  s"    



ÿz PagingTests.test_emptyFilePagingc                 C   sª   t | d\}}}t| jtddd}| d|¡ | d¡}g }t |d¡ |j	¡ |s^| 
¡  qP|  d |d ¡td	¡ |  td
d¡ |  tddid¡ |  |jjg ¡ dS )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é	   rk  r0  rl  r3   r   rm  )rx  rn  r•   N)rv   r`  ra  rZ  r*  r+  r
   ro  rñ   r½   rA   ré   rp  r]  rW  rX  rd  Úchunks©r%   r›   rÒ   rA   rv  r…   r9  r'   r'   r(   Útest_filePagingWithCallback  s*     ÿ

ÿÿÿz'PagingTests.test_filePagingWithCallbackc                 C   s„   t | d\}}}t| jdƒ}| d|¡ | d¡}g }t |d¡ |j¡ |sX| 	¡  qJ|  
d |d ¡td¡ |  
|jjg ¡ dS )z<
        Test L{util.FilePager} without a callback.
        r  Nr0  rl  r3   r   rm  )rv   r`  ra  r*  r+  r
   ro  rñ   r½   rA   ré   rp  r]  rd  rz  r{  r'   r'   r(   Útest_filePagingWithoutCallback)  s    

ÿz*PagingTests.test_filePagingWithoutCallbackN)
r*   r+   r,   r0   rå   rr  rs  rw  r|  r}  r'   r'   r'   r(   re  Ë  s   	re  c                   @   s   e Zd Zdd„ ZdS )ÚDumbPublishablec                 C   s   ddiS )NrQ  rD   r'   r.   r'   r'   r(   ÚgetStateToPublish<  s    z!DumbPublishable.getStateToPublishN)r*   r+   r,   r  r'   r'   r'   r(   r~  ;  s   r~  c                   @   s   e Zd Zdd„ ZdS )ÚDumbPubc                 C   s
   d| _ d S rx   )rP  r.   r'   r'   r(   Ú	activatedA  s    zDumbPub.activatedN)r*   r+   r,   r  r'   r'   r'   r(   r€  @  s   r€  c                   @   s   e Zd Zdd„ Zdd„ ZdS )rO  c                 C   s   t dƒ| _d S )NZTESTING)r~  Úpubr.   r'   r'   r(   r=   F  s    zGetPublisher.__init__c                 C   s   | j S r-   )r‚  r.   r'   r'   r(   Úremote_getPubI  s    zGetPublisher.remote_getPubN)r*   r+   r,   r=   rƒ  r'   r'   r'   r(   rO  E  s   rO  c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚDisconnectionTestsz'
    Test disconnection callbacks.
    c                 G   s   t d|f ƒ‚d S )Nz I shouldn't have been called: %s©r   )r%   rY  r'   r'   r(   r  T  s    zDisconnectionTests.errorc                 C   s
   d| _ dS )z.
        Called on broker disconnect.
        rD   N)ÚgotCallbackr.   r'   r'   r(   ÚgotDisconnectedX  s    z"DisconnectionTests.gotDisconnectedc                 C   s   |   || j¡ d| _dS )z7
        Called on RemoteReference disconnect.
        rD   N)ré   ÚremoteObjectÚobjectCallback©r%   Úor'   r'   r(   ÚobjectDisconnected^  s    z%DisconnectionTests.objectDisconnectedc                 C   sf   t | d\}}}| ¡  | dtƒ ¡ | d¡}g }| dtƒ ¡ |j¡ | 	¡  |  
t|ƒd¡ d S )Nr  r‹  Z
setBadCopyrD   )rv   rA   r*  Ú
BadCopySetr+  r£   ÚBadCopyablerI  r½   rB   ré   r8  )r%   r›   rÒ   rA   Úgr9  r'   r'   r(   Útest_badSerializatione  s    
z(DisconnectionTests.test_badSerializationc                 C   s$  t | d\}}}| ¡  | dtƒ ¡ | d¡}| ¡  | ¡  | ¡  | | j¡ |  | j|j¡ | 	| j¡ |  
| j|j¡ | | j¡ |  |j|j¡ |  | j|j¡ | 	| j¡ |  
|j|j¡ |  
| j|j¡ | | j¡ | | j¡ || _| t tj¡¡ |  | j¡ |  | j¡ d S )Nr  r‹  )rv   rA   r*  rw   r+  ÚnotifyOnDisconnectr  rB  ZdisconnectsZdontNotifyOnDisconnectrG  Z_disconnectedZdisconnectCallbacksr‡  rŒ  rˆ  rK   r   rL   r   rM   r7  r†  r‰  )r%   r›   rÒ   rA   Úrr'   r'   r(   Útest_disconnectiono  s.    
z%DisconnectionTests.test_disconnectionN)	r*   r+   r,   r0   r  r‡  rŒ  r  r“  r'   r'   r'   r(   r„  O  s   
r„  c                   @   s   e Zd ZdS )ÚFreakOutNrŠ   r'   r'   r'   r(   r”  “  s   r”  c                   @   s   e Zd Zdd„ ZdS )rŽ  c                 C   s
   t ƒ ‚d S r-   )r”  )r%   Úpr'   r'   r(   ÚgetStateToCopyFor˜  s    zBadCopyable.getStateToCopyForN)r*   r+   r,   r–  r'   r'   r'   r(   rŽ  —  s   rŽ  c                   @   s   e Zd Zdd„ ZdS )r  c                 C   s   d S r-   r'   )r%   Zbcr'   r'   r(   Úremote_setBadCopy  s    zBadCopySet.remote_setBadCopyN)r*   r+   r,   r—  r'   r'   r'   r(   r  œ  s   r  c                   @   s*   e Zd ZdZdd„ Zd
dd„Zdd„ Zd	S )ÚLocalRemoteTestr   c                 C   s   |d S rx   r'   )r%   r…   r'   r'   r(   Ú	sync_add1¤  s    zLocalRemoteTest.sync_add1rD   c                 C   s   || S r-   r'   )r%   r…   r†   r'   r'   r(   Ú	async_add§  s    zLocalRemoteTest.async_addc                 C   s
   t ƒ ‚d S r-   r…  r.   r'   r'   r(   Ú
async_failª  s    zLocalRemoteTest.async_failN)r   rD   )r*   r+   r,   ZreportAllTracebacksr™  rš  r›  r'   r'   r'   r(   r˜  ¡  s   
r˜  c                   @   s@   e Zd ZdZd ZZdd„ Zdd„ Zdd„ Zd	d
„ Z	dd„ Z
dS )Ú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                 C   s
   || _ d S r-   ©r5   ©r%   r5   r'   r'   r(   r=   º  s    zMyPerspective.__init__c                 C   s   | j S )zT
        Return the avatar identifier which was used to access this avatar.
        r  r.   r'   r'   r(   Úperspective_getAvatarId¾  s    z%MyPerspective.perspective_getAvatarIdc                 C   s   t ƒ S r-   )ÚMyViewr.   r'   r'   r(   Úperspective_getViewPointÅ  s    z&MyPerspective.perspective_getViewPointc                 C   s   || 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#  r'   r'   r(   Úperspective_addÉ  s    zMyPerspective.perspective_addc                 C   s
   d| _ d S ©NT©Ú	loggedOutr.   r'   r'   r(   ÚlogoutÒ  s    zMyPerspective.logoutN)r*   r+   r,   r0   ÚloggedInr¥  r=   rŸ  r¡  r¢  r¦  r'   r'   r'   r(   rœ  ¯  s   	rœ  c                   @   s    e Zd ZdZeZdZdd„ ZdS )Ú	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                 C   s`   |t jkst‚|dkst‚|tjkr6t jtƒ dd„ fS |  |¡| _d| j_t j| j| jj	fS d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                   S   s   d S r-   r'   r'   r'   r'   r(   r2   ð  r3   z)TestRealm.requestAvatar.<locals>.<lambda>TN)
r	   r4   rÈ   r   Z	ANONYMOUSrÙ   ÚperspectiveFactoryÚlastPerspectiver§  r¦  ©r%   r5   r6   Ú	interfacer'   r'   r(   r7   ç  s    
 þzTestRealm.requestAvatar)r*   r+   r,   r0   rœ  rª  r«  r7   r'   r'   r'   r(   r¨  ×  s   r¨  c                   @   s   e Zd Zdd„ ZdS )r   c                 C   s
   t |tƒS r-   )r"   rœ  r$   r'   r'   r(   Ú
view_checkü  s    zMyView.view_checkN)r*   r+   r,   r®  r'   r'   r'   r(   r   ú  s   r   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú
LeakyRealmz]
    A realm which hangs onto a reference to the mind object in its logout
    function.
    c                 C   s
   || _ dS )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%   Z	mindEaterr'   r'   r(   r=     s    zLeakyRealm.__init__c                    s*   |   ˆ ¡ |  |¡‰tjˆ‡ ‡fdd„fS )Nc                      s   ˆ ˆ  ¡ fS r-   )r¦  r'   ©r6   Zperspr'   r(   r2     r3   z*LeakyRealm.requestAvatar.<locals>.<lambda>)r°  rª  r	   r4   r¬  r'   r±  r(   r7     s    

zLeakyRealm.requestAvatarN)r*   r+   r,   r0   r=   r7   r'   r'   r'   r(   r¯    s   
r¯  c                   @   s   e Zd ZdZdd„ ZdS )ÚNewCredLeakTestsz/
    Tests to try to trigger memory leaks.
    c                    sž   dˆ_ ‡fdd„}tˆt|ƒd\}‰‰g ‰ | d¡}| dd¡}dd	„ }| |¡ ‡ ‡‡fd
d„}| |¡ ˆ ¡  ˆ ˆ dg¡ t 	¡  ˆ 
ˆ  ¡ ¡ dS )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                    s   t  | ¡ˆ _d S r-   )Úweakrefrä   ÚmindRef)r6   r.   r'   r(   Ú
setMindRef$  s    z4NewCredLeakTests.test_logoutLeak.<locals>.setMindRef)r[   rt   rÿ   Úloginro   c                 S   s$   | \}}t ƒ }| dt |d¡|¡S )NÚrespondro   )rw   r£   r	   r·  )r…   Z	challengeZ
challengerr6   r'   r'   r(   Ú
cbResponse/  s    
 ÿz4NewCredLeakTests.test_logoutLeak.<locals>.cbResponsec                    s*   ˆ  ¡  ˆ  d¡ ˆ t tdƒ¡¡ d S )NrD   Zboom)rC   r½   rK   r   rL   r   )Ú_)ÚconnectionBrokenrA   r^   r'   r(   rK   5  s    
z8NewCredLeakTests.test_logoutLeak.<locals>.connectionLostrD   )r´  rv   r¯  r+  r£   rñ   rB   ré   rE  rF  rK  )r%   rµ  r]   rÿ   rË   r¸  rK   r'   )rº  rA   r%   r^   r(   Útest_logoutLeak  s"     ÿ


z NewCredLeakTests.test_logoutLeakN)r*   r+   r,   r0   r»  r'   r'   r'   r(   r²    s   r²  c                   @   s¦   e Zd ZdZdd„ Zd%dd„Ze ej	¡fdd„Z
d	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ ZdS )&ÚNewCredTestsz=
    Tests related to the L{twisted.cred} support in PB.
    c                 C   s0   t ƒ | _t | j¡| _t| jƒ| _t ¡ | _dS )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¨  rt   r   rr   rý   r\   r	   rs   rT   r.   r'   r'   r(   rå   K  s    zNewCredTests.setUpNc                 C   sR   t | | j| jƒ\| _| _| _tƒ | _tt	 
ddd¡| jƒ| _| j ¡  | j¡ dS )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}
        ZTCPrI   iá  N)r_   rT   r\   r9   r:   rA   r`   ZconnectorStaterg   r   ZIPv4AddressrP   rc   rñ   ÚestablishClientAndServer)r%   Z_ignoredr'   r'   r(   r½  W  s      ÿþ
ÿz%NewCredTests.establishClientAndServerc                 C   s0   |   | jjj¡ | j |¡ | j | j|¡ dS )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)r7  r9   rH   r6  rK   rT   rR   rP   )r%   rQ   r'   r'   r(   ÚcompleteClientLostConnectionx  s    z)NewCredTests.completeClientLostConnectionc                    sB   ˆ   ¡  ˆ j ¡ }‡ fdd„}‡ fdd„}| |¡ | |¡ |S )z¶
        Assert that L{PBClientFactory.getRootObject}'s Deferred fires with
        a L{RemoteReference}, and that disconnecting it runs its
        disconnection callbacks.
        c                    s   ˆ   | tj¡ | S r-   )rè   r	   ÚRemoteReference©ÚrootObjr.   r'   r(   ÚgotRootObject•  s    z6NewCredTests.test_getRootObject.<locals>.gotRootObjectc                    s(   t ƒ }|  |j¡ ˆ j ¡  ˆ  ¡  |S r-   )r   r‘  re   rT   Ú
disconnectr¾  )rÁ  ÚdisconnectedDeferredr.   r'   r(   rÃ  ™  s
    
z3NewCredTests.test_getRootObject.<locals>.disconnect©r½  rT   rã   rñ   )r%   ÚrootObjDeferredrÂ  rÃ  r'   r.   r(   Útest_getRootObjectŒ  s    
	

zNewCredTests.test_getRootObjectc                    s(   ˆ   ¡  ˆ j ¡ }‡ fdd„}| |¡S )z“
        Test that when a connection is lost, calling a method on a
        RemoteReference obtained from it raises L{DeadReferenceError}.
        c                    s@   t ƒ }ˆ  |j¡ ‡ ‡fdd„}| |¡ ˆj ¡  ˆ ¡  |S )Nc                    s   ˆ  tjˆ jd¡ d S )NÚmethod)ZassertRaisesr	   ZDeadReferenceErrorr£   )Úign)rÁ  r%   r'   r(   ÚlostConnection´  s
     þzSNewCredTests.test_deadReferenceError.<locals>.gotRootObject.<locals>.lostConnection)r   r‘  re   rñ   rT   rÃ  r¾  )rÁ  rÄ  rÊ  r.   rÀ  r(   rÂ  °  s    

z;NewCredTests.test_deadReferenceError.<locals>.gotRootObjectrÅ  )r%   rÆ  rÂ  r'   r.   r(   Útest_deadReferenceError¨  s    
z$NewCredTests.test_deadReferenceErrorc                    sB   G dd„ dt jƒ}|ƒ ˆ _ˆ  ¡  ˆ j ¡ }‡ fdd„}| |¡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                   @   s   e Zd ZdZdd„ ZdS )z=NewCredTests.test_clientConnectionLost.<locals>.ReconnectOnceFc                 S   s0   | j  }d| _ tj | |||¡}|r,| ¡  |S r£  )ÚreconnectedAlreadyr	   rs   rR   rm   )r%   rP   rQ   Zreconnectingr  r'   r'   r(   rR   Ì  s       ÿzRNewCredTests.test_clientConnectionLost.<locals>.ReconnectOnce.clientConnectionLostN)r*   r+   r,   rÌ  rR   r'   r'   r'   r(   ÚReconnectOnceÉ  s   rÍ  c                    sH   ˆ   | tj¡ tƒ }|  |j¡ ˆ j ¡  ˆ  ¡  ‡ fdd„}| 	|¡S )Nc                    s    ˆ j  ¡ }‡ fdd„}| |¡S )Nc                    s6   ˆ   | tj¡ tƒ }|  |j¡ ˆ j ¡  ˆ  ¡  |S r-   )	rè   r	   r¿  r   r‘  re   rT   rÃ  r¾  )ZanotherRootObjrË   r.   r'   r(   ÚgotAnotherRootObjectæ  s    
zqNewCredTests.test_clientConnectionLost.<locals>.gotRootObject.<locals>.disconnected.<locals>.gotAnotherRootObject)rT   rã   rñ   )rÉ  rË   rÎ  r.   r'   r(   rJ   ã  s    
zSNewCredTests.test_clientConnectionLost.<locals>.gotRootObject.<locals>.disconnected)
rè   r	   r¿  r   r‘  re   rT   rÃ  r¾  rñ   )rÁ  rË   rJ   r.   r'   r(   rÂ  Ú  s    
z=NewCredTests.test_clientConnectionLost.<locals>.gotRootObject)r	   rs   rT   r½  rã   rñ   )r%   rÍ  rÆ  rÂ  r'   r.   r(   Útest_clientConnectionLostÃ  s    
z&NewCredTests.test_clientConnectionLostc                 C   s<   |   ¡  | j d¡}| t tƒ ¡¡ | t 	t
j¡¡ dS )z”
        Test that if a Broker loses its connection without receiving any bytes,
        it doesn't raise any exceptions or log any errors.
        )rI   i90  N)r½  r\   rV   rX   r   rY   rW   rK   r   rL   r   rM   )r%   ZserverProtor'   r'   r(   Útest_immediateCloseó  s    z NewCredTests.test_immediateClosec                 C   s<   t  ¡ }| t dd¡¡}| dt tdƒ¡¡ |  	|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	   rs   r¶  r   ÚUsernamePasswordZclientConnectionFailedr   rL   r   ÚassertFailure)r%   rT   ZloginDeferredr'   r'   r(   Útest_loginConnectionRefusedþ  s    
ÿÿþz(NewCredTests.test_loginConnectionRefusedc                    s®   ˆj  tjdd¡ t dd¡}d}tƒ ‰ ˆj ||¡}‡ ‡fdd„}‡fdd„}| 	|¡ | 	|¡ ‡fd	d
„}| 	|¡ ˆ 
¡  ˆj ¡  t ¡  ˆ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                    s"   ˆ  ˆjjj¡ ˆ | tj¡ ˆ S r-   )r7  rt   r«  r§  rè   r	   r¿  ©r¦   ©ZloginCompletedr%   r'   r(   ÚcbLogin!  s    z.NewCredTests.test_loginLogout.<locals>.cbLoginc                    s   ˆ j  ¡  ˆ  ¡  d S r-   )rT   rÃ  r¾  ©Úignoredr.   r'   r(   ÚcbDisconnect&  s    
z3NewCredTests.test_loginLogout.<locals>.cbDisconnectc                    s   ˆ   ˆ jjj¡ d S r-   )r7  rt   r«  r¥  rÜ  r.   r'   r(   ÚcbLogout-  s    z/NewCredTests.test_loginLogout.<locals>.cbLogoutN)r   ÚregisterCheckerr   rp   r   rÓ  r   rT   r¶  rñ   r½  rA   rB   rE  rF  re   )r%   Zcredsr6   rË   rÛ  rÞ  rß  r'   rÚ  r(   Útest_loginLogout  s&    
ÿ





zNewCredTests.test_loginLogoutc                    s¢   t ƒ ‰ G ‡ fdd„dtjƒ}|ˆj_ˆj tjdd¡ ˆj	 
t dd¡d¡}‡ fdd„}| |¡ ‡fd	d
„}| |¡ ˆ ¡  ˆj ¡  t ¡  ˆ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                       s$   e Zd ZdZdd„ Z‡ fdd„ZdS )z=NewCredTests.test_logoutAfterDecref.<locals>.EventPerspectivezQ
            An avatar which fires a Deferred when it is logged out.
            c                 S   s   d S r-   r'   rž  r'   r'   r(   r=   I  s    zFNewCredTests.test_logoutAfterDecref.<locals>.EventPerspective.__init__c                    s   ˆ   d ¡ d S r-   )re   r.   r¤  r'   r(   r¦  L  s    zDNewCredTests.test_logoutAfterDecref.<locals>.EventPerspective.logoutN)r*   r+   r,   r0   r=   r¦  r'   r¤  r'   r(   ÚEventPerspectiveE  s   râ  rÒ  r¬   rÑ  r©  c                    s   ˆ S r-   r'   )Zavatarr¤  r'   r(   Ú
cbLoggedInW  s    z7NewCredTests.test_logoutAfterDecref.<locals>.cbLoggedInc                    s   ˆ   ˆ jjji ¡ d S r-   )ré   r\   r   Z_localCleanuprÜ  r.   r'   r(   ÚcbLoggedOut]  s    z8NewCredTests.test_logoutAfterDecref.<locals>.cbLoggedOut)r   r	   ÚAvatarrt   rª  r   rà  r   rp   rT   r¶  r   rÓ  rñ   r½  rA   rB   rE  rF  )r%   râ  rË   rã  rä  r'   )r¥  r%   r(   Útest_logoutAfterDecref=  s&    

ÿ
 ÿ



z#NewCredTests.test_logoutAfterDecrefc                    sŒ   ˆ j  tjddd¡ ˆ j t dd¡d¡}ˆ j t dd¡d¡}t||gƒ}dd„ }| 	|¡ ‡ fd	d
„}| 	|¡ ˆ  
¡  ˆ 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                 S   s    | \}}t | d¡| d¡gƒS )NZgetAvatarId)r   r£   )r  ÚfirstÚsecondr'   r'   r(   rã  }  s
    þz5NewCredTests.test_concurrentLogin.<locals>.cbLoggedInc                    s$   | \}}ˆ   |d¡ ˆ   |d¡ d S )NrÑ  rè  )ré   )r…   ré  rê  r.   r'   r(   ÚcbAvatarIdsƒ  s    z6NewCredTests.test_concurrentLogin.<locals>.cbAvatarIds)r   rà  r   rp   rT   r¶  r   rÓ  r   rñ   r½  rA   rB   )r%   Ú
firstLoginÚsecondLoginrË   rã  rë  r'   r.   r(   Útest_concurrentLogino  s,     ÿÿ
 ÿ
 ÿ


z!NewCredTests.test_concurrentLoginc                    sŒ   ˆ j  tjdd¡ ˆ j t dd¡¡}ˆ j t dd¡¡}ˆ  |t	¡ ˆ  |t	¡ t
||gƒ}‡ fdd„}| |¡ ˆ  ¡  ˆ 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                    s   ˆ   t¡}ˆ  t|ƒd¡ d S )Nr   )ÚflushLoggedErrorsr   ré   r8  ©ÚignoreÚerrorsr.   r'   r(   Úcleanup   s    
z;NewCredTests.test_badUsernamePasswordLogin.<locals>.cleanup)r   rà  r   rp   rT   r¶  r   rÓ  rÔ  r   r   rñ   r½  rA   rB   )r%   rì  rí  rË   ró  r'   r.   r(   Útest_badUsernamePasswordLogin  s"    
ÿ
ÿ
ÿ

z*NewCredTests.test_badUsernamePasswordLoginc                 C   sX   | j  t ¡ ¡ | j t ¡ d¡}dd„ }| |¡ | | j	d¡ |  
¡  | 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                 S   s   |   dd¡S ©Nrç   é{   r"  rÙ  r'   r'   r(   rã  ´  s    z4NewCredTests.test_anonymousLogin.<locals>.cbLoggedInrö  )r   rà  r   ÚAllowAnonymousAccessrT   r¶  r   Ú	Anonymousrñ   ré   r½  rA   rB   )r%   rË   rã  r'   r'   r(   Útest_anonymousLogin«  s    

z NewCredTests.test_anonymousLoginc                    s^   ˆ j  tjdd¡ ˆ j t ¡ d¡}ˆ  |t	¡ ‡ fdd„}| 
|¡ ˆ  ¡  ˆ j ¡  |S )zg
        Verify that without an anonymous checker set up, anonymous login is
        rejected.
        Úpassr×  r©  c                    s   ˆ   t¡}ˆ  t|ƒd¡ d S rx   )rï  r   ré   r8  rð  r.   r'   r(   ró  É  s    
z=NewCredTests.test_anonymousLoginNotPermitted.<locals>.cleanup)r   rà  r   rp   rT   r¶  r   rø  rÔ  r   rñ   r½  rA   rB   ©r%   rË   ró  r'   r.   r(   Útest_anonymousLoginNotPermitted¿  s    
ÿ

z,NewCredTests.test_anonymousLoginNotPermittedc                 C   sl   | j  t ¡ ¡ | j  tjdd¡ | j t ¡ d¡}dd„ }| 	|¡ | 	| j
d¡ |  ¡  | j ¡  |S )z„
        Like L{test_anonymousLogin} but against a portal with a checker for
        both IAnonymous and IUsernamePassword.
        rÖ  r×  r©  c                 S   s   |   dd¡S rõ  r"  rÙ  r'   r'   r(   rÛ  Þ  s    zENewCredTests.test_anonymousLoginWithMultipleCheckers.<locals>.cbLoginrö  )r   rà  r   r÷  rp   rT   r¶  r   rø  rñ   ré   r½  rA   rB   ©r%   rË   rÛ  r'   r'   r(   Ú'test_anonymousLoginWithMultipleCheckersÔ  s    
ÿ

z4NewCredTests.test_anonymousLoginWithMultipleCheckersc                 C   sp   | j  t ¡ ¡ | j  tjdd¡ | j t dd¡d¡}dd„ }| 	|¡ | 	| j
d¡ |  ¡  | j ¡  |S )z€
        Like L{test_anonymousLoginWithMultipleCheckers} but check that
        username/password authentication works.
        rÖ  r×  rØ  r©  c                 S   s   |   ddd¡S )NÚaddr\  é   r"  rÙ  r'   r'   r(   rÛ  õ  s    zINewCredTests.test_authenticatedLoginWithMultipleCheckers.<locals>.cbLoginrö  )r   rà  r   r÷  rp   rT   r¶  r   rÓ  rñ   ré   r½  rA   rB   rý  r'   r'   r(   Ú+test_authenticatedLoginWithMultipleCheckersê  s    
ÿ
 ÿ

z8NewCredTests.test_authenticatedLoginWithMultipleCheckersc                 C   sp   | j  tjdd¡ | j t dd¡d¡}dd„ }| |¡ dd„ }| |¡ | | j	¡ |  
¡  | j ¡  |S )	zb
        Verify that a viewpoint can be retrieved after authenticating with
        cred.
        rÖ  r×  rØ  r©  c                 S   s
   |   d¡S )NZgetViewPointr"  rÙ  r'   r'   r(   rÛ    s    z'NewCredTests.test_view.<locals>.cbLoginc                 S   s
   |   d¡S )Nr³   r"  )Z	viewpointr'   r'   r(   ÚcbView  s    z&NewCredTests.test_view.<locals>.cbView)r   rà  r   rp   rT   r¶  r   rÓ  rñ   r7  r½  rA   rB   )r%   rË   rÛ  r  r'   r'   r(   Ú	test_view  s    
ÿ
 ÿ


zNewCredTests.test_view)N)r*   r+   r,   r0   rå   r½  r   rL   r   rM   r¾  rÇ  rË  rÏ  rÐ  rÕ  rá  ræ  rî  rô  rù  rü  rþ  r  r  r'   r'   r'   r(   r¼  G  s&   
"
ÿ
0/2 r¼  c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚNonSubclassingPerspectivec                 C   s   d S r-   r'   rž  r'   r'   r(   r=     s    z"NonSubclassingPerspective.__init__c                 C   s(   |  || ¡}|  || ¡}| |||f¡S r-   )ZunserializeZ	serialize)r%   rN   ÚmessagerY  Úkwargsr'   r'   r(   ÚperspectiveMessageReceived"  s    z4NonSubclassingPerspective.perspectiveMessageReceivedc                 C   s
   d| _ d S r£  r¤  r.   r'   r'   r(   r¦  (  s    z NonSubclassingPerspective.logoutN)r*   r+   r,   r=   r  r¦  r'   r'   r'   r(   r    s   r  c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚNSPTestsz
    Tests for authentication against a realm where the L{IPerspective}
    implementation is not a subclass of L{Avatar}.
    c                 C   sŠ   t ƒ | _t| j_t | j¡| _t ¡ | _| j 	dd¡ | j 
| j¡ tt | j¡ƒ| _tjd| jdd| _|  | jj¡ | j ¡ j| _d S )NrØ  rÖ  r   rI   )r­  )r¨  rt   r  rª  r   rr   r   rp   ru   ZaddUserrà  r   r	   rq   r	  r   r  r  rZ   r  r  Úportnor.   r'   r'   r(   rå   2  s    
zNSPTests.setUpc                    sn   t  ¡ ‰ ˆ  t dd¡d¡}t dˆjˆ ¡ | dd„ ¡ | ˆj	ddd	d
if¡ ‡ ‡fdd„}| |¡ |S )z
        An L{IPerspective} implementation which does not subclass
        L{Avatar} can expose remote methods for the client to call.
        rØ  rÖ  r©  rI   c                 S   s   | j ddddS )NÚANYTHINGÚhererç  )r0  r"  )r•  r'   r'   r(   r2   H  r3   z#NSPTests.test_NSP.<locals>.<lambda>r
  )r  r0  rç  c                    s$   ˆ   ¡  ˆjjD ]}|j ¡  qd S r-   )rÃ  r	  Z	protocolsrH   r
  )rÝ  r•  ©r	  r%   r'   r(   ró  K  s    z"NSPTests.test_NSP.<locals>.cleanup)
r	   rs   r¶  r   rÓ  r   r  r	  rñ   ré   rû  r'   r  r(   Útest_NSP?  s    ÿÿ
zNSPTests.test_NSPN)r*   r+   r,   r0   rå   r  r'   r'   r'   r(   r  -  s   r  c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú
IForwardedzA
    Interface used for testing L{util.LocalAsyncForwarder}.
    c                   C   s   dS )z,
        Simple synchronous method.
        Nr'   r'   r'   r'   r(   Ú	forwardMeY  s    zIForwarded.forwardMec                   C   s   dS )z-
        Simple asynchronous method.
        Nr'   r'   r'   r'   r(   ÚforwardDeferred^  s    zIForwarded.forwardDeferredN)r*   r+   r,   r0   r  r  r'   r'   r'   r(   r  T  s   r  c                   @   s0   e Zd ZdZdZdZdd„ Zdd„ Zdd„ Zd	S )
Ú	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                 C   s
   d| _ dS )z6
        Set a local flag to test afterwards.
        TN)Ú	forwardedr.   r'   r'   r(   r  q  s    zForwarded.forwardMec                 C   s
   d| _ dS )zv
        Set a local flag to test afterwards. This should not be called as it's
        not in the interface.
        TN)Úunforwardedr.   r'   r'   r(   ÚdontForwardMew  s    zForwarded.dontForwardMec                 C   s   t dƒS )z0
        Asynchronously return C{True}.
        T)r   r.   r'   r'   r(   r  ~  s    zForwarded.forwardDeferredN)	r*   r+   r,   r0   r  r  r  r  r  r'   r'   r'   r(   r  d  s   r  c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚSpreadUtilTestsz+
    Tests for L{twisted.spread.util}.
    c                 C   s   t ƒ }|  | dd¡d¡ dS )zk
        Call a synchronous method of a L{util.LocalAsRemote} object and check
        the result.
        Úadd1r   rž   N)r˜  ré   r£   rŠ  r'   r'   r(   Ú	test_syncŠ  s    zSpreadUtilTests.test_syncc                 C   s:   t ƒ }t ƒ }|jdddd}|  |t¡ | | 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£   rè   r   rñ   ré   )r%   r‹  rË   r'   r'   r(   Ú
test_async’  s    zSpreadUtilTests.test_asyncc                    s4   t ƒ }| d¡}‡ fdd„}| ‡ fdd„|¡ |S )zG
        Test an asynchronous failure on a remote method call.
        r  c                    s   ˆ   | tj¡ |  t¡ d S r-   )rè   r   rL   Ztrapr   )ri  r.   r'   r(   Úeb¤  s    z*SpreadUtilTests.test_asyncFail.<locals>.ebc                    s
   ˆ   d¡S )Nzsupposed to failr  rë   r.   r'   r(   r2   §  r3   z0SpreadUtilTests.test_asyncFail.<locals>.<lambda>)r˜  r£   rÊ   )r%   r‹  rË   r  r'   r.   r(   Útest_asyncFailž  s
    
zSpreadUtilTests.test_asyncFailc                 C   s$   t ƒ }| d¡}|  |dƒd¡ dS )zM
        Test the C{remoteMethod} facility of L{util.LocalAsRemote}.
        r  rž   r¡   N)r˜  r-  ré   )r%   r‹  Úmr'   r'   r(   Útest_remoteMethodª  s    
z!SpreadUtilTests.test_remoteMethodc                 C   sl   t ƒ }t |t¡}| d¡ |  |j¡ | d¡ |  |j¡ | d¡}g }| 	|j
¡ |  |d d¡ dS )ze
        Test a call to L{util.LocalAsyncForwarder} using L{Forwarded} local
        object.
        r  r  r  r   rD   N)r  r
   ZLocalAsyncForwarderr  r£   r7  r  rê   r  rñ   r½   ré   )r%   ri  ZlfZrrr9  r'   r'   r(   Útest_localAsyncForwarder²  s    


z(SpreadUtilTests.test_localAsyncForwarderN)	r*   r+   r,   r0   r  r  r  r  r  r'   r'   r'   r(   r  …  s   r  c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚPBWithSecurityOptionsTestsz&
    Test security customization.
    c                 C   s&   t  ¡ }| d¡}|  |jtj¡ dS )zl
        By default, client broker should use C{jelly.globalSecurity} as
        security settings.
        N)r	   rs   rV   r,  Úsecurityr   ÚglobalSecurity©r%   r	  rN   r'   r'   r(   Ú!test_clientDefaultSecurityOptionsÉ  s    
z<PBWithSecurityOptionsTests.test_clientDefaultSecurityOptionsc                 C   s*   t  tƒ ¡}| d¡}|  |jtj¡ dS )zl
        By default, server broker should use C{jelly.globalSecurity} as
        security settings.
        N)r	   rq   rÙ   rV   r,  r  r   r   r!  r'   r'   r(   Ú!test_serverDefaultSecurityOptionsÓ  s    
z<PBWithSecurityOptionsTests.test_serverDefaultSecurityOptionsc                 C   s0   t  ¡ }tj|d}| d¡}|  |j|¡ dS )zs
        Check that the security settings are passed from the client factory to
        the broker object.
        ©r  N)r   ÚSecurityOptionsr	   rs   rV   r,  r  ©r%   r  r	  rN   r'   r'   r(   Ú test_clientSecurityCustomizationÝ  s    
z;PBWithSecurityOptionsTests.test_clientSecurityCustomizationc                 C   s4   t  ¡ }tjtƒ |d}| d¡}|  |j|¡ dS )zs
        Check that the security settings are passed from the server factory to
        the broker object.
        r$  N)r   r%  r	   rq   rÙ   rV   r,  r  r&  r'   r'   r(   Ú test_serverSecurityCustomizationè  s    
z;PBWithSecurityOptionsTests.test_serverSecurityCustomizationN)r*   r+   r,   r0   r"  r#  r'  r(  r'   r'   r'   r(   r  Ä  s
   

r  )N)…r0   Z
__future__r   r   rC  r  r@   rE  r³  Úcollectionsr   Úior   rW   Zzope.interfacer   r   Ztwisted.trialr   Ztwisted.spreadr	   r
   r   r   Ztwisted.internetr   r   r   r   Ztwisted.internet.errorr   Ztwisted.internet.deferr   r   r   Ztwisted.protocols.policiesr   Ztwisted.pythonr   r   Ztwisted.python.compatr   r   r   Ztwisted.cred.errorr   r   Ztwisted.credr   r   r   Ztwisted.test.proto_helpersr    ZViewabler!   rå  r#   ZIRealmrÕ   r1   r8   r_   r`   rg   rv   r)  rw   r~   ZCopyabler€   Z
RemoteCopyr‰   ZsetUnjellyableForClassr‹   r’   ZsetUnjellyableFactoryForClassr“   Z	Cacheabler—   r™   rš   ZRemoteCacher«   r¯   r¶   r»   rÄ   rÍ   rÑ   rÓ   rØ   ZRootrÙ   rß   ZSynchronousTestCaserâ   rq   rý   ZTestCaser  r  r]  rW  rX  rZ  r[  r`  re  ZPublishabler~  ZRemotePublishedr€  rO  r„  r|   r”  rŽ  r  ZLocalAsRemoter˜  r4   rœ  r¨  r   r¯  r²  r¼  r  r  r  r  r  r  r'   r'   r'   r(   Ú<module>   sº   
(	>4$
				Q!6  
pD'#0   X' ?