U
    eà–_‘;  ã                   @   sH  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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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rDe '¡  dS )z!Tests for certbot.plugins.common.é    N)Ú
challenges)Úachallenges)Úcrypto_util)Úerrors)Úos)Ú
filesystem)Ú	acme_util)Úutilzrsa512_key.pems   token1)ÚtokenÚpendingzencryption-example.demo)ZchallbZdomainZaccount_keyc                   @   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   ú=/usr/lib/python3/dist-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_arguments1   s    z9PluginTest.setUp.<locals>.MockPlugin.add_parser_argumentsN)r   r   r   Úclassmethodr*   r   r   r   r   Ú
MockPlugin0   s   r,   Úmock©ÚconfigÚname)r   r!   Ú
plugin_clsr-   Ú	MagicMockr/   Úplugin)r   r!   r,   r   r   r   ÚsetUp-   s
    
zPluginTest.setUpc                 C   s&   |   d| jj¡ |   | j| jj¡ d S )Nr-   )r   r3   r0   r/   ©r   r   r   r   Ú	test_init9   s    zPluginTest.test_initc                 C   s   |   d| jj¡ d S )Nzmock-)r   r3   r   r5   r   r   r   r   =   s    z PluginTest.test_option_namespacec                 C   s   |   d| j d¡¡ d S )Nzmock-foo_barÚfoo_bar)r   r3   Zoption_namer5   r   r   r   Útest_option_name@   s    zPluginTest.test_option_namec                 C   s   |   d| jj¡ d S )NZmock_)r   r3   r   r5   r   r   r   r   C   s    zPluginTest.test_dest_namespacec                 C   s,   |   d| j d¡¡ |   d| j d¡¡ d S )NÚmock_foo_barr   r7   )r   r3   r%   r5   r   r   r   Ú	test_destF   s    zPluginTest.test_destc                 C   s   |   | jj| j d¡¡ d S )Nr   )r   r/   r9   r3   Úconfr5   r   r   r   Ú	test_confJ   s    zPluginTest.test_confc                 C   s.   t  ¡ }| j |d¡ |jjdddd d d S )Nr-   z--mock-foo-barr"   r#   r$   )r-   r2   r1   Zinject_parser_optionsÚadd_argumentÚassert_called_once_with)r   Úparserr   r   r   Útest_inject_parser_optionsM   s       ÿz%PluginTest.test_inject_parser_optionsN)r   r   r   r   r4   r6   r   r8   r   r:   r<   r@   r   r   r   r   r    *   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   )Ú	InstallerrB   r.   )ÚsuperrA   r4   r   Úmkdirr/   Z
config_dirr   rB   Ú	installerÚreverter)r   rB   ©Ú	__class__r   r   r4   Y   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_checkpointb   s    z)InstallerTest.test_add_to_real_checkpointc                 C   s   |   d¡ d S )NF©Ú_test_add_to_checkpoint_commonr5   r   r   r   Útest_add_to_real_checkpoint2g   s    z*InstallerTest.test_add_to_real_checkpoint2c                 C   s   |   d¡ d S )NTrQ   r5   r   r   r   Ú test_add_to_temporary_checkpointj   s    z.InstallerTest.test_add_to_temporary_checkpointc                 C   s@   t dƒ}d}tj| jj|d}|r(d}nd}|  ||||¡ d S )NrI   rJ   )Ú	temporaryZadd_to_temp_checkpointrK   )rL   Ú	functoolsÚpartialrE   rK   Ú_test_adapted_method)r   rU   rN   rO   Úinstaller_funcÚreverter_func_namer   r   r   rR   m   s    
ÿz,InstallerTest._test_add_to_checkpoint_commonc                 C   s   |   dd¡ d S )NZfinalize_checkpointr   ©rM   r5   r   r   r   Útest_finalize_checkpoint{   s    z&InstallerTest.test_finalize_checkpointc                 C   s   |   d¡ d S )NZrecovery_routiner[   r5   r   r   r   Útest_recovery_routine~   s    z#InstallerTest.test_recovery_routinec                 C   s   |   d¡ d S )NZrevert_temporary_configr[   r5   r   r   r   Útest_revert_temporary_config   s    z*InstallerTest.test_revert_temporary_configc                 C   s   |   dd¡ d S )NZrollback_checkpointsé*   r[   r5   r   r   r   Útest_rollback_checkpoints„   s    z'InstallerTest.test_rollback_checkpointsc                 C   s   |   d¡ d S )NÚview_config_changesr[   r5   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ÚfilterwarningsrE   ra   r5   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)ÚgetattrrE   rX   )r   r0   ÚargsÚkwargsrY   r   r   r   rM   —   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)
r-   ÚpatchÚobjectrF   r>   r   ZReverterErrorZside_effectÚassertRaisesZPluginError)r   rY   rZ   Zpassed_argsZpassed_kwargsZreverter_funcr   r   r   rX   ¢   s    
 ÿÿÿz"InstallerTest._test_adapted_methodc                 C   s$   | j  ¡  |  tj | j j¡¡ d S ©N)rE   Zinstall_ssl_dhparamsÚ
assertTruer   ÚpathÚisfileZssl_dhparamsr5   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.constantsrt   r   Ú	sha256sum)r   rt   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.)ru   rx   rp   rw   )r   rx   r   r   r   Ú$test_current_file_hash_in_all_hashes¾   s    ÿz2InstallerTest.test_current_file_hash_in_all_hashes)r   r   r   r   r4   rP   rS   rT   rR   r\   r]   r^   r`   rb   rh   rM   rX   rs   rw   ry   Ú__classcell__r   r   rG   r   rA   V   s"   	rA   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   r4   È   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~   Ú Ú*Z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Œ   r5   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Š   r5   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 )
NZ443z192.168.1.1:443rŽ   r~   r   r   z[fe00::1]:443r   r‚   )r   r’   r…   Úget_addr_objr†   rˆ   r‰   r5   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}   rL   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   r4   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)r-   r2   r   r›   Ú	performer)r   Zconfiguratorr›   r   r   r   r4     s    zChallengePerformerTest.setUpc                 C   s8   | j  td¡ |  dt| j jƒ¡ |  dg| j j¡ d S )Nr   r#   )rœ   Z	add_challÚACHALLr   ÚlenZachallsÚindicesr5   r   r   r   Útest_add_chall  s    z%ChallengePerformerTest.test_add_challc                 C   s   |   t| jj¡ d S ro   )rn   ÚNotImplementedErrorrœ   Zperformr5   r   r   r   Útest_perform$  s    z#ChallengePerformerTest.test_performN)r   r   r   r   r4   r    r¢   r   r   r   r   rš     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)rC   r£   r4   Úhashesr   rq   ÚjoinZtempdirÚ	dest_pathÚ	hash_pathÚold_pathÚsource_pathÚopenÚwriteÚappendr   rv   )r   rq   ÚfrG   r   r   r4   +  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   Ú_call7  s    ýz&InstallVersionControlledFileTest._callc                 C   s   t  | j¡S ro   )r   rv   rª   r5   r   r   r   Ú_current_file_hash>  s    z3InstallVersionControlledFileTest._current_file_hashc                 C   s0   |   tj | j¡¡ |  t | j¡|  ¡ ¡ d S ro   )	rp   r   rq   rr   r§   r   r   rv   r±   r5   r   r   r   Ú_assert_current_fileA  s    ÿz5InstallVersionControlledFileTest._assert_current_filec                 C   s(   |   tj | j¡¡ |  ¡  |  ¡  d S ro   )r—   r   rq   rr   r§   r°   r²   r5   r   r   r   Útest_no_fileF  s    z-InstallVersionControlledFileTest.test_no_filec                 C   s"   t dƒD ]}|  ¡  |  ¡  qd S )Né   )Úranger°   r²   )r   Ú_r   r   r   Útest_current_fileK  s    z2InstallVersionControlledFileTest.test_current_filec                 C   s$   t  | j| j¡ |  ¡  |  ¡  d S ro   )ÚshutilZcopyfiler©   r§   r°   r²   r5   r   r   r   Ú!test_prev_file_updates_to_currentQ  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¬   r-   rl   r—   ÚwarningÚcalledrp   r   rq   rr   r   r   rv   rª   r±   r–   )r   Úmod_ssl_confÚmock_loggerr   r   r   Ú3test_manually_modified_current_file_does_not_updateV  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¨   r-   rl   r°   r   r½   Z	call_argsr   rv   rª   r±   r—   r¾   )r   r¿   r®   rÀ   r   r   r   Ú&test_manually_modified_past_file_warnsc  s    ÿÿzGInstallVersionControlledFileTest.test_manually_modified_past_file_warns)r   r   r   r   r4   r°   r±   r²   r³   r·   r¹   rÁ   rÂ   rz   r   r   rG   r   r£   (  s   r£   Ú__main__)(r   rV   r¸   Zunittestre   ZjosepyZjoser-   Zacmer   Zcertbotr   r   r   Zcertbot.compatr   r   Zcertbot.testsr   r	   Z	test_utilZJWKRSAÚloadZload_vectorZAUTH_KEYZ"KeyAuthorizationAnnotatedChallengeZchall_to_challbZHTTP01r   ZTestCaser   r    ZConfigTestCaserA   r{   rš   ZTempDirTestCaser£   r   Úmainr   r   r   r   Ú<module>   s<   ÿ ý,oQL
