
    M/en8                        d 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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 ej.                  j1                   ej2                  d            Z ej6                   ej8                   e
j:                  d      ej<                        de      Z G d dej@                        Z! G d dej@                        Z" G d dejF                        Z$ G d dej@                        Z% G d dej@                        Z& G d dejN                        Z(e)dk(  r4 ejT                   ejV                  ejX                  dd e-gz                yy) z!Tests for certbot.plugins.common.    N)mock)
challenges)messages)achallenges)crypto_util)errors)
filesystem)os)	acme_util)utilzrsa512_key.pems   token1)tokenzencryption-example.demo)challbdomainaccount_keyc                   "    e Zd ZdZd Zd Zd Zy)NamespaceFunctionsTestz7Tests for certbot.plugins.common.*_namespace functions.c                 *    ddl m} d |d      k(  sJ y )Nr   )option_namespacezfoo-foo)certbot.plugins.commonr   )selfr   s     M/usr/lib/python3/dist-packages/certbot/_internal/tests/plugins/common_test.pytest_option_namespacez,NamespaceFunctionsTest.test_option_namespace   s    ;)%0000    c                 *    ddl m} d |d      k(  sJ y )Nr   dest_namespacefoo_r   r   r   r   r   s     r   test_dest_namespacez*NamespaceFunctionsTest.test_dest_namespace#   s    9....r   c                 *    ddl m} d |d      k(  sJ y )Nr   r   foo_bar_foo-barr   r    s     r   test_dest_namespace_with_dashesz6NamespaceFunctionsTest.test_dest_namespace_with_dashes'   s    9^I6666r   N)__name__
__module____qualname____doc__r   r!   r%    r   r   r   r      s    A1/7r   r   c                   F    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)
PluginTestz'Test for certbot.plugins.common.Plugin.c                     ddl m}  G d d|      }|| _        t        j                         | _         || j
                  d      | _        y )Nr   )Pluginc                   0    e Zd ZddZdefdZed        Zy)$PluginTest.setUp.<locals>.MockPluginreturnNc                      y Nr*   r   s    r   preparez,PluginTest.setUp.<locals>.MockPlugin.prepare3   s    r   c                      y)Ninfor*   r4   s    r   	more_infoz.PluginTest.setUp.<locals>.MockPlugin.more_info6   s    r   c                      |dddd        y )Nr$   different_to_foo_bar   destxyr*   )clsadds     r   add_parser_argumentsz9PluginTest.setUp.<locals>.MockPlugin.add_parser_arguments9   s    I$:a4Hr   )r1   N)r&   r'   r(   r5   strr8   classmethodrB   r*   r   r   
MockPluginr0   2   s)    3  I Ir   rE   r   configname)r   r.   
plugin_clsr   	MagicMockrG   plugin)r   r.   rE   s      r   setUpzPluginTest.setUp/   s<    1		I 		I %nn& &Ar   c                     d| j                   j                  k(  sJ | j                  | j                   j                  k(  sJ y )Nr   )rK   rH   rG   r4   s    r   	test_initzPluginTest.test_initA   s6    ))))){{dkk00000r   c                 :    d| j                   j                  k(  sJ y )Nzmock-)rK   r   r4   s    r   r   z PluginTest.test_option_namespaceE   s    $++66666r   c                 D    d| j                   j                  d      k(  sJ y )Nzmock-foo_barfoo_bar)rK   option_namer4   s    r   test_option_namezPluginTest.test_option_nameH   s    !8!8!CCCCr   c                 :    d| j                   j                  k(  sJ y )Nmock_)rK   r   r4   s    r   r!   zPluginTest.test_dest_namespaceK   s    $++44444r   c                     d| j                   j                  d      k(  sJ d| j                   j                  d      k(  sJ y )Nmock_foo_barr$   rQ   )rK   r=   r4   s    r   	test_destzPluginTest.test_destN   s<    !1!1)!<<<<!1!1)!<<<<r   c                 l    | j                   j                  | j                  j                  d      k(  sJ y )Nr$   )rG   rW   rK   confr4   s    r   	test_confzPluginTest.test_confR   s)    {{''4;;+;+;I+FFFFr   c                     t        j                         }| j                  j                  |d       |j                  j                  dddd        y )Nr   z--mock-foo-barr:   r;   r<   )r   rJ   rI   inject_parser_optionsadd_argumentassert_called_once_with)r   parsers     r   test_inject_parser_optionsz%PluginTest.test_inject_parser_optionsU   sH    !--ff= 	33#9Q$ 	4 	Hr   c                    d| j                   j                  t        j                  t        j                  g      v sJ d| j                   j                  t        j                  t        j                  t        j                  g      v sJ y )Nz8the mock plugin completed the required dns-01 challengeszDthe mock plugin completed the required dns-01 and http-01 challenges)rK   	auth_hintr   DNS01_AHTTP01_Ar4   s    r   test_fallback_auth_hintz"PluginTest.test_fallback_auth_hint]   s    Ikk++Y->->	@Q@Q,RST 	T TUkk++Y->->	@R@R-6->->-@ AA 	A Ar   N)r&   r'   r(   r)   rL   rN   r   rS   r!   rX   r[   ra   rf   r*   r   r   r,   r,   ,   s7    1B$17D5=GHAr   r,   c                   p     e Zd ZdZ fdZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Z xZS )InstallerTestz+Tests for certbot.plugins.common.Installer.c                     t         |           t        j                  | j                  j
                         ddlm}  || j                  d      | _        | j                  j                  | _	        y )Nr   )DummyInstaller	InstallerrF   )
superrL   r	   mkdirrG   
config_dircertbot.tests.utilrj   	installerreverter)r   rj   	__class__s     r   rL   zInstallerTest.setUph   sM    //05't{{(35//r   c                 6    ddh}d}| j                  d||       y )Nfoo.barbaz.quxfoo bar baz quxadd_to_checkpoint_test_wrapped_method)r   files
save_notess      r   test_add_to_real_checkpointz)InstallerTest.test_add_to_real_checkpointq   s$    I'&
!!"5ujIr   c                 &    | j                  d       y )NF_test_add_to_checkpoint_commonr4   s    r   test_add_to_real_checkpoint2z*InstallerTest.test_add_to_real_checkpoint2v   s    ++E2r   c                 &    | j                  d       y )NTr~   r4   s    r    test_add_to_temporary_checkpointz.InstallerTest.test_add_to_temporary_checkpointy   s    ++D1r   c                     ddh}d}t        j                  | j                  j                  |      }|rd}nd}| j	                  ||||       y )Nrt   ru   rv   )	temporaryadd_to_temp_checkpointrw   )	functoolspartialrp   rw   _test_adapted_method)r   r   rz   r{   installer_funcreverter_func_names         r   r   z,InstallerTest._test_add_to_checkpoint_common|   sW    I'&
"**4>>+K+K5>@ !9!4!!.2DeZXr   c                 (    | j                  dd       y )Nfinalize_checkpointr   rx   r4   s    r   test_finalize_checkpointz&InstallerTest.test_finalize_checkpoint   s    !!"7?r   c                 &    | j                  d       y )Nrecovery_routinerx   r4   s    r   test_recovery_routinez#InstallerTest.test_recovery_routine   s    !!"45r   c                 &    | j                  d       y )Nrevert_temporary_configrx   r4   s    r   test_revert_temporary_configz*InstallerTest.test_revert_temporary_config   s    !!";<r   c                 (    | j                  dd       y )Nrollback_checkpoints*   rx   r4   s    r   test_rollback_checkpointsz'InstallerTest.test_rollback_checkpoints   s    !!"8"=r   c                 ^    t        | j                  |      } | j                  ||g|i | y)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)getattrrp   r   )r   rH   argskwargsr   s        r   ry   z"InstallerTest._test_wrapped_method   s0     !6!!!.$HHHr   c                 `   t         j                  j                  | j                  |      5 } ||i |  |j                  |i | t
        j                  |_        t        j                  t
        j                        5   ||i | ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)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objectrq   r_   r   ReverterErrorside_effectpytestraisesPluginError)r   r   r   passed_argspassed_kwargsreverter_funcs         r   r   z"InstallerTest._test_adapted_method   s     ZZt}}.@A 	>]K9=91M11;P-P(.(<(<M%v112 >=}=>		> 	>> >		> 	>s$   AB$>	BB$B!	B$$B-c                     | j                   j                          t        j                  j	                  | j                   j
                        sJ y r3   )rp   install_ssl_dhparamsr
   pathisfilessl_dhparamsr4   s    r   test_install_ssl_dhparamsz'InstallerTest.test_install_ssl_dhparams   s2    ++-ww~~dnn99:::r   c                 8    ddl m} t        j                  |      S )Nr   )SSL_DHPARAMS_SRC)certbot._internal.constantsr   r   	sha256sum)r   r   s     r   _current_ssl_dhparams_hashz(InstallerTest._current_ssl_dhparams_hash   s    @$$%566r   c                 B    ddl m} | j                         |v sJ d       y )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.)r   r   r   )r   r   s     r   $test_current_file_hash_in_all_hashesz2InstallerTest.test_current_file_hash_in_all_hashes   s,    G..04KK 	TT	TKr   )r&   r'   r(   r)   rL   r|   r   r   r   r   r   r   r   ry   r   r   r   r   __classcell__rr   s   @r   rh   rh   e   sQ    50J
32Y@6=>	I>(;7Tr   rh   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	AddrTestz&Tests for certbot.plugins.common.Addr.c                 p   ddl m} |j                  d      | _        |j                  d      | _        |j                  d      | _        |j                  d      | _        |j                  d      | _        |j                  d      | _        |j                  d	      | _	        |j                  d
      | _
        y )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   s     r   rL   zAddrTest.setUp   s    /__]3
___5
__%56
__[1
__]3
__^4
__]3
__%DE
r   c                    | j                   j                         dk(  sJ | j                   j                         dk(  sJ | j                  j                         dk(  sJ | j                  j                         dk(  sJ | j                  j                         dk(  sJ | j                  j                         dk(  sJ | j
                  j                         dk(  sJ | j
                  j                         dk(  sJ | j                  j                         dk(  sJ | j                  j                         dk(  sJ | j                  j                         dk(  sJ | j                  j                         dk(  sJ | j                  j                         dk(  sJ | j                   j                         dk(  sJ | j                  j                         dk(  sJ | j                  j                         dk(  sJ y )	Nr    *80r   zfe00:0:0:0:0:0:0:15zfe00:1:2:3:4:5:6:7)r   get_addrget_portr   r   r   r   r   get_ipv6_explodedr   r   r4   s    r   test_fromstringzAddrTest.test_fromstring   s   zz""$555zz""$***zz""$555zz""$+++zz""$555zz""$,,,zz""$333zz""$***zz""$333zz""$+++zz""$333zz""$,,,zz++--. 	. .zz++- 	 zz""$+++zz++--. 	. .r   c                 <   t        | j                        dk(  sJ t        | j                        dk(  sJ t        | j                        dk(  sJ t        | j                        dk(  sJ t        | j
                        dk(  sJ t        | j                        dk(  sJ y )Nr   r   r   r   r   r   )rC   r   r   r   r   r   r   r4   s    r   test_strzAddrTest.test_str   s    4::-///4::/1114::"22224::+---4::-///4::.000r   c                    t        | j                  j                  d            dk(  sJ t        | j                  j                  d            dk(  sJ t        | j                  j                  d            dk(  sJ t        | j                  j                  d            dk(  sJ t        | j
                  j                  d            dk(  sJ t        | j                  j                  d            d	k(  sJ y )
N443z192.168.1.1:443r   r   r   r   z[fe00::1]:443r   r   )rC   r   get_addr_objr   r   r   r4   s    r   test_get_addr_objzAddrTest.test_get_addr_obj   s    4::**5126GGGG4::**2./=@@@4::**3/0OCCC4::**512oEEE4::**2./;>>>4::**3/0MAAAr   c                    | j                   | j                  j                  d      k(  sJ | j                   | j                  k7  sJ | j                   dk7  sJ | j                  | j                  j                  d      k(  sJ | j                  | j                  k7  sJ | j                  dk7  sJ ddlm} | j                  |j                  d      k(  sJ | j                  |j                  d      k(  sJ y )Nr   i  r   r   z[fe00:0:0::1]z[fe00:0::0:0:1])r   r   r   r   r   r   r   r   r   s     r   test_eqzAddrTest.test_eq   s    zzTZZ44R8888zzTZZ'''zzT!!!zzTZZ44R8888zzTZZ'''zzT!!!/zzT___====zzT__->????r   c                 $   ddl m} | j                  | j                  h}|j	                  d      }|j	                  d      }||h}||k(  sJ | j
                  | j                  h}|j	                  d      }|j	                  d      }||h}	||	k(  sJ y )Nr   r   r   r   r   r   )r   r   r   r   r   r   r   )
r   r   set_aaddr1baddr2bset_bset_caddr4baddr5bset_ds
             r   test_set_inclusionzAddrTest.test_set_inclusion  s    /TZZ(/1 ~~TZZ(-/ ~~r   N)
r&   r'   r(   r)   rL   r   r   r   r   r   r*   r   r   r   r      s&    0	F.*1B
@r   r   c                   "    e Zd ZdZd Zd Zd Zy)ChallengePerformerTestz4Tests for certbot.plugins.common.ChallengePerformer.c                 R    t        j                         }ddlm}  ||      | _        y )Nr   )ChallengePerformer)r   rJ   r   r   	performer)r   configuratorr   s      r   rL   zChallengePerformerTest.setUp  s    ~~'=+L9r   c                     | j                   j                  t        d       dt        | j                   j                        k(  sJ dg| j                   j
                  k(  sJ y )Nr   r;   )r   	add_challACHALLlenachallsindicesr4   s    r   test_add_challz%ChallengePerformerTest.test_add_chall  sM      +C..////sdnn,,,,,r   c                     t        j                  t              5  | j                  j	                          d d d        y # 1 sw Y   y xY wr3   )r   r   NotImplementedErrorr   performr4   s    r   test_performz#ChallengePerformerTest.test_perform#  s4    ]]./ 	%NN""$	% 	% 	%s	   >AN)r&   r'   r(   r)   rL   r   r   r*   r   r   r   r     s    >:-
%r   r   c                   R     e Zd ZdZ fdZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Z xZS ) InstallVersionControlledFileTestzATests for certbot.plugins.common.install_version_controlled_file.c                    t         |           dg| _        t        j                  j                  | j                  d      | _        t        j                  j                  | j                  d      | _        t        j                  j                  | j                  d      | _	        t        j                  j                  | j                  d      | _
        | j                  | j                  fD ]V  }t        |d      5 }|j                  |       d d d        | j                  j                  t        j                  |             X y # 1 sw Y   :xY w)Nsomeotherhashzoptions-ssl-dest.confz.options-ssl-conf.txtzoptions-ssl-old.confzoptions-ssl-src.confw)rl   rL   hashesr
   r   jointempdir	dest_path	hash_pathold_pathsource_pathopenwriteappendr   r   )r   r   frr   s      r   rL   z&InstallVersionControlledFileTest.setUp+  s    &'dll4KLdll4KLT\\3IJ77<<6LM%%t}}6 	<DdC AKK{44T:;	< s   ;EE	c                 v    ddl m}  || j                  | j                  | j                  | j
                         y )Nr   )install_version_controlled_file)r   r	  r   r  r  r   )r   r	  s     r   _callz&InstallVersionControlledFileTest._call7  s*    J'(,(,(8(8(,	5r   c                 @    t        j                  | j                        S r3   )r   r   r  r4   s    r   _current_file_hashz3InstallVersionControlledFileTest._current_file_hash>  s    $$T%5%566r   c                     t         j                  j                  | j                        sJ t	        j
                  | j                        | j                         k(  sJ y r3   )r
   r   r   r   r   r   r  r4   s    r   _assert_current_filez5InstallVersionControlledFileTest._assert_current_fileA  sI    ww~~dnn---$$T^^4##%& 	& &r   c                     t         j                  j                  | j                        rJ | j	                          | j                          y r3   )r
   r   r   r   r
  r  r4   s    r   test_no_filez-InstallVersionControlledFileTest.test_no_fileF  s1    77>>$..111

!!#r   c                 d    t        d      D ]"  }| j                          | j                          $ y )N   )ranger
  r  )r   _s     r   test_current_filez2InstallVersionControlledFileTest.test_current_fileK  s*    q 	(AJJL%%'	(r   c                     t        j                  | j                  | j                         | j	                          | j                          y r3   )shutilcopyfiler  r   r
  r  r4   s    r   !test_prev_file_updates_to_currentzBInstallVersionControlledFileTest.test_prev_file_updates_to_currentQ  s-    t~~6

!!#r   c                 d   | j                          t        | j                  d      5 }|j                  d       d d d        t	        j
                  d      5 }| j                          |j                  j                  du sJ 	 d d d        t        j                  j                  | j                        sJ t        j                  | j                        | j                         k(  sJ t        j                  | j                        | j                         k7  sJ y # 1 sw Y   xY w# 1 sw Y   xY w)Naa new line for the wrong hash
certbot.plugins.common.loggerF)r
  r  r   r  r   r   warningcalledr
   r   r   r   r   r  r  )r   mod_ssl_confmock_loggers      r   3test_manually_modified_current_file_does_not_updatezTInstallVersionControlledFileTest.test_manually_modified_current_file_does_not_updateV  s   

$..#& 	B,@A	BZZ78 	7KJJL&&--666	7 ww~~dnn---$$T%5%56##%& 	& &$$T^^4##%& 	& &	B 	B	7 	7s   D+D&D#&D/c                    t        | j                  d      5 }|j                  d       d d d        t        | j                  d      5 }|j                  d       d d d        t	        j
                  d      5 }| j                          |j                  j                  d   d   dk(  sJ 	 d d d        t        j                  | j                        | j                         k(  sJ t	        j
                  d      5 }| j                          |j                  j                  du sJ 	 d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)	Nr  r  r   hashofanoldversionr  r   zh%s has been manually modified; updated file saved to %s. We recommend updating %s for security purposes.F)r  r   r  r  r   r   r
  r  	call_argsr   r   r  r  r  )r   r   r  r!  s       r   &test_manually_modified_past_file_warnszGInstallVersionControlledFileTest.test_manually_modified_past_file_warnsc  sM   $..#& 	B,@A	B$..#& 	*!GG()	*ZZ78 	OKJJL&&003A6OO O O	O
 $$T%5%56##%& 	& & ZZ78 	7KJJL&&--666	7 	7	B 	B	* 	*	O 	O	7 	7s/   D-D:62E8+E-D7:EEE)r&   r'   r(   r)   rL   r
  r  r  r  r  r  r"  r&  r   r   s   @r   r   r   (  s3    K
<57&
$
($
&7r   r   __main__r;   ).r)   r   r  sysunittestr   josepyjoser   acmer   r   certbotr   r   r   certbot.compatr	   r
   certbot.testsr   r   	test_utilJWKRSAloadload_vectorAUTH_KEY"KeyAuthorizationAnnotatedChallengechall_to_challbHTTP01STATUS_PENDINGr   TestCaser   r,   ConfigTestCaserh   r   r   TempDirTestCaser   r&   exitmainargv__file__r*   r   r   <module>r@     sP   '   
          %  # +;;1I112BCD	7	7	7,9,,->Z->->Y-O-5-D-DF,(
D7X.. 7 6A"" 6Ar\TI,, \T~Nx   Nb%X.. %&J7y'@'@ J7X zCHH[V[[!"
234 r   