M/eF ddZddlZddlmZddlZddlmZddlZddl Z ddl Z ddl m Z ddl Z ddl Z ddlZddlmZddlmZddlmZdd lmZdd lmZdd lmZdd lmZdd lmZddlZddlmZddlmZddlmZddlmZddl Z!ddl"m#Z#ddl$m%Z%ddl$m&Z&ddl'm(Z(ddl'm)Z)ddl'm*Z*ddl+m,Z-ddl.m/Z/ddl.m0Z0ddl1m&Z2ddl3m4Z4e jjdk\rddl6m7Z8nddl8Z8Gdde4jrZ:de;d e;fd!Zd#e;d$e?d%e?d e?fd&Z@de;d e#jfd'ZBde;d e#jfd(ZDde;d e!jfd)ZFde;d e!jfd*ZHde;d e#jfd+ZJdId,e;d-e;d.eKd e;fd/ZLd ejfd0ZN dJd1eed ejfd2ZOGd3d4ZPd ePfd5ZQd1ed ePfd6ZRd7ed8ed dfd9ZSGd:d;ejZUGd<d=eUZVd>e jd?e jd@e;d dfdAZXdBegefdCe;d dfdDZYdEe;d eedFefgedFefffdGZZd@e;d e;fdHZ[y)KzTest utilities.N) ExitStack)reload) synchronize)Any)Callable)cast)IO)Iterable)List)Optional)Union)mock)default_backend) serialization) RSAPrivateKey)crypto) configuration)util) constants)lock)storage)obj) filesystem)os)common) c eZdZdZdeefdZdedededededd f d Z dded ed ee e eefdd fd Z de efdZ ddeede dd fdZddZddZededdd fdZddZdefdZy )DummyInstallerz(Dummy installer plugin for test purpose.returncgSNselfs 4/usr/lib/python3/dist-packages/certbot/tests/util.py get_all_nameszDummyInstaller.get_all_names1 domain cert_pathkey_path chain_pathfullchain_pathNcyr"r#)r%r*r+r,r-r.s r& deploy_certzDummyInstaller.deploy_cert4 r) enhancementoptionscyr"r#)r%r*r2r3s r&enhancezDummyInstaller.enhance8r1r)cgSr"r#r$s r&supported_enhancementsz%DummyInstaller.supported_enhancements<r(r)title temporarycyr"r#)r%r8r9s r&savezDummyInstaller.save? r)cyr"r#r$s r& config_testzDummyInstaller.config_testBr<r)cyr"r#r$s r&restartzDummyInstaller.restartEr<r)add).Ncyr"r#)clsrAs r&add_parser_argumentsz#DummyInstaller.add_parser_argumentsHr1r)cyr"r#r$s r&preparezDummyInstaller.prepareLr<r)cy)Nr#r$s r& more_infozDummyInstaller.more_infoOsr)r")NFr N)__name__ __module__ __qualname____doc__r strr'r0r r r r5r7boolr;r>r@ classmethodrrDrFrIr#r)r&rr/s2x} # #  RU $' ,0 <@ c  !%S 3"78 DH S  (3- 4 D    x ': t   3r)rnamesr ct}tj|jt j t jdg|}|jt j|}t|S)zPath to a test vector.testdata) ratexitregistercloseimportlib_resourcesfiles __package__joinpath enter_contextas_filerO)rR _file_manager vector_refpaths r& vector_pathraSsdKM OOM''(@$**;7@@TeTJ  & &':'B'B:'N OD t9r)ctjtjdg|}|j } |j j ddjS#t$r|cYSwxYw)zLoad contents of a test vector.rTz  ) rXrYrZr[ read_bytesdecodereplaceencode ValueError)rRr_datas r& load_vectorrj\sp@$**;7@@TeTJ  "D{{}$$VT299;;  s-A** A87A8filename loader_pem loader_derctjj|\}}|jdk(r|S|jdk(r|St d)Nz.pemz.derz1Loader could not be recognized based on extension)rr`splitextlowerrh)rkrlrm_exts r& _guess_loaderrsisN WW  h 'FAs yy{f   H IIr)ct|dtjtj}tj|t |S)zLoad certificate.)rsr FILETYPE_PEM FILETYPE_ASN1load_certificaterjrRloaders r& load_certr{rs;  b 6&&(<(<>F  " "6;+> ??r)ct|dtjtj}tj|t |S)zLoad certificate request.ru)rsrrvrwload_certificate_requestrjrys r&load_csrr~ys;  b 6&&(<(<>F  * *6;3F GGr)c8tjt|S)z(Load ComparableX509 certificate request.)joseComparableX509r~)rRs r&load_comparable_csrrs   x/ 00r)c 6t|dtjtj}|tjk(rtj }ntj }tjtt|t|dtS)zLoad RSA private key.ruN)passwordbackend) rsrrvrwrload_pem_private_keyload_der_private_keyrComparableRSAKeyrrrjr)rRrz loader_fns r&load_rsa_private_keyrsx 59f&9&96;O;O PF $$$!66 !66   ] {E*T?CT U W XXr)ct|dtjtj}tj|t |S)zLoad pyOpenSSL private key.ru)rsrrvrwload_privatekeyrjrys r&load_pyopenssl_private_keyrs;  b 6&&(<(<>F  ! !&+u*= >>r) config_dirtestfileecc |dtd }tjjtj }tjjtj |}tjjtj|}|||fD]7}tjj|r#tj|9tdj|rdnd}tj|D]T} tjtjj|| tjj|| Vt j"D]r} tj$tjj|dj| tjj|dj| ttjj||} t't|5} t'| d5} | j)fd | Ddddddd| S#1swYxYw#1swY| SxYw) aCreates a lineage defined by testfile. This creates the archive, live, and renewal directories if necessary and creates a simple lineage. :param str config_dir: path to the configuration directory :param str testfile: configuration file to base the lineage on :param bool ec: True if we generate the lineage with an ECDSA key :returns: path to the renewal conf file for the created lineage :rtype: str Nz.confzsample-archive{}z-ecrHz{0}1.pemz{0}.pemwc3BK|]}|jdyw)MAGICDIRN)rf).0liners r& zmake_lineage..s#F9= Z4Fs)lenrr`joinrRENEWAL_CONFIGS_DIR ARCHIVE_DIRLIVE_DIRexistsrmakedirsraformatlistdirshutilcopyfilerALL_FOURsymlinkopen writelines) rrr lineage_nameconf_dir archive_dirlive_dir directorysample_archivekind conf_pathsrcdsts ` r& make_lineagersNc'l]+Lww||I113H'',,I))<9Kww||I&& 6H"8X7+ ww~~i(    *+!!3!:!:B5B!OPN >*9 ^T: [$7 99  C 277<< Z->->t-DE77<<)*:*:4*@A CC Z8.>&>**46 66224A4H H"  sB BBvaluec$|jr3||jvrtd|zt|j||S|dk7r|jj ||dvrt|j||St j|||S)a Before it is frozen, attributes are set on the FreezableMock instance and added to the _frozen_set. Attributes in the _frozen_set cannot be changed after the FreezableMock is frozen. In this case, they are set on the underlying _mock. In cases of return_value and side_effect, these attributes are always passed through to the instance's _mock and added to the _frozen_set before the object is frozen. zCannot change frozen attribute rr)rrrsetattrrrAr __setattr__)r%rrs r&rzFreezableMock.__setattr__*s <<t'''$%F%MNN4::tU3 3 =     & 2 24::tU3 3!!$e44r)rJ)rKrLrMrNrrrrPr rrrrrrrOrrr#r)r&rrs ',RV%)]]%:%:t8HS#X'>EF??402K22 ^ #'t:LMK GV[ 12 NN 7 ;;2s=B-c dtdtdtddffd dtdtddffd }t}ttj Dcgc]8}t ttj |r|jd s|:}}|D]/}|d k(rtd  }n td | }t|||1|jtd | Scc}w)Nmessage unused_args unused_kwargsr c.|rj|yy)z$Write to message to stdout. N)write)rrrrs r& _write_msgz9_create_display_util_mock_with_stdout.._write_msgRs  LL ! r)rrc.t|||i|y)z< Mock function for display utility methods. N)r)rrrs r& mock_methodz:_create_display_util_mock_with_stdout..mock_methodXs 4(D#F#r)rrTrr) rOrrrrrrrrrr)rrrrrrrrs` @r&rrQs"C"s"S"T" $3$#$$$oG$' (?(?$@2Dw{'>'>EF??402K2. ^ #'t-79K(t-8:K-. NN 7 ;;2s=C&rrc|r|dn|dg}|jdd|jdd|jddd}tj|i|y)Nrrdefaultcli_flagforce_interactiveF)rrr)get display_utilassert_valid_call)rr assert_args assert_kwargss r&rrps`"47y(9:K::i.JJz40#ZZ(;UCM ""KA=Ar)c eZdZdZddZddZy)TempDirTestCasezBBase test class which sets up and tears down a temporary directoryNc6tj|_y)zExecute before testN)tempfilemkdtemptempdirr$s r&setUpzTempDirTestCase.setUps'') r)ctjgtj_t j t j|jy)zExecute after testN) loggingshutdown getLoggerhandlersr_release_locksrrmtreerr$s r&tearDownzTempDirTestCase.tearDowns? (*$  dll#r)rJ)rKrLrMrNrrr#r)r&rr|sL* $r)rc$eZdZdZdfd ZxZS)ConfigTestCasez2Test class which sets up a NamespaceConfig object.c t|tjt j dit jtj|_ |jjid|jj_ tjj!|j"d|jj_tjj!|j"d|jj_tjj!|j"d|jj_tjd|jj_tjd|jj_tjd|jj_d|jj_y) Ncertonlyconfigworklogsauth_cert_pathauth_chain_pathzhttps://example.comr#)superrrNamespaceConfigrrcopydeepcopyr CLI_DEFAULTSrset_argument_sources namespaceverbrr`rrrwork_dirlogs_dirr+r.r-server)r% __class__s r&rzConfigTestCase.setUps=  #33 NN CT]]9+A+AB C   ((,%/ "+-77<< h+O ()+dllF)K &)+dllF)K &*3*@*@AQ*R '/8/E/EFW/X ,+4+A+ABS+T ('< $r)rJ)rKrLrMrNr __classcell__)r$s@r&rrs<==r)revent_in event_outr`c8tjj|rtj|}ntj |} |j |jdsJd |jy#|jwxYw)a Acquire a file lock on given path, then wait to release it. This worker is coordinated using events to signal when the lock should be acquired and released. :param multiprocessing.Event event_in: event object to signal when to release the lock :param multiprocessing.Event event_out: event object to signal when the lock is acquired :param path: the path to lock timeoutz*Timeout while waiting to release the lock.N) rr`isdirrlock_dirLockFilerwaitrelease)r&r'r`my_locks r& _handle_lockr2sm ww}}T--%--% }}R}(V*VV(s )BBcallback path_to_lockcttttj}tj}tjt |||f}|j |jdsJd||j|jd|jdk(sJy)z Grab a lock on path_to_lock from a foreign process then execute the callback. :param callable callback: object to call after acquiring the lock :param str path_to_lock: path to file or directory to lock )targetr r*z*Timeout while waiting to acquire the lock.rN) reload_modulermultiprocessingEventProcessr2startr/rrexitcode)r3r4 emit_event receive_eventprocesss r& lock_and_callrAs$ &&(J#))+M%%\,6 |+TVG MMO   b  )W+WW ) JNN LLL   q  r)reason.cPdtdtfdtdtfffd }|S)zFDecorator to skip permanently a test on Windows. A reason is required.function.r c^tjtjdk(|S)zWrapped versionwin32)unittestskipIfsysplatform)rDrBs r&wrapperz skip_on_windows..wrappers$?xs||w6?IIr))rr)rBrKs` r&skip_on_windowsrLs2J(38,J#s(1CJ Nr)cftjjtj|S)z Return the given path joined to the tempdir path for the current platform Eg.: 'cert' => /tmp/cert (Linux) or 'C:\Users\currentuser\AppData\Temp\cert' (Windows) )rr`rr gettempdir)r`s r& temp_joinrOs" 77<<++-t 44r))Tr")\rNrU contextlibrr importlibrr8rr r9rrrIrtypingrrrr r r r r rGrcryptography.hazmat.backendsrcryptography.hazmat.primitivesr-cryptography.hazmat.primitives.asymmetric.rsarjosepyrOpenSSLrcertbotrrcertbot._internalrrrcertbot._internal.displayrrcertbot.compatrrcertbot.displayrcertbot.pluginsr version_infoimportlib.resources resourcesrX InstallerrrOrabytesrjintrsX509r{X509Reqr~rrrrPKeyrrPrrrrrrrrTestCaserrr:r2rArLrOr#r)r&rhs - ' 88G!'"%8%0"v5!V%%!H   JCJSJcJcJ@c@fkk@HSHV^^H11(;(;1 X X)>)> X?s?v{{?*S*C*T*S*ZTDNNT2 $_ _(,_@B5B5J <= <<"<<> Bc BS BT B$h''$,=_=&;,,9J9JRUZ^&!HRW-!S!T!6CHhsCx.@-A8CQTHCU-U$V5C5C5r)