ó
Y[c           @   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 Z d d l m Z d d l	 m
 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 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z  d „  Z! d „  Z" d „  Z# d d „ Z$ d d d „ Z& d e' f d „  ƒ  YZ( d „  Z) d „  Z* d „  Z+ d e j, f d „  ƒ  YZ- d e- f d  „  ƒ  YZ. d! „  Z/ d" „  Z0 d S(#   sJ   Test utilities.

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

iÿÿÿÿN(   t   default_backend(   t   serialization(   t   reload_module(   t	   constants(   t
   interfaces(   t   storage(   t   util(   t   configurationc          G   s   t  j t t j j d |  Œ ƒ S(   s   Path to a test vector.t   testdata(   t   pkg_resourcest   resource_filenamet   __name__t   ost   patht   join(   t   names(    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyt   vector_path   s    c          G   s   t  j t t j j d |  Œ ƒ S(   s   Load contents of a test vector.R   (   R	   t   resource_stringR   R   R   R   (   R   (    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyt   load_vector$   s    c         C   sT   t  j j |  ƒ \ } } | j ƒ  d k r. | S| j ƒ  d k rD | St d ƒ ‚ d  S(   Ns   .pems   .ders1   Loader could not be recognized based on extension(   R   R   t   splitextt   lowert
   ValueError(   t   filenamet
   loader_pemt
   loader_dert   _t   ext(    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyt   _guess_loader+   s    c          G   s;   t  |  d t j j t j j ƒ } t j j | t |  Œ  ƒ S(   s   Load certificate.iÿÿÿÿ(   R   t   OpenSSLt   cryptot   FILETYPE_PEMt   FILETYPE_ASN1t   load_certificateR   (   R   t   loader(    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyt	   load_cert5   s    c          G   s;   t  |  d t j j t j j ƒ } t j j | t |  Œ  ƒ S(   s   Load certificate request.iÿÿÿÿ(   R   R   R   R   R   t   load_certificate_requestR   (   R   R!   (    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyt   load_csr<   s    c          G   s   t  j t |  Œ  ƒ S(   s(   Load ComparableX509 certificate request.(   t   joset   ComparableX509R$   (   R   (    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyt   load_comparable_csrC   s    c          G   sD   t  |  d t j t j ƒ } t j | t |  Œ  d d d t ƒ  ƒƒ S(   s   Load RSA private key.iÿÿÿÿt   passwordt   backendN(	   R   R   t   load_pem_private_keyt   load_der_private_keyR%   t   ComparableRSAKeyR   t   NoneR    (   R   R!   (    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyt   load_rsa_private_keyH   s    	c          G   s;   t  |  d t j j t j j ƒ } t j j | t |  Œ  ƒ S(   s   Load pyOpenSSL private key.iÿÿÿÿ(   R   R   R   R   R   t   load_privatekeyR   (   R   R!   (    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyt   load_pyopenssl_private_keyP   s    c         C   s7   t  t d ƒ r t j |  | ƒ S|  r, d „  Sd „  Sd S(   sq  Skip tests unless a condition holds.

    This implements the basic functionality of unittest.skipUnless
    which is only available on Python 2.7+.

    :param bool condition: If ``False``, the test will be skipped
    :param str reason: the reason for skipping the test

    :rtype: callable
    :returns: decorator that hides tests unless condition is ``True``

    t
   skipUnlessc         S   s   |  S(   N(    (   t   cls(    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyt   <lambda>g   s    c         S   s   d  S(   N(   R-   (   R2   (    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyR3   i   s    N(   t   hasattrt   unittestR1   (   t	   conditiont   reason(    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyt   skip_unlessW   s
    c            s¯  | t  d ƒ  } t j j ˆ  t j ƒ } t j j ˆ  t j | ƒ } t j j ˆ  t j | ƒ } x9 | | | f D]( } t j j | ƒ so t j	 | ƒ qo qo Wt
 d ƒ } xE t j | ƒ D]4 } t j t j j | | ƒ t j j | | ƒ ƒ q· WxQ t j D]F } t j t j j | d j | ƒ ƒ t j j | d j | ƒ ƒ ƒ qù Wt j j ˆ  | | ƒ }	 t t
 | ƒ ƒ < }
 t |	 d ƒ $ } | j ‡  f d †  |
 Dƒ ƒ Wd QXWd QX|	 S(   sp  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

    s   .confs   sample-archives   {0}1.pems   {0}.pemt   wc         3   s!   |  ] } | j  d  ˆ  ƒ Vq d S(   t   MAGICDIRN(   t   replace(   t   .0t   line(   t
   config_dir(    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pys	   <genexpr>“   s    N(   t   lenR   R   R   R   t   RENEWAL_CONFIGS_DIRt   ARCHIVE_DIRt   LIVE_DIRt   existst   makedirsR   t   listdirt   shutilt   copyfileR   t   ALL_FOURt   symlinkt   formatt   opent
   writelines(   R>   t   testfilet   lineage_namet   conf_dirt   archive_dirt   live_dirt	   directoryt   sample_archivet   kindt	   conf_patht   srct   dst(    (   R>   s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyt   make_lineagel   s.    			!##s   zope.component.getUtilityc         C   s   t  j |  d t ƒS(   s;  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

    t   new_callable(   t   mockt   patcht   _create_get_utility_mock(   t   target(    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyt   patch_get_utility˜   s    c         C   s7   | r | n	 t  j ƒ  } t | ƒ } t j |  d | ƒS(   s  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

    t   new(   t   sixt   StringIOt$   _create_get_utility_mock_with_stdoutRZ   R[   (   R]   t   stdoutt   freezable_mock(    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyt   patch_get_utility_with_stdout§   s    t   FreezableMockc           B   sJ   e  Z d  Z e d e j j d „ Z d „  Z	 d „  Z
 d „  Z d „  Z RS(   sÄ  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.

    c         C   sa   | r t  ƒ  n	 t  d ƒ |  _ | |  _ t j ƒ  |  _ | t j j k rT | |  _ n  | |  _	 d  S(   Nt   freeze(   s   freeze(
   t   sett   _frozen_sett   _funcRZ   t	   MagicMockt   _mockt   sentinelt   DEFAULTt   return_valuet   _frozen(   t   selft   frozent   funcRo   (    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyt   __init__Ì   s    	c         C   s   t  |  _ d S(   s)   Freeze object preventing further changes.N(   t   TrueRp   (   Rq   (    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyRg   Ô   s    c         O   s2   |  j  d  k	 r" |  j  | | Ž  n  |  j | | Ž  S(   N(   Rj   R-   Rl   (   Rq   t   argst   kwargs(    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyt   __call__Ø   s    c         C   s¥   | d k r8 y t  j |  | ƒ SWq¡ t k
 r4 t SXni | d k r] t t  j |  d ƒ | ƒ S| d k sx | |  j k rˆ t  j |  | ƒ St t  j |  d ƒ | ƒ Sd  S(   NRp   Ro   t   side_effectRl   Ri   (   s   return_valueRy   (   t   objectt   __getattribute__t   AttributeErrort   Falset   getattrRi   (   Rq   t   name(    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyR{   Ý   s    c         C   s–   |  j  rA | |  j k r+ t d | ƒ ‚ qA t |  j | | ƒ Sn  | d k r` |  j j | ƒ n  | d k r t |  j | | ƒ St j |  | | ƒ Sd S(   sÍ   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.

        s   Cannot change frozen attribute Ri   Ro   Ry   N(   s   return_values   side_effect(   Rp   Ri   R|   t   setattrRl   t   addRz   t   __setattr__(   Rq   R   t   value(    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyR‚   ê   s    	N(   R   t
   __module__t   __doc__R}   R-   RZ   Rm   Rn   Rt   Rg   Rx   R{   R‚   (    (    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyRf   ¿   s   			c          C   st   t  ƒ  }  xK t j j ƒ  D]: } | d k r t  d t d t ƒ } t |  | | ƒ q q W|  j ƒ  t  d t d |  ƒ S(   Nt   notificationRr   Rs   Ro   (   Rf   R   t   IDisplayR   Ru   t   _assert_valid_callR€   Rg   (   t   displayR   t   frozen_mock(    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyR\     s    	
c            s·   ‡ f d †  ‰  ‡  f d †  } t  ƒ  } xp t j j ƒ  D]_ } | d k rq t  d t d ˆ  ƒ } t | | | ƒ q7 t  d t d | ƒ } t | | | ƒ q7 W| j ƒ  t  d t d | ƒ S(   Nc            s   |  r ˆ  j  |  ƒ n  d S(   s$   Write to message to stdout.
        N(   t   write(   t   messaget   unused_argst   unused_kwargs(   Rc   (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyt
   _write_msg  s    c             s   t  |  | ƒ ˆ  |  | Ž  d S(   s5   
        Mock function for IDisplay methods.
        N(   Rˆ   (   Rv   Rw   (   R   (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyt   mock_method  s    R†   Rr   Rs   Ro   (   Rf   R   R‡   R   Ru   R€   Rg   (   Rc   R   R‰   R   RŠ   (    (   R   Rc   s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyRb     s    			
c          O   sy   |  r |  d n | d g } i  } | j  d d  ƒ | d <| j  d d  ƒ | d <| j  d t ƒ | d <t j | | Ž  d  S(   Ni    RŒ   t   defaultt   cli_flagt   force_interactive(   t   getR-   R}   t   display_utilt   assert_valid_call(   Rv   Rw   t   assert_argst   assert_kwargs(    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyRˆ   -  s    t   TempDirTestCasec           B   s    e  Z d  Z d „  Z d „  Z RS(   sB   Base test class which sets up and tears down a temporary directoryc         C   s   t  j ƒ  |  _ d  S(   N(   t   tempfilet   mkdtempt   tempdir(   Rq   (    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyt   setUp<  s    c         C   s   t  j |  j ƒ d  S(   N(   RF   t   rmtreeRœ   (   Rq   (    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyt   tearDown?  s    (   R   R„   R…   R   RŸ   (    (    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyR™   9  s   	t   ConfigTestCasec           B   s   e  Z d  Z d „  Z RS(   s8   Test class which sets up a NamespaceConfig object.

    c         C   sà   t  t |  ƒ j ƒ  t j t j t j   ƒ |  _	 d |  j	 _
 t j j |  j d ƒ |  j	 _ t j j |  j d ƒ |  j	 _ t j j |  j d ƒ |  j	 _ t j d |  j	 _ t j d |  j	 _ t j d |  j	 _ d |  j	 _ d  S(   Nt   certonlyt   configt   workt   logst   auth_cert_patht   auth_chain_paths   https://example.com(   t   superR    R   R   t   NamespaceConfigRZ   Rk   R   t   CLI_DEFAULTSR¢   t   verbR   R   R   Rœ   R>   t   work_dirt   logs_dirt	   cert_patht   fullchain_patht
   chain_patht   server(   Rq   (    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyR   F  s    (   R   R„   R…   R   (    (    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyR    B  s   c         C   s–   t  t ƒ t j ƒ  } | j ƒ  | | f } t j d t d | ƒ } | j ƒ  | j ƒ  |  ƒ  | j	 ƒ  | j
 ƒ  | j ƒ  | j d k s’ t ‚ d S(   s­   Grab a lock for lock_path and call func.

    :param callable func: object to call after acquiring the lock
    :param str lock_path: path to file or directory to lock

    R]   Rv   i    N(   R   R   t   multiprocessingt	   Conditiont   acquiret   Processt	   hold_lockt   startt   waitt   notifyt   releaseR   t   exitcodet   AssertionError(   Rs   t	   lock_patht   cvt
   child_argst   child(    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyt   lock_and_callT  s    






c         C   so   d d l  m } t j j | ƒ r4 | j | ƒ } n | j | ƒ } |  j ƒ  |  j ƒ  |  j	 ƒ  | j
 ƒ  d S(   s¶   Acquire a file lock at lock_path and wait to release it.

    :param multiprocessing.Condition cv: condition for synchronization
    :param str lock_path: path to the file lock

    iÿÿÿÿ(   t   lockN(   t   certbotRÁ   R   R   t   isdirt   lock_dirt   LockFileR³   R¸   R·   R¹   (   R½   R¼   RÁ   t   my_lock(    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyRµ   n  s    


(1   R…   R±   R   R	   RF   Rš   R5   t   cryptography.hazmat.backendsR    t   cryptography.hazmat.primitivesR   RZ   R   t   josepyR%   R`   t	   six.movesR   RÂ   R   R   R   R   R   t   certbot.displayR•   R   R   R   R"   R$   R'   R.   R0   R8   RX   R^   R-   Re   Rz   Rf   R\   Rb   Rˆ   t   TestCaseR™   R    RÀ   Rµ   (    (    (    s6   /usr/lib/python2.7/dist-packages/certbot/tests/util.pyt   <module>   sP   			
							,F	
				