o
    Óó·iÍ#  ã                   @   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   úhC:\Users\psant\Downloads\PAF backend\school\venv\Lib\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.   )ÚassertEqualÚ	condition)Úer   r   r   Ú<lambda>\   s    z?SASLInitiatingInitializerTests.test_onFailure.<locals>.<lambda>N)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   r7   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   r8   r#   r/   r;   r<   r   r   r   Útest_sendAuthNoInitialResponsel   ó   

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   rA   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+   r8   r	   r3   r9   Ú
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   rD   s   responseé   zcmVzcG9uc2U=N)r+   r8   r	   r3   r9   rE   r    r   rF   r#   r/   r;   rG   )r   rI   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+   r8   r	   r3   r9   rF   r/   r    r   rG   rH   r   r   r   Útest_onChallengeEmpty˜   s   
z4SASLInitiatingInitializerTests.test_onChallengeEmptyc                 C   ó@   | j  ¡ }t tdf¡}| d¡ | j  |¡ |  |tj	¡ |S )zJ
        Test receiving a challenge message with illegal padding.
        r   zbXkg=Y2hhbGxlbmdlN©
r+   r8   r	   r3   r9   rE   rF   r5   r   ZSASLIncorrectEncodingErrorrH   r   r   r   Útest_onChallengeIllegalPadding£   ó   

z=SASLInitiatingInitializerTests.test_onChallengeIllegalPaddingc                 C   rN   )zM
        Test receiving a challenge message with illegal characters.
        r   zbXkg*Y2hhbGxlbmdlNrO   rH   r   r   r   Ú!test_onChallengeIllegalCharacters®   rQ   z@SASLInitiatingInitializerTests.test_onChallengeIllegalCharactersc                 C   rN   )z?
        Test receiving a malformed challenge message.
        r   ÚaNrO   rH   r   r   r   Útest_onChallengeMalformed¹   rQ   z8SASLInitiatingInitializerTests.test_onChallengeMalformedN)r   r   r   r   r,   r6   r=   r@   rC   rJ   rL   rM   rP   rR   rT   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    )ÚcontentN)r	   r3   r9   r4   r   Úfeaturesr:   r   r+   r!   r    )r   r   Ú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/   rZ   ©r   r   r   r   r   Útest_anonymousä   ó   z9SASLInitiatingInitializerSetMechanismTests.test_anonymousc                 C   ó0   t  d¡| j_ d| j_d}|  ||  |¡¡ dS )zE
        Test setting PLAIN as the authentication mechanism.
        útest@example.comÚsecretZPLAINNr\   r_   r   r   r   Ú
test_plainî   ra   z5SASLInitiatingInitializerSetMechanismTests.test_plainc                 C   rb   )zJ
        Test setting DIGEST-MD5 as the authentication mechanism.
        rc   rd   z
DIGEST-MD5Nr\   r_   r   r   r   Útest_digestø   ra   z6SASLInitiatingInitializerSetMechanismTests.test_digestc                 C   ó,   t  d¡| j_ d| j_|  tj| jd¡ dS )zK
        Test using an unacceptable SASL authentication mechanism.
        rc   rd   ÚSOMETHING_UNACCEPTABLEN©r   r]   r%   r^   ÚassertRaisesr   ZSASLNoAcceptableMechanismrZ   r   r   r   r   Útest_notAcceptable  s
   
ÿz=SASLInitiatingInitializerSetMechanismTests.test_notAcceptablec                 C   rg   )zW
        Test using an unacceptable SASL authentication mechanism with no JID.
        r[   rd   rh   Nri   r   r   r   r   Útest_notAcceptableWithoutUser  s
   
ÿzHSASLInitiatingInitializerSetMechanismTests.test_notAcceptableWithoutUserN)r   r   r   r   r,   rZ   r`   re   rf   rk   rl   r   r   r   r   rU   Å   s    


rU   N)Zzope.interfacer   Ztwisted.internetr   Ztwisted.trialr   Ztwisted.words.protocols.jabberr   r   r   r   Ztwisted.words.xishr	   r9   ZISASLMechanismr   rV   r   ZTestCaser"   rU   r   r   r   r   Ú<module>   s    