AnonSec Shell
Server IP : 209.38.156.173  /  Your IP : 216.73.216.122   [ Reverse IP ]
Web Server : Apache/2.4.52 (Ubuntu)
System : Linux lakekumayuhotel 5.15.0-136-generic #147-Ubuntu SMP Sat Mar 15 15:53:30 UTC 2025 x86_64
User : root ( 0)
PHP Version : 8.1.2-1ubuntu2.22
Disable Function : NONE
Domains : 2 Domains
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : ON
Directory :  /lib/python3/dist-packages/twisted/spread/test/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME ]     [ BACKUP SHELL ]     [ JUMPING ]     [ MASS DEFACE ]     [ SCAN ROOT ]     [ SYMLINK ]     

Current File : /lib/python3/dist-packages/twisted/spread/test/__pycache__/test_pb.cpython-310.pyc
o

�b8��@srdZddlZddlZddlZddlZddlZddlmZddlm	Z
ddlmZddl
mZmZddlmZmZmZddlmZmZdd	lmZmZmZmZdd
lmZmZmZddl m!Z!ddl"m#Z#dd
l$m%Z%m&Z&ddl'm(Z(ddl)m*Z*m+Z+m,Z,m-Z-ddl.m/Z/ddl0m1Z1Gdd�de+j2�Z3Gdd�de+j4�Z5eej6�Gdd�d��Z7Gdd�d�Z8dd�Z9Gdd�d�Z:Gdd�de/�Z;d�d d!�Z<Gd"d#�d#e+j=�Z>Gd$d%�d%e+j=�Z?Gd&d'�d'e+j@�ZAGd(d)�d)e+jB�ZCe+�DeAeC�Gd*d+�d+e+j@�ZEd,d-�ZFe+�GeEeF�Gd.d/�d/e+j=�ZHGd0d1�d1e+jI�ZJGd2d3�d3e+j=�ZKGd4d5�d5e+jI�ZLGd6d7�d7e+jM�ZNe+�DeLeN�Gd8d9�d9e+jM�ZOe+�DeJeO�Gd:d;�d;e+j=�ZPGd<d=�d=e+j=�ZQGd>d?�d?e+j=�ZRGd@dA�dAe+j=�ZSGdBdC�dCe+j@e+jB�ZTe+�DeTeT�GdDdE�dEe+j@e+jB�ZUe+�DeUeU�GdFdG�dGe+jIe+jM�ZVe+�DeVeV�GdHdI�dIe+jW�ZXGdJdK�dKe+jW�ZYGdLdM�dMe1jZ�Z[GdNdO�dOe+j\�Z]GdPdQ�dQe1j^�Z_GdRdS�dSe1j^�Z`dTZadabdacdUdV�ZdGdWdX�dXe+j=�ZeGdYdZ�dZe+j=�ZfGd[d\�d\e1j^�ZgGd]d^�d^e,jh�ZiGd_d`�d`e,jj�ZkGdadb�dbe+j=�Zle+�Deiek�Gdcdd�dde1j^�ZmGdedf�dfen�ZoGdgdh�dhe+j@�ZpGdidj�dje+j=�ZqGdkdl�dle-jr�Zsee+jt�Gdmdn�dne+j4��ZuGdodp�dp�ZvGdqdr�dre+j2�ZwGdsdt�dtev�ZxGdudv�dve1j^�ZyGdwdx�dxe1j^�Zzee+jt�Gdydz�dz��Z{Gd{d|�d|e1j^�Z|Gd}d~�d~e�Z}ee}�Gdd��d���Z~Gd�d��d�e1j^�ZGd�d��d�e1j^�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.
�N)�deque)�BytesIO)�Dict)�	Interface�implementer)�checkers�credentials�portal)�UnauthorizedLogin�UnhandledCredentials)�address�main�protocol�reactor)�Deferred�
gatherResults�succeed)�ConnectionRefusedError)�WrappingFactory)�failure�log)�	iterbytes)�jelly�pb�publish�util)�_FakeConnector)�unittestc@�eZdZdd�ZdS)�DummycCst|t�rdSdS)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'�rc@�eZdZdZdd�ZdS)r!zE
    An L{IPerspective} avatar which will be used in some tests.
    cC�t�S�N)r�r#r%r%r&�perspective_getDummyViewPoint4�z.DummyPerspective.perspective_getDummyViewPointN)r(r)r*�__doc__r0r%r%r%r&r!/�r!c@r)�
DummyRealmcGs.|D]}|tjur|t|�dd�fSqdS)NcS�dSr.r%r%r%r%r&�<lambda>=�z*DummyRealm.requestAvatar.<locals>.<lambda>)r�IPerspectiver!)r#�avatarId�mind�
interfaces�ifacer%r%r&�
requestAvatar:s

��zDummyRealm.requestAvatarN)r(r)r*r=r%r%r%r&r48sr4c@�0eZdZdZdd�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#r@rArBrCr%r%r&�__init__Gs
zIOPump.__init__cCsNd|_t��d}|js!|��r#t��|krdS|js%|��sdSdSdSdS)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&�flushMs�zIOPump.flushcC�
d|_dS)zd
        Stop a running L{flush} operation, even if data remains to be
        transferred.
        TN)rFr/r%r%r&�stopZ�
zIOPump.stopcCs�|j�d�|j�d�|j��}|j��}|j�d�|j�d�|j��|j��|jj��|jj��t	|�D]}|j�
|�q<t	|�D]}|j�
|�qI|sV|rXdSdS)zX
        Move data back and forth.

        Returns whether any data was moved.
        r�)rB�seekrC�read�truncater@�	transport�_checkProducerrAr�dataReceived)r#�cData�sData�byter%r%r&rHas"



zIOPump.pumpN)r(r)r*r2rDrJrLrHr%r%r%r&r?@s
r?cs�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��dSdSr.)�disconnected�connectionLostr�Failurer
�CONNECTION_DONE)�brokerr%r%r&�maybeDisconnect�s�z/connectServerAndClient.<locals>.maybeDisconnectcs�jdt�tj�d�dS)N)�	connector�reason)�clientConnectionLostrr[r
r\r%��
clientFactoryr%r&�disconnectClientFactory�s
�z7connectServerAndClient.<locals>.disconnectClientFactory)�
buildProtocol�StringIO�makeConnectionr�FileWrapperr?�
addCleanuprH)�testrc�
serverFactory�addr�clientBroker�serverBroker�clientTransport�serverTransportrHr^rdr%rbr&�connectServerAndClient{s

	

rqc@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
    cC�t�|_dSr.)r�
notificationsr/r%r%r&rD��z(_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}
        )rrt�
appendleft)r#�notifierr%r%r&�notifyOnConnect�s	z/_ReconnectingFakeConnectorState.notifyOnConnectcCs$|jr|j���|�|jsdSdS)z1
        Fire all pending notifications.
        N)rt�pop�callbackr/r%r%r&�	notifyAll�s�z)_ReconnectingFakeConnectorState.notifyAllN)r(r)r*r2rDrxr{r%r%r%r&rr�s

rrcs,eZdZdZ�fdd�Z�fdd�Z�ZS)�_ReconnectingFakeConnectorzc
    A fake L{IConnector} that can fire L{Deferred}s when its
    C{connect} method is called.
    cst��|�||_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)�superrD�_state)r#r�state��	__class__r%r&rD�s	
z#_ReconnectingFakeConnector.__init__cst���|j��dS)zM
        A C{connect} implementation that calls C{reconnectCallback}
        N)r}�connectr~r{r/r�r%r&r��s
z"_ReconnectingFakeConnector.connect)r(r)r*r2rDr��
__classcell__r%r%r�r&r|�sr|cCs>|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)�guest)	r4r�'InMemoryUsernamePasswordDatabaseDontUser�PBServerFactoryr	�Portal�PBClientFactoryrq)rj�realm�checkerrkrcr%r%r&�connectedServerAndClient�s

r�c@�eZdZdd�Zdd�ZdS)�SimpleRemotecC�||_|dS�NrN)�arg�r#r�r%r%r&�remote_thunk��zSimpleRemote.remote_thunkcC�t��r.)�	Exceptionr�r%r%r&�remote_knuth�r1zSimpleRemote.remote_knuthN)r(r)r*r�r�r%r%r%r&r��sr�c@r)�NestedRemotecCr-r.)r�r/r%r%r&�remote_getSimpler1zNestedRemote.remote_getSimpleN)r(r)r*r�r%r%r%r&r�r+r�c@r)�
SimpleCopycC�d|_ddi|_dg|_dS�NrN�Hello�Worldrj��x�y�zr/r%r%r&rD�
zSimpleCopy.__init__N�r(r)r*rDr%r%r%r&r�r+r�c@�eZdZdS)�SimpleLocalCopyN�r(r)r*r%r%r%r&r�
�r�c@s.eZdZUdZiZeedfed<dd�ZdS)�SimpleFactoryCopyz]
    @cvar allIDs: hold every created instances of this class.
    @type allIDs: C{dict}
    �allIDscCs||_|tj|<dSr.)�idr�r�)r#r�r%r%r&rDszSimpleFactoryCopy.__init__N)	r(r)r*r2r�r�int�__annotations__rDr%r%r%r&r�s
r�cCsZ|�dd�}|durtdt|�����|tjvr tdtj����tj|}|s+td��|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 zfactory class has no ID: z&factory method found no object with id)�get�RuntimeError�reprr�r�)r�stateId�instr%r%r&�createFactoryCopy!s

r�c@r�)�
NestedCopycCr-r.)r�r/r%r%r&�remote_getCopy5r1zNestedCopy.remote_getCopycCst|�Sr.)r�)r#�valuer%r%r&�remote_getFactory8�zNestedCopy.remote_getFactoryN)r(r)r*r�r�r%r%r%r&r�4�r�c@r)�SimpleCachecCr�r�r�r/r%r%r&�	__init___=r�zSimpleCache.__init___N)r(r)r*r�r%r%r%r&r�<r+r�c@r�)�NestedComplicatedCachecCrsr.)�VeryVeryComplicatedCacheable�cr/r%r%r&rDDruzNestedComplicatedCache.__init__cC�|jSr.)r�r/r%r%r&�remote_getCacheGr1z&NestedComplicatedCache.remote_getCacheN)r(r)r*rDr�r%r%r%r&r�Cr�r�c@�,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
r�cCsd|_d|_d|_dS)NrN���r�r��foor/r%r%r&rDLs
z%VeryVeryComplicatedCacheable.__init__cCsd|_|j�dd�dS)N�r�)r��observer�
callRemoter/r%r%r&�setFoo4Q�z$VeryVeryComplicatedCacheable.setFoo4cCs||_|j|j|jd�S)Nr�)r�r�r�r��r#�perspectiver�r%r%r&�getStateToCacheAndObserveForUr�z9VeryVeryComplicatedCacheable.getStateToCacheAndObserveForcCs,t�d�|�d�||jkrd|_dSdS)Nzstopped observing�end)r�msgr�r�r�r%r%r&�stoppedObservingYs




�z-VeryVeryComplicatedCacheable.stoppedObservingN)r(r)r*rDr�r�r�r%r%r%r&r�Ks
r�c@r�)�RatherBaroqueCachecC�
||_dSr.�r�)r#�newFoor%r%r&�observe_fooa�
zRatherBaroqueCache.observe_foocCst�d�dS)Nzthe end of things)rr�r/r%r%r&�observe_endd�zRatherBaroqueCache.observe_endN)r(r)r*r�r�r%r%r%r&r�`r�r�c@r�)
�SimpleLocalCachecC�|j�|�dSr.)�__dict__�update)r#rr%r%r&�setCopyableStatel�z!SimpleLocalCache.setCopyableStatecCr�r.)�checkr/r%r%r&�checkMethodor1zSimpleLocalCache.checkMethodcCs|Sr.r%r/r%r%r&�	checkSelfr�zSimpleLocalCache.checkSelfcC�dSr�r%r/r%r%r&r�ur�zSimpleLocalCache.checkN)r(r)r*r�r�r�r�r%r%r%r&r�k�
r�c@�$eZdZdd�Zdd�Zdd�ZdS)�NestedCachecCrsr.)r�r�r/r%r%r&rD}ruzNestedCache.__init__cCs|j|jgSr.�r�r/r%r%r&r��ruzNestedCache.remote_getCachecCs
|j|uSr.r��r#�cacher%r%r&�remote_putCache�r�zNestedCache.remote_putCacheN)r(r)r*rDr�r�r%r%r%r&r�|sr�c@r�)
�
ObservablecCs
g|_dSr.)�	observersr/r%r%r&rD�r�zObservable.__init__cCr�r.)r��append�r#�obsr%r%r&�remote_observe�r�zObservable.remote_observecCr�r.)r��remover�r%r%r&�remote_unobserve�r�zObservable.remote_unobservecCs|jD]	}|�d||�qdS)N�notify)r�r�)r#�objr�r%r%r&r��s
�zObservable.notifyN)r(r)r*rDr�r�r�r%r%r%r&r��r�r�c@r�)
�DeferredRemotecC�
d|_dS)Nr��runr/r%r%r&rD�r�zDeferredRemote.__init__cCr�r�r�r�r%r%r&�runMe�r�zDeferredRemote.runMecCsJd��)Nrzshouldn't have been run!r%r�r%r%r&�	dontRunMe�r�zDeferredRemote.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*rDr�r�r�r%r%r%r&r��s
r�c@seZdZdZdZdd�ZdS)�ObserverrNcCs"||_|jd|_|�d|�dS)NrN�	unobserve)r��notifiedr�)r#�otherr�r%r%r&�
remote_notify�szObserver.remote_notify)r(r)r*rr�rr%r%r%r&r��sr�c@r)�NewStyleCopycCr�r.)�s)r#rr%r%r&rD�r�zNewStyleCopy.__init__Nr�r%r%r%r&r�r+rc@s(eZdZdZdZdZdd�Zdd�ZdS)�
NewStyleCopy2rrNcCs"tjd7_t�|�}d|_|S)NrNr�)r�	allocated�object�__new__r�)r#r�r%r%r&r�s
zNewStyleCopy2.__new__cCstjd7_dSr�)r�initializedr/r%r%r&rD�szNewStyleCopy2.__init__N)r(r)r*rr	r�rrDr%r%r%r&r�src@r)�NewStyleCacheCopycCr�r.)r�r�r%r%r&r��r1z.NewStyleCacheCopy.getStateToCacheAndObserveForN)r(r)r*r�r%r%r%r&r
�r+r
c@r�)�EchoercCs|Sr.r%�r#�str%r%r&�remote_echo�r�zEchoer.remote_echocKs||fSr.r%)r#r
�kwr%r%r&�remote_echoWithKeywords�r�zEchoer.remote_echoWithKeywordsN)r(r)r*rrr%r%r%r&r�r�rc@r�)�CachedReturnercCr�r.�r�r�r%r%r&rD�r�zCachedReturner.__init__cCr�r.rrr%r%r&�remote_giveMeCache�r1z!CachedReturner.remote_giveMeCacheN)r(r)r*rDrr%r%r%r&r�r�rc@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.
        )rjrcrkN)rr�rrkr�rqrArH�successResultOf�
getRootObject�ref)r#rcr@r%r%r&�setUp�s�zNewStyleTests.setUpcCsdt_dt_dt_dS)zp
        Close client and server connections, reset values of L{NewStyleCopy2}
        class variables.
        rrNN)rrr	r�r/r%r%r&�tearDown�s
zNewStyleTests.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���|�u�dS)Nr�)�assertIsInstancer�assertEqualr�assertFalse��res��origr#r%r&�cbsz'NewStyleTests.test_newStyle.<locals>.cb�rrr�rHrJ�addCallback�r#r�r"r%r r&�
test_newStyle�s

zNewStyleTests.test_newStylecsVt����tjd���tjd��j�d��}�j����fdd�}|�|�|S)zN
        Send a new style object and check the number of allocations.
        rNrcsF��|t���|jd���tjd���tjd���|��dS)Nr�r�rN)rrrr�rr	�assertIsNotrr r%r&r"s
z$NewStyleTests.test_alloc.<locals>.cb)	rrrr	rr�rHrJr$r%r%r r&�
test_alloc
s

	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.
        �value1�echoWithKeywords�one�two��keyword1�keyword2csh��|t���|dt���|dt���|djd���|d����|dddd��dS)NrrNr)r+r,r-)r�tupler�dictrrr'rr r%r&r".sz3NewStyleTests.test_newStyleWithKeywords.<locals>.cbr#r%r%r r&�test_newStyleWithKeywords#s�

z'NewStyleTests.test_newStyleWithKeywordsN)r(r)r*rrr&r(r2r%r%r%r&r�s	rc@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)rr�rDr�connectionMade)r#�rootr%r%r&rDIsz&ConnectionNotifyServerFactory.__init__cCs.||_|jd}|_|dur|�d�dSdS)zF
        Store the protocol and fire the connection deferred.
        N)�protocolInstancer4rz)r#rr�r%r%r&�clientConnectionMadePs
�z2ConnectionNotifyServerFactory.clientConnectionMade)r(r)r*r2r6rDr7r%r%r%r&r3:s
r3c@r�)�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�r�	localhostcs
|�_dSr.�rr:r/r%r&�gotRoothr�z*NewStyleCachedTests.setUp.<locals>.gotRoot)r
r!rr�	listenTCPr3rrArr��
connectTCP�getHost�portrr$�factoryr4r)r#rcr;�d1�d2r%r/r&r[s�
zNewStyleCachedTests.setUpcCs(|jjjj��|jjj��|j��S)z6
        Close client and server connections.
        )rAr@r6rR�loseConnectionrr]�
stopListeningr/r%r%r&ros
zNewStyleCachedTests.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|�|r!|�_�j�d�j�SdS)Nr�rE)	rr
rrr'r!rrr�)r�againr/r%r&r"s�z2NewStyleCachedTests.test_newStyleCache.<locals>.cbTF)rr�r!r$r%r%r/r&�test_newStyleCachews

z&NewStyleCachedTests.test_newStyleCacheN)r(r)r*rrrGr%r%r%r&r8Zsr8c@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�WdStyYdSw)N�None-None-TESTING.pub)�os�unlink�OSErrorr/r%r%r&r�s
�zBrokerTests.tearDowncC�|�d|���dS)Nz&This should cause a return value, not ��fail)r#�errorr%r%r&�
thunkErrorBad��zBrokerTests.thunkErrorBadcCr�r.)�thunkResult�r#�resultr%r%r&�thunkResultGood�r�zBrokerTests.thunkResultGoodcCr5r.r%)r#�tbr%r%r&�thunkErrorGood�r�zBrokerTests.thunkErrorGoodcCrM)Nz This should cause an error, not rNrTr%r%r&�thunkResultBad�rRzBrokerTests.thunkResultBadc	Cs�t|d�\}}}Gdd�dtj�}Gdd�dtj�}|�d|��|�d�}|�}|�}|�d||�|��|��|��|�|j|d�|�	|�
d�|�
d��dS)	N�rjc@r)z%BrokerTests.test_reference.<locals>.XcSr�r.)�caughtr�r%r%r&�remote_catch�r�z2BrokerTests.test_reference.<locals>.X.remote_catchN)r(r)r*r\r%r%r%r&�X�r+r]c@r)z%BrokerTests.test_reference.<locals>.YcSs|�d|�dS)N�catch�r��r#�a�br%r%r&�remote_throw�r�z2BrokerTests.test_reference.<locals>.Y.remote_throwN)r(r)r*rcr%r%r%r&�Y�r+rdr��throwzX should have caught Z)r�r�
Referenceable�setNameForLocal�
remoteForNamer�rH�assertIsr[r�remoteMethod)	r#r�rrHr]rdr�r�r�r%r%r&�test_reference�s
zBrokerTests.test_referencecCs�t|d�\}}}||f||ffD]5\}}t�}|�d|�|�d�}d|_|�d|jd��|j|j�|�	�|�	�|�
|j|jd�qdS)NrZr���thunkrNzresult wasn't received.)r�r�rgrh�expectedThunkResultr�r�rVrQrHrrS)r#r�rrHr�r�r��barr%r%r&�test_result�s
�
��zBrokerTests.test_resultcCr�r.)�nestedRemoterTr%r%r&�refcountResult�r�zBrokerTests.refcountResultc
	Cs�g}g}t|d�\}}}t�}|�d|�|�d�}ttjd�D]}|jjs+|jjr-n|�	d��
|j|j�|��q!tjd}	|�
|jjd�|�t|�|	d|	�dt|����dS)	NrZr��
�	getSimplerNztransport was not closedz	expected z got )r�r�rgrh�ranger�MAX_BROKER_REFSrR�closedr�r�r�rH�
assertTruer�len)
r#�l�er�rrHr�r��igno�expectedr%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)	NrZr��getCopyrNr�r�rrj)r�r�rgrhr�r�rVrQrHrrSr�r�r�)r#r�rrHr�r�r%r%r&�	test_copy�s
zBrokerTests.test_copycCs�t|d�\}}}t�}t�}|�d|�|�d�}|�d|�|��|�d�|��|��|�d�|��|��|�|j	d�|�
|j	dd�dS)NrZra�observerNrsz
didn't notifyznotified too much)r�r�r�rgrhr�rHr��assertIsNotNoner�r)r#r�rrHrarb�rar%r%r&�test_observe�s


zBrokerTests.test_observecCs�t|d�\}}}t�}|�d|�|�d�}|��|��g}|�d��|j�|��|��|�|j	d�|j
�d�|�|j	dd�|��|��|�|ddd	�dS)
NrZr��	doItLaterzDeferred method run too early.rEzDeferred method run too late.r�zIncorrect result.)
r�r�rgrhrHr�r$r�rr�r�rzr)r#r�rrHr�r{�resultsr%r%r&�
test_defers 
zBrokerTests.test_defercCs�t|d�\}}}t�}|�d|�|�d�}|�d��|j|j�|��|��|j	j
}|�||j�|`	t
jdkr?t��|��|��|��|�||j�dS)NrZr�rt�)r�r�rgrhr�r�rrrQrHrq�luid�assertIn�localObjects�sys�
hexversion�gc�collect�assertNotIn)r#r�rrHr�ro�rluidr%r%r&�
test_refcounts$

�
zBrokerTests.test_refcountcCs:t|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}|�|��j|d
�|�|��|d�g}|�d|��|j�|�
�|�|d�|�|�d�|�d��|j}
|
dj}|�|
|jd
�~	~|�
�~
~|�
�tjdkr�t��|�
�|�|
|jd�|�|
|jd�|�||jd�|�||jd�|� |j!d�dS)NrZr��xxx�getCacherrNr�r�r�zpotential refcounting issuez!other potential refcounting issue�putCachezremote 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)"r�r�r�r�rgrhr�r$r��
addErrbackrJrr�r�r�r�ry�assertIdenticalr��__self__r�rxrjr�r��remotelyCachedObjectsr�r�r�r�r��locallyCachedObjects�assertIsNoner�)r#r�rrHr��obj2�vcc�o2�o3�coll�complex�cp�col2r��baroqueLuidr%r%r&�
test_cache4sj

�

��zBrokerTests.test_cachecCszt�d�Wn	tyYnwt|d�\}}}t�}|�d|�|�d�}g}|�d��|j	|j
�|��|��}|�
|jd�|�
|jd�|�
|jd�|�
|jd�t|d�\}}}|�d|�|�d�}|�d��|j	|j
�|��|��}|�
|jd�dS)NrIrZr��getPubrNr)rJrKrLr��GetPublisherrgrhr�r�r�rQrJryr�activateCalled�isActivated�yayIGotPublished�_wasCleanWhenLoaded)r#r�rrHr�ro�accumr�r%r%r&�test_publishablevs0�

zBrokerTests.test_publishablecCs|j|_dSr.)r�rS)r#�valr%r%r&�gotCopy�ruzBrokerTests.gotCopycCs|t|d�\}}}d}t�}|�d|�|�d�}|�d|��|j|j�|��|��|��|�	|j
|d|j
���dS)NrZ�cr��
getFactoryz!ID not correct on factory object )r�r�rgrhr�r�r�rYrHrrS)r#r�rrH�IDr�r�r%r%r&�test_factoryCopy�s

�zBrokerTests.test_factoryCopy)r(r)r*rSrrQrVrXrYrkrprrr~r�r�r�r�r�r�r�r�r%r%r%r&rH�s&BrHs�helloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldcOs|a|adSr.)�callbackArgs�callbackKeyword)�argsrr%r%r&�finishedCallback�sr�c@r�)�	PagerizercOs||||_|_|_dSr.)rzr�r)r#rzr�rr%r%r&rD�szPagerizer.__init__cCs4tj|td|jg|j�Ri|j��d|_|_dS)N�d)r�StringPager�	bigStringrzr�r�r#�	collectorr%r%r&�remote_getPages�s
���zPagerizer.remote_getPagesN)r(r)r*rDr�r%r%r%r&r��r�r�c@s eZdZdZdd�Zdd�ZdS)�
FilePagerizerNcOs ||_||||_|_|_dSr.)�filenamerzr�r)r#r�rzr�rr%r%r&rD�szFilePagerizer.__init__cCs<tj|t|jd�|jg|j�Ri|j��|_d|_|_dS)N�rb)r�	FilePager�openr�rzr�r�pagerr�r%r%r&r��s���zFilePagerizer.remote_getPages)r(r)r*r�rDr�r%r%r%r&r��sr�c@s@eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�PagingTestsz0
    Test pb objects sending data by pages.
    cCsF|��|_t|jd��}|�t�Wd�dS1swYdS)z?
        Create a file used to test L{util.FilePager}.
        �wbN)�mktempr�r��writer�)r#�fr%r%r&r�s
"�zPagingTests.setUpcCs�t|d�\}}}|�dttddd��|�d�}g}t�|d��|j�|s,|�	�|r&|�
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.
        rZr��hellors�r��getPages�r�'Pages received not equal to pages sent!)r��Completed callback not invokedr�N)r�rgr�r�rhr�getAllPagesr$r�rHr�joinr�r�r��r#r�rrHr�rzr%r%r&�test_pagingWithCallback�s
��
�z#PagingTests.test_pagingWithCallbackcCsnt|d�\}}}|�dtd��|�d�}g}t�|d��|j�|s)|��|r#|�	d�
|d�td�dS)z>
        Test L{util.StringPager} without a callback.
        rZr�Nr�r�rr�)r�rgr�rhrr�r$r�rHrr�r�r�r%r%r&�test_pagingWithoutCallback�s
��z&PagingTests.test_pagingWithoutCallbackc	Cs�|��}t|d���t|d�\}}}t|d�}|�d|�|�d�}g}t�|d��	|j
�d}|sE|dkrE|��|d8}|sE|dks7|sL|�d	�|�
d
�|d�d
d�dS)z@
        Test L{util.FilePager}, sending an empty file.
        �wrZNror�rsrrNzgetAllPages timed outr�r�)r�r��closer�r�rgrhrr�r$r�rHrOrr�)	r#�
filenameEmptyr�rrH�	pagerizerr�rz�ttlr%r%r&�test_emptyFilePaging�s 

�
z PagingTests.test_emptyFilePagingcCs�t|d�\}}}t|jtddd�}|�d|�|�d�}g}t�|d��|j	�|s0|�
�|r*|�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.
        rZ�frodo�	r�ror�r�rr�)r�r�r�N)r�r�r�r�rgrhrr�r$r�rHrr�r�r�r�r��chunks�r#r�rrHr�r�rzr%r%r&�test_filePagingWithCallbacks"
��
�z'PagingTests.test_filePagingWithCallbackcCs�t|d�\}}}t|jd�}|�d|�|�d�}g}t�|d��|j�|s-|�	�|r'|�
d�|d�td�|�
|j
jg�dS)z<
        Test L{util.FilePager} without a callback.
        rZNror�r�rr�)r�r�r�rgrhrr�r$r�rHrr�r�r�r�r�r%r%r&�test_filePagingWithoutCallback%s
��z*PagingTests.test_filePagingWithoutCallbackN)
r(r)r*r2rr�r�r�r�r�r%r%r%r&r��sr�c@r)�DumbPublishablecCsddiS)Nr�rNr%r/r%r%r&�getStateToPublish8r�z!DumbPublishable.getStateToPublishN)r(r)r*r�r%r%r%r&r�7r+r�c@r)�DumbPubcCr�r�)r�r/r%r%r&�	activated=r�zDumbPub.activatedN)r(r)r*r�r%r%r%r&r�<r+r�c@r�)r�cCstd�|_dS)N�TESTING)r��pubr/r%r%r&rDBr�zGetPublisher.__init__cCr�r.)r�r/r%r%r&�
remote_getPubEr1zGetPublisher.remote_getPubN)r(r)r*rDr�r%r%r%r&r�Ar�r�c@�8eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�DisconnectionTestsz'
    Test disconnection callbacks.
    cGstd|����)NzI shouldn't have been called: �r�)r#r�r%r%r&rPQr�zDisconnectionTests.errorcCrK)z.
        Called on broker disconnect.
        rNN)�gotCallbackr/r%r%r&�gotDisconnectedT�
z"DisconnectionTests.gotDisconnectedcCs|�||j�d|_dS)z7
        Called on RemoteReference disconnect.
        rNN)r�remoteObject�objectCallback�r#�or%r%r&�objectDisconnectedZs
z%DisconnectionTests.objectDisconnectedcCsft|d�\}}}|��|�dt��|�d�}g}|�dt���|j�|�	�|�
t|�d�dS)NrZr��
setBadCopyrN)r�rHrg�
BadCopySetrhr��BadCopyabler�r�rJrry)r#r�rrH�grzr%r%r&�test_badSerializationas
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)NrZr�)r�rHrgr�rh�notifyOnDisconnectrPr��disconnects�dontNotifyOnDisconnectr��
_disconnected�disconnectCallbacksr�r�r�rZrr[r
r\rxr�r�)r#r�rrH�rr%r%r&�test_disconnectionks.
z%DisconnectionTests.test_disconnectionN)	r(r)r*r2rPr�r�r�r�r%r%r%r&r�Ls
r�c@r�)�FreakOutNr�r%r%r%r&r�r�rc@r)r�cCr�r.)r)r#�pr%r%r&�getStateToCopyFor�r1zBadCopyable.getStateToCopyForN)r(r)r*rr%r%r%r&r��r+r�c@r)r�cCr5r.r%)r#�bcr%r%r&�remote_setBadCopy�r�zBadCopySet.remote_setBadCopyN)r(r)r*rr%r%r%r&r��r+r�c@s*eZdZdZdd�Zd
dd�Zdd�Zd	S)�LocalRemoteTestrcCs|dSr�r%)r#r�r%r%r&�	sync_add1�r�zLocalRemoteTest.sync_add1rNcC�||Sr.r%)r#r�r�r%r%r&�	async_add�r�zLocalRemoteTest.async_addcCr�r.r�r/r%r%r&�
async_fail�r1zLocalRemoteTest.async_failN)rrN)r(r)r*�reportAllTracebacksrrr	r%r%r%r&r�s

rc@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}
    FcCr�r.�r9�r#r9r%r%r&rD�r�zMyPerspective.__init__cCr�)zT
        Return the avatar identifier which was used to access this avatar.
        rr/r%r%r&�perspective_getAvatarId�sz%MyPerspective.perspective_getAvatarIdcCr-r.)�MyViewr/r%r%r&�perspective_getViewPoint�r1z&MyPerspective.perspective_getViewPointcCr)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_addcCr��NT��	loggedOutr/r%r%r&�logout�r�zMyPerspective.logoutN)r(r)r*r2�loggedInrrDrrrrr%r%r%r&r�src@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\|tjksJ�|dks
J�|tjurtjt�dd�fS|�|�|_d|j_tj|j|jjfS)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!cSr5r.r%r%r%r%r&r6�r7z)TestRealm.requestAvatar.<locals>.<lambda>T)	rr8r�	ANONYMOUSr�perspectiveFactory�lastPerspectiverr�r#r9r:�	interfacer%r%r&r=�s
zTestRealm.requestAvatar)r(r)r*r2rrrr=r%r%r%r&r�s
rc@r)rcCs
t|t�Sr.)r rr"r%r%r&�
view_check�r�zMyView.view_checkN)r(r)r*rr%r%r%r&r�r+rc@� 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#�	mindEaterr%r%r&rD�s
zLeakyRealm.__init__cs*|���|�|��tj���fdd�fS)Ncs����fSr.)rr%�r:�perspr%r&r6sz*LeakyRealm.requestAvatar.<locals>.<lambda>)r!rrr8rr%r#r&r=s

zLeakyRealm.requestAvatarN)r(r)r*r2rDr=r%r%r%r&r �s	r c@r,)�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)r:r/r%r&�
setMindRefr�z4NewCredLeakTests.test_logoutLeak.<locals>.setMindRef)rjr�r5�loginr�cSs$|\}}t�}|�dt�|d�|�S)N�respondr�)r�r�rr*)r��	challenge�
challengerr:r%r%r&�
cbResponse$s
�z4NewCredLeakTests.test_logoutLeak.<locals>.cbResponsecs*�����d���t�td���dS)NrN�boom)rLr�rZrr[r�)�_)�connectionBrokenrHrnr%r&rZ-s
z8NewCredLeakTests.test_logoutLeak.<locals>.connectionLostrN)r'r�r rhr�r$rJrr�r�r�)r#r(rmr5r�r-rZr%)r0rHr#rnr&�test_logoutLeaks �


z NewCredLeakTests.test_logoutLeakN)r(r)r*r2r1r%r%r%r&r%	r3r%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)	rr�r	r�r3rkrr�rcr/r%r%r&rDs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}
        �TCPrXi�N)rqrcrkr@rArHrr�connectorStater|r�IPv4Addressr_rxr$�establishClientAndServer)r#�_ignoredr%r%r&r6Os
��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)rxr@rRrwrZrcrar_)r#r`r%r%r&�completeClientLostConnectionos
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.)rr�RemoteReference��rootObjr/r%r&�
gotRootObject�sz6NewCredTests.test_getRootObject.<locals>.gotRootObjectcs(t�}|�|j��j�����|Sr.)rr�rzrc�
disconnectr8)r;�disconnectedDeferredr/r%r&r=�s

z3NewCredTests.test_getRootObject.<locals>.disconnect�r6rcrr$)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)�assertRaisesr�DeadReferenceErrorr�)�ign)r;r#r%r&�lostConnection�szSNewCredTests.test_deadReferenceError.<locals>.gotRootObject.<locals>.lostConnection)rr�rzr$rcr=r8)r;r>rFr/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@r,)z=NewCredTests.test_clientConnectionLost.<locals>.ReconnectOnceFcSs0|j}d|_tj�||||�}|r|��|Sr)�reconnectedAlreadyrr�rar�)r#r_r`�reconnectingrUr%r%r&ra�s�zRNewCredTests.test_clientConnectionLost.<locals>.ReconnectOnce.clientConnectionLostN)r(r)r*rHrar%r%r%r&�
ReconnectOnce�srJcsH��|tj�t�}|�|j��j������fdd�}|�	|�S)Ncs �j��}�fdd�}|�|�S)Ncs6��|tj�t�}|�|j��j�����|Sr.)	rrr9rr�rzrcr=r8)�anotherRootObjr�r/r%r&�gotAnotherRootObject�s
zqNewCredTests.test_clientConnectionLost.<locals>.gotRootObject.<locals>.disconnected.<locals>.gotAnotherRootObject)rcrr$)rEr�rLr/r%r&rY�s

zSNewCredTests.test_clientConnectionLost.<locals>.gotRootObject.<locals>.disconnected)
rrr9rr�rzrcr=r8r$)r;r�rYr/r%r&r<�s


z=NewCredTests.test_clientConnectionLost.<locals>.gotRootObject)rr�rcr6rr$)r#rJr@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.
        )rXi90N)r6rkrergrrhrfrZrr[r
r\)r#�serverProtor%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)
rr�r)r�UsernamePassword�clientConnectionFailedrr[r�
assertFailure)r#rc�
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$�userrcs"���jjj���|tj��Sr.)rxr�rrrrr9�r���loginCompletedr#r%r&�cbLoginsz.NewCredTests.test_loginLogout.<locals>.cbLogincs�j�����dSr.)rcr=r8��ignoredr/r%r&�cbDisconnects
z3NewCredTests.test_loginLogout.<locals>.cbDisconnectcs���jjj�dSr.)rxr�rrr^r/r%r&�cbLogout&rRz/NewCredTests.test_loginLogout.<locals>.cbLogoutN)r	�registerCheckerrr�rrRrrcr)r$r6rHrJr�r�rz)r#�credsr:r�r]r`rar%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.
            cSr5r.r%r
r%r%r&rDCr�zFNewCredTests.test_logoutAfterDecref.<locals>.EventPerspective.__init__cs��d�dSr.)rzr/rr%r&rFr�zDNewCredTests.test_logoutAfterDecref.<locals>.EventPerspective.logoutN)r(r)r*r2rDrr%rr%r&�EventPerspective>srerQr�rPrcs�Sr.r%)�avatarrr%r&�
cbLoggedInSsz7NewCredTests.test_logoutAfterDecref.<locals>.cbLoggedIncs���jjji�dSr.)rrkr6�
_localCleanupr^r/r%r&�cbLoggedOutZsz8NewCredTests.test_logoutAfterDecref.<locals>.cbLoggedOut)rr�Avatarr�rr	rbrr�rcr)rrRr$r6rHrJr�r�)r#rer�rgrir%)rr#r&�test_logoutAfterDecref6s$
��



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.
        rQsquux)r��bazrPr�bazcSs |\}}t|�d�|�d�g�S)N�getAvatarId)rr�)rU�first�secondr%r%r&rg}s�z5NewCredTests.test_concurrentLogin.<locals>.cbLoggedIncs$|\}}��|d���|d�dS)NrPrm)r)r�rorpr/r%r&�cbAvatarIds�sz6NewCredTests.test_concurrentLogin.<locals>.cbAvatarIds)
r	rbrr�rcr)rrRrr$r6rHrJ)r#�
firstLogin�secondLoginr�rgrqr%r/r&�test_concurrentLoginls"���


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.
        rWrXs
nosuchuserrYs	wrongpassc���t�}��t|�d�dS)Nr�)�flushLoggedErrorsr
rry��ignore�errorsr/r%r&�cleanup��
z;NewCredTests.test_badUsernamePasswordLogin.<locals>.cleanup)r	rbrr�rcr)rrRrTr
rr$r6rHrJ)r#rrrsr�rzr%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.
        rcS�|�dd�S�Nr�{r_rZr%r%r&rg�ruz4NewCredTests.test_anonymousLogin.<locals>.cbLoggedInr)
r	rbr�AllowAnonymousAccessrcr)r�	Anonymousr$rr6rHrJ)r#r�rgr%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.
        �passrXrcrur�)rvrrryrwr/r%r&rz�r{z=NewCredTests.test_anonymousLoginNotPermitted.<locals>.cleanup)r	rbrr�rcr)rr�rTrr$r6rHrJ�r#r�rzr%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.
        rWrXrcSr}r~r_rZr%r%r&r]�ruzENewCredTests.test_anonymousLoginWithMultipleCheckers.<locals>.cbLoginr)r	rbrr�r�rcr)rr�r$rr6rHrJ�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.
        rWrXrYrcSs|�ddd�S)N�addr��r_rZr%r%r&r]�r�zINewCredTests.test_authenticatedLoginWithMultipleCheckers.<locals>.cbLoginr)r	rbrr�r�rcr)rrRr$rr6rHrJr�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.
        rWrXrYrcS�
|�d�S)N�getViewPointr_rZr%r%r&r]r�z'NewCredTests.test_view.<locals>.cbLogincSr�)Nr�r_)�	viewpointr%r%r&�cbViewr�z&NewCredTests.test_view.<locals>.cbView)
r	rbrr�rcr)rrRr$rxr6rHrJ)r#r�r]r�r%r%r&�	test_view
s
��


zNewCredTests.test_viewr.)r(r)r*r2rr6rr[r
r\r8rArGrMrOrVrdrkrtr|r�r�r�r�r�r%r%r%r&r2?s(

!
�3
16%r2c@r�)�NonSubclassingPerspectivecCr5r.r%r
r%r%r&rD*r�z"NonSubclassingPerspective.__init__cCs(|�||�}|�||�}|�|||f�Sr.)�unserialize�	serialize)r#r]�messager��kwargsr%r%r&�perspectiveMessageReceived.sz4NonSubclassingPerspective.perspectiveMessageReceivedcCr�rrr/r%r%r&r4r�z NonSubclassingPerspective.logoutN)r(r)r*rDr�rr%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}.
    cCs�t�|_t|j_t�|j�|_t��|_|j�	dd�|j�
|j�tt�
|j��|_tjd|jdd�|_|�|jj�|j��j|_dS)NrYrWrrX)r)rr�r�rr	r�rr�r��addUserrbrrr�r@rr<r?rirDr>�portnor/r%r%r&r>s
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.
        rYrWrrXcSs|jdddd�S)N�ANYTHING�hererl)ror_)rr%r%r&r6Rsz#NSPTests.test_NSP.<locals>.<lambda>r�)r�rorlcs$����jjD]}|j��qdSr.)r=r@�	protocolsrRrC)r_r�r@r#r%r&rzUs�z"NSPTests.test_NSP.<locals>.cleanup)
rr�r)rrRrr=r�r$rr�r%r�r&�test_NSPJs
zNSPTests.test_NSPN)r(r)r*r2rr�r%r%r%r&r�8sr�c@r)�
IForwardedzA
    Interface used for testing L{util.LocalAsyncForwarder}.
    cCr�)z,
        Simple synchronous method.
        Nr%r%r%r%r&�	forwardMecr7zIForwarded.forwardMecCr�)z-
        Simple asynchronous method.
        Nr%r%r%r%r&�forwardDeferredhr7zIForwarded.forwardDeferredN)r(r)r*r2r�r�r%r%r%r&r�^sr�c@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}
    FcCrK)z6
        Set a local flag to test afterwards.
        TN)�	forwardedr/r%r%r&r�|r�zForwarded.forwardMecCrK)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�rMzForwarded.dontForwardMecCstd�S)z0
        Asynchronously return C{True}.
        T)rr/r%r%r&r��szForwarded.forwardDeferredN)	r(r)r*r2r�r�r�r�r�r%r%r%r&r�ns	r�c@r�)
�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)rrr�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�)rr�rrr$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.
        rOcs��|tj�|�t�dSr.)rrr[�trapr�)r�r/r%r&�eb�sz*SpreadUtilTests.test_asyncFail.<locals>.ebcs
��d�S)Nzsupposed to failrNrr/r%r&r6�s
z0SpreadUtilTests.test_asyncFail.<locals>.<lambda>)rr�r�)r#r�r�r�r%r/r&�test_asyncFail�s

zSpreadUtilTests.test_asyncFailcCs$t�}|�d�}|�|d�d�dS)zM
        Test the C{remoteMethod} facility of L{util.LocalAsRemote}.
        r�r�r�N)rrjr)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.
        r�r�r�rrNN)r�r�LocalAsyncForwarderr�r�rxr�rr�r$r�r)r#r��lf�rrrzr%r%r&�test_localAsyncForwarder�s


z(SpreadUtilTests.test_localAsyncForwarderN)	r(r)r*r2r�r�r�r�r�r%r%r%r&r��sr�c@r>)�PBWithSecurityOptionsTestsz&
    Test security customization.
    cCs&t��}|�d�}|�|jtj�dS)zl
        By default, client broker should use C{jelly.globalSecurity} as
        security settings.
        N)rr�reri�securityr�globalSecurity�r#r@r]r%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)rr�rrerir�rr�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.
        �r�N)r�SecurityOptionsrr�rerir��r#r�r@r]r%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)rr�rr�rrerir�r�r%r%r&� test_serverSecurityCustomization�s
z;PBWithSecurityOptionsTests.test_serverSecurityCustomizationN)r(r)r*r2r�r�r�r�r%r%r%r&r��s		
r�r.)�r2r�rJr�rGr&�collectionsr�iorrf�typingr�zope.interfacerr�twisted.credrrr	�twisted.cred.errorr
r�twisted.internetrr
rr�twisted.internet.deferrrr�twisted.internet.errorr�twisted.protocols.policiesr�twisted.pythonrr�twisted.python.compatr�twisted.spreadrrrr�twisted.test.proto_helpersr�
twisted.trialr�Viewablerrjr!�IRealmr4r?rqrrr|r�rfr�r��Copyabler��
RemoteCopyr��setUnjellyableForClassr�r��setUnjellyableFactoryForClassr��	Cacheabler�r�r��RemoteCacher�r�r�r�r�r�rrr
�Rootrr�SynchronousTestCaserr�r3�TestCaser8rHr�r�r�r�r�r�r��Publishabler��RemotePublishedr�r�r�r�rr�r��
LocalAsRemoterr8rrrr r%r2r�r�r�r�r�r�r%r%r%r&�<module>s�	;3!
	

R 7lC
#!6l&!@

Anon7 - 2022
AnonSec Team