U
    
W[~  ã                   @   sr   d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	 G dd„ dej
ƒZG dd	„ d	ej
ƒZG d
d„ dej
ƒZdS )z>
Tests for L{twisted.words.protocols.jabber.sasl_mechanisms}.
é    )Úabsolute_importÚdivision)Úunittest)ÚnetworkString)Úsasl_mechanismsc                   @   s   e Zd ZdZdd„ ZdS )Ú
PlainTestszL
    Tests for L{twisted.words.protocols.jabber.sasl_mechanisms.Plain}.
    c                 C   s"   t  ddd¡}|  | ¡ d¡ dS )z,
        Test the initial response.
        NÚtestÚsecrets    test secret)r   ZPlainÚassertEqualÚgetInitialResponse©ÚselfÚm© r   úN/usr/lib/python3/dist-packages/twisted/words/test/test_jabbersaslmechanisms.pyÚtest_getInitialResponse   s    z"PlainTests.test_getInitialResponseN©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdd„ ZdS )ÚAnonymousTestszP
    Tests for L{twisted.words.protocols.jabber.sasl_mechanisms.Anonymous}.
    c                 C   s   t  ¡ }|  | ¡ d¡ dS )z8
        Test the initial response to be empty.
        N)r   Z	Anonymousr
   r   r   r   r   r   r       s    z&AnonymousTests.test_getInitialResponseNr   r   r   r   r   r      s   r   c                   @   sX   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S )ÚDigestMD5TestszP
    Tests for L{twisted.words.protocols.jabber.sasl_mechanisms.DigestMD5}.
    c                 C   s   t  ddd dd¡| _d S )NÚxmppzexample.orgr   r	   )r   Ú	DigestMD5Ú	mechanism©r   r   r   r   ÚsetUp-   s        ÿzDigestMD5Tests.setUpc                 C   s   |   | j ¡ d¡ dS )z=
        Test that no initial response is generated.
        N)ZassertIdenticalr   r   r   r   r   r   r   2   s    z&DigestMD5Tests.test_getInitialResponsec              
   C   sF   d}| j  | j  |¡¡}|d= |d= |  ddddgdd	d
dœ|¡ dS )zl
        The response to a Digest-MD5 challenge includes the parameters from the
        challenge.
        sJ   realm="localhost",nonce="1234",qop="auth",charset=utf-8,algorithm=md5-sessó   cnonceó   responseó   testó   1234ó   00000001ó   authó   utf-8s	   localhostó   xmpp/example.org©s   usernameó   nonces   ncó   qopó   charsetó   realms
   digest-uriN©r   Ú_parseÚgetResponser
   ©r   Ú	challengeZ
directivesr   r   r   Útest_getResponse9   s"    ÿ
ÿ    ýüzDigestMD5Tests.test_getResponsec              
   C   sF   d}| j  | j  |¡¡}|d= |d= |  ddddgdd	d
dœ|¡ dS )zs
        Bytes outside the ASCII range in the challenge are nevertheless
        included in the response.
        sS   realm="Ã©chec.example.org",nonce="1234",qop="auth",charset=utf-8,algorithm=md5-sessr   r   r    r!   r"   r#   r$   ó   Ã©chec.example.orgr%   r&   Nr+   r.   r   r   r   Útest_getResponseNonAsciiRealmK   s     
ÿ   üüz,DigestMD5Tests.test_getResponseNonAsciiRealmc                 C   s,   d}| j  | j  |¡¡}|  |d d¡ dS )zq
        The response to a challenge without a realm uses the host part of the
        JID as the realm.
        ó8   nonce="1234",qop="auth",charset=utf-8,algorithm=md5-sessr*   s   example.orgNr+   r.   r   r   r   Útest_getResponseNoRealm\   s
    
ÿz&DigestMD5Tests.test_getResponseNoRealmc                 C   s@   t  ddddd¡| _d}| j | j |¡¡}|  |d d¡ dS )	zÖ
        If the challenge does not include a realm and the host part of the JID
        includes bytes outside of the ASCII range, the response still includes
        the host part of the JID as the realm.
        r   u   Ã©chec.example.orgNr   r	   r3   r*   r1   )r   r   r   r,   r-   r
   r.   r   r   r   Útest_getResponseNoRealmIDNg   s        ÿ
ÿz)DigestMD5Tests.test_getResponseNoRealmIDNc                 C   s    d}| j  |¡}|  d|¡ dS )zW
        If the challenge just has a rspauth directive, the response is empty.
        s@   rspauth=cnNwYXV0aD1lYTQwZjYwMzM1YzQyN2I1NTI3Yjg0ZGJhYmNkZmZmZA==ó    N)r   r-   r
   )r   r/   Úresponser   r   r   Útest_getResponseRspauthu   s    ÿz&DigestMD5Tests.test_getResponseRspauthc              
   C   sx   d}d}t dd ƒ}d}d}d}d}d	 |¡}t d
|d||¡}	|	 |||| |¡| |¡| |¡|¡}
|  |
d¡ dS )zc
        The response to a Digest-MD5 challenge is computed according to RFC
        2831.
        zutf-8s   OA6MG9tEQGm2hhz%08x)é   s   OA6MHXh6VqTrRku   Ð˜chrisu   Ð˜secretu   Ð˜elwood.innosoft.comu   imap/Ð˜elwood.innosoft.coms   imapNs    7928f233258be88392424d094453c5e3)r   Úencoder   r   Z_calculateResponser
   )r   ÚcharsetZnonceZncZcnonceZusernameZpasswordZhostZ	digestURIr   r7   r   r   r   Útest_calculateResponse   s2    
    ÿ     þz%DigestMD5Tests.test_calculateResponsec                 C   s4   d}| j  |¡}|  dddddgddgd	œ|¡ d
S )zc
        A challenge can be parsed into a L{dict} with L{bytes} or L{list}
        values.
        sT   nonce="1234",qop="auth,auth-conf",charset=utf-8,algorithm=md5-sess,cipher="des,3des"s   md5-sessr!   r$   r#   s	   auth-confs   dess   3des)s	   algorithmr'   r)   r(   s   cipherN)r   r,   r
   r.   r   r   r   Ú
test_parse–   s    ÿ  ýüzDigestMD5Tests.test_parseN)r   r   r   r   r   r   r0   r2   r4   r5   r8   r<   r=   r   r   r   r   r   )   s   
r   N)r   Z
__future__r   r   Ztwisted.trialr   Ztwisted.python.compatr   Ztwisted.words.protocols.jabberr   ZTestCaser   r   r   r   r   r   r   Ú<module>   s   