HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //usr/lib/python3/dist-packages/twisted/spread/test/__pycache__/test_pb.cpython-38.pyc
U


W[���@s�dZddlmZmZddlZddlZddlZddlZddlZddl	m
Z
ddlmZ
ddlmZmZddlmZddlmZmZmZmZdd	lmZmZmZmZdd
lmZddlm Z m!Z!m"Z"ddl#m$Z$dd
l%m&Z&m'Z'ddl(m)Z)m*Z*m+Z+ddl,m-Z-m.Z.ddl/m0Z0m1Z1m2Z2ddl3m4Z4Gdd�dej5�Z6Gdd�dej7�Z8ee0j9�Gdd�de:��Z;Gdd�d�Z<dd�Z=Gdd�de:�Z>Gdd�de4�Z?d�d d!�Z@Gd"d#�d#ejA�ZBGd$d%�d%ejA�ZCGd&d'�d'ejD�ZEGd(d)�d)ejF�ZGe�HeEeG�Gd*d+�d+ejD�ZId,d-�ZJe�KeIeJ�Gd.d/�d/ejA�ZLGd0d1�d1ejM�ZNGd2d3�d3ejA�ZOGd4d5�d5ejM�ZPGd6d7�d7ejQ�ZRe�HePeR�Gd8d9�d9ejQ�ZSe�HeNeS�Gd:d;�d;ejA�ZTGd<d=�d=ejA�ZUGd>d?�d?ejA�ZVGd@dA�dAejA�ZWGdBdC�dCejDejFe:�ZXe�HeXeX�GdDdE�dEejDejFe:�ZYe�HeYeY�GdFdG�dGejMejQe:�ZZe�HeZeZ�GdHdI�dIej[�Z\GdJdK�dKej[�Z]GdLdM�dMej^�Z_GdNdO�dOej`�ZaGdPdQ�dQejb�ZcGdRdS�dSejb�ZddTZedafdagdUdV�ZhGdWdX�dXejA�ZiGdYdZ�dZejA�ZjGd[d\�d\ejb�ZkGd]d^�d^ejl�ZmGd_d`�d`ejn�ZoGdadb�dbejA�Zpe�Hemeo�Gdcdd�ddejb�ZqGdedf�dfer�ZsGdgdh�dhejD�ZtGdidj�djejA�ZuGdkdl�dlejv�Zweejx�Gdmdn�dnej7��ZyGdodp�dpe:�ZzGdqdr�drej5�Z{Gdsdt�dtez�Z|Gdudv�dvejb�Z}Gdwdx�dxejb�Z~eejx�Gdydz�dz��ZGd{d|�d|ejb�Z�Gd}d~�d~e�Z�ee��Gdd��d���Z�Gd�d��d�ejb�Z�Gd�d��d�ejb�Z�dS)�z�
Tests for Perspective Broker module.

TODO: update protocol level tests to use new connection API, leaving
only specific tests for old API.
�)�absolute_import�divisionN)�deque)�BytesIO)�implementer�	Interface)�unittest)�pb�util�publish�jelly)�protocol�main�reactor�address)�ConnectionRefusedError)�Deferred�
gatherResults�succeed)�WrappingFactory)�failure�log)�	iterbytes�range�_PY3)�UnauthorizedLogin�UnhandledCredentials)�portal�checkers�credentials)�_FakeConnectorc@seZdZdd�ZdS)�DummycCst|t�rdSdSdS)Nzhello world!zgoodbye, cruel world!)�
isinstance�DummyPerspective��self�user�r'�=/usr/lib/python3/dist-packages/twisted/spread/test/test_pb.py�view_doNothing&s
zDummy.view_doNothingN)�__name__�
__module__�__qualname__r)r'r'r'r(r!%sr!c@seZdZdZdd�ZdS)r#zE
    An L{IPerspective} avatar which will be used in some tests.
    cCst�S�N)r!�r%r'r'r(�perspective_getDummyViewPoint1sz.DummyPerspective.perspective_getDummyViewPointN)r*r+r,�__doc__r/r'r'r'r(r#-sr#c@seZdZdd�ZdS)�
DummyRealmcGs.|D]$}|tjkr|t|�dd�fSqdS)NcSsdSr-r'r'r'r'r(�<lambda>;�z*DummyRealm.requestAvatar.<locals>.<lambda>)r	�IPerspectiver#)r%�avatarId�mindZ
interfacesZifacer'r'r(�
requestAvatar8s
zDummyRealm.requestAvatarN)r*r+r,r7r'r'r'r(r16sr1c@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�IOPumpz�
    Utility to pump data between clients and servers for protocol testing.

    Perhaps this is a utility worthy of being in protocol.py?
    cCs||_||_||_||_dSr-)�client�server�clientIO�serverIO)r%r9r:r;r<r'r'r(�__init__DszIOPump.__init__cCs6d|_t��d}|js2|��r2t��|krdSqdS)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%Ztimeoutr'r'r(�flushKs
zIOPump.flushcCs
d|_dS)zd
        Stop a running L{flush} operation, even if data remains to be
        transferred.
        TN)r?r.r'r'r(�stopYszIOPump.stopcCs�|j�d�|j�d�|j��}|j��}|j�d�|j�d�|j��|j��|jj��|jj��t	|�D]}|j�
|�qxt	|�D]}|j�
|�q�|s�|r�dSdSdS)zX
        Move data back and forth.

        Returns whether any data was moved.
        r�N)r;�seekr<�read�truncater9�	transportZ_checkProducerr:rZdataReceived)r%ZcDataZsDataZbyter'r'r(rAas"



zIOPump.pumpN)r*r+r,r0r=rBrCrAr'r'r'r(r8>s
r8cs�d}��|�}|�|�}t�}t�}|�t�|��|�t�|��t||||�}dd�}	�fdd�}
|�|	|�|�|	|�|�|
�|��|||fS)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.1cSs|js|�t�tj��dSr-)�disconnected�connectionLostr�Failurer�CONNECTION_DONE)�brokerr'r'r(�maybeDisconnect�sz/connectServerAndClient.<locals>.maybeDisconnectcs�jdt�tj�d�dS)N)�	connector�reason)�clientConnectionLostrrLrrMr'��
clientFactoryr'r(�disconnectClientFactory�s
�z7connectServerAndClient.<locals>.disconnectClientFactory)�
buildProtocol�StringIO�makeConnectionr
�FileWrapperr8�
addCleanuprA)�testrT�
serverFactoryZaddr�clientBroker�serverBrokerZclientTransportZserverTransportrArOrUr'rSr(�connectServerAndClient|s

	
r_c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�_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
    cCst�|_dSr-)r�
notificationsr.r'r'r(r=�sz(_ReconnectingFakeConnectorState.__init__cCst�}|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}
        )rra�
appendleft)r%Znotifierr'r'r(�notifyOnConnect�s	z/_ReconnectingFakeConnectorState.notifyOnConnectcCs|jr|j���|�qdS)z1
        Fire all pending notifications.
        N)ra�pop�callbackr.r'r'r(�	notifyAll�sz)_ReconnectingFakeConnectorState.notifyAllN)r*r+r,r0r=rcrfr'r'r'r(r`�sr`cs,eZdZdZ�fdd�Z�fdd�Z�ZS)�_ReconnectingFakeConnectorzc
    A fake L{IConnector} that can fire L{Deferred}s when its
    C{connect} method is called.
    cstt|��|�||_dS)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)�superrgr=�_state)r%r�state��	__class__r'r(r=�s	z#_ReconnectingFakeConnector.__init__cstt|���|j��dS)zM
        A C{connect} implementation that calls C{reconnectCallback}
        N)rhrg�connectrirfr.rkr'r(rm�sz"_ReconnectingFakeConnector.connect)r*r+r,r0r=rm�
__classcell__r'r'rkr(rg�s
rgcCs>|pt�}tjdd�}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)Zguest)	r1r�'InMemoryUsernamePasswordDatabaseDontUser	�PBServerFactoryr�Portal�PBClientFactoryr_)r[�realm�checkerr\rTr'r'r(�connectedServerAndClient�s

rvc@seZdZdd�Zdd�ZdS)�SimpleRemotecCs||_|dS�NrD)�arg�r%ryr'r'r(�remote_thunkszSimpleRemote.remote_thunkcCs
t��dSr-)�	Exceptionrzr'r'r(�remote_knuthszSimpleRemote.remote_knuthN)r*r+r,r{r}r'r'r'r(rwsrwc@seZdZdd�ZdS)�NestedRemotecCst�Sr-)rwr.r'r'r(�remote_getSimple
szNestedRemote.remote_getSimpleN)r*r+r,rr'r'r'r(r~	sr~c@seZdZdd�ZdS)�
SimpleCopycCsd|_ddi|_dg|_dS�NrD�Hello�Worldr[��x�y�zr.r'r'r(r=s
zSimpleCopy.__init__N�r*r+r,r=r'r'r'r(r�sr�c@seZdZdS)�SimpleLocalCopyN�r*r+r,r'r'r'r(r�sr�c@seZdZdZiZdd�ZdS)�SimpleFactoryCopyz]
    @cvar allIDs: hold every created instances of this class.
    @type allIDs: C{dict}
    cCs||_|tj|<dSr-)�idr��allIDs)r%r�r'r'r(r=!szSimpleFactoryCopy.__init__N)r*r+r,r0r�r=r'r'r'r(r�sr�cCsZ|�dd�}|dkr&tdt|�f��|tjkr@tdtjf��tj|}|sVtd��|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 %szfactory class has no ID: %sz&factory method found no object with id)�get�RuntimeError�reprr�r�)rjZstateId�instr'r'r(�createFactoryCopy&s�
�
r�c@seZdZdd�Zdd�ZdS)�
NestedCopycCst�Sr-)r�r.r'r'r(�remote_getCopy;szNestedCopy.remote_getCopycCst|�Sr-)r�)r%�valuer'r'r(�remote_getFactory>szNestedCopy.remote_getFactoryN)r*r+r,r�r�r'r'r'r(r�:sr�c@seZdZdd�ZdS)�SimpleCachecCsd|_ddi|_dg|_dSr�r�r.r'r'r(�	__init___Ds
zSimpleCache.__init___N)r*r+r,r�r'r'r'r(r�Csr�c@seZdZdd�Zdd�ZdS)�NestedComplicatedCachecCst�|_dSr-)�VeryVeryComplicatedCacheable�cr.r'r'r(r=KszNestedComplicatedCache.__init__cCs|jSr-)r�r.r'r'r(�remote_getCacheNsz&NestedComplicatedCache.remote_getCacheN)r*r+r,r=r�r'r'r'r(r�Jsr�c@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
r�cCsd|_d|_d|_dS)NrD���r�r��foor.r'r'r(r=Ssz%VeryVeryComplicatedCacheable.__init__cCsd|_|j�dd�dS)N�r�)r��observer�
callRemoter.r'r'r(�setFoo4Xsz$VeryVeryComplicatedCacheable.setFoo4cCs||_|j|j|jd�S)Nr�)r�r�r�r��r%�perspectiver�r'r'r(�getStateToCacheAndObserveFor\s
�z9VeryVeryComplicatedCacheable.getStateToCacheAndObserveForcCs(t�d�|�d�||jkr$d|_dS)Nzstopped observing�end)r�msgr�r�r�r'r'r(�stoppedObservingbs


z-VeryVeryComplicatedCacheable.stoppedObservingN)r*r+r,r=r�r�r�r'r'r'r(r�Rsr�c@seZdZdd�Zdd�ZdS)�RatherBaroqueCachecCs
||_dSr-�r�)r%ZnewFoor'r'r(�observe_foojszRatherBaroqueCache.observe_foocCst�d�dS)Nzthe end of things)rr�r.r'r'r(�observe_endmszRatherBaroqueCache.observe_endN)r*r+r,r�r�r'r'r'r(r�isr�c@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�SimpleLocalCachecCs|j�|�dSr-)�__dict__�update)r%rjr'r'r(�setCopyableStatetsz!SimpleLocalCache.setCopyableStatecCs|jSr-)�checkr.r'r'r(�checkMethodwszSimpleLocalCache.checkMethodcCs|Sr-r'r.r'r'r(�	checkSelfzszSimpleLocalCache.checkSelfcCsdSrxr'r.r'r'r(r�}szSimpleLocalCache.checkN)r*r+r,r�r�r�r�r'r'r'r(r�ssr�c@s$eZdZdd�Zdd�Zdd�ZdS)�NestedCachecCst�|_dSr-)r�r�r.r'r'r(r=�szNestedCache.__init__cCs|j|jgSr-�r�r.r'r'r(r��szNestedCache.remote_getCachecCs
|j|kSr-r��r%�cacher'r'r(�remote_putCache�szNestedCache.remote_putCacheN)r*r+r,r=r�r�r'r'r'r(r��sr�c@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�
ObservablecCs
g|_dSr-)�	observersr.r'r'r(r=�szObservable.__init__cCs|j�|�dSr-)r��append�r%Zobsr'r'r(�remote_observe�szObservable.remote_observecCs|j�|�dSr-)r��remover�r'r'r(�remote_unobserve�szObservable.remote_unobservecCs|jD]}|�d||�qdS)N�notify)r�r�)r%�objr�r'r'r(r��s
zObservable.notifyN)r*r+r,r=r�r�r�r'r'r'r(r��sr�c@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�DeferredRemotecCs
d|_dS)Nr��runr.r'r'r(r=�szDeferredRemote.__init__cCs||_|dSrxr�rzr'r'r(�runMe�szDeferredRemote.runMecCsdstd��dS)Nrzshouldn't have been run!)�AssertionErrorrzr'r'r(�	dontRunMe�szDeferredRemote.dontRunMecCs 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�szDeferredRemote.remote_doItLaterN)r*r+r,r=r�r�r�r'r'r'r(r��sr�c@seZdZdZdZdd�ZdS)�ObserverrNcCs"||_|jd|_|�d|�dS)NrDZ	unobserve)r��notifiedr�)r%�otherr�r'r'r(�
remote_notify�szObserver.remote_notify)r*r+r,r�r�r�r'r'r'r(r��sr�c@seZdZdd�ZdS)�NewStyleCopycCs
||_dSr-)�s)r%r�r'r'r(r=�szNewStyleCopy.__init__Nr�r'r'r'r(r��sr�c@s(eZdZdZdZdZdd�Zdd�ZdS)�
NewStyleCopy2rrDcCs"tjd7_t�|�}d|_|S)NrDr�)r��	allocated�object�__new__r�)r%r�r'r'r(r��s
zNewStyleCopy2.__new__cCstjd7_dSrx)r��initializedr.r'r'r(r=�szNewStyleCopy2.__init__N)r*r+r,r�r�r�r�r=r'r'r'r(r��s
r�c@seZdZdd�ZdS)�NewStyleCacheCopycCs|jSr-)r�r�r'r'r(r��sz.NewStyleCacheCopy.getStateToCacheAndObserveForN)r*r+r,r�r'r'r'r(r��sr�c@seZdZdd�Zdd�ZdS)�EchoercCs|Sr-r'�r%�str'r'r(�remote_echo�szEchoer.remote_echocKs||fSr-r')r%r��kwr'r'r(�remote_echoWithKeywords�szEchoer.remote_echoWithKeywordsN)r*r+r,r�r�r'r'r'r(r��sr�c@seZdZdd�Zdd�ZdS)�CachedReturnercCs
||_dSr-�r�r�r'r'r(r=�szCachedReturner.__init__cCs|jSr-r�r�r'r'r(�remote_giveMeCache�sz!CachedReturner.remote_giveMeCacheN)r*r+r,r=r�r'r'r'r(r��sr�c@s4eZdZdd�Zdd�Zdd�Zdd�Zd	d
�ZdS)�
NewStyleTestscCsDt�t��|_t��}t|||jd�\}|_|_|�|�	��|_
dS)zY
        Create a pb server using L{Echoer} protocol and connect a client to it.
        )r[rTr\N)r	rqr�r\rsr_r:rAZsuccessResultOf�
getRootObject�ref)r%rTr9r'r'r(�setUp�s�zNewStyleTests.setUpcCsdt_dt_dt_dS)zp
        Close client and server connections, reset values of L{NewStyleCopy2}
        class variables.
        rrDN)r�r�r�r�r.r'r'r(�tearDown�szNewStyleTests.tearDowncs<td���j�d��}�j����fdd�}|�|�|S)zY
        Create a new style object, send it over the wire, and check the result.
        r��echocs,��|t���|jd���|�k�dS)Nr�)�assertIsInstancer��assertEqualr��assertFalse��res��origr%r'r(�cb
sz'NewStyleTests.test_newStyle.<locals>.cb�r�r�r�rArB�addCallback�r%r�r�r'r�r(�
test_newStyles

zNewStyleTests.test_newStylecsVt����tjd���tjd��j�d��}�j����fdd�}|�|�|S)zN
        Send a new style object and check the number of allocations.
        rDr�csF��|t���|jd���tjd���tjd���|��dS)Nr�r�rD)r�r�r�r�r�r��assertIsNotr�r�r'r(r�s
z$NewStyleTests.test_alloc.<locals>.cb)	r�r�r�r�r�r�rArBr�r�r'r�r(�
test_allocs

zNewStyleTests.test_alloccsBtd���jjd�ddd�}�j����fdd�}|�|�|S)zo
        Create a new style object with keywords,
        send it over the wire, and check the result.
        �value1ZechoWithKeywords�one�two�Zkeyword1Zkeyword2csh��|t���|dt���|dt���|djd���|d����|dddd��dS)NrrDr�r�r�r�)r��tupler��dictr�r�r�r�r�r'r(r�/sz3NewStyleTests.test_newStyleWithKeywords.<locals>.cbr�r�r'r�r(�test_newStyleWithKeywords&s
�

z'NewStyleTests.test_newStyleWithKeywordsN)r*r+r,r�r�r�r�r�r'r'r'r(r��s


r�c@s$eZdZdZdZdd�Zdd�ZdS)�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}
    NcCstj�||�t�|_dS)z)
        Initialize the factory.
        N)r	rqr=r�connectionMade)r%�rootr'r'r(r=Isz&ConnectionNotifyServerFactory.__init__cCs*||_|jd}|_|dk	r&|�d�dS)zF
        Store the protocol and fire the connection deferred.
        N)�protocolInstancer�re)r%r
r�r'r'r(�clientConnectionMadeQsz2ConnectionNotifyServerFactory.clientConnectionMade)r*r+r,r0rr=rr'r'r'r(r�;sr�c@s$eZdZdd�Zdd�Zdd�ZdS)�NewStyleCachedTestscsvt��_d�j_t�dtt�j����_t�	�}t�
d�j��j|��fdd�}|�
��|�}�jjj}t||g�S)zi
        Create a pb server using L{CachedReturner} protocol and connect a
        client to it.
        r�rZ	localhostcs
|�_dSr-�r�rr.r'r(�gotRootisz*NewStyleCachedTests.setUp.<locals>.gotRoot)r�r�r�r�	listenTCPr�r�r:r	rs�
connectTCP�getHost�portr�r��factoryr�r)r%rTrZd1Zd2r'r.r(r�]s��
zNewStyleCachedTests.setUpcCs(|jjjj��|jjj��|j��S)z6
        Close client and server connections.
        )r:r	rrH�loseConnectionr�rN�
stopListeningr.r'r'r(r�pszNewStyleCachedTests.tearDowncs8�j�d�j�}�fdd�}|�|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.
        �giveMeCachecsF��|t���d|j����j|�|rB|�_�j�d�j�SdS)Nr�r)	r�r�r�r�r�r�r�r�r�)r�Zagainr.r'r(r��sz2NewStyleCachedTests.test_newStyleCache.<locals>.cbTF)r�r�r�r�r�r'r.r(�test_newStyleCacheys
z&NewStyleCachedTests.test_newStyleCacheN)r*r+r,r�r�r
r'r'r'r(r\s	rc@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�ZdS)$�BrokerTestsNcCs(zt�d�Wntk
r"YnXdS)N�None-None-TESTING.pub)�os�unlink�OSErrorr.r'r'r(r��szBrokerTests.tearDowncCs|�d|f�dS)Nz(This should cause a return value, not %s��fail)r%�errorr'r'r(�
thunkErrorBad�szBrokerTests.thunkErrorBadcCs
||_dSr-)�thunkResult�r%�resultr'r'r(�thunkResultGood�szBrokerTests.thunkResultGoodcCsdSr-r')r%�tbr'r'r(�thunkErrorGood�szBrokerTests.thunkErrorGoodcCs|�d|f�dS)Nz"This should cause an error, not %srrr'r'r(�thunkResultBad�szBrokerTests.thunkResultBadc	Cs�t|d�\}}}Gdd�dtj�}Gdd�dtj�}|�d|��|�d�}|�}|�}|�d||�|��|��|��|�|j|d�|�	|�
d�|�
d��dS)	N�r[c@seZdZdd�ZdS)z%BrokerTests.test_reference.<locals>.XcSs
||_dSr-)�caughtrzr'r'r(�remote_catch�sz2BrokerTests.test_reference.<locals>.X.remote_catchN)r*r+r,r r'r'r'r(�X�sr!c@seZdZdd�ZdS)z%BrokerTests.test_reference.<locals>.YcSs|�d|�dS)NZcatch�r��r%�a�br'r'r(�remote_throw�sz2BrokerTests.test_reference.<locals>.Y.remote_throwN)r*r+r,r&r'r'r'r(�Y�sr'r��throwzX should have caught Z)rvr	�
Referenceable�setNameForLocal�
remoteForNamer�rA�assertIsrr��remoteMethod)	r%r�r�rAr!r'r�r�r�r'r'r(�test_reference�s
zBrokerTests.test_referencecCs�t|d�\}}}||f||ffD]j\}}t�}|�d|�|�d�}d|_|�d|jd��|j|j�|�	�|�	�|�
|j|jd�q dS)Nrr��ZthunkrDzresult wasn't received.)rvrwr*r+ZexpectedThunkResultr�r�rrrAr�r)r%r�r�rAr�r�r��barr'r'r(�test_result�s
��zBrokerTests.test_resultcCs
||_dSr-)�nestedRemoterr'r'r(�refcountResult�szBrokerTests.refcountResultc
Cs�g}g}t|d�\}}}t�}|�d|�|�d�}ttjd�D]6}|jjsV|jjrZqz|�	d��
|j|j�|��qBtjd}	|�
|jjd�|�t|�|	d|	t|�f�dS)Nrr��
�	getSimplerDztransport was not closedzexpected %s got %s)rvr~r*r+rr	ZMAX_BROKER_REFSrH�closedr�r�r�rA�
assertTruer��len)
r%�l�er�r�rAr�r�ZignoZexpectedr'r'r(�test_tooManyRefs�s 


�zBrokerTests.test_tooManyRefscCs�t|d�\}}}t�}|�d|�|�d�}|�d��|j|j�|��|��|�	|j
jd�|�	|j
jdd�|�	|j
j
dd�dS)	Nrr�ZgetCopyrDr�r�rr[)rvr�r*r+r�r�rrrAr�rr�r�r�)r%r�r�rAr�r�r'r'r(�	test_copy�s

�zBrokerTests.test_copycCs�t|d�\}}}t�}t�}|�d|�|�d�}|�d|�|��|�d�|��|��|�d�|��|��|�|j	d�|�
|j	dd�dS)Nrr$ZobserverDr4z
didn't notifyznotified too much)rvr�r�r*r+r�rAr�ZassertIsNotNoner�r�)r%r�r�rAr$r%Zrar'r'r(�test_observe�s


zBrokerTests.test_observecCs�t|d�\}}}t�}|�d|�|�d�}|��|��g}|�d��|j�|��|��|�|j	d�|j
�d�|�|j	dd�|��|��|�|ddd	�dS)
Nrr�Z	doItLaterzDeferred method run too early.r>zDeferred method run too late.r�zIncorrect result.)
rvr�r*r+rAr�r�r�r�r�r�rer�)r%r�r�rAr�r:Zresultsr'r'r(�
test_defers 
zBrokerTests.test_defercCs�t|d�\}}}t�}|�d|�|�d�}|�d��|j|j�|��|��|j	j
}|�||j�|`	t
jdkr~t��|��|��|��|�||j�dS)Nrr�r5�)rvr~r*r+r�r�r3rrAr2�luid�assertInZlocalObjects�sys�
hexversion�gc�collect�assertNotIn)r%r�r�rAr�r0Zrluidr'r'r(�
test_refcounts&

�
zBrokerTests.test_refcountcCsLt|d�\}}}t�}t�}|j}|�d|�|�d|�|�d�}|�d�}g}	|�d��|	j��	|	j�|�d��|	j��	|	j�g}
|�d��|
j�|�d��|
j�|�
�|�|
djd�|�|
dj
d�|�|
djd�|��|�
�|�|
djd	�|�t|	�d�|	dd}|�t�r>|��jn|��j|d
�|�|��|d�g}|�d|��|j�|�
�|�|d�|�|�d�|�d��|j}
|
dj}|�|
|jd
�~	~|�
�~
~|�
�tjdk�r�t��|�
�|� |
|jd�|� |
|j!d�|� ||jd�|� ||j!d�|�"|j#d�dS)Nrr�ZxxxZgetCacherrDr�r�r�zpotential refcounting issuez!other potential refcounting issueZputCachezremote cache doesn't have itr@zServer still had it after GCzClient still had it after GCz!Server still had complex after GCz!Client still had complex after GCzobserver was not removed)$rvr�r�r�r*r+r�r�r��
addErrbackrBr�r�r�r�r�r8ZassertIdenticalrr��__self__Zim_selfr�r7r-rArBZremotelyCachedObjectsrCrDrErFrGZlocallyCachedObjects�assertIsNoner�)r%r�r�rAr�Zobj2ZvccZo2Zo3Zcoll�complex�cpZcol2rAZbaroqueLuidr'r'r(�
test_cache2s�


��
�����

�
�
�
�
�zBrokerTests.test_cachecCszt�d�Wntk
r"YnXt|d�\}}}t�}|�d|�|�d�}g}|�d��|j	|j
�|��|��}|�
|jd�|�
|jd�|�
|jd�|�
|jd�t|d�\}}}|�d|�|�d�}|�d��|j	|j
�|��|��}|�
|jd�dS)Nrrr�ZgetPubrDr)rrrrv�GetPublisherr*r+r�r�r�rrBrdr��activateCalledZisActivated�yayIGotPublishedZ_wasCleanWhenLoaded)r%r�r�rAr�r0Zaccumr�r'r'r(�test_publishableys.

zBrokerTests.test_publishablecCs|j|_dSr-)r�r)r%�valr'r'r(�gotCopy�szBrokerTests.gotCopycCs|t|d�\}}}d}t�}|�d|�|�d�}|�d|��|j|j�|��|��|��|�	|j
|d|j
f�dS)Nr�cr�Z
getFactoryz#ID not correct on factory object %s)rvr�r*r+r�r�rTrrAr�r)r%r�r�rAZIDr�r�r'r'r(�test_factoryCopy�s
�

�zBrokerTests.test_factoryCopy)r*r+r,rr�rrrrr.r1r3r;r<r=r?rHrNrRrTrVr'r'r'r(r�s$
Grs�helloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldcOs|a|adSr-)�callbackArgs�callbackKeyword)�argsr�r'r'r(�finishedCallback�srZc@seZdZdd�Zdd�ZdS)�	PagerizercOs||||_|_|_dSr-)rerYr�)r%rerYr�r'r'r(r=�szPagerizer.__init__cCs.tj|td|jf|j�|j�d|_|_dS)N�d)r
ZStringPager�	bigStringrerYr��r%Z	collectorr'r'r(�remote_getPages�s
���zPagerizer.remote_getPagesN)r*r+r,r=r_r'r'r'r(r[�sr[c@s eZdZdZdd�Zdd�ZdS)�
FilePagerizerNcOs ||_||||_|_|_dSr-)�filenamererYr�)r%rarerYr�r'r'r(r=�szFilePagerizer.__init__cCs6tj|t|jd�|jf|j�|j�|_d|_|_dS)N�rb)r
Z	FilePager�openrarerYr��pagerr^r'r'r(r_�s���zFilePagerizer.remote_getPages)r*r+r,rdr=r_r'r'r'r(r`�sr`c@s@eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�PagingTestsz0
    Test pb objects sending data by pages.
    c	Cs0|��|_t|jd��}|�t�W5QRXdS)z?
        Create a file used to test L{util.FilePager}.
        �wbN)�mktemprarc�writer])r%�fr'r'r(r��s
zPagingTests.setUpcCs�t|d�\}}}|�dttddd��|�d�}g}t�|d��|j�|sV|�	�qH|�
d�|d�td	�|�
t
d
d�|�
tddid�d
S)zg
        Test L{util.StringPager}, passing a callback to fire when all pages
        are sent.
        rr��hellor4�r��getPagesr3r�'Pages received not equal to pages sent!)rj�Completed callback not invokedr�N)rvr*r[rZr+r
�getAllPagesr�r�rAr��joinr]rWrX�r%r�r�rAr�r9r'r'r(�test_pagingWithCallback�s 

���z#PagingTests.test_pagingWithCallbackcCslt|d�\}}}|�dtd��|�d�}g}t�|d��|j�|sP|��qB|�	d�
|d�td�dS)z>
        Test L{util.StringPager} without a callback.
        rr�Nrlr3rrm)rvr*r[r+r
ror�r�rAr�rpr]rqr'r'r(�test_pagingWithoutCallback�s

�z&PagingTests.test_pagingWithoutCallbackc	Cs�|��}t|d���t|d�\}}}t|d�}|�d|�|�d�}g}t�|d��	|j
�d}|s�|dkr�|��|d8}qb|s�|�d	�|�
d
�|d�d
d�dS)z@
        Test L{util.FilePager}, sending an empty file.
        �wrNr0rlr4rrDzgetAllPages timed outr3rm)rgrc�closervr`r*r+r
ror�r�rArr�rp)	r%Z
filenameEmptyr�r�rA�	pagerizerr�r9Zttlr'r'r(�test_emptyFilePaging�s"



�z PagingTests.test_emptyFilePagingcCs�t|d�\}}}t|jtddd�}|�d|�|�d�}g}t�|d��|j	�|s^|�
�qP|�d�|d�t
d	�|�td
d�|�tddid�|�|jjg�d
S)z�
        Test L{util.FilePager}, passing a callback to fire when all pages
        are sent, and verify that the pager doesn't keep chunks in memory.
        r�frodo�	rkr0rlr3rrm)rxrnr�N)rvr`rarZr*r+r
ror�r�rAr�rpr]rWrXrd�chunks�r%r�r�rArvr�r9r'r'r(�test_filePagingWithCallbacks*�

���z'PagingTests.test_filePagingWithCallbackcCs�t|d�\}}}t|jd�}|�d|�|�d�}g}t�|d��|j�|sX|�	�qJ|�
d�|d�td�|�
|j
jg�dS)z<
        Test L{util.FilePager} without a callback.
        rNr0rlr3rrm)rvr`rar*r+r
ror�r�rAr�rpr]rdrzr{r'r'r(�test_filePagingWithoutCallback)s

�z*PagingTests.test_filePagingWithoutCallbackN)
r*r+r,r0r�rrrsrwr|r}r'r'r'r(re�s	rec@seZdZdd�ZdS)�DumbPublishablecCsddiS)NrQrDr'r.r'r'r(�getStateToPublish<sz!DumbPublishable.getStateToPublishN)r*r+r,rr'r'r'r(r~;sr~c@seZdZdd�ZdS)�DumbPubcCs
d|_dSrx)rPr.r'r'r(�	activatedAszDumbPub.activatedN)r*r+r,r�r'r'r'r(r�@sr�c@seZdZdd�Zdd�ZdS)rOcCstd�|_dS)NZTESTING)r~�pubr.r'r'r(r=FszGetPublisher.__init__cCs|jSr-)r�r.r'r'r(�
remote_getPubIszGetPublisher.remote_getPubN)r*r+r,r=r�r'r'r'r(rOEsrOc@s8eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�DisconnectionTestsz'
    Test disconnection callbacks.
    cGstd|f��dS)Nz I shouldn't have been called: %s�r�)r%rYr'r'r(rTszDisconnectionTests.errorcCs
d|_dS)z.
        Called on broker disconnect.
        rDN)�gotCallbackr.r'r'r(�gotDisconnectedXsz"DisconnectionTests.gotDisconnectedcCs|�||j�d|_dS)z7
        Called on RemoteReference disconnect.
        rDN)r��remoteObject�objectCallback�r%�or'r'r(�objectDisconnected^sz%DisconnectionTests.objectDisconnectedcCsft|d�\}}}|��|�dt��|�d�}g}|�dt���|j�|�	�|�
t|�d�dS)Nrr�Z
setBadCopyrD)rvrAr*�
BadCopySetr+r��BadCopyablerIr�rBr�r8)r%r�r�rA�gr9r'r'r(�test_badSerializationes
z(DisconnectionTests.test_badSerializationcCs$t|d�\}}}|��|�dt��|�d�}|��|��|��|�|j�|�|j|j�|�	|j�|�
|j|j�|�|j�|�|j|j�|�|j|j�|�	|j�|�
|j|j�|�
|j|j�|�|j
�|�|j�||_|�t�tj��|�|j�|�|j�dS)Nrr�)rvrAr*rwr+�notifyOnDisconnectrrBZdisconnectsZdontNotifyOnDisconnectrGZ
_disconnectedZdisconnectCallbacksr�r�r�rKrrLrrMr7r�r�)r%r�r�rA�rr'r'r(�test_disconnectionos.
z%DisconnectionTests.test_disconnectionN)	r*r+r,r0rr�r�r�r�r'r'r'r(r�Os
r�c@seZdZdS)�FreakOutNr�r'r'r'r(r��sr�c@seZdZdd�ZdS)r�cCs
t��dSr-)r�)r%�pr'r'r(�getStateToCopyFor�szBadCopyable.getStateToCopyForN)r*r+r,r�r'r'r'r(r��sr�c@seZdZdd�ZdS)r�cCsdSr-r')r%Zbcr'r'r(�remote_setBadCopy�szBadCopySet.remote_setBadCopyN)r*r+r,r�r'r'r'r(r��sr�c@s*eZdZdZdd�Zd
dd�Zdd�Zd	S)�LocalRemoteTestrcCs|dSrxr')r%r�r'r'r(�	sync_add1�szLocalRemoteTest.sync_add1rDcCs||Sr-r')r%r�r�r'r'r(�	async_add�szLocalRemoteTest.async_addcCs
t��dSr-r�r.r'r'r(�
async_fail�szLocalRemoteTest.async_failN)rrD)r*r+r,ZreportAllTracebacksr�r�r�r'r'r'r(r��s
r�c@s@eZdZdZdZZdd�Zdd�Zdd�Zd	d
�Z	dd�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}
    FcCs
||_dSr-�r5�r%r5r'r'r(r=�szMyPerspective.__init__cCs|jS)zT
        Return the avatar identifier which was used to access this avatar.
        r�r.r'r'r(�perspective_getAvatarId�sz%MyPerspective.perspective_getAvatarIdcCst�Sr-)�MyViewr.r'r'r(�perspective_getViewPoint�sz&MyPerspective.perspective_getViewPointcCs||S)z�
        Add the given objects and return the result.  This is a method
        unavailable on L{Echoer}, so it can only be invoked by authenticated
        users who received their avatar from L{TestRealm}.
        r'r#r'r'r(�perspective_add�szMyPerspective.perspective_addcCs
d|_dS�NT��	loggedOutr.r'r'r(�logout�szMyPerspective.logoutN)r*r+r,r0�loggedInr�r=r�r�r�r�r'r'r'r(r��s	r�c@s eZdZdZeZdZdd�ZdS)�	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}.
    NcCs`|tjkst�|dkst�|tjkr6tjt�dd�fS|�|�|_d|j_tj|j|jj	fSdS)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!cSsdSr-r'r'r'r'r(r2�r3z)TestRealm.requestAvatar.<locals>.<lambda>TN)
r	r4r�rZ	ANONYMOUSr��perspectiveFactory�lastPerspectiver�r��r%r5r6�	interfacer'r'r(r7�s
�zTestRealm.requestAvatar)r*r+r,r0r�r�r�r7r'r'r'r(r��sr�c@seZdZdd�ZdS)r�cCs
t|t�Sr-)r"r�r$r'r'r(�
view_check�szMyView.view_checkN)r*r+r,r�r'r'r'r(r��sr�c@s eZdZdZdd�Zdd�ZdS)�
LeakyRealmz]
    A realm which hangs onto a reference to the mind object in its logout
    function.
    cCs
||_dS)z�
        Create a L{LeakyRealm}.

        @param mindEater: a callable that will be called with the C{mind}
        object when it is available
        N)�
_mindEater)r%Z	mindEaterr'r'r(r=szLeakyRealm.__init__cs*|���|�|��tj���fdd�fS)Ncs����fSr-)r�r'�r6Zperspr'r(r2r3z*LeakyRealm.requestAvatar.<locals>.<lambda>)r�r�r	r4r�r'r�r(r7s

zLeakyRealm.requestAvatarN)r*r+r,r0r=r7r'r'r'r(r�s
r�c@seZdZdZdd�ZdS)�NewCredLeakTestsz/
    Tests to try to trigger memory leaks.
    cs�d�_�fdd�}t�t|�d�\}��g�|�d�}|�dd�}dd	�}|�|����fd
d�}|�|�������dg�t�	���
����dS)
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.
        Ncst�|��_dSr-)�weakrefr��mindRef)r6r.r'r(�
setMindRef$sz4NewCredLeakTests.test_logoutLeak.<locals>.setMindRef)r[rtr��loginrocSs$|\}}t�}|�dt�|d�|�S)N�respondro)rwr�r	r�)r�Z	challengeZ
challengerr6r'r'r(�
cbResponse/s
�z4NewCredLeakTests.test_logoutLeak.<locals>.cbResponsecs*�����d���t�td���dS)NrDZboom)rCr�rKrrLr�)�_)�connectionBrokenrAr^r'r(rK5s
z8NewCredLeakTests.test_logoutLeak.<locals>.connectionLostrD)r�rvr�r+r�r�rBr�rErFrK)r%r�r]r�r�r�rKr')r�rAr%r^r(�test_logoutLeaks"�


z NewCredLeakTests.test_logoutLeakN)r*r+r,r0r�r'r'r'r(r�sr�c@s�eZdZdZdd�Zd%dd�Ze�ej	�fdd�Z
d	d
�Zdd�Zd
d�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�ZdS)&�NewCredTestsz=
    Tests related to the L{twisted.cred} support in PB.
    cCs0t�|_t�|j�|_t|j�|_t��|_dS)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�rtrrrr�r\r	rsrTr.r'r'r(r�KszNewCredTests.setUpNcCsRt||j|j�\|_|_|_t�|_tt	�
ddd�|j�|_|j���
|j�dS)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}
        ZTCPrIi�N)r_rTr\r9r:rAr`ZconnectorStatergrZIPv4AddressrPrcr��establishClientAndServer)r%Z_ignoredr'r'r(r�Ws��
�z%NewCredTests.establishClientAndServercCs0|�|jjj�|j�|�|j�|j|�dS)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)r7r9rHr6rKrTrRrP)r%rQr'r'r(�completeClientLostConnectionxsz)NewCredTests.completeClientLostConnectioncsB����j��}�fdd�}�fdd�}|�|�|�|�|S)z�
        Assert that L{PBClientFactory.getRootObject}'s Deferred fires with
        a L{RemoteReference}, and that disconnecting it runs its
        disconnection callbacks.
        cs��|tj�|Sr-)r�r	�RemoteReference��rootObjr.r'r(�
gotRootObject�sz6NewCredTests.test_getRootObject.<locals>.gotRootObjectcs(t�}|�|j��j�����|Sr-)rr�rerT�
disconnectr�)r��disconnectedDeferredr.r'r(r��s

z3NewCredTests.test_getRootObject.<locals>.disconnect�r�rTr�r�)r%�rootObjDeferredr�r�r'r.r(�test_getRootObject�s
	

zNewCredTests.test_getRootObjectcs(����j��}�fdd�}|�|�S)z�
        Test that when a connection is lost, calling a method on a
        RemoteReference obtained from it raises L{DeadReferenceError}.
        cs@t�}��|j���fdd�}|�|��j�����|S)Ncs��tj�jd�dS)N�method)ZassertRaisesr	ZDeadReferenceErrorr�)�ign)r�r%r'r(�lostConnection�s
�zSNewCredTests.test_deadReferenceError.<locals>.gotRootObject.<locals>.lostConnection)rr�rer�rTr�r�)r�r�r�r.r�r(r��s

z;NewCredTests.test_deadReferenceError.<locals>.gotRootObjectr�)r%r�r�r'r.r(�test_deadReferenceError�s
z$NewCredTests.test_deadReferenceErrorcsBGdd�dtj�}|��_����j��}�fdd�}|�|�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@seZdZdZdd�ZdS)z=NewCredTests.test_clientConnectionLost.<locals>.ReconnectOnceFcSs0|j}d|_tj�||||�}|r,|��|Sr�)�reconnectedAlreadyr	rsrRrm)r%rPrQZreconnectingrr'r'r(rR�s�zRNewCredTests.test_clientConnectionLost.<locals>.ReconnectOnce.clientConnectionLostN)r*r+r,r�rRr'r'r'r(�
ReconnectOnce�sr�csH��|tj�t�}|�|j��j������fdd�}|�	|�S)Ncs �j��}�fdd�}|�|�S)Ncs6��|tj�t�}|�|j��j�����|Sr-)	r�r	r�rr�rerTr�r�)ZanotherRootObjr�r.r'r(�gotAnotherRootObject�s
zqNewCredTests.test_clientConnectionLost.<locals>.gotRootObject.<locals>.disconnected.<locals>.gotAnotherRootObject)rTr�r�)r�r�r�r.r'r(rJ�s
zSNewCredTests.test_clientConnectionLost.<locals>.gotRootObject.<locals>.disconnected)
r�r	r�rr�rerTr�r�r�)r�r�rJr.r'r(r��s
z=NewCredTests.test_clientConnectionLost.<locals>.gotRootObject)r	rsrTr�r�r�)r%r�r�r�r'r.r(�test_clientConnectionLost�s
z&NewCredTests.test_clientConnectionLostcCs<|��|j�d�}|�t�t���|�t�	t
j��dS)z�
        Test that if a Broker loses its connection without receiving any bytes,
        it doesn't raise any exceptions or log any errors.
        )rIi90N)r�r\rVrXr
rYrWrKrrLrrM)r%ZserverProtor'r'r(�test_immediateClose�sz NewCredTests.test_immediateClosecCs<t��}|�t�dd��}|�dt�td���|�	|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	rsr�r�UsernamePasswordZclientConnectionFailedrrLr�
assertFailure)r%rTZ
loginDeferredr'r'r(�test_loginConnectionRefused�s
���z(NewCredTests.test_loginConnectionRefusedcs��j�tjdd��t�dd�}d}t���j�||�}��fdd�}�fdd�}|�	|�|�	|��fd	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�cs"���jjj���|tj��Sr-)r7rtr�r�r�r	r��r��ZloginCompletedr%r'r(�cbLogin!sz.NewCredTests.test_loginLogout.<locals>.cbLogincs�j�����dSr-)rTr�r���ignoredr.r'r(�cbDisconnect&s
z3NewCredTests.test_loginLogout.<locals>.cbDisconnectcs���jjj�dSr-)r7rtr�r�r�r.r'r(�cbLogout-sz/NewCredTests.test_loginLogout.<locals>.cbLogoutN)r�registerCheckerrrprr�rrTr�r�r�rArBrErFre)r%Zcredsr6r�r�r�r�r'r�r(�test_loginLogouts&
�





zNewCredTests.test_loginLogoutcs�t��G�fdd�dtj�}|�j_�j�tjdd���j	�
t�dd�d�}�fdd�}|�
|��fd	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.
        cs$eZdZdZdd�Z�fdd�ZdS)z=NewCredTests.test_logoutAfterDecref.<locals>.EventPerspectivezQ
            An avatar which fires a Deferred when it is logged out.
            cSsdSr-r'r�r'r'r(r=IszFNewCredTests.test_logoutAfterDecref.<locals>.EventPerspective.__init__cs��d�dSr-)rer.r�r'r(r�LszDNewCredTests.test_logoutAfterDecref.<locals>.EventPerspective.logoutN)r*r+r,r0r=r�r'r�r'r(�EventPerspectiveEsr�r�r�r�r�cs�Sr-r')Zavatarr�r'r(�
cbLoggedInWsz7NewCredTests.test_logoutAfterDecref.<locals>.cbLoggedIncs���jjji�dSr-)r�r\rZ
_localCleanupr�r.r'r(�cbLoggedOut]sz8NewCredTests.test_logoutAfterDecref.<locals>.cbLoggedOut)rr	�Avatarrtr�rr�rrprTr�rr�r�r�rArBrErF)r%r�r�r�r�r')r�r%r(�test_logoutAfterDecref=s&

�
�



z#NewCredTests.test_logoutAfterDecrefcs��j�tjddd���j�t�dd�d�}�j�t�dd�d�}t||g�}dd�}|�	|��fd	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�squux)r��bazr�r��bazcSs |\}}t|�d�|�d�g�S)NZgetAvatarId)rr�)r�first�secondr'r'r(r�}s
�z5NewCredTests.test_concurrentLogin.<locals>.cbLoggedIncs$|\}}��|d���|d�dS)Nr�r�)r�)r�r�r�r.r'r(�cbAvatarIds�sz6NewCredTests.test_concurrentLogin.<locals>.cbAvatarIds)
rr�rrprTr�rr�rr�r�rArB)r%�
firstLogin�secondLoginr�r�r�r'r.r(�test_concurrentLoginos,��
�
�


z!NewCredTests.test_concurrentLogincs��j�tjdd���j�t�dd��}�j�t�dd��}��|t	���|t	�t
||g�}�fdd�}|�|�����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	wrongpasscs��t�}��t|�d�dS)Nr�)�flushLoggedErrorsrr�r8��ignore�errorsr.r'r(�cleanup�s
z;NewCredTests.test_badUsernamePasswordLogin.<locals>.cleanup)rr�rrprTr�rr�r�rrr�r�rArB)r%r�r�r�r�r'r.r(�test_badUsernamePasswordLogin�s"
�
�
�

z*NewCredTests.test_badUsernamePasswordLogincCsX|j�t���|j�t��d�}dd�}|�|�|�|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�cSs|�dd�S�Nr��{r"r�r'r'r(r��sz4NewCredTests.test_anonymousLogin.<locals>.cbLoggedInr�)
rr�r�AllowAnonymousAccessrTr�r�	Anonymousr�r�r�rArB)r%r�r�r'r'r(�test_anonymousLogin�s

z NewCredTests.test_anonymousLogincs^�j�tjdd���j�t��d�}��|t	��fdd�}|�
|�����j�
�|S)zg
        Verify that without an anonymous checker set up, anonymous login is
        rejected.
        �passr�r�cs��t�}��t|�d�dSrx)r�rr�r8r�r.r'r(r��s
z=NewCredTests.test_anonymousLoginNotPermitted.<locals>.cleanup)rr�rrprTr�rr�r�rr�r�rArB�r%r�r�r'r.r(�test_anonymousLoginNotPermitted�s
�

z,NewCredTests.test_anonymousLoginNotPermittedcCsl|j�t���|j�tjdd��|j�t��d�}dd�}|�	|�|�	|j
d�|��|j�
�|S)z�
        Like L{test_anonymousLogin} but against a portal with a checker for
        both IAnonymous and IUsernamePassword.
        r�r�r�cSs|�dd�Sr�r"r�r'r'r(r��szENewCredTests.test_anonymousLoginWithMultipleCheckers.<locals>.cbLoginr�)rr�rr�rprTr�rr�r�r�r�rArB�r%r�r�r'r'r(�'test_anonymousLoginWithMultipleCheckers�s
�

z4NewCredTests.test_anonymousLoginWithMultipleCheckerscCsp|j�t���|j�tjdd��|j�t�dd�d�}dd�}|�	|�|�	|j
d�|��|j�
�|S)z�
        Like L{test_anonymousLoginWithMultipleCheckers} but check that
        username/password authentication works.
        r�r�r�r�cSs|�ddd�S)N�addr\�r"r�r'r'r(r��szINewCredTests.test_authenticatedLoginWithMultipleCheckers.<locals>.cbLoginr�)rr�rr�rprTr�rr�r�r�r�rArBr�r'r'r(�+test_authenticatedLoginWithMultipleCheckers�s
�
�

z8NewCredTests.test_authenticatedLoginWithMultipleCheckerscCsp|j�tjdd��|j�t�dd�d�}dd�}|�|�dd�}|�|�|�|j	�|�
�|j��|S)	zb
        Verify that a viewpoint can be retrieved after authenticating with
        cred.
        r�r�r�r�cSs
|�d�S)NZgetViewPointr"r�r'r'r(r�sz'NewCredTests.test_view.<locals>.cbLogincSs
|�d�S)Nr�r")Z	viewpointr'r'r(�cbViewsz&NewCredTests.test_view.<locals>.cbView)
rr�rrprTr�rr�r�r7r�rArB)r%r�r�rr'r'r(�	test_views
�
�


zNewCredTests.test_view)N)r*r+r,r0r�r�rrLrrMr�r�r�r�r�r�r�r�r�r�r�r�r�rrr'r'r'r(r�Gs&
"
�
0/2 r�c@s$eZdZdd�Zdd�Zdd�ZdS)�NonSubclassingPerspectivecCsdSr-r'r�r'r'r(r=sz"NonSubclassingPerspective.__init__cCs(|�||�}|�||�}|�|||f�Sr-)ZunserializeZ	serialize)r%rN�messagerY�kwargsr'r'r(�perspectiveMessageReceived"sz4NonSubclassingPerspective.perspectiveMessageReceivedcCs
d|_dSr�r�r.r'r'r(r�(sz NonSubclassingPerspective.logoutN)r*r+r,r=rr�r'r'r'r(rsrc@s eZdZdZdd�Zdd�ZdS)�NSPTestsz
    Tests for authentication against a realm where the L{IPerspective}
    implementation is not a subclass of L{Avatar}.
    cCs�t�|_t|j_t�|j�|_t��|_|j�	dd�|j�
|j�tt�
|j��|_tjd|jdd�|_|�|jj�|j��j|_dS)Nr�r�rrI)r�)r�rtrr�rrrrrpruZaddUserr�rr	rqr	rrrrZrr�portnor.r'r'r(r�2s
zNSPTests.setUpcsnt�����t�dd�d�}t�d�j��|�dd��|��j	ddd	d
if���fdd�}|�|�|S)
z�
        An L{IPerspective} implementation which does not subclass
        L{Avatar} can expose remote methods for the client to call.
        r�r�r�rIcSs|jdddd�S)N�ANYTHING�herer�)r0r")r�r'r'r(r2Hr3z#NSPTests.test_NSP.<locals>.<lambda>r
)rr0r�cs$����jjD]}|j��qdSr-)r�r	Z	protocolsrHr
)r�r��r	r%r'r(r�Ksz"NSPTests.test_NSP.<locals>.cleanup)
r	rsr�rr�rrr	r�r�r�r'rr(�test_NSP?s��
zNSPTests.test_NSPN)r*r+r,r0r�r
r'r'r'r(r-s
rc@s eZdZdZdd�Zdd�ZdS)�
IForwardedzA
    Interface used for testing L{util.LocalAsyncForwarder}.
    cCsdS)z,
        Simple synchronous method.
        Nr'r'r'r'r(�	forwardMeYszIForwarded.forwardMecCsdS)z-
        Simple asynchronous method.
        Nr'r'r'r'r(�forwardDeferred^szIForwarded.forwardDeferredN)r*r+r,r0rrr'r'r'r(rTsrc@s0eZdZdZdZdZdd�Zdd�Zdd�Zd	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}
    FcCs
d|_dS)z6
        Set a local flag to test afterwards.
        TN)�	forwardedr.r'r'r(rqszForwarded.forwardMecCs
d|_dS)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(�
dontForwardMewszForwarded.dontForwardMecCstd�S)z0
        Asynchronously return C{True}.
        T)rr.r'r'r(r~szForwarded.forwardDeferredN)	r*r+r,r0rrrrrr'r'r'r(rdsrc@s8eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�SpreadUtilTestsz+
    Tests for L{twisted.spread.util}.
    cCst�}|�|�dd�d�dS)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�szSpreadUtilTests.test_synccCs:t�}t�}|jdddd�}|�|t�|�|jd�|S)zm
        Call an asynchronous method of a L{util.LocalAsRemote} object and check
        the result.
        r�r�r�)r�r>)r�r�r�rr�r�)r%r�r�r'r'r(�
test_async�szSpreadUtilTests.test_asynccs4t�}|�d�}�fdd�}|��fdd�|�|S)zG
        Test an asynchronous failure on a remote method call.
        rcs��|tj�|�t�dSr-)r�rrLZtrapr�)rir.r'r(�eb�sz*SpreadUtilTests.test_asyncFail.<locals>.ebcs
��d�S)Nzsupposed to failrr�r.r'r(r2�r3z0SpreadUtilTests.test_asyncFail.<locals>.<lambda>)r�r�r�)r%r�r�rr'r.r(�test_asyncFail�s

zSpreadUtilTests.test_asyncFailcCs$t�}|�d�}|�|d�d�dS)zM
        Test the C{remoteMethod} facility of L{util.LocalAsRemote}.
        rr�r�N)r�r-r�)r%r��mr'r'r(�test_remoteMethod�s
z!SpreadUtilTests.test_remoteMethodcCslt�}t�|t�}|�d�|�|j�|�d�|�|j�|�d�}g}|�	|j
�|�|dd�dS)ze
        Test a call to L{util.LocalAsyncForwarder} using L{Forwarded} local
        object.
        rrrrrDN)rr
ZLocalAsyncForwarderrr�r7rr�rr�r�r�)r%riZlfZrrr9r'r'r(�test_localAsyncForwarder�s


z(SpreadUtilTests.test_localAsyncForwarderN)	r*r+r,r0rrrrrr'r'r'r(r�src@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�PBWithSecurityOptionsTestsz&
    Test security customization.
    cCs&t��}|�d�}|�|jtj�dS)zl
        By default, client broker should use C{jelly.globalSecurity} as
        security settings.
        N)r	rsrVr,�securityr�globalSecurity�r%r	rNr'r'r(�!test_clientDefaultSecurityOptions�s
z<PBWithSecurityOptionsTests.test_clientDefaultSecurityOptionscCs*t�t��}|�d�}|�|jtj�dS)zl
        By default, server broker should use C{jelly.globalSecurity} as
        security settings.
        N)r	rqr�rVr,rrr r!r'r'r(�!test_serverDefaultSecurityOptions�s
z<PBWithSecurityOptionsTests.test_serverDefaultSecurityOptionscCs0t��}tj|d�}|�d�}|�|j|�dS)zs
        Check that the security settings are passed from the client factory to
        the broker object.
        �rN)r�SecurityOptionsr	rsrVr,r�r%rr	rNr'r'r(� test_clientSecurityCustomization�s
z;PBWithSecurityOptionsTests.test_clientSecurityCustomizationcCs4t��}tjt�|d�}|�d�}|�|j|�dS)zs
        Check that the security settings are passed from the server factory to
        the broker object.
        r$N)rr%r	rqr�rVr,rr&r'r'r(� test_serverSecurityCustomization�s
z;PBWithSecurityOptionsTests.test_serverSecurityCustomizationN)r*r+r,r0r"r#r'r(r'r'r'r(r�s


r)N)�r0Z
__future__rrrCrr@rEr��collectionsr�iorrWZzope.interfacerrZ
twisted.trialrZtwisted.spreadr	r
rrZtwisted.internetr
rrrZtwisted.internet.errorrZtwisted.internet.deferrrrZtwisted.protocols.policiesrZtwisted.pythonrrZtwisted.python.compatrrrZtwisted.cred.errorrrZtwisted.credrrrZtwisted.test.proto_helpersr ZViewabler!r�r#ZIRealmr�r1r8r_r`rgrvr)rwr~ZCopyabler�Z
RemoteCopyr�ZsetUnjellyableForClassr�r�ZsetUnjellyableFactoryForClassr�Z	Cacheabler�r�r�ZRemoteCacher�r�r�r�r�r�r�r�r�ZRootr�r�ZSynchronousTestCaser�rqr�ZTestCaserrr]rWrXrZr[r`reZPublishabler~ZRemotePublishedr�rOr�r|r�r�r�Z
LocalAsRemoter�r4r�r�r�r�r�r�rrrrrrr'r'r'r(�<module>s�
(	>4$
		
		Q!6
pD'#0X' ?