a
    Oʞh2#                    @   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%   U/wd/webapps/venvs/v2025_4um/lib/python3.9/site-packages/passlib/tests/test_context.pymerge_dicts&   s    
r'   c                       sj  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.d/ Z!d0d1 Z"d2d3 Z#d4d5 Z$d6d7 Z%d8d9 Z&d:d; Z'd<d= Z(d>d? Z)d@dA Z*dBdC Z+dDdE Z,d}dGdHZ-dIdJ Z.dKdL Z/dMdN Z0dOdP Z1dQdR Z2dSdT Z3dFi fdFdUd ifdVi fdWi fgZ4dXdY Z5dZd[ 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>?       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r4   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.cfgiIq  i  iy  iȯ  )Zbsdi_crypt__min_roundsr7   r8   r:   z[passlib]
bsdi_crypt__min_rounds = 29001
bsdi_crypt__max_rounds = 35001
bsdi_crypt__default_rounds = 31001
sha512_crypt__min_rounds = 45000
r,   r5   )r)   r*   phpassr+   sha256_cryptr)   rA         r       H   )r4   
deprecatedr5   r7   r8   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 f i | j}| | | j t f i | j}| | | j t f i | j}| | | j t f i | j}| | | j t tdgd}| | d dS )ztest class constructorrA   r;   )rA   N)	r   assertEqualto_dictsample_1_dictsample_1_resolved_dictsample_2_dictsample_3_dictr   r4   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 f i | j}|jf i | j}| | | j | | | j |jf i | j}| | | j | | | j |jf i | j}| | | j | }| 	|| | | | j | | | j |j
f i | j | | | j | | | j dS )ztest copy() methodN)r   rZ   r   r\   rX   rY   sample_12_dictr]   sample_123_dictZassertIsNotr   )rU   cc1cc2Zcc2bcc3cc4r%   r%   r&   test_04_copy  s     zCryptContextTest.test_04_copyc                 C   s$   t f i | j}| t|d dS )ztest repr()z!^<CryptContext at 0x-?[0-9a-f]+>$N)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 f i | j}|ji dd | | | j t  }|| j || j	 | | | j	 dS )z test load() / load_path() methodra   NT)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  r2   )rL   r6   T)Ztoo__many__key__parts)Z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 f i | j}|  | | | j t f i | j}|jf i | j | | | j |jf i | j | | | j t f i | j}|| j | | | j t f 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;   N)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f i ddd}| |dd tdd}| |dd| | |dd| | |dd| | |f i ddi| | |f i ddi| tdd}| |dd| | |f i ddi| tdd}| |dd| | |dd| | |f i ddi| | |f i ddi| tdd}| |dd| | |f 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 f i |  S r   r   rY   )r"   r%   r%   r&   parse  s    z/CryptContextTest.test_20_options.<locals>.parser2   )__x)Zdefault__scheme__)Z__option)Zdefault____option)Z__scheme__optioni0u  )Z!category__scheme__option__invalidzadmin.context__schemesr*   md5_crypt,des_cryptr)   )r4   zadmin.context__defaultadminr?   )Zcontext__default)Zdefault__context__defaultzcontext.defaultzdefault.context.defaultadmin__context__defaultzadmin.context.defaultr6   )Zdefault__all__vary_roundszall.vary_roundszdefault.all.vary_rounds)admin__all__vary_roundszadmin.all.vary_roundsr@   P)rM   )Zmd5_crypt__identxx)r4   Zdes_crypt__salt)r4   Z	all__saltN)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  s   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     s   r   )Zadmin__context__schemes)r   rX   r4   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                    s    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  s    z3CryptContextTest.test_22_deprecated.<locals>.getdepr*   rH   r)   r;   )rH   r4   r   )r*   r)   rA   r4   rH   )r4   admin__context__deprecated)r4   r5   rH   )r4   r5   r   )r4   r   rH   )r4   r   r   )r4   rH   r   r   {   )rH   r4   r   userr   )N)	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)   )r5   r4   r;   r   )r4   r5      )r5   r4   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   r6   r   )vr%   r%   r&   r     s    z3CryptContextTest.test_24_vary_rounds.<locals>.parser2   z0.1z10%i  Z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  s    z)CryptContextTest.assertHandlerDerivedFromc                 C   s   t  }| | d | |jddd t f i | j}| | t| j | |jdddt| j t|jdd| jD ]\}}| t	|| qt f i | j
}| | d dS )ztest schemes() methodr%   Tresolver   unconfiguredN)r   rX   r4   rZ   tuplesample_1_schemessample_1_handlerszipr   r   r\   )rU   r_   r   Zcorrectr%   r%   r&   test_30_schemes  s    z CryptContextTest.test_30_schemesc                 C   s   t  }| t|j t f i | j}| | d | |jdddtj | |jddtj t f i | j	}| t|j t | j
d}| | d dS )ztest default_scheme() methodr*   Tr   r   r;   r)   N)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   sj  t  }| t|j | t|jd t f 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rf| |jtddd	tj
 | |jtddd	tj dS )ztest handler() methodr*   T)r   r)   mysql323sha256_crypt,md5_cryptr   staff)r   r   r   r   N)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   )_configZ_get_record_options_with_flag)r_   schemer   r%   r%   r&   options  s    z1CryptContextTest.test_33_options.<locals>.optionsT)r,   r)   r+   r,   r)   r2   皙?i N  r+   皙?g333333?r3   )
truncate_errorr4   rH   r6   rI   r9   r   r   Zadmin__bsdi_crypt__vary_roundsZadmin__sha512_crypt__max_rounds)r   )rH   vary_rounds
max_roundsr   r   )r   r   )rH   r   )r   )r   rH   )N)r   rX   r   
categoriesr   )rU   r   r   r%   r%   r&   test_33_options  sd    

z CryptContextTest.test_33_optionsc                 C   s<   t f i | j}| | | j | |jdd| j dS )ztest to_dict() methodTr   N)r   rZ   rX   rY   r[   r^   r%   r%   r&   test_34_to_dictC  s    z CryptContextTest.test_34_to_dictc                 C   s   t f 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 PasslibN)r   rZ   r   r   rX   ri   rh   rY   replaceZ!passlib.tests.test_utils_handlersr   r   )rU   r_   dumpctx2otherr   Z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 ]z}|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   r8   testr   Tr   r   notestr*   secretr)   N)r   r*   r)   r+   r   r/   rX   identifyr   r   verifyassertFalsegenhash	genconfigrn   r   )rU   handlersr   Z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r|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   )r4   rM   rN   Z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 r`qN| jt|jd|fi | qNt 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 f i | j}|d}| |d | |d| | |d| | jt|j	dd t dg}| j
D ] \}}| jt|j|fi | qp| tt  jd | jt|jddd	 d
S )ztest hash() methodpasswordz$5$rounds=3000$Z	passwordxi  rL   r)   r   r   r   N)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_hashFc              	   C   sP  t f i | j}| dg( | |jddddd W d   n1 sJ0    Y  | dg* | |jddddd	d
 W d   n1 s0    Y  | dg( | |jddddd W d   n1 s0    Y  | dg( | |jddddd W d   n1 s0    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   assertWarningListrX   r   rn   r   ro   )rU   Zuse_16_legacyr   r%   r%   r&   test_43_hash_legacy  s.    """$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 | qJt  }| |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 ]2\}}|du rq | 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 f 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() methodZ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   Z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   )hashlibr  
isinstancer   rk   r   	hexdigest)rU   r   r  r%   r%   r&   _calc_checksum  s    

zCCryptContextTest.test_46_needs_update.<locals>.dummy._calc_checksum)N)r   r   r   r/   _hash_prefixclassmethodr  r  r%   r  r%   r&   r    s
   r  r   NZbob)r   r  r)   r   r   r   r   r   )r   r   r   r  r   r   r   r   rX   r   rn   ro   r   )rU   r   r  r_   r   r	  r"   r%   r  r&   test_46_needs_update{  s6    




z%CryptContextTest.test_46_needs_updatec           
      C   s  t f 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 |	 q| ||dd t dg}| j	D ]2\}}	|du rJq4| j
t|jd|fi |	 q4| 
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   Z	wrongpassNr   FNr   r   r   r   r   r   )r   r   r   r   verify_and_updater   r  r   r   r   rn   ro   rX   r   )
rU   r   Zh1Zh2okZ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 s0    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 s0    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   )Zbadkwdautor   r  T)passlib.hashr)   r*   r  r   r   rX   Zcontext_kwdssetr   r   r   r  r  rn   ro   )
rU   r)   r*   r  Zdes_hashZpg_root_hashZpg_admin_hashr   r   r   r%   r%   r&   test_48_context_kwds  sJ    
24z%CryptContextTest.test_48_context_kwdsc                 C   sf  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 s0    Y  | |j	ddd|  | g * | |j	dddd|  W d   n1 s0    Y  | |j	dddd|  | |j	dddd|  | tgd 0 |jt
dd	 dt
dd	 d}W d   n1 sd0    Y  | |j	ddd|  | g * | |j	dddd|  W d   n1 s0    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	  r4   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   Zmin_desired_roundsZmax_desired_roundsZdefault_roundsr  r
   r   r   intr   rA   rn   r   datetimenowro   )rU   r   ZSTUBZcustom_handlerr   Z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   r$  r   -1%101%r   N0%r   r   r  d   z0.1%gMbP?i  i  100%      ?r   rn   r   r   rX   r'  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 roundsZbcrypt   rC      )r4   Zbcrypt__min_roundsZbcrypt__max_roundsZbcrypt__default_roundsr-  r   r.  r/  r   Nr0  r         r1  z1%g{Gz?z49%g\(\?z50%g      ?r2  r3  r4  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,  r&  z"vary_rounds had wrong lower limit:z"vary_rounds had wrong upper limit:N)r   Zdefault_salt_charsZmax_salt_sizer"  r   r   rh   r   addrX   minmax)rU   contextr   lowerupperr   r   seenir   rr%   r%   r&   r5    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   rF  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)	DelayHashrJ  delayr   Zdummy_verifyr   ZassertAlmostEqual)rU   expectedZ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                    s    fdd  D S )Nc                    s   g | ]} | jqS r%   r   )r.   r   r   r%   r&   r0   Q  r1   zLCryptContextTest.test_61_autodeprecate.<locals>.getstate.<locals>.<listcomp>r;   r   r%   r   r&   getstateP  s    z8CryptContextTest.test_61_autodeprecate.<locals>.getstatez sha256_crypt,md5_crypt,des_cryptr   r   )FTTr   r*   r?   )TFTr)   r   )TTFrA   Fr   zauto,md5_cryptzmd5_crypt,auto)N)r   rX   r   rn   r   )rU   rS  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)   rV  Zfoozno disabled hasher presentrE  Nzcannot restore original hash)passlib.excrU  r!  r*   rV  r   r   assertRaisesRegexru   disablerX   Zdefault_markerr   
is_enabledrn   r   enablero   r   )rU   rU  r*   rV  r_   r   Zh_refZh_otherZh_disZ	h_dis_refZh_dis_otherr%   r%   r&   test_disabled_hashesm  sX    




z%CryptContextTest.test_disabled_hashes)N)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  r  r#  r,  r6  r;  r5  rK  rR  rT  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 )	rM  z-dummy hasher which delays by specified amountZ
delay_hash(   r   z$x$c                 C   s6   t | j t|tr |d}ttd| 	 S )Nra   s   prefix)
timesleeprN  r  r   rk   r   r  sha1r  )rU   r   r%   r%   r&   r    s    

zDelayHash._calc_checksumN)r   r   r   __doc__r/   r   ZLOWER_HEX_CHARSZchecksum_charsZchecksum_sizerN  r   r  r  r%   r%   r%   r&   rM    s   rM  c                   @   s   e Zd Zd ZdS )dummy_2Nr   r%   r%   r%   r&   rd    s   rd  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 )Nrd  )unload_handler_nameZ
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 kwdsrd  passlib.tests.test_contextr)   r   Trd  r)   N)
r   has_crypt_handlerr   r   iterrX   r4   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 )Nrd  rg  Fc                    s      |  ttddgdgdS )Nrd  r)   r   )r   r   rj  )flagrT   r%   r&   onload  s    
z>LazyCryptContextTest.test_callable_constructor.<locals>.onloadT)rm  rl  rh  r)   )F)	r   ri  r   r   rX   r4   r   r   rH   )rU   rm  r   r%   rT   r&   test_callable_constructor  s    
z.LazyCryptContextTest.test_callable_constructorN)r   r   r   r]  rQ   rk  rn  r%   r%   r%   r&   re    s   re  )Arc  
__future__r   Zpasslib.utils.compatr   configparserr   ConfigParserr)  	functoolsr   logging	getLoggerr   logrq   rR   Zpasslibr   Zpasslib.contextr   r   rW  r	   r
   Zpasslib.utilsr   r   r   r   r   r   r   r   Zpasslib.utils.handlersutilsr   r   Zpasslib.tests.utilsr   r   r   r   r   r   Zpasslib.registryr   r   ri  r   rf  r   rr   abspathdirname__file__r{   r'   r(   r  r`  r   rM  rd  re  r%   r%   r%   r&   <module>   sL     	             