B
    u9a  ã               @   sf   d Z ddlZddlZddlZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZed	krbe 	¡  dS )
zTest the secrets module.

As most of the functions in secrets are thin wrappers around functions
defined elsewhere, we don't need to test them exhaustively.
é    Nc               @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚCompare_Digest_Testsz%Test secrets.compare_digest function.c          	   C   sR   xLdD ]D}|d }|d }|   t ||¡¡ |   t | d¡| d¡¡¡ qW d S )N)ÚaZbcdZxyz123éd   zutf-8)Ú
assertTrueÚsecretsÚcompare_digestÚencode)ÚselfÚsr   Úb© r   ú"/usr/lib/python3.7/test_secrets.pyÚ
test_equal   s
    
zCompare_Digest_Tests.test_equalc          	   C   s~   |   t dd¡¡ |   t dd¡¡ xTdD ]L}|d d }|d d }|   t ||¡¡ |   t | d	¡| d	¡¡¡ q*W d S )
NÚabcZabcds   abcs   abcd)ÚxZmnZa1b2c3r   ÚqÚkzutf-8)ZassertFalser   r   r   )r	   r
   r   r   r   r   r   Útest_unequal   s    
z!Compare_Digest_Tests.test_unequalc             C   sR   d}|  d¡}t|tƒst‚t|tƒs*t‚|  ttj||¡ |  ttj||¡ d S )NZabcdezutf-8)	r   Ú
isinstanceÚstrÚAssertionErrorÚbytesÚassertRaisesÚ	TypeErrorr   r   )r	   r   r   r   r   r   Útest_bad_types$   s    
z#Compare_Digest_Tests.test_bad_typesc             C   s,   |   t dd¡t¡ |   t dd¡t¡ d S )Nr   Zxyz)ÚassertIsInstancer   r   Úbool)r	   r   r   r   Ú	test_bool-   s    zCompare_Digest_Tests.test_boolN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r      s
   
	r   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚRandom_Testsz*Test wrappers around SystemRandom methods.c             C   s^   d}xTdD ]L}xFt dƒD ]:}t |¡}|  d|  ko@d| k n  |||f ¡ qW q
W d S )Nzrandbits(%d) returned %d)é   é   é   é   r   é   )Úranger   Zrandbitsr   )r	   ÚerrmsgZnumbitsÚiÚnr   r   r   Útest_randbits6   s
    

zRandom_Tests.test_randbitsc             C   s<   dddddddg}x$t dƒD ]}|  t |¡|k¡ qW d S )	Né   r'   é   é   é   é    é@   é
   )r(   r   r   Úchoice)r	   Úitemsr*   r   r   r   Útest_choice>   s    zRandom_Tests.test_choicec             C   sN   x(t ddƒD ]}|  t |¡t |ƒ¡ qW |  ttjd¡ |  ttjd¡ d S )Nr'   r3   r   éÿÿÿÿ)r(   ZassertInr   Z	randbelowr   Ú
ValueError)r	   r*   r   r   r   Útest_randbelowD   s    zRandom_Tests.test_randbelowN)r   r   r    r!   r,   r6   r9   r   r   r   r   r"   3   s   r"   c               @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚToken_TestszTest token functions.c          
   C   sÐ   x”t jt jt jfD ]€}| j|dj |j}y
|ƒ  W n" tk
rV   |  d| ¡ Y nX y|d ƒ W n" tk
r†   |  d| ¡ Y nX W d Q R X qW t j}|  	t
t  d ¡ƒ|¡ |  	t
t  d ¡ƒd| ¡ d S )N)Úfuncz$%s cannot be called with no argumentz%s cannot be called with Noner'   )r   Útoken_bytesÚ	token_hexÚtoken_urlsafeÚsubTestr   r   ZfailZDEFAULT_ENTROPYÚassertEqualÚlen)r	   r;   ÚnameÚsizer   r   r   Útest_token_defaultsO   s    

"zToken_Tests.test_token_defaultsc          
   C   sR   xLdD ]D}| j |d. |  t |¡t¡ |  tt |¡ƒ|¡ W d Q R X qW d S )N)r-   r/   é   r   )r+   )r?   r   r   r<   r   r@   rA   )r	   r+   r   r   r   Útest_token_bytesa   s    
zToken_Tests.test_token_bytesc          
   C   sl   xfdD ]^}| j |dH t |¡}|  |t¡ |  t|ƒd| ¡ |  tdd„ |D ƒƒ¡ W d Q R X qW d S )N)r-   r$   é   éZ   )r+   r'   c             s   s   | ]}|t jkV  qd S )N)ÚstringZ	hexdigits)Ú.0Úcr   r   r   ú	<genexpr>o   s    z-Token_Tests.test_token_hex.<locals>.<genexpr>)	r?   r   r=   r   r   r@   rA   r   Úall)r	   r+   r
   r   r   r   Útest_token_hexh   s    

zToken_Tests.test_token_hexc          
      sl   t jt j d ‰ xVdD ]N}| j|d8 t |¡}|  |t¡ |  t	‡ fdd„|D ƒƒ¡ W d Q R X qW d S )Nz-_)r-   é   é   éL   )r+   c             3   s   | ]}|ˆ kV  qd S )Nr   )rJ   rK   )Úlegalr   r   rL   x   s    z1Token_Tests.test_token_urlsafe.<locals>.<genexpr>)
rI   Zascii_lettersÚdigitsr?   r   r>   r   r   r   rM   )r	   r+   r
   r   )rR   r   Útest_token_urlsafeq   s    

zToken_Tests.test_token_urlsafeN)r   r   r    r!   rD   rF   rN   rT   r   r   r   r   r:   L   s
   	r:   Ú__main__)
r!   r   ZunittestrI   ZTestCaser   r"   r:   r   Úmainr   r   r   r   Ú<module>   s   $/