o
    ¼>hR6  ã                   @   s  d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
mZmZ ddlmZ ddlmZ dd	lmZ G d
d„ dƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ de
jƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZdS )z7
Tests for L{twisted.words.protocols.jabber.component}
é    )Úsha1)ÚverifyObject)Úfailure)Úunittest)Ú	componentÚijabberÚ	xmlstream)ÚJID)Údomish)ÚXmlPipec                   @   ó   e Zd Zdd„ Zdd„ ZdS )ÚDummyTransportc                 C   s
   || _ d S ©N)Úlist)Úselfr   © r   ú‡/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/words/test/test_jabbercomponent.pyÚ__init__   ó   
zDummyTransport.__init__c                 C   s   | j  |¡ d S r   )r   Úappend)r   Úbytesr   r   r   Úwrite   s   zDummyTransport.writeN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r      ó    r   c                   @   r   )Ú#ComponentInitiatingInitializerTestsc                 C   sj   g | _ t ¡ | _d| j_t | j¡| _d| j_| j j| j_| j 	¡  | j 
d¡ d| j_t | j¡| _d S )NÚsecretútest:componentz‚<stream:stream xmlns='test:component' xmlns:stream='http://etherx.jabber.org/streams' from='example.com' id='12345' version='1.0'>Ú12345)Úoutputr   ÚAuthenticatorÚauthenticatorÚpasswordÚ	XmlStreamÚ	namespacer   ÚsendÚconnectionMadeÚdataReceivedÚsidr   ÚComponentInitiatingInitializerÚinit©r   r   r   r   ÚsetUp   s   

ÿz)ComponentInitiatingInitializerTests.setUpc                 C   sb   | j  ¡ }| jd }|  d|j¡ |  d|j¡ |  tdƒ ¡ t|ƒ¡ g |_	| j
 | ¡ ¡ |S )z?
        Test basic operations of component handshake.
        éÿÿÿÿÚ	handshaker   ó   12345secret)r+   Ú
initializer    ÚassertEqualÚnameÚurir   Ú	hexdigestÚstrÚchildrenr   r(   ÚtoXml)r   Údr/   r   r   r   ÚtestHandshake-   s   

z1ComponentInitiatingInitializerTests.testHandshakeN)r   r   r   r-   r:   r   r   r   r   r      s    r   c                   @   r   )ÚComponentAuthTestsc                 C   ó
   d| _ d S ©NT)ÚauthComplete)r   Ústreamr   r   r   Ú
authPassedD   r   zComponentAuthTests.authPassedc                 C   s’   d| _ g }t dd¡}t |¡}t|ƒ|_| tj| j	¡ | 
¡  | d¡ tdƒ ¡  d¡}|  |d d| d	 ¡ | d
¡ |  | j d¡ d S )NFÚcjidr   sv   <stream:stream xmlns='jabber:component:accept' xmlns:stream='http://etherx.jabber.org/streams' from='cjid' id='12345'>r0   Úasciié   s   <handshake>s   </handshake>ú<handshake/>T)r>   r   ÚConnectComponentAuthenticatorr   r$   r   Ú	transportÚaddObserverÚSTREAM_AUTHD_EVENTr@   r'   r(   r   r5   Úencoder2   )r   ÚoutlistÚcaÚxsÚhvr   r   r   ÚtestAuthG   s   

ÿ
zComponentAuthTests.testAuthN)r   r   r   r@   rN   r   r   r   r   r;   C   r   r;   c                   @   s   e Zd ZdZdd„ ZdS )ÚServiceTestsz)
    Tests for L{component.Service}.
    c                 C   s   t  ¡ }ttj|ƒ dS )zF
        L{component.Service} implements L{ijabber.IService}.
        N)r   ÚServicer   r   ÚIService)r   Úservicer   r   r   Útest_interfacef   s   zServiceTests.test_interfaceN)r   r   r   Ú__doc__rS   r   r   r   r   rO   a   s    rO   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚJabberServiceHarnessc                 C   s   d| _ d| _d| _d S )NF)ÚcomponentConnectedFlagÚcomponentDisconnectedFlagÚtransportConnectedFlagr,   r   r   r   r   o   s   
zJabberServiceHarness.__init__c                 C   r<   r=   )rV   ©r   r   r   r   r   ÚcomponentConnectedt   r   z'JabberServiceHarness.componentConnectedc                 C   r<   r=   )rW   r,   r   r   r   ÚcomponentDisconnectedw   r   z*JabberServiceHarness.componentDisconnectedc                 C   r<   r=   )rX   rY   r   r   r   ÚtransportConnectedz   r   z'JabberServiceHarness.transportConnectedN)r   r   r   r   rZ   r[   r\   r   r   r   r   rU   n   s
    rU   c                   @   s   e Zd Zdd„ ZdS )ÚJabberServiceManagerTestsc                 C   sŒ   t  dd¡}tƒ }| |¡ g }| ¡  d ¡}| |_|j|j_| 	¡  |  
d|j¡ | |tj¡ |  
d|j¡ | d ¡ |  
d|j¡ d S )NÚfoor#   T)r   ÚServiceManagerrU   ÚsetServiceParentÚ
getFactoryÚbuildProtocolrF   r   r   r'   r2   rX   Údispatchr   rH   rV   ÚconnectionLostrW   )r   ÚsmÚsvcÚwlistrL   r   r   r   ÚtestSM   s   


z JabberServiceManagerTests.testSMN)r   r   r   rh   r   r   r   r   r]   ~   s    r]   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚRouterTestsz(
    Tests for L{component.Router}.
    c                    s|   t  ¡ }g ‰ ‡ fdd„|_tƒ }| d|j¡ |  dt|jƒ¡ |  |j|jd ¡ t	 
d¡}|j |¡ |  |gˆ ¡ dS )zJ
        Test route registration and routing on incoming stanzas.
        c                    ó
   ˆ   | ¡S r   ©r   ©Úelement©Úroutedr   r   Ú<lambda>«   ó   
 z+RouterTests.test_addRoute.<locals>.<lambda>zexample.orgrC   )ÚtestnsÚtestN)r   ÚRouterÚrouter   ÚaddRouteÚsinkr2   ÚlenÚroutesr
   ÚElementÚsourcer&   )r   ÚrouterÚpiperm   r   rn   r   Útest_addRoute¥   s   
zRouterTests.test_addRoutec                    s‚   t ƒ }t ƒ }t ¡ }| d|j¡ | d|j¡ g ‰ |j d‡ fdd„¡ t d¡}d|d< d|d< |j 	|¡ |  
|gˆ ¡ d	S )
z,
        Test routing of a message.
        úcomponent1.example.orgzcomponent2.example.orgú/*c                    rj   r   rk   rl   ©Úoutgoingr   r   rp   Á   rq   z(RouterTests.test_route.<locals>.<lambda>©NÚpresenceÚfromÚtoN©r   r   rt   rv   rw   r{   rG   r
   rz   r&   r2   )r   Ú
component1Ú
component2r|   Ústanzar   r   r   Ú
test_route¶   s   
zRouterTests.test_routec                    s‚   t ƒ }t ƒ }t ¡ }| d|j¡ | d|j¡ g ‰ |j d‡ fdd„¡ t d¡}d|d< d|d	< |j 	|¡ |  
|gˆ ¡ dS )
a  
        Test routing of a message using the default route.

        The default route is the one with L{None} as its key in the
        routing table. It is taken when there is no more specific route
        in the routing table that matches the stanza's destination.
        r   Nr€   c                    rj   r   rk   rl   r   r   r   rp   ×   rq   z/RouterTests.test_routeDefault.<locals>.<lambda>rƒ   r…   zexample.comr†   r‡   )r   rˆ   Ús2sr|   rŠ   r   r   r   Útest_routeDefaultÈ   s   
zRouterTests.test_routeDefaultN)r   r   r   rT   r~   r‹   r   r   r   r   r   ri       s
    ri   c                   @   sX   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )Ú!ListenComponentAuthenticatorTestsz>
    Tests for L{component.ListenComponentAuthenticator}.
    c                 C   s,   g | _ t d¡}t |¡| _| j j| j_d S )Nr   )r    r   ÚListenComponentAuthenticatorr   r$   r   r&   )r   r"   r   r   r   r-   ä   s   
z'ListenComponentAuthenticatorTests.setUpc                 C   s   | j  d¡ dS )zA
        Stub loseConnection because we are a transport.
        z	no reasonN)r   rd   r,   r   r   r   ÚloseConnectionê   s   z0ListenComponentAuthenticatorTests.loseConnectionc                    sŽ   g ‰ ‡ fdd„}| j }||_| | ¡ |  d|j¡ |  |j¡ | d¡ |  d|j	¡ |  
d|j¡ |  |j¡ |  d|jjfˆ d ¡ dS )zK
        The received stream header should set several attributes.
        c                    s   ˆ   | |f¡ d S r   rk   )ÚeventÚ
observerfn©Ú	observersr   r   ÚaddOnetimeObserverö   s   zPListenComponentAuthenticatorTests.test_streamStarted.<locals>.addOnetimeObserverNzz<stream:stream xmlns='jabber:component:accept' xmlns:stream='http://etherx.jabber.org/streams' to='component.example.org'>)r   r   r€   r.   )r   r•   ÚmakeConnectionÚassertIdenticalr)   ÚassertFalseÚ_headerSentr(   r2   ÚversionÚassertNotIdenticalÚ
assertTruer"   Ú	onElement)r   r•   rL   r   r“   r   Útest_streamStartedð   s   
ÿz4ListenComponentAuthenticatorTests.test_streamStartedc                 C   óL   g }| j }|j|_| | ¡ | d¡ |  dt|ƒ¡ |  d|d j¡ dS )zM
        The received stream header should have a correct namespace.
        zp<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' to='component.example.org'>rC   zinvalid-namespacer.   N©r   r   ÚsendStreamErrorr–   r(   r2   rx   Ú	condition©r   ÚstreamErrorsrL   r   r   r   Ú test_streamStartedWrongNamespace
  s   
ÿzBListenComponentAuthenticatorTests.test_streamStartedWrongNamespacec                 C   rŸ   )zJ
        The received stream header should have a 'to' attribute.
        z_<stream:stream xmlns='jabber:component:accept' xmlns:stream='http://etherx.jabber.org/streams'>rC   zimproper-addressingr.   Nr    r£   r   r   r   Útest_streamStartedNoTo  s   
ÿz8ListenComponentAuthenticatorTests.test_streamStartedNoToc                 C   sH   g }| j }|j|j_t d¡}| d¡ |j |¡ |  d|d ¡ dS )z<
        We expect a handshake element with a hash.
        )újabber:component:acceptr/   Ú1234r.   N)	r   r   r"   ÚonHandshaker
   rz   Ú
addContentr   r2   )r   Ú
handshakesrL   r/   r   r   r   Útest_onElement+  s   


z0ListenComponentAuthenticatorTests.test_onElementc                 C   sV   g }g }| j }|j|j_|j|_t d¡}|j |¡ |  |¡ |  	d|d j
¡ dS )z9
        Reject elements that are not handshakes
        )r§   Úmessageúnot-authorizedr.   N)r   r   r"   r©   r¡   r
   rz   r   r˜   r2   r¢   )r   r«   r¤   rL   rm   r   r   r   Útest_onElementNotHandshake9  s   


z<ListenComponentAuthenticatorTests.test_onElementNotHandshakec                    s`   g ‰ ‡ fdd„}| j }| t j|¡ d|_d}|j |¡ |  d| jd ¡ |  dtˆ ƒ¡ dS )	zU
        Receiving a handshake matching the secret authenticates the stream.
        c                    ó   ˆ   | ¡ d S r   rk   ©rL   ©Úauthdr   r   ÚauthenticatedO  ó   zIListenComponentAuthenticatorTests.test_onHandshake.<locals>.authenticatedr¨   Ú(32532c0f7dbf1253c095b18b18e36d38d94c1256rD   r.   rC   N)	r   r•   rH   r)   r"   r©   r2   r    rx   )r   r´   rL   ÚtheHashr   r²   r   Útest_onHandshakeI  s   z2ListenComponentAuthenticatorTests.test_onHandshakec                    sl   g }g ‰ ‡ fdd„}| j }| t j|¡ |j|_d|_d}|j |¡ |  d|d j	¡ |  dt
ˆ ƒ¡ dS )zH
        Receiving a bad handshake should yield a stream error.
        c                    r°   r   rk   r±   r²   r   r   r´   a  rµ   zRListenComponentAuthenticatorTests.test_onHandshakeWrongHash.<locals>.authenticatedr¨   r®   r.   r   N)r   r•   rH   r   r¡   r)   r"   r©   r2   r¢   rx   )r   r¤   r´   rL   r·   r   r²   r   Útest_onHandshakeWrongHashZ  s   z;ListenComponentAuthenticatorTests.test_onHandshakeWrongHashN)r   r   r   rT   r-   r   rž   r¥   r¦   r¬   r¯   r¸   r¹   r   r   r   r   rŽ   ß   s    rŽ   c                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )ÚXMPPComponentServerFactoryTestsz<
    Tests for L{component.XMPPComponentServerFactory}.
    c                 C   s8   t  ¡ | _t  | jd¡| _| j d ¡| _tdƒ| j_d S )Nr   úcomponent.example.org)	r   rt   r|   ÚXMPPComponentServerFactoryÚfactoryrb   r   r	   Ú
thisEntityr,   r   r   r   r-   t  s   
z%XMPPComponentServerFactoryTests.setUpc                 C   sV   | j  | j t j¡ |  d| j j¡ |  d| jj¡ |  d| j j¡ |  d| j j¡ dS )zY
        A new connection increases the stream serial count. No logs by default.
        r   rC   N)	r   rc   ÚSTREAM_CONNECTED_EVENTr2   Úserialr½   r—   ÚrawDataInFnÚrawDataOutFnr,   r   r   r   Útest_makeConnectionz  s
   z3XMPPComponentServerFactoryTests.test_makeConnectionc                 C   s>   d| j _| j | jtj¡ |  d| jj¡ |  d| jj¡ dS )zD
        Setting logTraffic should set up raw data loggers.
        TN)r½   Ú
logTrafficr   rc   r¿   r›   rÁ   rÂ   r,   r   r   r   Útest_makeConnectionLogTraffic„  s   z=XMPPComponentServerFactoryTests.test_makeConnectionLogTrafficc                 C   sX   | j  | j t j¡ G dd„ dtƒ}t |ƒ ¡}| j  |t j¡ |  dt|  	|¡ƒ¡ dS )zQ
        An observer for stream errors should trigger onError to log it.
        c                   @   s   e Zd ZdS )z?XMPPComponentServerFactoryTests.test_onError.<locals>.TestErrorN)r   r   r   r   r   r   r   Ú	TestError“  s    rÆ   rC   N)
r   rc   r¿   Ú	Exceptionr   ÚFailureÚSTREAM_ERROR_EVENTr2   rx   ÚflushLoggedErrors)r   rÆ   Úreasonr   r   r   Útest_onError  s
   z,XMPPComponentServerFactoryTests.test_onErrorc                 C   s<   | j  | j t j¡ |  d| jj¡ |  | j | jjd ¡ dS )zG
        Make sure a new stream is added to the routing table.
        r»   N)r   rc   rH   ÚassertInr|   ry   r—   r,   r   r   r   Útest_connectionInitializedš  s
   ÿz:XMPPComponentServerFactoryTests.test_connectionInitializedc                 C   s6   | j  | j t j¡ | j  dt j¡ |  d| jj¡ dS )zU
        Make sure a stream is removed from the routing table on disconnect.
        Nr»   )r   rc   rH   ÚSTREAM_END_EVENTÚassertNotInr|   ry   r,   r   r   r   Útest_connectionLost¤  s   z3XMPPComponentServerFactoryTests.test_connectionLostN)
r   r   r   rT   r-   rÃ   rÅ   rÌ   rÎ   rÑ   r   r   r   r   rº   o  s    
	
rº   N)rT   Úhashlibr   Úzope.interface.verifyr   Útwisted.pythonr   Útwisted.trialr   Útwisted.words.protocols.jabberr   r   r   Ú"twisted.words.protocols.jabber.jidr	   Útwisted.words.xishr
   Útwisted.words.xish.utilityr   r   ÚTestCaser   r;   rO   rP   rU   r]   ri   rŽ   rº   r   r   r   r   Ú<module>   s&   ("? 