
    gbr                     <   S r SSKJr  SSKJr  SSKrSSKr SSKJr  SSK
Jr  SSKJrJrJr  SSKJrJr  SSKJs  Jr  SS	KJrJr  SS
KJrJrJr  \" \ 5      r! " S S\5      r" " S S\5      r# " S S\RH                  5      r% " S S\5      r&g! \	 a    Sr N|f = f)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                      \ rS rSrSrSrSr\R                  R                  \R                  R                  \R                  R                  \5      S5      5      r\R                  R                  \5      (       d  \(       a	  \" SS5      r\R!                  S5      (       d   e\" / SQS	S
SSSSS9rSS	S
SSSSS.r\" \R*                  \R,                  \R.                  \R0                  /S	S
SSSSS9rSr\" SSSSS9r\" / SQS	S
SSSSSS9r\" SS9r\" / SQSS
SSSSSS9rSr\" S/S
SSSS9r \S-   r!\RE                  5       r#\#RI                  S /S S!/S"9  \RE                  5       r%\%RI                  S S#S$.5        \RE                  5       r&\&RI                  S /S S!/S".5        S% r'S& r(S' r)S( r*S) r+S* r,S+ r-S, r.S- r/S. r0S/ r1S0 r2S1 r3S2 r4S3 r5S4 r6S5 r7S6 r8S7r9g8)9CryptPolicyTest%   z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                     [         R                  " U 5        [        R                  " SS5        [        R                  " SS5        [        R                  " SS5        [        R                  " SS5        g )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    ^/home/matz/Project1/venv/lib/python3.13/site-packages/passlib/tests/test_context_deprecated.pyr4   CryptPolicyTest.setUp   sX    t L	N O	Q*LM*KL    c                 .   [        S0 U R                  D6nU R                  UR                  5       U R                  5        [        U R                  5      nU R                  UR                  5       U R                  5        U R	                  [
        [         0 0 5        U R	                  [
        [         0 SS9  U R	                  [
        [         / SQSS9   " S S[        R                  5      nU R	                  [        [         U/S9  U R	                  [
        [         [        R                  /S9   " S	 S
[        R                  5      nU R	                  [        [         X3/S9  U R	                  [        [         S/S/S9  U R	                  [        [         S/SS9  g)ztest CryptPolicy() constructor   )dummyr   r   )r    bad__key__bsdi_crypt__max_roundsc                       \ rS rSrSrSrg)5CryptPolicyTest.test_00_constructor.<locals>.nameless   N __name__
__module____qualname____firstlineno__name__static_attributes__rC   r;   r9   namelessrA      s    Dr;   rK   r   c                       \ rS rSrSrSrg)4CryptPolicyTest.test_00_constructor.<locals>.dummy_1   dummy_1rC   NrD   rC   r;   r9   rP   rN      s    Dr;   rP   r   r   r   r.   r   r   NrC   )
r   sample_config_1pdassertEqualto_dictassertRaises	TypeErroruhStaticHandler
ValueErrorKeyError)r8   policyrK   rP   s       r9   test_00_constructor#CryptPolicyTest.test_00_constructor   s`   6t556)4+A+ABT334)4+A+AB)["b9)["A> 	)[O/4 	 		r'' 	*kH:F 	)[2;K;K:LM	b&& 	(K'9KL 	(K#.-&1] 	 	4
 	(K#.-"- 	 	/r;   c                     U R                   n[        R                  " U5      nU R                  UR	                  5       U R
                  5        U R                  [        [        R                  US-   5        g)z(test CryptPolicy.from_path() constructorxxxN)sample_config_1s_pathr   	from_pathrT   rU   rS   rV   EnvironmentError)r8   pathr\   s      r9   test_01_from_path_simple(CryptPolicyTest.test_01_from_path_simple  s[    
 ))&&t,)4+A+AB 	*K,A,A4%<Pr;   c                 V   U R                  5       n[        XR                  5        [        R                  " U5      nU R                  UR                  5       U R                  5        [        XR                  R                  SS5      5        [        R                  " U5      nU R                  UR                  5       U R                  5        [        U R                  SSS9n[        X5        [        R                  " USS9nU R                  UR                  5       U R                  5        g)z7test CryptPolicy.from_path() constructor with encodings

utf-16utf-8source_encodingencodingN)
mktempr   sample_config_1sr   rb   rT   rU   rS   replacer
   )r8   rd   r\   uc2s       r9   test_01_from_path!CryptPolicyTest.test_01_from_path  s    {{} 	,,-&&t,)4+A+AB 	,,44T&AB&&t,)4+A+AB t,,hP&&th?)4+A+ABr;   c                 2   [         R                  " U R                  5      nU R                  UR	                  5       U R
                  5        [         R                  " U R                  R                  SS5      5      nU R                  UR	                  5       U R
                  5        [        U R                  5      n[         R                  " U5      nU R                  UR	                  5       U R
                  5        [        U R                  SSS9n[         R                  " USS9nU R                  UR	                  5       U R
                  5        [         R                  " U R                  5      nU R                  UR	                  5       U R                  5        g)z*test CryptPolicy.from_string() constructorrh   ri   rj   rk   rl   rn   N)r   from_stringrq   rT   rU   rS   rr   r   r
   sample_config_4ssample_config_4pd)r8   r\   datars   s       r9   test_02_from_string#CryptPolicyTest.test_02_from_string"  s5    (()>)>?)4+A+AB ((!!))$v68)4+A+AB $//0((.)4+A+AB t,,hP((x@)4+A+AB (()>)>?)4+A+ABr;   c                    [         R                  " U R                  5      nU R                  UR	                  5       U R
                  5        [         R                  " U R                  5      nU R                  UR	                  5       U R
                  5        [         R                  " U R
                  R                  5       5      nU R                  UR	                  5       U R
                  5        [         R                  " U5      nU R                  X5        U R                  [        [         R                  S5        U R                  [        [         R                  / 5        g)z*test CryptPolicy.from_source() constructorr=   N)r   from_sourcera   rT   rU   rS   rq   copyassertIsrV   rW   )r8   r\   p2s      r9   test_03_from_source#CryptPolicyTest.test_03_from_source;  s     (()C)CD)4+A+AB (()>)>?)4+A+AB (()?)?)D)D)FG)4+A+AB $$V,f! 	)[%<%<a@)[%<%<bAr;   c                    U R                  [        [        R                  / 5        [        R                  " U R                  /5      nU R                  UR                  5       U R                  5        [        R                  " U R                  U R                  U R                  /5      nU R                  UR                  5       U R                  5        g)z+test CryptPolicy.from_sources() constructorN)rV   rZ   r   from_sourcesrq   rT   rU   rS   ra   sample_config_2ssample_config_3pdsample_config_123pd)r8   r\   s     r9   test_04_from_sources$CryptPolicyTest.test_04_from_sourcesQ  s     	*k&>&>C ))4+@+@*AB)4+A+AB ))&&!!"" 	)4+C+CDr;   c                    [        S0 U R                  D6nUR                  " S0 U R                  D6nU R	                  UR                  5       U R                  5        UR                  " S0 U R                  D6nU R	                  UR                  5       U R                  5        UR                  U R                  5      nU R	                  UR                  5       U R                  5        g)z&test CryptPolicy.replace() constructorNrC   )	r   rS   rr   sample_config_2pdrT   rU   sample_config_12pdr   r   )r8   p1r   p2bp3s        r9   test_05_replaceCryptPolicyTest.test_05_replaced  s     24112 ZZ1$001t'>'>? jj24112(?(?@ ZZ../t'?'?@r;   c                     U R                  [        [        S/SS9  U R                  [        [        S/SS9  U R                  [        [        S/S/S9  g)z!test CryptPolicy() forbidden kwdsr   xx)r   des_crypt__salt)r   	all__saltr   )r   user__context__schemesN)rV   r[   r   r7   s    r9   test_06_forbidden!CryptPolicyTest.test_06_forbiddenu  sg     	(K M  	 	
 	(K M 	 	
 	(K M$/= 	 	
r;   c                     [        S0 U R                  D6nU R                  UR                  5       5        [        S0 U R                  D6nU R                  UR                  5       (       + 5        g)ztest has_schemes() methodNrC   )r   rS   
assertTruehas_schemesr   )r8   r   r   s      r9   test_10_has_schemes#CryptPolicyTest.test_10_has_schemes  sS     24112()24112BNN,,-r;   c                    [        S0 U R                  D6nU R                  S   nU R                  [	        UR                  5       5      U5        [        S0 U R                  D6nU R                  [	        UR                  5       5      / 5        g)ztest iter_handlers() methodr   NrC   )r   rS   sample_config_1prdrT   listiter_handlersr   )r8   r   sr   s       r9   test_11_iter_handlers%CryptPolicyTest.test_11_iter_handlers  sv     24112##I.b..011524112b..0126r;   c                 j   [        S0 U R                  D6nU R                  UR                  S5      [        R
                  5        U R                  UR                  S5      S5        U R                  [        UR                  SSS9  U R                  UR                  5       [        R                  5        g)ztest get_handler() methodr   sha256_cryptNTrequiredrC   )	r   rS   r   get_handlerr   r   rV   r[   r   )r8   r   s     r9   test_12_get_handler#CryptPolicyTest.test_12_get_handler  s     24112 	bnn\2DOOD 	bnn^4d;(BNNNTR 	bnn&7r;   c           
         [        S0 U R                  D6nU R                  UR                  S5      [	        SSSSS95        U R                  UR                  S5      [	        SSS	S
95        [         R
                  " U R                  5      nU R                  UR                  S5      [	        SSS95        U R                  UR                  SS5      [	        SSS95        U R                  UR                  SS5      [	        SSS95        g)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   NrC   )r   r   rT   get_optionsdictrw   rx   )r8   p12p4s      r9   test_13_get_options#CryptPolicyTest.test_13_get_options  s    4D3346t"8
 	 	8	:
 	 $$T%:%:;7:
 	 	?B
 	 	@$C
 	r;   c                 :   [        S	0 U R                  D6n[        S	0 U R                  D6nU R                  UR	                  S5      5        U R                  UR	                  [
        R                  5      5        U R                  UR	                  S5      5        U R                  UR	                  S5      5        U R                  UR	                  [
        R                  5      5        U R                  UR	                  S5      5        U R                  UR	                  SS5      5        U R                  UR	                  SS5      5        U R                  UR	                  SS5      5        U R                  UR	                  SS5      5        [        SS/S/S/S9nU R                  UR	                  S5      5        U R                  UR	                  S5      5        U R                  UR	                  SS5      5        U R                  UR	                  SS5      5        g)
z#test handler_is_deprecated() methodr   r   r   r   r   r   )r   r.   user__context__deprecatedNrC   )r   rS   sample_config_5pdassertFalsehandler_is_deprecatedr   r   r   )r8   papbpcs       r9   test_14_handler_is_deprecated-CryptPolicyTest.test_14_handler_is_deprecated  s   241122411211+>?11$//BC11.AB00=>11$//BC11.AB 	00fEF11,GH00gFG00wGH  +.#}'2m

 	00=>11+>?11+vFG00fEFr;   c                 l   [         R                  " S[        S9  [        5       nU R	                  UR                  5       S5        U R	                  UR                  S5      S5        UR                  SS9nU R	                  UR                  5       S5        U R	                  UR                  S5      S5        g)z!test get_min_verify_time() methodr1   )categoryr   r   r   )min_verify_timeN)r5   r6   DeprecationWarningr   rT   get_min_verify_timerr   )r8   r   r   s      r9   test_15_min_verify_time'CryptPolicyTest.test_15_min_verify_time  s     	3EF]//115//8!<ZZZ+//115//8!<r;   c                 \   [        S0 U R                  D6nU R                  [        UR	                  5       5      U R                  5        U R                  [        UR	                  SS95      U R
                  5        U R                  [        UR	                  SS95      U R                  5        g)ztest iter_config() methodTresolve)iniNrC   )r   r   rT   r   iter_configsample_config_5prdsample_config_5pidr8   p5s     r9   test_20_iter_config#CryptPolicyTest.test_20_iter_config  s    24112bnn./1G1GHbnnTn:;T=T=TUbnnn679P9PQr;   c                     [        S0 U R                  D6nU R                  UR                  5       U R                  5        U R                  UR                  SS9U R                  5        g)ztest to_dict() methodTr   NrC   )r   r   rT   rU   r   r   s     r9   test_21_to_dictCryptPolicyTest.test_21_to_dict	  sR    24112t'='=>D143J3JKr;   c                    [        S0 U R                  D6nUR                  5       n[         R                  " U5      nU R	                  UR                  5       U R                  5        UR                  SS9nU R                  U[        5        g)ztest to_string() methodzlatin-1rn   NrC   )r   r   	to_stringrw   rT   rU   assertIsInstancebytes)r8   r   r   r   s       r9   test_22_to_string!CryptPolicyTest.test_22_to_string  sl    24112LLN$$Q't'='=>LL)L,a'r;   rC   N):rE   rF   rG   rH   __doc__descriptionPrefixrq   osrd   abspathjoindirname__file__ra   existsr   
startswithr   rS   sample_config_1pidr   r   r   r   r   r   r   r   r   r   r   rx   ry   sample_config_5sr   r   updater   r   r4   r]   re   rt   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   rJ   rC   r;   r9   r   r   %   s   ! &	 GGOOBGGLL
!#9-; <77>>/005F 1/2H!J &&';<<<<K!&%*#(#(	 D!&%*#(#(	 NNDNNDOOTEVEVW!&%*#(#(	 !&!&%*!& K!&!&%*#(!&
  K !&!&%*#(!&
 "$#("&*/
 ( + 
 *..0"_'2L%B  
 ,002!$; 
 ,002 #m'2L&A	 M&/P
QC(C2B,E&A"
,.78%NG<= RL(r;   r   c                   f    \ rS rSrSrSrS rS rS rS r	\
" / SQS	/S
SSSSSSSSS9rS rS rSrg)CryptContextTesti   ztest CryptContext classr   c                     [         R                  " U 5        [        R                  " SS5        [        R                  " SS5        [        R                  " SS5        [        R                  " SS5        g )Nr1   z3CryptContext\(\)\.replace\(\) has been deprecated.*z9The CryptContext ``policy`` keyword has been deprecated.*=.*(CryptPolicy|context\.policy).*(has|have) been deprecated.*r2   r3   r7   s    r9   r4   CryptContextTest.setUp$  s[    t V	X \	^*ij O	Qr;   c                 8   [        [        R                  [        R                  [        R                  /5      nUR
                  R                  5       u  p#nU R                  U[        R                  5        U R                  U[        R                  5        U R                  U[        R                  5        [        / SQ5      nUR
                  R                  5       u  p#nU R                  U[        R                  5        U R                  U[        R                  5        U R                  U[        R                  5        UR
                  n[        US9nU R                  UR                  5       UR                  5       5        [        USS9nU R                  UR                  5       UR                  5       5        U R                  UR                  5       [        / SQSS95        U R                  [        [        USS5        U R                  [        [         SS9  g)	ztest constructorr   r   r   )r\   r   )r\   r   rR   r\   Nx)r   r   r   r   r   r\   r   r   rT   rU   assertNotEqualr   rV   rW   setattr)r8   cccbar\   s         r9   r]   $CryptContextTest.test_00_constructor1  ss    4>>4??DNNKL		'')Aa(a)a( BC		'')Aa(a)a( (v~~'78>BJJL&..*:;t4Z4@(B 	C 	)Wb(DA)\#>r;   c                    [        / SQ5      nU R                  UR                  R                  5       [        R
                  5        UR                  5       nU R                  X!5        UR                  SS9nU R                  X15        U R                  UR                  R                  5       [        R                  5        g)ztest replace()r   r   r)   N)	r   r   r\   r   r   r   rr   assertIsNotr   )r8   r   cc2cc3s       r9   test_01_replace CryptContextTest.test_01_replaceN  s     BCbii++-t~~>jjl! jjj.! 	cjj,,.@r;   c                    [        5       nU R                  [        UR                  SSS9  U R                  [        UR                  S5        U R                  [        UR
                  SS5        [        S/5      n[        / S9nX!l        U R                  [        UR                  SSS9  U R                  [        UR                  S5        U R                  [        UR
                  SS5        g)ztest no handlersr   Tr   secretr   rL   N)r   rV   r[   identifyr   verifyr   r\   )r8   r   ps      r9   test_02_no_handlers$CryptContextTest.test_02_no_handlers_  s     ^(BKK$G(BGGX6(BIIx@ ;-(#	(BKK$G(BGGX6(BIIx@r;   )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                    [        S0 U R                  D6nU R                  UR                  S5      5        U R	                  UR                  S5      5        U R                  UR                  S5      5        U R	                  UR                  S5      5        U R	                  UR                  S5      5        U R                  UR                  S5      5        g)	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/2hdFpxA3fANrC   )r   sample_policy_1r   hash_needs_updater   r8   r   s     r9   test_12_hash_needs_update*CryptContextTest.test_12_hash_needs_update  s    1D001 	,,_=>--.RST 	,,-z{|--.{|} 	--.{|},,-z{|r;   c                    [         R                  " SS5        [        S/5      nS0 4SSS04S0 4S0 44 H(  u  p#U R                  " [        UR
                  U40 UD6  M*     [        S/5      nU R                  [        UR
                  S5        g)	z'test non-string hash values cause errorr1   z0.*needs_update.*'scheme' keyword is deprecated.*r   Nschemer=   rC   mysql323)r5   r6   r   rV   rW   r  )r8   r   r   kwdsr   s        r9   test_30_nonstring_hash'CryptContextTest.test_30_nonstring_hash  s    *\] ;-(r
+./BRJD i)=)=tLtL J<()S%:%:DAr;   rC   N)rE   rF   rG   rH   r   r   r4   r]   r   r  r   r  r  r  rJ   rC   r;   r9   r   r      s`    !&Q?:A"A* '&)$%')+&''+'++/%&O}&Br;   r   c                       \ rS rSrS rSrg)dummy_2i  rC   NrD   rC   r;   r9   r   r     s    Dr;   r   c                   *    \ rS rSrSrS rS rS rSrg)LazyCryptContextTesti  r	   c                     [         R                  " U 5        [        S5        U R                  [        S5        [        R
                  " SS5        [        R
                  " SS5        g )Nr   r1   z1CryptContext\(\)\.replace\(\) has been deprecatedr   )r   r4   unload_handler_name
addCleanupr5   r6   r7   s    r9   r4   LazyCryptContextTest.setUp  sN    t 	I&+Y7 	 T	V*ijr;   c                    U R                  [        S5      5        [        SS5        [        [	        SS/5      S/S9nU R                  [        SS5      5        U R                  UR                  R                  S5      5        U R                  UR                  R                  5       SS/5        U R                  [        SS5      5        g)ztest plain kwdsr   passlib.tests.test_contextr   )r.   TN)
r   has_crypt_handlerr   r	   iterr   r\   r   rT   r   r  s     r9   test_kwd_constructor)LazyCryptContextTest.test_kwd_constructor  s    *956#I/KLdI{#;<+W*9d;<		77DE**,y+.FG))T:;r;   c                   ^  T R                  [        S5      5        [        SS5        SU 4S jjn[        USS9nT R                  [        SS5      5        T R	                  UR
                  R                  S5      5        T R                  UR
                  R                  5       SS/5        T R	                  [        SS5      5        g)	z0test create_policy() hook, returning CryptPolicyr   r(  c                 R   > TR                  U 5        [        [        SS/5      S/S9$ )Nr   r   rQ   )r   r   r*  )flagr8   s    r9   create_policyELazyCryptContextTest.test_callable_constructor.<locals>.create_policy  s*    OOD!tY,D'ES^R_``r;   T)r0  r/  r   N)F)	r   r)  r   r	   r   r\   r   rT   r   )r8   r0  r   s   `  r9   test_callable_constructor.LazyCryptContextTest.test_callable_constructor  s    *956#I/KL	a ME*9d;<		77DE**,y+.FG))T:;r;   rC   N)	rE   rF   rG   rH   r   r4   r+  r2  rJ   rC   r;   r9   r"  r"    s    *
k<<r;   r"  )'r   
__future__r   loggingr   r   r5   pkg_resourcesr   ImportErrorpasslibr   passlib.contextr   r   r	   passlib.utilsr
   r   passlib.utils.handlersutilshandlersrX   passlib.tests.utilsr   r   passlib.registryr   r   r)  r   r$  rE   logr   r   rY   r   r"  rC   r;   r9   <module>rA     s    &  	 /  G G . # # 2 
 
r(h r(vJBx JBfb -<8 -<G  s   B BB