o
    5Îhbr  ã                   @   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  i¨a  é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   úy/var/www/html/construction_image-detection-poc/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    
ÿ   ~ 