
\\"B                 @   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 e j j e j d	   Z e j d
 e j e j d d  d  d d d e  e j d
 e j e j d d  d  d d d e  g Z Gd d   d e j  Z Gd d   d e j  Z Gd d   d e j  Z  Gd d   d e j  Z! Gd d   d e j  Z" Gd d   d e j  Z# Gd d    d  e j$  Z% e& d! k re j'   d S)"z!Tests for certbot.plugins.common.    N)
challenges)achallenges)crypto_util)errors)	acme_util)utilzrsa512_key.pemZchallbtokens   token1pendingZdomainzencryption-example.demo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 } |  j 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 } |  j 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 } |  j d | d   d  S)Nr   )r   Zfoo_bar_zfoo-bar)r   r   r   )r   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               @   sv   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 } Gd d   d |  } | |  _ t j   |  _ | d |  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 d  d  S)Nzfoo-bardestdifferent_to_foo_barx   yr   )clsaddr   r   r   add_parser_arguments8   s    z9PluginTest.setUp.<locals>.MockPlugin.add_parser_argumentsN)r   r   r   classmethodr$   r   r   r   r   
MockPlugin7   s   r&   confignamemock)r   r   
plugin_clsr)   	MagicMockr'   plugin)r   r   r&   r   r   r   setUp4   s
    	zPluginTest.setUpc             C   s3   |  j  d |  j j  |  j  |  j |  j j  d  S)Nr)   )r   r,   r(   r'   )r   r   r   r   	test_init@   s    zPluginTest.test_initc             C   s   |  j  d |  j j  d  S)Nzmock-)r   r,   r   )r   r   r   r   r   D   s    z PluginTest.test_option_namespacec             C   s    |  j  d |  j j d   d  S)Nzmock-foo_barfoo_bar)r   r,   Zoption_name)r   r   r   r   test_option_nameG   s    zPluginTest.test_option_namec             C   s   |  j  d |  j j  d  S)NZmock_)r   r,   r   )r   r   r   r   r   J   s    zPluginTest.test_dest_namespacec             C   s<   |  j  d |  j j d   |  j  d |  j j d   d  S)Nmock_foo_barzfoo-barr/   )r   r,   r   )r   r   r   r   	test_destM   s    zPluginTest.test_destc             C   s&   |  j  |  j j |  j j d   d  S)Nzfoo-bar)r   r'   r1   r,   conf)r   r   r   r   	test_confQ   s    zPluginTest.test_confc             C   sE   t  j   } |  j j | d  | j j d d d d d d d  d  S)Nr)   z--mock-foo-barr   r   r   r    r!   )r)   r+   r*   Zinject_parser_optionsadd_argumentassert_called_once_with)r   parserr   r   r   test_inject_parser_optionsT   s    	z%PluginTest.test_inject_parser_optionsN)r   r   r   r   r-   r.   r   r0   r   r2   r4   r8   r   r   r   r   r   1   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   S) InstallerTestz+Tests for certbot.plugins.common.Installer.c                s{   t  t |   j   t j |  j j  d d l m } t	 j
 d    | d |  j d d  |  _ Wd  QRX|  j j |  _ d  S)Nr   )	Installerz(certbot.plugins.common.reverter.Reverterr'   r(   r:   )superr9   r-   osmkdirr'   
config_dirr   r:   r)   patch	installerreverter)r   r:   )	__class__r   r   r-   `   s    zInstallerTest.setUpc             C   s)   t  d  } d } |  j d | |  d  S)Nfoo.barbaz.quxzfoo bar baz quxadd_to_checkpoint)rC   rD   )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   |  j  d  d  S)NF)_test_add_to_checkpoint_common)r   r   r   r   test_add_to_real_checkpoint2o   s    z*InstallerTest.test_add_to_real_checkpoint2c             C   s   |  j  d  d  S)NT)rK   )r   r   r   r    test_add_to_temporary_checkpointr   s    z.InstallerTest.test_add_to_temporary_checkpointc             C   sh   t  d  } d } t j |  j j d | } | rB |  j j } n |  j j } |  j | | | |  d  S)Nfoo.barbaz.quxzfoo bar baz qux	temporary)rN   rO   )rF   	functoolspartialr@   rE   rA   Zadd_to_temp_checkpoint_test_adapted_method)r   rP   rH   rI   installer_funcreverter_funcr   r   r   rK   u   s    	z,InstallerTest._test_add_to_checkpoint_commonc             C   s   |  j  d d  d  S)NZfinalize_checkpointr   )rG   )r   r   r   r   test_finalize_checkpoint   s    z&InstallerTest.test_finalize_checkpointc             C   s   |  j  d  d  S)NZrecovery_routine)rG   )r   r   r   r   test_recovery_routine   s    z#InstallerTest.test_recovery_routinec             C   s   |  j  d  d  S)NZrevert_temporary_config)rG   )r   r   r   r   test_revert_temporary_config   s    z*InstallerTest.test_revert_temporary_configc             C   s   |  j  d d  d  S)NZrollback_checkpoints*   )rG   )r   r   r   r   test_rollback_checkpoints   s    z'InstallerTest.test_rollback_checkpointsc             C   s   |  j  d  d  S)NZview_config_changes)rG   )r   r   r   r   test_view_config_changes   s    z&InstallerTest.test_view_config_changesc             O   s>   t  |  j |  } t  |  j |  } |  j | | | |  d S)zTest 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)getattrr@   rA   rS   )r   r(   argskwargsrT   rU   r   r   r   rG      s    z"InstallerTest._test_wrapped_methodc             O   sF   | | |   | j  | |   t j | _ |  j t j | | |  d S)a  Test an adapted reverter method

        :param callable installer_func: installer method to test
        :param mock.MagicMock reverter_func: mocked adapated
            reverter method
        :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)r6   r   ZReverterErrorZside_effectassertRaisesZPluginError)r   rT   rU   Zpassed_argsZpassed_kwargsr   r   r   rS      s
    z"InstallerTest._test_adapted_methodc             C   s0   |  j  j   |  j t j j |  j  j   d  S)N)r@   Zinstall_ssl_dhparams
assertTruer<   pathisfileZssl_dhparams)r   r   r   r   test_install_ssl_dhparams   s    z'InstallerTest.test_install_ssl_dhparamsc             C   s   d d l  m } t j |  S)Nr   )SSL_DHPARAMS_SRC)certbot.constantsrd   r   	sha256sum)r   rd   r   r   r   _current_ssl_dhparams_hash   s    z(InstallerTest._current_ssl_dhparams_hashc             C   s0   d d l  m } |  j |  j   | 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.)re   rh   r`   rg   )r   rh   r   r   r   $test_current_file_hash_in_all_hashes   s    z2InstallerTest.test_current_file_hash_in_all_hashes)r   r   r   r   r-   rJ   rL   rM   rK   rV   rW   rX   rZ   r[   rG   rS   rc   rg   ri   r   r   )rB   r   r9   ]   s    
r9   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   s   d d l  m } | j d  |  _ | j d  |  _ | j d  |  _ | j d  |  _ | j d  |  _ | j d  |  _ | j d	  |  _	 | j d
  |  _
 d  S)Nr   )Addrz192.168.1.1z192.168.1.1:*z192.168.1.1:80z	[fe00::1]z[fe00::1]:*z[fe00::1]:80z[fe00::1]:5z[fe00:1:2:3:4:5:6:7:8:9]:8080)r   rk   
fromstringaddr1addr2addr3addr4addr5addr6addr7addr8)r   rk   r   r   r   r-      s    zAddrTest.setUpc             C   s  |  j  |  j j   d  |  j  |  j j   d  |  j  |  j j   d  |  j  |  j j   d  |  j  |  j j   d  |  j  |  j j   d  |  j  |  j j   d  |  j  |  j j   d  |  j  |  j j   d  |  j  |  j j   d  |  j  |  j j   d  |  j  |  j j   d  |  j  |  j j	   d  |  j  |  j j	   d  |  j  |  j
 j   d  |  j  |  j j	   d  d  S)	Nz192.168.1.1 *Z80z	[fe00::1]zfe00:0:0:0:0:0:0:15zfe00:1:2:3:4:5:6:7)r   rm   Zget_addrZget_portrn   ro   rp   rq   rr   Zget_ipv6_explodedrs   rt   )r   r   r   r   test_fromstring   s&    zAddrTest.test_fromstringc             C   s   |  j  t |  j  d  |  j  t |  j  d  |  j  t |  j  d  |  j  t |  j  d  |  j  t |  j  d  |  j  t |  j  d  d  S)Nz192.168.1.1z192.168.1.1:*z192.168.1.1:80z	[fe00::1]z[fe00::1]:*z[fe00::1]:80)r   strrm   rn   ro   rp   rq   rr   )r   r   r   r   test_str   s    zAddrTest.test_strc             C   s   |  j  t |  j j d   d  |  j  t |  j j d   d  |  j  t |  j j d   d  |  j  t |  j j d   d  |  j  t |  j j d   d  |  j  t |  j j d   d	  d  S)
NZ443z192.168.1.1:443ru   z192.168.1.1rv   z192.168.1.1:*z[fe00::1]:443z	[fe00::1]z[fe00::1]:*)r   ry   rm   get_addr_objrn   rp   rq   )r   r   r   r   test_get_addr_obj   s    """""zAddrTest.test_get_addr_objc             C   s   |  j  |  j |  j j d   |  j |  j |  j  |  j |  j d k  |  j  |  j |  j j d   |  j |  j |  j  |  j |  j d k  d d l m	 } |  j  |  j | j
 d   |  j  |  j | j
 d   d  S)Nru   i  r   )rk   z[fe00:0:0::1]z[fe00:0::0:0:1])r   rm   rn   r{   assertNotEqualassertFalserp   rq   r   rk   rl   )r   rk   r   r   r   test_eq   s    zAddrTest.test_eqc       
      C   s   d d l  m } t |  j |  j g  } | j d  } | j d  } t | | g  } |  j | |  t |  j |  j g  } | j d  } | j d  } t | | g  }	 |  j | |	  d  S)Nr   )rk   z192.168.1.1z192.168.1.1:*z	[fe00::1]z[fe00::1]:*)	r   rk   rF   rm   rn   rl   r   rp   rq   )
r   rk   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   r-   rx   rz   r|   r   r   r   r   r   r   rj      s   rj   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  j   } d d l m } | |  |  _ d  S)Nr   )ChallengePerformer)r)   r+   r   r   	performer)r   configuratorr   r   r   r   r-     s    zChallengePerformerTest.setUpc             C   sP   |  j  j t d d  |  j d t |  j  j   |  j d g |  j  j  d  S)Nr   r    )r   Z	add_challACHALLSr   lenZachallsindices)r   r   r   r   test_add_chall  s    z%ChallengePerformerTest.test_add_challc             C   s   |  j  t |  j j  d  S)N)r_   NotImplementedErrorr   Zperform)r   r   r   r   test_perform!  s    z#ChallengePerformerTest.test_performN)r   r   r   r   r-   r   r   r   r   r   r   r     s   r   c               @   sF   e  Z d  Z 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   s}   t  j   |  _ t j   } t j j |  j d  | j _	 t j j |  j d  | j _
 d d l m } | d |  |  _ d  S)Nr'   Zworkr   )TLSSNI01r   )tempfileZmkdtemptempdirr)   r+   r<   ra   joinr'   r>   Zwork_dirr   r   sni)r   r   r   r   r   r   r-   (  s    zTLSSNI01Test.setUpc             C   s   t  j |  j  d  S)N)shutilZrmtreer   )r   r   r   r   tearDown1  s    zTLSSNI01Test.tearDownc             C   sO  t  j   t  j   } } t j   } t  j   } d | j j _ t j	 d  } | t j
 d  | f f | j _ t  j d | d d > t  j d |  $ |  j | |  j j | d   Wd  QRXWd  QRX| j |  j j |  d	  | j j j t j d   | j |  j j |  d	 d
 d | j j j t j j t j j |   d  S)Nr   zrsa512_key.pemzcert_512.pemzcertbot.plugins.common.openZcreateTz%certbot.plugins.common.util.safe_openZrandomS1wbchmod   )r)   	mock_openr   ZTLSSNI01Responser+   ZchallencodeZreturn_value	test_utilZload_pyopenssl_private_keyZ	load_certZresponse_and_validationr?   r   r   Z_setup_challenge_certr6   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_cert4  s&    !		
z&TLSSNI01Test.test_setup_challenge_certc             C   sB   t  d } |  j |  j j |  | j | 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_domainS  s    
zTLSSNI01Test.test_get_z_domainN)r   r   r   r   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   S) InstallVersionControlledFileTestzATests for certbot.plugins.common.install_version_controlled_file.c                s   t  t |   j   d g |  _ t j j |  j d  |  _ t j j |  j d  |  _	 t j j |  j d  |  _
 t j j |  j d  |  _ xY |  j |  j
 f D]E } t | d   } | j |  Wd  QRX|  j j t j |   q Wd  S)NZsomeotherhashzoptions-ssl-dest.confz.options-ssl-conf.txtzoptions-ssl-old.confzoptions-ssl-src.confw)r;   r   r-   hashesr<   ra   r   r   	dest_path	hash_pathold_pathsource_pathopenr   appendr   rf   )r   ra   f)rB   r   r   r-   \  s    z&InstallVersionControlledFileTest.setUpc             C   s3   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   _callh  s
    	z&InstallVersionControlledFileTest._callc             C   s   t  j |  j  S)N)r   rf   r   )r   r   r   r   _current_file_hasho  s    z3InstallVersionControlledFileTest._current_file_hashc             C   sB   |  j  t j j |  j   |  j t j |  j  |  j    d  S)N)	r`   r<   ra   rb   r   r   r   rf   r   )r   r   r   r   _assert_current_filer  s    z5InstallVersionControlledFileTest._assert_current_filec             C   s4   |  j  t j j |  j   |  j   |  j   d  S)N)r~   r<   ra   rb   r   r   r   )r   r   r   r   test_no_filew  s    
z-InstallVersionControlledFileTest.test_no_filec             C   s/   x( t  d  D] } |  j   |  j   q Wd  S)N   )ranger   r   )r   _r   r   r   test_current_file|  s    
z2InstallVersionControlledFileTest.test_current_filec             C   s.   t  j |  j |  j  |  j   |  j   d  S)N)r   Zcopyfiler   r   r   r   )r   r   r   r   !test_prev_file_updates_to_current  s    
zBInstallVersionControlledFileTest.test_prev_file_updates_to_currentc             C   s   |  j    t |  j d   } | j d  Wd  QRXt j d  $ } |  j    |  j | j j  Wd  QRX|  j	 t
 j j |  j   |  j t j |  j  |  j    |  j t j |  j  |  j    d  S)Naza new line for the wrong hash
zcertbot.plugins.common.logger)r   r   r   r   r)   r?   r~   warningcalledr`   r<   ra   rb   r   r   rf   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   } | j d  Wd  QRXt  |  j d   } | j d  Wd  QRXt j d  / } |  j   |  j | j j	 d d d  Wd  QRX|  j t
 j |  j  |  j    t j d  $ } |  j   |  j | j j  Wd  QRXd  S)Nr   za new line for the wrong hash
r   Zhashofanoldversionzcertbot.plugins.common.loggerr   zh%s has been manually modified; updated file saved to %s. We recommend updating %s for security purposes.)r   r   r   r   r)   r?   r   r   r   	call_argsr   rf   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   r-   r   r   r   r   r   r   r   r   r   r   )rB   r   r   Y  s   r   __main__)(r   rQ   r<   r   r   ZunittestZjosepyZjoser)   r   Zacmer   Zcertbotr   r   r   Zcertbot.testsr   r   r   ZJWKRSAloadr   ZAUTH_KEYZ"KeyAuthorizationAnnotatedChallengeZchall_to_challbr   r   ZTestCaser   r   ZConfigTestCaser9   rj   r   r   ZTempDirTestCaser   r   mainr   r   r   r   <module>   s@   		,eQ4L