o
    5hU@                    @   s  d Z ddlmZ ddlmZ ddlZddlmZmZ ddl	Z	ddl
Z
e
eZddlZddlZddlZddlZddlZddlZddlZddlmZmZ ddlmZmZ ddlZddlmZ dd	lmZ dd
lmZ ddl m!Z! ddl"m#Z$m%Z%m&Z&m'Z'm(Z( ddl)m*Z*m+Z+m,Z,m-Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4 ddlm5Z5m6Z6m7Z7m8Z8m9Z9m:Z: ddl;m<Z< ddl=m>  m?Z@ g dZAzddlBZCW n eDy   dZEY nw dZEdd ZFdd ZGeGe3ZHg dZIeIJejKLddM N ZOdNddZPdd ZQdOddZRdd  ZSdPd!d"ZTd#d$ ZUd%d& ZVd'd( ZWejXd)d* ZYd+d, ZZd-d. Z[d/d0 Z\d1d2 Z]d3d4 Z^d5d6 Z_dQd9d:Z`dRd=d>ZaG d?d@ d@e$Z#dAdgZbdBdC ZcG dDdE dEe#ZdG dFdG dGedZeG dHdI dIedZfG dJdK dKedZgG dLdM dMejhZidS )Szhelpers for passlib unittests    )with_statement)	unhexlifyN)wrapspartial)PasslibHashWarningPasslibConfigWarning)PY3JYTHON)warn)exc)MissingBackendError)TestCaseskipskipIf
skipUnlessSkipTest)
has_rounds_infohas_salt_inforounds_cost_valuesrng
getrandstris_ascii_safeto_native_strrepeat_stringtickbatch)	iteritemsirangeuunicodePY2nullcontext)classproperty)	TEST_MODEset_fileget_filer   HandlerCaseFTc                 C   sJ   t j| }t j| |kr#td t | d t j| |ksdS dS )zensure file's mtime has changed皙?N)ospathgetmtimetimesleeputime)r)   last r/   g/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/passlib/tests/utils.pyensure_mtime_changed;   s
   
r1   c                    s&   fdd t  fddtdD S )Nc                     s(      } }| |kr  }| |ks	||  S Nr/   )startcurtimerr/   r0   sampleE   s
   
z%_get_timer_resolution.<locals>.samplec                 3   s    | ]}  V  qd S r2   r/   .0_)r7   r/   r0   	<genexpr>J   s    z(_get_timer_resolution.<locals>.<genexpr>   )minranger5   r/   )r7   r6   r0   _get_timer_resolutionD   s   r?   )quickdefaultfullPASSLIB_TEST_MODErA   c                 C   s0   | rt t| k rdS |rt t|krdS dS )a  check if test for specified mode should be enabled.

    ``"quick"``
        run the bare minimum tests to ensure functionality.
        variable-cost hashes are tested at their lowest setting.
        hash algorithms are only tested against the backend that will
        be used on the current host. no fuzz testing is done.

    ``"default"``
        same as ``"quick"``, except: hash algorithms are tested
        at default levels, and a brief round of fuzz testing is done
        for each hash.

    ``"full"``
        extra regression and internal tests are enabled, hash algorithms are tested
        against all available backends, unavailable ones are mocked whre possible,
        additional time is devoted to fuzz testing.
    FT)
_test_mode_TEST_MODESindex)r=   maxr/   r/   r0   r#   T   s
   r#   c                 C   s$   t | drdS d| jv pt| tjS )z'check if handler supports 'relaxed' kwdorig_prefixFrelaxed)hasattrsetting_kwds
issubclassuhGenericHandlerhandlerr/   r/   r0   has_relaxed_settingp   s
   
rQ   c                 C   s   t | } | |ddjS )z'get effective rounds value from handlerT)roundsuse_defaults)unwrap_handlerrR   )rP   rR   r/   r/   r0   get_effective_rounds|   s   rU   c              	   C   sR   z|   }W n
 ty   Y dS w z| d |   |kW | | S | | w )z*check if backend is the default for sourceFrA   )get_backendr   set_backend)rP   backendorigr/   r/   r0   is_default_backend   s   
rZ   c                 c   s\    |du r	|   }| j}|r||d nd}||d D ]}||kr+| |r+|V  qdS )z
    iterate over alternate backends available to handler.

    .. warning::
        not thread-safe due to has_backend() call
    N   r   )rV   backendsrF   has_backend)rP   currentfallbackr\   idxrX   r/   r/   r0   iter_alt_backends   s   ra   c                  O   s   t | i |D ]}|  S d S r2   )ra   )argskwdsrX   r/   r/   r0   get_alt_backend   s   rd   c                 C   s   t | dr| j} t | ds| S )z5return original handler, removing any wrapper objectswrapped)rJ   re   rO   r/   r/   r0   rT      s   

rT   c                 C   sd   | |krdS t | tjr| r| |krdS | j} | sdS t | tr+t| tjr+t| |S td| f )zG
    test if <handler> was derived from <base> via <base.using()>.
    TFz%don't know how to inspect handler: %r)
isinstancerM   PrefixWrapper_derived_fromtyperL   MinimalHandlerNotImplementedError)rP   baser/   r/   r0   handler_derived_from   s   
rm   c                 #   s    t | tr&t| tjr&| j  fdd}|| _z	dV  W  | _dS  | _w t | tjrGt| j dV  W d   dS 1 s@w   Y  dS dV  dS )a  
    internal helper for do_config_encrypt() --
    context manager which temporarily replaces handler's _calc_checksum()
    with one that uses min_rounds; useful when trying to generate config
    with high rounds value, but don't care if output is correct.
    c                    s6   | j }z| j| _  | g|R i |W || _ S || _ w r2   )rR   
min_rounds)selfrb   rc   rR   re   r/   r0   wrapper   s
   z&patch_calc_min_rounds.<locals>.wrapperN)	rf   ri   rL   rM   	HasRounds_calc_checksumrg   patch_calc_min_roundsre   )rP   rq   r/   rp   r0   rt      s   "rt   c                 C   sN   t |tr
|d}t| d}|| W d   dS 1 s w   Y  dS )zset file to specified bytesutf-8wbN)rf   r   encodeopenwrite)r)   contentfhr/   r/   r0   r$      s
   

"r$   c                 C   s6   t | d}| W  d   S 1 sw   Y  dS )zread file as bytesrbN)rx   read)r)   r{   r/   r/   r0   r%      s   $r%   c                 C   sF   t | ts| S tr| dS z| dW S  ty"   | d Y S w )z*convert native string to non-native stringru   latin-1)rf   strr   rw   decodeUnicodeDecodeErrorsourcer/   r/   r0   tonn   s   

r   c                 C   s   t tdd| S )zT
    helper for represent byte strings in hex.

    usage: ``hb("deadbeef23")``
    z\s )r   resubr   r/   r/   r0   hb   s   r   c                 C   s   | |k r|S | |kr|S | S r2   r/   )valuelowerupperr/   r/   r0   limit   s
   r   c                 C   s,   t  }t  | | k r	 t  | | k s
dS dS )zAbecause time.sleep() doesn't even have 10ms accuracy on some OSesNr   )delayr3   r/   r/   r0   
quicksleep  s   r   r[   
   c           
      C   sr   ddl m} ddlm} || |pdd}d}t | }	 t|||}	t |kr4|	| t||dfS |d9 }q)	z
    timeit() wrapper which tries to get as accurate a measurement as possible w/in maxtime seconds.

    :returns:
        ``(avg_seconds_per_call, log10_number_of_repetitions)``
    r   )Timerlogr   )setupr[   Tr   )timeitr   mathr   r   r=   repeatint)
funcr   maxtimebestofr   r   r6   numberenddeltar/   r/   r0   	time_call	  s   

r         F"*! c                    s    fdd}|S )z<
    decorator run test method w/ multiple fixed seeds.
    c                    s   t   fdd}|S )Nc                     s8   t }t D ]}|d|d< | i | q	d S )N    seed)randomRandomr   getrandbits)rb   rc   r   r:   )countr   master_seedr/   r0   rq      s
   
z6run_with_fixed_seeds.<locals>.builder.<locals>.wrapper)r   )r   rq   r   r   r   r0   builder  s   z%run_with_fixed_seeds.<locals>.builderr/   )r   r   r   r/   r   r0   run_with_fixed_seeds  s   r   c                       s8  e Zd ZdZdZ fddZedd Zedd Zd	Z	d	Z
 fd
dZdd Zd	Zdd Zd3 fdd	Zdd Ze ZZ					d4ddZG dd dejZd5ddZd3ddZdd Zdd Zd d! Zd"d# Zd$d% Ze Z dZ!dZ"d6d'd(Z#e$e%d)Z&e'j( fd*d+Z)dZ*d,d- Z+d7d/d0Z,d1d2 Z-  Z.S )8r   aR  passlib-specific test case class

    this class adds a number of features to the standard TestCase...
    * common prefix for all test descriptions
    * resets warnings filter & registry for every test
    * tweaks to message formatting
    * __msg__ kwd added to assertRaises()
    * suite of methods for matching against warnings
    Nc                    s0   t t|  }| j}|rd||pt| f }|S )z;wrap shortDescription() method to prepend descriptionPrefixz%s: %s)superr   shortDescriptiondescriptionPrefixr   )ro   descprefix	__class__r/   r0   r   B  s
   zTestCase.shortDescriptionc                 C   s    | j }|dpt| d| dS )Nr:   z_%s__unittest_skipF)__name__
startswithgetattr)clsnamer/   r/   r0   __unittest_skip__N  s   
zTestCase.__unittest_skip__c                 C   s   | j  S r2   )r   r   r/   r/   r0   __test__U  s   zTestCase.__test__Tc                    s(   t t|   |   | tdd d S )NENABLE_DEBUG_ONLY_REPRT)r   r   setUpsetUpWarnings	patchAttrr   ro   r   r/   r0   r   d  s   zTestCase.setUpc                 C   sL   | j r$t }|  | |j tdd tdd tdd dS dS )z6helper to init warning filters before subclass setUp()ignorez<the method .*\.(encrypt|genconfig|genhash)\(\) is deprecatedz&the 'vary_rounds' option is deprecatedz0Support for `(py-bcrypt|bcryptor)` is deprecatedN)resetWarningStatereset_warnings	__enter__
addCleanup__exit__warningsfilterwarnings)ro   ctxr/   r/   r0   r   j  s   	zTestCase.setUpWarningsc                 C   s0   | j r|r| drd| |f S |p|S )N:z%s %s)longMessagerstripendswith)ro   msgstdr/   r/   r0   _formatMessage  s   zTestCase._formatMessagec           	   
      s   | dd }|d u rtt| j|d g|R i |S z	||i |}W n |y7 } z|W  Y d }~S d }~ww d||f }| | ||)N__msg__z-function returned %r, expected it to raise %r)popr   r   assertRaisesfailureExceptionr   )	ro   	_exc_type	_callablerb   rc   r   resulterrr   r   r/   r0   r     s"   zTestCase.assertRaisesc                 O   s   t d)Nz%this alias is deprecated by unittest2)AssertionError)ro   akr/   r/   r0   assertEquals  s   zTestCase.assertEqualsc	                 C   s   t |dr|}	|j}nd}	|r| t||| |r#| t||| |r,| ||| |s0|r[|	s6td|	j}
|
dsC|
drI|
dd }
|rR| |
|| |r[| |
|| |rm|	sctd| |	j	|| dS dS )zcheck if warning matches specified parameters.
        'warning' is the instance of Warning to match against;
        can also be instance of WarningMessage (as returned by catch_warnings).
        categoryNz7matching on filename requires a WarningMessage instancez.pycz.pyoz5matching on lineno requires a WarningMessage instance)
rJ   messageassertEqualr   assertRegexassertIsInstance	TypeErrorfilenamer   lineno)ro   warning
message_rer   r   filename_rer   r   r   wmsgrealr/   r/   r0   assertWarning  s2   
zTestCase.assertWarningc                       s0   e Zd ZdZ fddZdd Zdd Z  ZS )zTestCase._AssertWarningListz'context manager for assertWarningList()c                    s,   || _ || _ttj| | _| jjdd d S )NTrecord)caserc   r   r   _AssertWarningList_AssertWarningList__super__init__)ro   r   rc   r   r/   r0   r     s   z$TestCase._AssertWarningList.__init__c                 C   s   | j  | _d S r2   )r   r   r   r   r/   r/   r0   r     s   z%TestCase._AssertWarningList.__enter__c                 G   s8   | j j|  |d d u r| jj| jfi | j d S d S Nr   )r   r   r   assertWarningListr   rc   )ro   exc_infor/   r/   r0   r     s   z$TestCase._AssertWarningList.__exit__r   
__module____qualname____doc__r   r   r   __classcell__r/   r/   r   r0   r     s
    r   c              	   C   s  |du r|dus
J | j | ||dS |dusJ t|ttfs"|g}t|D ]D\}}t|tr5t|d}nt|trEt|t	rEt|d}n	t|tsNt
dz|| }W n
 ty^   Y  nw | j|fd|i| q&t|t|krudS dt|t|| ||f }| | ||)zBcheck that warning list (e.g. from catch_warnings) matches patternN)r   r   )r   r   z#entry must be str, warning, or dictr   z0expected %d warnings, found %d: wlist=%s desc=%r)r   rf   listtuple	enumerater   dictri   rL   Warningr   
IndexErrorr   len_formatWarningListr   r   )ro   wlistr   r   r`   entrydatar   r/   r/   r0   r     s2   

zTestCase.assertWarningListc                 O   s4   |du rg }| j ||g|R i | |dd= dS )zD[deprecated] assertWarningList() variant that clears list afterwardsN)r   )ro   r  r   rb   rc   r/   r/   r0   consumeWarningList   s   zTestCase.consumeWarningListc                 C   sZ   d}t |drd|j|jf }|jr|d|jf 7 }|j}t|}d|j|jt||f S )Nr   r   z filename=%r lineno=%rz line=%rz<%s.%s message=%r%s>)	rJ   r   r   liner   ri   r   r   r   )ro   r  tailr   r/   r/   r0   _formatWarning  s   

zTestCase._formatWarningc                    s   dd  fdd|D  S )Nz[%s], c                 3   s    | ]}  |V  qd S r2   )r	  )r9   r  r   r/   r0   r;     s    z.TestCase._formatWarningList.<locals>.<genexpr>)join)ro   r  r/   r   r0   r    s   zTestCase._formatWarningListc                 C   s.   ddl m} |sddl m} | d| dS )z,helper to skip test if stringprep is missingr   )
stringprep)_stringprep_missing_reasonz%not available - stringprep module is N)passlib.utilsr  r  skipTest)ro   r  r  r/   r/   r0   require_stringprep  s   zTestCase.require_stringprepc                 C   s   t |s| d| dS )z8skip test for all PASSLIB_TEST_MODE values below <level>zrequires >= %r test modeN)r#   r  )ro   levelr/   r/   r0   require_TEST_MODE!  s   zTestCase.require_TEST_MODEc                 C   s   t r| dS dS )z'skip test if writeable FS not availablez.GAE doesn't offer read/write filesystem accessN)GAEr  r   r/   r/   r0   require_writeable_filesystem&  s   
z%TestCase.require_writeable_filesystemrA   c           	      C   s&  | j }|r||v r|| S | jy | j }|r%||v r%|| W  d   S |s,i  }| _ |p0tj}|du rQttjdpFtjdpFt	d }t_t
d| t| }dt||j|j| j|g}t|d }t|dd d}t| }||< |W  d   S 1 sw   Y  dS )	aU  
        Return a :class:`random.Random` object for current test method to use.
        Within an instance, multiple calls with the same name will return
        the same object.

        When first created, each RNG will be seeded with value derived from
        a global seed, the test class module & name, the current test method name,
        and the **name** parameter.

        The global seed taken from the $RANDOM_TEST_SEED env var,
        the $PYTHONHASHSEED env var, or a randomly generated the
        first time this method is called. In all cases, the value
        is logged for reproducibility.

        :param name:
            name to uniquely identify separate RNGs w/in a test
            (e.g. for threaded tests).

        :param seed:
            override global seed when initialzing rng.

        :rtype: random.Random
        NRANDOM_TEST_SEEDPYTHONHASHSEEDr   zusing RANDOM_TEST_SEED=%d
ru      )_random_cache_random_global_lockr   _random_global_seedr   r(   environgetsys_rngr   r   infori   r  r   r   r   _testMethodNamehashlibsha256rw   	hexdigestr   r   )	ro   r   r   cacheglobal_seedr   r   digestr   r/   r/   r0   	getRandom9  s8   


$zTestCase.getRandomsubTestc                 /   s    ddd}|   }||i |}| jr tt| j|i |}nt }|< |d| zdV  W n( ty@   |d| Y n tyY } z|	d|t
|jt|  d}~ww W d   n1 sdw   Y  |d| dS )	zt
        wrapper/backport for .subTest() which also traps SkipTest errors.
        (see source for details)
        Nc                 [   s@   | rd|  nd}|r|dd dd | D  7 }| pdS )Nz[%s] r   z(%s) c                 s   s    | ]	}d t | V  qdS )z%s=%rN)r   )r9   itemr/   r/   r0   r;     s    z:TestCase.subTest.<locals>._render_title.<locals>.<genexpr>z	<subtest>)r  itemsstrip)_msgparamsoutr/   r/   r0   _render_title  s    z'TestCase.subTest.<locals>._render_titlezrunning subtest: %szsubtest skipped: %szsubtest failed: %s: %s: %rzsubtest passed: %sr2   )	getLoggerhas_real_subtestr   r   r(  r!   r  r   	Exceptionr   ri   r   r   )ro   rb   rc   r0  test_logtitler   r   r   r/   r0   r(  |  s,   

zTestCase.subTestc                    sb   |    tj|i |\}}t| | j  du r*g   | _ fdd}| |  | |S )z1create temp file that's cleaned up at end of testNc                     s.    D ]} t j| rt |  q d d = d S r2   )r(   r)   existsremove)r)   queuer/   r0   cleaner  s
   
z TestCase.mktemp.<locals>.cleaner)r  tempfilemkstempr(   close_mktemp_queuer   append)ro   rb   rc   fdr)   r:  r/   r8  r0   mktemp  s   



zTestCase.mktempFc                    sz   zt  }W n ty   |r  fdd}| | Y n	w | t | |r5t||}t|| t | dS )z=monkeypatch object value, restoring original value on cleanupc                      s&   zt   W d S  ty   Y d S w r2   )delattrAttributeErrorr/   attrobjr/   r0   cleanup  s
   z#TestCase.patchAttr.<locals>.cleanupN)r   rC  r   setattrr   r   )ro   rF  rE  r   require_existingwraprY   rG  r/   rD  r0   r     s   

zTestCase.patchAttrc                 C   s@   t | }|jd t|d|j }| j}|r|d | }t|S )z9
        return logger named after current test.
        .r   )ri   r   r   r   r   loggingr1  )ro   r   r)   r   r/   r/   r0   r1    s   
zTestCase.getLoggerr2   )NNNNNNN)NNN)rA   N)TF)/r   r   r   r   r   r   r"   r   r   _TestCase__unittest_skipr   r   r   r   r   r   r   assertNotEqualsassertRegexMatchesr   r   catch_warningsr   r   r  r	  r  r  r  r  	threadingLockr  r  r  r'  rJ   	_TestCaser2  
contextlibcontextmanagerr(  r>  rA  r   r1  r   r/   r/   r   r0   r   -  sT    



0



A8
r   anyc                 C   s
   d| _ | S )a0  
    decorator for HandlerCase.create_backend_case() --
    used to decorate methods that should be run even if backend isn't present
    (by default, full test suite is skipped when backend is missing)

    NOTE: tests decorated with this should not rely on handler have expected (or any!) backend.
    T)_doesnt_require_backendr   r/   r/   r0   doesnt_require_backend  s   rX  c                       s<  e Zd ZdZdZdZg Zg Zg Zg Z	g Z
g dZededdgZdZdZdZdZedd	 Zd
Zedd Zedd Zdd ZdddZdd Zdd Zdd ZdddZdddZdd Z dd  Z!d!d" Z"dd#d$Z#d%Z$ed&d' Z%ed(d) Z&dZ'd*d+ Z( fd,d-Z)d.d/ Z*d0d1 Z+d2d3 Z,dd4d5Z-d6d7 Z.d8d9 Z/d:d; Z0d<d= Z1d>d? Z2d@dA Z3edBdC Z4dDdE Z5dFdG Z6dHdI Z7dZ8dJdK Z9dLdM Z:edNdO Z;dPdQ Z<dRdS Z=dTdU Z>dVdW Z?dXdY Z@dZd[ ZA fd\d]ZBd^d_ ZCd`da ZDdbdc ZEddde ZFdfdg ZGdhdi ZHdjdk ZIdldm ZJdndo ZKdpdq ZLdrds ZMdtdu ZNdvdw ZOdxdy ZPdzd{ ZQd|d} ZRd~d ZSdd ZTdd ZUdd ZVdd ZWdd ZXdd ZYdd ZZdd Z[dd Z\dd Z]g Z^dd Z_dd Z`dd Zadd ZbdddZcdd Zdedd Zeedd ZfdZgdddZhdd ZiG dd dejZkdd Zl  ZmS )r&   a  base class for testing password hash handlers (esp passlib.utils.handlers subclasses)

    In order to use this to test a handler,
    create a subclass will all the appropriate attributes
    filled as listed in the example below,
    and run the subclass via unittest.

    .. todo::

        Document all of the options HandlerCase offers.

    .. note::

        This is subclass of :class:`unittest.TestCase`
        (or :class:`unittest2.TestCase` if available).
    N))	des_crypt6f8c114b58f2c)	md5_cryptz"$1$dOHYPKoP$tnxS1T8Q6VVn3kpV8cN6o.)sha512_cryptzx$6$rounds=123456$asaltof16chars..$BtCwjqMJGx5hrJhZywWvt0RLE8uZ4oPwcelCjmw2kSYu.Ec6ycULevoBK25fs2xXgMNrCzIMVcgEJAstJeonj1testu   €¥$s   €¥$Fc                 C   s   dt | jddv rdS d S )Nos_cryptr\   r/       )r   rP   r   r/   r/   r0   forbidden_charactersZ  s   z HandlerCase.forbidden_charactersTc                 C   s,   | j }|j}t|dr|d| f 7 }|S )NrV   z (%s backend))rP   r   rJ   rV   )ro   rP   r   r/   r/   r0   r   g  s
   
zHandlerCase.descriptionPrefixc                 c   sX    | j D ]	\}}||fV  q| jD ]
\}}}||fV  q| jD ]
\}}}||fV  qdS )z*iterate through known (secret, hash) pairsN)known_correct_hashesknown_correct_configsknown_alternate_hashes)r   secrethashconfigaltr/   r/   r0   iter_known_hashesv  s   zHandlerCase.iter_known_hashesc                 C   s   t |  }|  |S )z#test random sample secret/hash pair)r   rh  r'  choice)ro   knownr/   r/   r0   get_sample_hash  s   zHandlerCase.get_sample_hashc                 C   sz   |  ||}| |du p|du d|f  | jjs|r,|sdS |s'd||f }| ||r0dS |s8d||f }| |)z>helper to check verify() outcome, honoring is_disabled_handlerTFz'verify() returned non-boolean value: %rNz4verify incorrectly returned True: secret=%r, hash=%rz!verify failed: secret=%r, hash=%r)	do_verify
assertTruerP   is_disabledr   )ro   rd  re  r   negater   r/   r/   r0   check_verify  s"   

zHandlerCase.check_verifyc                 C   s   |  |td||f  d S )Nz'%s() failed to return native string: %r)r   r   )ro   r   	func_namer/   r/   r0   check_returned_native_str  s   
z%HandlerCase.check_returned_native_strc                 C   s   | j }d|jv r?d|vrA|j}|j}tddr td||d< dS d}t|dddkr/||8 }n|d|>  }td|||d< dS dS dS )	z0subclassable method to populate default settingsrR   r@   rG   r<   rounds_costNlog2r[   )rP   rK   rn   default_roundsr#   rG   r   )ro   rc   rP   mndffactorr/   r/   r0   populate_settings  s   

zHandlerCase.populate_settingsc                 C   s   |S )z?subclassable method allowing 'secret' to be encode context kwdsr/   )ro   rd  rc   r/   r/   r0   populate_context  s   zHandlerCase.populate_contextc                 K   s   |  | |du ri }| ||}|rGg }|r$|jdi | |d | | |p.| jj|fi |W  d   S 1 s@w   Y  dS |pK| jjdi |j|fi |S )z3call handler's hash() method with specified optionsNz"passing settings to.*is deprecatedr/   )	rz  r{  updater?  r   rP   encryptusingre  )ro   rd  use_encryptrP   contextsettingsr   r/   r/   r0   
do_encrypt  s   

$$zHandlerCase.do_encryptc                 K   s&   |  ||}|p
| jj||fi |S )zcall handler's verify method)r{  rP   verify)ro   rd  re  rP   rc   r/   r/   r0   rl    s   zHandlerCase.do_verifyc                 C   s   | j |S )zcall handler's identify method)rP   identifyro   re  r/   r/   r0   do_identify  s   zHandlerCase.do_identifyc                 K   s   |  | | jjdi |S )z6call handler's genconfig method with specified optionsNr/   )rz  rP   	genconfig)ro   rc   r/   r/   r0   do_genconfig  s   
zHandlerCase.do_genconfigc                 K   s"   |  ||}| jj||fi |S )z4call handler's genhash method with specified options)r{  rP   genhash)ro   rd  rf  rc   r/   r/   r0   
do_genhash  s   zHandlerCase.do_genhashc                 K   sl   |p| j jdi |}|du ri }| d|}t| |j|fi |W  d   S 1 s/w   Y  dS )z
        return sample hash for handler, w/o caring if digest is valid
        (uses some monkeypatching to minimize digest calculation cost)
        Nr   r/   )rP   r~  r{  rt   re  )ro   rP   r  r  rd  r/   r/   r0   do_stub_encrypt  s   
$zHandlerCase.do_stub_encryptzbackend not availablec                 C   s0   | j }t||stdsdS ||rdS | jS )z
        helper for create_backend_case() --
        returns reason to skip backend, or None if backend should be tested
        rB   z$only default backend is being testedN)rP   rZ   r#   r]   _BACKEND_NOT_AVAILABLE)r   rX   rP   r/   r/   r0   _get_skip_backend_reason  s   
z$HandlerCase._get_skip_backend_reasonc              	   C   s   | j }|j}t|dsJ d||jv sJ d|f | f}|dkr'|tf7 }td||f |td||f || || jd}|S )Nr\   z0handler must support uh.HasManyBackends protocolzunknown backend: %rr^  z
%s_%s_testz%s (%s backend))r   rX   _skip_backend_reasonr   )	rP   r   rJ   r\   OsCryptMixinri   r   r  r   )r   rX   rP   r   basessubclsr/   r/   r0   create_backend_case  s$   



zHandlerCase.create_backend_casec                 C   s   t | | jd}t |dd S )z]
        check if current test method decorated with doesnt_require_backend() helper
        NrW  F)r   r   )ro   methr/   r/   r0   _test_requires_backend  s   z"HandlerCase._test_requires_backendc                    s   |   }|r| jr| | jtt|   | j}| j}|rFt|ds't	dz| 
|j|  || W n tjjyE   |rC Y nw ddlm} | |d| d d S )NrW   z)handler doesn't support multiple backendsr   )handlersr   zsalt generator)r  r  r  r   r&   r   rP   rX   rJ   RuntimeErrorr   rW   rV   rM   r   r   r  r  r   r'  )ro   test_requires_backendrP   rX   r  r   r/   r0   r   $  s&   

zHandlerCase.setUpc                    s   | j   fdd}|d}| |d | |td | | |kd | td|d|f  |d	}| |d
ud | |td |d}| |d
ud | |td d
S )zvalidate required attributesc                    s   t  | d S r2   )r   r   rO   r/   r0   gaH  s   z3HandlerCase.test_01_required_attributes.<locals>.gar   zname not defined:zname must be native strzname not lower-case:z^[a-z0-9_]+$z&name must be alphanum + underscore: %rrK   Nzsetting_kwds must be defined:zsetting_kwds must be a tuple:context_kwdszcontext_kwds must be defined:zcontext_kwds must be a tuple:)rP   rm  r   r   r   r   matchr   )ro   r  r   r  r  r/   rO   r0   test_01_required_attributesE  s   z'HandlerCase.test_01_required_attributesc                 C   sT   |   }| |d | d|}| |d | d| | | |d|f  dS )ztest basic config-string workflow

        this tests that genconfig() returns the expected types,
        and that identify() and genhash() handle the result correctly.
        r  stubr  r   z4identify() failed to identify genconfig() output: %rN)r  rr  r  rl  rm  r  )ro   rf  r   r/   r/   r0   test_02_config_workflowe  s   
z#HandlerCase.test_02_config_workflowc                 C   s.   | j }| }| || | |j|j dS )ztest basic using() workflowN)rP   r~  assertIsNotr   r   )ro   rP   r  r/   r/   r0   test_02_using_workflow  s   z"HandlerCase.test_02_using_workflowc              
   C   s  d}| j D ]}| j||d}| |d | || | j||dd | ||}| |d | jjrB| jrB| ||d|||f  n| 	||d|||f  | ||}| |d | jjro| jso| 	||d	||||f  n| ||d
||||f  | 
| | qdS )ztest basic hash-string workflow.

        this tests that hash()'s hashes are accepted
        by verify() and identify(), and regenerated correctly by genhash().
        the test is run against a couple of different stock passwords.
        r  )r  re  T)ro  r  zBgenhash() failed to salt result hash: secret=%r hash=%r: result=%rz@genhash() failed to reproduce hash: secret=%r hash=%r: result=%rzYgenhash() failed to reproduce disabled-hash: secret=%r hash=%r other_secret=%r: result=%rzGgenhash() duplicated hash: secret=%r hash=%r wrong_secret=%r: result=%rN)stock_passwordsr  rr  rp  r  rP   rn  disabled_contains_saltassertNotEqualr   rm  r  )ro   use_16_legacywrong_secretrd  r   otherr/   r/   r0   test_03_hash_workflow  s4   






z!HandlerCase.test_03_hash_workflowc                 C   s   | j dd dS )zEtest hash-string workflow with legacy .encrypt() & .genhash() methodsT)r  N)r  r   r/   r/   r0   test_03_legacy_hash_workflow  s   z(HandlerCase.test_03_legacy_hash_workflowc                 C   s   |  td}| |d | dt| | tdt| | dt|}| |d | jjr;| jr;| || n| 	|| | tdt|}| |d | jjr_| jr_| || n| 	|| | 
| t| dS )z#test hashes can be unicode or bytesr  re  r  N)r  r   rr  rp  r  rP   rn  r  r  r   rm  r  )ro   r   r  r/   r/   r0   test_04_hash_types  s   zHandlerCase.test_04_hash_typesc                 C   s   | j }t|ds| d| |j|  |jD ]=}| |t | 	|t
d|f  ||}|du rB|| | | | q|du rO| t|j| qtd||f dS )ztest multi-backend supportrW   zhandler only has one backendzinvalid backend name: %rTFz*has_backend(%r) returned invalid value: %rN)rP   rJ   r  r   rW   rV   r\   r   r   assertNotInRESERVED_BACKEND_NAMESr]   r   r   r   r   )ro   rP   rX   retr/   r/   r0   test_05_backends  s*   





zHandlerCase.test_05_backendsc                 C   s   d| j jvr| dd S )Nsaltzhandler doesn't have salt)rP   rK   r  r   r/   r/   r0   require_salt!  s   
zHandlerCase.require_saltc                 C   s    |    t| js| dd S )Nz!handler doesn't provide salt info)r  r   rP   r  r   r/   r/   r0   require_salt_info%  s   

zHandlerCase.require_salt_infoc                 C   s  |    | j}| j}|jdu}|r|jdk r|d|jdk r#|d|r/|j|jkr/|d|j|jk r9|d|rE|j|jkrE|dd	|jvrZ|rR|j|jk rZtd
|jf  |j	rx|j
sd|d|j
D ]}||j	vru|d|f qgdS |j
s|ddS )z!validate optional salt attributesNr[   zmax_salt_chars must be >= 1r   zmin_salt_chars must be >= 0z(min_salt_chars must be <= max_salt_charsz*default_salt_size must be >= min_salt_sizez*default_salt_size must be <= max_salt_size	salt_sizezT%s: hash handler supports range of salt sizes, but doesn't offer 'salt_size' settingz$default_salt_chars must not be emptyzEdefault_salt_chars must be subset of salt_chars: %r not in salt_charsz;default_salt_chars MUST be specified if salt_chars is empty)r  r   rP   max_salt_sizemin_salt_sizedefault_salt_sizerK   r
   r   
salt_charsdefault_salt_chars)ro   r   r   mx_setcr/   r/   r0    test_10_optional_salt_attributes*  s:   



z,HandlerCase.test_10_optional_salt_attributesc                 C   sB   | j }t|sJ d|j ddlm} t|j|t|jd S )z%calculate number of salt bits in hashzneed explicit bit-size for r   r      )	rP   r   r   r   r   r   r  r  r  )ro   rP   r   r/   r/   r0   	salt_bitsS  s   zHandlerCase.salt_bitsc                    sD      tddj   fdd}|j |fdd dS )z4test hash() / genconfig() creates new salt each timer[   r  c                    s8   |  }t  D ]}|  }||kr d S qd f )Nz.failed to find different salt after %d samples)r   r   )r   value1r:   value2samplesro   r/   r0   sampleri  s   z0HandlerCase.test_11_unique_salt.<locals>.samplerc                      s
     dS Nr  )r  r/   r   r/   r0   <lambda>r  s   
 z1HandlerCase.test_11_unique_salt.<locals>.<lambda>N)r  rG   r  r  )ro   r  r/   r  r0   test_11_unique_salt_  s
   
zHandlerCase.test_11_unique_saltc                 C   s   |    | j}|jdd }|j}|| }| j|d | jd|d |dkr3| jt| j|dd d | jt| jd|d d dS )z.test hash() / genconfig() honors min_salt_sizer   r[   r  r  r  Nr   )r  rP   r  r  r  r  r   
ValueError)ro   rP   	salt_charmin_sizes1r/   r/   r0   test_12_min_salt_sizet  s   


z!HandlerCase.test_12_min_salt_sizec           	      C   sB  |    | j}|j}|jdd }|du s|dkr9|d }| j|d}| j|| d}| || | jdd dS || }| j|d}| j|d || }| jt| j|d | jt| j|d d t|rt	j
dd	 | j|dd
}W d   n1 s}w   Y  | || |j|k r| j|dd d}| || dS dS )z.test hash() / genconfig() honors max_salt_sizer   r[   Ni   i   r  r  Tr   )r  rI   r   )r  rP   r  r  r  r  r   r  rQ   r   rP  r   r  )	ro   rP   max_sizer  r  c1c2s2c3r/   r/   r0   test_13_max_salt_size  s2   
z!HandlerCase.test_13_max_salt_sizec                 C   s    | j rddlm} ||}|S )zprepare generated saltr   )bcrypt64)fuzz_salts_need_bcrypt_repairpasslib.utils.binaryr  repair_unused)ro   r  r  r/   r/   r0   prepare_salt  s   
zHandlerCase.prepare_saltc           
      C   s   |    | j}|j}|j}|j}t|t}t||pdD ]}t||k r)t	||}| 
|}| j|d qtd}|r@|d}t|d}|D ]}	|	|vr\| jt| j|	| d|	f d qGdS )	ztest hash() honors salt_charsr   r  u    ÿr~   r[   zinvalid salt char %r:)r  r   N)r  rP   r  r  r  rf   bytesr   r  r   r  r  r   rw   rG   r   r  )
ro   rP   mxrw  csrawr  r   chunkr  r/   r/   r0   test_14_salt_chars  s,   




zHandlerCase.test_14_salt_charsc                 C   s   t | jddr	tS tS )z)hack to determine salt keyword's datatype_salt_is_bytesF)r   rP   r  r   r   r/   r/   r0   	salt_type  s   zHandlerCase.salt_typec                 C   s   |    | j}t| jddpd}G dd dt}| jt| jd| d |tur5| jt| jdt	d| d |t
u sMtr?|tu sO| jt| jdd	| d d
S d
S d
S )ztest non-string salt valuesr  r      c                   @   s   e Zd ZdS )z+HandlerCase.test_15_salt_type.<locals>.fakeN)r   r   r   r/   r/   r/   r0   fake  s    r  r  r  x   xN)r  r  r   rP   objectr   r   r  r   r   r  r    )ro   r  r  r  r/   r/   r0   test_15_salt_type  s   zHandlerCase.test_15_salt_typec                 C   s  |    | j}|j}|j}|j}| jt|jdd | t	g |jddd}W d   n1 s1w   Y  | 
|j| |rp| jt|j|d d | t	g |j|d dd}W d   n1 sdw   Y  | 
|j| ||kr|j|d d}| 
|j|d  | 
|j| |j|d d}| 
|j|d  | 
|j| ||kr|}n|d }|jt|d}| 
|j| | jt|jt|d d |j|d	}| 
|j| dS )
z$Handler.using() -- default_salt_sizer   )r  T)r  rI   Nr[   r  xxxr  )r  rP   r  r  r  r   r  r~  r   r   r   r   )ro   rP   rw  r  rx  temprefr/   r/   r0   test_using_salt_size  s>   z HandlerCase.test_using_salt_sizec                 C   s   t | js
| dd S )Nzhandler lacks rounds attributes)r   rP   r  r   r/   r/   r0   require_rounds_info7  s   

zHandlerCase.require_rounds_infoc                 C   s   |    | j}| j}|jdu r|d|jdk r|d|jdk r%|d|j|jkr/|d|jdurH|j|jk r>|d|j|jkrH|d	|jtvrU|d
|jf dS )z#validate optional rounds attributesNzmax_rounds not specifiedr[   zmax_rounds must be >= 1r   zmin_rounds must be >= 0z min_rounds must be <= max_roundsz$default_rounds must be >= min_roundsz$default_rounds must be <= max_roundsz unknown rounds cost constant: %r)r  rP   r   
max_roundsrn   rv  rt  r   )ro   r   r   r/   r/   r0   "test_20_optional_rounds_attributes;  s&   




z.HandlerCase.test_20_optional_rounds_attributesc                 C   s`   |    | j}|j}| j|d | jd|d | jt| j|d d | jt| jd|d d dS )z+test hash() / genconfig() honors min_roundsrR   r  r[   N)r  rP   rn   r  r  r   r  )ro   rP   rn   r/   r/   r0   test_21_min_roundsY  s   zHandlerCase.test_21_min_roundsc                 C   sr   |    | j}|j}|dur%| jt| j|d d | jt| jd|d d |du r1| jdd dS | j|d dS )z+test hash() / genconfig() honors max_roundsNr[   r  r  i)r  rP   r  r   r  r  r  r  )ro   rP   r  r/   r/   r0   test_21b_max_roundsi  s   zHandlerCase.test_21b_max_roundsc           
         s   |    | j}|jdkr| | }t fdd|_|j}|j}|j}|p(d| d }||kr5|d7 }|| d }|p>d| d }|jdkrW|dO }|dO }|dO }d}nd}| 	g  |j|||d}	W d   n1 sqw   Y  ||	||||fS )	zU
        setup test helpers for testing handler.using()'s rounds parameters.
        
bsdi_cryptc                    s   t |  S r2   )r   _generate_roundsr   )r   orig_handlerr/   r0   r    s    z9HandlerCase._create_using_rounds_helper.<locals>.<lambda>i'  r  r[   )min_desired_roundsmax_desired_roundsrv  N)
r  rP   r   r~  classmethodr  rn   r  rv  r   )
ro   rP   orig_min_roundsorig_max_roundsorig_default_roundsmediumsmalllargeadjr  r   )r  r0   _create_using_rounds_helper  s8   

z'HandlerCase._create_using_rounds_helperc           
      C   s   |    | j}|j}|j}|j}|  \}}}}}}	| |j| | |j| | |jd | |jd | |j| | |j| | |j| | |j| | |j| | |j| dS )z@
        HasRounds.using() -- sanity check test harness
        N)	r  rP   rn   r  rv  r  r   r  r  )
ro   rP   r  r  r  r  r  r  r  r  r/   r/   r0   test_has_rounds_using_harness  s    z)HandlerCase.test_has_rounds_using_harnessc                 C   s  |   \}}}}}}|j}|j}|j}	|dkrH| jt|j|| d | tg |j|| dd}
W d   n1 s<w   Y  | 	|
j
| |r{| jt|j|| d | tg |j|| dd}
W d   n1 sow   Y  | 	|
j
| | g  |j|| d}
W d   n1 sw   Y  | 	|
j
||  |j|d|  d}
| 	|
j
|d|   | g  |j|| d}
W d   n1 sw   Y  | 	|
j
||  | 	t||| ||  | 	t||| | g  | 	t||| ||  W d   n	1 sw   Y  |j|d}
| 	|
j
| |jt|d}
| 	|
j
| | jt|jt|d d dS )	zF
        HasRounds.using() -- min_rounds / min_desired_rounds
        r   r  T)r  rI   Nr  rn   r  )r  rn   r  rv  r   r  r~  r   r   r   r  rU   r   )ro   rP   r  r  r  r  r  r  r  r  r  r/   r/   r0   "test_has_rounds_using_w_min_rounds  sH   z.HandlerCase.test_has_rounds_using_w_min_roundsc           
      C   s  |   \}}}}}}|j}|j}|dkrE| jt|j|| d | tg |j|| dd}	W d   n1 s9w   Y  | |	j	| |rx| jt|j|| d | tg |j|| dd}	W d   n1 slw   Y  | |	j	| | t
g |j|| d}	W d   n1 sw   Y  | |	j	| | jt|j|| || d |j|d|  d}	| |	j|d|   | g  |j|| d}	W d   n1 sw   Y  | |	j	||  | t||| ||  | t||| | g  | t||| ||  W d   n	1 sw   Y  |j|d	}	| |	j	| |jt|d}	| |	j	| | jt|jt|d
 d dS )zF
        HasRounds.using() -- max_rounds / max_desired_rounds
        r   )r  T)r  rI   Nr  r  r  r  r  r  )r  rn   r  r   r  r~  r   r   r   r  r   r  rU   r   )
ro   rP   r  r  r  r  r  r  r  r  r/   r/   r0   $test_has_rounds_replace_w_max_rounds  sN   
z0HandlerCase.test_has_rounds_replace_w_max_roundsc           	      C   s   |   \}}}}}}|j}|j|| d}| |j||  |j|| d}| |j||  | jt|j|| d |rG| jt|j|| d | t|| | t||| ||  |jt|d}| |j| | jt|jt|d d dS )z5
        HasRounds.using() -- default_rounds
        r  r  )rv  r  N)	r  r  r~  r   rv  r   r  rU   r   	ro   rP   r  r  r  r  r  r  r  r/   r/   r0   &test_has_rounds_using_w_default_rounds8  s   z2HandlerCase.test_has_rounds_using_w_default_roundsc           	      C   s   |   \}}}}}}|j}|j|| d}| |j||  | |j||  | |j||  |j|d || ||| d}| |j||  | |j| | |j||  dS )z-
        HasRounds.using() -- rounds
        r  r[   )rR   rn   rv  r  N)r  r  r~  r   r  rv  r  r  r/   r/   r0   test_has_rounds_using_w_rounds^  s   z*HandlerCase.test_has_rounds_using_w_roundsc                    s   |   \} }}}} fdd}| |dd | |dd | |dd | |dd | |dd | t|d | t|d	 d
S )z:
        HasRounds.using() -- vary_rounds parsing
        c                        j | djS )Nvary_rounds)r~  r  r   r  r/   r0   parsez     zFHandlerCase.test_has_rounds_using_w_vary_rounds_parsing.<locals>.parser'   z0.1z10%  1000gg?N)r  r   r   r  )ro   rP   r  r  r  r  r  r/   r
  r0   +test_has_rounds_using_w_vary_rounds_parsings  s   z7HandlerCase.test_has_rounds_using_w_vary_rounds_parsingc           	         s    \}}}}}dd   fdd}|d|| |d|| |||| ||  |dt||d t||d  |jdkrW|d	|| |d
|| |d|| | dS  jdd\}}|t||d  |t||d  |t||d  |t||d  dS )z=
        HasRounds.using() -- vary_rounds generation
        c                    s*   t  fddtdD }t|t|fS )Nc                 3   s    | ]}t  V  qd S r2   )rU   r8   r   r/   r0   r;         zjHandlerCase.test_has_rounds_using_w_vary_rounds_generation.<locals>.get_effective_range.<locals>.<genexpr>r  )setr   r=   rG   )r   seenr/   r   r0   get_effective_range  s   zWHandlerCase.test_has_rounds_using_w_vary_rounds_generation.<locals>.get_effective_rangec                    s8   j | d} |\}}||d ||d d S )Nr  z"vary_rounds had wrong lower limit:z"vary_rounds had wrong upper limit:)r~  r   )r  r   r   r  
seen_lower
seen_upperr  ro   r  r/   r0   assert_rounds_range  s   zWHandlerCase.test_has_rounds_using_w_vary_rounds_generation.<locals>.assert_rounds_ranger   z0%2   ru  z1%z49%z50%r        ?g?g333333?g      ?N)r  rG   r=   rt  r~  assertGreaterEqualassertLessEqual)	ro   rP   r  r  r  r  r  r   r   r/   r  r0   .test_has_rounds_using_w_vary_rounds_generation  s     
z:HandlerCase.test_has_rounds_using_w_vary_rounds_generationc                 C   s   |   \}}}}}}|j|d |d d}| j||d}| j||d}	| j||d}
| || | ||	 | ||
 | || | ||	 | ||
 dS )zF
        HasRounds.using() -- desired_rounds + needs_update()
        r  r   r  N)r  r~  r  assertFalseneeds_updaterm  )ro   rP   r  r  r  r  r  r  
small_hashmedium_hash
large_hashr/   r/   r0   &test_has_rounds_using_and_needs_update  s   z2HandlerCase.test_has_rounds_using_and_needs_updatec                 C   s*   | j }t|trt|tjs| dd S )Nz)handler doesn't derive from HasManyIdents)rP   rf   ri   rL   rM   HasManyIdentsr  )ro   rP   r/   r/   r0   require_many_idents  s   
zHandlerCase.require_many_identsc                 C   s6  | j }|   | d|jv  |jD ]	}| |td q| t|jdkd | |jtd | |j|jv d |j	r`t
|j	D ]\}}| |td | |td | ||jv d	|f  qA|}|  d }||}|d= |dd|ji| | jt|fi | |dd
di| | jt|fddi| dS )z$validate HasManyIdents configurationidentz!cls.ident_values must be unicode:r[   z'cls.ident_values must have 2+ elements:z"cls.default_ident must be unicode:z9cls.default_ident must specify member of cls.ident_valuesz'cls.ident_aliases keys must be unicode:z)cls.ident_aliases values must be unicode:z:cls.ident_aliases must map to cls.ident_values members: %rrS   TxXxNr/   )rP   r$  rm  rK   ident_valuesr   r   r  default_identident_aliasesr   rk  	parsehashr   r   r  )ro   r   r   aliasr%  rP   re  rc   r/   r/   r0   test_30_HasManyIdents  sF   


z!HandlerCase.test_30_HasManyIdentsc                 C   s6  |    | j}|j}|jD ]}||kr n
qtd||jf dd }| }| |j| |j|d}| |j| | |j| | ||| | ||| | jt|jdd |j|d}| |j| | |j| | jt	|j||d |j
r|j
 D ]\}}|j|d}| j|j|d| d	 qd
S d
S )z=HasManyIdents.using() -- 'default_ident' and 'ident' keywordsz6expected to find alternate ident: default=%r values=%rc                 S   s   t | } | ddjS )NT)rS   )rT   r%  r   r/   r/   r0   effective_ident  s   z?HandlerCase.test_has_many_idents_using.<locals>.effective_ident)r(  r&  )r%  )r(  r%  z	alias %r:r   N)r$  rP   r(  r'  r   r~  r   r   r  r   r)  r+  )ro   rP   
orig_ident	alt_identr-  r  r+  r%  r/   r/   r0   test_has_many_idents_using  s:   
z&HandlerCase.test_has_many_idents_usingc                    s   | j   jdu r| d j dS  js|  j d jvr'|  j dS  fdd}| |d j | |dd | |dd | |dd | |dd | 	t
|d	 dS )
zH
        validate 'truncate_error' setting & related attributes
        Ntruncate_errorc                    r  )Nr2  )r~  r2  r	  hasherr/   r0   parse_valueS  r  z<HandlerCase.test_truncate_error_setting.<locals>.parse_valueTtrueFfalser  )rP   truncate_sizer  rK   r2  r  truncate_verify_rejectrm  r   r   r  )ro   r6  r/   r4  r0   test_truncate_error_setting;  s    

z'HandlerCase.test_truncate_error_settingc                 C   s   | j }|jdur| |jd | dd}d}| |}|j }| j| |||dd |dd | }| | ||d	 dS )
zO
        test no password size limits enforced (if truncate_size=None)
        Nr[   ztruncate_size is seta   too many secretstoo many secretstoo many secretstoo many secretstoo many secretstoo many secretstoo many secretstoo many secretstoo many secretstoo many secretstoo many secretstoo many secretstoo many secretstoo many secretstoo many secretstoo many secretsr  zverify rejected correct secretr.  r   z full password not used in digest)	rP   r9  r  r  r  rn  r   rl  r  )ro   r5  rd  rg  re  verify_success
alt_secretr/   r/   r0   test_secret_wo_truncate_size\  s   

	
z(HandlerCase.test_secret_wo_truncate_sizec                 C   s  | j }|j}|s| dtj}d|jv r%|jdd}|jdd}tj}n|jr-d}|}n|}d}d}d}t	||d	 }|dd
 }	|dd
 | }
|	dd
 | }|j
 }|o[|j }|sb|sbJ ||fD ]-}| j|	|d}| | j|	||d| | | j|||dd | | j|||d| qf|r| j||d}| | j|||d| | | j|
||d| | | j|	||d |r| j|| j||d}| |j| dS dS )zQ
        test password size limits raise truncate_error (if appropriate)
        ztruncate_size not setr2  Fr3  TNztoo many secretsr  r[   r   rO   z truncate_size value is too large)rP   r9  r  r   PasswordSizeErrorrK   r~  PasswordTruncateErrorr2  r   rn  r:  r  r   rl  r  rm  r   r  )ro   rP   r9  size_error_typewithout_error
with_errorrl   rg  long_secretshort_secretalt_long_secretalt_short_secretshort_verify_successlong_verify_successcand_hasher
short_hash	long_hashr   r/   r/   r0   test_secret_w_truncate_size|  s~   


z'HandlerCase.test_secret_w_truncate_sizec                 C   s   | j du }| j dv }d}d}| |}|r$| jjs$| | ||d n
| | ||d | |}|rD| jjsD| | ||d n
| | ||d | ||}|s]| jjrf| jsf| 	||d d	S | 
||d d	S )
ztest password case sensitivityT)Tzverify-onlyr]  TESTz%verify() should not be case sensitivez!verify() should be case sensitivez&genhash() should not be case sensitivez"genhash() should be case sensitiveN)secret_case_insensitiver  rP   rn  rm  rl  r  r  r  r   r  )ro   hash_insensitiveverify_insensitiver   r   h1h2r/   r/   r0   test_61_secret_case_sensitive  s8   



z)HandlerCase.test_61_secret_case_sensitivec                 C   sx   |   d }| t| jd | t| jd| | t| jd| | t| jd | t| jd| | t| jd| dS )z&test non-string passwords are rejectedr[   N)rk  r   r   r  r  rl  r  r/   r/   r0   test_62_secret_border	  s   z!HandlerCase.test_62_secret_borderc                 C   sv   ddl m} ddlm} dd|  }|  d }| || j||}| |j| | || j	| | || j
|| dS )z"test MAX_PASSWORD_SIZE is enforcedr   )r?  )MAX_PASSWORD_SIZErK  r[   N)passlib.excr?  r  rV  rk  r   r  r   r  r  rl  )ro   r?  rV  rd  re  r   r/   r/   r0   test_63_large_secret	  s   z HandlerCase.test_63_large_secretc                 C   sj   | j }|s
| dtd}t|tr"ddlm} ||}|d}|D ]}| t	| j
|| |  q$dS )z1test forbidden characters not allowed in passwordznone listedr  r   )iter_byte_charsasciiN)r`  r  r   rf   r  passlib.utils.compatrY  rw   r   r  r  )ro   charsrl   rY  r  r/   r/   r0   test_64_forbidden_chars)	  s   


z#HandlerCase.test_64_forbidden_charsc                 C   s   |  |i }t| S r2   )r{  r   ro   rd  r/   r/   r0   is_secret_8bit9	  s   
zHandlerCase.is_secret_8bitc                 C   sB   t r| jdkrt|trz|d W dS  ty   Y dS w dS )zl
        check if we're expecting potential verify failure due to crypt.crypt() encoding limitation
        r^  ru   TF)r   rX   rf   r  r   r   r^  r/   r/   r0   expect_os_crypt_failure=	  s   z#HandlerCase.expect_os_crypt_failurec              	   C   s   |  | jp| jd d}|  D ]Z\}}| |rd}|  | |d|f  | |}z2| ||d||f  | ||}| 	|t
d|f  | jjrQ| jrQW q| ||d|||f  W q tyj   |sh Y qw |svtd| j  d	S d	S )
ztest known hasheszOtest must set at least one of 'known_correct_hashes' or 'known_correct_configs'FTz&identify() failed to identify hash: %rz1verify() of known hash failed: secret=%r, hash=%r,genhash() failed to return native string: %rzGgenhash() failed to reproduce known hash: secret=%r, hash=%r: result=%rz%s: no 8-bit secrets testedN)rm  ra  rb  rh  r_  r  r`  rp  r  r   r   rP   rn  r  r   r   r
   r   )ro   saw8bitrd  re  r`  r   r/   r/   r0   test_70_hashesH	  s@   




zHandlerCase.test_70_hashesc              
   C   s   | j s| d| j D ]A\}}}| | |d|f  | ||d||f  | ||}| |td|f  | jj	r?| j
r?q| ||d||||f  qdS )ztest known alternate hasheszno alternate hashes providedz0identify() failed to identify alternate hash: %rz;verify() of known alternate hash failed: secret=%r, hash=%rra  zYgenhash() failed to normalize known alternate hash: secret=%r, alt=%r, hash=%r: result=%rN)rc  r  rm  r  rp  r  r   r   rP   rn  r  r   )ro   rg  rd  re  r   r/   r/   r0   test_71_alternatest	  s*   



zHandlerCase.test_71_alternatesc              
   C   s   | j js| | jd | d| js| d| jr"tjdtd | jD ]<\}}}| 	| 
|d|f  | jt| j||d|f d | ||}| |td	|f  | ||d
||||f  q%dS )ztest known config stringsz&handler should not have config stringszhash has no settingszno config strings providedr   r   z5identify() failed to identify known config string: %rz+verify() failed to reject config string: %rr   ra  z^genhash() failed to reproduce known hash from config: secret=%r, config=%r, hash=%r: result=%rN)rP   rK   r  rb  r  filter_config_warningsr   r   r   rm  r  r   r  rl  r  r   r   r   )ro   rf  rd  re  r   r/   r/   r0   test_72_configs	  s8   



zHandlerCase.test_72_configsc                 C   p   | j s| d| j D ]*}| | |d|f  | jt| jd|d|f d | jt| jd|d|f d qdS )z)test known unidentifiably-mangled stringszno unidentified hashes providedz?identify() incorrectly identified known unidentifiable hash: %rr  z:verify() failed to throw error for unidentifiable hash: %rre  z;genhash() failed to throw error for unidentifiable hash: %rN)known_unidentified_hashesr  r  r  r   r  rl  r  r  r/   r/   r0   test_73_unidentified	  &   

z HandlerCase.test_73_unidentifiedc                 C   rh  )z-test known identifiable-but-malformed stringszno malformed hashes providedz6identify() failed to identify known malformed hash: %rr  z5verify() failed to throw error for malformed hash: %rre  z6genhash() failed to throw error for malformed hash: %rN)known_malformed_hashesr  rm  r  r   r  rl  r  r  r/   r/   r0   test_74_malformed	  rk  zHandlerCase.test_74_malformedc              	   C   s   | j r| d| js| d| jD ]X\}}|| jjkr@| | |d|f  | d| | d|}| 	|t
d|f  q| | |d||f  | jt| jd|d||f d | jt| jd|d	||f d qd
S )ztest known foreign hashesnot applicablezno foreign hashes providedz,identify() failed to identify known hash: %rr  ra  z:identify() incorrectly identified hash belonging to %s: %rz;verify() failed to throw error for hash belonging to %s: %rre  z<genhash() failed to throw error for hash belonging to %s: %rN)accepts_all_hashesr  known_other_hashesrP   r   rm  r  rl  r  r   r   r  r   r  )ro   r   re  r   r/   r/   r0   test_75_foreign	  s<   

zHandlerCase.test_75_foreignc                 C   s  |  t| jd |  t| jdd |  t| jdd |  t| jd |  t| jdd |  t| jdd tddfD ]?}| jrZ| | | | d| | d|}| |d q:| 	| |d | j t
| jd|dd	 | j t
| jd|d
d	 q:| d | d dS )z#test non-string hashes are rejectedNr  r[   r       r  z,identify() incorrectly identified empty hashz$verify() failed to reject empty hashre  z%genhash() failed to reject empty hashu   â¬Â¥$u   abc )r   r   r  rl  r  r   ro  rm  rr  r  r  )ro   re  r   r/   r/   r0   test_76_hash_border 
  s.   
zHandlerCase.test_76_hash_borderc                 C   s   t | jds
tdd S )Nr*  zparsehash() not implemented)rJ   rP   r   r   r/   r/   r0   require_parsehash2
  s   zHandlerCase.require_parsehashc           	      C   s   |    | j}| d}||}| |t |j|dd}| }|dd | || |j|dd}| }t	rCt
jdd	td
 dD ]}||v rb| || ||  | ||  || ||< qE| || dS )z
        parsehash()
        r  F)checksumru  NT)sanitizer   z(.*unequal comparison failed to convert.*r   )r  ru  )rt  rP   r  r*  r   r   copyr   r   r    r   r   UnicodeWarningr  assert_is_masked)	ro   rP   re  r   result2correct2result3correct3keyr/   r/   r0   test_70_parsehash6
  s,   

zHandlerCase.test_70_parsehashc                 C   sX   |du rdS |  |t t|dk r|n|dd }t|tdgkr%dS | d| )zQ
        check value properly masked by :func:`passlib.utils.mask_value`
        Nr     *Tzvalue not masked: %r)r   r   r  r  fail)ro   r   r  r/   r/   r0   ry  ]
  s   zHandlerCase.assert_is_maskedc                 C   sN   |    | j}|s| d| jD ]\}}| j|}| ||d|  qdS )z.
        parsehash() -- known outputs
        zno samples presentzhash=%r:N)rt  known_parsehash_resultsr  rP   r*  r   )ro   r  re  correctr   r/   r/   r0   test_71_parsehash_resultsk
  s   
z%HandlerCase.test_71_parsehash_resultsc                    s  | j jr	| dddlm} | j}|dkr| d| j|d}dd  |r-t j	}nd}| j
|d	}| | |}td
| j||t|d fdd|D  | }|| }	d}
| |	kr| }|d }|d }|d }|d }|rz||d< | j|fi |}|D ]L} |}|||fi |}|dkrq|du s|du sJ |s| d||||f | dk r|||fi |}|r|dkr| d|||||f q|
d7 }
| |	ks`td| j|| | |
 dS )a  fuzz testing -- random passwords and options

        This test attempts to perform some basic fuzz testing of the hash,
        based on whatever information can be found about it.
        It does as much as it can within a fixed amount of time
        (defaults to 1 second, but can be overridden via $PASSLIB_TEST_FUZZ_TIME).
        It tests the following:

        * randomly generated passwords including extended unicode chars
        * randomly selected rounds values (if rounds supported)
        * randomly selected salt sizes (if salts supported)
        * randomly selected identifiers (if multiple found)
        * runs output of selected backend against other available backends
          (if any) to detect errors occurring between different backends.
        * runs output against other "external" verifiers such as OS crypt()

        :param report_thread_state:
            if true, writes state of loop to current_thread().passlib_fuzz_state.
            used to help debug multi-threaded fuzz test issues (below)
        rn  r   r   disabled by test modethreadedc                 S   s   | j p| j d S r   )r   r   
splitlines)vr/   r/   r0   vname
     z-HandlerCase.test_77_fuzz_input.<locals>.vnamez	fuzz testr  z.%s: %s: started; max_time=%r verifiers=%d (%s)r
  c                 3   s    | ]} |V  qd S r2   r/   )r9   r  r  r/   r0   r;   
  r  z1HandlerCase.test_77_fuzz_input.<locals>.<genexpr>rd  r  r  r  r   TFzAfailed to verify against %r verifier: secret=%r config=%r hash=%rr'   z\was able to verify wrong password using %s: wrong_secret=%r real_secret=%r config=%r hash=%rr[   z!%s: %s: done; elapsed=%r count=%rN)rP   rn  r  r  r   max_fuzz_timeget_fuzz_verifiersrQ  current_threadr   r'  FuzzHashGeneratorr   debugr   r  r  generater  r   r   )ro   r  r   max_time	verifiersthread_namer   	generatorr3   stopr   optsrd  r  r  r   re  r  r   r   r/   r  r0   test_77_fuzz_input|
  sf   




$zHandlerCase.test_77_fuzz_inputc                    s
   d ddljjrdj}|dk sjdkr#d dg  fddfd	d
fddt|D }j| d }d}|D ]}|	| |
 s\qPtd|j| |d7 }qP d rxd d |f |rd||f dS )zmultithreaded fuzz testing -- random password & options using multiple threads

        run test_77 simultaneously in multiple threads
        in an attempt to detect any concurrency issues
        (e.g. the bug fixed by pybcrypt 0.3)
        rB   r   Nrn  r[   r  c                      sb   z	j dd W d S  ty   Y d S      d  d7  < W d     1 s+w   Y   )NTr  r   r[   )r  r   r/   )failedfailed_lockro   r/   r0   rq   
  s   
z3HandlerCase.test_78_fuzz_threading.<locals>.wrapperc                    sB   t  }d| |j|j jf }j|d}|d |  |S )NzFuzz-Thread-%d ('%s:%s.%s'))targetr   T)ri   r   r   r   Thread	setDaemonr3   )nr   r   thread)ro   rQ  rq   r/   r0   launch
  s   
z2HandlerCase.test_78_fuzz_threading.<locals>.launchc                    s   g | ]} |qS r/   r/   )r9   r  )r  r/   r0   
<listcomp>
  s    z6HandlerCase.test_78_fuzz_threading.<locals>.<listcomp>r  z%s timed out after %f secondszH%d/%d threads failed concurrent fuzz testing (see error log for details)zP%d/%d threads stalled during concurrent fuzz testing (see error log for details))r  rQ  rP   rn  r  fuzz_thread_countr  rR  r   r  is_aliver   errorr   r  )ro   thread_countthreadstimeoutstalledr  r/   )r  r  r  ro   rQ  rq   r0   test_78_fuzz_threading
  s:   


	


z"HandlerCase.test_78_fuzz_threadingc                 C   s<   t tjdpd}|r|S tddrdS tddrdS dS )z'amount of time to spend on fuzz testingPASSLIB_TEST_FUZZ_TIMEr   r@   rs  rA   r[      )floatr(   r  r  r#   ro   r   r/   r/   r0   r    s   

zHandlerCase.max_fuzz_timec                 C   s.   t tjdpd}|r|S tddrdS dS )z+number of threads for threaded fuzz testingPASSLIB_TEST_FUZZ_THREADSr   r@   rs  r   )r   r(   r  r  r#   r  r/   r/   r0   r  $  s   
zHandlerCase.fuzz_thread_count)fuzz_verifier_defaultc                    sv   | j  g }| jD ]}t| | }|dur|| qt dr9tdr9|s9 fdd}t D ]	}||| q/|S )zreturn list of password verifiers (including external libs)

        used by fuzz testing.
        verifiers should be callable with signature
        ``func(password: unicode, hash: ascii str) -> ok: bool``.
        Nr\   rB   c                    s*    fdd}d  d |_  d |_|S )Nc              	      s8     }z  | |W | S | w r2   )rV   rW   r  )rd  re  orig_backend)rX   rP   r/   r0   r   M  s
   
z;HandlerCase.get_fuzz_verifiers.<locals>.maker.<locals>.funccheck__backend-backend)r   r   )rX   r   rO   )rX   r0   makerL  s   
z-HandlerCase.get_fuzz_verifiers.<locals>.maker)rP   fuzz_verifiersr   r?  rJ   r#   ra   )ro   r  r  method_namer   r  rX   r/   rO   r0   r  8  s   

zHandlerCase.get_fuzz_verifiersc                    s,    fdd} j r j d |_|S d|_|S )Nc                    s    j | |fi |S r2   )rl  )rd  re  r   r   r/   r0   check_default^  r  z8HandlerCase.fuzz_verifier_default.<locals>.check_defaultr  ro   )rX   r   )ro   r  r/   r   r0   r  \  s   z!HandlerCase.fuzz_verifier_defaultc                   @   sv   e Zd ZdZedZdZeddddZi Z	dd	 Z
d
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )zHandlerCase.FuzzHashGeneratorz
        helper which takes care of generating random
        passwords & configuration options to test hash with.
        separate from test class so we can create one per thread.
        u   qwertyASDF1234<>.@*#! áəБℓru   random_roundsrandom_salt_sizerandom_ident)rR   r  r%  c                 C   s   || _ |j| _|| _d S r2   )r]  rP   r   )ro   r]  r   r/   r/   r0   r     s   
z&HandlerCase.FuzzHashGenerator.__init__c                    s4    fdd}   \}}t||| j| jdS )z
            generate random password and options for fuzz testing.
            :returns:
                `(secret, other_secret, settings_kwds, context_kwds)`
            c                    s6   i }|   D ]\}}t | }|d ur|||< q|S r2   )r+  r   )mapr/  r~  r  r   r   r/   r0   gendict  s   z7HandlerCase.FuzzHashGenerator.generate.<locals>.gendict)rd  r  r  r  )random_password_pairr   settings_mapcontext_map)ro   r  rd  r  r/   r   r0   r    s   z&HandlerCase.FuzzHashGenerator.generatec                 C   s   | j ||}tt|||S )z)generate random int w/ gauss distirbution)r   normalvariater   r   )ro   r   r   musigmar   r/   r/   r0   randintgauss  s   z*HandlerCase.FuzzHashGenerator.randintgaussc                 C   sX   | j }t|s	d S |jp|j}|j}|jdkr|}nt|d |j}| ||||d S )Nru  r  r  )rP   r   rv  rn   rt  r=   r  r  ro   rP   rA   r   r   r/   r/   r0   r    s   
z+HandlerCase.FuzzHashGenerator.random_roundsc                 C   sJ   | j }t|rd|jv sd S |j}|j}|jp|d }| ||||d S )Nr  r  r  )rP   r   rK   r  r  r  r  r  r/   r/   r0   r    s   z.HandlerCase.FuzzHashGenerator.random_salt_sizec                 C   sL   | j }| j}d|jvst|dsd S | dk rd S t|d|}||jS )Nr%  r'  r  re   )r   rP   rK   rJ   r   r   ri  r'  )ro   r   rP   r/   r/   r0   r    s   z*HandlerCase.FuzzHashGenerator.random_identc                 C   s`   |   }	 |   }| ||rnq| j}|ddr || j}|ddr,|| j}||fS )z=generate random password, and non-matching alternate passwordTr   r[   )random_passwordaccept_password_pairr   randintrw   password_encoding)ro   rd  r  r   r/   r/   r0   r    s   z2HandlerCase.FuzzHashGenerator.random_password_pairc                 C   s   | j }| dk rtdS | j}|jo|j}|pd}|dk s$| dk r0| dt|ddd}n| dt|dd	d
}t|| j	|}|rat
|trat|d|kra|dd }t|d|ksR|S )z*generate random passwords for fuzz testingg-C6?r   i?B r  r  r[      c   F      ru   Nr   )r   r   r   rP   r2  r9  r  r=   r   password_alphabetrf   r   r  rw   )ro   r   rP   r9  r  sizer   r/   r/   r0   r    s   z-HandlerCase.FuzzHashGenerator.random_passwordc                 C   s   ||kS )z-verify fuzz pair contains different passwordsr/   )ro   rd  r  r/   r/   r0   r    s   z2HandlerCase.FuzzHashGenerator.accept_password_pairN)r   r   r   r   r   r  r  r   r  r  r   r  r  r  r  r  r  r  r  r/   r/   r/   r0   r  i  s$    
		r  c              
   C   s  | j }|js!| t|d | t|d | | j | d| }| j|tdd | j	|
|d| d |  | jd }||}| j|tdd | j	|
|d| d | td|j| z	||}d	}W n ty } z
d	}|}W Y d	}~nd	}~ww |d	u r| j|td
d | || n| |t | dt| | }| jr| || n
|d	u r| || ||}	| jr| |	| n| |	| ||d }
| js|d	u r| |
| d	S | |
| d	S )z.disable() / .enable() methodsdisableenablern  z#disable() must return native stringr.  z0identify() didn't recognize disable() result: %rr[   zcannot restore original hashNz"enable() must return native stringr  )rP   rn  r  rJ   r  r  r  r   r   rm  r  r'  ri  rp  assertRaisesRegexr  r  r   r   r  )ro   rP   disabled_defaultr  disabled_stubr   r  edisabled_default2disabled_stub2disabled_otherr/   r/   r0   test_disable_and_enable  sh   



z#HandlerCase.test_disable_and_enableNF)FNNr2   NN)F)nr   r   r   r   rP   rX   ra  rb  rc  ri  rl  rp  r   r  rO  ro  r  rf  r"   r`  _HandlerCase__unittest_skippropertyr   r  rh  rk  rp  rr  rz  r{  r  rl  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r"  r$  r,  r1  r;  r>  rM  rT  rU  rX  r]  r_  r`  rc  rd  rg  rj  rm  rq  rs  r  rt  r  ry  r  r  r  r  r  r  r  r  r  r  r  r   r/   r/   r   r0   r&     s    




	






! )
	7#,)
6
2+8<&)34! m&,#&0'
YB


$ r&   c                       s   e Zd ZdZg ZdZdZdZ fddZe	dd Z
ed	d
 Zdd Ze	 fddZdd Zdd Zdd Zedd Zdd Zdd Z  ZS )r  a  helper used by create_backend_case() which adds additional features
    to test the os_crypt backend.

    * if crypt support is missing, inserts fake crypt support to simulate
      a working safe_crypt, to test passlib's codepath as fully as possible.

    * extra tests to verify non-conformant crypt implementations are handled
      correctly.

    * check that native crypt support is detected correctly for known platforms.
    Tr^  Fc                    s4   | j dksJ | jds|   tt|   d S )Nr^  )rX   rP   r]   _patch_safe_cryptr   r  r   r   r   r/   r0   r     s   zOsCryptMixin.setUpc                 C   s$   t | j}|  t|d}||fS )z
        return (handler, backend) pair to use for faking crypt.crypt() support for hash.
        backend will be None if none availabe.
        r^  )rT   rP   rV   rd   )r   rP   alt_backendr/   r/   r0   _get_safe_crypt_handler_backend  s   

z,OsCryptMixin._get_safe_crypt_handler_backendc                 C   s   |   d duS )z
        test if there's a fallback handler to test against if os_crypt can't support
        a specified secret (may be explicitly set to False for some subclasses)
        r   N)r  r   r/   r/   r0   has_os_crypt_fallback  s   z"OsCryptMixin.has_os_crypt_fallbackc                    sZ   |   \}}|std|   |  fdd}ddlm} | |d| d| _dS )zif crypt() doesn't support current hash alg, this patches
        safe_crypt() so that it transparently uses another one of the handler's
        backends, so that we can go ahead and test as much of code path
        as possible.
        z,handler has no available alternate backends!c                    s     | |}t|tsJ |S r2   )r  rf   r   rd  re  alt_handlerr/   r0   
crypt_stub  s   z2OsCryptMixin._patch_safe_crypt.<locals>.crypt_stubr   N_cryptT)r  r   r~  rW   r  utilsr   using_patched_crypt)ro   rP   r  r  modr/   r  r0   r    s   

zOsCryptMixin._patch_safe_cryptc                    sV   |dksJ t t| |}ddlm} || jkr)|r)tdr'|  d r'dS dS |S )z|
        make sure os_crypt backend is tested
        when it's known os_crypt will be faked by _patch_safe_crypt()
        r^  r   )	has_cryptrB   r[   Nz hash not supported by os crypt())r   r  r  r  r  r  r#   r  )r   rX   reasonr  r   r/   r0   r    s   z%OsCryptMixin._get_skip_backend_reasonc                    s8   ddl m}  fdd |j _d _| |d   S )z
        patch passlib.utils.safe_crypt() so it returns mock value for duration of test.
        returns function whose .return_value controls what's returned.
        this defaults to None.
        r   Nc                    s   | dkr
  | |S  jS )Nr]  )__wrapped__return_value)rd  rf  
mock_cryptr/   r0   r    s   z0OsCryptMixin._use_mock_crypt.<locals>.mock_cryptr  )r  r  r  r  r  r   )ro   r  r/   r  r0   _use_mock_crypt  s   zOsCryptMixin._use_mock_cryptc                    sb     d tjf   fdd}|ddd   |dd  |d  dS )	ztest with faulty crypt()r[   c                    s>   | _  jd  jd  jd d S r  )r  r   r  r  rl  r	  	exc_typesre  r  ro   r/   r0   r]    s   z/OsCryptMixin.test_80_faulty_crypt.<locals>.testz$xr  Nr   r  )rk  r   InternalBackendErrorr  )ro   r]  r/   r  r0   test_80_faulty_crypt  s   z!OsCryptMixin.test_80_faulty_cryptc                 C   s   |   }d|_| jr&| d}| d|}| || | | d| dS ddlm	} | 
 d }| || jd | || jd| | || jd| dS )ztest per-call crypt() fallbackNr  r   )r  r[   )r  r  r  r  r  r   rm  rl  rW  r  rk  r   )ro   r  rR  rS  err_typere  r/   r/   r0   test_81_crypt_fallback   s   
z#OsCryptMixin.test_81_crypt_fallbackc                 C   s   t | jdr| d| j }| jj}tj}| jD ]\}}t	||r% n
q| d||f |du r<| d||f ||krBdS |rO| 
d||f  dS | 
d||f  dS )a'  
        test platform-specific crypt() support detection

        NOTE: this is mainly just a sanity check to ensure the runtime
              detection is functioning correctly on some known platforms,
              so that we can feel more confident it'll work right on unknown ones.
        rH   znot applicable to wrappersz3no data for %r platform (current host support = %r)Nz9varied support on %r platform (current host support = %r)z5expected %r platform would have native support for %rz;did not expect %r platform would have native support for %r)rJ   rP   r  r  r   sysplatformplatform_crypt_supportr   r  r  )ro   using_backendr   r  patternexpectedr/   r/   r0   test_82_crypt_support  s2   


z"OsCryptMixin.test_82_crypt_supportc                    sP   j }jst|drdS ddlm ddlm  jj fdd}|S )ztest results against OS crypt()re   Nr   )crypt)_safe_crypt_lockc                    sN    |sdS t| }   | ||kW  d   S 1 s w   Y  dS )zstdlib-cryptr   N)crypt_supports_variantr   r  r   r  encodingro   r/   r0   check_cryptS  s   

$z5OsCryptMixin.fuzz_verifier_crypt.<locals>.check_crypt)rP   r  rJ   r  r  r   r  r  )ro   rP   r  r/   r  r0   fuzz_verifier_cryptE  s   	z OsCryptMixin.fuzz_verifier_cryptc                 C   s   dS )z~
        fuzzy_verified_crypt() helper --
        used to determine if os crypt() supports a particular hash variant.
        Tr/   r  r/   r/   r0   r  ^  s   z#OsCryptMixin.crypt_supports_variant)r   r   r   r   r  _OsCryptMixin__unittest_skiprX   r  r   r  r  r  r  r  r  r  r  r  rX  r  r  r  r   r/   r/   r   r0   r  f  s*    


/r  c                   @   sR   e Zd ZdZdZdZdZdZdd Zdd Z	d	d
 Z
dd ZG dd dejZdS )UserHandlerMixina(  helper for handlers w/ 'user' context kwd; mixin for HandlerCase

    this overrides the HandlerCase test harness methods
    so that a username is automatically inserted to hash/verify
    calls. as well, passing in a pair of strings as the password
    will be interpreted as (secret,user)
    userTFc                 C   s~   | j }d}|j|| jd}| jr,| t|j| | t|j|| | t|j|| dS || ||| ||| dS )ztest user context keywordr  r  N)rP   re  default_userrequires_userr   r   r  r  )ro   rP   passwordre  r/   r/   r0   test_80_user  s   
zUserHandlerMixin.test_80_userc                 C   sd   | j  }| }| jdt|dd}| jr$| | jd||dd dS | | jd||dd dS )ztest user case sensitivityr  r	  )r  z!user should not be case sensitivezuser should be case sensitiveN)	r
  r   r   r  r   user_case_insensitiverm  rl  r  )ro   r   r   re  r/   r/   r0   test_81_user_case  s   
z"UserHandlerMixin.test_81_user_casec                 C   sT   |   }| jd|dd}| jd|dd}| || | jd|dd}| || dS )ztest user used as saltr  adminr	  rootN)r  r  r   r  )ro   rf  rR  rS  h3r/   r/   r0   test_82_user_salt  s   z"UserHandlerMixin.test_82_user_saltc                 C   s8   t |tr
|\}}n| js|S | j}d|vr||d< |S )zinsert username into kwdsr  )rf   r   r  r
  )ro   rd  rc   r  r/   r/   r0   r{    s   

z!UserHandlerMixin.populate_contextc                   @   s4   e Zd Zejj Zejdd edZ	dd Z
dS )z"UserHandlerMixin.FuzzHashGeneratorrandom_userr	  	asdQWE123c                 C   s4   | j }| jjs| dk rd S t|| j|ddS )Nr'   r  r   )r   r]  r  r   r   user_alphabetr  )ro   r   r/   r/   r0   r    s   z.UserHandlerMixin.FuzzHashGenerator.random_userN)r   r   r   r&   r  r  rw  r|  r   r  r  r/   r/   r/   r0   r    s
    r  N)r   r   r   r   r
  r  r   _UserHandlerMixin__unittest_skipr  r  r  r{  r&   r  r/   r/   r/   r0   r  i  s    
r  c                   @   s@   e Zd ZdZdZeddedgZG dd dejZdd	 Z	d
S )EncodingHandlerMixina   helper for handlers w/ 'encoding' context kwd; mixin for HandlerCase

    this overrides the HandlerCase test harness methods
    so that an encoding can be inserted to hash/verify
    calls by passing in a pair of strings as the password
    will be interpreted as (secret,encoding)
    Tr]  s   testu   ¬ºc                   @   s   e Zd ZedZdS )z&EncodingHandlerMixin.FuzzHashGeneratoru   qwerty1234<>.@*#! ¬N)r   r   r   r   r  r/   r/   r/   r0   r    s    r  c                 C   s"   t |tr|\}}|d| |S )zinsert encoding into kwdsr  )rf   r   
setdefault)ro   rd  rc   r  r/   r/   r0   r{    s   
z%EncodingHandlerMixin.populate_contextN)
r   r   r   r   $_EncodingHandlerMixin__unittest_skipr   r  r&   r  r{  r/   r/   r/   r0   r    s    
r  c                       s:   e Zd ZdZd
 fdd	Z fddZ fdd	Z  ZS )r   z@catch_warnings() wrapper which clears warning registry & filtersalways.*c                    s:   t t| jdi | || _|rt|| _d S d | _d S )Nr/   )r   r   r   _reset_filterr   compile_reset_registry)ro   reset_filterreset_registryrc   r   r/   r0   r     s   zreset_warnings.__init__c                    s   t t|  }| jrt  t| j | j}|rFi  }| _t	t
j D ] \}}|d u s2||s3q%t|dd }|rE| ||< |  q%|S N__warningregistry__)r   r   r   r  r   resetwarningssimplefilterr  _orig_registryr   r  modulesr+  r  r   rw  clear)ro   r  r  backupr   r  regr   r/   r0   r     s    
zreset_warnings.__enter__c                    s   | j }|rA| j}ttj D ]1\}}|d u s||sqt|dd }|r)|  |	|}|r@|d u r;t
|d| q|| qtt| j|  d S r"  )r  r&  r   r  r'  r+  r  r   r(  r  rH  r|  r   r   r   )ro   r   r  r)  r   r  r*  rY   r   r/   r0   r     s    

zreset_warnings.__exit__)r  r  r   r/   r/   r   r0   r     s
    r   r  r2   r  )Nr[   r   )r   r   )jr   
__future__r   binasciir   rT  	functoolsr   r   r!  rL  r1  r   r   r   r   r(   r  r;  rQ  r+   rW  r   r   r[  r   r	   r   r
   passlibr   r   passlib.registryregistrypasslib.tests.backportsr   rS  r   r   r   r   r  r   r   r   r   r  r   r   r   r   r   r   r   r   r   r   r    r!   passlib.utils.decorr"   passlib.utils.handlersr  r  rM   __all__google.appenginegoogleImportErrorr  r1   r?   TICK_RESOLUTIONrE   rF   r  r  r,  r   rD   r#   rQ   rU   rZ   ra   rd   rT   rm   rU  rt   r$   r%   r   r   r   r   r   r   r  rX  r&   r  r  r  rP  r   r/   r/   r/   r0   <module>   s    0 	



 

   F                  y  a&