
    YfR6                        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
j                  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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 Zd ZdS )DummyTransportc                     || _         d S N)list)selfr   s     Y/var/www/html/env/lib/python3.11/site-packages/twisted/words/test/test_jabbercomponent.py__init__zDummyTransport.__init__   s    			    c                 :    | j                             |           d S r   )r   append)r   bytess     r   writezDummyTransport.write   s    	r   N)__name__
__module____qualname__r   r    r   r   r   r      s2                   r   r   c                       e Zd Zd Zd ZdS )#ComponentInitiatingInitializerTestsc                    g | _         t          j                    | _        d| j        _        t          j        | j                  | _        d| j        _        | j         j        | j        _        | j        	                                 | j        
                    d           d| j        _        t          j        | 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   s    r   setUpz)ComponentInitiatingInitializerTests.setUp   s    &466&.#",T-?@@#3 "k0%%'''##;	
 	
 	

 %<T^LL			r   c                    | 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   dr4   s      r   testHandshakez1ComponentInitiatingInitializerTests.testHandshake-   s    
 I  "" KO	in555)9=999233==??YPPP  	##IOO$5$5666r   N)r   r   r   r1   r?   r   r   r   r   r      s5        M M M"    r   r   c                       e Zd Zd Zd ZdS )ComponentAuthTestsc                     d| _         d S NT)authComplete)r   streams     r   
authPassedzComponentAuthTests.authPassedD   s     r   c                 :   d| _         g }t          j        dd          }t          j        |          }t          |          |_        |                    t          j        | j	                   |
                                 |                    d           t          d                                                              d          }|                     |d         d|z   d	z              |                    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'>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   !4VXFF $$%g..
y3T_EEE 	
 F	
 	
 	

 &''1133::7CC^b%8?%JKKK
'''*D11111r   N)r   r   r   rF   rU   r   r   r   rA   rA   C   s2        ! ! !2 2 2 2 2r   rA   c                       e Zd ZdZd ZdS )ServiceTestsz)
    Tests for L{component.Service}.
    c                 `    t          j                    }t          t          j        |           dS )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-         0 0 0 0 0r   rW   c                   &    e Zd Zd Zd Zd Zd ZdS )JabberServiceHarnessc                 0    d| _         d| _        d| _        d S )NF)componentConnectedFlagcomponentDisconnectedFlagtransportConnectedFlagr0   s    r   r   zJabberServiceHarness.__init__o   s     &+#).&&+###r   c                     d| _         d S rC   )ra   r   r	   s     r   componentConnectedz'JabberServiceHarness.componentConnectedt       &*###r   c                     d| _         d S rC   )rb   r0   s    r   componentDisconnectedz*JabberServiceHarness.componentDisconnectedw   s    )-&&&r   c                     d| _         d S rC   )rc   re   s     r   transportConnectedz'JabberServiceHarness.transportConnectedz   rg   r   N)r   r   r   r   rf   ri   rk   r   r   r   r_   r_   n   sP        , , ,
+ + +. . .+ + + + +r   r_   c                       e Zd Zd ZdS )JabberServiceManagerTestsc                 ,   t          j        dd          }t                      }|                    |           g }|                                                    d           }| |_        |j        |j        _        |	                                 | 
                    d|j                   |                    |t          j                   | 
                    d|j                   |                    d            | 
                    d|j                   d S )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8"$$R     ]]__**400"\ 	 	s9::: 	B	4555 	s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dS )RouterTestsz(
    Tests for L{component.Router}.
    c                    t          j                    }g fd|_        t                      }|                    d|j                   |                     dt          |j                             |                     |j        |j        d                    t          j
        d          }|j                            |           |                     |g           dS )zJ
        Test route registration and routing on incoming stanzas.
        c                 .                         |           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     !##====yyty111C..///FM-$@AAA.!344!!!'F+++++r   c                    t                      }t                      }t          j                    }|                    d|j                   |                    d|j                   g |j                            dfd           t          j        d          }d|d<   d|d<   |j        	                    |           | 
                    |g           dS )	z,
        Test routing of a message.
        component1.example.orgzcomponent2.example.org/*c                 .                         |           S r   r~   r   outgoings    r   r   z(RouterTests.test_route.<locals>.<lambda>   s    HOOG<T<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     YY
YY
!##0*/BBB0*/BBB%%d,T,T,T,TUUU 2331v/tv&&&&8,,,,,r   c                    t                      }t                      }t          j                    }|                    d|j                   |                    d|j                   g |j                            dfd           t          j        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                 .                         |           S r   r~   r   s    r   r   z/RouterTests.test_routeDefault.<locals>.<lambda>   s    X__W5M5M r   r   r   zexample.comr   r   )r   r   s2sr   r   r   s        @r   test_routeDefaultzRouterTests.test_routeDefault   s     YY
ii!##0*/BBBch'''
t%M%M%M%MNNN 2331v$tv&&&&8,,,,,r   N)r   r   r   r]   r   r   r   r   r   r   r{   r{      sK         , , ,"- - -$- - - - -r   r{   c                   H    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 ZdS )!ListenComponentAuthenticatorTestsz>
    Tests for L{component.ListenComponentAuthenticator}.
    c                     g | _         t          j        d          }t          j        |          | _        | j         j        | j        _        d S )Nr!   )r$   r   ListenComponentAuthenticatorr	   r(   r   r*   )r   r&   s     r   r1   z'ListenComponentAuthenticatorTests.setUp   s@    !>xHH",];;"k0r   c                 :    | j                             d           dS )zA
        Stub loseConnection because we are a transport.
        z	no reasonN)r	   ru   r0   s    r   loseConnectionz0ListenComponentAuthenticatorTests.loseConnection   s      	%%k22222r   c                    g f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                 6                         | |f           d S r   r~   )event
observerfn	observerss     r   addOnetimeObserverzPListenComponentAuthenticatorTests.test_streamStarted.<locals>.addOnetimeObserver   s"    eZ01111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 	2 	2 	2 ^ 2
$T26***(((
*	
 	
 	

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

 	C--...,l2.>.HIIIIIr   c                    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'>rJ   zimproper-addressingr3   Nr   r   s      r   test_streamStartedNoToz8ListenComponentAuthenticatorTests.test_streamStartedNoTo  s     ^)0
$
?	
 	
 	
 	C--....R0@0JKKKKKr   c                     g }| j         }|j        |j        _        t	          j        d          }|                    d           |j                            |           |                     d|d                    dS )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+  sy     
^'1'8$N#KLL	V$$$
""9---B00000r   c                    g }g }| j         }|j        |j        _        |j        |_        t          j        d          }|j                            |           |                     |           | 	                    d|d         j
                   dS )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$)0.!GHH
""7+++$$$)<+;+EFFFFFr   c                 2   g f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                 2                         |            d S 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JJ<
$$W---R999CJJ'''''r   c                 N   g }g f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                 2                         |            d S 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JJ)0
$$W---)<+;+EFFFCJJ'''''r   N)r   r   r   r]   r1   r   r   r   r   r   r   r   r   r   r   r   r   r      s         1 1 13 3 3L L L4J J J"L L L 1 1 1G G G ( ( ("( ( ( ( (r   r   c                   6    e Zd ZdZd Zd Zd Zd Zd Zd Z	dS )	XMPPComponentServerFactoryTestsz<
    Tests for L{component.XMPPComponentServerFactory}.
    c                     t          j                    | _        t          j        | j        d          | _        | j                            d           | _        t          d          | j        _        d S )Nr!   component.example.org)	r   r   r   XMPPComponentServerFactoryfactoryrs   r	   r
   
thisEntityr0   s    r   r1   z%XMPPComponentServerFactoryTests.setUpt  sX    &(( ;DKRR33D99$'(?$@$@!!!r   c                 Z   | 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   rJ   N)	r	   rt   STREAM_CONNECTED_EVENTr7   serialr   r   rawDataInFnrawDataOutFnr0   s    r   test_makeConnectionz3XMPPComponentServerFactoryTests.test_makeConnectionz  s     		0PQQQDN1222DL/000T4>#=>>>T4>#>?????r   c                     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	   rt   r   r   r   r   r0   s    r   test_makeConnectionLogTrafficz=XMPPComponentServerFactoryTests.test_makeConnectionLogTraffic  sg     #'	0PQQQdn&@AAAdn&ABBBBBr   c                 n   | j                             | j         t           j                    G d dt                    }t	          j         |                      }| j                             |t           j                   |                     dt          | 	                    |                               dS )zQ
        An observer for stream errors should trigger onError to log it.
        c                       e Zd ZdS )?XMPPComponentServerFactoryTests.test_onError.<locals>.TestErrorN)r   r   r   r   r   r   	TestErrorr     s        Dr   r   rJ   N)
r	   rt   r   	Exceptionr   FailureSTREAM_ERROR_EVENTr7   r   flushLoggedErrors)r   r   reasons      r   test_onErrorz,XMPPComponentServerFactoryTests.test_onError  s     		0PQQQ	 	 	 	 		 	 	 	 --	(DEEEC 6 6y A ABBCCCCCr   c                     | 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   rO   assertInr   r   r   r0   s    r   test_connectionInitializedz:XMPPComponentServerFactoryTests.test_connectionInitialized  so     		0LMMM-t{/ABBBNDK./FG	
 	
 	
 	
 	
r   c                     | 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   rO   STREAM_END_EVENTassertNotInr   r   r0   s    r   test_connectionLostz3XMPPComponentServerFactoryTests.test_connectionLost  s^     		0LMMMi&@AAA0$+2DEEEEEr   N)
r   r   r   r]   r1   r   r   r   r   r   r   r   r   r   r   o  s         A A A@ @ @C C CD D D
 
 
F F F F 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 H H H H H H H H 2 2 2 2 2 2 % % % % % % . . . . . .               % % % % %(*; % % %P2 2 2 2 2* 2 2 2<
0 
0 
0 
0 
08$ 
0 
0 
0+ + + + +9, + + + > > > > > 1 > > >D<- <- <- <- <-(# <- <- <-~M( M( M( M( M((9 M( M( M(`;F ;F ;F ;F ;Fh&7 ;F ;F ;F ;F ;Fr   