U
    
W[|7  ã                   @   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
 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)Úunicode)Ú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   úI/usr/lib/python3/dist-packages/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ÚnameZurir   Ú	hexdigestr   Zchildrenr	   r$   ZtoXml)r   Údr*   r   r   r   ÚtestHandshake+   s    

ÿz1ComponentInitiatingInitializerTests.testHandshakeN)r   r   r   r(   r0   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   Ú
authPassedB   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)r3   r   ZConnectComponentAuthenticatorr	   r    r   Ú	transportÚaddObserverÚSTREAM_AUTHD_EVENTr5   r#   r$   r   r.   Úencoder,   )r   ZoutlistZcaÚxsZhvr   r   r   ÚtestAuthE   s    

ÿ

zComponentAuthTests.testAuthN)r   r   r   r5   r>   r   r   r   r   r1   A   s   r1   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   Úservicer   r   r   Útest_interfaced   s    zServiceTests.test_interfaceN)r   r   r   Ú__doc__rB   r   r   r   r   r?   _   s   r?   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   n   s    zJabberServiceHarness.__init__c                 C   s
   d| _ d S r2   )rE   ©r   r	   r   r   r   ÚcomponentConnecteds   s    z'JabberServiceHarness.componentConnectedc                 C   s
   d| _ d S r2   )rF   r'   r   r   r   ÚcomponentDisconnectedv   s    z*JabberServiceHarness.componentDisconnectedc                 C   s
   d| _ d S r2   )rG   rH   r   r   r   ÚtransportConnectedy   s    z'JabberServiceHarness.transportConnectedN)r   r   r   r   rI   rJ   rK   r   r   r   r   rD   m   s   rD   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ServiceManagerrD   ZsetServiceParentZ
getFactoryÚbuildProtocolr9   r   r   r#   r,   rG   Údispatchr	   r;   rE   ÚconnectionLostrF   )r   ZsmZsvcZwlistr=   r   r   r   ÚtestSM~   s    


z JabberServiceManagerTests.testSMN)r   r   r   rP   r   r   r   r   rL   }   s   rL   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.orgr7   )ZtestnsZtestN)r   ÚRouterZrouter   ÚaddRouteÚsinkr,   ÚlenÚroutesr   ÚElementÚsourcer"   )r   ÚrouterÚpiperT   r   rU   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   rR   rS   ©Zoutgoingr   r   rV   Ã   rW   z(RouterTests.test_route.<locals>.<lambda>©NZpresenceÚfromÚtoN©r   r   rX   rY   rZ   r^   r:   r   r]   r"   r,   )r   Ú
component1Z
component2r_   Ústanzar   rd   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.
        rb   Nrc   c                    s
   ˆ   | ¡S r   rR   rS   rd   r   r   rV   Ú   rW   z/RouterTests.test_routeDefault.<locals>.<lambda>re   rf   zexample.comrg   rh   )r   ri   Zs2sr_   rj   r   rd   r   Útest_routeDefaultË   s    
zRouterTests.test_routeDefaultN)r   r   r   rC   ra   rk   rl   r   r   r   r   rQ       s   rQ   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	   rO   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   rR   )Z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   rc   r)   )r	   rp   ÚmakeConnectionÚassertIdenticalr%   ÚassertFalseZ_headerSentr$   r,   ÚversionÚassertNotIdenticalZ
assertTruer   Ú	onElement)r   rp   r=   r   ro   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'>r7   zinvalid-namespacer)   N©r	   r   ÚsendStreamErrorrq   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'>r7   zimproper-addressingr)   Nrx   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   r]   Z
addContentrv   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   ry   r   r]   rv   rs   r,   rz   )r   r‚   r|   r=   rT   r   r   r   Útest_onElementNotHandshake<  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   rR   ©r=   ©Zauthdr   r   ÚauthenticatedS  s    zIListenComponentAuthenticatorTests.test_onHandshake.<locals>.authenticatedr€   Z(32532c0f7dbf1253c095b18b18e36d38d94c1256r8   r)   r7   N)	r	   rp   r;   r%   r   r   r,   r   r[   )r   r‰   r=   ÚtheHashr   rˆ   r   Útest_onHandshakeM  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   rR   r‡   rˆ   r   r   r‰   f  s    zRListenComponentAuthenticatorTests.test_onHandshakeWrongHash.<locals>.authenticatedr€   r…   r)   r   N)r	   rp   r;   r   ry   r%   r   r   r,   rz   r[   )r   r|   r‰   r=   rŠ   r   rˆ   r   Útest_onHandshakeWrongHash_  s    z;ListenComponentAuthenticatorTests.test_onHandshakeWrongHashN)r   r   r   rC   r(   rn   rw   r}   r~   rƒ   r†   r‹   rŒ   r   r   r   r   rm   ã   s   rm   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   rX   r_   ZXMPPComponentServerFactoryÚfactoryrM   r	   r
   Z
thisEntityr'   r   r   r   r(   z  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   r7   N)	r	   rN   ÚSTREAM_CONNECTED_EVENTr,   Úserialr   rr   ÚrawDataInFnÚrawDataOutFnr'   r   r   r   Útest_makeConnection‚  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	   rN   r   ru   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–   r7   N)
r	   rN   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	   rN   r;   ZassertInr_   r\   rr   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	   rN   r;   ZSTREAM_END_EVENTZassertNotInr_   r\   r'   r   r   r   Útest_connectionLost²  s    z3XMPPComponentServerFactoryTests.test_connectionLostN)
r   r   r   rC   r(   r”   r•   r™   rš   r›   r   r   r   r   r   u  s   
r   N) rC   Zhashlibr   Zzope.interface.verifyr   Ztwisted.pythonr   Ztwisted.python.compatr   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   r1   r?   r@   rD   rL   rQ   rm   r   r   r   r   r   Ú<module>   s&   '#C 