U
    
W[>  ć                   @   sd   d Z 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 G dd	 d	e	Zd
S )z9
Tests for L{twisted.cred}'s implementation of CRAM-MD5.
é    )ŚdivisionŚabsolute_import)ŚHMAC)Śhexlify)ŚTestCase)ŚCramMD5Credentials)ŚIUsernameHashedPasswordc                   @   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ŚcZchal© r   śA/usr/lib/python3/dist-packages/twisted/cred/test/test_cramauth.pyŚtest_idempotentChallenge   s    z0CramMD5CredentialsTests.test_idempotentChallengec                 C   s6   t  }| ” }tt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}.
        ó   secretN)r   r
   r   r   Ś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   s6   t  }| ” }tt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   N)r   r
   r   r   r   r   r   r   r   r   r   r   Śtest_wrongPassword7   s    z*CramMD5CredentialsTests.test_wrongPasswordc              	   C   sR   t  }| ” }| d dtt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   supersecretN)r   r
   ZsetResponseŚjoinr   r   r   r   r   r   Zusernamer   r   r   r   Śtest_setResponseD   s    ’’z(CramMD5CredentialsTests.test_setResponsec                 C   s   |   t t”” dS )zd
        L{CramMD5Credentials} implements the L{IUsernameHashedPassword}
        interface.
        N)r   r   ZimplementedByr   )r   r   r   r   Śtest_interfaceS   s    ’z&CramMD5CredentialsTests.test_interfaceN)
Ś__name__Ś
__module__Ś__qualname__Ś__doc__r   r   r   r   r   r   r   r   r   r   r	      s   
	r	   N)r"   Z
__future__r   r   Zhmacr   Zbinasciir   Ztwisted.trial.unittestr   Ztwisted.cred.credentialsr   r   r	   r   r   r   r   Ś<module>   s   