U
    n5{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                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚDummyTransportc                 C   s
   || _ d S ©N)Úlist)Úselfr   © r   úK/tmp/pip-unpacked-wheel-mnhg7r0q/twisted/words/test/test_jabbercomponent.pyÚ__init__   s    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      s   r   c                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú#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   ó   12345secret)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'   r2   r   r   r   r   r      s   r   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚComponentAuthTestsc                 C   s
   d| _ d S ©NT)ÚauthComplete)r   Ústreamr   r   r   Ú
authPassedD   s    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)r5   r   ZConnectComponentAuthenticatorr   r   r   Ú	transportÚaddObserverÚSTREAM_AUTHD_EVENTr7   r"   r#   r   r.   Úencoder+   )r   ZoutlistÚcaÚxsZhvr   r   r   ÚtestAuthG   s    

ÿ
zComponentAuthTests.testAuthN)r   r   r   r7   rA   r   r   r   r   r3   C   s   r3   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__rD   r   r   r   r   rB   a   s   rB   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   s
   d| _ d S r4   )rG   ©r   r   r   r   r   ÚcomponentConnectedt   s    z'JabberServiceHarness.componentConnectedc                 C   s
   d| _ d S r4   )rH   r&   r   r   r   ÚcomponentDisconnectedw   s    z*JabberServiceHarness.componentDisconnectedc                 C   s
   d| _ d S r4   )rI   rJ   r   r   r   ÚtransportConnectedz   s    z'JabberServiceHarness.transportConnectedN)r   r   r   r   rK   rL   rM   r   r   r   r   rF   n   s   rF   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ServiceManagerrF   ZsetServiceParentZ
getFactoryÚbuildProtocolr;   r   r   r"   r+   rI   Údispatchr   r=   rG   ÚconnectionLostrH   )r   ÚsmZsvcZwlistr@   r   r   r   ÚtestSM   s    


z JabberServiceManagerTests.testSMN)r   r   r   rS   r   r   r   r   rN   ~   s   rN   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
   ˆ   | ¡S r   ©r   ©Úelement©Zroutedr   r   Ú<lambda>«   ó    z+RouterTests.test_addRoute.<locals>.<lambda>zexample.orgr9   )ZtestnsÚtestN)r   ÚRouterZrouter   ÚaddRouteÚsinkr+   ÚlenÚroutesr
   ÚElementÚsourcer!   )r   ÚrouterÚpiperW   r   rX   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                    s
   ˆ   | ¡S r   rU   rV   ©Úoutgoingr   r   rY   Á   rZ   z(RouterTests.test_route.<locals>.<lambda>©NZpresenceÚfromÚtoN©r   r   r\   r]   r^   rb   r<   r
   ra   r!   r+   )r   Ú
component1Z
component2rc   Ústanzar   rh   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.
        rf   Nrg   c                    s
   ˆ   | ¡S r   rU   rV   rh   r   r   rY   ×   rZ   z/RouterTests.test_routeDefault.<locals>.<lambda>rj   rk   zexample.comrl   rm   )r   rn   Zs2src   ro   r   rh   r   Útest_routeDefaultÈ   s    
zRouterTests.test_routeDefaultN)r   r   r   rE   re   rp   rq   r   r   r   r   rT       s   rT   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   rQ   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   rU   )Ú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   rg   r(   )r   rv   ÚmakeConnectionÚassertIdenticalr$   ÚassertFalseZ_headerSentr#   r+   ÚversionÚassertNotIdenticalÚ
assertTruer   Ú	onElement)r   rv   r@   r   ru   r   Útest_streamStartedð   s    
ÿz4ListenComponentAuthenticatorTests.test_streamStartedc                 C   sL   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'>r9   zinvalid-namespacer(   N©r   r   ÚsendStreamErrorrw   r#   r+   r_   Ú	condition©r   ÚstreamErrorsr@   r   r   r   Ú test_streamStartedWrongNamespace
  s    
ÿzBListenComponentAuthenticatorTests.test_streamStartedWrongNamespacec                 C   sL   g }| j }|j|_| | ¡ | d¡ |  dt|ƒ¡ |  d|d j¡ dS )zJ
        The received stream header should have a 'to' attribute.
        z_<stream:stream xmlns='jabber:component:accept' xmlns:stream='http://etherx.jabber.org/streams'>r9   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
   ra   Z
addContentr}   r+   )r   Ú
handshakesr@   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
   ra   r}   ry   r+   r�   )r   r‰   rƒ   r@   rW   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                    s   ˆ   | ¡ d S r   rU   ©r@   ©Zauthdr   r   ÚauthenticatedO  s    zIListenComponentAuthenticatorTests.test_onHandshake.<locals>.authenticatedr‡   Z(32532c0f7dbf1253c095b18b18e36d38d94c1256r:   r(   r9   N)	r   rv   r=   r$   r   rˆ   r+   r   r_   )r   r�   r@   Ú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                    s   ˆ   | ¡ d S r   rU   rŽ   r�   r   r   r�   a  s    zRListenComponentAuthenticatorTests.test_onHandshakeWrongHash.<locals>.authenticatedr‡   rŒ   r(   r   N)r   rv   r=   r   r€   r$   r   rˆ   r+   r�   r_   )r   rƒ   r�   r@   r‘   r   r�   r   Útest_onHandshakeWrongHashZ  s    z;ListenComponentAuthenticatorTests.test_onHandshakeWrongHashN)r   r   r   rE   r'   rs   r~   r„   r…   rŠ   r�   r’   r“   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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   r\   rc   ZXMPPComponentServerFactoryÚfactoryrO   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   r9   N)	r   rP   ÚSTREAM_CONNECTED_EVENTr+   Úserialr–   rx   Ú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   rP   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�   r9   N)
r   rP   r—   Ú	Exceptionr   ZFailureZSTREAM_ERROR_EVENTr+   r_   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   rP   r=   ZassertInrc   r`   rx   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   rP   r=   ZSTREAM_END_EVENTZassertNotInrc   r`   r&   r   r   r   Útest_connectionLost¤  s    z3XMPPComponentServerFactoryTests.test_connectionLostN)
r   r   r   rE   r'   r›   rœ   r    r¡   r¢   r   r   r   r   r”   o  s   
	
r”   N)rE   Ú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   r3   rB   rC   rF   rN   rT   rr   r”   r   r   r   r   Ú<module>   s$   ("? 