a
    )¶(bÍ#  ã                   @   sž   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„ dƒƒZG d	d
„ d
ejƒZG dd„ dejƒZG dd„ dejƒZdS )é    )Úimplementer)Údefer)Úunittest)ÚjidÚsaslÚsasl_mechanismsÚ	xmlstream)Údomishú urn:ietf:params:xml:ns:xmpp-saslc                   @   s4   e Zd ZdZdZdZdZdd„ Zdd„ Zd	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                 C   s
   || _ d S ©N©ÚinitialResponse)Úselfr   © r   úAlib/python3.9/site-packages/twisted/words/test/test_jabbersasl.pyÚ__init__"   s    zDummySASLMechanism.__init__c                 C   s   | j S r   r   ©r   r   r   r   ÚgetInitialResponse%   s    z%DummySASLMechanism.getInitialResponsec                 C   s   || _ | jS r   )Ú	challengeÚresponse)r   r   r   r   r   ÚgetResponse(   s    zDummySASLMechanism.getResponse)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Únamer   r   r   r   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdZd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                 C   s   t | jƒ| _d S r   )r   r   Ú	mechanismr   r   r   r   ÚsetMechanism;   s    z+DummySASLInitiatingInitializer.setMechanism)r   r   r   r   r   r!   r   r   r   r   r   -   s   r   c                   @   sh   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )ÚSASLInitiatingInitializerTestsz5
    Tests for L{sasl.SASLInitiatingInitializer}
    c                 C   sP   g | _ t ¡ | _t | 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   r   r   r   ÚsetUpD   s    

ÿz$SASLInitiatingInitializerTests.setUpc                    s\   t  d¡}| d¡ t ¡ ˆ j_ˆ j |¡ ˆ  ˆ jjt	j
¡ ˆ jj ‡ fdd„¡ ˆ jjS )zL
        Test that the SASL error condition is correctly extracted.
        )r
   Úfailureúnot-authorizedc                    s   ˆ   d| j¡S )Nr.   )ÚassertEqualZ	condition)Úer   r   r   Ú<lambda>\   r   z?SASLInitiatingInitializerTests.test_onFailure.<locals>.<lambda>)r	   ÚElementÚ
addElementr   ZDeferredr+   Z	_deferredZ	onFailureÚassertFailurer   ZSASLAuthErrorZaddCallback)r   r-   r   r   r   Útest_onFailureR   s    


ÿz-SASLInitiatingInitializerTests.test_onFailurec                 C   s\   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)	r+   r   Ústartr#   r/   ÚNS_XMPP_SASLÚurir   Ústr©r   r6   r   r   r   Útest_sendAuthInitialResponse`   s    

z;SASLInitiatingInitializerTests.test_sendAuthInitialResponsec                 C   s0   d| j _| j  ¡  | jd }|  dt|ƒ¡ dS )zK
        Test starting authentication without an initial response.
        Nr   Ú ©r+   r   r7   r#   r/   r:   r;   r   r   r   Útest_sendAuthNoInitialResponsel   s    

z=SASLInitiatingInitializerTests.test_sendAuthNoInitialResponsec                 C   s0   d| j _| j  ¡  | jd }|  dt|ƒ¡ dS )zS
        Test starting authentication where the initial response is empty.
        r   r   ú=Nr>   r;   r   r   r   Ú!test_sendAuthEmptyInitialResponseu   s    

z@SASLInitiatingInitializerTests.test_sendAuthEmptyInitialResponsec                 C   sP   | j  ¡ }t tdf¡}| d¡ | j  |¡ |  d| j jj	¡ | j  
d¡ |S )z5
        Test receiving a challenge message.
        r   ÚbXkgY2hhbGxlbmdls   my challengeN)r+   r7   r	   r2   r8   Ú
addContentÚonChallenger/   r    r   Ú	onSuccess©r   Údr   r   r   r   Útest_onChallenge~   s    

z/SASLInitiatingInitializerTests.test_onChallengec                 C   sb   | j  ¡ }t 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   rB   s   responseé   zcmVzcG9uc2U=N)r+   r7   r	   r2   r8   rC   r    r   rD   r#   r/   r:   rE   )r   rG   r   r   r   r   r   Útest_onChallengeResponseŠ   s    



z7SASLInitiatingInitializerTests.test_onChallengeResponsec                 C   sF   | j  ¡ }t tdf¡}| j  |¡ |  d| j jj¡ | j  	d¡ |S )z<
        Test receiving an empty challenge message.
        r   r   N)
r+   r7   r	   r2   r8   rD   r/   r    r   rE   rF   r   r   r   Útest_onChallengeEmpty˜   s    
z4SASLInitiatingInitializerTests.test_onChallengeEmptyc                 C   s@   | j  ¡ }t tdf¡}| d¡ | j  |¡ |  |tj	¡ |S )zJ
        Test receiving a challenge message with illegal padding.
        r   zbXkg=Y2hhbGxlbmdl©
r+   r7   r	   r2   r8   rC   rD   r4   r   ZSASLIncorrectEncodingErrorrF   r   r   r   Útest_onChallengeIllegalPadding£   s    

z=SASLInitiatingInitializerTests.test_onChallengeIllegalPaddingc                 C   s@   | j  ¡ }t tdf¡}| d¡ | j  |¡ |  |tj	¡ |S )zM
        Test receiving a challenge message with illegal characters.
        r   zbXkg*Y2hhbGxlbmdlrL   rF   r   r   r   Ú!test_onChallengeIllegalCharacters®   s    

z@SASLInitiatingInitializerTests.test_onChallengeIllegalCharactersc                 C   s@   | j  ¡ }t tdf¡}| d¡ | j  |¡ |  |tj	¡ |S )z?
        Test receiving a malformed challenge message.
        r   ÚarL   rF   r   r   r   Útest_onChallengeMalformed¹   s    

z8SASLInitiatingInitializerTests.test_onChallengeMalformedN)r   r   r   r   r,   r5   r<   r?   rA   rH   rJ   rK   rM   rN   rP   r   r   r   r   r"   ?   s   		r"   c                   @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )Ú*SASLInitiatingInitializerSetMechanismTestszB
    Test for L{sasl.SASLInitiatingInitializer.setMechanism}.
    c                 C   sR   g | _ t ¡ | _t | j¡| _| j j| j_| j ¡  | j d¡ t	 
| 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&   r'   r(   r)   r*   r   ÚSASLInitiatingInitializerr+   r   r   r   r   r,   Ê   s    

ÿz0SASLInitiatingInitializerSetMechanismTests.setUpc                 C   sD   t  tdf¡}|jd|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.
        Z
mechanismsr    )Zcontent)r	   r2   r8   r3   r   Zfeaturesr9   r   r+   r!   r    )r   r   Zfeaturer   r   r   Ú_setMechanismÙ   s
    
z8SASLInitiatingInitializerSetMechanismTests._setMechanismc                 C   s0   t  d¡| j_ d| j_d}|  ||  |¡¡ dS )zI
        Test setting ANONYMOUS as the authentication mechanism.
        úexample.comNZ	ANONYMOUS©r   ÚJIDr%   Úpasswordr/   rS   ©r   r   r   r   r   Útest_anonymousä   s    z9SASLInitiatingInitializerSetMechanismTests.test_anonymousc                 C   s0   t  d¡| j_ d| j_d}|  ||  |¡¡ dS )zE
        Test setting PLAIN as the authentication mechanism.
        útest@example.comÚsecretZPLAINNrU   rX   r   r   r   Ú
test_plainî   s    z5SASLInitiatingInitializerSetMechanismTests.test_plainc                 C   s0   t  d¡| j_ d| j_d}|  ||  |¡¡ dS )zJ
        Test setting DIGEST-MD5 as the authentication mechanism.
        rZ   r[   z
DIGEST-MD5NrU   rX   r   r   r   Útest_digestø   s    z6SASLInitiatingInitializerSetMechanismTests.test_digestc                 C   s,   t  d¡| j_ d| j_|  tj| jd¡ dS )zK
        Test using an unacceptable SASL authentication mechanism.
        rZ   r[   ÚSOMETHING_UNACCEPTABLEN©r   rV   r%   rW   ZassertRaisesr   ZSASLNoAcceptableMechanismrS   r   r   r   r   Útest_notAcceptable  s
    
ÿz=SASLInitiatingInitializerSetMechanismTests.test_notAcceptablec                 C   s,   t  d¡| j_ d| j_|  tj| jd¡ dS )zW
        Test using an unacceptable SASL authentication mechanism with no JID.
        rT   r[   r^   Nr_   r   r   r   r   Útest_notAcceptableWithoutUser  s
    
ÿzHSASLInitiatingInitializerSetMechanismTests.test_notAcceptableWithoutUserN)r   r   r   r   r,   rS   rY   r\   r]   r`   ra   r   r   r   r   rQ   Å   s   


rQ   N)Zzope.interfacer   Ztwisted.internetr   Ztwisted.trialr   Ztwisted.words.protocols.jabberr   r   r   r   Ztwisted.words.xishr	   r8   ZISASLMechanismr   rR   r   ZTestCaser"   rQ   r   r   r   r   Ú<module>   s    