U
    ÝÁ]p4  ã                   @   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"   ú>/usr/lib/python3/dist-packages/certbot/plugins/webroot_test.pyÚsetUp#   s*    
 ÿ  ÿþÿzAuthenticatorTest.setUpc                 C   s   t  | j¡ d S ©N)ÚshutilZ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ÚsixZ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   r5   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,   Ú
itervaluesr/   ©r!   Úmock_get_utilityÚmock_displayr"   r9   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 )Nr4   éÿÿÿÿr   c                 3   s   | ]}|ˆ d  d kV  qdS r6   r"   r7   r9   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   r@   rA   r*   rB   rC   r   rD   r,   rE   rF   r"   r9   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_directoryrJ   )r   r   r   r   r<   r=   r>   r?   r   ÚpatchrK   rL   r   r@   rA   r/   r   ©r!   rG   rH   Ú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   rQ   rJ   )r   r   r   r<   r=   r>   r?   r   rR   rK   rM   r   rN   r   r@   rA   rS   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   rM   r   rN   r   r@   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 )	NZnullZrndÚwZthingimyr   Úrz9Warning, running tests as root skips permissions tests...iÀ  )r   r   r   r   Ú	full_pathÚopenÚwriter   ÚchmodÚprintÚIOErrorrM   r   rN   r@   )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ÚEACCESrL   r   r@   rA   )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   rP   r   rQ   )r   r   r   r   r   r   r<   r=   r>   rL   r   Ú"KeyAuthorizationAnnotatedChallenger	   ÚHTTP01_PÚKEYr   rR   r?   r   r@   r/   r   )r!   rG   Znew_webrootrH   rA   rT   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   r2   r@   rA   r*   r   Z
check_moder   r   Úwalkr   r   Zhas_same_ownership)r!   ÚdirpathZdirnamesÚ_Z	directoryrZ   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 )Nr5   )Zkey_authorization)r   r2   r@   rA   r/   Úlenr*   r   r   Úexistsr   r[   Úreadr   Z!KeyAuthorizationChallengeResponseZverifyZchallri   Z
public_keyÚcleanupÚassertFalser   r   )r!   Z	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   r2   r@   rA   rr   rs   r   r   rp   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   rg   r	   Zchall_to_challbr   ZHTTP01ri   r   r   r   r   r2   r@   rA   rr   rs   r   r   rp   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 )NZleftover)r   r2   r@   rA   r   r   r   r   r   r   rr   rs   rp   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   r2   r@   rA   rc   rd   re   rL   rr   rs   r   r   rp   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   rg   r	   rh   ri   rA   r$   r(   r-   r1   r3   Ú	test_utilZpatch_get_utilityrI   rO   rU   rV   rW   ra   r   rR   rf   rj   rn   rt   ru   rx   rz   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   r/   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‰   ÚformatrA   r   r   ÚsplitÚ_get_config_after_performr/   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)rM   r   rN   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   rA   r   r   r   r   r/   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   rA   r   r   r/   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   r@   rA   r   )r!   r   r   r   r"   r"   r#   r   6  s    
z+WebrootActionTest._get_config_after_performN)r|   r}   r~   r   r   rg   r	   rh   ri   rA   r$   r   r’   r“   r•   r–   r   r"   r"   r"   r#   r     s     ÿr   Ú__main__)$r   Z
__future__r   r…   rd   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_vectorri   ZTestCaser
   r   r|   Úmainr"   r"   r"   r#   Ú<module>   s0    f;