;Xc@sdZddlmZddlZddlZddlmZddlZddlm Z m Z ddl m Z m Z ddlmZmZmZddljjZddlmZmZdd lmZeeZd Zd efd YZd efdYZdefdYZ dej!fdYZ"dej#ej$fdYZ%edZ&defdYZ'defdYZ(dS(s>tests for passlib.hash -- (c) Assurance Technologies 2003-2009i(twith_statementN(t getLogger(tldap_md5t sha256_crypt(tMissingBackendErrortPasslibHashWarning(t str_to_uasciit uascii_to_strtunicode(t HandlerCasetTestCase(tucs"fdt|S(s1generate all strings of given size using alphabetc3s^|dkr%xKD] }|VqWn5x2D]*}x!|dD]}||VqCWq,WdS(Nii((tsizetcharttail(talphabetthelper(sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyRs     (tset(RR ((RRsE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyt _makelangst SkeletonTestcBszeZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d ZRS( stest hash support classescsAdtjffdY|jjtd|jjd|jjtd|jjtd|jjd|jjtd|jjtd|jjtd|jtjd |jtjd |j j j d |jj d d|jj d td|jj d d|jj d td|jj d dd t |jtj d d|jtj d td|j j d d|j j d d t dd S(stest StaticHandler classtd1csMeZdZdZedZedZdZefdZ dZ RS(Rtflagt_tabics#t|j|||_dS(N(tsupert__init__R(tselfRtkwds(R(sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyR9scSs|jrtdStdS(Ntbta(RR (Rtsecret((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyt_calc_checksum=s(sflag( t__name__t __module__tnamet context_kwdsR t _hash_prefixtchecksum_charst checksum_sizetFalseRR((R(sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyR2s  t_at_bt_cRRtcittsRN(tuht StaticHandlert assertTruetidentifyR t assertFalset assertRaisest TypeErrortNonet assertEqualt genconfigthashtverifytTruet ValueError(R((RsE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyttest_00_static_handler/s*cCsdtjfdY}|jd|jd}WdQX|j|d|j|jd||j|jd||`|j t |jddS(stest StaticHandler legacy attrRcBs;eZdZedZedZedZRS(RcSsG| st|dkrtSyt|dWntk rBtSXtS(Ni(i(tlenR'tintR;R:(tclsR8((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyR1fs cSs~|dkrtdnt|tr<|jd}n|dk rg|j| rgtdntjd|j S(Nsno secret providedsutf-8s invalid hashtxyz( R5R4t isinstanceRtencodeR1R;thashlibtsha1t hexdigest(R?RR8((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pytgenhashps cSs7|dkrtdn|j|||jkS(Nsno hash specified(R5R;RFtlower(R?RR8((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyR9{s (R R!R"t classmethodR1RFR9(((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyRcs  s%d1.*should be updated.*_calc_checksumttestNt(7c622762588a0e5cc786ad0a143156f9fd38eea3txtest( R.R/tassertWarningListR8R6R0R9R2RFR3tNotImplementedError(RRR8((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyttest_01_calc_checksum_hack]scCsndtjfdY}|jt|jd |jt|jd|j|jd|j|jd|j|jdtj t d|_ |jt|jd |jt|jd|j|jd|j|jd|` t d |_ |jt|jd |jt|jd|j|jd |j|jd|` d S( stest GenericHandler.identify()RcBseZedZRS(cSsJt|tr!|jd}n|tdkr@|d|StdS(NtasciiRtchecksum(RAtbytestdecodeR R;(R?R8((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyt from_strings  (R R!RHRS(((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyRsiR,RRs@.s@at!s!aN( R.tGenericHandlerR3R4R1R5R2R0tretcompileR t _hash_regextident(RR((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyttest_10_identifys$ csdtjfdYd fd}|jt|td|j|tdtd|j|tdtd|jt|td|jt|td|jt|d|jjtdd S( s%test GenericHandler checksum handlingRcBs eZdZdZedZRS(Ritxz(R R!R"R&R R%(((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyRscsd||jS(NRP(RP(RPtk(R(sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyt norm_checksumstxxxtxxxxtxzxztxxxxxtxxyxN( R.RUR5R3R;R R6R4t_stub_checksum(RR]((RsE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyttest_11_norm_checksums"" cswdtjtjfdYfd}|j|dd|jt|td|jjd dS( s*test GenericHandler + HasRawChecksum mixinRcBseZdZdZRS(Ri(R R!R"R&(((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyRscs||jS(N(RP(RR\(R(sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyR]st1234RbtiNt(R.tHasRawChecksumRUR6R3R4R Rc(RR]((RsE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyttest_12_norm_checksum_raws c sdtjtjfdYfd}fd}tdd}tdd}tdd}|jt||jt|d d|j|d t|t j d t}|jt |d d |jt |d d |j ||j |d dd|j |d dd|j |d dd|j ||jt |d d|j |WdQXt j d t}|jt |d|jt |d|j ||j|d||j|d||j|d||j ||jt |d|j ||j|ddt||j |dgWdQX`|jg=|j t|dd|j t|ddWdQXdS(s#test GenericHandler + HasSalt mixinRcBs,eZdZdZdZdZdZdZRS(RtsaltiiiR(ssalt(R R!R"t setting_kwdst min_salt_sizet max_salt_sizetdefault_salt_sizet salt_chars(((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyRs cs |jS(N(Rj(R\(R(sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyt norm_saltscsjd||dtjS(Nt salt_sizet use_defaults(tusingR:Rj(tszR\(R(sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pytgen_saltsRiiiRjRrtrecordR,RtabatabbataaaabbNiiitrelaxedssalt_size.*above max_salt_size(R.tHasSaltRURR3R4R5tassertInR:twarningstcatch_warningsR;tconsumeWarningListR6RmRLR=(RRpRutsalts2tsalts3tsalts4twlog((RsE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyttest_20_norm_saltsF     c s_dtjtjfdYfd}|jt||jt|dd |j|dtd|jt|ddtj dt}|jt |dd |j ||j|dd d |j|ddd|j|dd d |j ||jt |dd |j |Wd QXd _ |jt|dtd S(s%test GenericHandler + HasRounds mixinRcBs&eZdZdZdZdZdZRS(Rtroundsiii(srounds(R R!R"Rkt min_roundst max_roundstdefault_rounds(((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyR8s cs |jS(N(R(R\(R(sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyt norm_rounds@sRRrig?RviiiiN( R.t HasRoundsRUR3R4R5R6R:R}R~R;RR(RRR((RsE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyttest_30_init_rounds5s"   cCsdtjtjfdY}|jt|j|jt|j|jt|jd|jt|jd|j|jt |_ |}|j |j dd|jd|jd|j |j dd|jt|jd|j |jd|j|jdt |_|j |j|jd|j |j dd|jt|jd|jt|jd|jdd |fd Y}|jt|jdd S( s+test GenericHandler + HasManyBackends mixinRcBsVeZdZdZdZeZeZedZ edZ dZ dZ RS( RRRcSs%|jr|j|jtStSdS(N(t _enable_at_set_calc_checksum_backendt_calc_checksum_aR:R'(R?((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyt_load_backend_ajs cSs%|jr|j|jtStSdS(N(t _enable_bRt_calc_checksum_bR:R'(R?((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyt_load_backend_brs cSsdS(NR((RR((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyRzscSsdS(NR((RR((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyR}s((RR( R R!R"RktbackendsR'RRRHRRRR(((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyRas tanytdefaultR-RRR+td2cBseZeZRS((R R!R:t_has_backend_a(((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyRsN(R.tHasManyBackendsRUR3Rt get_backendt set_backendR2t has_backendR:RR6RR0RR;tAssertionError(RRtobjR((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyttest_40_backends_s0        cCstjddtdddtjtjfdY}|jt|j|jt|j |jt|j d|jt|j d|j |j t |_ |}|j|jd d |j d |j d|j|jd d |jt|j d |j|j d |j |j d t |_|j|j |j d |j|jd d |jt|j d |jt|j d d S(s<test GenericHandler + HasManyBackends mixin (deprecated api)tignoretcategorytmessages0.* support for \._has_backend_.* is deprecated.*RcBs8eZdZdZdZeZeZdZdZ RS(RRRcSsdS(NR((RR((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyRscSsdS(NR((RR((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyRs((RR( R R!R"RkRR'Rt_has_backend_bRR(((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyRs RRR-RRR+N(R}tfilterwarningstDeprecationWarningR.RRUR3RRRR2RR:RR6RR0RR;(RRR((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyttest_41_backendss0       csdtjtjfdYfd}|jt||jt|dd|j|dttd|j|dtdtd|j|dtdtd|jt |dtd|j|dtd td|jt |dtd |j j td |j j td |j j td |j j td |j j td|jtj d|jtj dd_ |jt|dtdS(s#test GenericHandler + HasManyIdentsRcBsQeZdZdZedZededfZieded6ZRS(RRYs!As!BtA(sident(R R!R"RkR t default_identt ident_valuest ident_aliases(((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyRs  cs |jS(N(RY(R\(R(sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyt norm_identsRYRrs!As!Bs!CRtBs!Axxxs!Bxxxs!CxxxR,iN(R.t HasManyIdentsRUR3R4R5R6R:R R;R0R1R2RR(RR((RsE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyttest_50_norm_idents&%%% c Csddlm}|jjd}|j|itdd6tdd6d}td }td }|jj|}|j|td|d d d td||jj|dt }|j|td|d d d t|jj|dt}|j|td d d tdtddtd|jjd}|j|tdtddtdd dd}|j j|}|j|tddd ddd|j j|dt}|j|tdtdd ddtddS(stest parsehash()i(R8t OgAwTx2l6NADIt AwTx2l6NADIRPtOgRjs?$5$LKO/Ute40T3FNF95$U0prpBQd4PloSGU0pnpM4z9wKn4vZ1.jsrzQfPqxph9sLKO/Ute40T3FNF95s+U0prpBQd4PloSGU0pnpM4z9wKn4vZ1.jsrzQfPqxph9Ritimplicit_roundstsanitizesLK**************s+U0pr***************************************sL$5$rounds=10428$uy/jIAhCetNCTtb0$YWvUOXbkqlqhyoPMpN8BMe.ZGsGx2aBvxTvDFI613c3s+YWvUOXbkqlqhyoPMpN8BMe.ZGsGx2aBvxTvDFI613c3suy/jIAhCetNCTtb0i(s@$pbkdf2$60000$DoEwpvQeA8B4T.k951yLUQ$O26Y3/NJEiLCVaOVPxGXshyjW8ks;nI"U?[i`s0xO=\QsO26************************sDo********************N( tpasslibR8t des_cryptt parsehashR6R RtdictR:R't pbkdf2_sha1(RR8tresultthR-R+th1((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyttest_91_parsehashs>*  %      cCsddlm}|j|jjidd6dd6|j|jjidd6dd6|j|jd d d>|j|jjid d6d d6dd6|j|jd d d>|j|jjidd6dd6dd6dS(stest bitsize()i(R8iBRPi RjiiRiiiiRi`i iiNii( RR8R6Rtbitsizetbcryptt patchAttrRR(RR8((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyttest_92_bitsizeJs(R R!t__doc__R<RNRZRdRiRRRRRRR(((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyR)s . 3 # $  I * K : . 8tdummy_handler_in_registrycBs)eZdZdZdZdZRS(s6context manager that inserts dummy handler in registrycCs;||_td|tjftd|dd|_dS(Ntdummy_R"Rk((R"ttypeR.RURtdummy(RR"((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyRps cCs=ddlm}|j|jdt|j|j|jS(Ni(tregistryt locations(RRt_unload_handler_nameR"R'tregister_crypt_handlerR(RR((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyt __enter__wscGs*ddlm}|j|jdtdS(Ni(RR(RRRR"R'(Rtexc_infoR((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyt__exit__~s(R R!RRRR(((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyRns  tPrefixWrapperTestcBsVeZdZdZdZdZdZdZdZdZ dZ RS( stest PrefixWrapper classcCstjdddddt}|j|jd|j|jd|j|jt |j|jt t d}|j|jt WdQXdS(s*test PrefixWrapper lazy loading of handlerRRs{XXX}s{MD5}tlazyN( R.t PrefixWrapperR:R6t _wrapped_nametassertIst_wrapped_handlerR5twrappedRR(RRR((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyttest_00_lazy_loadingscCs}tjdddd}|j|jd|j|jt|j|jttd}|j|jtWdQXdS(s,test PrefixWrapper active loading of handlerRRs{XXX}s{MD5}N( R.RR6RRRRRR(RRR((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyttest_01_active_loadings cCs}tjdtdd}|j|jd|j|jt|j|jtt d}|j|jtWdQXdS(s4test PrefixWrapper with explicitly specified handlerRs{XXX}s{MD5}RN( R.RRR6RR5RRRR(RRR((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyttest_02_explicits cCstjdddd}|j|jd|j|jtj|jdt|ktjddd}|j|jt j|j dt|kdS(NRRs{XXX}s{MD5}RRR( R.RR6R"RRkRR2tdirRR0(RRR((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyttest_10_wrapped_attributesscCstjdddd}d}d}|j|jd|jt|jdd|j|jd|||jt|jd||j|j d||j |j ||j |j ||jt|j d||j |j d|dS( NRRs{XXX}s{MD5}s{XXX}X03MO1qnZdYdgyfeuILPmQ==s{MD5}X03MO1qnZdYdgyfeuILPmQ==s{XXX}1B2M2Y8AsgTpgAmY7PhCfg==tpassword(R.RR6R7R3R4RFR5R;R8R0R1R2R9(RRtdphtlph((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyttest_11_wrapped_methodssc Csqtjddd}|j|jtd|j|jdtjddd}|j|jd|j|jdtjdddd}|j|jd|j|jdtjdddd d }|j|jtd |j|jdtjdddd d }|jt tjdddd d |jt tjdddd d tjddd}|j|jd|j|jtdtdftjdddd t }|j|jtd|j|jd|jt tjddd t |j d#tjdddddd}WdQX|j|jd|j|jddS(Nth2Rs{XXX}s {XXX}{MD5}RRs{MD5}th3RYs{Xs{XXX}As{XYs{XXXXth4tphpasss{XXX}$P$s{XXX}$H$th5th6s#orig_prefix.*may not work correctlyth7t orig_prefixt$tprefixt?( R.RR6RYR RRR5R3R;R:RL(RR((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyt test_12_idents8  %$cCs5tjddddd}|jt|ddS(s test repr()Rt md5_crypts{XXX}Rs$1$s(?x)^PrefixWrapper\( ['"]h2['"],\s+ ['"]md5_crypt['"],\s+ prefix=u?["']{XXX}['"],\s+ orig_prefix=u?["']\$1\$['"] \)$N(R.Rt assertRegextrepr(RR((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyt test_13_reprscCs2tjdddd}|jt|jddS(stest orig_prefix sanity checkRRRs$6$RIN(R.RR3R;R8(RR((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyttest_14_bad_hash s( R R!RRRRRRRRR(((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyRs    / t UnsaltedHashcBs,eZdZdZejZdZdZRS(s!test algorithm which lacks a salttunsalted_test_hashi(cCsDt|tr!|jd}nd|}ttj|jS(Nsutf-8tboblious(RARRBRRCRDRE(RRtdata((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyRs ( R R!RR"R.tLOWER_HEX_CHARSR%R&R(((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyRs  t SaltedHashcBsoeZdZdZd ZdZdZdZej Z Z e j edZedZdZd ZRS( stest algorithm with a salttsalted_test_hashRjiii(s^@salt[0-9a-f]{42,44}$cCsc|j|s$tjj|nt|trE|jd}n|d|dd!d|dS(NRORjiiRP(R1R.texctInvalidHashErrorRARQRR(R?R8((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyRS0s cCs&td|j|jf}t|S(Ns @salt%s%s(R RjRPR(RR8((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyt to_string8scCs`t|tr!|jd}n|jjd||jjd}ttj|jS(Nsutf-8RO(RARRBRjRRCRDRE(RRR((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyR<s&(ssalt(R R!RR"RkRlRmR&R.RRoR%RVRWR RXRHRSRR(((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyR$s  s*\u0399\u03c9\u03b1\u03bd\u03bd\u03b7\u03c2tUnsaltedHashTestcBs)eZeZdedfgZdZRS(Rt(61cfd32684c47de231f1f982c214e884133762c0t(96b329d120b97ff81ada770042e44ba87343ad2bcCs3|jttdd|jttjdddS(NRjtxRi(R3R4RR7(R((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyt test_bad_kwdsSs(spasswordR(R R!Rthandlert UPASS_TEMPtknown_correct_hashesR(((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyRKstSaltedHashTestcBs)eZeZdedfgZdZRS(Rs/@salt77d71f8fe74f314dac946766c1ac4a2a58365482c0s1@salt9f978a9bfe360d069b0c13f2afecd570447407fa7e48cCsNtdtj}|jttd|dd|jttd|dddS(NRrRPRjR^(RR:RcR3R4R5R;(Rtstub((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyR_s(spasswords/@salt77d71f8fe74f314dac946766c1ac4a2a58365482c0(R R!RRRRR(((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyRWs()Rt __future__RRVRCtloggingRR}t passlib.hashRRt passlib.excRRtpasslib.utils.compatRRRtpasslib.utils.handlerstutilsthandlersR.tpasslib.tests.utilsR R R R tlogRRtobjectRRR/RR{RURRRR(((sE/usr/lib/python2.7/site-packages/passlib/tests/test_utils_handlers.pyts.     G %