a ‚oeÌaã@sfdZddlZddlZddlZddlZddlmZddlmZddlZddl m Z ddl m Z ddl m Zddl mZdd lmZddlm Z dd lmZdd lmZdd lmZdd lmZGdd„dejƒZGdd„dejƒZejddfdd„Z Gdd„dejƒZ!Gdd„dejƒZ"Gdd„dejƒZ#dd„Z$dd„Z%e&dk�rbe 'e (ej)dd…e*g¡¡dS) z)Tests for certbot._internal.auth_handler.éN)Úmock)Ú b64encode)Ú challenges)Úclient)Úerrors)Úmessages)Ú achallenges)Úobj)Úcommon)Ú acme_util)Úutilc@s,eZdZdd„Zdd„Zdd„Zdd„Zd S) ÚChallengeFactoryTestcCsFddlm}|ddtjdd�gƒ|_t tjdtj tjgd¡|_ dS)Nr©Ú AuthHandlerZmock_key)ÚkeyÚtesté) Úcertbot._internal.auth_handlerrrÚMockÚhandlerr Ú gen_authzrrÚSTATUS_PENDINGÚ CHALLENGESÚauthzr©Úselfr©rúJC:\Program Files\Certbot\pkgs\certbot\_internal\tests\auth_handler_test.pyÚsetUps    þzChallengeFactoryTest.setUpcCs8|j |jtdttjƒƒ¡}dd„|Dƒtjks4J‚dS)NrcSsg|] }|j‘qSr©Úchall©Ú.0ZachallrrrÚ (óz1ChallengeFactoryTest.test_all..)rÚ_challenge_factoryrÚrangeÚlenr r©rÚachallsrrrÚtest_all$sÿzChallengeFactoryTest.test_allcCs0|j |jdg¡}dd„|Dƒtjgks,J‚dS)NrcSsg|] }|j‘qSrrr!rrrr#-r$z6ChallengeFactoryTest.test_one_http..)rr%rr ÚHTTP01r(rrrÚ test_one_http*sz"ChallengeFactoryTest.test_one_httpcCsLt tjdtjddd�gtjg¡}|j |dg¡}t|dƒt j ksHJ‚dS)Nrr Z unrecognized)r Útypr) r rrrrrrr%ÚtyperZOther)rrr)rrrÚtest_unrecognized/sýz&ChallengeFactoryTest.test_unrecognizedN)Ú__name__Ú __module__Ú __qualname__rr*r,r/rrrrr s r c@sÂeZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z dd„Z dd„Z e d¡dd„ƒZdd„Zdd„Zdd„Zdd „Zd!d"„Zd#d$„Zd%d&„Zd'd(„Zd)d*„Zd+d,„Zd-S).ÚHandleAuthorizationsTestzmhandle_authorizations test. This tests everything except for all functions under _poll_challenges. cCs¤ddlm}t ¡|_tjdd�|_t |j¡tjdd�|_ t j g|j j _ t|j j_t ¡|_tjtjd�|_tjj|jj_||j |j|jgƒ|_t tj¡dS)NrrF)Údebug_challengesZ Authenticator©Úname)Úspec)rrrrÚ mock_displayÚ mock_configÚ display_objZ set_displayÚ MagicMockÚ mock_authrr+Úget_chall_prefÚ return_valueÚ gen_auth_respÚperformÚ side_effectÚ mock_accountÚ acme_clientZClientV2Úmock_netZ retry_afterrÚloggingÚdisableZCRITICALrrrrr@s     ÿzHandleAuthorizationsTest.setUpcCst tj¡dS©N©rErFZNOTSET©rrrrÚtearDownVsz!HandleAuthorizationsTest.tearDowncCstdtjd�}tj|gd�}tddd�|jj_t  d¡�Î}|j   ||j ¡}|jj jdks\J‚|jjjdksnJ‚|jjdks~J‚|jjdd d dksšJ‚|jjdd d d ks¶J‚|jjjdksÈJ‚|jjjd d d jd ksèJ‚t|ƒdksøJ‚Wdƒn1�s0YdS) NÚ0©ÚdomainZchalls©Zauthorizationséé)ÚretryÚ wait_valuez#certbot._internal.auth_handler.timeéréúhttp-01)Úgen_dom_authzrr rrr;Ú_gen_mock_on_pollrDÚpollrAÚpatchrÚhandle_authorizationsr9Úanswer_challengeÚ call_countÚsleepÚcall_args_listr<ÚcleanupÚ call_argsr-r')rrÚ mock_orderZ mock_timerrrÚ_test_name1_http_01_1_commonYs  z5HandleAuthorizationsTest._test_name1_http_01_1_commoncCs | ¡dSrG)rbrIrrrÚtest_name1_http_01_1_acme_2qsz4HandleAuthorizationsTest.test_name1_http_01_1_acme_2cCsÈtƒ|jj_|jjj tj ¡t dt j d�}t j|gd�}|j ||j¡}|jjjdks\J‚|jjjdksnJ‚|jjjdks€J‚|jjjdd}t|ƒdks¢J‚|djdks´J‚t|ƒdksÄJ‚dS©NrKrLrNrOrrU)rWrDrXrAr<r=r>ÚappendrÚDNS01rVr rrr;rrZr9r[r\r_r`r'r-)rrraZcleaned_up_achallsrrrÚ!test_name1_http_01_1_dns_1_acme_2ts z:HandleAuthorizationsTest.test_name1_http_01_1_dns_1_acme_2cCsštdtjd�tdtjd�tdtjd�g}tj|d�}tƒ|jj_|j   ||j ¡}|jj j dksbJ‚|jjj dkstJ‚|jjj dks†J‚t|ƒdks–J‚dS)NrKrLÚ1Ú2rNrTrO)rVr rrr;rWrDrXrArrZr9r[r\r<r_r')rÚauthzrsrarrrrÚ"test_name3_http_01_3_common_acme_2ˆs   þ  z;HandleAuthorizationsTest.test_name3_http_01_3_common_acme_2cCstjddd�}tdtjd�g}tj|d�}d}||jjj_ t ƒ|j j _ |j ||¡|j jjdksfJ‚|jjjdksxJ‚d |jjjddvs’J‚d |djjj›d �t|djjdjjƒ ¡|jjjddvsÚJ‚t|ƒ ¡|jjjddvsüJ‚dS) NTr©r4Z verbose_countrKrLrNó foobarbazrOúPass "-v" for more infoúhttp://ú/.well-known/acme-challenge/)rrrVr rr;rBrÚ thumbprintr>rWrDrXrArrZr[r\r8Ú notificationr`ÚbodyÚ identifierÚvaluerrr ÚtokenÚdecode©rZconfigrjraZaccount_key_thumbprintrrrÚtest_debug_challengesšs(   ÿÿþ ÿz.HandleAuthorizationsTest.test_debug_challengescCs‚tjddd�}tdtjgd�tdtjgd�g}tj|d�}d}||jjj _ t ƒ|j j _tjtjg|jj_ |j ||¡|j jjd ksˆJ‚|jjjdksšJ‚d |jjjd d vs´J‚d |d jjj›d �t|d jjd jjƒ ¡|jjjd d vsüJ‚t|ƒ ¡|jjjd d v�s J‚d|djjj›�|jjjd d v�sLJ‚|djjd   |jj¡|jjjd d v�s~J‚dS)NTrOrlrKrLrhrNrmrSrnrrorpz_acme-challenge.)!rrrVr r+rfr;rBrrqr>rWrDrXrArr<r=rrZr[r\r8rrr`rsrtrurr rvrwZ validationrxrrrÚtest_debug_challenges_verbose°s>ÿ   ÿ ÿÿþ ÿ ÿ ÿz6HandleAuthorizationsTest.test_debug_challenges_verbosecCshtdtjd�g}tj|d�}tj|jj_ t   tj¡� |j   ||j¡Wdƒn1sZ0YdS©NrKrLrN)rVr rrr;rÚAuthorizationErrorr<r@rAÚpytestÚraisesrrZr9©rrjrarrrÚtest_perform_failureÎs   z-HandleAuthorizationsTest.test_perform_failurecCsttdtjd�g}tj|d�}tdd�|jj_t j t j dd��$|j  ||jdd ¡Wdƒn1sf0YdS) NrKrLrNrS)rQú0All authorizations were not finalized by the CA.©ÚmatchFrO)rVr rrr;rWrDrXrAr}r~rr|rrZr9rrrrÚtest_max_retries_exceeded×s ÿz2HandleAuthorizationsTest.test_max_retries_exceededz)certbot._internal.auth_handler.time.sleepc sdtdtjd�g}tj|d�}tjj‰ddi‰‡fdd„}||_‡‡fdd „}tjd d �j }t t j |d �|j j_tjtjd d��Tt d¡�*}||j_|j ||jd¡Wdƒn1sº0YWdƒn1sØ0Y|jdksðJ‚|jddddk�s J‚t|jddd|dƒdk�s4J‚t|jddd|ddƒdk�s`J‚dS)NrKrLrNÚ time_sleptrcsˆd|7<dS)Nr…r)Zsecs)ÚstaterrÚmock_sleep_effectëszJHandleAuthorizationsTest.test_deadline_exceeded..mock_sleep_effectcsˆƒtjˆdd�S)Nr…)Úseconds)ÚdatetimeÚ timedeltar©Zorig_nowr†rrÚmock_now_effectïszHHandleAuthorizationsTest.test_deadline_exceeded..mock_now_effecté)Zminutes)ÚstatusrRr�r‚z0certbot._internal.auth_handler.datetime.datetimeFrTrOrS)rVr rrr;r‰ZnowrArŠrˆrWrrrDrXr}r~rr|rYrrZr9r\r^Úabs)rZ mock_sleeprjrar‡rŒÚintervalZmock_dtrr‹rÚtest_deadline_exceededãs*  ÿ ÿ N(z/HandleAuthorizationsTest.test_deadline_exceededcCsLtjgd�}t tj¡� |j ||j¡Wdƒn1s>0YdS)NrN) rr;r}r~rr|rrZr9)rrarrrÚtest_no_domainss z(HandleAuthorizationsTest.test_no_domainscCsštdtjd�g}tj|d�}|jjj t j ¡|j j   t j jt jjf¡tƒ|jj_|j  ||j¡|jjjdksvJ‚|jjjdddjdks–J‚dSrd)rVr rrr;r<r=r>rerr+rÚ pref_challsÚextendr-rfrWrDrXrArZr9r_r\r`rrrrÚ-test_preferred_challenge_choice_common_acme_2 s ÿ zFHandleAuthorizationsTest.test_preferred_challenge_choice_common_acme_2cCsntdtjd�g}tj|d�}|jj tj j ¡t   t j¡� |j ||j¡Wdƒn1s`0YdSr{)rVr rrr;rr“rerrfr-r}r~rr|rZr9rrrrÚ.test_preferred_challenges_not_supported_acme_2s  zGHandleAuthorizationsTest.test_preferred_challenges_not_supported_acme_2cCs^tdtjgd�g}tj|d�}t tj¡� |j   ||j ¡Wdƒn1sP0YdSr{) rVr rfrr;r}r~rr|rrZr9rrrrÚ%test_dns_only_challenge_not_supporteds z>HandleAuthorizationsTest.test_dns_only_challenge_not_supportedcCsštj|jj_tdtjd�}tj |gd�}t   tj¡� |j   ||j¡Wdƒn1sZ0Y|jjjdksvJ‚|jjjdddjdks–J‚dSrd)rr|r<r@rArVr rrr;r}r~rrZr9r_r\r`r-©rrrarrrÚtest_perform_error%s .z+HandleAuthorizationsTest.test_perform_errorcCsštj|jj_tdtjd�g}tj |d�}t   tj¡� |j   ||j¡Wdƒn1sZ0Y|jjjdksvJ‚|jjjdddjdks–J‚dSrd)rr|rDr[rArVr rrr;r}r~rrZr9r<r_r\r`r-rrrrÚtest_answer_error0s  .z*HandleAuthorizationsTest.test_answer_errorc CsÎtdtjd�g}tj|d�}ttjd�|jj _ t   ¡�Rt jtjdd��"|j ||jd¡Wdƒn1sp0YWdƒn1sŽ0Y|jjjdksªJ‚|jjjd d d jd ksÊJ‚dS) NrKrLrN©rŽzSome challenges have failed.r‚FrOrrU)rVr rrr;rWrÚSTATUS_INVALIDrDrXrAÚ test_utilÚpatch_display_utilr}r~rr|rrZr9r<r_r\r`r-rrrrÚtest_incomplete_authzr_error;s  Nz5HandleAuthorizationsTest.test_incomplete_authzr_errorc Csdd„}tdtjd�tdtjd�g}||jj_tj|d�}t d¡�"}|j   ||j d¡}Wdƒn1sl0Yt |ƒd ks†J‚|j d ks”J‚ttjd �|jj_t ¡�Rtjtjd d ��"|j   ||j d¡Wdƒn1sè0YWdƒn1�s0YdS) NcSs2ttjƒ}ttjƒ}|jjjdkr*||ƒS||ƒS)zBThis mock will invalidate one authzr, and invalidate the other oneúwill-be-invalid)rWrÚ STATUS_VALIDrœrsrtru)rZ valid_mockZ invalid_mockrrrÚ_conditional_mock_on_pollGs   zLHandleAuthorizationsTest.test_best_effort.._conditional_mock_on_pollz will-be-validrLr rNzAcertbot._internal.auth_handler.AuthHandler._report_failed_authzrsTrOr›zAll challenges have failed.r‚)rVr rrDrXrArr;rYrrZr9r'r\rWrrœr�ržr}r~rr|)rr¢rjraZ mock_reportZ valid_authzrrrrÚtest_best_effortFs  ÿ   0 z)HandleAuthorizationsTest.test_best_effortcCs t tjdtjgtjg¡}tj|gd�}t t j ¡� |j   ||j ¡Wdƒn1sZ0Yt tjdtjgtjg¡}tj|gd�}|j   ||j ¡dS)NrKrN)r rrrrfrr;r}r~rr|rrZr9r¡r˜rrrÚ"test_validated_challenge_not_rerunfsý.ýz;HandleAuthorizationsTest.test_validated_challenge_not_reruncCsædd„}dtjfdtjfdtjfg}dd„|Dƒ}tj|d�}||jj_|j  |¡\}}|jjj d ksjJ‚t |ƒd kszJ‚t |ƒd ksŠJ‚|d j j jdks J‚|d j jtjks¶J‚|d j j jdksÌJ‚|d j jtjksâJ‚d S) z“When we deactivate valid authzrs in an orderr, we expect them to become deactivated and to receive a list of deactivated authzrs in return.cSsR|jjtjkrD|jjjdkr&t d¡‚|jjtj d�}tj |d�}n t  d¡‚|S)NÚis_valid_but_will_failzMock deactivation ACME errorr›)rsz Can't deactivate non-valid authz) rsrŽrr¡rtruÚ acme_errorsÚErrorÚupdateÚSTATUS_DEACTIVATEDZAuthorizationResourcer)rZauthzbrrrÚ_mock_deactivate~s  zQHandleAuthorizationsTest.test_valid_authzrs_deactivated.._mock_deactivateZis_validZ is_pendingr¥c Ss.g|]&}t |d|dtjg|dg¡‘qS)rOr)r rr+)r"Úarrrr#ŒsÿÿzKHandleAuthorizationsTest.test_valid_authzrs_deactivated..rNrSrOrN)rr¡rrr;rDZdeactivate_authorizationrArZdeactivate_valid_authorizationsr\r'rsrtrurŽr©)rrªZ to_deactivateZorderrrjZfailedrrrÚtest_valid_authzrs_deactivated{s$ þÿ  z7HandleAuthorizationsTest.test_valid_authzrs_deactivatedN)r0r1r2Ú__doc__rrJrbrcrgrkryrzr€r„rrYr‘r’r•r–r—r™ršrŸr£r¤r¬rrrrr39s.       r3rOcsd|i‰‡‡‡fdd„}|S)NÚcountcspˆddˆd<ˆddkr ˆntj}t ||jjjdd„|jjDƒ|gt|jjƒ¡}|t j dt ˆƒid�fS)Nr®rOrcSsg|] }|j‘qSrr)r"Úchallbrrrr#¦r$z4_gen_mock_on_poll.._mock..z Retry-After)Zheaders) rrr rrsrtrurr'rr;Ústr)rZeffective_statusZ updated_azr©r†rŽrRrrÚ_mock süz _gen_mock_on_poll.._mockr)rŽrQrRr²rr±rrW�s rWc@s eZdZdZdd„Zdd„ZdS)ÚChallbToAchallTestz:Tests for certbot._internal.auth_handler.challb_to_achall.cCsddlm}||ddƒS)Nr)Úchallb_to_achallÚ account_keyrM)rr´)rr¯r´rrrÚ_call¯s zChallbToAchallTest._callcCs&| tj¡tjtjddd�ks"J‚dS)NrµrM)r¯rµrM)r¶r ÚHTTP01_PrZ"KeyAuthorizationAnnotatedChallengerIrrrÚtest_it³s  þÿzChallbToAchallTest.test_itN)r0r1r2r­r¶r¸rrrrr³¬sr³c@s<eZdZdZdd„Zdd„Zedd„ƒZdd „Zd d „Z d S) ÚGenChallengePathTestzBTests for certbot._internal.auth_handler.gen_challenge_path. cCst tj¡dSrG)rErFZFATALrIrrrr¾szGenChallengePathTest.setUpcCst tj¡dSrGrHrIrrrrJÁszGenChallengePathTest.tearDowncCsddlm}|||ƒS)Nr)Úgen_challenge_path)rrº)ÚclsÚchallbsZ preferencesrºrrrr¶Äs zGenChallengePathTest._callcCsNtjtjf}tjtjg}| ||¡dks,J‚| |ddd…|¡dksJJ‚dS)z/Given DNS01 and HTTP01 with appropriate combos.)rNéÿÿÿÿ)rO)r ÚDNS01_Pr·rrfr+r¶©rr¼ZprefsrrrÚtest_common_caseÉs  z%GenChallengePathTest.test_common_casecCsLtjf}tjg}t tj¡�| ||¡Wdƒn1s>0YdSrG) r r¾rr+r}r~rr|r¶r¿rrrÚtest_not_supportedÑsz'GenChallengePathTest.test_not_supportedN) r0r1r2r­rrJÚ classmethodr¶rÀrÁrrrrr¹ºs r¹c@sNeZdZdZdd„Ze d¡dd„ƒZe d¡dd„ƒZe d¡d d „ƒZ d S) ÚReportFailedAuthzrsTestzLTests for certbot._internal.auth_handler.AuthHandler._report_failed_authzrs.cCsddlm}tjtjdd�|_d|j_d|jj_ ||jt ¡t ¡gƒ|_ t j dt jt jjddd �d œ}|d jduszJ‚t jfi|¤Ž}t j |d <t jfi|¤Ž}t ¡|_d |jjj_||g|jj_t jjddd �|d <t jfi|¤Ž}t ¡|_d|jjj_|g|jj_dS)NrrZbuzz)r7r6z the buzz hintÚuriZtlsÚdetail)rÅ)r rÄrŽÚerrorrÆr z example.comZdnsseczfoo.bar)rrrr;Ú plugin_commonZPluginr<r6Ú auth_hintr>rr r+rrœr§Z with_codeÚ descriptionZ ChallengeBodyÚauthzr1rsrtrurÚauthzr2)rrÚkwargsZhttp_01Z http_01_diffrrrrßs,  ü     zReportFailedAuthzrsTest.setUpz2certbot._internal.auth_handler.display_util.notifycCs|j |jg¡| d¡dS)Nz÷ Certbot failed to authenticate some domains (authenticator: buzz). The Certificate Authority reported these problems: Domain: example.com Type: tls Detail: detail Domain: example.com Type: tls Detail: detail Hint: the buzz hint )rÚ_report_failed_authzrsrÊÚassert_called_with©rÚ mock_notifyrrrÚtest_same_error_and_domainsÿz2ReportFailedAuthzrsTest.test_same_error_and_domaincCs4d|j_d|jj_|j |j|jg¡| d¡dS)NZquuxZquuuuuuxa' Certbot failed to authenticate some domains (authenticator: quux). The Certificate Authority reported these problems: Domain: foo.bar Type: dnssec Detail: detail Domain: example.com Type: tls Detail: detail Domain: example.com Type: tls Detail: detail Hint: quuuuuux ) r<r6rÈr>rrÍrÊrËrÎrÏrrrÚ!test_different_errors_and_domainss  ÿz9ReportFailedAuthzrsTest.test_different_errors_and_domainscCshddlm}tjdd�|_d|j_t|jj_||jt ¡t ¡gƒ|_ |j   |j g¡|j dksdJ‚dS)zMIf authenticator not derived from common.Plugin, we shouldn't call .auth_hintrrZquuzr5rON) rrrr;r<r6Ú ExceptionrÈrArrÍrÊr\)rrÐrrrrÚ!test_non_subclassed_authenticator)s  z9ReportFailedAuthzrsTest.test_non_subclassed_authenticatorN) r0r1r2r­rrrYrÑrÒrÔrrrrrÃÚs"  rÃcCsdd„|DƒS)z(Generate a dummy authorization response.cSsg|]}d|jj|jf‘qS)z%s%s)Ú __class__r0rM)r"r rrrr#8sÿz!gen_auth_resp..r)Z chall_listrrrr?6sÿr?cCst tj||tjgt|ƒ¡S)z!Generates new authzr for domains.)r rrrr'rLrrrrV<sþrVÚ__main__)+r­r‰rEÚsysZunittestrZjosepyrr}ZacmerrrCrr¦rZcertbotrZcertbot._internal.displayr r:Zcertbot.pluginsr rÇZ certbot.testsr r r�ZTestCaser r3r¡rWr³r¹rÃr?rVr0ÚexitÚmainÚargvÚ__file__rrrrÚs:            "f \