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   