
    VhR6                        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      Z G d dej(                        Z G d dej(                        Z G d dej(                        Z G d de
j0                        Z G d dej(                        Z G d dej(                        Z G d dej(                        Z G d dej(                        Zy)z7
Tests for L{twisted.words.protocols.jabber.component}
    )sha1)verifyObject)failure)unittest)	componentijabber	xmlstream)JID)domish)XmlPipec                       e Zd Zd Zd Zy)DummyTransportc                     || _         y N)list)selfr   s     W/home/dcms/DCMS/lib/python3.12/site-packages/twisted/words/test/test_jabbercomponent.py__init__zDummyTransport.__init__   s	    	    c                 :    | j                   j                  |       y r   )r   append)r   bytess     r   writezDummyTransport.write   s    		r   N)__name__
__module____qualname__r   r    r   r   r   r      s     r   r   c                       e Zd Zd Zd Zy)#ComponentInitiatingInitializerTestsc                    g | _         t        j                         | _        d| j                  _        t        j
                  | j                        | _        d| j                  _        | j                   j                  | j                  _        | j                  j                          | j                  j                  d       d| j                  _        t        j                  | j                        | _        y )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   s    r   setUpz)ComponentInitiatingInitializerTests.setUp   s    &446&.#",,T-?-?@#3 "kk00%%'##;	

 %<<T^^L	r   c                    | j                   j                         }| j                  d   }| j                  d|j                         | j                  d|j
                         | j                  t        d      j                         t        |             g |_	        | j                  j                  |j                                |S )z?
        Test basic operations of component handshake.
        	handshaker"      12345secret)r/   
initializer$   assertEqualnameurir   	hexdigeststrchildrenr	   r,   toXml)r   dr4   s      r   testHandshakez1ComponentInitiatingInitializerTests.testHandshake-   s    
 II  " KKO	inn5)9==923==?YP  	##IOO$56r   N)r   r   r   r1   r?   r   r   r   r   r      s    M"r   r   c                       e Zd Zd Zd Zy)ComponentAuthTestsc                     d| _         y NT)authComplete)r   streams     r   
authPassedzComponentAuthTests.authPassedD   s
     r   c                    d| _         g }t        j                  dd      }t        j                  |      }t        |      |_        |j                  t        j                  | j                         |j                          |j                  d       t        d      j                         j                  d      }| j                  |d   d|z   d	z          |j                  d
       | j                  | j                   d       y )NFcjidr!   sv   <stream:stream xmlns='jabber:component:accept' xmlns:stream='http://etherx.jabber.org/streams' from='cjid' id='12345'>r5   ascii   s   <handshake>s   </handshake><handshake/>T)rD   r   ConnectComponentAuthenticatorr	   r(   r   	transportaddObserverSTREAM_AUTHD_EVENTrF   r+   r,   r   r:   encoder7   )r   outlistcaxshvs        r   testAuthzComponentAuthTests.testAuthG   s    !44VXF  $%g.
y33T__E 	
 F	

 &'113::7C^b%8?%JK
'**D1r   N)r   r   r   rF   rU   r   r   r   rA   rA   C   s    !2r   rA   c                       e Zd ZdZd Zy)ServiceTestsz)
    Tests for L{component.Service}.
    c                 `    t        j                         }t        t        j                  |       y)zF
        L{component.Service} implements L{ijabber.IService}.
        N)r   Servicer   r   IService)r   services     r   test_interfacezServiceTests.test_interfacef   s"     ##%W%%w/r   N)r   r   r   __doc__r\   r   r   r   rW   rW   a   s    0r   rW   c                   $    e Zd Zd Zd Zd Zd Zy)JabberServiceHarnessc                 .    d| _         d| _        d| _        y )NF)componentConnectedFlagcomponentDisconnectedFlagtransportConnectedFlagr0   s    r   r   zJabberServiceHarness.__init__o   s    &+#).&&+#r   c                     d| _         y rC   )ra   r   r	   s     r   componentConnectedz'JabberServiceHarness.componentConnectedt   
    &*#r   c                     d| _         y rC   )rb   r0   s    r   componentDisconnectedz*JabberServiceHarness.componentDisconnectedw   s
    )-&r   c                     d| _         y rC   )rc   re   s     r   transportConnectedz'JabberServiceHarness.transportConnectedz   rg   r   N)r   r   r   r   rf   ri   rk   r   r   r   r_   r_   n   s    ,
+.+r   r_   c                       e Zd Zd Zy)JabberServiceManagerTestsc                    t        j                  dd      }t               }|j                  |       g }|j	                         j                  d       }| |_        |j                  |j                  _        |j                          | j                  d|j                         |j                  |t        j                         | j                  d|j                         |j!                  d        | j                  d|j"                         y )Nfoor'   T)r   ServiceManagerr_   setServiceParent
getFactorybuildProtocolrM   r   r   r+   r7   rc   dispatchr	   rO   ra   connectionLostrb   )r   smsvcwlistrS   s        r   testSMz JabberServiceManagerTests.testSM   s    %%eZ8"$R   ]]_**40"\\ 	 	s99: 	B	445 	s99: 	$ 	s<<=r   N)r   r   r   ry   r   r   r   rm   rm   ~   s    >r   rm   c                   "    e Zd ZdZd Zd Zd Zy)RouterTestsz(
    Tests for L{component.Router}.
    c                    t        j                         }g fd|_        t               }|j	                  d|j
                         | j                  dt        |j                               | j                  |j
                  |j                  d          t        j                  d      }|j                  j                  |       | j                  |g       y)zJ
        Test route registration and routing on incoming stanzas.
        c                 &    j                  |       S r   r   )elementrouteds    r   <lambda>z+RouterTests.test_addRoute.<locals>.<lambda>   s    v}}W'= r   zexample.orgrJ   )testnstestN)r   Routerrouter   addRoutesinkr7   lenroutesr   Elementsourcer*   )r   routerpiper   r   s       @r   test_addRoutezRouterTests.test_addRoute   s     !!#=ytyy1C./FMM-$@A..!34!'F+r   c                    t               }t               }t        j                         }|j                  d|j                         |j                  d|j                         g |j
                  j                  dfd       t        j                  d      }d|d<   d|d<   |j
                  j                  |       | j                  |g       y)	z,
        Test routing of a message.
        component1.example.orgzcomponent2.example.org/*c                 &    j                  |       S r   r~   r   outgoings    r   r   z(RouterTests.test_route.<locals>.<lambda>   s    HOOG<T r   NpresencefromtoNr   r   r   r   r   r   rN   r   r   r*   r7   )r   
component1
component2r   stanzar   s        @r   
test_routezRouterTests.test_route   s     Y
Y
!!#0*//B0*//B%%d,TU 231v/tv&&8,r   c                    t               }t               }t        j                         }|j                  d|j                         |j                  d|j                         g |j
                  j                  dfd       t        j                  d      }d|d<   d|d<   |j
                  j                  |       | j                  |g       y)	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                 &    j                  |       S r   r~   r   s    r   r   z/RouterTests.test_routeDefault.<locals>.<lambda>   s    X__W5M r   r   r   zexample.comr   r   )r   r   s2sr   r   r   s        @r   test_routeDefaultzRouterTests.test_routeDefault   s     Y
i!!#0*//Bchh'

t%MN 231v$tv&&8,r   N)r   r   r   r]   r   r   r   r   r   r   r{   r{      s    ,"-$-r   r{   c                   F    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)!ListenComponentAuthenticatorTestsz>
    Tests for L{component.ListenComponentAuthenticator}.
    c                     g | _         t        j                  d      }t        j                  |      | _        | j                   j
                  | j                  _        y )Nr!   )r$   r   ListenComponentAuthenticatorr	   r(   r   r*   )r   r&   s     r   r1   z'ListenComponentAuthenticatorTests.setUp   sA    !>>xH",,];"kk00r   c                 :    | j                   j                  d       y)zA
        Stub loseConnection because we are a transport.
        z	no reasonN)r	   ru   r0   s    r   loseConnectionz0ListenComponentAuthenticatorTests.loseConnection   s     	%%k2r   c                    g fd}| j                   }||_        |j                  |        | j                  d|j                         | j                  |j                         |j                  d       | j                  d|j                         | j                  d|j                         | j                  |j                         | j                  d|j                  j                  fd          y)zK
        The received stream header should set several attributes.
        c                 ,    j                  | |f       y r   r~   )event
observerfn	observerss     r   addOnetimeObserverzPListenComponentAuthenticatorTests.test_streamStarted.<locals>.addOnetimeObserver   s    eZ01r   Nzz<stream:stream xmlns='jabber:component:accept' xmlns:stream='http://etherx.jabber.org/streams' to='component.example.org'>)r   r   r   r3   )r	   r   makeConnectionassertIdenticalr-   assertFalse_headerSentr,   r7   versionassertNotIdentical
assertTruer&   	onElement)r   r   rS   r   s      @r   test_streamStartedz4ListenComponentAuthenticatorTests.test_streamStarted   s     		2 ^^ 2
$T266*(
*	

 	,bff-'$ 0 0 : :;Yr]Kr   c                     g }| j                   }|j                  |_        |j                  |        |j	                  d       | j                  dt        |             | j                  d|d   j                         y)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'>rJ   zinvalid-namespacer3   Nr	   r   sendStreamErrorr   r,   r7   r   	conditionr   streamErrorsrS   s      r    test_streamStartedWrongNamespacezBListenComponentAuthenticatorTests.test_streamStartedWrongNamespace
  sr     ^^)00
$
*	

 	C-.,l2.>.H.HIr   c                     g }| j                   }|j                  |_        |j                  |        |j	                  d       | j                  dt        |             | j                  d|d   j                         y)zJ
        The received stream header should have a 'to' attribute.
        z_<stream:stream xmlns='jabber:component:accept' xmlns:stream='http://etherx.jabber.org/streams'>rJ   zimproper-addressingr3   Nr   r   s      r   test_streamStartedNoToz8ListenComponentAuthenticatorTests.test_streamStartedNoTo  sr     ^^)00
$
?	
 	C-..R0@0J0JKr   c                    g }| j                   }|j                  |j                  _        t	        j
                  d      }|j                  d       |j                  j                  |       | j                  d|d          y)z<
        We expect a handshake element with a hash.
        )jabber:component:acceptr4   1234r3   N)	r	   r   r&   onHandshaker   r   
addContentr   r7   )r   
handshakesrS   r4   s       r   test_onElementz0ListenComponentAuthenticatorTests.test_onElement+  sk     
^^'1'8'8$NN#KL	V$
""9-B0r   c                 <   g }g }| j                   }|j                  |j                  _        |j                  |_        t        j                  d      }|j                  j                  |       | j                  |       | j                  d|d   j                         y)z9
        Reject elements that are not handshakes
        )r   messagenot-authorizedr3   N)r	   r   r&   r   r   r   r   r   r   r7   r   )r   r   r   rS   r   s        r   test_onElementNotHandshakez<ListenComponentAuthenticatorTests.test_onElementNotHandshake9  s     
^^'1'8'8$)00..!GH
""7+$)<+;+E+EFr   c                 (   g fd}| j                   }|j                  t         j                  |       d|_        d}|j                  j                  |       | j                  d| j                  d          | j                  dt                     y)zU
        Receiving a handshake matching the secret authenticates the stream.
        c                 (    j                  |        y r   r~   rS   authds    r   authenticatedzIListenComponentAuthenticatorTests.test_onHandshake.<locals>.authenticatedO      LLr   r   (32532c0f7dbf1253c095b18b18e36d38d94c1256rK   r3   rJ   N)	r	   r   rO   r-   r&   r   r7   r$   r   )r   r   rS   theHashr   s       @r   test_onHandshakez2ListenComponentAuthenticatorTests.test_onHandshakeI  s|     	 ^^
i::MJ<
$$W-R9CJ'r   c                 N   g }g fd}| j                   }|j                  t         j                  |       |j                  |_        d|_        d}|j                  j                  |       | j                  d|d   j                         | j                  dt                     y)zH
        Receiving a bad handshake should yield a stream error.
        c                 (    j                  |        y r   r~   r   s    r   r   zRListenComponentAuthenticatorTests.test_onHandshakeWrongHash.<locals>.authenticateda  r   r   r   r   r3   r   N)r	   r   rO   r   r   r-   r&   r   r7   r   r   )r   r   r   rS   r   r   s        @r   test_onHandshakeWrongHashz;ListenComponentAuthenticatorTests.test_onHandshakeWrongHashZ  s     	 ^^
i::MJ)00
$$W-)<+;+E+EFCJ'r   N)r   r   r   r]   r1   r   r   r   r   r   r   r   r   r   r   r   r   r      s8    13L4J"L 1G ("(r   r   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	XMPPComponentServerFactoryTestsz<
    Tests for L{component.XMPPComponentServerFactory}.
    c                     t        j                         | _        t        j                  | j                  d      | _        | j                  j                  d       | _        t        d      | j                  _        y )Nr!   component.example.org)	r   r   r   XMPPComponentServerFactoryfactoryrs   r	   r
   
thisEntityr0   s    r   r1   z%XMPPComponentServerFactoryTests.setUpt  sR    &&( ;;DKKR33D9$'(?$@!r   c                    | j                   j                  | j                   t         j                         | j                  d| j                   j                         | j                  d| j
                  j                         | j                  d| j                   j                         | j                  d| j                   j                         y)zY
        A new connection increases the stream serial count. No logs by default.
        r   rJ   N)	r	   rt   STREAM_CONNECTED_EVENTr7   serialr   r   rawDataInFnrawDataOutFnr0   s    r   test_makeConnectionz3XMPPComponentServerFactoryTests.test_makeConnectionz  s     		0P0PQDNN112DLL//0T4>>#=#=>T4>>#>#>?r   c                 &   d| j                   _        | j                  j                  | j                  t        j                         | j                  d| j                  j                         | j                  d| j                  j                         y)zD
        Setting logTraffic should set up raw data loggers.
        TN)r   
logTrafficr	   rt   r   r   r   r   r0   s    r   test_makeConnectionLogTrafficz=XMPPComponentServerFactoryTests.test_makeConnectionLogTraffic  sb     #'	0P0PQdnn&@&@Adnn&A&ABr   c                 f   | j                   j                  | j                   t         j                          G d dt              }t	        j
                   |             }| j                   j                  |t         j                         | j                  dt        | j                  |                   y)zQ
        An observer for stream errors should trigger onError to log it.
        c                       e Zd Zy)?XMPPComponentServerFactoryTests.test_onError.<locals>.TestErrorN)r   r   r   r   r   r   	TestErrorr     s    r   r   rJ   N)
r	   rt   r   	Exceptionr   FailureSTREAM_ERROR_EVENTr7   r   flushLoggedErrors)r   r   reasons      r   test_onErrorz,XMPPComponentServerFactoryTests.test_onError  sz     		0P0PQ		 	 -	(D(DEC 6 6y ABCr   c                    | j                   j                  | j                   t         j                         | j                  d| j                  j
                         | j                  | j                   | j                  j
                  d          y)zG
        Make sure a new stream is added to the routing table.
        r   N)r	   rt   rO   assertInr   r   r   r0   s    r   test_connectionInitializedz:XMPPComponentServerFactoryTests.test_connectionInitialized  sb     		0L0LM-t{{/A/ABNNDKK../FG	
r   c                    | j                   j                  | j                   t         j                         | j                   j                  dt         j                         | j	                  d| j
                  j                         y)zU
        Make sure a stream is removed from the routing table on disconnect.
        Nr   )r	   rt   rO   STREAM_END_EVENTassertNotInr   r   r0   s    r   test_connectionLostz3XMPPComponentServerFactoryTests.test_connectionLost  sW     		0L0LMi&@&@A0$++2D2DEr   N)
r   r   r   r]   r1   r   r   r   r   r   r   r   r   r   r   o  s*    A@CD
Fr   r   N)r]   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   rA   rW   rY   r_   rm   r{   r   r   r   r   r   <module>r     s     . " " H H 2 % .   %(*;*; %P2** 2<
08$$ 
0+9,, + > 1 1 >D<-(## <-~M((9(9 M(`;Fh&7&7 ;Fr   