o
    Óó·iR6  ã                   @   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   úmC:\Users\psant\Downloads\PAF backend\school\venv\Lib\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'>Z12345)Úoutputr   ZAuthenticatorÚauthenticatorÚpasswordÚ	XmlStreamÚ	namespacer   ÚsendÚconnectionMadeÚdataReceivedÚsidr   Z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   ó   12345secretN)r(   Z
initializer   ÚassertEqualÚnameÚurir   Ú	hexdigestÚstrÚchildrenr   r&   ZtoXml)r   Údr,   r   r   r   ÚtestHandshake-   s   

z1ComponentInitiatingInitializerTests.testHandshakeN)r   r   r   r*   r5   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 )NFZcjidr   sv   <stream:stream xmlns='jabber:component:accept' xmlns:stream='http://etherx.jabber.org/streams' from='cjid' id='12345'>r-   Úasciié   s   <handshake>s   </handshake>ú<handshake/>T)r9   r   ZConnectComponentAuthenticatorr   r"   r   Ú	transportÚaddObserverÚSTREAM_AUTHD_EVENTr;   r%   r&   r   r1   Úencoder.   )r   ZoutlistÚcaÚxsZhvr   r   r   ÚtestAuthG   s   

ÿ
zComponentAuthTests.testAuthN)r   r   r   r;   rE   r   r   r   r   r6   C   r   r6   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   ZIService)r   Zservicer   r   r   Útest_interfacef   s   zServiceTests.test_interfaceN)r   r   r   Ú__doc__rH   r   r   r   r   rF   a   s    rF   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   r7   r8   )rK   ©r   r   r   r   r   ÚcomponentConnectedt   r   z'JabberServiceHarness.componentConnectedc                 C   r7   r8   )rL   r)   r   r   r   ÚcomponentDisconnectedw   r   z*JabberServiceHarness.componentDisconnectedc                 C   r7   r8   )rM   rN   r   r   r   ÚtransportConnectedz   r   z'JabberServiceHarness.transportConnectedN)r   r   r   r   rO   rP   rQ   r   r   r   r   rJ   n   s
    rJ   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 )NZfoor!   T)r   ZServiceManagerrJ   ZsetServiceParentZ
getFactoryÚbuildProtocolr?   r   r   r%   r.   rM   Údispatchr   rA   rK   ÚconnectionLostrL   )r   ÚsmZsvcZwlistrD   r   r   r   ÚtestSM   s   


z JabberServiceManagerTests.testSMN)r   r   r   rW   r   r   r   r   rR   ~   s    rR   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©Zroutedr   r   Ú<lambda>«   ó   
 z+RouterTests.test_addRoute.<locals>.<lambda>zexample.orgr=   )ZtestnsÚtestN)r   ÚRouterZrouter   ÚaddRouteÚsinkr.   ÚlenÚroutesr
   ÚElementÚsourcer$   )r   ÚrouterÚpiper\   r   r]   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                    rY   r   rZ   r[   ©Úoutgoingr   r   r^   Á   r_   z(RouterTests.test_route.<locals>.<lambda>©NZpresenceÚfromÚtoN©r   r   ra   rb   rc   rg   r@   r
   rf   r$   r.   )r   Ú
component1Z
component2rh   Ústanzar   rm   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.
        rk   Nrl   c                    rY   r   rZ   r[   rm   r   r   r^   ×   r_   z/RouterTests.test_routeDefault.<locals>.<lambda>ro   rp   zexample.comrq   rr   )r   rs   Zs2srh   rt   r   rm   r   Útest_routeDefaultÈ   s   
zRouterTests.test_routeDefaultN)r   r   r   rI   rj   ru   rv   r   r   r   r   rX       s
    rX   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   Z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   rU   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   rZ   )ÚeventZ
observerfn©Z	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   rl   r+   )r   r{   ÚmakeConnectionÚassertIdenticalr'   ÚassertFalseZ_headerSentr&   r.   ÚversionÚassertNotIdenticalÚ
assertTruer    Ú	onElement)r   r{   rD   r   rz   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'>r=   zinvalid-namespacer+   N©r   r   ÚsendStreamErrorr|   r&   r.   rd   Ú	condition©r   ÚstreamErrorsrD   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'>r=   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
   rf   Z
addContentr‚   r.   )r   Ú
handshakesrD   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
   rf   r‚   r~   r.   r‡   )r   r�   r‰   rD   r\   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   rZ   ©rD   ©Zauthdr   r   ÚauthenticatedO  ó   zIListenComponentAuthenticatorTests.test_onHandshake.<locals>.authenticatedr�   Z(32532c0f7dbf1253c095b18b18e36d38d94c1256r>   r+   r=   N)	r   r{   rA   r'   r    rŽ   r.   r   rd   )r   r—   rD   Ú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   rZ   r•   r–   r   r   r—   a  r˜   zRListenComponentAuthenticatorTests.test_onHandshakeWrongHash.<locals>.authenticatedr�   r’   r+   r   N)r   r{   rA   r   r†   r'   r    rŽ   r.   r‡   rd   )r   r‰   r—   rD   r™   r   r–   r   Útest_onHandshakeWrongHashZ  s   z;ListenComponentAuthenticatorTests.test_onHandshakeWrongHashN)r   r   r   rI   r*   rx   rƒ   rŠ   r‹   r�   r“   rš   r›   r   r   r   r   rw   ß   s    rw   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   ra   rh   ZXMPPComponentServerFactoryÚfactoryrS   r   r	   Z
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   r=   N)	r   rT   ÚSTREAM_CONNECTED_EVENTr.   Ú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ž   Z
logTrafficr   rT   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¥   r=   N)
r   rT   rŸ   Ú	Exceptionr   ZFailureZSTREAM_ERROR_EVENTr.   rd   Z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   rT   rA   ZassertInrh   re   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   rT   rA   ZSTREAM_END_EVENTZassertNotInrh   re   r)   r   r   r   Útest_connectionLost¤  s   z3XMPPComponentServerFactoryTests.test_connectionLostN)
r   r   r   rI   r*   r£   r¤   r¨   r©   rª   r   r   r   r   rœ   o  s    
	
rœ   N)rI   Úhashlibr   Zzope.interface.verifyr   Ztwisted.pythonr   Ztwisted.trialr   Ztwisted.words.protocols.jabberr   r   r   Z"twisted.words.protocols.jabber.jidr	   Ztwisted.words.xishr
   Ztwisted.words.xish.utilityr   r   ZTestCaser   r6   rF   rG   rJ   rR   rX   rw   rœ   r   r   r   r   Ú<module>   s&   ("? 