a fWc&@sdZddlmZddlmZddlZddlmZmZddl m Z m Z m Z m Z mZmZmZddlmZmZmZGdd d eZGd d d eZGd d d eZGdddeZddlmZmZGdddeZGdddeZdS)ztests for passlib.util)with_statement)partialN) is_ascii_safeto_bytes)irangePY2PY3uunicode join_bytesPYPY)TestCasehbrun_with_fixed_seedsc@seZdZdZddZddZddZdd Zd d Ze d d ddZ ddZ ddZ ddZ ddZddZddZddZddZd S)!MiscTestz#tests various parts of utils modulecCsLddlm}|t|dt||dt|v|dt|vdS)ztest compat's lazymoduler)compatz,^$Z UnicodeIOrN) passlib.utilsrZ assertRegexreprdir assertTrue)selfrr.testcSs|jSN)xvar)clsrrrxprop)sz/MiscTest.test_classproperty..test.xpropN)__name__ __module__ __qualname__rr rrrrtest'sr$rr ) passlib.utils.decorrobject assertEqualr __dict__assertIsim_func__func__)rr$proprrrtest_classproperty$s   zMiscTest.test_classpropertycCsxddlm}|ddddd}|d|jv|ttd d "||d d d Wdn1sj0YdS)Nr)deprecated_functionz1.6z1.8)Z deprecatedZremovedcWs|S)ztest docstringr)argsrrr test_func5sz4MiscTest.test_deprecated_function..test_funcz.. deprecated::zvthe function passlib.tests.test_utils.test_func() is deprecated as of Passlib 1.6, and will be removed in Passlib 1.8.)categorymessager)rr3)r%r.r__doc__ZassertWarningListdictDeprecationWarningr')rr.r0rrrtest_deprecated_function1s    z!MiscTest.test_deprecated_functioncsnddlmGfdddt}|}||jd||jd||jd|j}tsj||j|j dS)Nrmemoized_propertycseZdZdZddZdS)z.MiscTest.test_memoized_property..dummyrcSs|j}|d|_|S)Nr)counter)rvaluerrrr;Is z4MiscTest.test_memoized_property..dummy.valueN)r!r"r#r:r;rr8rrdummyFsr<r) r%r9r&r'r;r:rr)r*r+)rr<dr,rr8rtest_memoized_propertyCs  zMiscTest.test_memoized_propertycCszddlm}t||}|t|dd|d}|d}||t|t|d|t|d|||dS)zgetrandbytes()r) getrandbytes N) rr?r getRandomr'lenassertIsInstancebytesassertNotEqual)rr?wrapperabrrrtest_getrandbytesXs  zMiscTest.test_getrandbytesi)countcCsddlm}t||j|d}||ddd|t|dd|t|dd||ddd |tdd }|tdd }||t | |||t t |tdtd td g|d d }|d d }||t | |||t t |dtdtd td gdS)z getrandstr()r getrandstr)seedabcrGZaaaaa rHcabcasciiN)rrLrrAr' assertRaises ValueErrorr rCr rEsortedsetrDdecode)rrMrLrFxyrrrtest_getrandstrds     &    zMiscTest.test_getrandstrcCs0ddlm}tdd|t|dddS)zgenerate_password()r)generate_passwordignorez1The function.*generate_password\(\) is deprecatedN)rr^warningsfilterwarningsr'rB)rr^rrrtest_generate_passwords  zMiscTest.test_generate_passwordcCsDddlm}ddlm}|dg}|||||| dS)ztest is_crypt_context()r)is_crypt_context) CryptContextZ des_cryptN)rrdZpasslib.contextrer assertFalse)rrdreZccrrrtest_is_crypt_contexts    zMiscTest.test_is_crypt_contextcCsbddl}ddlm}||}|dd}||}|dd}||||||dS)ztest genseed()rN)genseedld( )randomrrhZRandomrandintrErM)rrirhrngrGrHrrr test_genseeds      zMiscTest.test_genseedcs*ddlm}m}m}ddlm}m}|}|sf||d||ddd||dd| d |st| d d D]}||vrxqqx|d }||t d drj j dfdd}td} || } || | } || t|| | ||t| t| | | ddd} ||| | | td} || }||| ||||t| t|||t|d| ||d| ||d| ddlm}|j}zpdfdd|_dD](||d| d||d| qd||d| d||d| W||_n||_0dS)ztest crypt.crypt() wrappersr) has_crypt safe_crypt test_crypt)get_supported_os_crypt_schemesget_crypt_handlerrr$ZaaNz aaqPiZY5xR5l.zcrypt.crypt() not availablez0crypt() present, but no supported schemes found!)Z md5_cryptZ sha256_cryptrP min_rounds)roundscs"|}t|tr|d}|S)Nutf-8)hash isinstancerDrZZsecretru)hasherrrget_hashs   z%MiscTest.test_crypt..get_hashxxutestሴcsSrrrw)retvalrrz%MiscTest.test_crypt..)NrO:z:0z*0Zxxx)rrmrnroZpasslib.registryrprqr'rfZskipTestZfailgetattrZusingrrr rCr rrVrWrZutilsZ_crypt)rrmrnrorprqZ supportedschemerys1Zh1resultZh1xs2Zh2modZorigr)rxr}rrosZ         zMiscTest.test_cryptc sddlmm|ttdd|ttdd|ttdd|tdtd|tdd|tdd|tdtd|tdd|tdtd|tddfdd}td td td d fD]r}||r|||d |fn|t|||||d |f|d}|||d |fqtd tdftdtdftdtdftdtdftd d ddtdtd d ftdtd ftd tdftd tdftdtd ff D]\}}||rP||rP|||d||f|||d||fn |t|||t|||||d||f|||d||f|d}|d}|||d||f|||d||fqdS)ztest consteq()rconsteq str_consteqrOrrNcsuptpt|Sr)rr)r;rrrconsteq_supports_stringsz6MiscTest.test_consteq..consteq_supports_stringrGrNuÿ¢r@z value %r:latin-1rSZabcabcZzbaabcZabzabcZabcabzrPZabcdefZdefabcZqwertyuiopasdfghjklzxcvbnmz values %r %r:) rrrrV TypeErrorr rencoderf)rrr;lrrrr test_consteqsZ       zMiscTest.test_consteqcCs*|ddlm}|t|d|t|d|t|d||tdtd||tdtd||tdtd ||td td ||td td ||td td |t|td|t|td|t|td|t|td|t|td|t|td|t|td|t|td|t|td|t|td|t|td|t|td|t|td|t|td|t|td||tdtd||tdtd||tdtd|t|td |t|td!||td"td"||td#td$||td%td%||td&td&||td'td(||td)td$|t|td*|t|td|t|td||tdtddS)+z"test saslprep() unicode normalizerr)saslprepNrrrO­u$­$‍$z$$$u $ $ $ $z$ $ $ $uààr|u᠎uuu﷐uu�u⿰u‎uuऀu￸u󠀁uا1uاuابuا1بuاAبuxاzZxAzuI­XZIXuserZUSERªrGuⅨ)Zrequire_stringpreprrrVrr'r rW)rsprrr test_saslprepesR zMiscTest.test_saslprepcCs|ddlm}||dg||dg||ddg||ddg||dddg||d ddgdS) Nr) splitcommarO,rGz a , z a , brHz a, b, )rrr')rrrrrtest_splitcommas zMiscTest.test_splitcommacCsRddlm}dD]}|dt|}t|dD]}d||f}|||}|d|jt|||d|jt|t|||d|jt|t|d|d |d|j ||d t||dq2qd D]L}t|}t|dD]2}d||f}|||}|j ||d ||dqqd D]T}t|}t|dD]8}d||f}|||}|j ||d |d |dq4qd}| ||dd| ||d d| ||dd| ||d d| ||dd| ||dd| ||dd| ||dd| ||dd| ||dd| ||dd| ||ddd S)z! utf8_truncate() r) utf8_truncate) r1s123s s sa þþs&abcdefghjusdfaoiu þþdsfioauweoiruerrtzsource=%r index=%r: )msgrN)s s )s s MNOPrMr3sMNsMNsMNrQsMNO r@ ) rrrZrBrangeZassertLessEqualassertGreaterEqualmin assertLessr')rrsourceendidxprefixrrrrtest_utf8_truncatesn     "     $          zMiscTest.test_utf8_truncateN)r!r"r#r4rr-r7r>rIrr]rcrgrlrorrrrrrrrrs     (VfO rc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS) CodecTestz,tests bytes/unicode helpers in passlib.utilscCsrtrddl}|t|jnddl}|t|j|dt|dttrb|dddn |dddS)z*test b() helper, bytes and native str typerNrrÿ) rbuiltinsr)rDZ __builtin__strrCr'rZ)rrrrr test_bytes:s  zCodecTest.test_bytescCsddlm}||tdd||tdd||tddd|t|tdd ||dd||dd||dd||ddd||ddd d d|t|dd |t|d d S) ztest to_bytes()r)rrNrTrÿrrrUrOrtN)rrr'r rVrWAssertionErrorr)rrrrr test_to_bytesJs   zCodecTest.test_to_bytescCsddlm}||tdtd||tdtd||tddtd||dtd||dtd||dd td|t|d|t|dd |t|d d S) ztest to_unicode()r) to_unicoderNrrUrTrrrN)rrr'r rVrWrr)rrrrrtest_to_unicodegs  zCodecTest.test_to_unicodecCsddlm}||tddd||dddtr`||tddd|t|ddn&|t|tdd||ddd||tdd d||d d d||tdd trdnd||dd trdnd|t|d dd S) ztest to_native_str()r) to_native_strrNrUrTrsàuàrrtN) rrr'r rrVUnicodeDecodeErrorUnicodeEncodeErrorr)rrrrrtest_to_native_str}s*     zCodecTest.test_to_native_strcCsPddlm}||d||td||d||tddS)ztest is_ascii_safe()r)rsabczabcsabcuabc€N)rrrr rf)rrrrrtest_is_ascii_safes  zCodecTest.test_is_ascii_safecCsddlm}||dd||dd||dd||dd||dd||dd||dd||dddS) ztest is_same_codec()r) is_same_codecNrUASCIIrtutf8ZUTF_8)rrrrf)rrrrrtest_is_same_codecs zCodecTest.test_is_same_codecN) r!r"r#r4rrrrrrrrrrr7src@s8eZdZdZddZddZddZdd Zd d Zd S) Base64EngineTestz%test standalone parts of Base64EnginecCsRddlm}m}|t|d|t||dd|t||ddddS)Nr) Base64Engine AB64_CHARSrrPA)passlib.utils.binaryrrrVrrW)rrrrrrtest_constructorsz!Base64EngineTest.test_constructorcCsddlm}||dtd||tdtd|t|td|t|d|t|d|t|td ||d td |t|d ||d td||dtd||dtd||dtd||dtddS)z ab64_decode()r) ab64_decoderT69b7rNabÿabab!ab!abcd69b71dabcdeabcdef69b71d79abcdeQabcdefg 69b71d79f8ab+/69bfbfab./N)rrr'rr rVrWr)rrrrrtest_ab64_decodes z!Base64EngineTest.test_ab64_decodecCsddlm}||tdd|tr,tnt|tdd||tdd||tdd ||td d ||td d dS)z ab64_encode()r) ab64_encoderrTrrrrrrrrrN) rrr'rrVrrrrZ)rrrrrtest_ab64_encodes z!Base64EngineTest.test_ab64_encodecCsddlm}||dtd||tdtd|t|td|t|d|t|d|t|td ||d td |t|d ||d td||dtd||dtddS)z b64s_decode()r) b64s_decoderTrrNrrrrrrrrrrrrN)rrr'rr rVrWr)rrrrrtest_b64s_decodes z!Base64EngineTest.test_b64s_decodecCsddlm}||tdd|tr,tnt|tdd||tdd||tdd ||td d ||td d dS)z b64s_encode()r) b64s_encoderrTrrrrrrrrrN) rrr'rrVrrrrZ)rrrrrtest_b64s_encode s z!Base64EngineTest.test_b64s_encodeN) r!r"r#r4rrrrrrrrrrs  rc@seZdZdZdZdZdZdZddZddZ dd Z d d Z d d Z ddZ ddZddZddgdfddgdfgZddgdfgZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,ZdS)- _Base64Testz+common tests for all Base64Engine instancesN?cstfdd|DS)z!generate byte string from offsetsc3s"|]}jj||dVqdS)rN)enginebytemap).0orrr 5rz _Base64Test.m..)r )roffsetsrrrm3sz _Base64Test.mcCs<|j}|j}|jD]$\}}||}|||d|fqdS)z,test encode_bytes() against reference inputsz encode %r:N)r encode_bytes encoded_datar')rrrrawencodedrrrrtest_encode_bytes:s z_Base64Test.test_encode_bytescCs0|j}|j}|t|td|t|ddS)z"test encode_bytes() with bad inputr|N)rrrVrr )rrrrrrtest_encode_bytes_badBsz!_Base64Test.test_encode_bytes_badcCs<|j}|j}|jD]$\}}||}|||d|fqdS)z,test decode_bytes() against reference inputsz decode %r:N)r decode_bytesrr')rrrZrrrrrrtest_decode_bytesLs z_Base64Test.test_decode_bytesc CsDtr ddnt}|j}|j}|j}d}|||dd|tddD]d}|jrn|dkr\|n|d|d>}n|dkr|d|d>n|}|||dd|>|d |qF|||ddd|dtddD]l}|jr|dkr|n|d|d>}n|dkr|d|d>n|}|||ddd|>||d |qd S) z(test decode_bytes() ignores padding bitscSs t|gSr)rD)vrrrr~Vrz7_Base64Test.test_decode_bytes_padding..rrrrrr3z %d/4 bits:z %d/2 bits:N)rchrrrrr'rbig)rZbchrrrrZZBNULLiZcorrectrrrtest_decode_bytes_paddingTs&"z%_Base64Test.test_decode_bytes_paddingcCst|j}|j}|t||jdd||j|jv|t||jd|t||jdd|t|ddS)z"test decode_bytes() with bad inputNrQr) rrrVrWrrbad_bytercharmap)rrrZrrrtest_decode_bytes_badosz!_Base64Test.test_decode_bytes_badc Cs2|j}ddlm}m}|}d}tdD]}||r|j}|jD],\}}}||}|||}|||q dS)ztest decode_transposed_bytes()N)rr rdecode_transposed_bytesr')rrrrrrrrrrtest_decode_transposed_bytess   z(_Base64Test.test_decode_transposed_bytescCs8|j}|jD]&\}}}||}|t|j||q dS)z8test decode_transposed_bytes() fails if map is a one-wayN)rrrrVrr)rrr_rrrrr test_decode_transposed_bytes_bads   z,_Base64Test.test_decode_transposed_bytes_badcCs|}|j}t|d|}t|d|}| d}||d}d|>} |D](\} } || } || t|| | qL|t|d|t|| |D] \} } ||| | d| fq|j} |t|| d|d|t|| d|d|t||j ||t ||j d|t |ddd l m }td D]}|d| d} || } |t| |||| | |||j|} || } || dd | || | d | || } |r|| dd | dd n || | qs(  $(i?