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/web/test/__pycache__/test_distrib.cpython-38.pyc
U


W[pG�@sXdZddlmZddlmZzddlZWnek
r@dZYnXddlmZddl	m
Z
mZddlm
Z
mZddlmZdd	lmZdd
lmZddlmZmZmZmZmZddlmZmZdd
lmZddl m!Z!ddl"m#Z#ddl$m%Z%Gdd�dej&�Z'Gdd�dej(�Z(Gdd�de)�Z*Gdd�dej+�Z,Gdd�d�Z-Gdd�dej+�Z.dS)z#
Tests for L{twisted.web.distrib}.
�)�abspath)�parseStringN)�verifyObject)�filepath�failure)�reactor�defer)�unittest)�pb��
SIZE_LIMIT)�distrib�client�resource�static�server)�DummyRequest�DummyChannel)�_render)�
proto_helpers)�Headers)�globalLogPublisherc@seZdZdS)�MySiteN)�__name__�
__module__�__qualname__�rr�?/usr/lib/python3/dist-packages/twisted/web/test/test_distrib.pyrsrc@seZdZdZdZdd�ZdS)�PBServerFactoryz�
    A PB server factory which keeps track of the most recent protocol it
    created.

    @ivar proto: L{None} or the L{Broker} instance most recently returned
        from C{buildProtocol}.
    NcCstj�||�|_|jS�N)r
r�
buildProtocol�proto)�self�addrrrrr ,szPBServerFactory.buildProtocol)rrr�__doc__r!r rrrrr"src@seZdZdZdS)�ArbitraryErrorz%
    An exception for this test.
    N)rrrr$rrrrr%2sr%c@s�eZdZdZdZdZdZdd�Zdd�Zdd�Z	dd	�Z
d
d�Zdd
�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�ZdS)�DistribTestsNcs�t��t��g�|jdk	r>|jjdk	r>|jj��fdd��n�d�d�|jdk	r�|jjdk	r�|jjj��fdd��|jjjj	�
�n�d�d�|jdk	r���|j�
��|jdk	rΈ�|j�
��t���S)z�
        Clean up all the event sources left behind by either directly by
        test methods or indirectly via some distrib API.
        Ncs�d�d�S�Nr��callbackr�Zdlrr�<lambda>F�z'DistribTests.tearDown.<locals>.<lambda>rcs�d�d�S)N�r(rr*rrr+Kr,r-)r�Deferred�f1r!�notifyOnDisconnectr)�subZ	publisherZbrokerZ	transportZloseConnection�port1�append�
stopListening�port2Z
gatherResults�r"rr*r�tearDown?s

�

zDistribTests.tearDowncCs�t��}|�dt�dd��t�|�}tt�	|��|_
t�d|j
�|_
t�d|j
��j�|_t��}|�d|j�t|�}t�d|�|_t�t�}d�|j��j�}|�d�}|�d	|�}|�tj�|�|jd�|S)
Nstheresrootz
text/plainr�	127.0.0.1sherezhttp://127.0.0.1:{}/here/there�ascii�GET)r�Resource�putChildrZDatar�Siterr
�ResourcePublisherr/r�	listenTCPr2�ResourceSubscription�getHost�portr1rr5r�Agent�format�encode�request�addCallback�readBody�assertEqual)r"Zr1Zsite1Zr2�f2�agent�url�drrr�testDistribVs*

�

�
zDistribTests.testDistribcCs�t��}|�d|�t�|�}tt�|��|_}t	j
d|dd�}|�|j�|�
�}t�|j|j�|_}t�|�}t	j
d|dd�}	|�|	j�|	�
�}
|	|
fS)z�
        Set up a resource on a distrib site using L{ResourcePublisher}.

        @param child: The resource to publish using distrib.

        @return: A tuple consisting of the host and port on which to contact
            the created site.
        schildrr8)Z	interface)rr;r<rr=rr
r>r/rr?Z
addCleanupr4rAr@�hostrBr1)r"�childZdistribRootZdistribSiteZdistribFactoryZdistribPortr#ZmainRootZmainSite�mainPort�mainAddrrrr�_setupDistribServerms,	
�
��

z DistribTests._setupDistribServercKsR|�|�\}}t�t�}d|j|jf}|�d�}|jd|f|�}|�tj	�|S)a�
        Set up a resource on a distrib site using L{ResourcePublisher} and
        then retrieve it from a L{ResourceSubscription} via an HTTP client.

        @param child: The resource to publish using distrib.
        @param **kwargs: Extra keyword arguments to pass to L{Agent.request} when
            requesting the resource.

        @return: A L{Deferred} which fires with the result of the request.
        zhttp://%s:%s/childr9r:)
rSrrCrrOrBrErFrGrH)r"rP�kwargsrQrRrKrLrMrrr�_requestTest�s

zDistribTests._requestTestcKsT|�|�\}}d�|j|j�}|�d�}t�t�jd|f|�}dd�}|�	|�|S)a;
        Set up a resource on a distrib site using L{ResourcePublisher} and
        then retrieve it from a L{ResourceSubscription} via an HTTP client.

        @param child: The resource to publish using distrib.
        @param **kwargs: Extra keyword arguments to pass to L{Agent.request} when
            requesting the resource.

        @return: A L{Deferred} which fires with a tuple consisting of a
            L{twisted.test.proto_helpers.AccumulatingProtocol} containing the
            body of the response and an L{IResponse} with the response itself.
        zhttp://{}:{}/childr9r:cs8t�������t��}�_|���fdd��|S)Ncs��fSrr)�_�Zprotocol�responserrr+�r,zGDistribTests._requestAgentTest.<locals>.cbCollectBody.<locals>.<lambda>)rZAccumulatingProtocolZdeliverBodyrr.ZclosedDeferredrG)rXrMrrWr�
cbCollectBody�s

z5DistribTests._requestAgentTest.<locals>.cbCollectBody)
rSrDrOrBrErrCrrFrG)r"rPrTrQrRrLrMrYrrr�_requestAgentTest�s


zDistribTests._requestAgentTestcs|i�t���t���dg�G��fdd�dtj�}���fdd���j|�tddgi�d�}���fd	d
�}|�|�|S)z�
        The request headers are available on the request object passed to a
        distributed resource's C{render} method.
        NcseZdZ��fdd�ZdS)z>DistribTests.test_requestHeaders.<locals>.ReportRequestHeaderscs |�d<��t|j����dS)Nrr,)�update�dict�requestHeadersZgetAllRawHeaders�r"rF��reqr]rr�render�s
�zEDistribTests.test_requestHeaders.<locals>.ReportRequestHeaders.renderN�rrrrarr_rr�ReportRequestHeaders�srccsDdd��D�}��d|���d|����d|�t���dS)NcSsg|]}|d�qS)�
log_formatr)�.0�errr�
<listcomp>�szHDistribTests.test_requestHeaders.<locals>.check_logs.<locals>.<listcomp>zconnected to publisherz3could not connect to distributed web service: {msg}r)�assertInrZremoveObserver)Zmsgs)�logObserverr`r"rr�
check_logs�s�z4DistribTests.test_requestHeaders.<locals>.check_logsZfooZbar)Zheaderscs$�jj������ddg�dS)NsFoosbar)r/r!r0rI��result)rjr]r"rr�cbRequested�sz5DistribTests.test_requestHeaders.<locals>.cbRequested)	r�EventLoggingObserverrZaddObserverrr;rUrrG)r"rcrFrmr)rjrir`r]r"r�test_requestHeaders�s

�
z DistribTests.test_requestHeaderscs8Gdd�dtj�}��|��}�fdd�}|�|�|S)z�
        The response code can be set by the request object passed to a
        distributed resource's C{render} method.
        c@seZdZdd�ZdS)z>DistribTests.test_requestResponseCode.<locals>.SetResponseCodecSs|�d�dS)N����ZsetResponseCoder^rrrra�s
zEDistribTests.test_requestResponseCode.<locals>.SetResponseCode.renderNrbrrrr�SetResponseCode�srscs:��|djd���|djd���|djd�dS)Nrr,r-rpsOK�rI�data�code�phraserkr6rrrm�sz:DistribTests.test_requestResponseCode.<locals>.cbRequested�rr;rZrG�r"rsrFrmrr6r�test_requestResponseCode�s

z%DistribTests.test_requestResponseCodecs8Gdd�dtj�}��|��}�fdd�}|�|�|S)z�
        The response code and message can be set by the request object passed to
        a distributed resource's C{render} method.
        c@seZdZdd�ZdS)zEDistribTests.test_requestResponseCodeMessage.<locals>.SetResponseCodecSs|�dd�dS)Nrp�some-messagerqrrr^rrrra�szLDistribTests.test_requestResponseCodeMessage.<locals>.SetResponseCode.renderNrbrrrrrs�srscs:��|djd���|djd���|djd�dS)Nrr,r-rpr{rtrkr6rrrm�szADistribTests.test_requestResponseCodeMessage.<locals>.cbRequestedrxryrr6r�test_requestResponseCodeMessage�s

z,DistribTests.test_requestResponseCodeMessagecCs8Gdd�dtj�}|�|��}|�|jdtd�|S)z�
        If a string longer than the Banana size limit is passed to the
        L{distrib.Request} passed to the remote resource, it is broken into
        smaller strings to be transported over the PB connection.
        c@seZdZdd�ZdS)z0DistribTests.test_largeWrite.<locals>.LargeWritecSs |�dtd�|��tjS�N�x�y)�writerZfinishrZNOT_DONE_YETr^rrrrasz7DistribTests.test_largeWrite.<locals>.LargeWrite.renderNrbrrrr�
LargeWrite
sr�r~r�rr;rUrGrIr)r"r�rFrrr�test_largeWriteszDistribTests.test_largeWritecCs8Gdd�dtj�}|�|��}|�|jdtd�|S)z�
        Like L{test_largeWrite}, but for the case where C{render} returns a
        long string rather than explicitly passing it to L{Request.write}.
        c@seZdZdd�ZdS)z2DistribTests.test_largeReturn.<locals>.LargeReturncSsdtdSr}rr^rrrrasz9DistribTests.test_largeReturn.<locals>.LargeReturn.renderNrbrrrr�LargeReturnsr�r~rr�)r"r�rFrrr�test_largeReturnszDistribTests.test_largeReturncsltt����_}t�d|��_}t�d|�	�j
��_}tdg��t
|��}��fdd�}|�|�|S)zz
        If there is an error issuing the request to the remote publisher, an
        error response is returned.
        rr8r,c
s\���jd���tj�}��t|�d�ddddddd	d
ddg
}��d�|�g�j�dS)
Ni�r-r,s<html>s:  <head><title>500 - Server Connection Lost</title></head>s  <body>s#    <h1>Server Connection Lost</h1>s�    <p>Connection to distributed server lost:<pre>[Failure instance: Traceback from remote host -- twisted.spread.flavors.NoSuchMethod: No such method: remote_requests]</pre></p>s	  </body>s</html>�
)rI�responseCode�flushLoggedErrorsr
ZNoSuchMethod�len�join�written)�ignored�errorsZexpected�rFr"rr�
cbRendered4s�z4DistribTests.test_connectionLost.<locals>.cbRendered)rr
ZRootr/rr?r2r
r@rArBr1rrrG)r"Z
serverFactoryZ
serverPortZsubscriptionrMr�rr�r�test_connectionLost&s�



z DistribTests.test_connectionLostcCs`tj�|t�}t�t��}tdg�}t�	|�}|�
|�|�dt|��|�
d|dd�dS)zQ
        When a request fails, the string form of the failure is logged.
        r,r-zFailure instancerrdN)rrn�createWithCleanuprr�Failurer%rr
ZIssueZfailed�assertEqualsr�rh)r"ri�frFZissuerrr�test_logFailedQs�



�zDistribTests.test_logFailedcCsftj�|t�}t�}t�|�}t�t	��}|�
|�|�t�|�dt
|��|�|dd|�dS)zf
        When L{twisted.web.distrib.Request}'s fail is called, the failure
        is logged.
        r-rZlog_failureN)rrnr�rr%rr�r
ZRequestrZfailr�r�r�ZassertIs)r"ri�errr�r`rrr�test_requestFailes�


zDistribTests.test_requestFail)rrrr2r5r1r/r7rNrSrUrZrorzr|r�r�r�r�r�rrrrr&9s"&+r&c@s$eZdZdd�Zdd�Zdd�ZdS)�_PasswordDatabasecCs
||_dSr)�_users)r"Zusersrrr�__init__ysz_PasswordDatabase.__init__cCs
t|j�Sr)�iterr�r6rrr�getpwall}sz_PasswordDatabase.getpwallcCs*|jD]}|d|kr|Sqt��dSr')r��KeyError)r"Zusername�userrrr�getpwnam�s

z_PasswordDatabase.getpwnamN)rrrr�r�r�rrrrr�xsr�c@sneZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
edkrjde
_dS)�UserDirectoryTestszj
    Tests for L{UserDirectory}, a resource for listing all user resources
    available on a system.
    cCsTddddd|��df|_dddd	d
|��df|_t|j|jg�|_t�|j�|_dS)N�alice�x�{i�zAlice,,,z/bin/sh�bob��i7zBob,,,)Zmktempr�r�r�Zdatabaser
�
UserDirectory�	directoryr6rrr�setUp�szUserDirectoryTests.setUpcCs|�ttj|j��dS)zK
        L{UserDirectory} instances provide L{resource.IResource}.
        N)Z
assertTruerrZ	IResourcer�r6rrr�test_interface�sz!UserDirectoryTests.test_interfacecs>t|g���j�|��}t|��}��fdd�}|�|�|S)zr
        Verify that requesting the C{name} child of C{self.directory} results
        in a 404 response.
        cs���jd�dS)Ni�)rIr�)r�r�rrr��sz/UserDirectoryTests._404Test.<locals>.cbRendered)rr��getChildrrG)r"�namerlrMr�rr�r�_404Test�s


zUserDirectoryTests._404TestcCs
|�d�S)z�
        L{UserDirectory.getChild} returns a resource which renders a 404
        response when passed a string which does not correspond to any known
        user.
        Zcarol�r�r6rrr�test_getInvalidUser�sz&UserDirectoryTests.test_getInvalidUsercCs
|�d�S)z�
        L{UserDirectory.getChild} returns a resource which renders a 404
        response when passed a string which corresponds to a known user who has
        neither a user directory nor a user distrib socket.
        r�r�r6rrr�test_getUserWithoutResource�sz.UserDirectoryTests.test_getUserWithoutResourcecCs\t�|jd�}|�d�}|��tdg�}|j�d|�}|�|t	j
�|�|j|j�dS)z�
        L{UserDirectory.getChild} returns a L{static.File} instance when passed
        the name of a user with a home directory containing a I{public_html}
        directory.
        ����public_htmlr�N)
r�FilePathr�rP�makedirsrr�r��assertIsInstancerZFilerI�path)r"�homer�rFrlrrr�test_getPublicHTMLChild�s

z*UserDirectoryTests.test_getPublicHTMLChildcCsnt�|jd�}|��|�d�}tdg�}|j�d|�}|�|t	j
�|�|jd�|�t
|j�|j�dS)z�
        L{UserDirectory.getChild} returns a L{ResourceSubscription} instance
        when passed the name of a user suffixed with C{".twistd"} who has a
        home directory containing a I{.twistd-web-pb} socket.
        r��.twistd-web-pbr�z
bob.twistdZunixN)rr�r�r�rPrr�r�r�r
r@rIrOrrBr�)r"r��webrFrlrrr�test_getDistribChild�s

z'UserDirectoryTests.test_getDistribChildcCs(tdg�}d|_|�tj|jj|�dS)zr
        L{UserDirectory.render} raises L{UnsupportedMethod} in response to a
        non-I{GET} request.
        rqZPOSTN)r�methodZassertRaisesrZUnsupportedMethodr�rar^rrr�test_invalidMethod�s
�z%UserDirectoryTests.test_invalidMethodcsxt��jd��d�}|��t��jd�}|��|�d��d�tdg��t�j	��}��fdd�}|�
|�|S)z}
        L{UserDirectory} renders a list of links to available user content
        in response to a I{GET} request.
        r�r�r�r,rqcs�td��j��}|�d�\}}��|jjd���|j�d�d���|jjjd���|jjd���|j�d�d���|jjjd�dS)	Nr,Zli�aZhrefzalice/zAlice (file)zbob.twistd/zBob (twistd))	rr�r�ZgetElementsByTagNamerIZ
firstChildZtagNameZgetAttributeru)r�Zdocumentr�r�r�rrr��sz2UserDirectoryTests.test_render.<locals>.cbRendered)rr�r�rPr�r�Z
setContentrrr�rG)r"r�r�rlr�rr�r�test_render�s

zUserDirectoryTests.test_rendercCst��}|�|jt�dS)z�
        If L{UserDirectory} is instantiated with no arguments, it uses the
        L{pwd} module as its password database.
        N)r
r�ZassertIdenticalZ_pwd�pwd)r"r�rrr�test_passwordDatabasesz(UserDirectoryTests.test_passwordDatabaseNzpwd module required)rrrr$r�r�r�r�r�r�r�r�r�r�r��skiprrrrr��s		 r�)/r$�os.pathrZxml.dom.minidomrr��ImportErrorZzope.interface.verifyrZtwisted.pythonrrZtwisted.internetrrZ
twisted.trialr	Ztwisted.spreadr
Ztwisted.spread.bananarZtwisted.webr
rrrrZtwisted.web.test.test_webrrZtwisted.web.test._utilrZtwisted.testrZtwisted.web.http_headersrZtwisted.loggerrr=rr�	Exceptionr%ZTestCaser&r�r�rrrr�<module>s4
A