U
    ÓZjp4  ã                   @   s  d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z
ddlZddlZddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ dd	lmZ e
j e d¡¡ZG dd„ dejƒZ G dd„ dejƒZ!e"dkrþe #¡  dS )z"Tests for certbot.plugins.webroot.é    )Úprint_functionN)Ú
challenges)Úachallenges)Úerrors)Úos)Ú
filesystem)Úutil)Ú	acme_utilzrsa512_key.pemc                   @   sö   e Zd ZdZejejded�Z	dd„ Z
dd„ Zdd	„ Zd
d„ Zdd„ Ze ¡ dd„ ƒZe ¡ dd„ ƒZe ¡ dd„ ƒZe ¡ dd„ ƒZdd„ Zdd„ Ze d¡dd„ ƒZe ¡ dd„ ƒZdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Ze d)¡d*d+„ ƒZ d,S )-ÚAuthenticatorTestz0Tests for certbot.plugins.webroot.Authenticator.ú	thing.com©ZchallbÚdomainZaccount_keyc                 C   s–   ddl m} t ¡ | _tj | jd¡| _t 	| j¡ tj | jd¡| _
tj | jdd¡| _tj | jd¡| _tj| jd| jid�| _|| jdƒ| _d S )	Nr   ©ÚAuthenticatorÚwebrootz.well-knownzacme-challengeZ:ZXZhR3hmQURzNnBTUmIyTEF2OUlaZjE3RHQzanV4R0orUEN0OTJ3citvQQr   )Úwebroot_pathÚwebroot_map)Úcertbot.plugins.webrootr   ÚtempfileÚmkdtempZ	workspacer   ÚpathÚjoinr   ÚmkdirÚpartial_root_challenge_pathÚroot_challenge_pathÚvalidation_pathÚmockÚ	MagicMockÚconfigÚauth©Úselfr   © r"   ú_/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/certbot/plugins/webroot_test.pyÚsetUp#   s*    
 ÿ  ÿþÿzAuthenticatorTest.setUpc                 C   s   t  | j¡ d S ©N)ÚshutilÚrmtreer   ©r!   r"   r"   r#   ÚtearDown7   s    zAuthenticatorTest.tearDownc                 C   s0   | j  ¡ }|  t|tjƒ¡ |  | j|k¡ d S r%   )r   Ú	more_infoÚ
assertTrueÚ
isinstanceÚsixÚstring_typesr   )r!   r*   r"   r"   r#   Útest_more_info:   s    
z AuthenticatorTest.test_more_infoc                 C   s&   t  ¡ }| j |¡ |  d|j¡ d S )Né   )r   r   r   Zadd_parser_argumentsÚassertEqualZ
call_count)r!   Úaddr"   r"   r#   Útest_add_parser_arguments?   s    z+AuthenticatorTest.test_add_parser_argumentsc                 C   s   | j  ¡  d S r%   )r   Úpreparer(   r"   r"   r#   Útest_prepareD   s    zAuthenticatorTest.test_preparec                    s´   g | j _d| ji| j _|ƒ }tjdf|j_| j 	| j
g¡ |  |jj¡ |jjD ]D‰ |  | j
jˆ d d k¡ |  t‡ fdd„t | j j¡D ƒƒ¡ qP|  | j j| j
j | j¡ d S )Núotherthing.comé   r   c                 3   s   | ]}|ˆ d  d kV  qdS ©r   r7   Nr"   ©Ú.0r   ©Úcallr"   r#   Ú	<genexpr>R   s   ÿz;AuthenticatorTest.test_webroot_from_list.<locals>.<genexpr>)r   r   r   r   Údisplay_utilÚOKÚmenuÚreturn_valuer   ÚperformÚachallr+   ÚcalledÚcall_args_listr   Úallr-   Ú
itervaluesr1   ©r!   Úmock_get_utilityÚmock_displayr"   r;   r#   Útest_webroot_from_listG   s    þÿz(AuthenticatorTest.test_webroot_from_listc                    s¤   g | j _d| ji| j _|ƒ }tjdff|j_|  t	j
| jj| jg¡ |  |jj¡ |jjD ]D‰ |  | jjˆ d d k¡ |  t‡ fdd„t | j j¡D ƒƒ¡ qZd S )Nr6   éÿÿÿÿr   c                 3   s   | ]}|ˆ d  d kV  qdS r8   r"   r9   r;   r"   r#   r=   c   s   ÿzKAuthenticatorTest.test_webroot_from_list_help_and_cancel.<locals>.<genexpr>)r   r   r   r   r>   ÚCANCELr@   Úside_effectÚassertRaisesr   ÚPluginErrorr   rB   rC   r+   rD   rE   r   rF   r-   rG   rH   r"   r;   r#   Ú&test_webroot_from_list_help_and_cancelX   s    þz8AuthenticatorTest.test_webroot_from_list_help_and_cancelc              	   C   s†   g | j _d| ji| j _|ƒ }tjdf|j_t 	d¡�.}tj
dftj| jff|_| j | jg¡ W 5 Q R X |  | j j| jj | j¡ d S )Núsomething.comr   ú'certbot.display.ops.validated_directoryrL   )r   r   r   r   r>   r?   r@   rA   r   ÚpatchrM   rN   r   rB   rC   r1   r   ©r!   rI   rJ   Úmr"   r"   r#   Útest_new_webrootg   s    
ÿz"AuthenticatorTest.test_new_webrootc              	   C   sb   g | j _i | j _|ƒ }tjdf|j_t d¡�*}tj	df|_|  
tj| jj| jg¡ W 5 Q R X d S )Nr   rS   rL   )r   r   r   r>   r?   r@   rA   r   rT   rM   rO   r   rP   r   rB   rC   rU   r"   r"   r#   Ú!test_new_webroot_empty_map_cancelv   s    þz3AuthenticatorTest.test_new_webroot_empty_map_cancelc                 C   s(   d | j _i | j _|  tj| jjg ¡ d S r%   )r   r   r   rO   r   rP   r   rB   r(   r"   r"   r#   Útest_perform_missing_rootƒ   s    z+AuthenticatorTest.test_perform_missing_rootc              	   C   s¤   t j | jd¡| j_t j | jd¡}t|dƒ�}| d¡ W 5 Q R X t | jd¡ zt|dƒ t	dƒ W n( t
k
r�   |  tj| jjg ¡ Y nX t | jd¡ d S )	NÚnullZrndÚwZthingimyr   Úrz9Warning, running tests as root skips permissions tests...iÀ  )r   r   r   r   Ú	full_pathÚopenÚwriter   ÚchmodÚprintÚIOErrorrO   r   rP   rB   )r!   Zpermission_canaryÚfr"   r"   r#   Ú"test_perform_reraises_other_errorsˆ   s    
z4AuthenticatorTest.test_perform_reraises_other_errorsz@certbot.plugins.webroot.filesystem.copy_ownership_and_apply_modec                 C   s"   t tjdƒ|_| j | jg¡ d S )NÚmsg)ÚOSErrorÚerrnoÚEACCESrN   r   rB   rC   )r!   Zmock_ownershipr"   r"   r#   Útest_failed_chown–   s    z#AuthenticatorTest.test_failed_chownc              	   C   s˜   t  ¡ }g | j_d| ji| j_|ƒ }tjdftj|ff|j_	t
jtjdtd�}t d¡� }tj|f|_| j |g¡ W 5 Q R X |  | jj|j |¡ d S )Nzwhatever.comr   rR   r   rS   )r   r   r   r   r   r   r>   r?   r@   rN   r   Ú"KeyAuthorizationAnnotatedChallenger	   ÚHTTP01_PÚKEYr   rT   rA   r   rB   r1   r   )r!   rI   Znew_webrootrJ   rC   rV   r"   r"   r#   Ú#test_perform_new_webroot_not_in_map›   s     ÿ  ÿz5AuthenticatorTest.test_perform_new_webroot_not_in_mapc                 C   sŠ   | j  ¡  | j  | jg¡ |  t | jd¡¡ t 	| j
¡D ]4\}}}|D ]$}tj
 ||¡}|  t |d¡¡ qHq:|  t | j| j
¡¡ d S )Ni¤  ií  )r   r4   rB   rC   r+   r   Z
check_moder   r   Úwalkr   r   Zhas_same_ownership)r!   ÚdirpathÚdirnamesÚ_Ú	directoryr]   r"   r"   r#   Útest_perform_permissionsª   s    
z*AuthenticatorTest.test_perform_permissionsc              	   C   sÎ   | j  ¡  | j  | jg¡}|  dt|ƒ¡ |  tj 	| j
¡¡ t| j
ƒ�}| ¡ }W 5 Q R X |  tj|d� | jjt ¡ ¡¡ | j  | jg¡ |  tj 	| j
¡¡ |  tj 	| j¡¡ |  tj 	| j¡¡ d S )Nr7   )Zkey_authorization)r   r4   rB   rC   r1   Úlenr+   r   r   Úexistsr   r^   Úreadr   Z!KeyAuthorizationChallengeResponseÚverifyZchallrl   Z
public_keyÚcleanupÚassertFalser   r   )r!   Ú	responsesZvalidation_fZ
validationr"   r"   r#   Útest_perform_cleanupº   s$    
ÿ þÿz&AuthenticatorTest.test_perform_cleanupc                 C   sb   t  | j¡ | j ¡  | j | jg¡ | j | jg¡ |  t	j
 | j¡¡ |  t	j
 | j¡¡ d S r%   )r   r   r   r   r4   rB   rC   rx   ry   r   r   ru   r   r   r(   r"   r"   r#   Ú"test_perform_cleanup_existing_dirsË   s    
z4AuthenticatorTest.test_perform_cleanup_existing_dirsc                 C   s¼   t jt tjdd�d¡dtd�}d}t | j	¡ | j
 ¡  | j
 || jg¡ | j
 | jg¡ |  tj |¡¡ |  tj | j¡¡ | j
 |g¡ |  tj | j¡¡ |  tj | j¡¡ d S )Ns   bingo)ÚtokenÚpendingr   r   ZYmluZ28)r   rj   r	   Zchall_to_challbr   ZHTTP01rl   r   r   r   r   r4   rB   rC   rx   ry   r   r   ru   r+   r   r   )r!   Zbingo_achallZbingo_validation_pathr"   r"   r#   Ú(test_perform_cleanup_multiple_challengesÕ   s$    
 ÿ ý
z:AuthenticatorTest.test_perform_cleanup_multiple_challengesc                 C   sz   | j  ¡  | j  | jg¡ tj | jd¡}t 	|¡ | j  
| jg¡ |  tj | j¡¡ |  tj | j¡¡ t |¡ d S )NÚleftover)r   r4   rB   rC   r   r   r   r   r   r   rx   ry   ru   r   r+   Úrmdir)r!   Zleftover_pathr"   r"   r#   Útest_cleanup_leftoversç   s    

z(AuthenticatorTest.test_cleanup_leftoverszcertbot.compat.os.rmdirc                 C   sj   | j  ¡  | j  | jg¡ tƒ }tj|_||_| j  | jg¡ |  	t
j | j¡¡ |  t
j | j¡¡ d S r%   )r   r4   rB   rC   rf   rg   rh   rN   rx   ry   r   r   ru   r   r+   r   )r!   Z
mock_rmdirZos_errorr"   r"   r#   Útest_cleanup_failureô   s    
z&AuthenticatorTest.test_cleanup_failureN)!Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rj   r	   rk   rl   rC   r$   r)   r/   r3   r5   Ú	test_utilZpatch_get_utilityrK   rQ   rW   rX   rY   rd   r   rT   ri   rm   rs   r{   r|   r   r‚   rƒ   r"   r"   r"   r#   r
      s@     ÿ






r
   c                   @   sZ   e Zd ZdZejejde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 )ÚWebrootActionTestz#Tests for webroot argparse actions.r   r   c                 C   sF   ddl m} t ¡ | _t ¡ | _| jjdddg d� | 	| jd¡ d S )Nr   r   z-dz	--domainsÚappend)ÚactionÚdefaultr   )
r   r   r   r   r   ÚargparseÚArgumentParserÚparserÚadd_argumentZinject_parser_optionsr    r"   r"   r#   r$     s    


 ÿzWebrootActionTest.setUpc                 C   s4   | j  dt d| ji¡g¡}|  |jd | j¡ d S )Nz--webroot-mapr   )r�   Ú
parse_argsÚjsonÚdumpsr   r1   r   )r!   Úargsr"   r"   r#   Útest_webroot_map_action  s    ÿz)WebrootActionTest.test_webroot_map_actionc                 C   sD   | j  d | jj| j¡ ¡ ¡}|  |¡}|  |j	| jj | j¡ d S )Nz-d {0} -w {1})
r�   r‘   ÚformatrC   r   r   ÚsplitÚ_get_config_after_performr1   r   ©r!   r”   r   r"   r"   r#   Útest_domain_before_webroot  s
    ÿ
z,WebrootActionTest.test_domain_before_webrootc                 C   s4   |   tj| jjd ¡ ¡ |   tj| jjd ¡ ¡ d S )Nz-d foo -w bar -w bazz-d foo -w bar -d baz -w qux)rO   r   rP   r�   r‘   r—   r(   r"   r"   r#   Ú test_domain_before_webroot_error  s    ÿÿz2WebrootActionTest.test_domain_before_webroot_errorc                 C   sb   | j  d | j| jjt ¡ ¡ ¡ ¡}|  	|j
| jj | j¡ |  |¡}|  	|j
| jj | j¡ d S ©Nz-w {0} -d {1} -w {2} -d bar)r�   r‘   r–   r   rC   r   r   r   r—   r1   r   r˜   r™   r"   r"   r#   Útest_multiwebroot!  s    
  ÿ

 ÿz#WebrootActionTest.test_multiwebrootc                 C   sX   t  ¡ }| j d | j| jj|¡ ¡ ¡}|  	|j
| jj| ji¡ |  	|j| j|g¡ d S rœ   )r   r   r�   r‘   r–   r   rC   r   r—   r1   r   r   )r!   Zother_webroot_pathr”   r"   r"   r#   Ú(test_webroot_map_partial_without_perform)  s    
  ÿ
z:WebrootActionTest.test_webroot_map_partial_without_performc                 C   s*   ddl m} ||dƒ}| | jg¡ |jS )Nr   r   r   )r   r   rB   rC   r   )r!   r   r   r   r"   r"   r#   r˜   6  s    
z+WebrootActionTest._get_config_after_performN)r„   r…   r†   r‡   r   rj   r	   rk   rl   rC   r$   r•   rš   r›   r�   rž   r˜   r"   r"   r"   r#   r‰     s     ÿr‰   Ú__main__)$r‡   Ú
__future__r   r�   rg   r’   r&   r   ZunittestZjosepyZjoser   r-   Zacmer   Zcertbotr   r   Zcertbot.compatr   r   Zcertbot.displayr   r>   Zcertbot.testsr	   rˆ   ZJWKRSAÚloadZload_vectorrl   ZTestCaser
   r‰   r„   Úmainr"   r"   r"   r#   Ú<module>   s0    f;