
    Vh@                        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 	 dd	lmZ d
ZdZdZdZdez  ZdZdez  Z G d dej4                        Z G d d      Z G d deej4                        Z G d deej4                        Z G d deej4                        Z G d dej4                        Z  G d d ej4                        Z!y# e$ r dZdZY w xY w)!z4
Tests for L{twisted.words.protocols.jabber.client}
    )sha1)skipIf)defer)unittest)clienterrorjid	xmlstream)SASLInitiatingInitializer)utility)ssl)F N)TzSSL not availablez//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                       e Zd Zd Zd Zd Zy)CheckVersionInitializerTestsc                     t        j                         }t        j                  |      }t        j                  |      | _        y N)r
   Authenticator	XmlStreamr   CheckVersionInitializerinit)selfaxss      T/home/dcms/DCMS/lib/python3.12/site-packages/twisted/words/test/test_jabberclient.pysetUpz"CheckVersionInitializerTests.setUp#   s2    ##%  #2226	    c                 n    d| j                   j                  _        | j                   j                          y)z3
        Test supported version number 1.0
        )   r   N)r   r
   version
initializer   s    r   testSupportedz*CheckVersionInitializerTests.testSupported(   s&     '-		#		r   c                     d| j                   j                  _        | j                  t        j
                  | j                   j                        }| j                  d|j                         y)zK
        Test unsupported version number 0.0, and check exception.
        )r   r   zunsupported-versionN)	r   r
   r   assertRaisesr   StreamErrorr    assertEqual	condition)r   excs     r   testNotSupportedz-CheckVersionInitializerTests.testNotSupported/   sM     '-		# 1 14993G3GH.>r   N)__name__
__module____qualname__r   r"   r)    r   r   r   r   "   s    7
?r   r   c                       e Zd ZdZd Zd Zy)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                     g | _         t        j                         | _        | j                  j                  | _        t        j                  d      | _        | j                  | j
                  _        y )Nzexample.org)outputr   XmlPipepipesinkr
   ConnectAuthenticatorauthenticatorr!   s    r   r   z"InitiatingInitializerHarness.setUpE   sK    OO%	&;;MJ'+'9'9$r   c                     t        j                         }|j                  |       | j                  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addCallbackr3   sourceaddOnetimeObservercallback)r   eventhandlerds       r   waitForz$InitiatingInitializerHarness.waitForL   s?     NN	g		++E1::>r   N)r*   r+   r,   __doc__r   r@   r-   r   r   r/   r/   8   s    
:r   r/   c                   :     e Zd ZdZ fdZd Zd Zd Zd Z xZ	S )IQAuthInitializerTestsz0
    Tests for L{client.IQAuthInitializer}.
    c                     t         |           t        j                  | j                        | _        t        j                  d      | j                  _        d| j                  _	        y )Nuser@example.com/resourcesecret)
superr   r   IQAuthInitializerr
   r   r	   JIDr6   passwordr   	__class__s    r   r   zIQAuthInitializerTests.setUpd   sH    ,,T^^<	!$)D!E&.#r   c                       fd} fd j                  t        |      } j                  j                         }t	        j
                  ||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                 n   t        j                  | d      }|j                  d       |j                  j                  d       |j                  j                  d       |j                  j                  d       j	                  t
              }j                  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usernamerJ   resource	r
   
toResponse
addElementrR   r@   IQ_AUTH_SETr3   r:   sendiqresponser?   	onAuthSetr   s      r   	onAuthGetz7IQAuthInitializerTests.testPlainText.<locals>.onAuthGets        !++B9H ;<NN%%j1NN%%j1NN%%j1 [)4A II!!(+Hr   c                    j                  dt        | j                  j                               j                  dt        | j                  j                               j                  dt        | j                  j
                               t        j                  | d      }j                  j                  j                  |       y)
            Called when the initializer sent the authentication request.

            The server checks the credentials and responds with an empty result
            signalling success.
            userrF   rT   rP   N)r&   strrR   rS   rJ   rT   r
   rV   r3   r:   rY   r[   r\   r   s     r   r]   z7IQAuthInitializerTests.testPlainText.<locals>.onAuthSet   s     VS):):%;<Xs288+<+<'=>ZRXX->->)?@ !++B9HII!!(+r   )r@   IQ_AUTH_GETr   r    r   gatherResultsr   r^   d1d2r]   s   `   @r   testPlainTextz$IQAuthInitializerTests.testPlainTextj   sH    	.	,  \\+y1 YY!!#""B8,,r   c                       fd} fdd j                   _         j                  t        |      } j                  j                         }t        j                  ||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                 n   t        j                  | d      }|j                  d       |j                  j                  d       |j                  j                  d       |j                  j                  d       j	                  t
              }j                  j                  j                  |       |S )z
            Called when the initializer sent a query for authentication methods.

            The response informs the client that digest authentication is
            supported.
            rP   rQ   rS   digestrT   rU   rZ   s      r   r^   z4IQAuthInitializerTests.testDigest.<locals>.onAuthGet   s     !++B9H ;<NN%%j1NN%%h/NN%%j1 [)4A II!!(+Hr   c                    j                  dt        | j                  j                               j                  t	        d      j                         t        | j                  j                               j                  dt        | j                  j                               t        j                  | d      }j                  j                  j                  |       y)ra   rb   s   12345secretrT   rP   N)r&   rc   rR   rS   r   	hexdigestrm   rT   r
   rV   r3   r:   rY   rd   s     r   r]   z4IQAuthInitializerTests.testDigest.<locals>.onAuthSet   s     VS):):%;<T.1;;=s288???STZRXX->->)?@ !++B9HII!!(+r   12345)r
   sidr@   re   r   r    r   rf   rg   s   `   @r   
testDigestz!IQAuthInitializerTests.testDigest   sV    	.	,  % \\+y1 YY!!#""B8,,r   c                       fd} j                  t        |      } j                  j                         } j	                  |t
        j                         t        j                  ||g      S )zT
        Test initializer failure of request for fields for authentication.
        c                     t        j                  d      j                  |       }j                  j                  j                  |       y)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   StanzaErrorrV   r3   r:   rY   rd   s     r   r^   z?IQAuthInitializerTests.testFailRequestFields.<locals>.onAuthGet   s9     (()9:EEbIHII!!(+r   	r@   re   r   r    assertFailurer   rw   r   rf   )r   r^   rh   ri   s   `   r   testFailRequestFieldsz,IQAuthInitializerTests.testFailRequestFields   s[    
	, \\+y1 YY!!# 	2u001""B8,,r   c                       fd} fd j                  t        |      } j                  j                         } j	                  |t
        j                         t        j                  ||g      S )z;
        Test initializer failure to authenticate.
        c                 n   t        j                  | d      }|j                  d       |j                  j                  d       |j                  j                  d       |j                  j                  d       j	                  t
              }j                  j                  j                  |       |S rO   rU   rZ   s      r   r^   z6IQAuthInitializerTests.testFailAuth.<locals>.onAuthGet   r_   r   c                     t        j                  d      j                  |       }j                  j                  j                  |       y)z
            Called when the initializer sent the authentication request.

            The server checks the credentials and responds with a not-authorized
            stanza error.
            ru   Nrv   rd   s     r   r]   z6IQAuthInitializerTests.testFailAuth.<locals>.onAuthSet  s9     (()9:EEbIHII!!(+r   rx   rg   s   `   @r   testFailAuthz#IQAuthInitializerTests.testFailAuth   s`    
	.	, \\+y1 YY!!# 	2u001""B8,,r   )
r*   r+   r,   rA   r   rj   rr   rz   r~   __classcell__rL   s   @r   rC   rC   _   s#    /4-l9-v-2/-r   rC   c                   .     e Zd ZdZ fdZd Zd Z xZS )BindInitializerTestsz.
    Tests for L{client.BindInitializer}.
    c                     t         |           t        j                  | j                        | _        t        j                  d      | j                  _        y )NrE   )	rG   r   r   BindInitializerr
   r   r	   rI   r6   rK   s    r   r   zBindInitializerTests.setUp+  s:    **4>>:	!$)D!Er   c                       fd} fd} j                  t        |      } j                  j                         }|j	                  |       t        j                  ||g      S )zK
        Set up a stream, and act as if resource binding succeeds.
        c                     t        j                  | d      }|j                  t        df       |j                  j                  dd       j
                  j                  j                  |       y )NrP   bindr	   user@example.com/other resource)content)r
   rV   rW   NS_BINDr   r3   r:   rY   rd   s     r   onBindz.BindInitializerTests.testBasic.<locals>.onBind5  sX     ++B9H& 12MM$$U4U$VII!!(+r   c                 x    j                  t        j                  d      j                  j                         y )Nr   )r&   r	   rI   r6   )rP   r   s    r   cbz*BindInitializerTests.testBasic.<locals>.cb;  s,    9:D<N<N<R<Rr   )r@   IQ_BIND_SETr   startr9   r   rf   )r   r   r   rh   ri   s   `    r   	testBasiczBindInitializerTests.testBasic0  sP    
	,	
 \\+v.YY__
r""B8,,r   c                       fd} j                  t        |      } j                  j                         } j	                  |t
        j                         t        j                  ||g      S )zH
        Set up a stream, and act as if resource binding fails.
        c                     t        j                  d      j                  |       }j                  j                  j                  |       y )Nconflictrv   rd   s     r   r   z0BindInitializerTests.testFailure.<locals>.onBindJ  s6    ((4??CHII!!(+r   )	r@   r   r   r   ry   r   rw   r   rf   )r   r   rh   ri   s   `   r   testFailurez BindInitializerTests.testFailureE  sU    
	, \\+v.YY__2u001""B8,,r   )r*   r+   r,   rA   r   r   r   r   r   s   @r   r   r   &  s    F
-*-r   r   c                   .     e Zd ZdZ fdZd Zd Z xZS )SessionInitializerTestsz1
    Tests for L{client.SessionInitializer}.
    c                 j    t         |           t        j                  | j                        | _        y r   )rG   r   r   SessionInitializerr
   r   rK   s    r   r   zSessionInitializerTests.setUpY  s"    --dnn=	r   c                       fd} j                  t        |      } j                  j                         }t	        j
                  ||g      S )zP
        Set up a stream, and act as if session establishment succeeds.
        c                 |    t        j                  | d      }j                  j                  j	                  |       y )NrP   )r
   rV   r3   r:   rY   rd   s     r   	onSessionz6SessionInitializerTests.testSuccess.<locals>.onSessionb  s-     ++B9HII!!(+r   )r@   IQ_SESSION_SETr   r   r   rf   r   r   rh   ri   s   `   r   testSuccessz#SessionInitializerTests.testSuccess]  s?    
	, \\.)4YY__""B8,,r   c                       fd} j                  t        |      } j                  j                         } j	                  |t
        j                         t        j                  ||g      S )zM
        Set up a stream, and act as if session establishment fails.
        c                     t        j                  d      j                  |       }j                  j                  j                  |       y )N	forbiddenrv   rd   s     r   r   z6SessionInitializerTests.testFailure.<locals>.onSessiono  s6    ((5@@DHII!!(+r   )	r@   r   r   r   ry   r   rw   r   rf   r   s   `   r   r   z#SessionInitializerTests.testFailurej  sU    
	, \\.)4YY__2u001""B8,,r   )r*   r+   r,   rA   r   r   r   r   r   s   @r   r   r   T  s    >--r   r   c                       e Zd ZdZd Zy)BasicAuthenticatorTestszB
    Test for both BasicAuthenticator and basicClientFactory.
    c                 b   t        j                  d      | _        t        j                  | j                  d      j                  d      }| j                  d|j                  j                         | j                  | j                  |j                  j                          | j                  d|j                  j                         |j                  \  }}| j                  |t        j                         | j                  |t        j                         | j                  |j                          y)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}.
        rE   rF   Nexample.com)r	   rI   
client_jidr   basicClientFactorybuildProtocolr&   r6   	otherHostrJ   initializersassertIsInstancer
   TLSInitiatingInitializerrH   assertFalserequired)r   r   tlsauths       r   
test_basicz"BasicAuthenticatorTests.test_basic~  s     ''"=>
 &&tAOOPTU 	(8(8(B(BC"*:*:*>*>?2#3#3#<#<= OO	Tc9#E#EFdF$<$<=&r   N)r*   r+   r,   rA   r   r-   r   r   r   r   y  s    'r   r   c                   ,    e Zd ZdZd Z ee d        Zy)XMPPAuthenticatorTestsz@
    Test for both XMPPAuthenticator and XMPPClientFactory.
    c                 v   t        j                  d      | _        t        j                  | j                  d      j                  d      }| j                  d|j                  j                         | j                  | j                  |j                  j                          | j                  d|j                  j                         |j                  \  }}}}}| j                  |t        j                         | j                  |t               | j                  |t        j                         | j                  |t        j                          | j#                  |j$                         | j#                  |j$                         | j#                  |j$                         | j'                  |j$                         y)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.
        rE   rF   Nr   )r	   rI   r   r   XMPPClientFactoryr   r&   r6   r   rJ   r   r   r
   r   r   r   r   
assertTruer   r   )r   r   r   r   saslr   sessions          r   r   z!XMPPAuthenticatorTests.test_basic  s?    ''"=>
 %%doox@NNtT 	(8(8(B(BC"*:*:*>*>?2#3#3#<#<= -/OO)dD'c9#E#EFd$=>dF$:$:;gv'@'@A%&&))*r   c                   
 g 
d
fd	}t        j                  d      | _        t        j                         }t        j                  | j                  d|      }| j                  t        j                  d|       |j                  d      }|j                  \  }}}}}	| j                  |t        j                         | j                  |
d          y)	zG
        A TLS configuration is passed to the TLS initializer.
        Nc                 (    j                  |       y r   )append)r   r   r   configurationForTLSconfigss       r   r   z:XMPPAuthenticatorTests.test_tlsConfiguration.<locals>.init  s    NN./r   rE   rF   )r   __init__r   )TN)r	   rI   r   r   CertificateOptionsr   r   patchr
   r   r   r   r   assertIs)r   r   r   factoryr   r   r   r   r   r   r   s             @r   test_tlsConfigurationz,XMPPAuthenticatorTests.test_tlsConfiguration  s    
 	0 ''"=>
 "446**OOX;N
 	

955z4H""4( -/OO)dD'c9#E#EF)71:6r   N)r*   r+   r,   rA   r   r   skipWhenNoSSLr   r-   r   r   r   r     s&    +B ]7 7r   r   )"rA   hashlibr   r   r   twisted.internetr   twisted.trialtwisted.words.protocols.jabberr   r   r	   r
   #twisted.words.protocols.jabber.saslr   twisted.words.xishr   r   r   ImportErrorre   rX   r   r   
NS_SESSIONr   TestCaser   r/   rC   r   r   r   r   r-   r   r   <module>r      s   
   " " H H I & $
  M??
,2W<2
8:E?8#4#4 ?,$ $ND-98;L;L D-N+-79J9J +-\"-:H<M<M "-J"'h// "'J@7X.. @7U  0
C/M0s   C 	C&%C&