
    M/eb`                         d 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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  G d
 dej&                        Zedk(  r4 ej,                   ej.                  ej0                  dd egz                yy)z)Tests for certbot_nginx._internal.parser.    N)List)errors)os)nginxparser)obj)parser)	test_utilc                       e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)NginxParserTestzNginx Parser Test.c                     t        j                  | j                         t        j                  | j                         t        j                  | j                         y N)shutilrmtreetemp_dir
config_dirwork_dir)selfs    K/usr/lib/python3/dist-packages/certbot_nginx/_internal/tests/parser_test.pytearDownzNginxParserTest.tearDown   s2    dmm$doo&dmm$    c                     t         j                  j                  | j                  d      }t	        j
                  |      }|j                  | j                  k(  sJ y )Nz*etc_nginx/////ubuntu_nginx/../../etc_nginx)r   pathjoinr   r   NginxParserrootconfig_path)r   r   nparsers      r   test_root_normalizedz$NginxParserTest.test_root_normalized   sG    ww||DMM ,; <$$T*||t/////r   c                 z   t        j                         }	 t        j                  | j                         t	        j
                  t         j                  j                  | j                              }|j                  | j                  k(  sJ 	 t        j                  |       y # t        j                  |       w xY wr   )
r   getcwdchdirr   r   r   r   relpathr   r   )r   curr_dirr   s      r   test_root_absolutez"NginxParserTest.test_root_absolute    sx    99;	 HHT]]#((9I9I)JKG<<4#3#3333HHXBHHXs   A6B# #B:c                     t        j                  | j                  t        j                  j
                  z         }|j                  | j                  k(  sJ y r   )r   r   r   r   r   sepr   r   r   s     r   test_root_no_trailing_slashz+NginxParserTest.test_root_no_trailing_slash,   s=    $$T%5%5%CD||t/////r   c                    t        j                  | j                        }|j                          dD ch c]  }|j	                  |       c}t        |j                  j                               k(  sJ g dg|j                  |j	                  d         k(  sJ dgddgddgdd	gdd
gggg|j                  |j	                  d         k(  sJ yc c}w )z+Test recursive conf file parsing.

        )foo.conf
nginx.confserver.confz
mime.typessites-enabled/defaultzsites-enabled/both.comsites-enabled/example.comzsites-enabled/headers.comzsites-enabled/migration.comzsites-enabled/sslon.comsites-enabled/globalssl.comzsites-enabled/ipv6.comzsites-enabled/ipv6ssl.comzsites-enabled/example.net)server_namesomenamealiasanother.aliasr,   serverlisten69.50.225.155:9000	127.0.0.1r0   .example.com	example.*r.   N)r   r   r   loadabs_pathsetparsedkeys)r   r   xs      r   	test_loadzNginxParserTest.test_load0   s    $$T%5%56
89  # 9 W^^00234 	4 4 FF (8(8(GHI 	I Ix)=>)1;(?)6(G)6(DF G H !(8(88): ;	; 	; ;9s   Cc                    t        j                  | j                        }t        j                  dk7  rUd|j                  d      k(  sJ t        j                  j                  | j                  d      |j                  d      k(  sJ y d|j                  d      k(  sJ t        j                  j                  | j                  d      |j                  d      k(  sJ y )Nntz/etc/nginx/*zfoo/barzC:\etc\nginx\*zfoo\bar)r   r   r   r   namer;   r   r   r'   s     r   test_abs_pathzNginxParserTest.test_abs_pathL   s    $$T%5%5677d?!W%5%5n%EEEE77<< 0 0)<$--i89 9 9 ''*:*:;N*OOOO77<< 0 0*=$--j9: : :r   c                    t        j                  | j                        }|j                  dd       |j	                  |j                  d            }dt        t        j                  |j                  d                  k(  sJ dt        t        j                  |j                  d                  k(  sJ d	gd
dgd
dgddgddgggg|d   k(  sJ y )NtestFlazyzsites-enabled/example.com.test   z*.test
   zsites-enabled/*.testr4   r5   r6   r7   r0   r8   r9   r   )r   r   r   filedump_parse_filesr;   lenglob)r   r   r=   s      r   test_filedumpzNginxParserTest.test_filedumpX   s    $$T%5%56e,%%g&6&6,'. /C		'"2"28"<=>>>>SIIg&&'=>?A A 	A Ax)=>)1;(?)6(G)6(DF G H  	# 	# #r   c                    dgdgdggdgdggfdgdgdggdggfdgdgdggdggfg g fdg fdgdgdggdgdgdgggddgddgddgddggfdgdgdggdgdgdgggddgddgddggfdgdgdggdgdgdgggddgddggfdgdgdggdgdgdgggddggfdgdgdggdgdgdgggg fg
}|D ])  \  }}g }t        j                  |d |fd       ||k(  r)J  y )	N      r   rI         c                 V    t        | t              xr t        |       dk\  xr | d   dk(  S )NrS   r   rQ   )
isinstancelistrM   )r?   s    r   <lambda>z7NginxParserTest.test__do_for_subarray.<locals>.<lambda>x   s0    jD.A /.$'FaK/.$%aDAI r   c                 $    |j                  |      S r   )append)r?   yptss      r   rX   z7NginxParserTest.test__do_for_subarray.<locals>.<lambda>{   s    CJJqM r   )r   _do_for_subarray)r   mylistsmylistresultpathss        r   test__do_for_subarrayz%NginxParserTest.test__do_for_subarrayg   s   S1#sOqcA3Z0S1#sOqcU+S1#sOqcU+8r7cA3_sQC!o6Q!Q!Q!Q8:cA3_sQC!o6!Q!Q!Q8PQcA3_sQC!o6!Q!Q8HIcA3_sQC!o6!QAcA3_sQC!o6;
= & 	#NFF%'E##F%. 6;$I	K
 F?"?	#r   c                 H   t        j                  | j                        }|j                         }t	        j
                  |j                  d      t	        j                  dddddd      gdddhg dg      }|D cg c]  }d|j                  v s| c}d   }||k(  sJ y c c}w )Nr/   z4.8.2.657TFzglobalssl.comr   )	r   r   r   
get_vhostsr   VirtualHostr;   Addrfilep)r   r   vhostsvhostr?   globalssl_coms         r   test_get_vhosts_global_sslz*NginxParserTest.test_get_vhosts_global_ssl~   s    $$T%5%56##% 0 01N O!$)T4*/"8 !9 $d_,=rA3H
 %+Iqo.HI!L%%% Js   9BBc                    t        j                  | j                        }|j                         }t	        j
                  |j                  d      t	        j                  dddddd      gddddhg g d      }t	        j
                  |j                  d      t	        j                  d	ddddd      t	        j                  dd
dddd      gddh dg g d      }t	        j
                  |j                  d      t	        j                  dddddd      t	        j                  dddddd      gddddhg dg      }t	        j
                  |j                  d      t	        j                  dddddd      t	        j                  dddddd      gdddhg dg      }t	        j
                  |j                  d      t	        j                  dddddd      gddddhg g d      }dt        |      k(  sJ |D cg c]  }d|j                  v s| c}d   }	||	k(  sJ |D cg c]  }d |j                  v s| c}d   }
||
k(  sJ |D cg c]  }d|j                  v s| c}d   }||k(  sJ |D cg c]  }d|j                  v s| c}d   }||k(  sJ |D cg c]  }d	|j                  v s| c}d   }||k(  sJ y c c}w c c}w c c}w c c}w c c}w )!Nr+    8080FT	localhost~^(www\.)?(example|bar)\.rJ   rS   	   r1   8000>   r2   r1   r3   )rJ   rS      r.   z69.50.225.1559000r7   r8   r9   r   r-   myhost	otherhostwww.example.orgr*   *80*.www.foo.com*.www.example.com)rQ   rS   r      example.comdefault)r   r   r   re   r   rf   r;   rg   rM   rh   names)r   r   ri   vhost1vhost2vhost3vhost4vhost5r?   example_comr   fooconfrp   r1   s                 r   test_get_vhostszNginxParserTest.test_get_vhosts   s	   $$T%5%56##%!1!1,!?"%((2vue+0%#9 ":!&"-&B"D!#Z1 !1!1,!?"%((:vue+0%#9"%((2vue+0%#9": "'!G!#[2 !1!12M!N"%((?F+0%#G"%((;E5+0%#9": "'"0+!>QCI !1!12I!J"%((8R+0%#9"%((;E4+0%#9": "'/@.A!#aS* !1!1*!="%((3dD+0%#9 ":!%to2E.G!#Y0 S[   "(EQMQWW,DqEaH$$$$=	QWW(<1=a@   $>
agg(=1>qA    &A1+*@QA!D	"""%?!qww)>A?B!!! F=>A?s<   /J9J9J>,J>KK*K>KK'Kc                    t        j                  | j                        }t        j                  d d d d d ddgddgddgddgd	d
ggggd       }|j                  |      rJ g dg dddgg|_        |j                  |      rJ ddgg dg|_        |j                  |      rJ ddgddgg dg|_        |j                  |      du sJ y )Nr5   zmyhost default_serverr0   ry   location/r   htmlindexzindex.html index.htm)r5   z*:80default_serverssl)r0   r|   r}   z/home/ubuntu/sites/foo/z80 sslr{   r   onT)r   r   r   r   rf   has_ssl_on_directiveraw)r   r   
mock_vhosts      r   test_has_ssl_on_directivez)NginxParserTest.test_has_ssl_on_directive   s   $$T%5%56__T4tT1201S!VV$4w@V6W#XY 	

 //
;;;EO!#<=?
 //
;;;#X.OQ
//
;;;#T* $-OQ
 ++J74???r   c           
         t        j                  | j                        }t        j                  |j                  d      d d d ddhd g d      }|j                  d      }ddh}||_        ||_        dg|_        |j                  |d	d
gddgg       |j                  |d	       |j                  |d       |j                  |   dgddgddgddgddgg gggk(  sJ y )Nr+   rp   rq   rr   r.   r8   r9   r   foobarssl_certificate/etc/ssl/cert2.pemr4   r5   r6   r7   r0   )r   r   r   r   rf   r;   rh   r   r   add_server_directivesremove_server_directivesr=   )r   r   r   r   r   s        r   test_remove_server_directivesz-NginxParserTest.test_remove_server_directives   s    $$T%5%56__W%5%5l%C%)4&1+G&I%):	7

 &&'BC-&
 
#
%%j(-u~8I8L8N 'O	P 	((U;((5FG~~k*jH&:;$k2)>:);7	   !! 	! !r   c                    t        j                  | j                        }t        j                  |j                  d      d d d ddhd g d      }|j                  |ddgg dg       t        j                  d      }t        j                  |j                  |j                  d               }d	t        t        j                  ||            k(  sJ |j                  d
      }ddh}||_        ||_        dg|_        |j                  |ddgddgg       |j                  |ddgg       ddlm} |j                  |   dgddgddgddgddgddgd|gddgd|gg g g
ggk(  sJ |j                  d      }h d}||_        ||_        g |_        t'        j(                  t*        j,                        5  |j                  |ddgddgg       d d d        y # 1 sw Y   y xY w)Nr+   rp   rq   rr   r   r   )z
 r    z/etc/ssl/cert.pemz&\n\s+ssl_certificate /etc/ssl/cert.pemrS   r.   r8   r9   r   r   r   COMMENTr4   r5   r6   r7   r0   #r,   >   r2   r1   r3   )r   r   r   r   rf   r;   r   recompiler   dumpsr=   rM   findallrh   r   r   certbot_nginx._internal.parserr   pytestraisesr   MisconfigurationError)	r   r   r   ssl_redumpr   r   r   server_confs	            r   test_add_server_directivesz*NginxParserTest.test_add_server_directives   s/   $$T%5%56__W%5%5l%C%)4&1+G&I%):	7

 	%%j(-u~ 8M 'N	O EF  0@0@0N!OPC

6401111&&'BC-&
 
#
%%j(-u~8I8L8N 'O	P 	%%jE5>2BC:~~k*jH&:;$k2)>:);7!5>>-/CD>2r  	 		 	 &&}56&
 

]]6778 	G))*!5>-/CDFG	G 	G 	Gs   -GGc                    t        j                  | j                        }|j                  d      }t	        j
                  |d d d ddhd dg      }|j                  |g dg       |j                  |ddd|j                  d	      gg       dd
lm} |j                  |   dgddgddgddgddgg dg d|j                  d	      gd|gg g	ggk(  sJ y )Nr.   r8   r9   r   )
  r   r   what a nice commentr   includer   zcomment_in_file.confr   r4   r5   r6   r7   r0   )r   r   r   r   )
r   r   r   r;   r   rf   r   r   r   r=   )r   r   r   r   r   s        r   test_comment_is_repeatablez*NginxParserTest.test_comment_is_repeatable  s   $$T%5%56&&'BC__[%)4&4k%B%)A30
 	%%j'P&Q	S%%j(.	3&-&6&67M&N(P 'Q	R 	;~~k*jH&:;$k2)>:);7<%w'7'78N'OP>   !	! 		! 	!r   c                    t        j                  | j                        }ddh}|j                  d      }t	        j
                  |d d d |d dg      }|j                  |ddgg       ddlm} |j                  |   dgd	d
gd	dgddgd|gddgg gggk(  sJ ddh|_
        |j                  |ddgg       |j                  |   dgd	d
gd	dgddgd|gddgg ddgd|gg g	ggk(  sJ y )Nr8   r9   r.   r   r0   z
foobar.comr   r4   r5   r6   r7   r   r   zcert.pem)r   r   r   r;   r   rf   update_or_add_server_directivesr   r   r=   r   )r   r   targetrh   r   r   s         r   test_replace_server_directivesz.NginxParserTest.test_replace_server_directives/  sV   $$T%5%56 +.  !<=__UD$fdQCP
//-67	9:~~e$jH&:;$k2)<83.);7   	  )+6
//+Z89	;~~e$jH&:;$k2)<83.);7-z:S'NB	   	 r   c                     d}h dh dddhddhh dh d	d
dhh dh dddhdht               dhdhdhg}g d}t        |      D ]#  \  }}|t        j                  |||         k(  r#J  y )Nwww.eff.org>   irrelevant.long.name.eff.org*.orgr   >   eff.orgww2.eff.orgtest.www.eff.org	*.eff.org.www.eff.org.eff.orgr   >   www.eff.	www.eff.**.www.eff.org>   *.eff.*r   ~^(www\.)?(eff.+)rz   r   >   .test.eff.orgwww.*r   >   r   r   r   r   r   r   www.Eff.org.efF.org))exactr   NN)r   r   )wildcard_startr   )wildcard_endr   )regexr   )r   rz   r   r   )r   r   r   )r   r   r   r   )r   r   )r   r   )r<   	enumerater   get_best_match)r   target_namer   winnersiwinners         r   test_get_best_matchz#NginxParserTest.test_get_best_matchH  s    #I?~.g&;A+,A5!7+3  #7+ 	JIAv#22;aIJ J J	Jr   c                     t        j                  g dg dg dg      }ddlm} ddlm}  ||d        ||d       |j
                  g d|dg d|g dgk(  sJ y )	N)
ar   br   )cr   d)r   er   fr   )COMMENT_BLOCK)comment_directiverS   r   )r   UnspacedListr   r   r   spaced)r   blockr   r   s       r   test_comment_directivez&NginxParserTest.test_comment_directivem  sj    (('!*# $ 	AD%#%#||'! # # 	# #r   c           	          t        j                  d      }|d   d   }ddlm}  ||dd        ||dd        ||d	d
       |j                  g dg dg dg dg dg dg ddgk(  sJ y )Na4  
            server {
                listen 80;
                root /var/www/html;
                index star.html;

                server_name *.functorkitten.xyz;
                ssl_session_timeout 1440m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

                ssl_prefer_server_ciphers on;
            }r   rS   )_comment_out_directiverI   blah1rT   blah2   blah3)
                r5   r   r{   )r   r   r   z/var/www/html)r   r   r   z	star.html)

                r0   r   z*.functorkitten.xyz)r   r   z1 ssl_session_timeout 1440m; # duplicated in blah1)r   r   z; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # duplicated in blah2)r   r   z4 ssl_prefer_server_ciphers on; # duplicated in blah3z
            )r   loadsr   r   r   )r   server_blockr   r   s       r   test_comment_out_directivez*NginxParserTest.test_comment_out_directive  s    "(( 
* 
 Q"Iua1ua1ua1||7@=O\Ua   	 r   c                    t        j                  ddgg      }|d   rJ t        j                  g dg      }|d   sJ t        j                  ddgddgg      }|d   rJ t        j                  ddgddgg      }|d   sJ y )Nr5   443r   )r5   r   r   offr   )r   _parse_server_rawr   r4   s     r   test_parse_server_raw_sslz)NginxParserTest.test_parse_server_raw_ssl  s    ))u+
  %=  ))$+
  e}}))uu~+
  %=  ))ut}+
  e}}r   c                 Z    t        j                  ddgg      }t        |d         dk(  sJ y )Nr5   zunix:/var/run/nginx.sockaddrsr   )r   r   rM   r   s     r   test_parse_server_raw_unixz*NginxParserTest.test_parse_server_raw_unix  s9    ))12+
  6'?#q(((r   c                 x    t        j                  | j                        }|j                  ddgg      }|d   sJ y )Nr5   r   r   )r   r   r   parse_server)r   r   r4   s      r   $test_parse_server_global_ssl_appliedz4NginxParserTest.test_parse_server_global_ssl_applied  sC    $$T%5%56%%u'
  e}}r   c                    t        j                  | j                        }|j                         }|D cg c]  }d|j                  v s| c}d   }|j                  |d      }|j                  d       t        t        |j                              j                  du sJ |j                  |j                  k7  sJ t        j                  | j                        }|j                         }|D cg c]  }d|j                  v s| }}t        |      dk(  sJ |d	   }	t        t        |	j                              j                  du sJ t        t        |j                              t        t        |	j                              k(  sJ t        |j                        t        |	j                        k(  sJ t        t        |j                              j                  t        t        |	j                                    sJ y c c}w c c}w )
Nr   r   Tremove_singleton_listen_paramsrn   extFrQ   rS   )r   r   r   re   rh   duplicate_vhostrK   nextiterr   r   r   rM   r   r   super_eq)
r   r   ri   r?   r   	new_vhostnew_nparser
new_vhostsnew_defaultsnew_vhost_parseds
             r   test_duplicate_vhostz$NginxParserTest.test_duplicate_vhost  s   $$T%5%56##%$=	QWW(<1=a@++GTX+Y	R  D)*22e;;;~~--- (()9)9: ++-
#-Faagg1EFF< A%%%'?D)//0199UBBBD'(D6F6L6L1M,NNNN7;;3'7';';#<<<<D'(11$t<L<R<R7S2TUUU# > Gs   HH1H	H	c                    t        j                  | j                        }|j                         }|D cg c]  }d|j                  v s| c}d   }|j                  |d      }|j                  d       |j                  D ]  }|j                  sJ  |j                  |d      }|j                  d       d}|j                  D ]  }|j                  s|j                  sJ d}! |sJ y c c}w )Nipv6sslr   Tr   rn   r   F)
r   r   r   re   rh   r  rK   r   ipv6onlyipv6)	r   r   ri   r?   r  r  addridentical_vhostcalleds	            r   $test_duplicate_vhost_remove_ipv6onlyz4NginxParserTest.test_duplicate_vhost_remove_ipv6only  s    $$T%5%56##%$=	QWW(<1=a@++GTX+Y	R OO 	%D}}$$	% "11'Z_1`R #)) 	Dyy}}$}	 v >s   C0C0c                     t        j                  | j                        }|j                  d      }|j	                  |      }dg|d   d   d   k(  sJ ddg|d   d   d   d   k(  sJ y )	Nvalid_unicode_comments.confr4   r   rQ   r5   r{   rS   rR   )r   r   r   r;   rL   r   r   r   r=   s       r   test_valid_unicode_charactersz-NginxParserTest.test_valid_unicode_characters  sy    $$T%5%56 =>%%d+zVAYq\!_,,,$6!9Q<?1#5555r   c                     t        j                  | j                        }|j                  |j	                  d             |j                  d       y)zLThis tests the parser's ability to load and save a config containing Unicoder  FrG   N)r   r   r   rL   r;   rK   r'   s     r   test_valid_unicode_roundtripz,NginxParserTest.test_valid_unicode_roundtrip  sH    $$T%5%56:;	
 	e$r   c                    | j                         5 }t        j                  | j                        }|j	                  d      }|j                  |      }d d d        g k(  sJ t        d j                  D              sJ y # 1 sw Y   /xY w)Ninvalid_unicode_comments.confc              3   0   K   | ]  }d |v xr d|v   ywzinvalid characterzUTF-8N .0outputs     r   	<genexpr>zBNginxParserTest.test_invalid_unicode_characters.<locals>.<genexpr>  *      
 !F*CF1BC
   )
assertLogsr   r   r   r;   rL   anyr!  r   logr   r   r=   s        r   test_invalid_unicode_charactersz/NginxParserTest.test_invalid_unicode_characters  s    __ 	0#(()9)9:G##$CDD))$/F	0
 V|| 
**
 
 	
 
	0 	0s   ABB
c                     t        j                  | j                        }|j                  d      }t        j                  |      }dg|d   d   k(  sJ ddg|d   d   d   k(  sJ y )	Nr  r4   rQ   r   r5   r{   rS   rR   )r   r   r   r;   _parse_ssl_optionsr  s       r   ,test_valid_unicode_characters_in_ssl_optionsz<NginxParserTest.test_valid_unicode_characters_in_ssl_options  sp    $$T%5%56 =>**40zVAYq\)))$6!9Q<?222r   c                 "   | j                         5 }t        j                  | j                        }|j	                  d      }t        j
                  |      }d d d        g k(  sJ t        d j                  D              sJ y # 1 sw Y   /xY w)Nr  c              3   0   K   | ]  }d |v xr d|v   ywr  r  r  s     r   r"  zQNginxParserTest.test_invalid_unicode_characters_in_ssl_options.<locals>.<genexpr>  r#  r$  )r%  r   r   r   r;   r+  r&  r!  r'  s        r   .test_invalid_unicode_characters_in_ssl_optionsz>NginxParserTest.test_invalid_unicode_characters_in_ssl_options
  s    __ 	5#(()9)9:G##$CDD..t4F	5
 V|| 
**
 
 	
 
	5 	5s   ABBN) __name__
__module____qualname____doc__r   r   r$   r(   r@   rD   rO   rb   rl   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r)  r,  r/  r  r   r   r   r      s    %
0
0;8	:##.
&2"h@(!0+GZ!02#JJ#$6*)V.*6%

3

r   r   __main__rS   )r3  rN   r   r   systypingr   r   certbotr   certbot.compatr   certbot_nginx._internalr   r   r   certbot_nginx._internal.testsr	   util	NginxTestr   r0  exitmainargv__file__r  r   r   <module>rA     sx    /  	  
     / ' * ;B
dnn B
J zCHH[V[[!"
234 r   