a fŸWcørã@sdZddlmZddlmZddlZddlZddlmZm Z ddl m Z ddl m Z ddlmZdd lmZmZmZmZdd l mZdd lmZdd lmZeeƒZddd„Zej d¡p¼dZddd„Z dd„Z!dd„Z"e"ƒ\Z#Z$e %e#d¡Z&Gdd„deƒZ'Gdd„deƒZ(dS)z@tests for passlib.apache -- (c) Assurance Technologies 2008-2011é)Úwith_statement)Ú getLoggerN)ÚapacheÚregistry)ÚMissingBackendError)Úirange)Úunittest)ÚTestCaseÚget_fileÚset_fileÚensure_mtime_changed)Úu)Úto_bytes)Úto_unicode_for_identifyé cCs0tj |¡}tj |¡|}t |||f¡dS)z)backdate file's mtime by specified amountN)ÚosÚpathÚgetatimeÚgetmtimeÚutime)rÚoffsetZatimeÚmtime©rú=/usr/lib/python3.9/site-packages/passlib/tests/test_apache.pyÚbackdate_file_mtimes rZPASSLIB_TEST_HTPASSWD_PATHZhtpasswdcCsb|dur| d¡}tjtg|tjtj|r0tjndd}| |¡\}}| ¡}t|pVdƒ}||fS)z$ helper to run htpasswd cmd Núutf-8)ÚstdoutÚstderrÚstdinÚ) ÚencodeÚ subprocessÚPopenÚ htpasswd_pathÚPIPEZSTDOUTZ communicateÚwaitr)ÚargsrÚprocÚoutÚerrÚrcrrrÚ_call_htpasswd+s ÿ r+cCstd||g|ƒ\}}| S)z% wrapper for htpasswd verify z-vi)r+)rÚuserÚpasswordr(r*rrrÚ_call_htpasswd_verify9sr.cCsDztgƒ\}}Wnty$YdS0|s4t d¡d|v}d|fS)z0 helper to check if htpasswd is present )FFz htpasswd test returned with rc=0z -B T)r+ÚOSErrorÚlogZwarning)r(r*Z have_bcryptrrrÚ_detect_htpasswdAs  r1z requires `htpasswd` cmdline toolc@sêeZdZdZdZdZdZdZdZdZ dZ d Z d d „Z d d „Z dd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zd d!„Zd"d#„Zd$d%„Zd&d'„Zd(d)„Zd*d+„Zd,d-„Zed.d/„ƒZee  !e" #d0¡d1¡d2d3„ƒƒZ$d4S)5ÚHtpasswdFileTestztest HtpasswdFile classÚ HtpasswdFilestuser2:2CHkkwa2AtqGs user3:{SHA}3ipNV1GrBtxPmHFC21fCbVCSXIo= user4:pass4 user1:$apr1$t4tc7jTh$GPIWVUo8sQKJlUdV8V5vu0 s4user3:{SHA}3ipNV1GrBtxPmHFC21fCbVCSXIo= user4:pass4 syuser2:pass2x user3:{SHA}3ipNV1GrBtxPmHFC21fCbVCSXIo= user4:pass4 user1:$apr1$t4tc7jTh$GPIWVUo8sQKJlUdV8V5vu0 user5:pass5 suseræ:2CHkkwa2AtqGs suseræ:2CHkkwa2AtqGs suser1:pass1 user1:pass2 s user2:2CHkkwa2AtqGs user3:{SHA}3ipNV1GrBtxPmHFC21fCbVCSXIo= user4:pass4 user1:$apr1$t4tc7jTh$GPIWVUo8sQKJlUdV8V5vu0 user5:$2a$12$yktDxraxijBZ360orOyCOePFGhuis/umyPNJoL5EbsLk.s6SWdrRO user6:$5$rounds=110000$cCRp/xUUGVgwR4aP$p0.QKFS5qLNRqw1/47lXYiAcgIjJK.WjCO8nrEKuUK. cCs*| ¡}t||jƒt |¡}| | ¡|j¡| |j|¡| |j ¡|d|_| |j|d¡|  |j ¡tj|dd}| | ¡d¡| |j|¡|  |j ¡|  d¡tj|dd}Wdƒn1sØ0Y| | ¡d¡| |j|¡|  |j ¡t   |¡| ttj|¡dS) útest constructor autoloadÚxT©Únewóz ``autoload=False`` is deprecatedF)ZautoloadN)Úmktempr Ú sample_01rr3Ú assertEqualÚ to_stringrÚ assertTruerÚ assertFalseÚassertWarningListrÚremoveÚ assertRaisesÚIOError©ÚselfrÚhtrrrÚtest_00_constructor_autoload}s(       ,  z-HtpasswdFileTest.test_00_constructor_autoloadcCsP| ¡}t||jƒtj |¡}| | ¡|j¡| |jd¡|  |j ¡dS©N) r9r r:rr3Z from_pathr;r<rr>rrCrrrÚtest_00_from_pathŸs   z"HtpasswdFileTest.test_00_from_pathcCsdtj |j¡}| | d¡¡| | d¡¡| | d¡¡| | ¡|j ¡|  t |jd¡dS)ú test delete()Úuser1Úuser2Úuser5úuser:N) rr3Ú from_stringr:r=Údeleter>r;r<Ú sample_02rAÚ ValueError©rDrErrrÚtest_01_delete§s zHtpasswdFileTest.test_01_deletecCsf| ¡}d}t||ƒt |¡}| d¡| t|ƒ|¡tj|dd}| d¡| t|ƒd¡dS)Nsuser1:pass1 user2:pass2 rJT)Úautosaves user2:pass2 )r9r rr3rOr;r ©rDrZsamplerErrrÚtest_01_delete_autosave²s    z(HtpasswdFileTest.test_01_delete_autosavecCstjj|jdd}| | dd¡¡| | dd¡¡| | ¡|j ¡|  d¡"tjj|jdd}Wd ƒn1sz0Y| | dd¡¡| | dd¡¡| | ¡|j ¡|  t |jd d ¡|  d ¡|  dd ¡Wd ƒn1sø0Y| | dd ¡¡d S)ztest set_password()Ú plaintext©Údefault_schemerKÚpass2xrLÚpass5z``default`` is deprecated)ÚdefaultNrMÚpassúupdate\(\) is deprecatedÚtest)rr3rNr:r=Ú set_passwordr>r;r<Ú sample_03r?rArQÚupdateÚcheck_passwordrRrrrÚtest_02_set_password¿sÿ 0 *z%HtpasswdFileTest.test_02_set_passwordcCsl| ¡}d}t||ƒt |¡}| dd¡| t|ƒ|¡tj|ddd}| dd¡| t|ƒd¡dS)Nó user1:pass1 rJÚpass2rWT)rYrTs user1:pass2 )r9r rr3r`r;r rUrrrÚtest_02_set_password_autosaveÖs    z.HtpasswdFileTest.test_02_set_password_autosavecCs’dd„}| |dƒd¡| |dƒd¡| t|d¡| |dƒtjd¡| |dƒtjd¡| |dƒtjd¡| |d ƒtjd¡d S) z%test set_password() -- default_schemecSs*tj|d}| dd¡|j | d¡¡S)NrXrJÚpass1)rr3r`ÚcontextZidentifyÚget_hash)ÚschemerErrrÚcheckæs  zCHtpasswdFileTest.test_02_set_password_default_scheme..checkZ sha256_cryptZ des_cryptZxxxZportableZportable_apache_22Úhost_apache_22N)r;rAÚKeyErrorrÚhtpasswd_defaults)rDrlrrrÚ#test_02_set_password_default_schemeãsz4HtpasswdFileTest.test_02_set_password_default_schemecCsLtj |j¡}| dd¡| d¡| dd¡| t| ¡ƒgd¢¡dS)ú test users()rLr[Úuser3Úpass3©rJrKrrÚuser4rLN) rr3rNr:r`rOr;ÚsortedÚusersrRrrrÚ test_03_usersús    zHtpasswdFileTest.test_03_usersc Cstj |j¡}| t|jdd¡| | dd¡du¡tddƒD]f}t |ƒ}z8| | d|d|¡¡| | d|d¡du¡Wq@t y¤|d kržYq@‚Yq@0q@| t |jd d¡|  d gd ¡4| |  d d¡¡| |  d d¡¡Wdƒn1s0YdS)útest check_password()éZpass9Zuser9Nér,r]FÚ5rMúverify\(\) is deprecatedérJrhrf)rr3rNÚ sample_05rAÚ TypeErrorrcr=rÚstrrrQr?Úverifyr>©rDrEÚirrrÚtest_04_check_passwords   z'HtpasswdFileTest.test_04_check_passwordcCs| ¡}t|dƒt|dƒtj|dd}| | ¡d¡| dd¡| ¡| | ¡d¡t||j ƒ| ¡| | ¡|j ¡| d d ¡|  ¡| | ¡|j ¡t ¡}|  t |j ¡|  t |j¡t||j ƒt ¡}|  |¡| | dd¡¡d S) ú test load()rérWrXr8rJrhrerLr[N)r9r rrr3r;r<r`Úload_if_changedr:ÚloadrAÚ RuntimeErrorÚ sample_dupr=rc©rDrZhaÚhbZhcrrrÚ test_05_loads*       zHtpasswdFileTest.test_05_loadcCs| ¡}t||jƒt |¡}| d¡| d¡| ¡| t|ƒ|j ¡tjdd}|  dd¡|  t |j¡| |¡| t|ƒd¡dS)ú test save()rJrKrWrXrhreN) r9r r:rr3rOÚsaver;r rPr`rArŠ©rDrrErrrrÚ test_06_saveAs       zHtpasswdFileTest.test_06_savecCs¸|jttjddtjj|jddd}| | ¡tdƒg¡|  d¡"tjj|jdd}Wdƒn1sn0Y| | ¡d g¡tjj|j d dd}| | ¡tdƒg¡dS) ztest 'encoding' kwdúutf-16©ÚencodingrT©r•Zreturn_unicodeõuseræz``encoding=None`` is deprecatedNsuseræúlatin-1) rArQrr3rNÚsample_04_utf8r;rwr r?Úsample_04_latin1rRrrrÚtest_07_encodingsWs ÿ 0 ÿz"HtpasswdFileTest.test_07_encodingscCs„tj |j¡}| | d¡d¡| | d¡d¡| | d¡d¡| d¡"| | d¡d¡Wdƒn1sv0YdS)útest get_hash()rrs!{SHA}3ipNV1GrBtxPmHFC21fCbVCSXIo=ruspass4rLNúfind\(\) is deprecated)rr3rNr:r;rjr?ÚfindrRrrrÚtest_08_get_hashks  z!HtpasswdFileTest.test_08_get_hashcCs<tj |j¡}| | ¡|j¡t ¡}| | ¡d¡dS)ztest to_stringr8N)rr3rNr:r;r<rRrrrÚtest_09_to_stringusz"HtpasswdFileTest.test_09_to_stringcCstjddddd}t|ƒdS)NZfakepathTr˜)rTr7r•)rr3ÚreprrRrrrÚ test_10_repr€szHtpasswdFileTest.test_10_reprcCs(| ttjjd¡| ttjjd¡dS)Nsrealm:user1:pass1 spass1 )rArQrr3rN©rDrrrÚtest_11_malformed„s  ÿ ÿz"HtpasswdFileTest.test_11_malformedcCs|jttjjddddS)Nr8)r)rAr€rr3rNr£rrrÚtest_12_from_stringŠs ÿz$HtpasswdFileTest.test_12_from_stringcCs²tdƒ}tj |¡}| t| ¡ƒgd¢¡| dd¡| t| ¡ƒgd¢¡| dd¡| t| ¡ƒgd¢¡| d¡| t| ¡ƒgd ¢¡td ƒ}| |  ¡|¡d S) zwhitespace & comment handlingz` user2:pass2 user4:pass4 user7:pass7 user1:pass1 # legacy users #user6:pass6 user5:pass5 )rJrKrurLÚuser7ruZalthash4Úuser6Zalthash6)rJrKrurLr§r¦r¦)rJrKrurLr§zd user2:pass2 user4:althash4 user1:pass1 # legacy users #user6:pass6 user5:pass5 user6:althash6 N) rrr3rNr;rvrwÚset_hashrOr<)rDÚsourcerEÚtargetrrrÚtest_13_whitespacesÿ    ÿ z#HtpasswdFileTest.test_13_whitespacecs| ¡}tj|dd‰‡fdd„}ˆ d|ddƒ¡tjd}ˆ d |d|ƒ¡tjd }ˆ d |d|ƒ¡ˆ d d ¡ˆ ¡| t|ddƒ¡| t|d dƒ¡| t|d dƒ¡| t|d dƒ¡| t|ddƒ¡| t|d dƒ¡| t|d dƒ¡dS)z; verify "htpasswd" command can read output T©rr7csˆj |¡ |¡SrG©riZhandlerÚhash©Úpwdrk©rErrÚ hash_schemeÆsz>HtpasswdFileTest.test_htpasswd_cmd_verify..hash_schemerJr-Z apr_md5_cryptrmrKÚhostrrruz$xxx$foo$bar$bazÚwrongN) r9rr3r¨rorr>r.r=)rDrr²Zhost_no_bcryptZ host_bestrr±rÚtest_htpasswd_cmd_verify¾s"    z)HtpasswdFileTest.test_htpasswd_cmd_verifyÚbcryptzbcrypt support requiredcs|| ¡}tj|dd‰‡fdd„}ˆ d|ddƒ¡ˆ ¡| t|ddƒ¡trf| t|ddƒ¡n| t|ddƒ¡d S) zµ verify "htpasswd" command can read bcrypt format this tests for regression of issue 95, where we output "$2b$" instead of "$2y$"; fixed in v1.7.2. Tr¬csˆj |¡ |¡SrGr­r¯r±rrr²îszEHtpasswdFileTest.test_htpasswd_cmd_verify_bcrypt..hash_schemerJr-r¶r´N) r9rr3r¨rr>r.ÚHAVE_HTPASSWD_BCRYPTr=)rDrr²rr±rÚtest_htpasswd_cmd_verify_bcryptãs  z0HtpasswdFileTest.test_htpasswd_cmd_verify_bcryptN)%Ú__name__Ú __module__Ú __qualname__Ú__doc__ÚdescriptionPrefixr:rPrar™ršr‹rrFrHrSrVrdrgrprxr…rŽr’r›rŸr r¢r¤r¥r«Úrequires_htpasswd_cmdrµrÚ skipUnlessrZ has_backendr¸rrrrr2Zs@"   %  / $r2c@seZdZdZdZdZdZdZdZdZ dd „Z d d „Z d d „Z dd„Z dd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zd d!„Zd"S)#ÚHtdigestFileTestztest HtdigestFile classÚ HtdigestFiles´user2:realm:549d2a5f4659ab39a80dac99e159ab19 user3:realm:a500bb8c02f6a9170ae46af10c898744 user4:realm:ab7b5d5f28ccc7666315f508c7358519 user1:realm:2a6cf53e7d8f8cf39d946dc880b14128 sZuser3:realm:a500bb8c02f6a9170ae46af10c898744 user4:realm:ab7b5d5f28ccc7666315f508c7358519 sáuser2:realm:5ba6d8328943c23c64b50f8b29566059 user3:realm:a500bb8c02f6a9170ae46af10c898744 user4:realm:ab7b5d5f28ccc7666315f508c7358519 user1:realm:2a6cf53e7d8f8cf39d946dc880b14128 user5:realm:03c55fdc6bf71552356ad401bdb9af19 s0useræ:realmæ:549d2a5f4659ab39a80dac99e159ab19 s.useræ:realmæ:549d2a5f4659ab39a80dac99e159ab19 cCsl| ¡}t||jƒt |¡}| | ¡|j¡tj|dd}| | ¡d¡t |¡|  t tj|¡dS)r4Tr6r8N) r9r r:rrÁr;r<rr@rArBrCrrrrFs   z-HtdigestFileTest.test_00_constructor_autoloadcCstj |j¡}| | dd¡¡| | dd¡¡| | dd¡¡| | dd¡¡| | ¡|j ¡|  t |jdd¡|  t |jdd ¡d S) rIrJÚrealmrKrLrrÚrealm5rMr,úrealm:N) rrÁrNr:r=rOr>r;r<rPrArQrRrrrrS+szHtdigestFileTest.test_01_deletecCs”| ¡}t||jƒt |¡}| | dd¡¡| | dd¡¡| | dd¡¡| t |ƒ|j¡d|_ | | dd¡¡| t |ƒ|j ¡dS)NrJrÂrrrÃrLTrK) r9r r:rrÁr=rOr>r;r rTrPrCrrrrV:s  z(HtdigestFileTest.test_01_delete_autosavecCstj |j¡}| | ddd¡¡| | ddd¡¡| | ¡|j ¡|  t |jdd¡d|_ | dd¡|  ddd¡|  t|jddd ¡|  t|jd dd ¡|  t|jd d d ¡|  t|jd d d ¡| d¡| ddd¡Wdƒn1sø0Y| |  dd¡¡dS)z test update()rKrÂrZrLr[rsZrealm2rMr]Auuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuur,rÄArrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr^r_N)rrÁrNr:r=r`r>r;r<rarAr€Ú default_realmrcrQr?rbrRrrrrdHs  ,z%HtdigestFileTest.test_02_set_passwordcCsdtj |j¡}| ddd¡| dd¡| ddd¡| t| d¡ƒgd¢¡|  t |jd¡dS) rqrLrÂr[rrrsrtrzN) rrÁrNr:r`rOr;rvrwrAr€rRrrrrxds  zHtdigestFileTest.test_03_usersc CsJtj |j¡}| t|jddd¡| t|jddd¡| | ddd¡d¡tddƒD]B}t |ƒ}|  | d|dd|¡¡| | d|dd¡d ¡qV| t|jdd¡d|_ |  | d d ¡¡| | dd¡d¡|  d gd ¡8|  |  d dd ¡¡| |  d dd¡¡Wdƒn1s(0Y| t|jddd¡dS)ryrzrÂr[r,rLNr‡r]FrJrhr}r~rfrM)rrÁrNr:rAr€rcZassertIsrrr=rÅr?r‚r>rQrƒrrrr…ns 4z'HtdigestFileTest.test_04_check_passwordcCsP| ¡}t|dƒt|dƒt |¡}| | ¡d¡| ddd¡| ¡| | ¡d¡t||j ƒ| ¡| | ¡|j ¡| ddd ¡|  ¡| | ¡|j ¡t ¡}|  t |j ¡|  t |j¡t ¡}|  |¡| | ¡|j ¡t |ƒt|dƒ| d ¡|j d d Wd ƒn1s20Y| | ¡d¡d S)r†rr‡r8rJrÂrhs-user1:realm:2a6cf53e7d8f8cf39d946dc880b14128 rLr[z!load\(force=False\) is deprecatedF)ÚforceN)r9r rrrÁr;r<r`rˆr:r‰rArŠr r?rŒrrrrއs2       ,zHtdigestFileTest.test_05_loadcCs–| ¡}t||jƒt |¡}| dd¡| dd¡| ¡| t|ƒ|j ¡t ¡}|  ddd¡|  t |j¡| |¡| t|ƒ|  ¡¡dS)rrJrÂrKrhN)r9r r:rrÁrOrr;r rPr`rArŠr<r‘rrrr’°s     zHtdigestFileTest.test_06_savecCshtj |j¡}| | d¡d¡| | ¡dg¡| | d¡d¡| | ¡g¡| | ¡d¡dS)ztest realms() & delete_realm()r5rrÂér8N)rrÁrNr:r;Z delete_realmÚrealmsr<rRrrrÚtest_07_realmsÆs zHtdigestFileTest.test_07_realmscCsŒtj |j¡}| | dd¡d¡| | dd¡d¡| | dd¡d¡| d¡$| | dd¡d¡Wdƒn1s~0YdS) rœrrrÂZ a500bb8c02f6a9170ae46af10c898744ruZ ab7b5d5f28ccc7666315f508c7358519rLNr)rrÁrNr:r;rjr?ržrRrrrrŸÑs  z!HtdigestFileTest.test_08_get_hashcCs¢|jttjddtjj|jddd}| | ¡tdƒg¡| |  tdƒ¡tdƒg¡tjj|j ddd}| | ¡tdƒg¡| |  tdƒ¡tdƒg¡d S) ztest encoding parameterr“r”rTr–urealmær—r˜N) rArQrrÁrNr™r;rÈr rwršrRrrrÚtest_09_encodingsÛsz"HtdigestFileTest.test_09_encodingscCs<tj |j¡}| | ¡|j¡t ¡}| | ¡d¡dS)ztest to_string()r8N)rrÁrNr:r;r<rRrrrÚtest_10_to_stringêsz"HtdigestFileTest.test_10_to_stringcCs(| ttjjd¡| ttjjd¡dS)Nsrealm:user1:pass1:other re)rArQrrÁrNr£rrrr¤õs  ÿ ÿz"HtdigestFileTest.test_11_malformedN)r¹rºr»r¼r½r:rPrar™ršrFrSrVrdrxr…rŽr’rÉrŸrÊrËr¤rrrrrÀs( )   rÀ)r)N))r¼Z __future__rZloggingrrr!ZpasslibrrZ passlib.excrZpasslib.utils.compatrZpasslib.tests.backportsrZpasslib.tests.utilsr r r r r Z passlib.utilsrZpasslib.utils.handlersrr¹r0rÚenvironÚgetr#r+r.r1Z HAVE_HTPASSWDr·r¿r¾r2rÀrrrrÚs2            )