o
    Óó·i6÷  ã                   @   sr  d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z
 ddlmZ ddlmZmZ ddlmZmZmZ ddlmZmZ dd	lmZmZmZmZ dd
lmZmZmZ ddl m!Z! ddl"m#Z# ddl$m%Z% ddl&m'Z'm(Z( ddl)m*Z* ddl+m,Z,m-Z-m.Z.m/Z/ ddl0m1Z1 G dd„ de-j2ƒZ3G dd„ de-j4ƒZ5eej6ƒG dd„ dƒƒZ7G dd„ dƒZ8dd„ Z9G dd„ dƒZ:G dd„ de#ƒZ;d…d d!„Z<G d"d#„ d#e-j=ƒZ>G d$d%„ d%e-j=ƒZ?G d&d'„ d'e-j@ƒZAG d(d)„ d)e-jBƒZCe- DeAeC¡ G d*d+„ d+e-j@ƒZEd,d-„ ZFe- GeEeF¡ G d.d/„ d/e-j=ƒZHG d0d1„ d1e-jIƒZJG d2d3„ d3e-j=ƒZKG d4d5„ d5e-jIƒZLG d6d7„ d7e-jMƒZNe- DeLeN¡ G d8d9„ d9e-jMƒZOe- DeJeO¡ G d:d;„ d;e-j=ƒZPG d<d=„ d=e-j=ƒZQG d>d?„ d?e-j=ƒZRG d@dA„ dAe-j=ƒZSG dBdC„ dCe-j@e-jBƒZTe- DeTeT¡ G dDdE„ dEe-j@e-jBƒZUe- DeUeU¡ G dFdG„ dGe-jIe-jMƒZVe- DeVeV¡ G dHdI„ dIe-jWƒZXG dJdK„ dKe-jWƒZYG dLdM„ dMe1jZƒZ[G dNdO„ dOe-j\ƒZ]G dPdQ„ dQe1j^ƒZ_G dRdS„ dSe1j^ƒZ`dTZadabdacdUdV„ ZdG dWdX„ dXe-j=ƒZeG dYdZ„ dZe-j=ƒZfG d[d\„ d\e1j^ƒZgG d]d^„ d^e.jhƒZiG d_d`„ d`e.jjƒZkG dadb„ dbe-j=ƒZle- Deiek¡ G dcdd„ dde1j^ƒZmG dedf„ dfenƒZoG dgdh„ dhe-j@ƒZpG didj„ dje-j=ƒZqG dkdl„ dle/jrƒZsee-jtƒG dmdn„ dne-j4ƒƒZuG dodp„ dpƒZvG dqdr„ dre-j2ƒZwG dsdt„ dtevƒZxG dudv„ dve1j^ƒZyG dwdx„ dxe1j^ƒZzee-jtƒG dydz„ dzƒƒZ{G d{d|„ d|e1j^ƒZ|G d}d~„ d~eƒZ}ee}ƒG dd€„ d€ƒƒZ~G d�d‚„ d‚e1j^ƒZG dƒd„„ d„e1j^ƒZ€dS )†z�
Tests for Perspective Broker module.

TODO: update protocol level tests to use new connection API, leaving
only specific tests for old API.
é    N)Údeque)ÚBytesIO)ÚDict)Ú	InterfaceÚimplementer)ÚcheckersÚcredentialsÚportal)ÚUnauthorizedLoginÚUnhandledCredentials)ÚaddressÚmainÚprotocolÚreactor)ÚDeferredÚgatherResultsÚsucceed)ÚConnectionRefusedError)Ú_FakeConnector)ÚWrappingFactory)ÚfailureÚlog)Ú	iterbytes)ÚjellyÚpbÚpublishÚutil)Úunittestc                   @   ó   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%   úaC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-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ÚmindZ
interfacesZ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>   r?   r@   rA   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)rD   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   é   N)r@   ÚseekrA   ÚreadÚtruncater>   Ú	transportZ_checkProducerr?   r   ZdataReceived)r#   ZcDataZsDataÚbyter%   r%   r&   rF   a   s"   



zIOPump.pumpN)r(   r)   r*   r2   rB   rH   rJ   rF   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   rU   r   rV   r%   ©ÚclientFactoryr%   r&   ÚdisconnectClientFactory�   s   
ÿz7connectServerAndClient.<locals>.disconnectClientFactoryN)ÚbuildProtocolÚStringIOÚmakeConnectionr   ÚFileWrapperr=   Ú
addCleanuprF   )Útestr]   ÚserverFactoryÚaddrÚclientBrokerÚserverBrokerZclientTransportZserverTransportrF   rX   r^   r%   r\   r&   ÚconnectServerAndClient{   s   

	

ri   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&   rB   ·   ó   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}
        N)r   rl   Ú
appendleft)r#   Z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)rl   ÚpopÚcallbackr/   r%   r%   r&   Ú	notifyAllÇ   s   ÿz)_ReconnectingFakeConnectorState.notifyAllN)r(   r)   r*   r2   rB   ro   rr   r%   r%   r%   r&   rj   ®   s
    rj   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)ÚsuperrB   Ú_state)r#   r   Ústate©Ú	__class__r%   r&   rB   Õ   s   	
z#_ReconnectingFakeConnector.__init__c                    s   t ƒ  ¡  | j ¡  dS )zM
        A C{connect} implementation that calls C{reconnectCallback}
        N)rt   Úconnectru   rr   r/   rw   r%   r&   ry   á   s   
z"_ReconnectingFakeConnector.connect)r(   r)   r*   r2   rB   ry   Ú__classcell__r%   r%   rw   r&   rs   Ï   s    rs   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guestN)	r4   r   Ú'InMemoryUsernamePasswordDatabaseDontUser   ÚPBServerFactoryr	   ÚPortalÚPBClientFactoryri   )rd   ÚrealmÚcheckerre   r]   r%   r%   r&   ÚconnectedServerAndClienté   s
   
r‚   c                   @   ó   e Zd Zdd„ Zdd„ ZdS )ÚSimpleRemotec                 C   ó   || _ |d S ©NrL   )Ú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 ©NrL   ÚHelloÚWorldrd   ©ÚxÚyÚzr/   r%   r%   r&   rB     ó   
zSimpleCopy.__init__N©r(   r)   r*   rB   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&   rB     s   zSimpleFactoryCopy.__init__N)	r(   r)   r*   r2   r    r   ÚintÚ__annotations__rB   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    )rv   Z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   rk   r.   )ÚVeryVeryComplicatedCacheableÚcr/   r%   r%   r&   rB   D  rm   zNestedComplicatedCache.__init__c                 C   ó   | j S r.   )r³   r/   r%   r%   r&   Úremote_getCacheG  r1   z&NestedComplicatedCache.remote_getCacheN)r(   r)   r*   rB   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 )NrL   é   é   ©r–   r—   Úfoor/   r%   r%   r&   rB   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*   rB   r¾   rÂ   rÅ   r%   r%   r%   r&   r²   K  s
    r²   c                   @   rƒ   )ÚRatherBaroqueCachec                 C   ó
   || _ d S r.   ©rº   )r#   Z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#   rv   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   rk   r.   )r¯   r–   r/   r%   r%   r&   rB   }  rm   zNestedCache.__init__c                 C   s   | j | j gS r.   ©r–   r/   r%   r%   r&   rµ   €  rm   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*   rB   rµ   rÞ   r%   r%   r%   r&   rÚ   |  s    rÚ   c                   @   r¶   )
Ú
Observablec                 C   s
   g | _ d S r.   )Ú	observersr/   r%   r%   r&   rB   ˆ  rÊ   zObservable.__init__c                 C   rÎ   r.   )rà   Úappend©r#   Z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*   rB   rã   rå   ræ   r%   r%   r%   r&   rß   ‡  rØ   rß   c                   @   r¶   )
ÚDeferredRemotec                 C   ó
   d| _ d S )Nr   ©Úrunr/   r%   r%   r&   rB   —  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.
        N)r   ÚaddCallbacksrì   rí   Úd)r#   rï   r%   r%   r&   Úremote_doItLater¡  s   zDeferredRemote.remote_doItLaterN)r(   r)   r*   rB   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 )NrL   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                   @   r   )ÚNewStyleCopyc                 C   rÇ   r.   )Ús)r#   rö   r%   r%   r&   rB   ·  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   rL   c                 C   s"   t  jd7  _t | ¡}d|_|S )NrL   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&   rB   É  s   zNewStyleCopy2.__init__N)r(   r)   r*   rø   rû   r«   rú   rB   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&   rB   á  rÊ   zCachedReturner.__init__c                 C   r´   r.   r  rþ   r%   r%   r&   Úremote_giveMeCacheä  r1   z!CachedReturner.remote_giveMeCacheN)r(   r)   r*   rB   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.
        )rd   r]   re   N)r   r}   rý   re   r   ri   r?   rF   ZsuccessResultOfÚgetRootObjectÚref)r#   r]   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   rL   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>.cbN©rõ   r  r½   rF   rH   Ú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.
        rL   r  c                    sF   ˆ  | t¡ ˆ | jd¡ ˆ tjd¡ ˆ tjd¡ ˆ | ˆ ¡ d S )Nr·   r¸   rL   )r  r÷   r  r«   rø   rû   ÚassertIsNotr  r  r%   r&   r    s
   z$NewStyleTests.test_alloc.<locals>.cbN)	r÷   r  rø   rû   r  r½   rF   rH   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   rL   r  r  r  r  )r  Útuplerõ   Údictr  rö   r  r  r  r%   r&   r  .  s   z3NewStyleTests.test_newStyleWithKeywords.<locals>.cbNr  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   r}   rB   r   ÚconnectionMade)r#   Úrootr%   r%   r&   rB   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)ÚprotocolInstancer"  rq   )r#   r   rï   r%   r%   r&   ÚclientConnectionMadeP  s
   ÿz2ConnectionNotifyServerFactory.clientConnectionMade)r(   r)   r*   r2   r$  rB   r%  r%   r%   r%   r&   r!  :  s
    r!  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>.gotRootN)rü   r  rö   r   Ú	listenTCPr!  r  r?   r   r   Ú
connectTCPÚgetHostÚportr  r  Úfactoryr"  r   )r#   r]   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.
        N)r?   r.  r$  rP   ÚloseConnectionr  rW   Ú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«   r1  )	r  rü   r  rö   r  r  r  r  r½   )r  Zagainr/   r%   r&   r    s   ûz2NewStyleCachedTests.test_newStyleCache.<locals>.cbTFN)r  r½   r  r  r  r%   r/   r&   Útest_newStyleCachew  s
   z&NewStyleCachedTests.test_newStyleCacheN)r(   r)   r*   r	  r
  r2  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 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   r8  )Nz This should cause an error, not r9  r?  r%   r%   r&   ÚthunkResultBad¤  r=  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©rd   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*   rG  r%   r%   r%   r&   ÚXª  r+   rH  c                   @   r   )z%BrokerTests.test_reference.<locals>.Yc                 S   s   |  d|¡ d S )NZcatch©r½   ©r#   ÚaÚbr%   r%   r&   Úremote_throw¯  rÒ   z2BrokerTests.test_reference.<locals>.Y.remote_throwN)r(   r)   r*   rM  r%   r%   r%   r&   ÚY®  r+   rN  r—   ÚthrowzX should have caught Z)r‚   r   ÚReferenceableÚsetNameForLocalÚremoteForNamer½   rF   ÚassertIsrF  r  ÚremoteMethod)	r#   r³   rö   rF   rH  rN  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 )NrE  rº   é   ZthunkrL   zresult wasn't received.)r‚   r„   rQ  rR  ZexpectedThunkResultr½   rî   rA  r<  rF   r  r>  )r#   r³   rö   rF   r–   r—   rº   Úbarr%   r%   r&   Útest_result¿  s   
ÿ
ÿòzBrokerTests.test_resultc                 C   rÇ   r.   )ÚnestedRemoter?  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- q=| 	d¡ 
|j|j¡ | ¡  q!tjd }	|  |jjd¡ |  t|ƒ|	d|	› dt|ƒ› �¡ d S )	NrE  rº   é
   Ú	getSimplerL   ztransport was not closedz	expected z got )r‚   rŽ   rQ  rR  Úranger   ZMAX_BROKER_REFSrP   Úclosedr½   rî   rá   rF   Ú
assertTruer  Úlen)
r#   ÚlÚer³   rö   rF   rº   r–   ZignoÚexpectedr%   r%   r&   Útest_tooManyRefsÖ  s   


&zBrokerTests.test_tooManyRefsc                 C   sŽ   t | d�\}}}tƒ }| d|¡ | d¡}| d¡ | j| j¡ | ¡  | ¡  |  	| j
jd¡ |  	| j
jd d¡ |  	| j
jd d¡ d S )	NrE  rº   ZgetCopyrL   r“   r”   r   rd   )r‚   r©   rQ  rR  r½   rî   rA  r<  rF   r  r>  r–   r—   r˜   )r#   r³   rö   rF   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 )NrE  rK  ZobserverL   r[  zdidn't notifyznotified too much)r‚   rß   rñ   rQ  rR  r½   rF   ræ   ZassertIsNotNonerç   r  )r#   r³   rö   rF   rK  rL  Ú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 )
NrE  rï   Z	doItLaterzDeferred method run too early.rC   zDeferred method run too late.r   é   zIncorrect result.)r‚   rè   rQ  rR  rF   r½   r  rá   r  rë   rï   rq   r  )r#   r³   rö   rF   rï   rb  Ú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 )NrE  rº   r\  é   )r‚   rŽ   rQ  rR  r½   rî   rZ  r<  rF   rY  ÚluidÚassertInZlocalObjectsÚsysÚ
hexversionÚgcÚcollectÚassertNotIn)r#   r³   rö   rF   rº   rW  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 )NrE  rç   ZxxxZgetCacher   rL   r·   r¸   r»   zpotential refcounting issuez!other potential refcounting issueZputCachezremote cache doesn't have itrk  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³   rQ  rR  r½   r  rá   Ú
addErrbackrH   r  r–   r—   rº   r¾   r`  ZassertIdenticalrÔ   Ú__self__rÕ   r_  rT  rl  rm  ZremotelyCachedObjectsrn  ro  rp  rq  rr  ZlocallyCachedObjectsÚassertIsNoner¼   )r#   r³   rö   rF   rç   Úobj2ZvccZo2Zo3ZcollÚcomplexÚcpZcol2rl  Z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 )Nr4  rE  rº   ZgetPubrL   r   )r5  r6  r7  r‚   ÚGetPublisherrQ  rR  r½   rî   rá   r<  rH   rp   r  ÚactivateCalledZisActivatedÚyayIGotPublishedZ_wasCleanWhenLoaded)r#   r³   rö   rF   rº   rW  Úaccumrç   r%   r%   r&   Útest_publishablev  s0   ÿ

zBrokerTests.test_publishablec                 C   s   |j | _d S r.   )r¡   r>  )r#   Úvalr%   r%   r&   ÚgotCopy’  rm   zBrokerTests.gotCopyc                 C   s|   t | d�\}}}d}tƒ }| d|¡ | d¡}| d|¡ | j| j¡ | ¡  | ¡  | ¡  |  	| j
|d| j
› �¡ d S )NrE  éc   rº   Z
getFactoryz!ID not correct on factory object )r‚   r©   rQ  rR  r½   rî   r�  rD  rF   r  r>  )r#   r³   rö   rF   ÚIDrç   r–   r%   r%   r&   Útest_factoryCopy•  s   

ýzBrokerTests.test_factoryCopy)r(   r)   r*   r>  r
  r<  rA  rC  rD  rU  rX  rZ  rd  re  rg  rj  rs  rz  r  r�  r„  r%   r%   r%   r&   r3  ‘  s&    Br3  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.   )rq   r‡  r  )r#   rq   r‡  r  r%   r%   r&   rB   ³  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Ú	bigStringrq   r‡  r  ©r#   Ú	collectorr%   r%   r&   Úremote_getPages¶  s   
ÿÿÿzPagerizer.remote_getPagesN)r(   r)   r*   rB   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.   )Úfilenamerq   r‡  r  )r#   r�  rq   r‡  r  r%   r%   r&   rB   À  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�  rq   r‡  r  ÚpagerrŒ  r%   r%   r&   rŽ  Ä  s   ÿÿÿzFilePagerizer.remote_getPages)r(   r)   r*   r“  rB   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.
        rE  rº   Úhellor[  ©r«   ÚgetPagesó    r   ú'Pages received not equal to pages sent!)r™  úCompleted callback not invokedr«   N)r‚   rQ  r‰  rˆ  rR  r   ÚgetAllPagesr  rá   rF   r  Újoinr‹  r…  r†  ©r#   r³   rö   rF   r–   ra  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.
        rE  rº   Nr›  rœ  r   r�  )r‚   rQ  r‰  rR  r   rŸ  r  rá   rF   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.
        ÚwrE  NrW  r›  r[  r   rL   zgetAllPages timed outrœ  r�  )r–  r’  Úcloser‚   r�  rQ  rR  r   rŸ  r  rá   rF   r:  r  r   )	r#   ZfilenameEmptyr³   rö   rF   Ú	pagerizerr–   ra  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	¡ |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.
        rE  Úfrodoé	   rš  rW  r›  rœ  r   r�  )r¨  rž  r«   N)r‚   r�  r�  rˆ  rQ  rR  r   rŸ  r  rá   rF   r  r   r‹  r…  r†  r“  Úchunks©r#   r³   rö   rF   r¦  r–   ra  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.
        rE  NrW  r›  rœ  r   r�  )r‚   r�  r�  rQ  rR  r   rŸ  r  rá   rF   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}  rL   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 )NZTESTING)r®  Úpubr/   r%   r%   r&   rB   B  rÌ   zGetPublisher.__init__c                 C   r´   r.   )r²  r/   r%   r%   r&   Úremote_getPubE  r1   zGetPublisher.remote_getPubN)r(   r)   r*   rB   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&   r;  Q  rÌ   zDisconnectionTests.errorc                 C   rI   )z.
        Called on broker disconnect.
        rL   N)ÚgotCallbackr/   r%   r%   r&   ÚgotDisconnectedT  ó   
z"DisconnectionTests.gotDisconnectedc                 C   s   |   || j¡ d| _dS )z7
        Called on RemoteReference disconnect.
        rL   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 )NrE  r½  Z
setBadCopyrL   )r‚   rF   rQ  Ú
BadCopySetrR  r½   ÚBadCopyablert  rá   rH   r  r`  )r#   r³   rö   rF   Úgra  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 )NrE  r½  )r‚   rF   rQ  r„   rR  ÚnotifyOnDisconnectr;  rm  ZdisconnectsZdontNotifyOnDisconnectrr  Z_disconnectedZdisconnectCallbacksr¸  r¾  rº  rT   r   rU   r   rV   r_  r·  r»  )r#   r³   rö   rF   Úrr%   r%   r&   Útest_disconnectionk  s.   
z%DisconnectionTests.test_disconnectionN)	r(   r)   r*   r2   r;  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#   Z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_add1rL   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   rL   )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   rÇ   r.   ©r9   ©r#   r9   r%   r%   r&   rB   ¶  rÊ   zMyPerspective.__init__c                 C   r´   )zT
        Return the avatar identifier which was used to access this avatar.
        NrÐ  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}.
        Nr%   rJ  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Ø  rB   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>TN)	r   r8   r   Z	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#   Z	mindEaterr%   r%   r&   rB   ú  s   
zLeakyRealm.__init__c                    s*   |   ˆ ¡ |  |¡‰tjˆ‡ ‡fdd„fS )Nc                      s   ˆ ˆ  ¡ fS r.   )rÙ  r%   ©r:   Z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   rB   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)rd   r€   r#  Úloginr{   c                 S   s$   | \}}t ƒ }| dt |d¡|¡S )NÚrespondr{   )r„   r½   r   rë  )r–   Ú	challengeZ
challengerr:   r%   r%   r&   Ú
cbResponse$  s
   ÿz4NewCredLeakTests.test_logoutLeak.<locals>.cbResponsec                    s*   ˆ  ¡  ˆ  d¡ ˆ t tdƒ¡¡ d S )NrL   Zboom)rJ   rá   rT   r   rU   r¥   )Ú_)ÚconnectionBrokenrF   rh   r%   r&   rT   -  s   
z8NewCredLeakTests.test_logoutLeak.<locals>.connectionLostrL   )rè  r‚   rã  rR  r½   r  rH   r  rp  rq  rv  )r#   ré  rg   r#  rï   rí  rT   r%   )rï  rF   r#   rh   r&   Útest_logoutLeak  s    ÿ


z NewCredLeakTests.test_logoutLeakN)r(   r)   r*   r2   rð  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~   r!  re   r   r   r]   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TCPrR   iá  N)ri   r]   re   r>   r?   rF   rj   ZconnectorStaters   r   ÚIPv4AddressrY   ro   r  ÚestablishClientAndServer)r#   Z_ignoredr%   r%   r&   ró  O  s   
ÿÿz%NewCredTests.establishClientAndServerc                 C   s0   |   | jjj¡ | j |¡ | j | j|¡ dS )aŒ  
        Asserts that the client broker's transport was closed and then
        mimics the event loop by calling the broker's connectionLost
        callback with C{reason}, followed by C{self.clientFactory}'s
        C{clientConnectionLost}

        @param reason: (optional) the reason to pass to the client
            broker's connectionLost callback
        @type reason: L{Failure}
        N)r_  r>   rP   r^  rT   r]   r[   rY   )r#   rZ   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Ã  rq   r]   Ú
disconnectrô  )r÷  ÚdisconnectedDeferredr/   r%   r&   rù  �  s
   
z3NewCredTests.test_getRootObject.<locals>.disconnectN©ró  r]   r  r  )r#   ÚrootObjDeferredrø  rù  r%   r/   r&   Útest_getRootObjectƒ  s   

	
zNewCredTests.test_getRootObjectc                    s(   ˆ   ¡  ˆ j ¡ }‡ fdd„}| |¡S )z“
        Test that when a connection is lost, calling a method on a
        RemoteReference obtained from it raises L{DeadReferenceError}.
        c                    s@   t ƒ }ˆ  |j¡ ‡ ‡fdd„}| |¡ ˆj ¡  ˆ ¡  |S )Nc                    s   ˆ  tjˆ jd¡ d S )NÚmethod)ÚassertRaisesr   ZDeadReferenceErrorr½   )Úign)r÷  r#   r%   r&   ÚlostConnectionª  s   zSNewCredTests.test_deadReferenceError.<locals>.gotRootObject.<locals>.lostConnection)r   rÃ  rq   r  r]   rù  rô  )r÷  rú  r  r/   rö  r&   rø  ¦  s   

z;NewCredTests.test_deadReferenceError.<locals>.gotRootObjectNrû  )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   r[   ry   )r#   rY   rZ   Zreconnectingr@  r%   r%   r&   r[   À  s   ÿzRNewCredTests.test_clientConnectionLost.<locals>.ReconnectOnce.clientConnectionLostN)r(   r)   r*   r  r[   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Ã  rq   r]   rù  rô  )ZanotherRootObjrï   r/   r%   r&   ÚgotAnotherRootObjectÛ  s   
zqNewCredTests.test_clientConnectionLost.<locals>.gotRootObject.<locals>.disconnected.<locals>.gotAnotherRootObject)r]   r  r  )r   rï   r  r/   r%   r&   rS   Ø  s   

zSNewCredTests.test_clientConnectionLost.<locals>.gotRootObject.<locals>.disconnected)
r  r   rõ  r   rÃ  rq   r]   rù  rô  r  )r÷  rï   rS   r/   r%   r&   rø  Ï  s   

z=NewCredTests.test_clientConnectionLost.<locals>.gotRootObjectN)r   r   r]   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.
        )rR   i90  N)ró  re   r_   ra   r   rb   r`   rT   r   rU   r   rV   )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   r   rê  r   ÚUsernamePasswordZclientConnectionFailedr   rU   r   ÚassertFailure)r#   r]   ZloginDeferredr%   r%   r&   Útest_loginConnectionRefusedó  s   
ÿÿþz(NewCredTests.test_loginConnectionRefusedc                    s®   ˆj  tjdd�¡ t dd¡}d}tƒ ‰ ˆj ||¡}‡ ‡fdd„}‡fdd„}| 	|¡ | 	|¡ ‡fd	d
„}| 	|¡ ˆ 
¡  ˆj ¡  t ¡  ˆj ¡  ˆ  d¡ |S )zŸ
        Test that login can be performed with IUsernamePassword credentials and
        that when the connection is dropped the avatar is logged out.
        ó   pass©r$   ó   userrÜ  c                    s"   ˆ  ˆjjj¡ ˆ | tj¡ ˆ S r.   )r_  r€   rÞ  rÚ  r  r   rõ  ©rÁ   ©ZloginCompletedr#   r%   r&   ÚcbLogin  s   z.NewCredTests.test_loginLogout.<locals>.cbLoginc                    s   ˆ j  ¡  ˆ  ¡  d S r.   )r]   rù  rô  ©Úignoredr/   r%   r&   ÚcbDisconnect  s   
z3NewCredTests.test_loginLogout.<locals>.cbDisconnectc                    s   ˆ   ˆ jjj¡ d S r.   )r_  r€   rÞ  rØ  r  r/   r%   r&   ÚcbLogout&  r=  z/NewCredTests.test_loginLogout.<locals>.cbLogoutN)r	   ÚregisterCheckerr   r|   r   r
  r   r]   rê  r  ró  rF   rH   rp  rq  rq   )r#   Úcredsr:   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   r5   r.   r%   rÑ  r%   r%   r&   rB   C  rÖ   zFNewCredTests.test_logoutAfterDecref.<locals>.EventPerspective.__init__c                    s   ˆ   d ¡ d S r.   )rq   r/   r×  r%   r&   rÙ  F  rÌ   zDNewCredTests.test_logoutAfterDecref.<locals>.EventPerspective.logoutN)r(   r)   r*   r2   rB   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  re   r$  Z_localCleanupr  r/   r%   r&   ÚcbLoggedOutZ  s   z8NewCredTests.test_logoutAfterDecref.<locals>.cbLoggedOutN)r   r   ÚAvatarr€   rÝ  r	   r  r   r|   r]   rê  r   r
  r  ró  rF   rH   rp  rq  )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>.cbAvatarIdsN)r	   r  r   r|   r]   rê  r   r
  r   r  ró  rF   rH   )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                    ó   ˆ   t¡}ˆ  t|ƒd¡ d S )Nr·   )ÚflushLoggedErrorsr
   r  r`  ©ÚignoreÚerrorsr/   r%   r&   Úcleanup¥  ó   
z;NewCredTests.test_badUsernamePasswordLogin.<locals>.cleanupN)r	   r  r   r|   r]   rê  r   r
  r  r
   r   r  ró  rF   rH   )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   ó   |   dd¡S ©Nr  é{   rI  r  r%   r%   r&   r  ¹  rm   z4NewCredTests.test_anonymousLogin.<locals>.cbLoggedInr1  N)r	   r  r   ÚAllowAnonymousAccessr]   rê  r   Ú	Anonymousr  r  ró  rF   rH   )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                    r'  r†   )r(  r   r  r`  r)  r/   r%   r&   r,  Ï  r-  z=NewCredTests.test_anonymousLoginNotPermitted.<locals>.cleanupN)r	   r  r   r|   r]   rê  r   r3  r  r   r  ró  rF   rH   ©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   r/  r0  rI  r  r%   r%   r&   r  å  rm   zENewCredTests.test_anonymousLoginWithMultipleCheckers.<locals>.cbLoginr1  N)r	   r  r   r2  r|   r]   rê  r   r3  r  r  ró  rF   rH   ©r#   rï   r  r%   r%   r&   Ú'test_anonymousLoginWithMultipleCheckersÚ  s   
ÿ

z4NewCredTests.test_anonymousLoginWithMultipleCheckersc                 C   sp   | j  t ¡ ¡ | j  tjdd�¡ | j t dd¡d¡}dd„ }| 	|¡ | 	| j
d¡ |  ¡  | j ¡  |S )	z€
        Like L{test_anonymousLoginWithMultipleCheckers} but check that
        username/password authentication works.
        r  r  r  rÜ  c                 S   s   |   ddd¡S )NÚaddrŠ  é   rI  r  r%   r%   r&   r  þ  rÌ   zINewCredTests.test_authenticatedLoginWithMultipleCheckers.<locals>.cbLoginr1  N)r	   r  r   r2  r|   r]   rê  r   r
  r  r  ró  rF   rH   r8  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   ó
   |   d¡S )NZgetViewPointrI  r  r%   r%   r&   r    rÊ   z'NewCredTests.test_view.<locals>.cbLoginc                 S   r=  )NrÓ   rI  )Z	viewpointr%   r%   r&   ÚcbView  rÊ   z&NewCredTests.test_view.<locals>.cbViewN)r	   r  r   r|   r]   rê  r   r
  r  r_  ró  rF   rH   )r#   rï   r  r>  r%   r%   r&   Ú	test_view
  s   
ÿÿ


zNewCredTests.test_viewr.   )r(   r)   r*   r2   r	  ró  r   rU   r   rV   rô  rý  r  r  r  r  r  r  r&  r.  r4  r7  r9  r<  r?  r%   r%   r%   r&   rñ  ?  s(    

!
ÿ3
16%rñ  c                   @   rÙ   )ÚNonSubclassingPerspectivec                 C   r5   r.   r%   rÑ  r%   r%   r&   rB   *  rÖ   z"NonSubclassingPerspective.__init__c                 C   s(   |  || ¡}|  || ¡}| |||f¡S r.   )ZunserializeÚ	serialize)r#   rW   Ú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*   rB   rD  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 )Nr  r  r   rR   )rà  )rÛ  r€   r@  rÝ  r	   r~   r   r|   r�   ZaddUserr  r   r   r}   r.  r   r*  r-  rc   r0  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Ü  rR   c                 S   s   | j dddd�S )NÚANYTHINGÚherer  )rW  rI  )rÇ  r%   r%   r&   r6   R  s    z#NSPTests.test_NSP.<locals>.<lambda>rG  )rH  rW  r  c                    s$   ˆ   ¡  ˆjjD ]}|j ¡  qd S r.   )rù  r.  Ú	protocolsrP   r/  )r  rÇ  ©r.  r#   r%   r&   r,  U  s   ÿz"NSPTests.test_NSP.<locals>.cleanupN)
r   r   rê  r   r
  r   r+  rF  r  r  r6  r%   rJ  r&   Útest_NSPJ  s   
zNSPTests.test_NSPN)r(   r)   r*   r2   r	  rK  r%   r%   r%   r&   rE  8  s    rE  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   rM  rN  r%   r%   r%   r&   rL  ^  s    rL  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   rI   )z6
        Set a local flag to test afterwards.
        TN)Ú	forwardedr/   r%   r%   r&   rM  |  r¹  zForwarded.forwardMec                 C   rI   )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‚  rK   zForwarded.dontForwardMec                 C   s   t dƒS )z0
        Asynchronously return C{True}.
        TN)r   r/   r%   r%   r&   rN  ‰  s   zForwarded.forwardDeferredN)	r(   r)   r*   r2   rP  rQ  rM  rR  rN  r%   r%   r%   r&   rO  n  s    	rO  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—   rh  N)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   rU   Ztrapr¥   )r˜  r/   r%   r&   Úeb°  s   z*SpreadUtilTests.test_asyncFail.<locals>.ebc                    s
   ˆ   d¡S )Nzsupposed to failr9  r  r/   r%   r&   r6   ´  s   
 z0SpreadUtilTests.test_asyncFail.<locals>.<lambda>N)rÊ  r½   rî   )r#   r½  rï   rW  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}.
        rT  r¸   r»   N)rÊ  rT  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.
        rM  rR  rN  r   rL   N)rO  r   ZLocalAsyncForwarderrL  r½   r_  rP  r  rQ  r  rá   r  )r#   r˜  ÚlfÚrrra  r%   r%   r&   Útest_localAsyncForwarder¿  s   


z(SpreadUtilTests.test_localAsyncForwarderN)	r(   r)   r*   r2   rU  rV  rX  rZ  r]  r%   r%   r%   r&   rS  �  s    rS  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   r_   rS  Úsecurityr   ÚglobalSecurity©r#   r.  rW   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ý   r_   rS  r_  r   r`  ra  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   r_   rS  r_  ©r#   r_  r.  rW   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.
        rd  N)r   re  r   r}   rý   r_   rS  r_  rf  r%   r%   r&   Ú test_serverSecurityCustomizationñ  s   
z;PBWithSecurityOptionsTests.test_serverSecurityCustomizationN)r(   r)   r*   r2   rb  rc  rg  rh  r%   r%   r%   r&   r^  Ð  s    		
r^  r.   )�r2   rp  r5  rn  rE   rç  Úcollectionsr   Úior   r`   Útypingr   Zzope.interfacer   r   Ztwisted.credr   r   r	   Ztwisted.cred.errorr
   r   Ztwisted.internetr   r   r   r   Ztwisted.internet.deferr   r   r   Ztwisted.internet.errorr   Ztwisted.internet.testingr   Ztwisted.protocols.policiesr   Ztwisted.pythonr   r   Ztwisted.python.compatr   Ztwisted.spreadr   r   r   r   Ztwisted.trialr   ZViewabler   r  r!   ZIRealmr4   r=   ri   rj   rs   r‚   rP  r„   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  r}   r!  ZTestCaser&  r3  r‹  r…  r†  rˆ  r‰  r�  r”  ZPublishabler®  ZRemotePublishedr°  r{  rµ  rŒ   rÆ  rÀ  r¿  ZLocalAsRemoterÊ  r8   rÏ  rÛ  rÓ  rã  ræ  rñ  r@  rE  rL  rO  rS  r^  r%   r%   r%   r&   Ú<module>   sÄ   	;3!
	
R 7  lC#!6   l&!@