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 ZcertsZhttp_01_resourcesmgr)selfr rS/usr/lib/python3.9/site-packages/certbot/_internal/tests/plugins/standalone_test.pysetUps zServerManagerTest.setUpcCsdSNrrrrr test_init szServerManagerTest.test_initcCs2|jjd|d}|dd}|jj|ddS)Nrportchallenge_type)r)rrun getsocknamesstop)rrserverrrrr_test_run_stop$sz ServerManagerTest._test_run_stopcCs|tjdSr)rrHTTP01rrrrtest_run_stop_http_01+sz'ServerManagerTest.test_run_stop_http_01cCsD|jjdtjd}|dd}|jj|tjd}|j|dS)Nrrr)rrrr rr)rrrserver2rrrtest_run_idempotent.s  z%ServerManagerTest.test_run_idempotentcCsttj}|d|d}t}z|d|fWntyNYn0ttj"|j j |t j dWdn1s0Y| | dS)Nrrr%)r)socketZAF_INET6bind getsocknameOSErrorpytestraisesr StandaloneBindErrorrrrr close)rZ some_serverrZmaybe_another_serverrrrtest_run_bind_error7s    $z%ServerManagerTest.test_run_bind_errorN) __name__ __module__ __qualname____doc__rrrr!r#r.rrrrr s r cCs4ttjtjd}|d|d}||S)z%Gets an open port number from the OS.rr$r)r&ZAF_INETZ SOCK_STREAMr'r(r-)Z open_socketrrrr get_open_portHs   r3c@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)rr5r MagicMockr3configauthservers)rr5rrrrTs zAuthenticatorTest.setUpcCsdSrrrrrrtest_more_info[sz AuthenticatorTest.test_more_infocCsdSrrrrrrtest_get_chall_pref^sz%AuthenticatorTest.test_get_chall_prefcCs&|}|j|}dd|D}dS)NcSsg|]}||jqSr)response account_key).0Zachallrrr fz2AuthenticatorTest.test_perform..) _get_achallsr:perform)rZachallsr>Zexpectedrrr test_performbs zAuthenticatorTest.test_performcCs^|}tj}ttj|dd}|gdtg|jjj_ |j }d|_ | | |dS)NerrnoT)rG EADDRINUSEr r,rr8r:r;r side_effectyesno return_valuerE_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) rLrMrGrJr*r+r PluginError _fail_performrN)rrOrPrSrQrrr test_perform_eaddrinuse_no_retryus(z2AuthenticatorTest.test_perform_eaddrinuse_no_retrycCs|j\}}dSr)Z call_args)rrSZ yesno_argsZ yesno_kwargsrrrrNs z,AuthenticatorTest._assert_correct_yesno_callcCs@tj}ttj||Wdn1s20YdSr)rGZEACCESr*r+r rUrVrrQrrrtest_perform_eaccessz%AuthenticatorTest.test_perform_eaccescCs@tj}ttj||Wdn1s20YdSr)rGZENOTCONNr*r+r r,rVrXrrr$test_perform_unexpected_socket_errorsz6AuthenticatorTest.test_perform_unexpected_socket_errorcCs4ttj|dd}||jjj_|j| dS)NrFrH) r r,rr8r:r;rrKrDrC)rrQrRrrrrVs zAuthenticatorTest._fail_performcCs.d}tjtd}tjtj||d}|gS)Ns localhostzrsa512_key.pem)Zchallbdomainr?) joseZJWKload test_utilZ load_vectorrZ"KeyAuthorizationAnnotatedChallenger ZHTTP01_P)clsr[keyZhttp_01rrrrCs zAuthenticatorTest._get_achallscCsddd|jjj_|jjdd|jjdddg|jdg|jjj dddi|jjj_|jdg|jdg|jjj ddS) NZserver1r")rrIZchall1Zchall2Zchall3rrI) r:r;ZrunningrMZservedaddupdateZcleanuprZassert_called_once_withZassert_called_withrrrr test_cleanups zAuthenticatorTest.test_cleanupcCsd|j_d|j_d|j_dS)NZ80z 127.0.0.1)r9r6Zhttp01_addressrrrrtest_auth_hintsz AuthenticatorTest.test_auth_hintN)r/r0r1r2rr<r=rEr^Zpatch_display_utilrTrWrNrYrZrV classmethodrCrcrdrrrrr4Qs"   r4__main__r)#r2rGr&systypingrrrZunittestrZjosepyr\ZOpenSSL.cryptoZOpenSSLr*ZacmerrZacme_standaloneZcertbotrr Z certbot.testsr r r^ZTestCaser r3r4r/exitmainargv__file__rrrrs,          1 n