a fŸWcbrã@sdZddlmZddlmZddlZddlZzddlmZWne yRdZYn0ddl m Z ddl m Z mZmZddlmZmZddlmmZdd lmZmZdd lmZmZmZee ƒZ!Gd d „d eƒZ"Gd d„deƒZ#Gdd„dej$ƒZ%Gdd„deƒZ&dS)atests 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 @s eZdZdZdZdZej ej  ej  e ¡d¡¡Z ej  e ¡sLerLeddƒZ e d¡sZJ‚egd¢dd d d d d dZddd d d d d dœZeejejejejgdd d d d d dZdZedddddZegd¢dd dddd ddZeddZegd¢dd dddd ddZdZedgd ddd dZedZ e !¡Z"e"j#dgdd gd!e !¡Z$e$ #dd"d#œ¡e !¡Z%e% #dgdd gd!œ¡d$d%„Z&d&d'„Z'd(d)„Z(d*d+„Z)d,d-„Z*d.d/„Z+d0d1„Z,d2d3„Z-d4d5„Z.d6d7„Z/d8d9„Z0d:d;„Z1dd?„Z3d@dA„Z4dBdC„Z5dDdE„Z6dFdG„Z7dHS)IÚCryptPolicyTestztest CryptPolicy objectrzî[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 zsample_config_1s.cfgz passlib.testsz[passlib] schemes©Ú des_cryptÚ md5_cryptÚ bsdi_cryptÚ sha512_cryptrçš™™™™™¹?é0ui¨aé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)rrzall.vary_roundszbsdi_crypt.max_roundszbsdi_crypt.default_roundszsha512_crypt.max_roundszsha512_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_roundsrrr )rrrr%rrrr r©rz£ [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çš™™™™™©?)rrrZadmin__all__vary_roundsZadmin__sha512_crypt__max_roundszJdeprecated = des_crypt admin__context__deprecated = des_crypt, bsdi_crypt rr)Ú deprecatedZadmin__context__deprecatedzdes_crypt, bsdi_crypt)r)zadmin.context.deprecatedcCs>t |¡t dd¡t dd¡t dd¡t dd¡dS)NÚignorez)The CryptPolicy class has been deprecatedú,the method.*hash_needs_update.*is deprecatedz The 'all' scheme is deprecated.*zbsdi_crypt rounds should be odd©r ÚsetUpÚwarningsÚfilterwarnings©Úself©r2úI/usr/lib/python3.9/site-packages/passlib/tests/test_context_deprecated.pyr-Ñs ÿÿ zCryptPolicyTest.setUpcCstfi|j¤Ž}| | ¡|j¡t|jƒ}| | ¡|j¡| ttii¡|jttidd|jttgd¢ddGdd„dtjƒ}|jtt|gd|jtttjgdGd d „d tjƒ}|jt t||gd|jt td gd gd |jt td gd ddS)ztest CryptPolicy() constructoré)Zdummyrr)rZ bad__key__bsdi_crypt__max_roundsc@seZdZdZdS)z5CryptPolicyTest.test_00_constructor..namelessN©Ú__name__Ú __module__Ú __qualname__Únamer2r2r2r3Únamelessìsr:©rc@seZdZdZdS)z4CryptPolicyTest.test_00_constructor..dummy_1Údummy_1Nr5r2r2r2r3r<ôsr<rr©rr)©rrN) rÚsample_config_1pdÚ assertEqualÚto_dictÚ assertRaisesÚ TypeErrorÚuhÚ StaticHandlerÚ ValueErrorÚKeyError)r1Úpolicyr:r<r2r2r3Útest_00_constructorÚs. þþþz#CryptPolicyTest.test_00_constructorcCs:|j}t |¡}| | ¡|j¡| ttj|d¡dS)z(test CryptPolicy.from_path() constructorZxxxN)Úsample_config_1s_pathrÚ from_pathr@rAr?rBÚEnvironmentError)r1ÚpathrHr2r2r3Útest_01_from_path_simples z(CryptPolicyTest.test_01_from_path_simplecCsž| ¡}t||jƒt |¡}| | ¡|j¡t||j dd¡ƒt |¡}| | ¡|j¡t |jddd}t||ƒtj|dd}| | ¡|j¡dS)z7test CryptPolicy.from_path() constructor with encodingsÚ ú úutf-16úutf-8©Zsource_encoding©ÚencodingN) Zmktempr Úsample_config_1srrKr@rAr?Úreplacer )r1rMrHÚuc2r2r2r3Útest_01_from_paths    z!CryptPolicyTest.test_01_from_pathcCs¼t |j¡}| | ¡|j¡t |j dd¡¡}| | ¡|j¡t|jƒ}t |¡}| | ¡|j¡t|jddd}tj|dd}| | ¡|j¡t |j ¡}| | ¡|j ¡dS)z*test CryptPolicy.from_string() constructorrOrPrQrRrSrTN) rÚ from_stringrVr@rAr?rWr r Úsample_config_4sÚsample_config_4pd)r1rHÚdatarXr2r2r3Útest_02_from_string"s  ÿ   z#CryptPolicyTest.test_02_from_stringcCs˜t |j¡}| | ¡|j¡t |j¡}| | ¡|j¡t |j ¡¡}| | ¡|j¡t |¡}| ||¡|  t tjd¡|  t tjg¡dS)z*test CryptPolicy.from_source() constructorr4N) rZ from_sourcerJr@rAr?rVÚcopyÚassertIsrBrC)r1rHÚp2r2r2r3Útest_03_from_source;s    z#CryptPolicyTest.test_03_from_sourcecCs\| ttjg¡t |jg¡}| | ¡|j¡t |j|j |j g¡}| | ¡|j ¡dS)z+test CryptPolicy.from_sources() constructorN) rBrFrZ from_sourcesrVr@rAr?rJÚsample_config_2sÚsample_config_3pdÚsample_config_123pd)r1rHr2r2r3Útest_04_from_sourcesQsýÿz$CryptPolicyTest.test_04_from_sourcescCsztfi|j¤Ž}|jfi|j¤Ž}| | ¡|j¡|jfi|j¤Ž}| | ¡|j¡| |j¡}| | ¡|j¡dS)z&test CryptPolicy.replace() constructorN) rr?rWÚsample_config_2pdr@rAÚsample_config_12pdrdre)r1Úp1raZp2bÚp3r2r2r3Útest_05_replaceds zCryptPolicyTest.test_05_replacecCsB|jttdgdd|jttdgdd|jttdgdgddS)z!test CryptPolicy() forbidden kwdsrÚxx)rZdes_crypt__salt)rZ all__saltr)rZuser__context__schemesN)rBrGrr0r2r2r3Útest_06_forbiddenusþþþz!CryptPolicyTest.test_06_forbiddencCsBtfi|j¤Ž}| | ¡¡tfi|j¤Ž}| | ¡ ¡dS)ztest has_schemes() methodN)rr?Ú assertTrueZ has_schemesrd)r1rirjr2r2r3Útest_10_has_schemes‹sz#CryptPolicyTest.test_10_has_schemescCsVtfi|j¤Ž}|jd}| t| ¡ƒ|¡tfi|j¤Ž}| t| ¡ƒg¡dS)ztest iter_handlers() methodrN)rr?Úsample_config_1prdr@ÚlistÚ iter_handlersrd)r1riÚsrjr2r2r3Útest_11_iter_handlers”s  z%CryptPolicyTest.test_11_iter_handlerscCs`tfi|j¤Ž}| | d¡tj¡| | d¡d¡|jt|jddd| | ¡tj¡dS)ztest get_handler() methodrÚ sha256_cryptNT©Úrequired) rr?r`Ú get_handlerrrrBrGr)r1rir2r2r3Útest_12_get_handleržs z#CryptPolicyTest.test_12_get_handlerc Cs¬tfi|j¤Ž}| | d¡tddddd¡| | d¡tddd d ¡t |j¡}| | d¡tdd d ¡| | dd ¡tdd d ¡| | dd¡tddd ¡dS)ztest get_options() methodrrr!r"r#)Ú vary_roundsÚ min_roundsÚ max_roundsZdefault_roundsrr$r)rzr{r|r')rzr|ÚuserÚadminr(rN)rrhr@Z get_optionsÚdictrZr[)r1Zp12Zp4r2r2r3Útest_13_get_options­s2ûü ýýýz#CryptPolicyTest.test_13_get_optionscCs*tfi|j¤Ž}tfi|j¤Ž}| | d¡¡| | tj¡¡| | d¡¡| | d¡¡| | tj¡¡| | d¡¡| | dd¡¡| | dd¡¡| | dd¡¡| | dd¡¡tddgdgdgd}| | d¡¡| | d¡¡| | dd¡¡| | dd¡¡dS) z#test handler_is_deprecated() methodrrr}rr~r)rr)Zuser__context__deprecatedN)rr?Úsample_config_5pdÚ assertFalseÚhandler_is_deprecatedrrrn)r1ÚpaÚpbZpcr2r2r3Útest_14_handler_is_deprecatedÔs*ýz-CryptPolicyTest.test_14_handler_is_deprecatedcCshtjdtdtƒ}| | ¡d¡| | d¡d¡|jdd}| | ¡d¡| | d¡d¡dS)z!test get_min_verify_time() methodr*)Úcategoryrr~r)Zmin_verify_timeN)r.r/ÚDeprecationWarningrr@Zget_min_verify_timerW)r1r„r…r2r2r3Útest_15_min_verify_timeòs z'CryptPolicyTest.test_15_min_verify_timecCs^tfi|j¤Ž}| t| ¡ƒ|j¡| t|jddƒ|j¡| t|jddƒ|j¡dS)ztest iter_config() methodT©Úresolve)ZiniN)rrr@rZ iter_configÚsample_config_5prdÚsample_config_5pid©r1Zp5r2r2r3Útest_20_iter_configsz#CryptPolicyTest.test_20_iter_configcCs<tfi|j¤Ž}| | ¡|j¡| |jdd|j¡dS)ztest to_dict() methodTrŠN)rrr@rArŒrŽr2r2r3Útest_21_to_dict szCryptPolicyTest.test_21_to_dictcCsPtfi|j¤Ž}| ¡}t |¡}| | ¡|j¡|jdd}| |t¡dS)ztest to_string() methodzlatin-1rTN)rrZ to_stringrZr@rAZassertIsInstanceÚbytes)r1r„rsr…r2r2r3Útest_22_to_strings   z!CryptPolicyTest.test_22_to_stringN)8r6r7r8Ú__doc__ÚdescriptionPrefixrVÚosrMÚabspathÚjoinÚdirnameÚ__file__rJÚexistsrÚ startswithrr?Zsample_config_1pidrrrrrrprcrgrhrdrer[r\Zsample_config_5sr_rÚupdaterrŒr-rIrNrYr^rbrfrkrmrortryr€r†r‰rrr’r2r2r2r3r%sÚ   ÿÿø ø øú ÷ÿ÷ ù þþü  (   'rc @sheZdZdZdZdd„Zdd„Zdd„Zd d „Ze gd ¢d gd ddddddddd Z dd„Z dd„Z dS)ÚCryptContextTestztest CryptContext classrcCs>t |¡t dd¡t dd¡t dd¡t dd¡dS)Nr*z3CryptContext\(\)\.replace\(\) has been deprecated.*z9The CryptContext ``policy`` keyword has been deprecated.*ú=.*(CryptPolicy|context\.policy).*(has|have) been deprecated.*r+r,r0r2r2r3r-$s ÿÿ ÿzCryptContextTest.setUpcCsttjtjtjgƒ}|j ¡\}}}| |tj¡| |tj¡| |tj¡tgd¢ƒ}|j ¡\}}}| |tj¡| |tj¡| |tj¡|j}t|d}| |  ¡|  ¡¡t|dd}|  |  ¡|  ¡¡| |  ¡t gd¢dd¡|  t t|dd¡|j t tdddS) ztest constructor©rrr)rHr)rHrr>rHNÚx)rrrrrrHrrr`r@rAZassertNotEqualrrBrCÚsetattr)r1ÚccÚcÚbÚarHr2r2r3rI1s(   ÿz$CryptContextTest.test_00_constructorcCsdtgd¢ƒ}| |j ¡tj¡| ¡}| ||¡|jdd}| ||¡| |j ¡tj¡dS)ztest replace()rŸrr&N) rr`rHrxrrrWZ assertIsNotr)r1r¢Úcc2Zcc3r2r2r3Útest_01_replaceNs    z CryptContextTest.test_01_replacecCstƒ}|jt|jddd| t|jd¡| t|jdd¡tdgƒ}tgd}||_|jt|jddd| t|jd¡| t|jdd¡dS)ztest no handlersrTrvZsecretrr;N)rrBrGZidentifyrZverifyrrH)r1r¢Úpr2r2r3Útest_02_no_handlers_s  z$CryptContextTest.test_02_no_handlers)rrZphpassrrurruééri¸ iÐÚHé) rr)rrrZbsdi_crypt__vary_roundsZsha256_crypt__max_roundsZsha256_crypt__min_roundsZsha256_crypt__default_roundsZ phpass__identZphpass__default_roundscCsttfi|j¤Ž}| | d¡¡| | d¡¡| | d¡¡| | d¡¡| | d¡¡| | d¡¡dS)ztest hash_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/2hdFpxA3fAN)rÚsample_policy_1rnÚhash_needs_updater‚©r1r¢r2r2r3Útest_12_hash_needs_updateƒsz*CryptContextTest.test_12_hash_needs_updatecCsvt dd¡tdgƒ}difdddifdifdiffD] \}}|jt|j|fi|¤Žq6tdgƒ}| t|jd¡dS) z'test non-string hash values cause errorr*z0.*needs_update.*'scheme' keyword is deprecated.*rNÚschemer4r2Zmysql323)r.r/rrBrCr¯)r1r¢rÚkwdsr¦r2r2r3Útest_30_nonstring_hash–s   û  z'CryptContextTest.test_30_nonstring_hashN) r6r7r8r“r”r-rIr§r©rr®r±r´r2r2r2r3r s* ôrc@seZdZdZdS)Údummy_2Nr5r2r2r2r3rµ³srµc@s(eZdZdZdd„Zdd„Zdd„ZdS) ÚLazyCryptContextTestrcCs:t |¡tdƒ| td¡t dd¡t dd¡dS)Nrµr*z1CryptContext\(\)\.replace\(\) has been deprecatedrž)r r-Úunload_handler_nameZ addCleanupr.r/r0r2r2r3r-¹s  ÿzLazyCryptContextTest.setUpcCsz| tdƒ¡tddƒttddgƒdgd}| tddƒ¡| |j d¡¡| |j  ¡ddg¡| tddƒ¡dS)ztest plain kwdsrµúpasslib.tests.test_contextr)r)TN) r‚Úhas_crypt_handlerr rÚiterrnrHrƒr@rr°r2r2r3Útest_kwd_constructorÅs z)LazyCryptContextTest.test_kwd_constructorcs~ˆ tdƒ¡tddƒd ‡fdd„ }t|dd}ˆ tddƒ¡ˆ |j d¡¡ˆ |j ¡ddg¡ˆ tddƒ¡d S) z0test create_policy() hook, returning CryptPolicyrµr¸Fcs ˆ |¡ttddgƒdgdS)Nrµrr=)rnrrº)Úflagr0r2r3Ú create_policyØs zELazyCryptContextTest.test_callable_constructor..create_policyT)r½r¼rN)F) r‚r¹r rrnrHrƒr@r)r1r½r¢r2r0r3Útest_callable_constructorÓs  z.LazyCryptContextTest.test_callable_constructorN)r6r7r8r”r-r»r¾r2r2r2r3r¶¶s r¶)'r“Z __future__rZloggingrr•r.Z pkg_resourcesrÚ ImportErrorZpasslibrZpasslib.contextrrrZ passlib.utilsr r Zpasslib.utils.handlersZutilsÚhandlersrDZpasslib.tests.utilsr r Zpasslib.registryr rr¹rr·r6ÚlogrrrErµr¶r2r2r2r3Ús.     ~