o
    ¼>h£@  ã                   @   s8  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mZ ddlmZ ddlmZ zdd	lmZ W n eyG   d
ZdZY nw dZdZdZdZde ZdZde ZG dd„ dejƒZG dd„ dƒZG dd„ deejƒZG dd„ deejƒZG dd„ deejƒZG dd„ dejƒZ G dd „ d ejƒZ!d
S )!z4
Tests for L{twisted.words.protocols.jabber.client}
é    )Úsha1)ÚskipIf)Údefer)Úunittest)ÚclientÚerrorÚjidÚ	xmlstream)ÚSASLInitiatingInitializer)Úutility)ÚsslN)TzSSL not available)FÚ z//iq[@type="get"]/query[@xmlns="jabber:iq:auth"]z//iq[@type="set"]/query[@xmlns="jabber:iq:auth"]z urn:ietf:params:xml:ns:xmpp-bindz"/iq[@type="set"]/bind[@xmlns="%s"]z#urn:ietf:params:xml:ns:xmpp-sessionz%/iq[@type="set"]/session[@xmlns="%s"]c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚCheckVersionInitializerTestsc                 C   s"   t  ¡ }t  |¡}t |¡| _d S ©N)r	   ÚAuthenticatorÚ	XmlStreamr   ÚCheckVersionInitializerÚinit)ÚselfÚaÚxs© r   ú„/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/words/test/test_jabberclient.pyÚsetUp#   s   
z"CheckVersionInitializerTests.setUpc                 C   s   d| j j_| j  ¡  dS )z3
        Test supported version number 1.0
        )é   r   N)r   r	   ÚversionÚ
initialize©r   r   r   r   ÚtestSupported(   s   
z*CheckVersionInitializerTests.testSupportedc                 C   s.   d| j j_|  tj| j j¡}|  d|j¡ dS )zK
        Test unsupported version number 0.0, and check exception.
        )r   r   zunsupported-versionN)	r   r	   r   ÚassertRaisesr   ÚStreamErrorr   ÚassertEqualÚ	condition)r   Úexcr   r   r   ÚtestNotSupported/   s   
z-CheckVersionInitializerTests.testNotSupportedN)Ú__name__Ú
__module__Ú__qualname__r   r   r$   r   r   r   r   r   "   s    r   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚInitiatingInitializerHarnessaè  
    Testing harness for interacting with XML stream initializers.

    This sets up an L{utility.XmlPipe} to create a communication channel between
    the initializer and the stubbed receiving entity. It features a sink and
    source side that both act similarly to a real L{xmlstream.XmlStream}. The
    sink is augmented with an authenticator to which initializers can be added.

    The harness also provides some utility methods to work with event observers
    and deferreds.
    c                 C   s4   g | _ t ¡ | _| jj| _t d¡| _| j| j_d S )Nzexample.org)Úoutputr   ÚXmlPipeÚpipeÚsinkr	   ÚConnectAuthenticatorÚauthenticatorr   r   r   r   r   E   s
   

z"InitiatingInitializerHarness.setUpc                 C   s(   t  ¡ }| |¡ | jj ||j¡ |S )aù  
        Observe an output event, returning a deferred.

        The returned deferred will be fired when the given event has been
        observed on the source end of the L{XmlPipe} tied to the protocol
        under test. The handler is added as the first callback.

        @param event: The event to be observed. See
            L{utility.EventDispatcher.addOnetimeObserver}.
        @param handler: The handler to be called with the observed event object.
        @rtype: L{defer.Deferred}.
        )r   ÚDeferredÚaddCallbackr+   ÚsourceÚaddOnetimeObserverÚcallback)r   ÚeventÚhandlerÚdr   r   r   ÚwaitForL   s   
z$InitiatingInitializerHarness.waitForN)r%   r&   r'   Ú__doc__r   r7   r   r   r   r   r(   8   s    r(   c                       s@   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Z‡  Z	S )ÚIQAuthInitializerTestsz0
    Tests for L{client.IQAuthInitializer}.
    c                    s2   t ƒ  ¡  t | j¡| _t d¡| j_d| j_	d S )Núuser@example.com/resourceÚsecret)
Úsuperr   r   ÚIQAuthInitializerr	   r   r   ÚJIDr.   Úpasswordr   ©Ú	__class__r   r   r   d   s   
zIQAuthInitializerTests.setUpc                    s>   ‡ ‡fdd„}‡fdd„‰ ˆ  t|¡}ˆj ¡ }t ||g¡S )zè
        Test plain-text authentication.

        Act as a server supporting plain-text authentication and expect the
        C{password} field to be filled with the password. Then act as if
        authentication succeeds.
        c                    óX   t  | d¡}| d¡ |j d¡ |j d¡ |j d¡ ˆ tˆ ¡}ˆjj |¡ |S ©zÄ
            Called when the initializer sent a query for authentication methods.

            The response informs the client that plain-text authentication
            is supported.
            Úresult©zjabber:iq:authÚqueryÚusernamer?   Úresource©	r	   Ú
toResponseÚ
addElementrF   r7   ÚIQ_AUTH_SETr+   r1   Úsend©ÚiqÚresponser6   ©Ú	onAuthSetr   r   r   Ú	onAuthGets   ó   	
z7IQAuthInitializerTests.testPlainText.<locals>.onAuthGetc                    sZ   ˆ   dt| jjƒ¡ ˆ   dt| jjƒ¡ ˆ   dt| jjƒ¡ t | d¡}ˆ jj	 
|¡ dS )úÇ
            Called when the initializer sent the authentication request.

            The server checks the credentials and responds with an empty result
            signalling success.
            Úuserr;   rH   rD   N)r!   ÚstrrF   rG   r?   rH   r	   rJ   r+   r1   rM   ©rO   rP   r   r   r   rR   Š   s
   z7IQAuthInitializerTests.testPlainText.<locals>.onAuthSet)r7   ÚIQ_AUTH_GETr   r   r   ÚgatherResults©r   rS   Úd1Úd2r   rQ   r   ÚtestPlainTextj   s
   	
z$IQAuthInitializerTests.testPlainTextc                    sF   ‡ ‡fdd„}‡fdd„‰ dˆj _ˆ t|¡}ˆj ¡ }t ||g¡S )a"  
        Test digest authentication.

        Act as a server supporting digest authentication and expect the
        C{digest} field to be filled with a sha1 digest of the concatenated
        stream session identifier and password. Then act as if authentication
        succeeds.
        c                    rB   )zÀ
            Called when the initializer sent a query for authentication methods.

            The response informs the client that digest authentication is
            supported.
            rD   rE   rG   ÚdigestrH   rI   rN   rQ   r   r   rS   ª   rT   z4IQAuthInitializerTests.testDigest.<locals>.onAuthGetc                    sb   ˆ   dt| jjƒ¡ ˆ   tdƒ ¡ t| jjƒ¡ ˆ   dt| jjƒ¡ t 	| d¡}ˆ j
j |¡ dS )rU   rV   s   12345secretrH   rD   N)r!   rW   rF   rG   r   Ú	hexdigestr_   rH   r	   rJ   r+   r1   rM   rX   r   r   r   rR   Á   s
   z4IQAuthInitializerTests.testDigest.<locals>.onAuthSetÚ12345)r	   Úsidr7   rY   r   r   r   rZ   r[   r   rQ   r   Ú
testDigest    s   

z!IQAuthInitializerTests.testDigestc                    ó>   ‡ fdd„}ˆ   t|¡}ˆ j ¡ }ˆ  |tj¡ t ||g¡S )zT
        Test initializer failure of request for fields for authentication.
        c                    ó"   t  d¡ | ¡}ˆ jj |¡ dS )z²
            Called when the initializer sent a query for authentication methods.

            The server responds that the client is not authorized to authenticate.
            únot-authorizedN©r   ÚStanzaErrorrJ   r+   r1   rM   rX   r   r   r   rS   à   s   z?IQAuthInitializerTests.testFailRequestFields.<locals>.onAuthGet©	r7   rY   r   r   ÚassertFailurer   rh   r   rZ   r[   r   r   r   ÚtestFailRequestFieldsÛ   s
   

z,IQAuthInitializerTests.testFailRequestFieldsc                    sL   ‡ ‡fdd„}‡fdd„‰ ˆ  t|¡}ˆj ¡ }ˆ |tj¡ t ||g¡S )z;
        Test initializer failure to authenticate.
        c                    rB   rC   rI   rN   rQ   r   r   rS   ù   rT   z6IQAuthInitializerTests.testFailAuth.<locals>.onAuthGetc                    re   )zÂ
            Called when the initializer sent the authentication request.

            The server checks the credentials and responds with a not-authorized
            stanza error.
            rf   Nrg   rX   r   r   r   rR     s   z6IQAuthInitializerTests.testFailAuth.<locals>.onAuthSetri   r[   r   rQ   r   ÚtestFailAuthô   s   
z#IQAuthInitializerTests.testFailAuth)
r%   r&   r'   r8   r   r^   rc   rk   rl   Ú__classcell__r   r   r@   r   r9   _   s    6;r9   c                       ó0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚBindInitializerTestsz.
    Tests for L{client.BindInitializer}.
    c                    s*   t ƒ  ¡  t | j¡| _t d¡| j_d S )Nr:   )	r<   r   r   ÚBindInitializerr	   r   r   r>   r.   r   r@   r   r   r   +  s   
zBindInitializerTests.setUpc                    sF   ‡ fdd„}‡ fdd„}ˆ   t|¡}ˆ j ¡ }| |¡ t ||g¡S )zK
        Set up a stream, and act as if resource binding succeeds.
        c                    s<   t  | d¡}| tdf¡ |jjddd ˆ jj |¡ d S )NrD   Úbindr   úuser@example.com/other resource)Úcontent)r	   rJ   rK   ÚNS_BINDrq   r+   r1   rM   rX   r   r   r   ÚonBind5  s   z.BindInitializerTests.testBasic.<locals>.onBindc                    s   ˆ   t d¡ˆ jj¡ d S )Nrr   )r!   r   r>   r.   )rD   r   r   r   Úcb;  s   ÿz*BindInitializerTests.testBasic.<locals>.cb)r7   ÚIQ_BIND_SETr   Ústartr0   r   rZ   )r   ru   rv   r\   r]   r   r   r   Ú	testBasic0  s   

zBindInitializerTests.testBasicc                    rd   )zH
        Set up a stream, and act as if resource binding fails.
        c                    ó"   t  d¡ | ¡}ˆ jj |¡ d S )NÚconflictrg   rX   r   r   r   ru   J  ó   z0BindInitializerTests.testFailure.<locals>.onBind)	r7   rw   r   rx   rj   r   rh   r   rZ   )r   ru   r\   r]   r   r   r   ÚtestFailureE  ó
   
z BindInitializerTests.testFailure)r%   r&   r'   r8   r   ry   r}   rm   r   r   r@   r   ro   &  s
    ro   c                       rn   )ÚSessionInitializerTestsz1
    Tests for L{client.SessionInitializer}.
    c                    s   t ƒ  ¡  t | j¡| _d S r   )r<   r   r   ÚSessionInitializerr	   r   r   r@   r   r   r   Y  s   
zSessionInitializerTests.setUpc                    s0   ‡ fdd„}ˆ   t|¡}ˆ j ¡ }t ||g¡S )zP
        Set up a stream, and act as if session establishment succeeds.
        c                    s   t  | d¡}ˆ jj |¡ d S )NrD   )r	   rJ   r+   r1   rM   rX   r   r   r   Ú	onSessionb  s   z6SessionInitializerTests.testSuccess.<locals>.onSession)r7   ÚIQ_SESSION_SETr   rx   r   rZ   ©r   r   r\   r]   r   r   r   ÚtestSuccess]  s   
z#SessionInitializerTests.testSuccessc                    rd   )zM
        Set up a stream, and act as if session establishment fails.
        c                    rz   )NÚ	forbiddenrg   rX   r   r   r   r   o  r|   z6SessionInitializerTests.testFailure.<locals>.onSession)	r7   r‚   r   rx   rj   r   rh   r   rZ   rƒ   r   r   r   r}   j  r~   z#SessionInitializerTests.testFailure)r%   r&   r'   r8   r   r„   r}   rm   r   r   r@   r   r   T  s
    r   c                   @   s   e Zd ZdZdd„ ZdS )ÚBasicAuthenticatorTestszB
    Test for both BasicAuthenticator and basicClientFactory.
    c                 C   sˆ   t  d¡| _t | jd¡ d¡}|  d|jj¡ |  | j|jj ¡ |  d|jj	¡ |j
\}}|  |tj¡ |  |tj¡ |  |j¡ dS )a  
        Authenticator and stream are properly constructed by the factory.

        The L{xmlstream.XmlStream} protocol created by the factory has the new
        L{client.BasicAuthenticator} instance in its C{authenticator}
        attribute.  It is set up with C{jid} and C{password} as passed to the
        factory, C{otherHost} taken from the client JID. The stream futher has
        two initializers, for TLS and authentication, of which the first has
        its C{required} attribute set to C{True}.
        r:   r;   Núexample.com)r   r>   Ú
client_jidr   ÚbasicClientFactoryÚbuildProtocolr!   r.   Ú	otherHostr?   ÚinitializersÚassertIsInstancer	   ÚTLSInitiatingInitializerr=   ÚassertFalseÚrequired)r   r   ÚtlsÚauthr   r   r   Ú
test_basic~  s   
z"BasicAuthenticatorTests.test_basicN)r%   r&   r'   r8   r“   r   r   r   r   r†   y  s    r†   c                   @   s(   e Zd ZdZdd„ ZeeŽ dd„ ƒZdS )ÚXMPPAuthenticatorTestsz@
    Test for both XMPPAuthenticator and XMPPClientFactory.
    c                 C   sÌ   t  d¡| _t | jd¡ d¡}|  d|jj¡ |  | j|jj ¡ |  d|jj	¡ |j
\}}}}}|  |tj¡ |  |t¡ |  |tj¡ |  |tj¡ |  |j¡ |  |j¡ |  |j¡ |  |j¡ dS )zú
        Test basic operations.

        Setup an XMPPClientFactory, which sets up an XMPPAuthenticator, and let
        it produce a protocol instance. Then inspect the instance variables of
        the authenticator and XML stream objects.
        r:   r;   Nr‡   )r   r>   rˆ   r   ÚXMPPClientFactoryrŠ   r!   r.   r‹   r?   rŒ   r   r	   rŽ   r
   rp   r€   Ú
assertTruer   r   )r   r   r   r‘   Úsaslrq   Úsessionr   r   r   r“   £  s   z!XMPPAuthenticatorTests.test_basicc           
         s„   g ‰ d
‡ fdd„	}t  d¡| _t ¡ }tj| jd|d}|  tj	d|¡ | 
d¡}|j\}}}}}	|  |tj	¡ |  |ˆ d	 ¡ dS )zG
        A TLS configuration is passed to the TLS initializer.
        TNc                    s   ˆ   |¡ d S r   )Úappend)r   r   r   ÚconfigurationForTLS©Úconfigsr   r   r   Ë  s   z:XMPPAuthenticatorTests.test_tlsConfiguration.<locals>.initr:   r;   )rš   Ú__init__r   )TN)r   r>   rˆ   r   ÚCertificateOptionsr   r•   Úpatchr	   rŽ   rŠ   rŒ   r   ÚassertIs)
r   r   rš   Úfactoryr   r   r‘   r—   rq   r˜   r   r›   r   Útest_tlsConfigurationÄ  s   ÿ
z,XMPPAuthenticatorTests.test_tlsConfigurationN)r%   r&   r'   r8   r“   r   ÚskipWhenNoSSLr¢   r   r   r   r   r”   ž  s
    !r”   )"r8   Úhashlibr   r   r   Útwisted.internetr   Útwisted.trialÚtwisted.words.protocols.jabberr   r   r   r	   Ú#twisted.words.protocols.jabber.saslr
   Útwisted.words.xishr   r   ÚImportErrorr£   rY   rL   rt   rw   Ú
NS_SESSIONr‚   ÚTestCaser   r(   r9   ro   r   r†   r”   r   r   r   r   Ú<module>   s:   þ' H.%%