
    M/e                        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 ddlZddlm	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  G d dej4                        Zd Z G d dej4                        Zedk(  r4 ej>                   ej@                  ejB                  dd e"gz                yy)z/Tests for certbot._internal.plugins.standalone.    N)Dict)Set)Tuple)mock)
challenges)
standalone)achallenges)errors)	acme_util)utilc                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	ServerManagerTestz=Tests for certbot._internal.plugins.standalone.ServerManager.c                 p    ddl m} i | _        i | _         || j                  | j                        | _        y )Nr   )ServerManager)$certbot._internal.plugins.standaloner   certshttp_01_resourcesmgr)selfr   s     Q/usr/lib/python3/dist-packages/certbot/_internal/tests/plugins/standalone_test.pysetUpzServerManagerTest.setUp   s,    FSU
[] T-C-CD    c                     | j                   j                  | j                  u sJ | j                   j                  | j                  u sJ y N)r   r   r   r   s    r   	test_initzServerManagerTest.test_init    s:    xx~~+++xx))T-C-CCCCr   c                 "   | j                   j                  d|      }|j                         d   d   }| j                   j                         ||ik(  sJ | j                   j	                  |       | j                   j                         i k(  sJ y )Nr   portchallenge_type   )r   )r   rungetsocknamesrunningstop)r   r    serverr   s       r   _test_run_stopz ServerManagerTest._test_run_stop$   s|    1^D""$Q'*xx!dF^3334 xx!R'''r   c                 B    | j                  t        j                         y r   )r'   r   HTTP01r   s    r   test_run_stop_http_01z'ServerManagerTest.test_run_stop_http_01+   s    J--.r   c                    | j                   j                  dt        j                        }|j	                         d   d   }| j                   j                  |t        j                        }| j                   j                         ||ik(  sJ ||u sJ | j                   j                  |       | j                   j                         i k(  sJ y )Nr   r   r!   )r   r"   r   r)   r#   r$   r%   )r   r&   r   server2s       r   test_run_idempotentz%ServerManagerTest.test_run_idempotent.   s    1Z5F5FG""$Q'*((,,D9J9J,Kxx!dF^333   dxx!R'''r   c                 X   t        j                   t         j                        }|j                  d       |j                         d   }t        j                          }	 |j                  d|f       t        j                  t        j                        5  | j                  j                  |t        j                         d d d        | j                  j                         i k(  sJ |j                          |j                          y # t         j                  $ r Y w xY w# 1 sw Y   bxY w)N r   r!   r0   )r    )socketAF_INET6bindgetsocknameerrorpytestraisesr
   StandaloneBindErrorr   r"   r   r)   r$   close)r   some_serverr   maybe_another_servers       r   test_run_bind_errorz%ServerManagerTest.test_run_bind_error7   s    mmFOO4!&&(+%}}	 %%r4j1 ]]6556 	.HHLL%,,  .	. xx!R'''""$ || 			. 	.s   D ,D DD D)N)
__name__
__module____qualname____doc__r   r   r'   r*   r-   r<    r   r   r   r      s%    GED(/(%r   r   c                      t        j                   t         j                  t         j                  d      } | j                  d       | j	                         d   }| j                          |S )z%Gets an open port number from the OS.r   r/   r!   )r1   AF_INETSOCK_STREAMr3   r4   r9   )open_socketr   s     r   get_open_portrF   H   sP    --0B0BAFKW""$Q'DKr   c                       e Zd ZdZd Zd Zd Zd Z ej                         d        Z
 ej                         d        Zd Zd	 Zd
 Zd Zed        Zd Zd Zy)AuthenticatorTestz=Tests for certbot._internal.plugins.standalone.Authenticator.c                     ddl m} t        j                  t	                     | _         || j
                  d      | _        t        j                         | j                  _        y )Nr   )Authenticator)http01_portr   )name)r   rJ   r   	MagicMockrF   configauthservers)r   rJ   s     r   r   zAuthenticatorTest.setUpT   s<    FnnA!$++LA	 NN,		r   c                 X    t        | j                  j                         t              sJ y r   )
isinstancerO   	more_infostrr   s    r   test_more_infoz AuthenticatorTest.test_more_info[   s    $))--/555r   c                 d    | j                   j                  d       t        j                  gk(  sJ y )N)domain)rO   get_chall_prefr   r)   r   s    r   test_get_chall_prefz%AuthenticatorTest.test_get_chall_pref^   s2    yy''t'4$++,- 	- -r   c                     | j                         }| j                  j                  |      }|D cg c]  }|j                  |j                         }}||k(  sJ y c c}w r   )_get_achallsrO   performresponseaccount_key)r   achallsr]   achallexpecteds        r   test_performzAuthenticatorTest.test_performb   sZ    ##%99$$W-FMNFFOOF$6$67NN8### Os   "Ac                 l    |       }t         j                  }t        j                  t	        j
                  |      d      }|gdt	        j
                         gz  z   | j                  j                  j                  _	        |j                  }d|_        | j                          | j                  |       y )Nerrno   T)re   
EADDRINUSEr
   r8   r   rM   rO   rP   r"   side_effectyesnoreturn_valuerb   _assert_correct_yesno_call)r   mock_get_utilitymock_utilityencountered_errnor5   
mock_yesnos         r   test_perform_eaddrinuse_retryz/AuthenticatorTest.test_perform_eaddrinuse_retryi   s    ')!,,**4>>@Q+RTVW-2Ga4>>;K:L6L,L		)!''
"&
''
3r   c                     |       }|j                   }d|_        t        j                  }t	        j
                  t        j                        5  | j                  |       d d d        | j                  |       y # 1 sw Y   xY w)NF)
rj   rk   re   rh   r6   r7   r
   PluginError_fail_performrl   )r   rm   rn   rp   ro   s        r    test_perform_eaddrinuse_no_retryz2AuthenticatorTest.test_perform_eaddrinuse_no_retryu   sm    ')!''
"'
!,,]]6--. 	201	2''
3	2 	2s   A::Bc                 \    |j                   \  }}d|d   v sJ |j                  dd      rJ y )Nzin user   defaultT)	call_argsget)r   rp   
yesno_argsyesno_kwargss       r   rl   z,AuthenticatorTest._assert_correct_yesno_call   s=    #-#7#7 
L:a=(((##It4444r   c                     t         j                  }t        j                  t        j
                        5  | j                  |       d d d        y # 1 sw Y   y xY wr   )re   EACCESr6   r7   r
   rs   rt   r   ro   s     r   test_perform_eaccesz%AuthenticatorTest.test_perform_eacces   sB    !LL]]6--. 	201	2 	2 	2   AAc                     t         j                  }t        j                  t        j
                        5  | j                  |       d d d        y # 1 sw Y   y xY wr   )re   ENOTCONNr6   r7   r
   r8   rt   r~   s     r   $test_perform_unexpected_socket_errorz6AuthenticatorTest.test_perform_unexpected_socket_error   sB    !NN]]6556 	201	2 	2 	2r   c                     t        j                  t        j                  |      d      }|| j                  j
                  j                  _        | j                  j                  | j                                y )Nrd   rf   )
r
   r8   r   rM   rO   rP   r"   ri   r\   r[   )r   ro   r5   s      r   rt   zAuthenticatorTest._fail_perform   sR    **4>>@Q+RTVW,1		)		$++-.r   c                     d}t         j                  j                  t        j                  d            }t        j                  t        j                  ||      }|gS )Ns	   localhostzrsa512_key.pem)challbrW   r^   )	joseJWKload	test_utilload_vectorr	   "KeyAuthorizationAnnotatedChallenger   HTTP01_P)clsrW   keyhttp_01s       r   r[   zAuthenticatorTest._get_achalls   sM    hhmmI112BCD@@%%f#G yr   c                    ddd| j                   j                  j                  _        | j                   j                  d   j                  d       | j                   j                  d   j                  ddg       | j                   j                  dg       | j                   j                  t               ddhdk(  sJ | j                   j                  j                  j                  d       d	di| j                   j                  j                  _        | j                   j                  dg       | j                   j                  t               dhdk(  sJ d| j                   j                  j                  j                  k(  sJ | j                   j                  dg       | j                   j                  t               t               dk(  sJ | j                   j                  j                  j                  d	       y )
Nserver1r,   )r!   rg   chall1chall2chall3)r   r,   r!   rg   )rO   rP   r$   rk   servedaddupdatecleanupsetr%   assert_called_once_with
call_countassert_called_withr   s    r   test_cleanupzAuthenticatorTest.test_cleanup   s   2
		!!. 			#''1		#**Hh+?@		8*%yyu(H)=$? ? 	? ?		66q9 y2
		!!. 			8*%yyu($5 5 	5 5DII%%**55555		8*%yyu$0 0 	0 0		11!4r   c                     d| j                   _        d | j                   _        d| j                  j	                  g       v sJ d| j                   _        d| j                  j	                  g       v sJ y )N80z
on port 80z	127.0.0.1zon 127.0.0.1:80)rN   rK   http01_addressrO   	auth_hintr   s    r   test_auth_hintz AuthenticatorTest.test_auth_hint   s`    "&%)"tyy2226666%0" DII$7$7$;;;;r   N)r=   r>   r?   r@   r   rU   rY   rb   r   patch_display_utilrq   ru   rl   r   r   rt   classmethodr[   r   r   rA   r   r   rH   rH   Q   s    G-6-$ "Y!!#	4 $	4 "Y!!#4 $45
2
2
/
  54<r   rH   __main__r!   )#r@   re   r1   systypingr   r   r   unittestr   josepyr   OpenSSL.cryptoOpenSSLr6   acmer   r   acme_standalonecertbotr	   r
   certbot.testsr   r   r   TestCaser   rF   rH   r=   exitmainargv__file__rA   r   r   <module>r      s    5   
          .   # +.%)) .%bk<)) k<\ zCHH[V[[!"
234 r   