o
    ¯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 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)ÚWrappingFactory)ÚfailureÚlog)Ú	iterbytes)ÚjellyÚpbÚpublishÚutil)Ú_FakeConnector)Úunittestc                   @   ó   e Zd Zdd„ ZdS )ÚDummyc                 C   s   t |tƒrdS dS )Nzhello world!zgoodbye, cruel world!)Ú
isinstanceÚDummyPerspective©ÚselfÚuser© r%   ú=/usr/lib/python3/dist-packages/twisted/spread/test/test_pb.pyÚview_doNothing(   s   
zDummy.view_doNothingN)Ú__name__Ú
__module__Ú__qualname__r'   r%   r%   r%   r&   r   '   ó    r   c                   @   ó   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   ó   t ƒ S ©N)r   ©r#   r%   r%   r&   Úperspective_getDummyViewPoint4   ó   z.DummyPerspective.perspective_getDummyViewPointN)r(   r)   r*   Ú__doc__r0   r%   r%   r%   r&   r!   /   ó    r!   c                   @   r   )Ú
DummyRealmc                 G   s.   |D ]}|t ju r|t|ƒdd„ f  S qd S )Nc                   S   ó   d S r.   r%   r%   r%   r%   r&   Ú<lambda>=   ó    z*DummyRealm.requestAvatar.<locals>.<lambda>)r   ÚIPerspectiver!   )r#   ÚavatarIdÚmindÚ
interfacesÚifacer%   r%   r&   ÚrequestAvatar:   s
   
ÿÿzDummyRealm.requestAvatarN)r(   r)   r*   r=   r%   r%   r%   r&   r4   8   s    r4   c                   @   ó0   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#   r@   rA   rB   rC   r%   r%   r&   Ú__init__G   s   
zIOPump.__init__c                 C   sN   d| _ t ¡ d }| j s!|  ¡ r#t ¡ |krdS | j s%|  ¡ sdS dS dS 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   ó
   d| _ dS )zd
        Stop a running L{flush} operation, even if data remains to be
        transferred.
        TN)rF   r/   r%   r%   r&   ÚstopZ   ó   
zIOPump.stopc                 C   s´   | j  d¡ | j d¡ | j  ¡ }| j ¡ }| j  d¡ | j d¡ | j  ¡  | j ¡  | jj ¡  | jj ¡  t	|ƒD ]}| j 
|¡ q<t	|ƒD ]}| j 
|¡ qI|sV|rXdS dS )zX
        Move data back and forth.

        Returns whether any data was moved.
        r   é   )rB   ÚseekrC   ÚreadÚtruncater@   Ú	transportÚ_checkProducerrA   r   ÚdataReceived)r#   ÚcDataÚsDataÚbyter%   r%   r&   rH   a   s"   



zIOPump.pumpN)r(   r)   r*   r2   rD   rJ   rL   rH   r%   r%   r%   r&   r?   @   s    r?   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 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   r[   r   r\   r%   ©ÚclientFactoryr%   r&   ÚdisconnectClientFactory�   s   
ÿz7connectServerAndClient.<locals>.disconnectClientFactory)ÚbuildProtocolÚStringIOÚmakeConnectionr   ÚFileWrapperr?   Ú
addCleanuprH   )Útestrc   ÚserverFactoryÚaddrÚclientBrokerÚserverBrokerÚclientTransportÚserverTransportrH   r^   rd   r%   rb   r&   ÚconnectServerAndClient{   s   

	

rq   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   ó   t ƒ | _d S r.   )r   Únotificationsr/   r%   r%   r&   rD   ·   ó   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   rt   Ú
appendleft)r#   Únotifierr%   r%   r&   ÚnotifyOnConnectº   s   	z/_ReconnectingFakeConnectorState.notifyOnConnectc                 C   s$   | j r| j  ¡  | ¡ | j sdS dS )z1
        Fire all pending notifications.
        N)rt   ÚpopÚcallbackr/   r%   r%   r&   Ú	notifyAllÇ   s   ÿz)_ReconnectingFakeConnectorState.notifyAllN)r(   r)   r*   r2   rD   rx   r{   r%   r%   r%   r&   rr   ®   s
    rr   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)ÚsuperrD   Ú_state)r#   r   Ústate©Ú	__class__r%   r&   rD   Õ   s   	
z#_ReconnectingFakeConnector.__init__c                    s   t ƒ  ¡  | j ¡  dS )zM
        A C{connect} implementation that calls C{reconnectCallback}
        N)r}   Úconnectr~   r{   r/   r€   r%   r&   r‚   á   s   
z"_ReconnectingFakeConnector.connect)r(   r)   r*   r2   rD   r‚   Ú__classcell__r%   r%   r€   r&   r|   Ï   s    r|   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)Úguest)	r4   r   Ú'InMemoryUsernamePasswordDatabaseDontUser   ÚPBServerFactoryr	   ÚPortalÚPBClientFactoryrq   )rj   ÚrealmÚcheckerrk   rc   r%   r%   r&   ÚconnectedServerAndClienté   s
   
rŒ   c                   @   ó   e Zd Zdd„ Zdd„ ZdS )ÚSimpleRemotec                 C   ó   || _ |d S ©NrN   )Úarg©r#   r‘   r%   r%   r&   Úremote_thunkù   ó   zSimpleRemote.remote_thunkc                 C   ó   t ƒ ‚r.   )Ú	Exceptionr’   r%   r%   r&   Úremote_knuthý   r1   zSimpleRemote.remote_knuthN)r(   r)   r*   r“   r—   r%   r%   r%   r&   rŽ   ø   s    rŽ   c                   @   r   )ÚNestedRemotec                 C   r-   r.   )rŽ   r/   r%   r%   r&   Úremote_getSimple  r1   zNestedRemote.remote_getSimpleN)r(   r)   r*   r™   r%   r%   r%   r&   r˜     r+   r˜   c                   @   r   )Ú
SimpleCopyc                 C   ó   d| _ ddi| _dg| _d S ©NrN   ÚHelloÚWorldrj   ©ÚxÚyÚzr/   r%   r%   r&   rD     ó   
zSimpleCopy.__init__N©r(   r)   r*   rD   r%   r%   r%   r&   rš     r+   rš   c                   @   ó   e Zd ZdS )ÚSimpleLocalCopyN©r(   r)   r*   r%   r%   r%   r&   r¦     ó    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&   rD     s   zSimpleFactoryCopy.__init__N)	r(   r)   r*   r2   rª   r   ÚintÚ__annotations__rD   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| }|s+tdƒ‚|S )zl
    Factory of L{SimpleFactoryCopy}, getting a created instance given the
    C{id} found in C{state}.
    r«   Nz&factory copy state has no 'id' member zfactory class has no ID: z&factory method found no object with id)ÚgetÚRuntimeErrorÚreprr©   rª   )r   ÚstateIdÚinstr%   r%   r&   ÚcreateFactoryCopy!  s   

r³   c                   @   r�   )Ú
NestedCopyc                 C   r-   r.   )rš   r/   r%   r%   r&   Úremote_getCopy5  r1   zNestedCopy.remote_getCopyc                 C   s   t |ƒS r.   )r©   )r#   Úvaluer%   r%   r&   Úremote_getFactory8  ó   zNestedCopy.remote_getFactoryN)r(   r)   r*   rµ   r·   r%   r%   r%   r&   r´   4  ó    r´   c                   @   r   )ÚSimpleCachec                 C   r›   rœ   rŸ   r/   r%   r%   r&   Ú	__init___=  r£   zSimpleCache.__init___N)r(   r)   r*   r»   r%   r%   r%   r&   rº   <  r+   rº   c                   @   r�   )ÚNestedComplicatedCachec                 C   rs   r.   )ÚVeryVeryComplicatedCacheableÚcr/   r%   r%   r&   rD   D  ru   zNestedComplicatedCache.__init__c                 C   ó   | j S r.   )r¾   r/   r%   r%   r&   Úremote_getCacheG  r1   z&NestedComplicatedCache.remote_getCacheN)r(   r)   r*   rD   rÀ   r%   r%   r%   r&   r¼   C  r¹   r¼   c                   @   ó,   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 )NrN   é   é   ©r    r¡   Úfoor/   r%   r%   r&   rD   L  s   
z%VeryVeryComplicatedCacheable.__init__c                 C   s   d| _ | j dd¡ d S )Né   rÅ   )rÅ   ÚobserverÚ
callRemoter/   r%   r%   r&   ÚsetFoo4Q  ó   z$VeryVeryComplicatedCacheable.setFoo4c                 C   s   || _ | j| j| jdœS )NrÄ   )rÇ   r    r¡   rÅ   ©r#   ÚperspectiverÇ   r%   r%   r&   ÚgetStateToCacheAndObserveForU  rÊ   z9VeryVeryComplicatedCacheable.getStateToCacheAndObserveForc                 C   s,   t  d¡ | d¡ || jkrd | _d S d S )Nzstopped observingÚend)r   ÚmsgrÈ   rÇ   rË   r%   r%   r&   ÚstoppedObservingY  s
   



ÿz-VeryVeryComplicatedCacheable.stoppedObservingN)r(   r)   r*   rD   rÉ   rÍ   rÐ   r%   r%   r%   r&   r½   K  s
    r½   c                   @   r�   )ÚRatherBaroqueCachec                 C   ó
   || _ d S r.   ©rÅ   )r#   ÚnewFoor%   r%   r&   Úobserve_fooa  ó   
zRatherBaroqueCache.observe_fooc                 C   s   t  d¡ d S )Nzthe end of things)r   rÏ   r/   r%   r%   r&   Úobserve_endd  ó   zRatherBaroqueCache.observe_endN)r(   r)   r*   rÕ   r×   r%   r%   r%   r&   rÑ   `  r¹   rÑ   c                   @   rÁ   )
ÚSimpleLocalCachec                 C   ó   | j  |¡ d S r.   )Ú__dict__Úupdate)r#   r   r%   r%   r&   ÚsetCopyableStatel  ó   z!SimpleLocalCache.setCopyableStatec                 C   r¿   r.   )Úcheckr/   r%   r%   r&   ÚcheckMethodo  r1   zSimpleLocalCache.checkMethodc                 C   s   | S r.   r%   r/   r%   r%   r&   Ú	checkSelfr  ó   zSimpleLocalCache.checkSelfc                 C   ó   dS r�   r%   r/   r%   r%   r&   rß   u  râ   zSimpleLocalCache.checkN)r(   r)   r*   rÝ   rà   rá   rß   r%   r%   r%   r&   rÙ   k  ó
    rÙ   c                   @   ó$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚNestedCachec                 C   rs   r.   )rº   r    r/   r%   r%   r&   rD   }  ru   zNestedCache.__init__c                 C   s   | j | j gS r.   ©r    r/   r%   r%   r&   rÀ   €  ru   zNestedCache.remote_getCachec                 C   s
   | j |u S r.   rç   ©r#   Úcacher%   r%   r&   Úremote_putCacheƒ  rÖ   zNestedCache.remote_putCacheN)r(   r)   r*   rD   rÀ   rê   r%   r%   r%   r&   ræ   |  s    ræ   c                   @   rÁ   )
Ú
Observablec                 C   s
   g | _ d S r.   )Ú	observersr/   r%   r%   r&   rD   ˆ  rÖ   zObservable.__init__c                 C   rÚ   r.   )rì   Úappend©r#   Úobsr%   r%   r&   Úremote_observe‹  rÞ   zObservable.remote_observec                 C   rÚ   r.   )rì   Úremoverî   r%   r%   r&   Úremote_unobserveŽ  rÞ   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*   rD   rð   rò   ró   r%   r%   r%   r&   rë   ‡  rä   rë   c                   @   rÁ   )
ÚDeferredRemotec                 C   ó
   d| _ d S )Nr   ©Úrunr/   r%   r%   r&   rD   —  rÖ   zDeferredRemote.__init__c                 C   r�   r�   r÷   r’   r%   r%   r&   ÚrunMeš  r”   zDeferredRemote.runMec                 C   s   J dƒ‚)Nr   zshouldn't have been run!r%   r’   r%   r%   r&   Ú	dontRunMež  r¸   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*   rD   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 )NrN   Ú	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                   @   r   )ÚNewStyleCopyc                 C   rÒ   r.   )Ús)r#   r  r%   r%   r&   rD   ·  rÖ   zNewStyleCopy.__init__Nr¤   r%   r%   r%   r&   r  ¶  r+   r  c                   @   s(   e Zd ZdZdZdZdd„ Zdd„ ZdS )ÚNewStyleCopy2r   rN   c                 C   s"   t  jd7  _t | ¡}d|_|S )NrN   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 r�   )r  Úinitializedr/   r%   r%   r&   rD   É  s   zNewStyleCopy2.__init__N)r(   r)   r*   r  r	  r¶   r  rD   r%   r%   r%   r&   r  ¾  s    r  c                   @   r   )ÚNewStyleCacheCopyc                 C   r¿   r.   )rÛ   rË   r%   r%   r&   rÍ   Ñ  r1   z.NewStyleCacheCopy.getStateToCacheAndObserveForN)r(   r)   r*   rÍ   r%   r%   r%   r&   r
  Ð  r+   r
  c                   @   r�   )ÚEchoerc                 C   s   |S r.   r%   ©r#   Ústr%   r%   r&   Úremote_echoÙ  râ   zEchoer.remote_echoc                 K   s   ||fS r.   r%   )r#   r  Úkwr%   r%   r&   Úremote_echoWithKeywordsÜ  r¸   zEchoer.remote_echoWithKeywordsN)r(   r)   r*   r  r  r%   r%   r%   r&   r  Ø  r¹   r  c                   @   r�   )ÚCachedReturnerc                 C   rÒ   r.   ©ré   rè   r%   r%   r&   rD   á  rÖ   zCachedReturner.__init__c                 C   r¿   r.   r  r  r%   r%   r&   Úremote_giveMeCacheä  r1   z!CachedReturner.remote_giveMeCacheN)r(   r)   r*   rD   r  r%   r%   r%   r&   r  à  r¹   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.
        )rj   rc   rk   N)r   r‡   r  rk   r‰   rq   rA   rH   ÚsuccessResultOfÚgetRootObjectÚref)r#   rc   r@   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   rN   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È   rH   rJ   Ú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.
        rN   r  c                    sF   ˆ  | t¡ ˆ | jd¡ ˆ tjd¡ ˆ tjd¡ ˆ | ˆ ¡ d S )NrÂ   rÃ   rN   )r  r  r  r¶   r  r	  ÚassertIsNotr  r   r%   r&   r"    s
   z$NewStyleTests.test_alloc.<locals>.cb)	r  r  r  r	  r  rÈ   rH   rJ   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.
        Úvalue1ÚechoWithKeywordsÚoneÚtwo©Úkeyword1Úkeyword2c                    sh   ˆ  | t¡ ˆ  | d t¡ ˆ  | d t¡ ˆ | d jd¡ ˆ | d ˆ ¡ ˆ | d dddœ¡ d S )Nr   rN   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(  r2  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   r‡   rD   r   ÚconnectionMade)r#   Úrootr%   r%   r&   rD   I  s   z&ConnectionNotifyServerFactory.__init__c                 C   s.   || _ | jd}| _|dur| d¡ dS dS )zF
        Store the protocol and fire the connection deferred.
        N)ÚprotocolInstancer4  rz   )r#   r   rü   r%   r%   r&   ÚclientConnectionMadeP  s
   ÿz2ConnectionNotifyServerFactory.clientConnectionMade)r(   r)   r*   r2   r6  rD   r7  r%   r%   r%   r&   r3  :  s
    r3  c                   @   rå   )Ú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  rÖ   z*NewStyleCachedTests.setUp.<locals>.gotRoot)r
  r!  r  r   Ú	listenTCPr3  r  rA   r   r‰   Ú
connectTCPÚgetHostÚportr  r$  Úfactoryr4  r   )r#   rc   r;  Úd1Ú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.
        )rA   r@  r6  rR   ÚloseConnectionr  r]   Ú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| ¡ |r!| ˆ _ˆ j dˆ j¡S d S )Nr¶   rE  )	r  r
  r  r  r'  r!  r  r  rÈ   )r  Ú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  rG  r%   r%   r%   r&   r8  Z  s    r8  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 d S  ty   Y d S w )NúNone-None-TESTING.pub)ÚosÚunlinkÚOSErrorr/   r%   r%   r&   r  ”  s
   ÿzBrokerTests.tearDownc                 C   ó   |   d|› �¡ d S )Nz&This should cause a return value, not ©Úfail)r#   Úerrorr%   r%   r&   ÚthunkErrorBad›  ó   zBrokerTests.thunkErrorBadc                 C   rÒ   r.   )ÚthunkResult©r#   Úresultr%   r%   r&   ÚthunkResultGoodž  rÖ   zBrokerTests.thunkResultGoodc                 C   r5   r.   r%   )r#   Útbr%   r%   r&   ÚthunkErrorGood¡  râ   zBrokerTests.thunkErrorGoodc                 C   rM  )Nz This should cause an error, not rN  rT  r%   r%   r&   ÚthunkResultBad¤  rR  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©rj   c                   @   r   )z%BrokerTests.test_reference.<locals>.Xc                 S   rÒ   r.   )Úcaughtr’   r%   r%   r&   Úremote_catch«  rÖ   z2BrokerTests.test_reference.<locals>.X.remote_catchN)r(   r)   r*   r\  r%   r%   r%   r&   ÚXª  r+   r]  c                   @   r   )z%BrokerTests.test_reference.<locals>.Yc                 S   s   |  d|¡ d S )NÚcatch©rÈ   ©r#   ÚaÚbr%   r%   r&   Úremote_throw¯  rÞ   z2BrokerTests.test_reference.<locals>.Y.remote_throwN)r(   r)   r*   rc  r%   r%   r%   r&   ÚY®  r+   rd  r¡   ÚthrowzX should have caught Z)rŒ   r   ÚReferenceableÚsetNameForLocalÚremoteForNamerÈ   rH   ÚassertIsr[  r  ÚremoteMethod)	r#   r¾   r  rH   r]  rd  r¡   r    r¢   r%   r%   r&   Útest_reference§  s   
zBrokerTests.test_referencec                 C   s�   t | d�\}}}||f||ffD ]5\}}tƒ }| d|¡ | d¡}d| _| d| jd ¡ | j| j¡ | 	¡  | 	¡  |  
| j| jd¡ qd S )NrZ  rÅ   é   ÚthunkrN   zresult wasn't received.)rŒ   rŽ   rg  rh  ÚexpectedThunkResultrÈ   rû   rV  rQ  rH   r  rS  )r#   r¾   r  rH   r    r¡   rÅ   Úbarr%   r%   r&   Útest_result¿  s   
ÿ
ÿòzBrokerTests.test_resultc                 C   rÒ   r.   )ÚnestedRemoterT  r%   r%   r&   ÚrefcountResultÓ  rÖ   zBrokerTests.refcountResultc           
   	   C   sº   g }g }t | d�\}}}tƒ }| d|¡ | d¡}ttjd ƒD ]}|jjs+|jjr- n| 	d¡ 
|j|j¡ | ¡  q!tjd }	|  |jjd¡ |  t|ƒ|	d|	› dt|ƒ› �¡ d S )	NrZ  rÅ   é
   Ú	getSimplerN   ztransport was not closedz	expected z got )rŒ   r˜   rg  rh  Úranger   ÚMAX_BROKER_REFSrR   ÚclosedrÈ   rû   rí   rH   Ú
assertTruer  Úlen)
r#   ÚlÚer¾   r  rH   rÅ   r    Ú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 )	NrZ  rÅ   ÚgetCopyrN   r�   rž   r   rj   )rŒ   r´   rg  rh  rÈ   rû   rV  rQ  rH   r  rS  r    r¡   r¢   )r#   r¾   r  rH   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 )NrZ  ra  ÚobserverN   rs  zdidn't notifyznotified too much)rŒ   rë   rþ   rg  rh  rÈ   rH   ró   ÚassertIsNotNonerô   r  )r#   r¾   r  rH   ra  rb  Ú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 )
NrZ  rü   Ú	doItLaterzDeferred method run too early.rE   zDeferred method run too late.r   é   zIncorrect result.)rŒ   rõ   rg  rh  rH   rÈ   r$  rí   r  rø   rü   rz   r  )r#   r¾   r  rH   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 )NrZ  rÅ   rt  é   )rŒ   r˜   rg  rh  rÈ   rû   rr  rQ  rH   rq  ÚluidÚassertInÚlocalObjectsÚsysÚ
hexversionÚgcÚcollectÚassertNotIn)r#   r¾   r  rH   rÅ   ro  Ú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 )NrZ  rô   ÚxxxÚgetCacher   rN   rÂ   rÃ   rÆ   zpotential refcounting issuez!other potential refcounting issueÚputCachezremote cache doesn't have itr‰  zServer still had it after GCzClient still had it after GCz!Server still had complex after GCz!Client still had complex after GCzobserver was not removed)"rŒ   ræ   r¼   r¾   rg  rh  rÈ   r$  rí   Ú
addErrbackrJ   r  r    r¡   rÅ   rÉ   ry  ÚassertIdenticalrà   Ú__self__rá   rx  rj  rŠ  r‹  ÚremotelyCachedObjectsr�  rŽ  r�  r�  r‘  ÚlocallyCachedObjectsÚassertIsNonerÇ   )r#   r¾   r  rH   rô   Úobj2ÚvccÚo2Úo3ÚcollÚcomplexÚcpÚcol2rŠ  ÚbaroqueLuidr%   r%   r&   Ú
test_cache4  sj   

ÿ

ÿÿzBrokerTests.test_cachec                 C   s  zt  d¡ W n	 ty   Y nw 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 )NrI  rZ  rÅ   ÚgetPubrN   r   )rJ  rK  rL  rŒ   ÚGetPublisherrg  rh  rÈ   rû   rí   rQ  rJ   ry   r  ÚactivateCalledÚisActivatedÚyayIGotPublishedÚ_wasCleanWhenLoaded)r#   r¾   r  rH   rÅ   ro  Úaccumrô   r%   r%   r&   Útest_publishablev  s0   ÿ

zBrokerTests.test_publishablec                 C   s   |j | _d S r.   )r«   rS  )r#   Úvalr%   r%   r&   ÚgotCopy’  ru   zBrokerTests.gotCopyc                 C   s|   t | d�\}}}d}tƒ }| d|¡ | d¡}| d|¡ | j| j¡ | ¡  | ¡  | ¡  |  	| j
|d| j
› �¡ d S )NrZ  éc   rÅ   Ú
getFactoryz!ID not correct on factory object )rŒ   r´   rg  rh  rÈ   rû   r°  rY  rH   r  rS  )r#   r¾   r  rH   ÚIDrô   r    r%   r%   r&   Útest_factoryCopy•  s   

ýzBrokerTests.test_factoryCopy)r(   r)   r*   rS  r  rQ  rV  rX  rY  rk  rp  rr  r~  r€  r„  rˆ  r“  r¦  r®  r°  r´  r%   r%   r%   r&   rH  ‘  s&    BrH  sô  helloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldc                  O   s   | a |ad S r.   )ÚcallbackArgsÚcallbackKeyword)Úargsr  r%   r%   r&   ÚfinishedCallback¬  s   r¸  c                   @   r�   )Ú	Pagerizerc                 O   s   |||| _ | _| _d S r.   )rz   r·  r  )r#   rz   r·  r  r%   r%   r&   rD   ³  s   zPagerizer.__init__c                 C   s4   t j|td| jg| j¢R i | j¤Ž d  | _| _d S )Néd   )r   ÚStringPagerÚ	bigStringrz   r·  r  ©r#   Ú	collectorr%   r%   r&   Úremote_getPages¶  s   
ÿÿÿzPagerizer.remote_getPagesN)r(   r)   r*   rD   r¿  r%   r%   r%   r&   r¹  ²  r¹   r¹  c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚFilePagerizerNc                 O   s    || _ |||| _| _| _d S r.   )Úfilenamerz   r·  r  )r#   rÁ  rz   r·  r  r%   r%   r&   rD   À  s   zFilePagerizer.__init__c                 C   s<   t j|t| jdƒ| jg| j¢R i | j¤Ž| _d  | _| _d S )NÚrb)r   Ú	FilePagerÚopenrÁ  rz   r·  r  Úpagerr½  r%   r%   r&   r¿  Ä  s   ÿÿÿzFilePagerizer.remote_getPages)r(   r)   r*   rÅ  rD   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   sF   |   ¡ | _t| jdƒ�}| t¡ W d  ƒ dS 1 sw   Y  dS )z?
        Create a file used to test L{util.FilePager}.
        ÚwbN)ÚmktemprÁ  rÄ  Ú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¡ |s,| 	¡  |r&|  
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.
        rZ  rÅ   Úhellors  ©r¶   ÚgetPagesó    r   ú'Pages received not equal to pages sent!)rË  úCompleted callback not invokedr¶   N)rŒ   rg  r¹  r¸  rh  r   ÚgetAllPagesr$  rí   rH   r  Újoinr¼  rµ  r¶  ©r#   r¾   r  rH   r    rz  r%   r%   r&   Útest_pagingWithCallbackØ  s   
ÿÿ
ÿz#PagingTests.test_pagingWithCallbackc                 C   sn   t | d�\}}}| dtdƒ¡ | d¡}g }t |d¡ |j¡ |s)| ¡  |r#|  	d 
|d ¡td¡ dS )z>
        Test L{util.StringPager} without a callback.
        rZ  rÅ   NrÍ  rÎ  r   rÏ  )rŒ   rg  r¹  rh  r   rÑ  r$  rí   rH   r  rÒ  r¼  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}|sE|dkrE| ¡  |d8 }|sE|dks7|sL|  d	¡ |  d
 |d ¡d
d¡ dS )z@
        Test L{util.FilePager}, sending an empty file.
        ÚwrZ  Nro  rÍ  rs  r   rN   zgetAllPages timed outrÎ  rÏ  )rÈ  rÄ  ÚcloserŒ   rÀ  rg  rh  r   rÑ  r$  rí   rH   rO  r  rÒ  )	r#   ÚfilenameEmptyr¾   r  rH   Ú	pagerizerr    rz  Ú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	¡ |s0| 
¡  |r*|  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.
        rZ  Úfrodoé	   rÌ  ro  rÍ  rÎ  r   rÏ  )rÜ  rÐ  r¶   N)rŒ   rÀ  rÁ  r¸  rg  rh  r   rÑ  r$  rí   rH   r  rÒ  r¼  rµ  r¶  rÅ  Úchunks©r#   r¾   r  rH   rÙ  r    rz  r%   r%   r&   Útest_filePagingWithCallback  s"   
ÿÿ
ÿz'PagingTests.test_filePagingWithCallbackc                 C   s†   t | d�\}}}t| jdƒ}| d|¡ | d¡}g }t |d¡ |j¡ |s-| 	¡  |r'|  
d |d ¡td¡ |  
|jjg ¡ dS )z<
        Test L{util.FilePager} without a callback.
        rZ  Nro  rÍ  rÎ  r   rÏ  )rŒ   rÀ  rÁ  rg  rh  r   rÑ  r$  rí   rH   r  rÒ  r¼  rÅ  rÞ  rß  r%   r%   r&   Útest_filePagingWithoutCallback%  s   
ÿÿz*PagingTests.test_filePagingWithoutCallbackN)
r(   r)   r*   r2   r  rÔ  rÕ  rÛ  rà  rá  r%   r%   r%   r&   rÆ  Ë  s    rÆ  c                   @   r   )ÚDumbPublishablec                 C   s   ddiS )Nr«  rN   r%   r/   r%   r%   r&   ÚgetStateToPublish8  r¸   z!DumbPublishable.getStateToPublishN)r(   r)   r*   rã  r%   r%   r%   r&   râ  7  r+   râ  c                   @   r   )ÚDumbPubc                 C   rö   r�   )r©  r/   r%   r%   r&   Ú	activated=  rÖ   zDumbPub.activatedN)r(   r)   r*   rå  r%   r%   r%   r&   rä  <  r+   rä  c                   @   r�   )r¨  c                 C   s   t dƒ| _d S )NÚTESTING)râ  Úpubr/   r%   r%   r&   rD   B  rØ   zGetPublisher.__init__c                 C   r¿   r.   )rç  r/   r%   r%   r&   Úremote_getPubE  r1   zGetPublisher.remote_getPubN)r(   r)   r*   rD   rè  r%   r%   r%   r&   r¨  A  r¹   r¨  c                   @   ó8   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|› �ƒ‚)NzI shouldn't have been called: ©r¯   )r#   r·  r%   r%   r&   rP  Q  rØ   zDisconnectionTests.errorc                 C   rK   )z.
        Called on broker disconnect.
        rN   N)ÚgotCallbackr/   r%   r%   r&   ÚgotDisconnectedT  ó   
z"DisconnectionTests.gotDisconnectedc                 C   s   |   || j¡ d| _dS )z7
        Called on RemoteReference disconnect.
        rN   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 )NrZ  rò  Ú
setBadCopyrN   )rŒ   rH   rg  Ú
BadCopySetrh  rÈ   ÚBadCopyabler—  rí   rJ   r  ry  )r#   r¾   r  rH   Úgrz  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 )NrZ  rò  )rŒ   rH   rg  rŽ   rh  ÚnotifyOnDisconnectrP  r‹  ÚdisconnectsÚdontNotifyOnDisconnectr‘  Ú_disconnectedÚdisconnectCallbacksrí  ró  rï  rZ   r   r[   r   r\   rx  rì  rð  )r#   r¾   r  rH   Úrr%   r%   r&   Útest_disconnectionk  s.   
z%DisconnectionTests.test_disconnectionN)	r(   r)   r*   r2   rP  rí  ró  rø  rÿ  r%   r%   r%   r&   rê  L  s    
rê  c                   @   r¥   )ÚFreakOutNr§   r%   r%   r%   r&   r   �  r¨   r   c                   @   r   )rö  c                 C   r•   r.   )r   )r#   Úpr%   r%   r&   ÚgetStateToCopyFor”  r1   zBadCopyable.getStateToCopyForN)r(   r)   r*   r  r%   r%   r%   r&   rö  “  r+   rö  c                   @   r   )rõ  c                 C   r5   r.   r%   )r#   Úbcr%   r%   r&   Úremote_setBadCopy™  râ   zBadCopySet.remote_setBadCopyN)r(   r)   r*   r  r%   r%   r%   r&   rõ  ˜  r+   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 r�   r%   )r#   r    r%   r%   r&   Ú	sync_add1   r¸   zLocalRemoteTest.sync_add1rN   c                 C   ó   || S r.   r%   )r#   r    r¡   r%   r%   r&   Ú	async_add£  r¸   zLocalRemoteTest.async_addc                 C   r•   r.   rë  r/   r%   r%   r&   Ú
async_fail¦  r1   zLocalRemoteTest.async_failN)r   rN   )r(   r)   r*   Ú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   rÒ   r.   ©r9   ©r#   r9   r%   r%   r&   rD   ¶  rÖ   zMyPerspective.__init__c                 C   r¿   )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   r-   r.   )ÚMyViewr/   r%   r%   r&   Úperspective_getViewPoint¿  r1   z&MyPerspective.perspective_getViewPointc                 C   r  )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   rö   ©NT©Ú	loggedOutr/   r%   r%   r&   ÚlogoutÊ  rÖ   zMyPerspective.logoutN)r(   r)   r*   r2   ÚloggedInr  rD   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 rt jtƒ dd„ fS |  |¡| _d| j_t j| j| jjfS )zè
        Verify that the mind and interface supplied have the expected values
        (this should really be done somewhere else, like inside a test method)
        and return an avatar appropriate for the given identifier.
        úBRAINS!c                   S   r5   r.   r%   r%   r%   r%   r&   r6   è  r7   z)TestRealm.requestAvatar.<locals>.<lambda>T)	r   r8   r   Ú	ANONYMOUSr  ÚperspectiveFactoryÚlastPerspectiver  r  ©r#   r9   r:   Ú	interfacer%   r%   r&   r=   ß  s   
zTestRealm.requestAvatar)r(   r)   r*   r2   r  r  r  r=   r%   r%   r%   r&   r  Î  s
    r  c                   @   r   )r  c                 C   s
   t |tƒS r.   )r    r  r"   r%   r%   r&   Ú
view_checkð  rÖ   zMyView.view_checkN)r(   r)   r*   r  r%   r%   r%   r&   r  ï  r+   r  c                   @   ó    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#   Ú	mindEaterr%   r%   r&   rD   ú  s   
zLeakyRealm.__init__c                    s*   |   ˆ ¡ |  |¡‰tjˆ‡ ‡fdd„fS )Nc                      s   ˆ ˆ  ¡ fS r.   )r  r%   ©r:   Úperspr%   r&   r6     s    z*LeakyRealm.requestAvatar.<locals>.<lambda>)r!  r  r   r8   r  r%   r#  r&   r=     s   

zLeakyRealm.requestAvatarN)r(   r)   r*   r2   rD   r=   r%   r%   r%   r&   r   ô  s    	r   c                   @   r,   )Ú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)r:   r/   r%   r&   Ú
setMindRef  rÞ   z4NewCredLeakTests.test_logoutLeak.<locals>.setMindRef)rj   rŠ   r5  Úloginr„   c                 S   s$   | \}}t ƒ }| dt |d¡|¡S )NÚrespondr„   )rŽ   rÈ   r   r*  )r    Ú	challengeÚ
challengerr:   r%   r%   r&   Ú
cbResponse$  s
   ÿz4NewCredLeakTests.test_logoutLeak.<locals>.cbResponsec                    s*   ˆ  ¡  ˆ  d¡ ˆ t tdƒ¡¡ d S )NrN   Úboom)rL   rí   rZ   r   r[   r¯   )Ú_)ÚconnectionBrokenrH   rn   r%   r&   rZ   -  s   
z8NewCredLeakTests.test_logoutLeak.<locals>.connectionLostrN   )r'  rŒ   r   rh  rÈ   r$  rJ   r  r�  r�  rœ  )r#   r(  rm   r5  rü   r-  rZ   r%   )r0  rH   r#   rn   r&   Útest_logoutLeak  s    ÿ


z NewCredLeakTests.test_logoutLeakN)r(   r)   r*   r2   r1  r%   r%   r%   r&   r%  	  r3   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  rŠ   r	   rˆ   r3  rk   r   r‰   rc   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}
        ÚTCPrX   iá  N)rq   rc   rk   r@   rA   rH   rr   ÚconnectorStater|   r   ÚIPv4Addressr_   rx   r$  ÚestablishClientAndServer)r#   Ú_ignoredr%   r%   r&   r6  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)rx  r@   rR   rw  rZ   rc   ra   r_   )r#   r`   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ù  rz   rc   Ú
disconnectr8  )r;  ÚdisconnectedDeferredr/   r%   r&   r=  �  s
   
z3NewCredTests.test_getRootObject.<locals>.disconnect©r6  rc   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   ÚDeadReferenceErrorrÈ   )Úign)r;  r#   r%   r&   ÚlostConnectionª  s   zSNewCredTests.test_deadReferenceError.<locals>.gotRootObject.<locals>.lostConnection)r   rù  rz   r$  rc   r=  r8  )r;  r>  rF  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                   @   r,   )z=NewCredTests.test_clientConnectionLost.<locals>.ReconnectOnceFc                 S   s0   | j  }d| _ tj | |||¡}|r| ¡  |S r  )ÚreconnectedAlreadyr   r‰   ra   r‚   )r#   r_   r`   ÚreconnectingrU  r%   r%   r&   ra   À  s   ÿzRNewCredTests.test_clientConnectionLost.<locals>.ReconnectOnce.clientConnectionLostN)r(   r)   r*   rH  ra   r%   r%   r%   r&   ÚReconnectOnce½  s    rJ  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   r9  r   rù  rz   rc   r=  r8  )ÚanotherRootObjrü   r/   r%   r&   ÚgotAnotherRootObjectÛ  s   
zqNewCredTests.test_clientConnectionLost.<locals>.gotRootObject.<locals>.disconnected.<locals>.gotAnotherRootObject)rc   r  r$  )rE  rü   rL  r/   r%   r&   rY   Ø  s   

zSNewCredTests.test_clientConnectionLost.<locals>.gotRootObject.<locals>.disconnected)
r  r   r9  r   rù  rz   rc   r=  r8  r$  )r;  rü   rY   r/   r%   r&   r<  Ï  s   

z=NewCredTests.test_clientConnectionLost.<locals>.gotRootObject)r   r‰   rc   r6  r  r$  )r#   rJ  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.
        )rX   i90  N)r6  rk   re   rg   r   rh   rf   rZ   r   r[   r   r\   )r#   Ú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   r‰   r)  r   ÚUsernamePasswordÚclientConnectionFailedr   r[   r   ÚassertFailure)r#   rc   Ú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.   )rx  rŠ   r  r  r  r   r9  ©rÌ   ©ÚloginCompletedr#   r%   r&   ÚcbLogin  s   z.NewCredTests.test_loginLogout.<locals>.cbLoginc                    s   ˆ j  ¡  ˆ  ¡  d S r.   )rc   r=  r8  ©Úignoredr/   r%   r&   ÚcbDisconnect  s   
z3NewCredTests.test_loginLogout.<locals>.cbDisconnectc                    s   ˆ   ˆ jjj¡ d S r.   )rx  rŠ   r  r  r^  r/   r%   r&   ÚcbLogout&  rR  z/NewCredTests.test_loginLogout.<locals>.cbLogoutN)r	   ÚregisterCheckerr   r†   r   rR  r   rc   r)  r$  r6  rH   rJ   r�  r�  rz   )r#   Úcredsr:   rü   r]  r`  ra  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   r5   r.   r%   r  r%   r%   r&   rD   C  râ   zFNewCredTests.test_logoutAfterDecref.<locals>.EventPerspective.__init__c                    s   ˆ   d ¡ d S r.   )rz   r/   r  r%   r&   r  F  rØ   zDNewCredTests.test_logoutAfterDecref.<locals>.EventPerspective.logoutN)r(   r)   r*   r2   rD   r  r%   r  r%   r&   ÚEventPerspective>  s    re  rQ  rÓ   rP  r  c                    s   ˆ S r.   r%   )Úavatarr  r%   r&   Ú
cbLoggedInS  s   z7NewCredTests.test_logoutAfterDecref.<locals>.cbLoggedInc                    s   ˆ   ˆ jjji ¡ d S r.   )r  rk   r6  Ú_localCleanupr^  r/   r%   r&   ÚcbLoggedOutZ  s   z8NewCredTests.test_logoutAfterDecref.<locals>.cbLoggedOut)r   r   ÚAvatarrŠ   r  r	   rb  r   r†   rc   r)  r   rR  r$  r6  rH   rJ   r�  r�  )r#   re  rü   rg  ri  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.
        rQ  s   quux)rÅ   ÚbazrP  r  ó   bazc                 S   s    | \}}t | d¡| d¡gƒS )NÚgetAvatarId)r   rÈ   )rU  ÚfirstÚsecondr%   r%   r&   rg  }  s   ÿz5NewCredTests.test_concurrentLogin.<locals>.cbLoggedInc                    s$   | \}}ˆ   |d¡ ˆ   |d¡ d S )NrP  rm  )r  )r    ro  rp  r/   r%   r&   ÚcbAvatarIds…  s   z6NewCredTests.test_concurrentLogin.<locals>.cbAvatarIds)r	   rb  r   r†   rc   r)  r   rR  r   r$  r6  rH   rJ   )r#   Ú
firstLoginÚsecondLoginrü   rg  rq  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.
        rW  rX  s
   nosuchuserrY  s	   wrongpassc                    ó   ˆ   t¡}ˆ  t|ƒd¡ d S )NrÂ   )ÚflushLoggedErrorsr
   r  ry  ©ÚignoreÚerrorsr/   r%   r&   Úcleanup¥  ó   
z;NewCredTests.test_badUsernamePasswordLogin.<locals>.cleanup)r	   rb  r   r†   rc   r)  r   rR  rT  r
   r   r$  r6  rH   rJ   )r#   rr  rs  rü   rz  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   ó   |   dd¡S ©Nr  é{   r_  rZ  r%   r%   r&   rg  ¹  ru   z4NewCredTests.test_anonymousLogin.<locals>.cbLoggedInr  )r	   rb  r   ÚAllowAnonymousAccessrc   r)  r   Ú	Anonymousr$  r  r6  rH   rJ   )r#   rü   rg  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.
        ÚpassrX  r  c                    ru  r�   )rv  r   r  ry  rw  r/   r%   r&   rz  Ï  r{  z=NewCredTests.test_anonymousLoginNotPermitted.<locals>.cleanup)r	   rb  r   r†   rc   r)  r   r�  rT  r   r$  r6  rH   rJ   ©r#   rü   rz  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.
        rW  rX  r  c                 S   r}  r~  r_  rZ  r%   r%   r&   r]  å  ru   zENewCredTests.test_anonymousLoginWithMultipleCheckers.<locals>.cbLoginr  )r	   rb  r   r€  r†   rc   r)  r   r�  r$  r  r6  rH   rJ   ©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.
        rW  rX  rY  r  c                 S   s   |   ddd¡S )NÚaddrº  é   r_  rZ  r%   r%   r&   r]  þ  rØ   zINewCredTests.test_authenticatedLoginWithMultipleCheckers.<locals>.cbLoginr  )r	   rb  r   r€  r†   rc   r)  r   rR  r$  r  r6  rH   rJ   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.
        rW  rX  rY  r  c                 S   ó
   |   d¡S )NÚgetViewPointr_  rZ  r%   r%   r&   r]    rÖ   z'NewCredTests.test_view.<locals>.cbLoginc                 S   r‹  )Nrß   r_  )Ú	viewpointr%   r%   r&   ÚcbView  rÖ   z&NewCredTests.test_view.<locals>.cbView)r	   rb  r   r†   rc   r)  r   rR  r$  rx  r6  rH   rJ   )r#   rü   r]  rŽ  r%   r%   r&   Ú	test_view
  s   
ÿÿ


zNewCredTests.test_viewr.   )r(   r)   r*   r2   r  r6  r   r[   r   r\   r8  rA  rG  rM  rO  rV  rd  rk  rt  r|  r‚  r…  r‡  rŠ  r�  r%   r%   r%   r&   r2  ?  s(    

!
ÿ3
16%r2  c                   @   rå   )ÚNonSubclassingPerspectivec                 C   r5   r.   r%   r  r%   r%   r&   rD   *  râ   z"NonSubclassingPerspective.__init__c                 C   s(   |  || ¡}|  || ¡}| |||f¡S r.   )ÚunserializeÚ	serialize)r#   r]   Úmessager·  Úkwargsr%   r%   r&   ÚperspectiveMessageReceived.  s   z4NonSubclassingPerspective.perspectiveMessageReceivedc                 C   rö   r  r  r/   r%   r%   r&   r  4  rÖ   z NonSubclassingPerspective.logoutN)r(   r)   r*   rD   r•  r  r%   r%   r%   r&   r�  (  s    r�  c                   @   r  )Ú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 )NrY  rW  r   rX   )r  )r  rŠ   r�  r  r	   rˆ   r   r†   r‹   ÚaddUserrb  r   r   r‡   r@  r   r<  r?  ri   rD  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.
        rY  rW  r  rX   c                 S   s   | j dddd�S )NÚANYTHINGÚhererl  )ro  r_  )r  r%   r%   r&   r6   R  s    z#NSPTests.test_NSP.<locals>.<lambda>r™  )rš  ro  rl  c                    s$   ˆ   ¡  ˆjjD ]}|j ¡  qd S r.   )r=  r@  Ú	protocolsrR   rC  )r_  r  ©r@  r#   r%   r&   rz  U  s   ÿz"NSPTests.test_NSP.<locals>.cleanup)
r   r‰   r)  r   rR  r   r=  r˜  r$  r  r„  r%   rœ  r&   Útest_NSPJ  s   
zNSPTests.test_NSPN)r(   r)   r*   r2   r  r�  r%   r%   r%   r&   r–  8  s    r–  c                   @   r  )Ú
IForwardedzA
    Interface used for testing L{util.LocalAsyncForwarder}.
    c                   C   rã   )z,
        Simple synchronous method.
        Nr%   r%   r%   r%   r&   Ú	forwardMec  r7   zIForwarded.forwardMec                   C   rã   )z-
        Simple asynchronous method.
        Nr%   r%   r%   r%   r&   ÚforwardDeferredh  r7   zIForwarded.forwardDeferredN)r(   r)   r*   r2   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   rK   )z6
        Set a local flag to test afterwards.
        TN)Ú	forwardedr/   r%   r%   r&   rŸ  |  rî  zForwarded.forwardMec                 C   rK   )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‚  rM   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*   r2   r¢  r£  rŸ  r¤  r   r%   r%   r%   r&   r¡  n  s    	r¡  c                   @   ré  )Ú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.
        rO  c                    s   ˆ   | tj¡ |  t¡ d S r.   )r  r   r[   Útrapr¯   )rÊ  r/   r%   r&   Úeb°  s   z*SpreadUtilTests.test_asyncFail.<locals>.ebc                    s
   ˆ   d¡S )Nzsupposed to failrN  r  r/   r%   r&   r6   ´  s   
 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  rj  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   rN   N)r¡  r   ÚLocalAsyncForwarderrž  rÈ   rx  r¢  r  r£  r$  rí   r  )r#   rÊ  ÚlfÚrrrz  r%   r%   r&   Útest_localAsyncForwarder¿  s   


z(SpreadUtilTests.test_localAsyncForwarderN)	r(   r)   r*   r2   r§  r¨  r«  r­  r±  r%   r%   r%   r&   r¥  �  s    r¥  c                   @   r>   )Ú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   r‰   re   ri  Úsecurityr   ÚglobalSecurity©r#   r@  r]   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   r‡   r  re   ri  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   r‰   re   ri  r³  ©r#   r³  r@  r]   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   r‡   r  re   ri  r³  rº  r%   r%   r&   Ú test_serverSecurityCustomizationñ  s   
z;PBWithSecurityOptionsTests.test_serverSecurityCustomizationN)r(   r)   r*   r2   r¶  r·  r»  r¼  r%   r%   r%   r&   r²  Ð  s    		
r²  r.   )�r2   r�  rJ  r�  rG   r&  Úcollectionsr   Úior   rf   Útypingr   Úzope.interfacer   r   Útwisted.credr   r   r	   Útwisted.cred.errorr
   r   Útwisted.internetr   r   r   r   Útwisted.internet.deferr   r   r   Útwisted.internet.errorr   Útwisted.protocols.policiesr   Útwisted.pythonr   r   Útwisted.python.compatr   Útwisted.spreadr   r   r   r   Útwisted.test.proto_helpersr   Útwisted.trialr   ÚViewabler   rj  r!   ÚIRealmr4   r?   rq   rr   r|   rŒ   rf  rŽ   r˜   ÚCopyablerš   Ú
RemoteCopyr¦   ÚsetUnjellyableForClassr©   r³   ÚsetUnjellyableFactoryForClassr´   Ú	Cacheablerº   r¼   r½   ÚRemoteCacherÑ   rÙ   ræ   rë   rõ   rþ   r  r  r
  ÚRootr  r  ÚSynchronousTestCaser  r‡   r3  ÚTestCaser8  rH  r¼  rµ  r¶  r¸  r¹  rÀ  rÆ  ÚPublishablerâ  ÚRemotePublishedrä  r¨  rê  r–   r   rö  rõ  ÚLocalAsRemoter  r8   r  r  r  r   r%  r2  r�  r–  rž  r¡  r¥  r²  r%   r%   r%   r&   Ú<module>   sÄ   	;3!
	
R 7  lC#!6   l&!@