
    XR_br                     R   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 n# e	$ r dZY nw xY 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c mZ dd	lmZmZ dd
lmZmZmZ  ee           Z! G d de          Z" G d de          Z# G d dej$                  Z% G 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            
          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          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          ZdZ edddd          Z eg ddd	ddddd          Z ed          Z eg ddd	ddddd          ZdZ edgd	ddd          Zedz   Z e!                                Z"e"#                    dgdd g!           e!                                Z$e$#                    dd"d#           e!                                Z%e%#                    dgdd gd!           d$ Z&d% Z'd& Z(d' Z)d( Z*d) Z+d* Z,d+ Z-d, Z.d- Z/d. Z0d/ Z1d0 Z2d1 Z3d2 Z4d3 Z5d4 Z6d5 Z7d6S )7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                     t          j        |            t          j        dd           t          j        dd           t          j        dd           t          j        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selfs    G/usr/lib/python3/dist-packages/passlib/tests/test_context_deprecated.pyr3   zCryptPolicyTest.setUp   sz    t L	N 	N 	N O	Q 	Q 	Q*LMMM*KLLLLL    c                    t          di | j        }|                     |                                | j                   t          | j                  }|                     |                                | j                   |                     t
          t           i i            |                     t
          t           i d           |                     t
          t           g dd            G d dt          j                  }|                     t          t           |g           |                     t
          t           t          j        g            G d	 d
t          j                  }|                     t          t           ||g           |                     t          t           dgdg           |                     t          t           dgd           dS )ztest CryptPolicy() constructor   )dummyr   r   )r    bad__key__bsdi_crypt__max_roundsc                       e Zd ZdZdS )5CryptPolicyTest.test_00_constructor.<locals>.namelessN__name__
__module____qualname__name r9   r8   namelessr?      s        DDDr9   rF   r   c                       e Zd ZdZdS )4CryptPolicyTest.test_00_constructor.<locals>.dummy_1dummy_1Nr@   rE   r9   r8   rJ   rI      s        DDDr9   rJ   r   r   r   r-   r   r   NrE   )
r   sample_config_1pdassertEqualto_dictassertRaises	TypeErroruhStaticHandler
ValueErrorKeyError)r7   policyrF   rJ   s       r8   test_00_constructorz#CryptPolicyTest.test_00_constructor   s   66t566))4+ABBBT344))4+ABBB)["b999)["A>>> 	)[OOO/4 	 	 	 		 	 	 	 	r' 	 	 	*kH:FFF 	)[2;K:LMMM	 	 	 	 	b& 	 	 	(K'79KLLL 	(K#.-&1] 	 	4 	4 	4
 	(K#.-"- 	 	/ 	/ 	/ 	/ 	/r9   c                     | j         }t          j        |          }|                     |                                | j                   |                     t          t          j        |dz              dS )z(test CryptPolicy.from_path() constructorxxxN)sample_config_1s_pathr   	from_pathrN   rO   rM   rP   EnvironmentError)r7   pathrV   s      r8   test_01_from_path_simplez(CryptPolicyTest.test_01_from_path_simple  sg    
 )&t,,))4+ABBB 	*K,A4%<PPPPPr9   c                    |                                  }t          || j                   t          j        |          }|                     |                                | j                   t          || j                            dd                     t          j        |          }|                     |                                | j                   t          | j        dd          }t          ||           t          j        |d          }|                     |                                | j                   dS )z7test CryptPolicy.from_path() constructor with encodings

utf-16utf-8source_encodingencodingN)
mktempr   sample_config_1sr   r[   rN   rO   rM   replacer
   )r7   r]   rV   uc2s       r8   test_01_from_pathz!CryptPolicyTest.test_01_from_path  s   {{}} 	t,---&t,,))4+ABBB 	t,44T&AABBB&t,,))4+ABBB t,hPPPs&th???))4+ABBBBBr9   c                 0   t          j        | j                  }|                     |                                | j                   t          j        | j                            dd                    }|                     |                                | j                   t          | j                  }t          j        |          }|                     |                                | j                   t          | j        dd          }t          j        |d          }|                     |                                | j                   t          j        | j	                  }|                     |                                | j
                   dS )z*test CryptPolicy.from_string() constructorr`   ra   rb   rc   rd   rf   N)r   from_stringri   rN   rO   rM   rj   r   r
   sample_config_4ssample_config_4pd)r7   rV   datark   s       r8   test_02_from_stringz#CryptPolicyTest.test_02_from_string"  s[    ()>??))4+ABBB (!))$v668 8))4+ABBB $/00(..))4+ABBB t,hPPP(x@@@))4+ABBB ()>??))4+ABBBBBr9   c                    t          j        | j                  }|                     |                                | j                   t          j        | j                  }|                     |                                | j                   t          j        | j                                                  }|                     |                                | j                   t          j        |          }|                     ||           | 	                    t          t           j        d           | 	                    t          t           j        g            dS )z*test CryptPolicy.from_source() constructorr;   N)r   from_sourcerZ   rN   rO   rM   ri   copyassertIsrP   rQ   )r7   rV   p2s      r8   test_03_from_sourcez#CryptPolicyTest.test_03_from_source;  s    ()CDD))4+ABBB ()>??))4+ABBB ()?)D)D)F)FGG))4+ABBB $V,,fb!!! 	)[%<a@@@)[%<bAAAAAr9   c                    |                      t          t          j        g            t          j        | j        g          }|                     |                                | j                   t          j        | j        | j	        | j
        g          }|                     |                                | j                   dS )z+test CryptPolicy.from_sources() constructorN)rP   rT   r   from_sourcesri   rN   rO   rM   rZ   sample_config_2ssample_config_3pdsample_config_123pd)r7   rV   s     r8   test_04_from_sourcesz$CryptPolicyTest.test_04_from_sourcesQ  s     	*k&>CCC )4+@*ABB))4+ABBB )&!"  	))4+CDDDDDr9   c                    t          di | j        } |j        di | j        }|                     |                                | j                    |j        di | j        }|                     |                                | j                   |                    | j                  }|                     |                                | j                   dS )z&test CryptPolicy.replace() constructorNrE   )	r   rM   rj   sample_config_2pdrN   rO   sample_config_12pdr|   r}   )r7   p1rw   p2bp3s        r8   test_05_replacezCryptPolicyTest.test_05_replaced  s     224122 RZ11$011t'>??? bj224122(?@@@ ZZ.//t'?@@@@@r9   c                     |                      t          t          dgd           |                      t          t          dgd           |                      t          t          dgdg           dS )z!test CryptPolicy() forbidden kwdsr   xx)r   des_crypt__salt)r   	all__saltr   )r   user__context__schemesN)rP   rU   r   r6   s    r8   test_06_forbiddenz!CryptPolicyTest.test_06_forbiddenu  s     	(K M  	 	
 	
 	
 	(K M 	 	
 	
 	
 	(K M$/= 	 	
 	
 	
 	
 	
r9   c                     t          di | j        }|                     |                                           t          di | j        }|                     |                                            dS )ztest has_schemes() methodNrE   )r   rM   
assertTruehas_schemesr|   )r7   r   r   s      r8   test_10_has_schemesz#CryptPolicyTest.test_10_has_schemes  sq     224122(()))224122BNN,,,-----r9   c                 8   t          di | j        }| j        d         }|                     t	          |                                          |           t          di | j        }|                     t	          |                                          g            dS )ztest iter_handlers() methodr   NrE   )r   rM   sample_config_1prdrN   listiter_handlersr|   )r7   r   sr   s       r8   test_11_iter_handlersz%CryptPolicyTest.test_11_iter_handlers  s     224122#I.b..00111555224122b..0011266666r9   c                    t          di | j        }|                     |                    d          t          j                   |                     |                    d          d           |                     t          |j        dd           |                     |                                t          j                   dS )ztest get_handler() methodr   sha256_cryptNTrequiredrE   )	r   rM   rv   get_handlerr   r   rP   rU   r   )r7   r   s     r8   test_12_get_handlerz#CryptPolicyTest.test_12_get_handler  s     224122 	bnn\22DODDD 	bnn^44d;;;(BNNTRRR 	bnn&&77777r9   c           	         t          di | j        }|                     |                    d          t	          dddd                     |                     |                    d          t	          ddd	
                     t          j        | j                  }|                     |                    d          t	          dd                     |                     |                    dd          t	          dd                     |                     |                    dd          t	          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   NrE   )r   r   rN   get_optionsdictrn   ro   )r7   p12p4s      r8   test_13_get_optionsz#CryptPolicyTest.test_13_get_options  s    44D34466t"8
 8
 8
 	 	 	 	88	:
 :
 :
 	 	 	 $T%:;;77:
 :
 :
 	 	 	 	??B
 B
 B
 	 	 	 	@@$C
 C
 C
 	 	 	 	 	r9   c                 
   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S )
z#test handler_is_deprecated() methodr   r   r   r   r   r   )r   r-   user__context__deprecatedNrE   )r   rM   sample_config_5pdassertFalsehandler_is_deprecatedr   r   r   )r7   papbpcs       r8   test_14_handler_is_deprecatedz-CryptPolicyTest.test_14_handler_is_deprecated  s@   22412222412211+>>???11$/BBCCC11.AABBB00==>>>11$/BBCCC11.AABBB 	00fEEFFF11,GGHHH00gFFGGG00wGGHHH  +.#}'2m
 
 

 	00==>>>11+>>???11+vFFGGG00fEEFFFFFr9   c                    t          j        dt                     t                      }|                     |                                d           |                     |                    d          d           |                    d          }|                     |                                d           |                     |                    d          d           dS )z!test get_min_verify_time() methodr0   )categoryr   r   r   )min_verify_timeN)r4   r5   DeprecationWarningr   rN   get_min_verify_timerj   )r7   r   r   s      r8   test_15_min_verify_timez'CryptPolicyTest.test_15_min_verify_time  s     	3EFFFF]]//111555//88!<<<ZZZ++//111555//88!<<<<<r9   c                    t          di | j        }|                     t          |                                          | j                   |                     t          |                    d                    | j                   |                     t          |                    d                    | j                   dS )ztest iter_config() methodTresolve)iniNrE   )r   r   rN   r   iter_configsample_config_5prdsample_config_5pidr7   p5s     r8   test_20_iter_configz#CryptPolicyTest.test_20_iter_config  s    224122bnn..//1GHHHbnnTn::;;T=TUUUbnnn66779PQQQQQr9   c                     t          di | j        }|                     |                                | j                   |                     |                    d          | j                   dS )ztest to_dict() methodTr   NrE   )r   r   rN   rO   r   r   s     r8   test_21_to_dictzCryptPolicyTest.test_21_to_dict	  sf    224122t'=>>>D1143JKKKKKr9   c                 4   t          di | j        }|                                }t          j        |          }|                     |                                | j                   |                    d          }|                     |t                     dS )ztest to_string() methodzlatin-1rf   NrE   )r   r   	to_stringrn   rN   rO   assertIsInstancebytes)r7   r   r   r   s       r8   test_22_to_stringz!CryptPolicyTest.test_22_to_string  s    224122LLNN$Q''t'=>>>LL)L,,a'''''r9   N)8rA   rB   rC   __doc__descriptionPrefixri   osr]   abspathjoindirname__file__rZ   existsr   
startswithr   rM   sample_config_1pidr   r   r   r   r   r   r{   r   r   r|   r}   ro   rp   sample_config_5sru   r   updater   r   r3   rW   r^   rl   rr   rx   r~   r   r   r   r   r   r   r   r   r   r   r   rE   r9   r8   r   r   %   s@       !! &	 GOOBGLL
!!#9-; -; < <7>>/00 J5F J 1 1/2H!J !J &&';<<<<<KKK!&%*#(#(	 	 	 D!&%*#(#(	 	 NDNDOTEVW!&%*#(#(	 	 	 !&!&%*!&   KKK!&!&%*#(!&
 
 
    $KKK !&!&%*#(!&
 
 
 "$#("&*/
 
 
 ( + 
 *..00"_'2L%B    
 ,0022!$;    
 ,0022 #m'2L&A	    M M M&/ &/ &/P
Q 
Q 
QC C C(C C C2B B B,E E E&A A A"
 
 
,. . .7 7 78 8 8% % %NG G G<= = = R R RL L L( ( ( ( (r9   r   c                   l    e Zd ZdZdZd Zd Zd Zd Z e	g ddgd	d
ddddddd          Z
d Zd ZdS )CryptContextTestztest CryptContext classr   c                     t          j        |            t          j        dd           t          j        dd           t          j        dd           t          j        dd           d S )Nr0   z3CryptContext\(\)\.replace\(\) has been deprecated.*z9The CryptContext ``policy`` keyword has been deprecated.*=.*(CryptPolicy|context\.policy).*(has|have) been deprecated.*r1   r2   r6   s    r8   r3   zCryptContextTest.setUp$  s    t V	X 	X 	X \	^ 	^ 	^*ijjj O	Q 	Q 	Q 	Q 	Qr9   c                    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          |          }|                     |	                                |	                                           t          |d          }| 
                    |	                                |	                                           |                     |	                                t          g dd                     |                     t          t          |dd           |                     t          t           d           dS )	ztest constructorr   r   r   )rV   r   )rV   r   rL   rV   Nx)r   r   r   r   r   rV   r   rv   rN   rO   assertNotEqualr   rP   rQ   setattr)r7   cccbarV   s         r8   rW   z$CryptContextTest.test_00_constructor1  s    4>4?DNKLL	''))!Aa(((a)))a((( BBBCC	''))!Aa(((a)))a((( (((v~~'7'7888>>>BJJLL&..*:*:;;;t4Z4Z4Z4@(B (B (B 	C 	C 	C 	)Wb(DAAA)\#>>>>>r9   c                    t          g d          }|                     |j                                        t          j                   |                                }|                     ||           |                    d          }|                     ||           |                     |j                                        t          j                   dS )ztest replace()r   r   r(   N)	r   rv   rV   r   r   r   rj   assertIsNotr   )r7   r   cc2cc3s       r8   test_01_replacez CryptContextTest.test_01_replaceN  s     BBBCCbi++--t~>>>jjllb!!! jjj..b!!! 	cj,,..@@@@@r9   c                    t                      }|                     t          |j        dd           |                     t          |j        d           |                     t          |j        dd           t          dg          }t          g           }||_        |                     t          |j        dd           |                     t          |j        d           |                     t          |j        dd           dS )ztest no handlersr   Tr   secretr   rG   N)r   rP   rU   identifyr   verifyr   rV   )r7   r   ps      r8   test_02_no_handlersz$CryptContextTest.test_02_no_handlers_  s     ^^(BK$GGG(BGX666(BIx@@@ ;-((###	(BK$GGG(BGX666(BIx@@@@@r9   )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                    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/2hdFpxA3fANrE   )r   sample_policy_1r   hash_needs_updater   r7   r   s     r8   test_12_hash_needs_updatez*CryptContextTest.test_12_hash_needs_update  s    11D011 	,,_==>>>--.RSSTTT 	,,-z{{|||--.{||}}} 	--.{||}}},,-z{{|||||r9   c                    t          j        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 |  t          dg          }|                     t          |j        d           dS )	z'test non-string hash values cause errorr0   z0.*needs_update.*'scheme' keyword is deprecated.*r   Nschemer;   rE   mysql323)r4   r5   r   rP   rQ   r   )r7   r   r   kwdsr   s        r8   test_30_nonstring_hashz'CryptContextTest.test_30_nonstring_hash  s    *\]]] ;-((r
+./BR 	M 	MJD$ Di)=tLLtLLLLJ<(()S%:DAAAAAr9   N)rA   rB   rC   r   r   r3   rW   r   r   r   r   r   r  rE   r9   r8   r   r      s        !!&Q Q Q? ? ?:A A A"A A A* d' ' '&)$%')+&''+'++/%&  O} } }&B B B B Br9   r   c                       e Zd Zd ZdS )dummy_2Nr@   rE   r9   r8   r  r    s        DDDr9   r  c                   $    e Zd ZdZd Zd Zd ZdS )LazyCryptContextTestr	   c                     t          j        |            t          d           |                     t          d           t	          j        dd           t	          j        dd           d S )Nr  r0   z1CryptContext\(\)\.replace\(\) has been deprecatedr   )r   r3   unload_handler_name
addCleanupr4   r5   r6   s    r8   r3   zLazyCryptContextTest.setUp  sp    t 	I&&&+Y777 	 T	V 	V 	V*ijjjjjr9   c                    |                      t          d                     t          dd           t          t	          ddg          dg          }|                      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   rV   r   rN   r   r   s     r8   test_kwd_constructorz)LazyCryptContextTest.test_kwd_constructor  s    *955666#I/KLLLdI{#;<<+WWW*9d;;<<<	77DDEEE**,,y+.FGGG))T::;;;;;r9   c                                           t          d                     t          dd           d	 fd	}t          |d          }                      t          dd                                          |j                            d                                          |j                                        ddg                                t          dd                     dS )
z0test create_policy() hook, returning CryptPolicyr  r
  Fc                 p                         |            t          t          ddg          dg          S )Nr  r   rK   )r   r   r  )flagr7   s    r8   create_policyzELazyCryptContextTest.test_callable_constructor.<locals>.create_policy  s9    OOD!!!tY,D'E'ES^R_````r9   T)r  r  r   N)F)	r   r  r   r	   r   rV   r   rN   r   )r7   r  r   s   `  r8   test_callable_constructorz.LazyCryptContextTest.test_callable_constructor  s    *955666#I/KLLL	a 	a 	a 	a 	a 	a MEEE*9d;;<<<	77DDEEE**,,y+.FGGG))T::;;;;;r9   N)rA   rB   rC   r   r3   r  r  rE   r9   r8   r  r    sJ        *
k 
k 
k< < << < < < <r9   r  )'r   
__future__r   loggingr   r   r4   pkg_resourcesr   ImportErrorpasslibr   passlib.contextr   r   r	   passlib.utilsr
   r   passlib.utils.handlersutilshandlersrR   passlib.tests.utilsr   r   passlib.registryr   r   r  r   r  rA   logr   r   rS   r  r  rE   r9   r8   <module>r      s    & % % % % %       				 ///////          G G G G G G G G G G . . . . . . . . # # # # # # # # # 2 2 2 2 2 2 2 2         
 i
r( r( r( r( r(h r( r( r(vJB JB JB JB JBx JB JB JBf    b   -< -< -< -< -<8 -< -< -< -< -<s    ))