U
    ]7                     @   s  d Z ddlZddlZddlZddlZddlZddlmZmZ ddl	Z	ddl
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mZ ddlmZ ddlmZ ddlmZ  dd Z!dd Z"dd Z#dd Z$dd Z%dd Z&dd Z'dd Z(dd  Z)d:d"d#Z*d;d$d%Z+G d&d' d'e,Z-d(d) Z.d*d+ Z/d,d- Z0G d.d/ d/ej1Z2G d0d1 d1e2Z3d2d3 Z4d4d5 Z5d6d7 Z6d8d9 Z7dS )<zJTest utilities.

.. warning:: This module is not part of the public API.

    N)ProcessEvent)reload_module)default_backend)serialization)configuration)	constants)
interfaces)lock)storage)util)os)
filesystemc                  G   s   t ttjjd|  S )zPath to a test vector.testdata)r   )pkg_resourcesZresource_filename__name__r   pathjoinnames r   4/usr/lib/python3/dist-packages/certbot/tests/util.pyvector_path!   s     r   c                  G   sL   t ttjjd|  }z| dd W S  t	k
rF   | Y S X dS )zLoad contents of a test vector.r   z

N)r   )
r   Zresource_stringr   r   r   r   decodereplaceencode
ValueError)r   datar   r   r   load_vector'   s     r   c                 C   s<   t j| \}}| dkr |S | dkr0|S tdd S )Nz.pemz.derz1Loader could not be recognized based on extension)r   r   splitextlowerr   )filenameZ
loader_pemZ
loader_der_Zextr   r   r   _guess_loader5   s    r$   c                  G   s*   t | d tjjtjj}tj|t|  S )zLoad certificate.)r$   OpenSSLcryptoFILETYPE_PEMFILETYPE_ASN1Zload_certificater   r   loaderr   r   r   	load_cert?   s      r,   c                  G   s*   t | d tjjtjj}tj|t|  S )zLoad certificate request.r%   )r$   r&   r'   r(   r)   Zload_certificate_requestr   r*   r   r   r   load_csrF   s      r-   c                  G   s   t t|  S )z(Load ComparableX509 certificate request.)joseZComparableX509r-   r   r   r   r   load_comparable_csrM   s    r/   c                  G   s.   t | d tjtj}t|t|  dt dS )zLoad RSA private key.r%   N)ZpasswordZbackend)r$   r   Zload_pem_private_keyZload_der_private_keyr.   ZComparableRSAKeyr   r   r*   r   r   r   load_rsa_private_keyR   s      r0   c                  G   s*   t | d tjjtjj}tj|t|  S )zLoad pyOpenSSL private key.r%   )r$   r&   r'   r(   r)   Zload_privatekeyr   r*   r   r   r   load_pyopenssl_private_keyZ   s      r1   c              
      s6  |dt d  }tj tj}tj tj|}tj tj|}|||fD ]}tj|sPt	
| qPtd}t|D ]$}ttj||tj|| q~tjD ]0}ttj|d|tj|d| qtj ||}	tt|4}
t|	d}| fdd|
D  W 5 Q R X W 5 Q R X |	S )	ap  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

    :returns: path to the renewal conf file for the created lineage
    :rtype: str

    Nz.confzsample-archivez{0}1.pemz{0}.pemwc                 3   s   | ]}| d  V  qdS )ZMAGICDIRN)r   ).0line
config_dirr   r   	<genexpr>   s    zmake_lineage.<locals>.<genexpr>)lenr   r   r   r   ZRENEWAL_CONFIGS_DIRZARCHIVE_DIRZLIVE_DIRexistsr   makedirsr   listdirshutilZcopyfiler   ZALL_FOURsymlinkformatopen
writelines)r6   ZtestfileZlineage_nameZconf_dirZarchive_dirZlive_dirZ	directoryZsample_archiveZkindZ	conf_pathsrcZdstr   r5   r   make_lineagea   sD         
rB   zope.component.getUtilityc                 C   s   t j| tdS )a;  Patch zope.component.getUtility to use a special mock IDisplay.

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

    :param str target: path to patch

    :returns: mock zope.component.getUtility
    :rtype: mock.MagicMock

    )Znew_callable)mockpatch_create_get_utility_mock)targetr   r   r   patch_get_utility   s    rH   c                 C   s&   |r|nt  }t|}tj| |dS )a  Patch zope.component.getUtility to use a special mock IDisplay.

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

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

    :param str target: path to patch
    :param object stdout: object to write standard output to; it is
        expected to have a `write` method

    :returns: mock zope.component.getUtility
    :rtype: mock.MagicMock

    )new)sixStringIO$_create_get_utility_mock_with_stdoutrD   rE   )rG   stdoutZfreezable_mockr   r   r   patch_get_utility_with_stdout   s    rN   c                   @   sD   e Zd ZdZddejjfddZdd Zdd	 Z	d
d Z
dd ZdS )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.

    FNc                 C   s>   |r
t  ndh| _|| _t | _|tjjkr4|| _|| _	d S )Nfreeze)
set_frozen_set_funcrD   	MagicMock_mocksentinelDEFAULTreturn_value_frozen)selffrozenfuncrX   r   r   r   __init__   s    
zFreezableMock.__init__c                 C   s
   d| _ dS )z)Freeze object preventing further changes.TN)rY   rZ   r   r   r   rP      s    zFreezableMock.freezec                 O   s"   | j d k	r| j || | j||S )N)rS   rU   )rZ   argskwargsr   r   r   __call__   s    
zFreezableMock.__call__c                 C   s~   |dkr0zt | |W S  tk
r,   Y dS X nJ|dkrJtt | d|S |dks\|| jkrht | |S tt | d|S d S )NrY   FrX   Zside_effectrU   rR   )object__getattribute__AttributeErrorgetattrrR   )rZ   namer   r   r   rd      s    
zFreezableMock.__getattribute__c                 C   sd   | j r,|| jkrtd| nt| j||S |dkr@| j| |dkrVt| j||S t| ||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 rR   rb   )rY   rR   re   setattrrU   addrc   __setattr__)rZ   rg   valuer   r   r   rj      s    
zFreezableMock.__setattr__)r   
__module____qualname____doc__rD   rV   rW   r]   rP   ra   rd   rj   r   r   r   r   rO      s   rO   c                  C   sJ   t  } tj D ]$}|dkrt dtd}t| || q|   t d| dS )NnotificationTr[   r\   r[   rX   )rO   r	   IDisplayr   _assert_valid_callrh   rP   )displayrg   frozen_mockr   r   r   rF      s    rF   c                    s|   fdd  fdd}t  }tj D ]>}|dkrNt d d}t||| q(t d|d}t||| q(|  t d|dS )	Nc                    s   | r  |  dS )z$Write to message to stdout.
        N)write)messageZunused_argsZunused_kwargs)rM   r   r   
_write_msg  s    z8_create_get_utility_mock_with_stdout.<locals>._write_msgc                     s   t | |  | | dS )z5
        Mock function for IDisplay methods.
        N)rs   )r_   r`   )rx   r   r   mock_method  s    
z9_create_get_utility_mock_with_stdout.<locals>.mock_methodro   Trp   rq   )rO   r	   rr   r   rh   rP   )rM   ry   rt   rg   ru   r   )rx   rM   r   rL     s    rL   c                  O   sZ   | r| d n|d g}i }| dd |d< | dd |d< | dd|d< tj|| d S )Nr   rw   defaultZcli_flagZforce_interactiveF)getdisplay_utilZassert_valid_call)r_   r`   Zassert_argsZassert_kwargsr   r   r   rs   #  s    rs   c                   @   s    e Zd ZdZdd Zdd ZdS )TempDirTestCasezBBase test class which sets up and tears down a temporary directoryc                 C   s   t  | _dS )zExecute before testN)tempfileZmkdtemptempdirr^   r   r   r   setUp1  s    zTempDirTestCase.setUpc                 C   s*   t   g t  _t  t| j dS )zExecute after testN)	loggingZshutdownZ	getLoggerZhandlersr   Z_release_locksr<   Zrmtreer   r^   r   r   r   tearDown5  s    
zTempDirTestCase.tearDownN)r   rl   rm   rn   r   r   r   r   r   r   r}   .  s   r}   c                       s    e Zd ZdZ fddZ  ZS )ConfigTestCasez2Test class which sets up a NamespaceConfig object.c                    s   t t|   ttjf tj| _	d| j	_
tj| jd| j	_tj| jd| j	_tj| jd| j	_tjd | j	_tjd | j	_tjd | j	_d| j	_d S )NZcertonlyconfigZworkZlogsZauth_cert_pathZauth_chain_pathzhttps://example.com)superr   r   r   ZNamespaceConfigrD   rT   r   ZCLI_DEFAULTSr   Zverbr   r   r   r   r6   Zwork_dirZlogs_dirZ	cert_pathZfullchain_pathZ
chain_pathZserverr^   	__class__r   r   r   F  s    zConfigTestCase.setUp)r   rl   rm   rn   r   __classcell__r   r   r   r   r   D  s   r   c                 C   sR   t j|rt|}n
t|}z |  | jdds@t	dW 5 |  X dS )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
       Ztimeoutz*Timeout while waiting to release the lock.N)
r   r   isdirr
   Zlock_dirZLockFilereleaserQ   waitAssertionError)Zevent_inZ	event_outr   Zmy_lockr   r   r   _handle_lockU  s    
r   c                 C   sn   t t t }t }tt|||fd}|  |jddsBtd|   |  |j	dd |j
dksjtdS )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
    )rG   r_   
   r   z*Timeout while waiting to acquire the lock.r   N)r   r   r   r   r   startr   r   rQ   r   Zexitcode)callbackZpath_to_lockZ
emit_eventZreceive_eventZprocessr   r   r   lock_and_callh  s    r   c                    s    fdd}|S )zFDecorator to skip permanently a test on Windows. A reason is required.c                    s   t tjdk | S )zWrapped versionZwin32)unittestZskipIfsysplatform)Zfunctionreasonr   r   wrapper  s    z skip_on_windows.<locals>.wrapperr   )r   r   r   r   r   skip_on_windows  s    r   c                 C   s   t jt | 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~   Z
gettempdir)r   r   r   r   	temp_join  s    r   )rC   )rC   N)8rn   r   r<   r   r~   r   Zmultiprocessingr   r   r&   Zjosepyr.   rD   r   rJ   Z	six.movesr   Zcryptography.hazmat.backendsr   Zcryptography.hazmat.primitivesr   Zcertbotr   r   r	   r
   r   r   Zcertbot.compatr   r   Zcertbot.displayr|   r   r   r$   r,   r-   r/   r0   r1   rB   rH   rN   rc   rO   rF   rL   rs   ZTestCaser}   r   r   r   r   r   r   r   r   r   <module>   s\   
,
  
E