a
    Oʞhr                     @   s  d Z ddlmZ ddlmZ ddlZddlZddlmZ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 dd
lmZ ddlmZ ddlmZ eeZdddZejdpdZdddZ dd Z!dd Z"e" \Z#Z$e%e#dZ&G dd deZ'G dd deZ(dS )z@tests for passlib.apache -- (c) Assurance Technologies 2008-2011    )with_statement)	getLoggerN)apacheregistry)MissingBackendError)irange)unittest)TestCaseget_fileset_fileensure_mtime_changed)u)to_bytes)to_unicode_for_identify
   c                 C   s0   t j| }t j| | }t | ||f dS )z)backdate file's mtime by specified amountN)ospathgetatimegetmtimeutime)r   offsetatimemtime r   T/wd/webapps/venvs/v2025_4um/lib/python3.9/site-packages/passlib/tests/test_apache.pybackdate_file_mtime   s    r   ZPASSLIB_TEST_HTPASSWD_PATHZhtpasswdc                 C   sb   |dur| d}tjtg|  tjtj|r0tjndd}||\}}| }t|pVd}||fS )z$
    helper to run htpasswd cmd
    Nutf-8)stdoutstderrstdin )	encode
subprocessPopenhtpasswd_pathPIPESTDOUTcommunicatewaitr   )argsr   procouterrrcr   r   r   _call_htpasswd+   s    
r.   c                 C   s   t d| |g|\}}| S )z%
    wrapper for htpasswd verify
    z-vi)r.   )r   userpasswordr+   r-   r   r   r   _call_htpasswd_verify9   s    r1   c                  C   sD   zt g \} }W n ty$   Y dS 0 |s4td d| v }d|fS )z0
    helper to check if htpasswd is present
    )FFz htpasswd test returned with rc=0z -B T)r.   OSErrorlogwarning)r+   r-   Zhave_bcryptr   r   r   _detect_htpasswdA   s    
r5   z requires `htpasswd` cmdline toolc                   @   s   e Zd ZdZdZd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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$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zed.d/ Zee !e"#d0d1d2d3 Z$d4S )5HtpasswdFileTestztest HtpasswdFile classHtpasswdFilest   user2:2CHkkwa2AtqGs
user3:{SHA}3ipNV1GrBtxPmHFC21fCbVCSXIo=
user4:pass4
user1:$apr1$t4tc7jTh$GPIWVUo8sQKJlUdV8V5vu0
s4   user3:{SHA}3ipNV1GrBtxPmHFC21fCbVCSXIo=
user4:pass4
sy   user2:pass2x
user3:{SHA}3ipNV1GrBtxPmHFC21fCbVCSXIo=
user4:pass4
user1:$apr1$t4tc7jTh$GPIWVUo8sQKJlUdV8V5vu0
user5:pass5
s   useræ:2CHkkwa2AtqGs
s   user:2CHkkwa2AtqGs
s   user1:pass1
user1:pass2
s  user2:2CHkkwa2AtqGs
user3:{SHA}3ipNV1GrBtxPmHFC21fCbVCSXIo=
user4:pass4
user1:$apr1$t4tc7jTh$GPIWVUo8sQKJlUdV8V5vu0
user5:$2a$12$yktDxraxijBZ360orOyCOePFGhuis/umyPNJoL5EbsLk.s6SWdrRO
user6:$5$rounds=110000$cCRp/xUUGVgwR4aP$p0.QKFS5qLNRqw1/47lXYiAcgIjJK.WjCO8nrEKuUK.
c                 C   s*  |   }t|| j t|}| | | j | |j| | |j	 |d |_| |j|d  | 
|j	 tj|dd}| | d | |j| | 
|j	 | d tj|dd}W d   n1 s0    Y  | | d | |j| | 
|j	 t| | ttj| dS )	test constructor autoloadxTnew    z ``autoload=False`` is deprecatedF)ZautoloadN)mktempr   	sample_01r   r7   assertEqual	to_stringr   
assertTruer   assertFalseassertWarningListr   removeassertRaisesIOErrorselfr   htr   r   r   test_00_constructor_autoload}   s(    

,
z-HtpasswdFileTest.test_00_constructor_autoloadc                 C   sP   |   }t|| j tj|}| | | j | |jd  | 	|j
 d S N)r=   r   r>   r   r7   	from_pathr?   r@   r   rB   r   rG   r   r   r   test_00_from_path   s    z"HtpasswdFileTest.test_00_from_pathc                 C   sd   t j| j}| |d | |d | |d | | | j	 | 
t|jd dS )test delete()user1user2user5user:N)r   r7   from_stringr>   rA   deleterB   r?   r@   	sample_02rE   
ValueErrorrH   rI   r   r   r   test_01_delete   s    zHtpasswdFileTest.test_01_deletec                 C   sf   |   }d}t|| t|}|d | t|| tj|dd}|d | t|d d S )Ns   user1:pass1
user2:pass2
rO   T)autosaves   user2:pass2
)r=   r   r   r7   rT   r?   r
   rH   r   samplerI   r   r   r   test_01_delete_autosave   s    



z(HtpasswdFileTest.test_01_delete_autosavec                 C   s  t jj| jdd}| |dd | |dd | | | j	 | 
d" t jj| jdd}W d	   n1 sz0    Y  | |dd | |dd | | | j	 | t|jd
d | 
d |dd W d	   n1 s0    Y  | |dd d	S )ztest set_password()	plaintextdefault_schemerP   pass2xrQ   pass5z``default`` is deprecated)defaultNrR   passupdate\(\) is deprecatedtest)r   r7   rS   r>   rA   set_passwordrB   r?   r@   	sample_03rC   rE   rV   updatecheck_passwordrW   r   r   r   test_02_set_password   s    0*z%HtpasswdFileTest.test_02_set_passwordc                 C   sl   |   }d}t|| t|}|dd | t|| tj|ddd}|dd | t|d d S )N   user1:pass1
rO   pass2r]   T)r_   rY   s   user1:pass2
)r=   r   r   r7   rf   r?   r
   rZ   r   r   r   test_02_set_password_autosave   s    

z.HtpasswdFileTest.test_02_set_password_autosavec                 C   s   dd }|  |dd |  |dd | t|d |  |dtjd  |  |dtjd  |  |dtjd  |  |d	tjd  d	S )
z%test set_password() -- default_schemec                 S   s*   t j| d}|dd |j|dS )Nr^   rO   pass1)r   r7   rf   contextidentifyget_hash)schemerI   r   r   r   check   s    zCHtpasswdFileTest.test_02_set_password_default_scheme.<locals>.checkZsha256_cryptZ	des_cryptZxxxZportableZportable_apache_22host_apache_22N)r?   rE   KeyErrorr   htpasswd_defaults)rH   rs   r   r   r   #test_02_set_password_default_scheme   s    z4HtpasswdFileTest.test_02_set_password_default_schemec                 C   sL   t j| j}|dd |d |dd | t| g d dS )test users()rQ   ra   user3pass3rO   rP   ry   user4rQ   N)	r   r7   rS   r>   rf   rT   r?   sortedusersrW   r   r   r   test_03_users   s
    
zHtpasswdFileTest.test_03_usersc              	   C   s  t j| j}| t|jdd | |dddu  tddD ]f}t	|}z8| |d| d|  | |d| ddu  W q@ t
y   |d	krY q@ Y q@0 q@| t|jd
d | dgd 4 | |dd | |dd W d   n1 s0    Y  dS )test check_password()   Zpass9Zuser9N   r/   rc   F5rR   verify\(\) is deprecated   rO   rn   rl   )r   r7   rS   	sample_05rE   	TypeErrorri   rA   r   strr   rV   rC   verifyrB   rH   rI   ir   r   r   test_04_check_password  s     
z'HtpasswdFileTest.test_04_check_passwordc                 C   s  |   }t|d t|d tj|dd}| | d |dd |  | | d t|| j	 |  | | | j	 |d	d
 |
  | | | j	 t }| t|j
 | t|j t|| j t }|
| | |dd dS )test load()r       r]   r^   r<   rO   rn   rk   rQ   ra   N)r=   r   r   r   r7   r?   r@   rf   load_if_changedr>   loadrE   RuntimeError
sample_duprA   ri   rH   r   ZhahbZhcr   r   r   test_05_load  s*    


zHtpasswdFileTest.test_05_loadc                 C   s   |   }t|| j t|}|d |d |  | t|| j	 tjdd}|
dd | t|j || | t|d dS )test save()rO   rP   r]   r^   rn   rk   N)r=   r   r>   r   r7   rT   saver?   r
   rU   rf   rE   r   rH   r   rI   r   r   r   r   test_06_saveA  s    



zHtpasswdFileTest.test_06_savec                 C   s   | j ttjdd tjj| jddd}| | tdg | 	d" tjj| jdd}W d   n1 sn0    Y  | | d	g tjj| j
d
dd}| | tdg dS )ztest 'encoding' kwdutf-16encodingr   Tr   Zreturn_unicode   useræz``encoding=None`` is deprecatedNs   userælatin-1)rE   rV   r   r7   rS   sample_04_utf8r?   r~   r   rC   sample_04_latin1rW   r   r   r   test_07_encodingsW  s    0
z"HtpasswdFileTest.test_07_encodingsc                 C   s   t j| j}| |dd | |dd | |dd | d" | |dd W d   n1 sv0    Y  dS )test get_hash()ry   s!   {SHA}3ipNV1GrBtxPmHFC21fCbVCSXIo=r|   s   pass4rQ   Nfind\(\) is deprecated)r   r7   rS   r>   r?   rq   rC   findrW   r   r   r   test_08_get_hashk  s    z!HtpasswdFileTest.test_08_get_hashc                 C   s<   t j| j}| | | j t  }| | d dS )ztest to_stringr<   N)r   r7   rS   r>   r?   r@   rW   r   r   r   test_09_to_stringu  s    z"HtpasswdFileTest.test_09_to_stringc                 C   s   t jddddd}t| d S )NZfakepathTr   )rY   r;   r   )r   r7   reprrW   r   r   r   test_10_repr  s    zHtpasswdFileTest.test_10_reprc                 C   s(   |  ttjjd |  ttjjd d S )Ns   realm:user1:pass1
s   pass1
)rE   rV   r   r7   rS   rH   r   r   r   test_11_malformed  s    z"HtpasswdFileTest.test_11_malformedc                 C   s   | j ttjjdd d d S )Nr<   )r   )rE   r   r   r7   rS   r   r   r   r   test_12_from_string  s    z$HtpasswdFileTest.test_12_from_stringc                 C   s   t d}tj|}| t| g d |dd | t| g d |dd | t| g d |d | t| g d	 t d
}| |	 | dS )zwhitespace & comment handlingz`
user2:pass2
user4:pass4
user7:pass7
 	 
user1:pass1
 # legacy users
#user6:pass6
user5:pass5

)rO   rP   r|   rQ   user7r|   Zalthash4user6Zalthash6)rO   rP   r|   rQ   r   r   r   )rO   rP   r|   rQ   r   zd
user2:pass2
user4:althash4
 	 
user1:pass1
 # legacy users
#user6:pass6
user5:pass5
user6:althash6
N)
r   r   r7   rS   r?   r}   r~   set_hashrT   r@   )rH   sourcerI   targetr   r   r   test_13_whitespace  s    
z#HtpasswdFileTest.test_13_whitespacec                    s  |   }tj|dd  fdd} d|dd tjd } d	|d| tjd
 } d|d|  dd    | t|dd | t|d	d | t|dd | t|dd | t|dd | t|d	d | t|dd dS )z;
        verify "htpasswd" command can read output
        Tr   r;   c                    s    j || S rK   ro   handlerhashpwdrr   rI   r   r   hash_scheme  s    z>HtpasswdFileTest.test_htpasswd_cmd_verify.<locals>.hash_schemerO   r0   Zapr_md5_cryptrt   rP   hostry   r|   z$xxx$foo$bar$bazwrongN)	r=   r   r7   r   rv   r   rB   r1   rA   )rH   r   r   Zhost_no_bcryptZ	host_bestr   r   r   test_htpasswd_cmd_verify  s"    

z)HtpasswdFileTest.test_htpasswd_cmd_verifybcryptzbcrypt support requiredc                    s|   |   }tj|dd  fdd} d|dd    | t|dd trf| t|dd n| t|dd d	S )
z
        verify "htpasswd" command can read bcrypt format

        this tests for regression of issue 95, where we output "$2b$" instead of "$2y$";
        fixed in v1.7.2.
        Tr   c                    s    j || S rK   r   r   r   r   r   r     s    zEHtpasswdFileTest.test_htpasswd_cmd_verify_bcrypt.<locals>.hash_schemerO   r0   r   r   N)	r=   r   r7   r   r   rB   r1   HAVE_HTPASSWD_BCRYPTrA   )rH   r   r   r   r   r   test_htpasswd_cmd_verify_bcrypt  s    	z0HtpasswdFileTest.test_htpasswd_cmd_verify_bcryptN)%__name__
__module____qualname____doc__descriptionPrefixr>   rU   rg   r   r   r   r   rJ   rM   rX   r\   rj   rm   rw   r   r   r   r   r   r   r   r   r   r   r   requires_htpasswd_cmdr   r   
skipUnlessr   Zhas_backendr   r   r   r   r   r6   Z   s@   "%
/
$r6   c                   @   s   e Zd Zd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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 )#HtdigestFileTestztest HtdigestFile classHtdigestFiles   user2:realm:549d2a5f4659ab39a80dac99e159ab19
user3:realm:a500bb8c02f6a9170ae46af10c898744
user4:realm:ab7b5d5f28ccc7666315f508c7358519
user1:realm:2a6cf53e7d8f8cf39d946dc880b14128
sZ   user3:realm:a500bb8c02f6a9170ae46af10c898744
user4:realm:ab7b5d5f28ccc7666315f508c7358519
s   user2:realm:5ba6d8328943c23c64b50f8b29566059
user3:realm:a500bb8c02f6a9170ae46af10c898744
user4:realm:ab7b5d5f28ccc7666315f508c7358519
user1:realm:2a6cf53e7d8f8cf39d946dc880b14128
user5:realm:03c55fdc6bf71552356ad401bdb9af19
s0   useræ:realmæ:549d2a5f4659ab39a80dac99e159ab19
s.   user:realm:549d2a5f4659ab39a80dac99e159ab19
c                 C   sl   |   }t|| j t|}| | | j tj|dd}| | d t| | 	t
tj| dS )r8   Tr:   r<   N)r=   r   r>   r   r   r?   r@   r   rD   rE   rF   rG   r   r   r   rJ     s    

z-HtdigestFileTest.test_00_constructor_autoloadc                 C   s   t j| j}| |dd | |dd | |dd | |dd | | | j	 | 
t|jdd | 
t|jdd	 d
S )rN   rO   realmrP   rQ   ry   realm5rR   r/   realm:N)r   r   rS   r>   rA   rT   rB   r?   r@   rU   rE   rV   rW   r   r   r   rX   +  s    zHtdigestFileTest.test_01_deletec                 C   s   |   }t|| j t|}| |dd | |dd | |dd | t	|| j d|_
| |dd | t	|| j d S )NrO   r   ry   r   rQ   TrP   )r=   r   r>   r   r   rA   rT   rB   r?   r
   rY   rU   rG   r   r   r   r\   :  s    
z(HtdigestFileTest.test_01_delete_autosavec                 C   s  t j| j}| |ddd | |ddd | | | j	 | 
t|jdd d|_|dd |ddd | 
t|jddd	 | 
t|jd
dd	 | 
t|jddd	 | 
t|jddd	 | d |ddd W d   n1 s0    Y  | |dd dS )ztest update()rP   r   r`   rQ   ra   rz   Zrealm2rR   rc   A   uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuur/   r   A   rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrd   re   N)r   r   rS   r>   rA   rf   rB   r?   r@   rg   rE   r   default_realmri   rV   rC   rh   rW   r   r   r   rj   H  s    ,z%HtdigestFileTest.test_02_set_passwordc                 C   sd   t j| j}|ddd |dd |ddd | t|dg d | 	t
|jd dS )	rx   rQ   r   ra   ry   rz   r{   r   N)r   r   rS   r>   rf   rT   r?   r}   r~   rE   r   rW   r   r   r   r   d  s    zHtdigestFileTest.test_03_usersc              	   C   sJ  t j| j}| t|jddd | t|jddd | |dddd tddD ]B}t	|}| 
|d| dd|  | |d| ddd	 qV| t|jdd d|_| 
|d
d | |ddd | dgd 8 | 
|d
dd | |d
dd W d   n1 s(0    Y  | t|jddd dS )r   r   r   ra   r/   rQ   Nr   rc   FrO   rn   r   r   rl   rR   )r   r   rS   r>   rE   r   ri   ZassertIsr   r   rA   r   rC   r   rB   rV   r   r   r   r   r   n  s     4z'HtdigestFileTest.test_04_check_passwordc                 C   sP  |   }t|d t|d t|}| | d |ddd |  | | d t|| j	 |  | | | j	 |ddd	 |
  | | | j	 t }| t|j
 | t|j t }|
| | | | j	 t| t|d | d
 |j
dd W d   n1 s20    Y  | | d dS )r   r    r   r<   rO   r   rn   s-   user1:realm:2a6cf53e7d8f8cf39d946dc880b14128
rQ   ra   z!load\(force=False\) is deprecatedF)forceN)r=   r   r   r   r   r?   r@   rf   r   r>   r   rE   r   r   rC   r   r   r   r   r     s2    




,zHtdigestFileTest.test_05_loadc                 C   s   |   }t|| j t|}|dd |dd |  | t|| j	 t }|
ddd | t|j || | t||  dS )r   rO   r   rP   rn   N)r=   r   r>   r   r   rT   r   r?   r
   rU   rf   rE   r   r@   r   r   r   r   r     s    

zHtdigestFileTest.test_06_savec                 C   sh   t j| j}| |dd | | dg | |dd | | g  | | d dS )ztest realms() & delete_realm()r9   r   r      r<   N)r   r   rS   r>   r?   Zdelete_realmrealmsr@   rW   r   r   r   test_07_realms  s    zHtdigestFileTest.test_07_realmsc                 C   s   t j| j}| |ddd | |ddd | |ddd | d$ | |ddd W d   n1 s~0    Y  dS )	r   ry   r   Z a500bb8c02f6a9170ae46af10c898744r|   Z ab7b5d5f28ccc7666315f508c7358519rQ   Nr   )r   r   rS   r>   r?   rq   rC   r   rW   r   r   r   r     s    z!HtdigestFileTest.test_08_get_hashc                 C   s   | j ttjdd tjj| jddd}| | tdg | |	tdtdg tjj| j
ddd}| | tdg | |	tdtdg d	S )
ztest encoding parameterr   r   r   Tr   u   realmær   r   N)rE   rV   r   r   rS   r   r?   r   r   r~   r   rW   r   r   r   test_09_encodings  s    z"HtdigestFileTest.test_09_encodingsc                 C   s<   t j| j}| | | j t  }| | d dS )ztest to_string()r<   N)r   r   rS   r>   r?   r@   rW   r   r   r   test_10_to_string  s    z"HtdigestFileTest.test_10_to_stringc                 C   s(   |  ttjjd |  ttjjd d S )Ns   realm:user1:pass1:other
rk   )rE   rV   r   r   rS   r   r   r   r   r     s    z"HtdigestFileTest.test_11_malformedN)r   r   r   r   r   r>   rU   rg   r   r   rJ   rX   r\   rj   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s(   
)
r   )r   )N))r   
__future__r   loggingr   r   r"   Zpasslibr   r   Zpasslib.excr   Zpasslib.utils.compatr   Zpasslib.tests.backportsr   Zpasslib.tests.utilsr	   r
   r   r   r   Zpasslib.utilsr   Zpasslib.utils.handlersr   r   r3   r   environgetr$   r.   r1   r5   ZHAVE_HTPASSWDr   r   r   r6   r   r   r   r   r   <module>   s2   


   )