ok

Mini Shell

Direktori : /lib/python3.6/site-packages/certbot/tests/__pycache__/
Upload File :
Current File : //lib/python3.6/site-packages/certbot/tests/__pycache__/util.cpython-36.pyc

3

گa�M�@s�dZddlmZddlZddlZddlZddlmZddlZddl	Z	ddl
Z
ddlmZddlm
Z
ddlmZddlmZdd	lmZdd
lmZddlmZddlmZddlZddlZdd
lmZddlmZddlZddlmZddlZddlm Z ddlm!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/yddl0Z0ej1de2�Wn"e3k
�r�ddlm0Z0YnXGdd�de/j4�Z5e6e6d�dd�Z7e6e8d�d d!�Z9e6e:e:e:d"�d#d$�Z;e6ej<d�d%d&�Z=e6ej>d�d'd(�Z?e6ej@d�d)d*�ZAe6ejBd�d+d,�ZCe6ejDd�d-d.�ZEdZe6e6eFe6d0�d1d2�ZGd[e6e0jHd4�d5d6�ZId\e6eee0jHd7�d8d9�ZJe0jHd:�d;d<�ZKd]eee0jHd=�d>d?�ZLGd@dA�dA�ZMeMd:�dBdC�ZNeeMd=�dDdE�ZOeeddF�dGdH�ZPGdIdJ�dJejQ�ZRGdKdL�dLeR�ZSejTejTe6ddM�dNdO�ZUe
gefe6ddP�dQdR�ZVe6e
e
dSefge
dSeffdT�dUdV�ZWe6e6dW�dXdY�ZXdS)^zTest utilities.�)�reloadN)�synchronize)�Any)�Callable)�cast)�IO)�Iterable)�List)�Optional)�Union)�default_backend)�
serialization)�crypto)�
configuration)�util)�	constants)�lock)�storage)�obj)�
filesystem)�os)�commonz�The external mock module is being used for backwards compatibility since it is available, however, future versions of Certbot's tests will use unittest.mock. Be sure to update your code accordingly.)�mockc@s�eZdZdZeed�dd�Zeeeeedd�dd�Zdeeee	e
eefdd	�d
d�Ze
ed�dd
�Zdeee
dd�dd�Zdd�dd�Zdd�dd�Zeed dd�dd��Zdd�dd�Zed�dd�ZdS)!�DummyInstallerz(Dummy installer plugin for test purpose.)�returncCsdS)N�)�selfrr�/usr/lib/python3.6/util.py�
get_all_names7szDummyInstaller.get_all_namesN)�domain�	cert_path�key_path�
chain_path�fullchain_pathrcCsdS)Nr)rrr r!r"r#rrr�deploy_cert:szDummyInstaller.deploy_cert)r�enhancement�optionsrcCsdS)Nr)rrr%r&rrr�enhance>szDummyInstaller.enhancecCsdS)Nr)rrrr�supported_enhancementsBsz%DummyInstaller.supported_enhancementsF)�title�	temporaryrcCsdS)Nr)rr)r*rrr�saveEszDummyInstaller.savecCsdS)Nr)rrrr�config_testHszDummyInstaller.config_testcCsdS)Nr)rrrr�restartKszDummyInstaller.restart.)�addrcCsdS)Nr)�clsr.rrr�add_parser_argumentsNsz#DummyInstaller.add_parser_argumentscCsdS)Nr)rrrr�prepareRszDummyInstaller.preparecCsdS)Nr)rrrr�	more_infoUszDummyInstaller.more_info)N)NF).N)�__name__�
__module__�__qualname__�__doc__r�strrr$r
rr	r'r(�boolr+r,r-�classmethodrr0r1r2rrrrr5s&r)�namesrcGstjttjjd|���S)zPath to a test vector.�testdata)r;)�
pkg_resourcesZresource_filenamer3r�path�join)r:rrr�vector_pathYsr?cGsFtjttjjd|���}y|j�jdd�j�St	k
r@|SXdS)zLoad contents of a test vector.r;z
�
N)r;)
r<Zresource_stringr3rr=r>�decode�replace�encode�
ValueError)r:�datarrr�load_vector_srF)�filename�
loader_pem�
loader_derrcCs<tjj|�\}}|j�dkr |S|j�dkr0|Std��dS)Nz.pemz.derz1Loader could not be recognized based on extension)rr=�splitext�lowerrD)rGrHrI�_Zextrrr�
_guess_loadermsrMcGs$t|dtjtj�}tj|t|��S)zLoad certificate.����)rMr�FILETYPE_PEM�
FILETYPE_ASN1Zload_certificaterF)r:�loaderrrr�	load_certvsrScGs$t|dtjtj�}tj|t|��S)zLoad certificate request.rNrO)rMrrPrQZload_certificate_requestrF)r:rRrrr�load_csr}srTcGstjt|��S)z(Load ComparableX509 certificate request.)�jose�ComparableX509rT)r:rrr�load_comparable_csr�srWcGsFt|dtjtj�}|tjkr&tj}ntj}tj|t	|�dt
�d��S)zLoad RSA private key.rNN)ZpasswordZbackendrO)rMrrPrQr
Zload_pem_private_keyZload_der_private_keyrU�ComparableRSAKeyrFr)r:rRZ	loader_fnrrr�load_rsa_private_key�s
rYcGs$t|dtjtj�}tj|t|��S)zLoad pyOpenSSL private key.rNrO)rMrrPrQZload_privatekeyrF)r:rRrrr�load_pyopenssl_private_key�srZF)�
config_dir�testfile�ecrc
sP|dtd��}tjj�tj�}tjj�tj|�}tjj�tj|�}x(|||fD]}tjj|�sRt	j
|�qRWtdj|r~dnd��}x2tj
|�D]$}	tjtjj||	�tjj||	��q�Wx:tjD]0}	tjtjj|dj|	��tjj|dj|	���q�Wtjj�||�}
tt|���4}t|
d��}|j�fd	d
�|D��WdQRXWdQRX|
S)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.confzsample-archive{}z-ec�z{0}1.pemz{0}.pem�wc3s|]}|jd��VqdS)ZMAGICDIRN)rB)�.0�line)r[rr�	<genexpr>�szmake_lineage.<locals>.<genexpr>)�lenrr=r>rZRENEWAL_CONFIGS_DIRZARCHIVE_DIRZLIVE_DIR�existsr�makedirsr?�format�listdir�shutilZcopyfilerZALL_FOUR�symlink�open�
writelines)
r[r\r]Zlineage_nameZconf_dirZarchive_dirZlive_dirZ	directoryZsample_archiveZkindZ	conf_path�src�dstr)r[r�make_lineage�s.
(rn�zope.component.getUtility)�targetrcCs tjd�ttjtj|td��S)z�Deprecated, patch certbot.display.util directly or use patch_display_util instead.

    :param str target: path to patch

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

    z�Decorator certbot.tests.util.patch_get_utility is deprecated. You should now patch certbot.display.util yourself directly or use certbot.tests.util.patch_display_util as a temporary workaround.)�new_callable)�warnings�warnrr�	MagicMock�patch�_create_display_util_mock)rprrr�patch_get_utility�s	
rw)rp�stdoutrcCs8tjd�|r|ntj�}t|�}ttjtj||d��S)aEDeprecated, patch certbot.display.util directly
    or use patch_display_util_with_stdout instead.

    :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

    z�Decorator certbot.tests.util.patch_get_utility_with_stdout is deprecated. You should now patch certbot.display.util yourself directly or use use certbot.tests.util.patch_display_util_with_stdout as a temporary workaround.)�new)	rrrs�io�StringIO�%_create_display_util_mock_with_stdoutrrrtru)rprxZfreezable_mockrrr�patch_get_utility_with_stdout�s
r})rcCsttjtjdtd��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
    also asserts that methods are called with valid arguments.

    The mock created by this patch mocks out Certbot internals so this can be
    used like the old patch_get_utility function. 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

    z)certbot._internal.display.obj.get_display)rq)rrrtrurvrrrr�patch_display_util�sr~)rxrcCs*|r|ntj�}ttjtjdt|�d��S)aWPatch 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 so this can be
    used like the old patch_get_utility function. 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

    z)certbot._internal.display.obj.get_display)ry)rzr{rrrtrur|)rxrrr�patch_display_util_with_stdoutsrc@s|eZdZdZddejjfeede	fe	dd�dd�Z
dd�d	d
�Ze	e	ejd�dd
�Z
ee	d�dd�Zee	dd�dd�ZdS)�
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_valuercCs>|r
t�ndh|_||_tj�|_|tjjkr4||_||_	dS)N�freeze)
�set�_frozen_set�_funcrrt�_mock�sentinel�DEFAULTr��_frozen)rr�r�r�rrr�__init__5s
zFreezableMock.__init__)rcCs
d|_dS)z)Freeze object preventing further changes.TN)r�)rrrrr�>szFreezableMock.freeze)�args�kwargsrcOs"|jdk	r|j||�|j||�S)N)r�r�)rr�r�rrr�__call__Bs
zFreezableMock.__call__)�namercCsz|dkr,ytj||�Stk
r(dSXnJ|dkrFttj|d�|�S|dksX||jkrdtj||�Sttj|d�|�SdS)Nr�Fr��side_effectr�r�)r�r�)�object�__getattribute__�AttributeError�getattrr�)rr�rrrr�GszFreezableMock.__getattribute__)r��valuercCsb|jr*||jkrtd|��t|j||�S|dkr>|jj|�|dkrTt|j||�Stj|||�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 r�r�r�)r�r�)r�r�r��setattrr�r.r��__setattr__)rr�r�rrrr�Ts
zFreezableMock.__setattr__)r3r4r5r6rr�r�r8rrr�r�rtr�r7r�r�rrrrr�(s$
r�cCs\t�}dd�ttj�D�}x,|D]$}|dkr tdtd�}t|||�q W|j�td|d�S)NcSs,g|]$}tttj|��r|jd�r|�qS)�__)�callabler��display_obj�FileDisplay�
startswith)r`r�rrr�
<listcomp>psz-_create_display_util_mock.<locals>.<listcomp>�notificationT)r�r�)r�r�)r��dirr�r��_assert_valid_callr�r�)�display�method_list�method�frozen_mockrrrrvms
rvcs�tttdd��fdd��ttdd��fdd�}t�}dd�ttj�D�}x:|D]2}|d	krhtd
�d�}ntd
|d�}t|||�qNW|j�td
|d�S)
N)�message�unused_args�
unused_kwargsrcs|r�j|�dS)z$Write to message to stdout.
        N)�write)r�r�r�)rxrr�
_write_msg|sz9_create_display_util_mock_with_stdout.<locals>._write_msg)r�r�rcst||��||�dS)z<
        Mock function for display utility methods.
        N)r�)r�r�)r�rr�mock_method�s
z:_create_display_util_mock_with_stdout.<locals>.mock_methodcSs,g|]$}tttj|��r|jd�r|�qS)r�)r�r�r�r�r�)r`r�rrrr��sz9_create_display_util_mock_with_stdout.<locals>.<listcomp>r�T)r�r�)r�r�)r7rr�r�r�r�r�r�)rxr�r�r�r�r�r)r�rxrr|{s

r|)r�r�rcOsZ|r|dn|dg}i}|jdd�|d<|jdd�|d<|jdd�|d<tj||�dS)Nrr��defaultZcli_flagZforce_interactiveF)�get�display_utilZassert_valid_call)r�r�Zassert_argsZ
assert_kwargsrrrr��sr�c@s,eZdZdZdd�dd�Zdd�dd�ZdS)�TempDirTestCasezBBase test class which sets up and tears down a temporary directoryN)rcCstj�|_dS)zExecute before testN)�tempfileZmkdtemp�tempdir)rrrr�setUp�szTempDirTestCase.setUpcCs*tj�gtj�_tj�tj|j�dS)zExecute after testN)	�loggingZshutdownZ	getLoggerZhandlersrZ_release_locksrhZrmtreer�)rrrr�tearDown�s
zTempDirTestCase.tearDown)r3r4r5r6r�r�rrrrr��sr�cs&eZdZdZdd��fdd�Z�ZS)�ConfigTestCasez2Test class which sets up a NamespaceConfig object.N)rcs�t�j�tjtjftj��|_d|jj	_
tjj
|jd�|jj	_tjj
|jd�|jj	_tjj
|jd�|jj	_tjd|jj	_tjd|jj	_tjd|jj	_d|jj	_dS)NZcertonly�configZworkZlogsZauth_cert_pathZauth_chain_pathzhttps://example.com)�superr�rZNamespaceConfigrrtrZCLI_DEFAULTSr��	namespaceZverbrr=r>r�r[Zwork_dirZlogs_dirr r#r"Zserver)r)�	__class__rrr��s

zConfigTestCase.setUp)r3r4r5r6r��
__classcell__rr)r�rr��sr�)�event_in�	event_outr=rc
CsRtjj|�rtj|�}n
tj|�}z |j�|jdd�s@td��Wd|j	�XdS)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=�isdirrZlock_dirZLockFiler��wait�AssertionError�release)r�r�r=Zmy_lockrrr�_handle_lock�s
r�)�callback�path_to_lockrcCsttt�tj�}tj�}tjt|||fd�}|j�|jdd�sHtd��|�|j	�|j
dd�|jdkspt�dS)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
    )rpr��
)r�z*Timeout while waiting to acquire the lock.rN)�
reload_moduler�multiprocessing�EventZProcessr��startr�r�r�r>Zexitcode)r�r�Z
emit_eventZ
receive_eventZprocessrrr�
lock_and_call�sr�.)�reasonrcs(tdtftdtfd��fdd�}|S)zFDecorator to skip permanently a test on Windows. A reason is required..)�functionrcstjtjdk��|�S)zWrapped versionZwin32)�unittestZskipIf�sys�platform)r�)r�rr�wrapper�sz skip_on_windows.<locals>.wrapper)rr)r�r�r)r�r�skip_on_windows�s$r�)r=rcCstjjtj�|�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=r>r�Z
gettempdir)r=rrr�	temp_joinsr�)F)ro)roN)N)Yr6�	importlibrr�rzr�r�rrhr�r�Ztypingrrrrrr	r
rr�rrZcryptography.hazmat.backendsrZcryptography.hazmat.primitivesr
ZjosepyrUZOpenSSLrr<ZcertbotrrZcertbot._internalrrrZcertbot._internal.displayrr�Zcertbot.compatrrZcertbot.displayr�Zcertbot.pluginsrrrs�PendingDeprecationWarning�ImportErrorZ	Installerrr7r?�bytesrF�intrMZX509rSZX509ReqrTrVrWrXrYZPKeyrZr8rnrtrwr}r~rr�rvr|r�ZTestCaser�r�r�r�r�r�r�rrrr�<module>s�
$	- E*

Zerion Mini Shell 1.0