a }|g@sdZddlZddlZddlZddlmZddlmZddlmZddlZddlm Z ddl Z ddl Z ddlZddlmZddlmZdd lmZdd lmZdd lmZdd lmZGd ddejZddZGdddejZedkr ee ej!dde"gdS)z/Tests for certbot._internal.plugins.standalone.N)Dict)Set)Tuple)mock) challenges) standalone) achallenges)errors) acme_util)utilc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS)ServerManagerTestz=Tests for certbot._internal.plugins.standalone.ServerManager.cCs,ddlm}i|_i|_||j|j|_dS)Nr) ServerManager)$certbot._internal.plugins.standaloner certshttp_01_resourcesmgr)selfr rS/usr/lib/python3.9/site-packages/certbot/_internal/tests/plugins/standalone_test.pysetUps zServerManagerTest.setUpcCs(|jj|jusJ|jj|jus$JdSN)rrrrrrr test_init szServerManagerTest.test_initcCsZ|jjd|d}|dd}|j||iks6J|jj|d|jiksVJdS)Nrportchallenge_type)r)rrun getsocknamesrunningstop)rrserverrrrr_test_run_stop$s z ServerManagerTest._test_run_stopcCs|tjdSr)r"rHTTP01rrrrtest_run_stop_http_01+sz'ServerManagerTest.test_run_stop_http_01cCsx|jjdtjd}|dd}|jj|tjd}|j||iksJJ||usVJ|j||jikstJdS)Nrrr)rrrr#rrr )rr!rserver2rrrtest_run_idempotent.s  z%ServerManagerTest.test_run_idempotentcCsttj}|d|d}t}z|d|fWntyNYn0ttj"|j j |t j dWdn1s0Y|j iksJ||dS)Nrrr()r)socketZAF_INET6bind getsocknameOSErrorpytestraisesr StandaloneBindErrorrrrr#rclose)rZ some_serverrZmaybe_another_serverrrrtest_run_bind_error7s    $z%ServerManagerTest.test_run_bind_errorN) __name__ __module__ __qualname____doc__rrr"r$r&r1rrrrr s r cCs4ttjtjd}|d|d}||S)z%Gets an open port number from the OS.rr'r)r)ZAF_INETZ SOCK_STREAMr*r+r0)Z open_socketrrrr get_open_portHs   r6c@seZdZdZddZddZddZdd Ze d d Z e d d Z ddZ ddZ ddZddZeddZddZddZdS)AuthenticatorTestz=Tests for certbot._internal.plugins.standalone.Authenticator.cCs<ddlm}tjtd|_||jdd|_t|j_dS)Nr) Authenticator) http01_portr)name)rr8r MagicMockr6configauthservers)rr8rrrrTs zAuthenticatorTest.setUpcCst|jtsJdSr) isinstancer=Z more_infostrrrrrtest_more_info[sz AuthenticatorTest.test_more_infocCs|jjddtjgksJdS)N)domain)r=Zget_chall_prefrr#rrrrtest_get_chall_pref^s z%AuthenticatorTest.test_get_chall_prefcCs2|}|j|}dd|D}||ks.JdS)NcSsg|]}||jqSr)response account_key).0Zachallrrr fz2AuthenticatorTest.test_perform..) _get_achallsr=perform)rZachallsrDZexpectedrrr test_performbs zAuthenticatorTest.test_performcCs^|}tj}ttj|dd}|gdtg|jjj_ |j }d|_ | | |dS)NerrnoT)rM EADDRINUSEr r/rr;r=r>r side_effectyesno return_valuerK_assert_correct_yesno_call)rmock_get_utility mock_utilityencountered_errnoerror mock_yesnorrrtest_perform_eaddrinuse_retryisz/AuthenticatorTest.test_perform_eaddrinuse_retrycCs\|}|j}d|_tj}ttj||Wdn1sD0Y| |dS)NF) rRrSrMrPr-r.r PluginError _fail_performrT)rrUrVrYrWrrr test_perform_eaddrinuse_no_retryus(z2AuthenticatorTest.test_perform_eaddrinuse_no_retrycCs.|j\}}d|dvsJ|ddr*JdS)Nzin userdefaultT)Z call_argsget)rrYZ yesno_argsZ yesno_kwargsrrrrTs z,AuthenticatorTest._assert_correct_yesno_callcCs@tj}ttj||Wdn1s20YdSr)rMZEACCESr-r.r r[r\rrWrrrtest_perform_eaccessz%AuthenticatorTest.test_perform_eaccescCs@tj}ttj||Wdn1s20YdSr)rMZENOTCONNr-r.r r/r\r`rrr$test_perform_unexpected_socket_errorsz6AuthenticatorTest.test_perform_unexpected_socket_errorcCs4ttj|dd}||jjj_|j| dS)NrLrN) r r/rr;r=r>rrQrJrI)rrWrXrrrr\s zAuthenticatorTest._fail_performcCs.d}tjtd}tjtj||d}|gS)Ns localhostzrsa512_key.pem)ZchallbrBrE) joseZJWKload test_utilZ load_vectorrZ"KeyAuthorizationAnnotatedChallenger ZHTTP01_P)clsrBkeyZhttp_01rrrrIs zAuthenticatorTest._get_achallscCsddd|jjj_|jjdd|jjdddg|jdg|jjtddhdksdJ|jjj dd di|jjj_|jdg|jjtdhdksJd|jjj j ksJ|jdg|jjttdksJ|jjj d dS) Nserver1r%)rrOZchall1Zchall2Zchall3)rhr%rrO) r=r>rrSZservedaddupdateZcleanupsetr Zassert_called_once_withZ call_countZassert_called_withrrrr test_cleanups,    zAuthenticatorTest.test_cleanupcCsDd|j_d|j_d|jgvs$Jd|j_d|jgvs@JdS)NZ80z on port 80z 127.0.0.1zon 127.0.0.1:80)r<r9Zhttp01_addressr=Z auth_hintrrrrtest_auth_hints z AuthenticatorTest.test_auth_hintN)r2r3r4r5rrArCrKreZpatch_display_utilrZr]rTrarbr\ classmethodrIrlrmrrrrr7Qs"   r7__main__r)#r5rMr)systypingrrrZunittestrZjosepyrcZOpenSSL.cryptoZOpenSSLr-ZacmerrZacme_standaloneZcertbotrr Z certbot.testsr r reZTestCaser r6r7r2exitmainargv__file__rrrrs,          1 n