
     h                     .   d Z ddlZddlmZ ddlZ ej
                  e      ZddlZddl	Z
ddlmZ ddlmZmZ ddlmZmZ ddlmZ ddlmZmZmZ d	gZexZZej:                  d
k\  rddlmZ ej:                  dk\  rddlmZ dZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'ejP                  jR                  dk(  sJ d       ejP                  jT                  dk\  sJ d       d Z+ e+       Z,d Z- G d de      Z.dj_                  d      Z0e0dz  dd Z1e0d z  dd! Z2 G d" d#e      Z3y)$z"passlib.tests -- test passlib.totp    N)partial)exc)unicodeu)TestCase	time_call)totp)TOTP	AppWalletAES_SUPPORT
EngineTest)   r   )Error)r   r   abcdefs    4AOGGDBBQSYHNTUZs
   c!vΙs   ]90 Dy/4&S3JDVB7QD2R7JPXXJBSWY3DPEHPK3PXPs
   Hello!ޭ   zunexpected float_info.radix,   z#double precision unexpectedly smallc                      d} d}	 | dz  }	 t         j                   j                  |dz
        j                  }||k  rn|} ;| dz  } d}t        | |      S # t        t        t
        f$ r Y )w xY w)z8
    helper to calc max_time_t constant (see below)
    i   @r      l    )datetimeutcfromtimestampyear
ValueErrorOSErrorOverflowErrormin)valuer   
next_value	next_yearmax_datetime_timestamps        Y/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/passlib/tests/test_totp.py_get_max_time_tr$   8   s     ED
aZ
	 )):::a<HMMI, t9 > 
QJE
 *u,--C G]3 	 	s   ,A A+*A+c                     | dz  dz   dz  S )N          )raw_sizes    r#   to_b32_sizer+   i   s    qL1""    c                   N    e Zd ZdZd Zd Zd ZddZd Zd Z		 	 dd	Z
d
 Zd Zy)AppWalletTestzpasslib.totp.AppWalletc                    t               }| j                  |j                  i        | j                  |j                         ddd}t        |      }| j                  |j                  |       | j                  |j                         t        d      }| j                  |j                  |       t        d      }| j                  |j                  ddd       t        d      }| j                  |j                  |       | j                  t        t         d       | j                  t        t         d	       | j                  t        t         d
dd       y)z-constructor -- 'secrets' param -- input types   aaa   bbb12z
 1: aaa
# comment
 
2: bbb   z"1: aaa: bbb 
# comment
 
2: bbb   s   aaa: bbbz{"1":"aaa","2":"bbb"}{   z[123]aaa N)	r   assertEqual_secretsassertFalsehas_secrets
assertTrueassertRaises	TypeErrorr   )selfwalletrefs      r#   test_secrets_typesz AppWalletTest.test_secrets_typesv   s    "-++, (3#.**+ ?@#. BC&*IJ 23#. 	)Y4 	)Y8 	*iu21FGr,   c           	      "   dddd}t        |      }| j                  |j                  |       t        t        d      dt        d      dt        d      di      }| j                  |j                  |       t        dddd      }| j                  |j                  |       | j	                  t
        t         d	d
i       t        ddi      }| j	                  t        t         dd
i       | j	                  t        t         dd
i       t        t        d
      ddd      }| j                  |j                  |       | j	                  t
        t         ddi       | j	                  t
        t         ddi       | j	                  t
        t         dg i       y)z9constructor -- 'secrets' param -- tag/value normalizationr0   r1   s   ccc)r3   02Cr3   rD   rE   )r   rD   rE   )r   r6   z1-2_3.4z-abczab*$bbbr5   N)r   r8   r9   r   r=   r>   r   )r?   rA   r@   s      r#   test_secrets_tagszAppWalletTest.test_secrets_tags   sS    &v63#. AcFFAdGVQsVVLM#. vV&AB#. 	)Yu> Iv./ 	*i&%A*i&%A 5VDE#. 	)Yc
;)Yd<)Yb	:r,   c                    t        ddd      }| j                  |j                  d       | j                  |j                  |j                        d       t        dddd      }| j                  |j                  d       | j                  |j                  |j                        d	       t        ddddd
      }| j                  |j                  d
       | j                  |j                  |j                        d       | j	                  t
        t         ddddd       t               }| j                  |j                  d       | j	                  t
        |j                  d       y)z"constructor -- 'default_tag' paramonetwo)r3   rD   rD   s   twor6   )r3   rD   ArK   r0   r3   )default_tags   oneBN)r   r8   rL   
get_secretr=   KeyError)r?   r@   s     r#   test_default_tagzAppWalletTest.test_default_tag   s<    e45++T2**6+=+=>G e%@A++S1**6+=+=>G e%@cR++S1**6+=+=>G 	(IU%e/T&) 	 	+ ++T2(F$5$5t<r,   Nc                     t         r|xr	  |        y  y |xr | j                  t        |       | j                  d      )Nz$'cryptography' package not installed)r   r=   RuntimeErrorskipTest)r?   canarys     r#   require_aes_supportz!AppWalletTest.require_aes_support   s8    vx>t((v>-- FGGr,   c                 d   t        t        t        d      }t        ddddd      }| j	                  t        |j                  |             | j                  |j                  |      d	   t               t        ddd
dd      }| j                  |j                  |      d	   t               t        ddddd      }| j                  |j                  |      d	   t               |j                         }|j                  d       | j                  |j                  |      d	   d       |j                         }|j                  d       | j                  t        |j                  |       |j                         }|j                  d       | j                  t        |j                  |       y)z.decrypt_key()r2   r      6D7N7W53O7HHS37NLUFQMHCTEGSNPFN5CGBJr3   vcsktrT   r   SPZJ54Y6IPUD2BYA4C6AZGDXXTVQOWYLC2AUr&   FCCTARTIJWE7CPQHUDKA D2DRS32YESGHHINWFFCELKN7Z6NAHM4Mr4   r_   s
   D6.F7Q3  )r[   N)r   PASS1PASS2dictrU   r   decrypt_keyr8   KEY1_RAWKEY2_RAWcopyupdater=   rO   r   )r?   r@   CIPHER1CIPHER2CIPHER3temps         r#   test_decrypt_keyzAppWalletTest.test_decrypt_key   s~    U34 b$:+s4  0B0BG(L M 	++G4Q7B b$:+s4++G4Q7B a#9;sD++G4Q7B ||~c++D1!46OP ||~c(F$6$6= ||~c*f&8&8$?r,   c                    | j                          t        t        t        dd      }t	        ddddd      }| j                  |j                  |      d          |j                         }|j                  d	       | j                  |j                  |      d          |j                         }|j                  d
       | j                  |j                  |      d          y)z$.decrypt_key() -- needs_recrypt flagr2   rW   encrypt_costr   AAAAr4   rZ   r&   )r\   r3   re   N)
rU   r   rh   ri   rj   r:   rk   rn   ro   r<   )r?   r@   rA   rs   s       r#   test_decrypt_key_needs_recryptz,AppWalletTest.test_decrypt_key_needs_recrypt  s      "U3"E Q"&C8++C034 xxza**4034 xxzc**4034r,   c                    | j                  t        |      t        g d             | j                  |d   d       | j                  |d   |       | j                  |d   |j                         | j                  t        |d         t	        |j
                               | j                  t        |d         t	        t        |                   |j                  |      \  }}| j                  ||       | j                  ||       y)	z.check encrypt_key() result has expected format)r[   r_   r\   r]   r^   r[   r   r_   r\   r]   r^   N)r8   setrw   lenr+   	salt_sizerk   )r?   resultr@   keytagneeds_recrypt
result_keyresult_needs_recrypts           r#   assertSaneResultzAppWalletTest.assertSaneResult0  s     	Vc*C&DEa(c*f&9&9:VC[);v7G7G+HIVC[);s3x+@A+1+=+=f+E(
(S)-}=r,   c                    t        dt        id      }| j                  t        |j                  t
                     |j	                  t
              }| j                  ||t
               |j	                  t
              }| j                  ||t
               | j                  |d   |d          | j                  |d   |d          t        dt        id      }|j	                  t
              }| j                  ||t
               t        t        t        d      }|j	                  t
              }| j                  ||t
        d	
       t        dt        i      }d|_	        |j	                  t
              }| j                  ||t
               |j	                  t              }| j                  ||t               | j                  t        |j                  d       y)z.encrypt_key()r3   r(   rv   r`   r]   r^      r2   r4   )r   @   r,   N)r   rh   rU   r   encrypt_keyrl   r   assertNotEqualri   r}   rm   r=   r   )r?   r@   r~   otherwallet2s        r#   test_encrypt_keyzAppWalletTest.test_encrypt_keyA  s    C<a8  0B0BH(M N ##H-ffh7 ""8,ffh7E#Js4E#Js4 S%Lq9$$X.fgx8 %e45$$X.fgxSA S%L)$$X.fgx8 ##H-ffh7 	*f&8&8#>r,   c                 l   | j                          t        ddi      }|xj                  dz  c_        t        t	        |j
                  t              d      \  }}|xj                  dz  c_        t        t	        |j
                  t              d      \  }}| j                  ||dz  |dz  dz  	       y
)z verify cost parameter via timingr3   r6   r   r   )maxtimer   r&         ?)deltaN)rU   r   rw   r   r   r   rl   assertAlmostEqual)r?   r@   r   _delta2s        r#   test_encrypt_cost_timingz&AppWalletTest.test_encrypt_cost_timingj  s      " C<(q WV%7%7BANq 	q gf&8&8(CQO	 	vuQwuQwmDr,   N)r3   F)__name__
__module____qualname__descriptionPrefixrB   rG   rP   rU   rt   ry   r   r   r   r)   r,   r#   r.   r.   o   sF    00Hd!;J=@H$@L5, 9<',>"'?REr,   r.   12345678901234567890ascii    r'   r   c                   p    e Zd ZdZdZ fdZd ZdcdZd Z e	ddd	d
      Z
 e	dd      dddddg e	dd
      dddddg e	dd      ddg e	edd      ddd d!d"d#g e	edd$      d%d&d'd(d)d*g e	edd+      d,d-d.d/d0d1g e	d2d      d3d4g e	d2d5d67      d8g e	edd9:      d;g e	edd$d9<      d=g e	edd+d9<      d>ggZd? Zd@ ZdA ZdB ZdC ZdD ZdE ZdF ZdG ZdH ZdI ZdcdJZdK ZdL ZdM ZdN ZdO ZdP Z dddQZ!dR Z"dS Z#dT Z$dU Z%	 dcdVZ&	 dcdWZ'dX Z(dY Z)dZ Z*d[ Z+d\ Z,d] Z-d^ Z.d_ Z/d` Z0da Z1db Z2 xZ3S )eTotpTestz3
    common code shared by TotpTest & HotpTest
    zpasslib.totp.TOTPc                     t         t        |           ddlm} |j                          | j                  t        d| j                                y )Nr   )lookup_hashrng)	superr   setUppasslib.crypto.digestr   clear_cache	patchAttrtotp_module	getRandom)r?   r   	__class__s     r#   r   zTotpTest.setUp  s:    h#% 	6! 	{E4>>+;<r,   c                 L    | j                         j                         t        z  S )zY
        helper to generate random epoch time
        :returns float: epoch time
        )r   random
max_time_tr?   s    r#   randtimezTotpTest.randtime  s    
 ~~&&(:55r,   c                 "   | j                         }d|vrd|d<   |j                  d|j                  dd             |j                  d|j                  g d             |j                  d	|j                  dd
              |xs t        di |S )z@
        helper which generates a random TOTP instance.
        r   Tnewdigitsr   
   alg)sha1sha256sha512periodx   r)   )r   
setdefaultrandintchoicer
   )r?   clskwdsr   s       r#   randotpzTotpTest.randotp  s     nnDK#++a"45szz*FGH#++b#"67t$t$$r,   c                    | j                         }| j                         }| j                  |j                  |j                  d       t        d      D ]-  }|j                  |j                  k7  r n#| j                         }/ | j                  d       t        d      D ]-  }|j                  |j                  k7  r y| j                         }/ | j                  d       y)z,
        internal test -- randotp()
        zkey not randomized:r   zdigits not randomizedzalg not randomizedN)r   r   r   ranger   failr   )r?   otp1otp2r   s       r#   test_randotpzTotpTest.test_randotp  s     ||~||~DHHdhh0EF r 	/A{{dkk)<<>D	/
 II-. r 	,Axx488#<<>D	,
 II*+r,   base32r      r&   )formatr   r   r   ACDEFGHJKL234567r   )r   r   )鷼6T221105)鸼6T178491)鹼6Tr   )ռ6Tr   )ּ6T915114)r   20221105)r   86178491)r   r   )r   r   )r   03915114zS3JD-VB7Q-D2R7-JPXX)i5T000492)ST897212raw)r   r   r   );   94287082)5:B07081804)5:B14050471)I89005924) 5w69279037)    H/P 65353130r   )r   46119246)r   68084774)r   67062674)r   91819424)r   90698825)r   77737706r   )r   90693936)r   25091201)r   99943326)r   93441116)r   38618901)r   47863826r   )iS727248)i.ѡT122419	   )   )r   r   r   )iѡT	662331049<   )r   r   r   )r   19360094)r   r   r   r   )r   40857319)r   37023009c           	   #   b  K   ddl m} | j                  D ]  }| j                  j	                         }|j                  |d          |dd D ]^  }t        |      dk(  r|\  }}}n|\  }}d}t        j                  d||||        |di |}d|j                  d|d	|d
}	|||||	f `  yw)zv
        helper to iterate over test vectors.
        yields ``(totp, time, token, expires, prefix)`` tuples.
        r   r
   r   Nr   z+test vector: %r time=%r token=%r expires=%rzalg= time= token=z: r)   )
passlib.totpr
   vectorsvector_defaultsrn   ro   r|   logdebugr   )
r?   r
   rowr   entrytimetokenexpiresotpprefixs
             r#   iter_test_vectorszTotpTest.iter_test_vectors.  s     
 	&<< 	8C'',,.DKKAQR 8u:?+0(D%"'KD%"G 		GtUZ\cdlTl8;uM4778	8s   B-B/c                     | j                  t        t               | j                  t        t        dd       t        d      }t        d      }| j                  |j                  |j                         y)zconstructor -- 'new'  parameter4aoggdbbqsyhntuzT)r   r   r   N)r=   r>   r
   r   r   )r?   r  r   s      r#   test_ctor_w_newzTotpTest.test_ctor_w_newG  sX     	)T*)T/AtL tn~CGGTXX.r,   c                    | j                  t        t        dd      j                        d       | j                  t        t        dd      j                        d       | j                  t        t        dd      j                        d       | j                  t        t        dd	
      j                        d	       | j                  t        t        dd
      j                        d       | j	                  t
        t        ddd       | j	                  t
        t        dd
       | j                  t        t        j                  d      g      5  t        dd      }ddd       y# 1 sw Y   yxY w)z constructor -- 'size'  parameterTr   )r   r      r   r   r   r   r   r   size      )r   r  r   r   z-.*for security purposes, secret key must be.*category
message_re0A0A0A0A0A0A0A0A0AhexN)
r8   r|   r
   r   r=   r   assertWarningListrj   r   PasslibSecurityWarning)r?   r   s     r#   test_ctor_w_sizezTotpTest.test_ctor_w_sizeS  s-    	Td7;;<bATd9==>CTd9==>C 	Td48892>Td48892> 	*d26J 	*d1= ##c88Etu%  	$ VU#A	$ 	$ 	$s   E--E6c                 D   | j                  t        t              j                  t               | j                  t        t        j                               j                  t               | j                  t        d      j                  t               | j                  t        t        d       | j                  t        dd      j                  t               | j                  t        t        dd       | j                  t        t        d      j                  t               y)z,constructor -- 'key' and 'format' parametersz 4aog gdbb qsyh ntuz zao!ggdbbqsyhntuze01c630c2184b076ce99r  X01c630c2184b076ce99r   N)	r8   r
   KEY1r   rl   lowerr=   Base32DecodeErrorBase16DecodeErrorr   s    r#   test_ctor_w_key_and_formatz#TotpTest.test_ctor_w_key_and_formatk  s     	d2 	djjl+//: 	56::HE 	+T3EF 	4e<@@(K 	+T3I5Q 	h.22H=r,   c                     | j                  t        t        d      j                  d       | j                  t        t        d      j                  d       | j	                  t
        t        t        d       y)zconstructor -- 'alg' parameterzSHA-256r   r   SHA256zSHA-333N)r8   r
   r  r   r=   r   r   s    r#   test_ctor_w_algzTotpTest.test_ctor_w_alg  sW     	d	266Ad155x@ 	*dDi@r,   c                 4   | j                  t        t        t        d       | j	                  t        t        d      j
                  d       | j	                  t        t        d      j
                  d       | j                  t        t        t        d       y)z!constructor -- 'digits' parameterr(   r   r   r      N)r=   r   r
   r  r8   r   r   s    r#   test_ctor_w_digitszTotpTest.test_ctor_w_digits  sj    *dD;d1-44a8d2.55r:*dD<r,   c                    | j                  t        t              j                  d       | j                  t        t        d      j                  d       | j	                  t
        t        t        d       | j	                  t
        t        t        d       | j	                  t        t        t        d       | j	                  t        t        t        d       y)	z!constructor -- 'period' parameterr   ?   r         ?abcr   N)r8   r
   r  r   r=   r>   r   r   s    r#   test_ctor_w_periodzTotpTest.test_ctor_w_period  s     	d**B/ 	d2.55r: 	)T4<)T4> 	*dD;*dD<r,   c                     | j                  t        t              j                  d       | j                  t        t        d      j                  d       | j	                  t
        t        t        d       y)z constructor -- 'label' parameterNzfoo@bar)labelzfoo:bar)r8   r
   r  r4  r=   r   r   s    r#   test_ctor_w_labelzTotpTest.test_ctor_w_label  sP    d))40d)4::IF*dD	Br,   c                     | j                  t        t              j                  d       | j                  t        t        d      j                  d       | j	                  t
        t        t        d       y)z!constructor -- 'issuer' parameterNzfoo.comissuerzfoo.com:bar)r8   r
   r  r8  r=   r   r   s    r#   test_ctor_w_issuerzTotpTest.test_ctor_w_issuer  sP    d**D1d95<<iH*dDGr,   c                    | j                  t        t              j                  d       | j                   t        j                  d      t              j                  d       | j                  t        t        j                  d       | j                  t        t        j                  d       | j                  t        t        j                  d       | j                  t        t        j                  d       y)	zusing() -- 'period' parameterr   r-  r.  r/  r0  r   r1  N)r8   r
   r  r   usingr=   r>   r   r   s    r#   test_using_w_periodzTotpTest.test_using_w_period  s     	d**B/ 	.2.t4;;R@ 	)TZZ<)TZZ> 	*djj;*djj<r,   c                    | j                         }| j                  |j                  t        j                         | j                  |j                  d      t        t        j                                      dgfd}| j                  t        j                  |            }| j                  |j                  d      d       | j                  |j                  d      d       | j                  t        t        j                  d       d	}| j                  t        |t        j                  d
        | j                  t        |t        j                  d        y)zusing -- 'now' parameterNgHz^@c                  (     dxx   dz  cc<    d   S )Nr   r   r)   )counters   r#   nowz&TotpTest.test_using_w_now.<locals>.now  s    AJ!OJ1:r,   r@  )r   ~      r5   z)now\(\) function must return non-negativec                       y)Nr0  r)   r)   r,   r#   <lambda>z+TotpTest.test_using_w_now.<locals>.<lambda>      r,   c                       y)Nr1  r)   r)   r,   r#   rE  z+TotpTest.test_using_w_now.<locals>.<lambda>  rF  r,   )r   assertIsr@  _timer  r   normalize_timeintr
   r;  r8   r=   r>   assertRaisesRegexAssertionError)r?   r  r@  msg_rer?  s       @r#   test_using_w_nowzTotpTest.test_using_w_now  s    llncgguzz*s11$7UZZ\9JK (	 lltzzc2l3++D137++D137 	)TZZS9 >~vtzz}U 	~vtzzzRr,   c                    || j                  d      }| j                  |j                  t        d            d       | j                  |j                  d      d       | j                  |j                  d      d       | j                  |j                  d      d       | j	                  t
        |j                  d	       | j	                  t
        |j                  d       | j	                  t        j                  |j                  d
       | j	                  t        j                  |j                  d       | j	                  t        j                  |j                  d       y)z$normalize_token() -- instance methodN   r)  1234567s   1234567i iG 0234567g    2A12345601234567iNa )r   r8   normalize_tokenr   r=   r>   r   MalformedTokenErrorr?   r  s     r#   test_normalize_token_instancez&TotpTest.test_normalize_token_instance  s   ;,,a,(C 	,,Qy\:IF,,Z8)D 	,,W5yA 	,,V4i@ 	)S%8%8)D)S%8%8$? 	#1133F3FQ 	#1133F3F
S#1133F3FQr,   c                 P    | j                  t        j                  d             y)z!normalize_token() -- class methodrQ  r)  )r  N)rY  r
   r;  r   s    r#   test_normalize_token_classz#TotpTest.test_normalize_token_class  s    **tzz/C*Dr,   c                    t        j                         }| j                  |      }t        d      D ]  }| j	                         t              }| j                  |j                        |       | j                  |j                  |dz         |       | j                  |j                  |      |       t        j                  j                        }| j                  |j                  |      |       |j                  }	 t        fd      |_
        | j                  |j                  d      |       ||_
         | j                  t        |j                  d       y# ||_
        w xY w)znormalize_time()r   r   c                       S r   r)   r  s   r#   rE  z.TotpTest.test_normalize_time.<locals>.<lambda>  s    t r,   N1234)r
   r;  r   r   r   rK  r8   rJ  r   r   r@  staticmethodr=   r>   )r?   TotpFactoryr  r   tintdtorigr  s          @r#   test_normalize_timezTotpTest.test_normalize_time  s*   jjlll;'r 	'A==?Dt9DS//5t<S//s
;TBS//5t<""33D9BS//3T:??D'".|"<  !3!3D!94@"&#	'& 	)S%7%7@ #'s   4E$$	E-c                    | j                         }t        t        d      }| j                  |j                  t               | j                  |j
                  d       | j                  |j                  t               | j                  |j                         d       | j                  |j                  d      d       | j                  |j                  d      t               | j                  |j                  d	      d
       t        d|j                  dd            }|j
                  }|j                  }|j                         }y)z pretty_key() and .key attributesr   r  z4AOG-GDBB-QSYH-NTUZ )sepz4AOG GDBB QSYH NTUZFr  )r   ze01c-630c-2184-b076-ce99Tr   r  r  N)
r   r
   rl   r8   r   hex_key
base32_keyr  
pretty_keyr   )r?   r   r  r   s       r#   test_key_attrszTotpTest.test_key_attrs&  s    nn 8U#(+&<=. 	)+@AC02GHE2D9u57QR t#++b""56KKNNNNr,   c                 |   ddl m}m}  |d      }|j                  d      }| j	                  ||       | j                  |j                  d       | j                  |j                  d       | j                  |j                  d       | j                  |d       | j                  t        |      d	       | j                  |d   d       | j                  |d
   d       | j                  t        |j                  d       | j                  t        |j                  d	       | j                  |       d |_        | j                  |j                  d       | j                  |j                          d |_        | j                  |j                  d       | j#                  |j                          |j                  d      }| j%                  ||       | j                  ||       |j                  d      }| j%                  ||       | j                  ||       |j                  d      }| j'                  ||       y)zgenerate() -- TotpToken() classr   )r
   	TotpTokens3jdvb7qd2r7jpxxr   r   iTT)r   rp  r   r   c                       y)Ng  Tn'Ar)   r)   r,   r#   rE  z*TotpTest.test_totp_token.<locals>.<lambda>T  rF  r,   r   c                       y)NiUTr)   r)   r,   r#   rE  z*TotpTest.test_totp_token.<locals>.<lambda>X  rF  r,   i7TiWTN)r   r
   rn  generateassertIsInstancer8   r  r?  expire_timer|   r=   
IndexError__getitem__r<   r@  	remainingvalidr:   assertIsNotr   )r?   r
   rn  r  r~   result2result3result4s           r#   test_totp_tokenzTotpTest.test_totp_token?  s   0 %&j)fi0x02++Z8!78Va(H-J/*f&8&8"=*f&8&8!< ())3/%%))1-& ,,z*&)&) ,,z*&)&) ,,z*GV,r,   c                 ^   ddl m}  |d      }| j                         |j                        }|j                  }| j                  |t               |j                  dz  }| j                  |j                  |dz         j                  |       | j                  |j                  |dz         j                  |       t        j                  j                        }| j                  t        |j                  |            t                     | j                  |j                  |      j                  |         |j                  fd      |j                  	      }| j                  |j                         j                  |       | j!                  t"        |j                  d
       y)z
generate()r   r   Tr  r      c                       S r   r)   r^  s   r#   rE  z(TotpTest.test_generate.<locals>.<lambda>  s    d r,   rA  )r   r1  N)r   r
   r   rt  r  ru  r   r?  r8   r   r   r   rK  rJ  r;  rj  r=   r   )	r?   r
   r  r~   r  
start_timerc  r   r  s	           @r#   test_generatezTotpTest.test_generatej  sI   % tn}}d#eW-^^b(
 	j2o6<<eD 	CLLb9??G //5S//34c$i@ 	b)//7 ,ztzzl+?..6 	*cllB7r,   c                 ,   | j                         D ]  \  }}}}}|j                  |      }| j                  |j                  ||       | j                  |j                  ||j
                  z  |       |sf| j                  |j                  |        y)zgenerate() -- reference vectorsmsgN)r	  rt  r8   r  r?  r   rv  )r?   r  r  r  r  r  r~   s          r#   !test_generate_w_reference_vectorsz*TotpTest.test_generate_w_reference_vectors  s    151G1G1I 	>-Cugv\\$'FV\\5f=V^^TSZZ-?VL  !3!3W=	>r,   c                    ddl m} | j                  ||       | j                  |j                  t               | j                  |j                  j                  |       | j                  |j                  ||dz          ||z  }||z   }	| j                  |j                  |	|dz          | j                  |j                  ||dz          | j                  |j                  ||dz          | j                  |j                  ||z          |	dz   |z  }
| j                  |j                  |
       | j                  |j                  |
|z          | j                  t        |      d	       | j                  ||	|f       | j                  t         |j"                  d
       | j                  |d   |	       | j                  |d   |       | j                  t         |j"                  d	       | j%                  |       y )Nr   	TotpMatchz matched time:r  z matched counter:z expected counter:z	 skipped:r   r   rq  )r   r  ru  r	   r
   r8   r   r  r?  expected_counterskippedcache_secondsrv  
cache_timer|   r=   rw  rx  r<   )r?   matchr  r  r   windowr  r  expectedr?  rv  s              r#   assertTotpMatchzTotpTest.assertTotpMatch  s   * 	eY/ 	ejj$/**F3 	Ts5E/EF6>W$S;N5NO//sEY?YZS;5FG,,fvo>{f,**K8));+?@ 	UQ'$0*e&7&7<q7+q4(*e&7&7; 	r,   c                     dd} t        j                  fd      t              }|j                  |      }| j	                  |d       y)z!match() -- valid TotpMatch objectk781501c                       dz   S NiQ r)   r^  s   r#   rE  z8TotpTest.test_totp_match_w_valid_token.<locals>.<lambda>      TI%5 r,   rA  r   r  r  Nr
   r;  KEY3r  r  r?   r  r  r~   r  s       @r#   test_totp_match_w_valid_tokenz&TotpTest.test_totp_match_w_valid_token  sH    6djj56t<5$'V$:r,   c                     ddl m} dd} t        j                  fd      t              }|j                  |dz
        }| j                  |dz
  d	       y
)z3match() -- valid TotpMatch object with future tokenr   r  r  r  c                       dz   S r  r)   r^  s   r#   rE  z8TotpTest.test_totp_match_w_older_token.<locals>.<lambda>  r  r,   rA  r   r   r  N)r   r  r
   r;  r  r  r  )r?   r  r  r  r~   r  s        @r#   test_totp_match_w_older_tokenz&TotpTest.test_totp_match_w_older_token  sS    *6djj56t<5$),V$)Q?r,   c                     dd} t        j                  fd      t              }|j                  |dz         }| j	                  |dz   d       y)	z1match() -- valid TotpMatch object with past tokenr  r  c                       dz   S r  r)   r^  s   r#   rE  z6TotpTest.test_totp_match_w_new_token.<locals>.<lambda>  r  r,   rA  r   r1  r  Nr  r  s       @r#   test_totp_match_w_new_tokenz$TotpTest.test_totp_match_w_new_token  sP    6djj56t<5$),V$)R@r,   c                     dd} t        j                  fd      t              }| j                  t        j
                  |j                  |dz          y)z#match() -- invalid TotpMatch objectr  r  c                       dz   S r  r)   r^  s   r#   rE  z:TotpTest.test_totp_match_w_invalid_token.<locals>.<lambda>  r  r,   rA  r   N)r
   r;  r  r=   r   InvalidTokenErrorr  )r?   r  r  r  s      @r#   test_totp_match_w_invalid_tokenz(TotpTest.test_totp_match_w_invalid_token  sE    6djj56t<#//E4"9Mr,   c                    d|j                   d|j                  d|j                  d|d|d|d} |j                  ||fi |}| j	                  ||j                  |      |j                  |j                  dd	      ||
       y)z,helper to test otp.match() output is correctkey= alg= period=r   
 gen_time=r   :r  r   )r  r   r  r  r  N)rj  r   r   r  r  rJ  get)	r?   expect_skippedr  r  r  gen_timer   r  r~   s	            r#   assertVerifyMatcheszTotpTest.assertVerifyMatches  sz    
 ~~sww

E8TK5$/$/V"%"4"4T":$'JJ$(HHXr$:%3!$ 	 	&r,   c                     d|j                   d|j                  d|j                  d|d|d|d} | j                  ||j                  ||fd|i|S )	z/helper to test otp.match() throws correct errorr  r  r  r   r  r   r  __msg__)rj  r   r   r=   r  )r?   	exc_classr  r  r  r  r   r  s           r#   assertVerifyRaiseszTotpTest.assertVerifyRaises  sV     ~~sww

E8TK t  CIIud 6),6046 	6r,   c                 0   | j                         }|j                  }| j                         }|j                  |      j                  }t        ||      }t        | j                  fi |}t        | j                  fi |} |t        j                  |||z
  d        |d|||z
  |        |d|||z
  d|z          |d||d        |t        j                  |||z   d        |d|||z   |        |d|||z   d|z          |t        j                  ||d|z  z   d        |t        j                  ||d|z  z   |        |d||d|z  z   d|z         t        j                  j                  |      } |d||d        |t        |d       y)	z)match() -- 'time' and 'window' parametersr  r  r   r  r   r   r1  N)r   r   r   rt  r  rj   r   r  r  r   r  r   r   r   )	r?   r  r   r  r  commonassertMatchesr=   rc  s	            r#   test_match_w_windowzTotpTest.test_match_w_window  s    lln}}T"((#- 8 8CFCt66A&A 	S**E4&=Kb%v>b%q6zB 	aQ/ 	S**E4&=Kb%v>b%q6zB 	S**E4!f*3DQOS**E4!f*3DVTb%F
!21v:F //5a1- 	Z+r,   c                    | j                         }|j                  }| j                         }t        ||      }t	        | j
                  fi |}t	        | j                  fi |}d|z  }|j                  ||z
        j                  } |t        j                  ||d        |d||d|        |j                  ||z         j                  }	 |t        j                  |	|d        |d|	|d|       y)zmatch() -- 'skew' parametersr  r   r   r  rq  )r  skewN)r   r   r   rj   r   r  r  rt  r  r   r  )
r?   r  r   r  r  r  r=   r  behind_tokenahead_tokens
             r#   test_match_w_skewzTotpTest.test_match_w_skew&  s     lln}}#- 8 8CFCt66A&A 6z||D4K066S**L$qIb,QdUC ll4$;/55S**KaHb+tADAr,   c                    | j                         }|j                  }| j                         }|j                  |      }|j                  }|j
                  }|j                  }t        ||      }t        | j                  fi |}	t        | j                  fi |}
 |	d|||z   |        |	d|||z   |dz
  |        |
t        j                  ||d|z  z   ||        |
t        j                  |||z   ||      }| j                  |j                  |        |
t        j                  |||d      }| j                  |j                  |       y)	z0match() -- 'reuse' and 'last_counter' parametersr  r1  r  r   )last_counterr  r   r   N)r   r   r   rt  r  r?  rv  rj   r   r  r  r   r  UsedTokenErrorr8   )r?   r  r   r  tdatar  r?  rv  r  r  r=   errs               r#   test_match_w_reusezTotpTest.test_match_w_reuse=  s@    lln}}T"--''#- 8 8CFCt66A&A 	b%v> 	b%WQY#	%
 	S**E4!f*3D")&	:
 3--udVm(/@+6 3--ud(/;+6r,   c                 b   t        d      }|j                  }d}| j                   |d|             | j                   |d|             | j                  t        j
                  |d|       | j                  t        j
                  |d|       | j                  t        j
                  |d|       y)	zmatch() -- token normalizationotxl2f5cctbprpzx6Tz    3 32-136  s   3321361234512345X0123456N)r
   r  r<   r=   r   rW  )r?   r  r  r  s       r#    test_match_w_token_normalizationz)TotpTest.test_match_w_token_normalizationf  s     %&		 	.56 	i./ 	#115'4H 	#115(DI 	#115)TJr,   c                 *   | j                         D ]  \  }}}}}|j                  } |||      }| j                  |       | j                  |j                  ||j
                  z  |       | j                  t        j                  |||dz   d        y)zmatch() -- reference vectorsr  d   r   r  N)	r	  r  r<   r8   r?  r   r=   r   r  )r?   r  r  r  r  r  r  r~   s           r#   test_match_w_reference_vectorsz'TotpTest.test_match_w_reference_vectors|  s    .2.D.D.F 
	Y*CugsIIE 5$'FOOF#V^^TSZZ-?SI c33UE4#:VWX
	Yr,   c                    ddl m} d |j                  fd      }t        ddd	      }|j	                  d
|      }| j                  |       t        ddd	      }| j                  t        j                  |j                  d|       t        dd      }| j                  t        |j                  d|       d}|j	                  d
|      }| j                  |       d}|j	                  d
|      }| j                  |       y)zverify()r   r   r  c                       S r   r)   r^  s   r#   rE  z&TotpTest.test_verify.<locals>.<lambda>  s    T r,   rA  r   r	   r  r[   typer   332136r^  332155r[   r  z3{"v": 1, "type": "totp", "key": "otxl2f5cctbprpzx"}z,otpauth://totp/Label?secret=otxl2f5cctbprpzxN)
r   r
   r;  rj   verifyr  r=   r   r  r   )r?   r
   ra  source1r  source1json
source1urir  s          @r#   test_verifyzTotpTest.test_verify  s   
 	& djj\2 -?@""8W5U. -?@#//1C1CXwW (*k&8&8(GL L""8[9U. D
""8Z8U.r,   c                    ddl m} |j                  } |t        d            }| j	                  |j
                  t                |d      }| j	                  |j
                  t                |t        ddt                    }| j	                  |j
                  t                |t        d            }| j	                  |j
                  t                |d	      }| j	                  |j
                  t               | j                   ||      |       t               } |j                  |
      j                  |      }| j                  ||       | j	                  |j                         |j                                 |j                  |
      j                  |      }| j                  ||       | j                  t        |t        d             | j                  t        |d       y)zfrom_source()r   r   Notpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=ExamplesN   otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Exampler   r	   r  z3{"v": 1, "type": "totp", "key": "JBSWY3DPEHPK3PXP"}s3   {"v": 1, "type": "totp", "key": "JBSWY3DPEHPK3PXP"})r@   foos   fooN)r   r
   from_sourcer   r8   r   KEY4_RAWrj   KEY4rH  r   r;  r{  to_dictr=   r   )r?   r
   r  r  wallet1r   r   s          r#   test_from_sourcezTotpTest.test_from_source  s   %&& ! - . /(+  , -(+ $T:;(+ !QRS(+ PQ(+ 	k#&, +tzz)55c:s#7 tzz)55d;dD! 	*k1U8<*k6:r,   c                    ddl m} |j                  } |d      }| j                  ||       | j	                  |j
                  t               | j	                  |j                  d       | j	                  |j                  d       | j	                  |j                  d       | j	                  |j                  d       | j	                  |j                  d        |d	      }| j	                  |j
                  t               | j                  t        |d
       | j                  t        |d        |d      }| j	                  |j                  d       | j	                  |j                  d        |d      }| j	                  |j                  d       | j	                  |j                  d        |d      }| j	                  |j                  d       | j	                  |j                  d       | j                  t        |j                  d        |d      }| j	                  |j                  d       | j                  t        |d        |d      }| j	                  |j                  d       | j                  t        |d       | j                  t        |d       | j                  t        |d        |d      }| j	                  |j                  d       | j                  t        |d       | j                  t        |d       | j                  t!        t"        j$                  d !      g      5   |d"      }d#d#d#       | j	                  |j&                  t(               | j	                  |j                  d       y## 1 sw Y   FxY w)$z
from_uri()r   r   r  alice@google.comExampler   r   r   zNotpauth://totp/Example:alice@google.com?secret=jbswy3dpehpk3pxp&issuer=Examplez0otpauth://totp/Example:alice@google.com?digits=6z?otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHP@3PXPzOotpauth://totp/Provider1:Alice%20Smith?secret=JBSWY3DPEHPK3PXP&issuer=Provider1Alice Smith	Provider1zMotpauth://totp/Big%20Corporation%3A%20alice@bigco.com?secret=JBSWY3DPEHPK3PXPzalice@bigco.comzBig CorporationzOotpauth://totp/alice@bigco.com?secret=JBSWY3DPEHPK3PXP&issuer=Big%20CorporationzGotpauth://totp/Provider1:alice?secret=JBSWY3DPEHPK3PXP&issuer=Provider2zPotpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&algorithm=SHA256r   zPotpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&algorithm=SHA333zHotpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&digits=8r&   zHotpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&digits=AzJotpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&digits=%20zIotpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&digits=15zIotpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&period=63r-  zHotpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&period=0zIotpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&period=-1z!unexpected parameters encounteredr  zQotpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&foo=bar&period=63N)r   r
   from_uriru  r8   r   r  r4  r8  r   r   r   r=   r   r!  r  rj   r   PasslibRuntimeWarningrj  r  )r?   r
   r  r  s       r#   test_from_urizTotpTest.test_from_uri  s*   %==  ( )c4((+$67Y/&)R(Q'  ( )(+ 	*h0bc 	+X 8Q 	R  * +M2[1  1 2$56%67 hi$56%67 	*dmmc	e ij(+ 	*h 1[ 	\ abQ' 	*h0z{*h0|}*h0{| bcR( 	*h 1S 	T 	*h 1T 	U ###33@cd%
  	0  / 0C	0
 	.R(	0 	0s   1	M??Nc                 6   t        t        ddd      }| j                  |j                  dd      d       | j	                  t
        |j                  dd       | j                  |j                  d      d	       d|_        | j                  |j                         d	       d|_        | j                  |j                         d       | j	                  t
        |j                  d
       | j	                  t
        |j                  dd       | j                  t        t        d      j                  d      d       | j                  t        t        d      j                  d      d       | j                  t        t        d      j                  d      d       y)zto_uri()r   r   r   r   r   r   r  Example OrgzZotpauth://totp/Example%20Org:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example%20OrgNz7otpauth://totp/alice@google.com?secret=JBSWY3DPEHPK3PXPzlabel:with:semicolonszissuer:with:semicolonsr   r%  zHotpauth://totp/alice@google.com?secret=JBSWY3DPEHPK3PXP&algorithm=SHA256r&   r)  z@otpauth://totp/alice@google.com?secret=JBSWY3DPEHPK3PXP&digits=8r-  r.  zAotpauth://totp/alice@google.com?secret=JBSWY3DPEHPK3PXP&period=63)r
   r  r8   to_urir=   r   r4  r8  rX  s     r#   test_to_urizTotpTest.test_to_uriW  si    4VAb9$6F0	1
 	*cjj$F 	$67R	T '	R	T #
1	2
 	*cjj2IJ 	*cjj2DF^_
 	d1889KL,	- 	d1-445GH$	% 	d2.556HI%	&r,   c           
         ddl m} |j                  } |t        ddt        dd            }| j                  ||       | j                  |j                  t               | j                  |j                  d       | j                  |j                  d       | j                  |j                  d       | j                  |j                  d	       | j                  |j                  d
       | j                  t        |t        dt                     | j                  t        |t        ddt                     | j                  t        |t        ddt                     | j                  t        |t        dt                      |t        ddt        j!                         dd            }| j                  |j                  t               | j                  t        |t        dd             | j                  t"        |t        ddd              |t        ddt        dd            }| j                  |j                  d       | j                  |j                  d        |t        ddt        d            }| j                  |j                  d       | j                  t        |t        ddt        d              |t        ddt        d            }| j                  |j                  d       | j                  t$        |t        ddt        d             | j                  t        |t        ddt        d              |t        ddt        d            }| j                  |j                  d       | j                  t        |t        ddt        d             | j                  t        |t        ddt        d             | j                  t$        |t        ddt        d             y) zfrom_dict()r   r   r   r	   r  r  r[   r  r   r4  r8  r   r   r   )r  r   r  rg   )r[   r   r  zJBSWY3DPEHP@3PXPr  r  r   r[   r  r   r   sha333r&   r[   r  r   r   rK      r-  r[   r  r   r   r1  r5   )r[   r  r   INVALIDN)r   r
   	from_dictrj   r  ru  r8   r   r  r4  r8  r   r   r   r=   r   r   r!  r>   )r?   r
   r  r  s       r#   test_from_dictzTotpTest.test_from_dict  s   %NN	
 qv4?QZcdec4((+$67Y/&)R(Q' 	*i6t1LM 	*iD1QR*i&d1ST 	*it1DE qv4::<GYbklm(+ 	*i1GH 	+Y5GH	J qv4}U`abM2[1 qv4XFG(+ 	*iDV^1_` qv4BCQ' 	)Yqv4X[0\]*iDY[1\] qv4CDR( 	*iDYZ1[\*iDY[1\]
 	)Yqv4Y\0]^r,   c           
         t        t        ddd      }| j                  |j                         t	        ddt                     t        t        ddddd	
      }| j                  |j                         t	        ddt        dd	             t        t        dddd      }| j                  |j                         t	        ddt        d             t        t        dddd	      }| j                  |j                         t	        ddt        d	             t        j
                  d	      } |t              }| j                  |j                         t	        ddt                      |t        d	      }| j                  |j                         t	        ddt                     | j                  t        t        d      j                         t	        ddt        d             | j                  t        t        d      j                         t	        ddt        d             | j                  t        t        d      j                         t	        ddt        d             y)z	to_dict()r   r   r   r  r   r	   r  r  r  )r   r   r   r4  r8  r  )r   r   r   r4  )r[   r  r   r4  )r   r   r   r8  )r[   r  r   r8  r7  r   r%  r  r&   r)  r  r-  r.  r  N)r
   r  r8   r  rj   r;  )r?   r  ra  s      r#   test_to_dictzTotpTest.test_to_dict  s    4VAb9qv4(HI 4VAb+MCD$6}N	O
 4VAb+-D$68	9
 4VAb')D%24	5
 jj6$qv4(HI $}5D)IJ
 	d199;DhG	I 	d1-557DC	E 	d2.668DD	Fr,   r   )r   r   r   r7   )4r   r   r   __doc__r   r   r   r   r   rj   r   RFC_KEY_BYTES_20RFC_KEY_BYTES_32RFC_KEY_BYTES_64r   r	  r  r  r#  r'  r+  r2  r5  r9  r<  rO  rY  r[  re  rl  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   __classcell__)r   s   @r#   r   r     sN    ,
=6
%,< (r!LO 
$Q	/"""""	
 
$Q	/$$$$$	
 
'	2""	
 
"5f	=$$$$%	
 
"5h	?$$$$%	
 
"5h	?$$$$%	
 
$Q	/1GI_`	$Qr	:<UV 
"5	<>VW	"5hr	JLde	"5hr	JLdeaRGh82
/$0>0A=="CH="SDR4EA<2)-V8@>B;@AN +/& )-6.,`B.'7RK,Y"/H*;^u)n8&z\_|9Fr,   r   )4r  r   	functoolsr   logging	getLoggerr   r   sysr  rI  passlibr   passlib.utils.compatr   r   passlib.tests.utilsr   r   r	   r   r   r
   r   r   __all__r>   r!  r"  version_infobinasciir   rh   ri   r  rl   rm   r  r  r  
float_inforadixmant_digr$   r   r+   r.   encoder  r  r  r   r)   r,   r#   <module>r     sH   (
   'g''1 
   + 3 ' 5 5  )2 1 %u3u3-C$ ~~q  ?"? ?
~~"$ K&K K+.^ 
#JEH JEj *009 $Q&, $Q&, 
`Fx `Fr,   