
\\/                 @   se  d  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 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 e j j e j d
   Z Gd d   d e	 j  Z Gd d   d e	 j  Z  e! d k rae	 j"   d S)z"Tests for certbot.plugins.webroot.    )print_functionN)
challenges)achallenges)compat)errors)util)	acme_utilzrsa512_key.pemc               @   s  e  Z d  Z d Z e j d e j d d d e  Z	 d d   Z
 d d	   Z d
 d   Z d d   Z d d   Z e j   d d    Z e j   d d    Z e j   d d    Z e j   d d    Z d d   Z d d   Z e j d  e j d  d d     Z e j   d  d!    Z d" d#   Z d$ d%   Z d& d'   Z d( d)   Z d* d+   Z  e j d,  d- d.    Z! d/ S)0AuthenticatorTestz0Tests for certbot.plugins.webroot.Authenticator.challbdomainz	thing.comaccount_keyc             C   s   d d l  m } t j   |  _ t j j |  j d  |  _ t j j |  j d d  |  _ t j j |  j d  |  _	 t
 j d |  j d d |  j i  |  _ | |  j d	  |  _ d  S)
Nr   )Authenticatorz.well-knownzacme-challengeZ:ZXZhR3hmQURzNnBTUmIyTEF2OUlaZjE3RHQzanV4R0orUEN0OTJ3citvQQwebroot_pathwebroot_mapz	thing.comwebroot)certbot.plugins.webrootr   tempfilemkdtemppathosjoinpartial_root_challenge_pathroot_challenge_pathvalidation_pathmock	MagicMockconfigauth)selfr    r   >/usr/lib/python3/dist-packages/certbot/plugins/webroot_test.pysetUp%   s    			zAuthenticatorTest.setUpc             C   s   t  j |  j  d  S)N)shutilZrmtreer   )r   r   r   r    tearDown3   s    zAuthenticatorTest.tearDownc             C   sB   |  j  j   } |  j t | t j   |  j |  j | k  d  S)N)r   	more_info
assertTrue
isinstancesixZstring_typesr   )r   r$   r   r   r    test_more_info6   s    z AuthenticatorTest.test_more_infoc             C   s3   t  j   } |  j j |  |  j d | j  d  S)N   )r   r   r   Zadd_parser_argumentsassertEqualZ
call_count)r   addr   r   r    test_add_parser_arguments;   s    z+AuthenticatorTest.test_add_parser_argumentsc             C   s   |  j  j   d  S)N)r   prepare)r   r   r   r    test_prepare@   s    zAuthenticatorTest.test_preparec                s   g  |  j  _ d |  j i |  j  _ |   } t j d f | j _ |  j j	 |  j
 g  |  j | j j  xj | j j D]\   |  j |  j
 j   d d k  |  j t   f d d   t j |  j  j  D   qu W|  j |  j  j |  j
 j |  j  d  S)Nzotherthing.com   r   c             3   s#   |  ] } |   d  d k Vq d S)r   r/   Nr   ).0r   )callr   r    	<genexpr>O   s   z;AuthenticatorTest.test_webroot_from_list.<locals>.<genexpr>)r   r   r   r   display_utilOKmenureturn_valuer   performachallr%   calledcall_args_listr   allr'   
itervaluesr*   )r   mock_get_utilitymock_displayr   )r1   r    test_webroot_from_listC   s    	!	!z(AuthenticatorTest.test_webroot_from_listc                s   g  |  j  _ d |  j i |  j  _ |   } t j d f f | j _ |  j t	 j
 |  j j |  j g  |  j | j j  xj | j j D]\   |  j |  j j   d d k  |  j t   f d d   t j |  j  j  D   q Wd  S)Nzotherthing.comr/   r   c             3   s#   |  ] } |   d  d k Vq d S)r   r/   Nr   )r0   r   )r1   r   r    r2   `   s   zKAuthenticatorTest.test_webroot_from_list_help_and_cancel.<locals>.<genexpr>)r   r   r   r   r3   CANCELr5   side_effectassertRaisesr   PluginErrorr   r7   r8   r%   r9   r:   r   r;   r'   r<   )r   r=   r>   r   )r1   r    &test_webroot_from_list_help_and_cancelT   s    	"!	z8AuthenticatorTest.test_webroot_from_list_help_and_cancelc          
   C   s   g  |  j  _ d |  j i |  j  _ |   } t j d f | j _ t j	 d  A } t j
 d f t j |  j f f | _ |  j j |  j g  Wd  QRX|  j |  j  j |  j j |  j  d  S)Nzsomething.comr   z'certbot.display.ops.validated_directoryr/   r@   )r   r   r   r   r3   r4   r5   r6   r   patchrA   rB   r   r7   r8   r*   r   )r   r=   r>   mr   r   r    test_new_webrootc   s    	z"AuthenticatorTest.test_new_webrootc             C   s   g  |  j  _ i  |  j  _ |   } t j d f | j _ t j d  ; } t j	 d f | _ |  j
 t j |  j j |  j g  Wd  QRXd  S)Nr   z'certbot.display.ops.validated_directoryr/   r@   )r   r   r   r3   r4   r5   r6   r   rF   rA   rC   r   rD   r   r7   r8   )r   r=   r>   rG   r   r   r    !test_new_webroot_empty_map_cancelr   s    		z3AuthenticatorTest.test_new_webroot_empty_map_cancelc             C   s8   d  |  j  _ i  |  j  _ |  j t j |  j j g   d  S)N)r   r   r   rC   r   rD   r   r7   )r   r   r   r    test_perform_missing_root   s    z+AuthenticatorTest.test_perform_missing_rootc             C   s   t  j j |  j d  |  j _ t  j j |  j d  } t | d   } | j d  Wd  QRXt  j |  j d  y t | d  t d  Wn. t	 k
 r |  j
 t j |  j j g   Yn Xt  j |  j d  d  S)	NZnullZrndwZthingimyr   rz9Warning, running tests as root skips permissions tests...i  )r   r   r   r   	full_pathopenwritechmodprintIOErrorrC   r   rD   r7   )r   Zpermission_canaryfr   r   r    "test_perform_reraises_other_errors   s    !z4AuthenticatorTest.test_perform_reraises_other_errorszOn Windows, there is no chown.z certbot.plugins.webroot.os.chownc             C   s/   t  t j d  | _ |  j j |  j g  d  S)Nmsg)OSErrorerrnoEACCESrB   r   r7   r8   )r   Z
mock_chownr   r   r    test_failed_chown   s    z#AuthenticatorTest.test_failed_chownc          
   C   s   t  j   } g  |  j _ d |  j i |  j _ |   } t j d f t j | f f | j _	 t
 j d t j d d d t  } t j d  , } t j | f | _ |  j j | g  Wd  QRX|  j |  j j | j |  d  S)Nzwhatever.comr   r
   r   zsomething.comr   z'certbot.display.ops.validated_directory)r   r   r   r   r   r   r3   r4   r5   rB   r   "KeyAuthorizationAnnotatedChallenger   HTTP01_PKEYr   rF   r6   r   r7   r*   r   )r   r=   Znew_webrootr>   r8   rG   r   r   r    #test_perform_new_webroot_not_in_map   s    		z5AuthenticatorTest.test_perform_new_webroot_not_in_mapc             C   s(  |  j  j   |  j  j |  j g  |  j t j t j |  j	  j
 d   xn t j |  j  D]Z \ } } } xH | D]@ } t j j | |  } |  j t j t j |  j
 d   qt Wq^ Wt j |  j  j } t j |  j  j } |  j t j |  j	  j |  |  j t j |  j	  j |  d  S)Ni  i  )r   r-   r7   r8   r%   r   Zcompare_file_modesr   statr   st_modewalkr   r   st_gidst_uidr*   )r   dirpathZdirnames_Z	directoryrM   Z
parent_gidZ
parent_uidr   r   r    test_perform_permissions   s    ("-z*AuthenticatorTest.test_perform_permissionsc             C   s  |  j  j   |  j  j |  j g  } |  j d t |   |  j t j j	 |  j
   t |  j
   } | j   } Wd  QRX|  j t j d |  j |  j j t j     |  j  j |  j g  |  j t j j	 |  j
   |  j t j j	 |  j   |  j t j j	 |  j   d  S)Nr/   Zkey_authorization)r   r-   r7   r8   r*   lenr%   r   r   existsr   rN   readr   Z!KeyAuthorizationChallengeResponseZverifyZchallr\   Z
public_keycleanupassertFalser   r   )r   Z	responsesZvalidation_fZ
validationr   r   r    test_perform_cleanup   s    		z&AuthenticatorTest.test_perform_cleanupc             C   s   t  j |  j  |  j j   |  j j |  j g  |  j j |  j g  |  j t  j	 j
 |  j   |  j t  j	 j
 |  j   d  S)N)r   mkdirr   r   r-   r7   r8   ri   rj   r   rg   r   r   )r   r   r   r    "test_perform_cleanup_existing_dirs   s    z4AuthenticatorTest.test_perform_cleanup_existing_dirsc             C   s  t  j d t j t j d d  d  d d d t  } d } t j |  j	  |  j
 j   |  j
 j | |  j g  |  j
 j |  j g  |  j t j j |   |  j t j j |  j   |  j
 j | g  |  j t j j |  j   |  j t j j |  j   d  S)	Nr
   tokens   bingopendingr   z	thing.comr   ZYmluZ28)r   rZ   r   Zchall_to_challbr   ZHTTP01r\   r   rl   r   r   r-   r7   r8   ri   rj   r   rg   r%   r   r   )r   Zbingo_achallZbingo_validation_pathr   r   r    (test_perform_cleanup_multiple_challenges   s    	z:AuthenticatorTest.test_perform_cleanup_multiple_challengesc             C   s   |  j  j   |  j  j |  j g  t j j |  j d  } t j |  |  j  j	 |  j g  |  j
 t j j |  j   |  j t j j |  j   t j |  d  S)NZleftover)r   r-   r7   r8   r   r   r   r   rl   ri   rj   rg   r   r%   rmdir)r   Zleftover_pathr   r   r    test_cleanup_leftovers   s    z(AuthenticatorTest.test_cleanup_leftoverszos.rmdirc             C   s   |  j  j   |  j  j |  j g  t   } t j | _ | | _ |  j  j |  j g  |  j	 t
 j j |  j   |  j t
 j j |  j   d  S)N)r   r-   r7   r8   rV   rW   rX   rB   ri   rj   r   r   rg   r   r%   r   )r   Z
mock_rmdirZos_errorr   r   r    test_cleanup_failure   s    		z&AuthenticatorTest.test_cleanup_failureN)"__name__
__module____qualname____doc__r   rZ   r   r[   r\   r8   r!   r#   r(   r,   r.   	test_utilZpatch_get_utilityr?   rE   rH   rI   rJ   rT   Zskip_on_windowsr   rF   rY   r]   re   rk   rm   rp   rr   rs   r   r   r   r    r	      s.   	
r	   c               @   s   e  Z d  Z d Z e j d e j d d d e  Z	 d d   Z
 d d	   Z d
 d   Z d d   Z d d   Z d d   Z d S)WebrootActionTestz#Tests for webroot argparse actions.r
   r   z	thing.comr   c             C   sd   d d l  m } t j   |  _ t j   |  _ |  j j d d d d d g  | j	 |  j d  d  S)	Nr   )r   z-dz	--domainsactionappenddefaultr   )
r   r   r   r   r   argparseArgumentParserparseradd_argumentZinject_parser_options)r   r   r   r   r    r!     s    zWebrootActionTest.setUpc             C   sH   |  j  j d t j d |  j i  g  } |  j | j d |  j  d  S)Nz--webroot-mapz	thing.com)r   
parse_argsjsondumpsr   r*   r   )r   argsr   r   r    test_webroot_map_action  s    	!z)WebrootActionTest.test_webroot_map_actionc             C   s`   |  j  j d j |  j j |  j  j    } |  j |  } |  j | j	 |  j j |  j  d  S)Nz-d {0} -w {1})
r   r   formatr8   r   r   split_get_config_after_performr*   r   )r   r   r   r   r   r    test_domain_before_webroot  s    	$z,WebrootActionTest.test_domain_before_webrootc             C   sH   |  j  t j |  j j d j    |  j  t j |  j j d j    d  S)Nz-d foo -w bar -w bazz-d foo -w bar -d baz -w qux)rC   r   rD   r   r   r   )r   r   r   r     test_domain_before_webroot_error  s    z2WebrootActionTest.test_domain_before_webroot_errorc             C   s   |  j  j d j |  j |  j j t j    j    } |  j	 | j
 |  j j |  j  |  j |  } |  j	 | j
 |  j j |  j  d  S)Nz-w {0} -d {1} -w {2} -d bar)r   r   r   r   r8   r   r   r   r   r*   r   r   )r   r   r   r   r   r    test_multiwebroot$  s    ' z#WebrootActionTest.test_multiwebrootc             C   s9   d d l  m } | | d  } | j |  j g  | j S)Nr   )r   r   )r   r   r7   r8   r   )r   r   r   r   r   r   r    r   ,  s    z+WebrootActionTest._get_config_after_performN)rt   ru   rv   rw   r   rZ   r   r[   r\   r8   r!   r   r   r   r   r   r   r   r   r    ry     s   	ry   __main__)#rw   Z
__future__r   r}   rW   r   r   r"   r   ZunittestZjosepyZjoser   r'   Zacmer   Zcertbotr   r   r   Zcertbot.displayr   r3   Zcertbot.testsr   rx   ZJWKRSAloadZload_vectorr\   ZTestCaser	   ry   rt   mainr   r   r   r    <module>   s.   .