
    M/e<                        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	m
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d
lmZ ej8                  j;                   ej<                  d            Z G d de	j@                        Z! G d de	j@                        Z"e#dk(  r4 ejH                   ejJ                  ejL                  dd e'gz                yy)z,Tests for certbot._internal.plugins.webroot.    )print_functionN)mock)
challenges)achallenges)errors)
filesystem)os)util)	acme_utilzrsa512_key.pemc                      e Zd ZdZ ej
                  ej                  de      Z	d Z
d Zd Zd Zd Z ej                          d	        Z ej&                  ej*                  d
       ej                          d               Z ej&                  ej*                  d
       ej                          d               Z ej&                  ej*                  d
      d        Z ej                          d        Z ej                          d        Z ej                          d        Zd Zd Z ej>                  d      d        Z  ej                          d        Z!d Z"d Z#d Z$d Z%d Z& ej>                  d      d        Z'y)AuthenticatorTestz:Tests for certbot._internal.plugins.webroot.Authenticator.	thing.comchallbdomainaccount_keyc                    ddl m} t        j                         | _        t
        j                  j                  | j                  d      | _        t        j                  | j                         t
        j                  j                  | j                  d      | _
        t
        j                  j                  | j                  dd      | _        t
        j                  j                  | j                  d      | _        t        j                  | j                  d| j                  i      | _         || j                  d      | _        y )	Nr   Authenticatorwebroot.well-knownacme-challenge:ZXZhR3hmQURzNnBTUmIyTEF2OUlaZjE3RHQzanV4R0orUEN0OTJ3citvQQr   )webroot_pathwebroot_map)!certbot._internal.plugins.webrootr   tempfilemkdtemp	workspacer	   pathjoinr   mkdirpartial_root_challenge_pathroot_challenge_pathvalidation_pathr   	MagicMockconfigauthselfr   s     N/usr/lib/python3/dist-packages/certbot/_internal/tests/plugins/webroot_test.pysetUpzAuthenticatorTest.setUp#   s    C "))+GGLL;	#+-77<<II},&(#%77<<II}&6$8 !ww||$$H J nn$))2=tyy1IK!$++y9	    c                 B    t        j                  | j                         y N)shutilrmtreer    r*   s    r+   tearDownzAuthenticatorTest.tearDown8   s    dii r-   c                 |    | j                   j                         }t        |t              sJ | j                  |v sJ y r/   )r(   	more_info
isinstancestrr    )r*   r5   s     r+   test_more_infoz AuthenticatorTest.test_more_info;   s6    II'')	)S)))yyI%%%r-   c                     t        j                         }| j                  j                  |       d|j                  k(  sJ y )N   )r   r&   r(   add_parser_arguments
call_count)r*   adds     r+   test_add_parser_argumentsz+AuthenticatorTest.test_add_parser_arguments@   s2    nn		&&s+CNN"""r-   c                 8    | j                   j                          y r/   )r(   preparer2   s    r+   test_preparezAuthenticatorTest.test_prepareE   s    		r-   c                    g | j                   _        d| j                  i| j                   _         |       }t        j
                  df|j                  _        | j                  j                  | j                  g       |j                  j                  sJ |j                  j                  D ]Z  | j                  j                  d   d   v sJ t        fd| j                   j                  j                         D              rZJ  | j                   j                  | j                  j                     | j                  k(  sJ y )Notherthing.com   r   c              3   2   K   | ]  }|d    d   v   ywr   rD   N .0r   calls     r+   	<genexpr>z;AuthenticatorTest.test_webroot_from_list.<locals>.<genexpr>S   (      A 471:%A   )r'   r   r    r   display_utilOKmenureturn_valuer(   performachallcalledcall_args_listr   allvaluesr*   mock_get_utilitymock_displayrJ   s      @r+   test_webroot_from_listz(AuthenticatorTest.test_webroot_from_listH   s   #% #3TYY"?')*6//1)>&		4;;-(  '''' %%44 	AD;;%%a333 A#{{66==?A A A A	A
 {{&&t{{'9'9:# 	# #r-   zTest specific to Windows)reasonc                     |       }t         j                  df|j                  _        | j                  j                  | j                  g       t        j                  j                  t        j                  j                  | j                  d            sJ | j                  j                  | j                  g       t        j                  j                  t        j                  j                  | j                  d            rJ y )NrD   
web.config)rN   rO   rP   rQ   r(   rR   rS   r	   r    existsr!   r$   cleanup)r*   rY   rZ   s      r+   (test_webconfig_file_generate_and_cleanupz:AuthenticatorTest.test_webconfig_file_generate_and_cleanupY   s     ()*6//1)>&		4;;-(ww~~bggll4+C+C\RSSS		4;;-(77>>"'',,t/G/G"VWWWWr-   c                 
    |       }t         j                  df|j                  _        t        j
                  j                  | j
                  dd      }t        j                  |       t        j
                  j                  |d      }t        |d      5 }|j                  d       d d d        | j                  j                  | j                  g       ddlm} |j!                  |      }dd	lm} ||vsJ y # 1 sw Y   SxY w)
NrD   r   r   r^   w	somethingr   )crypto_util)_WEB_CONFIG_SHA256SUMS)rN   rO   rP   rQ   r	   r    r!   r   makedirsopenwriter(   rR   rS   certbotre   	sha256sumr   rf   )	r*   rY   rZ   challenge_pathwebconfig_pathfilere   webconfig_hashrf   s	            r+   $test_foreign_webconfig_file_handlingz6AuthenticatorTest.test_foreign_webconfig_file_handlingd   s     ()*6//1)>&dii@PQN+nlC.#& 	$$JJ{#	$		4;;-('$..~>L%;;;;	$ 	$s   C99Dc                 D   t        j                  t        j                  t	        j
                  d      d      dt              }| j                  | j                  j                  d<   t        j                  j                  | j                  dd      }t        j                  |       t        j                  j                  |d      }t        |d	      5 }|j                  d
       d d d        | j                   j#                  | j$                  |g       y # 1 sw Y   1xY w)N   bingotokenpendingzsecond-thing.comr   r   r   r^   rc   rd   )r   "KeyAuthorizationAnnotatedChallenger   chall_to_challbr   HTTP01KEYr    r'   r   r	   r!   r   rg   rh   ri   r(   rR   rS   )r*   achall_2rl   rm   rn   s        r+   'test_foreign_webconfig_multiple_domainsz9AuthenticatorTest.test_foreign_webconfig_multiple_domainsv   s     AA,,Z->->X-NPYZ%38 7;ii 23dii@PQN+nlC.#& 	$$JJ{#	$		4;;12	$ 	$s   DDc                    g | j                   _        d| j                  i| j                   _         |       }t        j
                  dff|j                  _        t        j                  t        j                        5  | j                  j                  | j                  g       d d d        |j                  j                  sJ |j                  j                   D ]Z  | j                  j"                  d   d   v sJ t%        fd| j                   j                  j'                         D              rZJ  y # 1 sw Y   xY w)NrC   r   c              3   2   K   | ]  }|d    d   v   ywrF   rG   rH   s     r+   rK   zKAuthenticatorTest.test_webroot_from_list_help_and_cancel.<locals>.<genexpr>   rL   rM   )r'   r   r    r   rN   CANCELrP   side_effectpytestraisesr   PluginErrorr(   rR   rS   rT   rU   r   rV   rW   rX   s      @r+   &test_webroot_from_list_help_and_cancelz8AuthenticatorTest.test_webroot_from_list_help_and_cancel   s   #% #3TYY"?')*6*=*=r)B(D%]]6--. 	-IIt{{m,	-  '''' %%44 	AD;;%%a333 A#{{66==?A A A A	A	- 	-s   <'D77E c                 .   g | j                   _        d| j                  i| j                   _         |       }t        j
                  df|j                  _        t        j                  d      5 }t        j                  dft        j
                  | j                  ff|_        | j                  j                  | j                  g       d d d        | j                   j                  | j                  j                     | j                  k(  sJ y # 1 sw Y   FxY w)Nsomething.comr   'certbot.display.ops.validated_directoryr}   )r'   r   r    r   rN   rO   rP   rQ   r   patchr   r   r(   rR   rS   r   r*   rY   rZ   ms       r+   test_new_webrootz"AuthenticatorTest.test_new_webroot   s    #% #2DII">')*6//1)>&ZZAB 	-a*1126*ootyy:<AM IIt{{m,		- {{&&t{{'9'9:diiGGG	- 	-s   ,ADDc                    g | j                   _        i | j                   _         |       }t        j                  df|j
                  _        t        j                  d      5 }t        j                  df|_        t        j                  t        j                        5  | j                  j                  | j                   g       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr   r   r}   )r'   r   r   rN   rO   rP   rQ   r   r   r   r   r   r   r   r(   rR   rS   r   s       r+   !test_new_webroot_empty_map_cancelz3AuthenticatorTest.test_new_webroot_empty_map_cancel   s    #% "$')*6//1)>&ZZAB 	1a*1126ANv112 1		!!4;;-01	1 	11 1	1 	1s$    ;C'CCC	CC(c                     d | j                   _        i | j                   _        t        j                  t
        j                        5  | j                  j                  g        d d d        y # 1 sw Y   y xY wr/   )	r'   r   r   r   r   r   r   r(   rR   r2   s    r+   test_perform_missing_rootz+AuthenticatorTest.test_perform_missing_root   sS    #' "$]]6--. 	"IIb!	" 	" 	"s   A++A4c                    t         j                  j                  | j                  d      | j                  _        t         j                  j                  | j                  d      }t        |d      5 }|j                  d       d d d        t        j                  | j                  d       	 t        |d      5  	 d d d        t        d       t        j                  | j                  d       y # 1 sw Y   lxY w# 1 sw Y   AxY w# t        $ rW t        j                  t        j                        5  | j                  j                  g        d d d        n# 1 sw Y   nxY wY w xY w)	Nnullrndrc   thingimyr   rz9Warning, running tests as root skips permissions tests...i  )r	   r    r!   r(   	full_pathrh   ri   r   chmodprintIOErrorr   r   r   r   rR   )r*   permission_canaryfs      r+   "test_perform_reraises_other_errorsz4AuthenticatorTest.test_perform_reraises_other_errors   s    ggll499f=		GGLLE:#S) 	 QGGJ	 E*	&'- MN
 	E*	  	    	&v112 &		!!"%& & &	&sN   0C-+D 7C99D -C69D>D ,E%1E	E%E	E%$E%zJcertbot._internal.plugins.webroot.filesystem.copy_ownership_and_apply_modec                     t        t        j                  d      |_        | j                  j                  | j                  g       y )Nmsg)OSErrorerrnoEACCESr   r(   rR   rS   )r*   mock_ownerships     r+   test_failed_chownz#AuthenticatorTest.test_failed_chown   s-    %,U\\5%A"		4;;-(r-   c                 Z   t        j                         }g | j                  _        d| j                  i| j                  _         |       }t        j                  dft        j                  |ff|j                  _	        t        j                  t        j                  dt              }t        j                   d      5 }t        j                  |f|_        | j$                  j'                  |g       d d d        | j                  j
                  |j(                     |k(  sJ y # 1 sw Y   2xY w)Nzwhatever.comr   r   r   r   )r   r   r'   r   r    r   rN   rO   rP   r   r   rv   r   HTTP01_Pry   r   r   rQ   r(   rR   r   )r*   rY   new_webrootrZ   rS   r   s         r+   #test_perform_new_webroot_not_in_mapz5AuthenticatorTest.test_perform_new_webroot_not_in_map   s    &&(#% #1499"=')*6//1)=*6//;)G)I%??%%o3PZZAB 	(a*oo{<ANIIvh'	( {{&&v}}5DDD	( 	(s   <4D!!D*c                    | j                   j                          | j                   j                  | j                  g       t	        j
                  | j                  d      sJ t        j                  | j                        D ]E  \  }}}|D ]:  }t        j                  j                  ||      }t	        j
                  |d      r:J  G t	        j                  | j                  | j                        sJ y )Ni  i  )r(   r@   rR   rS   r   
check_moder%   r	   walkr    r!   has_same_ownership)r*   dirpathdirnames_	directoryr   s         r+   test_perform_permissionsz*AuthenticatorTest.test_perform_permissions   s    		 			4;;-($$T%9%95AAA %'GGDII$6 	? GXq% ?	GGLL)<	!,,Y>>>?	?
 ,,T-A-A499MMMr-   c                 X   | j                   j                          | j                   j                  | j                  g      }dt	        |      k(  sJ t
        j                  j                  | j                        sJ t        | j                        5 }|j                         }d d d        t        j                        j                  | j                  j                  t        j!                               sJ | j                   j#                  | j                  g       t
        j                  j                  | j                        rJ t
        j                  j                  | j$                        rJ t
        j                  j                  | j&                        rJ y # 1 sw Y   xY w)NrD   )key_authorization)r(   r@   rR   rS   lenr	   r    r_   r%   rh   readr   !KeyAuthorizationChallengeResponseverifychallry   
public_keyr`   r$   r#   )r*   	responsesvalidation_f
validations       r+   test_perform_cleanupz&AuthenticatorTest.test_perform_cleanup   s>   		II%%t{{m4	C	N"""ww~~d22333$&&' 	-<%**,J	-;;",..4fKK%%s~~'7/99 	9 9 			4;;-(77>>$"6"677777>>$":":;;;77>>$"B"BCCCC	- 	-s   F  F)c                    t        j                  | j                         | j                  j	                          | j                  j                  | j                  g       | j                  j                  | j                  g       t        j                  j                  | j                        rJ t        j                  j                  | j                        rJ y r/   )r   r"   r#   r(   r@   rR   rS   r`   r	   r    r_   r%   r$   r2   s    r+   "test_perform_cleanup_existing_dirsz4AuthenticatorTest.test_perform_cleanup_existing_dirs   s    99:				4;;-(		4;;-( 77>>$"6"677777>>$":":;;;;r-   c                    t        j                  t        j                  t	        j
                  d      d      dt              }d}t        j                  | j                         | j                  j                          | j                  j                  || j                  g       | j                  j                  | j                  g       t        j                   j#                  |      rJ t        j                   j#                  | j$                        sJ | j                  j                  |g       t        j                   j#                  | j&                        rJ t        j                   j#                  | j$                        rJ y )Nrr   rs   ru   r   r   YmluZ28)r   rv   r   rw   r   rx   ry   r   r"   r#   r(   r@   rR   rS   r`   r	   r    r_   r$   r%   )r*   bingo_achallbingo_validation_paths      r+   (test_perform_cleanup_multiple_challengesz:AuthenticatorTest.test_perform_cleanup_multiple_challenges  s   "EE,,!!19>C1
 !*99:				<56		4;;-(77>>"7888ww~~d66777		<.)77>>$"6"677777>>$":":;;;;r-   c                 $   | j                   j                          | j                   j                  | j                  g       t        j
                  j                  | j                  d      }t        j                  |       | j                   j                  | j                  g       t        j
                  j                  | j                        rJ t        j
                  j                  | j                        sJ t	        j                  |       y )Nleftover)r(   r@   rR   rS   r	   r    r!   r$   r   r"   r`   r_   r%   rmdir)r*   leftover_paths     r+   test_cleanup_leftoversz(AuthenticatorTest.test_cleanup_leftovers  s    				4;;-(T%=%=zJ'		4;;-(77>>$"6"6777ww~~d66777
r-   zcertbot.compat.os.rmdirc                    | j                   j                          | j                   j                  | j                  g       t	               }t
        j                  |_        ||_        | j                   j                  | j                  g       t        j                  j                  | j                        rJ t        j                  j                  | j                        sJ y r/   )r(   r@   rR   rS   r   r   r   r   r`   r	   r    r_   r%   r$   )r*   
mock_rmdiros_errors      r+   test_cleanup_failurez&AuthenticatorTest.test_cleanup_failure$  s    				4;;-(9!)
		4;;-(77>>$"6"6777ww~~d66777r-   N)(__name__
__module____qualname____doc__r   rv   r   r   ry   rS   r,   r3   r8   r>   rA   	test_utilpatch_display_utilr[   unittestskipIfr   
POSIX_MODEra   rp   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rG   r-   r+   r   r      s   D;[;;!!+3HF:*!&
#
 "Y!!## $#  X__Z**3MN!Y!!#X $ OX X__Z**3MN!Y!!#< $ O<  X__Z**3MN3 O3 "Y!!#A $A "Y!!#H $H "Y!!#	1 $	1"+  TZZ\]) ^) "Y!!#E $EN D <<$  TZZ)*
8 +
8r-   r   c                   x    e Zd ZdZ ej
                  ej                  de      Z	d Z
d Zd Zd Zd Zd	 Zd
 Zy)WebrootActionTestz#Tests for webroot argparse actions.r   r   c                     ddl m} t        j                         | _        t        j                         | _        | j                  j                  dddg        |j                  | j                  d       y )Nr   r   z-dz	--domainsappend)actiondefaultr   )
r   r   r   r   r    argparseArgumentParserparseradd_argumentinject_parser_optionsr)   s     r+   r,   zWebrootActionTest.setUp8  sY    C$$&	--/  {(0" 	! 	>++DKKCr-   c                     | j                   j                  dt        j                  d| j                  i      g      }|j
                  d   | j                  k(  sJ y )Nz--webroot-mapr   )r   
parse_argsjsondumpsr    r   )r*   argss     r+   test_webroot_map_actionz)WebrootActionTest.test_webroot_map_action@  sN    {{%%djj+tyy)ABCE,		999r-   c                 8   | j                   j                  dj                  | j                  j                  | j
                        j                               }| j                  |      }|j                  | j                  j                     | j
                  k(  sJ y )Nz-d {0} -w {1})	r   r   formatrS   r   r    split_get_config_after_performr   r*   r   r'   s      r+   test_domain_before_webrootz,WebrootActionTest.test_domain_before_webrootE  su    {{%%""4;;#5#5tyyAGGIK//5!!$++"4"45BBBr-   c                    t        j                  t        j                        5  | j                  j                  dj                                d d d        t        j                  t        j                        5  | j                  j                  dj                                d d d        y # 1 sw Y   _xY w# 1 sw Y   y xY w)Nz-d foo -w bar -w bazz-d foo -w bar -d baz -w qux)r   r   r   r   r   r   r   r2   s    r+    test_domain_before_webroot_errorz2WebrootActionTest.test_domain_before_webroot_errorK  s    ]]6--. 	CKK""#9#?#?#AB	C]]6--. 	JKK""#@#F#F#HI	J 	J	C 	C	J 	Js   *B,9*B8,B58Cc                    | j                   j                  dj                  | j                  | j                  j
                  t        j                               j                               }|j                  | j                  j
                     | j                  k(  sJ | j                  |      }|j                  | j                  j
                     | j                  k(  sJ y Nz-w {0} -d {1} -w {2} -d bar)r   r   r   r    rS   r   r   r   r   r   r   r   s      r+   test_multiwebrootz#WebrootActionTest.test_multiwebrootQ  s    {{%%&C&J&JIIt{{))8+;+;+='??DuwH 2 23tyy@@@//5!!$++"4"45BBBr-   c                 x   t        j                         }| j                  j                  dj	                  | j
                  | j                  j                  |      j                               }|j                  | j                  j                  | j
                  ik(  sJ |j                  | j
                  |gk(  sJ y r   )r   r   r   r   r   r    rS   r   r   r   r   )r*   other_webroot_pathr   s      r+   (test_webroot_map_partial_without_performz:WebrootActionTest.test_webroot_map_partial_without_performX  s     &--/{{%%&C&J&JIIt{{))+='??DuwHDKK$6$6		#BBBB  TYY0B$CCCCr-   c                 p    ddl m}  ||d      }|j                  | j                  g       |j                  S )Nr   r   r   )r   r   rR   rS   r'   )r*   r'   r   r(   s       r+   r   z+WebrootActionTest._get_config_after_performe  s-    CVY/dkk]#{{r-   N)r   r   r   r   r   rv   r   r   ry   rS   r,   r   r   r   r   r   r   rG   r-   r+   r   r   2  sM    -;[;;!!+3HFD:
CJCDr-   r   __main__rD   )(r   
__future__r   r   r   r   r0   sysr   r   r   josepyjoser   acmer   rj   r   r   certbot.compatr   r	   certbot.displayr
   rN   certbot.testsr   r   JWKRSAloadload_vectorry   TestCaser   r   r   exitmainargv__file__rG   r-   r+   <module>r     s    2 %     
         %  0 # +
kk,y,,-=>?R8)) R8j7)) 7t zCHH[V[[!"
234 r-   