U
    ÓZj^F  ã                   @   sŠ  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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eje ejdd�d¡ded�eje ejdd�d¡ded�gZG dd„ dejƒZ G dd„ dejƒZ!G dd„ dej"ƒZ#G dd„ dejƒZ$G dd„ dejƒ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.common.é    N)Ú
challenges)Úachallenges)Úcrypto_util)Úerrors)Úos)Ú
filesystem)Ú	acme_util)Úutilúrsa512_key.pems   token1)ÚtokenÚpendingzencryption-example.demo)ZchallbÚdomainÚaccount_keys   token2zcertbot.democ                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚNamespaceFunctionsTestz7Tests for certbot.plugins.common.*_namespace functions.c                 C   s    ddl m} |  d|dƒ¡ d S )Nr   )Úoption_namespacezfoo-Úfoo)Úcertbot.plugins.commonr   ÚassertEqual)Úselfr   © r   ú^/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/certbot/plugins/common_test.pyÚtest_option_namespace%   s    z,NamespaceFunctionsTest.test_option_namespacec                 C   s    ddl m} |  d|dƒ¡ d S )Nr   ©Údest_namespaceZfoo_r   ©r   r   r   ©r   r   r   r   r   Útest_dest_namespace)   s    z*NamespaceFunctionsTest.test_dest_namespacec                 C   s    ddl m} |  d|dƒ¡ d S )Nr   r   Zfoo_bar_úfoo-barr   r   r   r   r   Útest_dest_namespace_with_dashes-   s    z6NamespaceFunctionsTest.test_dest_namespace_with_dashesN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   "   s   r   c                   @   sP   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S )Ú
PluginTestz'Test for certbot.plugins.common.Plugin.c                 C   s@   ddl m} G dd„ d|ƒ}|| _t ¡ | _|| jdd�| _d S )Nr   )ÚPluginc                   @   s   e Zd Zedd„ ƒZdS )z$PluginTest.setUp.<locals>.MockPluginc                 S   s   |dddd d� d S )Nr   Údifferent_to_foo_baré   ©ÚdestÚxÚyr   )ÚclsÚaddr   r   r   Úadd_parser_arguments9   s    z9PluginTest.setUp.<locals>.MockPlugin.add_parser_argumentsN)r   r    r!   Úclassmethodr-   r   r   r   r   Ú
MockPlugin8   s   r/   Úmock©ÚconfigÚname)r   r$   Ú
plugin_clsr0   Ú	MagicMockr2   Úplugin)r   r$   r/   r   r   r   ÚsetUp5   s
    
zPluginTest.setUpc                 C   s&   |   d| jj¡ |   | j| jj¡ d S )Nr0   )r   r6   r3   r2   ©r   r   r   r   Ú	test_initA   s    zPluginTest.test_initc                 C   s   |   d| jj¡ d S )Nzmock-)r   r6   r   r8   r   r   r   r   E   s    z PluginTest.test_option_namespacec                 C   s   |   d| j d¡¡ d S )Nzmock-foo_barÚfoo_bar)r   r6   Zoption_namer8   r   r   r   Útest_option_nameH   s    zPluginTest.test_option_namec                 C   s   |   d| jj¡ d S )NZmock_)r   r6   r   r8   r   r   r   r   K   s    zPluginTest.test_dest_namespacec                 C   s,   |   d| j d¡¡ |   d| j d¡¡ d S )NÚmock_foo_barr   r:   )r   r6   r(   r8   r   r   r   Ú	test_destN   s    zPluginTest.test_destc                 C   s   |   | jj| j d¡¡ d S )Nr   )r   r2   r<   r6   Zconfr8   r   r   r   Ú	test_confR   s    zPluginTest.test_confc                 C   s.   t  ¡ }| j |d¡ |jjdddd d� d S )Nr0   z--mock-foo-barr%   r&   r'   )r0   r5   r4   Zinject_parser_optionsÚadd_argumentÚassert_called_once_with)r   Úparserr   r   r   Útest_inject_parser_optionsU   s       ÿz%PluginTest.test_inject_parser_optionsN)r   r    r!   r"   r7   r9   r   r;   r   r=   r>   rB   r   r   r   r   r#   2   s   r#   c                       s˜   e Zd ZdZ‡ f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‡  ZS )"ÚInstallerTestz+Tests for certbot.plugins.common.Installer.c                    sF   t t| ƒ ¡  t | jj¡ ddlm} || jdd�| _	| j	j
| _
d S )Nr   )Ú	InstallerrD   r1   )ÚsuperrC   r7   r   Úmkdirr2   Ú
config_dirr   rD   Ú	installerÚreverter)r   rD   ©Ú	__class__r   r   r7   a   s    ÿzInstallerTest.setUpc                 C   s   t dƒ}d}|  d||¡ d S )N©zfoo.barzbaz.quxúfoo bar baz quxÚadd_to_checkpoint)ÚsetÚ_test_wrapped_method)r   ÚfilesÚ
save_notesr   r   r   Útest_add_to_real_checkpointj   s    z)InstallerTest.test_add_to_real_checkpointc                 C   s   |   d¡ d S )NF©Ú_test_add_to_checkpoint_commonr8   r   r   r   Útest_add_to_real_checkpoint2o   s    z*InstallerTest.test_add_to_real_checkpoint2c                 C   s   |   d¡ d S )NTrT   r8   r   r   r   Ú test_add_to_temporary_checkpointr   s    z.InstallerTest.test_add_to_temporary_checkpointc                 C   s@   t dƒ}d}tj| jj|d�}|r(d}nd}|  ||||¡ d S )NrL   rM   )Ú	temporaryZadd_to_temp_checkpointrN   )rO   Ú	functoolsÚpartialrH   rN   Ú_test_adapted_method)r   rX   rQ   rR   Úinstaller_funcÚreverter_func_namer   r   r   rU   u   s    
ÿz,InstallerTest._test_add_to_checkpoint_commonc                 C   s   |   dd¡ d S )NZfinalize_checkpointr   ©rP   r8   r   r   r   Útest_finalize_checkpointƒ   s    z&InstallerTest.test_finalize_checkpointc                 C   s   |   d¡ d S )NZrecovery_routiner^   r8   r   r   r   Útest_recovery_routine†   s    z#InstallerTest.test_recovery_routinec                 C   s   |   d¡ d S )NZrevert_temporary_configr^   r8   r   r   r   Útest_revert_temporary_config‰   s    z*InstallerTest.test_revert_temporary_configc                 C   s   |   dd¡ d S )NZrollback_checkpointsé*   r^   r8   r   r   r   Útest_rollback_checkpointsŒ   s    z'InstallerTest.test_rollback_checkpointsc                 C   s   |   d¡ d S )NÚview_config_changesr^   r8   r   r   r   Útest_view_config_changes�   s    z&InstallerTest.test_view_config_changesc              	   C   s2   t  ¡ �  t jdddd� | j ¡  W 5 Q R X d S )NÚerrorz.*view_config_changesz	.*common$)Úmodule)ÚwarningsÚcatch_warningsÚfilterwarningsrH   rd   r8   r   r   r   Ú+test_view_config_changes_warning_supression’   s    
	z9InstallerTest.test_view_config_changes_warning_supressionc                 O   s$   t | j|ƒ}| j||f|ž|Ž dS )zÎTest a wrapped reverter method.

        :param str name: name of the method to test
        :param tuple args: position arguments to method
        :param dict kwargs: keyword arguments to method

        N)ÚgetattrrH   r[   )r   r3   ÚargsÚkwargsr\   r   r   r   rP   Ÿ   s    z"InstallerTest._test_wrapped_methodc              	   O   sT   t j | j|¡�:}|||Ž |j||Ž tj|_| jtj	|f|ž|Ž W 5 Q R X dS )aÂ  Test an adapted reverter method

        :param callable installer_func: installer method to test
        :param str reverter_func_name: name of the method on the
            reverter that should be called
        :param tuple passed_args: positional arguments passed from
            installer method to the reverter method
        :param dict passed_kargs: keyword arguments passed from
            installer method to the reverter method

        N)
r0   ÚpatchÚobjectrI   r@   r   ZReverterErrorZside_effectÚassertRaisesZPluginError)r   r\   r]   Zpassed_argsZpassed_kwargsZreverter_funcr   r   r   r[   ª   s    
 ÿÿÿz"InstallerTest._test_adapted_methodc                 C   s$   | j  ¡  |  tj | j j¡¡ d S ©N)rH   Zinstall_ssl_dhparamsÚ
assertTruer   ÚpathÚisfileZssl_dhparamsr8   r   r   r   Útest_install_ssl_dhparams¾   s    
z'InstallerTest.test_install_ssl_dhparamsc                 C   s   ddl m} t |¡S )Nr   )ÚSSL_DHPARAMS_SRC)Úcertbot.constantsrw   r   Ú	sha256sum)r   rw   r   r   r   Ú_current_ssl_dhparams_hashÂ   s    z(InstallerTest._current_ssl_dhparams_hashc                 C   s$   ddl m} |  |  ¡ |kd¡ d S )Nr   )ÚALL_SSL_DHPARAMS_HASHESzwConstants.ALL_SSL_DHPARAMS_HASHES must be appended with the sha256 hash of self.config.ssl_dhparams when it is updated.)rx   r{   rs   rz   )r   r{   r   r   r   Ú$test_current_file_hash_in_all_hashesÆ   s    ÿz2InstallerTest.test_current_file_hash_in_all_hashes)r   r    r!   r"   r7   rS   rV   rW   rU   r_   r`   ra   rc   re   rk   rP   r[   rv   rz   r|   Ú__classcell__r   r   rJ   r   rC   ^   s"   	rC   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S )ÚAddrTestz-Tests for certbot.client.plugins.common.Addr.c                 C   sp   ddl m} | d¡| _| d¡| _| d¡| _| d¡| _| d¡| _| d¡| _| d	¡| _	| d
¡| _
d S )Nr   ©ÚAddrú192.168.1.1ú192.168.1.1:*ú192.168.1.1:80ú	[fe00::1]ú[fe00::1]:*ú[fe00::1]:80z[fe00::1]:5z[fe00:1:2:3:4:5:6:7:8:9]:8080)r   r€   Ú
fromstringÚaddr1Úaddr2Úaddr3Úaddr4Úaddr5Úaddr6Úaddr7Úaddr8©r   r€   r   r   r   r7   Ð   s    zAddrTest.setUpc                 C   s$  |   | j ¡ d¡ |   | j ¡ d¡ |   | j ¡ d¡ |   | j ¡ d¡ |   | j ¡ d¡ |   | j ¡ d¡ |   | j ¡ d¡ |   | j ¡ d¡ |   | j ¡ d¡ |   | j ¡ d¡ |   | j ¡ d¡ |   | j ¡ d¡ |   | j 	¡ d¡ |   | j 	¡ d¡ |   | j
 ¡ d¡ |   | j 	¡ d¡ d S )	Nr�   Ú Ú*Ú80r„   zfe00:0:0:0:0:0:0:1Ú5zfe00:1:2:3:4:5:6:7)r   rˆ   Zget_addrZget_portr‰   rŠ   r‹   rŒ   r�   Zget_ipv6_explodedrŽ   r�   r8   r   r   r   Útest_fromstringÛ   s,    ÿÿÿzAddrTest.test_fromstringc                 C   sp   |   t| jƒd¡ |   t| jƒd¡ |   t| jƒd¡ |   t| jƒd¡ |   t| jƒd¡ |   t| jƒd¡ d S )Nr�   r‚   rƒ   r„   r…   r†   )r   Ústrrˆ   r‰   rŠ   r‹   rŒ   r�   r8   r   r   r   Útest_strð   s    zAddrTest.test_strc                 C   s”   |   t| j d¡ƒd¡ |   t| j d¡ƒd¡ |   t| j d¡ƒd¡ |   t| j d¡ƒd¡ |   t| j d¡ƒd¡ |   t| j d¡ƒd	¡ d S )
NÚ443z192.168.1.1:443r‘   r�   r’   r‚   z[fe00::1]:443r„   r…   )r   r–   rˆ   Úget_addr_objr‰   r‹   rŒ   r8   r   r   r   Útest_get_addr_objø   s    zAddrTest.test_get_addr_objc                 C   s¤   |   | j| j d¡¡ |  | j| j¡ |  | jdk¡ |   | j| j d¡¡ |  | j| j¡ |  | jdk¡ ddlm	} |   | j| 
d¡¡ |   | j| 
d¡¡ d S )Nr‘   i  r   r   z[fe00:0:0::1]z[fe00:0::0:0:1])r   rˆ   r‰   r™   ÚassertNotEqualÚassertFalser‹   rŒ   r   r€   r‡   r�   r   r   r   Útest_eq   s    zAddrTest.test_eqc           
      C   sˆ   ddl m} t| j| jgƒ}| d¡}| d¡}t||gƒ}|  ||¡ t| j| jgƒ}| d¡}| d¡}t||gƒ}	|  ||	¡ d S )Nr   r   r�   r‚   r„   r…   )	r   r€   rO   rˆ   r‰   r‡   r   r‹   rŒ   )
r   r€   Zset_aZaddr1bZaddr2bZset_bZset_cZaddr4bZaddr5bZset_dr   r   r   Útest_set_inclusion  s    



zAddrTest.test_set_inclusionN)
r   r    r!   r"   r7   r•   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S )	ÚChallengePerformerTestz4Tests for certbot.plugins.common.ChallengePerformer.c                 C   s"   t  ¡ }ddlm} ||ƒ| _d S )Nr   )ÚChallengePerformer)r0   r5   r   r    Ú	performer)r   Úconfiguratorr    r   r   r   r7   !  s    zChallengePerformerTest.setUpc                 C   s<   | j  td d¡ |  dt| j jƒ¡ |  dg| j j¡ d S )Nr   r&   )r¡   Z	add_challÚACHALLSr   ÚlenZachallsÚindicesr8   r   r   r   Útest_add_chall'  s    z%ChallengePerformerTest.test_add_challc                 C   s   |   t| jj¡ d S rr   )rq   ÚNotImplementedErrorr¡   Zperformr8   r   r   r   Útest_perform,  s    z#ChallengePerformerTest.test_performN)r   r    r!   r"   r7   r¦   r¨   r   r   r   r   rŸ     s   rŸ   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚTLSSNI01Testz*Tests for certbot.plugins.common.TLSSNI01.c                 C   sV   t  ¡ | _t ¡ }tj | jd¡|j_	tj | jd¡|j_
ddlm} ||d�| _d S )Nr2   Zworkr   ©ÚTLSSNI01)r¢   )ÚtempfileÚmkdtempÚtempdirr0   r5   r   rt   Újoinr2   rG   Zwork_dirr   r«   Úsni)r   r¢   r«   r   r   r   r7   3  s    
zTLSSNI01Test.setUpc                 C   s   t  | j¡ d S rr   )ÚshutilÚrmtreer®   r8   r   r   r   ÚtearDown<  s    zTLSSNI01Test.tearDownc              
   C   sö   t  ¡ t  ¡  }}t ¡ }t  ¡ }d|jj_t 	d¡}|t 
d¡|ff|j_t jd|dd��4 t  d|¡� |  || j |d¡¡ W 5 Q R X W 5 Q R X | | j |¡d	¡ |jj t d¡¡ |j| j |¡d	d
d� |jj tj tjj|¡¡ d S )Nr   r
   zcert_512.pemzcertbot.plugins.common.openT)Úcreatez%certbot.plugins.common.util.safe_openZrandomS1Úwbé   )Úchmod)r0   Ú	mock_openr   ZTLSSNI01Responser5   ZchallÚencodeZreturn_valueÚ	test_utilZload_pyopenssl_private_keyZ	load_certZresponse_and_validationro   r   r°   Z_setup_challenge_certr@   Zget_cert_pathÚwriteÚload_vectorZget_key_pathÚOpenSSLZcryptoZdump_privatekeyZFILETYPE_PEM)r   r¸   Zmock_safe_openÚresponseÚachallÚkeyr   r   r   Útest_setup_challenge_cert?  s>    

 ÿ ÿÿ ÿÿ
  ÿÿz&TLSSNI01Test.test_setup_challenge_certc                 C   s0   t d }|  | j |¡| |j¡j d¡¡ d S )Nr   zutf-8)r£   r   r°   Zget_z_domainr¾   r   Zz_domainÚdecode)r   r¿   r   r   r   Útest_get_z_domain^  s    ÿzTLSSNI01Test.test_get_z_domainc              	   C   s@   t  d¡�}ddlm} W 5 Q R X |  |jd d  d¡¡ d S )Nz$certbot.plugins.common.warnings.warnr   rª   r«   )r0   ro   r   r«   rs   Ú	call_argsÚ
startswith)r   Z	mock_warnr«   r   r   r   Útest_warningc  s    zTLSSNI01Test.test_warningN)	r   r    r!   r"   r7   r³   rÁ   rÃ   rÆ   r   r   r   r   r©   0  s   	r©   c                       s`   e Zd ZdZ‡ f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‡  ZS )Ú InstallVersionControlledFileTestzATests for certbot.plugins.common.install_version_controlled_file.c              
      s¦   t t| ƒ ¡  dg| _tj | jd¡| _tj | jd¡| _	tj | jd¡| _
tj | jd¡| _| j| j
fD ]6}t|dƒ�}| |¡ W 5 Q R X | j t |¡¡ qjd S )NZsomeotherhashzoptions-ssl-dest.confz.options-ssl-conf.txtzoptions-ssl-old.confzoptions-ssl-src.confÚw)rE   rÇ   r7   Úhashesr   rt   r¯   r®   Ú	dest_pathÚ	hash_pathÚold_pathÚsource_pathÚopenr»   Úappendr   ry   )r   rt   ÚfrJ   r   r   r7   l  s    z&InstallVersionControlledFileTest.setUpc                 C   s&   ddl m} || j| j| j| jƒ d S )Nr   )Úinstall_version_controlled_file)r   rÑ   rÊ   rË   rÍ   rÉ   )r   rÑ   r   r   r   Ú_callx  s    ýz&InstallVersionControlledFileTest._callc                 C   s   t  | j¡S rr   )r   ry   rÍ   r8   r   r   r   Ú_current_file_hash  s    z3InstallVersionControlledFileTest._current_file_hashc                 C   s0   |   tj | j¡¡ |  t | j¡|  ¡ ¡ d S rr   )	rs   r   rt   ru   rÊ   r   r   ry   rÓ   r8   r   r   r   Ú_assert_current_file‚  s    ÿz5InstallVersionControlledFileTest._assert_current_filec                 C   s(   |   tj | j¡¡ |  ¡  |  ¡  d S rr   )rœ   r   rt   ru   rÊ   rÒ   rÔ   r8   r   r   r   Útest_no_file‡  s    z-InstallVersionControlledFileTest.test_no_filec                 C   s"   t dƒD ]}|  ¡  |  ¡  qd S )Né   )ÚrangerÒ   rÔ   )r   Ú_r   r   r   Útest_current_fileŒ  s    z2InstallVersionControlledFileTest.test_current_filec                 C   s$   t  | j| j¡ |  ¡  |  ¡  d S rr   )r±   ÚcopyfilerÌ   rÊ   rÒ   rÔ   r8   r   r   r   Ú!test_prev_file_updates_to_current’  s    zBInstallVersionControlledFileTest.test_prev_file_updates_to_currentc              	   C   sž   |   ¡  t| jdƒ�}| d¡ W 5 Q R X t d¡�}|   ¡  |  |jj¡ W 5 Q R X |  	t
j | j¡¡ |  t | j¡|  ¡ ¡ |  t | j¡|  ¡ ¡ d S )NÚaúa new line for the wrong hash
úcertbot.plugins.common.logger)rÒ   rÎ   rÊ   r»   r0   ro   rœ   ÚwarningÚcalledrs   r   rt   ru   r   r   ry   rÍ   rÓ   r›   )r   Úmod_ssl_confÚmock_loggerr   r   r   Ú3test_manually_modified_current_file_does_not_update—  s    ÿÿzTInstallVersionControlledFileTest.test_manually_modified_current_file_does_not_updatec              	   C   sÂ   t | jdƒ�}| d¡ W 5 Q R X t | jdƒ�}| d¡ W 5 Q R X t d¡�&}|  ¡  |  |jj	d d d¡ W 5 Q R X |  t
 | j¡|  ¡ ¡ t d¡�}|  ¡  |  |jj¡ W 5 Q R X d S )NrÜ   rÝ   rÈ   ZhashofanoldversionrÞ   r   zh%s has been manually modified; updated file saved to %s. We recommend updating %s for security purposes.)rÎ   rÊ   r»   rË   r0   ro   rÒ   r   rß   rÄ   r   ry   rÍ   rÓ   rœ   rà   )r   rá   rÐ   râ   r   r   r   Ú&test_manually_modified_past_file_warns¤  s    ÿÿzGInstallVersionControlledFileTest.test_manually_modified_past_file_warns)r   r    r!   r"   r7   rÒ   rÓ   rÔ   rÕ   rÙ   rÛ   rã   rä   r}   r   r   rJ   r   rÇ   i  s   rÇ   Ú__main__)+r"   rY   r±   r¬   Zunittestrh   r½   ZjosepyZjoser0   Zacmer   Zcertbotr   r   r   Zcertbot.compatr   r   Zcertbot.testsr   r	   rº   ZJWKRSAÚloadr¼   ZAUTH_KEYZ"KeyAuthorizationAnnotatedChallengeZchall_to_challbr«   r£   ZTestCaser   r#   ZConfigTestCaserC   r~   rŸ   r©   ZTempDirTestCaserÇ   r   Úmainr   r   r   r   Ú<module>   sV   
 ÿ ý
 ÿ ýû,oQ9L
