
     hbr                     :   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 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jH                        Z% G d de      Z&y# e	$ r dZY {w xY w)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                  j                  ej                  j                  ej                  j                  e      d            Zej                  j                  e      ser	 edd      Zej                  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jC                         Z"e"jG                  dgdd g!       ejC                         Z$e$jG                  dd"d#       ejC                         Z%e%jG                  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 Z7y6)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       y )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/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/passlib/tests/test_context_deprecated.pyr3   zCryptPolicyTest.setUp   sX    t L	N O	Q*LM*KL    c                 Z   t        di | j                  }| j                  |j                         | j                         t        | j                        }| j                  |j                         | j                         | j	                  t
        t         i i        | j	                  t
        t         i d       | j	                  t
        t         g dd        G d dt        j                        }| j	                  t        t         |g       | j	                  t
        t         t        j                  g        G d	 d
t        j                        }| j	                  t        t         ||g       | j	                  t        t         dgdg       | j	                  t        t         dgd       y)ztest CryptPolicy() constructor   )dummyr   r   )r    bad__key__bsdi_crypt__max_roundsc                       e Zd ZdZy)5CryptPolicyTest.test_00_constructor.<locals>.namelessN__name__
__module____qualname__name r9   r8   namelessr?      s    Dr9   rF   r   c                       e Zd ZdZy)4CryptPolicyTest.test_00_constructor.<locals>.dummy_1dummy_1Nr@   rE   r9   r8   rJ   rI      s    Dr9   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   sb   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'79KL 	(K#.-&1] 	 	4
 	(K#.-"- 	 	/r9   c                     | j                   }t        j                  |      }| j                  |j	                         | j
                         | j                  t        t        j                  |dz          y)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  s[    
 ))&&t,)4+A+AB 	*K,A,A4%<Pr9   c                 b   | j                         }t        || j                         t        j                  |      }| j                  |j                         | j                         t        || j                  j                  dd             t        j                  |      }| j                  |j                         | j                         t        | j                  dd      }t        ||       t        j                  |d      }| j                  |j                         | j                         y)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+AB 	t,,44T&AB&&t,)4+A+AB t,,hPs&&th?)4+A+ABr9   c                 4   t        j                  | j                        }| j                  |j	                         | j
                         t        j                  | j                  j                  dd            }| j                  |j	                         | j
                         t        | j                        }t        j                  |      }| j                  |j	                         | j
                         t        | j                  dd      }t        j                  |d      }| j                  |j	                         | j
                         t        j                  | j                        }| j                  |j	                         | j                         y)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"  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Br9   c                    t        j                  | j                        }| j                  |j	                         | j
                         t        j                  | j                        }| j                  |j	                         | j
                         t        j                  | j
                  j                               }| j                  |j	                         | j
                         t        j                  |      }| j                  ||       | j                  t        t         j                  d       | j                  t        t         j                  g        y)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)CD)4+A+AB (()>)>?)4+A+AB (()?)?)D)D)FG)4+A+AB $$V,fb! 	)[%<%<a@)[%<%<bAr9   c                    | j                  t        t        j                  g        t        j                  | j                  g      }| j                  |j                         | j                         t        j                  | j                  | j                  | j                  g      }| j                  |j                         | j                         y)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 ))4+@+@*AB)4+A+AB ))&&!!"" 	)4+C+CDr9   c                    t        di | j                  } |j                  di | j                  }| j	                  |j                         | j                          |j                  di | j                  }| j	                  |j                         | j                         |j                  | j                        }| j	                  |j                         | j                         y)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     24112 RZZ1$001t'>'>? bjj24112(?(?@ ZZ../t'?'?@r9   c                     | j                  t        t        dgd       | j                  t        t        dgd       | j                  t        t        dgdg       y)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  sg     	(K M  	 	
 	(K M 	 	
 	(K M$/= 	 	
r9   c                     t        di | j                  }| j                  |j                                t        di | j                  }| j                  |j                                 y)ztest has_schemes() methodNrE   )r   rM   
assertTruehas_schemesr|   )r7   r   r   s      r8   test_10_has_schemesz#CryptPolicyTest.test_10_has_schemes  sS     24112()24112BNN,,-r9   c                    t        di | j                  }| j                  d   }| j                  t	        |j                               |       t        di | j                  }| j                  t	        |j                               g        y)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  sv     24112##I.b..011524112b..0126r9   c                 p   t        di | j                  }| j                  |j                  d      t        j
                         | j                  |j                  d      d       | j                  t        |j                  dd       | j                  |j                         t        j                         y)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     24112 	bnn\2DOOD 	bnn^4d;(BNNNTR 	bnn&7r9   c           	      .   t        di | j                  }| j                  |j                  d      t	        dddd             | j                  |j                  d      t	        ddd	
             t        j
                  | j                        }| j                  |j                  d      t	        dd             | j                  |j                  dd      t	        dd             | j                  |j                  dd      t	        dd             y)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    4D3346t"8
 	 	8	:
 	 $$T%:%:;7:
 	 	?B
 	 	@$C
 	r9   c                 @   t        d	i | j                  }t        d	i | j                  }| j                  |j	                  d             | j                  |j	                  t
        j                               | j                  |j	                  d             | j                  |j	                  d             | j                  |j	                  t
        j                               | j                  |j	                  d             | j                  |j	                  dd             | j                  |j	                  dd             | j                  |j	                  dd             | j                  |j	                  dd             t        ddgdgdg      }| j                  |j	                  d             | j                  |j	                  d             | j                  |j	                  dd             | j                  |j	                  dd             y)
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   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Fr9   c                 v   t        j                  dt               t               }| j	                  |j                         d       | j	                  |j                  d      d       |j                  d      }| j	                  |j                         d       | j	                  |j                  d      d       y)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]//115//8!<ZZZ+//115//8!<r9   c                 h   t        di | j                  }| j                  t        |j	                               | j                         | j                  t        |j	                  d            | j
                         | j                  t        |j	                  d            | j                         y)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    24112bnn./1G1GHbnnTn:;T=T=TUbnnn679P9PQr9   c                     t        di | j                  }| j                  |j                         | j                         | j                  |j                  d      | j                         y)ztest to_dict() methodTr   NrE   )r   r   rN   rO   r   r   s     r8   test_21_to_dictzCryptPolicyTest.test_21_to_dict	  sR    24112t'='=>D143J3JKr9   c                    t        di | j                  }|j                         }t        j                  |      }| j	                  |j                         | j                         |j                  d      }| j                  |t               y)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  sl    24112LLN$$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   ! &	 GGOOBGGLL
!#9-; <77>>/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(r9   r   c                   d    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y)CryptContextTestztest CryptContext classr   c                     t        j                  |        t        j                  dd       t        j                  dd       t        j                  dd       t        j                  dd       y )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 \	^*ij O	Qr9   c                 T   t        t        j                  t        j                  t        j                  g      }|j
                  j                         \  }}}| j                  |t        j                         | j                  |t        j                         | j                  |t        j                         t        g d      }|j
                  j                         \  }}}| j                  |t        j                         | j                  |t        j                         | j                  |t        j                         |j
                  }t        |      }| j                  |j                         |j                                t        |d      }| j                  |j                         |j                                | j                  |j                         t        g dd             | j                  t        t        |dd       | j                  t        t         d       y)	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  sw    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)\#>r9   c                    t        g d      }| j                  |j                  j                         t        j
                         |j                         }| j                  ||       |j                  d      }| j                  ||       | j                  |j                  j                         t        j                         y)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Cbii++-t~~>jjlb! jjj.b! 	cjj,,.@r9   c                    t               }| j                  t        |j                  dd       | j                  t        |j                  d       | j                  t        |j
                  dd       t        dg      }t        g       }||_        | j                  t        |j                  dd       | j                  t        |j                  d       | j                  t        |j
                  dd       y)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     ^(BKK$G(BGGX6(BIIx@ ;-(#	(BKK$G(BGGX6(BIIx@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                  }| j                  |j                  d             | j	                  |j                  d             | j                  |j                  d             | j	                  |j                  d             | j	                  |j                  d             | j                  |j                  d             y)	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    1D001 	,,_=>--.RS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      }| j                  t        |j
                  d       y)	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JD$ Di)=)=tLtL	M J<()S%:%:DAr9   N)rA   rB   rC   r   r   r3   rW   r   r   r   r   r   r  rE   r9   r8   r   r      s`    !&Q?:A"A* '&)$%')+&''+'++/%&O}&Br9   r   c                       e Zd Zd Zy)dummy_2Nr@   rE   r9   r8   r  r    s    Dr9   r  c                   "    e Zd ZdZd Zd Zd Zy)LazyCryptContextTestr	   c                     t        j                  |        t        d       | j                  t        d       t	        j
                  dd       t	        j
                  dd       y )Nr  r0   z1CryptContext\(\)\.replace\(\) has been deprecatedr   )r   r3   unload_handler_name
addCleanupr4   r5   r6   s    r8   r3   zLazyCryptContextTest.setUp  sN    t 	I&+Y7 	 T	V*ijr9   c                    | j                  t        d             t        dd       t        t	        ddg      dg      }| j                  t        dd             | j                  |j                  j                  d             | j                  |j                  j                         ddg       | j                  t        dd             y)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6#I/KLdI{#;<+W*9d;<		77DE**,y+.FG))T:;r9   c                      j                  t        d             t        dd       d fd	}t        |d      } j                  t        dd              j	                  |j
                  j                  d              j                  |j
                  j                         ddg        j	                  t        dd             y)	z0test create_policy() hook, returning CryptPolicyr  r
  c                 X    j                  |        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  s*    OOD!tY,D'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6#I/KL	a ME*9d;<		77DE**,y+.FG))T:;r9   N)rA   rB   rC   r   r3   r  r  rE   r9   r8   r  r    s    *
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 . # # 2 
 
r(h r(vJBx JBfb -<8 -<G  s   B BB