a
    )¶(b8÷  ã                   @   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%m&Z& ddl'm(Z( ddl)m*Z*m+Z+m,Z,m-Z- ddl.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 dd‚„ 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)ÚWrappingFactory)ÚfailureÚlog)Ú	iterbytes)ÚjellyÚpbÚpublishÚutil)Ú_FakeConnector)Ú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$   ú:lib/python3.9/site-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_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u 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"   Z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sDataZbyter$   r$   r%   r>   a   s"    



zIOPump.pumpN)r'   r(   r)   r-   r:   r?   r@   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   rI   r   rJ   r$   ©ÚclientFactoryr$   r%   ÚdisconnectClientFactory   s    ÿz7connectServerAndClient.<locals>.disconnectClientFactory)ÚbuildProtocolÚStringIOÚmakeConnectionr   ÚFileWrapperr5   Ú
addCleanupr>   )ÚtestrQ   ÚserverFactoryZaddrÚclientBrokerÚserverBrokerZclientTransportZserverTransportr>   rL   rR   r$   rP   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   r^   Ú
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)r^   ÚpopÚcallbackr+   r$   r$   r%   Ú	notifyAllÇ   s    z)_ReconnectingFakeConnectorState.notifyAllN)r'   r(   r)   r-   r:   r`   rc   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)re   Úconnectrf   rc   r+   rh   r$   r%   rj   á   s    
z"_ReconnectingFakeConnector.connect)r'   r(   r)   r-   r:   rj   Ú__classcell__r$   r$   rh   r%   rd   Ï   s   rd   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\   )rX   ÚrealmÚcheckerrY   rQ   r$   r$   r%   ÚconnectedServerAndClienté   s
    
rs   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚSimpleRemotec                 C   s   || _ |d S ©NrA   )Úarg©r"   rv   r$   r$   r%   Úremote_thunkù   s    zSimpleRemote.remote_thunkc                 C   s
   t ƒ ‚d S r*   )Ú	Exceptionrw   r$   r$   r%   Úremote_knuthý   s    zSimpleRemote.remote_knuthN)r'   r(   r)   rx   rz   r$   r$   r$   r%   rt   ø   s   rt   c                   @   s   e Zd Zdd„ ZdS )ÚNestedRemotec                 C   s   t ƒ S r*   )rt   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 ©NrA   ÚHelloÚWorldrX   ©Ú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u r&tdt| ƒ› ƒ‚|tjv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‰   )rg   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 )NrA   é   é   ©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"   rg   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 ru   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 |u 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 ru   rÄ   rw   r$   r$   r%   ÚrunMeš  s    zDeferredRemote.runMec                 C   s   dsJ dƒ‚d S )Nr   zshouldn't have been run!r$   rw   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 )NrA   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   rA   c                 C   s"   t  jd7  _t | ¡}d|_|S )NrA   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 ru   )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.
        )rX   rQ   rY   N)r   rn   r×   rY   rp   r\   r7   r>   ZsuccessResultOfÚgetRootObjectÚref)r"   rQ   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   rA   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¡ ˆ | ˆ u ¡ 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.
        rA   rå   c                    sF   ˆ  | t¡ ˆ | jd¡ ˆ tjd¡ ˆ tjd¡ ˆ | ˆ ¡ d S )Nrœ   r   rA   )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   rA   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   rn   r:   r   ÚconnectionMade)r"   Úrootr$   r$   r%   r:   I  s    z&ConnectionNotifyServerFactory.__init__c                 C   s*   || _ | jd }| _|dur&| d¡ dS )zF
        Store the protocol and fire the connection deferred.
        N)ÚprotocolInstancerü   rb   )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   Z	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   rp   Ú
connectTCPÚgetHostÚportrá   rï   Úfactoryrü   r   )r"   rQ   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.
        )r7   r  rþ   rE   ÚloseConnectionrâ   rK   Ú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y    Y n0 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©rX   c                   @   s   e Zd Zdd„ ZdS )z%BrokerTests.test_reference.<locals>.Xc                 S   s
   || _ d S r*   )Úcaughtrw   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)rs   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thunkrA   zresult wasn't received.)rs   rt   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Ÿ   é
   Ú	getSimplerA   ztransport was not closedz	expected z got )rs   r{   r(  r)  Úranger   ZMAX_BROKER_REFSrE   Úclosedr¢   rÈ   r¼   r>   Ú
assertTruerç   Úlen)
r"   ÚlÚerš   rÐ   r>   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getCopyrA   r   r€   r   rX   )rs   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observerA   r2  zdidn't notifyznotified too much)rs   rº   rË   r(  r)  r¢   r>   rÁ   ZassertIsNotNonerÂ   rç   )r"   rš   rÐ   r>   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.)rs   rÃ   r(  r)  r>   r¢   rï   r¼   rè   rÅ   rÉ   rb   rç   )r"   rš   rÐ   r>   rÉ   r9  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Ÿ   r3  é   )rs   r{   r(  r)  r¢   rÈ   r1  r  r>   r0  ÚluidÚassertInZlocalObjectsÚsysÚ
hexversionÚgcÚcollectÚassertNotIn)r"   rš   rÐ   r>   rŸ   r.  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   rA   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)"rs   rµ   r˜   rš   r(  r)  r¢   rï   r¼   Ú
addErrbackr?   rç   r‚   rƒ   rŸ   r£   r7  ZassertIdenticalr³   Ú__self__r´   r6  r+  r@  rA  ZremotelyCachedObjectsrB  rC  rD  rE  rF  ZlocallyCachedObjectsÚassertIsNoner¡   )r"   rš   rÐ   r>   rÂ   Zobj2ZvccZo2Zo3ZcollÚcomplexZcpZcol2r@  ZbaroqueLuidr$   r$   r%   Ú
test_cache4  sj    

ÿ
ÿÿzBrokerTests.test_cachec                 C   s  zt  d¡ W n ty    Y n0 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getPubrA   r   )r  r  r  rs   ÚGetPublisherr(  r)  r¢   rÈ   r¼   r  r?   ra   rç   ÚactivateCalledZisActivatedÚyayIGotPublishedZ_wasCleanWhenLoaded)r"   rš   rÐ   r>   rŸ   r.  Z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 )rs   r’   r(  r)  r¢   rÈ   rR  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  r,  r/  r1  r:  r;  r<  r>  rG  rL  rP  rR  rT  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    rX  c                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú	Pagerizerc                 O   s   |||  | _ | _| _d S r*   )rb   rW  rÛ   )r"   rb   rW  rÛ   r$   r$   r%   r:   ³  s    zPagerizer.__init__c                 C   s4   t j|td| jg| j¢R i | j¤Ž d  | _| _d S )Néd   )r   ZStringPagerÚ	bigStringrb   rW  rÛ   ©r"   Z	collectorr$   r$   r%   Úremote_getPages¶  s    
ÿÿÿzPagerizer.remote_getPagesN)r'   r(   r)   r:   r]  r$   r$   r$   r%   rY  ²  s   rY  c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚFilePagerizerNc                 O   s    || _ |||  | _| _| _d S r*   )Úfilenamerb   rW  rÛ   )r"   r_  rb   rW  rÛ   r$   r$   r%   r:   À  s    zFilePagerizer.__init__c                 C   s<   t j|t| jdƒ| jg| j¢R i | j¤Ž| _d  | _| _d S )NÚrb)r   Z	FilePagerÚopenr_  rb   rW  rÛ   Úpagerr\  r$   r$   r%   r]  Ä  s    ÿÿÿzFilePagerizer.remote_getPages)r'   r(   r)   rb  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   sD   |   ¡ | _t| jdƒ}| t¡ W d  ƒ n1 s60    Y  dS )z?
        Create a file used to test L{util.FilePager}.
        ÚwbN)Úmktempr_  ra  Ú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Ÿ   Úhellor2  ©r”   ÚgetPagesr0   r   ú'Pages received not equal to pages sent!)rh  úCompleted callback not invokedr”   N)rs   r(  rY  rX  r)  r   ÚgetAllPagesrï   r¼   r>   rç   Újoinr[  rU  rV  ©r"   rš   rÐ   r>   r‚   r8  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Ÿ   Nrj  r0   r   rk  )rs   r(  rY  r)  r   rm  rï   r¼   r>   rç   rn  r[  ro  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  Nr.  rj  r2  r   rA   zgetAllPages timed outr0   rk  )re  ra  Úclosers   r^  r(  r)  r   rm  rï   r¼   r>   r  rç   rn  )	r"   ZfilenameEmptyrš   rÐ   r>   Ú	pagerizerr‚   r8  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é	   ri  r.  rj  r0   r   rk  )rv  rl  r”   N)rs   r^  r_  rX  r(  r)  r   rm  rï   r¼   r>   rç   rn  r[  rU  rV  rb  Úchunks©r"   rš   rÐ   r>   rt  r‚   r8  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  Nr.  rj  r0   r   rk  )rs   r^  r_  r(  r)  r   rm  rï   r¼   r>   rç   rn  r[  rb  rx  ry  r$   r$   r%   Útest_filePagingWithoutCallback%  s    

ÿz*PagingTests.test_filePagingWithoutCallbackN)
r'   r(   r)   r-   rã   rp  rq  ru  rz  r{  r$   r$   r$   r%   rc  Ë  s   rc  c                   @   s   e Zd Zdd„ ZdS )ÚDumbPublishablec                 C   s   ddiS )NrO  rA   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 ru   )rN  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 )rM  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%   rM  A  s   rM  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"   rW  r$   r$   r%   r  Q  s    zDisconnectionTests.errorc                 C   s
   d| _ dS )z.
        Called on broker disconnect.
        rA   N)ÚgotCallbackr+   r$   r$   r%   ÚgotDisconnectedT  s    z"DisconnectionTests.gotDisconnectedc                 C   s   |   || j¡ d| _dS )z7
        Called on RemoteReference disconnect.
        rA   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
setBadCopyrA   )rs   r>   r(  Ú
BadCopySetr)  r¢   ÚBadCopyablerH  r¼   r?   rç   r7  )r"   rš   rÐ   r>   Úgr8  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‰  )rs   r>   r(  rt   r)  ÚnotifyOnDisconnectr  rA  ZdisconnectsZdontNotifyOnDisconnectrF  Z_disconnectedZdisconnectCallbacksr…  rŠ  r†  rH   r   rI   r   rJ   r6  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 ru   r$   )r"   r‚   r$   r$   r%   Ú	sync_add1   s    zLocalRemoteTest.sync_add1rA   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   rA   )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J ‚|dksJ ‚|tju 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   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)rX   rq   rý   Úloginrl   c                 S   s$   | \}}t ƒ }| dt |d¡|¡S )NÚrespondrl   )rt   r¢   r   rµ  )r‚   Z	challengeZ
challengerr3   r$   r$   r%   Ú
cbResponse$  s
    ÿz4NewCredLeakTests.test_logoutLeak.<locals>.cbResponsec                    s*   ˆ  ¡  ˆ  d¡ ˆ t tdƒ¡¡ d S )NrA   Zboom)r@   r¼   rH   r   rI   rŽ   )Ú_)ÚconnectionBrokenr>   r[   r$   r%   rH   -  s    
z8NewCredLeakTests.test_logoutLeak.<locals>.connectionLostrA   )r²  rs   r­  r)  r¢   rï   r?   rç   rD  rE  rJ  )r"   r³  rZ   rý   rÉ   r¶  rH   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¦  rq   r	   ro   rû   rY   r   rp   rQ   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TCPrF   iá  N)r\   rQ   rY   r6   r7   r>   r]   ZconnectorStaterd   r   ZIPv4AddressrM   r`   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)r6  r6   rE   r5  rH   rQ   rO   rM   )r"   rN   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  rb   rQ   Ú
disconnectr¼  )r¿  ÚdisconnectedDeferredr+   r$   r%   rÁ    s
    
z3NewCredTests.test_getRootObject.<locals>.disconnect©r»  rQ   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  rb   rï   rQ   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   rp   rO   rj   )r"   rM   rN   Zreconnectingr  r$   r$   r%   rO   À  s    ÿzRNewCredTests.test_clientConnectionLost.<locals>.ReconnectOnce.clientConnectionLostN)r'   r(   r)   rÊ  rO   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  rb   rQ   rÁ  r¼  )ZanotherRootObjrÉ   r+   r$   r%   ÚgotAnotherRootObjectÛ  s    
zqNewCredTests.test_clientConnectionLost.<locals>.gotRootObject.<locals>.disconnected.<locals>.gotAnotherRootObject)rQ   rá   rï   )rÇ  rÉ   rÌ  r+   r$   r%   rG   Ø  s    
zSNewCredTests.test_clientConnectionLost.<locals>.gotRootObject.<locals>.disconnected)
ræ   r   r½  r   r  rb   rQ   rÁ  r¼  rï   )r¿  rÉ   rG   r+   r$   r%   rÀ  Ï  s    
z=NewCredTests.test_clientConnectionLost.<locals>.gotRootObject)r   rp   rQ   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.
        )rF   i90  N)r»  rY   rS   rU   r   rV   rT   rH   r   rI   r   rJ   )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   rp   r´  r   ÚUsernamePasswordZclientConnectionFailedr   rI   r   ÚassertFailure)r"   rQ   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*   )r6  rq   r©  r¥  ræ   r   r½  ©r¥   ©ZloginCompletedr"   r$   r%   ÚcbLogin  s    z.NewCredTests.test_loginLogout.<locals>.cbLoginc                    s   ˆ j  ¡  ˆ  ¡  d S r*   )rQ   rÁ  r¼  ©Úignoredr+   r$   r%   ÚcbDisconnect  s    
z3NewCredTests.test_loginLogout.<locals>.cbDisconnectc                    s   ˆ   ˆ jjj¡ d S r*   )r6  rq   r©  r£  rÚ  r+   r$   r%   ÚcbLogout&  s    z/NewCredTests.test_loginLogout.<locals>.cbLogoutN)r	   ÚregisterCheckerr   rm   r   rÑ  r   rQ   r´  rï   r»  r>   r?   rD  rE  rb   )r"   Z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*   )rb   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ç   rY   rþ   Z_localCleanuprÚ  r+   r$   r%   ÚcbLoggedOutZ  s    z8NewCredTests.test_logoutAfterDecref.<locals>.cbLoggedOut)r   r   ÚAvatarrq   r¨  r	   rÞ  r   rm   rQ   r´  r   rÑ  rï   r»  r>   r?   rD  rE  )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   rm   rQ   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ç   r7  ©ÚignoreÚerrorsr+   r$   r%   Úcleanup¥  s    
z;NewCredTests.test_badUsernamePasswordLogin.<locals>.cleanup)r	   rÞ  r   rm   rQ   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   ÚAllowAnonymousAccessrQ   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 ru   )rí  r   rç   r7  rî  r+   r$   r%   rñ  Ï  s    
z=NewCredTests.test_anonymousLoginNotPermitted.<locals>.cleanup)r	   rÞ  r   rm   rQ   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õ  rm   rQ   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ÚaddrZ  é   r   r×  r$   r$   r%   rÙ  þ  s    zINewCredTests.test_authenticatedLoginWithMultipleCheckers.<locals>.cbLoginrô  )r	   rÞ  r   rõ  rm   rQ   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   rm   rQ   r´  r   rÑ  rï   r6  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   rI   r   rJ   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unserializeZ	serialize)r"   rK   ÚmessagerW  Ú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   rF   )r«  )r¦  rq   r  r¨  r	   ro   r   rm   rr   ZaddUserrÞ  r   r   rn   r  r   r  r  rW   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§  rF   c                 S   s   | j ddddS )NÚANYTHINGÚhererå  )r.  r   )r“  r$   r$   r%   r/   R  r0   z#NSPTests.test_NSP.<locals>.<lambda>r  )r	  r.  rå  c                    s$   ˆ   ¡  ˆjjD ]}|j ¡  qd S r*   )rÁ  r  Z	protocolsrE   r  )rÛ  r“  ©r  r"   r$   r%   rñ  U  s    z"NSPTests.test_NSP.<locals>.cleanup)
r   rp   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ƒ   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   rI   ZtraprŽ   )rg  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–  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   rA   N)r  r   ZLocalAsyncForwarderr  r¢   r6  r  rè   r  rï   r¼   rç   )r"   rg  ZlfZrrr8  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   rp   rS   r*  Úsecurityr   ÚglobalSecurity©r"   r  rK   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   rn   r×   rS   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   rp   rS   r*  r  ©r"   r  r  rK   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   rn   r×   rS   r*  r  r$  r$   r$   r%   Ú test_serverSecurityCustomizationñ  s    
z;PBWithSecurityOptionsTests.test_serverSecurityCustomizationN)r'   r(   r)   r-   r   r!  r%  r&  r$   r$   r$   r%   r  Ð  s
   		
r  )N)r-   rD  r  rB  r=   r±  Úcollectionsr   Úior   rT   Ú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.protocols.policiesr   Ztwisted.pythonr   r   Ztwisted.python.compatr   Ztwisted.spreadr   r   r   r   Ztwisted.test.proto_helpersr   Ztwisted.trialr   ZViewabler   rã  r    ZIRealmr.   r5   r\   r]   rd   rs   r'  rt   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à   rn   rû   ZTestCaser   r  r[  rU  rV  rX  rY  r^  rc  ZPublishabler|  ZRemotePublishedr~  rM  r‚  ry   r’  rŒ  r‹  ZLocalAsRemoter–  r1   rš  r¦  rž  r­  r°  rº  r  r  r  r  r  r  r$   r$   r$   r%   Ú<module>   sÂ   	;3!
	
R 7  lC#!6   l&!@