o
    ¼>hÍ#  ã                   @   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   ú‚/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/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>)r	   ÚElementÚ
addElementr   ÚDeferredr+   Ú	_deferredÚ	onFailureÚassertFailurer   ÚSASLAuthErrorÚ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   r<   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   r=   r#   r/   r@   rA   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   ú=NrD   rA   r   r   r   Ú!test_sendAuthEmptyInitialResponseu   rF   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	   r3   r>   Ú
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   rI   s   responseé   zcmVzcG9uc2U=N)r+   r=   r	   r3   r>   rJ   r    r   rK   r#   r/   r@   rL   )r   rN   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	   r3   r>   rK   r/   r    r   rL   rM   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=Y2hhbGxlbmdl©
r+   r=   r	   r3   r>   rJ   rK   r8   r   ÚSASLIncorrectEncodingErrorrM   r   r   r   Útest_onChallengeIllegalPadding£   ó   

z=SASLInitiatingInitializerTests.test_onChallengeIllegalPaddingc                 C   rS   )zM
        Test receiving a challenge message with illegal characters.
        r   zbXkg*Y2hhbGxlbmdlrT   rM   r   r   r   Ú!test_onChallengeIllegalCharacters®   rW   z@SASLInitiatingInitializerTests.test_onChallengeIllegalCharactersc                 C   rS   )z?
        Test receiving a malformed challenge message.
        r   ÚarT   rM   r   r   r   Útest_onChallengeMalformed¹   rW   z8SASLInitiatingInitializerTests.test_onChallengeMalformedN)r   r   r   r   r,   r;   rB   rE   rH   rO   rQ   rR   rV   rX   rZ   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.
        Ú
mechanismsr    )Úcontent)r	   r3   r>   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.comNÚ	ANONYMOUS©r   ÚJIDr%   Úpasswordr/   ra   ©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ÚsecretÚPLAINNrd   rg   r   r   r   Ú
test_plainî   ri   z5SASLInitiatingInitializerSetMechanismTests.test_plainc                 C   rj   )zJ
        Test setting DIGEST-MD5 as the authentication mechanism.
        rk   rl   z
DIGEST-MD5Nrd   rg   r   r   r   Útest_digestø   ri   z6SASLInitiatingInitializerSetMechanismTests.test_digestc                 C   ó,   t  d¡| j_ d| j_|  tj| jd¡ dS )zK
        Test using an unacceptable SASL authentication mechanism.
        rk   rl   ÚSOMETHING_UNACCEPTABLEN©r   re   r%   rf   ÚassertRaisesr   ÚSASLNoAcceptableMechanismra   r   r   r   r   Útest_notAcceptable  s
   
ÿz=SASLInitiatingInitializerSetMechanismTests.test_notAcceptablec                 C   rp   )zW
        Test using an unacceptable SASL authentication mechanism with no JID.
        rb   rl   rq   Nrr   r   r   r   r   Útest_notAcceptableWithoutUser  s
   
ÿzHSASLInitiatingInitializerSetMechanismTests.test_notAcceptableWithoutUserN)r   r   r   r   r,   ra   rh   rn   ro   ru   rv   r   r   r   r   r[   Å   s    


r[   N)Úzope.interfacer   Útwisted.internetr   Útwisted.trialr   Útwisted.words.protocols.jabberr   r   r   r   Útwisted.words.xishr	   r>   ÚISASLMechanismr   r\   r   ÚTestCaser"   r[   r   r   r   r   Ú<module>   s    