o
    ¯b–  ã                   @   sT   d Z ddlZddlmZ ddlmZ ddlmZmZ ddl	m
Z
 G dd„ de
ƒZdS )	z9
Tests for L{twisted.cred}'s implementation of CRAM-MD5.
é    N)Úhexlify)ÚHMAC)ÚCramMD5CredentialsÚIUsernameHashedPassword)ÚTestCasec                   @   s@   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S )ÚCramMD5CredentialsTestsz*
    Tests for L{CramMD5Credentials}.
    c                 C   s"   t ƒ }| ¡ }|  || ¡ ¡ dS )z‡
        The same L{CramMD5Credentials} will always provide the same challenge,
        no matter how many times it is called.
        N)r   ÚgetChallengeÚassertEqual©ÚselfÚcÚchal© r   úA/usr/lib/python3/dist-packages/twisted/cred/test/test_cramauth.pyÚtest_idempotentChallenge   s   z0CramMD5CredentialsTests.test_idempotentChallengec                 C   s<   t ƒ }| ¡ }ttd|tjd� ¡ ƒ|_|  | 	d¡¡ dS )a2  
        When a valid response (which is a hex digest of the challenge that has
        been encrypted by the user's shared secret) is set on the
        L{CramMD5Credentials} that created the challenge, and C{checkPassword}
        is called with the user's shared secret, it will return L{True}.
        ó   secret©Ú	digestmodN)
r   r   r   r   ÚhashlibÚmd5ÚdigestÚresponseÚ
assertTrueÚcheckPasswordr
   r   r   r   Útest_checkPassword   s   z*CramMD5CredentialsTests.test_checkPasswordc                 C   s   t ƒ }|  | d¡¡ dS )zg
        When there is no response set, calling C{checkPassword} will return
        L{False}.
        r   N)r   ÚassertFalser   )r   r   r   r   r   Útest_noResponse+   s   z'CramMD5CredentialsTests.test_noResponsec                 C   s<   t ƒ }| ¡ }ttd|tjd� ¡ ƒ|_|  | 	d¡¡ dS )a   
        When an invalid response is set on the L{CramMD5Credentials} (one that
        is not the hex digest of the challenge, encrypted with the user's shared
        secret) and C{checkPassword} is called with the user's correct shared
        secret, it will return L{False}.
        s   thewrongsecretr   r   N)
r   r   r   r   r   r   r   r   r   r   r
   r   r   r   Útest_wrongPassword3   s   ÿz*CramMD5CredentialsTests.test_wrongPasswordc                 C   sX   t ƒ }| ¡ }| d dttd|tjd� ¡ ƒf¡¡ |  	| 
d¡¡ |  |jd¡ dS )zÂ
        When C{setResponse} is called with a string that is the username and
        the hashed challenge separated with a space, they will be set on the
        L{CramMD5Credentials}.
        ó    s   squirrels   supersecretr   N)r   r   ÚsetResponseÚjoinr   r   r   r   r   r   r   r	   Úusernamer
   r   r   r   Útest_setResponseA   s   þÿÿz(CramMD5CredentialsTests.test_setResponsec                 C   s   |   t t¡¡ dS )zd
        L{CramMD5Credentials} implements the L{IUsernameHashedPassword}
        interface.
        N)r   r   ÚimplementedByr   )r   r   r   r   Útest_interfaceT   s   z&CramMD5CredentialsTests.test_interfaceN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r"   r$   r   r   r   r   r      s    	r   )r(   r   Úbinasciir   Úhmacr   Útwisted.cred.credentialsr   r   Útwisted.trial.unittestr   r   r   r   r   r   Ú<module>   s   