
    M/eF              	       d   d Z ddlZddlmZ ddlZddlmZ ddlZddl	Z	ddl
Z
ddl
mZ ddlZddlZddlZddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlZddlmZ ddlmZ ddlmZ ddlmZ ddl Z!ddl"m#Z# ddl$m%Z% ddl$m&Z& ddl'm(Z( ddl'm)Z) ddl'm*Z* ddl+m,Z- ddl.m/Z/ ddl.m0Z0 ddl1m&Z2 ddl3m4Z4 ejj                  dk\  rddl6m7Z8 nddl8Z8 G d de4jr                        Z:de;d e;fd!Z<de;d e=fd"Z>d#e;d$e?d%e?d e?fd&Z@de;d e#j                  fd'ZBde;d e#j                  fd(ZDde;d e!j                  fd)ZFde;d e!j                  fd*ZHde;d e#j                  fd+ZJdId,e;d-e;d.eKd e;fd/ZLd ej                  fd0ZN	 dJd1ee   d ej                  fd2ZO G d3 d4      ZPd ePfd5ZQd1ed ePfd6ZRd7ed8ed dfd9ZS G d: d;ej                        ZU G d< d=eU      ZVd>ej                  d?ej                  d@e;d dfdAZXdBeg ef   dCe;d dfdDZYdEe;d eedFef   gedFef   f   fdGZZd@e;d e;fdHZ[y)KzTest 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                       e Zd ZdZdee   fdZdedededededd	fd
Z	 ddededee	e
e   ef      dd	fdZde
e   fdZddee   dedd	fdZddZddZeded   dd	fd       ZddZdefdZy	)DummyInstallerz(Dummy installer plugin for test purpose.returnc                     g S N selfs    4/usr/lib/python3/dist-packages/certbot/tests/util.pyget_all_nameszDummyInstaller.get_all_names1       	    domain	cert_pathkey_path
chain_pathfullchain_pathNc                      y r"   r#   )r%   r*   r+   r,   r-   r.   s         r&   deploy_certzDummyInstaller.deploy_cert4       r)   enhancementoptionsc                      y r"   r#   )r%   r*   r2   r3   s       r&   enhancezDummyInstaller.enhance8   r1   r)   c                     g S r"   r#   r$   s    r&   supported_enhancementsz%DummyInstaller.supported_enhancements<   r(   r)   title	temporaryc                      y r"   r#   )r%   r8   r9   s      r&   savezDummyInstaller.save?       r)   c                      y r"   r#   r$   s    r&   config_testzDummyInstaller.config_testB   r<   r)   c                      y r"   r#   r$   s    r&   restartzDummyInstaller.restartE   r<   r)   add).Nc                      y r"   r#   )clsrA   s     r&   add_parser_argumentsz#DummyInstaller.add_parser_argumentsH   r1   r)   c                      y r"   r#   r$   s    r&   preparezDummyInstaller.prepareL   r<   r)   c                      y)N r#   r$   s    r&   	more_infozDummyInstaller.more_infoO   s    r)   r"   )NFr    N)__name__
__module____qualname____doc__r
   strr'   r0   r   r   r   r5   r7   boolr;   r>   r@   classmethodr   rD   rF   rI   r#   r)   r&   r   r   /   s    2x} # #  RU $',0
 <@c  !%S	3"78DHS	 (3- 4 D  x	': t  3 r)   r   namesr    c                     t               }t        j                  |j                          t	        j
                  t              j                  dg|  }|j                  t	        j                  |            }t        |      S )zPath to a test vector.testdata)r   atexitregistercloseimportlib_resourcesfiles__package__joinpathenter_contextas_filerO   )rR   _file_manager
vector_refpaths       r&   vector_pathra   S   sd    KM
OOM''(@$**;7@@TeTJ&&':'B'B:'NODt9r)   c                       t        j                  t              j                  dg|  }|j	                         }	 |j                         j                  dd      j                         S # t        $ r |cY S w xY w)zLoad contents of a test vector.rT   z

)	rX   rY   rZ   r[   
read_bytesdecodereplaceencode
ValueError)rR   r_   datas      r&   load_vectorrj   \   sp    @$**;7@@TeTJ  "D{{}$$VT299;;  s   -A* *A87A8filename
loader_pem
loader_derc                     t         j                  j                  |       \  }}|j                         dk(  r|S |j                         dk(  r|S t	        d      )Nz.pemz.derz1Loader could not be recognized based on extension)r   r`   splitextlowerrh   )rk   rl   rm   _exts        r&   _guess_loaderrs   i   sN    WWh'FAs
yy{f		
H
IIr)   c                      t        | d   t        j                  t        j                        }t        j                  |t        |        S )zLoad certificate.)rs   r   FILETYPE_PEMFILETYPE_ASN1load_certificaterj   rR   loaders     r&   	load_certr{   r   s;    b	6&&(<(<>F""6;+>??r)   c                      t        | d   t        j                  t        j                        }t        j                  |t        |        S )zLoad certificate request.ru   )rs   r   rv   rw   load_certificate_requestrj   ry   s     r&   load_csrr~   y   s;    b	6&&(<(<>F**6;3FGGr)   c                  8    t        j                  t        |        S )z(Load ComparableX509 certificate request.)joseComparableX509r~   )rR   s    r&   load_comparable_csrr      s    x/00r)   c                  6   t        | d   t        j                  t        j                        }|t        j                  k(  rt        j
                  }nt        j                  }t        j                  t        t         |t        |  dt                                 S )zLoad RSA private key.ru   N)passwordbackend)rs   r   rv   rw   r   load_pem_private_keyload_der_private_keyr   ComparableRSAKeyr   r   rj   r   )rR   rz   	loader_fns      r&   load_rsa_private_keyr      sx    59f&9&96;O;OPF$$$!66	!66	  ]{E*T?CTU	WX Xr)   c                      t        | d   t        j                  t        j                        }t        j                  |t        |        S )zLoad pyOpenSSL private key.ru   )rs   r   rv   rw   load_privatekeyrj   ry   s     r&   load_pyopenssl_private_keyr      s;    b	6&&(<(<>F!!&+u*=>>r)   
config_dirtestfileecc           
          |dt        d        }t        j                  j                   t        j
                        }t        j                  j                   t        j                  |      }t        j                  j                   t        j                  |      }|||fD ]7  }t        j                  j                  |      r#t        j                  |       9 t        dj                  |rdnd            }t        j                  |      D ]T  }	t        j                  t        j                  j                  ||	      t        j                  j                  ||	             V t         j"                  D ]r  }	t        j$                  t        j                  j                  |dj                  |	            t        j                  j                  |dj                  |	                   t t        j                  j                   ||      }
t'        t        |            5 }t'        |
d      5 }|j)                   fd	|D               ddd       ddd       |
S # 1 sw Y   xY w# 1 sw Y   |
S xY w)
a  Creates 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.confzsample-archive{}z-ecrH   z{0}1.pemz{0}.pemwc              3   B   K   | ]  }|j                  d         yw)MAGICDIRN)rf   ).0liner   s     r&   	<genexpr>zmake_lineage.<locals>.<genexpr>   s#      F9=Z4Fs   )lenr   r`   joinr   RENEWAL_CONFIGS_DIRARCHIVE_DIRLIVE_DIRexistsr   makedirsra   formatlistdirshutilcopyfiler   ALL_FOURsymlinkopen
writelines)r   r   r   lineage_nameconf_dirarchive_dirlive_dir	directorysample_archivekind	conf_pathsrcdsts   `            r&   make_lineager      s    Nc'l]+Lww||I113H'',,I))<9Kww||I&&6H "8X7 +	ww~~i(	*+ !!3!:!:B5B!OPN

>* 9^T:[$7	99    C


277<<Z->->t-DE77<<)*:*:4*@A	CC Z8<I	k(#	$ F)S! 	FSNN FADF F	FF
 		F 	FF
 s$    I&-II&I#	I&&I0c                  h    t        t        j                  t        j                  dt                    S )aD  Patch certbot.display.util to use a special mock display utility.

    The mock display utility works like a regular mock object, except it also
    also asserts that methods are called with valid arguments.

    The mock created by this patch mocks out Certbot internals. That is, the
    mock object will be called by the certbot.display.util functions and the
    mock returned by that call will be used as the display utility. This was
    done to simplify the transition from zope.component and mocking
    certbot.display.util functions directly in test code should be preferred
    over using this function in the future.

    See https://github.com/certbot/certbot/issues/8948

    :returns: patch on the function used internally by certbot.display.util to
        get a display utility instance
    :rtype: mock.MagicMock

    )certbot._internal.display.obj.get_display)new_callable)r   r   	MagicMockpatch_create_display_util_mockr#   r)   r&   patch_display_utilr      s+    ( 

+V8Q!S T Tr)   stdoutc           	          | r| nt        j                         } t        t        j                  t        j
                  dt        |                   S )a  Patch certbot.display.util to use a special mock display utility.

    The mock display utility works like a regular mock object, except it also
    asserts that methods are called with valid arguments.

    The mock created by this patch mocks out Certbot internals. That is, the
    mock object will be called by the certbot.display.util functions and the
    mock returned by that call will be used as the display utility. This was
    done to simplify the transition from zope.component and mocking
    certbot.display.util functions directly in test code should be preferred
    over using this function in the future.

    See https://github.com/certbot/certbot/issues/8948

    The `message` argument passed to the display utility methods is passed to
    stdout's write method.

    :param object stdout: object to write standard output to; it is
        expected to have a `write` method
    :returns: patch on the function used internally by certbot.display.util to
        get a display utility instance
    :rtype: mock.MagicMock

    r   )new)ioStringIOr   r   r   r   %_create_display_util_mock_with_stdout)r   s    r&   patch_display_util_with_stdoutr      s@    4 V2;;=F

+V/TU[/\!^ _ _r)   c            	           e Zd ZdZddej
                  j                  fdedee	de
f      de
ddfd	Zdd
Zde
de
dej                  fdZdede
fdZdede
ddfdZy)FreezableMocka  Mock object with the ability to freeze attributes.

    This class works like a regular mock.MagicMock object, except
    attributes and behavior set before the object is frozen cannot
    be changed during tests.

    If a func argument is provided to the constructor, this function
    is called first when an instance of FreezableMock is called,
    followed by the usual behavior defined by MagicMock. The return
    value of func is ignored.

    FNfrozenfunc.return_valuer    c                     |r
t               ndh| _        || _        t        j                         | _        |t        j                  j                  k7  r|| _        || _	        y )Nfreeze)
set_frozen_set_funcr   r   _mocksentinelDEFAULTr   _frozen)r%   r   r   r   s       r&   __init__zFreezableMock.__init__  sH    $*35
^^%
4==000 ,Dr)   c                     d| _         y)z)Freeze object preventing further changes.TN)r   r$   s    r&   r   zFreezableMock.freeze  s	    r)   argskwargsc                 b    | j                    | j                   |i |  | j                  |i |S r"   )r   r   )r%   r   r   s      r&   __call__zFreezableMock.__call__  s5    ::!DJJ''tzz4*6**r)   namec                 2   |dk(  r	 t         j                  | |      S |dv r t        t         j                  | d      |      S |dk(  s|| j                  v rt         j                  | |      S t        t         j                  | d      |      S # t        $ r Y yw xY w)Nr   Fr   side_effectr   r   )object__getattribute__AttributeErrorgetattrr   )r%   r   s     r&   r   zFreezableMock.__getattribute__  s    9..tT:: 556224A4HH]"dd.>.>&>**4666224A4HH " s   B
 
	BBvaluec                 $   | j                   r3|| j                  v rt        d|z         t        | j                  ||      S |dk7  r| j                  j                  |       |dv rt        | j                  ||      S t        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.

        zCannot change frozen attribute r   r   )r   r   r   setattrr   rA   r   __setattr__)r%   r   r   s      r&   r   zFreezableMock.__setattr__*  s     <<t'''$%F%MNN4::tU33=   &224::tU33!!$e44r)   rJ   )rK   rL   rM   rN   r   r   r   rP   r   r   r   r   r   r   r   rO   r   r   r#   r)   r&   r   r      s     ',RV%)]]%:%:t 8HS#X<N3O "?C+c +S +T^^ +
IS IS I5 5C 5D 5r)   r   c            	      d   t               } t        t        j                        D cg c]8  }t	        t        t        j                  |            r|j                  d      s|: }}|D ]&  }|dk7  s	t        dt              }t        | ||       ( | j                          t        d|       S c c}w )N__notificationTr   r   r   r   )
r   dirdisplay_objFileDisplaycallabler   
startswith_assert_valid_callr   r   )displayr   method_listmethodfrozen_mocks        r&   r   r   C  s    oG$'(?(?$@ 2Dw{'>'>EF??40  2K 2  2^#'t:LMKGV[12 NN7;;2s   =B-c           	          dt         dt        dt        dd f fddt        dt        dd ffd}t               }t        t        j
                        D cg c]8  }t        t        t        j
                  |            r|j                  d	      s|: }}|D ]/  }|d
k(  rt        d      }nt        d|      }t        |||       1 |j                          t        d|      S c c}w )Nmessageunused_argsunused_kwargsr    c                 .    | rj                  |        yy)z$Write to message to stdout.
        N)write)r   r   r   r   s      r&   
_write_msgz9_create_display_util_mock_with_stdout.<locals>._write_msgR  s     LL! r)   r   r   c                  .    t        | |        | i | y)z<
        Mock function for display utility methods.
        N)r   )r   r   r   s     r&   mock_methodz:_create_display_util_mock_with_stdout.<locals>.mock_methodX  s     	4(D#F#r)   r   r   Tr   r   )rO   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   s   `      @r&   r   r   Q  s    "C "s "S "T "$3 $# $$ $ oG$'(?(?$@ 2Dw{'>'>EF??40  2K 2  .^#'t-79K (t-8:K-. NN7;;2s   =C&r   r   c                      | r| d   n|d   g}|j                  dd       |j                  dd       |j                  dd      d}t        j                  |i | y )Nr   r   defaultcli_flagforce_interactiveF)r   r   r   )getdisplay_utilassert_valid_call)r   r   assert_argsassert_kwargss       r&   r   r   p  s`    "47y(9:K ::i.JJz40#ZZ(;UCM ""KA=Ar)   c                        e Zd ZdZddZddZy)TempDirTestCasezBBase test class which sets up and tears down a temporary directoryNc                 6    t        j                         | _        y)zExecute before testN)tempfilemkdtemptempdirr$   s    r&   setUpzTempDirTestCase.setUp  s    '')r)   c                     t        j                          g t        j                         _        t	        j
                          t        j                  | j                         y)zExecute after testN)	loggingshutdown	getLoggerhandlersr   _release_locksr   rmtreer  r$   s    r&   tearDownzTempDirTestCase.tearDown  s?     	 (*$dll#r)   rJ   )rK   rL   rM   rN   r  r  r#   r)   r&   r  r  |  s    L*$r)   r  c                   $     e Zd ZdZd fdZ xZS )ConfigTestCasez2Test class which sets up a NamespaceConfig object.c           	         t         |           t        j                  t	        j
                  di t        j                  t        j                              | _
        | j                  j                  i        d| j                  j                  _        t        j                  j!                  | j"                  d      | j                  j                  _        t        j                  j!                  | j"                  d      | j                  j                  _        t        j                  j!                  | j"                  d      | j                  j                  _        t        j                  d   | j                  j                  _        t        j                  d   | j                  j                  _        t        j                  d   | j                  j                  _        d| j                  j                  _        y )	Ncertonlyconfigworklogsauth_cert_pathauth_chain_pathzhttps://example.comr#   )superr  r   NamespaceConfigr   r   copydeepcopyr   CLI_DEFAULTSr  set_argument_sources	namespaceverbr   r`   r   r  r   work_dirlogs_dirr+   r.   r-   server)r%   	__class__s    r&   r  zConfigTestCase.setUp  s=   #33NNCT]]9+A+ABC
 	((,%/"+-77<<h+O()+dllF)K&)+dllF)K&*3*@*@AQ*R'/8/E/EFW/X,+4+A+ABS+T('<$r)   rJ   )rK   rL   rM   rN   r  __classcell__)r$  s   @r&   r  r    s    <= =r)   r  event_in	event_outr`   c                 8   t         j                  j                  |      rt        j                  |      }nt        j
                  |      }	 |j                          | j                  d      sJ d       	 |j                          y# |j                          w xY w)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)	r   r`   isdirr   lock_dirLockFiler   waitrelease)r&  r'  r`   my_locks       r&   _handle_lockr2    sm     
ww}}T--%--%}}R}(V*VV(s   )B Bcallbackpath_to_lockc                 t   t        t               t        j                         }t        j                         }t        j                  t
        |||f      }|j                          |j                  d      sJ d        |         |j                          |j                  d       |j                  dk(  sJ y)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.r   N)reload_moduler   multiprocessingEventProcessr2  startr/  r   r   exitcode)r3  r4  
emit_eventreceive_eventprocesss        r&   lock_and_callrA    s     $ &&(J#))+M%%\,6|+TVGMMO b)W+WW)JNN LLLq   r)   reason.c                 P     dt         dt        f   dt         dt        f   f fd}|S )zFDecorator to skip permanently a test on Windows. A reason is required.function.r    c                 ^     t        j                  t        j                  dk(        |       S )zWrapped versionwin32)unittestskipIfsysplatform)rD  rB  s    r&   wrapperz skip_on_windows.<locals>.wrapper  s$    ?xs||w6?IIr)   )r   r   )rB  rK  s   ` r&   skip_on_windowsrL    s2    J(38, J#s(1C J Nr)   c                 f    t         j                  j                  t        j                         |       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)
    )r   r`   r   r  
gettempdir)r`   s    r&   	temp_joinrO    s"    
 77<<++-t44r)   )Tr"   )\rN   rU   
contextlibr   r  	importlibr   r8  r   r	  r9  r   r   rI  r  typingr   r   r   r	   r
   r   r   r   rG  r   cryptography.hazmat.backendsr   cryptography.hazmat.primitivesr   -cryptography.hazmat.primitives.asymmetric.rsar   josepyr   OpenSSLr   certbotr   r   certbot._internalr   r   r   certbot._internal.displayr   r   certbot.compatr   r   certbot.displayr   certbot.pluginsr   version_infoimportlib.resources	resourcesrX   	Installerr   rO   ra   bytesrj   intrs   X509r{   X509Reqr~   r   r   r   r   PKeyr   rP   r   r   r   r   r   r   r   r   TestCaser  r  r:  r2  rA  rL  rO  r#   r)   r&   <module>rh     s        - 	   '  
            8 8 G   !  ' " % 8 %  0 "v5!V%% !H  
 
 
JC JS Jc Jc J@c @fkk @HS HV^^ H1 1(;(; 1

X 
X)>)> 
X?s ?v{{ ?*S *C *T *S *ZTDNN T2  $__(,_@B5 B5J<= <<" < <>	Bc 	BS 	BT 	B$h'' $,=_ =&;,, 9J9J RU Z^ &!HRW- !S !T !6C HhsCx.@-A8CQTHCU-U$V 5C 5C 5r)   