U
    øÐLa$  ã                   @   s¾   d Z ddlmZ ddlmZ ddlZddlZddlmZm	Z	m
Z
 ddlmZmZmZmZmZ ddlm  mZ ddlmZ eeƒZG dd	„ d	ejƒZG d
d„ dejƒZdZG dd„ deƒZdS )z>tests for passlib.hash -- (c) Assurance Technologies 2003-2009é    )Úwith_statement)Ú	getLoggerN)ÚhashÚregistryÚexc)Úregister_crypt_handlerÚregister_crypt_handler_pathÚget_crypt_handlerÚlist_crypt_handlersÚ_unload_handler_name)ÚTestCasec                   @   s   e Zd Zd ZdS )Údummy_0N©Ú__name__Ú
__module__Ú__qualname__Úname© r   r   ú?/tmp/pip-unpacked-wheel-8v2dfbvt/passlib/tests/test_registry.pyr      s   r   c                   @   s   e Zd ZdZdS )Úalt_dummy_0r   Nr   r   r   r   r   r      s   r   é   c                       sP   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
‡  ZS )ÚRegistryTestzpasslib.registryc                    s>   t t| ƒ ¡  ttjƒ‰ttjƒ‰ ‡ ‡fdd„}|  |¡ d S )Nc                      s0   t j ¡  t j ˆ¡ t j ¡  t j ˆ ¡ d S )N)r   Ú
_locationsÚclearÚupdateÚ	_handlersr   ©ÚhandlersÚ	locationsr   r   Úrestore0   s    

z#RegistryTest.setUp.<locals>.restore)Úsuperr   ÚsetUpÚdictr   r   r   Z
addCleanup)Úselfr   ©Ú	__class__r   r   r!   *   s
    

zRegistryTest.setUpc                 C   s¶   t tƒ ttƒ |  tttd¡ ttddƒ}tƒ }|t_|  tj|¡ |dkrdt`|  	t
tdƒ¡ n|t_|  tj|¡ G dd„ dtjƒ}|t_|  tdƒ|¡ |  tttd|¡ dS )ztest passlib.hash proxy objectZfooeyÚ
__loader__Nc                   @   s   e Zd ZdZdS )z-RegistryTest.test_hash_proxy.<locals>.dummy_1Údummy_1Nr   r   r   r   r   r'   P   s   r'   Zdummy_1x)Údirr   ÚreprÚassertRaisesÚAttributeErrorÚgetattrÚobjectr&   ÚassertIsÚassertFalseÚhasattrÚuhÚStaticHandlerr'   r	   Ú
ValueErrorÚsetattr)r#   ÚoldÚtestr'   r   r   r   Útest_hash_proxy7   s     zRegistryTest.test_hash_proxyc              	   C   sp  t j}|  d|k¡ |  ttdƒ¡ |  ttdd¡ |  ttdt	d ¡ |  ttdt	d ¡ tdt	ƒ |  dt
ƒ k¡ |  dt
ddk¡ |  tjt¡ |  dt
ddk¡ tdƒ tdt	d ƒ |  tjt¡ tdƒ tdt	ƒ |  ttd¡ td	t	ƒ |  ttd	¡ td	ƒ tj d
d¡ tdd
ƒ t ¡  t ddt¡ tdƒ}W 5 Q R X ddlm} |  ||j¡ dS )z"test register_crypt_handler_path()r   z.test_registryz:dummy_0:xxxz:dummy_0.xxxT)Zloaded_onlyz:alt_dummy_0Údummy_xr   z passlib.tests._test_bad_registerNZ	dummy_badÚignoreZ
xxxxxxxxxxr   )Ú_test_bad_register)r   r   Ú
assertTruer/   r0   r   r*   r3   r   r   r
   r.   r   Úunload_handler_namer   Ú	TypeErrorr	   ÚsysÚmodulesÚpopÚwarningsÚcatch_warningsÚfilterwarningsÚDeprecationWarningZpasslib.testsr:   Zalt_dummy_bad)r#   ÚpathsÚhZtbrr   r   r   Ú test_register_crypt_handler_pathX   sH     ÿ ÿ ÿ




z-RegistryTest.test_register_crypt_handler_pathc              
   C   sP  |   tti ¡ |   tttdtjftddƒ¡ |   tttdtjftddƒ¡ |   tttdtjftddƒ¡ |   tttdtjftddƒ¡ |   tttdtjftddƒ¡ G dd	„ d	tjƒ}G d
d„ dtjƒ}|  d	t	ƒ k¡ t|ƒ t|ƒ |  
td	ƒ|¡ |   tt|¡ |  
td	ƒ|¡ t|dd |  
td	ƒ|¡ |  d	t	ƒ k¡ dS )ztest register_crypt_handler()ÚxN)r   ZAB_CDzab-cdZab__cdÚdefaultc                   @   s   e Zd ZdZdS )z9RegistryTest.test_register_crypt_handler.<locals>.dummy_1r'   Nr   r   r   r   r   r'   “   s   r'   c                   @   s   e Zd ZdZdS )z:RegistryTest.test_register_crypt_handler.<locals>.dummy_1br'   Nr   r   r   r   r   Údummy_1b–   s   rJ   T)Úforce)r*   r=   r   r3   Útyper1   r2   r"   r;   r
   r.   r	   ÚKeyError)r#   r'   rJ   r   r   r   Útest_register_crypt_handlerˆ   s"         z(RegistryTest.test_register_crypt_handlerc              	   C   sÚ   G dd„ dt jƒ}|  ttd¡ |  tddƒd¡ t|ƒ |  tdƒ|¡ t ¡ > t 	ddt
¡ |  tdƒ|¡ tdtƒ |  tdƒt¡ W 5 Q R X d	d
lm} d|jd< dD ]$}|  tt|¡ |  t|dƒd¡ q°dS )ztest get_crypt_handler()c                   @   s   e Zd ZdZdS )z4RegistryTest.test_get_crypt_handler.<locals>.dummy_1r'   Nr   r   r   r   r   r'   ª   s   r'   Nr9   zMhandler names should be lower-case, and use underscores instead of hyphens:.*zDUMMY-1r   zDUMMY-0r   )r   ÚdummyÚ_fake)rP   Ú__package__)r1   r2   r*   rM   r	   r.   r   rA   rB   rC   ÚUserWarningr   r   r   Úpasslibr   Ú__dict__)r#   r'   r   r   r   r   r   Útest_get_crypt_handler§   s    


z#RegistryTest.test_get_crypt_handlerc                 C   sD   ddl m} dtjd< |ƒ D ]}|  | d¡d| ¡ qtdƒ dS )ztest list_crypt_handlers()r   ©r
   rO   rP   Ú_z%r: N)Úpasslib.registryr
   r   rT   r/   Ú
startswithr<   )r#   r
   r   r   r   r   Útest_list_crypt_handlersÆ   s
    

z%RegistryTest.test_list_crypt_handlersc              	   C   sŒ   ddl m} ddlm}m} |ƒ D ]d}| d¡rD|dd… |ƒ krDq"|dkrNq"z|  ||ƒ¡ W q" tjk
r„   ||kr~Y q"‚ Y q"X q"dS )z-verify we have tests for all builtin handlersr   rV   )Úget_handler_caseÚconditionally_available_hashesZldap_é   N)Zroundup_plaintext)	rX   r
   Zpasslib.tests.test_handlersr[   r\   rY   r;   r   ZMissingBackendError)r#   r
   r[   r\   r   r   r   r   Útest_handlersÐ   s    
zRegistryTest.test_handlers)r   r   r   ZdescriptionPrefixr!   r7   rG   rN   rU   rZ   r^   Ú__classcell__r   r   r$   r   r   &   s   !0
r   )Ú__doc__Ú
__future__r   Úloggingr   rA   r>   rS   r   r   r   rX   r   r   r	   r
   r   r<   Zpasslib.utils.handlersÚutilsr   r1   Zpasslib.tests.utilsr   r   Úlogr2   r   r   r8   r   r   r   r   r   Ú<module>   s   	