U
    
W[Ø$  ã                   @   s¼   d dl mZm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ZeejƒG d	d
„ d
eƒƒZG dd„ dejƒZG dd„ de
jƒZG dd„ de
jƒZdS )é    )Úabsolute_importÚdivision)Úimplementer)Údefer)Úunicode)Úunittest)ÚsaslÚsasl_mechanismsÚ	xmlstreamÚjid)Ú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   úD/usr/lib/python3/dist-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   ÚsetUpF   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 )Nr1   )ÚassertEqualZ	condition)Úer   r   r   Ú<lambda>]   s    ÿz?SASLInitiatingInitializerTests.test_onFailure.<locals>.<lambda>)r   ÚElementÚ
addElementr   ZDeferredr.   Z	_deferredZ	onFailureÚassertFailurer   ZSASLAuthErrorZaddCallback)r   r0   r   r   r   Útest_onFailureS   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&   r2   ÚNS_XMPP_SASLÚurir!   r   ©r   r9   r   r   r   Útest_sendAuthInitialResponsec   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   r:   r&   r2   Ústrr=   r   r   r   Útest_sendAuthNoInitialResponsep   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   ú=N)r.   r   r:   r&   r2   r   r=   r   r   r   Ú!test_sendAuthEmptyInitialResponsez   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.   r:   r   r5   r;   Ú
addContentÚonChallenger2   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.   r:   r   r5   r;   rE   r#   r   rF   r&   r2   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.   r:   r   r5   r;   rF   r2   r#   r   rG   rH   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.   r:   r   r5   r;   rE   rF   r7   r   ZSASLIncorrectEncodingErrorrH   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*Y2hhbGxlbmdlrN   rH   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   ÚarN   rH   r   r   r   Útest_onChallengeMalformedÄ   s    

z8SASLInitiatingInitializerTests.test_onChallengeMalformedN)r   r   r   r    r/   r8   r>   rA   rC   rJ   rL   rM   rO   rP   rR   r   r   r   r   r%   A   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   r5   r;   r6   r
   Zfeaturesr<   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(   Úpasswordr2   rU   ©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PLAINNrW   rZ   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.
        r\   r]   z
DIGEST-MD5NrW   rZ   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.
        r\   r]   ÚSOMETHING_UNACCEPTABLEN©r   rX   r(   rY   ZassertRaisesr   ZSASLNoAcceptableMechanismrU   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.
        rV   r]   r`   Nra   r   r   r   r   Útest_notAcceptableWithoutUser  s     ÿzHSASLInitiatingInitializerSetMechanismTests.test_notAcceptableWithoutUserN)r   r   r   r    r/   rU   r[   r^   r_   rb   rc   r   r   r   r   rS   Ð   s   rS   N)Z
__future__r   r   Zzope.interfacer   Ztwisted.internetr   Ztwisted.python.compatr   Ztwisted.trialr   Ztwisted.words.protocols.jabberr   r	   r
   r   Ztwisted.words.xishr   r;   ZISASLMechanismÚobjectr   rT   r"   ZTestCaser%   rS   r   r   r   r   Ú<module>   s    