a }|ägn8ã@szdZddlZddlZddlZddlZddlmZddlZddlZddl m Z ddl m Z ddl m Z ddl mZddl mZdd lmZdd lmZdd lmZdd lmZej e d ¡¡Ze je e jdde j¡dedZGdd„dej ƒZ!Gdd„dej ƒZ"Gdd„dej#ƒZ$Gdd„dej ƒZ%Gdd„dej ƒZ&Gdd„dej'ƒZ(e)dkrve *e +ej,dd…e-g¡¡dS) z!Tests for certbot.plugins.common.éN)Úmock)Ú challenges)Úmessages)Ú achallenges)Ú crypto_util)Úerrors)Ú filesystem)Úos)Ú acme_util)Úutilzrsa512_key.pemstoken1)Útokenzencryption-example.demo)ZchallbÚdomainZ account_keyc@s(eZdZdZdd„Zdd„Zdd„ZdS) ÚNamespaceFunctionsTestz7Tests for certbot.plugins.common.*_namespace functions.cCs ddlm}d|dƒksJ‚dS)Nr)Úoption_namespacezfoo-Úfoo)Úcertbot.plugins.commonr)Úselfr©rúO/usr/lib/python3.9/site-packages/certbot/_internal/tests/plugins/common_test.pyÚtest_option_namespaces z,NamespaceFunctionsTest.test_option_namespacecCs ddlm}d|dƒksJ‚dS)Nr©Údest_namespaceZfoo_r©rr©rrrrrÚtest_dest_namespace#s z*NamespaceFunctionsTest.test_dest_namespacecCs ddlm}d|dƒksJ‚dS)NrrZfoo_bar_úfoo-barrrrrrÚtest_dest_namespace_with_dashes's z6NamespaceFunctionsTest.test_dest_namespace_with_dashesN)Ú__name__Ú __module__Ú __qualname__Ú__doc__rrrrrrrrsrc@sXeZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z dd„Z dS)Ú PluginTestz'Test for certbot.plugins.common.Plugin.cCs@ddlm}Gdd„d|ƒ}||_t ¡|_||jdd|_dS)Nr)ÚPluginc@s4eZdZddœdd„Zedœdd„Zedd„ƒZdS) z$PluginTest.setUp..MockPluginN)ÚreturncSsdS©Nr©rrrrÚprepare3sz,PluginTest.setUp..MockPlugin.preparecSsdS)NÚinforr%rrrÚ more_info6sz.PluginTest.setUp..MockPlugin.more_infocSs|ddddddS)NrÚdifferent_to_foo_baré©ÚdestÚxÚyr)ÚclsÚaddrrrÚadd_parser_arguments9sz9PluginTest.setUp..MockPlugin.add_parser_arguments)rrrr&Ústrr(Ú classmethodr1rrrrÚ MockPlugin2sr4r©ÚconfigÚname)rr"Ú plugin_clsrÚ MagicMockr6Úplugin)rr"r4rrrÚsetUp/s    zPluginTest.setUpcCs&d|jjksJ‚|j|jjks"J‚dS)Nr)r:r7r6r%rrrÚ test_initAszPluginTest.test_initcCsd|jjksJ‚dS)Nzmock-)r:rr%rrrrEsz PluginTest.test_option_namespacecCsd|j d¡ksJ‚dS)Nz mock-foo_barÚfoo_bar)r:Z option_namer%rrrÚtest_option_nameHszPluginTest.test_option_namecCsd|jjksJ‚dS)NZmock_)r:rr%rrrrKszPluginTest.test_dest_namespacecCs,d|j d¡ksJ‚d|j d¡ks(J‚dS)NÚ mock_foo_barrr=)r:r,r%rrrÚ test_destNszPluginTest.test_destcCs|jj|j d¡ksJ‚dS)Nr)r6r?r:Zconfr%rrrÚ test_confRszPluginTest.test_confcCs.t ¡}|j |d¡|jjddddddS)Nrz--mock-foo-barr)r*r+)rr9r8Zinject_parser_optionsÚ add_argumentÚassert_called_once_with)rÚparserrrrÚtest_inject_parser_optionsUs ÿz%PluginTest.test_inject_parser_optionscCs@d|j tjtjg¡vsJ‚d|j tjtjtjg¡vsrr@rArErFrrrrr!,sr!csˆeZdZdZ‡fdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd„Z‡ZS)Ú InstallerTestz+Tests for certbot.plugins.common.Installer.csBtƒ ¡t |jj¡ddlm}||jdd|_|jj |_ dS)Nr)ÚDummyInstallerZ Installerr5) Úsuperr;rÚmkdirr6Z config_dirZcertbot.tests.utilrHÚ installerÚreverter)rrH©Ú __class__rrr;hs  ÿzInstallerTest.setUpcCsddh}d}| d||¡dS)Núfoo.barúbaz.quxúfoo bar baz quxÚadd_to_checkpoint©Ú_test_wrapped_method)rÚfilesÚ save_notesrrrÚtest_add_to_real_checkpointqsz)InstallerTest.test_add_to_real_checkpointcCs| d¡dS)NF©Ú_test_add_to_checkpoint_commonr%rrrÚtest_add_to_real_checkpoint2vsz*InstallerTest.test_add_to_real_checkpoint2cCs| d¡dS)NTrXr%rrrÚ test_add_to_temporary_checkpointysz.InstallerTest.test_add_to_temporary_checkpointcCs@ddh}d}tj|jj|d}|r(d}nd}| ||||¡dS)NrOrPrQ)Ú temporaryZadd_to_temp_checkpointrR)Ú functoolsÚpartialrKrRÚ_test_adapted_method)rr\rUrVÚinstaller_funcÚreverter_func_namerrrrY|s ÿz,InstallerTest._test_add_to_checkpoint_commoncCs| dd¡dS)NZfinalize_checkpointrrSr%rrrÚtest_finalize_checkpointŠsz&InstallerTest.test_finalize_checkpointcCs| d¡dS)NZrecovery_routinerSr%rrrÚtest_recovery_routinesz#InstallerTest.test_recovery_routinecCs| d¡dS)NZrevert_temporary_configrSr%rrrÚtest_revert_temporary_configsz*InstallerTest.test_revert_temporary_configcCs| dd¡dS)NZrollback_checkpointsé*rSr%rrrÚtest_rollback_checkpoints“sz'InstallerTest.test_rollback_checkpointscOs*t|j|ƒ}|j||g|¢Ri|¤ŽdS)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)ÚgetattrrKr_)rr7ÚargsÚkwargsr`rrrrT–s z"InstallerTest._test_wrapped_methodc Os”tj |j|¡p}||i|¤Ž|j|i|¤Žtj|_t  tj ¡||i|¤ŽWdƒn1sh0YWdƒn1s†0YdS)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ÚobjectrLrCrZ ReverterErrorZ side_effectÚpytestÚraisesZ PluginError)rr`raZ passed_argsZ passed_kwargsZ reverter_funcrrrr_¡s z"InstallerTest._test_adapted_methodcCs"|j ¡tj |jj¡sJ‚dSr$)rKZinstall_ssl_dhparamsr ÚpathÚisfileZ ssl_dhparamsr%rrrÚtest_install_ssl_dhparamsµs z'InstallerTest.test_install_ssl_dhparamscCsddlm}t |¡S)Nr)ÚSSL_DHPARAMS_SRC)Úcertbot._internal.constantsrqrÚ sha256sum)rrqrrrÚ_current_ssl_dhparams_hash¹s z(InstallerTest._current_ssl_dhparams_hashcCs$ddlm}| ¡|vs Jdƒ‚dS)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.)rrrurt)rrurrrÚ$test_current_file_hash_in_all_hashes½s ÿz2InstallerTest.test_current_file_hash_in_all_hashes)rrrr r;rWrZr[rYrbrcrdrfrTr_rprtrvÚ __classcell__rrrMrrGes  rGc@s@eZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dS)ÚAddrTestz&Tests for certbot.plugins.common.Addr.cCspddlm}| d¡|_| d¡|_| d¡|_| d¡|_| d¡|_| d¡|_| d ¡|_ | d ¡|_ dS) 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) rrzÚ fromstringÚaddr1Úaddr2Úaddr3Úaddr4Úaddr5Úaddr6Úaddr7Úaddr8©rrzrrrr;Çs        zAddrTest.setUpcCs(|j ¡dksJ‚|j ¡dks$J‚|j ¡dks6J‚|j ¡dksHJ‚|j ¡dksZJ‚|j ¡dkslJ‚|j ¡dks~J‚|j ¡dksJ‚|j ¡dks¢J‚|j ¡dks´J‚|j ¡dksÆJ‚|j ¡dksØJ‚|j ¡dksêJ‚|j ¡dksüJ‚|j  ¡dksJ‚|j  ¡dks$J‚dS) Nr{ÚÚ*Z80r~zfe00:0:0:0:0:0:0:1Ú5zfe00:1:2:3:4:5:6:7) r‚Zget_addrZget_portrƒr„r…r†r‡Zget_ipv6_explodedrˆr‰r%rrrÚtest_fromstringÒs,ÿÿÿzAddrTest.test_fromstringcCspt|jƒdksJ‚t|jƒdks$J‚t|jƒdks6J‚t|jƒdksHJ‚t|jƒdksZJ‚t|jƒdkslJ‚dS)Nr{r|r}r~rr€)r2r‚rƒr„r…r†r‡r%rrrÚtest_strçs zAddrTest.test_strcCs”t|j d¡ƒdksJ‚t|j d¡ƒdks0J‚t|j d¡ƒdksHJ‚t|j d¡ƒdks`J‚t|j d¡ƒdksxJ‚t|j d¡ƒd ksJ‚dS) NZ443z192.168.1.1:443r‹r{rŒr|z [fe00::1]:443r~r)r2r‚Ú get_addr_objrƒr…r†r%rrrÚtest_get_addr_objïs zAddrTest.test_get_addr_objcCs |j|j d¡ksJ‚|j|jks&J‚|jdks4J‚|j|j d¡ksJJ‚|j|jksZJ‚|jdkshJ‚ddlm}|j| d¡ksˆJ‚|j| d¡ksœJ‚dS)Nr‹i rryz [fe00:0:0::1]z[fe00:0::0:0:1])r‚rƒrr…r†rrzrrŠrrrÚtest_eq÷s zAddrTest.test_eqc Csxddlm}|j|jh}| d¡}| d¡}||h}||ks@J‚|j|jh}| d¡}| d¡}||h} || kstJ‚dS)Nrryr{r|r~r)rrzr‚rƒrr…r†) rrzZset_aZaddr1bZaddr2bZset_bZset_cZaddr4bZaddr5bZset_drrrÚtest_set_inclusions        zAddrTest.test_set_inclusionN) rrrr r;rŽrr‘r’r“rrrrrxÄs  rxc@s(eZdZdZdd„Zdd„Zdd„ZdS) ÚChallengePerformerTestz4Tests for certbot.plugins.common.ChallengePerformer.cCs"t ¡}ddlm}||ƒ|_dS)Nr)ÚChallengePerformer)rr9rr•Ú performer)rZ configuratorr•rrrr;s zChallengePerformerTest.setUpcCs8|j td¡dt|jjƒks"J‚dg|jjks4J‚dS)Nrr*)r–Z add_challÚACHALLÚlenZachallsÚindicesr%rrrÚtest_add_challsz%ChallengePerformerTest.test_add_challcCs8t t¡|j ¡Wdƒn1s*0YdSr$)rlrmÚNotImplementedErrorr–Zperformr%rrrÚ test_perform#s z#ChallengePerformerTest.test_performN)rrrr r;ršrœrrrrr”sr”cs`eZdZdZ‡fdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z dd„Z ‡Z S)Ú InstallVersionControlledFileTestzATests for certbot.plugins.common.install_version_controlled_file.c s¶tƒ ¡dg|_tj |jd¡|_tj |jd¡|_tj |jd¡|_ tj |jd¡|_ |j |j fD]J}t |dƒ}|  |¡Wdƒn1s”0Y|j  t |¡¡qfdS)NZ someotherhashzoptions-ssl-dest.confz.options-ssl-conf.txtzoptions-ssl-old.confzoptions-ssl-src.confÚw)rIr;Úhashesr rnÚjoinZtempdirÚ dest_pathÚ hash_pathÚold_pathÚ source_pathÚopenÚwriteÚappendrrs)rrnÚfrMrrr;+s  (z&InstallVersionControlledFileTest.setUpcCs&ddlm}||j|j|j|jƒdS)Nr)Úinstall_version_controlled_file)rr©r¡r¢r¤rŸ)rr©rrrÚ_call7s  ýz&InstallVersionControlledFileTest._callcCs t |j¡Sr$)rrsr¤r%rrrÚ_current_file_hash>sz3InstallVersionControlledFileTest._current_file_hashcCs.tj |j¡sJ‚t |j¡| ¡ks*J‚dSr$)r rnror¡rrsr«r%rrrÚ_assert_current_fileAs ÿz5InstallVersionControlledFileTest._assert_current_filecCs&tj |j¡rJ‚| ¡| ¡dSr$)r rnror¡rªr¬r%rrrÚ test_no_fileFsz-InstallVersionControlledFileTest.test_no_filecCs"tdƒD]}| ¡| ¡qdS)Né)Úrangerªr¬)rÚ_rrrÚtest_current_fileKs z2InstallVersionControlledFileTest.test_current_filecCs$t |j|j¡| ¡| ¡dSr$)ÚshutilÚcopyfiler£r¡rªr¬r%rrrÚ!test_prev_file_updates_to_currentQszBInstallVersionControlledFileTest.test_prev_file_updates_to_currentcCsÆ| ¡t|jdƒ}| d¡Wdƒn1s40Yt d¡(}| ¡|jjdusbJ‚Wdƒn1sv0Ytj   |j¡s’J‚t   |j ¡| ¡ksªJ‚t   |j¡| ¡ksÂJ‚dS)NÚaúa new line for the wrong hash úcertbot.plugins.common.loggerF)rªr¥r¡r¦rrjÚwarningÚcalledr rnrorrsr¤r«)rÚ mod_ssl_confÚ mock_loggerrrrÚ3test_manually_modified_current_file_does_not_updateVs( . ÿ ÿzTInstallVersionControlledFileTest.test_manually_modified_current_file_does_not_updatecCst|jdƒ}| d¡Wdƒn1s,0Yt|jdƒ}| d¡Wdƒn1sb0Yt d¡0}| ¡|jjdddks˜J‚Wdƒn1s¬0Yt   |j ¡|  ¡ksÎJ‚t d¡(}| ¡|jj dusòJ‚Wdƒn1s0YdS) Nrµr¶ržZhashofanoldversionr·rzh%s has been manually modified; updated file saved to %s. We recommend updating %s for security purposes.F)r¥r¡r¦r¢rrjrªr¸Z call_argsrrsr¤r«r¹)rrºr¨r»rrrÚ&test_manually_modified_past_file_warnscs(( ÿ& ÿ zGInstallVersionControlledFileTest.test_manually_modified_past_file_warns)rrrr r;rªr«r¬r­r±r´r¼r½rwrrrMrr(s  rÚ__main__r*).r r]r²ÚsysZunittestrZjosepyZjoserlZacmerrZcertbotrrrZcertbot.compatrr Z certbot.testsr r Z test_utilZJWKRSAÚloadZ load_vectorZAUTH_KEYZ"KeyAuthorizationAnnotatedChallengeZchall_to_challbZHTTP01ZSTATUS_PENDINGr—ZTestCaserr!ZConfigTestCaserGrxr”ZTempDirTestCaserrÚexitÚmainÚargvÚ__file__rrrrÚs>          ÿý9_QL