o
    Ƌi2#                    @   s  d Z ddlmZ ddlmZ erddlmZ nddlmZ ddlZddl	m
Z
 ddlZeeZddlZddlZddlmZ ddlmZmZ dd	lmZmZ dd
lmZmZ ddlmZmZmZmZm Z m!Z! ddl"m#  m$Z% ddl&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z, ddl-m.Z.m/Z0m1Z2m3Z3 ej45ej46e7Z8dd Z9G dd de'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'Z@dS )ztests for passlib.context    )with_statement)PY3)NoSectionErrorN)partial)hash)CryptContextLazyCryptContext)PasslibConfigWarningPasslibHashWarning)tick
to_unicode)irangeuunicodestr_to_uasciiPY2PY26)TestCaseset_fileTICK_RESOLUTION
quicksleep	time_callhandler_derived_from)register_crypt_handler_path_has_crypt_handler_unload_handler_nameget_crypt_handlerc                 O   s.   |   }|D ]}|| q|r|| |S N)copyupdate)firstargskwdstargetarg r%   ^/home/air/goalskill_sales/back/venv/lib/python3.10/site-packages/passlib/tests/test_context.pymerge_dicts&   s   
r'   c                       sl  e Zd ZdZg dZdd eD Zeeddddd	d
dZeeedZ	e
dZe
dZejedZee
de
dZejedZee
de
ddZejedZ	 edddddZdZeeeZeddZeeeZeg d d!gd"d#d$d%d&d'd&d(d)d*Z fd+d,Zd-d. Z d/d0 Z!d1d2 Z"d3d4 Z#d5d6 Z$d7d8 Z%d9d: Z&d;d< Z'd=d> Z(d?d@ Z)dAdB Z*dCdD Z+dEdF Z,d}dHdIZ-dJdK Z.dLdM Z/dNdO Z0dPdQ Z1dRdS Z2dTdU Z3dGi fdGdVd!ifdWi fdXi fgZ4dYdZ Z5d[d\ Z6d]d^ Z7d_d` Z8d~dadbZ9dcdd Z:dedf Z;dgdh Z<didj Z=dkdl Z>dmdn Z?dodp Z@dqdr ZAdsdt ZBdudv ZCdwdx ZDdydz ZEd{d| ZF  ZGS )CryptContextTestr   )	des_crypt	md5_crypt
bsdi_cryptsha512_cryptc                 C   s   g | ]}t |qS r%   )r   .0namer%   r%   r&   
<listcomp>?   s    zCryptContextTest.<listcomp>r*   皙?i1u  ia  iP  @  )schemesdefaultall__vary_roundsbsdi_crypt__max_roundsbsdi_crypt__default_roundssha512_crypt__max_roundssha512_crypt__min_roundsr3   a  [passlib]
schemes = des_crypt, md5_crypt, bsdi_crypt, sha512_crypt
default = md5_crypt
; this is using %...
all__vary_rounds = 10%%
bsdi_crypt__default_rounds = 25001
bsdi_crypt__max_rounds = 30001
sha512_crypt__max_rounds = 50000
sha512_crypt__min_rounds = 40000
z[passlib]
schemes = des_crypt, md5_crypt, bsdi_crypt, sha512_crypt
default = md5_crypt
all__vary_rounds = 0.1
bsdi_crypt__default_rounds = 25001
bsdi_crypt__max_rounds = 30001
sha512_crypt__max_rounds = 50000
sha512_crypt__min_rounds = 40000

zsample1.cfg
z
zsample1b.cfg	[passlib]z
[mypolicy]utf-16zsample1c.cfgFiIq  i  iy  iȯ  )bsdi_crypt__min_roundsr6   r7   r9   z[passlib]
bsdi_crypt__min_rounds = 29001
bsdi_crypt__max_rounds = 35001
bsdi_crypt__default_rounds = 31001
sha512_crypt__min_rounds = 45000
r,   r4   )r)   r*   phpassr+   sha256_cryptr)   rA         r       H   )r3   
deprecatedr4   r6   r7   bsdi_crypt__vary_roundssha256_crypt__max_roundssha256_crypt__min_roundssha256_crypt__default_roundsphpass__identphpass__default_roundsc                    s*   t t|   tdd tdd d S )Nignorez The 'all' scheme is deprecated.*z4.*'scheme' keyword is deprecated as of Passlib 1.7.*)superr(   setUpwarningsfilterwarningsself	__class__r%   r&   rQ      s   zCryptContextTest.setUpc                 C   s   t  }| | i  t di | j}| | | j t di | j}| | | j t di | j}| | | j t di | j}| | | j t tdgd}| | d dS )ztest class constructorrA   r:   )rA   Nr%   )	r   assertEqualto_dictsample_1_dictsample_1_resolved_dictsample_2_dictsample_3_dictr   r3   rU   ctxr%   r%   r&   test_01_constructor   s   z$CryptContextTest.test_01_constructorc                 C   s   t | j}| | | j t | j}| | | j t | jd}| | | j t | j}| | | j t j| j	ddd}| | | j | 
tt jd | j
tt j| jdd dS )ztest from_string() constructorutf-8mypolicyr=   sectionencodingNfakesectionrd   )r   from_stringsample_1_unicoderX   rY   rZ   sample_1_unnormalizedencodesample_1b_unicodesample_1c_bytesassertRaises	TypeErrorr   r^   r%   r%   r&   test_02_from_string   s    


z$CryptContextTest.test_02_from_stringc                 C   s   t j| jstd| j t| j}| | | j	 t| j
}| | | j	 tj| jddd}| | | j	 | ttjt jtd | jttj| jdd dS )	ztest from_path() constructorzcan't find data file: %rrb   r=   rc   zsample1xxx.cfgrf   rg   N)ospathexistssample_1_pathRuntimeErrorr   	from_pathrX   rY   rZ   sample_1b_pathsample_1c_pathrn   EnvironmentErrorjoinherer   r^   r%   r%   r&   test_03_from_path   s    



z"CryptContextTest.test_03_from_pathc                 C   s  t di | j}|jdi | j}| | | j | | | j |jdi | j}| | | j | | | j |jdi | j}| | | j | }| 	|| | | | j | | | j |j
di | j | | | j | | | j dS )ztest copy() methodNr%   )r   rZ   r   r\   rX   rY   sample_12_dictr]   sample_123_dictassertIsNotr   )rU   cc1cc2cc2bcc3cc4r%   r%   r&   test_04_copy  s    zCryptContextTest.test_04_copyc                 C   s$   t di | j}| t|d dS )ztest repr()z!^<CryptContext at 0x-?[0-9a-f]+>$Nr%   )r   rZ   assertRegexrepr)rU   r   r%   r%   r&   test_09_repr+  s   zCryptContextTest.test_09_reprc                 C   s   t  }|| j | | | j || j | | | j || jd | | | j | t|jd t di | j}|ji dd | | | j t  }|| j || j	 | | | j	 dS )z test load() / load_path() methodra   NT)r   r%   )
r   loadrZ   rX   rY   ri   rk   rn   ro   r\   r^   r%   r%   r&   test_10_load4  s   
zCryptContextTest.test_10_loadc                 C   s   t ddgddd}| }| jt|jdd | | | | jt|jdd | | | | jt|jd	d
 | | | dS )z$test load() errors restore old stater)   rA   i  r1   )rL   r5   T)too__many__key__parts)fake_context_optioni'  rK   N)r   	to_stringrn   ro   r   rX   KeyError
ValueError)rU   ccresultr%   r%   r&   test_11_load_rollback_  s   z&CryptContextTest.test_11_load_rollbackc                 C   s  t di | j}|  | | | j t di | j}|jdi | j | | | j |jdi | j | | | j t di | j}|| j | | | j t di | j}|| j	 | | | j | 
t|ji i  | j
t|ji dgd | 
t|jd dS )ztest update() methodr)   r:   Nr%   )r   rZ   r   rX   rY   r\   r}   r]   r~   sample_2_unicodern   ro   r^   r%   r%   r&   test_12_updatew  s"   zCryptContextTest.test_12_updatec                 C   s6  dd }| j ttdd | j ttdd | j ttdd | j ttdd | j ttdd	 | j ttd
d | j t|fi ddi td'i ddd}| |dd tdd}| |dd| | |dd| | |dd| | |d'i ddi| | |d'i ddi| tdd}| |dd| | |d'i ddi| tdd}| |dd| | |dd| | |d'i ddi| | |d'i ddi| tdd}| |dd| | |d'i ddi| tddgd d!}| j t|jd d" | j ttdgd#d$ | j ttdgd#d% d&S )(ztest basic option parsingc                  [   s   t di |  S )Nr%   r   rY   )r"   r%   r%   r&   parse     z/CryptContextTest.test_20_options.<locals>.parser1   )__x)default__scheme__)__option)default____option)__scheme__optioni0u  )!category__scheme__option__invalidzadmin.context__schemesr*   md5_crypt,des_cryptr)   )r3   zadmin.context__defaultadminr?   )context__default)default__context__defaultzcontext.defaultzdefault.context.defaultadmin__context__defaultzadmin.context.defaultr5   )default__all__vary_roundszall.vary_roundszdefault.all.vary_rounds)admin__all__vary_roundszadmin.all.vary_roundsr@   P)rM   )md5_crypt__identxx)r3   des_crypt__salt)r3   	all__saltNr%   )rn   ro   r   r   rX   default_schemedictr   )rU   r   r_   r   r%   r%   r&   test_20_options  sR   	





z CryptContextTest.test_20_optionsc                 C   s   t dd}| | d t ddgd}| | d t dd}| | d t tjtjgd}| | d | jtt tj	gd G dd	 d	tj	}| jt
t |gd G d
d dtj	}| jtt ||gd | jtt dgd dS )z%test 'schemes' context option parsingNr:   r%   r)   r*   )r)   r*   z des_crypt, md5_crypt, c                   @   s   e Zd ZdZdS )z2CryptContextTest.test_21_schemes.<locals>.namelessN__name__
__module____qualname__r/   r%   r%   r%   r&   nameless      r   c                   @   s   e Zd ZdZdS )z1CryptContextTest.test_21_schemes.<locals>.dummy_1dummy_1Nr   r%   r%   r%   r&   r     r   r   )admin__context__schemes)r   rX   r3   r   r)   r*   rn   ro   uhStaticHandlerr   r   )rU   r   r   r   r%   r%   r&   test_21_schemes  s    


z CryptContextTest.test_21_schemesc                 C   s  ddd}t dgd}|jddgd | ||dg t dgddgd}| ||dg | jtt tjgddgd t d	g d
d}| ||ddg | jtt dgdgd | jtt dgdgd | jtt ddgddgd | jtt ddgddd | jtt ddgddd | jtt ddgddd | jtt ddgddd t ddgddg d | jtt dd t dgddgdgd}| ||dg | ||ddg | ||ddg t dgddgg d}| ||dg | ||ddg | ||dg  dS )z(test 'deprecated' context option parsingNc                        fdd  D S )Nc                    s   g | ]} | jr|qS r%   handlerrH   r-   categoryr_   r%   r&   r0     s    
zGCryptContextTest.test_22_deprecated.<locals>.getdep.<locals>.<listcomp>r:   r_   r   r%   r   r&   getdep     z3CryptContextTest.test_22_deprecated.<locals>.getdepr*   rH   r)   r:   )rH   r3   r   )r*   r)   rA   r3   rH   )r3   admin__context__deprecated)r3   r4   rH   )r3   r4   r   )r3   r   rH   )r3   r   r   )r3   rH   r   r   {   )rH   r3   r   userr   r   )	r   r   rX   rn   ro   r   r*   r   r   )rU   r   r   r%   r%   r&   test_22_deprecated  s~   
z#CryptContextTest.test_22_deprecatedc                 C   s  |  tdd tdd tdddgd}|  | d ttjddgd}|  | d tddgd}|  | d |jdd |  | d | jt	tdgdd | jt
tdd tdddgdd	}|  | d |  |d
d |  |dd dS )z%test 'default' context option parsingr*   r?   r)   )r4   r3   r:   r   )r3   r4      )r4   r3   r   r   r   N)rX   r   rY   r   r   r   r*   r   rn   r   ro   r^   r%   r%   r&   test_23_defaulth  s,   z CryptContextTest.test_23_defaultc                 C   s\   dd }|  |dd |  |dd |  |dd |  |dd |  |dd dS )	z&test 'vary_rounds' hash option parsingc                 S   s   t | d d S )Nr   r5   r   )vr%   r%   r&   r     r   z3CryptContextTest.test_24_vary_rounds.<locals>.parser1   z0.1z10%i  1000N)rX   )rU   r   r%   r%   r&   test_24_vary_rounds  s   z$CryptContextTest.test_24_vary_roundsNc                 C   s   | j t|||d d S )N)msg)
assertTruer   )rU   r   baser   r%   r%   r&   assertHandlerDerivedFrom  r   z)CryptContextTest.assertHandlerDerivedFromc                 C   s   t  }| | d | |jddd t di | j}| | t| j | |jdddt| j t|jdd| jD ]\}}| t	|| q@t di | j
}| | d dS )ztest schemes() methodr%   Tresolver   unconfiguredN)r   rX   r3   rZ   tuplesample_1_schemessample_1_handlerszipr   r   r\   )rU   r_   r   correctr%   r%   r&   test_30_schemes  s   z CryptContextTest.test_30_schemesc                 C   s   t  }| t|j t di | j}| | d | |jdddtj | |jddtj t di | j	}| t|j t | j
d}| | d dS )	ztest default_scheme() methodr*   Tr   r   r:   r)   Nr%   )r   rn   r   r   rZ   rX   r   r*   r   r\   r   r^   r%   r%   r&   test_31_default_scheme  s   z'CryptContextTest.test_31_default_schemec                 C   sl  t  }| t|j | t|jd t di | j}| |jddtj | | tj | |jdddtj	 | |dtj	 | t|jd t ddd}| |jddtj
 | | tj
 | |jddd	tj
 | |jdd
tj
 | |jddd	tj | |jdd
tj
 tr| |jtddd	tj
 | |jtddd	tj dS dS )ztest handler() methodr*   T)r   r)   mysql323sha256_crypt,md5_cryptr   staff)r   r   r   r   Nr%   )r   rn   r   r   rZ   rX   r   r*   r   r)   rA   r   r   r^   r%   r%   r&   test_32_handler  s(    z CryptContextTest.test_32_handlerc                 C   s<  ddd}t dg dddgdd	d
ddgdddd
}| |jjd | ||dtddd
d | ||ddtddd
d | ||ddtddd | ||dtddd | ||ddtddd | ||ddtddd | ||dtd	d | ||ddtd	d | ||ddtddd dS )z*test internal _get_record_options() methodNc                 S   s   | j ||d S )Nr   )_config_get_record_options_with_flag)r_   schemer   r%   r%   r&   options  r   z1CryptContextTest.test_33_options.<locals>.optionsT)r,   r)   r+   r,   r)   r1   皙?i N  r+   皙?g333333?r2   )
truncate_errorr3   rH   r5   rI   r8   r   r   admin__bsdi_crypt__vary_roundsadmin__sha512_crypt__max_rounds)r   )rH   vary_rounds
max_roundsr   r   )r   r   )rH   r   )r   )r   rH   r   )r   rX   r   
categoriesr   )rU   r   r   r%   r%   r&   test_33_options  sd   

z CryptContextTest.test_33_optionsc                 C   s<   t di | j}| | | j | |jdd| j dS )ztest to_dict() methodTr   Nr%   )r   rZ   rX   rY   r[   r^   r%   r%   r&   test_34_to_dictC  s   z CryptContextTest.test_34_to_dictc                 C   s   t d
i | j}| }ts| || j t |}| | | j |jdd}| ||dd ddl	m
} t |dg}| }| |d d	S )ztest to_string() methodzpassword-securityrg   r<   z[password-security]r   )UnsaltedHashr*   zM# NOTE: the 'unsalted_test_hash' handler\(s\) are not registered with PasslibNr%   )r   rZ   r   r   rX   ri   rh   rY   replace!passlib.tests.test_utils_handlersr   r   )rU   r_   dumpctx2otherr   ctx3r%   r%   r&   test_35_to_stringJ  s   
z"CryptContextTest.test_35_to_stringr   r   r%   c                 C   s  t jt jt jg}t|dd}|D ]=}|j d|jd}| |||j | |j|ddd| | |j|dd| | 	|
d| | |
d| q| d}| ||d	 |d
| }| ||d	 |jd
| d	d}| ||d	 | jt|jd
| dd dS )z-test basic hash/identify/verify functionality   r7   testr   Tr   r   notestr*   secretr)   N)r   r*   r)   r+   r   r/   rX   identifyr   r   verifyassertFalsegenhash	genconfigrn   r   )rU   handlersr   crypthr%   r%   r&   test_40_basicq  s    
zCryptContextTest.test_40_basicc                 C   sH  t ddgdddd}| | d | |jddd	 | |jdd
dd | |jdddd	 | |jdddddd trk|jdd}| |jtd
dd | |jtddd	 | t	t  j | jt	t  jdd | jt	|jdd | jt
|jddd | jt
|jdd | jt
|jdd dS )ztest genconfig() methodr*   r@   rF   rG   r   )r3   rM   rN   admin__phpass__identz$1$r   z$H$5r   )r   r   z$P$5r   ........   )r   saltroundsidentz"$P$6........22zGEuacuPOqEpYPDeR0R/r?   r   faker   N)r   r   r  
startswithrX   r   r   r   rn   r   ro   rU   r   c2r%   r%   r&   test_41_genconfig  s.   z"CryptContextTest.test_41_genconfigc                 C   s   t dg}|d}| jD ]\}}| jt|j||fi | qt dg}| jD ]\}}|du r0q'| jt|jd|fi | q't dg}| t|jdd | tt  jdd | jt|jd|dd | jt|jd|d	d | jt|jd|d	d
 dS )ztest genhash() methodr)   stubNr   r   r   r  r   r   r   )r   r   nonstring_vectorsrn   ro   r  r   r  )rU   r   r   r   r"   configr%   r%   r&   test_42_genhash  s   



z CryptContextTest.test_42_genhashc                 C   s   t di | j}|d}| |d | |d| | |d| | jt|j	dd t dg}| j
D ]\}}| jt|j|fi | q8| tt  jd | jt|jddd	 d
S )ztest hash() methodpasswordz$5$rounds=3000$	passwordxi  rL   r)   r   r   r   Nr%   )r   sample_4_dictr   r   r  r   r  rn   r   r   r  ro   r   )rU   r   r   r   r"   r%   r%   r&   test_43_hash  s   


zCryptContextTest.test_43_hashc              	   C   sN  t di | j}| dg | |jddddd W d   n1 s%w   Y  | dg | |jddddd	d
 W d   n1 sHw   Y  | dg | |jddddd W d   n1 sjw   Y  | dg | |jddddd W d   n1 sw   Y  | jt|jddd | jt|jddd dS )z;test hash() method -- legacy 'scheme' and settings keywords"passing settings to.*is deprecatedr  r@   r	  )r   r  z"$H$5........De04R5Egz0aq8Tf.1eVhY/Nr   )r   r  r  z"$P$5........De04R5Egz0aq8Tf.1eVhY/  naclr  r  z?$5$rounds=1999$nacl$nmfwJIxqj0csloAAvSER0B8LU0ERCAbhmMug4Twl609  z?$5$rounds=2001$nacl$8PdeoPL4aXQnJ0woHhqgIw/efyfCKC2WHneOpnvF.31r   r  r   r   r%   )r   r  assertWarningListrX   r   rn   r   ro   )rU   use_16_legacyr   r%   r%   r&   test_43_hash_legacy  s6   	z$CryptContextTest.test_43_hash_legacyc                 C   s   g d}t |dd}| |dd | jt|jddd t dg}| jD ]\}}| jt|j|fi | q%t  }| |d	d | jt|jd	dd | jt|jdd
d dS )ztest identify() border casesr*   r)   r+   r   r   z$9$232323123$1287319827NT)requiredr)   r   r   r   )	r   rX   r   rn   r   r  ro   assertIsr   )rU   r  r   r   r"   r%   r%   r&   test_44_identify.  s   
z!CryptContextTest.test_44_identifyc                 C   s  g d}t |dd}tjd}| |d| | |d|  | |jd|dd | |jd|dd  | jt|jd|dd | t|jd	d
 t dg}|d	 }}| jD ]\}}| jt|j||fi | q]| 	||d t dg}| jD ]\}}|du rq| jt|jd|fi | q| t
t  jdd | jt
|jd|dd | jt|jd|dd | jt|jd|dd dS )ztest verify() scheme kwdr$  r   r   r   r   r*   r   r+   r  z$6$232323123$1287319827r)   Nr   r   r  r   r   )r   r   r*   r   r   rn   r   r  ro   r  r   )rU   r  r   r  refhashr   r"   r%   r%   r&   test_45_verifyH  s.   

zCryptContextTest.test_45_verifyc                    s  t di | j}| |d | |d | |d | |d | |d | |d g  G  fdddtj}t |g}|d	 }}| || |  |d
fg  d
d
= | |j|dd |  |dfg  d
d
= | |j|dd |  |dfg  d
d
= t dg}| j	D ]\}}| j
t|j|fi | q| 
tt  jd | j
t|j|dd | j
t|j|dd | j
t|j|dd d
S )ztest 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/2hdFpxA3fAc                       s.   e Zd ZdZdZed fdd	Zdd ZdS )	z4CryptContextTest.test_46_needs_update.<locals>.dummydummy@Nc                    s     ||f |dkS )Nnu)append)clsr   r   check_stater%   r&   needs_update  s   zACryptContextTest.test_46_needs_update.<locals>.dummy.needs_updatec                 S   s0   ddl m} t|tr|d}t|| S )Nr   )md5ra   )hashlibr3  
isinstancer   rk   r   	hexdigest)rU   r   r3  r%   r%   r&   _calc_checksum  s   

zCCryptContextTest.test_46_needs_update.<locals>.dummy._calc_checksumr   )r   r   r   r/   _hash_prefixclassmethodr2  r7  r%   r0  r%   r&   r+    s    r+  r   Nbob)r   r-  r)   r   r  r   r   r   r%   )r   r  r   r2  r  r   r   r   rX   r  rn   ro   r   )rU   r   r+  r_   r   r(  r"   r%   r0  r&   test_46_needs_update{  s6   




z%CryptContextTest.test_46_needs_updatec           
      C   s  t di | j}|dd}|dd}|d|\}}| | | |d |d|\}}| | | |d |d|\}}| | | ||d |d|\}}| | | |d t dg}|d }}| j	D ]\}}	| j
t|j||fi |	 qv| ||dd t dg}| j	D ]\}}	|du rq| j
t|jd|fi |	 q| 
tt  jdd	 | j
t|jd|d
d | j
t|jd|dd | j
t|jd|dd dS )ztest verify_and_update()r)   r  rA   	wrongpassNr  FNr   r   r  r   r   r   r%   )r   r  r   r   verify_and_updater  r&  r   r   r  rn   ro   rX   r   )
rU   r   h1h2oknew_hashr   r(  r   r"   r%   r%   r&   test_47_verify_and_update  s:   





z*CryptContextTest.test_47_verify_and_updatec           
      C   s  ddl m}m}m} |d}|jddd}|jddd}t||g}| |jt  | 	|
|dd | 	|d| | |d|d | d	g | jt|jddd W d
   n1 sew   Y  | jt|jd|dd | jt|jd|dd t||g}| |jtdg | 	|
|dd | 	|d| | |d|d | 	|
|jdddd | 	|jd|dd | |jd|ddd | d	g | jt|jddd W d
   n1 sw   Y  | jt|jd|dd | jt|jd|dd t||gdd}	| |	jtdg | t|	jd | t|	jd| | t|	jd| | |	jddd| | 	|	jd|dd | |	jd|ddd | |	jd|ddd | |	jd|ddd|f d
S )zLhash(), verify(), and verify_and_update() -- discard unused context keywordsr   )r)   r*   postgres_md5r  root)r   r   r)   )TNr  Nr   )badkwdautor   r=  T)passlib.hashr)   r*   rD  r   r   rX   context_kwdssetr   r   r   r>  r!  rn   ro   )
rU   r)   r*   rD  des_hashpg_root_hashpg_admin_hashr   r   r   r%   r%   r&   test_48_context_kwds  sN   
z%CryptContextTest.test_48_context_kwdsc                 C   sb  t dgdddd}d}|dd}| |jd | |jd | |jd | tgd  |jd	dd	d
}W d   n1 sAw   Y  | |j	ddd|  | g  | |j	dddd|  W d   n1 sow   Y  | |j	dddd|  | |j	dddd|  | tgd  |jt
dd	 dt
dd	 d}W d   n1 sw   Y  | |j	ddd|  | g  | |j	dddd|  W d   n1 sw   Y  | |j	dddd|  | |j	dddd|  | |j	ddd|  tjj}|jd|d> d}| |j	ddd||f  |jddd}| |j	ddd|  | jt|jdd  |jdd  |jdd  | jt|jdd  t dgd!}| jt|jddd" | jtt d#d$ | jtt d#d% | jtt d#d& | jtt d#d  tj }| jtt d|d$ | jtt d|d% | jtt d|d& | jtt d|d  dS )'ztest rounds limitsrA   rE   rD   i	  r3   rK   rJ   rL   z+...........................................N   i  )rK   rJ   rL   r  r  z$5$rounds=1000$nacl$r  r  z$5$rounds=1999$nacl$z$5$rounds=2000$nacl$r   z$5$rounds=2001$nacl$g    eA)rJ   rK   rL   z$5$rounds=999999999$nacl$i  z$5$rounds=3001$nacl$z$5$rounds=3000$nacl$i  z$5$rounds=2999$nacl$z$5$rounds=2500$nacl$r   )rL   rJ   z$5$rounds=%d$nacl$%sr  r:   )rK   rJ   r   r   )rJ   r   )r   _get_recordrX   min_desired_roundsmax_desired_roundsdefault_roundsr!  r
   r   r  intr   rA   rn   r   datetimenowro   )rU   r   STUBcustom_handlerr  dfbadr%   r%   r&   test_50_rounds_limitsQ  s   

z&CryptContextTest.test_50_rounds_limitsc                 C   sx  t dgdddd}| jt|jdd | jt|jdd | jt|jd	d |jd
d}| |ddjd
 | |ddd |jdd}| |ddjd
 | |ddd |jdd}| |ddjd | |ddd |jdd}| |ddjd | |ddd |jdd}| |ddjd | |ddd |jdd}| |ddjd | |ddd dS )ztest linear vary roundsrA   i  i  rE   rO  r   -1%101%r   N0%r   r  r   d   z0.1%gMbP?i  i  100%      ?r   rn   r   r   rX   rR  r   assert_rounds_ranger  r%   r%   r&   test_51_linear_vary_rounds  s4   z+CryptContextTest.test_51_linear_vary_roundsc                 C   s  t dgdddd}| jt|jdd | jt|jdd | jt|jd	d |jd
d}| |ddjd
 | |ddd |jdd}| |ddjd
 | |ddd |jdd}| |ddjd | |ddd |jdd}| |ddjd | |ddd |jdd}| |ddjd | |ddd |jdd}| |ddjd | |ddd |jdd}| |ddjd | |ddd |jdd}| |ddjd | |ddd dS )ztest log2 vary roundsbcrypt   rC      )r3   bcrypt__min_roundsbcrypt__max_roundsbcrypt__default_roundsr^  r   r_  r`  r   Nra  r         rb  z1%g{Gz?z49%g\(\?z50%g      ?rc  rd  re  r  r%   r%   r&   test_52_log2_vary_rounds  s@   z)CryptContextTest.test_52_log2_vary_roundsc                 C   s~   | |}|jdd |j }t }tdD ]}|j||d}	||	j}
||
 q| 	t
||d | 	t||d dS )z2helper to check vary_rounds covers specified ranger   r   i,  rQ  z"vary_rounds had wrong lower limit:z"vary_rounds had wrong upper limit:N)r   default_salt_charsmax_salt_sizerJ  r   r  rh   r  addrX   minmax)rU   contextr   lowerupperr   r  seenir  rr%   r%   r&   rf    s   
z$CryptContextTest.assert_rounds_rangec                 C   sp   t jddtd tdgd}| |jd | |jddjd | |jd	djd | |jd
djd dS )zharden_verify -- parsingrO   z.*harden_verify.*r   rA   r:   N )harden_verifytruefalse)rR   rS   DeprecationWarningr   rX   r}  usingr^   r%   r%   r&   test_harden_verify_parsing-  s   z+CryptContextTest.test_harden_verify_parsingc                 C   sP   d}d}t  }||_t|gd}|  t|j\}}| j|||| d dS )z'
        dummy_verify() method
        r   r   r:   )deltaN)	DelayHashr  delayr   dummy_verifyr   assertAlmostEqual)rU   expectedaccuracyr   r_   elapsed_r%   r%   r&   test_dummy_verify9  s   z"CryptContextTest.test_dummy_verifyc                 C   s  ddd}t ddd}| ||dg d | ||dg d |jd	d
 | ||dg d | ||dg d |jdd | ||dg d | ||dg d t dgdd}| ||ddg | ||ddg | jtt ddd | jtt ddd dS )z+test deprecated='auto' is handled correctlyNc                    r   )Nc                    s   g | ]	} | jqS r%   r   )r.   r   r   r%   r&   r0   Q  s    zLCryptContextTest.test_61_autodeprecate.<locals>.getstate.<locals>.<listcomp>r:   r   r%   r   r&   getstateP  r   z8CryptContextTest.test_61_autodeprecate.<locals>.getstatez sha256_crypt,md5_crypt,des_cryptrG  r   )FTTr   r*   r?   )TFTr)   r   )TTFrA   Fr   zauto,md5_cryptzmd5_crypt,autor   )r   rX   r   rn   r   )rU   r  r_   r%   r%   r&   test_61_autodeprecateM  s&   



z&CryptContextTest.test_61_autodeprecatec                 C   s&  ddl m} ddlm}m} tdg}tddg}|d}|d}| td|j	 | td|j	| | td|j	| |	 }| 
||j |	|}	| 
|	|j|  |	|}
| 
|
|j|  | 
|	|	|	 | || | ||j| | ||j| | ||j|	 | || | ||j| | || | ||	 | ||jd | t|jd	 | 
||| | ||j| | ||j| | ||j|	 | ||jd | t|jd	 | 
||| | ||j| | td
|j| | 
||	| d	S )zdisabled hash supportr   )UnknownHashError)r*   unix_disabledr)   r  foozno disabled hasher presentr|  Nzcannot restore original hash)passlib.excr  rH  r*   r  r   r   assertRaisesRegexru   disablerX   default_markerr   
is_enabledrn   r  enablero   r   )rU   r  r*   r  r_   r   h_refh_otherh_dis	h_dis_refh_dis_otherr%   r%   r&   test_disabled_hashesm  sX   




z%CryptContextTest.test_disabled_hashesr   F)Hr   r   r   descriptionPrefixr   r   r   rZ   r'   r[   r   rj   ri   rq   rr   rz   r{   rt   r   rl   rw   rk   rm   rx   r   r\   r   r}   r]   r~   r  rQ   r`   rp   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r#  r'  r)  r;  rC  rN  r]  rg  rp  rf  r  r  r  r  __classcell__r%   r%   rV   r&   r(   1   s    

	
	
	+%L$\%
$O!
1$
13G:Uv&/ r(   c                   @   s2   e Zd ZdZdZejZdZdZ	e
dZdd ZdS )	r  z-dummy hasher which delays by specified amount
delay_hash(   r   z$x$c                 C   s6   t | j t|tr|d}ttd| 	 S )Nra   s   prefix)
timesleepr  r5  r   rk   r   r4  sha1r6  )rU   r   r%   r%   r&   r7    s   

zDelayHash._calc_checksumN)r   r   r   __doc__r/   r   LOWER_HEX_CHARSchecksum_charschecksum_sizer  r   r8  r7  r%   r%   r%   r&   r    s    r  c                   @   s   e Zd Zd ZdS )dummy_2Nr   r%   r%   r%   r&   r    r   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 d | t d d S )Nr  )unload_handler_name
addCleanuprT   r%   r%   r&   rQ     s   zLazyCryptContextTest.setUpc                 C   st   |  td tdd ttddgdgd}|  tdd | | d | |dj	 | tdd dS )ztest plain kwdsr  passlib.tests.test_contextr)   r   Tr  r)   N)
r  has_crypt_handlerr   r   iterrX   r3   r   r   rH   )rU   r   r%   r%   r&   test_kwd_constructor  s   
z)LazyCryptContextTest.test_kwd_constructorc                    sx     td tdd d
 fdd	}t|dd}  tdd  | d  |d	j  tdd d S )Nr  r  Fc                    s      |  ttddgdgdS )Nr  r)   r   )r   r   r  )flagrT   r%   r&   onload  s   
z>LazyCryptContextTest.test_callable_constructor.<locals>.onloadT)r  r  r  r)   r  )	r  r  r   r   rX   r3   r   r   rH   )rU   r  r   r%   rT   r&   test_callable_constructor  s   
z.LazyCryptContextTest.test_callable_constructorN)r   r   r   r  rQ   r  r  r%   r%   r%   r&   r    s
    r  )Ar  
__future__r   passlib.utils.compatr   configparserr   ConfigParserrW  	functoolsr   logging	getLoggerr   logrq   rR   passlibr   passlib.contextr   r   r  r	   r
   passlib.utilsr   r   r   r   r   r   r   r   passlib.utils.handlersutilsr  r   passlib.tests.utilsr   r   r   r   r   r   passlib.registryr   r   r  r   r  r   rr   abspathdirname__file__r{   r'   r(   r4  r  r   r  r  r  r%   r%   r%   r&   <module>   sN      	             