U
    ˜­“]Ì@  ã                   @   sx   d 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	 G dd„ de	j
ƒZG dd	„ d	e	jƒZed
krte ¡  dS )z Tests for certbot_apache.parser.é    N)Úerrors)Úos)Úutilc                       s,  e Zd ZdZ‡ f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„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Ze d ¡e d!¡d"d#„ ƒƒZe d ¡e d$¡d%d&„ ƒƒZe d ¡e d$¡d'd(„ ƒƒZe d$¡d)d*„ ƒZe d+¡e d,¡d-d.„ ƒƒZe d,¡d/d0„ ƒZd1d2„ Z‡  ZS )3ÚBasicParserTestzApache Parser Test.c                    s   t t| ƒ ¡  d S ©N)Úsuperr   ÚsetUp©Úself©Ú	__class__© úB/usr/lib/python3/dist-packages/certbot_apache/tests/parser_test.pyr      s    zBasicParserTest.setUpc                 C   s(   t  | j¡ t  | j¡ t  | j¡ d S r   ©ÚshutilZrmtreeÚtemp_dirZ
config_dirZwork_dirr	   r   r   r   ÚtearDown   s    zBasicParserTest.tearDownc                 C   s4   | j  tj | j jdd¡¡ |  tj| j j	d¡ d S )Nzconf-availablezbad_conf_file.confz	httpd.aug)
ÚparserÚ
parse_filer   ÚpathÚjoinÚrootÚassertRaisesr   ÚPluginErrorZcheck_parsing_errorsr	   r   r   r   Útest_bad_parse   s     ÿ  ÿzBasicParserTest.test_bad_parsec                 C   s.   t  ¡ }t|_|| jj_|  tj	| jj
¡ d S r   )ÚmockÚMockÚIOErrorÚside_effectr   ÚaugZsaver   r   r   Zunsaved_files)r
   Z	mock_saver   r   r   Útest_bad_save   s    
zBasicParserTest.test_bad_savec                 C   sL   t jdgd}|| jj_|  | j ¡ dg¡ t| jjj_|  	| j ¡ ¡ d S )NZ	something)Úreturn_value)
r   r   r   r   ÚmatchÚassertEqualZcheck_aug_versionÚRuntimeErrorr   ZassertFalse)r
   Z
mock_matchr   r   r   Útest_aug_version%   s    
ÿz BasicParserTest.test_aug_versionc                 C   s(   t  | jjd ¡ |  tj| jj¡ d S )Nr   )r   Úremover   Úlocr   r   ÚNoInstallationErrorZ_find_config_rootr	   r   r   r   Útest_find_config_root_no_root.   s
     ÿz-BasicParserTest.test_find_config_root_no_rootc                 C   s>   t j | jdd¡}| j |¡ | jj d| ¡}|  |¡ dS )z•Test parse_file.

        certbot.conf is chosen as the test file as it will not be
        included during the normal course of execution.

        znot-parsed-by-defaultzcertbot.confz!/augeas/load/Httpd/incl [. ='%s']N)	r   r   r   Úconfig_pathr   r   r   r"   Ú
assertTrue)r
   Z	file_pathÚmatchesr   r   r   Útest_parse_file4   s      ÿÿzBasicParserTest.test_parse_filec                 C   s>   | j  dd¡}| j  d¡}|  t|ƒd¡ |  t|ƒd¡ d S )NZListenZ80Zdocumentrooté   é   )r   Úfind_dirr#   Úlen)r
   ÚtestZtest2r   r   r   Útest_find_dirF   s    zBasicParserTest.test_find_dirc                 C   s’   d| j jd  }| j  |dd¡ |  | j  dd|¡¡ | j  |ddddd	g¡ | j  dd |¡}t|ƒD ]&\}}|  | j j |¡t	|d
 ƒ¡ qfd S )Nú/filesÚdefaultZAddDirectiver2   ÚAddListÚ1Ú2Ú3Ú4r.   )
r   r'   Zadd_dirr+   r0   Ú	enumerater#   r   ÚgetÚstr©r
   Zaug_defaultr,   Úir"   r   r   r   Útest_add_dirN   s    ÿzBasicParserTest.test_add_dirc                 C   s¬   d| j jd  }| j  |dd¡ |  | j  dd|¡¡ |  | j j |d ¡d¡ | j  |dddd	d
g¡ | j  dd |¡}t|ƒD ]&\}}|  | j j |¡t	|d ƒ¡ q€d S )Nr4   r5   ZAddDirectiveBeginningZ	testBeginz/directive[1]r6   r7   r8   r9   r:   r.   )
r   r'   Zadd_dir_beginningr+   r0   r#   r   r<   r;   r=   r>   r   r   r   Útest_add_dir_beginningZ   s     þÿþz&BasicParserTest.test_add_dir_beginningc                 C   s   |   d | j d¡¡ d S )Nz/files/whatever/nonexistent)r#   r   Zget_argr	   r   r   r   Útest_empty_argk   s    
ÿzBasicParserTest.test_empty_argc                 C   sl   ddl m} | jj d¡ | j || jjd ƒddg¡ | j dd¡}|  t	|ƒd¡ |  
d|d k¡ d	S )
zbtest add_dir_to_ifmodssl.

        Path must be valid before attempting to add to augeas

        r   ©Úget_aug_pathú	mod_ssl.cr5   ÚFakeDirectiveÚ123r.   ÚIfModuleN©Úcertbot_apache.parserrD   r   ÚmodulesÚaddZadd_dir_to_ifmodsslr'   r0   r#   r1   r+   ©r
   rD   r,   r   r   r   Útest_add_dir_to_ifmodsslo   s     þz(BasicParserTest.test_add_dir_to_ifmodsslc                 C   sn   ddl m} | jj d¡ | j || jjd ƒddddg¡ | j d¡}|  t	|ƒd	¡ |  
d
|d k¡ d S )Nr   rC   rE   r5   rF   rG   Z456Z789é   rH   rI   rM   r   r   r   Ú!test_add_dir_to_ifmodssl_multiple‚   s     þz1BasicParserTest.test_add_dir_to_ifmodssl_multiplec                 C   s    ddl m} |  d|dƒ¡ d S )Nr   rC   z/files/etc/apachez/etc/apache)rJ   rD   r#   )r
   rD   r   r   r   Útest_get_aug_path   s    z!BasicParserTest.test_get_aug_pathc              	   C   sX   t  d¡D}ddg|j_| j ¡ }|  |d |d ¡ |  |d |d ¡ W 5 Q R X d S )Nzcertbot_apache.parser.os.pathFr5   ZlistenÚname)r   ÚpatchÚisfiler   r   Z_set_locationsr#   )r
   Z	mock_pathZresultsr   r   r   Útest_set_locations”   s
    
z"BasicParserTest.test_set_locationsz+certbot_apache.parser.ApacheParser.find_dirz*certbot_apache.parser.ApacheParser.get_argc              	   C   sN   ddddddddg|_ d |_ t d	¡}| j ¡  |  |jj¡ W 5 Q R X d S )
Nr7   r8   r9   r:   Ú5Ú6Ú7Ú8zcertbot_apache.parser.logger)r!   r   rS   r   Zparse_modulesr+   ÚdebugZcalled)r
   Zmock_argZ	mock_findZmock_loggerr   r   r   Útest_parse_modules_bad_syntaxŸ   s
    
z-BasicParserTest.test_parse_modules_bad_syntaxú3certbot_apache.parser.ApacheParser._get_runtime_cfgc              	      s   d‰ d‰d‰‡ ‡‡fdd„}||_ ddddd	œ}tƒ | j_t d
¡B}| j ¡  |  | jj|¡ |  t	| jjƒd¡ |  |j
d¡ W 5 Q R X d S )Naß  ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex ssl-stapling: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
Mutex watchdog-callback: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: TEST
Define: DUMP_RUN_CFG
Define: U_MICH
Define: TLS=443
Define: example_path=Documents/path
User: name="www-data" id=33 not_used
Group: name="www-data" id=33 not_used
aí  Included configuration files:
  (*) /etc/apache2/apache2.conf
    (146) /etc/apache2/mods-enabled/access_compat.load
    (146) /etc/apache2/mods-enabled/alias.load
    (146) /etc/apache2/mods-enabled/auth_basic.load
    (146) /etc/apache2/mods-enabled/authn_core.load
    (146) /etc/apache2/mods-enabled/authn_file.load
    (146) /etc/apache2/mods-enabled/authz_core.load
    (146) /etc/apache2/mods-enabled/authz_host.load
    (146) /etc/apache2/mods-enabled/authz_user.load
    (146) /etc/apache2/mods-enabled/autoindex.load
    (146) /etc/apache2/mods-enabled/deflate.load
    (146) /etc/apache2/mods-enabled/dir.load
    (146) /etc/apache2/mods-enabled/env.load
    (146) /etc/apache2/mods-enabled/filter.load
    (146) /etc/apache2/mods-enabled/mime.load
    (146) /etc/apache2/mods-enabled/mpm_event.load
    (146) /etc/apache2/mods-enabled/negotiation.load
    (146) /etc/apache2/mods-enabled/reqtimeout.load
    (146) /etc/apache2/mods-enabled/setenvif.load
    (146) /etc/apache2/mods-enabled/socache_shmcb.load
    (146) /etc/apache2/mods-enabled/ssl.load
    (146) /etc/apache2/mods-enabled/status.load
    (147) /etc/apache2/mods-enabled/alias.conf
    (147) /etc/apache2/mods-enabled/autoindex.conf
    (147) /etc/apache2/mods-enabled/deflate.conf
añ  Loaded Modules:
 core_module (static)
 so_module (static)
 watchdog_module (static)
 http_module (static)
 log_config_module (static)
 logio_module (static)
 version_module (static)
 unixd_module (static)
 access_compat_module (shared)
 alias_module (shared)
 auth_basic_module (shared)
 authn_core_module (shared)
 authn_file_module (shared)
 authz_core_module (shared)
 authz_host_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 deflate_module (shared)
 dir_module (shared)
 env_module (shared)
 filter_module (shared)
 mime_module (shared)
 mpm_event_module (shared)
 negotiation_module (shared)
 reqtimeout_module (shared)
 setenvif_module (shared)
 socache_shmcb_module (shared)
 ssl_module (shared)
 status_module (shared)
c                    s4   | d dkrˆ S | d dkr ˆS | d dkr0ˆS dS )zMock command outputéÿÿÿÿZDUMP_RUN_CFGZDUMP_INCLUDESZDUMP_MODULESNr   )Úcmd©Z
define_valÚinc_valZmod_valr   r   Úmock_get_varsú   s    zDBasicParserTest.test_update_runtime_variables.<locals>.mock_get_varsÚ Z443zDocuments/path)ZTESTZU_MICHZTLSZexample_pathú-certbot_apache.parser.ApacheParser.parse_fileé:   é   )r   Úsetr   rK   r   rS   Úupdate_runtime_variablesr#   Z	variablesr1   Ú
call_count)r
   Úmock_cfgÚ_ra   Zexpected_varsÚ
mock_parser   r_   r   Útest_update_runtime_variables©   s(    ÿÿÿ!
ÿ
ÿ
z-BasicParserTest.test_update_runtime_variablesc              	   C   sz   d  | jjd tj | jjd ¡¡}||_tƒ | j_t	 
d¡2}| j ¡  |  t| jjƒd¡ |  |jd¡ W 5 Q R X d S )Nz…Included configuration files:
  (*) {0}
    (146) /etc/apache2/mods-enabled/access_compat.load
    (146) {1}/mods-enabled/alias.load
r   rc   r   r.   )Úformatr   r'   r   r   Údirnamer!   rf   rK   r   rS   rg   r#   r1   rh   )r
   ri   rj   r`   rk   r   r   r   Ú(test_update_runtime_variables_alt_values  s    ÿ
ú
ÿ
z8BasicParserTest.test_update_runtime_variables_alt_valuesc                 C   s,   d|_ | j ¡  d|_ |  tj| jj¡ d S )NzDefine: TLS=443=24z'Define: DUMP_RUN_CFG
Define: TLS=443=24)r!   r   rg   r   r   r   )r
   ri   r   r   r   Ú#test_update_runtime_vars_bad_output*  s    
 ÿz3BasicParserTest.test_update_runtime_vars_bad_outputz5certbot_apache.configurator.ApacheConfigurator.optionz&certbot_apache.parser.subprocess.Popenc                 C   s"   t |_d|_|  tj| jj¡ d S )NZnonexistent)ÚOSErrorr   r!   r   r   ÚMisconfigurationErrorr   rg   )r
   Ú
mock_popenZmock_optr   r   r   Ú test_update_runtime_vars_bad_ctl3  s    þz0BasicParserTest.test_update_runtime_vars_bad_ctlc                 C   s&   d|ƒ j _d|_|  tj| jj¡ d S )N)rb   rb   r]   )Zcommunicater!   Ú
returncoder   r   rr   r   rg   )r
   rs   r   r   r   Ú!test_update_runtime_vars_bad_exit<  s    
þz1BasicParserTest.test_update_runtime_vars_bad_exitc                 C   s`   ddl m} | j || jjd ƒd¡ | j d¡}|  t|ƒd¡ |  | jjd |d k¡ d S )Nr   rC   rR   Z123456r.   )	rJ   rD   r   Zadd_commentr'   Zfind_commentsr#   r1   r+   )r
   rD   Zcommr   r   r   Útest_add_commentD  s
    z BasicParserTest.test_add_comment)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r    r%   r)   r-   r3   r@   rA   rB   rN   rP   rQ   rU   r   rS   r[   rl   ro   rp   rt   rv   rw   Ú__classcell__r   r   r   r   r      sB   	h

r   c                       sh   e Zd Z‡ fdd„Zdd„ Ze d¡dd„ ƒZdd	„ Ze d
¡dd„ ƒZ	dd„ Z
dd„ Zdd„ Z‡  ZS )ÚParserInitTestc                    s   t t| ƒ ¡  d S r   )r   r}   r   r	   r   r   r   r   M  s    zParserInitTest.setUpc                 C   s(   t  | j¡ t  | j¡ t  | j¡ d S r   r   r	   r   r   r   r   P  s    zParserInitTest.tearDownz.certbot_apache.parser.ApacheParser.init_augeasc                 C   sH   ddl m} tj|_t ¡ | j_| j	tj|t
j | j¡dd| jd d S )Nr   ©ÚApacheParserú/dummy/vhostpath©é   é   é   ©ÚversionÚconfigurator)rJ   r   r   r(   r   r   r   ÚconfigZconfig_testr   r   r   Úrelpathr*   )r
   Zmock_init_augeasr   r   r   r   Útest_prepare_no_augeasU  s       ýz%ParserInitTest.test_prepare_no_augeasc              	   C   sP   ddl m} t d¡0}d|_| jtj|tj	 
| j¡dd| jd W 5 Q R X d S )Nr   r~   z4certbot_apache.parser.ApacheParser.check_aug_versionFr€   r   r…   )rJ   r   r   rS   r!   r   r   ZNotSupportedErrorr   r   r‰   r*   rˆ   )r
   r   Zmock_cr   r   r   Útest_init_old_aug_  s       ýz ParserInitTest.test_init_old_augr\   c                 C   s:   ddl m} d|_| jtj|tj | j	¡dd| j
d d S )Nr   r~   zDefine: TESTr€   )r‚   r‚   r„   r…   )rJ   r   r!   r   r   r   r   r   r‰   r*   rˆ   )r
   ri   r   r   r   r   Útest_unparseableh  s       ýzParserInitTest.test_unparseablec              	   C   sV   ddl m} t d¡& tj | jd¡}||d| jd}W 5 Q R X |  	|j
| j¡ d S )Nr   r~   ú;certbot_apache.parser.ApacheParser.update_runtime_variablesz@debian_apache_2_4/////multiple_vhosts/../multiple_vhosts/apache2r€   ©r‡   )rJ   r   r   rS   r   r   r   r   rˆ   r#   r   r*   )r
   r   r   r   r   r   r   Útest_root_normalizedq  s    þz#ParserInitTest.test_root_normalizedc              	   C   sP   ddl m} t d¡  |tj | j¡d| jd}W 5 Q R X |  	|j
| j¡ d S ©Nr   r~   r   r€   rŽ   )rJ   r   r   rS   r   r   r‰   r*   rˆ   r#   r   ©r
   r   r   r   r   r   Útest_root_absolute~  s     þz!ParserInitTest.test_root_absolutec              	   C   sP   ddl m} t d¡  || jtjj d| jd}W 5 Q R X |  	|j
| j¡ d S r   )rJ   r   r   rS   r*   r   r   Úseprˆ   r#   r   r‘   r   r   r   Útest_root_no_trailing_slashˆ  s     þz*ParserInitTest.test_root_no_trailing_slash)rx   ry   rz   r   r   r   rS   rŠ   r‹   rŒ   r   r’   r”   r|   r   r   r   r   r}   L  s   
		

r}   Ú__main__)r{   r   Zunittestr   Zcertbotr   Zcertbot.compatr   Zcertbot_apache.testsr   Z
ParserTestr   Z
ApacheTestr}   rx   Úmainr   r   r   r   Ú<module>   s     @F