
\\*                 @   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 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 Gd d	   d	 e j  Z Gd
 d   d e  Z d d d d d  Z d d   Z d S)z$Common utilities for certbot_apache.    N)util)common)configurator)
entrypoint)objc                   s7   e  Z d  Z d d d   f d d  Z d d   Z   S)
ApacheTestz!debian_apache_2_4/multiple_vhostsz)debian_apache_2_4/multiple_vhosts/apache2z9debian_apache_2_4/multiple_vhosts/apache2/sites-availablec                so  t  t |   j   t j d | d d  \ |  _ |  _ |  _ t j	 j
 |  j |  |  _ t j	 j
 |  j |  |  _ t j j t j d   |  _ t |  j | |  j |  j  |  _ t j	 j
 |  j d  } t j	 j |  s d  Sx t j |  D]w } | d k rq t j	 j
 | |  } t j	 j |  s t j |  t j	 j
 t j	 j d |  } t j | |  q Wd  S)Ntest_dirpkgzcertbot_apache.testszrsa512_key.pemzsites-enabledznon-symlink.confzsites-available)superr   setUpr   Z	dir_setuptemp_dir
config_dirwork_dirospathjoinconfig_path
vhost_pathjoseZJWKRSAload	test_utilZload_vectorZ	rsa512jwkget_apache_configuratorconfigexistslistdirislinkremovepardirsymlink)selfr   config_root
vhost_rootZsites_enabledZvhost_basenameZvhosttarget)	__class__ ;/usr/lib/python3/dist-packages/certbot_apache/tests/util.pyr      s,    		zApacheTest.setUpc             C   s4   t  j |  j  t  j |  j  t  j |  j  d  S)N)shutilZrmtreer   r   r   )r   r$   r$   r%   tearDown=   s    zApacheTest.tearDown)__name__
__module____qualname__r   r'   r$   r$   )r#   r%   r      s   "r   c                   s+   e  Z d  Z d d d   f d d  Z   S)
ParserTestz!debian_apache_2_4/multiple_vhostsz)debian_apache_2_4/multiple_vhosts/apache2z9debian_apache_2_4/multiple_vhosts/apache2/sites-availablec                s   t  t |   j | | |  t j j t j t j	 d   d d l
 m } t j d t j j t j j B |  _ t j d  , | |  j |  j |  j d |  j |  _ Wd  QRXd  S)NFr   )ApacheParserflagsz;certbot_apache.parser.ApacheParser.update_runtime_variablesr   )r
   r+   r   zopeZ	componentZprovideUtilitydisplay_utilZFileDisplaysysstdoutZcertbot_apache.parserr,   augeasZAugeasZNONEZNO_MODL_AUTOLOADZaugmockpatchr   r   r   parser)r   r   r    r!   r,   )r#   r$   r%   r   E   s    
	zParserTest.setUp)r(   r)   r*   r   r$   r$   )r#   r%   r+   C   s   r+            Zgenericc          $   C   s^  t  j j | d  } t j d |  d d d d d |  d d d	 | d
 | d d d t  j j | d  d t  j j | d  d |  } t j d   t j d   }	 d |	 _ t j d   y t j | }
 Wn t	 k
 r t
 j }
 Yn X|
 d | d d d |  } | s| |
 j d <n | | j _ |
 j d | j _ | j   Wd QRXWd QRXWd QRX| S)zCreate an Apache Configurator with the specified options.

    :param conf: Function that returns binary paths. self.conf in Configurator

    backupsZapache_server_rootapache_vhost_rootNZapache_le_vhost_extz-le-ssl.confZapache_challenge_locationZapache_enmodZ
backup_dirr   Zhttp01_portP   Ztemp_checkpoint_dirZtemp_checkpointsZin_progress_dirZIN_PROGRESSr   z+certbot_apache.configurator.util.run_scriptz+certbot_apache.configurator.util.exe_existsTz;certbot_apache.parser.ApacheParser.update_runtime_variablesr   nameZapacheversionr!   Zctl)r   r   r   r3   Z	MagicMockr4   Zreturn_valuer   ZOVERRIDE_CLASSESKeyErrorr   ZApacheConfiguratorZOS_DEFAULTSr   r:   Z
apache_ctlZprepare)r   r   r   r   r=   Zos_infoZconf_vhost_pathr9   Zmock_le_configZmock_exe_existsZconfig_classr   r$   r$   r%   r   W   s<    
				r   c             C   s  | d k rVt  j j |  | d  } d | } t j t  j j | d  t  j j | d  t t j j d  g  d d d	  t j t  j j | d
  t  j j | d  t t j j d  g  d d  t j t  j j | d  t  j j | d  t t j j d  t j j d  g  d d d  t j t  j j | d  t  j j | d  t t j j d  g  d d d d d g t j t  j j | d  t  j j | d  t t j j d  g  d d d d t j t  j j | d  t  j j | d  t t j j d  g  d d  t j t  j j | d  t  j j | d  t t j j d  g  d d d d d g t j t  j j | d  t  j j | d  t t j j d   g  d d d!  t j t  j j | d"  t  j j | d#  t t j j d  g  d d d$  t j t  j j | d  t  j j | d%  t t j j d  g  d d d&  t j t  j j |  | d'  d t  j j |  | d(  t t j j d  g  d d d)  t j t  j j | d*  t  j j | d+  t t j j d,  g  d d d-  t j t  j j | d.  t  j j | d/  t t j j d   g  d d d-  g } | S| d0 k rt  j j |  | d1  } d | } t j t  j j | d2  t  j j | d3  t t j j d  g  d d d  t j t  j j | d2  t  j j | d4  t t j j d  g  d d d5  t j t  j j | d6  t  j j | d7  t t j j d  g  d d d8  t j t  j j | d6  t  j j | d9  t t j j d  g  d d d:  t j t  j j | d6  t  j j | d;  t t j j d  g  d d d<  g } | Sd= S)>z4Return the ground truth for the specified directory.z!debian_apache_2_4/multiple_vhostszapache2/sites-enabledz/fileszencryption-example.confz#encryption-example.conf/Virtualhostz*:80FTzencryption-example.demozdefault-ssl.confz%default-ssl.conf/IfModule/VirtualHostz_default_:443z000-default.confz000-default.conf/VirtualHostz[::]:80zip-172-30-0-17zcertbot.confzcertbot.conf/VirtualHostzcertbot.demoaliaseszwww.certbot.demozmod_macro-example.confz(mod_macro-example.conf/Macro/VirtualHostZmodmacrozdefault-ssl-port-only.confz/default-ssl-port-only.conf/IfModule/VirtualHostzwildcard.confzwildcard.conf/VirtualHostz*.blue.purple.comzocsp-ssl.confz"ocsp-ssl.conf/IfModule/VirtualHostz10.2.3.4:443zocspvhost.comznon-symlink.confznon-symlink.conf/VirtualHostznonsym.linkz&default-ssl-port-only.conf/VirtualHost zapache2/apache2.confz apache2/apache2.conf/VirtualHostzvhost.in.rootconfzduplicatehttp.confzduplicatehttp.conf/VirtualHostz10.2.3.4:80zduplicate.example.comzduplicatehttps.confz(duplicatehttps.conf/IfModule/VirtualHostzdebian_apache_2_4/multi_vhostszapache2/sites-availablezdefault.confzdefault.conf/VirtualHost[1]zdefault.conf/VirtualHost[2]zbanana.vomit.comzmulti-vhost.confzmulti-vhost.conf/VirtualHost[1]z1.multi.vhost.tldz%multi-vhost.conf/IfModule/VirtualHostz2.multi.vhost.tldzmulti-vhost.conf/VirtualHost[2]z3.multi.vhost.tldN)r   r   r   r   ZVirtualHostsetZAddrZ
fromstring)r   Zconfig_nameprefixZaug_preZvh_truthr$   r$   r%   get_vh_truth   s    	
!!!$	
rC   )r6   r7   r8   )__doc__r   r&   r0   Zunittestr2   Zjosepyr   r3   Zzope.componentr.   Zcertbot.displayr   r/   Zcertbot.pluginsr   Zcertbot.testsr   Zcertbot_apacher   r   r   ZTestCaser   r+   r   rC   r$   r$   r$   r%   <module>   s(   ,*