o
    Ԏjbr                     @   s  d Z ddlmZ ddlmZ ddlZddlZzddlmZ W n e	y)   dZY nw ddl
mZ ddlmZmZmZ ddlmZmZ ddlm  mZ dd	lmZmZ dd
lmZmZmZ ee Z!G dd deZ"G dd deZ#G dd dej$Z%G dd deZ&dS )a  tests for passlib.context

this file is a clone of the 1.5 test_context.py,
containing the tests using the legacy CryptPolicy api.
it's being preserved here to ensure the old api doesn't break
(until Passlib 1.8, when this and the legacy api will be removed).
    )with_statement)	getLoggerN)resource_filename)hash)CryptContextCryptPolicyLazyCryptContext)to_bytes
to_unicode)TestCaseset_file)register_crypt_handler_path_has_crypt_handler_unload_handler_namec                
   @   s
  e Zd ZdZdZdZejej	ej
edZejes&er&eddZeds-J eg ddd	d
ddddZddd	d
ddddZeejejejejgdd	d
ddddZdZedddddZeg ddd	ddddddZeddZeg ddd	ddddddZdZedgd	ddddZed Z e! Z"e"j#dgdd gd! e! Z$e$#dd"d# e! Z%e%#dgdd gd! d$d% Z&d&d' Z'd(d) Z(d*d+ Z)d,d- Z*d.d/ Z+d0d1 Z,d2d3 Z-d4d5 Z.d6d7 Z/d8d9 Z0d:d; Z1d<d= Z2d>d? Z3d@dA Z4dBdC Z5dDdE Z6dFdG Z7dHS )ICryptPolicyTestztest CryptPolicy objectr   z[passlib]
schemes = des_crypt, md5_crypt, bsdi_crypt, sha512_crypt
default = md5_crypt
all.vary_rounds = 10%%
bsdi_crypt.max_rounds = 30000
bsdi_crypt.default_rounds = 25000
sha512_crypt.max_rounds = 50000
sha512_crypt.min_rounds = 40000
zsample_config_1s.cfgzpasslib.testsz[passlib]
schemes	des_crypt	md5_crypt
bsdi_cryptsha512_cryptr   皙?0u  ia  P  @  )schemesdefaultall__vary_roundsbsdi_crypt__max_roundsbsdi_crypt__default_roundssha512_crypt__max_roundssha512_crypt__min_roundsz.des_crypt, md5_crypt, bsdi_crypt, sha512_crypt)r   r   zall.vary_roundszbsdi_crypt.max_roundszbsdi_crypt.default_roundszsha512_crypt.max_roundszsha512_crypt.min_roundsz[passlib]
bsdi_crypt.min_rounds = 29000
bsdi_crypt.max_rounds = 35000
bsdi_crypt.default_rounds = 31000
sha512_crypt.min_rounds = 45000
Hq  鸈  y  ȯ  )bsdi_crypt__min_roundsr   r   r    )r   r   r   r%   r   r   r   r    r   r   z
[passlib]
schemes = sha512_crypt
all.vary_rounds = 10%%
default.sha512_crypt.max_rounds = 20000
admin.all.vary_rounds = 5%%
admin.sha512_crypt.max_rounds = 40000
 N  皙?)r   r   r   admin__all__vary_roundsadmin__sha512_crypt__max_roundszJdeprecated = des_crypt
admin__context__deprecated = des_crypt, bsdi_crypt
r   r   )
deprecatedadmin__context__deprecatedzdes_crypt, bsdi_crypt)r+   zadmin.context.deprecatedc                 C   >   t |  tdd tdd tdd tdd d S )Nignorez)The CryptPolicy class has been deprecated,the method.*hash_needs_update.*is deprecatedz The 'all' scheme is deprecated.*zbsdi_crypt rounds should be oddr   setUpwarningsfilterwarningsself r6   e/home/air/Certificate/back/venv/lib/python3.10/site-packages/passlib/tests/test_context_deprecated.pyr1      s   
zCryptPolicyTest.setUpc                 C   s  t di | j}| | | j t | j}| | | j | tt i i  | jtt i dd | jtt g ddd G dd dtj}| jtt |gd | jtt tjgd G d	d
 d
tj}| jt	t ||gd | jt	t dgdgd | jt	t dgdd dS )ztest CryptPolicy() constructor   )dummyr   r   )r    bad__key__bsdi_crypt__max_roundsc                   @   s   e Zd ZdZdS )z5CryptPolicyTest.test_00_constructor.<locals>.namelessN__name__
__module____qualname__namer6   r6   r6   r7   nameless       r@   r   c                   @   s   e Zd ZdZdS )z4CryptPolicyTest.test_00_constructor.<locals>.dummy_1dummy_1Nr;   r6   r6   r6   r7   rC      rA   rC   r   r   r   r+   r   r   Nr6   )
r   sample_config_1pdassertEqualto_dictassertRaises	TypeErroruhStaticHandler
ValueErrorKeyError)r5   policyr@   rC   r6   r6   r7   test_00_constructor   s.   

z#CryptPolicyTest.test_00_constructorc                 C   s:   | j }t|}| | | j | ttj|d  dS )z(test CryptPolicy.from_path() constructorxxxN)sample_config_1s_pathr   	from_pathrG   rH   rF   rI   EnvironmentError)r5   pathrO   r6   r6   r7   test_01_from_path_simple  s   
z(CryptPolicyTest.test_01_from_path_simplec                 C   s   |   }t|| j t|}| | | j t|| jdd t|}| | | j t	| jddd}t|| tj|dd}| | | j dS )z7test CryptPolicy.from_path() constructor with encodings

utf-16utf-8source_encodingencodingN)
mktempr   sample_config_1sr   rS   rG   rH   rF   replacer	   )r5   rU   rO   uc2r6   r6   r7   test_01_from_path  s   


z!CryptPolicyTest.test_01_from_pathc                 C   s   t | j}| | | j t | jdd}| | | j t| j}t |}| | | j t| jddd}t j|dd}| | | j t | j	}| | | j
 dS )z*test CryptPolicy.from_string() constructorrW   rX   rY   rZ   r[   r]   N)r   from_stringr`   rG   rH   rF   ra   r
   r	   sample_config_4ssample_config_4pd)r5   rO   datarb   r6   r6   r7   test_02_from_string"  s   

z#CryptPolicyTest.test_02_from_stringc                 C   s   t | j}| | | j t | j}| | | j t | j }| | | j t |}| || | 	t
t jd | 	t
t jg  dS )z*test CryptPolicy.from_source() constructorr8   N)r   from_sourcerR   rG   rH   rF   r`   copyassertIsrI   rJ   )r5   rO   p2r6   r6   r7   test_03_from_source;  s   
z#CryptPolicyTest.test_03_from_sourcec                 C   s\   |  ttjg  t| jg}| | | j t| j| j	| j
g}| | | j dS )z+test CryptPolicy.from_sources() constructorN)rI   rM   r   from_sourcesr`   rG   rH   rF   rR   sample_config_2ssample_config_3pdsample_config_123pd)r5   rO   r6   r6   r7   test_04_from_sourcesQ  s   z$CryptPolicyTest.test_04_from_sourcesc                 C   sz   t di | j}|jdi | j}| | | j |jdi | j}| | | j || j}| | | j dS )z&test CryptPolicy.replace() constructorNr6   )	r   rF   ra   sample_config_2pdrG   rH   sample_config_12pdrp   rq   )r5   p1rl   p2bp3r6   r6   r7   test_05_replaced  s   zCryptPolicyTest.test_05_replacec                 C   sB   | j ttdgdd | j ttdgdd | j ttdgdgd dS )z!test CryptPolicy() forbidden kwdsr   xx)r   des_crypt__salt)r   	all__saltr   )r   user__context__schemesN)rI   rN   r   r4   r6   r6   r7   test_06_forbiddenu  s   
z!CryptPolicyTest.test_06_forbiddenc                 C   sB   t di | j}| |  t di | j}| |   dS )ztest has_schemes() methodNr6   )r   rF   
assertTruehas_schemesrp   )r5   ru   rw   r6   r6   r7   test_10_has_schemes  s   z#CryptPolicyTest.test_10_has_schemesc                 C   sV   t di | j}| jd }| t| | t di | j}| t| g  dS )ztest iter_handlers() methodr   Nr6   )r   rF   sample_config_1prdrG   listiter_handlersrp   )r5   ru   srw   r6   r6   r7   test_11_iter_handlers  s
   
z%CryptPolicyTest.test_11_iter_handlersc                 C   s`   t di | j}| |dtj | |dd | jt|jddd | | tj dS )ztest get_handler() methodr   sha256_cryptNTrequiredr6   )	r   rF   rk   get_handlerr   r   rI   rN   r   )r5   ru   r6   r6   r7   test_12_get_handler  s
   z#CryptPolicyTest.test_12_get_handlerc              	   C   s   t di | j}| |dtddddd | |dtddd	d
 t | j}| |dtddd | |ddtddd | |ddtddd dS )ztest get_options() methodr   r   r!   r"   r#   )vary_rounds
min_rounds
max_roundsdefault_roundsr   r$   r   )r   r   r   r'   )r   r   useradminr(   r   Nr6   )r   rt   rG   get_optionsdictrd   re   )r5   p12p4r6   r6   r7   test_13_get_options  s2   z#CryptPolicyTest.test_13_get_optionsc                 C   s*  t d	i | j}t d	i | j}| |d | |tj | |d | |d | |tj | |d | |dd | |dd | |dd | |dd t ddgdgdgd}| |d | |d | |dd | |dd dS )
z#test handler_is_deprecated() methodr   r   r   r   r   r   )r   r+   user__context__deprecatedNr6   )r   rF   sample_config_5pdassertFalsehandler_is_deprecatedr   r   r~   )r5   papbpcr6   r6   r7   test_14_handler_is_deprecated  s*   z-CryptPolicyTest.test_14_handler_is_deprecatedc                 C   sh   t jdtd t }| | d | |dd |jdd}| | d | |dd dS )z!test get_min_verify_time() methodr.   )categoryr   r   r   )min_verify_timeN)r2   r3   DeprecationWarningr   rG   get_min_verify_timera   )r5   r   r   r6   r6   r7   test_15_min_verify_time  s   z'CryptPolicyTest.test_15_min_verify_timec                 C   s^   t di | j}| t| | j | t|jdd| j | t|jdd| j dS )ztest iter_config() methodTresolve)iniNr6   )r   r   rG   r   iter_configsample_config_5prdsample_config_5pidr5   p5r6   r6   r7   test_20_iter_config  s   z#CryptPolicyTest.test_20_iter_configc                 C   s<   t di | j}| | | j | |jdd| j dS )ztest to_dict() methodTr   Nr6   )r   r   rG   rH   r   r   r6   r6   r7   test_21_to_dict	  s   zCryptPolicyTest.test_21_to_dictc                 C   sP   t di | j}| }t |}| | | j |jdd}| |t dS )ztest to_string() methodzlatin-1r]   Nr6   )r   r   	to_stringrd   rG   rH   assertIsInstancebytes)r5   r   r   r   r6   r6   r7   test_22_to_string  s   
z!CryptPolicyTest.test_22_to_stringN)8r<   r=   r>   __doc__descriptionPrefixr`   osrU   abspathjoindirname__file__rR   existsr   
startswithr   rF   sample_config_1pidr   r   r   r   r   r   ro   rs   rt   rp   rq   re   rf   sample_config_5srj   r   updater   r   r1   rP   rV   rc   rh   rm   rr   rx   r}   r   r   r   r   r   r   r   r   r   r6   r6   r6   r7   r   %   s    

		
	(	
'r   c                   @   sh   e Zd ZdZdZdd Zdd Zdd Zd	d
 Ze	g ddgddddddddddZ
dd Zdd ZdS )CryptContextTestztest CryptContext classr   c                 C   r-   )Nr.   z3CryptContext\(\)\.replace\(\) has been deprecated.*z9The CryptContext ``policy`` keyword has been deprecated.*=.*(CryptPolicy|context\.policy).*(has|have) been deprecated.*r/   r0   r4   r6   r6   r7   r1   $  s   
zCryptContextTest.setUpc                 C   s  t tjtjtjg}|j \}}}| |tj | |tj | |tj t g d}|j \}}}| |tj | |tj | |tj |j}t |d}| |	 |	  t |dd}| 
|	 |	  | |	 tg ddd | tt|dd | jtt dd dS )	ztest constructorr   r   r   )rO   r   )rO   r   rE   rO   Nx)r   r   r   r   r   rO   r   rk   rG   rH   assertNotEqualr   rI   rJ   setattr)r5   cccbarO   r6   r6   r7   rP   1  s(   
z$CryptContextTest.test_00_constructorc                 C   sd   t g d}| |j tj | }| || |jdd}| || | |j tj dS )ztest replace()r   r   r&   N)	r   rk   rO   r   r   r   ra   assertIsNotr   )r5   r   cc2cc3r6   r6   r7   test_01_replaceN  s   z CryptContextTest.test_01_replacec                 C   s   t  }| jt|jddd | t|jd | t|jdd t dg}tg d}||_| jt|jddd | t|jd | t|jdd dS )ztest no handlersr   Tr   secretr   rB   N)r   rI   rN   identifyr   verifyr   rO   )r5   r   pr6   r6   r7   test_02_no_handlers_  s   

z$CryptContextTest.test_02_no_handlers)r   r   phpassr   r   r   r         r   i  i  H   )r   r+   r   r   r   bsdi_crypt__vary_roundssha256_crypt__max_roundssha256_crypt__min_roundssha256_crypt__default_roundsphpass__identphpass__default_roundsc                 C   st   t di | j}| |d | |d | |d | |d | |d | |d dS )	ztest hash_needs_update() method9XXD4trGYeGJAz"$1$J8HC2RCr$HcmM.7NxB2weSvlw2FgzU0zK$5$rounds=1999$jD81UCoo.zI.UETs$Y7qSTQ6mTiU9qZB4fRr43wRgQq4V.5AAf7F97Pzxey/zK$5$rounds=2000$228SSRje04cnNCaQ$YGV4RYu.5sNiBvorQDlO0WWQjyJVGKBcJXz3OtyQ2u8zK$5$rounds=3000$fS9iazEwTKi7QPW4$VasgBC8FqlOvD7x2HhABaMXCTh9jwHclPA9j5YQdns.zK$5$rounds=3001$QlFHHifXvpFX4PLs$/0ekt7lSs/lOikSerQ0M/1porEHxYq7W/2hdFpxA3fANr6   )r   sample_policy_1r~   hash_needs_updater   r5   r   r6   r6   r7   test_12_hash_needs_update  s   z*CryptContextTest.test_12_hash_needs_updatec                 C   sv   t dd tdg}di fdddifdi fdi ffD ]\}}| jt|j|fi | qtdg}| t|jd dS )	z'test non-string hash values cause errorr.   z0.*needs_update.*'scheme' keyword is deprecated.*r   Nschemer8   r6   mysql323)r2   r3   r   rI   rJ   r   )r5   r   r   kwdsr   r6   r6   r7   test_30_nonstring_hash  s   


z'CryptContextTest.test_30_nonstring_hashN)r<   r=   r>   r   r   r1   rP   r   r   r   r   r   r   r6   r6   r6   r7   r      s,    r   c                   @   s   e Zd Zd ZdS )dummy_2Nr;   r6   r6   r6   r7   r     rA   r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	LazyCryptContextTestr   c                 C   s:   t |  td | td tdd tdd d S )Nr   r.   z1CryptContext\(\)\.replace\(\) has been deprecatedr   )r   r1   unload_handler_name
addCleanupr2   r3   r4   r6   r6   r7   r1     s   
zLazyCryptContextTest.setUpc                 C   sz   |  td tdd ttddgdgd}|  tdd | |jd | |j	 ddg | tdd dS )ztest plain kwdsr   passlib.tests.test_contextr   )r+   TN)
r   has_crypt_handlerr   r   iterr~   rO   r   rG   r   r   r6   r6   r7   test_kwd_constructor  s   
z)LazyCryptContextTest.test_kwd_constructorc                    s~     td tdd d
 fdd	}t|dd}  tdd  |jd  |j ddg  tdd d	S )z0test create_policy() hook, returning CryptPolicyr   r   Fc                    s      |  ttddgdgdS )Nr   r   rD   )r~   r   r   )flagr4   r6   r7   create_policy  s   
zELazyCryptContextTest.test_callable_constructor.<locals>.create_policyT)r   r   r   N)F)	r   r   r   r   r~   rO   r   rG   r   )r5   r   r   r6   r4   r7   test_callable_constructor  s   
z.LazyCryptContextTest.test_callable_constructorN)r<   r=   r>   r   r1   r   r   r6   r6   r6   r7   r     s
    r   )'r   
__future__r   loggingr   r   r2   pkg_resourcesr   ImportErrorpasslibr   passlib.contextr   r   r   passlib.utilsr	   r
   passlib.utils.handlersutilshandlersrK   passlib.tests.utilsr   r   passlib.registryr   r   r   r   r   r<   logr   r   rL   r   r   r6   r6   r6   r7   <module>   s2    
   ~ 