o
    Ƌir                     @   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p^d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   ]/home/air/goalskill_sales/back/venv/lib/python3.10/site-packages/passlib/tests/test_apache.pybackdate_file_mtime   s   r   PASSLIB_TEST_HTPASSWD_PATHhtpasswdc                 C   sb   |dur	| d}tjtg|  tjtj|rtjndd}||\}}| }t|p+d}||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   
r0   c                 C   s   t d| |g|\}}| S )z%
    wrapper for htpasswd verify
    z-vi)r0   )r   userpasswordr-   r/   r   r   r   _call_htpasswd_verify9   s   r3   c                  C   sD   zt g \} }W n
 ty   Y dS w |std d| v }d|fS )z0
    helper to check if htpasswd is present
    )FFz htpasswd test returned with rc=0z -B T)r0   OSErrorlogwarning)r-   r/   have_bcryptr   r   r   _detect_htpasswdA   s   
r8   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 slw   Y  | | d | |j| | 
|j	 t| | ttj| dS )	test constructor autoloadxTnew    z ``autoload=False`` is deprecatedF)autoloadN)mktempr   	sample_01r   r:   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)rA   r   rB   r   r:   	from_pathrC   rD   r   rF   r   rK   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   r:   from_stringrB   rE   deleterF   rC   rD   	sample_02rI   
ValueErrorrL   rM   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
rS   T)autosaves   user2:pass2
)rA   r   r   r:   rX   rC   r
   rL   r   samplerM   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 s=w   Y  | |dd | |dd | | | j	 | t|jd
d | 
d |dd W d	   n1 s|w   Y  | |dd d	S )ztest set_password()	plaintextdefault_schemerT   pass2xrU   pass5z``default`` is deprecated)defaultNrV   passupdate\(\) is deprecatedtest)r   r:   rW   rB   rE   set_passwordrF   rC   rD   	sample_03rG   rI   rZ   updatecheck_passwordr[   r   r   r   test_02_set_password   s"   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
rS   pass2ra   T)rc   r]   s   user1:pass2
)rA   r   r   r:   rj   rC   r
   r^   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 )Nrb   rS   pass1)r   r:   rj   contextidentifyget_hash)schemerM   r   r   r   check   s   zCHtpasswdFileTest.test_02_set_password_default_scheme.<locals>.checksha256_crypt	des_cryptxxxportableportable_apache_22host_apache_22N)rC   rI   KeyErrorr   htpasswd_defaults)rL   rw   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()rU   re   user3pass3rS   rT   r   user4rU   N)	r   r:   rW   rB   rj   rX   rC   sortedusersr[   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 ]0}t	|}z| |d| d|  | |d| ddu  W q  t
yP   |d	krOY q  w | t|jd
d | dgd  | |dd | |dd W d   dS 1 sw   Y  dS )test check_password()   pass9user9N   r1   rg   F5rV   verify\(\) is deprecated   rS   rr   rp   )r   r:   rW   	sample_05rI   	TypeErrorrm   rE   r   strr   rZ   rG   verifyrF   rL   rM   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"      ra   rb   r?   rS   rr   ro   rU   re   N)rA   r   r   r   r:   rC   rD   rj   load_if_changedrB   loadrI   RuntimeError
sample_duprE   rm   rL   r   hahb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()rS   rT   ra   rb   rr   ro   N)rA   r   rB   r   r:   rX   saverC   r
   rY   rj   rI   r   rL   r   rM   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 s7w   Y  | | d	g tjj| j
d
dd}| | tdg dS )ztest 'encoding' kwdutf-16encodingr   Tr   return_unicode   useræz``encoding=None`` is deprecatedNs   userælatin-1)rI   rZ   r   r:   rW   sample_04_utf8rC   r   r   rG   sample_04_latin1r[   r   r   r   test_07_encodingsW  s   
z"HtpasswdFileTest.test_07_encodingsc                 C   s   t j| j}| |dd | |dd | |dd | d | |dd W d   dS 1 s<w   Y  dS )test get_hash()r   s!   {SHA}3ipNV1GrBtxPmHFC21fCbVCSXIo=r   s   pass4rU   Nfind\(\) is deprecated)r   r:   rW   rB   rC   ru   rG   findr[   r   r   r   test_08_get_hashk  s   "z!HtpasswdFileTest.test_08_get_hashc                 C   <   t j| j}| | | j t  }| | d dS )ztest to_stringr?   N)r   r:   rW   rB   rC   rD   r[   r   r   r   test_09_to_stringu     z"HtpasswdFileTest.test_09_to_stringc                 C   s   t jddddd}t| d S )NfakepathTr   )r]   r>   r   )r   r:   reprr[   r   r   r   test_10_repr  s   zHtpasswdFileTest.test_10_reprc                 C   (   |  ttjjd |  ttjjd d S )Ns   realm:user1:pass1
s   pass1
)rI   rZ   r   r:   rW   rL   r   r   r   test_11_malformed     z"HtpasswdFileTest.test_11_malformedc                 C   s   | j ttjjdd d d S )Nr?   )r   )rI   r   r   r:   rW   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

)rS   rT   r   rU   user7r   althash4user6althash6)rS   rT   r   rU   r   r   r   )rS   rT   r   rU   r   zd
user2:pass2
user4:althash4
 	 
user1:pass1
 # legacy users
#user6:pass6
user5:pass5
user6:althash6
N)
r   r   r:   rW   rC   r   r   set_hashrX   rD   )rL   sourcerM   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                        j || S rO   rs   handlerhashpwdrv   rM   r   r   hash_scheme     z>HtpasswdFileTest.test_htpasswd_cmd_verify.<locals>.hash_schemerS   r2   apr_md5_cryptr}   rT   hostr   r   z$xxx$foo$bar$bazwrongN)	rA   r   r:   r   r   r   rF   r3   rE   )rL   r   r   host_no_bcrypt	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r4| t|dd d	S | 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                    r   rO   r   r   r   r   r   r     r   zEHtpasswdFileTest.test_htpasswd_cmd_verify_bcrypt.<locals>.hash_schemerS   r2   r   r   N)	rA   r   r:   r   r   rF   r3   HAVE_HTPASSWD_BCRYPTrE   )rL   r   r   r   r   r   test_htpasswd_cmd_verify_bcrypt  s   	z0HtpasswdFileTest.test_htpasswd_cmd_verify_bcryptN)%__name__
__module____qualname____doc__descriptionPrefixrB   rY   rk   r   r   r   r   rN   rQ   r\   r`   rn   rq   r   r   r   r   r   r   r   r   r   r   r   r   requires_htpasswd_cmdr   r   
skipUnlessr   has_backendr   r   r   r   r   r9   Z   sB    "%
/
$r9   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 )r;   Tr=   r?   N)rA   r   rB   r   r   rC   rD   r   rH   rI   rJ   rK   r   r   r   rN     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 )rR   rS   realmrT   rU   r   realm5rV   r1   realm:N)r   r   rW   rB   rE   rX   rF   rC   rD   rY   rI   rZ   r[   r   r   r   r\   +  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 )NrS   r   r   r   rU   TrT   )rA   r   rB   r   r   rE   rX   rF   rC   r
   r]   rY   rK   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 s|w   Y  | |dd dS )ztest update()rT   r   rd   rU   re   r   realm2rV   rg      uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuur1   r      rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrh   ri   N)r   r   rW   rB   rE   rj   rF   rC   rD   rk   rI   r   default_realmrm   rZ   rG   rl   r[   r   r   r   rn   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 )	r   rU   r   re   r   r   r   r   N)r   r   rW   rB   rj   rX   rC   r   r   rI   r   r[   r   r   r   r   d  s   zHtdigestFileTest.test_03_usersc              	   C   sH  t j| j}| t|jddd | t|jddd | |dddd tddD ]!}t	|}| 
|d| dd|  | |d| ddd	 q+| t|jdd d|_| 
|d
d | |ddd | dgd  | 
|d
dd | |d
dd W d   n1 sw   Y  | t|jddd dS )r   r   r   re   r1   rU   Nr   rg   FrS   rr   r   r   rp   rV   )r   r   rW   rB   rI   r   rm   assertIsr   r   rE   r   rG   r   rF   rZ   r   r   r   r   r   n  s"   z'HtdigestFileTest.test_04_check_passwordc                 C   sN  |   }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 sw   Y  | | d dS )r   r"   r   r?   rS   r   rr   s-   user1:realm:2a6cf53e7d8f8cf39d946dc880b14128
rU   re   z!load\(force=False\) is deprecatedF)forceN)rA   r   r   r   r   rC   rD   rj   r   rB   r   rI   r   r   rG   r   r   r   r   r     s4   




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   rS   r   rT   rr   N)rA   r   rB   r   r   rX   r   rC   r
   rY   rj   rI   r   rD   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()r<   r   r      r?   N)r   r   rW   rB   rC   delete_realmrealmsrD   r[   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   dS 1 s@w   Y  dS )	r   r   r    a500bb8c02f6a9170ae46af10c898744r    ab7b5d5f28ccc7666315f508c7358519rU   Nr   )r   r   rW   rB   rC   ru   rG   r   r[   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)rI   rZ   r   r   rW   r   rC   r   r   r   r   r[   r   r   r   test_09_encodings  s    z"HtdigestFileTest.test_09_encodingsc                 C   r   )ztest to_string()r?   N)r   r   rW   rB   rC   rD   r[   r   r   r   test_10_to_string  r   z"HtdigestFileTest.test_10_to_stringc                 C   r   )Ns   realm:user1:pass1:other
ro   )rI   rZ   r   r   rW   r   r   r   r   r     r   z"HtdigestFileTest.test_11_malformedN)r   r   r   r   r   rB   rY   rk   r   r   rN   r\   r`   rn   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s*    
)
r   )r   rO   ))r   
__future__r   loggingr   r   r$   passlibr   r   passlib.excr   passlib.utils.compatr   passlib.tests.backportsr   passlib.tests.utilsr	   r
   r   r   r   passlib.utilsr   passlib.utils.handlersr   r   r5   r   environgetr&   r0   r3   r8   HAVE_HTPASSWDr   r   r   r9   r   r   r   r   r   <module>   s4    


   )