U
    ¸~Hj6÷  ã                   @   sr  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„ de-j2ƒZ3G dd„ de-j4ƒZ5eej6ƒG dd„ dƒƒZ7G dd„ dƒZ8dd„ Z9G dd„ dƒZ:G dd„ de#ƒZ;d…d d!„Z<G d"d#„ d#e-j=ƒZ>G d$d%„ d%e-j=ƒZ?G d&d'„ d'e-j@ƒZAG d(d)„ d)e-jBƒZCe- DeAeC¡ G d*d+„ d+e-j@ƒZEd,d-„ ZFe- GeEeF¡ G d.d/„ d/e-j=ƒZHG d0d1„ d1e-jIƒZJG d2d3„ d3e-j=ƒZKG d4d5„ d5e-jIƒZLG d6d7„ d7e-jMƒZNe- DeLeN¡ G d8d9„ d9e-jMƒZOe- DeJeO¡ G d:d;„ d;e-j=ƒZPG d<d=„ d=e-j=ƒZQG d>d?„ d?e-j=ƒZRG d@dA„ dAe-j=ƒZSG dBdC„ dCe-j@e-jBƒZTe- DeTeT¡ G dDdE„ dEe-j@e-jBƒZUe- DeUeU¡ G dFdG„ dGe-jIe-jMƒZVe- DeVeV¡ G dHdI„ dIe-jWƒZXG dJdK„ dKe-jWƒZYG dLdM„ dMe1jZƒZ[G dNdO„ dOe-j\ƒZ]G dPdQ„ dQe1j^ƒZ_G dRdS„ dSe1j^ƒZ`dTZadabdacdUdV„ ZdG dWdX„ dXe-j=ƒZeG dYdZ„ dZe-j=ƒZfG d[d\„ d\e1j^ƒZgG d]d^„ d^e.jhƒZiG d_d`„ d`e.jjƒZkG dadb„ dbe-j=ƒZle- Deiek¡ G dcdd„ dde1j^ƒZmG dedf„ dfenƒZoG dgdh„ dhe-j@ƒZpG didj„ dje-j=ƒZqG dkdl„ dle/jrƒZsee-jtƒG dmdn„ dne-j4ƒƒZuG dodp„ dpƒZvG dqdr„ dre-j2ƒZwG dsdt„ dtevƒZxG dudv„ dve1j^ƒZyG dwdx„ dxe1j^ƒZzee-jtƒG dydz„ dzƒƒZ{G d{d|„ d|e1j^ƒZ|G d}d~„ d~eƒZ}ee}ƒG dd€„ d€ƒƒZ~G d�d‚„ d‚e1j^ƒZG dƒd„„ d„e1j^ƒ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.
é    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                   @   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$   ú?/tmp/pip-unpacked-wheel-ewod1tgb/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_getDummyViewPoint4   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%   ÚrequestAvatar:   s    
zDummyRealm.requestAvatarN)r'   r(   r)   r4   r$   r$   r$   r%   r.   8   s   r.   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"   r6   r7   r8   r9   r$   r$   r%   Ú__init__G   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"   Útimeoutr$   r$   r%   ÚflushM   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%   ÚstopZ   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)r8   Úseekr9   ÚreadÚtruncater6   Ú	transportZ_checkProducerr7   r   ZdataReceived)r"   ZcDataZsDataÚbyter$   r$   r%   r>   a   s"    



zIOPump.pumpN)r'   r(   r)   r-   r:   r@   rA   r>   r$   r$   r$   r%   r5   @   s
   r5   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   rK   r   rL   r$   ©ÚclientFactoryr$   r%   ÚdisconnectClientFactory�   s     
ÿz7connectServerAndClient.<locals>.disconnectClientFactory)ÚbuildProtocolÚStringIOÚmakeConnectionr   ÚFileWrapperr5   Ú
addCleanupr>   )ÚtestrS   ÚserverFactoryÚaddrÚclientBrokerÚserverBrokerZclientTransportZserverTransportr>   rN   rT   r$   rR   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)   r-   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 ƒ  |¡ || _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)Úsuperr:   Ú_state)r"   r   Ústate©Ú	__class__r$   r%   r:   Õ   s    	z#_ReconnectingFakeConnector.__init__c                    s   t ƒ  ¡  | j ¡  dS )zM
        A C{connect} implementation that calls C{reconnectCallback}
        N)rh   Úconnectri   rf   r+   rk   r$   r%   rm   á   s    
z"_ReconnectingFakeConnector.connect)r'   r(   r)   r-   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)	r.   r   Ú'InMemoryUsernamePasswordDatabaseDontUser   ÚPBServerFactoryr	   ÚPortalÚPBClientFactoryr_   )rZ   ÚrealmÚcheckerr[   rS   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 ©NrB   )Ú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 ©NrB   ÚHelloÚWorldrZ   ©Ú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U dZi Zeed f ed< dd„ ZdS )ÚSimpleFactoryCopyz]
    @cvar allIDs: hold every created instances of this class.
    @type allIDs: C{dict}
    ÚallIDsc                 C   s   || _ | tj|< d S r*   )Úidr‹   rŒ   )r"   r�   r$   r$   r%   r:     s    zSimpleFactoryCopy.__init__N)	r'   r(   r)   r-   rŒ   r   ÚintÚ__annotations__r:   r$   r$   r$   r%   r‹     s   
r‹   c                 C   sZ   |   dd¡}|dkr&tdt| ƒ› �ƒ‚|tjkr@tdtj› �ƒ‚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 zfactory class has no ID: z&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_getCopy5  s    zNestedCopy.remote_getCopyc                 C   s   t |ƒS r*   )r‹   )r"   Úvaluer$   r$   r%   Úremote_getFactory8  s    zNestedCopy.remote_getFactoryN)r'   r(   r)   r–   r˜   r$   r$   r$   r%   r•   4  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___=  s    
zSimpleCache.__init___N)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 )ÚNestedComplicatedCachec                 C   s   t ƒ | _d S r*   )ÚVeryVeryComplicatedCacheableÚcr+   r$   r$   r%   r:   D  s    zNestedComplicatedCache.__init__c                 C   s   | j S r*   )r�   r+   r$   r$   r%   Úremote_getCacheG  s    z&NestedComplicatedCache.remote_getCacheN)r'   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d„ Zdd„ Zd	S )
rœ   c                 C   s   d| _ d| _d| _d S )NrB   é   é   ©r…   r†   Úfoor+   r$   r$   r%   r:   L  s    z%VeryVeryComplicatedCacheable.__init__c                 C   s   d| _ | j dd¡ d S )Né   r¢   )r¢   ÚobserverÚ
callRemoter+   r$   r$   r%   ÚsetFoo4Q  s    z$VeryVeryComplicatedCacheable.setFoo4c                 C   s   || _ | j| j| jdœS )Nr¡   )r¤   r…   r†   r¢   ©r"   Úperspectiver¤   r$   r$   r%   ÚgetStateToCacheAndObserveForU  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%   ÚstoppedObservingY  s    


z-VeryVeryComplicatedCacheable.stoppedObservingN)r'   r(   r)   r:   r¦   r©   r¬   r$   r$   r$   r%   rœ   K  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_fooa  s    zRatherBaroqueCache.observe_fooc                 C   s   t  d¡ d S )Nzthe end of things)r   r«   r+   r$   r$   r%   Úobserve_endd  s    zRatherBaroqueCache.observe_endN)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 )
ÚSimpleLocalCachec                 C   s   | j  |¡ d S r*   )Ú__dict__Úupdate)r"   rj   r$   r$   r%   ÚsetCopyableStatel  s    z!SimpleLocalCache.setCopyableStatec                 C   s   | j S r*   )Úcheckr+   r$   r$   r%   ÚcheckMethodo  s    zSimpleLocalCache.checkMethodc                 C   s   | S r*   r$   r+   r$   r$   r%   Ú	checkSelfr  s    zSimpleLocalCache.checkSelfc                 C   s   dS rx   r$   r+   r$   r$   r%   rµ   u  s    zSimpleLocalCache.checkN)r'   r(   r)   r´   r¶   r·   rµ   r$   r$   r$   r%   r±   k  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 )NrB   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   rB   c                 C   s"   t  jd7  _t | ¡}d|_|S )NrB   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.
        )rZ   rS   r[   N)r   rq   rÛ   r[   rs   r_   r7   r>   ZsuccessResultOfÚgetRootObjectÚref)r"   rS   r6   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   rB   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¥   r>   r@   Ú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.
        rB   ré   c                    sF   ˆ  | t¡ ˆ | jd¡ ˆ tjd¡ ˆ tjd¡ ˆ | ˆ ¡ d S )NrŸ   r    rB   )rê   rÕ   rë   r—   rÖ   rÙ   ÚassertIsNotrí   rï   r$   r%   rñ     s
    z$NewStyleTests.test_alloc.<locals>.cb)	rÕ   rë   rÖ   rÙ   ræ   r¥   r>   r@   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   rB   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%   ÚclientConnectionMadeP  s    z2ConnectionNotifyServerFactory.clientConnectionMade)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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   Ú	localhostc                    s
   | ˆ _ d S r*   ©ræ   r  r+   r$   r%   ÚgotRooth  s    z*NewStyleCachedTests.setUp.<locals>.gotRoot)rÚ   rð   rÔ   r   Ú	listenTCPrÿ   rá   r7   r   rs   Ú
connectTCPÚgetHostÚportrå   ró   Úfactoryr   r   )r"   rS   r  Ú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.
        )r7   r  r  rF   ÚloseConnectionræ   rM   ÚstopListeningr+   r$   r$   r%   rè   o  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_newStyleCachew  s
    z&NewStyleCachedTests.test_newStyleCacheN)r'   r(   r)   rç   rè   r  r$   r$   r$   r%   r  Z  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|› �¡ d S )Nz&This should cause a return value, not ©Ú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|› �¡ d S )Nz This should cause an error, not r  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©rZ   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¥   r>   ÚassertIsr#  rë   ÚremoteMethod)	r"   r�   rÔ   r>   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thunkrB   zresult wasn't received.)rv   rw   r.  r/  ZexpectedThunkResultr¥   rÌ   r  r  r>   rë   r  )r"   r�   rÔ   r>   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|	› dt|ƒ› �¡ d S )	Nr"  r¢   é
   Ú	getSimplerB   ztransport was not closedz	expected z got )rv   r~   r.  r/  Úranger   ZMAX_BROKER_REFSrF   Úclosedr¥   rÌ   r¿   r>   Ú
assertTruerë   Úlen)
r"   ÚlÚer�   rÔ   r>   r¢   r…   ZignoÚ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getCopyrB   r‚   rƒ   r   rZ   )rv   r•   r.  r/  r¥   rÌ   r  r  r>   rë   r  r…   r†   r‡   )r"   r�   rÔ   r>   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observerB   r8  zdidn't notifyznotified too much)rv   r½   rÏ   r.  r/  r¥   r>   rÄ   ZassertIsNotNonerÅ   rë   )r"   r�   rÔ   r>   r(  r)  Ú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/  r>   r¥   ró   r¿   rì   rÈ   rÍ   re   rë   )r"   r�   rÔ   r>   rÍ   r?  Ú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¢   r9  é   )rv   r~   r.  r/  r¥   rÌ   r7  r  r>   r6  ÚluidÚassertInZlocalObjectsÚsysÚ
hexversionÚgcÚcollectÚassertNotIn)r"   r�   rÔ   r>   r¢   r4  Zrluidr$   r$   r%   Útest_refcount  s&    

 ÿ
zBrokerTests.test_refcountc                 C   s<  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 }|  | ¡ 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   rB   rŸ   r    r£   zpotential refcounting issuez!other potential refcounting issueZputCachezremote cache doesn't have itrH  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¿   Ú
addErrbackr@   rë   r…   r†   r¢   r¦   r=  ZassertIdenticalr¶   Ú__self__r·   r<  r1  rI  rJ  ZremotelyCachedObjectsrK  rL  rM  rN  rO  ZlocallyCachedObjectsÚassertIsNoner¤   )r"   r�   rÔ   r>   rÅ   Úobj2ZvccZo2Zo3ZcollÚcomplexÚcpZcol2rI  ZbaroqueLuidr$   r$   r%   Ú
test_cache4  sv    

  ÿ
  ÿ  ÿ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getPubrB   r   )r  r  r  rv   ÚGetPublisherr.  r/  r¥   rÌ   r¿   r  r@   rd   rë   ÚactivateCalledZisActivatedÚyayIGotPublishedZ_wasCleanWhenLoaded)r"   r�   rÔ   r>   r¢   r4  ÚaccumrÅ   r$   r$   r%   Útest_publishablev  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
› �¡ d S )Nr"  éc   r¢   Z
getFactoryz!ID not correct on factory object )rv   r•   r.  r/  r¥   rÌ   r^  r!  r>   rë   r  )r"   r�   rÔ   r>   ZIDrÅ   r…   r$   r$   r%   Útest_factoryCopy•  s    

ýzBrokerTests.test_factoryCopy)r'   r(   r)   r  rè   r  r  r   r!  r2  r5  r7  rA  rB  rD  rG  rP  rW  r\  r^  r`  r$   r$   r$   r%   r  ‘  s$   Br  sô  helloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldc                  O   s   | a |ad S r*   )ÚcallbackArgsÚcallbackKeyword)Úargsrß   r$   r$   r%   ÚfinishedCallback¬  s    rd  c                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú	Pagerizerc                 O   s   |||  | _ | _| _d S r*   )re   rc  rß   )r"   re   rc  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   rc  rß   ©r"   Ú	collectorr$   r$   r%   Úremote_getPages¶  s       ÿÿÿzPagerizer.remote_getPagesN)r'   r(   r)   r:   rj  r$   r$   r$   r%   re  ²  s   re  c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚFilePagerizerNc                 O   s    || _ |||  | _| _| _d S r*   )Úfilenamere   rc  rß   )r"   rl  re   rc  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Úopenrl  re   rc  rß   Úpagerrh  r$   r$   r%   rj  Ä  s     
 ÿÿÿzFilePagerizer.remote_getPages)r'   r(   r)   ro  r:   rj  r$   r$   r$   r%   rk  ½  s   rk  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)Úmktemprl  rn  Úwriterg  )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¢   Úhellor8  ©r—   ÚgetPagesr0   r   ú'Pages received not equal to pages sent!)ru  úCompleted callback not invokedr—   N)rv   r.  re  rd  r/  r   ÚgetAllPagesró   r¿   r>   rë   Újoinrg  ra  rb  ©r"   r�   rÔ   r>   r…   r>  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¢   Nrw  r0   r   rx  )rv   r.  re  r/  r   rz  ró   r¿   r>   rë   r{  rg  r|  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"  Nr4  rw  r8  r   rB   zgetAllPages timed outr0   rx  )rr  rn  Úcloserv   rk  r.  r/  r   rz  ró   r¿   r>   r  rë   r{  )	r"   ZfilenameEmptyr�   rÔ   r>   Ú	pagerizerr…   r>  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é	   rv  r4  rw  r0   r   rx  )rƒ  ry  r—   N)rv   rk  rl  rd  r.  r/  r   rz  ró   r¿   r>   rë   r{  rg  ra  rb  ro  Úchunks©r"   r�   rÔ   r>   r�  r…   r>  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"  Nr4  rw  r0   r   rx  )rv   rk  rl  r.  r/  r   rz  ró   r¿   r>   rë   r{  rg  ro  r…  r†  r$   r$   r%   Útest_filePagingWithoutCallback%  s    

  ÿz*PagingTests.test_filePagingWithoutCallbackN)
r'   r(   r)   r-   rç   r}  r~  r‚  r‡  rˆ  r$   r$   r$   r%   rp  Ë  s   rp  c                   @   s   e Zd Zdd„ ZdS )ÚDumbPublishablec                 C   s   ddiS )NrZ  rB   r$   r+   r$   r$   r%   ÚgetStateToPublish8  s    z!DumbPublishable.getStateToPublishN)r'   r(   r)   rŠ  r$   r$   r$   r%   r‰  7  s   r‰  c                   @   s   e Zd Zdd„ ZdS )ÚDumbPubc                 C   s
   d| _ d S rx   )rY  r+   r$   r$   r%   Ú	activated=  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 )rX  c                 C   s   t dƒ| _d S )NZTESTING)r‰  Úpubr+   r$   r$   r%   r:   B  s    zGetPublisher.__init__c                 C   s   | j S r*   )r�  r+   r$   r$   r%   Úremote_getPubE  s    zGetPublisher.remote_getPubN)r'   r(   r)   r:   rŽ  r$   r$   r$   r%   rX  A  s   rX  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|› �ƒ‚d S )NzI shouldn't have been called: ©r‘   )r"   rc  r$   r$   r%   r  Q  s    zDisconnectionTests.errorc                 C   s
   d| _ dS )z.
        Called on broker disconnect.
        rB   N)ÚgotCallbackr+   r$   r$   r%   ÚgotDisconnectedT  s    z"DisconnectionTests.gotDisconnectedc                 C   s   |   || j¡ d| _dS )z7
        Called on RemoteReference disconnect.
        rB   N)rë   ÚremoteObjectÚobjectCallback©r"   Úor$   r$   r%   ÚobjectDisconnectedZ  s    z%DisconnectionTests.objectDisconnectedc                 C   sf   t | d�\}}}| ¡  | dtƒ ¡ | d¡}g }| dtƒ ¡ |j¡ | 	¡  |  
t|ƒd¡ d S )Nr"  r–  Z
setBadCopyrB   )rv   r>   r.  Ú
BadCopySetr/  r¥   ÚBadCopyablerQ  r¿   r@   rë   r=  )r"   r�   rÔ   r>   Úgr>  r$   r$   r%   Útest_badSerializationa  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   r>   r.  rw   r/  ÚnotifyOnDisconnectr  rJ  ZdisconnectsZdontNotifyOnDisconnectrO  Z_disconnectedZdisconnectCallbacksr’  r—  r“  rJ   r   rK   r   rL   r<  r‘  r”  )r"   r�   rÔ   r>   Úrr$   r$   r%   Útest_disconnectionk  s.    
z%DisconnectionTests.test_disconnectionN)	r'   r(   r)   r-   r  r’  r—  r›  rž  r$   r$   r$   r%   r�  L  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_add1rB   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   rB   )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*   ©r2   ©r"   r2   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)   r-   Ú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%   r/   è  r0   z)TestRealm.requestAvatar.<locals>.<lambda>TN)
r   r1   rÊ   r   Z	ANONYMOUSrÛ   ÚperspectiveFactoryÚlastPerspectiver²  r±  ©r"   r2   r3   Ú	interfacer$   r$   r%   r4   ß  s    
zTestRealm.requestAvatar)r'   r(   r)   r-   r§  rµ  r¶  r4   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$   ©r3   Zperspr$   r%   r/     r0   z*LeakyRealm.requestAvatar.<locals>.<lambda>)r»  rµ  r   r1   r·  r$   r¼  r%   r4     s    

zLeakyRealm.requestAvatarN)r'   r(   r)   r-   r:   r4   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)r3   r+   r$   r%   Ú
setMindRef  s    z4NewCredLeakTests.test_logoutLeak.<locals>.setMindRef)rZ   rt   r  Úloginro   c                 S   s$   | \}}t ƒ }| dt |d¡|¡S )NÚrespondro   )rw   r¥   r   rÂ  )r…   Ú	challengeZ
challengerr3   r$   r$   r%   Ú
cbResponse$  s     
 ÿz4NewCredLeakTests.test_logoutLeak.<locals>.cbResponsec                    s*   ˆ  ¡  ˆ  d¡ ˆ t tdƒ¡¡ d S )NrB   Zboom)rA   r¿   rJ   r   rK   r‘   )Ú_)ÚconnectionBrokenr>   r^   r$   r%   rJ   -  s    
z8NewCredLeakTests.test_logoutLeak.<locals>.connectionLostrB   )r¿  rv   rº  r/  r¥   ró   r@   rë   rM  rN  rS  )r"   rÀ  r]   r  rÍ   rÄ  rJ   r$   )rÆ  r>   r"   r^   r%   Útest_logoutLeak  s"     ÿ


z NewCredLeakTests.test_logoutLeakN)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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   rS   r+   r$   r$   r%   rç   D  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TCPrH   iá  N)r_   rS   r[   r6   r7   r>   r`   ZconnectorStaterg   r   ÚIPv4AddressrO   rc   ró   ÚestablishClientAndServer)r"   Z_ignoredr$   r$   r%   rÊ  O  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)r<  r6   rF   r;  rJ   rS   rQ   rO   )r"   rP   r$   r$   r%   ÚcompleteClientLostConnectiono  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   rS   Ú
disconnectrË  )rÎ  ÚdisconnectedDeferredr+   r$   r%   rÐ  �  s
    
z3NewCredTests.test_getRootObject.<locals>.disconnect©rÊ  rS   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)ÚassertRaisesr   ZDeadReferenceErrorr¥   )Úign)rÎ  r"   r$   r%   ÚlostConnectionª  s    zSNewCredTests.test_deadReferenceError.<locals>.gotRootObject.<locals>.lostConnection)r   rœ  re   ró   rS   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   rQ   rm   )r"   rO   rP   Zreconnectingr  r$   r$   r%   rQ   À  s       ÿzRNewCredTests.test_clientConnectionLost.<locals>.ReconnectOnce.clientConnectionLostN)r'   r(   r)   rÚ  rQ   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   rS   rÐ  rË  )ZanotherRootObjrÍ   r+   r$   r%   ÚgotAnotherRootObjectÛ  s    
zqNewCredTests.test_clientConnectionLost.<locals>.gotRootObject.<locals>.disconnected.<locals>.gotAnotherRootObject)rS   rå   ró   )r×  rÍ   rÜ  r+   r$   r%   rI   Ø  s    
zSNewCredTests.test_clientConnectionLost.<locals>.gotRootObject.<locals>.disconnected)
rê   r   rÌ  r   rœ  re   rS   rÐ  rË  ró   )rÎ  rÍ   rI   r+   r$   r%   rÏ  Ï  s    
z=NewCredTests.test_clientConnectionLost.<locals>.gotRootObject)r   rs   rS   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.
        )rH   i90  N)rÊ  r[   rU   rW   r   rX   rV   rJ   r   rK   r   rL   )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   rK   r   ÚassertFailure)r"   rS   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*   )r<  rt   r¶  r²  rê   r   rÌ  ©r¨   ©ZloginCompletedr"   r$   r%   ÚcbLogin  s    z.NewCredTests.test_loginLogout.<locals>.cbLoginc                    s   ˆ j  ¡  ˆ  ¡  d S r*   )rS   rÐ  rË  ©Úignoredr+   r$   r%   ÚcbDisconnect  s    
z3NewCredTests.test_loginLogout.<locals>.cbDisconnectc                    s   ˆ   ˆ jjj¡ d S r*   )r<  rt   r¶  r°  rê  r+   r$   r%   ÚcbLogout&  s    z/NewCredTests.test_loginLogout.<locals>.cbLogoutN)r	   ÚregisterCheckerr   rp   r   rá  r   rS   rÁ  ró   rÊ  r>   r@   rM  rN  re   )r"   Úcredsr3   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:   C  s    zFNewCredTests.test_logoutAfterDecref.<locals>.EventPerspective.__init__c                    s   ˆ   d ¡ d S r*   )re   r+   r¯  r$   r%   r±  F  s    zDNewCredTests.test_logoutAfterDecref.<locals>.EventPerspective.logoutN)r'   r(   r)   r-   r:   r±  r$   r¯  r$   r%   ÚEventPerspective>  s   rñ  rà  r®   rß  r´  c                    s   ˆ S r*   r$   )Zavatarr¯  r$   r%   Ú
cbLoggedInS  s    z7NewCredTests.test_logoutAfterDecref.<locals>.cbLoggedInc                    s   ˆ   ˆ jjji ¡ d S r*   )rë   r[   r  Z_localCleanuprê  r+   r$   r%   ÚcbLoggedOutZ  s    z8NewCredTests.test_logoutAfterDecref.<locals>.cbLoggedOut)r   r   ÚAvatarrt   rµ  r	   rî  r   rp   rS   rÁ  r   rá  ró   rÊ  r>   r@   rM  rN  )r"   rñ  rÍ   rò  ró  r$   )r°  r"   r%   Útest_logoutAfterDecref6  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   rS   rÁ  r   rá  r   ró   rÊ  r>   r@   )r"   Ú
firstLoginÚsecondLoginrÍ   rò  rú  r$   r+   r%   Útest_concurrentLoginl  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ë   r=  ©ÚignoreÚerrorsr+   r$   r%   Úcleanup¥  s    
z;NewCredTests.test_badUsernamePasswordLogin.<locals>.cleanup)r	   rî  r   rp   rS   rÁ  r   rá  râ  r
   r   ró   rÊ  r>   r@   )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   ÚAllowAnonymousAccessrS   rÁ  r   Ú	Anonymousró   rë   rÊ  r>   r@   )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ë   r=  rÿ  r+   r$   r%   r  Ï  s    
z=NewCredTests.test_anonymousLoginNotPermitted.<locals>.cleanup)r	   rî  r   rp   rS   rÁ  r   r  râ  r   ró   rÊ  r>   r@   ©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   rS   rÁ  r   r  ró   rë   rÊ  r>   r@   ©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Úaddrf  é   r&  rç  r$   r$   r%   ré  þ  s    zINewCredTests.test_authenticatedLoginWithMultipleCheckers.<locals>.cbLoginr  )r	   rî  r   r  rp   rS   rÁ  r   rá  ró   rë   rÊ  r>   r@   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   rS   rÁ  r   rá  ró   r<  rÊ  r>   r@   )r"   rÍ   ré  r  r$   r$   r%   Ú	test_view
  s    
ÿ
 ÿ


zNewCredTests.test_view)N)r'   r(   r)   r-   rç   rÊ  r   rK   r   rL   rË  rÔ  rÙ  rÝ  rÞ  rã  rð  rõ  rý  r  r  r  r  r  r  r$   r$   r$   r%   rÈ  ?  s&   
!
ÿ
3
16%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unserializeÚ	serialize)r"   rM   Úmessagerc  Úkwargsr$   r$   r%   ÚperspectiveMessageReceived.  s    z4NonSubclassingPerspective.perspectiveMessageReceivedc                 C   s
   d| _ d S r®  r¯  r+   r$   r$   r%   r±  4  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   rH   )r¸  )r³  rt   r  rµ  r	   rr   r   rp   ru   ZaddUserrî  r   r   rq   r  r   r  r  rY   r  r
  Úportnor+   r$   r$   r%   rç   >  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´  rH   c                 S   s   | j dddd�S )NÚANYTHINGÚhererö  )r4  r&  )r   r$   r$   r%   r/   R  r0   z#NSPTests.test_NSP.<locals>.<lambda>r  )r  r4  rö  c                    s$   ˆ   ¡  ˆjjD ]}|j ¡  qd S r*   )rÐ  r  Z	protocolsrF   r  )rë  r   ©r  r"   r$   r%   r  U  s    z"NSPTests.test_NSP.<locals>.cleanup)
r   rs   rÁ  r   rá  r   r	  r  ró   rë   r
  r$   r  r%   Útest_NSPJ  s    
zNSPTests.test_NSPN)r'   r(   r)   r-   rç   r  r$   r$   r$   r%   r  8  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%   Ú	forwardMec  s    zIForwarded.forwardMec                   C   s   dS )z-
        Simple asynchronous method.
        Nr$   r$   r$   r$   r%   ÚforwardDeferredh  s    zIForwarded.forwardDeferredN)r'   r(   r)   r-   r  r   r$   r$   r$   r%   r  ^  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  |  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%   ÚdontForwardMe‚  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)   r-   r"  r#  r  r$  r   r$   r$   r$   r%   r!  n  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†   rE  )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   rK   Ztrapr‘   )rt  r+   r$   r%   Úeb°  s    z*SpreadUtilTests.test_asyncFail.<locals>.ebc                    s
   ˆ   d¡S )Nzsupposed to failr  rí   r+   r$   r%   r/   ´  r0   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£  r1  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   rB   N)r!  r   ZLocalAsyncForwarderr  r¥   r<  r"  rì   r#  ró   r¿   rë   )r"   rt  ÚlfÚrrr>  r$   r$   r%   Útest_localAsyncForwarder¿  s    


z(SpreadUtilTests.test_localAsyncForwarderN)	r'   r(   r)   r-   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   rU   r0  Úsecurityr   ÚglobalSecurity©r"   r  rM   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Û   rU   r0  r1  r   r2  r3  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.
        ©r1  N)r   ÚSecurityOptionsr   rs   rU   r0  r1  ©r"   r1  r  rM   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.
        r6  N)r   r7  r   rq   rÛ   rU   r0  r1  r8  r$   r$   r%   Ú test_serverSecurityCustomizationñ  s    
z;PBWithSecurityOptionsTests.test_serverSecurityCustomizationN)r'   r(   r)   r-   r4  r5  r9  r:  r$   r$   r$   r%   r0  Ð  s
   		
r0  )N)�r-   rM  r  rK  r=   r¾  Úcollectionsr   Úior   rV   Útypingr   Zzope.interfacer   r   Ztwisted.credr   r   r	   Ztwisted.cred.errorr
   r   Ztwisted.internetr   r   r   r   Ztwisted.internet.deferr   r   r   Ztwisted.internet.errorr   Ztwisted.internet.testingr   Ztwisted.protocols.policiesr   Ztwisted.pythonr   r   Ztwisted.python.compatr   Ztwisted.spreadr   r   r   r   Ztwisted.trialr   ZViewabler   rô  r    ZIRealmr.   r5   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  rg  ra  rb  rd  re  rk  rp  ZPublishabler‰  ZRemotePublishedr‹  rX  r�  r|   rŸ  r™  r˜  ZLocalAsRemoter£  r1   r§  r³  r«  rº  r½  rÈ  r  r  r  r!  r%  r0  r$   r$   r$   r%   Ú<module>   sÂ   	;3!
	
R 7  lC#!6   l&!@