a }|äg<ã@s(dZddlmZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl Z ddl Z ddlmZddlmZddlmZddlmZdd lmZdd lmZdd lmZdd lmZe j e d ¡¡ZGd d„de j ƒZ!Gdd„de j ƒZ"e#dkr$e $e  %ej&dd…e'g¡¡dS)z,Tests for certbot._internal.plugins.webroot.é)Úprint_functionN)Úmock)Ú challenges)Ú achallenges)Úerrors)Ú filesystem)Úos)Úutil)Ú acme_utilzrsa512_key.pemc@sNeZdZdZejejdedZ dd„Z dd„Z dd „Z d d „Z d d „Ze ¡dd„ƒZejejdde ¡dd„ƒƒZejejdde ¡dd„ƒƒZejejdddd„ƒZe ¡dd„ƒZe ¡dd„ƒZe ¡dd„ƒZdd„Zd d!„Ze d"¡d#d$„ƒZ e ¡d%d&„ƒZ!d'd(„Z"d)d*„Z#d+d,„Z$d-d.„Z%d/d0„Z&e d1¡d2d3„ƒZ'd4S)5ÚAuthenticatorTestz:Tests for certbot._internal.plugins.webroot.Authenticator.ú thing.com©ZchallbÚdomainZ account_keycCs–ddlm}t ¡|_tj |jd¡|_t  |j¡tj |jd¡|_ tj |jdd¡|_ tj |j d¡|_ t j|jd|jid|_||jdƒ|_dS) Nr©Ú AuthenticatorÚwebrootú .well-knownúacme-challengeZ:ZXZhR3hmQURzNnBTUmIyTEF2OUlaZjE3RHQzanV4R0orUEN0OTJ3citvQQr )Ú webroot_pathÚ webroot_map)Ú!certbot._internal.plugins.webrootrÚtempfileÚmkdtempZ workspacerÚpathÚjoinrÚmkdirÚpartial_root_challenge_pathÚroot_challenge_pathÚvalidation_pathrÚ MagicMockÚconfigÚauth©Úselfr©r$úP/usr/lib/python3.9/site-packages/certbot/_internal/tests/plugins/webroot_test.pyÚsetUp#s$   ÿÿþÿzAuthenticatorTest.setUpcCst |j¡dS©N)ÚshutilÚrmtreer©r#r$r$r%ÚtearDown8szAuthenticatorTest.tearDowncCs|j ¡}dSr')r!Ú more_info)r#r,r$r$r%Útest_more_info;s z AuthenticatorTest.test_more_infocCst ¡}|j |¡dSr')rrr!Zadd_parser_arguments)r#Úaddr$r$r%Útest_add_parser_arguments@s z+AuthenticatorTest.test_add_parser_argumentscCs|j ¡dSr')r!Úpreparer*r$r$r%Ú test_prepareEszAuthenticatorTest.test_preparecsLg|j_d|ji|j_|ƒ}tjdf|j_|j  |j g¡|jj D]‰qBdS)Núotherthing.comé) r rrrÚ display_utilÚOKÚmenuÚ return_valuer!ÚperformÚachallÚcall_args_list©r#Úmock_get_utilityÚ mock_displayr$©Úcallr%Útest_webroot_from_listHs z(AuthenticatorTest.test_webroot_from_listzTest specific to Windows)ÚreasoncCs8|ƒ}tjdf|j_|j |jg¡|j |jg¡dS)Nr3)r4r5r6r7r!r8r9Úcleanupr;r$r$r%Ú(test_webconfig_file_generate_and_cleanupYs z:AuthenticatorTest.test_webconfig_file_generate_and_cleanupc Cs¨|ƒ}tjdf|j_tj |jdd¡}t |¡tj |d¡}t |dƒ}|  d¡Wdƒn1sh0Y|j   |j g¡ddlm}| |¡}dd lm}dS) Nr3rrú web.configÚwÚ somethingr)Ú crypto_util)Ú_WEB_CONFIG_SHA256SUMS)r4r5r6r7rrrrÚmakedirsÚopenÚwriter!r8r9ÚcertbotrGZ sha256sumrrH) r#r<r=Úchallenge_pathÚwebconfig_pathÚfilerGZwebconfig_hashrHr$r$r%Ú$test_foreign_webconfig_file_handlingds  (   z6AuthenticatorTest.test_foreign_webconfig_file_handlingcCs¢tjt tjddd¡dtd}|j|jj d<t j  |jdd¡}t   |¡t j  |d¡}t|d ƒ}| d ¡Wdƒn1s‚0Y|j |j|g¡dS) Nóbingo©ÚtokenÚpendingzsecond-thing.comr rrrDrErF)rÚ"KeyAuthorizationAnnotatedChallenger Úchall_to_challbrÚHTTP01ÚKEYrr rrrrrIrJrKr!r8r9)r#Zachall_2rMrNrOr$r$r%Ú'test_foreign_webconfig_multiple_domainsvsþ  (z9AuthenticatorTest.test_foreign_webconfig_multiple_domainscszg|j_d|ji|j_|ƒ}tjdff|j_t  t j ¡ |j   |jg¡Wdƒn1s^0Y|jjD]‰qpdS)Nr2éÿÿÿÿ)r rrrr4ÚCANCELr6Ú side_effectÚpytestÚraisesrÚ PluginErrorr!r8r9r:r;r$r>r%Ú&test_webroot_from_list_help_and_cancel†s. z8AuthenticatorTest.test_webroot_from_list_help_and_cancelcCs€g|j_d|ji|j_|ƒ}tjdf|j_t  d¡8}tj dftj|jff|_ |j   |jg¡Wdƒn1sr0YdS)Nú something.comrú'certbot.display.ops.validated_directoryrZ)r rrrr4r5r6r7rÚpatchr[r\r!r8r9©r#r<r=Úmr$r$r%Útest_new_webroot–s  ÿ.z"AuthenticatorTest.test_new_webrootc Csšg|j_i|j_|ƒ}tjdf|j_t d¡X}tj df|_t   t j ¡ |j |jg¡Wdƒn1sn0YWdƒn1sŒ0YdS)NrrbrZ)r rrr4r5r6r7rrcr[r]r^rr_r!r8r9rdr$r$r%Ú!test_new_webroot_empty_map_cancel¥s  z3AuthenticatorTest.test_new_webroot_empty_map_cancelcCsLd|j_i|j_t tj¡|j g¡Wdƒn1s>0YdSr') r rrr]r^rr_r!r8r*r$r$r%Útest_perform_missing_root±sz+AuthenticatorTest.test_perform_missing_rootc Csútj |jd¡|j_tj |jd¡}t|dƒ}| d¡Wdƒn1sN0Yt |jd¡z6t|dƒWdƒn1sˆ0Yt dƒWnJt yæt   t j¡|j g¡Wdƒn1sØ0YYn0t |jd¡dS) NZnullZrndrEZthingimyrÚrz9Warning, running tests as root skips permissions tests...iÀ)rrrr!Ú full_pathrJrKrÚchmodÚprintÚOSErrorr]r^rr_r8)r#Zpermission_canaryÚfr$r$r%Ú"test_perform_reraises_other_errors·s (   0z4AuthenticatorTest.test_perform_reraises_other_errorszJcertbot._internal.plugins.webroot.filesystem.copy_ownership_and_apply_modecCs"ttjdƒ|_|j |jg¡dS)NÚmsg)rmÚerrnoÚEACCESr\r!r8r9)r#Zmock_ownershipr$r$r%Útest_failed_chownÇsz#AuthenticatorTest.test_failed_chowncCs–t ¡}g|j_d|ji|j_|ƒ}tjdftj|ff|j_ t j t j dtd}t d¡*}tj|f|_|j |g¡Wdƒn1sˆ0YdS)Nz whatever.comrrar rb)rrr rrrr4r5r6r\rrUr ÚHTTP01_PrXrrcr7r!r8)r#r<Z new_webrootr=r9rer$r$r%Ú#test_perform_new_webroot_not_in_mapÌsÿÿ  ,z5AuthenticatorTest.test_perform_new_webroot_not_in_mapcCsN|j ¡|j |jg¡t |j¡D]"\}}}|D]}tj ||¡}q4q&dSr')r!r0r8r9rÚwalkrr)r#ÚdirpathÚdirnamesÚ_Z directoryrjr$r$r%Útest_perform_permissionsÛs z*AuthenticatorTest.test_perform_permissionscCs`|j ¡|j |jg¡}t|jƒ}| ¡}Wdƒn1sB0Y|j |jg¡dSr')r!r0r8r9rJrÚreadrB)r#Z responsesZ validation_fZ validationr$r$r%Útest_perform_cleanupës   &z&AuthenticatorTest.test_perform_cleanupcCs:t |j¡|j ¡|j |jg¡|j |jg¡dSr')rrrr!r0r8r9rBr*r$r$r%Ú"test_perform_cleanup_existing_dirsûs   z4AuthenticatorTest.test_perform_cleanup_existing_dirscCsntjt tjddd¡dtd}d}t |j ¡|j   ¡|j   ||j g¡|j  |j g¡|j  |g¡dS)NrQrRrTr r ZYmluZ28)rrUr rVrrWrXrrrr!r0r8r9rB)r#Z bingo_achallZbingo_validation_pathr$r$r%Ú(test_perform_cleanup_multiple_challengess ÿý  z:AuthenticatorTest.test_perform_cleanup_multiple_challengescCsR|j ¡|j |jg¡tj |jd¡}t  |¡|j  |jg¡t  |¡dS)NZleftover) r!r0r8r9rrrrrrrBÚrmdir)r#Z leftover_pathr$r$r%Útest_cleanup_leftoverss   z(AuthenticatorTest.test_cleanup_leftoverszcertbot.compat.os.rmdircCsB|j ¡|j |jg¡tƒ}tj|_||_|j |jg¡dSr') r!r0r8r9rmrqrrr\rB)r#Z mock_rmdirZos_errorr$r$r%Útest_cleanup_failure$s z&AuthenticatorTest.test_cleanup_failureN)(Ú__name__Ú __module__Ú __qualname__Ú__doc__rrUr rtrXr9r&r+r-r/r1Ú test_utilZpatch_display_utilr@ÚunittestZskipIfrZ POSIX_MODErCrPrYr`rfrgrhrorrcrsrurzr|r}r~r€rr$r$r$r%r sLÿ           r c@sZeZdZdZejejdedZ dd„Z dd„Z dd „Z d d „Z d d „Zdd„Zdd„ZdS)ÚWebrootActionTestz#Tests for webroot argparse actions.r r cCsFddlm}t ¡|_t ¡|_|jjdddgd|  |jd¡dS)Nrrz-dz --domainsÚappend)ÚactionÚdefaultr) rrrrrÚargparseÚArgumentParserÚparserÚ add_argumentZinject_parser_optionsr"r$r$r%r&8s    ÿzWebrootActionTest.setUpcCs |j dt d|ji¡g¡}dS)Nz --webroot-mapr )rŽÚ parse_argsÚjsonÚdumpsr)r#Úargsr$r$r%Útest_webroot_map_action@sÿz)WebrootActionTest.test_webroot_map_actioncCs,|j d |jj|j¡ ¡¡}| |¡}dS)Nz -d {0} -w {1})rŽrÚformatr9rrÚsplitÚ_get_config_after_perform©r#r“r r$r$r%Útest_domain_before_webrootEs ÿ z,WebrootActionTest.test_domain_before_webrootcCs|t tj¡ |j d ¡¡Wdƒn1s20Yt tj¡ |j d ¡¡Wdƒn1sn0YdS)Nz-d foo -w bar -w bazz-d foo -w bar -d baz -w qux)r]r^rr_rŽrr–r*r$r$r%Ú test_domain_before_webroot_errorKs.z2WebrootActionTest.test_domain_before_webroot_errorcCs2|j d |j|jjt ¡¡ ¡¡}|  |¡}dS©Nz-w {0} -d {1} -w {2} -d bar) rŽrr•rr9rrrr–r—r˜r$r$r%Útest_multiwebrootQs  ÿ  z#WebrootActionTest.test_multiwebrootcCs,t ¡}|j d |j|jj|¡ ¡¡}dSr›) rrrŽrr•rr9rr–)r#Zother_webroot_pathr“r$r$r%Ú(test_webroot_map_partial_without_performXs   ÿ z:WebrootActionTest.test_webroot_map_partial_without_performcCs*ddlm}||dƒ}| |jg¡|jS)Nrrr)rrr8r9r )r#r rr!r$r$r%r—es  z+WebrootActionTest._get_config_after_performN)r‚rƒr„r…rrUr rtrXr9r&r”r™ršrœrr—r$r$r$r%rˆ2sÿ rˆÚ__main__r3)(r…Z __future__rrŒrqr‘r(Úsysrr‡rZjosepyZjoser]ZacmerrLrrZcertbot.compatrrZcertbot.displayr r4Z certbot.testsr r†ZJWKRSAÚloadZ load_vectorrXZTestCaser rˆr‚ÚexitÚmainÚargvÚ__file__r$r$r$r%Ús4          :