U
    ÓZj%W  ã                   @   s8  d 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 dd
lmZ dd	lmZ G dd„ dejƒZG dd„ dejƒZejddfdd„ZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZdd„ Zddd„Zedk�r4e  ¡  dS )zTests for certbot.auth_handler.é    N)Ú
challenges)Úclient)Úmessages)Úerrors)Úachallenges)Ú
interfaces)Úutil)Ú	acme_utilc                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚChallengeFactoryTestc                 C   sH   ddl m} |d d tjdd�g ƒ| _t tjdtj	tjgd d¡| _
d S )Nr   ©ÚAuthHandlerZmock_key©ÚkeyÚtesté   F)Úcertbot.auth_handlerr   ÚmockÚMockÚhandlerr	   Ú
gen_authzrr   ÚSTATUS_PENDINGÚ
CHALLENGESÚauthzr©Úselfr   © r   úb/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/certbot/tests/auth_handler_test.pyÚsetUp   s      
 þzChallengeFactoryTest.setUpc                 C   s8   | j  | jtdttjƒƒ¡}|  dd„ |D ƒtj¡ d S )Nr   c                 S   s   g | ]
}|j ‘qS r   ©Úchall©Ú.0Úachallr   r   r   Ú
<listcomp>)   s     z1ChallengeFactoryTest.test_all.<locals>.<listcomp>)r   Ú_challenge_factoryr   ÚrangeÚlenr	   r   ÚassertEqual©r   Zachallsr   r   r   Útest_all$   s     ÿ ÿzChallengeFactoryTest.test_allc                 C   s0   | j  | jdg¡}|  dd„ |D ƒtjg¡ d S )Nr   c                 S   s   g | ]
}|j ‘qS r   r   r    r   r   r   r#   /   s     z6ChallengeFactoryTest.test_one_http.<locals>.<listcomp>)r   r$   r   r'   r	   ÚHTTP01r(   r   r   r   Útest_one_http+   s
     ÿz"ChallengeFactoryTest.test_one_httpc                 C   s>   t  tjdtjddd�gtjg¡}|  tj| j	j
|dg¡ d S )Nr   r   Zunrecognized)r   Útypr   )r	   r   r   r   r   r   ÚassertRaisesr   ÚErrorr   r$   )r   r   r   r   r   Útest_unrecognized1   s     ý   ÿz&ChallengeFactoryTest.test_unrecognizedN)Ú__name__Ú
__module__Ú__qualname__r   r)   r+   r/   r   r   r   r   r
      s   
r
   c                   @   sè   e 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d&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8S )9ÚHandleAuthorizationsTestzmhandle_authorizations test.

    This tests everything except for all functions under _poll_challenges.

    c                 C   sÈ   ddl m} t ¡ | _tj | jtj	¡ tj tjdd�tj
¡ tjdd�| _tjg| jj_t| jj_tjt dd¡d	�| _tjtjd
�| _d| j_tjj| jj_|| j| j| jg ƒ| _t tj¡ d S )Nr   r   F©Zdebug_challengesZApacheConfigurator)ÚnameÚ	file_pathZPEMr   )Úspecé   ) r   r   r   r   Úmock_displayÚzopeÚ	componentÚprovideUtilityr   ZIDisplayÚIConfigÚ	MagicMockÚ	mock_authr   r*   Úget_chall_prefÚreturn_valueÚgen_auth_respÚperformÚside_effectr   ÚKeyZmock_accountÚacme_clientZClientÚmock_netÚacme_versionÚretry_afterr   ÚloggingÚdisableÚCRITICALr   r   r   r   r   B   s0    
 ÿ
 ÿ
   ÿzHandleAuthorizationsTest.setUpc                 C   s   t  t j¡ d S ©N©rJ   rK   ÚNOTSET©r   r   r   r   ÚtearDown[   s    z!HandleAuthorizationsTest.tearDownc              	   C   s  t dtj|d�}tj|gd�}tddd�| jj_t 	d¡�Ä}| j
 |¡}|  | jjjd¡ |  | jjjd¡ |  |jjd¡ |  |jjd d	 d	 dk¡ |  |jjd d	 d	 d
k¡ |  | jjjd¡ |  | jjjd	 d	 d	 jd¡ |  t|ƒd¡ W 5 Q R X d S )NÚ0©ÚdomainÚchallsÚcombos©Zauthorizationsr8   é   )ÚretryÚ
wait_valuezcertbot.auth_handler.timeé   r   é   úhttp-01)Úgen_dom_authzrr	   r   r   r>   Ú_gen_mock_on_pollrG   ÚpollrD   Úpatchr   Úhandle_authorizationsr'   Úanswer_challengeÚ
call_countÚsleepÚ
assertTrueÚcall_args_listr?   ÚcleanupÚ	call_argsr,   r&   )r   rV   r   Ú
mock_orderZ	mock_timer   r   r   Ú_test_name1_http_01_1_common^   s      ÿz5HandleAuthorizationsTest._test_name1_http_01_1_commonc                 C   s   | j dd� d S ©NT©rV   )rk   rP   r   r   r   Útest_name1_http_01_1_acme_1w   s    z4HandleAuthorizationsTest.test_name1_http_01_1_acme_1c                 C   s   d| j _| jdd� d S ©Nr[   Frm   )rG   rH   rk   rP   r   r   r   Útest_name1_http_01_1_acme_2z   s    z4HandleAuthorizationsTest.test_name1_http_01_1_acme_2c                 C   sº   t ƒ | jj_| jjj tj	¡ t
dtjdd�}tj|gd�}| j |¡}|  | jjjd¡ |  | jjjd¡ |  | jjjd¡ | jjjd d D ]}|  |jdk¡ q�|  t|ƒd¡ d S )	NrR   FrS   rW   r[   r8   r   )r]   zdns-01)r_   rG   r`   rD   r?   r@   rA   Úappendr   ÚDNS01r^   r	   r   r   r>   r   rb   r'   rc   rd   rh   ri   rf   r,   r&   )r   r   rj   r"   r   r   r   Ú!test_name1_http_01_1_dns_1_acme_1~   s    z:HandleAuthorizationsTest.test_name1_http_01_1_dns_1_acme_1c                 C   sÎ   d| j _tƒ | j j_| jjj t	j
¡ tdtjdd�}tj|gd�}| j |¡}|  | j jjd¡ |  | j jjd¡ |  | jjjd¡ | jjjd d }|  t|ƒd¡ |  |d jd¡ |  t|ƒd¡ d S )	Nr[   rR   FrS   rW   r8   r   r]   )rG   rH   r_   r`   rD   r?   r@   rA   rq   r   rr   r^   r	   r   r   r>   r   rb   r'   rc   rd   rh   ri   r&   r,   )r   r   rj   Zcleaned_up_achallsr   r   r   Ú!test_name1_http_01_1_dns_1_acme_2’   s    z:HandleAuthorizationsTest.test_name1_http_01_1_dns_1_acme_2c                 C   s®   t jttj|d�| jj_tdtjd�tdtjd�tdtjd�g}tj	|d�}t
ƒ | jj_| j |¡}|  | jjjd¡ |  | jjjd¡ |  | jjjd¡ |  t|ƒd¡ d S )	N)rU   rV   rR   ©rT   rU   Ú1Ú2rW   r\   r8   )Ú	functoolsÚpartialr^   r	   r   rG   Zrequest_domain_challengesrD   r   r>   r_   r`   r   rb   r'   rc   rd   r?   rh   r&   )r   rV   Úauthzrsrj   r   r   r   r   Ú_test_name3_http_01_3_common§   s       ÿþz5HandleAuthorizationsTest._test_name3_http_01_3_commonc                 C   s   | j dd� d S rl   )r{   rP   r   r   r   Ú"test_name3_http_01_3_common_acme_1¼   s    z;HandleAuthorizationsTest.test_name3_http_01_3_common_acme_1c                 C   s   d| j _| jdd� d S ro   )rG   rH   r{   rP   r   r   r   Ú"test_name3_http_01_3_common_acme_2¿   s    z;HandleAuthorizationsTest.test_name3_http_01_3_common_acme_2c                 C   st   t j tjdd�tj¡ tdtj	d�g}tj
|d�}tƒ | jj_| j |¡ |  | jjjd¡ |  | jjjd¡ d S )NTr4   rR   ru   rW   r8   )r:   r;   r<   r   r   r   r=   r^   r	   r   r>   r_   rG   r`   rD   r   rb   r'   rc   rd   r9   Znotification©r   rz   rj   r   r   r   Útest_debug_challengesÃ   s    
 ÿz.HandleAuthorizationsTest.test_debug_challengesc                 C   s@   t dtjd�g}tj|d�}tj| jj_	|  
tj| jj|¡ d S ©NrR   ru   rW   )r^   r	   r   r   r>   r   ÚAuthorizationErrorr?   rC   rD   r-   r   rb   r~   r   r   r   Útest_perform_failureÐ   s      ÿz-HandleAuthorizationsTest.test_perform_failurec              	   C   sl   t dtjd�g}tj|d�}tdd�| jj_|  	t
j¡�}| j |dd¡ W 5 Q R X |  dt|jƒk¡ d S )	NrR   ru   rW   r[   )rY   Fr8   z0All authorizations were not finalized by the CA.)r^   r	   r   r   r>   r_   rG   r`   rD   r-   r   r�   r   rb   rf   ÚstrÚ	exception©r   rz   rj   Úerrorr   r   r   Útest_max_retries_exceededÙ   s    z2HandleAuthorizationsTest.test_max_retries_exceededc                 C   s$   t jg d�}|  tj| jj|¡ d S )NrW   )r   r>   r-   r   r�   r   rb   )r   rj   r   r   r   Útest_no_domainså   s    z(HandleAuthorizationsTest.test_no_domainsc                 C   s˜   t dtj|d�g}tj|d�}| jjj t	j
¡ | jj t	j
jt	jjf¡ tƒ | jj_| j |¡ |  | jjjd¡ |  | jjjd d d jd¡ d S )NrR   rS   rW   r8   r   r]   )r^   r	   r   r   r>   r?   r@   rA   rq   r   r*   r   Úpref_challsÚextendr,   rr   r_   rG   r`   rD   rb   r'   rh   rd   ri   ©r   rV   rz   rj   r   r   r   Ú'_test_preferred_challenge_choice_commoné   s    ÿ ÿz@HandleAuthorizationsTest._test_preferred_challenge_choice_commonc                 C   s   | j dd� d S rl   )rŒ   rP   r   r   r   Ú-test_preferred_challenge_choice_common_acme_1ù   s    zFHandleAuthorizationsTest.test_preferred_challenge_choice_common_acme_1c                 C   s   d| j _| jdd� d S ro   )rG   rH   rŒ   rP   r   r   r   Ú-test_preferred_challenge_choice_common_acme_2ü   s    zFHandleAuthorizationsTest.test_preferred_challenge_choice_common_acme_2c                 C   sH   t dtj|d�g}tj|d�}| jj tj	j
¡ |  tj| jj|¡ d S )NrR   rS   rW   )r^   r	   r   r   r>   r   r‰   rq   r   rr   r,   r-   r   r�   rb   r‹   r   r   r   Ú/_test_preferred_challenges_not_supported_common   s      ÿzHHandleAuthorizationsTest._test_preferred_challenges_not_supported_commonc                 C   s   | j dd� d S rl   )r�   rP   r   r   r   Ú.test_preferred_challenges_not_supported_acme_1  s    zGHandleAuthorizationsTest.test_preferred_challenges_not_supported_acme_1c                 C   s   d| j _| jdd� d S ro   )rG   rH   r�   rP   r   r   r   Ú.test_preferred_challenges_not_supported_acme_2
  s    zGHandleAuthorizationsTest.test_preferred_challenges_not_supported_acme_2c                 C   s6   t dtjgd�g}tj|d�}|  tj| jj	|¡ d S r€   )
r^   r	   rr   r   r>   r-   r   r�   r   rb   r~   r   r   r   Ú%test_dns_only_challenge_not_supported  s      ÿz>HandleAuthorizationsTest.test_dns_only_challenge_not_supportedc                 C   st   t j| jj_tdtjdd�}tj	|gd�}|  
t j| jj|¡ |  | jjjd¡ |  | jjjd d d jd¡ d S )NrR   TrS   rW   r8   r   r]   )r   r�   r?   rC   rD   r^   r	   r   r   r>   r-   r   rb   r'   rh   rd   ri   r,   ©r   r   rj   r   r   r   Útest_perform_error  s     ÿz+HandleAuthorizationsTest.test_perform_errorc                 C   sr   t j| jj_tdtjd�g}tj	|d�}|  
t j| jj|¡ |  | jjjd¡ |  | jjjd d d jd¡ d S )NrR   ru   rW   r8   r   r]   )r   r�   rG   rc   rD   r^   r	   r   r   r>   r-   r   rb   r'   r?   rh   rd   ri   r,   r~   r   r   r   Útest_answer_error  s      ÿ ÿz*HandleAuthorizationsTest.test_answer_errorc              
   C   s²   t dtjd�g}tj|d�}ttjd�| jj	_
t ¡ �, |  tj¡�}| j |d¡ W 5 Q R X W 5 Q R X |  dt|jƒk¡ |  | jjjd¡ |  | jjjd d d jd	¡ d S )
NrR   ru   rW   ©ÚstatusFzSome challenges have failed.r8   r   r]   )r^   r	   r   r   r>   r_   r   ÚSTATUS_INVALIDrG   r`   rD   Ú	test_utilÚpatch_get_utilityr-   r   r�   r   rb   rf   rƒ   r„   r'   r?   rh   rd   ri   r,   r…   r   r   r   Útest_incomplete_authzr_error+  s    
" ÿz5HandleAuthorizationsTest.test_incomplete_authzr_errorc              
   C   sà   dd„ }t dtjd�t dtjd�g}|| jj_tj|d�}t d¡�}| j	 
|d¡}W 5 Q R X |  t|ƒd	¡ |  |jd	¡ ttjd
�| jj_t ¡ �, |  tj¡�}| j	 
|d¡ W 5 Q R X W 5 Q R X |  dt|jƒk¡ d S )Nc                 S   s2   t tjƒ}t tjƒ}| jjjdkr*|| ƒS || ƒS )zBThis mock will invalidate one authzr, and invalidate the other oneúwill-be-invalid)r_   r   ÚSTATUS_VALIDr˜   ÚbodyÚ
identifierÚvalue)r   Z
valid_mockZinvalid_mockr   r   r   Ú_conditional_mock_on_poll9  s
    

zLHandleAuthorizationsTest.test_best_effort.<locals>._conditional_mock_on_pollzwill-be-validru   rœ   rW   z+certbot.auth_handler._report_failed_authzrsTr8   r–   zAll challenges have failed.)r^   r	   r   rG   r`   rD   r   r>   ra   r   rb   r'   r&   rd   r_   r   r˜   r™   rš   r-   r   r�   rf   rƒ   r„   )r   r¡   rz   rj   Zmock_reportZvalid_authzrr†   r   r   r   Útest_best_effort8  s    
ÿ

"z)HandleAuthorizationsTest.test_best_effortc                 C   sx   t  tjdt jgtjgd¡}tj|gd�}|  tj	| j
j|¡ t  tjdt jgtjgd¡}tj|gd�}| j
 |¡ d S )NrR   FrW   )r	   r   r   r   rr   r   r>   r-   r   r�   r   rb   r�   r“   r   r   r   Ú"test_validated_challenge_not_rerunY  s,      ý  ÿ  ýz;HandleAuthorizationsTest.test_validated_challenge_not_rerunc                 C   sæ   dd„ }dt jfdt jfdt jfg}dd„ |D ƒ}tj|d�}|| jj_| j 	|¡\}}|  
| jjjd	¡ |  
t|ƒd
¡ |  
t|ƒd
¡ |  
|d jjjd¡ |  
|d jjt j¡ |  
|d jjjd¡ |  
|d jjt j¡ dS )z“When we deactivate valid authzrs in an orderr, we expect them to become deactivated
        and to receive a list of deactivated authzrs in return.c                 S   sR   | j jtjkrD| j jjdkr&t d¡‚| j jtj	d�}tj
|d�} n
t d¡‚| S )NÚis_valid_but_will_failzMock deactivation ACME errorr–   )rž   z Can't deactivate non-valid authz)rž   r—   r   r�   rŸ   r    Úacme_errorsr.   ÚupdateÚSTATUS_DEACTIVATEDZAuthorizationResourcer   )r   Zauthzbr   r   r   Ú_mock_deactivateq  s    

zQHandleAuthorizationsTest.test_valid_authzrs_deactivated.<locals>._mock_deactivateZis_validZ
is_pendingr¤   c              	   S   s0   g | ](}t  |d  |d t jg|d  dg¡‘qS )r8   r   F)r	   r   r*   )r!   Úar   r   r   r#     s   ÿ
ÿzKHandleAuthorizationsTest.test_valid_authzrs_deactivated.<locals>.<listcomp>rW   r[   r8   r   N)r   r�   r   r   r>   rG   Zdeactivate_authorizationrD   r   Zdeactivate_valid_authorizationsr'   rd   r&   rž   rŸ   r    r—   r§   )r   r¨   Zto_deactivateZorderrrz   Zfailedr   r   r   Útest_valid_authzrs_deactivatedn  s$    
þÿ
z7HandleAuthorizationsTest.test_valid_authzrs_deactivatedN)r0   r1   r2   Ú__doc__r   rQ   rk   rn   rp   rs   rt   r{   r|   r}   r   r‚   r‡   rˆ   rŒ   r�   rŽ   r�   r�   r‘   r’   r”   r•   r›   r¢   r£   rª   r   r   r   r   r3   ;   s8   	!r3   r8   c                    s   d|i‰ ‡ ‡‡fdd„}|S )NÚcountc                    sv   ˆ d d ˆ d< ˆ d dk r ˆnt j}t || jjjdd„ | jjD ƒ|gt| jjƒ | jj	¡}|t
jdtˆƒid�fS )Nr¬   r8   r   c                 S   s   g | ]
}|j ‘qS r   r   )r!   Úchallbr   r   r   r#   ™  s     z4_gen_mock_on_poll.<locals>._mock.<locals>.<listcomp>zRetry-After)Úheaders)r   r   r	   r   rž   rŸ   r    r   r&   Úcombinationsr   r>   rƒ   )r   Zeffective_statusZupdated_azr©Ústater—   rZ   r   r   Ú_mock“  s    ûz _gen_mock_on_poll.<locals>._mockr   )r—   rY   rZ   r²   r   r°   r   r_   �  s    
r_   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚChallbToAchallTestz0Tests for certbot.auth_handler.challb_to_achall.c                 C   s   ddl m} ||ddƒS )Nr   )Úchallb_to_achallÚaccount_keyrT   )r   r´   )r   r­   r´   r   r   r   Ú_call£  s    zChallbToAchallTest._callc                 C   s&   |   |  tj¡tjtjddd�¡ d S )Nrµ   rT   )r­   rµ   rT   )r'   r¶   r	   ÚHTTP01_Pr   Z"KeyAuthorizationAnnotatedChallengerP   r   r   r   Útest_it§  s    
 þþzChallbToAchallTest.test_itN)r0   r1   r2   r«   r¶   r¸   r   r   r   r   r³      s   r³   c                   @   s<   e Zd ZdZdd„ Zdd„ Zedd„ ƒZdd	„ Zd
d„ Z	dS )ÚGenChallengePathTestzˆTests for certbot.auth_handler.gen_challenge_path.

    .. todo:: Add more tests for dumb_path... depending on what we want to do.

    c                 C   s   t  t j¡ d S rM   )rJ   rK   ÚFATALrP   r   r   r   r   ¶  s    zGenChallengePathTest.setUpc                 C   s   t  t j¡ d S rM   rN   rP   r   r   r   rQ   ¹  s    zGenChallengePathTest.tearDownc                 C   s   ddl m} ||||ƒS )Nr   )Úgen_challenge_path)r   r»   )ÚclsÚchallbsZpreferencesr¯   r»   r   r   r   r¶   ¼  s    zGenChallengePathTest._callc                 C   sˆ   t jt jf}tjtjg}d}|  |  |||¡d¡ |  |  ||d¡¡ |  |  |ddd… ||¡d¡ |  |  |ddd… |d¡¡ dS )z/Given DNS01 and HTTP01 with appropriate combos.)©r   ©r8   r¾   Néÿÿÿÿr¿   )	r	   ÚDNS01_Pr·   r   rr   r*   r'   r¶   rf   ©r   r½   ZprefsrV   r   r   r   Útest_common_caseÁ  s     z%GenChallengePathTest.test_common_casec                 C   sH   t jt jf}tjg}d}|  tj| j|||¡ |  tj| j||d ¡ d S )N))r   r8   )	r	   rÁ   r·   r   r*   r-   r   r�   r¶   rÂ   r   r   r   Útest_not_supportedÎ  s"        ÿ    ÿz'GenChallengePathTest.test_not_supportedN)
r0   r1   r2   r«   r   rQ   Úclassmethodr¶   rÃ   rÄ   r   r   r   r   r¹   °  s   
r¹   c                   @   s8   e Zd ZdZdd„ Ze ¡ dd„ ƒZe ¡ dd„ ƒZdS )	ÚReportFailedAuthzrsTestz6Tests for certbot.auth_handler._report_failed_authzrs.c                 C   s¼   t jdtjtjjddd�dœ}|  |d jd k	¡ tjf |Ž}t j|d< tjf |Ž}t	 
¡ | _d| jjj_||g| jj_tjd	dd
�|d< tjf |Ž}t	 
¡ | _d| jjj_|g| jj_d S )NÚuriÚtlsÚdetail)rÉ   )r   rÇ   r—   r†   r†   r   zexample.comZdnssec)r,   rÉ   zfoo.bar)r	   r*   r   r˜   r.   Z	with_coderf   ÚdescriptionZChallengeBodyr   r>   Úauthzr1rž   rŸ   r    r   Úauthzr2)r   ÚkwargsZhttp_01Zhttp_01_diffr   r   r   r   ß  s"    ü


zReportFailedAuthzrsTest.setUpc                 C   sV   ddl m} | | jgd¡ |ƒ jj}|  t|ƒdk¡ |  d|d d d k¡ d S )Nr   ©Úauth_handlerr   r8   z.Domain: example.com
Type:   tls
Detail: detail)ÚcertbotrÏ   Ú_report_failed_authzrsrË   Úadd_messagerg   rf   r&   )r   Ú	mock_zoperÏ   Ú	call_listr   r   r   Útest_same_error_and_domainú  s
    
z2ReportFailedAuthzrsTest.test_same_error_and_domainc                 C   s8   ddl m} | | j| jgd¡ |  |ƒ jjdk¡ d S )Nr   rÎ   r   r[   )rÐ   rÏ   rÑ   rË   rÌ   rf   rÒ   rd   )r   rÓ   rÏ   r   r   r   Ú!test_different_errors_and_domains  s    z9ReportFailedAuthzrsTest.test_different_errors_and_domainsN)	r0   r1   r2   r«   r   r™   rš   rÕ   rÖ   r   r   r   r   rÆ   Û  s   
rÆ   c                 C   s   dd„ | D ƒS )z(Generate a dummy authorization response.c                 S   s   g | ]}d |j j|jf ‘qS )z%s%s)Ú	__class__r0   rT   )r!   r   r   r   r   r#     s   ÿz!gen_auth_resp.<locals>.<listcomp>r   )Z
chall_listr   r   r   rB     s    ÿrB   Tc                 C   s    t  tj| |tjgt|ƒ |¡S )z!Generates new authzr for domains.)r	   r   r   r   r&   rS   r   r   r   r^     s       þr^   Ú__main__)T)!r«   rx   rJ   Zunittestr   Zzope.componentr:   Zacmer   r   rF   r   r   r¥   rÐ   r   r   r   Zcertbot.testsr	   r™   ZTestCaser
   r3   r�   r_   r³   r¹   rÆ   rB   r^   r0   Úmainr   r   r   r   Ú<module>   s6   $  W+0

