B
    u9a  ã               @   sL   d dl Z d dlmZ d dlZe d¡ZG dd„ dejƒZedkrHe 	¡  dS )é    N)ÚsupportÚcryptc               @   sx   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Ze e	j
e	jkpBe	je	jkd	¡d
d„ ƒZe e	je	jkd¡dd„ ƒZdd„ ZdS )ÚCryptTestCasec             C   sR   t   d¡}t   d|¡}|  ||¡ t   dd¡}|d k	rNt   d|¡}|  ||¡ d S )NÚ
mypasswordZab)r   ÚassertEqual)ÚselfÚcrÚcr2© r
   ú /usr/lib/python3.7/test_crypt.pyÚ
test_crypt	   s    
zCryptTestCase.test_cryptc          
   C   sx   |   ttjƒd¡ x`tjD ]V}t |¡}|  t|ƒ|j ddddddh¡ |jr|  |j|d t|ƒ|j … ¡ qW d S )Né@   r   é   é   é   é   é   )	r   Úlenr   Z
_saltcharsÚmethodsÚmksaltÚassertInÚ
salt_charsZident)r   ÚmethodÚsaltr
   r
   r   Ú	test_salt   s    
"zCryptTestCase.test_saltc             C   sn   xht jD ]^}t   d|¡}|  t|ƒ|j¡ t   d|¡}|  ||¡ t   dt  |¡¡}|  t|ƒ|j¡ qW d S )NZassword)r   r   r   r   Z
total_sizer   )r   r   r   r	   r
   r
   r   Útest_saltedcrypt   s    zCryptTestCase.test_saltedcryptc             C   sL   |   ttjƒdk¡ tj d¡r4|  tjtjg¡ n|  tjd tj	¡ d S )Nr   Zopenbsdéÿÿÿÿ)
Ú
assertTruer   r   r   ÚsysÚplatformÚ
startswithr   ÚMETHOD_BLOWFISHÚMETHOD_CRYPT)r   r
   r
   r   Útest_methods#   s    zCryptTestCase.test_methodszrequires support of SHA-2c          	   C   s–   xt jt jfD ]€}xzdD ]r}t j||d}|  d| |¡ |  t|ƒ|j dtt|ƒƒ ¡ t   d|¡}|  	|¡ t   d|¡}|  ||¡ qW qW d S )N)iè  i'  i † )Úroundsz$rounds=%d$é   r   )
r   ÚMETHOD_SHA256ÚMETHOD_SHA512r   r   r   r   r   Ústrr   )r   r   r$   r   r   r	   r
   r
   r   Útest_sha2_rounds*   s    

zCryptTestCase.test_sha2_roundszrequires support of Blowfishc             C   s†   x€t ddƒD ]r}tjtjd|> d}|  d| |¡ |  t|ƒtjj ddh¡ t d|¡}|  |¡ t d|¡}|  ||¡ qW d S )	Nr   r%   r   )r$   z$%02d$r   r   r   )	Úranger   r   r!   r   r   r   r   r   )r   Z
log_roundsr   r   r	   r
   r
   r   Útest_blowfish_rounds9   s    
z"CryptTestCase.test_blowfish_roundsc             C   s  xžt jt jt jfD ]Š}|  t¡ t j|dd W d Q R X |  t¡ t j|dd W d Q R X x<ddddd> fD ](}|  t¡ t j||d W d Q R X qpW qW |  t¡ t jt jdd W d Q R X x:t jt j	fD ]*}|  
td	¡ t j|d
d W d Q R X qÔW d S )NZ4096)r$   g      °@r   r   r   iç  iè  r   i   )r   r&   r'   r!   ZassertRaisesÚ	TypeErrorr   Ú
ValueErrorr"   Z
METHOD_MD5ZassertRaisesRegex)r   r   r$   r
   r
   r   Útest_invalid_roundsE   s    
 z!CryptTestCase.test_invalid_roundsN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r#   ÚunittestZ
skipUnlessr   r&   r   r'   r)   r!   r+   r.   r
   r
   r
   r   r      s   		
r   Ú__main__)
r   Ztestr   r2   Úimport_moduler   ZTestCaser   r/   Úmainr
   r
   r
   r   Ú<module>   s   
O