
    Yf#                     
   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Z ee	j                   G d d                      Z G d	 d
ej                  Z G d dej                  Z G d dej                  ZdS )    )implementer)defer)unittest)jidsaslsasl_mechanisms	xmlstream)domish urn:ietf:params:xml:ns:xmpp-saslc                   0    e Zd ZdZdZdZdZd Zd Zd Z	dS )DummySASLMechanisma  
    Dummy SASL mechanism.

    This just returns the initialResponse passed on creation, stores any
    challenges and replies with the value of C{response}.

    @ivar challenge: Last received challenge.
    @type challenge: C{unicode}.
    @ivar initialResponse: Initial response to be returned when requested
                           via C{getInitialResponse} or L{None}.
    @type initialResponse: C{unicode}
    NDUMMY    c                     || _         d S NinitialResponse)selfr   s     T/var/www/html/env/lib/python3.11/site-packages/twisted/words/test/test_jabbersasl.py__init__zDummySASLMechanism.__init__"   s    .r   c                     | j         S r   r   r   s    r   getInitialResponsez%DummySASLMechanism.getInitialResponse%   s    ##r   c                     || _         | j        S r   )	challengeresponse)r   r   s     r   getResponsezDummySASLMechanism.getResponse(   s    "}r   )
__name__
__module____qualname____doc__r   namer   r   r   r    r   r   r   r      s\          IDH/ / /$ $ $    r   r   c                       e Zd ZdZdZd ZdS )DummySASLInitiatingInitializerac  
    Dummy SASL Initializer for initiating entities.

    This hardwires the SASL mechanism to L{DummySASLMechanism}, that is
    instantiated with the value of C{initialResponse}.

    @ivar initialResponse: The initial response to be returned by the
                           dummy SASL mechanism or L{None}.
    @type initialResponse: C{unicode}.
    Nc                 8    t          | j                  | _        d S r   )r   r   	mechanismr   s    r   setMechanismz+DummySASLInitiatingInitializer.setMechanism;   s    +D,@AAr   )r   r   r    r!   r   r(   r#   r   r   r%   r%   -   s9        	 	 OB B B B Br   r%   c                   T    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 Zd ZdS )SASLInitiatingInitializerTestsz5
    Tests for L{sasl.SASLInitiatingInitializer}
    c                 D   g | _         t          j                    | _        t          j        | j                  | _        | j         j        | j        _        | j                                         | j                            d           t          | j                  | _
        d S )Ns   <stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' from='example.com' id='12345' version='1.0'>)outputr	   Authenticatorauthenticator	XmlStreamappendsendconnectionMadedataReceivedr%   initr   s    r   setUpz$SASLInitiatingInitializerTests.setUpD   s    &466",T-?@@"k0%%'''##<	
 	
 	

 34>BB			r   c                 t    t          j        d          }|                    d           t          j                     j        _         j                            |                                 j        j        t          j
                    j        j                             fd            j        j        S )zL
        Test that the SASL error condition is correctly extracted.
        )r   failurenot-authorizedc                 :                         d| j                  S )Nr8   )assertEqual	condition)er   s    r   <lambda>z?SASLInitiatingInitializerTests.test_onFailure.<locals>.<lambda>\   s    d&&'7EE r   )r
   Element
addElementr   Deferredr4   	_deferred	onFailureassertFailurer   SASLAuthErroraddCallback)r   r7   s   ` r   test_onFailurez-SASLInitiatingInitializerTests.test_onFailureR   s     .!PQQ+,,,#n..		G$$$49.0BCCC	''EEEE	
 	
 	
 y""r   c                 ^   d| j         _        | j                                          | j        d         }|                     t
          |j                   |                     d|j                   |                     d|d                    |                     dt          |                     dS )zH
        Test starting authentication with an initial response.
        s   dummyr   authr   r'   zZHVtbXk=N)	r4   r   startr,   r:   NS_XMPP_SASLurir"   strr   rH   s     r   test_sendAuthInitialResponsez;SASLInitiatingInitializerTests.test_sendAuthInitialResponse`   s     %-	!	{1~tx000+++${"3444SYY/////r   c                     d| j         _        | j                                          | j        d         }|                     dt          |                     dS )zK
        Test starting authentication without an initial response.
        Nr    r4   r   rI   r,   r:   rL   rM   s     r   test_sendAuthNoInitialResponsez=SASLInitiatingInitializerTests.test_sendAuthNoInitialResponsel   sM     %)	!	{1~SYY'''''r   c                     d| j         _        | j                                          | j        d         }|                     dt          |                     dS )zS
        Test starting authentication where the initial response is empty.
        r   r   =NrQ   rM   s     r   !test_sendAuthEmptyInitialResponsez@SASLInitiatingInitializerTests.test_sendAuthEmptyInitialResponseu   sM     %(	!	{1~c$ii(((((r   c                 J   | j                                         }t          j        t          df          }|                    d           | j                             |           |                     d| j         j        j	                   | j         
                    d           |S )z5
        Test receiving a challenge message.
        r   bXkgY2hhbGxlbmdls   my challengeN)r4   rI   r
   r>   rJ   
addContentonChallenger:   r'   r   	onSuccessr   dr   s      r   test_onChallengez/SASLInitiatingInitializerTests.test_onChallenge~   s     IOONL+#>??	/000	i((($)*=*GHHH	D!!!r   c                    | j                                         }t          j        t          df          }|                    d           d| j         j        _        | j                             |           | j	        d         }| 
                    dt          |                     | j                             d           |S )zS
        A non-empty response gets encoded and included as character data.
        r   rW   s   response   zcmVzcG9uc2U=N)r4   rI   r
   r>   rJ   rX   r'   r   rY   r,   r:   rL   rZ   )r   r\   r   r   s       r   test_onChallengeResponsez7SASLInitiatingInitializerTests.test_onChallengeResponse   s     IOONL+#>??	/000'2	$	i(((;q>X777	D!!!r   c                     | j                                         }t          j        t          df          }| j                             |           |                     d| j         j        j                   | j         	                    d           |S )z<
        Test receiving an empty challenge message.
        r   r   N)
r4   rI   r
   r>   rJ   rY   r:   r'   r   rZ   r[   s      r   test_onChallengeEmptyz4SASLInitiatingInitializerTests.test_onChallengeEmpty   sx     IOONL+#>??		i(((di1;<<<	D!!!r   c                    | j                                         }t          j        t          df          }|                    d           | j                             |           |                     |t          j	                   |S )zJ
        Test receiving a challenge message with illegal padding.
        r   zbXkg=Y2hhbGxlbmdl
r4   rI   r
   r>   rJ   rX   rY   rC   r   SASLIncorrectEncodingErrorr[   s      r   test_onChallengeIllegalPaddingz=SASLInitiatingInitializerTests.test_onChallengeIllegalPadding   r     IOONL+#>??	0111	i(((1d=>>>r   c                    | j                                         }t          j        t          df          }|                    d           | j                             |           |                     |t          j	                   |S )zM
        Test receiving a challenge message with illegal characters.
        r   zbXkg*Y2hhbGxlbmdlrd   r[   s      r   !test_onChallengeIllegalCharactersz@SASLInitiatingInitializerTests.test_onChallengeIllegalCharacters   rg   r   c                    | j                                         }t          j        t          df          }|                    d           | j                             |           |                     |t          j	                   |S )z?
        Test receiving a malformed challenge message.
        r   ard   r[   s      r   test_onChallengeMalformedz8SASLInitiatingInitializerTests.test_onChallengeMalformed   sq     IOONL+#>??	S!!!	i(((1d=>>>r   N)r   r   r    r!   r5   rF   rN   rR   rU   r]   r`   rb   rf   ri   rl   r#   r   r   r*   r*   ?   s         C C C# # #
0 
0 
0( ( () ) )
 
 
  	 	 		 	 		 	 		 	 	 	 	r   r*   c                   <    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	S )
*SASLInitiatingInitializerSetMechanismTestszB
    Test for L{sasl.SASLInitiatingInitializer.setMechanism}.
    c                 N   g | _         t          j                    | _        t          j        | j                  | _        | j         j        | j        _        | j                                         | j                            d           t          j
        | j                  | _        d S )Nz<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' from='example.com' id='12345' version='1.0'>)r,   r	   r-   r.   r/   r0   r1   r2   r3   r   SASLInitiatingInitializerr4   r   s    r   r5   z0SASLInitiatingInitializerSetMechanismTests.setUp   s    &466",T-?@@"k0%%'''##;	
 	
 	
 24>BB			r   c                     t          j        t          df          }|                    d|           || j        j        |j        |j        f<   | j        	                                 | j        j
        j        S )zX
        Set up the XML Stream to have a SASL feature with the given mechanism.
        
mechanismsr'   )content)r
   r>   rJ   r?   r	   featuresrK   r"   r4   r(   r'   )r   r"   features      r   _setMechanismz8SASLInitiatingInitializerSetMechanismTests._setMechanism   sk     .,!=>>;555?Fgl ;<	   y"''r   c                     t          j        d          | j        _         d| j        _        d}|                     ||                     |                     dS )zI
        Test setting ANONYMOUS as the authentication mechanism.
        example.comN	ANONYMOUSr   JIDr.   passwordr:   rv   r   r"   s     r   test_anonymousz9SASLInitiatingInitializerSetMechanismTests.test_anonymous   sS     "%!7!7&*#t11$7788888r   c                     t          j        d          | j        _         d| j        _        d}|                     ||                     |                     dS )zE
        Test setting PLAIN as the authentication mechanism.
        test@example.comsecretPLAINNrz   r}   s     r   
test_plainz5SASLInitiatingInitializerSetMechanismTests.test_plain   sT     "%);!<!<&.#t11$7788888r   c                     t          j        d          | j        _         d| j        _        d}|                     ||                     |                     dS )zJ
        Test setting DIGEST-MD5 as the authentication mechanism.
        r   r   z
DIGEST-MD5Nrz   r}   s     r   test_digestz6SASLInitiatingInitializerSetMechanismTests.test_digest   sT     "%);!<!<&.#t11$7788888r   c                     t          j        d          | j        _         d| j        _        |                     t
          j        | j        d           dS )zK
        Test using an unacceptable SASL authentication mechanism.
        r   r   SOMETHING_UNACCEPTABLENr   r{   r.   r|   assertRaisesr   SASLNoAcceptableMechanismrv   r   s    r   test_notAcceptablez=SASLInitiatingInitializerSetMechanismTests.test_notAcceptable  sU    
 "%);!<!<&.#*D,>@X	
 	
 	
 	
 	
r   c                     t          j        d          | j        _         d| j        _        |                     t
          j        | j        d           dS )zW
        Test using an unacceptable SASL authentication mechanism with no JID.
        rx   r   r   Nr   r   s    r   test_notAcceptableWithoutUserzHSASLInitiatingInitializerSetMechanismTests.test_notAcceptableWithoutUser  sT     "%!7!7&.#*D,>@X	
 	
 	
 	
 	
r   N)r   r   r    r!   r5   rv   r~   r   r   r   r   r#   r   r   rn   rn      s         C C C	( 	( 	(9 9 99 9 99 9 9

 

 

	
 	
 	
 	
 	
r   rn   N)zope.interfacer   twisted.internetr   twisted.trialr   twisted.words.protocols.jabberr   r   r   r	   twisted.words.xishr
   rJ   ISASLMechanismr   rp   r%   TestCaser*   rn   r#   r   r   <module>r      sf  
 ' & & & & & " " " " " " " " " " " " P P P P P P P P P P P P % % % % % %1 _+,,       -,:B B B B BT%C B B B$C C C C CX%6 C C CLR
 R
 R
 R
 R
1B R
 R
 R
 R
 R
r   